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