serial.c 59 KB

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