core.c 17 KB

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