usbtty.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * (C) Copyright 2003
  3. * Gerry Hamel, geh@ti.com, Texas Instruments
  4. *
  5. * (C) Copyright 2006
  6. * Bryan O'Donoghue, bodonoghue@codehermit.ie
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <common.h>
  24. #include <config.h>
  25. #include <circbuf.h>
  26. #include <devices.h>
  27. #include "usbtty.h"
  28. #include "usb_cdc_acm.h"
  29. #include "usbdescriptors.h"
  30. #ifdef DEBUG
  31. #define TTYDBG(fmt,args...)\
  32. serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
  33. #else
  34. #define TTYDBG(fmt,args...) do{}while(0)
  35. #endif
  36. #if 1
  37. #define TTYERR(fmt,args...)\
  38. serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
  39. __LINE__,##args)
  40. #else
  41. #define TTYERR(fmt,args...) do{}while(0)
  42. #endif
  43. /*
  44. * Defines
  45. */
  46. #define NUM_CONFIGS 1
  47. #define MAX_INTERFACES 2
  48. #define NUM_ENDPOINTS 3
  49. #define ACM_TX_ENDPOINT 3
  50. #define ACM_RX_ENDPOINT 2
  51. #define GSERIAL_TX_ENDPOINT 2
  52. #define GSERIAL_RX_ENDPOINT 1
  53. #define NUM_ACM_INTERFACES 2
  54. #define NUM_GSERIAL_INTERFACES 1
  55. #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
  56. #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
  57. /*
  58. * Buffers to hold input and output data
  59. */
  60. #define USBTTY_BUFFER_SIZE 256
  61. static circbuf_t usbtty_input;
  62. static circbuf_t usbtty_output;
  63. /*
  64. * Instance variables
  65. */
  66. static device_t usbttydev;
  67. static struct usb_device_instance device_instance[1];
  68. static struct usb_bus_instance bus_instance[1];
  69. static struct usb_configuration_instance config_instance[NUM_CONFIGS];
  70. static struct usb_interface_instance interface_instance[MAX_INTERFACES];
  71. static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
  72. /* one extra for control endpoint */
  73. static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
  74. /*
  75. * Global flag
  76. */
  77. int usbtty_configured_flag = 0;
  78. /*
  79. * Serial number
  80. */
  81. static char serial_number[16];
  82. /*
  83. * Descriptors, Strings, Local variables.
  84. */
  85. /* defined and used by usbdcore_ep0.c */
  86. extern struct usb_string_descriptor **usb_strings;
  87. /* Indicies, References */
  88. static unsigned short rx_endpoint = 0;
  89. static unsigned short tx_endpoint = 0;
  90. static unsigned short interface_count = 0;
  91. static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
  92. /* USB Descriptor Strings */
  93. static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
  94. static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
  95. static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
  96. static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
  97. static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
  98. static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
  99. static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
  100. /* Standard USB Data Structures */
  101. static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
  102. static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
  103. static struct usb_configuration_descriptor *configuration_descriptor = 0;
  104. static struct usb_device_descriptor device_descriptor = {
  105. .bLength = sizeof(struct usb_device_descriptor),
  106. .bDescriptorType = USB_DT_DEVICE,
  107. .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
  108. .bDeviceSubClass = 0x00,
  109. .bDeviceProtocol = 0x00,
  110. .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
  111. .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
  112. .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
  113. .iManufacturer = STR_MANUFACTURER,
  114. .iProduct = STR_PRODUCT,
  115. .iSerialNumber = STR_SERIAL,
  116. .bNumConfigurations = NUM_CONFIGS
  117. };
  118. /*
  119. * Static CDC ACM specific descriptors
  120. */
  121. struct acm_config_desc {
  122. struct usb_configuration_descriptor configuration_desc;
  123. /* Master Interface */
  124. struct usb_interface_descriptor interface_desc;
  125. struct usb_class_header_function_descriptor usb_class_header;
  126. struct usb_class_call_management_descriptor usb_class_call_mgt;
  127. struct usb_class_abstract_control_descriptor usb_class_acm;
  128. struct usb_class_union_function_descriptor usb_class_union;
  129. struct usb_endpoint_descriptor notification_endpoint;
  130. /* Slave Interface */
  131. struct usb_interface_descriptor data_class_interface;
  132. struct usb_endpoint_descriptor
  133. data_endpoints[NUM_ENDPOINTS-1] __attribute__((packed));
  134. } __attribute__((packed));
  135. static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
  136. {
  137. .configuration_desc ={
  138. .bLength =
  139. sizeof(struct usb_configuration_descriptor),
  140. .bDescriptorType = USB_DT_CONFIG,
  141. .wTotalLength =
  142. cpu_to_le16(sizeof(struct acm_config_desc)),
  143. .bNumInterfaces = NUM_ACM_INTERFACES,
  144. .bConfigurationValue = 1,
  145. .iConfiguration = STR_CONFIG,
  146. .bmAttributes =
  147. BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
  148. .bMaxPower = USBTTY_MAXPOWER
  149. },
  150. /* Interface 1 */
  151. .interface_desc = {
  152. .bLength = sizeof(struct usb_interface_descriptor),
  153. .bDescriptorType = USB_DT_INTERFACE,
  154. .bInterfaceNumber = 0,
  155. .bAlternateSetting = 0,
  156. .bNumEndpoints = 0x01,
  157. .bInterfaceClass =
  158. COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
  159. .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
  160. .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
  161. .iInterface = STR_CTRL_INTERFACE,
  162. },
  163. .usb_class_header = {
  164. .bFunctionLength =
  165. sizeof(struct usb_class_header_function_descriptor),
  166. .bDescriptorType = CS_INTERFACE,
  167. .bDescriptorSubtype = USB_ST_HEADER,
  168. .bcdCDC = cpu_to_le16(110),
  169. },
  170. .usb_class_call_mgt = {
  171. .bFunctionLength =
  172. sizeof(struct usb_class_call_management_descriptor),
  173. .bDescriptorType = CS_INTERFACE,
  174. .bDescriptorSubtype = USB_ST_CMF,
  175. .bmCapabilities = 0x00,
  176. .bDataInterface = 0x01,
  177. },
  178. .usb_class_acm = {
  179. .bFunctionLength =
  180. sizeof(struct usb_class_abstract_control_descriptor),
  181. .bDescriptorType = CS_INTERFACE,
  182. .bDescriptorSubtype = USB_ST_ACMF,
  183. .bmCapabilities = 0x00,
  184. },
  185. .usb_class_union = {
  186. .bFunctionLength =
  187. sizeof(struct usb_class_union_function_descriptor),
  188. .bDescriptorType = CS_INTERFACE,
  189. .bDescriptorSubtype = USB_ST_UF,
  190. .bMasterInterface = 0x00,
  191. .bSlaveInterface0 = 0x01,
  192. },
  193. .notification_endpoint = {
  194. .bLength =
  195. sizeof(struct usb_endpoint_descriptor),
  196. .bDescriptorType = USB_DT_ENDPOINT,
  197. .bEndpointAddress = 0x01 | USB_DIR_IN,
  198. .bmAttributes = USB_ENDPOINT_XFER_INT,
  199. .wMaxPacketSize
  200. = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
  201. .bInterval = 0xFF,
  202. },
  203. /* Interface 2 */
  204. .data_class_interface = {
  205. .bLength =
  206. sizeof(struct usb_interface_descriptor),
  207. .bDescriptorType = USB_DT_INTERFACE,
  208. .bInterfaceNumber = 0x01,
  209. .bAlternateSetting = 0x00,
  210. .bNumEndpoints = 0x02,
  211. .bInterfaceClass =
  212. COMMUNICATIONS_INTERFACE_CLASS_DATA,
  213. .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
  214. .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
  215. .iInterface = STR_DATA_INTERFACE,
  216. },
  217. .data_endpoints = {
  218. {
  219. .bLength =
  220. sizeof(struct usb_endpoint_descriptor),
  221. .bDescriptorType = USB_DT_ENDPOINT,
  222. .bEndpointAddress = 0x02 | USB_DIR_OUT,
  223. .bmAttributes =
  224. USB_ENDPOINT_XFER_BULK,
  225. .wMaxPacketSize =
  226. cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
  227. .bInterval = 0xFF,
  228. },
  229. {
  230. .bLength =
  231. sizeof(struct usb_endpoint_descriptor),
  232. .bDescriptorType = USB_DT_ENDPOINT,
  233. .bEndpointAddress = 0x03 | USB_DIR_IN,
  234. .bmAttributes =
  235. USB_ENDPOINT_XFER_BULK,
  236. .wMaxPacketSize =
  237. cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
  238. .bInterval = 0xFF,
  239. },
  240. },
  241. },
  242. };
  243. static struct rs232_emu rs232_desc={
  244. .dter = 115200,
  245. .stop_bits = 0x00,
  246. .parity = 0x00,
  247. .data_bits = 0x08
  248. };
  249. /*
  250. * Static Generic Serial specific data
  251. */
  252. struct gserial_config_desc {
  253. struct usb_configuration_descriptor configuration_desc;
  254. struct usb_interface_descriptor
  255. interface_desc[NUM_GSERIAL_INTERFACES] __attribute__((packed));
  256. struct usb_endpoint_descriptor
  257. data_endpoints[NUM_ENDPOINTS] __attribute__((packed));
  258. } __attribute__((packed));
  259. static struct gserial_config_desc
  260. gserial_configuration_descriptors[NUM_CONFIGS] ={
  261. {
  262. .configuration_desc ={
  263. .bLength = sizeof(struct usb_configuration_descriptor),
  264. .bDescriptorType = USB_DT_CONFIG,
  265. .wTotalLength =
  266. cpu_to_le16(sizeof(struct gserial_config_desc)),
  267. .bNumInterfaces = NUM_GSERIAL_INTERFACES,
  268. .bConfigurationValue = 1,
  269. .iConfiguration = STR_CONFIG,
  270. .bmAttributes =
  271. BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
  272. .bMaxPower = USBTTY_MAXPOWER
  273. },
  274. .interface_desc = {
  275. {
  276. .bLength =
  277. sizeof(struct usb_interface_descriptor),
  278. .bDescriptorType = USB_DT_INTERFACE,
  279. .bInterfaceNumber = 0,
  280. .bAlternateSetting = 0,
  281. .bNumEndpoints = NUM_ENDPOINTS,
  282. .bInterfaceClass =
  283. COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
  284. .bInterfaceSubClass =
  285. COMMUNICATIONS_NO_SUBCLASS,
  286. .bInterfaceProtocol =
  287. COMMUNICATIONS_NO_PROTOCOL,
  288. .iInterface = STR_DATA_INTERFACE
  289. },
  290. },
  291. .data_endpoints = {
  292. {
  293. .bLength =
  294. sizeof(struct usb_endpoint_descriptor),
  295. .bDescriptorType = USB_DT_ENDPOINT,
  296. .bEndpointAddress = 0x01 | USB_DIR_OUT,
  297. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  298. .wMaxPacketSize =
  299. cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
  300. .bInterval= 0xFF,
  301. },
  302. {
  303. .bLength =
  304. sizeof(struct usb_endpoint_descriptor),
  305. .bDescriptorType = USB_DT_ENDPOINT,
  306. .bEndpointAddress = 0x02 | USB_DIR_IN,
  307. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  308. .wMaxPacketSize =
  309. cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
  310. .bInterval = 0xFF,
  311. },
  312. {
  313. .bLength =
  314. sizeof(struct usb_endpoint_descriptor),
  315. .bDescriptorType = USB_DT_ENDPOINT,
  316. .bEndpointAddress = 0x03 | USB_DIR_IN,
  317. .bmAttributes = USB_ENDPOINT_XFER_INT,
  318. .wMaxPacketSize =
  319. cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
  320. .bInterval = 0xFF,
  321. },
  322. },
  323. },
  324. };
  325. /*
  326. * Static Function Prototypes
  327. */
  328. static void usbtty_init_strings (void);
  329. static void usbtty_init_instances (void);
  330. static void usbtty_init_endpoints (void);
  331. static void usbtty_init_terminal_type(short type);
  332. static void usbtty_event_handler (struct usb_device_instance *device,
  333. usb_device_event_t event, int data);
  334. static int usbtty_cdc_setup(struct usb_device_request *request,
  335. struct urb *urb);
  336. static int usbtty_configured (void);
  337. static int write_buffer (circbuf_t * buf);
  338. static int fill_buffer (circbuf_t * buf);
  339. void usbtty_poll (void);
  340. /* utility function for converting char* to wide string used by USB */
  341. static void str2wide (char *str, u16 * wide)
  342. {
  343. int i;
  344. for (i = 0; i < strlen (str) && str[i]; i++){
  345. #if defined(__LITTLE_ENDIAN)
  346. wide[i] = (u16) str[i];
  347. #elif defined(__BIG_ENDIAN)
  348. wide[i] = ((u16)(str[i])<<8);
  349. #else
  350. #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
  351. #endif
  352. }
  353. }
  354. /*
  355. * Test whether a character is in the RX buffer
  356. */
  357. int usbtty_tstc (void)
  358. {
  359. struct usb_endpoint_instance *endpoint =
  360. &endpoint_instance[rx_endpoint];
  361. /* If no input data exists, allow more RX to be accepted */
  362. if(usbtty_input.size <= 0){
  363. udc_unset_nak(endpoint->endpoint_address&0x03);
  364. }
  365. usbtty_poll ();
  366. return (usbtty_input.size > 0);
  367. }
  368. /*
  369. * Read a single byte from the usb client port. Returns 1 on success, 0
  370. * otherwise. When the function is succesfull, the character read is
  371. * written into its argument c.
  372. */
  373. int usbtty_getc (void)
  374. {
  375. char c;
  376. struct usb_endpoint_instance *endpoint =
  377. &endpoint_instance[rx_endpoint];
  378. while (usbtty_input.size <= 0) {
  379. udc_unset_nak(endpoint->endpoint_address&0x03);
  380. usbtty_poll ();
  381. }
  382. buf_pop (&usbtty_input, &c, 1);
  383. udc_set_nak(endpoint->endpoint_address&0x03);
  384. return c;
  385. }
  386. /*
  387. * Output a single byte to the usb client port.
  388. */
  389. void usbtty_putc (const char c)
  390. {
  391. buf_push (&usbtty_output, &c, 1);
  392. /* If \n, also do \r */
  393. if (c == '\n')
  394. buf_push (&usbtty_output, "\r", 1);
  395. /* Poll at end to handle new data... */
  396. if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
  397. usbtty_poll ();
  398. }
  399. }
  400. /* usbtty_puts() helper function for finding the next '\n' in a string */
  401. static int next_nl_pos (const char *s)
  402. {
  403. int i;
  404. for (i = 0; s[i] != '\0'; i++) {
  405. if (s[i] == '\n')
  406. return i;
  407. }
  408. return i;
  409. }
  410. /*
  411. * Output a string to the usb client port - implementing flow control
  412. */
  413. static void __usbtty_puts (const char *str, int len)
  414. {
  415. int maxlen = usbtty_output.totalsize;
  416. int space, n;
  417. /* break str into chunks < buffer size, if needed */
  418. while (len > 0) {
  419. usbtty_poll ();
  420. space = maxlen - usbtty_output.size;
  421. /* Empty buffer here, if needed, to ensure space... */
  422. if (space) {
  423. write_buffer (&usbtty_output);
  424. n = MIN (space, MIN (len, maxlen));
  425. buf_push (&usbtty_output, str, n);
  426. str += n;
  427. len -= n;
  428. }
  429. }
  430. }
  431. void usbtty_puts (const char *str)
  432. {
  433. int n;
  434. int len = strlen (str);
  435. /* add '\r' for each '\n' */
  436. while (len > 0) {
  437. n = next_nl_pos (str);
  438. if (str[n] == '\n') {
  439. __usbtty_puts (str, n + 1);
  440. __usbtty_puts ("\r", 1);
  441. str += (n + 1);
  442. len -= (n + 1);
  443. } else {
  444. /* No \n found. All done. */
  445. __usbtty_puts (str, n);
  446. break;
  447. }
  448. }
  449. /* Poll at end to handle new data... */
  450. usbtty_poll ();
  451. }
  452. /*
  453. * Initialize the usb client port.
  454. *
  455. */
  456. int drv_usbtty_init (void)
  457. {
  458. int rc;
  459. char * sn;
  460. char * tt;
  461. int snlen;
  462. /* Ger seiral number */
  463. if (!(sn = getenv("serial#"))) {
  464. sn = "000000000000";
  465. }
  466. snlen = strlen(sn);
  467. if (snlen > sizeof(serial_number) - 1) {
  468. printf ("Warning: serial number %s is too long (%d > %lu)\n",
  469. sn, snlen, (ulong)(sizeof(serial_number) - 1));
  470. snlen = sizeof(serial_number) - 1;
  471. }
  472. memcpy (serial_number, sn, snlen);
  473. serial_number[snlen] = '\0';
  474. /* Decide on which type of UDC device to be.
  475. */
  476. if(!(tt = getenv("usbtty"))) {
  477. tt = "generic";
  478. }
  479. usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
  480. /* prepare buffers... */
  481. buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
  482. buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
  483. /* Now, set up USB controller and infrastructure */
  484. udc_init (); /* Basic USB initialization */
  485. usbtty_init_strings ();
  486. usbtty_init_instances ();
  487. udc_startup_events (device_instance);/* Enable dev, init udc pointers */
  488. udc_connect (); /* Enable pullup for host detection */
  489. usbtty_init_endpoints ();
  490. /* Device initialization */
  491. memset (&usbttydev, 0, sizeof (usbttydev));
  492. strcpy (usbttydev.name, "usbtty");
  493. usbttydev.ext = 0; /* No extensions */
  494. usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
  495. usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
  496. usbttydev.getc = usbtty_getc; /* 'getc' function */
  497. usbttydev.putc = usbtty_putc; /* 'putc' function */
  498. usbttydev.puts = usbtty_puts; /* 'puts' function */
  499. rc = device_register (&usbttydev);
  500. return (rc == 0) ? 1 : rc;
  501. }
  502. static void usbtty_init_strings (void)
  503. {
  504. struct usb_string_descriptor *string;
  505. usbtty_string_table[STR_LANG] =
  506. (struct usb_string_descriptor*)wstrLang;
  507. string = (struct usb_string_descriptor *) wstrManufacturer;
  508. string->bLength = sizeof(wstrManufacturer);
  509. string->bDescriptorType = USB_DT_STRING;
  510. str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
  511. usbtty_string_table[STR_MANUFACTURER]=string;
  512. string = (struct usb_string_descriptor *) wstrProduct;
  513. string->bLength = sizeof(wstrProduct);
  514. string->bDescriptorType = USB_DT_STRING;
  515. str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
  516. usbtty_string_table[STR_PRODUCT]=string;
  517. string = (struct usb_string_descriptor *) wstrSerial;
  518. string->bLength = sizeof(serial_number);
  519. string->bDescriptorType = USB_DT_STRING;
  520. str2wide (serial_number, string->wData);
  521. usbtty_string_table[STR_SERIAL]=string;
  522. string = (struct usb_string_descriptor *) wstrConfiguration;
  523. string->bLength = sizeof(wstrConfiguration);
  524. string->bDescriptorType = USB_DT_STRING;
  525. str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
  526. usbtty_string_table[STR_CONFIG]=string;
  527. string = (struct usb_string_descriptor *) wstrDataInterface;
  528. string->bLength = sizeof(wstrDataInterface);
  529. string->bDescriptorType = USB_DT_STRING;
  530. str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
  531. usbtty_string_table[STR_DATA_INTERFACE]=string;
  532. string = (struct usb_string_descriptor *) wstrCtrlInterface;
  533. string->bLength = sizeof(wstrCtrlInterface);
  534. string->bDescriptorType = USB_DT_STRING;
  535. str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
  536. usbtty_string_table[STR_CTRL_INTERFACE]=string;
  537. /* Now, initialize the string table for ep0 handling */
  538. usb_strings = usbtty_string_table;
  539. }
  540. static void usbtty_init_instances (void)
  541. {
  542. int i;
  543. /* initialize device instance */
  544. memset (device_instance, 0, sizeof (struct usb_device_instance));
  545. device_instance->device_state = STATE_INIT;
  546. device_instance->device_descriptor = &device_descriptor;
  547. device_instance->event = usbtty_event_handler;
  548. device_instance->cdc_recv_setup = usbtty_cdc_setup;
  549. device_instance->bus = bus_instance;
  550. device_instance->configurations = NUM_CONFIGS;
  551. device_instance->configuration_instance_array = config_instance;
  552. /* initialize bus instance */
  553. memset (bus_instance, 0, sizeof (struct usb_bus_instance));
  554. bus_instance->device = device_instance;
  555. bus_instance->endpoint_array = endpoint_instance;
  556. bus_instance->max_endpoints = 1;
  557. bus_instance->maxpacketsize = 64;
  558. bus_instance->serial_number_str = serial_number;
  559. /* configuration instance */
  560. memset (config_instance, 0,
  561. sizeof (struct usb_configuration_instance));
  562. config_instance->interfaces = interface_count;
  563. config_instance->configuration_descriptor = configuration_descriptor;
  564. config_instance->interface_instance_array = interface_instance;
  565. /* interface instance */
  566. memset (interface_instance, 0,
  567. sizeof (struct usb_interface_instance));
  568. interface_instance->alternates = 1;
  569. interface_instance->alternates_instance_array = alternate_instance;
  570. /* alternates instance */
  571. memset (alternate_instance, 0,
  572. sizeof (struct usb_alternate_instance));
  573. alternate_instance->interface_descriptor = interface_descriptors;
  574. alternate_instance->endpoints = NUM_ENDPOINTS;
  575. alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
  576. /* endpoint instances */
  577. memset (&endpoint_instance[0], 0,
  578. sizeof (struct usb_endpoint_instance));
  579. endpoint_instance[0].endpoint_address = 0;
  580. endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
  581. endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
  582. endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
  583. endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
  584. udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
  585. for (i = 1; i <= NUM_ENDPOINTS; i++) {
  586. memset (&endpoint_instance[i], 0,
  587. sizeof (struct usb_endpoint_instance));
  588. endpoint_instance[i].endpoint_address =
  589. ep_descriptor_ptrs[i - 1]->bEndpointAddress;
  590. endpoint_instance[i].rcv_attributes =
  591. ep_descriptor_ptrs[i - 1]->bmAttributes;
  592. endpoint_instance[i].rcv_packetSize =
  593. le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
  594. endpoint_instance[i].tx_attributes =
  595. ep_descriptor_ptrs[i - 1]->bmAttributes;
  596. endpoint_instance[i].tx_packetSize =
  597. le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
  598. endpoint_instance[i].tx_attributes =
  599. ep_descriptor_ptrs[i - 1]->bmAttributes;
  600. urb_link_init (&endpoint_instance[i].rcv);
  601. urb_link_init (&endpoint_instance[i].rdy);
  602. urb_link_init (&endpoint_instance[i].tx);
  603. urb_link_init (&endpoint_instance[i].done);
  604. if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
  605. endpoint_instance[i].tx_urb =
  606. usbd_alloc_urb (device_instance,
  607. &endpoint_instance[i]);
  608. else
  609. endpoint_instance[i].rcv_urb =
  610. usbd_alloc_urb (device_instance,
  611. &endpoint_instance[i]);
  612. }
  613. }
  614. static void usbtty_init_endpoints (void)
  615. {
  616. int i;
  617. bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
  618. for (i = 1; i <= NUM_ENDPOINTS; i++) {
  619. udc_setup_ep (device_instance, i, &endpoint_instance[i]);
  620. }
  621. }
  622. /* usbtty_init_terminal_type
  623. *
  624. * Do some late binding for our device type.
  625. */
  626. static void usbtty_init_terminal_type(short type)
  627. {
  628. switch(type){
  629. /* CDC ACM */
  630. case 0:
  631. /* Assign endpoint descriptors */
  632. ep_descriptor_ptrs[0] =
  633. &acm_configuration_descriptors[0].notification_endpoint;
  634. ep_descriptor_ptrs[1] =
  635. &acm_configuration_descriptors[0].data_endpoints[0];
  636. ep_descriptor_ptrs[2] =
  637. &acm_configuration_descriptors[0].data_endpoints[1];
  638. /* Enumerate Device Descriptor */
  639. device_descriptor.bDeviceClass =
  640. COMMUNICATIONS_DEVICE_CLASS;
  641. device_descriptor.idProduct =
  642. cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
  643. /* Assign endpoint indices */
  644. tx_endpoint = ACM_TX_ENDPOINT;
  645. rx_endpoint = ACM_RX_ENDPOINT;
  646. /* Configuration Descriptor */
  647. configuration_descriptor =
  648. (struct usb_configuration_descriptor*)
  649. &acm_configuration_descriptors;
  650. /* Interface count */
  651. interface_count = NUM_ACM_INTERFACES;
  652. break;
  653. /* BULK IN/OUT & Default */
  654. case 1:
  655. default:
  656. /* Assign endpoint descriptors */
  657. ep_descriptor_ptrs[0] =
  658. &gserial_configuration_descriptors[0].data_endpoints[0];
  659. ep_descriptor_ptrs[1] =
  660. &gserial_configuration_descriptors[0].data_endpoints[1];
  661. ep_descriptor_ptrs[2] =
  662. &gserial_configuration_descriptors[0].data_endpoints[2];
  663. /* Enumerate Device Descriptor */
  664. device_descriptor.bDeviceClass = 0xFF;
  665. device_descriptor.idProduct =
  666. cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
  667. /* Assign endpoint indices */
  668. tx_endpoint = GSERIAL_TX_ENDPOINT;
  669. rx_endpoint = GSERIAL_RX_ENDPOINT;
  670. /* Configuration Descriptor */
  671. configuration_descriptor =
  672. (struct usb_configuration_descriptor*)
  673. &gserial_configuration_descriptors;
  674. /* Interface count */
  675. interface_count = NUM_GSERIAL_INTERFACES;
  676. break;
  677. }
  678. }
  679. /******************************************************************************/
  680. static struct urb *next_urb (struct usb_device_instance *device,
  681. struct usb_endpoint_instance *endpoint)
  682. {
  683. struct urb *current_urb = NULL;
  684. int space;
  685. /* If there's a queue, then we should add to the last urb */
  686. if (!endpoint->tx_queue) {
  687. current_urb = endpoint->tx_urb;
  688. } else {
  689. /* Last urb from tx chain */
  690. current_urb =
  691. p2surround (struct urb, link, endpoint->tx.prev);
  692. }
  693. /* Make sure this one has enough room */
  694. space = current_urb->buffer_length - current_urb->actual_length;
  695. if (space > 0) {
  696. return current_urb;
  697. } else { /* No space here */
  698. /* First look at done list */
  699. current_urb = first_urb_detached (&endpoint->done);
  700. if (!current_urb) {
  701. current_urb = usbd_alloc_urb (device, endpoint);
  702. }
  703. urb_append (&endpoint->tx, current_urb);
  704. endpoint->tx_queue++;
  705. }
  706. return current_urb;
  707. }
  708. static int write_buffer (circbuf_t * buf)
  709. {
  710. if (!usbtty_configured ()) {
  711. return 0;
  712. }
  713. struct usb_endpoint_instance *endpoint =
  714. &endpoint_instance[tx_endpoint];
  715. struct urb *current_urb = NULL;
  716. current_urb = next_urb (device_instance, endpoint);
  717. /* TX data still exists - send it now
  718. */
  719. if(endpoint->sent < current_urb->actual_length){
  720. if(udc_endpoint_write (endpoint)){
  721. /* Write pre-empted by RX */
  722. return -1;
  723. }
  724. }
  725. if (buf->size) {
  726. char *dest;
  727. int space_avail;
  728. int popnum, popped;
  729. int total = 0;
  730. /* Break buffer into urb sized pieces,
  731. * and link each to the endpoint
  732. */
  733. while (buf->size > 0) {
  734. if (!current_urb) {
  735. TTYERR ("current_urb is NULL, buf->size %d\n",
  736. buf->size);
  737. return total;
  738. }
  739. dest = (char*)current_urb->buffer +
  740. current_urb->actual_length;
  741. space_avail =
  742. current_urb->buffer_length -
  743. current_urb->actual_length;
  744. popnum = MIN (space_avail, buf->size);
  745. if (popnum == 0)
  746. break;
  747. popped = buf_pop (buf, dest, popnum);
  748. if (popped == 0)
  749. break;
  750. current_urb->actual_length += popped;
  751. total += popped;
  752. /* If endpoint->last == 0, then transfers have
  753. * not started on this endpoint
  754. */
  755. if (endpoint->last == 0) {
  756. if(udc_endpoint_write (endpoint)){
  757. /* Write pre-empted by RX */
  758. return -1;
  759. }
  760. }
  761. }/* end while */
  762. return total;
  763. }
  764. return 0;
  765. }
  766. static int fill_buffer (circbuf_t * buf)
  767. {
  768. struct usb_endpoint_instance *endpoint =
  769. &endpoint_instance[rx_endpoint];
  770. if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
  771. unsigned int nb = 0;
  772. char *src = (char *) endpoint->rcv_urb->buffer;
  773. unsigned int rx_avail = buf->totalsize - buf->size;
  774. if(rx_avail >= endpoint->rcv_urb->actual_length){
  775. nb = endpoint->rcv_urb->actual_length;
  776. buf_push (buf, src, nb);
  777. endpoint->rcv_urb->actual_length = 0;
  778. }
  779. return nb;
  780. }
  781. return 0;
  782. }
  783. static int usbtty_configured (void)
  784. {
  785. return usbtty_configured_flag;
  786. }
  787. /******************************************************************************/
  788. static void usbtty_event_handler (struct usb_device_instance *device,
  789. usb_device_event_t event, int data)
  790. {
  791. switch (event) {
  792. case DEVICE_RESET:
  793. case DEVICE_BUS_INACTIVE:
  794. usbtty_configured_flag = 0;
  795. break;
  796. case DEVICE_CONFIGURED:
  797. usbtty_configured_flag = 1;
  798. break;
  799. case DEVICE_ADDRESS_ASSIGNED:
  800. usbtty_init_endpoints ();
  801. default:
  802. break;
  803. }
  804. }
  805. /******************************************************************************/
  806. int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
  807. {
  808. switch (request->bRequest){
  809. case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
  810. break;
  811. case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
  812. break;
  813. case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
  814. * per character */
  815. break;
  816. case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
  817. break;
  818. case ACM_GET_LINE_ENCODING : /* request DTE rate,
  819. * stop/parity bits */
  820. memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
  821. urb->actual_length = sizeof(rs232_desc);
  822. break;
  823. default:
  824. return 1;
  825. }
  826. return 0;
  827. }
  828. /******************************************************************************/
  829. /*
  830. * Since interrupt handling has not yet been implemented, we use this function
  831. * to handle polling. This is called by the tstc,getc,putc,puts routines to
  832. * update the USB state.
  833. */
  834. void usbtty_poll (void)
  835. {
  836. /* New interrupts? */
  837. udc_irq();
  838. /* Write any output data to host buffer
  839. * (do this before checking interrupts to avoid missing one)
  840. */
  841. if (usbtty_configured ()) {
  842. write_buffer (&usbtty_output);
  843. }
  844. /* New interrupts? */
  845. udc_irq();
  846. /* Check for new data from host..
  847. * (do this after checking interrupts to get latest data)
  848. */
  849. if (usbtty_configured ()) {
  850. fill_buffer (&usbtty_input);
  851. }
  852. /* New interrupts? */
  853. udc_irq();
  854. }