serial.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  1. /*
  2. * g_serial.c -- USB gadget serial driver
  3. *
  4. * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com)
  5. *
  6. * This code is based in part on the Gadget Zero driver, which
  7. * is Copyright (C) 2003 by David Brownell, all rights reserved.
  8. *
  9. * This code also borrows from usbserial.c, which is
  10. * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
  11. * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
  12. * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com)
  13. *
  14. * This software is distributed under the terms of the GNU General
  15. * Public License ("GPL") as published by the Free Software Foundation,
  16. * either version 2 of that License or (at your option) any later version.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/utsname.h>
  21. #include <linux/device.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/usb/ch9.h>
  25. #include <linux/usb/cdc.h>
  26. #include <linux/usb/gadget.h>
  27. #include "gadget_chips.h"
  28. /* Defines */
  29. #define GS_VERSION_STR "v2.2"
  30. #define GS_VERSION_NUM 0x0202
  31. #define GS_LONG_NAME "Gadget Serial"
  32. #define GS_SHORT_NAME "g_serial"
  33. #define GS_MAJOR 127
  34. #define GS_MINOR_START 0
  35. #define GS_NUM_PORTS 16
  36. #define GS_NUM_CONFIGS 1
  37. #define GS_NO_CONFIG_ID 0
  38. #define GS_BULK_CONFIG_ID 1
  39. #define GS_ACM_CONFIG_ID 2
  40. #define GS_MAX_NUM_INTERFACES 2
  41. #define GS_BULK_INTERFACE_ID 0
  42. #define GS_CONTROL_INTERFACE_ID 0
  43. #define GS_DATA_INTERFACE_ID 1
  44. #define GS_MAX_DESC_LEN 256
  45. #define GS_DEFAULT_READ_Q_SIZE 32
  46. #define GS_DEFAULT_WRITE_Q_SIZE 32
  47. #define GS_DEFAULT_WRITE_BUF_SIZE 8192
  48. #define GS_TMP_BUF_SIZE 8192
  49. #define GS_CLOSE_TIMEOUT 15
  50. #define GS_DEFAULT_USE_ACM 0
  51. #define GS_DEFAULT_DTE_RATE 9600
  52. #define GS_DEFAULT_DATA_BITS 8
  53. #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
  54. #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
  55. /* maxpacket and other transfer characteristics vary by speed. */
  56. static inline struct usb_endpoint_descriptor *
  57. choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
  58. struct usb_endpoint_descriptor *fs)
  59. {
  60. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  61. return hs;
  62. return fs;
  63. }
  64. /* debug settings */
  65. #ifdef DEBUG
  66. static int debug = 1;
  67. #else
  68. #define debug 0
  69. #endif
  70. #define gs_debug(format, arg...) \
  71. do { if (debug) pr_debug(format, ## arg); } while (0)
  72. #define gs_debug_level(level, format, arg...) \
  73. do { if (debug >= level) pr_debug(format, ## arg); } while (0)
  74. /* Thanks to NetChip Technologies for donating this product ID.
  75. *
  76. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  77. * Instead: allocate your own, using normal USB-IF procedures.
  78. */
  79. #define GS_VENDOR_ID 0x0525 /* NetChip */
  80. #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
  81. #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
  82. #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
  83. #define GS_NOTIFY_MAXPACKET 8
  84. /* Structures */
  85. struct gs_dev;
  86. /* circular buffer */
  87. struct gs_buf {
  88. unsigned int buf_size;
  89. char *buf_buf;
  90. char *buf_get;
  91. char *buf_put;
  92. };
  93. /* list of requests */
  94. struct gs_req_entry {
  95. struct list_head re_entry;
  96. struct usb_request *re_req;
  97. };
  98. /* the port structure holds info for each port, one for each minor number */
  99. struct gs_port {
  100. struct gs_dev *port_dev; /* pointer to device struct */
  101. struct tty_struct *port_tty; /* pointer to tty struct */
  102. spinlock_t port_lock;
  103. int port_num;
  104. int port_open_count;
  105. int port_in_use; /* open/close in progress */
  106. wait_queue_head_t port_write_wait;/* waiting to write */
  107. struct gs_buf *port_write_buf;
  108. struct usb_cdc_line_coding port_line_coding;
  109. };
  110. /* the device structure holds info for the USB device */
  111. struct gs_dev {
  112. struct usb_gadget *dev_gadget; /* gadget device pointer */
  113. spinlock_t dev_lock; /* lock for set/reset config */
  114. int dev_config; /* configuration number */
  115. struct usb_ep *dev_notify_ep; /* address of notify endpoint */
  116. struct usb_ep *dev_in_ep; /* address of in endpoint */
  117. struct usb_ep *dev_out_ep; /* address of out endpoint */
  118. struct usb_endpoint_descriptor /* descriptor of notify ep */
  119. *dev_notify_ep_desc;
  120. struct usb_endpoint_descriptor /* descriptor of in endpoint */
  121. *dev_in_ep_desc;
  122. struct usb_endpoint_descriptor /* descriptor of out endpoint */
  123. *dev_out_ep_desc;
  124. struct usb_request *dev_ctrl_req; /* control request */
  125. struct list_head dev_req_list; /* list of write requests */
  126. int dev_sched_port; /* round robin port scheduled */
  127. struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
  128. };
  129. /* Functions */
  130. /* module */
  131. static int __init gs_module_init(void);
  132. static void __exit gs_module_exit(void);
  133. /* tty driver */
  134. static int gs_open(struct tty_struct *tty, struct file *file);
  135. static void gs_close(struct tty_struct *tty, struct file *file);
  136. static int gs_write(struct tty_struct *tty,
  137. const unsigned char *buf, int count);
  138. static void gs_put_char(struct tty_struct *tty, unsigned char ch);
  139. static void gs_flush_chars(struct tty_struct *tty);
  140. static int gs_write_room(struct tty_struct *tty);
  141. static int gs_chars_in_buffer(struct tty_struct *tty);
  142. static void gs_throttle(struct tty_struct * tty);
  143. static void gs_unthrottle(struct tty_struct * tty);
  144. static void gs_break(struct tty_struct *tty, int break_state);
  145. static int gs_ioctl(struct tty_struct *tty, struct file *file,
  146. unsigned int cmd, unsigned long arg);
  147. static void gs_set_termios(struct tty_struct *tty, struct ktermios *old);
  148. static int gs_send(struct gs_dev *dev);
  149. static int gs_send_packet(struct gs_dev *dev, char *packet,
  150. unsigned int size);
  151. static int gs_recv_packet(struct gs_dev *dev, char *packet,
  152. unsigned int size);
  153. static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
  154. static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
  155. /* gadget driver */
  156. static int gs_bind(struct usb_gadget *gadget);
  157. static void gs_unbind(struct usb_gadget *gadget);
  158. static int gs_setup(struct usb_gadget *gadget,
  159. const struct usb_ctrlrequest *ctrl);
  160. static int gs_setup_standard(struct usb_gadget *gadget,
  161. const struct usb_ctrlrequest *ctrl);
  162. static int gs_setup_class(struct usb_gadget *gadget,
  163. const struct usb_ctrlrequest *ctrl);
  164. static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
  165. static void gs_disconnect(struct usb_gadget *gadget);
  166. static int gs_set_config(struct gs_dev *dev, unsigned config);
  167. static void gs_reset_config(struct gs_dev *dev);
  168. static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
  169. u8 type, unsigned int index, int is_otg);
  170. static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
  171. gfp_t kmalloc_flags);
  172. static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
  173. static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
  174. gfp_t kmalloc_flags);
  175. static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
  176. static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags);
  177. static void gs_free_ports(struct gs_dev *dev);
  178. /* circular buffer */
  179. static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags);
  180. static void gs_buf_free(struct gs_buf *gb);
  181. static void gs_buf_clear(struct gs_buf *gb);
  182. static unsigned int gs_buf_data_avail(struct gs_buf *gb);
  183. static unsigned int gs_buf_space_avail(struct gs_buf *gb);
  184. static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
  185. unsigned int count);
  186. static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
  187. unsigned int count);
  188. /* external functions */
  189. extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
  190. /* Globals */
  191. static struct gs_dev *gs_device;
  192. static const char *EP_IN_NAME;
  193. static const char *EP_OUT_NAME;
  194. static const char *EP_NOTIFY_NAME;
  195. static struct mutex gs_open_close_lock[GS_NUM_PORTS];
  196. static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
  197. static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
  198. static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
  199. static unsigned int use_acm = GS_DEFAULT_USE_ACM;
  200. /* tty driver struct */
  201. static const struct tty_operations gs_tty_ops = {
  202. .open = gs_open,
  203. .close = gs_close,
  204. .write = gs_write,
  205. .put_char = gs_put_char,
  206. .flush_chars = gs_flush_chars,
  207. .write_room = gs_write_room,
  208. .ioctl = gs_ioctl,
  209. .set_termios = gs_set_termios,
  210. .throttle = gs_throttle,
  211. .unthrottle = gs_unthrottle,
  212. .break_ctl = gs_break,
  213. .chars_in_buffer = gs_chars_in_buffer,
  214. };
  215. static struct tty_driver *gs_tty_driver;
  216. /* gadget driver struct */
  217. static struct usb_gadget_driver gs_gadget_driver = {
  218. #ifdef CONFIG_USB_GADGET_DUALSPEED
  219. .speed = USB_SPEED_HIGH,
  220. #else
  221. .speed = USB_SPEED_FULL,
  222. #endif /* CONFIG_USB_GADGET_DUALSPEED */
  223. .function = GS_LONG_NAME,
  224. .bind = gs_bind,
  225. .unbind = gs_unbind,
  226. .setup = gs_setup,
  227. .disconnect = gs_disconnect,
  228. .driver = {
  229. .name = GS_SHORT_NAME,
  230. },
  231. };
  232. /* USB descriptors */
  233. #define GS_MANUFACTURER_STR_ID 1
  234. #define GS_PRODUCT_STR_ID 2
  235. #define GS_SERIAL_STR_ID 3
  236. #define GS_BULK_CONFIG_STR_ID 4
  237. #define GS_ACM_CONFIG_STR_ID 5
  238. #define GS_CONTROL_STR_ID 6
  239. #define GS_DATA_STR_ID 7
  240. /* static strings, in UTF-8 */
  241. static char manufacturer[50];
  242. static struct usb_string gs_strings[] = {
  243. { GS_MANUFACTURER_STR_ID, manufacturer },
  244. { GS_PRODUCT_STR_ID, GS_LONG_NAME },
  245. { GS_SERIAL_STR_ID, "0" },
  246. { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
  247. { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
  248. { GS_CONTROL_STR_ID, "Gadget Serial Control" },
  249. { GS_DATA_STR_ID, "Gadget Serial Data" },
  250. { } /* end of list */
  251. };
  252. static struct usb_gadget_strings gs_string_table = {
  253. .language = 0x0409, /* en-us */
  254. .strings = gs_strings,
  255. };
  256. static struct usb_device_descriptor gs_device_desc = {
  257. .bLength = USB_DT_DEVICE_SIZE,
  258. .bDescriptorType = USB_DT_DEVICE,
  259. .bcdUSB = __constant_cpu_to_le16(0x0200),
  260. .bDeviceSubClass = 0,
  261. .bDeviceProtocol = 0,
  262. .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
  263. .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
  264. .iManufacturer = GS_MANUFACTURER_STR_ID,
  265. .iProduct = GS_PRODUCT_STR_ID,
  266. .iSerialNumber = GS_SERIAL_STR_ID,
  267. .bNumConfigurations = GS_NUM_CONFIGS,
  268. };
  269. static struct usb_otg_descriptor gs_otg_descriptor = {
  270. .bLength = sizeof(gs_otg_descriptor),
  271. .bDescriptorType = USB_DT_OTG,
  272. .bmAttributes = USB_OTG_SRP,
  273. };
  274. static struct usb_config_descriptor gs_bulk_config_desc = {
  275. .bLength = USB_DT_CONFIG_SIZE,
  276. .bDescriptorType = USB_DT_CONFIG,
  277. /* .wTotalLength computed dynamically */
  278. .bNumInterfaces = 1,
  279. .bConfigurationValue = GS_BULK_CONFIG_ID,
  280. .iConfiguration = GS_BULK_CONFIG_STR_ID,
  281. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  282. .bMaxPower = 1,
  283. };
  284. static struct usb_config_descriptor gs_acm_config_desc = {
  285. .bLength = USB_DT_CONFIG_SIZE,
  286. .bDescriptorType = USB_DT_CONFIG,
  287. /* .wTotalLength computed dynamically */
  288. .bNumInterfaces = 2,
  289. .bConfigurationValue = GS_ACM_CONFIG_ID,
  290. .iConfiguration = GS_ACM_CONFIG_STR_ID,
  291. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  292. .bMaxPower = 1,
  293. };
  294. static const struct usb_interface_descriptor gs_bulk_interface_desc = {
  295. .bLength = USB_DT_INTERFACE_SIZE,
  296. .bDescriptorType = USB_DT_INTERFACE,
  297. .bInterfaceNumber = GS_BULK_INTERFACE_ID,
  298. .bNumEndpoints = 2,
  299. .bInterfaceClass = USB_CLASS_CDC_DATA,
  300. .bInterfaceSubClass = 0,
  301. .bInterfaceProtocol = 0,
  302. .iInterface = GS_DATA_STR_ID,
  303. };
  304. static const struct usb_interface_descriptor gs_control_interface_desc = {
  305. .bLength = USB_DT_INTERFACE_SIZE,
  306. .bDescriptorType = USB_DT_INTERFACE,
  307. .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
  308. .bNumEndpoints = 1,
  309. .bInterfaceClass = USB_CLASS_COMM,
  310. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  311. .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  312. .iInterface = GS_CONTROL_STR_ID,
  313. };
  314. static const struct usb_interface_descriptor gs_data_interface_desc = {
  315. .bLength = USB_DT_INTERFACE_SIZE,
  316. .bDescriptorType = USB_DT_INTERFACE,
  317. .bInterfaceNumber = GS_DATA_INTERFACE_ID,
  318. .bNumEndpoints = 2,
  319. .bInterfaceClass = USB_CLASS_CDC_DATA,
  320. .bInterfaceSubClass = 0,
  321. .bInterfaceProtocol = 0,
  322. .iInterface = GS_DATA_STR_ID,
  323. };
  324. static const struct usb_cdc_header_desc gs_header_desc = {
  325. .bLength = sizeof(gs_header_desc),
  326. .bDescriptorType = USB_DT_CS_INTERFACE,
  327. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  328. .bcdCDC = __constant_cpu_to_le16(0x0110),
  329. };
  330. static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
  331. .bLength = sizeof(gs_call_mgmt_descriptor),
  332. .bDescriptorType = USB_DT_CS_INTERFACE,
  333. .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
  334. .bmCapabilities = 0,
  335. .bDataInterface = 1, /* index of data interface */
  336. };
  337. static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
  338. .bLength = sizeof(gs_acm_descriptor),
  339. .bDescriptorType = USB_DT_CS_INTERFACE,
  340. .bDescriptorSubType = USB_CDC_ACM_TYPE,
  341. .bmCapabilities = 0,
  342. };
  343. static const struct usb_cdc_union_desc gs_union_desc = {
  344. .bLength = sizeof(gs_union_desc),
  345. .bDescriptorType = USB_DT_CS_INTERFACE,
  346. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  347. .bMasterInterface0 = 0, /* index of control interface */
  348. .bSlaveInterface0 = 1, /* index of data interface */
  349. };
  350. static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
  351. .bLength = USB_DT_ENDPOINT_SIZE,
  352. .bDescriptorType = USB_DT_ENDPOINT,
  353. .bEndpointAddress = USB_DIR_IN,
  354. .bmAttributes = USB_ENDPOINT_XFER_INT,
  355. .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
  356. .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
  357. };
  358. static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
  359. .bLength = USB_DT_ENDPOINT_SIZE,
  360. .bDescriptorType = USB_DT_ENDPOINT,
  361. .bEndpointAddress = USB_DIR_IN,
  362. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  363. };
  364. static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
  365. .bLength = USB_DT_ENDPOINT_SIZE,
  366. .bDescriptorType = USB_DT_ENDPOINT,
  367. .bEndpointAddress = USB_DIR_OUT,
  368. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  369. };
  370. static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
  371. (struct usb_descriptor_header *) &gs_otg_descriptor,
  372. (struct usb_descriptor_header *) &gs_bulk_interface_desc,
  373. (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
  374. (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
  375. NULL,
  376. };
  377. static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
  378. (struct usb_descriptor_header *) &gs_otg_descriptor,
  379. (struct usb_descriptor_header *) &gs_control_interface_desc,
  380. (struct usb_descriptor_header *) &gs_header_desc,
  381. (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
  382. (struct usb_descriptor_header *) &gs_acm_descriptor,
  383. (struct usb_descriptor_header *) &gs_union_desc,
  384. (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
  385. (struct usb_descriptor_header *) &gs_data_interface_desc,
  386. (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
  387. (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
  388. NULL,
  389. };
  390. static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
  391. .bLength = USB_DT_ENDPOINT_SIZE,
  392. .bDescriptorType = USB_DT_ENDPOINT,
  393. .bEndpointAddress = USB_DIR_IN,
  394. .bmAttributes = USB_ENDPOINT_XFER_INT,
  395. .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
  396. .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
  397. };
  398. static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
  399. .bLength = USB_DT_ENDPOINT_SIZE,
  400. .bDescriptorType = USB_DT_ENDPOINT,
  401. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  402. .wMaxPacketSize = __constant_cpu_to_le16(512),
  403. };
  404. static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
  405. .bLength = USB_DT_ENDPOINT_SIZE,
  406. .bDescriptorType = USB_DT_ENDPOINT,
  407. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  408. .wMaxPacketSize = __constant_cpu_to_le16(512),
  409. };
  410. static struct usb_qualifier_descriptor gs_qualifier_desc = {
  411. .bLength = sizeof(struct usb_qualifier_descriptor),
  412. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  413. .bcdUSB = __constant_cpu_to_le16 (0x0200),
  414. /* assumes ep0 uses the same value for both speeds ... */
  415. .bNumConfigurations = GS_NUM_CONFIGS,
  416. };
  417. static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
  418. (struct usb_descriptor_header *) &gs_otg_descriptor,
  419. (struct usb_descriptor_header *) &gs_bulk_interface_desc,
  420. (struct usb_descriptor_header *) &gs_highspeed_in_desc,
  421. (struct usb_descriptor_header *) &gs_highspeed_out_desc,
  422. NULL,
  423. };
  424. static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
  425. (struct usb_descriptor_header *) &gs_otg_descriptor,
  426. (struct usb_descriptor_header *) &gs_control_interface_desc,
  427. (struct usb_descriptor_header *) &gs_header_desc,
  428. (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
  429. (struct usb_descriptor_header *) &gs_acm_descriptor,
  430. (struct usb_descriptor_header *) &gs_union_desc,
  431. (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
  432. (struct usb_descriptor_header *) &gs_data_interface_desc,
  433. (struct usb_descriptor_header *) &gs_highspeed_in_desc,
  434. (struct usb_descriptor_header *) &gs_highspeed_out_desc,
  435. NULL,
  436. };
  437. /* Module */
  438. MODULE_DESCRIPTION(GS_LONG_NAME);
  439. MODULE_AUTHOR("Al Borchers");
  440. MODULE_LICENSE("GPL");
  441. #ifdef DEBUG
  442. module_param(debug, int, S_IRUGO|S_IWUSR);
  443. MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
  444. #endif
  445. module_param(read_q_size, uint, S_IRUGO);
  446. MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
  447. module_param(write_q_size, uint, S_IRUGO);
  448. MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
  449. module_param(write_buf_size, uint, S_IRUGO);
  450. MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
  451. module_param(use_acm, uint, S_IRUGO);
  452. MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
  453. module_init(gs_module_init);
  454. module_exit(gs_module_exit);
  455. /*
  456. * gs_module_init
  457. *
  458. * Register as a USB gadget driver and a tty driver.
  459. */
  460. static int __init gs_module_init(void)
  461. {
  462. int i;
  463. int retval;
  464. retval = usb_gadget_register_driver(&gs_gadget_driver);
  465. if (retval) {
  466. pr_err("gs_module_init: cannot register gadget driver, "
  467. "ret=%d\n", retval);
  468. return retval;
  469. }
  470. gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
  471. if (!gs_tty_driver)
  472. return -ENOMEM;
  473. gs_tty_driver->owner = THIS_MODULE;
  474. gs_tty_driver->driver_name = GS_SHORT_NAME;
  475. gs_tty_driver->name = "ttygs";
  476. gs_tty_driver->major = GS_MAJOR;
  477. gs_tty_driver->minor_start = GS_MINOR_START;
  478. gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  479. gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  480. gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  481. gs_tty_driver->init_termios = tty_std_termios;
  482. gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  483. tty_set_operations(gs_tty_driver, &gs_tty_ops);
  484. for (i=0; i < GS_NUM_PORTS; i++)
  485. mutex_init(&gs_open_close_lock[i]);
  486. retval = tty_register_driver(gs_tty_driver);
  487. if (retval) {
  488. usb_gadget_unregister_driver(&gs_gadget_driver);
  489. put_tty_driver(gs_tty_driver);
  490. pr_err("gs_module_init: cannot register tty driver, "
  491. "ret=%d\n", retval);
  492. return retval;
  493. }
  494. pr_info("gs_module_init: %s %s loaded\n",
  495. GS_LONG_NAME, GS_VERSION_STR);
  496. return 0;
  497. }
  498. /*
  499. * gs_module_exit
  500. *
  501. * Unregister as a tty driver and a USB gadget driver.
  502. */
  503. static void __exit gs_module_exit(void)
  504. {
  505. tty_unregister_driver(gs_tty_driver);
  506. put_tty_driver(gs_tty_driver);
  507. usb_gadget_unregister_driver(&gs_gadget_driver);
  508. pr_info("gs_module_exit: %s %s unloaded\n",
  509. GS_LONG_NAME, GS_VERSION_STR);
  510. }
  511. /* TTY Driver */
  512. /*
  513. * gs_open
  514. */
  515. static int gs_open(struct tty_struct *tty, struct file *file)
  516. {
  517. int port_num;
  518. unsigned long flags;
  519. struct gs_port *port;
  520. struct gs_dev *dev;
  521. struct gs_buf *buf;
  522. struct mutex *mtx;
  523. int ret;
  524. port_num = tty->index;
  525. gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
  526. if (port_num < 0 || port_num >= GS_NUM_PORTS) {
  527. pr_err("gs_open: (%d,%p,%p) invalid port number\n",
  528. port_num, tty, file);
  529. return -ENODEV;
  530. }
  531. dev = gs_device;
  532. if (dev == NULL) {
  533. pr_err("gs_open: (%d,%p,%p) NULL device pointer\n",
  534. port_num, tty, file);
  535. return -ENODEV;
  536. }
  537. mtx = &gs_open_close_lock[port_num];
  538. if (mutex_lock_interruptible(mtx)) {
  539. pr_err("gs_open: (%d,%p,%p) interrupted waiting for mutex\n",
  540. port_num, tty, file);
  541. return -ERESTARTSYS;
  542. }
  543. spin_lock_irqsave(&dev->dev_lock, flags);
  544. if (dev->dev_config == GS_NO_CONFIG_ID) {
  545. pr_err("gs_open: (%d,%p,%p) device is not connected\n",
  546. port_num, tty, file);
  547. ret = -ENODEV;
  548. goto exit_unlock_dev;
  549. }
  550. port = dev->dev_port[port_num];
  551. if (port == NULL) {
  552. pr_err("gs_open: (%d,%p,%p) NULL port pointer\n",
  553. port_num, tty, file);
  554. ret = -ENODEV;
  555. goto exit_unlock_dev;
  556. }
  557. spin_lock(&port->port_lock);
  558. spin_unlock(&dev->dev_lock);
  559. if (port->port_dev == NULL) {
  560. pr_err("gs_open: (%d,%p,%p) port disconnected (1)\n",
  561. port_num, tty, file);
  562. ret = -EIO;
  563. goto exit_unlock_port;
  564. }
  565. if (port->port_open_count > 0) {
  566. ++port->port_open_count;
  567. gs_debug("gs_open: (%d,%p,%p) already open\n",
  568. port_num, tty, file);
  569. ret = 0;
  570. goto exit_unlock_port;
  571. }
  572. tty->driver_data = NULL;
  573. /* mark port as in use, we can drop port lock and sleep if necessary */
  574. port->port_in_use = 1;
  575. /* allocate write buffer on first open */
  576. if (port->port_write_buf == NULL) {
  577. spin_unlock_irqrestore(&port->port_lock, flags);
  578. buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
  579. spin_lock_irqsave(&port->port_lock, flags);
  580. /* might have been disconnected while asleep, check */
  581. if (port->port_dev == NULL) {
  582. pr_err("gs_open: (%d,%p,%p) port disconnected (2)\n",
  583. port_num, tty, file);
  584. port->port_in_use = 0;
  585. ret = -EIO;
  586. goto exit_unlock_port;
  587. }
  588. if ((port->port_write_buf=buf) == NULL) {
  589. pr_err("gs_open: (%d,%p,%p) cannot allocate "
  590. "port write buffer\n",
  591. port_num, tty, file);
  592. port->port_in_use = 0;
  593. ret = -ENOMEM;
  594. goto exit_unlock_port;
  595. }
  596. }
  597. /* wait for carrier detect (not implemented) */
  598. /* might have been disconnected while asleep, check */
  599. if (port->port_dev == NULL) {
  600. pr_err("gs_open: (%d,%p,%p) port disconnected (3)\n",
  601. port_num, tty, file);
  602. port->port_in_use = 0;
  603. ret = -EIO;
  604. goto exit_unlock_port;
  605. }
  606. tty->driver_data = port;
  607. port->port_tty = tty;
  608. port->port_open_count = 1;
  609. port->port_in_use = 0;
  610. gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
  611. ret = 0;
  612. exit_unlock_port:
  613. spin_unlock_irqrestore(&port->port_lock, flags);
  614. mutex_unlock(mtx);
  615. return ret;
  616. exit_unlock_dev:
  617. spin_unlock_irqrestore(&dev->dev_lock, flags);
  618. mutex_unlock(mtx);
  619. return ret;
  620. }
  621. /*
  622. * gs_close
  623. */
  624. #define GS_WRITE_FINISHED_EVENT_SAFELY(p) \
  625. ({ \
  626. int cond; \
  627. \
  628. spin_lock_irq(&(p)->port_lock); \
  629. cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \
  630. spin_unlock_irq(&(p)->port_lock); \
  631. cond; \
  632. })
  633. static void gs_close(struct tty_struct *tty, struct file *file)
  634. {
  635. struct gs_port *port = tty->driver_data;
  636. struct mutex *mtx;
  637. if (port == NULL) {
  638. pr_err("gs_close: NULL port pointer\n");
  639. return;
  640. }
  641. gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
  642. mtx = &gs_open_close_lock[port->port_num];
  643. mutex_lock(mtx);
  644. spin_lock_irq(&port->port_lock);
  645. if (port->port_open_count == 0) {
  646. pr_err("gs_close: (%d,%p,%p) port is already closed\n",
  647. port->port_num, tty, file);
  648. goto exit;
  649. }
  650. if (port->port_open_count > 1) {
  651. --port->port_open_count;
  652. goto exit;
  653. }
  654. /* free disconnected port on final close */
  655. if (port->port_dev == NULL) {
  656. kfree(port);
  657. goto exit;
  658. }
  659. /* mark port as closed but in use, we can drop port lock */
  660. /* and sleep if necessary */
  661. port->port_in_use = 1;
  662. port->port_open_count = 0;
  663. /* wait for write buffer to drain, or */
  664. /* at most GS_CLOSE_TIMEOUT seconds */
  665. if (gs_buf_data_avail(port->port_write_buf) > 0) {
  666. spin_unlock_irq(&port->port_lock);
  667. wait_event_interruptible_timeout(port->port_write_wait,
  668. GS_WRITE_FINISHED_EVENT_SAFELY(port),
  669. GS_CLOSE_TIMEOUT * HZ);
  670. spin_lock_irq(&port->port_lock);
  671. }
  672. /* free disconnected port on final close */
  673. /* (might have happened during the above sleep) */
  674. if (port->port_dev == NULL) {
  675. kfree(port);
  676. goto exit;
  677. }
  678. gs_buf_clear(port->port_write_buf);
  679. tty->driver_data = NULL;
  680. port->port_tty = NULL;
  681. port->port_in_use = 0;
  682. gs_debug("gs_close: (%d,%p,%p) completed\n",
  683. port->port_num, tty, file);
  684. exit:
  685. spin_unlock_irq(&port->port_lock);
  686. mutex_unlock(mtx);
  687. }
  688. /*
  689. * gs_write
  690. */
  691. static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
  692. {
  693. unsigned long flags;
  694. struct gs_port *port = tty->driver_data;
  695. int ret;
  696. if (port == NULL) {
  697. pr_err("gs_write: NULL port pointer\n");
  698. return -EIO;
  699. }
  700. gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
  701. count);
  702. if (count == 0)
  703. return 0;
  704. spin_lock_irqsave(&port->port_lock, flags);
  705. if (port->port_dev == NULL) {
  706. pr_err("gs_write: (%d,%p) port is not connected\n",
  707. port->port_num, tty);
  708. ret = -EIO;
  709. goto exit;
  710. }
  711. if (port->port_open_count == 0) {
  712. pr_err("gs_write: (%d,%p) port is closed\n",
  713. port->port_num, tty);
  714. ret = -EBADF;
  715. goto exit;
  716. }
  717. count = gs_buf_put(port->port_write_buf, buf, count);
  718. spin_unlock_irqrestore(&port->port_lock, flags);
  719. gs_send(gs_device);
  720. gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
  721. count);
  722. return count;
  723. exit:
  724. spin_unlock_irqrestore(&port->port_lock, flags);
  725. return ret;
  726. }
  727. /*
  728. * gs_put_char
  729. */
  730. static void gs_put_char(struct tty_struct *tty, unsigned char ch)
  731. {
  732. unsigned long flags;
  733. struct gs_port *port = tty->driver_data;
  734. if (port == NULL) {
  735. pr_err("gs_put_char: NULL port pointer\n");
  736. return;
  737. }
  738. gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
  739. port->port_num, tty, ch, __builtin_return_address(0));
  740. spin_lock_irqsave(&port->port_lock, flags);
  741. if (port->port_dev == NULL) {
  742. pr_err("gs_put_char: (%d,%p) port is not connected\n",
  743. port->port_num, tty);
  744. goto exit;
  745. }
  746. if (port->port_open_count == 0) {
  747. pr_err("gs_put_char: (%d,%p) port is closed\n",
  748. port->port_num, tty);
  749. goto exit;
  750. }
  751. gs_buf_put(port->port_write_buf, &ch, 1);
  752. exit:
  753. spin_unlock_irqrestore(&port->port_lock, flags);
  754. }
  755. /*
  756. * gs_flush_chars
  757. */
  758. static void gs_flush_chars(struct tty_struct *tty)
  759. {
  760. unsigned long flags;
  761. struct gs_port *port = tty->driver_data;
  762. if (port == NULL) {
  763. pr_err("gs_flush_chars: NULL port pointer\n");
  764. return;
  765. }
  766. gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
  767. spin_lock_irqsave(&port->port_lock, flags);
  768. if (port->port_dev == NULL) {
  769. pr_err("gs_flush_chars: (%d,%p) port is not connected\n",
  770. port->port_num, tty);
  771. goto exit;
  772. }
  773. if (port->port_open_count == 0) {
  774. pr_err("gs_flush_chars: (%d,%p) port is closed\n",
  775. port->port_num, tty);
  776. goto exit;
  777. }
  778. spin_unlock_irqrestore(&port->port_lock, flags);
  779. gs_send(gs_device);
  780. return;
  781. exit:
  782. spin_unlock_irqrestore(&port->port_lock, flags);
  783. }
  784. /*
  785. * gs_write_room
  786. */
  787. static int gs_write_room(struct tty_struct *tty)
  788. {
  789. int room = 0;
  790. unsigned long flags;
  791. struct gs_port *port = tty->driver_data;
  792. if (port == NULL)
  793. return 0;
  794. spin_lock_irqsave(&port->port_lock, flags);
  795. if (port->port_dev != NULL && port->port_open_count > 0
  796. && port->port_write_buf != NULL)
  797. room = gs_buf_space_avail(port->port_write_buf);
  798. spin_unlock_irqrestore(&port->port_lock, flags);
  799. gs_debug("gs_write_room: (%d,%p) room=%d\n",
  800. port->port_num, tty, room);
  801. return room;
  802. }
  803. /*
  804. * gs_chars_in_buffer
  805. */
  806. static int gs_chars_in_buffer(struct tty_struct *tty)
  807. {
  808. int chars = 0;
  809. unsigned long flags;
  810. struct gs_port *port = tty->driver_data;
  811. if (port == NULL)
  812. return 0;
  813. spin_lock_irqsave(&port->port_lock, flags);
  814. if (port->port_dev != NULL && port->port_open_count > 0
  815. && port->port_write_buf != NULL)
  816. chars = gs_buf_data_avail(port->port_write_buf);
  817. spin_unlock_irqrestore(&port->port_lock, flags);
  818. gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
  819. port->port_num, tty, chars);
  820. return chars;
  821. }
  822. /*
  823. * gs_throttle
  824. */
  825. static void gs_throttle(struct tty_struct *tty)
  826. {
  827. }
  828. /*
  829. * gs_unthrottle
  830. */
  831. static void gs_unthrottle(struct tty_struct *tty)
  832. {
  833. }
  834. /*
  835. * gs_break
  836. */
  837. static void gs_break(struct tty_struct *tty, int break_state)
  838. {
  839. }
  840. /*
  841. * gs_ioctl
  842. */
  843. static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  844. {
  845. struct gs_port *port = tty->driver_data;
  846. if (port == NULL) {
  847. pr_err("gs_ioctl: NULL port pointer\n");
  848. return -EIO;
  849. }
  850. gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
  851. port->port_num, tty, file, cmd, arg);
  852. /* handle ioctls */
  853. /* could not handle ioctl */
  854. return -ENOIOCTLCMD;
  855. }
  856. /*
  857. * gs_set_termios
  858. */
  859. static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
  860. {
  861. }
  862. /*
  863. * gs_send
  864. *
  865. * This function finds available write requests, calls
  866. * gs_send_packet to fill these packets with data, and
  867. * continues until either there are no more write requests
  868. * available or no more data to send. This function is
  869. * run whenever data arrives or write requests are available.
  870. */
  871. static int gs_send(struct gs_dev *dev)
  872. {
  873. int ret,len;
  874. unsigned long flags;
  875. struct usb_ep *ep;
  876. struct usb_request *req;
  877. struct gs_req_entry *req_entry;
  878. if (dev == NULL) {
  879. pr_err("gs_send: NULL device pointer\n");
  880. return -ENODEV;
  881. }
  882. spin_lock_irqsave(&dev->dev_lock, flags);
  883. ep = dev->dev_in_ep;
  884. while(!list_empty(&dev->dev_req_list)) {
  885. req_entry = list_entry(dev->dev_req_list.next,
  886. struct gs_req_entry, re_entry);
  887. req = req_entry->re_req;
  888. len = gs_send_packet(dev, req->buf, ep->maxpacket);
  889. if (len > 0) {
  890. gs_debug_level(3, "gs_send: len=%d, 0x%2.2x "
  891. "0x%2.2x 0x%2.2x ...\n", len,
  892. *((unsigned char *)req->buf),
  893. *((unsigned char *)req->buf+1),
  894. *((unsigned char *)req->buf+2));
  895. list_del(&req_entry->re_entry);
  896. req->length = len;
  897. spin_unlock_irqrestore(&dev->dev_lock, flags);
  898. if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
  899. pr_err(
  900. "gs_send: cannot queue read request, ret=%d\n",
  901. ret);
  902. spin_lock_irqsave(&dev->dev_lock, flags);
  903. break;
  904. }
  905. spin_lock_irqsave(&dev->dev_lock, flags);
  906. } else {
  907. break;
  908. }
  909. }
  910. spin_unlock_irqrestore(&dev->dev_lock, flags);
  911. return 0;
  912. }
  913. /*
  914. * gs_send_packet
  915. *
  916. * If there is data to send, a packet is built in the given
  917. * buffer and the size is returned. If there is no data to
  918. * send, 0 is returned. If there is any error a negative
  919. * error number is returned.
  920. *
  921. * Called during USB completion routine, on interrupt time.
  922. *
  923. * We assume that disconnect will not happen until all completion
  924. * routines have completed, so we can assume that the dev_port
  925. * array does not change during the lifetime of this function.
  926. */
  927. static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
  928. {
  929. unsigned int len;
  930. struct gs_port *port;
  931. /* TEMPORARY -- only port 0 is supported right now */
  932. port = dev->dev_port[0];
  933. if (port == NULL) {
  934. pr_err("gs_send_packet: port=%d, NULL port pointer\n", 0);
  935. return -EIO;
  936. }
  937. spin_lock(&port->port_lock);
  938. len = gs_buf_data_avail(port->port_write_buf);
  939. if (len < size)
  940. size = len;
  941. if (size == 0)
  942. goto exit;
  943. size = gs_buf_get(port->port_write_buf, packet, size);
  944. if (port->port_tty)
  945. wake_up_interruptible(&port->port_tty->write_wait);
  946. exit:
  947. spin_unlock(&port->port_lock);
  948. return size;
  949. }
  950. /*
  951. * gs_recv_packet
  952. *
  953. * Called for each USB packet received. Reads the packet
  954. * header and stuffs the data in the appropriate tty buffer.
  955. * Returns 0 if successful, or a negative error number.
  956. *
  957. * Called during USB completion routine, on interrupt time.
  958. *
  959. * We assume that disconnect will not happen until all completion
  960. * routines have completed, so we can assume that the dev_port
  961. * array does not change during the lifetime of this function.
  962. */
  963. static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
  964. {
  965. unsigned int len;
  966. struct gs_port *port;
  967. int ret;
  968. struct tty_struct *tty;
  969. /* TEMPORARY -- only port 0 is supported right now */
  970. port = dev->dev_port[0];
  971. if (port == NULL) {
  972. pr_err("gs_recv_packet: port=%d, NULL port pointer\n",
  973. port->port_num);
  974. return -EIO;
  975. }
  976. spin_lock(&port->port_lock);
  977. if (port->port_open_count == 0) {
  978. pr_err("gs_recv_packet: port=%d, port is closed\n",
  979. port->port_num);
  980. ret = -EIO;
  981. goto exit;
  982. }
  983. tty = port->port_tty;
  984. if (tty == NULL) {
  985. pr_err("gs_recv_packet: port=%d, NULL tty pointer\n",
  986. port->port_num);
  987. ret = -EIO;
  988. goto exit;
  989. }
  990. if (port->port_tty->magic != TTY_MAGIC) {
  991. pr_err("gs_recv_packet: port=%d, bad tty magic\n",
  992. port->port_num);
  993. ret = -EIO;
  994. goto exit;
  995. }
  996. len = tty_buffer_request_room(tty, size);
  997. if (len > 0) {
  998. tty_insert_flip_string(tty, packet, len);
  999. tty_flip_buffer_push(port->port_tty);
  1000. wake_up_interruptible(&port->port_tty->read_wait);
  1001. }
  1002. ret = 0;
  1003. exit:
  1004. spin_unlock(&port->port_lock);
  1005. return ret;
  1006. }
  1007. /*
  1008. * gs_read_complete
  1009. */
  1010. static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
  1011. {
  1012. int ret;
  1013. struct gs_dev *dev = ep->driver_data;
  1014. if (dev == NULL) {
  1015. pr_err("gs_read_complete: NULL device pointer\n");
  1016. return;
  1017. }
  1018. switch(req->status) {
  1019. case 0:
  1020. /* normal completion */
  1021. gs_recv_packet(dev, req->buf, req->actual);
  1022. requeue:
  1023. req->length = ep->maxpacket;
  1024. if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
  1025. pr_err(
  1026. "gs_read_complete: cannot queue read request, ret=%d\n",
  1027. ret);
  1028. }
  1029. break;
  1030. case -ESHUTDOWN:
  1031. /* disconnect */
  1032. gs_debug("gs_read_complete: shutdown\n");
  1033. gs_free_req(ep, req);
  1034. break;
  1035. default:
  1036. /* unexpected */
  1037. pr_err(
  1038. "gs_read_complete: unexpected status error, status=%d\n",
  1039. req->status);
  1040. goto requeue;
  1041. break;
  1042. }
  1043. }
  1044. /*
  1045. * gs_write_complete
  1046. */
  1047. static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
  1048. {
  1049. struct gs_dev *dev = ep->driver_data;
  1050. struct gs_req_entry *gs_req = req->context;
  1051. if (dev == NULL) {
  1052. pr_err("gs_write_complete: NULL device pointer\n");
  1053. return;
  1054. }
  1055. switch(req->status) {
  1056. case 0:
  1057. /* normal completion */
  1058. requeue:
  1059. if (gs_req == NULL) {
  1060. pr_err("gs_write_complete: NULL request pointer\n");
  1061. return;
  1062. }
  1063. spin_lock(&dev->dev_lock);
  1064. list_add(&gs_req->re_entry, &dev->dev_req_list);
  1065. spin_unlock(&dev->dev_lock);
  1066. gs_send(dev);
  1067. break;
  1068. case -ESHUTDOWN:
  1069. /* disconnect */
  1070. gs_debug("gs_write_complete: shutdown\n");
  1071. gs_free_req(ep, req);
  1072. break;
  1073. default:
  1074. pr_err(
  1075. "gs_write_complete: unexpected status error, status=%d\n",
  1076. req->status);
  1077. goto requeue;
  1078. break;
  1079. }
  1080. }
  1081. /* Gadget Driver */
  1082. /*
  1083. * gs_bind
  1084. *
  1085. * Called on module load. Allocates and initializes the device
  1086. * structure and a control request.
  1087. */
  1088. static int __init gs_bind(struct usb_gadget *gadget)
  1089. {
  1090. int ret;
  1091. struct usb_ep *ep;
  1092. struct gs_dev *dev;
  1093. int gcnum;
  1094. /* Some controllers can't support CDC ACM:
  1095. * - sh doesn't support multiple interfaces or configs;
  1096. * - sa1100 doesn't have a third interrupt endpoint
  1097. */
  1098. if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
  1099. use_acm = 0;
  1100. gcnum = usb_gadget_controller_number(gadget);
  1101. if (gcnum >= 0)
  1102. gs_device_desc.bcdDevice =
  1103. cpu_to_le16(GS_VERSION_NUM | gcnum);
  1104. else {
  1105. pr_warning("gs_bind: controller '%s' not recognized\n",
  1106. gadget->name);
  1107. /* unrecognized, but safe unless bulk is REALLY quirky */
  1108. gs_device_desc.bcdDevice =
  1109. __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
  1110. }
  1111. usb_ep_autoconfig_reset(gadget);
  1112. ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
  1113. if (!ep)
  1114. goto autoconf_fail;
  1115. EP_IN_NAME = ep->name;
  1116. ep->driver_data = ep; /* claim the endpoint */
  1117. ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
  1118. if (!ep)
  1119. goto autoconf_fail;
  1120. EP_OUT_NAME = ep->name;
  1121. ep->driver_data = ep; /* claim the endpoint */
  1122. if (use_acm) {
  1123. ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
  1124. if (!ep) {
  1125. pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
  1126. goto autoconf_fail;
  1127. }
  1128. gs_device_desc.idProduct = __constant_cpu_to_le16(
  1129. GS_CDC_PRODUCT_ID),
  1130. EP_NOTIFY_NAME = ep->name;
  1131. ep->driver_data = ep; /* claim the endpoint */
  1132. }
  1133. gs_device_desc.bDeviceClass = use_acm
  1134. ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
  1135. gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1136. if (gadget_is_dualspeed(gadget)) {
  1137. gs_qualifier_desc.bDeviceClass = use_acm
  1138. ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
  1139. /* assume ep0 uses the same packet size for both speeds */
  1140. gs_qualifier_desc.bMaxPacketSize0 =
  1141. gs_device_desc.bMaxPacketSize0;
  1142. /* assume endpoints are dual-speed */
  1143. gs_highspeed_notify_desc.bEndpointAddress =
  1144. gs_fullspeed_notify_desc.bEndpointAddress;
  1145. gs_highspeed_in_desc.bEndpointAddress =
  1146. gs_fullspeed_in_desc.bEndpointAddress;
  1147. gs_highspeed_out_desc.bEndpointAddress =
  1148. gs_fullspeed_out_desc.bEndpointAddress;
  1149. }
  1150. usb_gadget_set_selfpowered(gadget);
  1151. if (gadget_is_otg(gadget)) {
  1152. gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
  1153. gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1154. gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1155. }
  1156. gs_device = dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
  1157. if (dev == NULL)
  1158. return -ENOMEM;
  1159. snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
  1160. init_utsname()->sysname, init_utsname()->release,
  1161. gadget->name);
  1162. dev->dev_gadget = gadget;
  1163. spin_lock_init(&dev->dev_lock);
  1164. INIT_LIST_HEAD(&dev->dev_req_list);
  1165. set_gadget_data(gadget, dev);
  1166. if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
  1167. pr_err("gs_bind: cannot allocate ports\n");
  1168. gs_unbind(gadget);
  1169. return ret;
  1170. }
  1171. /* preallocate control response and buffer */
  1172. dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
  1173. GFP_KERNEL);
  1174. if (dev->dev_ctrl_req == NULL) {
  1175. gs_unbind(gadget);
  1176. return -ENOMEM;
  1177. }
  1178. dev->dev_ctrl_req->complete = gs_setup_complete;
  1179. gadget->ep0->driver_data = dev;
  1180. pr_info("gs_bind: %s %s bound\n",
  1181. GS_LONG_NAME, GS_VERSION_STR);
  1182. return 0;
  1183. autoconf_fail:
  1184. pr_err("gs_bind: cannot autoconfigure on %s\n", gadget->name);
  1185. return -ENODEV;
  1186. }
  1187. /*
  1188. * gs_unbind
  1189. *
  1190. * Called on module unload. Frees the control request and device
  1191. * structure.
  1192. */
  1193. static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
  1194. {
  1195. struct gs_dev *dev = get_gadget_data(gadget);
  1196. gs_device = NULL;
  1197. /* read/write requests already freed, only control request remains */
  1198. if (dev != NULL) {
  1199. if (dev->dev_ctrl_req != NULL) {
  1200. gs_free_req(gadget->ep0, dev->dev_ctrl_req);
  1201. dev->dev_ctrl_req = NULL;
  1202. }
  1203. gs_free_ports(dev);
  1204. if (dev->dev_notify_ep)
  1205. usb_ep_disable(dev->dev_notify_ep);
  1206. if (dev->dev_in_ep)
  1207. usb_ep_disable(dev->dev_in_ep);
  1208. if (dev->dev_out_ep)
  1209. usb_ep_disable(dev->dev_out_ep);
  1210. kfree(dev);
  1211. set_gadget_data(gadget, NULL);
  1212. }
  1213. pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
  1214. GS_VERSION_STR);
  1215. }
  1216. /*
  1217. * gs_setup
  1218. *
  1219. * Implements all the control endpoint functionality that's not
  1220. * handled in hardware or the hardware driver.
  1221. *
  1222. * Returns the size of the data sent to the host, or a negative
  1223. * error number.
  1224. */
  1225. static int gs_setup(struct usb_gadget *gadget,
  1226. const struct usb_ctrlrequest *ctrl)
  1227. {
  1228. int ret = -EOPNOTSUPP;
  1229. struct gs_dev *dev = get_gadget_data(gadget);
  1230. struct usb_request *req = dev->dev_ctrl_req;
  1231. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  1232. u16 wValue = le16_to_cpu(ctrl->wValue);
  1233. u16 wLength = le16_to_cpu(ctrl->wLength);
  1234. switch (ctrl->bRequestType & USB_TYPE_MASK) {
  1235. case USB_TYPE_STANDARD:
  1236. ret = gs_setup_standard(gadget,ctrl);
  1237. break;
  1238. case USB_TYPE_CLASS:
  1239. ret = gs_setup_class(gadget,ctrl);
  1240. break;
  1241. default:
  1242. pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
  1243. "value=%04x, index=%04x, length=%d\n",
  1244. ctrl->bRequestType, ctrl->bRequest,
  1245. wValue, wIndex, wLength);
  1246. break;
  1247. }
  1248. /* respond with data transfer before status phase? */
  1249. if (ret >= 0) {
  1250. req->length = ret;
  1251. req->zero = ret < wLength
  1252. && (ret % gadget->ep0->maxpacket) == 0;
  1253. ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1254. if (ret < 0) {
  1255. pr_err("gs_setup: cannot queue response, ret=%d\n",
  1256. ret);
  1257. req->status = 0;
  1258. gs_setup_complete(gadget->ep0, req);
  1259. }
  1260. }
  1261. /* device either stalls (ret < 0) or reports success */
  1262. return ret;
  1263. }
  1264. static int gs_setup_standard(struct usb_gadget *gadget,
  1265. const struct usb_ctrlrequest *ctrl)
  1266. {
  1267. int ret = -EOPNOTSUPP;
  1268. struct gs_dev *dev = get_gadget_data(gadget);
  1269. struct usb_request *req = dev->dev_ctrl_req;
  1270. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  1271. u16 wValue = le16_to_cpu(ctrl->wValue);
  1272. u16 wLength = le16_to_cpu(ctrl->wLength);
  1273. switch (ctrl->bRequest) {
  1274. case USB_REQ_GET_DESCRIPTOR:
  1275. if (ctrl->bRequestType != USB_DIR_IN)
  1276. break;
  1277. switch (wValue >> 8) {
  1278. case USB_DT_DEVICE:
  1279. ret = min(wLength,
  1280. (u16)sizeof(struct usb_device_descriptor));
  1281. memcpy(req->buf, &gs_device_desc, ret);
  1282. break;
  1283. case USB_DT_DEVICE_QUALIFIER:
  1284. if (!gadget_is_dualspeed(gadget))
  1285. break;
  1286. ret = min(wLength,
  1287. (u16)sizeof(struct usb_qualifier_descriptor));
  1288. memcpy(req->buf, &gs_qualifier_desc, ret);
  1289. break;
  1290. case USB_DT_OTHER_SPEED_CONFIG:
  1291. if (!gadget_is_dualspeed(gadget))
  1292. break;
  1293. /* fall through */
  1294. case USB_DT_CONFIG:
  1295. ret = gs_build_config_buf(req->buf, gadget,
  1296. wValue >> 8, wValue & 0xff,
  1297. gadget_is_otg(gadget));
  1298. if (ret >= 0)
  1299. ret = min(wLength, (u16)ret);
  1300. break;
  1301. case USB_DT_STRING:
  1302. /* wIndex == language code. */
  1303. ret = usb_gadget_get_string(&gs_string_table,
  1304. wValue & 0xff, req->buf);
  1305. if (ret >= 0)
  1306. ret = min(wLength, (u16)ret);
  1307. break;
  1308. }
  1309. break;
  1310. case USB_REQ_SET_CONFIGURATION:
  1311. if (ctrl->bRequestType != 0)
  1312. break;
  1313. spin_lock(&dev->dev_lock);
  1314. ret = gs_set_config(dev, wValue);
  1315. spin_unlock(&dev->dev_lock);
  1316. break;
  1317. case USB_REQ_GET_CONFIGURATION:
  1318. if (ctrl->bRequestType != USB_DIR_IN)
  1319. break;
  1320. *(u8 *)req->buf = dev->dev_config;
  1321. ret = min(wLength, (u16)1);
  1322. break;
  1323. case USB_REQ_SET_INTERFACE:
  1324. if (ctrl->bRequestType != USB_RECIP_INTERFACE
  1325. || !dev->dev_config
  1326. || wIndex >= GS_MAX_NUM_INTERFACES)
  1327. break;
  1328. if (dev->dev_config == GS_BULK_CONFIG_ID
  1329. && wIndex != GS_BULK_INTERFACE_ID)
  1330. break;
  1331. /* no alternate interface settings */
  1332. if (wValue != 0)
  1333. break;
  1334. spin_lock(&dev->dev_lock);
  1335. /* PXA hardware partially handles SET_INTERFACE;
  1336. * we need to kluge around that interference. */
  1337. if (gadget_is_pxa(gadget)) {
  1338. ret = gs_set_config(dev, use_acm ?
  1339. GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
  1340. goto set_interface_done;
  1341. }
  1342. if (dev->dev_config != GS_BULK_CONFIG_ID
  1343. && wIndex == GS_CONTROL_INTERFACE_ID) {
  1344. if (dev->dev_notify_ep) {
  1345. usb_ep_disable(dev->dev_notify_ep);
  1346. usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
  1347. }
  1348. } else {
  1349. usb_ep_disable(dev->dev_in_ep);
  1350. usb_ep_disable(dev->dev_out_ep);
  1351. usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
  1352. usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
  1353. }
  1354. ret = 0;
  1355. set_interface_done:
  1356. spin_unlock(&dev->dev_lock);
  1357. break;
  1358. case USB_REQ_GET_INTERFACE:
  1359. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
  1360. || dev->dev_config == GS_NO_CONFIG_ID)
  1361. break;
  1362. if (wIndex >= GS_MAX_NUM_INTERFACES
  1363. || (dev->dev_config == GS_BULK_CONFIG_ID
  1364. && wIndex != GS_BULK_INTERFACE_ID)) {
  1365. ret = -EDOM;
  1366. break;
  1367. }
  1368. /* no alternate interface settings */
  1369. *(u8 *)req->buf = 0;
  1370. ret = min(wLength, (u16)1);
  1371. break;
  1372. default:
  1373. pr_err("gs_setup: unknown standard request, type=%02x, "
  1374. "request=%02x, value=%04x, index=%04x, length=%d\n",
  1375. ctrl->bRequestType, ctrl->bRequest,
  1376. wValue, wIndex, wLength);
  1377. break;
  1378. }
  1379. return ret;
  1380. }
  1381. static int gs_setup_class(struct usb_gadget *gadget,
  1382. const struct usb_ctrlrequest *ctrl)
  1383. {
  1384. int ret = -EOPNOTSUPP;
  1385. struct gs_dev *dev = get_gadget_data(gadget);
  1386. struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
  1387. struct usb_request *req = dev->dev_ctrl_req;
  1388. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  1389. u16 wValue = le16_to_cpu(ctrl->wValue);
  1390. u16 wLength = le16_to_cpu(ctrl->wLength);
  1391. switch (ctrl->bRequest) {
  1392. case USB_CDC_REQ_SET_LINE_CODING:
  1393. /* FIXME Submit req to read the data; have its completion
  1394. * handler copy that data to port->port_line_coding (iff
  1395. * it's valid) and maybe pass it on. Until then, fail.
  1396. */
  1397. pr_warning("gs_setup: set_line_coding "
  1398. "unuspported\n");
  1399. break;
  1400. case USB_CDC_REQ_GET_LINE_CODING:
  1401. port = dev->dev_port[0]; /* ACM only has one port */
  1402. ret = min(wLength,
  1403. (u16)sizeof(struct usb_cdc_line_coding));
  1404. if (port) {
  1405. spin_lock(&port->port_lock);
  1406. memcpy(req->buf, &port->port_line_coding, ret);
  1407. spin_unlock(&port->port_lock);
  1408. }
  1409. break;
  1410. case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  1411. /* FIXME Submit req to read the data; have its completion
  1412. * handler use that to set the state (iff it's valid) and
  1413. * maybe pass it on. Until then, fail.
  1414. */
  1415. pr_warning("gs_setup: set_control_line_state "
  1416. "unuspported\n");
  1417. break;
  1418. default:
  1419. pr_err("gs_setup: unknown class request, "
  1420. "type=%02x, request=%02x, value=%04x, "
  1421. "index=%04x, length=%d\n",
  1422. ctrl->bRequestType, ctrl->bRequest,
  1423. wValue, wIndex, wLength);
  1424. break;
  1425. }
  1426. return ret;
  1427. }
  1428. /*
  1429. * gs_setup_complete
  1430. */
  1431. static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
  1432. {
  1433. if (req->status || req->actual != req->length) {
  1434. pr_err("gs_setup_complete: status error, status=%d, "
  1435. "actual=%d, length=%d\n",
  1436. req->status, req->actual, req->length);
  1437. }
  1438. }
  1439. /*
  1440. * gs_disconnect
  1441. *
  1442. * Called when the device is disconnected. Frees the closed
  1443. * ports and disconnects open ports. Open ports will be freed
  1444. * on close. Then reallocates the ports for the next connection.
  1445. */
  1446. static void gs_disconnect(struct usb_gadget *gadget)
  1447. {
  1448. unsigned long flags;
  1449. struct gs_dev *dev = get_gadget_data(gadget);
  1450. spin_lock_irqsave(&dev->dev_lock, flags);
  1451. gs_reset_config(dev);
  1452. /* free closed ports and disconnect open ports */
  1453. /* (open ports will be freed when closed) */
  1454. gs_free_ports(dev);
  1455. /* re-allocate ports for the next connection */
  1456. if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
  1457. pr_err("gs_disconnect: cannot re-allocate ports\n");
  1458. spin_unlock_irqrestore(&dev->dev_lock, flags);
  1459. pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
  1460. }
  1461. /*
  1462. * gs_set_config
  1463. *
  1464. * Configures the device by enabling device specific
  1465. * optimizations, setting up the endpoints, allocating
  1466. * read and write requests and queuing read requests.
  1467. *
  1468. * The device lock must be held when calling this function.
  1469. */
  1470. static int gs_set_config(struct gs_dev *dev, unsigned config)
  1471. {
  1472. int i;
  1473. int ret = 0;
  1474. struct usb_gadget *gadget = dev->dev_gadget;
  1475. struct usb_ep *ep;
  1476. struct usb_endpoint_descriptor *ep_desc;
  1477. struct usb_request *req;
  1478. struct gs_req_entry *req_entry;
  1479. if (dev == NULL) {
  1480. pr_err("gs_set_config: NULL device pointer\n");
  1481. return 0;
  1482. }
  1483. if (config == dev->dev_config)
  1484. return 0;
  1485. gs_reset_config(dev);
  1486. switch (config) {
  1487. case GS_NO_CONFIG_ID:
  1488. return 0;
  1489. case GS_BULK_CONFIG_ID:
  1490. if (use_acm)
  1491. return -EINVAL;
  1492. /* device specific optimizations */
  1493. if (gadget_is_net2280(gadget))
  1494. net2280_set_fifo_mode(gadget, 1);
  1495. break;
  1496. case GS_ACM_CONFIG_ID:
  1497. if (!use_acm)
  1498. return -EINVAL;
  1499. /* device specific optimizations */
  1500. if (gadget_is_net2280(gadget))
  1501. net2280_set_fifo_mode(gadget, 1);
  1502. break;
  1503. default:
  1504. return -EINVAL;
  1505. }
  1506. dev->dev_config = config;
  1507. gadget_for_each_ep(ep, gadget) {
  1508. if (EP_NOTIFY_NAME
  1509. && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
  1510. ep_desc = choose_ep_desc(gadget,
  1511. &gs_highspeed_notify_desc,
  1512. &gs_fullspeed_notify_desc);
  1513. ret = usb_ep_enable(ep,ep_desc);
  1514. if (ret == 0) {
  1515. ep->driver_data = dev;
  1516. dev->dev_notify_ep = ep;
  1517. dev->dev_notify_ep_desc = ep_desc;
  1518. } else {
  1519. pr_err("gs_set_config: cannot enable NOTIFY "
  1520. "endpoint %s, ret=%d\n",
  1521. ep->name, ret);
  1522. goto exit_reset_config;
  1523. }
  1524. }
  1525. else if (strcmp(ep->name, EP_IN_NAME) == 0) {
  1526. ep_desc = choose_ep_desc(gadget,
  1527. &gs_highspeed_in_desc,
  1528. &gs_fullspeed_in_desc);
  1529. ret = usb_ep_enable(ep,ep_desc);
  1530. if (ret == 0) {
  1531. ep->driver_data = dev;
  1532. dev->dev_in_ep = ep;
  1533. dev->dev_in_ep_desc = ep_desc;
  1534. } else {
  1535. pr_err("gs_set_config: cannot enable IN "
  1536. "endpoint %s, ret=%d\n",
  1537. ep->name, ret);
  1538. goto exit_reset_config;
  1539. }
  1540. }
  1541. else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
  1542. ep_desc = choose_ep_desc(gadget,
  1543. &gs_highspeed_out_desc,
  1544. &gs_fullspeed_out_desc);
  1545. ret = usb_ep_enable(ep,ep_desc);
  1546. if (ret == 0) {
  1547. ep->driver_data = dev;
  1548. dev->dev_out_ep = ep;
  1549. dev->dev_out_ep_desc = ep_desc;
  1550. } else {
  1551. pr_err("gs_set_config: cannot enable OUT "
  1552. "endpoint %s, ret=%d\n",
  1553. ep->name, ret);
  1554. goto exit_reset_config;
  1555. }
  1556. }
  1557. }
  1558. if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
  1559. || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
  1560. pr_err("gs_set_config: cannot find endpoints\n");
  1561. ret = -ENODEV;
  1562. goto exit_reset_config;
  1563. }
  1564. /* allocate and queue read requests */
  1565. ep = dev->dev_out_ep;
  1566. for (i=0; i<read_q_size && ret == 0; i++) {
  1567. if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
  1568. req->complete = gs_read_complete;
  1569. if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
  1570. pr_err("gs_set_config: cannot queue read "
  1571. "request, ret=%d\n", ret);
  1572. }
  1573. } else {
  1574. pr_err("gs_set_config: cannot allocate "
  1575. "read requests\n");
  1576. ret = -ENOMEM;
  1577. goto exit_reset_config;
  1578. }
  1579. }
  1580. /* allocate write requests, and put on free list */
  1581. ep = dev->dev_in_ep;
  1582. for (i=0; i<write_q_size; i++) {
  1583. if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
  1584. req_entry->re_req->complete = gs_write_complete;
  1585. list_add(&req_entry->re_entry, &dev->dev_req_list);
  1586. } else {
  1587. pr_err("gs_set_config: cannot allocate "
  1588. "write requests\n");
  1589. ret = -ENOMEM;
  1590. goto exit_reset_config;
  1591. }
  1592. }
  1593. pr_info("gs_set_config: %s configured, %s speed %s config\n",
  1594. GS_LONG_NAME,
  1595. gadget->speed == USB_SPEED_HIGH ? "high" : "full",
  1596. config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
  1597. return 0;
  1598. exit_reset_config:
  1599. gs_reset_config(dev);
  1600. return ret;
  1601. }
  1602. /*
  1603. * gs_reset_config
  1604. *
  1605. * Mark the device as not configured, disable all endpoints,
  1606. * which forces completion of pending I/O and frees queued
  1607. * requests, and free the remaining write requests on the
  1608. * free list.
  1609. *
  1610. * The device lock must be held when calling this function.
  1611. */
  1612. static void gs_reset_config(struct gs_dev *dev)
  1613. {
  1614. struct gs_req_entry *req_entry;
  1615. if (dev == NULL) {
  1616. pr_err("gs_reset_config: NULL device pointer\n");
  1617. return;
  1618. }
  1619. if (dev->dev_config == GS_NO_CONFIG_ID)
  1620. return;
  1621. dev->dev_config = GS_NO_CONFIG_ID;
  1622. /* free write requests on the free list */
  1623. while(!list_empty(&dev->dev_req_list)) {
  1624. req_entry = list_entry(dev->dev_req_list.next,
  1625. struct gs_req_entry, re_entry);
  1626. list_del(&req_entry->re_entry);
  1627. gs_free_req_entry(dev->dev_in_ep, req_entry);
  1628. }
  1629. /* disable endpoints, forcing completion of pending i/o; */
  1630. /* completion handlers free their requests in this case */
  1631. if (dev->dev_notify_ep) {
  1632. usb_ep_disable(dev->dev_notify_ep);
  1633. dev->dev_notify_ep = NULL;
  1634. }
  1635. if (dev->dev_in_ep) {
  1636. usb_ep_disable(dev->dev_in_ep);
  1637. dev->dev_in_ep = NULL;
  1638. }
  1639. if (dev->dev_out_ep) {
  1640. usb_ep_disable(dev->dev_out_ep);
  1641. dev->dev_out_ep = NULL;
  1642. }
  1643. }
  1644. /*
  1645. * gs_build_config_buf
  1646. *
  1647. * Builds the config descriptors in the given buffer and returns the
  1648. * length, or a negative error number.
  1649. */
  1650. static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
  1651. u8 type, unsigned int index, int is_otg)
  1652. {
  1653. int len;
  1654. int high_speed = 0;
  1655. const struct usb_config_descriptor *config_desc;
  1656. const struct usb_descriptor_header **function;
  1657. if (index >= gs_device_desc.bNumConfigurations)
  1658. return -EINVAL;
  1659. /* other speed switches high and full speed */
  1660. if (gadget_is_dualspeed(g)) {
  1661. high_speed = (g->speed == USB_SPEED_HIGH);
  1662. if (type == USB_DT_OTHER_SPEED_CONFIG)
  1663. high_speed = !high_speed;
  1664. }
  1665. if (use_acm) {
  1666. config_desc = &gs_acm_config_desc;
  1667. function = high_speed
  1668. ? gs_acm_highspeed_function
  1669. : gs_acm_fullspeed_function;
  1670. } else {
  1671. config_desc = &gs_bulk_config_desc;
  1672. function = high_speed
  1673. ? gs_bulk_highspeed_function
  1674. : gs_bulk_fullspeed_function;
  1675. }
  1676. /* for now, don't advertise srp-only devices */
  1677. if (!is_otg)
  1678. function++;
  1679. len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
  1680. if (len < 0)
  1681. return len;
  1682. ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
  1683. return len;
  1684. }
  1685. /*
  1686. * gs_alloc_req
  1687. *
  1688. * Allocate a usb_request and its buffer. Returns a pointer to the
  1689. * usb_request or NULL if there is an error.
  1690. */
  1691. static struct usb_request *
  1692. gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
  1693. {
  1694. struct usb_request *req;
  1695. if (ep == NULL)
  1696. return NULL;
  1697. req = usb_ep_alloc_request(ep, kmalloc_flags);
  1698. if (req != NULL) {
  1699. req->length = len;
  1700. req->buf = kmalloc(len, kmalloc_flags);
  1701. if (req->buf == NULL) {
  1702. usb_ep_free_request(ep, req);
  1703. return NULL;
  1704. }
  1705. }
  1706. return req;
  1707. }
  1708. /*
  1709. * gs_free_req
  1710. *
  1711. * Free a usb_request and its buffer.
  1712. */
  1713. static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
  1714. {
  1715. if (ep != NULL && req != NULL) {
  1716. kfree(req->buf);
  1717. usb_ep_free_request(ep, req);
  1718. }
  1719. }
  1720. /*
  1721. * gs_alloc_req_entry
  1722. *
  1723. * Allocates a request and its buffer, using the given
  1724. * endpoint, buffer len, and kmalloc flags.
  1725. */
  1726. static struct gs_req_entry *
  1727. gs_alloc_req_entry(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
  1728. {
  1729. struct gs_req_entry *req;
  1730. req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
  1731. if (req == NULL)
  1732. return NULL;
  1733. req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
  1734. if (req->re_req == NULL) {
  1735. kfree(req);
  1736. return NULL;
  1737. }
  1738. req->re_req->context = req;
  1739. return req;
  1740. }
  1741. /*
  1742. * gs_free_req_entry
  1743. *
  1744. * Frees a request and its buffer.
  1745. */
  1746. static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
  1747. {
  1748. if (ep != NULL && req != NULL) {
  1749. if (req->re_req != NULL)
  1750. gs_free_req(ep, req->re_req);
  1751. kfree(req);
  1752. }
  1753. }
  1754. /*
  1755. * gs_alloc_ports
  1756. *
  1757. * Allocate all ports and set the gs_dev struct to point to them.
  1758. * Return 0 if successful, or a negative error number.
  1759. *
  1760. * The device lock is normally held when calling this function.
  1761. */
  1762. static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
  1763. {
  1764. int i;
  1765. struct gs_port *port;
  1766. if (dev == NULL)
  1767. return -EIO;
  1768. for (i=0; i<GS_NUM_PORTS; i++) {
  1769. if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
  1770. return -ENOMEM;
  1771. port->port_dev = dev;
  1772. port->port_num = i;
  1773. port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
  1774. port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
  1775. port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
  1776. port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
  1777. spin_lock_init(&port->port_lock);
  1778. init_waitqueue_head(&port->port_write_wait);
  1779. dev->dev_port[i] = port;
  1780. }
  1781. return 0;
  1782. }
  1783. /*
  1784. * gs_free_ports
  1785. *
  1786. * Free all closed ports. Open ports are disconnected by
  1787. * freeing their write buffers, setting their device pointers
  1788. * and the pointers to them in the device to NULL. These
  1789. * ports will be freed when closed.
  1790. *
  1791. * The device lock is normally held when calling this function.
  1792. */
  1793. static void gs_free_ports(struct gs_dev *dev)
  1794. {
  1795. int i;
  1796. unsigned long flags;
  1797. struct gs_port *port;
  1798. if (dev == NULL)
  1799. return;
  1800. for (i=0; i<GS_NUM_PORTS; i++) {
  1801. if ((port=dev->dev_port[i]) != NULL) {
  1802. dev->dev_port[i] = NULL;
  1803. spin_lock_irqsave(&port->port_lock, flags);
  1804. if (port->port_write_buf != NULL) {
  1805. gs_buf_free(port->port_write_buf);
  1806. port->port_write_buf = NULL;
  1807. }
  1808. if (port->port_open_count > 0 || port->port_in_use) {
  1809. port->port_dev = NULL;
  1810. wake_up_interruptible(&port->port_write_wait);
  1811. if (port->port_tty) {
  1812. tty_hangup(port->port_tty);
  1813. }
  1814. spin_unlock_irqrestore(&port->port_lock, flags);
  1815. } else {
  1816. spin_unlock_irqrestore(&port->port_lock, flags);
  1817. kfree(port);
  1818. }
  1819. }
  1820. }
  1821. }
  1822. /* Circular Buffer */
  1823. /*
  1824. * gs_buf_alloc
  1825. *
  1826. * Allocate a circular buffer and all associated memory.
  1827. */
  1828. static struct gs_buf *gs_buf_alloc(unsigned int size, gfp_t kmalloc_flags)
  1829. {
  1830. struct gs_buf *gb;
  1831. if (size == 0)
  1832. return NULL;
  1833. gb = kmalloc(sizeof(struct gs_buf), kmalloc_flags);
  1834. if (gb == NULL)
  1835. return NULL;
  1836. gb->buf_buf = kmalloc(size, kmalloc_flags);
  1837. if (gb->buf_buf == NULL) {
  1838. kfree(gb);
  1839. return NULL;
  1840. }
  1841. gb->buf_size = size;
  1842. gb->buf_get = gb->buf_put = gb->buf_buf;
  1843. return gb;
  1844. }
  1845. /*
  1846. * gs_buf_free
  1847. *
  1848. * Free the buffer and all associated memory.
  1849. */
  1850. static void gs_buf_free(struct gs_buf *gb)
  1851. {
  1852. if (gb) {
  1853. kfree(gb->buf_buf);
  1854. kfree(gb);
  1855. }
  1856. }
  1857. /*
  1858. * gs_buf_clear
  1859. *
  1860. * Clear out all data in the circular buffer.
  1861. */
  1862. static void gs_buf_clear(struct gs_buf *gb)
  1863. {
  1864. if (gb != NULL)
  1865. gb->buf_get = gb->buf_put;
  1866. /* equivalent to a get of all data available */
  1867. }
  1868. /*
  1869. * gs_buf_data_avail
  1870. *
  1871. * Return the number of bytes of data available in the circular
  1872. * buffer.
  1873. */
  1874. static unsigned int gs_buf_data_avail(struct gs_buf *gb)
  1875. {
  1876. if (gb != NULL)
  1877. return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
  1878. else
  1879. return 0;
  1880. }
  1881. /*
  1882. * gs_buf_space_avail
  1883. *
  1884. * Return the number of bytes of space available in the circular
  1885. * buffer.
  1886. */
  1887. static unsigned int gs_buf_space_avail(struct gs_buf *gb)
  1888. {
  1889. if (gb != NULL)
  1890. return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
  1891. else
  1892. return 0;
  1893. }
  1894. /*
  1895. * gs_buf_put
  1896. *
  1897. * Copy data data from a user buffer and put it into the circular buffer.
  1898. * Restrict to the amount of space available.
  1899. *
  1900. * Return the number of bytes copied.
  1901. */
  1902. static unsigned int
  1903. gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
  1904. {
  1905. unsigned int len;
  1906. if (gb == NULL)
  1907. return 0;
  1908. len = gs_buf_space_avail(gb);
  1909. if (count > len)
  1910. count = len;
  1911. if (count == 0)
  1912. return 0;
  1913. len = gb->buf_buf + gb->buf_size - gb->buf_put;
  1914. if (count > len) {
  1915. memcpy(gb->buf_put, buf, len);
  1916. memcpy(gb->buf_buf, buf+len, count - len);
  1917. gb->buf_put = gb->buf_buf + count - len;
  1918. } else {
  1919. memcpy(gb->buf_put, buf, count);
  1920. if (count < len)
  1921. gb->buf_put += count;
  1922. else /* count == len */
  1923. gb->buf_put = gb->buf_buf;
  1924. }
  1925. return count;
  1926. }
  1927. /*
  1928. * gs_buf_get
  1929. *
  1930. * Get data from the circular buffer and copy to the given buffer.
  1931. * Restrict to the amount of data available.
  1932. *
  1933. * Return the number of bytes copied.
  1934. */
  1935. static unsigned int
  1936. gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
  1937. {
  1938. unsigned int len;
  1939. if (gb == NULL)
  1940. return 0;
  1941. len = gs_buf_data_avail(gb);
  1942. if (count > len)
  1943. count = len;
  1944. if (count == 0)
  1945. return 0;
  1946. len = gb->buf_buf + gb->buf_size - gb->buf_get;
  1947. if (count > len) {
  1948. memcpy(buf, gb->buf_get, len);
  1949. memcpy(buf+len, gb->buf_buf, count - len);
  1950. gb->buf_get = gb->buf_buf + count - len;
  1951. } else {
  1952. memcpy(buf, gb->buf_get, count);
  1953. if (count < len)
  1954. gb->buf_get += count;
  1955. else /* count == len */
  1956. gb->buf_get = gb->buf_buf;
  1957. }
  1958. return count;
  1959. }