ep0.c 21 KB

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