ep0.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. char *cp;
  170. /*dbg_ep0(3, "max: %x type: %x index: %x", max, descriptor_type, index); */
  171. if (!urb || !urb->buffer || !urb->buffer_length
  172. || (urb->buffer_length < 255)) {
  173. dbg_ep0 (2, "invalid urb %p", urb);
  174. return -1L;
  175. }
  176. /* setup tx urb */
  177. urb->actual_length = 0;
  178. cp = (char*)urb->buffer;
  179. dbg_ep0 (2, "%s", USBD_DEVICE_DESCRIPTORS (descriptor_type));
  180. switch (descriptor_type) {
  181. case USB_DESCRIPTOR_TYPE_DEVICE:
  182. {
  183. struct usb_device_descriptor *device_descriptor;
  184. if (!
  185. (device_descriptor =
  186. usbd_device_device_descriptor (device, port))) {
  187. return -1;
  188. }
  189. /* copy descriptor for this device */
  190. copy_config (urb, device_descriptor,
  191. sizeof (struct usb_device_descriptor),
  192. max);
  193. /* correct the correct control endpoint 0 max packet size into the descriptor */
  194. device_descriptor =
  195. (struct usb_device_descriptor *) urb->buffer;
  196. }
  197. dbg_ep0(3, "copied device configuration, actual_length: 0x%x", urb->actual_length);
  198. break;
  199. case USB_DESCRIPTOR_TYPE_CONFIGURATION:
  200. {
  201. struct usb_configuration_descriptor
  202. *configuration_descriptor;
  203. struct usb_device_descriptor *device_descriptor;
  204. if (!
  205. (device_descriptor =
  206. usbd_device_device_descriptor (device, port))) {
  207. return -1;
  208. }
  209. /*dbg_ep0(2, "%d %d", index, device_descriptor->bNumConfigurations); */
  210. if (index >= device_descriptor->bNumConfigurations) {
  211. dbg_ep0 (0, "index too large: %d >= %d", index,
  212. device_descriptor->
  213. bNumConfigurations);
  214. return -1;
  215. }
  216. if (!
  217. (configuration_descriptor =
  218. usbd_device_configuration_descriptor (device,
  219. port,
  220. index))) {
  221. dbg_ep0 (0,
  222. "usbd_device_configuration_descriptor failed: %d",
  223. index);
  224. return -1;
  225. }
  226. dbg_ep0(0, "attempt to copy %d bytes to urb\n",cpu_to_le16(configuration_descriptor->wTotalLength));
  227. copy_config (urb, configuration_descriptor,
  228. cpu_to_le16(configuration_descriptor->wTotalLength),
  229. max);
  230. }
  231. break;
  232. case USB_DESCRIPTOR_TYPE_STRING:
  233. {
  234. struct usb_string_descriptor *string_descriptor;
  235. if (!(string_descriptor = usbd_get_string (index))) {
  236. serial_printf("Invalid string index %d\n", index);
  237. return -1;
  238. }
  239. dbg_ep0(3, "string_descriptor: %p length %d", string_descriptor, string_descriptor->bLength);
  240. copy_config (urb, string_descriptor, string_descriptor->bLength, max);
  241. }
  242. break;
  243. case USB_DESCRIPTOR_TYPE_INTERFACE:
  244. serial_printf("USB_DESCRIPTOR_TYPE_INTERFACE - error not implemented\n");
  245. return -1;
  246. case USB_DESCRIPTOR_TYPE_ENDPOINT:
  247. serial_printf("USB_DESCRIPTOR_TYPE_ENDPOINT - error not implemented\n");
  248. return -1;
  249. case USB_DESCRIPTOR_TYPE_HID:
  250. {
  251. serial_printf("USB_DESCRIPTOR_TYPE_HID - error not implemented\n");
  252. return -1; /* unsupported at this time */
  253. #if 0
  254. int bNumInterface =
  255. le16_to_cpu (urb->device_request.wIndex);
  256. int bAlternateSetting = 0;
  257. int class = 0;
  258. struct usb_class_descriptor *class_descriptor;
  259. if (!(class_descriptor =
  260. usbd_device_class_descriptor_index (device,
  261. port, 0,
  262. bNumInterface,
  263. bAlternateSetting,
  264. class))
  265. || class_descriptor->descriptor.hid.bDescriptorType != USB_DT_HID) {
  266. dbg_ep0 (3, "[%d] interface is not HID",
  267. bNumInterface);
  268. return -1;
  269. }
  270. /* copy descriptor for this class */
  271. copy_config (urb, class_descriptor,
  272. class_descriptor->descriptor.hid.bLength,
  273. max);
  274. #endif
  275. }
  276. break;
  277. case USB_DESCRIPTOR_TYPE_REPORT:
  278. {
  279. serial_printf("USB_DESCRIPTOR_TYPE_REPORT - error not implemented\n");
  280. return -1; /* unsupported at this time */
  281. #if 0
  282. int bNumInterface =
  283. le16_to_cpu (urb->device_request.wIndex);
  284. int bAlternateSetting = 0;
  285. int class = 0;
  286. struct usb_class_report_descriptor *report_descriptor;
  287. if (!(report_descriptor =
  288. usbd_device_class_report_descriptor_index
  289. (device, port, 0, bNumInterface,
  290. bAlternateSetting, class))
  291. || report_descriptor->bDescriptorType !=
  292. USB_DT_REPORT) {
  293. dbg_ep0 (3, "[%d] descriptor is not REPORT",
  294. bNumInterface);
  295. return -1;
  296. }
  297. /* copy report descriptor for this class */
  298. /*copy_config(urb, &report_descriptor->bData[0], report_descriptor->wLength, max); */
  299. if (max - urb->actual_length > 0) {
  300. int length =
  301. MIN (report_descriptor->wLength,
  302. max - urb->actual_length);
  303. memcpy (urb->buffer + urb->actual_length,
  304. &report_descriptor->bData[0], length);
  305. urb->actual_length += length;
  306. }
  307. #endif
  308. }
  309. break;
  310. case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
  311. {
  312. /* If a USB device supports both a full speed and low speed operation
  313. * we must send a Device_Qualifier descriptor here
  314. */
  315. return -1;
  316. }
  317. default:
  318. return -1;
  319. }
  320. dbg_ep0 (1, "urb: buffer: %p buffer_length: %2d actual_length: %2d tx_packetSize: %2d",
  321. urb->buffer, urb->buffer_length, urb->actual_length,
  322. device->bus->endpoint_array[0].tx_packetSize);
  323. /*
  324. if ((urb->actual_length < max) && !(urb->actual_length % device->bus->endpoint_array[0].tx_packetSize)) {
  325. dbg_ep0(0, "adding null byte");
  326. urb->buffer[urb->actual_length++] = 0;
  327. dbg_ep0(0, "urb: buffer_length: %2d actual_length: %2d packet size: %2d",
  328. urb->buffer_length, urb->actual_length device->bus->endpoint_array[0].tx_packetSize);
  329. }
  330. */
  331. return 0;
  332. }
  333. /**
  334. * ep0_recv_setup - called to indicate URB has been received
  335. * @urb: pointer to struct urb
  336. *
  337. * Check if this is a setup packet, process the device request, put results
  338. * back into the urb and return zero or non-zero to indicate success (DATA)
  339. * or failure (STALL).
  340. *
  341. */
  342. int ep0_recv_setup (struct urb *urb)
  343. {
  344. /*struct usb_device_request *request = urb->buffer; */
  345. /*struct usb_device_instance *device = urb->device; */
  346. struct usb_device_request *request;
  347. struct usb_device_instance *device;
  348. int address;
  349. dbg_ep0 (0, "entering ep0_recv_setup()");
  350. if (!urb || !urb->device) {
  351. dbg_ep0 (3, "invalid URB %p", urb);
  352. return -1;
  353. }
  354. request = &urb->device_request;
  355. device = urb->device;
  356. dbg_ep0 (3, "urb: %p device: %p", urb, urb->device);
  357. /*dbg_ep0(2, "- - - - - - - - - -"); */
  358. dbg_ep0 (2,
  359. "bmRequestType:%02x bRequest:%02x wValue:%04x wIndex:%04x wLength:%04x %s",
  360. request->bmRequestType, request->bRequest,
  361. le16_to_cpu (request->wValue), le16_to_cpu (request->wIndex),
  362. le16_to_cpu (request->wLength),
  363. USBD_DEVICE_REQUESTS (request->bRequest));
  364. /* handle USB Standard Request (c.f. USB Spec table 9-2) */
  365. if ((request->bmRequestType & USB_REQ_TYPE_MASK) != 0) {
  366. if(device->device_state <= STATE_CONFIGURED){
  367. /* Attempt to handle a CDC specific request if we are
  368. * in the configured state.
  369. */
  370. return device->cdc_recv_setup(request,urb);
  371. }
  372. dbg_ep0 (1, "non standard request: %x",
  373. request->bmRequestType & USB_REQ_TYPE_MASK);
  374. return -1; /* Stall here */
  375. }
  376. switch (device->device_state) {
  377. case STATE_CREATED:
  378. case STATE_ATTACHED:
  379. case STATE_POWERED:
  380. /* It actually is important to allow requests in these states,
  381. * Windows will request descriptors before assigning an
  382. * address to the client.
  383. */
  384. /*dbg_ep0 (1, "request %s not allowed in this state: %s", */
  385. /* USBD_DEVICE_REQUESTS(request->bRequest), */
  386. /* usbd_device_states[device->device_state]); */
  387. /*return -1; */
  388. break;
  389. case STATE_INIT:
  390. case STATE_DEFAULT:
  391. switch (request->bRequest) {
  392. case USB_REQ_GET_STATUS:
  393. case USB_REQ_GET_INTERFACE:
  394. case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
  395. case USB_REQ_CLEAR_FEATURE:
  396. case USB_REQ_SET_FEATURE:
  397. case USB_REQ_SET_DESCRIPTOR:
  398. /* case USB_REQ_SET_CONFIGURATION: */
  399. case USB_REQ_SET_INTERFACE:
  400. dbg_ep0 (1,
  401. "request %s not allowed in DEFAULT state: %s",
  402. USBD_DEVICE_REQUESTS (request->bRequest),
  403. usbd_device_states[device->device_state]);
  404. return -1;
  405. case USB_REQ_SET_CONFIGURATION:
  406. case USB_REQ_SET_ADDRESS:
  407. case USB_REQ_GET_DESCRIPTOR:
  408. case USB_REQ_GET_CONFIGURATION:
  409. break;
  410. }
  411. case STATE_ADDRESSED:
  412. case STATE_CONFIGURED:
  413. break;
  414. case STATE_UNKNOWN:
  415. dbg_ep0 (1, "request %s not allowed in UNKNOWN state: %s",
  416. USBD_DEVICE_REQUESTS (request->bRequest),
  417. usbd_device_states[device->device_state]);
  418. return -1;
  419. }
  420. /* handle all requests that return data (direction bit set on bm RequestType) */
  421. if ((request->bmRequestType & USB_REQ_DIRECTION_MASK)) {
  422. dbg_ep0 (3, "Device-to-Host");
  423. switch (request->bRequest) {
  424. case USB_REQ_GET_STATUS:
  425. return ep0_get_status (device, urb, request->wIndex,
  426. request->bmRequestType &
  427. USB_REQ_RECIPIENT_MASK);
  428. case USB_REQ_GET_DESCRIPTOR:
  429. return ep0_get_descriptor (device, urb,
  430. le16_to_cpu (request->wLength),
  431. le16_to_cpu (request->wValue) >> 8,
  432. le16_to_cpu (request->wValue) & 0xff);
  433. case USB_REQ_GET_CONFIGURATION:
  434. serial_printf("get config %d\n", device->configuration);
  435. return ep0_get_one (device, urb,
  436. device->configuration);
  437. case USB_REQ_GET_INTERFACE:
  438. return ep0_get_one (device, urb, device->alternate);
  439. case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
  440. return -1;
  441. case USB_REQ_CLEAR_FEATURE:
  442. case USB_REQ_SET_FEATURE:
  443. case USB_REQ_SET_ADDRESS:
  444. case USB_REQ_SET_DESCRIPTOR:
  445. case USB_REQ_SET_CONFIGURATION:
  446. case USB_REQ_SET_INTERFACE:
  447. return -1;
  448. }
  449. }
  450. /* handle the requests that do not return data */
  451. else {
  452. /*dbg_ep0(3, "Host-to-Device"); */
  453. switch (request->bRequest) {
  454. case USB_REQ_CLEAR_FEATURE:
  455. case USB_REQ_SET_FEATURE:
  456. dbg_ep0 (0, "Host-to-Device");
  457. switch (request->
  458. bmRequestType & USB_REQ_RECIPIENT_MASK) {
  459. case USB_REQ_RECIPIENT_DEVICE:
  460. /* XXX DEVICE_REMOTE_WAKEUP or TEST_MODE would be added here */
  461. /* XXX fall through for now as we do not support either */
  462. case USB_REQ_RECIPIENT_INTERFACE:
  463. case USB_REQ_RECIPIENT_OTHER:
  464. dbg_ep0 (0, "request %s not",
  465. USBD_DEVICE_REQUESTS (request->bRequest));
  466. default:
  467. return -1;
  468. case USB_REQ_RECIPIENT_ENDPOINT:
  469. dbg_ep0 (0, "ENDPOINT: %x", le16_to_cpu (request->wValue));
  470. if (le16_to_cpu (request->wValue) == USB_ENDPOINT_HALT) {
  471. /*return usbd_device_feature (device, le16_to_cpu (request->wIndex), */
  472. /* request->bRequest == USB_REQ_SET_FEATURE); */
  473. /* NEED TO IMPLEMENT THIS!!! */
  474. return -1;
  475. } else {
  476. dbg_ep0 (1, "request %s bad wValue: %04x",
  477. USBD_DEVICE_REQUESTS
  478. (request->bRequest),
  479. le16_to_cpu (request->wValue));
  480. return -1;
  481. }
  482. }
  483. case USB_REQ_SET_ADDRESS:
  484. /* check if this is a re-address, reset first if it is (this shouldn't be possible) */
  485. if (device->device_state != STATE_DEFAULT) {
  486. dbg_ep0 (1, "set_address: %02x state: %s",
  487. le16_to_cpu (request->wValue),
  488. usbd_device_states[device->device_state]);
  489. return -1;
  490. }
  491. address = le16_to_cpu (request->wValue);
  492. if ((address & 0x7f) != address) {
  493. dbg_ep0 (1, "invalid address %04x %04x",
  494. address, address & 0x7f);
  495. return -1;
  496. }
  497. device->address = address;
  498. /*dbg_ep0(2, "address: %d %d %d", */
  499. /* request->wValue, le16_to_cpu(request->wValue), device->address); */
  500. return 0;
  501. case USB_REQ_SET_DESCRIPTOR: /* XXX should we support this? */
  502. dbg_ep0 (0, "set descriptor: NOT SUPPORTED");
  503. return -1;
  504. case USB_REQ_SET_CONFIGURATION:
  505. /* c.f. 9.4.7 - the top half of wValue is reserved */
  506. device->configuration = le16_to_cpu(request->wValue) & 0xff;
  507. /* reset interface and alternate settings */
  508. device->interface = device->alternate = 0;
  509. /*dbg_ep0(2, "set configuration: %d", device->configuration); */
  510. /*serial_printf("DEVICE_CONFIGURED.. event?\n"); */
  511. return 0;
  512. case USB_REQ_SET_INTERFACE:
  513. device->interface = le16_to_cpu (request->wIndex);
  514. device->alternate = le16_to_cpu (request->wValue);
  515. /*dbg_ep0(2, "set interface: %d alternate: %d", device->interface, device->alternate); */
  516. serial_printf ("DEVICE_SET_INTERFACE.. event?\n");
  517. return 0;
  518. case USB_REQ_GET_STATUS:
  519. case USB_REQ_GET_DESCRIPTOR:
  520. case USB_REQ_GET_CONFIGURATION:
  521. case USB_REQ_GET_INTERFACE:
  522. case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
  523. return -1;
  524. }
  525. }
  526. return -1;
  527. }