serial.c 58 KB

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