serial.c 61 KB

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