serial.c 58 KB

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