core.c 16 KB

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