Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Gerry Hamel, geh@ti.com, Texas Instruments |
| 5 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 6 | * (C) Copyright 2006 |
| 7 | * Bryan O'Donoghue, deckard@CodeHermit.ie |
| 8 | * |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 9 | * Based on |
| 10 | * linux/drivers/usbd/ep0.c |
| 11 | * |
| 12 | * Copyright (c) 2000, 2001, 2002 Lineo |
| 13 | * Copyright (c) 2001 Hewlett Packard |
| 14 | * |
| 15 | * By: |
| 16 | * Stuart Lynne <sl@lineo.com>, |
| 17 | * Tom Rushworth <tbr@lineo.com>, |
| 18 | * Bruce Balden <balden@lineo.com> |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * This is the builtin ep0 control function. It implements all required functionality |
| 23 | * for responding to control requests (SETUP packets). |
| 24 | * |
| 25 | * XXX |
| 26 | * |
| 27 | * Currently we do not pass any SETUP packets (or other) to the configured |
| 28 | * function driver. This may need to change. |
| 29 | * |
| 30 | * XXX |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 31 | * |
| 32 | * As alluded to above, a simple callback cdc_recv_setup has been implemented |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 33 | * in the usb_device data structure to facilicate passing |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 34 | * Common Device Class packets to a function driver. |
| 35 | * |
| 36 | * XXX |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
Simon Glass | bd7a59a | 2019-11-14 12:57:23 -0700 | [diff] [blame] | 39 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8f6bcf4 | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 40 | #include <usbdevice.h> |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 41 | |
| 42 | #if 0 |
| 43 | #define dbg_ep0(lvl,fmt,args...) serial_printf("[%s] %s:%d: "fmt"\n",__FILE__,__FUNCTION__,__LINE__,##args) |
| 44 | #else |
| 45 | #define dbg_ep0(lvl,fmt,args...) |
| 46 | #endif |
| 47 | |
Pali Rohár | 2cecb46 | 2020-12-26 19:12:11 +0100 | [diff] [blame] | 48 | __maybe_unused static char *usbd_device_descriptors[] = { |
| 49 | "UNKNOWN", /* 0 */ |
| 50 | "DEVICE", /* 1 */ |
| 51 | "CONFIG", /* 2 */ |
| 52 | "STRING", /* 3 */ |
| 53 | "INTERFACE", /* 4 */ |
| 54 | "ENDPOINT", /* 5 */ |
| 55 | "DEVICE QUALIFIER", /* 6 */ |
| 56 | "OTHER SPEED", /* 7 */ |
| 57 | "INTERFACE POWER", /* 8 */ |
| 58 | }; |
| 59 | |
| 60 | #define USBD_DEVICE_DESCRIPTORS(x) (((unsigned int)x <= USB_DESCRIPTOR_TYPE_INTERFACE_POWER) ? \ |
| 61 | usbd_device_descriptors[x] : "UNKNOWN") |
| 62 | |
| 63 | __maybe_unused static char *usbd_device_states[] = { |
| 64 | "STATE_INIT", |
| 65 | "STATE_CREATED", |
| 66 | "STATE_ATTACHED", |
| 67 | "STATE_POWERED", |
| 68 | "STATE_DEFAULT", |
| 69 | "STATE_ADDRESSED", |
| 70 | "STATE_CONFIGURED", |
| 71 | "STATE_UNKNOWN", |
| 72 | }; |
| 73 | |
| 74 | #define USBD_DEVICE_STATE(x) (((unsigned int)x <= STATE_UNKNOWN) ? usbd_device_states[x] : "UNKNOWN") |
| 75 | |
| 76 | __maybe_unused static char *usbd_device_requests[] = { |
| 77 | "GET STATUS", /* 0 */ |
| 78 | "CLEAR FEATURE", /* 1 */ |
| 79 | "RESERVED", /* 2 */ |
| 80 | "SET FEATURE", /* 3 */ |
| 81 | "RESERVED", /* 4 */ |
| 82 | "SET ADDRESS", /* 5 */ |
| 83 | "GET DESCRIPTOR", /* 6 */ |
| 84 | "SET DESCRIPTOR", /* 7 */ |
| 85 | "GET CONFIGURATION", /* 8 */ |
| 86 | "SET CONFIGURATION", /* 9 */ |
| 87 | "GET INTERFACE", /* 10 */ |
| 88 | "SET INTERFACE", /* 11 */ |
| 89 | "SYNC FRAME", /* 12 */ |
| 90 | }; |
| 91 | |
| 92 | #define USBD_DEVICE_REQUESTS(x) (((unsigned int)x <= USB_REQ_SYNCH_FRAME) ? usbd_device_requests[x] : "UNKNOWN") |
| 93 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 94 | /* EP0 Configuration Set ********************************************************************* */ |
| 95 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 96 | /** |
| 97 | * ep0_get_status - fill in URB data with appropriate status |
| 98 | * @device: |
| 99 | * @urb: |
| 100 | * @index: |
| 101 | * @requesttype: |
| 102 | * |
| 103 | */ |
| 104 | static int ep0_get_status (struct usb_device_instance *device, |
| 105 | struct urb *urb, int index, int requesttype) |
| 106 | { |
| 107 | char *cp; |
| 108 | |
| 109 | urb->actual_length = 2; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 110 | cp = (char*)urb->buffer; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 111 | cp[0] = cp[1] = 0; |
| 112 | |
| 113 | switch (requesttype) { |
| 114 | case USB_REQ_RECIPIENT_DEVICE: |
| 115 | cp[0] = USB_STATUS_SELFPOWERED; |
| 116 | break; |
| 117 | case USB_REQ_RECIPIENT_INTERFACE: |
| 118 | break; |
| 119 | case USB_REQ_RECIPIENT_ENDPOINT: |
| 120 | cp[0] = usbd_endpoint_halted (device, index); |
| 121 | break; |
| 122 | case USB_REQ_RECIPIENT_OTHER: |
| 123 | urb->actual_length = 0; |
| 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | dbg_ep0 (2, "%02x %02x", cp[0], cp[1]); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * ep0_get_one |
| 133 | * @device: |
| 134 | * @urb: |
| 135 | * @result: |
| 136 | * |
| 137 | * Set a single byte value in the urb send buffer. Return non-zero to signal |
| 138 | * a request error. |
| 139 | */ |
| 140 | static int ep0_get_one (struct usb_device_instance *device, struct urb *urb, |
| 141 | __u8 result) |
| 142 | { |
| 143 | urb->actual_length = 1; /* XXX 2? */ |
| 144 | ((char *) urb->buffer)[0] = result; |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * copy_config |
| 150 | * @urb: pointer to urb |
| 151 | * @data: pointer to configuration data |
| 152 | * @length: length of data |
| 153 | * |
| 154 | * Copy configuration data to urb transfer buffer if there is room for it. |
| 155 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 156 | void copy_config (struct urb *urb, void *data, int max_length, |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 157 | int max_buf) |
| 158 | { |
| 159 | int available; |
| 160 | int length; |
| 161 | |
| 162 | /*dbg_ep0(3, "-> actual: %d buf: %d max_buf: %d max_length: %d data: %p", */ |
| 163 | /* urb->actual_length, urb->buffer_length, max_buf, max_length, data); */ |
| 164 | |
| 165 | if (!data) { |
| 166 | dbg_ep0 (1, "data is NULL"); |
| 167 | return; |
| 168 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 169 | length = max_length; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 170 | |
| 171 | if (length > max_length) { |
| 172 | dbg_ep0 (1, "length: %d >= max_length: %d", length, |
| 173 | max_length); |
| 174 | return; |
| 175 | } |
| 176 | /*dbg_ep0(1, " actual: %d buf: %d max_buf: %d max_length: %d length: %d", */ |
| 177 | /* urb->actual_length, urb->buffer_length, max_buf, max_length, length); */ |
| 178 | |
| 179 | if ((available = |
| 180 | /*urb->buffer_length */ max_buf - urb->actual_length) <= 0) { |
| 181 | return; |
| 182 | } |
| 183 | /*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */ |
| 184 | /* urb->actual_length, urb->buffer_length, max_buf, length, available); */ |
| 185 | |
| 186 | if (length > available) { |
| 187 | length = available; |
| 188 | } |
| 189 | /*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */ |
| 190 | /* urb->actual_length, urb->buffer_length, max_buf, length, available); */ |
| 191 | |
| 192 | memcpy (urb->buffer + urb->actual_length, data, length); |
| 193 | urb->actual_length += length; |
| 194 | |
| 195 | dbg_ep0 (3, |
| 196 | "copy_config: <- actual: %d buf: %d max_buf: %d max_length: %d available: %d", |
| 197 | urb->actual_length, urb->buffer_length, max_buf, max_length, |
| 198 | available); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * ep0_get_descriptor |
| 203 | * @device: |
| 204 | * @urb: |
| 205 | * @max: |
| 206 | * @descriptor_type: |
| 207 | * @index: |
| 208 | * |
| 209 | * Called by ep0_rx_process for a get descriptor device command. Determine what |
| 210 | * descriptor is being requested, copy to send buffer. Return zero if ok to send, |
| 211 | * return non-zero to signal a request error. |
| 212 | */ |
| 213 | static int ep0_get_descriptor (struct usb_device_instance *device, |
| 214 | struct urb *urb, int max, int descriptor_type, |
| 215 | int index) |
| 216 | { |
| 217 | int port = 0; /* XXX compound device */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 218 | |
| 219 | /*dbg_ep0(3, "max: %x type: %x index: %x", max, descriptor_type, index); */ |
| 220 | |
| 221 | if (!urb || !urb->buffer || !urb->buffer_length |
| 222 | || (urb->buffer_length < 255)) { |
| 223 | dbg_ep0 (2, "invalid urb %p", urb); |
| 224 | return -1L; |
| 225 | } |
| 226 | |
| 227 | /* setup tx urb */ |
| 228 | urb->actual_length = 0; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 229 | |
| 230 | dbg_ep0 (2, "%s", USBD_DEVICE_DESCRIPTORS (descriptor_type)); |
| 231 | |
| 232 | switch (descriptor_type) { |
| 233 | case USB_DESCRIPTOR_TYPE_DEVICE: |
| 234 | { |
| 235 | struct usb_device_descriptor *device_descriptor; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 236 | if (! |
| 237 | (device_descriptor = |
| 238 | usbd_device_device_descriptor (device, port))) { |
| 239 | return -1; |
| 240 | } |
| 241 | /* copy descriptor for this device */ |
| 242 | copy_config (urb, device_descriptor, |
| 243 | sizeof (struct usb_device_descriptor), |
| 244 | max); |
| 245 | |
| 246 | /* correct the correct control endpoint 0 max packet size into the descriptor */ |
| 247 | device_descriptor = |
| 248 | (struct usb_device_descriptor *) urb->buffer; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 249 | |
| 250 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 251 | dbg_ep0(3, "copied device configuration, actual_length: 0x%x", urb->actual_length); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 252 | break; |
| 253 | |
| 254 | case USB_DESCRIPTOR_TYPE_CONFIGURATION: |
| 255 | { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 256 | struct usb_configuration_descriptor |
| 257 | *configuration_descriptor; |
| 258 | struct usb_device_descriptor *device_descriptor; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 259 | if (! |
| 260 | (device_descriptor = |
| 261 | usbd_device_device_descriptor (device, port))) { |
| 262 | return -1; |
| 263 | } |
| 264 | /*dbg_ep0(2, "%d %d", index, device_descriptor->bNumConfigurations); */ |
Harald Welte | ed1c3f0 | 2008-07-07 00:58:05 +0800 | [diff] [blame] | 265 | if (index >= device_descriptor->bNumConfigurations) { |
| 266 | dbg_ep0 (0, "index too large: %d >= %d", index, |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 267 | device_descriptor-> |
| 268 | bNumConfigurations); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | if (! |
| 273 | (configuration_descriptor = |
| 274 | usbd_device_configuration_descriptor (device, |
| 275 | port, |
| 276 | index))) { |
| 277 | dbg_ep0 (0, |
| 278 | "usbd_device_configuration_descriptor failed: %d", |
| 279 | index); |
| 280 | return -1; |
| 281 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 282 | dbg_ep0(0, "attempt to copy %d bytes to urb\n",cpu_to_le16(configuration_descriptor->wTotalLength)); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 283 | copy_config (urb, configuration_descriptor, |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 284 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 285 | cpu_to_le16(configuration_descriptor->wTotalLength), |
| 286 | max); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 287 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 288 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 289 | break; |
| 290 | |
| 291 | case USB_DESCRIPTOR_TYPE_STRING: |
| 292 | { |
| 293 | struct usb_string_descriptor *string_descriptor; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 294 | if (!(string_descriptor = usbd_get_string (index))) { |
Pali Rohár | e467000 | 2021-02-07 14:50:11 +0100 | [diff] [blame] | 295 | dbg_ep0(0, "Invalid string index %d\n", index); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 296 | return -1; |
| 297 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 298 | dbg_ep0(3, "string_descriptor: %p length %d", string_descriptor, string_descriptor->bLength); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 299 | copy_config (urb, string_descriptor, string_descriptor->bLength, max); |
| 300 | } |
| 301 | break; |
| 302 | case USB_DESCRIPTOR_TYPE_INTERFACE: |
Pali Rohár | e467000 | 2021-02-07 14:50:11 +0100 | [diff] [blame] | 303 | dbg_ep0(2, "USB_DESCRIPTOR_TYPE_INTERFACE - error not implemented\n"); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 304 | return -1; |
| 305 | case USB_DESCRIPTOR_TYPE_ENDPOINT: |
Pali Rohár | e467000 | 2021-02-07 14:50:11 +0100 | [diff] [blame] | 306 | dbg_ep0(2, "USB_DESCRIPTOR_TYPE_ENDPOINT - error not implemented\n"); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 307 | return -1; |
| 308 | case USB_DESCRIPTOR_TYPE_HID: |
| 309 | { |
Pali Rohár | e467000 | 2021-02-07 14:50:11 +0100 | [diff] [blame] | 310 | dbg_ep0(2, "USB_DESCRIPTOR_TYPE_HID - error not implemented\n"); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 311 | return -1; /* unsupported at this time */ |
| 312 | #if 0 |
| 313 | int bNumInterface = |
| 314 | le16_to_cpu (urb->device_request.wIndex); |
| 315 | int bAlternateSetting = 0; |
| 316 | int class = 0; |
| 317 | struct usb_class_descriptor *class_descriptor; |
| 318 | |
| 319 | if (!(class_descriptor = |
| 320 | usbd_device_class_descriptor_index (device, |
| 321 | port, 0, |
| 322 | bNumInterface, |
| 323 | bAlternateSetting, |
| 324 | class)) |
| 325 | || class_descriptor->descriptor.hid.bDescriptorType != USB_DT_HID) { |
| 326 | dbg_ep0 (3, "[%d] interface is not HID", |
| 327 | bNumInterface); |
| 328 | return -1; |
| 329 | } |
| 330 | /* copy descriptor for this class */ |
| 331 | copy_config (urb, class_descriptor, |
| 332 | class_descriptor->descriptor.hid.bLength, |
| 333 | max); |
| 334 | #endif |
| 335 | } |
| 336 | break; |
| 337 | case USB_DESCRIPTOR_TYPE_REPORT: |
| 338 | { |
Pali Rohár | e467000 | 2021-02-07 14:50:11 +0100 | [diff] [blame] | 339 | dbg_ep0(2, "USB_DESCRIPTOR_TYPE_REPORT - error not implemented\n"); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 340 | return -1; /* unsupported at this time */ |
| 341 | #if 0 |
| 342 | int bNumInterface = |
| 343 | le16_to_cpu (urb->device_request.wIndex); |
| 344 | int bAlternateSetting = 0; |
| 345 | int class = 0; |
| 346 | struct usb_class_report_descriptor *report_descriptor; |
| 347 | |
| 348 | if (!(report_descriptor = |
| 349 | usbd_device_class_report_descriptor_index |
| 350 | (device, port, 0, bNumInterface, |
| 351 | bAlternateSetting, class)) |
| 352 | || report_descriptor->bDescriptorType != |
| 353 | USB_DT_REPORT) { |
| 354 | dbg_ep0 (3, "[%d] descriptor is not REPORT", |
| 355 | bNumInterface); |
| 356 | return -1; |
| 357 | } |
| 358 | /* copy report descriptor for this class */ |
| 359 | /*copy_config(urb, &report_descriptor->bData[0], report_descriptor->wLength, max); */ |
| 360 | if (max - urb->actual_length > 0) { |
| 361 | int length = |
Masahiro Yamada | b62b39b | 2014-09-18 13:28:06 +0900 | [diff] [blame] | 362 | min(report_descriptor->wLength, |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 363 | max - urb->actual_length); |
| 364 | memcpy (urb->buffer + urb->actual_length, |
| 365 | &report_descriptor->bData[0], length); |
| 366 | urb->actual_length += length; |
| 367 | } |
| 368 | #endif |
| 369 | } |
| 370 | break; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 371 | case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER: |
Vipin KUMAR | bdb1770 | 2012-03-26 15:38:06 +0530 | [diff] [blame] | 372 | return -1; |
Vipin KUMAR | bdb1770 | 2012-03-26 15:38:06 +0530 | [diff] [blame] | 373 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 374 | default: |
| 375 | return -1; |
| 376 | } |
| 377 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 378 | dbg_ep0 (1, "urb: buffer: %p buffer_length: %2d actual_length: %2d tx_packetSize: %2d", |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 379 | urb->buffer, urb->buffer_length, urb->actual_length, |
| 380 | device->bus->endpoint_array[0].tx_packetSize); |
| 381 | /* |
| 382 | if ((urb->actual_length < max) && !(urb->actual_length % device->bus->endpoint_array[0].tx_packetSize)) { |
| 383 | dbg_ep0(0, "adding null byte"); |
| 384 | urb->buffer[urb->actual_length++] = 0; |
| 385 | dbg_ep0(0, "urb: buffer_length: %2d actual_length: %2d packet size: %2d", |
| 386 | urb->buffer_length, urb->actual_length device->bus->endpoint_array[0].tx_packetSize); |
| 387 | } |
| 388 | */ |
| 389 | return 0; |
| 390 | |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * ep0_recv_setup - called to indicate URB has been received |
| 395 | * @urb: pointer to struct urb |
| 396 | * |
| 397 | * Check if this is a setup packet, process the device request, put results |
| 398 | * back into the urb and return zero or non-zero to indicate success (DATA) |
| 399 | * or failure (STALL). |
| 400 | * |
| 401 | */ |
| 402 | int ep0_recv_setup (struct urb *urb) |
| 403 | { |
| 404 | /*struct usb_device_request *request = urb->buffer; */ |
| 405 | /*struct usb_device_instance *device = urb->device; */ |
| 406 | |
| 407 | struct usb_device_request *request; |
| 408 | struct usb_device_instance *device; |
| 409 | int address; |
| 410 | |
| 411 | dbg_ep0 (0, "entering ep0_recv_setup()"); |
| 412 | if (!urb || !urb->device) { |
| 413 | dbg_ep0 (3, "invalid URB %p", urb); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | request = &urb->device_request; |
| 418 | device = urb->device; |
| 419 | |
| 420 | dbg_ep0 (3, "urb: %p device: %p", urb, urb->device); |
| 421 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 422 | /*dbg_ep0(2, "- - - - - - - - - -"); */ |
| 423 | |
| 424 | dbg_ep0 (2, |
| 425 | "bmRequestType:%02x bRequest:%02x wValue:%04x wIndex:%04x wLength:%04x %s", |
| 426 | request->bmRequestType, request->bRequest, |
| 427 | le16_to_cpu (request->wValue), le16_to_cpu (request->wIndex), |
| 428 | le16_to_cpu (request->wLength), |
| 429 | USBD_DEVICE_REQUESTS (request->bRequest)); |
| 430 | |
| 431 | /* handle USB Standard Request (c.f. USB Spec table 9-2) */ |
| 432 | if ((request->bmRequestType & USB_REQ_TYPE_MASK) != 0) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 433 | if(device->device_state <= STATE_CONFIGURED){ |
| 434 | /* Attempt to handle a CDC specific request if we are |
| 435 | * in the configured state. |
| 436 | */ |
| 437 | return device->cdc_recv_setup(request,urb); |
| 438 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 439 | dbg_ep0 (1, "non standard request: %x", |
| 440 | request->bmRequestType & USB_REQ_TYPE_MASK); |
| 441 | return -1; /* Stall here */ |
| 442 | } |
| 443 | |
| 444 | switch (device->device_state) { |
| 445 | case STATE_CREATED: |
| 446 | case STATE_ATTACHED: |
| 447 | case STATE_POWERED: |
| 448 | /* It actually is important to allow requests in these states, |
| 449 | * Windows will request descriptors before assigning an |
| 450 | * address to the client. |
| 451 | */ |
| 452 | |
| 453 | /*dbg_ep0 (1, "request %s not allowed in this state: %s", */ |
| 454 | /* USBD_DEVICE_REQUESTS(request->bRequest), */ |
| 455 | /* usbd_device_states[device->device_state]); */ |
| 456 | /*return -1; */ |
| 457 | break; |
| 458 | |
| 459 | case STATE_INIT: |
| 460 | case STATE_DEFAULT: |
| 461 | switch (request->bRequest) { |
| 462 | case USB_REQ_GET_STATUS: |
| 463 | case USB_REQ_GET_INTERFACE: |
| 464 | case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */ |
| 465 | case USB_REQ_CLEAR_FEATURE: |
| 466 | case USB_REQ_SET_FEATURE: |
| 467 | case USB_REQ_SET_DESCRIPTOR: |
| 468 | /* case USB_REQ_SET_CONFIGURATION: */ |
| 469 | case USB_REQ_SET_INTERFACE: |
| 470 | dbg_ep0 (1, |
| 471 | "request %s not allowed in DEFAULT state: %s", |
| 472 | USBD_DEVICE_REQUESTS (request->bRequest), |
| 473 | usbd_device_states[device->device_state]); |
| 474 | return -1; |
| 475 | |
| 476 | case USB_REQ_SET_CONFIGURATION: |
| 477 | case USB_REQ_SET_ADDRESS: |
| 478 | case USB_REQ_GET_DESCRIPTOR: |
| 479 | case USB_REQ_GET_CONFIGURATION: |
| 480 | break; |
| 481 | } |
| 482 | case STATE_ADDRESSED: |
| 483 | case STATE_CONFIGURED: |
| 484 | break; |
| 485 | case STATE_UNKNOWN: |
| 486 | dbg_ep0 (1, "request %s not allowed in UNKNOWN state: %s", |
| 487 | USBD_DEVICE_REQUESTS (request->bRequest), |
| 488 | usbd_device_states[device->device_state]); |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | /* handle all requests that return data (direction bit set on bm RequestType) */ |
| 493 | if ((request->bmRequestType & USB_REQ_DIRECTION_MASK)) { |
| 494 | |
| 495 | dbg_ep0 (3, "Device-to-Host"); |
| 496 | |
| 497 | switch (request->bRequest) { |
| 498 | |
| 499 | case USB_REQ_GET_STATUS: |
| 500 | return ep0_get_status (device, urb, request->wIndex, |
| 501 | request->bmRequestType & |
| 502 | USB_REQ_RECIPIENT_MASK); |
| 503 | |
| 504 | case USB_REQ_GET_DESCRIPTOR: |
| 505 | return ep0_get_descriptor (device, urb, |
| 506 | le16_to_cpu (request->wLength), |
| 507 | le16_to_cpu (request->wValue) >> 8, |
| 508 | le16_to_cpu (request->wValue) & 0xff); |
| 509 | |
| 510 | case USB_REQ_GET_CONFIGURATION: |
Pali Rohár | e467000 | 2021-02-07 14:50:11 +0100 | [diff] [blame] | 511 | dbg_ep0(2, "get config %d\n", device->configuration); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 512 | return ep0_get_one (device, urb, |
| 513 | device->configuration); |
| 514 | |
| 515 | case USB_REQ_GET_INTERFACE: |
| 516 | return ep0_get_one (device, urb, device->alternate); |
| 517 | |
| 518 | case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */ |
| 519 | return -1; |
| 520 | |
| 521 | case USB_REQ_CLEAR_FEATURE: |
| 522 | case USB_REQ_SET_FEATURE: |
| 523 | case USB_REQ_SET_ADDRESS: |
| 524 | case USB_REQ_SET_DESCRIPTOR: |
| 525 | case USB_REQ_SET_CONFIGURATION: |
| 526 | case USB_REQ_SET_INTERFACE: |
| 527 | return -1; |
| 528 | } |
| 529 | } |
| 530 | /* handle the requests that do not return data */ |
| 531 | else { |
| 532 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 533 | /*dbg_ep0(3, "Host-to-Device"); */ |
| 534 | switch (request->bRequest) { |
| 535 | |
| 536 | case USB_REQ_CLEAR_FEATURE: |
| 537 | case USB_REQ_SET_FEATURE: |
| 538 | dbg_ep0 (0, "Host-to-Device"); |
| 539 | switch (request-> |
| 540 | bmRequestType & USB_REQ_RECIPIENT_MASK) { |
| 541 | case USB_REQ_RECIPIENT_DEVICE: |
| 542 | /* XXX DEVICE_REMOTE_WAKEUP or TEST_MODE would be added here */ |
| 543 | /* XXX fall through for now as we do not support either */ |
| 544 | case USB_REQ_RECIPIENT_INTERFACE: |
| 545 | case USB_REQ_RECIPIENT_OTHER: |
| 546 | dbg_ep0 (0, "request %s not", |
| 547 | USBD_DEVICE_REQUESTS (request->bRequest)); |
| 548 | default: |
| 549 | return -1; |
| 550 | |
| 551 | case USB_REQ_RECIPIENT_ENDPOINT: |
| 552 | dbg_ep0 (0, "ENDPOINT: %x", le16_to_cpu (request->wValue)); |
| 553 | if (le16_to_cpu (request->wValue) == USB_ENDPOINT_HALT) { |
| 554 | /*return usbd_device_feature (device, le16_to_cpu (request->wIndex), */ |
| 555 | /* request->bRequest == USB_REQ_SET_FEATURE); */ |
| 556 | /* NEED TO IMPLEMENT THIS!!! */ |
| 557 | return -1; |
| 558 | } else { |
| 559 | dbg_ep0 (1, "request %s bad wValue: %04x", |
| 560 | USBD_DEVICE_REQUESTS |
| 561 | (request->bRequest), |
| 562 | le16_to_cpu (request->wValue)); |
| 563 | return -1; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | case USB_REQ_SET_ADDRESS: |
| 568 | /* check if this is a re-address, reset first if it is (this shouldn't be possible) */ |
| 569 | if (device->device_state != STATE_DEFAULT) { |
| 570 | dbg_ep0 (1, "set_address: %02x state: %s", |
| 571 | le16_to_cpu (request->wValue), |
| 572 | usbd_device_states[device->device_state]); |
| 573 | return -1; |
| 574 | } |
| 575 | address = le16_to_cpu (request->wValue); |
| 576 | if ((address & 0x7f) != address) { |
| 577 | dbg_ep0 (1, "invalid address %04x %04x", |
| 578 | address, address & 0x7f); |
| 579 | return -1; |
| 580 | } |
| 581 | device->address = address; |
| 582 | |
| 583 | /*dbg_ep0(2, "address: %d %d %d", */ |
| 584 | /* request->wValue, le16_to_cpu(request->wValue), device->address); */ |
| 585 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 586 | return 0; |
| 587 | |
| 588 | case USB_REQ_SET_DESCRIPTOR: /* XXX should we support this? */ |
| 589 | dbg_ep0 (0, "set descriptor: NOT SUPPORTED"); |
| 590 | return -1; |
| 591 | |
| 592 | case USB_REQ_SET_CONFIGURATION: |
| 593 | /* c.f. 9.4.7 - the top half of wValue is reserved */ |
Harald Welte | ed1c3f0 | 2008-07-07 00:58:05 +0800 | [diff] [blame] | 594 | device->configuration = le16_to_cpu(request->wValue) & 0xff; |
| 595 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 596 | /* reset interface and alternate settings */ |
| 597 | device->interface = device->alternate = 0; |
| 598 | |
| 599 | /*dbg_ep0(2, "set configuration: %d", device->configuration); */ |
Pali Rohár | e467000 | 2021-02-07 14:50:11 +0100 | [diff] [blame] | 600 | /*dbg_ep0(2, "DEVICE_CONFIGURED.. event?\n"); */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 601 | return 0; |
| 602 | |
| 603 | case USB_REQ_SET_INTERFACE: |
| 604 | device->interface = le16_to_cpu (request->wIndex); |
| 605 | device->alternate = le16_to_cpu (request->wValue); |
| 606 | /*dbg_ep0(2, "set interface: %d alternate: %d", device->interface, device->alternate); */ |
Pali Rohár | e467000 | 2021-02-07 14:50:11 +0100 | [diff] [blame] | 607 | dbg_ep0(2, "DEVICE_SET_INTERFACE.. event?\n"); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 608 | return 0; |
| 609 | |
| 610 | case USB_REQ_GET_STATUS: |
| 611 | case USB_REQ_GET_DESCRIPTOR: |
| 612 | case USB_REQ_GET_CONFIGURATION: |
| 613 | case USB_REQ_GET_INTERFACE: |
| 614 | case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */ |
| 615 | return -1; |
| 616 | } |
| 617 | } |
| 618 | return -1; |
| 619 | } |