usbtty.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. #ifdef CONFIG_USB_TTY
  25. #include <circbuf.h>
  26. #include <devices.h>
  27. #include "usbtty.h"
  28. #include "usb_cdc_acm.h"
  29. #include "usbdescriptors.h"
  30. #include <config.h> /* If defined, override Linux identifiers with
  31. * vendor specific ones */
  32. #if 0
  33. #define TTYDBG(fmt,args...)\
  34. serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
  35. #else
  36. #define TTYDBG(fmt,args...) do{}while(0)
  37. #endif
  38. #if 1
  39. #define TTYERR(fmt,args...)\
  40. serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
  41. __LINE__,##args)
  42. #else
  43. #define TTYERR(fmt,args...) do{}while(0)
  44. #endif
  45. /*
  46. * Defines
  47. */
  48. #define NUM_CONFIGS 1
  49. #define MAX_INTERFACES 2
  50. #define NUM_ENDPOINTS 3
  51. #define ACM_TX_ENDPOINT 3
  52. #define ACM_RX_ENDPOINT 2
  53. #define GSERIAL_TX_ENDPOINT 2
  54. #define GSERIAL_RX_ENDPOINT 1
  55. #define NUM_ACM_INTERFACES 2
  56. #define NUM_GSERIAL_INTERFACES 1
  57. #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
  58. #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
  59. /*
  60. * Buffers to hold input and output data
  61. */
  62. #define USBTTY_BUFFER_SIZE 256
  63. static circbuf_t usbtty_input;
  64. static circbuf_t usbtty_output;
  65. /*
  66. * Instance variables
  67. */
  68. static device_t usbttydev;
  69. static struct usb_device_instance device_instance[1];
  70. static struct usb_bus_instance bus_instance[1];
  71. static struct usb_configuration_instance config_instance[NUM_CONFIGS];
  72. static struct usb_interface_instance interface_instance[MAX_INTERFACES];
  73. static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
  74. /* one extra for control endpoint */
  75. static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
  76. /*
  77. * Global flag
  78. */
  79. int usbtty_configured_flag = 0;
  80. /*
  81. * Serial number
  82. */
  83. static char serial_number[16];
  84. /*
  85. * Descriptors, Strings, Local variables.
  86. */
  87. /* defined and used by usbdcore_ep0.c */
  88. extern struct usb_string_descriptor **usb_strings;
  89. /* Indicies, References */
  90. static unsigned short rx_endpoint = 0;
  91. static unsigned short tx_endpoint = 0;
  92. static unsigned short interface_count = 0;
  93. static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
  94. /* USB Descriptor Strings */
  95. static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
  96. static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
  97. static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
  98. static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
  99. static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
  100. static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
  101. static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
  102. /* Standard USB Data Structures */
  103. static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
  104. static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
  105. static struct usb_configuration_descriptor *configuration_descriptor = 0;
  106. static struct usb_device_descriptor device_descriptor = {
  107. .bLength = sizeof(struct usb_device_descriptor),
  108. .bDescriptorType = USB_DT_DEVICE,
  109. .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
  110. .bDeviceSubClass = 0x00,
  111. .bDeviceProtocol = 0x00,
  112. .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
  113. .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
  114. .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
  115. .iManufacturer = STR_MANUFACTURER,
  116. .iProduct = STR_PRODUCT,
  117. .iSerialNumber = STR_SERIAL,
  118. .bNumConfigurations = NUM_CONFIGS
  119. };
  120. /*
  121. * Static CDC ACM specific descriptors
  122. */
  123. struct acm_config_desc {
  124. struct usb_configuration_descriptor configuration_desc;
  125. /* Master Interface */
  126. struct usb_interface_descriptor interface_desc;
  127. struct usb_class_header_function_descriptor usb_class_header;
  128. struct usb_class_call_management_descriptor usb_class_call_mgt;
  129. struct usb_class_abstract_control_descriptor usb_class_acm;
  130. struct usb_class_union_function_descriptor usb_class_union;
  131. struct usb_endpoint_descriptor notification_endpoint;
  132. /* Slave Interface */
  133. struct usb_interface_descriptor data_class_interface;
  134. struct usb_endpoint_descriptor
  135. data_endpoints[NUM_ENDPOINTS-1] __attribute__((packed));
  136. } __attribute__((packed));
  137. static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
  138. {
  139. .configuration_desc ={
  140. .bLength =
  141. sizeof(struct usb_configuration_descriptor),
  142. .bDescriptorType = USB_DT_CONFIG,
  143. .wTotalLength =
  144. cpu_to_le16(sizeof(struct acm_config_desc)),
  145. .bNumInterfaces = NUM_ACM_INTERFACES,
  146. .bConfigurationValue = 1,
  147. .iConfiguration = STR_CONFIG,
  148. .bmAttributes =
  149. BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
  150. .bMaxPower = USBTTY_MAXPOWER
  151. },
  152. /* Interface 1 */
  153. .interface_desc = {
  154. .bLength = sizeof(struct usb_interface_descriptor),
  155. .bDescriptorType = USB_DT_INTERFACE,
  156. .bInterfaceNumber = 0,
  157. .bAlternateSetting = 0,
  158. .bNumEndpoints = 0x01,
  159. .bInterfaceClass =
  160. COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
  161. .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
  162. .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
  163. .iInterface = STR_CTRL_INTERFACE,
  164. },
  165. .usb_class_header = {
  166. .bFunctionLength =
  167. sizeof(struct usb_class_header_function_descriptor),
  168. .bDescriptorType = CS_INTERFACE,
  169. .bDescriptorSubtype = USB_ST_HEADER,
  170. .bcdCDC = cpu_to_le16(110),
  171. },
  172. .usb_class_call_mgt = {
  173. .bFunctionLength =
  174. sizeof(struct usb_class_call_management_descriptor),
  175. .bDescriptorType = CS_INTERFACE,
  176. .bDescriptorSubtype = USB_ST_CMF,
  177. .bmCapabilities = 0x00,
  178. .bDataInterface = 0x01,
  179. },
  180. .usb_class_acm = {
  181. .bFunctionLength =
  182. sizeof(struct usb_class_abstract_control_descriptor),
  183. .bDescriptorType = CS_INTERFACE,
  184. .bDescriptorSubtype = USB_ST_ACMF,
  185. .bmCapabilities = 0x00,
  186. },
  187. .usb_class_union = {
  188. .bFunctionLength =
  189. sizeof(struct usb_class_union_function_descriptor),
  190. .bDescriptorType = CS_INTERFACE,
  191. .bDescriptorSubtype = USB_ST_UF,
  192. .bMasterInterface = 0x00,
  193. .bSlaveInterface0 = 0x01,
  194. },
  195. .notification_endpoint = {
  196. .bLength =
  197. sizeof(struct usb_endpoint_descriptor),
  198. .bDescriptorType = USB_DT_ENDPOINT,
  199. .bEndpointAddress = 0x01 | USB_DIR_IN,
  200. .bmAttributes = USB_ENDPOINT_XFER_INT,
  201. .wMaxPacketSize
  202. = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
  203. .bInterval = 0xFF,
  204. },
  205. /* Interface 2 */
  206. .data_class_interface = {
  207. .bLength =
  208. sizeof(struct usb_interface_descriptor),
  209. .bDescriptorType = USB_DT_INTERFACE,
  210. .bInterfaceNumber = 0x01,
  211. .bAlternateSetting = 0x00,
  212. .bNumEndpoints = 0x02,
  213. .bInterfaceClass =
  214. COMMUNICATIONS_INTERFACE_CLASS_DATA,
  215. .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
  216. .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
  217. .iInterface = STR_DATA_INTERFACE,
  218. },
  219. .data_endpoints = {
  220. {
  221. .bLength =
  222. sizeof(struct usb_endpoint_descriptor),
  223. .bDescriptorType = USB_DT_ENDPOINT,
  224. .bEndpointAddress = 0x02 | USB_DIR_OUT,
  225. .bmAttributes =
  226. USB_ENDPOINT_XFER_BULK,
  227. .wMaxPacketSize =
  228. cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
  229. .bInterval = 0xFF,
  230. },
  231. {
  232. .bLength =
  233. sizeof(struct usb_endpoint_descriptor),
  234. .bDescriptorType = USB_DT_ENDPOINT,
  235. .bEndpointAddress = 0x03 | USB_DIR_IN,
  236. .bmAttributes =
  237. USB_ENDPOINT_XFER_BULK,
  238. .wMaxPacketSize =
  239. cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
  240. .bInterval = 0xFF,
  241. },
  242. },
  243. },
  244. };
  245. static struct rs232_emu rs232_desc={
  246. .dter = 115200,
  247. .stop_bits = 0x00,
  248. .parity = 0x00,
  249. .data_bits = 0x08
  250. };
  251. /*
  252. * Static Generic Serial specific data
  253. */
  254. struct gserial_config_desc {
  255. struct usb_configuration_descriptor configuration_desc;
  256. struct usb_interface_descriptor
  257. interface_desc[NUM_GSERIAL_INTERFACES] __attribute__((packed));
  258. struct usb_endpoint_descriptor
  259. data_endpoints[NUM_ENDPOINTS] __attribute__((packed));
  260. } __attribute__((packed));
  261. static struct gserial_config_desc
  262. gserial_configuration_descriptors[NUM_CONFIGS] ={
  263. {
  264. .configuration_desc ={
  265. .bLength = sizeof(struct usb_configuration_descriptor),
  266. .bDescriptorType = USB_DT_CONFIG,
  267. .wTotalLength =
  268. cpu_to_le16(sizeof(struct gserial_config_desc)),
  269. .bNumInterfaces = NUM_GSERIAL_INTERFACES,
  270. .bConfigurationValue = 1,
  271. .iConfiguration = STR_CONFIG,
  272. .bmAttributes =
  273. BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
  274. .bMaxPower = USBTTY_MAXPOWER
  275. },
  276. .interface_desc = {
  277. {
  278. .bLength =
  279. sizeof(struct usb_interface_descriptor),
  280. .bDescriptorType = USB_DT_INTERFACE,
  281. .bInterfaceNumber = 0,
  282. .bAlternateSetting = 0,
  283. .bNumEndpoints = NUM_ENDPOINTS,
  284. .bInterfaceClass =
  285. COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
  286. .bInterfaceSubClass =
  287. COMMUNICATIONS_NO_SUBCLASS,
  288. .bInterfaceProtocol =
  289. COMMUNICATIONS_NO_PROTOCOL,
  290. .iInterface = STR_DATA_INTERFACE
  291. },
  292. },
  293. .data_endpoints = {
  294. {
  295. .bLength =
  296. sizeof(struct usb_endpoint_descriptor),
  297. .bDescriptorType = USB_DT_ENDPOINT,
  298. .bEndpointAddress = 0x01 | USB_DIR_OUT,
  299. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  300. .wMaxPacketSize =
  301. cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
  302. .bInterval= 0xFF,
  303. },
  304. {
  305. .bLength =
  306. sizeof(struct usb_endpoint_descriptor),
  307. .bDescriptorType = USB_DT_ENDPOINT,
  308. .bEndpointAddress = 0x02 | USB_DIR_IN,
  309. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  310. .wMaxPacketSize =
  311. cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
  312. .bInterval = 0xFF,
  313. },
  314. {
  315. .bLength =
  316. sizeof(struct usb_endpoint_descriptor),
  317. .bDescriptorType = USB_DT_ENDPOINT,
  318. .bEndpointAddress = 0x03 | USB_DIR_IN,
  319. .bmAttributes = USB_ENDPOINT_XFER_INT,
  320. .wMaxPacketSize =
  321. cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
  322. .bInterval = 0xFF,
  323. },
  324. },
  325. },
  326. };
  327. /*
  328. * Static Function Prototypes
  329. */
  330. static void usbtty_init_strings (void);
  331. static void usbtty_init_instances (void);
  332. static void usbtty_init_endpoints (void);
  333. static void usbtty_init_terminal_type(short type);
  334. static void usbtty_event_handler (struct usb_device_instance *device,
  335. usb_device_event_t event, int data);
  336. static int usbtty_cdc_setup(struct usb_device_request *request,
  337. struct urb *urb);
  338. static int usbtty_configured (void);
  339. static int write_buffer (circbuf_t * buf);
  340. static int fill_buffer (circbuf_t * buf);
  341. void usbtty_poll (void);
  342. /* utility function for converting char* to wide string used by USB */
  343. static void str2wide (char *str, u16 * wide)
  344. {
  345. int i;
  346. for (i = 0; i < strlen (str) && str[i]; i++){
  347. #if defined(__LITTLE_ENDIAN)
  348. wide[i] = (u16) str[i];
  349. #elif defined(__BIG_ENDIAN)
  350. wide[i] = ((u16)(str[i])<<8);
  351. #else
  352. #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
  353. #endif
  354. }
  355. }
  356. /*
  357. * Test whether a character is in the RX buffer
  358. */
  359. int usbtty_tstc (void)
  360. {
  361. struct usb_endpoint_instance *endpoint =
  362. &endpoint_instance[rx_endpoint];
  363. /* If no input data exists, allow more RX to be accepted */
  364. if(usbtty_input.size <= 0){
  365. udc_unset_nak(endpoint->endpoint_address&0x03);
  366. }
  367. usbtty_poll ();
  368. return (usbtty_input.size > 0);
  369. }
  370. /*
  371. * Read a single byte from the usb client port. Returns 1 on success, 0
  372. * otherwise. When the function is succesfull, the character read is
  373. * written into its argument c.
  374. */
  375. int usbtty_getc (void)
  376. {
  377. char c;
  378. struct usb_endpoint_instance *endpoint =
  379. &endpoint_instance[rx_endpoint];
  380. while (usbtty_input.size <= 0) {
  381. udc_unset_nak(endpoint->endpoint_address&0x03);
  382. usbtty_poll ();
  383. }
  384. buf_pop (&usbtty_input, &c, 1);
  385. udc_set_nak(endpoint->endpoint_address&0x03);
  386. return c;
  387. }
  388. /*
  389. * Output a single byte to the usb client port.
  390. */
  391. void usbtty_putc (const char c)
  392. {
  393. buf_push (&usbtty_output, &c, 1);
  394. /* If \n, also do \r */
  395. if (c == '\n')
  396. buf_push (&usbtty_output, "\r", 1);
  397. /* Poll at end to handle new data... */
  398. if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
  399. usbtty_poll ();
  400. }
  401. }
  402. /* usbtty_puts() helper function for finding the next '\n' in a string */
  403. static int next_nl_pos (const char *s)
  404. {
  405. int i;
  406. for (i = 0; s[i] != '\0'; i++) {
  407. if (s[i] == '\n')
  408. return i;
  409. }
  410. return i;
  411. }
  412. /*
  413. * Output a string to the usb client port - implementing flow control
  414. */
  415. static void __usbtty_puts (const char *str, int len)
  416. {
  417. int maxlen = usbtty_output.totalsize;
  418. int space, n;
  419. /* break str into chunks < buffer size, if needed */
  420. while (len > 0) {
  421. usbtty_poll ();
  422. space = maxlen - usbtty_output.size;
  423. /* Empty buffer here, if needed, to ensure space... */
  424. if (space) {
  425. write_buffer (&usbtty_output);
  426. n = MIN (space, MIN (len, maxlen));
  427. buf_push (&usbtty_output, str, n);
  428. str += n;
  429. len -= n;
  430. }
  431. }
  432. }
  433. void usbtty_puts (const char *str)
  434. {
  435. int n;
  436. int 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 > %d)\n",
  471. sn, snlen, 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. }
  857. #endif