serial.c 58 KB

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