usb.h revision 193074
1/* $FreeBSD: head/sys/dev/usb/usb_compat_linux.h 193074 2009-05-30 00:22:57Z thompsa $ */
2/*-
3 * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved.
4 * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#ifndef _USB_COMPAT_LINUX_H
29#define	_USB_COMPAT_LINUX_H
30
31struct usb_device;
32struct usb_interface;
33struct usb_driver;
34struct urb;
35
36typedef void *pm_message_t;
37typedef void (usb_complete_t)(struct urb *);
38
39#define	USB_MAX_FULL_SPEED_ISOC_FRAMES (60 * 1)
40#define	USB_MAX_HIGH_SPEED_ISOC_FRAMES (60 * 8)
41
42/*
43 * Linux compatible USB device drivers put their device information
44 * into the "usb_device_id" structure using the "USB_DEVICE()" macro.
45 * The "MODULE_DEVICE_TABLE()" macro can be used to export this
46 * information to userland.
47 */
48struct usb_device_id {
49	/* which fields to match against */
50	uint16_t match_flags;
51#define	USB_DEVICE_ID_MATCH_VENDOR		0x0001
52#define	USB_DEVICE_ID_MATCH_PRODUCT		0x0002
53#define	USB_DEVICE_ID_MATCH_DEV_LO		0x0004
54#define	USB_DEVICE_ID_MATCH_DEV_HI		0x0008
55#define	USB_DEVICE_ID_MATCH_DEV_CLASS		0x0010
56#define	USB_DEVICE_ID_MATCH_DEV_SUBCLASS	0x0020
57#define	USB_DEVICE_ID_MATCH_DEV_PROTOCOL	0x0040
58#define	USB_DEVICE_ID_MATCH_INT_CLASS		0x0080
59#define	USB_DEVICE_ID_MATCH_INT_SUBCLASS	0x0100
60#define	USB_DEVICE_ID_MATCH_INT_PROTOCOL	0x0200
61
62	/* Used for product specific matches; the BCD range is inclusive */
63	uint16_t idVendor;
64	uint16_t idProduct;
65	uint16_t bcdDevice_lo;
66	uint16_t bcdDevice_hi;
67
68	/* Used for device class matches */
69	uint8_t	bDeviceClass;
70	uint8_t	bDeviceSubClass;
71	uint8_t	bDeviceProtocol;
72
73	/* Used for interface class matches */
74	uint8_t	bInterfaceClass;
75	uint8_t	bInterfaceSubClass;
76	uint8_t	bInterfaceProtocol;
77
78	/* Hook for driver specific information */
79	unsigned long driver_info;
80};
81
82#define	USB_DEVICE_ID_MATCH_DEVICE \
83	(USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
84
85#define	USB_DEVICE(vend,prod) \
86	.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = (vend), \
87	.idProduct = (prod)
88
89/* The "usb_driver" structure holds the Linux USB device driver
90 * callbacks, and a pointer to device ID's which this entry should
91 * match against. Usually this entry is exposed to the USB emulation
92 * layer using the "USB_DRIVER_EXPORT()" macro, which is defined
93 * below.
94 */
95struct usb_driver {
96	const char *name;
97
98	int     (*probe) (struct usb_interface *intf,
99	    	const	struct usb_device_id *id);
100
101	void    (*disconnect) (struct usb_interface *intf);
102
103	int     (*ioctl) (struct usb_interface *intf, unsigned int code,
104	    	void  *buf);
105
106	int     (*suspend) (struct usb_interface *intf, pm_message_t message);
107	int     (*resume) (struct usb_interface *intf);
108
109	const struct usb_device_id *id_table;
110
111	void    (*shutdown) (struct usb_interface *intf);
112
113	LIST_ENTRY(usb_driver) linux_driver_list;
114};
115
116#define	USB_DRIVER_EXPORT(id,p_usb_drv) \
117  SYSINIT(id,SI_SUB_KLD,SI_ORDER_FIRST,usb_linux_register,p_usb_drv); \
118  SYSUNINIT(id,SI_SUB_KLD,SI_ORDER_ANY,usb_linux_deregister,p_usb_drv)
119
120#define	USB_DT_ENDPOINT_SIZE		7
121#define	USB_DT_ENDPOINT_AUDIO_SIZE	9
122
123/*
124 * Endpoints
125 */
126#define	USB_ENDPOINT_NUMBER_MASK	0x0f	/* in bEndpointAddress */
127#define	USB_ENDPOINT_DIR_MASK		0x80
128
129#define	USB_ENDPOINT_XFERTYPE_MASK	0x03	/* in bmAttributes */
130#define	USB_ENDPOINT_XFER_CONTROL	0
131#define	USB_ENDPOINT_XFER_ISOC		1
132#define	USB_ENDPOINT_XFER_BULK		2
133#define	USB_ENDPOINT_XFER_INT		3
134#define	USB_ENDPOINT_MAX_ADJUSTABLE	0x80
135
136/* CONTROL REQUEST SUPPORT */
137
138/*
139 * Definition of direction mask for
140 * "bEndpointAddress" and "bmRequestType":
141 */
142#define	USB_DIR_MASK			0x80
143#define	USB_DIR_OUT			0x00	/* write to USB device */
144#define	USB_DIR_IN			0x80	/* read from USB device */
145
146/*
147 * Definition of type mask for
148 * "bmRequestType":
149 */
150#define	USB_TYPE_MASK			(0x03 << 5)
151#define	USB_TYPE_STANDARD		(0x00 << 5)
152#define	USB_TYPE_CLASS			(0x01 << 5)
153#define	USB_TYPE_VENDOR			(0x02 << 5)
154#define	USB_TYPE_RESERVED		(0x03 << 5)
155
156/*
157 * Definition of receiver mask for
158 * "bmRequestType":
159 */
160#define	USB_RECIP_MASK			0x1f
161#define	USB_RECIP_DEVICE		0x00
162#define	USB_RECIP_INTERFACE		0x01
163#define	USB_RECIP_ENDPOINT		0x02
164#define	USB_RECIP_OTHER			0x03
165
166/*
167 * Definition of standard request values for
168 * "bRequest":
169 */
170#define	USB_REQ_GET_STATUS		0x00
171#define	USB_REQ_CLEAR_FEATURE		0x01
172#define	USB_REQ_SET_FEATURE		0x03
173#define	USB_REQ_SET_ADDRESS		0x05
174#define	USB_REQ_GET_DESCRIPTOR		0x06
175#define	USB_REQ_SET_DESCRIPTOR		0x07
176#define	USB_REQ_GET_CONFIGURATION	0x08
177#define	USB_REQ_SET_CONFIGURATION	0x09
178#define	USB_REQ_GET_INTERFACE		0x0A
179#define	USB_REQ_SET_INTERFACE		0x0B
180#define	USB_REQ_SYNCH_FRAME		0x0C
181
182#define	USB_REQ_SET_ENCRYPTION		0x0D	/* Wireless USB */
183#define	USB_REQ_GET_ENCRYPTION		0x0E
184#define	USB_REQ_SET_HANDSHAKE		0x0F
185#define	USB_REQ_GET_HANDSHAKE		0x10
186#define	USB_REQ_SET_CONNECTION		0x11
187#define	USB_REQ_SET_SECURITY_DATA	0x12
188#define	USB_REQ_GET_SECURITY_DATA	0x13
189#define	USB_REQ_SET_WUSB_DATA		0x14
190#define	USB_REQ_LOOPBACK_DATA_WRITE	0x15
191#define	USB_REQ_LOOPBACK_DATA_READ	0x16
192#define	USB_REQ_SET_INTERFACE_DS	0x17
193
194/*
195 * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
196 * are read as a bit array returned by USB_REQ_GET_STATUS.  (So there
197 * are at most sixteen features of each type.)
198 */
199#define	USB_DEVICE_SELF_POWERED		0	/* (read only) */
200#define	USB_DEVICE_REMOTE_WAKEUP	1	/* dev may initiate wakeup */
201#define	USB_DEVICE_TEST_MODE		2	/* (wired high speed only) */
202#define	USB_DEVICE_BATTERY		2	/* (wireless) */
203#define	USB_DEVICE_B_HNP_ENABLE		3	/* (otg) dev may initiate HNP */
204#define	USB_DEVICE_WUSB_DEVICE		3	/* (wireless) */
205#define	USB_DEVICE_A_HNP_SUPPORT	4	/* (otg) RH port supports HNP */
206#define	USB_DEVICE_A_ALT_HNP_SUPPORT	5	/* (otg) other RH port does */
207#define	USB_DEVICE_DEBUG_MODE		6	/* (special devices only) */
208
209#define	USB_ENDPOINT_HALT		0	/* IN/OUT will STALL */
210
211#define	PIPE_ISOCHRONOUS		0x01	/* UE_ISOCHRONOUS */
212#define	PIPE_INTERRUPT			0x03	/* UE_INTERRUPT */
213#define	PIPE_CONTROL			0x00	/* UE_CONTROL */
214#define	PIPE_BULK			0x02	/* UE_BULK */
215
216/* Whenever Linux references an USB endpoint:
217 * a) to initialize "urb->pipe"
218 * b) second argument passed to "usb_control_msg()"
219 *
220 * Then it uses one of the following macros. The "endpoint" argument
221 * is the physical endpoint value masked by 0xF. The "dev" argument
222 * is a pointer to "struct usb_device".
223 */
224#define	usb_sndctrlpipe(dev,endpoint) \
225  usb_find_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_OUT)
226
227#define	usb_rcvctrlpipe(dev,endpoint) \
228  usb_find_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_IN)
229
230#define	usb_sndisocpipe(dev,endpoint) \
231  usb_find_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_OUT)
232
233#define	usb_rcvisocpipe(dev,endpoint) \
234  usb_find_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_IN)
235
236#define	usb_sndbulkpipe(dev,endpoint) \
237  usb_find_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_OUT)
238
239#define	usb_rcvbulkpipe(dev,endpoint) \
240  usb_find_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_IN)
241
242#define	usb_sndintpipe(dev,endpoint) \
243  usb_find_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_OUT)
244
245#define	usb_rcvintpipe(dev,endpoint) \
246  usb_find_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_IN)
247
248/*
249 * The following structure is used to extend "struct urb" when we are
250 * dealing with an isochronous endpoint. It contains information about
251 * the data offset and data length of an isochronous packet.
252 * The "actual_length" field is updated before the "complete"
253 * callback in the "urb" structure is called.
254 */
255struct usb_iso_packet_descriptor {
256	uint32_t offset;		/* depreciated buffer offset (the
257					 * packets are usually back to back) */
258	uint16_t length;		/* expected length */
259	uint16_t actual_length;
260	uint16_t status;
261};
262
263/*
264 * The following structure holds various information about an USB
265 * transfer. This structure is used for all kinds of USB transfers.
266 *
267 * URB is short for USB Request Block.
268 */
269struct urb {
270	TAILQ_ENTRY(urb) bsd_urb_list;
271	struct cv cv_wait;
272
273	struct usb_device *dev;		/* (in) pointer to associated device */
274	struct usb_host_endpoint *pipe;	/* (in) pipe pointer */
275	uint8_t *setup_packet;		/* (in) setup packet (control only) */
276	uint8_t *bsd_data_ptr;
277	void   *transfer_buffer;	/* (in) associated data buffer */
278	void   *context;		/* (in) context for completion */
279	usb_complete_t *complete;	/* (in) completion routine */
280
281	usb_size_t transfer_buffer_length;/* (in) data buffer length */
282	usb_size_t bsd_length_rem;
283	usb_size_t actual_length;	/* (return) actual transfer length */
284	usb_timeout_t timeout;		/* FreeBSD specific */
285
286	uint16_t transfer_flags;	/* (in) */
287#define	URB_SHORT_NOT_OK	0x0001	/* report short transfers like errors */
288#define	URB_ISO_ASAP		0x0002	/* ignore "start_frame" field */
289#define	URB_ZERO_PACKET		0x0004	/* the USB transfer ends with a short
290					 * packet */
291#define	URB_NO_TRANSFER_DMA_MAP 0x0008	/* "transfer_dma" is valid on submit */
292#define	URB_WAIT_WAKEUP		0x0010	/* custom flags */
293#define	URB_IS_SLEEPING		0x0020	/* custom flags */
294
295	usb_frcount_t start_frame;	/* (modify) start frame (ISO) */
296	usb_frcount_t number_of_packets;	/* (in) number of ISO packets */
297	uint16_t interval;		/* (modify) transfer interval
298					 * (INT/ISO) */
299	uint16_t error_count;		/* (return) number of ISO errors */
300	int16_t	status;			/* (return) status */
301
302	uint8_t	setup_dma;		/* (in) not used on FreeBSD */
303	uint8_t	transfer_dma;		/* (in) not used on FreeBSD */
304	uint8_t	bsd_isread;
305
306	struct usb_iso_packet_descriptor iso_frame_desc[];	/* (in) ISO ONLY */
307};
308
309/* various prototypes */
310
311int	usb_submit_urb(struct urb *urb, uint16_t mem_flags);
312int	usb_unlink_urb(struct urb *urb);
313int	usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe);
314int	usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *pipe,
315	    uint8_t request, uint8_t requesttype, uint16_t value,
316	    uint16_t index, void *data, uint16_t size, usb_timeout_t timeout);
317int	usb_set_interface(struct usb_device *dev, uint8_t ifnum,
318	    uint8_t alternate);
319int	usb_setup_endpoint(struct usb_device *dev,
320	    struct usb_host_endpoint *uhe, usb_frlength_t bufsize);
321
322struct usb_host_endpoint *usb_find_host_endpoint(struct usb_device *dev,
323	    uint8_t type, uint8_t ep);
324struct urb *usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags);
325struct usb_host_interface *usb_altnum_to_altsetting(
326	    const struct usb_interface *intf, uint8_t alt_index);
327struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no);
328
329void   *usb_buffer_alloc(struct usb_device *dev, usb_size_t size,
330	    uint16_t mem_flags, uint8_t *dma_addr);
331void   *usb_get_intfdata(struct usb_interface *intf);
332
333void	usb_buffer_free(struct usb_device *dev, usb_size_t size, void *addr, uint8_t dma_addr);
334void	usb_free_urb(struct urb *urb);
335void	usb_init_urb(struct urb *urb);
336void	usb_kill_urb(struct urb *urb);
337void	usb_set_intfdata(struct usb_interface *intf, void *data);
338void	usb_linux_register(void *arg);
339void	usb_linux_deregister(void *arg);
340
341#define	interface_to_usbdev(intf) (intf)->linux_udev
342#define	interface_to_bsddev(intf) (intf)->linux_udev->bsd_udev
343
344#endif					/* _USB_COMPAT_LINUX_H */
345