1155192Srwatson/* $FreeBSD: releng/10.3/sys/dev/usb/usb_compat_linux.h 198776 2009-11-01 21:48:18Z thompsa $ */
2180701Srwatson/*-
3155192Srwatson * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved.
4155192Srwatson * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
5155192Srwatson *
6155192Srwatson * Redistribution and use in source and binary forms, with or without
7155192Srwatson * modification, are permitted provided that the following conditions
8155192Srwatson * are met:
9155192Srwatson * 1. Redistributions of source code must retain the above copyright
10155192Srwatson *    notice, this list of conditions and the following disclaimer.
11155192Srwatson * 2. Redistributions in binary form must reproduce the above copyright
12155192Srwatson *    notice, this list of conditions and the following disclaimer in the
13155192Srwatson *    documentation and/or other materials provided with the distribution.
14180701Srwatson *
15155192Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18155192Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19155192Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23155192Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24155192Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25155192Srwatson * SUCH DAMAGE.
26155192Srwatson */
27155192Srwatson
28155192Srwatson#ifndef _USB_COMPAT_LINUX_H
29155192Srwatson#define	_USB_COMPAT_LINUX_H
30155192Srwatson
31178186Srwatsonstruct usb_device;
32178186Srwatsonstruct usb_interface;
33178186Srwatsonstruct usb_driver;
34155192Srwatsonstruct urb;
35155192Srwatson
36155192Srwatsontypedef void *pm_message_t;
37155192Srwatsontypedef void (usb_complete_t)(struct urb *);
38155192Srwatson
39155192Srwatson#define	USB_MAX_FULL_SPEED_ISOC_FRAMES (60 * 1)
40155192Srwatson#define	USB_MAX_HIGH_SPEED_ISOC_FRAMES (60 * 8)
41155192Srwatson
42181060Scsjp#define	USB_DEVICE_ID_MATCH_DEVICE \
43155192Srwatson	(USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
44155192Srwatson
45155192Srwatson#define	USB_DEVICE(vend,prod) \
46155192Srwatson	.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = (vend), \
47155192Srwatson	.idProduct = (prod)
48155192Srwatson
49155192Srwatson/* The "usb_driver" structure holds the Linux USB device driver
50155192Srwatson * callbacks, and a pointer to device ID's which this entry should
51155192Srwatson * match against. Usually this entry is exposed to the USB emulation
52155192Srwatson * layer using the "USB_DRIVER_EXPORT()" macro, which is defined
53155192Srwatson * below.
54155192Srwatson */
55155192Srwatsonstruct usb_driver {
56155192Srwatson	const char *name;
57180735Srwatson
58155192Srwatson	int     (*probe) (struct usb_interface *intf,
59155192Srwatson	    	const	struct usb_device_id *id);
60155192Srwatson
61155192Srwatson	void    (*disconnect) (struct usb_interface *intf);
62155192Srwatson
63155192Srwatson	int     (*ioctl) (struct usb_interface *intf, unsigned int code,
64155192Srwatson	    	void  *buf);
65155192Srwatson
66155192Srwatson	int     (*suspend) (struct usb_interface *intf, pm_message_t message);
67155192Srwatson	int     (*resume) (struct usb_interface *intf);
68156889Srwatson
69156889Srwatson	const struct usb_device_id *id_table;
70155192Srwatson
71155192Srwatson	void    (*shutdown) (struct usb_interface *intf);
72155192Srwatson
73155192Srwatson	LIST_ENTRY(usb_driver) linux_driver_list;
74155192Srwatson};
75155192Srwatson
76155192Srwatson#define	USB_DRIVER_EXPORT(id,p_usb_drv) \
77155192Srwatson  SYSINIT(id,SI_SUB_KLD,SI_ORDER_FIRST,usb_linux_register,p_usb_drv); \
78155192Srwatson  SYSUNINIT(id,SI_SUB_KLD,SI_ORDER_ANY,usb_linux_deregister,p_usb_drv)
79155192Srwatson
80155192Srwatson#define	USB_DT_ENDPOINT_SIZE		7
81155192Srwatson#define	USB_DT_ENDPOINT_AUDIO_SIZE	9
82155192Srwatson
83173142Srwatson/*
84155192Srwatson * Endpoints
85155192Srwatson */
86155192Srwatson#define	USB_ENDPOINT_NUMBER_MASK	0x0f	/* in bEndpointAddress */
87155192Srwatson#define	USB_ENDPOINT_DIR_MASK		0x80
88155192Srwatson
89155192Srwatson#define	USB_ENDPOINT_XFERTYPE_MASK	0x03	/* in bmAttributes */
90155192Srwatson#define	USB_ENDPOINT_XFER_CONTROL	0
91155192Srwatson#define	USB_ENDPOINT_XFER_ISOC		1
92155192Srwatson#define	USB_ENDPOINT_XFER_BULK		2
93155192Srwatson#define	USB_ENDPOINT_XFER_INT		3
94155192Srwatson#define	USB_ENDPOINT_MAX_ADJUSTABLE	0x80
95156889Srwatson
96155192Srwatson/* CONTROL REQUEST SUPPORT */
97155192Srwatson
98156889Srwatson/*
99155192Srwatson * Definition of direction mask for
100156889Srwatson * "bEndpointAddress" and "bmRequestType":
101155192Srwatson */
102155192Srwatson#define	USB_DIR_MASK			0x80
103156889Srwatson#define	USB_DIR_OUT			0x00	/* write to USB device */
104155192Srwatson#define	USB_DIR_IN			0x80	/* read from USB device */
105155192Srwatson
106155192Srwatson/*
107155192Srwatson * Definition of type mask for
108155192Srwatson * "bmRequestType":
109155192Srwatson */
110155192Srwatson#define	USB_TYPE_MASK			(0x03 << 5)
111155192Srwatson#define	USB_TYPE_STANDARD		(0x00 << 5)
112155192Srwatson#define	USB_TYPE_CLASS			(0x01 << 5)
113155192Srwatson#define	USB_TYPE_VENDOR			(0x02 << 5)
114155192Srwatson#define	USB_TYPE_RESERVED		(0x03 << 5)
115155192Srwatson
116155192Srwatson/*
117155192Srwatson * Definition of receiver mask for
118155192Srwatson * "bmRequestType":
119155192Srwatson */
120155192Srwatson#define	USB_RECIP_MASK			0x1f
121155192Srwatson#define	USB_RECIP_DEVICE		0x00
122155192Srwatson#define	USB_RECIP_INTERFACE		0x01
123155192Srwatson#define	USB_RECIP_ENDPOINT		0x02
124155192Srwatson#define	USB_RECIP_OTHER			0x03
125155192Srwatson
126155192Srwatson/*
127155192Srwatson * Definition of standard request values for
128155192Srwatson * "bRequest":
129155192Srwatson */
130155192Srwatson#define	USB_REQ_GET_STATUS		0x00
131155192Srwatson#define	USB_REQ_CLEAR_FEATURE		0x01
132156889Srwatson#define	USB_REQ_SET_FEATURE		0x03
133155192Srwatson#define	USB_REQ_SET_ADDRESS		0x05
134155192Srwatson#define	USB_REQ_GET_DESCRIPTOR		0x06
135155192Srwatson#define	USB_REQ_SET_DESCRIPTOR		0x07
136155192Srwatson#define	USB_REQ_GET_CONFIGURATION	0x08
137155192Srwatson#define	USB_REQ_SET_CONFIGURATION	0x09
138155192Srwatson#define	USB_REQ_GET_INTERFACE		0x0A
139155192Srwatson#define	USB_REQ_SET_INTERFACE		0x0B
140155192Srwatson#define	USB_REQ_SYNCH_FRAME		0x0C
141155192Srwatson
142155192Srwatson#define	USB_REQ_SET_ENCRYPTION		0x0D	/* Wireless USB */
143155192Srwatson#define	USB_REQ_GET_ENCRYPTION		0x0E
144155192Srwatson#define	USB_REQ_SET_HANDSHAKE		0x0F
145155192Srwatson#define	USB_REQ_GET_HANDSHAKE		0x10
146155192Srwatson#define	USB_REQ_SET_CONNECTION		0x11
147156889Srwatson#define	USB_REQ_SET_SECURITY_DATA	0x12
148155192Srwatson#define	USB_REQ_GET_SECURITY_DATA	0x13
149155192Srwatson#define	USB_REQ_SET_WUSB_DATA		0x14
150173142Srwatson#define	USB_REQ_LOOPBACK_DATA_WRITE	0x15
151155192Srwatson#define	USB_REQ_LOOPBACK_DATA_READ	0x16
152155192Srwatson#define	USB_REQ_SET_INTERFACE_DS	0x17
153155192Srwatson
154155192Srwatson/*
155155192Srwatson * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
156155192Srwatson * are read as a bit array returned by USB_REQ_GET_STATUS.  (So there
157155192Srwatson * are at most sixteen features of each type.)
158155192Srwatson */
159159269Srwatson#define	USB_DEVICE_SELF_POWERED		0	/* (read only) */
160155192Srwatson#define	USB_DEVICE_REMOTE_WAKEUP	1	/* dev may initiate wakeup */
161155192Srwatson#define	USB_DEVICE_TEST_MODE		2	/* (wired high speed only) */
162155192Srwatson#define	USB_DEVICE_BATTERY		2	/* (wireless) */
163155192Srwatson#define	USB_DEVICE_B_HNP_ENABLE		3	/* (otg) dev may initiate HNP */
164155192Srwatson#define	USB_DEVICE_WUSB_DEVICE		3	/* (wireless) */
165155192Srwatson#define	USB_DEVICE_A_HNP_SUPPORT	4	/* (otg) RH port supports HNP */
166156889Srwatson#define	USB_DEVICE_A_ALT_HNP_SUPPORT	5	/* (otg) other RH port does */
167155192Srwatson#define	USB_DEVICE_DEBUG_MODE		6	/* (special devices only) */
168155192Srwatson
169155192Srwatson#define	USB_ENDPOINT_HALT		0	/* IN/OUT will STALL */
170159269Srwatson
171156889Srwatson#define	PIPE_ISOCHRONOUS		0x01	/* UE_ISOCHRONOUS */
172155192Srwatson#define	PIPE_INTERRUPT			0x03	/* UE_INTERRUPT */
173159269Srwatson#define	PIPE_CONTROL			0x00	/* UE_CONTROL */
174156889Srwatson#define	PIPE_BULK			0x02	/* UE_BULK */
175155192Srwatson
176155192Srwatson/* Whenever Linux references an USB endpoint:
177156889Srwatson * a) to initialize "urb->endpoint"
178155192Srwatson * b) second argument passed to "usb_control_msg()"
179155192Srwatson *
180155192Srwatson * Then it uses one of the following macros. The "endpoint" argument
181155192Srwatson * is the physical endpoint value masked by 0xF. The "dev" argument
182156889Srwatson * is a pointer to "struct usb_device".
183155192Srwatson */
184155192Srwatson#define	usb_sndctrlpipe(dev,endpoint) \
185176690Srwatson  usb_find_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_OUT)
186155192Srwatson
187155192Srwatson#define	usb_rcvctrlpipe(dev,endpoint) \
188155192Srwatson  usb_find_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_IN)
189156889Srwatson
190155192Srwatson#define	usb_sndisocpipe(dev,endpoint) \
191155192Srwatson  usb_find_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_OUT)
192155192Srwatson
193155192Srwatson#define	usb_rcvisocpipe(dev,endpoint) \
194155192Srwatson  usb_find_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_IN)
195155192Srwatson
196155192Srwatson#define	usb_sndbulkpipe(dev,endpoint) \
197155192Srwatson  usb_find_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_OUT)
198155192Srwatson
199155192Srwatson#define	usb_rcvbulkpipe(dev,endpoint) \
200155192Srwatson  usb_find_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_IN)
201155192Srwatson
202155192Srwatson#define	usb_sndintpipe(dev,endpoint) \
203155192Srwatson  usb_find_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_OUT)
204155192Srwatson
205155192Srwatson#define	usb_rcvintpipe(dev,endpoint) \
206155192Srwatson  usb_find_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_IN)
207155192Srwatson
208155192Srwatson/*
209155192Srwatson * The following structure is used to extend "struct urb" when we are
210155192Srwatson * dealing with an isochronous endpoint. It contains information about
211155192Srwatson * the data offset and data length of an isochronous packet.
212155192Srwatson * The "actual_length" field is updated before the "complete"
213155192Srwatson * callback in the "urb" structure is called.
214155192Srwatson */
215155192Srwatsonstruct usb_iso_packet_descriptor {
216155192Srwatson	uint32_t offset;		/* depreciated buffer offset (the
217155192Srwatson					 * packets are usually back to back) */
218155192Srwatson	uint16_t length;		/* expected length */
219155192Srwatson	uint16_t actual_length;
220155192Srwatson	 int16_t status;		/* transfer status */
221155192Srwatson};
222155192Srwatson
223155192Srwatson/*
224155192Srwatson * The following structure holds various information about an USB
225155192Srwatson * transfer. This structure is used for all kinds of USB transfers.
226155192Srwatson *
227155192Srwatson * URB is short for USB Request Block.
228155192Srwatson */
229155192Srwatsonstruct urb {
230155192Srwatson	TAILQ_ENTRY(urb) bsd_urb_list;
231155192Srwatson	struct cv cv_wait;
232155192Srwatson
233155192Srwatson	struct usb_device *dev;		/* (in) pointer to associated device */
234181053Srwatson	struct usb_host_endpoint *endpoint;	/* (in) pipe pointer */
235155192Srwatson	uint8_t *setup_packet;		/* (in) setup packet (control only) */
236155192Srwatson	uint8_t *bsd_data_ptr;
237155192Srwatson	void   *transfer_buffer;	/* (in) associated data buffer */
238155192Srwatson	void   *context;		/* (in) context for completion */
239155192Srwatson	usb_complete_t *complete;	/* (in) completion routine */
240155192Srwatson
241155192Srwatson	usb_size_t transfer_buffer_length;/* (in) data buffer length */
242155192Srwatson	usb_size_t bsd_length_rem;
243156889Srwatson	usb_size_t actual_length;	/* (return) actual transfer length */
244155192Srwatson	usb_timeout_t timeout;		/* FreeBSD specific */
245155192Srwatson
246155192Srwatson	uint16_t transfer_flags;	/* (in) */
247176690Srwatson#define	URB_SHORT_NOT_OK	0x0001	/* report short transfers like errors */
248156889Srwatson#define	URB_ISO_ASAP		0x0002	/* ignore "start_frame" field */
249155192Srwatson#define	URB_ZERO_PACKET		0x0004	/* the USB transfer ends with a short
250155192Srwatson					 * packet */
251156889Srwatson#define	URB_NO_TRANSFER_DMA_MAP 0x0008	/* "transfer_dma" is valid on submit */
252156889Srwatson#define	URB_WAIT_WAKEUP		0x0010	/* custom flags */
253156889Srwatson#define	URB_IS_SLEEPING		0x0020	/* custom flags */
254155192Srwatson
255155192Srwatson	usb_frcount_t start_frame;	/* (modify) start frame (ISO) */
256155192Srwatson	usb_frcount_t number_of_packets;	/* (in) number of ISO packets */
257155192Srwatson	uint16_t interval;		/* (modify) transfer interval
258155192Srwatson					 * (INT/ISO) */
259155192Srwatson	uint16_t error_count;		/* (return) number of ISO errors */
260155192Srwatson	int16_t	status;			/* (return) status */
261155192Srwatson
262155192Srwatson	uint8_t	setup_dma;		/* (in) not used on FreeBSD */
263155192Srwatson	uint8_t	transfer_dma;		/* (in) not used on FreeBSD */
264155192Srwatson	uint8_t	bsd_isread;
265155192Srwatson	uint8_t kill_count;		/* FreeBSD specific */
266155192Srwatson
267155192Srwatson	struct usb_iso_packet_descriptor iso_frame_desc[];	/* (in) ISO ONLY */
268155192Srwatson};
269155192Srwatson
270155192Srwatson/* various prototypes */
271155192Srwatson
272155192Srwatsonint	usb_submit_urb(struct urb *urb, uint16_t mem_flags);
273155192Srwatsonint	usb_unlink_urb(struct urb *urb);
274155192Srwatsonint	usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe);
275155192Srwatsonint	usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *ep,
276155192Srwatson	    uint8_t request, uint8_t requesttype, uint16_t value,
277155192Srwatson	    uint16_t index, void *data, uint16_t size, usb_timeout_t timeout);
278155192Srwatsonint	usb_set_interface(struct usb_device *dev, uint8_t ifnum,
279155192Srwatson	    uint8_t alternate);
280155192Srwatsonint	usb_setup_endpoint(struct usb_device *dev,
281155192Srwatson	    struct usb_host_endpoint *uhe, usb_frlength_t bufsize);
282155192Srwatson
283155192Srwatsonstruct usb_host_endpoint *usb_find_host_endpoint(struct usb_device *dev,
284155192Srwatson	    uint8_t type, uint8_t ep);
285155192Srwatsonstruct urb *usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags);
286155192Srwatsonstruct usb_host_interface *usb_altnum_to_altsetting(
287155192Srwatson	    const struct usb_interface *intf, uint8_t alt_index);
288155192Srwatsonstruct usb_interface *usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no);
289155192Srwatson
290155192Srwatsonvoid   *usb_buffer_alloc(struct usb_device *dev, usb_size_t size,
291155192Srwatson	    uint16_t mem_flags, uint8_t *dma_addr);
292155192Srwatsonvoid   *usbd_get_intfdata(struct usb_interface *intf);
293155192Srwatson
294155192Srwatsonvoid	usb_buffer_free(struct usb_device *dev, usb_size_t size, void *addr, uint8_t dma_addr);
295155192Srwatsonvoid	usb_free_urb(struct urb *urb);
296155192Srwatsonvoid	usb_init_urb(struct urb *urb);
297155192Srwatsonvoid	usb_kill_urb(struct urb *urb);
298155192Srwatsonvoid	usb_set_intfdata(struct usb_interface *intf, void *data);
299155192Srwatsonvoid	usb_linux_register(void *arg);
300155192Srwatsonvoid	usb_linux_deregister(void *arg);
301155192Srwatson
302155192Srwatsonvoid	usb_fill_bulk_urb(struct urb *, struct usb_device *,
303155192Srwatson	    struct usb_host_endpoint *, void *, int, usb_complete_t, void *);
304155192Srwatsonint	usb_bulk_msg(struct usb_device *, struct usb_host_endpoint *,
305155192Srwatson	    void *, int, uint16_t *, usb_timeout_t);
306155192Srwatson
307155192Srwatson#define	interface_to_usbdev(intf) (intf)->linux_udev
308155192Srwatson#define	interface_to_bsddev(intf) (intf)->linux_udev
309155192Srwatson
310155192Srwatson#endif					/* _USB_COMPAT_LINUX_H */
311155192Srwatson