usbdcore.c 17 KB

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