usbtty.c 26 KB

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