Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 1 | // USB functions and data. |
| 2 | #ifndef __USB_H |
| 3 | #define __USB_H |
| 4 | |
Kevin O'Connor | 3df600b | 2013-09-14 19:28:55 -0400 | [diff] [blame] | 5 | #include "stacks.h" // struct mutex_s |
Kevin O'Connor | 8ebcac0 | 2010-03-09 19:58:23 -0500 | [diff] [blame] | 6 | |
Kevin O'Connor | d28b0fe | 2010-03-28 15:11:19 -0400 | [diff] [blame] | 7 | // Information on a USB end point. |
Kevin O'Connor | 357bdfa | 2010-02-26 08:57:13 -0500 | [diff] [blame] | 8 | struct usb_pipe { |
Kevin O'Connor | 98af353 | 2012-02-20 12:54:49 -0500 | [diff] [blame] | 9 | union { |
| 10 | struct usb_s *cntl; |
| 11 | struct usb_pipe *freenext; |
| 12 | }; |
Kevin O'Connor | 4547eb9 | 2010-02-28 02:17:28 -0500 | [diff] [blame] | 13 | u8 type; |
| 14 | u8 ep; |
| 15 | u8 devaddr; |
Kevin O'Connor | 190cc62 | 2010-03-09 19:43:52 -0500 | [diff] [blame] | 16 | u8 speed; |
Kevin O'Connor | 4547eb9 | 2010-02-28 02:17:28 -0500 | [diff] [blame] | 17 | u16 maxpacket; |
Kevin O'Connor | 01de9bd | 2012-03-05 22:41:21 -0500 | [diff] [blame] | 18 | u8 eptype; |
Kevin O'Connor | 357bdfa | 2010-02-26 08:57:13 -0500 | [diff] [blame] | 19 | }; |
| 20 | |
Kevin O'Connor | 4f74dad | 2012-03-06 08:20:40 -0500 | [diff] [blame] | 21 | // Common information for usb devices. |
| 22 | struct usbdevice_s { |
| 23 | struct usbhub_s *hub; |
| 24 | struct usb_pipe *defpipe; |
Kevin O'Connor | 81ee3bb | 2013-12-27 21:09:53 -0500 | [diff] [blame^] | 25 | u32 slotid; |
Kevin O'Connor | 4f74dad | 2012-03-06 08:20:40 -0500 | [diff] [blame] | 26 | u32 port; |
Kevin O'Connor | 6a8e895 | 2012-03-08 07:20:30 -0500 | [diff] [blame] | 27 | struct usb_config_descriptor *config; |
| 28 | struct usb_interface_descriptor *iface; |
| 29 | int imax; |
Kevin O'Connor | 4f74dad | 2012-03-06 08:20:40 -0500 | [diff] [blame] | 30 | u8 speed; |
Kevin O'Connor | 77ab4e1 | 2012-03-08 08:44:32 -0500 | [diff] [blame] | 31 | u8 devaddr; |
Kevin O'Connor | 4f74dad | 2012-03-06 08:20:40 -0500 | [diff] [blame] | 32 | }; |
| 33 | |
Kevin O'Connor | 406fad6 | 2010-02-28 02:23:19 -0500 | [diff] [blame] | 34 | // Common information for usb controllers. |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 35 | struct usb_s { |
Kevin O'Connor | 98af353 | 2012-02-20 12:54:49 -0500 | [diff] [blame] | 36 | struct usb_pipe *freelist; |
Kevin O'Connor | 8ebcac0 | 2010-03-09 19:58:23 -0500 | [diff] [blame] | 37 | struct mutex_s resetlock; |
Kevin O'Connor | 8ff8e01 | 2011-07-09 14:11:21 -0400 | [diff] [blame] | 38 | struct pci_device *pci; |
Kevin O'Connor | 406fad6 | 2010-02-28 02:23:19 -0500 | [diff] [blame] | 39 | u8 type; |
| 40 | u8 maxaddr; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 41 | }; |
| 42 | |
Kevin O'Connor | d28b0fe | 2010-03-28 15:11:19 -0400 | [diff] [blame] | 43 | // Information for enumerating USB hubs |
| 44 | struct usbhub_s { |
| 45 | struct usbhub_op_s *op; |
Kevin O'Connor | ea27478 | 2012-03-08 07:49:09 -0500 | [diff] [blame] | 46 | struct usbdevice_s *usbdev; |
Kevin O'Connor | d28b0fe | 2010-03-28 15:11:19 -0400 | [diff] [blame] | 47 | struct usb_s *cntl; |
| 48 | struct mutex_s lock; |
| 49 | u32 powerwait; |
| 50 | u32 port; |
| 51 | u32 threads; |
| 52 | u32 portcount; |
| 53 | u32 devcount; |
| 54 | }; |
| 55 | |
| 56 | // Hub callback (32bit) info |
| 57 | struct usbhub_op_s { |
| 58 | int (*detect)(struct usbhub_s *hub, u32 port); |
| 59 | int (*reset)(struct usbhub_s *hub, u32 port); |
| 60 | void (*disconnect)(struct usbhub_s *hub, u32 port); |
| 61 | }; |
| 62 | |
Gerd Hoffmann | e144bb7 | 2013-06-03 16:30:18 +0200 | [diff] [blame] | 63 | #define USB_TYPE_UHCI 1 |
| 64 | #define USB_TYPE_OHCI 2 |
| 65 | #define USB_TYPE_EHCI 3 |
| 66 | #define USB_TYPE_XHCI 4 |
Kevin O'Connor | 190cc62 | 2010-03-09 19:43:52 -0500 | [diff] [blame] | 67 | |
Gerd Hoffmann | e144bb7 | 2013-06-03 16:30:18 +0200 | [diff] [blame] | 68 | #define USB_FULLSPEED 0 |
| 69 | #define USB_LOWSPEED 1 |
| 70 | #define USB_HIGHSPEED 2 |
| 71 | #define USB_SUPERSPEED 3 |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 72 | |
Gerd Hoffmann | e144bb7 | 2013-06-03 16:30:18 +0200 | [diff] [blame] | 73 | #define USB_MAXADDR 127 |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 74 | |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 75 | |
| 76 | /**************************************************************** |
| 77 | * usb structs and flags |
| 78 | ****************************************************************/ |
| 79 | |
Kevin O'Connor | 8bbc79c | 2010-02-14 12:16:32 -0500 | [diff] [blame] | 80 | // USB mandated timings (in ms) |
| 81 | #define USB_TIME_SIGATT 100 |
| 82 | #define USB_TIME_ATTDB 100 |
| 83 | #define USB_TIME_DRST 10 |
| 84 | #define USB_TIME_DRSTR 50 |
| 85 | #define USB_TIME_RSTRCY 10 |
| 86 | |
| 87 | #define USB_TIME_SETADDR_RECOVERY 2 |
| 88 | |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 89 | #define USB_PID_OUT 0xe1 |
| 90 | #define USB_PID_IN 0x69 |
| 91 | #define USB_PID_SETUP 0x2d |
| 92 | |
| 93 | #define USB_DIR_OUT 0 /* to device */ |
| 94 | #define USB_DIR_IN 0x80 /* to host */ |
| 95 | |
| 96 | #define USB_TYPE_MASK (0x03 << 5) |
| 97 | #define USB_TYPE_STANDARD (0x00 << 5) |
| 98 | #define USB_TYPE_CLASS (0x01 << 5) |
| 99 | #define USB_TYPE_VENDOR (0x02 << 5) |
| 100 | #define USB_TYPE_RESERVED (0x03 << 5) |
| 101 | |
| 102 | #define USB_RECIP_MASK 0x1f |
| 103 | #define USB_RECIP_DEVICE 0x00 |
| 104 | #define USB_RECIP_INTERFACE 0x01 |
| 105 | #define USB_RECIP_ENDPOINT 0x02 |
| 106 | #define USB_RECIP_OTHER 0x03 |
| 107 | |
| 108 | #define USB_REQ_GET_STATUS 0x00 |
| 109 | #define USB_REQ_CLEAR_FEATURE 0x01 |
| 110 | #define USB_REQ_SET_FEATURE 0x03 |
| 111 | #define USB_REQ_SET_ADDRESS 0x05 |
| 112 | #define USB_REQ_GET_DESCRIPTOR 0x06 |
| 113 | #define USB_REQ_SET_DESCRIPTOR 0x07 |
| 114 | #define USB_REQ_GET_CONFIGURATION 0x08 |
| 115 | #define USB_REQ_SET_CONFIGURATION 0x09 |
| 116 | #define USB_REQ_GET_INTERFACE 0x0A |
| 117 | #define USB_REQ_SET_INTERFACE 0x0B |
| 118 | #define USB_REQ_SYNCH_FRAME 0x0C |
| 119 | |
| 120 | struct usb_ctrlrequest { |
| 121 | u8 bRequestType; |
| 122 | u8 bRequest; |
| 123 | u16 wValue; |
| 124 | u16 wIndex; |
| 125 | u16 wLength; |
| 126 | } PACKED; |
| 127 | |
| 128 | #define USB_DT_DEVICE 0x01 |
| 129 | #define USB_DT_CONFIG 0x02 |
| 130 | #define USB_DT_STRING 0x03 |
| 131 | #define USB_DT_INTERFACE 0x04 |
| 132 | #define USB_DT_ENDPOINT 0x05 |
| 133 | #define USB_DT_DEVICE_QUALIFIER 0x06 |
| 134 | #define USB_DT_OTHER_SPEED_CONFIG 0x07 |
Gerd Hoffmann | ee9b84f | 2013-06-13 14:23:08 +0200 | [diff] [blame] | 135 | #define USB_DT_ENDPOINT_COMPANION 0x30 |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 136 | |
| 137 | struct usb_device_descriptor { |
| 138 | u8 bLength; |
| 139 | u8 bDescriptorType; |
| 140 | |
| 141 | u16 bcdUSB; |
| 142 | u8 bDeviceClass; |
| 143 | u8 bDeviceSubClass; |
| 144 | u8 bDeviceProtocol; |
| 145 | u8 bMaxPacketSize0; |
| 146 | u16 idVendor; |
| 147 | u16 idProduct; |
| 148 | u16 bcdDevice; |
| 149 | u8 iManufacturer; |
| 150 | u8 iProduct; |
| 151 | u8 iSerialNumber; |
| 152 | u8 bNumConfigurations; |
| 153 | } PACKED; |
| 154 | |
| 155 | #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ |
| 156 | #define USB_CLASS_AUDIO 1 |
| 157 | #define USB_CLASS_COMM 2 |
| 158 | #define USB_CLASS_HID 3 |
| 159 | #define USB_CLASS_PHYSICAL 5 |
| 160 | #define USB_CLASS_STILL_IMAGE 6 |
| 161 | #define USB_CLASS_PRINTER 7 |
| 162 | #define USB_CLASS_MASS_STORAGE 8 |
| 163 | #define USB_CLASS_HUB 9 |
| 164 | |
| 165 | struct usb_config_descriptor { |
| 166 | u8 bLength; |
| 167 | u8 bDescriptorType; |
| 168 | |
| 169 | u16 wTotalLength; |
| 170 | u8 bNumInterfaces; |
| 171 | u8 bConfigurationValue; |
| 172 | u8 iConfiguration; |
| 173 | u8 bmAttributes; |
| 174 | u8 bMaxPower; |
| 175 | } PACKED; |
| 176 | |
| 177 | struct usb_interface_descriptor { |
| 178 | u8 bLength; |
| 179 | u8 bDescriptorType; |
| 180 | |
| 181 | u8 bInterfaceNumber; |
| 182 | u8 bAlternateSetting; |
| 183 | u8 bNumEndpoints; |
| 184 | u8 bInterfaceClass; |
| 185 | u8 bInterfaceSubClass; |
| 186 | u8 bInterfaceProtocol; |
| 187 | u8 iInterface; |
| 188 | } PACKED; |
| 189 | |
| 190 | struct usb_endpoint_descriptor { |
| 191 | u8 bLength; |
| 192 | u8 bDescriptorType; |
| 193 | |
| 194 | u8 bEndpointAddress; |
| 195 | u8 bmAttributes; |
| 196 | u16 wMaxPacketSize; |
| 197 | u8 bInterval; |
| 198 | } PACKED; |
| 199 | |
| 200 | #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */ |
| 201 | #define USB_ENDPOINT_DIR_MASK 0x80 |
| 202 | |
| 203 | #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */ |
| 204 | #define USB_ENDPOINT_XFER_CONTROL 0 |
| 205 | #define USB_ENDPOINT_XFER_ISOC 1 |
| 206 | #define USB_ENDPOINT_XFER_BULK 2 |
| 207 | #define USB_ENDPOINT_XFER_INT 3 |
| 208 | #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80 |
| 209 | |
Kevin O'Connor | 4547eb9 | 2010-02-28 02:17:28 -0500 | [diff] [blame] | 210 | |
| 211 | /**************************************************************** |
Gerd Hoffmann | 5208d93 | 2012-07-20 10:59:23 +0200 | [diff] [blame] | 212 | * usb mass storage flags |
| 213 | ****************************************************************/ |
| 214 | |
| 215 | #define US_SC_ATAPI_8020 0x02 |
| 216 | #define US_SC_ATAPI_8070 0x05 |
| 217 | #define US_SC_SCSI 0x06 |
| 218 | |
Gerd Hoffmann | e53e30d | 2012-07-20 10:59:24 +0200 | [diff] [blame] | 219 | #define US_PR_BULK 0x50 /* bulk-only transport */ |
| 220 | #define US_PR_UAS 0x62 /* usb attached scsi */ |
Gerd Hoffmann | 5208d93 | 2012-07-20 10:59:23 +0200 | [diff] [blame] | 221 | |
| 222 | /**************************************************************** |
Kevin O'Connor | 4547eb9 | 2010-02-28 02:17:28 -0500 | [diff] [blame] | 223 | * function defs |
| 224 | ****************************************************************/ |
| 225 | |
| 226 | // usb.c |
Kevin O'Connor | c3d96c2 | 2012-03-10 09:03:25 -0500 | [diff] [blame] | 227 | struct usb_pipe *usb_alloc_pipe(struct usbdevice_s *usbdev |
| 228 | , struct usb_endpoint_descriptor *epdesc); |
Kevin O'Connor | fbf66f1 | 2012-03-10 09:13:58 -0500 | [diff] [blame] | 229 | int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize); |
| 230 | int usb_poll_intr(struct usb_pipe *pipe, void *data); |
| 231 | int send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req |
| 232 | , void *data); |
| 233 | void free_pipe(struct usb_pipe *pipe); |
| 234 | struct usb_pipe *usb_getFreePipe(struct usb_s *cntl, u8 eptype); |
| 235 | void usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev |
| 236 | , struct usb_endpoint_descriptor *epdesc); |
Kevin O'Connor | eb50eae | 2012-03-10 08:48:31 -0500 | [diff] [blame] | 237 | int usb_getFrameExp(struct usbdevice_s *usbdev |
| 238 | , struct usb_endpoint_descriptor *epdesc); |
Kevin O'Connor | 6a8e895 | 2012-03-08 07:20:30 -0500 | [diff] [blame] | 239 | struct usb_endpoint_descriptor *findEndPointDesc(struct usbdevice_s *usbdev |
| 240 | , int type, int dir); |
Kevin O'Connor | fbf66f1 | 2012-03-10 09:13:58 -0500 | [diff] [blame] | 241 | void usb_enumerate(struct usbhub_s *hub); |
| 242 | void usb_setup(void); |
Kevin O'Connor | 4547eb9 | 2010-02-28 02:17:28 -0500 | [diff] [blame] | 243 | |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 244 | #endif // usb.h |