ep0.c 17 KB

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