serial.c 58 KB

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