usbtty.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
  45. #define CONFIG_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(CONFIG_USBD_CONFIGURATION_STR)-1)];
  87. static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
  88. static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_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. #if defined(CONFIG_USBD_HS)
  108. static struct usb_qualifier_descriptor qualifier_descriptor = {
  109. .bLength = sizeof(struct usb_qualifier_descriptor),
  110. .bDescriptorType = USB_DT_QUAL,
  111. .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
  112. .bDeviceClass = COMMUNICATIONS_DEVICE_CLASS,
  113. .bDeviceSubClass = 0x00,
  114. .bDeviceProtocol = 0x00,
  115. .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
  116. .bNumConfigurations = NUM_CONFIGS
  117. };
  118. #endif
  119. /*
  120. * Static CDC ACM specific descriptors
  121. */
  122. struct acm_config_desc {
  123. struct usb_configuration_descriptor configuration_desc;
  124. /* Master Interface */
  125. struct usb_interface_descriptor interface_desc;
  126. struct usb_class_header_function_descriptor usb_class_header;
  127. struct usb_class_call_management_descriptor usb_class_call_mgt;
  128. struct usb_class_abstract_control_descriptor usb_class_acm;
  129. struct usb_class_union_function_descriptor usb_class_union;
  130. struct usb_endpoint_descriptor notification_endpoint;
  131. /* Slave Interface */
  132. struct usb_interface_descriptor data_class_interface;
  133. struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
  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 = UDC_INT_ENDPOINT | 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 = UDC_OUT_ENDPOINT | 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 = UDC_IN_ENDPOINT | 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 interface_desc[NUM_GSERIAL_INTERFACES];
  255. struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
  256. } __attribute__((packed));
  257. static struct gserial_config_desc
  258. gserial_configuration_descriptors[NUM_CONFIGS] ={
  259. {
  260. .configuration_desc ={
  261. .bLength = sizeof(struct usb_configuration_descriptor),
  262. .bDescriptorType = USB_DT_CONFIG,
  263. .wTotalLength =
  264. cpu_to_le16(sizeof(struct gserial_config_desc)),
  265. .bNumInterfaces = NUM_GSERIAL_INTERFACES,
  266. .bConfigurationValue = 1,
  267. .iConfiguration = STR_CONFIG,
  268. .bmAttributes =
  269. BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
  270. .bMaxPower = USBTTY_MAXPOWER
  271. },
  272. .interface_desc = {
  273. {
  274. .bLength =
  275. sizeof(struct usb_interface_descriptor),
  276. .bDescriptorType = USB_DT_INTERFACE,
  277. .bInterfaceNumber = 0,
  278. .bAlternateSetting = 0,
  279. .bNumEndpoints = NUM_ENDPOINTS,
  280. .bInterfaceClass =
  281. COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
  282. .bInterfaceSubClass =
  283. COMMUNICATIONS_NO_SUBCLASS,
  284. .bInterfaceProtocol =
  285. COMMUNICATIONS_NO_PROTOCOL,
  286. .iInterface = STR_DATA_INTERFACE
  287. },
  288. },
  289. .data_endpoints = {
  290. {
  291. .bLength =
  292. sizeof(struct usb_endpoint_descriptor),
  293. .bDescriptorType = USB_DT_ENDPOINT,
  294. .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
  295. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  296. .wMaxPacketSize =
  297. cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
  298. .bInterval= 0xFF,
  299. },
  300. {
  301. .bLength =
  302. sizeof(struct usb_endpoint_descriptor),
  303. .bDescriptorType = USB_DT_ENDPOINT,
  304. .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
  305. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  306. .wMaxPacketSize =
  307. cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
  308. .bInterval = 0xFF,
  309. },
  310. {
  311. .bLength =
  312. sizeof(struct usb_endpoint_descriptor),
  313. .bDescriptorType = USB_DT_ENDPOINT,
  314. .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
  315. .bmAttributes = USB_ENDPOINT_XFER_INT,
  316. .wMaxPacketSize =
  317. cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
  318. .bInterval = 0xFF,
  319. },
  320. },
  321. },
  322. };
  323. /*
  324. * Static Function Prototypes
  325. */
  326. static void usbtty_init_strings (void);
  327. static void usbtty_init_instances (void);
  328. static void usbtty_init_endpoints (void);
  329. static void usbtty_init_terminal_type(short type);
  330. static void usbtty_event_handler (struct usb_device_instance *device,
  331. usb_device_event_t event, int data);
  332. static int usbtty_cdc_setup(struct usb_device_request *request,
  333. struct urb *urb);
  334. static int usbtty_configured (void);
  335. static int write_buffer (circbuf_t * buf);
  336. static int fill_buffer (circbuf_t * buf);
  337. void usbtty_poll (void);
  338. /* utility function for converting char* to wide string used by USB */
  339. static void str2wide (char *str, u16 * wide)
  340. {
  341. int i;
  342. for (i = 0; i < strlen (str) && str[i]; i++){
  343. #if defined(__LITTLE_ENDIAN)
  344. wide[i] = (u16) str[i];
  345. #elif defined(__BIG_ENDIAN)
  346. wide[i] = ((u16)(str[i])<<8);
  347. #else
  348. #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
  349. #endif
  350. }
  351. }
  352. /*
  353. * Test whether a character is in the RX buffer
  354. */
  355. int usbtty_tstc(struct stdio_dev *dev)
  356. {
  357. struct usb_endpoint_instance *endpoint =
  358. &endpoint_instance[rx_endpoint];
  359. /* If no input data exists, allow more RX to be accepted */
  360. if(usbtty_input.size <= 0){
  361. udc_unset_nak(endpoint->endpoint_address&0x03);
  362. }
  363. usbtty_poll ();
  364. return (usbtty_input.size > 0);
  365. }
  366. /*
  367. * Read a single byte from the usb client port. Returns 1 on success, 0
  368. * otherwise. When the function is succesfull, the character read is
  369. * written into its argument c.
  370. */
  371. int usbtty_getc(struct stdio_dev *dev)
  372. {
  373. char c;
  374. struct usb_endpoint_instance *endpoint =
  375. &endpoint_instance[rx_endpoint];
  376. while (usbtty_input.size <= 0) {
  377. udc_unset_nak(endpoint->endpoint_address&0x03);
  378. usbtty_poll ();
  379. }
  380. buf_pop (&usbtty_input, &c, 1);
  381. udc_set_nak(endpoint->endpoint_address&0x03);
  382. return c;
  383. }
  384. /*
  385. * Output a single byte to the usb client port.
  386. */
  387. void usbtty_putc(struct stdio_dev *dev, const char c)
  388. {
  389. if (!usbtty_configured ())
  390. return;
  391. /* If \n, also do \r */
  392. if (c == '\n')
  393. buf_push (&usbtty_output, "\r", 1);
  394. buf_push(&usbtty_output, &c, 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(struct stdio_dev *dev, const char *str)
  432. {
  433. int n;
  434. int len;
  435. if (!usbtty_configured ())
  436. return;
  437. len = strlen (str);
  438. /* add '\r' for each '\n' */
  439. while (len > 0) {
  440. n = next_nl_pos (str);
  441. if (str[n] == '\n') {
  442. __usbtty_puts(str, n);
  443. __usbtty_puts("\r\n", 2);
  444. str += (n + 1);
  445. len -= (n + 1);
  446. } else {
  447. /* No \n found. All done. */
  448. __usbtty_puts (str, n);
  449. break;
  450. }
  451. }
  452. /* Poll at end to handle new data... */
  453. usbtty_poll ();
  454. }
  455. /*
  456. * Initialize the usb client port.
  457. *
  458. */
  459. int drv_usbtty_init (void)
  460. {
  461. int rc;
  462. char * sn;
  463. char * tt;
  464. int snlen;
  465. /* Get serial number */
  466. sn = env_get("serial#");
  467. if (!sn)
  468. sn = "000000000000";
  469. snlen = strlen(sn);
  470. if (snlen > sizeof(serial_number) - 1) {
  471. printf ("Warning: serial number %s is too long (%d > %lu)\n",
  472. sn, snlen, (ulong)(sizeof(serial_number) - 1));
  473. snlen = sizeof(serial_number) - 1;
  474. }
  475. memcpy (serial_number, sn, snlen);
  476. serial_number[snlen] = '\0';
  477. /* Decide on which type of UDC device to be.
  478. */
  479. tt = env_get("usbtty");
  480. if (!tt)
  481. tt = "generic";
  482. usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
  483. /* prepare buffers... */
  484. buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
  485. buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
  486. /* Now, set up USB controller and infrastructure */
  487. udc_init (); /* Basic USB initialization */
  488. usbtty_init_strings ();
  489. usbtty_init_instances ();
  490. usbtty_init_endpoints ();
  491. udc_startup_events (device_instance);/* Enable dev, init udc pointers */
  492. udc_connect (); /* Enable pullup for host detection */
  493. /* Device initialization */
  494. memset (&usbttydev, 0, sizeof (usbttydev));
  495. strcpy (usbttydev.name, "usbtty");
  496. usbttydev.ext = 0; /* No extensions */
  497. usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
  498. usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
  499. usbttydev.getc = usbtty_getc; /* 'getc' function */
  500. usbttydev.putc = usbtty_putc; /* 'putc' function */
  501. usbttydev.puts = usbtty_puts; /* 'puts' function */
  502. rc = stdio_register (&usbttydev);
  503. return (rc == 0) ? 1 : rc;
  504. }
  505. static void usbtty_init_strings (void)
  506. {
  507. struct usb_string_descriptor *string;
  508. usbtty_string_table[STR_LANG] =
  509. (struct usb_string_descriptor*)wstrLang;
  510. string = (struct usb_string_descriptor *) wstrManufacturer;
  511. string->bLength = sizeof(wstrManufacturer);
  512. string->bDescriptorType = USB_DT_STRING;
  513. str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
  514. usbtty_string_table[STR_MANUFACTURER]=string;
  515. string = (struct usb_string_descriptor *) wstrProduct;
  516. string->bLength = sizeof(wstrProduct);
  517. string->bDescriptorType = USB_DT_STRING;
  518. str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
  519. usbtty_string_table[STR_PRODUCT]=string;
  520. string = (struct usb_string_descriptor *) wstrSerial;
  521. string->bLength = sizeof(serial_number);
  522. string->bDescriptorType = USB_DT_STRING;
  523. str2wide (serial_number, string->wData);
  524. usbtty_string_table[STR_SERIAL]=string;
  525. string = (struct usb_string_descriptor *) wstrConfiguration;
  526. string->bLength = sizeof(wstrConfiguration);
  527. string->bDescriptorType = USB_DT_STRING;
  528. str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
  529. usbtty_string_table[STR_CONFIG]=string;
  530. string = (struct usb_string_descriptor *) wstrDataInterface;
  531. string->bLength = sizeof(wstrDataInterface);
  532. string->bDescriptorType = USB_DT_STRING;
  533. str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
  534. usbtty_string_table[STR_DATA_INTERFACE]=string;
  535. string = (struct usb_string_descriptor *) wstrCtrlInterface;
  536. string->bLength = sizeof(wstrCtrlInterface);
  537. string->bDescriptorType = USB_DT_STRING;
  538. str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
  539. usbtty_string_table[STR_CTRL_INTERFACE]=string;
  540. /* Now, initialize the string table for ep0 handling */
  541. usb_strings = usbtty_string_table;
  542. }
  543. #define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
  544. &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
  545. static void usbtty_init_instances (void)
  546. {
  547. int i;
  548. /* initialize device instance */
  549. memset (device_instance, 0, sizeof (struct usb_device_instance));
  550. device_instance->device_state = STATE_INIT;
  551. device_instance->device_descriptor = &device_descriptor;
  552. #if defined(CONFIG_USBD_HS)
  553. device_instance->qualifier_descriptor = &qualifier_descriptor;
  554. #endif
  555. device_instance->event = usbtty_event_handler;
  556. device_instance->cdc_recv_setup = usbtty_cdc_setup;
  557. device_instance->bus = bus_instance;
  558. device_instance->configurations = NUM_CONFIGS;
  559. device_instance->configuration_instance_array = config_instance;
  560. /* initialize bus instance */
  561. memset (bus_instance, 0, sizeof (struct usb_bus_instance));
  562. bus_instance->device = device_instance;
  563. bus_instance->endpoint_array = endpoint_instance;
  564. bus_instance->max_endpoints = 1;
  565. bus_instance->maxpacketsize = 64;
  566. bus_instance->serial_number_str = serial_number;
  567. /* configuration instance */
  568. memset (config_instance, 0,
  569. sizeof (struct usb_configuration_instance));
  570. config_instance->interfaces = interface_count;
  571. config_instance->configuration_descriptor = configuration_descriptor;
  572. config_instance->interface_instance_array = interface_instance;
  573. /* interface instance */
  574. memset (interface_instance, 0,
  575. sizeof (struct usb_interface_instance));
  576. interface_instance->alternates = 1;
  577. interface_instance->alternates_instance_array = alternate_instance;
  578. /* alternates instance */
  579. memset (alternate_instance, 0,
  580. sizeof (struct usb_alternate_instance));
  581. alternate_instance->interface_descriptor = interface_descriptors;
  582. alternate_instance->endpoints = NUM_ENDPOINTS;
  583. alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
  584. /* endpoint instances */
  585. memset (&endpoint_instance[0], 0,
  586. sizeof (struct usb_endpoint_instance));
  587. endpoint_instance[0].endpoint_address = 0;
  588. endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
  589. endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
  590. endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
  591. endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
  592. udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
  593. for (i = 1; i <= NUM_ENDPOINTS; i++) {
  594. memset (&endpoint_instance[i], 0,
  595. sizeof (struct usb_endpoint_instance));
  596. endpoint_instance[i].endpoint_address =
  597. ep_descriptor_ptrs[i - 1]->bEndpointAddress;
  598. endpoint_instance[i].rcv_attributes =
  599. ep_descriptor_ptrs[i - 1]->bmAttributes;
  600. endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
  601. endpoint_instance[i].tx_attributes =
  602. ep_descriptor_ptrs[i - 1]->bmAttributes;
  603. endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
  604. endpoint_instance[i].tx_attributes =
  605. ep_descriptor_ptrs[i - 1]->bmAttributes;
  606. urb_link_init (&endpoint_instance[i].rcv);
  607. urb_link_init (&endpoint_instance[i].rdy);
  608. urb_link_init (&endpoint_instance[i].tx);
  609. urb_link_init (&endpoint_instance[i].done);
  610. if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
  611. endpoint_instance[i].tx_urb =
  612. usbd_alloc_urb (device_instance,
  613. &endpoint_instance[i]);
  614. else
  615. endpoint_instance[i].rcv_urb =
  616. usbd_alloc_urb (device_instance,
  617. &endpoint_instance[i]);
  618. }
  619. }
  620. static void usbtty_init_endpoints (void)
  621. {
  622. int i;
  623. bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
  624. for (i = 1; i <= NUM_ENDPOINTS; i++) {
  625. udc_setup_ep (device_instance, i, &endpoint_instance[i]);
  626. }
  627. }
  628. /* usbtty_init_terminal_type
  629. *
  630. * Do some late binding for our device type.
  631. */
  632. static void usbtty_init_terminal_type(short type)
  633. {
  634. switch(type){
  635. /* CDC ACM */
  636. case 0:
  637. /* Assign endpoint descriptors */
  638. ep_descriptor_ptrs[0] =
  639. &acm_configuration_descriptors[0].notification_endpoint;
  640. ep_descriptor_ptrs[1] =
  641. &acm_configuration_descriptors[0].data_endpoints[0];
  642. ep_descriptor_ptrs[2] =
  643. &acm_configuration_descriptors[0].data_endpoints[1];
  644. /* Enumerate Device Descriptor */
  645. device_descriptor.bDeviceClass =
  646. COMMUNICATIONS_DEVICE_CLASS;
  647. device_descriptor.idProduct =
  648. cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
  649. #if defined(CONFIG_USBD_HS)
  650. qualifier_descriptor.bDeviceClass =
  651. COMMUNICATIONS_DEVICE_CLASS;
  652. #endif
  653. /* Assign endpoint indices */
  654. tx_endpoint = ACM_TX_ENDPOINT;
  655. rx_endpoint = ACM_RX_ENDPOINT;
  656. /* Configuration Descriptor */
  657. configuration_descriptor =
  658. (struct usb_configuration_descriptor*)
  659. &acm_configuration_descriptors;
  660. /* Interface count */
  661. interface_count = NUM_ACM_INTERFACES;
  662. break;
  663. /* BULK IN/OUT & Default */
  664. case 1:
  665. default:
  666. /* Assign endpoint descriptors */
  667. ep_descriptor_ptrs[0] =
  668. &gserial_configuration_descriptors[0].data_endpoints[0];
  669. ep_descriptor_ptrs[1] =
  670. &gserial_configuration_descriptors[0].data_endpoints[1];
  671. ep_descriptor_ptrs[2] =
  672. &gserial_configuration_descriptors[0].data_endpoints[2];
  673. /* Enumerate Device Descriptor */
  674. device_descriptor.bDeviceClass = 0xFF;
  675. device_descriptor.idProduct =
  676. cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
  677. #if defined(CONFIG_USBD_HS)
  678. qualifier_descriptor.bDeviceClass = 0xFF;
  679. #endif
  680. /* Assign endpoint indices */
  681. tx_endpoint = GSERIAL_TX_ENDPOINT;
  682. rx_endpoint = GSERIAL_RX_ENDPOINT;
  683. /* Configuration Descriptor */
  684. configuration_descriptor =
  685. (struct usb_configuration_descriptor*)
  686. &gserial_configuration_descriptors;
  687. /* Interface count */
  688. interface_count = NUM_GSERIAL_INTERFACES;
  689. break;
  690. }
  691. }
  692. /******************************************************************************/
  693. static struct urb *next_urb (struct usb_device_instance *device,
  694. struct usb_endpoint_instance *endpoint)
  695. {
  696. struct urb *current_urb = NULL;
  697. int space;
  698. /* If there's a queue, then we should add to the last urb */
  699. if (!endpoint->tx_queue) {
  700. current_urb = endpoint->tx_urb;
  701. } else {
  702. /* Last urb from tx chain */
  703. current_urb =
  704. p2surround (struct urb, link, endpoint->tx.prev);
  705. }
  706. /* Make sure this one has enough room */
  707. space = current_urb->buffer_length - current_urb->actual_length;
  708. if (space > 0) {
  709. return current_urb;
  710. } else { /* No space here */
  711. /* First look at done list */
  712. current_urb = first_urb_detached (&endpoint->done);
  713. if (!current_urb) {
  714. current_urb = usbd_alloc_urb (device, endpoint);
  715. }
  716. urb_append (&endpoint->tx, current_urb);
  717. endpoint->tx_queue++;
  718. }
  719. return current_urb;
  720. }
  721. static int write_buffer (circbuf_t * buf)
  722. {
  723. if (!usbtty_configured ()) {
  724. return 0;
  725. }
  726. struct usb_endpoint_instance *endpoint =
  727. &endpoint_instance[tx_endpoint];
  728. struct urb *current_urb = NULL;
  729. /* TX data still exists - send it now
  730. */
  731. if(endpoint->sent < endpoint->tx_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. current_urb = next_urb (device_instance, endpoint);
  747. dest = (char*)current_urb->buffer +
  748. current_urb->actual_length;
  749. space_avail =
  750. current_urb->buffer_length -
  751. current_urb->actual_length;
  752. popnum = min(space_avail, (int)buf->size);
  753. if (popnum == 0)
  754. break;
  755. popped = buf_pop (buf, dest, popnum);
  756. if (popped == 0)
  757. break;
  758. current_urb->actual_length += popped;
  759. total += popped;
  760. /* If endpoint->last == 0, then transfers have
  761. * not started on this endpoint
  762. */
  763. if (endpoint->last == 0) {
  764. if(udc_endpoint_write (endpoint)){
  765. /* Write pre-empted by RX */
  766. return -1;
  767. }
  768. }
  769. }/* end while */
  770. return total;
  771. }
  772. return 0;
  773. }
  774. static int fill_buffer (circbuf_t * buf)
  775. {
  776. struct usb_endpoint_instance *endpoint =
  777. &endpoint_instance[rx_endpoint];
  778. if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
  779. unsigned int nb = 0;
  780. char *src = (char *) endpoint->rcv_urb->buffer;
  781. unsigned int rx_avail = buf->totalsize - buf->size;
  782. if(rx_avail >= endpoint->rcv_urb->actual_length){
  783. nb = endpoint->rcv_urb->actual_length;
  784. buf_push (buf, src, nb);
  785. endpoint->rcv_urb->actual_length = 0;
  786. }
  787. return nb;
  788. }
  789. return 0;
  790. }
  791. static int usbtty_configured (void)
  792. {
  793. return usbtty_configured_flag;
  794. }
  795. /******************************************************************************/
  796. static void usbtty_event_handler (struct usb_device_instance *device,
  797. usb_device_event_t event, int data)
  798. {
  799. #if defined(CONFIG_USBD_HS)
  800. int i;
  801. #endif
  802. switch (event) {
  803. case DEVICE_RESET:
  804. case DEVICE_BUS_INACTIVE:
  805. usbtty_configured_flag = 0;
  806. break;
  807. case DEVICE_CONFIGURED:
  808. usbtty_configured_flag = 1;
  809. break;
  810. case DEVICE_ADDRESS_ASSIGNED:
  811. #if defined(CONFIG_USBD_HS)
  812. /*
  813. * is_usbd_high_speed routine needs to be defined by
  814. * specific gadget driver
  815. * It returns true if device enumerates at High speed
  816. * Retuns false otherwise
  817. */
  818. for (i = 0; i < NUM_ENDPOINTS; i++) {
  819. if (((ep_descriptor_ptrs[i]->bmAttributes &
  820. USB_ENDPOINT_XFERTYPE_MASK) ==
  821. USB_ENDPOINT_XFER_BULK)
  822. && is_usbd_high_speed()) {
  823. ep_descriptor_ptrs[i]->wMaxPacketSize =
  824. CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
  825. }
  826. endpoint_instance[i + 1].tx_packetSize =
  827. ep_descriptor_ptrs[i]->wMaxPacketSize;
  828. endpoint_instance[i + 1].rcv_packetSize =
  829. ep_descriptor_ptrs[i]->wMaxPacketSize;
  830. }
  831. #endif
  832. usbtty_init_endpoints ();
  833. default:
  834. break;
  835. }
  836. }
  837. /******************************************************************************/
  838. int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
  839. {
  840. switch (request->bRequest){
  841. case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
  842. break;
  843. case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
  844. break;
  845. case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
  846. * per character */
  847. break;
  848. case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
  849. break;
  850. case ACM_GET_LINE_ENCODING : /* request DTE rate,
  851. * stop/parity bits */
  852. memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
  853. urb->actual_length = sizeof(rs232_desc);
  854. break;
  855. default:
  856. return 1;
  857. }
  858. return 0;
  859. }
  860. /******************************************************************************/
  861. /*
  862. * Since interrupt handling has not yet been implemented, we use this function
  863. * to handle polling. This is called by the tstc,getc,putc,puts routines to
  864. * update the USB state.
  865. */
  866. void usbtty_poll (void)
  867. {
  868. /* New interrupts? */
  869. udc_irq();
  870. /* Write any output data to host buffer
  871. * (do this before checking interrupts to avoid missing one)
  872. */
  873. if (usbtty_configured ()) {
  874. write_buffer (&usbtty_output);
  875. }
  876. /* New interrupts? */
  877. udc_irq();
  878. /* Check for new data from host..
  879. * (do this after checking interrupts to get latest data)
  880. */
  881. if (usbtty_configured ()) {
  882. fill_buffer (&usbtty_input);
  883. }
  884. /* New interrupts? */
  885. udc_irq();
  886. }