serial.c 58 KB

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