usbdcore.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. /* XXX */
  128. configuration = configuration ? configuration - 1 : 0;
  129. if (configuration >= device->configurations) {
  130. return NULL;
  131. }
  132. return device->configuration_instance_array + configuration;
  133. }
  134. /* *
  135. * usbd_device_interface_instance
  136. * @device:
  137. * @configuration: index to configuration, 0 - N-1
  138. * @interface: index to interface
  139. *
  140. * Return the specified interface descriptor for the specified device.
  141. */
  142. struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
  143. {
  144. struct usb_configuration_instance *configuration_instance;
  145. if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
  146. return NULL;
  147. }
  148. if (interface >= configuration_instance->interfaces) {
  149. return NULL;
  150. }
  151. return configuration_instance->interface_instance_array + interface;
  152. }
  153. /* *
  154. * usbd_device_alternate_descriptor_list
  155. * @device:
  156. * @configuration: index to configuration, 0 - N-1
  157. * @interface: index to interface
  158. * @alternate: alternate setting
  159. *
  160. * Return the specified alternate descriptor for the specified device.
  161. */
  162. struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
  163. {
  164. struct usb_interface_instance *interface_instance;
  165. if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
  166. return NULL;
  167. }
  168. if (alternate >= interface_instance->alternates) {
  169. return NULL;
  170. }
  171. return interface_instance->alternates_instance_array + alternate;
  172. }
  173. /* *
  174. * usbd_device_device_descriptor
  175. * @device: which device
  176. * @configuration: index to configuration, 0 - N-1
  177. * @port: which port
  178. *
  179. * Return the specified configuration descriptor for the specified device.
  180. */
  181. struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
  182. {
  183. return (device->device_descriptor);
  184. }
  185. /**
  186. * usbd_device_configuration_descriptor
  187. * @device: which device
  188. * @port: which port
  189. * @configuration: index to configuration, 0 - N-1
  190. *
  191. * Return the specified configuration descriptor for the specified device.
  192. */
  193. struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
  194. usb_device_instance
  195. *device, int port, int configuration)
  196. {
  197. struct usb_configuration_instance *configuration_instance;
  198. if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
  199. return NULL;
  200. }
  201. return (configuration_instance->configuration_descriptor);
  202. }
  203. /**
  204. * usbd_device_interface_descriptor
  205. * @device: which device
  206. * @port: which port
  207. * @configuration: index to configuration, 0 - N-1
  208. * @interface: index to interface
  209. * @alternate: alternate setting
  210. *
  211. * Return the specified interface descriptor for the specified device.
  212. */
  213. struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
  214. *device, int port, int configuration, int interface, int alternate)
  215. {
  216. struct usb_interface_instance *interface_instance;
  217. if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
  218. return NULL;
  219. }
  220. if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
  221. return NULL;
  222. }
  223. return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
  224. }
  225. /**
  226. * usbd_device_endpoint_descriptor_index
  227. * @device: which device
  228. * @port: which port
  229. * @configuration: index to configuration, 0 - N-1
  230. * @interface: index to interface
  231. * @alternate: index setting
  232. * @index: which index
  233. *
  234. * Return the specified endpoint descriptor for the specified device.
  235. */
  236. struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
  237. *device, int port, int configuration, int interface, int alternate, int index)
  238. {
  239. struct usb_alternate_instance *alternate_instance;
  240. if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
  241. return NULL;
  242. }
  243. if (index >= alternate_instance->endpoints) {
  244. return NULL;
  245. }
  246. return *(alternate_instance->endpoints_descriptor_array + index);
  247. }
  248. /**
  249. * usbd_device_endpoint_transfersize
  250. * @device: which device
  251. * @port: which port
  252. * @configuration: index to configuration, 0 - N-1
  253. * @interface: index to interface
  254. * @index: which index
  255. *
  256. * Return the specified endpoint transfer size;
  257. */
  258. int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
  259. {
  260. struct usb_alternate_instance *alternate_instance;
  261. if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
  262. return 0;
  263. }
  264. if (index >= alternate_instance->endpoints) {
  265. return 0;
  266. }
  267. return *(alternate_instance->endpoint_transfersize_array + index);
  268. }
  269. /**
  270. * usbd_device_endpoint_descriptor
  271. * @device: which device
  272. * @port: which port
  273. * @configuration: index to configuration, 0 - N-1
  274. * @interface: index to interface
  275. * @alternate: alternate setting
  276. * @endpoint: which endpoint
  277. *
  278. * Return the specified endpoint descriptor for the specified device.
  279. */
  280. struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
  281. {
  282. struct usb_endpoint_descriptor *endpoint_descriptor;
  283. int i;
  284. for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
  285. if (endpoint_descriptor->bEndpointAddress == endpoint) {
  286. return endpoint_descriptor;
  287. }
  288. }
  289. return NULL;
  290. }
  291. /**
  292. * usbd_endpoint_halted
  293. * @device: point to struct usb_device_instance
  294. * @endpoint: endpoint to check
  295. *
  296. * Return non-zero if endpoint is halted.
  297. */
  298. int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
  299. {
  300. return (device->status == USB_STATUS_HALT);
  301. }
  302. /**
  303. * usbd_rcv_complete - complete a receive
  304. * @endpoint:
  305. * @len:
  306. * @urb_bad:
  307. *
  308. * Called from rcv interrupt to complete.
  309. */
  310. void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
  311. {
  312. if (endpoint) {
  313. struct urb *rcv_urb;
  314. /*usbdbg("len: %d urb: %p\n", len, endpoint->rcv_urb); */
  315. /* if we had an urb then update actual_length, dispatch if neccessary */
  316. if ((rcv_urb = endpoint->rcv_urb)) {
  317. /*usbdbg("actual: %d buffer: %d\n", */
  318. /*rcv_urb->actual_length, rcv_urb->buffer_length); */
  319. /* check the urb is ok, are we adding data less than the packetsize */
  320. if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
  321. /*usbdbg("updating actual_length by %d\n",len); */
  322. /* increment the received data size */
  323. rcv_urb->actual_length += len;
  324. } else {
  325. usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
  326. rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
  327. rcv_urb->actual_length = 0;
  328. rcv_urb->status = RECV_ERROR;
  329. }
  330. } else {
  331. usberr("no rcv_urb!");
  332. }
  333. } else {
  334. usberr("no endpoint!");
  335. }
  336. }
  337. /**
  338. * usbd_tx_complete - complete a transmit
  339. * @endpoint:
  340. * @resetart:
  341. *
  342. * Called from tx interrupt to complete.
  343. */
  344. void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
  345. {
  346. if (endpoint) {
  347. struct urb *tx_urb;
  348. /* if we have a tx_urb advance or reset, finish if complete */
  349. if ((tx_urb = endpoint->tx_urb)) {
  350. int sent = endpoint->last;
  351. endpoint->sent += sent;
  352. endpoint->last -= sent;
  353. if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
  354. tx_urb->actual_length = 0;
  355. endpoint->sent = 0;
  356. endpoint->last = 0;
  357. /* Remove from active, save for re-use */
  358. urb_detach(tx_urb);
  359. urb_append(&endpoint->done, tx_urb);
  360. /*usbdbg("done->next %p, tx_urb %p, done %p", */
  361. /* endpoint->done.next, tx_urb, &endpoint->done); */
  362. endpoint->tx_urb = first_urb_detached(&endpoint->tx);
  363. if( endpoint->tx_urb ) {
  364. endpoint->tx_queue--;
  365. usbdbg("got urb from tx list");
  366. }
  367. if( !endpoint->tx_urb ) {
  368. /*usbdbg("taking urb from done list"); */
  369. endpoint->tx_urb = first_urb_detached(&endpoint->done);
  370. }
  371. if( !endpoint->tx_urb ) {
  372. usbdbg("allocating new urb for tx_urb");
  373. endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
  374. }
  375. }
  376. }
  377. }
  378. }
  379. /* URB linked list functions ***************************************************** */
  380. /*
  381. * Initialize an urb_link to be a single element list.
  382. * If the urb_link is being used as a distinguished list head
  383. * the list is empty when the head is the only link in the list.
  384. */
  385. void urb_link_init (urb_link * ul)
  386. {
  387. if (ul) {
  388. ul->prev = ul->next = ul;
  389. }
  390. }
  391. /*
  392. * Detach an urb_link from a list, and set it
  393. * up as a single element list, so no dangling
  394. * pointers can be followed, and so it can be
  395. * joined to another list if so desired.
  396. */
  397. void urb_detach (struct urb *urb)
  398. {
  399. if (urb) {
  400. urb_link *ul = &urb->link;
  401. ul->next->prev = ul->prev;
  402. ul->prev->next = ul->next;
  403. urb_link_init (ul);
  404. }
  405. }
  406. /*
  407. * Return the first urb_link in a list with a distinguished
  408. * head "hd", or NULL if the list is empty. This will also
  409. * work as a predicate, returning NULL if empty, and non-NULL
  410. * otherwise.
  411. */
  412. urb_link *first_urb_link (urb_link * hd)
  413. {
  414. urb_link *nx;
  415. if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
  416. /* There is at least one element in the list */
  417. /* (besides the distinguished head). */
  418. return (nx);
  419. }
  420. /* The list is empty */
  421. return (NULL);
  422. }
  423. /*
  424. * Return the first urb in a list with a distinguished
  425. * head "hd", or NULL if the list is empty.
  426. */
  427. struct urb *first_urb (urb_link * hd)
  428. {
  429. urb_link *nx;
  430. if (NULL == (nx = first_urb_link (hd))) {
  431. /* The list is empty */
  432. return (NULL);
  433. }
  434. return (p2surround (struct urb, link, nx));
  435. }
  436. /*
  437. * Detach and return the first urb in a list with a distinguished
  438. * head "hd", or NULL if the list is empty.
  439. *
  440. */
  441. struct urb *first_urb_detached (urb_link * hd)
  442. {
  443. struct urb *urb;
  444. if ((urb = first_urb (hd))) {
  445. urb_detach (urb);
  446. }
  447. return urb;
  448. }
  449. /*
  450. * Append an urb_link (or a whole list of
  451. * urb_links) to the tail of another list
  452. * of urb_links.
  453. */
  454. void urb_append (urb_link * hd, struct urb *urb)
  455. {
  456. if (hd && urb) {
  457. urb_link *new = &urb->link;
  458. /* This allows the new urb to be a list of urbs, */
  459. /* with new pointing at the first, but the link */
  460. /* must be initialized. */
  461. /* Order is important here... */
  462. urb_link *pul = hd->prev;
  463. new->prev->next = hd;
  464. hd->prev = new->prev;
  465. new->prev = pul;
  466. pul->next = new;
  467. }
  468. }
  469. /* URB create/destroy functions ***************************************************** */
  470. /**
  471. * usbd_alloc_urb - allocate an URB appropriate for specified endpoint
  472. * @device: device instance
  473. * @endpoint: endpoint
  474. *
  475. * Allocate an urb structure. The usb device urb structure is used to
  476. * contain all data associated with a transfer, including a setup packet for
  477. * control transfers.
  478. *
  479. * NOTE: endpoint_address MUST contain a direction flag.
  480. */
  481. struct urb *usbd_alloc_urb (struct usb_device_instance *device, struct usb_endpoint_instance *endpoint)
  482. {
  483. struct urb *urb;
  484. if( !(urb = (struct urb*)malloc(sizeof(struct urb))) ) {
  485. usberr(" F A T A L: malloc(%u) FAILED!!!!", sizeof(struct urb));
  486. return NULL;
  487. }
  488. /* Fill in known fields */
  489. memset(urb, 0, sizeof(struct urb));
  490. urb->endpoint = endpoint;
  491. urb->device = device;
  492. urb->buffer = (u8*)urb->buffer_data;
  493. urb->buffer_length = sizeof(urb->buffer_data);
  494. urb_link_init (&urb->link);
  495. return urb;
  496. }
  497. /**
  498. * usbd_dealloc_urb - deallocate an URB and associated buffer
  499. * @urb: pointer to an urb structure
  500. *
  501. * Deallocate an urb structure and associated data.
  502. */
  503. void usbd_dealloc_urb (struct urb *urb)
  504. {
  505. if (urb) {
  506. free (urb);
  507. }
  508. }
  509. /* Event signaling functions ***************************************************** */
  510. /**
  511. * usbd_device_event - called to respond to various usb events
  512. * @device: pointer to struct device
  513. * @event: event to respond to
  514. *
  515. * Used by a Bus driver to indicate an event.
  516. */
  517. void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
  518. {
  519. usb_device_state_t state;
  520. if (!device || !device->bus) {
  521. usberr("(%p,%d) NULL device or device->bus", device, event);
  522. return;
  523. }
  524. state = device->device_state;
  525. usbinfo("%s", usbd_device_events[event]);
  526. switch (event) {
  527. case DEVICE_UNKNOWN:
  528. break;
  529. case DEVICE_INIT:
  530. device->device_state = STATE_INIT;
  531. break;
  532. case DEVICE_CREATE:
  533. device->device_state = STATE_ATTACHED;
  534. break;
  535. case DEVICE_HUB_CONFIGURED:
  536. device->device_state = STATE_POWERED;
  537. break;
  538. case DEVICE_RESET:
  539. device->device_state = STATE_DEFAULT;
  540. device->address = 0;
  541. break;
  542. case DEVICE_ADDRESS_ASSIGNED:
  543. device->device_state = STATE_ADDRESSED;
  544. break;
  545. case DEVICE_CONFIGURED:
  546. device->device_state = STATE_CONFIGURED;
  547. break;
  548. case DEVICE_DE_CONFIGURED:
  549. device->device_state = STATE_ADDRESSED;
  550. break;
  551. case DEVICE_BUS_INACTIVE:
  552. if (device->status != USBD_CLOSING) {
  553. device->status = USBD_SUSPENDED;
  554. }
  555. break;
  556. case DEVICE_BUS_ACTIVITY:
  557. if (device->status != USBD_CLOSING) {
  558. device->status = USBD_OK;
  559. }
  560. break;
  561. case DEVICE_SET_INTERFACE:
  562. break;
  563. case DEVICE_SET_FEATURE:
  564. break;
  565. case DEVICE_CLEAR_FEATURE:
  566. break;
  567. case DEVICE_POWER_INTERRUPTION:
  568. device->device_state = STATE_POWERED;
  569. break;
  570. case DEVICE_HUB_RESET:
  571. device->device_state = STATE_ATTACHED;
  572. break;
  573. case DEVICE_DESTROY:
  574. device->device_state = STATE_UNKNOWN;
  575. break;
  576. case DEVICE_FUNCTION_PRIVATE:
  577. break;
  578. default:
  579. usbdbg("event %d - not handled",event);
  580. break;
  581. }
  582. /*usbdbg("%s event: %d oldstate: %d newstate: %d status: %d address: %d",
  583. device->name, event, state,
  584. device->device_state, device->status, device->address); */
  585. /* tell the bus interface driver */
  586. if( device->event ) {
  587. /* usbdbg("calling device->event"); */
  588. device->event(device, event, data);
  589. }
  590. }