usbtty.c 28 KB

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