serial.c 56 KB

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