serial.c 60 KB

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