serial.c 58 KB

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