usb_compat_linux.h revision 296373
189400Smike/* $FreeBSD: releng/10.3/sys/dev/usb/usb_compat_linux.h 198776 2009-11-01 21:48:18Z thompsa $ */
241762Swes/*-
341762Swes * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved.
441762Swes * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
541762Swes *
641762Swes * Redistribution and use in source and binary forms, with or without
71573Srgrimes * modification, are permitted provided that the following conditions
81573Srgrimes * are met:
91573Srgrimes * 1. Redistributions of source code must retain the above copyright
101573Srgrimes *    notice, this list of conditions and the following disclaimer.
111573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer in the
131573Srgrimes *    documentation and/or other materials provided with the distribution.
1441762Swes *
151573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1641762Swes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18251069Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2241762Swes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2341762Swes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2441762Swes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2541762Swes * SUCH DAMAGE.
2641762Swes */
2741762Swes
2841762Swes#ifndef _USB_COMPAT_LINUX_H
2941762Swes#define	_USB_COMPAT_LINUX_H
3041762Swes
3141762Swesstruct usb_device;
3241762Swesstruct usb_interface;
331573Srgrimesstruct usb_driver;
341573Srgrimesstruct urb;
3589400Smike
3689400Smiketypedef void *pm_message_t;
3789400Smiketypedef void (usb_complete_t)(struct urb *);
3892986Sobrien
3992986Sobrien#define	USB_MAX_FULL_SPEED_ISOC_FRAMES (60 * 1)
4089400Smike#define	USB_MAX_HIGH_SPEED_ISOC_FRAMES (60 * 8)
411573Srgrimes
4289387Smike#define	USB_DEVICE_ID_MATCH_DEVICE \
4389387Smike	(USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
4489387Smike
451573Srgrimes#define	USB_DEVICE(vend,prod) \
461573Srgrimes	.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = (vend), \
47103057Stjr	.idProduct = (prod)
48103057Stjr
49103057Stjr/* The "usb_driver" structure holds the Linux USB device driver
50103057Stjr * callbacks, and a pointer to device ID's which this entry should
511573Srgrimes * match against. Usually this entry is exposed to the USB emulation
52103057Stjr * layer using the "USB_DRIVER_EXPORT()" macro, which is defined
531573Srgrimes * below.
5489400Smike */
5589385Smikestruct usb_driver {
561573Srgrimes	const char *name;
5789385Smike
5889385Smike	int     (*probe) (struct usb_interface *intf,
591573Srgrimes	    	const	struct usb_device_id *id);
6089385Smike
6189385Smike	void    (*disconnect) (struct usb_interface *intf);
6289385Smike
6341762Swes	int     (*ioctl) (struct usb_interface *intf, unsigned int code,
6489385Smike	    	void  *buf);
6589400Smike
6689385Smike	int     (*suspend) (struct usb_interface *intf, pm_message_t message);
6789385Smike	int     (*resume) (struct usb_interface *intf);
6889400Smike
6989385Smike	const struct usb_device_id *id_table;
7089385Smike
7189385Smike	void    (*shutdown) (struct usb_interface *intf);
7289385Smike
7341762Swes	LIST_ENTRY(usb_driver) linux_driver_list;
7489385Smike};
751573Srgrimes
7689385Smike#define	USB_DRIVER_EXPORT(id,p_usb_drv) \
7789385Smike  SYSINIT(id,SI_SUB_KLD,SI_ORDER_FIRST,usb_linux_register,p_usb_drv); \
7889385Smike  SYSUNINIT(id,SI_SUB_KLD,SI_ORDER_ANY,usb_linux_deregister,p_usb_drv)
7989385Smike
8089385Smike#define	USB_DT_ENDPOINT_SIZE		7
8189385Smike#define	USB_DT_ENDPOINT_AUDIO_SIZE	9
8289385Smike
8389385Smike/*
8489385Smike * Endpoints
8589385Smike */
8689385Smike#define	USB_ENDPOINT_NUMBER_MASK	0x0f	/* in bEndpointAddress */
8789400Smike#define	USB_ENDPOINT_DIR_MASK		0x80
8889400Smike
8989385Smike#define	USB_ENDPOINT_XFERTYPE_MASK	0x03	/* in bmAttributes */
9089385Smike#define	USB_ENDPOINT_XFER_CONTROL	0
9189385Smike#define	USB_ENDPOINT_XFER_ISOC		1
9289385Smike#define	USB_ENDPOINT_XFER_BULK		2
931573Srgrimes#define	USB_ENDPOINT_XFER_INT		3
9489385Smike#define	USB_ENDPOINT_MAX_ADJUSTABLE	0x80
9541762Swes
961573Srgrimes/* CONTROL REQUEST SUPPORT */
9741762Swes
9841762Swes/*
9941762Swes * Definition of direction mask for
10089385Smike * "bEndpointAddress" and "bmRequestType":
10141762Swes */
102103057Stjr#define	USB_DIR_MASK			0x80
10341762Swes#define	USB_DIR_OUT			0x00	/* write to USB device */
10441762Swes#define	USB_DIR_IN			0x80	/* read from USB device */
10589385Smike
10641762Swes/*
10741762Swes * Definition of type mask for
10841762Swes * "bmRequestType":
10941762Swes */
11089385Smike#define	USB_TYPE_MASK			(0x03 << 5)
11141762Swes#define	USB_TYPE_STANDARD		(0x00 << 5)
11289387Smike#define	USB_TYPE_CLASS			(0x01 << 5)
11389387Smike#define	USB_TYPE_VENDOR			(0x02 << 5)
11441762Swes#define	USB_TYPE_RESERVED		(0x03 << 5)
11589387Smike
11689387Smike/*
11789387Smike * Definition of receiver mask for
11889385Smike * "bmRequestType":
11989385Smike */
12089385Smike#define	USB_RECIP_MASK			0x1f
12189385Smike#define	USB_RECIP_DEVICE		0x00
12289385Smike#define	USB_RECIP_INTERFACE		0x01
12341762Swes#define	USB_RECIP_ENDPOINT		0x02
12489385Smike#define	USB_RECIP_OTHER			0x03
12589387Smike
12689385Smike/*
12741762Swes * Definition of standard request values for
12889385Smike * "bRequest":
12989387Smike */
13089385Smike#define	USB_REQ_GET_STATUS		0x00
1311573Srgrimes#define	USB_REQ_CLEAR_FEATURE		0x01
1321573Srgrimes#define	USB_REQ_SET_FEATURE		0x03
13389385Smike#define	USB_REQ_SET_ADDRESS		0x05
1341573Srgrimes#define	USB_REQ_GET_DESCRIPTOR		0x06
13541762Swes#define	USB_REQ_SET_DESCRIPTOR		0x07
13641762Swes#define	USB_REQ_GET_CONFIGURATION	0x08
137#define	USB_REQ_SET_CONFIGURATION	0x09
138#define	USB_REQ_GET_INTERFACE		0x0A
139#define	USB_REQ_SET_INTERFACE		0x0B
140#define	USB_REQ_SYNCH_FRAME		0x0C
141
142#define	USB_REQ_SET_ENCRYPTION		0x0D	/* Wireless USB */
143#define	USB_REQ_GET_ENCRYPTION		0x0E
144#define	USB_REQ_SET_HANDSHAKE		0x0F
145#define	USB_REQ_GET_HANDSHAKE		0x10
146#define	USB_REQ_SET_CONNECTION		0x11
147#define	USB_REQ_SET_SECURITY_DATA	0x12
148#define	USB_REQ_GET_SECURITY_DATA	0x13
149#define	USB_REQ_SET_WUSB_DATA		0x14
150#define	USB_REQ_LOOPBACK_DATA_WRITE	0x15
151#define	USB_REQ_LOOPBACK_DATA_READ	0x16
152#define	USB_REQ_SET_INTERFACE_DS	0x17
153
154/*
155 * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
156 * are read as a bit array returned by USB_REQ_GET_STATUS.  (So there
157 * are at most sixteen features of each type.)
158 */
159#define	USB_DEVICE_SELF_POWERED		0	/* (read only) */
160#define	USB_DEVICE_REMOTE_WAKEUP	1	/* dev may initiate wakeup */
161#define	USB_DEVICE_TEST_MODE		2	/* (wired high speed only) */
162#define	USB_DEVICE_BATTERY		2	/* (wireless) */
163#define	USB_DEVICE_B_HNP_ENABLE		3	/* (otg) dev may initiate HNP */
164#define	USB_DEVICE_WUSB_DEVICE		3	/* (wireless) */
165#define	USB_DEVICE_A_HNP_SUPPORT	4	/* (otg) RH port supports HNP */
166#define	USB_DEVICE_A_ALT_HNP_SUPPORT	5	/* (otg) other RH port does */
167#define	USB_DEVICE_DEBUG_MODE		6	/* (special devices only) */
168
169#define	USB_ENDPOINT_HALT		0	/* IN/OUT will STALL */
170
171#define	PIPE_ISOCHRONOUS		0x01	/* UE_ISOCHRONOUS */
172#define	PIPE_INTERRUPT			0x03	/* UE_INTERRUPT */
173#define	PIPE_CONTROL			0x00	/* UE_CONTROL */
174#define	PIPE_BULK			0x02	/* UE_BULK */
175
176/* Whenever Linux references an USB endpoint:
177 * a) to initialize "urb->endpoint"
178 * b) second argument passed to "usb_control_msg()"
179 *
180 * Then it uses one of the following macros. The "endpoint" argument
181 * is the physical endpoint value masked by 0xF. The "dev" argument
182 * is a pointer to "struct usb_device".
183 */
184#define	usb_sndctrlpipe(dev,endpoint) \
185  usb_find_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_OUT)
186
187#define	usb_rcvctrlpipe(dev,endpoint) \
188  usb_find_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_IN)
189
190#define	usb_sndisocpipe(dev,endpoint) \
191  usb_find_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_OUT)
192
193#define	usb_rcvisocpipe(dev,endpoint) \
194  usb_find_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_IN)
195
196#define	usb_sndbulkpipe(dev,endpoint) \
197  usb_find_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_OUT)
198
199#define	usb_rcvbulkpipe(dev,endpoint) \
200  usb_find_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_IN)
201
202#define	usb_sndintpipe(dev,endpoint) \
203  usb_find_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_OUT)
204
205#define	usb_rcvintpipe(dev,endpoint) \
206  usb_find_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_IN)
207
208/*
209 * The following structure is used to extend "struct urb" when we are
210 * dealing with an isochronous endpoint. It contains information about
211 * the data offset and data length of an isochronous packet.
212 * The "actual_length" field is updated before the "complete"
213 * callback in the "urb" structure is called.
214 */
215struct usb_iso_packet_descriptor {
216	uint32_t offset;		/* depreciated buffer offset (the
217					 * packets are usually back to back) */
218	uint16_t length;		/* expected length */
219	uint16_t actual_length;
220	 int16_t status;		/* transfer status */
221};
222
223/*
224 * The following structure holds various information about an USB
225 * transfer. This structure is used for all kinds of USB transfers.
226 *
227 * URB is short for USB Request Block.
228 */
229struct urb {
230	TAILQ_ENTRY(urb) bsd_urb_list;
231	struct cv cv_wait;
232
233	struct usb_device *dev;		/* (in) pointer to associated device */
234	struct usb_host_endpoint *endpoint;	/* (in) pipe pointer */
235	uint8_t *setup_packet;		/* (in) setup packet (control only) */
236	uint8_t *bsd_data_ptr;
237	void   *transfer_buffer;	/* (in) associated data buffer */
238	void   *context;		/* (in) context for completion */
239	usb_complete_t *complete;	/* (in) completion routine */
240
241	usb_size_t transfer_buffer_length;/* (in) data buffer length */
242	usb_size_t bsd_length_rem;
243	usb_size_t actual_length;	/* (return) actual transfer length */
244	usb_timeout_t timeout;		/* FreeBSD specific */
245
246	uint16_t transfer_flags;	/* (in) */
247#define	URB_SHORT_NOT_OK	0x0001	/* report short transfers like errors */
248#define	URB_ISO_ASAP		0x0002	/* ignore "start_frame" field */
249#define	URB_ZERO_PACKET		0x0004	/* the USB transfer ends with a short
250					 * packet */
251#define	URB_NO_TRANSFER_DMA_MAP 0x0008	/* "transfer_dma" is valid on submit */
252#define	URB_WAIT_WAKEUP		0x0010	/* custom flags */
253#define	URB_IS_SLEEPING		0x0020	/* custom flags */
254
255	usb_frcount_t start_frame;	/* (modify) start frame (ISO) */
256	usb_frcount_t number_of_packets;	/* (in) number of ISO packets */
257	uint16_t interval;		/* (modify) transfer interval
258					 * (INT/ISO) */
259	uint16_t error_count;		/* (return) number of ISO errors */
260	int16_t	status;			/* (return) status */
261
262	uint8_t	setup_dma;		/* (in) not used on FreeBSD */
263	uint8_t	transfer_dma;		/* (in) not used on FreeBSD */
264	uint8_t	bsd_isread;
265	uint8_t kill_count;		/* FreeBSD specific */
266
267	struct usb_iso_packet_descriptor iso_frame_desc[];	/* (in) ISO ONLY */
268};
269
270/* various prototypes */
271
272int	usb_submit_urb(struct urb *urb, uint16_t mem_flags);
273int	usb_unlink_urb(struct urb *urb);
274int	usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe);
275int	usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *ep,
276	    uint8_t request, uint8_t requesttype, uint16_t value,
277	    uint16_t index, void *data, uint16_t size, usb_timeout_t timeout);
278int	usb_set_interface(struct usb_device *dev, uint8_t ifnum,
279	    uint8_t alternate);
280int	usb_setup_endpoint(struct usb_device *dev,
281	    struct usb_host_endpoint *uhe, usb_frlength_t bufsize);
282
283struct usb_host_endpoint *usb_find_host_endpoint(struct usb_device *dev,
284	    uint8_t type, uint8_t ep);
285struct urb *usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags);
286struct usb_host_interface *usb_altnum_to_altsetting(
287	    const struct usb_interface *intf, uint8_t alt_index);
288struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no);
289
290void   *usb_buffer_alloc(struct usb_device *dev, usb_size_t size,
291	    uint16_t mem_flags, uint8_t *dma_addr);
292void   *usbd_get_intfdata(struct usb_interface *intf);
293
294void	usb_buffer_free(struct usb_device *dev, usb_size_t size, void *addr, uint8_t dma_addr);
295void	usb_free_urb(struct urb *urb);
296void	usb_init_urb(struct urb *urb);
297void	usb_kill_urb(struct urb *urb);
298void	usb_set_intfdata(struct usb_interface *intf, void *data);
299void	usb_linux_register(void *arg);
300void	usb_linux_deregister(void *arg);
301
302void	usb_fill_bulk_urb(struct urb *, struct usb_device *,
303	    struct usb_host_endpoint *, void *, int, usb_complete_t, void *);
304int	usb_bulk_msg(struct usb_device *, struct usb_host_endpoint *,
305	    void *, int, uint16_t *, usb_timeout_t);
306
307#define	interface_to_usbdev(intf) (intf)->linux_udev
308#define	interface_to_bsddev(intf) (intf)->linux_udev
309
310#endif					/* _USB_COMPAT_LINUX_H */
311