usbtty.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * (C) Copyright 2003
  3. * Gerry Hamel, geh@ti.com, Texas Instruments
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <common.h>
  21. #ifdef CONFIG_USB_TTY
  22. #include <circbuf.h>
  23. #include <devices.h>
  24. #include "usbtty.h"
  25. #if 0
  26. #define TTYDBG(fmt,args...) serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
  27. #else
  28. #define TTYDBG(fmt,args...) do{}while(0)
  29. #endif
  30. #if 0
  31. #define TTYERR(fmt,args...) serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
  32. #else
  33. #define TTYERR(fmt,args...) do{}while(0)
  34. #endif
  35. /*
  36. * Buffers to hold input and output data
  37. */
  38. #define USBTTY_BUFFER_SIZE 256
  39. static circbuf_t usbtty_input;
  40. static circbuf_t usbtty_output;
  41. /*
  42. * Instance variables
  43. */
  44. static device_t usbttydev;
  45. static struct usb_device_instance device_instance[1];
  46. static struct usb_bus_instance bus_instance[1];
  47. static struct usb_configuration_instance config_instance[NUM_CONFIGS];
  48. static struct usb_interface_instance interface_instance[NUM_INTERFACES];
  49. static struct usb_alternate_instance alternate_instance[NUM_INTERFACES];
  50. static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1]; /* one extra for control endpoint */
  51. /*
  52. * Static allocation of urbs
  53. */
  54. #define RECV_ENDPOINT 1
  55. #define TX_ENDPOINT 2
  56. /*
  57. * Global flag
  58. */
  59. int usbtty_configured_flag = 0;
  60. /*
  61. * Serial number
  62. */
  63. static char serial_number[16];
  64. /*
  65. * Descriptors
  66. */
  67. static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
  68. static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
  69. static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
  70. static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
  71. static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
  72. static u8 wstrInterface[2 + 2*(sizeof(CONFIG_USBD_INTERFACE_STR)-1)];
  73. static struct usb_string_descriptor *usbtty_string_table[] = {
  74. (struct usb_string_descriptor*)wstrLang,
  75. (struct usb_string_descriptor*)wstrManufacturer,
  76. (struct usb_string_descriptor*)wstrProduct,
  77. (struct usb_string_descriptor*)wstrSerial,
  78. (struct usb_string_descriptor*)wstrConfiguration,
  79. (struct usb_string_descriptor*)wstrInterface
  80. };
  81. extern struct usb_string_descriptor **usb_strings; /* defined and used by omap1510_ep0.c */
  82. static struct usb_device_descriptor device_descriptor = {
  83. bLength: sizeof(struct usb_device_descriptor),
  84. bDescriptorType: USB_DT_DEVICE,
  85. bcdUSB: USB_BCD_VERSION,
  86. bDeviceClass: USBTTY_DEVICE_CLASS,
  87. bDeviceSubClass: USBTTY_DEVICE_SUBCLASS,
  88. bDeviceProtocol: USBTTY_DEVICE_PROTOCOL,
  89. bMaxPacketSize0: EP0_MAX_PACKET_SIZE,
  90. idVendor: CONFIG_USBD_VENDORID,
  91. idProduct: CONFIG_USBD_PRODUCTID,
  92. bcdDevice: USBTTY_BCD_DEVICE,
  93. iManufacturer: STR_MANUFACTURER,
  94. iProduct: STR_PRODUCT,
  95. iSerialNumber: STR_SERIAL,
  96. bNumConfigurations: NUM_CONFIGS
  97. };
  98. static struct usb_configuration_descriptor config_descriptors[NUM_CONFIGS] = {
  99. {
  100. bLength: sizeof(struct usb_configuration_descriptor),
  101. bDescriptorType: USB_DT_CONFIG,
  102. wTotalLength: (sizeof(struct usb_configuration_descriptor)*NUM_CONFIGS) +
  103. (sizeof(struct usb_interface_descriptor)*NUM_INTERFACES) +
  104. (sizeof(struct usb_endpoint_descriptor)*NUM_ENDPOINTS),
  105. bNumInterfaces: NUM_INTERFACES,
  106. bConfigurationValue: 1,
  107. iConfiguration: STR_CONFIG,
  108. bmAttributes: BMATTRIBUTE_SELF_POWERED | BMATTRIBUTE_RESERVED,
  109. bMaxPower: USBTTY_MAXPOWER
  110. },
  111. };
  112. static struct usb_interface_descriptor interface_descriptors[NUM_INTERFACES] = {
  113. {
  114. bLength: sizeof(struct usb_interface_descriptor),
  115. bDescriptorType: USB_DT_INTERFACE,
  116. bInterfaceNumber: 0,
  117. bAlternateSetting: 0,
  118. bNumEndpoints: NUM_ENDPOINTS,
  119. bInterfaceClass: USBTTY_INTERFACE_CLASS,
  120. bInterfaceSubClass: USBTTY_INTERFACE_SUBCLASS,
  121. bInterfaceProtocol: USBTTY_INTERFACE_PROTOCOL,
  122. iInterface: STR_INTERFACE
  123. },
  124. };
  125. static struct usb_endpoint_descriptor ep_descriptors[NUM_ENDPOINTS] = {
  126. {
  127. bLength: sizeof(struct usb_endpoint_descriptor),
  128. bDescriptorType: USB_DT_ENDPOINT,
  129. bEndpointAddress: CONFIG_USBD_SERIAL_OUT_ENDPOINT | USB_DIR_OUT,
  130. bmAttributes: USB_ENDPOINT_XFER_BULK,
  131. wMaxPacketSize: CONFIG_USBD_SERIAL_OUT_PKTSIZE,
  132. bInterval: 0
  133. },
  134. {
  135. bLength: sizeof(struct usb_endpoint_descriptor),
  136. bDescriptorType: USB_DT_ENDPOINT,
  137. bEndpointAddress: CONFIG_USBD_SERIAL_IN_ENDPOINT | USB_DIR_IN,
  138. bmAttributes: USB_ENDPOINT_XFER_BULK,
  139. wMaxPacketSize: CONFIG_USBD_SERIAL_IN_PKTSIZE,
  140. bInterval: 0
  141. },
  142. {
  143. bLength: sizeof(struct usb_endpoint_descriptor),
  144. bDescriptorType: USB_DT_ENDPOINT,
  145. bEndpointAddress: CONFIG_USBD_SERIAL_INT_ENDPOINT | USB_DIR_IN,
  146. bmAttributes: USB_ENDPOINT_XFER_INT,
  147. wMaxPacketSize: CONFIG_USBD_SERIAL_INT_PKTSIZE,
  148. bInterval: 0
  149. },
  150. };
  151. static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS] = {
  152. &(ep_descriptors[0]),
  153. &(ep_descriptors[1]),
  154. &(ep_descriptors[2]),
  155. };
  156. /* utility function for converting char* to wide string used by USB */
  157. static void str2wide (char *str, u16 * wide)
  158. {
  159. int i;
  160. for (i = 0; i < strlen (str) && str[i]; i++)
  161. wide[i] = (u16) str[i];
  162. }
  163. /*
  164. * Prototypes
  165. */
  166. static void usbtty_init_strings (void);
  167. static void usbtty_init_instances (void);
  168. static void usbtty_init_endpoints (void);
  169. static void usbtty_event_handler (struct usb_device_instance *device,
  170. usb_device_event_t event, int data);
  171. static int usbtty_configured (void);
  172. static int write_buffer (circbuf_t * buf);
  173. static int fill_buffer (circbuf_t * buf);
  174. void usbtty_poll (void);
  175. static void pretend_interrupts (void);
  176. /*
  177. * Test whether a character is in the RX buffer
  178. */
  179. int usbtty_tstc (void)
  180. {
  181. usbtty_poll ();
  182. return (usbtty_input.size > 0);
  183. }
  184. /*
  185. * Read a single byte from the usb client port. Returns 1 on success, 0
  186. * otherwise. When the function is succesfull, the character read is
  187. * written into its argument c.
  188. */
  189. int usbtty_getc (void)
  190. {
  191. char c;
  192. while (usbtty_input.size <= 0) {
  193. usbtty_poll ();
  194. }
  195. buf_pop (&usbtty_input, &c, 1);
  196. return c;
  197. }
  198. /*
  199. * Output a single byte to the usb client port.
  200. */
  201. void usbtty_putc (const char c)
  202. {
  203. buf_push (&usbtty_output, &c, 1);
  204. /* If \n, also do \r */
  205. if (c == '\n')
  206. buf_push (&usbtty_output, "\r", 1);
  207. /* Poll at end to handle new data... */
  208. if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
  209. usbtty_poll ();
  210. }
  211. }
  212. /* usbtty_puts() helper function for finding the next '\n' in a string */
  213. static int next_nl_pos (const char *s)
  214. {
  215. int i;
  216. for (i = 0; s[i] != '\0'; i++) {
  217. if (s[i] == '\n')
  218. return i;
  219. }
  220. return i;
  221. }
  222. /*
  223. * Output a string to the usb client port.
  224. */
  225. static void __usbtty_puts (const char *str, int len)
  226. {
  227. int maxlen = usbtty_output.totalsize;
  228. int space, n;
  229. /* break str into chunks < buffer size, if needed */
  230. while (len > 0) {
  231. space = maxlen - usbtty_output.size;
  232. /* Empty buffer here, if needed, to ensure space... */
  233. if (space <= 0) {
  234. write_buffer (&usbtty_output);
  235. space = maxlen - usbtty_output.size;
  236. if (space <= 0) {
  237. space = len; /* allow old data to be overwritten. */
  238. }
  239. }
  240. n = MIN (space, MIN (len, maxlen));
  241. buf_push (&usbtty_output, str, n);
  242. str += n;
  243. len -= n;
  244. }
  245. }
  246. void usbtty_puts (const char *str)
  247. {
  248. int n;
  249. int len = strlen (str);
  250. /* add '\r' for each '\n' */
  251. while (len > 0) {
  252. n = next_nl_pos (str);
  253. if (str[n] == '\n') {
  254. __usbtty_puts (str, n + 1);
  255. __usbtty_puts ("\r", 1);
  256. str += (n + 1);
  257. len -= (n + 1);
  258. } else {
  259. /* No \n found. All done. */
  260. __usbtty_puts (str, n);
  261. break;
  262. }
  263. }
  264. /* Poll at end to handle new data... */
  265. usbtty_poll ();
  266. }
  267. /*
  268. * Initialize the usb client port.
  269. *
  270. */
  271. int drv_usbtty_init (void)
  272. {
  273. int rc;
  274. char * sn;
  275. int snlen;
  276. if (!(sn = getenv("serial#"))) {
  277. sn = "000000000000";
  278. }
  279. snlen = strlen(sn);
  280. if (snlen > sizeof(serial_number) - 1) {
  281. printf ("Warning: serial number %s is too long (%d > %d)\n",
  282. sn, snlen, sizeof(serial_number) - 1);
  283. snlen = sizeof(serial_number) - 1;
  284. }
  285. memcpy (serial_number, sn, snlen);
  286. serial_number[snlen] = '\0';
  287. /* prepare buffers... */
  288. buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
  289. buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
  290. /* Now, set up USB controller and infrastructure */
  291. udc_init (); /* Basic USB initialization */
  292. usbtty_init_strings ();
  293. usbtty_init_instances ();
  294. udc_startup_events (device_instance); /* Enable our device, initialize udc pointers */
  295. udc_connect (); /* Enable pullup for host detection */
  296. usbtty_init_endpoints ();
  297. /* Device initialization */
  298. memset (&usbttydev, 0, sizeof (usbttydev));
  299. strcpy (usbttydev.name, "usbtty");
  300. usbttydev.ext = 0; /* No extensions */
  301. usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
  302. usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
  303. usbttydev.getc = usbtty_getc; /* 'getc' function */
  304. usbttydev.putc = usbtty_putc; /* 'putc' function */
  305. usbttydev.puts = usbtty_puts; /* 'puts' function */
  306. rc = device_register (&usbttydev);
  307. return (rc == 0) ? 1 : rc;
  308. }
  309. static void usbtty_init_strings (void)
  310. {
  311. struct usb_string_descriptor *string;
  312. string = (struct usb_string_descriptor *) wstrManufacturer;
  313. string->bLength = sizeof (wstrManufacturer);
  314. string->bDescriptorType = USB_DT_STRING;
  315. str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
  316. string = (struct usb_string_descriptor *) wstrProduct;
  317. string->bLength = sizeof (wstrProduct);
  318. string->bDescriptorType = USB_DT_STRING;
  319. str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
  320. string = (struct usb_string_descriptor *) wstrSerial;
  321. string->bLength = 2 + 2*strlen(serial_number);
  322. string->bDescriptorType = USB_DT_STRING;
  323. str2wide (serial_number, string->wData);
  324. string = (struct usb_string_descriptor *) wstrConfiguration;
  325. string->bLength = sizeof (wstrConfiguration);
  326. string->bDescriptorType = USB_DT_STRING;
  327. str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
  328. string = (struct usb_string_descriptor *) wstrInterface;
  329. string->bLength = sizeof (wstrInterface);
  330. string->bDescriptorType = USB_DT_STRING;
  331. str2wide (CONFIG_USBD_INTERFACE_STR, string->wData);
  332. /* Now, initialize the string table for ep0 handling */
  333. usb_strings = usbtty_string_table;
  334. }
  335. static void usbtty_init_instances (void)
  336. {
  337. int i;
  338. /* initialize device instance */
  339. memset (device_instance, 0, sizeof (struct usb_device_instance));
  340. device_instance->device_state = STATE_INIT;
  341. device_instance->device_descriptor = &device_descriptor;
  342. device_instance->event = usbtty_event_handler;
  343. device_instance->bus = bus_instance;
  344. device_instance->configurations = NUM_CONFIGS;
  345. device_instance->configuration_instance_array = config_instance;
  346. /* initialize bus instance */
  347. memset (bus_instance, 0, sizeof (struct usb_bus_instance));
  348. bus_instance->device = device_instance;
  349. bus_instance->endpoint_array = endpoint_instance;
  350. bus_instance->max_endpoints = 1;
  351. bus_instance->maxpacketsize = 64;
  352. bus_instance->serial_number_str = serial_number;
  353. /* configuration instance */
  354. memset (config_instance, 0,
  355. sizeof (struct usb_configuration_instance));
  356. config_instance->interfaces = NUM_INTERFACES;
  357. config_instance->configuration_descriptor = config_descriptors;
  358. config_instance->interface_instance_array = interface_instance;
  359. /* interface instance */
  360. memset (interface_instance, 0,
  361. sizeof (struct usb_interface_instance));
  362. interface_instance->alternates = 1;
  363. interface_instance->alternates_instance_array = alternate_instance;
  364. /* alternates instance */
  365. memset (alternate_instance, 0,
  366. sizeof (struct usb_alternate_instance));
  367. alternate_instance->interface_descriptor = interface_descriptors;
  368. alternate_instance->endpoints = NUM_ENDPOINTS;
  369. alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
  370. /* endpoint instances */
  371. memset (&endpoint_instance[0], 0,
  372. sizeof (struct usb_endpoint_instance));
  373. endpoint_instance[0].endpoint_address = 0;
  374. endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
  375. endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
  376. endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
  377. endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
  378. udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
  379. for (i = 1; i <= NUM_ENDPOINTS; i++) {
  380. memset (&endpoint_instance[i], 0,
  381. sizeof (struct usb_endpoint_instance));
  382. endpoint_instance[i].endpoint_address =
  383. ep_descriptors[i - 1].bEndpointAddress;
  384. endpoint_instance[i].rcv_packetSize =
  385. ep_descriptors[i - 1].wMaxPacketSize;
  386. endpoint_instance[i].rcv_attributes =
  387. ep_descriptors[i - 1].bmAttributes;
  388. endpoint_instance[i].tx_packetSize =
  389. ep_descriptors[i - 1].wMaxPacketSize;
  390. endpoint_instance[i].tx_attributes =
  391. ep_descriptors[i - 1].bmAttributes;
  392. urb_link_init (&endpoint_instance[i].rcv);
  393. urb_link_init (&endpoint_instance[i].rdy);
  394. urb_link_init (&endpoint_instance[i].tx);
  395. urb_link_init (&endpoint_instance[i].done);
  396. if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
  397. endpoint_instance[i].tx_urb =
  398. usbd_alloc_urb (device_instance,
  399. &endpoint_instance[i]);
  400. else
  401. endpoint_instance[i].rcv_urb =
  402. usbd_alloc_urb (device_instance,
  403. &endpoint_instance[i]);
  404. }
  405. }
  406. static void usbtty_init_endpoints (void)
  407. {
  408. int i;
  409. bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
  410. for (i = 0; i <= NUM_ENDPOINTS; i++) {
  411. udc_setup_ep (device_instance, i, &endpoint_instance[i]);
  412. }
  413. }
  414. /*********************************************************************************/
  415. static struct urb *next_urb (struct usb_device_instance *device,
  416. struct usb_endpoint_instance *endpoint)
  417. {
  418. struct urb *current_urb = NULL;
  419. int space;
  420. /* If there's a queue, then we should add to the last urb */
  421. if (!endpoint->tx_queue) {
  422. current_urb = endpoint->tx_urb;
  423. } else {
  424. /* Last urb from tx chain */
  425. current_urb =
  426. p2surround (struct urb, link, endpoint->tx.prev);
  427. }
  428. /* Make sure this one has enough room */
  429. space = current_urb->buffer_length - current_urb->actual_length;
  430. if (space > 0) {
  431. return current_urb;
  432. } else { /* No space here */
  433. /* First look at done list */
  434. current_urb = first_urb_detached (&endpoint->done);
  435. if (!current_urb) {
  436. current_urb = usbd_alloc_urb (device, endpoint);
  437. }
  438. urb_append (&endpoint->tx, current_urb);
  439. endpoint->tx_queue++;
  440. }
  441. return current_urb;
  442. }
  443. static int write_buffer (circbuf_t * buf)
  444. {
  445. if (!usbtty_configured ()) {
  446. return 0;
  447. }
  448. if (buf->size) {
  449. struct usb_endpoint_instance *endpoint =
  450. &endpoint_instance[TX_ENDPOINT];
  451. struct urb *current_urb = NULL;
  452. char *dest;
  453. int space_avail;
  454. int popnum, popped;
  455. int total = 0;
  456. /* Break buffer into urb sized pieces, and link each to the endpoint */
  457. while (buf->size > 0) {
  458. current_urb = next_urb (device_instance, endpoint);
  459. if (!current_urb) {
  460. TTYERR ("current_urb is NULL, buf->size %d\n",
  461. buf->size);
  462. return total;
  463. }
  464. dest = current_urb->buffer +
  465. current_urb->actual_length;
  466. space_avail =
  467. current_urb->buffer_length -
  468. current_urb->actual_length;
  469. popnum = MIN (space_avail, buf->size);
  470. if (popnum == 0)
  471. break;
  472. popped = buf_pop (buf, dest, popnum);
  473. if (popped == 0)
  474. break;
  475. current_urb->actual_length += popped;
  476. total += popped;
  477. /* If endpoint->last == 0, then transfers have not started on this endpoint */
  478. if (endpoint->last == 0) {
  479. udc_endpoint_write (endpoint);
  480. }
  481. } /* end while */
  482. return total;
  483. } /* end if tx_urb */
  484. return 0;
  485. }
  486. static int fill_buffer (circbuf_t * buf)
  487. {
  488. struct usb_endpoint_instance *endpoint =
  489. &endpoint_instance[RECV_ENDPOINT];
  490. if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
  491. unsigned int nb = endpoint->rcv_urb->actual_length;
  492. char *src = (char *) endpoint->rcv_urb->buffer;
  493. buf_push (buf, src, nb);
  494. endpoint->rcv_urb->actual_length = 0;
  495. return nb;
  496. }
  497. return 0;
  498. }
  499. static int usbtty_configured (void)
  500. {
  501. return usbtty_configured_flag;
  502. }
  503. /*********************************************************************************/
  504. static void usbtty_event_handler (struct usb_device_instance *device,
  505. usb_device_event_t event, int data)
  506. {
  507. switch (event) {
  508. case DEVICE_RESET:
  509. case DEVICE_BUS_INACTIVE:
  510. usbtty_configured_flag = 0;
  511. break;
  512. case DEVICE_CONFIGURED:
  513. usbtty_configured_flag = 1;
  514. break;
  515. case DEVICE_ADDRESS_ASSIGNED:
  516. usbtty_init_endpoints ();
  517. default:
  518. break;
  519. }
  520. }
  521. /*********************************************************************************/
  522. /*
  523. * Since interrupt handling has not yet been implemented, we use this function
  524. * to handle polling. This is called by the tstc,getc,putc,puts routines to
  525. * update the USB state.
  526. */
  527. void usbtty_poll (void)
  528. {
  529. /* New interrupts? */
  530. pretend_interrupts ();
  531. /* Write any output data to host buffer (do this before checking interrupts to avoid missing one) */
  532. if (usbtty_configured ()) {
  533. write_buffer (&usbtty_output);
  534. }
  535. /* New interrupts? */
  536. pretend_interrupts ();
  537. /* Check for new data from host.. (do this after checking interrupts to get latest data) */
  538. if (usbtty_configured ()) {
  539. fill_buffer (&usbtty_input);
  540. }
  541. /* New interrupts? */
  542. pretend_interrupts ();
  543. }
  544. static void pretend_interrupts (void)
  545. {
  546. /* Loop while we have interrupts.
  547. * If we don't do this, the input chain
  548. * polling delay is likely to miss
  549. * host requests.
  550. */
  551. while (inw (UDC_IRQ_SRC) & ~UDC_SOF_Flg) {
  552. /* Handle any new IRQs */
  553. omap1510_udc_irq ();
  554. omap1510_udc_noniso_irq ();
  555. }
  556. }
  557. #endif