usbtty.c 27 KB

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