ep0.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. {
  310. /* If a USB device supports both a full speed and low speed operation
  311. * we must send a Device_Qualifier descriptor here
  312. */
  313. return -1;
  314. }
  315. default:
  316. return -1;
  317. }
  318. dbg_ep0 (1, "urb: buffer: %p buffer_length: %2d actual_length: %2d tx_packetSize: %2d",
  319. urb->buffer, urb->buffer_length, urb->actual_length,
  320. device->bus->endpoint_array[0].tx_packetSize);
  321. /*
  322. if ((urb->actual_length < max) && !(urb->actual_length % device->bus->endpoint_array[0].tx_packetSize)) {
  323. dbg_ep0(0, "adding null byte");
  324. urb->buffer[urb->actual_length++] = 0;
  325. dbg_ep0(0, "urb: buffer_length: %2d actual_length: %2d packet size: %2d",
  326. urb->buffer_length, urb->actual_length device->bus->endpoint_array[0].tx_packetSize);
  327. }
  328. */
  329. return 0;
  330. }
  331. /**
  332. * ep0_recv_setup - called to indicate URB has been received
  333. * @urb: pointer to struct urb
  334. *
  335. * Check if this is a setup packet, process the device request, put results
  336. * back into the urb and return zero or non-zero to indicate success (DATA)
  337. * or failure (STALL).
  338. *
  339. */
  340. int ep0_recv_setup (struct urb *urb)
  341. {
  342. /*struct usb_device_request *request = urb->buffer; */
  343. /*struct usb_device_instance *device = urb->device; */
  344. struct usb_device_request *request;
  345. struct usb_device_instance *device;
  346. int address;
  347. dbg_ep0 (0, "entering ep0_recv_setup()");
  348. if (!urb || !urb->device) {
  349. dbg_ep0 (3, "invalid URB %p", urb);
  350. return -1;
  351. }
  352. request = &urb->device_request;
  353. device = urb->device;
  354. dbg_ep0 (3, "urb: %p device: %p", urb, urb->device);
  355. /*dbg_ep0(2, "- - - - - - - - - -"); */
  356. dbg_ep0 (2,
  357. "bmRequestType:%02x bRequest:%02x wValue:%04x wIndex:%04x wLength:%04x %s",
  358. request->bmRequestType, request->bRequest,
  359. le16_to_cpu (request->wValue), le16_to_cpu (request->wIndex),
  360. le16_to_cpu (request->wLength),
  361. USBD_DEVICE_REQUESTS (request->bRequest));
  362. /* handle USB Standard Request (c.f. USB Spec table 9-2) */
  363. if ((request->bmRequestType & USB_REQ_TYPE_MASK) != 0) {
  364. if(device->device_state <= STATE_CONFIGURED){
  365. /* Attempt to handle a CDC specific request if we are
  366. * in the configured state.
  367. */
  368. return device->cdc_recv_setup(request,urb);
  369. }
  370. dbg_ep0 (1, "non standard request: %x",
  371. request->bmRequestType & USB_REQ_TYPE_MASK);
  372. return -1; /* Stall here */
  373. }
  374. switch (device->device_state) {
  375. case STATE_CREATED:
  376. case STATE_ATTACHED:
  377. case STATE_POWERED:
  378. /* It actually is important to allow requests in these states,
  379. * Windows will request descriptors before assigning an
  380. * address to the client.
  381. */
  382. /*dbg_ep0 (1, "request %s not allowed in this state: %s", */
  383. /* USBD_DEVICE_REQUESTS(request->bRequest), */
  384. /* usbd_device_states[device->device_state]); */
  385. /*return -1; */
  386. break;
  387. case STATE_INIT:
  388. case STATE_DEFAULT:
  389. switch (request->bRequest) {
  390. case USB_REQ_GET_STATUS:
  391. case USB_REQ_GET_INTERFACE:
  392. case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
  393. case USB_REQ_CLEAR_FEATURE:
  394. case USB_REQ_SET_FEATURE:
  395. case USB_REQ_SET_DESCRIPTOR:
  396. /* case USB_REQ_SET_CONFIGURATION: */
  397. case USB_REQ_SET_INTERFACE:
  398. dbg_ep0 (1,
  399. "request %s not allowed in DEFAULT state: %s",
  400. USBD_DEVICE_REQUESTS (request->bRequest),
  401. usbd_device_states[device->device_state]);
  402. return -1;
  403. case USB_REQ_SET_CONFIGURATION:
  404. case USB_REQ_SET_ADDRESS:
  405. case USB_REQ_GET_DESCRIPTOR:
  406. case USB_REQ_GET_CONFIGURATION:
  407. break;
  408. }
  409. case STATE_ADDRESSED:
  410. case STATE_CONFIGURED:
  411. break;
  412. case STATE_UNKNOWN:
  413. dbg_ep0 (1, "request %s not allowed in UNKNOWN state: %s",
  414. USBD_DEVICE_REQUESTS (request->bRequest),
  415. usbd_device_states[device->device_state]);
  416. return -1;
  417. }
  418. /* handle all requests that return data (direction bit set on bm RequestType) */
  419. if ((request->bmRequestType & USB_REQ_DIRECTION_MASK)) {
  420. dbg_ep0 (3, "Device-to-Host");
  421. switch (request->bRequest) {
  422. case USB_REQ_GET_STATUS:
  423. return ep0_get_status (device, urb, request->wIndex,
  424. request->bmRequestType &
  425. USB_REQ_RECIPIENT_MASK);
  426. case USB_REQ_GET_DESCRIPTOR:
  427. return ep0_get_descriptor (device, urb,
  428. le16_to_cpu (request->wLength),
  429. le16_to_cpu (request->wValue) >> 8,
  430. le16_to_cpu (request->wValue) & 0xff);
  431. case USB_REQ_GET_CONFIGURATION:
  432. serial_printf("get config %d\n", device->configuration);
  433. return ep0_get_one (device, urb,
  434. device->configuration);
  435. case USB_REQ_GET_INTERFACE:
  436. return ep0_get_one (device, urb, device->alternate);
  437. case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
  438. return -1;
  439. case USB_REQ_CLEAR_FEATURE:
  440. case USB_REQ_SET_FEATURE:
  441. case USB_REQ_SET_ADDRESS:
  442. case USB_REQ_SET_DESCRIPTOR:
  443. case USB_REQ_SET_CONFIGURATION:
  444. case USB_REQ_SET_INTERFACE:
  445. return -1;
  446. }
  447. }
  448. /* handle the requests that do not return data */
  449. else {
  450. /*dbg_ep0(3, "Host-to-Device"); */
  451. switch (request->bRequest) {
  452. case USB_REQ_CLEAR_FEATURE:
  453. case USB_REQ_SET_FEATURE:
  454. dbg_ep0 (0, "Host-to-Device");
  455. switch (request->
  456. bmRequestType & USB_REQ_RECIPIENT_MASK) {
  457. case USB_REQ_RECIPIENT_DEVICE:
  458. /* XXX DEVICE_REMOTE_WAKEUP or TEST_MODE would be added here */
  459. /* XXX fall through for now as we do not support either */
  460. case USB_REQ_RECIPIENT_INTERFACE:
  461. case USB_REQ_RECIPIENT_OTHER:
  462. dbg_ep0 (0, "request %s not",
  463. USBD_DEVICE_REQUESTS (request->bRequest));
  464. default:
  465. return -1;
  466. case USB_REQ_RECIPIENT_ENDPOINT:
  467. dbg_ep0 (0, "ENDPOINT: %x", le16_to_cpu (request->wValue));
  468. if (le16_to_cpu (request->wValue) == USB_ENDPOINT_HALT) {
  469. /*return usbd_device_feature (device, le16_to_cpu (request->wIndex), */
  470. /* request->bRequest == USB_REQ_SET_FEATURE); */
  471. /* NEED TO IMPLEMENT THIS!!! */
  472. return -1;
  473. } else {
  474. dbg_ep0 (1, "request %s bad wValue: %04x",
  475. USBD_DEVICE_REQUESTS
  476. (request->bRequest),
  477. le16_to_cpu (request->wValue));
  478. return -1;
  479. }
  480. }
  481. case USB_REQ_SET_ADDRESS:
  482. /* check if this is a re-address, reset first if it is (this shouldn't be possible) */
  483. if (device->device_state != STATE_DEFAULT) {
  484. dbg_ep0 (1, "set_address: %02x state: %s",
  485. le16_to_cpu (request->wValue),
  486. usbd_device_states[device->device_state]);
  487. return -1;
  488. }
  489. address = le16_to_cpu (request->wValue);
  490. if ((address & 0x7f) != address) {
  491. dbg_ep0 (1, "invalid address %04x %04x",
  492. address, address & 0x7f);
  493. return -1;
  494. }
  495. device->address = address;
  496. /*dbg_ep0(2, "address: %d %d %d", */
  497. /* request->wValue, le16_to_cpu(request->wValue), device->address); */
  498. return 0;
  499. case USB_REQ_SET_DESCRIPTOR: /* XXX should we support this? */
  500. dbg_ep0 (0, "set descriptor: NOT SUPPORTED");
  501. return -1;
  502. case USB_REQ_SET_CONFIGURATION:
  503. /* c.f. 9.4.7 - the top half of wValue is reserved */
  504. device->configuration = le16_to_cpu(request->wValue) & 0xff;
  505. /* reset interface and alternate settings */
  506. device->interface = device->alternate = 0;
  507. /*dbg_ep0(2, "set configuration: %d", device->configuration); */
  508. /*serial_printf("DEVICE_CONFIGURED.. event?\n"); */
  509. return 0;
  510. case USB_REQ_SET_INTERFACE:
  511. device->interface = le16_to_cpu (request->wIndex);
  512. device->alternate = le16_to_cpu (request->wValue);
  513. /*dbg_ep0(2, "set interface: %d alternate: %d", device->interface, device->alternate); */
  514. serial_printf ("DEVICE_SET_INTERFACE.. event?\n");
  515. return 0;
  516. case USB_REQ_GET_STATUS:
  517. case USB_REQ_GET_DESCRIPTOR:
  518. case USB_REQ_GET_CONFIGURATION:
  519. case USB_REQ_GET_INTERFACE:
  520. case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
  521. return -1;
  522. }
  523. }
  524. return -1;
  525. }