serial.c 56 KB

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