usbtty.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. /*
  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 (void)
  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 (void)
  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 (const char c)
  388. {
  389. if (!usbtty_configured ())
  390. return;
  391. buf_push (&usbtty_output, &c, 1);
  392. /* If \n, also do \r */
  393. if (c == '\n')
  394. buf_push (&usbtty_output, "\r", 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 (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 + 1);
  443. __usbtty_puts ("\r", 1);
  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. /* Ger seiral number */
  466. if (!(sn = getenv("serial#"))) {
  467. sn = "000000000000";
  468. }
  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. if(!(tt = getenv("usbtty"))) {
  480. tt = "generic";
  481. }
  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. device_instance->event = usbtty_event_handler;
  553. device_instance->cdc_recv_setup = usbtty_cdc_setup;
  554. device_instance->bus = bus_instance;
  555. device_instance->configurations = NUM_CONFIGS;
  556. device_instance->configuration_instance_array = config_instance;
  557. /* initialize bus instance */
  558. memset (bus_instance, 0, sizeof (struct usb_bus_instance));
  559. bus_instance->device = device_instance;
  560. bus_instance->endpoint_array = endpoint_instance;
  561. bus_instance->max_endpoints = 1;
  562. bus_instance->maxpacketsize = 64;
  563. bus_instance->serial_number_str = serial_number;
  564. /* configuration instance */
  565. memset (config_instance, 0,
  566. sizeof (struct usb_configuration_instance));
  567. config_instance->interfaces = interface_count;
  568. config_instance->configuration_descriptor = configuration_descriptor;
  569. config_instance->interface_instance_array = interface_instance;
  570. /* interface instance */
  571. memset (interface_instance, 0,
  572. sizeof (struct usb_interface_instance));
  573. interface_instance->alternates = 1;
  574. interface_instance->alternates_instance_array = alternate_instance;
  575. /* alternates instance */
  576. memset (alternate_instance, 0,
  577. sizeof (struct usb_alternate_instance));
  578. alternate_instance->interface_descriptor = interface_descriptors;
  579. alternate_instance->endpoints = NUM_ENDPOINTS;
  580. alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
  581. /* endpoint instances */
  582. memset (&endpoint_instance[0], 0,
  583. sizeof (struct usb_endpoint_instance));
  584. endpoint_instance[0].endpoint_address = 0;
  585. endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
  586. endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
  587. endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
  588. endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
  589. udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
  590. for (i = 1; i <= NUM_ENDPOINTS; i++) {
  591. memset (&endpoint_instance[i], 0,
  592. sizeof (struct usb_endpoint_instance));
  593. endpoint_instance[i].endpoint_address =
  594. ep_descriptor_ptrs[i - 1]->bEndpointAddress;
  595. endpoint_instance[i].rcv_attributes =
  596. ep_descriptor_ptrs[i - 1]->bmAttributes;
  597. endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
  598. endpoint_instance[i].tx_attributes =
  599. ep_descriptor_ptrs[i - 1]->bmAttributes;
  600. endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
  601. endpoint_instance[i].tx_attributes =
  602. ep_descriptor_ptrs[i - 1]->bmAttributes;
  603. urb_link_init (&endpoint_instance[i].rcv);
  604. urb_link_init (&endpoint_instance[i].rdy);
  605. urb_link_init (&endpoint_instance[i].tx);
  606. urb_link_init (&endpoint_instance[i].done);
  607. if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
  608. endpoint_instance[i].tx_urb =
  609. usbd_alloc_urb (device_instance,
  610. &endpoint_instance[i]);
  611. else
  612. endpoint_instance[i].rcv_urb =
  613. usbd_alloc_urb (device_instance,
  614. &endpoint_instance[i]);
  615. }
  616. }
  617. static void usbtty_init_endpoints (void)
  618. {
  619. int i;
  620. bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
  621. for (i = 1; i <= NUM_ENDPOINTS; i++) {
  622. udc_setup_ep (device_instance, i, &endpoint_instance[i]);
  623. }
  624. }
  625. /* usbtty_init_terminal_type
  626. *
  627. * Do some late binding for our device type.
  628. */
  629. static void usbtty_init_terminal_type(short type)
  630. {
  631. switch(type){
  632. /* CDC ACM */
  633. case 0:
  634. /* Assign endpoint descriptors */
  635. ep_descriptor_ptrs[0] =
  636. &acm_configuration_descriptors[0].notification_endpoint;
  637. ep_descriptor_ptrs[1] =
  638. &acm_configuration_descriptors[0].data_endpoints[0];
  639. ep_descriptor_ptrs[2] =
  640. &acm_configuration_descriptors[0].data_endpoints[1];
  641. /* Enumerate Device Descriptor */
  642. device_descriptor.bDeviceClass =
  643. COMMUNICATIONS_DEVICE_CLASS;
  644. device_descriptor.idProduct =
  645. cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
  646. /* Assign endpoint indices */
  647. tx_endpoint = ACM_TX_ENDPOINT;
  648. rx_endpoint = ACM_RX_ENDPOINT;
  649. /* Configuration Descriptor */
  650. configuration_descriptor =
  651. (struct usb_configuration_descriptor*)
  652. &acm_configuration_descriptors;
  653. /* Interface count */
  654. interface_count = NUM_ACM_INTERFACES;
  655. break;
  656. /* BULK IN/OUT & Default */
  657. case 1:
  658. default:
  659. /* Assign endpoint descriptors */
  660. ep_descriptor_ptrs[0] =
  661. &gserial_configuration_descriptors[0].data_endpoints[0];
  662. ep_descriptor_ptrs[1] =
  663. &gserial_configuration_descriptors[0].data_endpoints[1];
  664. ep_descriptor_ptrs[2] =
  665. &gserial_configuration_descriptors[0].data_endpoints[2];
  666. /* Enumerate Device Descriptor */
  667. device_descriptor.bDeviceClass = 0xFF;
  668. device_descriptor.idProduct =
  669. cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
  670. /* Assign endpoint indices */
  671. tx_endpoint = GSERIAL_TX_ENDPOINT;
  672. rx_endpoint = GSERIAL_RX_ENDPOINT;
  673. /* Configuration Descriptor */
  674. configuration_descriptor =
  675. (struct usb_configuration_descriptor*)
  676. &gserial_configuration_descriptors;
  677. /* Interface count */
  678. interface_count = NUM_GSERIAL_INTERFACES;
  679. break;
  680. }
  681. }
  682. /******************************************************************************/
  683. static struct urb *next_urb (struct usb_device_instance *device,
  684. struct usb_endpoint_instance *endpoint)
  685. {
  686. struct urb *current_urb = NULL;
  687. int space;
  688. /* If there's a queue, then we should add to the last urb */
  689. if (!endpoint->tx_queue) {
  690. current_urb = endpoint->tx_urb;
  691. } else {
  692. /* Last urb from tx chain */
  693. current_urb =
  694. p2surround (struct urb, link, endpoint->tx.prev);
  695. }
  696. /* Make sure this one has enough room */
  697. space = current_urb->buffer_length - current_urb->actual_length;
  698. if (space > 0) {
  699. return current_urb;
  700. } else { /* No space here */
  701. /* First look at done list */
  702. current_urb = first_urb_detached (&endpoint->done);
  703. if (!current_urb) {
  704. current_urb = usbd_alloc_urb (device, endpoint);
  705. }
  706. urb_append (&endpoint->tx, current_urb);
  707. endpoint->tx_queue++;
  708. }
  709. return current_urb;
  710. }
  711. static int write_buffer (circbuf_t * buf)
  712. {
  713. if (!usbtty_configured ()) {
  714. return 0;
  715. }
  716. struct usb_endpoint_instance *endpoint =
  717. &endpoint_instance[tx_endpoint];
  718. struct urb *current_urb = NULL;
  719. current_urb = next_urb (device_instance, endpoint);
  720. /* TX data still exists - send it now
  721. */
  722. if(endpoint->sent < current_urb->actual_length){
  723. if(udc_endpoint_write (endpoint)){
  724. /* Write pre-empted by RX */
  725. return -1;
  726. }
  727. }
  728. if (buf->size) {
  729. char *dest;
  730. int space_avail;
  731. int popnum, popped;
  732. int total = 0;
  733. /* Break buffer into urb sized pieces,
  734. * and link each to the endpoint
  735. */
  736. while (buf->size > 0) {
  737. if (!current_urb) {
  738. TTYERR ("current_urb is NULL, buf->size %d\n",
  739. buf->size);
  740. return total;
  741. }
  742. dest = (char*)current_urb->buffer +
  743. current_urb->actual_length;
  744. space_avail =
  745. current_urb->buffer_length -
  746. current_urb->actual_length;
  747. popnum = MIN (space_avail, buf->size);
  748. if (popnum == 0)
  749. break;
  750. popped = buf_pop (buf, dest, popnum);
  751. if (popped == 0)
  752. break;
  753. current_urb->actual_length += popped;
  754. total += popped;
  755. /* If endpoint->last == 0, then transfers have
  756. * not started on this endpoint
  757. */
  758. if (endpoint->last == 0) {
  759. if(udc_endpoint_write (endpoint)){
  760. /* Write pre-empted by RX */
  761. return -1;
  762. }
  763. }
  764. }/* end while */
  765. return total;
  766. }
  767. return 0;
  768. }
  769. static int fill_buffer (circbuf_t * buf)
  770. {
  771. struct usb_endpoint_instance *endpoint =
  772. &endpoint_instance[rx_endpoint];
  773. if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
  774. unsigned int nb = 0;
  775. char *src = (char *) endpoint->rcv_urb->buffer;
  776. unsigned int rx_avail = buf->totalsize - buf->size;
  777. if(rx_avail >= endpoint->rcv_urb->actual_length){
  778. nb = endpoint->rcv_urb->actual_length;
  779. buf_push (buf, src, nb);
  780. endpoint->rcv_urb->actual_length = 0;
  781. }
  782. return nb;
  783. }
  784. return 0;
  785. }
  786. static int usbtty_configured (void)
  787. {
  788. return usbtty_configured_flag;
  789. }
  790. /******************************************************************************/
  791. static void usbtty_event_handler (struct usb_device_instance *device,
  792. usb_device_event_t event, int data)
  793. {
  794. switch (event) {
  795. case DEVICE_RESET:
  796. case DEVICE_BUS_INACTIVE:
  797. usbtty_configured_flag = 0;
  798. break;
  799. case DEVICE_CONFIGURED:
  800. usbtty_configured_flag = 1;
  801. break;
  802. case DEVICE_ADDRESS_ASSIGNED:
  803. usbtty_init_endpoints ();
  804. default:
  805. break;
  806. }
  807. }
  808. /******************************************************************************/
  809. int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
  810. {
  811. switch (request->bRequest){
  812. case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
  813. break;
  814. case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
  815. break;
  816. case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
  817. * per character */
  818. break;
  819. case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
  820. break;
  821. case ACM_GET_LINE_ENCODING : /* request DTE rate,
  822. * stop/parity bits */
  823. memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
  824. urb->actual_length = sizeof(rs232_desc);
  825. break;
  826. default:
  827. return 1;
  828. }
  829. return 0;
  830. }
  831. /******************************************************************************/
  832. /*
  833. * Since interrupt handling has not yet been implemented, we use this function
  834. * to handle polling. This is called by the tstc,getc,putc,puts routines to
  835. * update the USB state.
  836. */
  837. void usbtty_poll (void)
  838. {
  839. /* New interrupts? */
  840. udc_irq();
  841. /* Write any output data to host buffer
  842. * (do this before checking interrupts to avoid missing one)
  843. */
  844. if (usbtty_configured ()) {
  845. write_buffer (&usbtty_output);
  846. }
  847. /* New interrupts? */
  848. udc_irq();
  849. /* Check for new data from host..
  850. * (do this after checking interrupts to get latest data)
  851. */
  852. if (usbtty_configured ()) {
  853. fill_buffer (&usbtty_input);
  854. }
  855. /* New interrupts? */
  856. udc_irq();
  857. }