usbtty.c 26 KB

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