usbtty.c 27 KB

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