usbdcore_ep0.c 17 KB

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