serial.c 60 KB

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