usbtty.c 26 KB

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