usb.h revision 193045
1139804Simp/* $FreeBSD: head/sys/dev/usb/usb_compat_linux.h 193045 2009-05-29 18:46:57Z thompsa $ */
230489Sphk/*-
330489Sphk * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved.
430489Sphk * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
530489Sphk *
630489Sphk * Redistribution and use in source and binary forms, with or without
730489Sphk * modification, are permitted provided that the following conditions
830489Sphk * are met:
930489Sphk * 1. Redistributions of source code must retain the above copyright
1030489Sphk *    notice, this list of conditions and the following disclaimer.
1130489Sphk * 2. Redistributions in binary form must reproduce the above copyright
1230489Sphk *    notice, this list of conditions and the following disclaimer in the
1330489Sphk *    documentation and/or other materials provided with the distribution.
1430489Sphk *
1530489Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1630489Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1730489Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1830489Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1930489Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2030489Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2130489Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2230489Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2330489Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2430489Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2530489Sphk * SUCH DAMAGE.
2630489Sphk */
2730489Sphk
2830489Sphk#ifndef _USB_COMPAT_LINUX_H
2930489Sphk#define	_USB_COMPAT_LINUX_H
3030489Sphk
3130489Sphkstruct usb_device;
3230489Sphkstruct usb_interface;
3330489Sphkstruct usb_driver;
3430489Sphkstruct urb;
35116182Sobrien
36116182Sobrientypedef void *pm_message_t;
37116182Sobrientypedef void (usb_complete_t)(struct urb *);
3830489Sphk
3930489Sphk#define	USB_MAX_FULL_SPEED_ISOC_FRAMES (60 * 1)
4060041Sphk#define	USB_MAX_HIGH_SPEED_ISOC_FRAMES (60 * 8)
4144272Sbde
4265770Sbp/*
43147198Sssouhlal * Linux compatible USB device drivers put their device information
4430489Sphk * into the "usb_device_id" structure using the "USB_DEVICE()" macro.
45114216Skan * The "MODULE_DEVICE_TABLE()" macro can be used to export this
4631561Sbde * information to userland.
4730743Sphk */
4851068Salfredstruct usb_device_id {
4967365Sjhb	/* which fields to match against */
5030492Sphk	uint16_t match_flags;
5130489Sphk#define	USB_DEVICE_ID_MATCH_VENDOR		0x0001
5230743Sphk#define	USB_DEVICE_ID_MATCH_PRODUCT		0x0002
5330489Sphk#define	USB_DEVICE_ID_MATCH_DEV_LO		0x0004
5465770Sbp#define	USB_DEVICE_ID_MATCH_DEV_HI		0x0008
5565770Sbp#define	USB_DEVICE_ID_MATCH_DEV_CLASS		0x0010
5665770Sbp#define	USB_DEVICE_ID_MATCH_DEV_SUBCLASS	0x0020
5765770Sbp#define	USB_DEVICE_ID_MATCH_DEV_PROTOCOL	0x0040
5865770Sbp#define	USB_DEVICE_ID_MATCH_INT_CLASS		0x0080
5965770Sbp#define	USB_DEVICE_ID_MATCH_INT_SUBCLASS	0x0100
6065770Sbp#define	USB_DEVICE_ID_MATCH_INT_PROTOCOL	0x0200
6165770Sbp
6265770Sbp	/* Used for product specific matches; the BCD range is inclusive */
6392723Salfred	uint16_t idVendor;
6492723Salfred	uint16_t idProduct;
6530489Sphk	uint16_t bcdDevice_lo;
6630489Sphk	uint16_t bcdDevice_hi;
6730489Sphk
6830489Sphk	/* Used for device class matches */
6930489Sphk	uint8_t	bDeviceClass;
7030489Sphk	uint8_t	bDeviceSubClass;
7130489Sphk	uint8_t	bDeviceProtocol;
7230489Sphk
7330489Sphk	/* Used for interface class matches */
74138290Sphk	uint8_t	bInterfaceClass;
75138290Sphk	uint8_t	bInterfaceSubClass;
76138339Sphk	uint8_t	bInterfaceProtocol;
77138339Sphk
78138290Sphk	/* Hook for driver specific information */
79138290Sphk	unsigned long driver_info;
80138290Sphk};
81138290Sphk
82138290Sphk#define	USB_DEVICE_ID_MATCH_DEVICE \
83138290Sphk	(USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT)
84143494Sjeff
85138290Sphk#define	USB_DEVICE(vend,prod) \
86147198Sssouhlal	.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = (vend), \
87138290Sphk	.idProduct = (prod)
88138290Sphk
89138290Sphk/* The "usb_driver" structure holds the Linux USB device driver
90138290Sphk * callbacks, and a pointer to device ID's which this entry should
91138290Sphk * match against. Usually this entry is exposed to the USB emulation
92138290Sphk * layer using the "USB_DRIVER_EXPORT()" macro, which is defined
93138290Sphk * below.
94138290Sphk */
95138290Sphkstruct usb_driver {
96138290Sphk	const char *name;
97138290Sphk
98138290Sphk	int     (*probe) (struct usb_interface *intf,
9930489Sphk	    	const	struct usb_device_id *id);
10030489Sphk
10191690Seivind	void    (*disconnect) (struct usb_interface *intf);
10291690Seivind
10391690Seivind	int     (*ioctl) (struct usb_interface *intf, unsigned int code,
10491690Seivind	    	void  *buf);
10591690Seivind
10630489Sphk	int     (*suspend) (struct usb_interface *intf, pm_message_t message);
10730492Sphk	int     (*resume) (struct usb_interface *intf);
10830489Sphk
10930489Sphk	const struct usb_device_id *id_table;
11030492Sphk
11130489Sphk	void    (*shutdown) (struct usb_interface *intf);
11230489Sphk
11330489Sphk	LIST_ENTRY(usb_driver) linux_driver_list;
11430489Sphk};
11530489Sphk
11630489Sphk#define	USB_DRIVER_EXPORT(id,p_usb_drv) \
11730492Sphk  SYSINIT(id,SI_SUB_KLD,SI_ORDER_FIRST,usb_linux_register,p_usb_drv); \
11830489Sphk  SYSUNINIT(id,SI_SUB_KLD,SI_ORDER_ANY,usb_linux_deregister,p_usb_drv)
11930489Sphk
12030492Sphk#define	USB_DT_ENDPOINT_SIZE		7
12130492Sphk#define	USB_DT_ENDPOINT_AUDIO_SIZE	9
12230492Sphk
12330492Sphk/*
12430492Sphk * Endpoints
12530492Sphk */
12630492Sphk#define	USB_ENDPOINT_NUMBER_MASK	0x0f	/* in bEndpointAddress */
12730492Sphk#define	USB_ENDPOINT_DIR_MASK		0x80
12830492Sphk
12930492Sphk#define	USB_ENDPOINT_XFERTYPE_MASK	0x03	/* in bmAttributes */
13030492Sphk#define	USB_ENDPOINT_XFER_CONTROL	0
13130492Sphk#define	USB_ENDPOINT_XFER_ISOC		1
13230492Sphk#define	USB_ENDPOINT_XFER_BULK		2
13330492Sphk#define	USB_ENDPOINT_XFER_INT		3
13430492Sphk#define	USB_ENDPOINT_MAX_ADJUSTABLE	0x80
13530492Sphk
13630492Sphk/* CONTROL REQUEST SUPPORT */
13730492Sphk
13830492Sphk/*
13930492Sphk * Definition of direction mask for
14030492Sphk * "bEndpointAddress" and "bmRequestType":
14130492Sphk */
14230492Sphk#define	USB_DIR_MASK			0x80
14330492Sphk#define	USB_DIR_OUT			0x00	/* write to USB device */
14491690Seivind#define	USB_DIR_IN			0x80	/* read from USB device */
14591690Seivind
14691690Seivind/*
14741056Speter * Definition of type mask for
14841056Speter * "bmRequestType":
14941056Speter */
15041056Speter#define	USB_TYPE_MASK			(0x03 << 5)
15172594Sbde#define	USB_TYPE_STANDARD		(0x00 << 5)
15241056Speter#define	USB_TYPE_CLASS			(0x01 << 5)
15341056Speter#define	USB_TYPE_VENDOR			(0x02 << 5)
15491690Seivind#define	USB_TYPE_RESERVED		(0x03 << 5)
15591690Seivind
15691690Seivind/*
15791690Seivind * Definition of receiver mask for
15891690Seivind * "bmRequestType":
15991690Seivind */
16091690Seivind#define	USB_RECIP_MASK			0x1f
16191690Seivind#define	USB_RECIP_DEVICE		0x00
16291690Seivind#define	USB_RECIP_INTERFACE		0x01
16391690Seivind#define	USB_RECIP_ENDPOINT		0x02
16491690Seivind#define	USB_RECIP_OTHER			0x03
16591690Seivind
16672594Sbde/*
16772594Sbde * Definition of standard request values for
16872594Sbde * "bRequest":
16972594Sbde */
17072594Sbde#define	USB_REQ_GET_STATUS		0x00
17172594Sbde#define	USB_REQ_CLEAR_FEATURE		0x01
17272594Sbde#define	USB_REQ_SET_FEATURE		0x03
17372594Sbde#define	USB_REQ_SET_ADDRESS		0x05
17472594Sbde#define	USB_REQ_GET_DESCRIPTOR		0x06
17572594Sbde#define	USB_REQ_SET_DESCRIPTOR		0x07
17672594Sbde#define	USB_REQ_GET_CONFIGURATION	0x08
17772594Sbde#define	USB_REQ_SET_CONFIGURATION	0x09
17872594Sbde#define	USB_REQ_GET_INTERFACE		0x0A
17946349Salc#define	USB_REQ_SET_INTERFACE		0x0B
18046349Salc#define	USB_REQ_SYNCH_FRAME		0x0C
18146349Salc
18246349Salc#define	USB_REQ_SET_ENCRYPTION		0x0D	/* Wireless USB */
18346349Salc#define	USB_REQ_GET_ENCRYPTION		0x0E
18458934Sphk#define	USB_REQ_SET_HANDSHAKE		0x0F
18558345Sphk#define	USB_REQ_GET_HANDSHAKE		0x10
186112067Skan#define	USB_REQ_SET_CONNECTION		0x11
18758345Sphk#define	USB_REQ_SET_SECURITY_DATA	0x12
18858934Sphk#define	USB_REQ_GET_SECURITY_DATA	0x13
18946349Salc#define	USB_REQ_SET_WUSB_DATA		0x14
19046349Salc#define	USB_REQ_LOOPBACK_DATA_WRITE	0x15
19130489Sphk#define	USB_REQ_LOOPBACK_DATA_READ	0x16
19230489Sphk#define	USB_REQ_SET_INTERFACE_DS	0x17
19330489Sphk
19430489Sphk/*
195111842Snjl * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
19658934Sphk * are read as a bit array returned by USB_REQ_GET_STATUS.  (So there
19730489Sphk * are at most sixteen features of each type.)
19859249Sphk */
19930489Sphk#define	USB_DEVICE_SELF_POWERED		0	/* (read only) */
20030489Sphk#define	USB_DEVICE_REMOTE_WAKEUP	1	/* dev may initiate wakeup */
20130492Sphk#define	USB_DEVICE_TEST_MODE		2	/* (wired high speed only) */
20291690Seivind#define	USB_DEVICE_BATTERY		2	/* (wireless) */
20391690Seivind#define	USB_DEVICE_B_HNP_ENABLE		3	/* (otg) dev may initiate HNP */
204112067Skan#define	USB_DEVICE_WUSB_DEVICE		3	/* (wireless) */
20591690Seivind#define	USB_DEVICE_A_HNP_SUPPORT	4	/* (otg) RH port supports HNP */
20691690Seivind#define	USB_DEVICE_A_ALT_HNP_SUPPORT	5	/* (otg) other RH port does */
20791690Seivind#define	USB_DEVICE_DEBUG_MODE		6	/* (special devices only) */
20891690Seivind
20991690Seivind#define	USB_ENDPOINT_HALT		0	/* IN/OUT will STALL */
21030492Sphk
21130492Sphk#define	PIPE_ISOCHRONOUS		0x01	/* UE_ISOCHRONOUS */
21230492Sphk#define	PIPE_INTERRUPT			0x03	/* UE_INTERRUPT */
21330492Sphk#define	PIPE_CONTROL			0x00	/* UE_CONTROL */
21430492Sphk#define	PIPE_BULK			0x02	/* UE_BULK */
21530492Sphk
21630492Sphk/* Whenever Linux references an USB endpoint:
21730492Sphk * a) to initialize "urb->pipe"
21830492Sphk * b) second argument passed to "usb_control_msg()"
21930492Sphk *
22030492Sphk * Then it uses one of the following macros. The "endpoint" argument
22130492Sphk * is the physical endpoint value masked by 0xF. The "dev" argument
22230492Sphk * is a pointer to "struct usb_device".
22330492Sphk */
22430492Sphk#define	usb_sndctrlpipe(dev,endpoint) \
22530492Sphk  usb_find_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_OUT)
22630492Sphk
22730492Sphk#define	usb_rcvctrlpipe(dev,endpoint) \
22830492Sphk  usb_find_host_endpoint(dev, PIPE_CONTROL, (endpoint) | USB_DIR_IN)
22930492Sphk
23030492Sphk#define	usb_sndisocpipe(dev,endpoint) \
23130492Sphk  usb_find_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_OUT)
23230492Sphk
23330492Sphk#define	usb_rcvisocpipe(dev,endpoint) \
23430492Sphk  usb_find_host_endpoint(dev, PIPE_ISOCHRONOUS, (endpoint) | USB_DIR_IN)
23530492Sphk
23630492Sphk#define	usb_sndbulkpipe(dev,endpoint) \
23730492Sphk  usb_find_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_OUT)
23830492Sphk
23930492Sphk#define	usb_rcvbulkpipe(dev,endpoint) \
24030492Sphk  usb_find_host_endpoint(dev, PIPE_BULK, (endpoint) | USB_DIR_IN)
24130492Sphk
24230492Sphk#define	usb_sndintpipe(dev,endpoint) \
24330513Sphk  usb_find_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_OUT)
24430513Sphk
24530513Sphk#define	usb_rcvintpipe(dev,endpoint) \
24630513Sphk  usb_find_host_endpoint(dev, PIPE_INTERRUPT, (endpoint) | USB_DIR_IN)
24730513Sphk
24830513Sphk/*
24930513Sphk * The following structure is used to extend "struct urb" when we are
25030513Sphk * dealing with an isochronous endpoint. It contains information about
25130513Sphk * the data offset and data length of an isochronous packet.
25283366Sjulian * The "actual_length" field is updated before the "complete"
25330513Sphk * callback in the "urb" structure is called.
254112067Skan */
25566355Sbpstruct usb_iso_packet_descriptor {
25630513Sphk	uint32_t offset;		/* depreciated buffer offset (the
25742900Seivind					 * packets are usually back to back) */
258105077Smckusick	uint16_t length;		/* expected length */
25942900Seivind	uint16_t actual_length;
260105077Smckusick	uint16_t status;
26183366Sjulian};
26242900Seivind
26330513Sphk/*
26430513Sphk * The following structure holds various information about an USB
26591690Seivind * transfer. This structure is used for all kinds of USB transfers.
26630513Sphk *
26730513Sphk * URB is short for USB Request Block.
26830513Sphk */
26930513Sphkstruct urb {
27030513Sphk	TAILQ_ENTRY(urb) bsd_urb_list;
27183366Sjulian	struct cv cv_wait;
27230513Sphk
27330513Sphk	struct usb_device *dev;		/* (in) pointer to associated device */
27466355Sbp	struct usb_host_endpoint *pipe;	/* (in) pipe pointer */
27530513Sphk	uint8_t *setup_packet;		/* (in) setup packet (control only) */
276105077Smckusick	uint8_t *bsd_data_ptr;
27783366Sjulian	void   *transfer_buffer;	/* (in) associated data buffer */
27830513Sphk	void   *context;		/* (in) context for completion */
27930513Sphk	usb_complete_t *complete;	/* (in) completion routine */
28091690Seivind
28130513Sphk	size_t transfer_buffer_length;/* (in) data buffer length */
28230513Sphk	size_t bsd_length_rem;
28330513Sphk	size_t actual_length;	/* (return) actual transfer length */
28430513Sphk	usb_timeout_t timeout;		/* FreeBSD specific */
28583366Sjulian
28630513Sphk	uint16_t transfer_flags;	/* (in) */
28730513Sphk#define	URB_SHORT_NOT_OK	0x0001	/* report short transfers like errors */
28830513Sphk#define	URB_ISO_ASAP		0x0002	/* ignore "start_frame" field */
289105077Smckusick#define	URB_ZERO_PACKET		0x0004	/* the USB transfer ends with a short
29030513Sphk					 * packet */
29130513Sphk#define	URB_NO_TRANSFER_DMA_MAP 0x0008	/* "transfer_dma" is valid on submit */
29230743Sphk#define	URB_WAIT_WAKEUP		0x0010	/* custom flags */
29330743Sphk#define	URB_IS_SLEEPING		0x0020	/* custom flags */
29430743Sphk
29530743Sphk	usb_frcount_t start_frame;	/* (modify) start frame (ISO) */
29630743Sphk	usb_frcount_t number_of_packets;	/* (in) number of ISO packets */
29730743Sphk	uint16_t interval;		/* (modify) transfer interval
29830743Sphk					 * (INT/ISO) */
29930743Sphk	uint16_t error_count;		/* (return) number of ISO errors */
30030743Sphk	int16_t	status;			/* (return) status */
30183366Sjulian
30230743Sphk	uint8_t	setup_dma;		/* (in) not used on FreeBSD */
30330743Sphk	uint8_t	transfer_dma;		/* (in) not used on FreeBSD */
30430743Sphk	uint8_t	bsd_isread;
30531727Swollman
30631727Swollman	struct usb_iso_packet_descriptor iso_frame_desc[];	/* (in) ISO ONLY */
30731727Swollman};
30831727Swollman
30931727Swollman/* various prototypes */
310120514Sphk
31130743Sphkint	usb_submit_urb(struct urb *urb, uint16_t mem_flags);
31231727Swollmanint	usb_unlink_urb(struct urb *urb);
31331727Swollmanint	usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe);
31431727Swollmanint	usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *pipe,
31530743Sphk	    uint8_t request, uint8_t requesttype, uint16_t value,
31630743Sphk	    uint16_t index, void *data, uint16_t size, usb_timeout_t timeout);
31730743Sphkint	usb_set_interface(struct usb_device *dev, uint8_t ifnum,
31831727Swollman	    uint8_t alternate);
31931727Swollmanint	usb_setup_endpoint(struct usb_device *dev,
32031727Swollman	    struct usb_host_endpoint *uhe, usb_frlength_t bufsize);
32130743Sphk
32231727Swollmanstruct usb_host_endpoint *usb_find_host_endpoint(struct usb_device *dev,
32331727Swollman	    uint8_t type, uint8_t ep);
32431727Swollmanstruct urb *usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags);
32531727Swollmanstruct usb_host_interface *usb_altnum_to_altsetting(
32631727Swollman	    const struct usb_interface *intf, uint8_t alt_index);
32783366Sjulianstruct usb_interface *usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no);
32831727Swollman
32931727Swollmanvoid   *usb_buffer_alloc(struct usb_device *dev, size_t size,
33076578Sjlemon	    uint16_t mem_flags, uint8_t *dma_addr);
33183366Sjulianvoid   *usb_get_intfdata(struct usb_interface *intf);
33276578Sjlemon
33331727Swollmanvoid	usb_buffer_free(struct usb_device *dev, size_t size, void *addr, uint8_t dma_addr);
33431727Swollmanvoid	usb_free_urb(struct urb *urb);
33530743Sphkvoid	usb_init_urb(struct urb *urb);
33662976Smckusickvoid	usb_kill_urb(struct urb *urb);
33762976Smckusickvoid	usb_set_intfdata(struct usb_interface *intf, void *data);
33862976Smckusickvoid	usb_linux_register(void *arg);
33962976Smckusickvoid	usb_linux_deregister(void *arg);
34062976Smckusick
34162976Smckusick#define	interface_to_usbdev(intf) (intf)->linux_udev
34262976Smckusick#define	interface_to_bsddev(intf) (intf)->linux_udev->bsd_udev
34362976Smckusick
34462976Smckusick#endif					/* _USB_COMPAT_LINUX_H */
34562976Smckusick