serial.c 58 KB

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