core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * Gerry Hamel, geh@ti.com, Texas Instruments
  5. *
  6. * Based on
  7. * linux/drivers/usbd/usbd.c.c - USB Device Core Layer
  8. *
  9. * Copyright (c) 2000, 2001, 2002 Lineo
  10. * Copyright (c) 2001 Hewlett Packard
  11. *
  12. * By:
  13. * Stuart Lynne <sl@lineo.com>,
  14. * Tom Rushworth <tbr@lineo.com>,
  15. * Bruce Balden <balden@lineo.com>
  16. */
  17. #include <malloc.h>
  18. #include <usbdevice.h>
  19. #define MAX_INTERFACES 2
  20. int maxstrings = 20;
  21. /* Global variables ************************************************************************** */
  22. struct usb_string_descriptor **usb_strings;
  23. int usb_devices;
  24. extern struct usb_function_driver ep0_driver;
  25. int registered_functions;
  26. int registered_devices;
  27. char *usbd_device_events[] = {
  28. "DEVICE_UNKNOWN",
  29. "DEVICE_INIT",
  30. "DEVICE_CREATE",
  31. "DEVICE_HUB_CONFIGURED",
  32. "DEVICE_RESET",
  33. "DEVICE_ADDRESS_ASSIGNED",
  34. "DEVICE_CONFIGURED",
  35. "DEVICE_SET_INTERFACE",
  36. "DEVICE_SET_FEATURE",
  37. "DEVICE_CLEAR_FEATURE",
  38. "DEVICE_DE_CONFIGURED",
  39. "DEVICE_BUS_INACTIVE",
  40. "DEVICE_BUS_ACTIVITY",
  41. "DEVICE_POWER_INTERRUPTION",
  42. "DEVICE_HUB_RESET",
  43. "DEVICE_DESTROY",
  44. "DEVICE_FUNCTION_PRIVATE",
  45. };
  46. char *usbd_device_states[] = {
  47. "STATE_INIT",
  48. "STATE_CREATED",
  49. "STATE_ATTACHED",
  50. "STATE_POWERED",
  51. "STATE_DEFAULT",
  52. "STATE_ADDRESSED",
  53. "STATE_CONFIGURED",
  54. "STATE_UNKNOWN",
  55. };
  56. char *usbd_device_requests[] = {
  57. "GET STATUS", /* 0 */
  58. "CLEAR FEATURE", /* 1 */
  59. "RESERVED", /* 2 */
  60. "SET FEATURE", /* 3 */
  61. "RESERVED", /* 4 */
  62. "SET ADDRESS", /* 5 */
  63. "GET DESCRIPTOR", /* 6 */
  64. "SET DESCRIPTOR", /* 7 */
  65. "GET CONFIGURATION", /* 8 */
  66. "SET CONFIGURATION", /* 9 */
  67. "GET INTERFACE", /* 10 */
  68. "SET INTERFACE", /* 11 */
  69. "SYNC FRAME", /* 12 */
  70. };
  71. char *usbd_device_descriptors[] = {
  72. "UNKNOWN", /* 0 */
  73. "DEVICE", /* 1 */
  74. "CONFIG", /* 2 */
  75. "STRING", /* 3 */
  76. "INTERFACE", /* 4 */
  77. "ENDPOINT", /* 5 */
  78. "DEVICE QUALIFIER", /* 6 */
  79. "OTHER SPEED", /* 7 */
  80. "INTERFACE POWER", /* 8 */
  81. };
  82. char *usbd_device_status[] = {
  83. "USBD_OPENING",
  84. "USBD_OK",
  85. "USBD_SUSPENDED",
  86. "USBD_CLOSING",
  87. };
  88. /* Descriptor support functions ************************************************************** */
  89. /**
  90. * usbd_get_string - find and return a string descriptor
  91. * @index: string index to return
  92. *
  93. * Find an indexed string and return a pointer to a it.
  94. */
  95. struct usb_string_descriptor *usbd_get_string (__u8 index)
  96. {
  97. if (index >= maxstrings) {
  98. return NULL;
  99. }
  100. return usb_strings[index];
  101. }
  102. /* Access to device descriptor functions ***************************************************** */
  103. /* *
  104. * usbd_device_configuration_instance - find a configuration instance for this device
  105. * @device:
  106. * @configuration: index to configuration, 0 - N-1
  107. *
  108. * Get specifed device configuration. Index should be bConfigurationValue-1.
  109. */
  110. static struct usb_configuration_instance *usbd_device_configuration_instance (struct usb_device_instance *device,
  111. unsigned int port, unsigned int configuration)
  112. {
  113. if (configuration >= device->configurations)
  114. return NULL;
  115. return device->configuration_instance_array + configuration;
  116. }
  117. /* *
  118. * usbd_device_interface_instance
  119. * @device:
  120. * @configuration: index to configuration, 0 - N-1
  121. * @interface: index to interface
  122. *
  123. * Return the specified interface descriptor for the specified device.
  124. */
  125. struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
  126. {
  127. struct usb_configuration_instance *configuration_instance;
  128. if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
  129. return NULL;
  130. }
  131. if (interface >= configuration_instance->interfaces) {
  132. return NULL;
  133. }
  134. return configuration_instance->interface_instance_array + interface;
  135. }
  136. /* *
  137. * usbd_device_alternate_descriptor_list
  138. * @device:
  139. * @configuration: index to configuration, 0 - N-1
  140. * @interface: index to interface
  141. * @alternate: alternate setting
  142. *
  143. * Return the specified alternate descriptor for the specified device.
  144. */
  145. struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
  146. {
  147. struct usb_interface_instance *interface_instance;
  148. if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
  149. return NULL;
  150. }
  151. if (alternate >= interface_instance->alternates) {
  152. return NULL;
  153. }
  154. return interface_instance->alternates_instance_array + alternate;
  155. }
  156. /* *
  157. * usbd_device_device_descriptor
  158. * @device: which device
  159. * @configuration: index to configuration, 0 - N-1
  160. * @port: which port
  161. *
  162. * Return the specified configuration descriptor for the specified device.
  163. */
  164. struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
  165. {
  166. return (device->device_descriptor);
  167. }
  168. /**
  169. * usbd_device_configuration_descriptor
  170. * @device: which device
  171. * @port: which port
  172. * @configuration: index to configuration, 0 - N-1
  173. *
  174. * Return the specified configuration descriptor for the specified device.
  175. */
  176. struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
  177. usb_device_instance
  178. *device, int port, int configuration)
  179. {
  180. struct usb_configuration_instance *configuration_instance;
  181. if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
  182. return NULL;
  183. }
  184. return (configuration_instance->configuration_descriptor);
  185. }
  186. /**
  187. * usbd_device_interface_descriptor
  188. * @device: which device
  189. * @port: which port
  190. * @configuration: index to configuration, 0 - N-1
  191. * @interface: index to interface
  192. * @alternate: alternate setting
  193. *
  194. * Return the specified interface descriptor for the specified device.
  195. */
  196. struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
  197. *device, int port, int configuration, int interface, int alternate)
  198. {
  199. struct usb_interface_instance *interface_instance;
  200. if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
  201. return NULL;
  202. }
  203. if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
  204. return NULL;
  205. }
  206. return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
  207. }
  208. /**
  209. * usbd_device_endpoint_descriptor_index
  210. * @device: which device
  211. * @port: which port
  212. * @configuration: index to configuration, 0 - N-1
  213. * @interface: index to interface
  214. * @alternate: index setting
  215. * @index: which index
  216. *
  217. * Return the specified endpoint descriptor for the specified device.
  218. */
  219. struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
  220. *device, int port, int configuration, int interface, int alternate, int index)
  221. {
  222. struct usb_alternate_instance *alternate_instance;
  223. if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
  224. return NULL;
  225. }
  226. if (index >= alternate_instance->endpoints) {
  227. return NULL;
  228. }
  229. return *(alternate_instance->endpoints_descriptor_array + index);
  230. }
  231. /**
  232. * usbd_device_endpoint_transfersize
  233. * @device: which device
  234. * @port: which port
  235. * @configuration: index to configuration, 0 - N-1
  236. * @interface: index to interface
  237. * @index: which index
  238. *
  239. * Return the specified endpoint transfer size;
  240. */
  241. int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
  242. {
  243. struct usb_alternate_instance *alternate_instance;
  244. if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
  245. return 0;
  246. }
  247. if (index >= alternate_instance->endpoints) {
  248. return 0;
  249. }
  250. return *(alternate_instance->endpoint_transfersize_array + index);
  251. }
  252. /**
  253. * usbd_device_endpoint_descriptor
  254. * @device: which device
  255. * @port: which port
  256. * @configuration: index to configuration, 0 - N-1
  257. * @interface: index to interface
  258. * @alternate: alternate setting
  259. * @endpoint: which endpoint
  260. *
  261. * Return the specified endpoint descriptor for the specified device.
  262. */
  263. struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
  264. {
  265. struct usb_endpoint_descriptor *endpoint_descriptor;
  266. int i;
  267. for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
  268. if (endpoint_descriptor->bEndpointAddress == endpoint) {
  269. return endpoint_descriptor;
  270. }
  271. }
  272. return NULL;
  273. }
  274. /**
  275. * usbd_endpoint_halted
  276. * @device: point to struct usb_device_instance
  277. * @endpoint: endpoint to check
  278. *
  279. * Return non-zero if endpoint is halted.
  280. */
  281. int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
  282. {
  283. return (device->status == USB_STATUS_HALT);
  284. }
  285. /**
  286. * usbd_rcv_complete - complete a receive
  287. * @endpoint:
  288. * @len:
  289. * @urb_bad:
  290. *
  291. * Called from rcv interrupt to complete.
  292. */
  293. void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
  294. {
  295. if (endpoint) {
  296. struct urb *rcv_urb;
  297. /*usbdbg("len: %d urb: %p\n", len, endpoint->rcv_urb); */
  298. /* if we had an urb then update actual_length, dispatch if neccessary */
  299. if ((rcv_urb = endpoint->rcv_urb)) {
  300. /*usbdbg("actual: %d buffer: %d\n", */
  301. /*rcv_urb->actual_length, rcv_urb->buffer_length); */
  302. /* check the urb is ok, are we adding data less than the packetsize */
  303. if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
  304. /*usbdbg("updating actual_length by %d\n",len); */
  305. /* increment the received data size */
  306. rcv_urb->actual_length += len;
  307. } else {
  308. usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
  309. rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
  310. rcv_urb->actual_length = 0;
  311. rcv_urb->status = RECV_ERROR;
  312. }
  313. } else {
  314. usberr("no rcv_urb!");
  315. }
  316. } else {
  317. usberr("no endpoint!");
  318. }
  319. }
  320. /**
  321. * usbd_tx_complete - complete a transmit
  322. * @endpoint:
  323. * @resetart:
  324. *
  325. * Called from tx interrupt to complete.
  326. */
  327. void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
  328. {
  329. if (endpoint) {
  330. struct urb *tx_urb;
  331. /* if we have a tx_urb advance or reset, finish if complete */
  332. if ((tx_urb = endpoint->tx_urb)) {
  333. int sent = endpoint->last;
  334. endpoint->sent += sent;
  335. endpoint->last -= sent;
  336. if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
  337. tx_urb->actual_length = 0;
  338. endpoint->sent = 0;
  339. endpoint->last = 0;
  340. /* Remove from active, save for re-use */
  341. urb_detach(tx_urb);
  342. urb_append(&endpoint->done, tx_urb);
  343. /*usbdbg("done->next %p, tx_urb %p, done %p", */
  344. /* endpoint->done.next, tx_urb, &endpoint->done); */
  345. endpoint->tx_urb = first_urb_detached(&endpoint->tx);
  346. if( endpoint->tx_urb ) {
  347. endpoint->tx_queue--;
  348. usbdbg("got urb from tx list");
  349. }
  350. if( !endpoint->tx_urb ) {
  351. /*usbdbg("taking urb from done list"); */
  352. endpoint->tx_urb = first_urb_detached(&endpoint->done);
  353. }
  354. if( !endpoint->tx_urb ) {
  355. usbdbg("allocating new urb for tx_urb");
  356. endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
  357. }
  358. }
  359. }
  360. }
  361. }
  362. /* URB linked list functions ***************************************************** */
  363. /*
  364. * Initialize an urb_link to be a single element list.
  365. * If the urb_link is being used as a distinguished list head
  366. * the list is empty when the head is the only link in the list.
  367. */
  368. void urb_link_init (urb_link * ul)
  369. {
  370. if (ul) {
  371. ul->prev = ul->next = ul;
  372. }
  373. }
  374. /*
  375. * Detach an urb_link from a list, and set it
  376. * up as a single element list, so no dangling
  377. * pointers can be followed, and so it can be
  378. * joined to another list if so desired.
  379. */
  380. void urb_detach (struct urb *urb)
  381. {
  382. if (urb) {
  383. urb_link *ul = &urb->link;
  384. ul->next->prev = ul->prev;
  385. ul->prev->next = ul->next;
  386. urb_link_init (ul);
  387. }
  388. }
  389. /*
  390. * Return the first urb_link in a list with a distinguished
  391. * head "hd", or NULL if the list is empty. This will also
  392. * work as a predicate, returning NULL if empty, and non-NULL
  393. * otherwise.
  394. */
  395. urb_link *first_urb_link (urb_link * hd)
  396. {
  397. urb_link *nx;
  398. if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
  399. /* There is at least one element in the list */
  400. /* (besides the distinguished head). */
  401. return (nx);
  402. }
  403. /* The list is empty */
  404. return (NULL);
  405. }
  406. /*
  407. * Return the first urb in a list with a distinguished
  408. * head "hd", or NULL if the list is empty.
  409. */
  410. struct urb *first_urb (urb_link * hd)
  411. {
  412. urb_link *nx;
  413. if (NULL == (nx = first_urb_link (hd))) {
  414. /* The list is empty */
  415. return (NULL);
  416. }
  417. return (p2surround (struct urb, link, nx));
  418. }
  419. /*
  420. * Detach and return the first urb in a list with a distinguished
  421. * head "hd", or NULL if the list is empty.
  422. *
  423. */
  424. struct urb *first_urb_detached (urb_link * hd)
  425. {
  426. struct urb *urb;
  427. if ((urb = first_urb (hd))) {
  428. urb_detach (urb);
  429. }
  430. return urb;
  431. }
  432. /*
  433. * Append an urb_link (or a whole list of
  434. * urb_links) to the tail of another list
  435. * of urb_links.
  436. */
  437. void urb_append (urb_link * hd, struct urb *urb)
  438. {
  439. if (hd && urb) {
  440. urb_link *new = &urb->link;
  441. /* This allows the new urb to be a list of urbs, */
  442. /* with new pointing at the first, but the link */
  443. /* must be initialized. */
  444. /* Order is important here... */
  445. urb_link *pul = hd->prev;
  446. new->prev->next = hd;
  447. hd->prev = new->prev;
  448. new->prev = pul;
  449. pul->next = new;
  450. }
  451. }
  452. /* URB create/destroy functions ***************************************************** */
  453. /**
  454. * usbd_alloc_urb - allocate an URB appropriate for specified endpoint
  455. * @device: device instance
  456. * @endpoint: endpoint
  457. *
  458. * Allocate an urb structure. The usb device urb structure is used to
  459. * contain all data associated with a transfer, including a setup packet for
  460. * control transfers.
  461. *
  462. * NOTE: endpoint_address MUST contain a direction flag.
  463. */
  464. struct urb *usbd_alloc_urb (struct usb_device_instance *device,
  465. struct usb_endpoint_instance *endpoint)
  466. {
  467. struct urb *urb;
  468. if (!(urb = (struct urb *) malloc (sizeof (struct urb)))) {
  469. usberr (" F A T A L: malloc(%zu) FAILED!!!!",
  470. sizeof (struct urb));
  471. return NULL;
  472. }
  473. /* Fill in known fields */
  474. memset (urb, 0, sizeof (struct urb));
  475. urb->endpoint = endpoint;
  476. urb->device = device;
  477. urb->buffer = (u8 *) urb->buffer_data;
  478. urb->buffer_length = sizeof (urb->buffer_data);
  479. urb_link_init (&urb->link);
  480. return urb;
  481. }
  482. /**
  483. * usbd_dealloc_urb - deallocate an URB and associated buffer
  484. * @urb: pointer to an urb structure
  485. *
  486. * Deallocate an urb structure and associated data.
  487. */
  488. void usbd_dealloc_urb (struct urb *urb)
  489. {
  490. if (urb) {
  491. free (urb);
  492. }
  493. }
  494. /* Event signaling functions ***************************************************** */
  495. /**
  496. * usbd_device_event - called to respond to various usb events
  497. * @device: pointer to struct device
  498. * @event: event to respond to
  499. *
  500. * Used by a Bus driver to indicate an event.
  501. */
  502. void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
  503. {
  504. usb_device_state_t state;
  505. if (!device || !device->bus) {
  506. usberr("(%p,%d) NULL device or device->bus", device, event);
  507. return;
  508. }
  509. state = device->device_state;
  510. usbinfo("%s", usbd_device_events[event]);
  511. switch (event) {
  512. case DEVICE_UNKNOWN:
  513. break;
  514. case DEVICE_INIT:
  515. device->device_state = STATE_INIT;
  516. break;
  517. case DEVICE_CREATE:
  518. device->device_state = STATE_ATTACHED;
  519. break;
  520. case DEVICE_HUB_CONFIGURED:
  521. device->device_state = STATE_POWERED;
  522. break;
  523. case DEVICE_RESET:
  524. device->device_state = STATE_DEFAULT;
  525. device->address = 0;
  526. break;
  527. case DEVICE_ADDRESS_ASSIGNED:
  528. device->device_state = STATE_ADDRESSED;
  529. break;
  530. case DEVICE_CONFIGURED:
  531. device->device_state = STATE_CONFIGURED;
  532. break;
  533. case DEVICE_DE_CONFIGURED:
  534. device->device_state = STATE_ADDRESSED;
  535. break;
  536. case DEVICE_BUS_INACTIVE:
  537. if (device->status != USBD_CLOSING) {
  538. device->status = USBD_SUSPENDED;
  539. }
  540. break;
  541. case DEVICE_BUS_ACTIVITY:
  542. if (device->status != USBD_CLOSING) {
  543. device->status = USBD_OK;
  544. }
  545. break;
  546. case DEVICE_SET_INTERFACE:
  547. break;
  548. case DEVICE_SET_FEATURE:
  549. break;
  550. case DEVICE_CLEAR_FEATURE:
  551. break;
  552. case DEVICE_POWER_INTERRUPTION:
  553. device->device_state = STATE_POWERED;
  554. break;
  555. case DEVICE_HUB_RESET:
  556. device->device_state = STATE_ATTACHED;
  557. break;
  558. case DEVICE_DESTROY:
  559. device->device_state = STATE_UNKNOWN;
  560. break;
  561. case DEVICE_FUNCTION_PRIVATE:
  562. break;
  563. default:
  564. usbdbg("event %d - not handled",event);
  565. break;
  566. }
  567. debug("%s event: %d oldstate: %d newstate: %d status: %d address: %d",
  568. device->name, event, state,
  569. device->device_state, device->status, device->address);
  570. /* tell the bus interface driver */
  571. if( device->event ) {
  572. /* usbdbg("calling device->event"); */
  573. device->event(device, event, data);
  574. }
  575. }