u_serial.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /*
  2. * u_serial.c - utilities for USB gadget "serial port"/TTY support
  3. *
  4. * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
  5. * Copyright (C) 2008 David Brownell
  6. * Copyright (C) 2008 by Nokia Corporation
  7. * Copyright (c) 2013, The Linux Foundation. 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. /* #define VERBOSE_DEBUG */
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/device.h>
  23. #include <linux/delay.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/slab.h>
  27. #include <linux/export.h>
  28. #include <linux/debugfs.h>
  29. #include "u_serial.h"
  30. /*
  31. * This component encapsulates the TTY layer glue needed to provide basic
  32. * "serial port" functionality through the USB gadget stack. Each such
  33. * port is exposed through a /dev/ttyGS* node.
  34. *
  35. * After initialization (gserial_setup), these TTY port devices stay
  36. * available until they are removed (gserial_cleanup). Each one may be
  37. * connected to a USB function (gserial_connect), or disconnected (with
  38. * gserial_disconnect) when the USB host issues a config change event.
  39. * Data can only flow when the port is connected to the host.
  40. *
  41. * A given TTY port can be made available in multiple configurations.
  42. * For example, each one might expose a ttyGS0 node which provides a
  43. * login application. In one case that might use CDC ACM interface 0,
  44. * while another configuration might use interface 3 for that. The
  45. * work to handle that (including descriptor management) is not part
  46. * of this component.
  47. *
  48. * Configurations may expose more than one TTY port. For example, if
  49. * ttyGS0 provides login service, then ttyGS1 might provide dialer access
  50. * for a telephone or fax link. And ttyGS2 might be something that just
  51. * needs a simple byte stream interface for some messaging protocol that
  52. * is managed in userspace ... OBEX, PTP, and MTP have been mentioned.
  53. */
  54. #define PREFIX "ttyGS"
  55. /*
  56. * gserial is the lifecycle interface, used by USB functions
  57. * gs_port is the I/O nexus, used by the tty driver
  58. * tty_struct links to the tty/filesystem framework
  59. *
  60. * gserial <---> gs_port ... links will be null when the USB link is
  61. * inactive; managed by gserial_{connect,disconnect}(). each gserial
  62. * instance can wrap its own USB control protocol.
  63. * gserial->ioport == usb_ep->driver_data ... gs_port
  64. * gs_port->port_usb ... gserial
  65. *
  66. * gs_port <---> tty_struct ... links will be null when the TTY file
  67. * isn't opened; managed by gs_open()/gs_close()
  68. * gserial->port_tty ... tty_struct
  69. * tty_struct->driver_data ... gserial
  70. */
  71. /* RX and TX queues can buffer QUEUE_SIZE packets before they hit the
  72. * next layer of buffering. For TX that's a circular buffer; for RX
  73. * consider it a NOP. A third layer is provided by the TTY code.
  74. */
  75. #define TX_QUEUE_SIZE 8
  76. #define TX_BUF_SIZE 4096
  77. #define WRITE_BUF_SIZE 8192 /* TX only */
  78. #define RX_QUEUE_SIZE 8
  79. #define RX_BUF_SIZE 4096
  80. /* circular buffer */
  81. struct gs_buf {
  82. unsigned buf_size;
  83. char *buf_buf;
  84. char *buf_get;
  85. char *buf_put;
  86. };
  87. /*
  88. * The port structure holds info for each port, one for each minor number
  89. * (and thus for each /dev/ node).
  90. */
  91. struct gs_port {
  92. spinlock_t port_lock; /* guard port_* access */
  93. struct gserial *port_usb;
  94. struct tty_struct *port_tty;
  95. unsigned open_count;
  96. bool openclose; /* open/close in progress */
  97. u8 port_num;
  98. wait_queue_head_t close_wait; /* wait for last close */
  99. struct list_head read_pool;
  100. int read_started;
  101. int read_allocated;
  102. struct list_head read_queue;
  103. unsigned n_read;
  104. struct work_struct push;
  105. struct list_head write_pool;
  106. int write_started;
  107. int write_allocated;
  108. struct gs_buf port_write_buf;
  109. wait_queue_head_t drain_wait; /* wait while writes drain */
  110. bool write_busy;
  111. /* REVISIT this state ... */
  112. struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
  113. unsigned long nbytes_from_host;
  114. unsigned long nbytes_to_tty;
  115. unsigned long nbytes_from_tty;
  116. unsigned long nbytes_to_host;
  117. };
  118. /* increase N_PORTS if you need more */
  119. #define N_PORTS 8
  120. static struct portmaster {
  121. struct mutex lock; /* protect open/close */
  122. struct gs_port *port;
  123. } ports[N_PORTS];
  124. static unsigned n_ports;
  125. static struct workqueue_struct *gserial_wq;
  126. #define GS_CLOSE_TIMEOUT 15 /* seconds */
  127. #ifdef VERBOSE_DEBUG
  128. #ifndef pr_vdebug
  129. #define pr_vdebug(fmt, arg...) \
  130. pr_debug(fmt, ##arg)
  131. #endif /* pr_vdebug */
  132. #else
  133. #ifndef pr_vdebug
  134. #define pr_vdebug(fmt, arg...) \
  135. ({ if (0) pr_debug(fmt, ##arg); })
  136. #endif /* pr_vdebug */
  137. #endif
  138. /*-------------------------------------------------------------------------*/
  139. /* Circular Buffer */
  140. /*
  141. * gs_buf_alloc
  142. *
  143. * Allocate a circular buffer and all associated memory.
  144. */
  145. static int gs_buf_alloc(struct gs_buf *gb, unsigned size)
  146. {
  147. gb->buf_buf = kmalloc(size, GFP_KERNEL);
  148. if (gb->buf_buf == NULL)
  149. return -ENOMEM;
  150. gb->buf_size = size;
  151. gb->buf_put = gb->buf_buf;
  152. gb->buf_get = gb->buf_buf;
  153. return 0;
  154. }
  155. /*
  156. * gs_buf_free
  157. *
  158. * Free the buffer and all associated memory.
  159. */
  160. static void gs_buf_free(struct gs_buf *gb)
  161. {
  162. kfree(gb->buf_buf);
  163. gb->buf_buf = NULL;
  164. }
  165. /*
  166. * gs_buf_clear
  167. *
  168. * Clear out all data in the circular buffer.
  169. */
  170. static void gs_buf_clear(struct gs_buf *gb)
  171. {
  172. gb->buf_get = gb->buf_put;
  173. /* equivalent to a get of all data available */
  174. }
  175. /*
  176. * gs_buf_data_avail
  177. *
  178. * Return the number of bytes of data written into the circular
  179. * buffer.
  180. */
  181. static unsigned gs_buf_data_avail(struct gs_buf *gb)
  182. {
  183. return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
  184. }
  185. /*
  186. * gs_buf_space_avail
  187. *
  188. * Return the number of bytes of space available in the circular
  189. * buffer.
  190. */
  191. static unsigned gs_buf_space_avail(struct gs_buf *gb)
  192. {
  193. return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
  194. }
  195. /*
  196. * gs_buf_put
  197. *
  198. * Copy data data from a user buffer and put it into the circular buffer.
  199. * Restrict to the amount of space available.
  200. *
  201. * Return the number of bytes copied.
  202. */
  203. static unsigned
  204. gs_buf_put(struct gs_buf *gb, const char *buf, unsigned count)
  205. {
  206. unsigned len;
  207. len = gs_buf_space_avail(gb);
  208. if (count > len)
  209. count = len;
  210. if (count == 0)
  211. return 0;
  212. len = gb->buf_buf + gb->buf_size - gb->buf_put;
  213. if (count > len) {
  214. memcpy(gb->buf_put, buf, len);
  215. memcpy(gb->buf_buf, buf+len, count - len);
  216. gb->buf_put = gb->buf_buf + count - len;
  217. } else {
  218. memcpy(gb->buf_put, buf, count);
  219. if (count < len)
  220. gb->buf_put += count;
  221. else /* count == len */
  222. gb->buf_put = gb->buf_buf;
  223. }
  224. return count;
  225. }
  226. /*
  227. * gs_buf_get
  228. *
  229. * Get data from the circular buffer and copy to the given buffer.
  230. * Restrict to the amount of data available.
  231. *
  232. * Return the number of bytes copied.
  233. */
  234. static unsigned
  235. gs_buf_get(struct gs_buf *gb, char *buf, unsigned count)
  236. {
  237. unsigned len;
  238. len = gs_buf_data_avail(gb);
  239. if (count > len)
  240. count = len;
  241. if (count == 0)
  242. return 0;
  243. len = gb->buf_buf + gb->buf_size - gb->buf_get;
  244. if (count > len) {
  245. memcpy(buf, gb->buf_get, len);
  246. memcpy(buf+len, gb->buf_buf, count - len);
  247. gb->buf_get = gb->buf_buf + count - len;
  248. } else {
  249. memcpy(buf, gb->buf_get, count);
  250. if (count < len)
  251. gb->buf_get += count;
  252. else /* count == len */
  253. gb->buf_get = gb->buf_buf;
  254. }
  255. return count;
  256. }
  257. /*-------------------------------------------------------------------------*/
  258. /* I/O glue between TTY (upper) and USB function (lower) driver layers */
  259. /*
  260. * gs_alloc_req
  261. *
  262. * Allocate a usb_request and its buffer. Returns a pointer to the
  263. * usb_request or NULL if there is an error.
  264. */
  265. struct usb_request *
  266. gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
  267. {
  268. struct usb_request *req;
  269. req = usb_ep_alloc_request(ep, kmalloc_flags);
  270. if (req != NULL) {
  271. req->length = len;
  272. req->buf = kmalloc(len, kmalloc_flags);
  273. if (req->buf == NULL) {
  274. usb_ep_free_request(ep, req);
  275. return NULL;
  276. }
  277. }
  278. return req;
  279. }
  280. /*
  281. * gs_free_req
  282. *
  283. * Free a usb_request and its buffer.
  284. */
  285. void gs_free_req(struct usb_ep *ep, struct usb_request *req)
  286. {
  287. kfree(req->buf);
  288. usb_ep_free_request(ep, req);
  289. }
  290. /*
  291. * gs_send_packet
  292. *
  293. * If there is data to send, a packet is built in the given
  294. * buffer and the size is returned. If there is no data to
  295. * send, 0 is returned.
  296. *
  297. * Called with port_lock held.
  298. */
  299. static unsigned
  300. gs_send_packet(struct gs_port *port, char *packet, unsigned size)
  301. {
  302. unsigned len;
  303. len = gs_buf_data_avail(&port->port_write_buf);
  304. if (len < size)
  305. size = len;
  306. if (size != 0)
  307. size = gs_buf_get(&port->port_write_buf, packet, size);
  308. return size;
  309. }
  310. /*
  311. * gs_start_tx
  312. *
  313. * This function finds available write requests, calls
  314. * gs_send_packet to fill these packets with data, and
  315. * continues until either there are no more write requests
  316. * available or no more data to send. This function is
  317. * run whenever data arrives or write requests are available.
  318. *
  319. * Context: caller owns port_lock; port_usb is non-null.
  320. */
  321. static int gs_start_tx(struct gs_port *port)
  322. /*
  323. __releases(&port->port_lock)
  324. __acquires(&port->port_lock)
  325. */
  326. {
  327. struct list_head *pool = &port->write_pool;
  328. struct usb_ep *in = port->port_usb->in;
  329. int status = 0;
  330. static long prev_len;
  331. bool do_tty_wake = false;
  332. while (!port->write_busy && !list_empty(pool)) {
  333. struct usb_request *req;
  334. int len;
  335. if (port->write_started >= TX_QUEUE_SIZE)
  336. break;
  337. req = list_entry(pool->next, struct usb_request, list);
  338. len = gs_send_packet(port, req->buf, TX_BUF_SIZE);
  339. if (len == 0) {
  340. /* Queue zero length packet explicitly to make it
  341. * work with UDCs which don't support req->zero flag
  342. */
  343. if (prev_len && (prev_len % in->maxpacket == 0)) {
  344. req->length = 0;
  345. list_del(&req->list);
  346. spin_unlock(&port->port_lock);
  347. status = usb_ep_queue(in, req, GFP_ATOMIC);
  348. spin_lock(&port->port_lock);
  349. if (!port->port_usb) {
  350. gs_free_req(in, req);
  351. break;
  352. }
  353. if (status) {
  354. printk(KERN_ERR "%s: %s err %d\n",
  355. __func__, "queue", status);
  356. list_add(&req->list, pool);
  357. }
  358. prev_len = 0;
  359. }
  360. wake_up_interruptible(&port->drain_wait);
  361. break;
  362. }
  363. do_tty_wake = true;
  364. req->length = len;
  365. list_del(&req->list);
  366. pr_vdebug(PREFIX "%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n",
  367. port->port_num, len, *((u8 *)req->buf),
  368. *((u8 *)req->buf+1), *((u8 *)req->buf+2));
  369. /* Drop lock while we call out of driver; completions
  370. * could be issued while we do so. Disconnection may
  371. * happen too; maybe immediately before we queue this!
  372. *
  373. * NOTE that we may keep sending data for a while after
  374. * the TTY closed (dev->ioport->port_tty is NULL).
  375. */
  376. port->write_busy = true;
  377. spin_unlock(&port->port_lock);
  378. status = usb_ep_queue(in, req, GFP_ATOMIC);
  379. spin_lock(&port->port_lock);
  380. port->write_busy = false;
  381. /*
  382. * If port_usb is NULL, gserial disconnect is called
  383. * while the spinlock is dropped and all requests are
  384. * freed. Free the current request here.
  385. */
  386. if (!port->port_usb) {
  387. do_tty_wake = false;
  388. gs_free_req(in, req);
  389. break;
  390. }
  391. port->write_busy = false;
  392. if (status) {
  393. pr_debug("%s: %s %s err %d\n",
  394. __func__, "queue", in->name, status);
  395. list_add(&req->list, pool);
  396. break;
  397. }
  398. prev_len = req->length;
  399. port->nbytes_from_tty += req->length;
  400. port->write_started++;
  401. }
  402. if (do_tty_wake && port->port_tty)
  403. tty_wakeup(port->port_tty);
  404. return status;
  405. }
  406. /*
  407. * Context: caller owns port_lock, and port_usb is set
  408. */
  409. static unsigned gs_start_rx(struct gs_port *port)
  410. /*
  411. __releases(&port->port_lock)
  412. __acquires(&port->port_lock)
  413. */
  414. {
  415. struct list_head *pool = &port->read_pool;
  416. struct usb_ep *out = port->port_usb->out;
  417. unsigned started = 0;
  418. while (!list_empty(pool)) {
  419. struct usb_request *req;
  420. int status;
  421. struct tty_struct *tty;
  422. /* no more rx if closed */
  423. tty = port->port_tty;
  424. if (!tty)
  425. break;
  426. if (port->read_started >= RX_QUEUE_SIZE)
  427. break;
  428. req = list_entry(pool->next, struct usb_request, list);
  429. list_del(&req->list);
  430. req->length = RX_BUF_SIZE;
  431. /* drop lock while we call out; the controller driver
  432. * may need to call us back (e.g. for disconnect)
  433. */
  434. spin_unlock(&port->port_lock);
  435. status = usb_ep_queue(out, req, GFP_ATOMIC);
  436. spin_lock(&port->port_lock);
  437. /*
  438. * If port_usb is NULL, gserial disconnect is called
  439. * while the spinlock is dropped and all requests are
  440. * freed. Free the current request here.
  441. */
  442. if (!port->port_usb) {
  443. started = 0;
  444. gs_free_req(out, req);
  445. break;
  446. }
  447. if (status) {
  448. pr_debug("%s: %s %s err %d\n",
  449. __func__, "queue", out->name, status);
  450. list_add(&req->list, pool);
  451. break;
  452. }
  453. port->read_started++;
  454. }
  455. return port->read_started;
  456. }
  457. /*
  458. * RX tasklet takes data out of the RX queue and hands it up to the TTY
  459. * layer until it refuses to take any more data (or is throttled back).
  460. * Then it issues reads for any further data.
  461. *
  462. * If the RX queue becomes full enough that no usb_request is queued,
  463. * the OUT endpoint may begin NAKing as soon as its FIFO fills up.
  464. * So QUEUE_SIZE packets plus however many the FIFO holds (usually two)
  465. * can be buffered before the TTY layer's buffers (currently 64 KB).
  466. */
  467. static void gs_rx_push(struct work_struct *w)
  468. {
  469. struct gs_port *port = container_of(w, struct gs_port, push);
  470. struct tty_struct *tty;
  471. struct list_head *queue = &port->read_queue;
  472. bool disconnect = false;
  473. bool do_push = false;
  474. /* hand any queued data to the tty */
  475. spin_lock_irq(&port->port_lock);
  476. tty = port->port_tty;
  477. while (!list_empty(queue)) {
  478. struct usb_request *req;
  479. req = list_first_entry(queue, struct usb_request, list);
  480. /* discard data if tty was closed */
  481. if (!tty)
  482. goto recycle;
  483. /* leave data queued if tty was rx throttled */
  484. if (test_bit(TTY_THROTTLED, &tty->flags))
  485. break;
  486. switch (req->status) {
  487. case -ESHUTDOWN:
  488. disconnect = true;
  489. pr_vdebug(PREFIX "%d: shutdown\n", port->port_num);
  490. break;
  491. default:
  492. /* presumably a transient fault */
  493. pr_warning(PREFIX "%d: unexpected RX status %d\n",
  494. port->port_num, req->status);
  495. /* FALLTHROUGH */
  496. case 0:
  497. /* normal completion */
  498. break;
  499. }
  500. /* push data to (open) tty */
  501. if (req->actual) {
  502. char *packet = req->buf;
  503. unsigned size = req->actual;
  504. unsigned n;
  505. int count;
  506. /* we may have pushed part of this packet already... */
  507. n = port->n_read;
  508. if (n) {
  509. packet += n;
  510. size -= n;
  511. }
  512. count = tty_insert_flip_string(tty, packet, size);
  513. port->nbytes_to_tty += count;
  514. if (count)
  515. do_push = true;
  516. if (count != size) {
  517. /* stop pushing; TTY layer can't handle more */
  518. port->n_read += count;
  519. pr_vdebug(PREFIX "%d: rx block %d/%d\n",
  520. port->port_num,
  521. count, req->actual);
  522. break;
  523. }
  524. port->n_read = 0;
  525. }
  526. recycle:
  527. list_move(&req->list, &port->read_pool);
  528. port->read_started--;
  529. }
  530. /* Push from tty to ldisc; without low_latency set this is handled by
  531. * a workqueue, so we won't get callbacks and can hold port_lock
  532. */
  533. if (tty && do_push)
  534. tty_flip_buffer_push(tty);
  535. /* We want our data queue to become empty ASAP, keeping data
  536. * in the tty and ldisc (not here). If we couldn't push any
  537. * this time around, there may be trouble unless there's an
  538. * implicit tty_unthrottle() call on its way...
  539. *
  540. * REVISIT we should probably add a timer to keep the work queue
  541. * from starving ... but it's not clear that case ever happens.
  542. */
  543. if (!list_empty(queue) && tty) {
  544. if (!test_bit(TTY_THROTTLED, &tty->flags)) {
  545. if (do_push)
  546. queue_work(gserial_wq, &port->push);
  547. else
  548. pr_warning(PREFIX "%d: RX not scheduled?\n",
  549. port->port_num);
  550. }
  551. }
  552. /* If we're still connected, refill the USB RX queue. */
  553. if (!disconnect && port->port_usb)
  554. gs_start_rx(port);
  555. spin_unlock_irq(&port->port_lock);
  556. }
  557. static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
  558. {
  559. struct gs_port *port = ep->driver_data;
  560. unsigned long flags;
  561. /* Queue all received data until the tty layer is ready for it. */
  562. spin_lock_irqsave(&port->port_lock, flags);
  563. port->nbytes_from_host += req->actual;
  564. list_add_tail(&req->list, &port->read_queue);
  565. queue_work(gserial_wq, &port->push);
  566. spin_unlock_irqrestore(&port->port_lock, flags);
  567. }
  568. static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
  569. {
  570. struct gs_port *port = ep->driver_data;
  571. unsigned long flags;
  572. spin_lock_irqsave(&port->port_lock, flags);
  573. port->nbytes_to_host += req->actual;
  574. list_add(&req->list, &port->write_pool);
  575. port->write_started--;
  576. switch (req->status) {
  577. default:
  578. /* presumably a transient fault */
  579. pr_warning("%s: unexpected %s status %d\n",
  580. __func__, ep->name, req->status);
  581. /* FALL THROUGH */
  582. case 0:
  583. /* normal completion */
  584. if (port->port_usb)
  585. gs_start_tx(port);
  586. break;
  587. case -ESHUTDOWN:
  588. /* disconnect */
  589. pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
  590. break;
  591. }
  592. spin_unlock_irqrestore(&port->port_lock, flags);
  593. }
  594. static void gs_free_requests(struct usb_ep *ep, struct list_head *head,
  595. int *allocated)
  596. {
  597. struct usb_request *req;
  598. while (!list_empty(head)) {
  599. req = list_entry(head->next, struct usb_request, list);
  600. list_del(&req->list);
  601. gs_free_req(ep, req);
  602. if (allocated)
  603. (*allocated)--;
  604. }
  605. }
  606. static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head,
  607. int queue_size, int req_size,
  608. void (*fn)(struct usb_ep *, struct usb_request *),
  609. int *allocated)
  610. {
  611. int i;
  612. struct usb_request *req;
  613. int n = allocated ? queue_size - *allocated : queue_size;
  614. /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't
  615. * do quite that many this time, don't fail ... we just won't
  616. * be as speedy as we might otherwise be.
  617. */
  618. for (i = 0; i < n; i++) {
  619. req = gs_alloc_req(ep, req_size, GFP_ATOMIC);
  620. if (!req)
  621. return list_empty(head) ? -ENOMEM : 0;
  622. req->complete = fn;
  623. list_add_tail(&req->list, head);
  624. if (allocated)
  625. (*allocated)++;
  626. }
  627. return 0;
  628. }
  629. /**
  630. * gs_start_io - start USB I/O streams
  631. * @dev: encapsulates endpoints to use
  632. * Context: holding port_lock; port_tty and port_usb are non-null
  633. *
  634. * We only start I/O when something is connected to both sides of
  635. * this port. If nothing is listening on the host side, we may
  636. * be pointlessly filling up our TX buffers and FIFO.
  637. */
  638. static int gs_start_io(struct gs_port *port)
  639. {
  640. struct list_head *head = &port->read_pool;
  641. struct usb_ep *ep = port->port_usb->out;
  642. int status;
  643. unsigned started;
  644. /* Allocate RX and TX I/O buffers. We can't easily do this much
  645. * earlier (with GFP_KERNEL) because the requests are coupled to
  646. * endpoints, as are the packet sizes we'll be using. Different
  647. * configurations may use different endpoints with a given port;
  648. * and high speed vs full speed changes packet sizes too.
  649. */
  650. status = gs_alloc_requests(ep, head, RX_QUEUE_SIZE, RX_BUF_SIZE,
  651. gs_read_complete, &port->read_allocated);
  652. if (status)
  653. return status;
  654. status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
  655. TX_QUEUE_SIZE, TX_BUF_SIZE, gs_write_complete, &port->write_allocated);
  656. if (status) {
  657. gs_free_requests(ep, head, &port->read_allocated);
  658. return status;
  659. }
  660. /* queue read requests */
  661. port->n_read = 0;
  662. started = gs_start_rx(port);
  663. if (!port->port_usb || !port->port_tty) {
  664. printk(KERN_ERR "usb:[%s] port_usb or port_tty is NULL!! started(%d)\n",
  665. __func__, started);
  666. return -EIO;
  667. }
  668. /* unblock any pending writes into our circular buffer */
  669. if (started) {
  670. tty_wakeup(port->port_tty);
  671. } else {
  672. gs_free_requests(ep, head, &port->read_allocated);
  673. gs_free_requests(port->port_usb->in, &port->write_pool,
  674. &port->write_allocated);
  675. status = -EIO;
  676. }
  677. return status;
  678. }
  679. /*-------------------------------------------------------------------------*/
  680. /* TTY Driver */
  681. /*
  682. * gs_open sets up the link between a gs_port and its associated TTY.
  683. * That link is broken *only* by TTY close(), and all driver methods
  684. * know that.
  685. */
  686. static int gs_open(struct tty_struct *tty, struct file *file)
  687. {
  688. int port_num = tty->index;
  689. struct gs_port *port;
  690. int status;
  691. do {
  692. mutex_lock(&ports[port_num].lock);
  693. port = ports[port_num].port;
  694. if (!port)
  695. status = -ENODEV;
  696. else {
  697. spin_lock_irq(&port->port_lock);
  698. /* already open? Great. */
  699. if (port->open_count) {
  700. status = 0;
  701. port->open_count++;
  702. /* currently opening/closing? wait ... */
  703. } else if (port->openclose) {
  704. status = -EBUSY;
  705. /* ... else we do the work */
  706. } else {
  707. status = -EAGAIN;
  708. port->openclose = true;
  709. }
  710. spin_unlock_irq(&port->port_lock);
  711. }
  712. mutex_unlock(&ports[port_num].lock);
  713. switch (status) {
  714. default:
  715. /* fully handled */
  716. return status;
  717. case -EAGAIN:
  718. /* must do the work */
  719. break;
  720. case -EBUSY:
  721. /* wait for EAGAIN task to finish */
  722. msleep(1);
  723. /* REVISIT could have a waitchannel here, if
  724. * concurrent open performance is important
  725. */
  726. break;
  727. }
  728. } while (status != -EAGAIN);
  729. /* Do the "real open" */
  730. spin_lock_irq(&port->port_lock);
  731. /* allocate circular buffer on first open */
  732. if (port->port_write_buf.buf_buf == NULL) {
  733. spin_unlock_irq(&port->port_lock);
  734. status = gs_buf_alloc(&port->port_write_buf, WRITE_BUF_SIZE);
  735. spin_lock_irq(&port->port_lock);
  736. if (status) {
  737. pr_debug("gs_open: ttyGS%d (%pK,%pK) no buffer\n",
  738. port->port_num, tty, file);
  739. port->openclose = false;
  740. goto exit_unlock_port;
  741. }
  742. }
  743. /* REVISIT if REMOVED (ports[].port NULL), abort the open
  744. * to let rmmod work faster (but this way isn't wrong).
  745. */
  746. /* REVISIT maybe wait for "carrier detect" */
  747. tty->driver_data = port;
  748. port->port_tty = tty;
  749. port->open_count = 1;
  750. port->openclose = false;
  751. /* if connected, start the I/O stream */
  752. if (port->port_usb) {
  753. struct gserial *gser = port->port_usb;
  754. pr_debug("gs_open: start ttyGS%d\n", port->port_num);
  755. gs_start_io(port);
  756. if (gser->connect)
  757. gser->connect(gser);
  758. }
  759. pr_debug("gs_open: ttyGS%d (%pK,%pK)\n", port->port_num, tty, file);
  760. status = 0;
  761. exit_unlock_port:
  762. spin_unlock_irq(&port->port_lock);
  763. return status;
  764. }
  765. static int gs_writes_finished(struct gs_port *p)
  766. {
  767. int cond;
  768. /* return true on disconnect or empty buffer */
  769. spin_lock_irq(&p->port_lock);
  770. cond = (p->port_usb == NULL) || !gs_buf_data_avail(&p->port_write_buf);
  771. spin_unlock_irq(&p->port_lock);
  772. return cond;
  773. }
  774. static void gs_close(struct tty_struct *tty, struct file *file)
  775. {
  776. struct gs_port *port = tty->driver_data;
  777. struct gserial *gser;
  778. spin_lock_irq(&port->port_lock);
  779. if (port->open_count != 1) {
  780. if (port->open_count == 0)
  781. WARN_ON(1);
  782. else
  783. --port->open_count;
  784. goto exit;
  785. }
  786. pr_debug("gs_close: ttyGS%d (%pK,%pK) ...\n", port->port_num, tty, file);
  787. /* mark port as closing but in use; we can drop port lock
  788. * and sleep if necessary
  789. */
  790. port->openclose = true;
  791. port->open_count = 0;
  792. gser = port->port_usb;
  793. if (gser && gser->disconnect)
  794. gser->disconnect(gser);
  795. /* wait for circular write buffer to drain, disconnect, or at
  796. * most GS_CLOSE_TIMEOUT seconds; then discard the rest
  797. */
  798. if (gs_buf_data_avail(&port->port_write_buf) > 0 && gser) {
  799. spin_unlock_irq(&port->port_lock);
  800. wait_event_interruptible_timeout(port->drain_wait,
  801. gs_writes_finished(port),
  802. GS_CLOSE_TIMEOUT * HZ);
  803. spin_lock_irq(&port->port_lock);
  804. gser = port->port_usb;
  805. }
  806. /* Iff we're disconnected, there can be no I/O in flight so it's
  807. * ok to free the circular buffer; else just scrub it. And don't
  808. * let the push work queue fire again until we're re-opened.
  809. */
  810. if (gser == NULL)
  811. gs_buf_free(&port->port_write_buf);
  812. else
  813. gs_buf_clear(&port->port_write_buf);
  814. tty->driver_data = NULL;
  815. port->port_tty = NULL;
  816. port->openclose = false;
  817. pr_debug("gs_close: ttyGS%d (%pK,%pK) done!\n",
  818. port->port_num, tty, file);
  819. wake_up_interruptible(&port->close_wait);
  820. exit:
  821. spin_unlock_irq(&port->port_lock);
  822. }
  823. static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
  824. {
  825. struct gs_port *port = tty->driver_data;
  826. unsigned long flags;
  827. int status;
  828. pr_vdebug("gs_write: ttyGS%d (%pK) writing %d bytes\n",
  829. port->port_num, tty, count);
  830. spin_lock_irqsave(&port->port_lock, flags);
  831. if (count)
  832. count = gs_buf_put(&port->port_write_buf, buf, count);
  833. /* treat count == 0 as flush_chars() */
  834. if (port->port_usb)
  835. status = gs_start_tx(port);
  836. spin_unlock_irqrestore(&port->port_lock, flags);
  837. return count;
  838. }
  839. static int gs_put_char(struct tty_struct *tty, unsigned char ch)
  840. {
  841. struct gs_port *port = tty->driver_data;
  842. unsigned long flags;
  843. int status;
  844. pr_vdebug("gs_put_char: (%d,%pK) char=0x%x, called from %pK\n",
  845. port->port_num, tty, ch, __builtin_return_address(0));
  846. spin_lock_irqsave(&port->port_lock, flags);
  847. status = gs_buf_put(&port->port_write_buf, &ch, 1);
  848. spin_unlock_irqrestore(&port->port_lock, flags);
  849. return status;
  850. }
  851. static void gs_flush_chars(struct tty_struct *tty)
  852. {
  853. struct gs_port *port = tty->driver_data;
  854. unsigned long flags;
  855. pr_vdebug("gs_flush_chars: (%d,%pK)\n", port->port_num, tty);
  856. spin_lock_irqsave(&port->port_lock, flags);
  857. if (port->port_usb)
  858. gs_start_tx(port);
  859. spin_unlock_irqrestore(&port->port_lock, flags);
  860. }
  861. static int gs_write_room(struct tty_struct *tty)
  862. {
  863. struct gs_port *port = tty->driver_data;
  864. unsigned long flags;
  865. int room = 0;
  866. spin_lock_irqsave(&port->port_lock, flags);
  867. if (port->port_usb)
  868. room = gs_buf_space_avail(&port->port_write_buf);
  869. spin_unlock_irqrestore(&port->port_lock, flags);
  870. pr_vdebug("gs_write_room: (%d,%pK) room=%d\n",
  871. port->port_num, tty, room);
  872. return room;
  873. }
  874. static int gs_chars_in_buffer(struct tty_struct *tty)
  875. {
  876. struct gs_port *port = tty->driver_data;
  877. unsigned long flags;
  878. int chars = 0;
  879. spin_lock_irqsave(&port->port_lock, flags);
  880. chars = gs_buf_data_avail(&port->port_write_buf);
  881. spin_unlock_irqrestore(&port->port_lock, flags);
  882. pr_vdebug("gs_chars_in_buffer: (%d,%pK) chars=%d\n",
  883. port->port_num, tty, chars);
  884. return chars;
  885. }
  886. /* undo side effects of setting TTY_THROTTLED */
  887. static void gs_unthrottle(struct tty_struct *tty)
  888. {
  889. struct gs_port *port = tty->driver_data;
  890. unsigned long flags;
  891. /*
  892. * tty's driver data is set to NULL during port close. Nothing
  893. * to do here.
  894. */
  895. if (!port)
  896. return;
  897. spin_lock_irqsave(&port->port_lock, flags);
  898. if (port->port_usb) {
  899. /* Kickstart read queue processing. We don't do xon/xoff,
  900. * rts/cts, or other handshaking with the host, but if the
  901. * read queue backs up enough we'll be NAKing OUT packets.
  902. */
  903. queue_work(gserial_wq, &port->push);
  904. pr_vdebug(PREFIX "%d: unthrottle\n", port->port_num);
  905. }
  906. spin_unlock_irqrestore(&port->port_lock, flags);
  907. }
  908. static int gs_break_ctl(struct tty_struct *tty, int duration)
  909. {
  910. struct gs_port *port = tty->driver_data;
  911. int status = 0;
  912. struct gserial *gser;
  913. pr_vdebug("gs_break_ctl: ttyGS%d, send break (%d) \n",
  914. port->port_num, duration);
  915. spin_lock_irq(&port->port_lock);
  916. gser = port->port_usb;
  917. if (gser && gser->send_break)
  918. status = gser->send_break(gser, duration);
  919. spin_unlock_irq(&port->port_lock);
  920. return status;
  921. }
  922. static int gs_tiocmget(struct tty_struct *tty)
  923. {
  924. struct gs_port *port = tty->driver_data;
  925. struct gserial *gser;
  926. unsigned int result = 0;
  927. spin_lock_irq(&port->port_lock);
  928. gser = port->port_usb;
  929. if (!gser) {
  930. result = -ENODEV;
  931. goto fail;
  932. }
  933. if (gser->get_dtr)
  934. result |= (gser->get_dtr(gser) ? TIOCM_DTR : 0);
  935. if (gser->get_rts)
  936. result |= (gser->get_rts(gser) ? TIOCM_RTS : 0);
  937. if (gser->serial_state & TIOCM_CD)
  938. result |= TIOCM_CD;
  939. if (gser->serial_state & TIOCM_RI)
  940. result |= TIOCM_RI;
  941. fail:
  942. spin_unlock_irq(&port->port_lock);
  943. return result;
  944. }
  945. static int gs_tiocmset(struct tty_struct *tty,
  946. unsigned int set, unsigned int clear)
  947. {
  948. struct gs_port *port = tty->driver_data;
  949. struct gserial *gser;
  950. int status = 0;
  951. spin_lock_irq(&port->port_lock);
  952. gser = port->port_usb;
  953. if (!gser) {
  954. status = -ENODEV;
  955. goto fail;
  956. }
  957. if (set & TIOCM_RI) {
  958. if (gser->send_ring_indicator) {
  959. gser->serial_state |= TIOCM_RI;
  960. status = gser->send_ring_indicator(gser, 1);
  961. }
  962. }
  963. if (clear & TIOCM_RI) {
  964. if (gser->send_ring_indicator) {
  965. gser->serial_state &= ~TIOCM_RI;
  966. status = gser->send_ring_indicator(gser, 0);
  967. }
  968. }
  969. if (set & TIOCM_CD) {
  970. if (gser->send_carrier_detect) {
  971. gser->serial_state |= TIOCM_CD;
  972. status = gser->send_carrier_detect(gser, 1);
  973. }
  974. }
  975. if (clear & TIOCM_CD) {
  976. if (gser->send_carrier_detect) {
  977. gser->serial_state &= ~TIOCM_CD;
  978. status = gser->send_carrier_detect(gser, 0);
  979. }
  980. }
  981. fail:
  982. spin_unlock_irq(&port->port_lock);
  983. return status;
  984. }
  985. static const struct tty_operations gs_tty_ops = {
  986. .open = gs_open,
  987. .close = gs_close,
  988. .write = gs_write,
  989. .put_char = gs_put_char,
  990. .flush_chars = gs_flush_chars,
  991. .write_room = gs_write_room,
  992. .chars_in_buffer = gs_chars_in_buffer,
  993. .unthrottle = gs_unthrottle,
  994. .break_ctl = gs_break_ctl,
  995. .tiocmget = gs_tiocmget,
  996. .tiocmset = gs_tiocmset,
  997. };
  998. /*-------------------------------------------------------------------------*/
  999. static struct tty_driver *gs_tty_driver;
  1000. static int
  1001. gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
  1002. {
  1003. struct gs_port *port;
  1004. port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
  1005. if (port == NULL)
  1006. return -ENOMEM;
  1007. spin_lock_init(&port->port_lock);
  1008. init_waitqueue_head(&port->close_wait);
  1009. init_waitqueue_head(&port->drain_wait);
  1010. INIT_WORK(&port->push, gs_rx_push);
  1011. INIT_LIST_HEAD(&port->read_pool);
  1012. INIT_LIST_HEAD(&port->read_queue);
  1013. INIT_LIST_HEAD(&port->write_pool);
  1014. port->port_num = port_num;
  1015. port->port_line_coding = *coding;
  1016. ports[port_num].port = port;
  1017. return 0;
  1018. }
  1019. #if defined(CONFIG_DEBUG_FS)
  1020. #define BUF_SIZE 512
  1021. static ssize_t debug_read_status(struct file *file, char __user *ubuf,
  1022. size_t count, loff_t *ppos)
  1023. {
  1024. struct gs_port *ui_dev = file->private_data;
  1025. struct tty_struct *tty;
  1026. struct gserial *gser;
  1027. char *buf;
  1028. unsigned long flags;
  1029. int i = 0;
  1030. int ret;
  1031. int result = 0;
  1032. tty = ui_dev->port_tty;
  1033. gser = ui_dev->port_usb;
  1034. if(!tty || !gser) {
  1035. printk(KERN_ERR "usb: tty or gser is NULL !!\n");
  1036. return -EINVAL;
  1037. }
  1038. buf = kzalloc(sizeof(char) * BUF_SIZE, GFP_KERNEL);
  1039. if (!buf)
  1040. return -ENOMEM;
  1041. spin_lock_irqsave(&ui_dev->port_lock, flags);
  1042. i += scnprintf(buf + i, BUF_SIZE - i,
  1043. "nbytes_from_host: %lu\n", ui_dev->nbytes_from_host);
  1044. i += scnprintf(buf + i, BUF_SIZE - i,
  1045. "nbytes_to_tty: %lu\n", ui_dev->nbytes_to_tty);
  1046. i += scnprintf(buf + i, BUF_SIZE - i, "nbytes_with_usb_OUT_txr: %lu\n",
  1047. (ui_dev->nbytes_from_host - ui_dev->nbytes_to_tty));
  1048. i += scnprintf(buf + i, BUF_SIZE - i,
  1049. "nbytes_from_tty: %lu\n", ui_dev->nbytes_from_tty);
  1050. i += scnprintf(buf + i, BUF_SIZE - i,
  1051. "nbytes_to_host: %lu\n", ui_dev->nbytes_to_host);
  1052. i += scnprintf(buf + i, BUF_SIZE - i, "nbytes_with_usb_IN_txr: %lu\n",
  1053. (ui_dev->nbytes_from_tty - ui_dev->nbytes_to_host));
  1054. if (tty)
  1055. i += scnprintf(buf + i, BUF_SIZE - i,
  1056. "tty_flags: %lu\n", tty->flags);
  1057. if (gser->get_dtr) {
  1058. result |= (gser->get_dtr(gser) ? TIOCM_DTR : 0);
  1059. i += scnprintf(buf + i, BUF_SIZE - i,
  1060. "DTR_status: %d\n", result);
  1061. }
  1062. spin_unlock_irqrestore(&ui_dev->port_lock, flags);
  1063. ret = simple_read_from_buffer(ubuf, count, ppos, buf, i);
  1064. kfree(buf);
  1065. return ret;
  1066. }
  1067. static ssize_t debug_write_reset(struct file *file, const char __user *buf,
  1068. size_t count, loff_t *ppos)
  1069. {
  1070. struct gs_port *ui_dev = file->private_data;
  1071. unsigned long flags;
  1072. spin_lock_irqsave(&ui_dev->port_lock, flags);
  1073. ui_dev->nbytes_from_host = ui_dev->nbytes_to_tty =
  1074. ui_dev->nbytes_from_tty = ui_dev->nbytes_to_host = 0;
  1075. spin_unlock_irqrestore(&ui_dev->port_lock, flags);
  1076. return count;
  1077. }
  1078. static int serial_debug_open(struct inode *inode, struct file *file)
  1079. {
  1080. file->private_data = inode->i_private;
  1081. return 0;
  1082. }
  1083. const struct file_operations debug_rst_ops = {
  1084. .open = serial_debug_open,
  1085. .write = debug_write_reset,
  1086. };
  1087. const struct file_operations debug_adb_ops = {
  1088. .open = serial_debug_open,
  1089. .read = debug_read_status,
  1090. };
  1091. struct dentry *gs_dent;
  1092. static void usb_debugfs_init(struct gs_port *ui_dev, int port_num)
  1093. {
  1094. char buf[48];
  1095. snprintf(buf, 48, "usb_serial%d", port_num);
  1096. gs_dent = debugfs_create_dir(buf, 0);
  1097. if (!gs_dent || IS_ERR(gs_dent))
  1098. return;
  1099. debugfs_create_file("readstatus", 0444, gs_dent, ui_dev,
  1100. &debug_adb_ops);
  1101. debugfs_create_file("reset", S_IRUGO | S_IWUSR,
  1102. gs_dent, ui_dev, &debug_rst_ops);
  1103. }
  1104. static void usb_debugfs_remove(void)
  1105. {
  1106. debugfs_remove_recursive(gs_dent);
  1107. }
  1108. #else
  1109. static inline void usb_debugfs_init(struct gs_port *ui_dev, int port_num) {}
  1110. static inline void usb_debugfs_remove(void) {}
  1111. #endif
  1112. /**
  1113. * gserial_setup - initialize TTY driver for one or more ports
  1114. * @g: gadget to associate with these ports
  1115. * @count: how many ports to support
  1116. * Context: may sleep
  1117. *
  1118. * The TTY stack needs to know in advance how many devices it should
  1119. * plan to manage. Use this call to set up the ports you will be
  1120. * exporting through USB. Later, connect them to functions based
  1121. * on what configuration is activated by the USB host; and disconnect
  1122. * them as appropriate.
  1123. *
  1124. * An example would be a two-configuration device in which both
  1125. * configurations expose port 0, but through different functions.
  1126. * One configuration could even expose port 1 while the other
  1127. * one doesn't.
  1128. *
  1129. * Returns negative errno or zero.
  1130. */
  1131. int gserial_setup(struct usb_gadget *g, unsigned count)
  1132. {
  1133. unsigned i;
  1134. struct usb_cdc_line_coding coding;
  1135. int status;
  1136. if (count == 0 || count > N_PORTS)
  1137. return -EINVAL;
  1138. gs_tty_driver = alloc_tty_driver(count);
  1139. if (!gs_tty_driver)
  1140. return -ENOMEM;
  1141. gs_tty_driver->driver_name = "g_serial";
  1142. gs_tty_driver->name = PREFIX;
  1143. /* uses dynamically assigned dev_t values */
  1144. gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1145. gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  1146. gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
  1147. | TTY_DRIVER_RESET_TERMIOS;
  1148. gs_tty_driver->init_termios = tty_std_termios;
  1149. /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on
  1150. * MS-Windows. Otherwise, most of these flags shouldn't affect
  1151. * anything unless we were to actually hook up to a serial line.
  1152. */
  1153. gs_tty_driver->init_termios.c_cflag =
  1154. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1155. gs_tty_driver->init_termios.c_ispeed = 9600;
  1156. gs_tty_driver->init_termios.c_ospeed = 9600;
  1157. coding.dwDTERate = cpu_to_le32(9600);
  1158. coding.bCharFormat = 8;
  1159. coding.bParityType = USB_CDC_NO_PARITY;
  1160. coding.bDataBits = USB_CDC_1_STOP_BITS;
  1161. tty_set_operations(gs_tty_driver, &gs_tty_ops);
  1162. gserial_wq = create_singlethread_workqueue("k_gserial");
  1163. if (!gserial_wq) {
  1164. status = -ENOMEM;
  1165. goto fail;
  1166. }
  1167. /* make devices be openable */
  1168. for (i = 0; i < count; i++) {
  1169. mutex_init(&ports[i].lock);
  1170. status = gs_port_alloc(i, &coding);
  1171. if (status) {
  1172. count = i;
  1173. goto fail;
  1174. }
  1175. }
  1176. n_ports = count;
  1177. /* export the driver ... */
  1178. status = tty_register_driver(gs_tty_driver);
  1179. if (status) {
  1180. pr_err("%s: cannot register, err %d\n",
  1181. __func__, status);
  1182. goto fail;
  1183. }
  1184. /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
  1185. for (i = 0; i < count; i++) {
  1186. struct device *tty_dev;
  1187. tty_dev = tty_register_device(gs_tty_driver, i, &g->dev);
  1188. if (IS_ERR(tty_dev))
  1189. pr_warning("%s: no classdev for port %d, err %ld\n",
  1190. __func__, i, PTR_ERR(tty_dev));
  1191. }
  1192. for (i = 0; i < count; i++)
  1193. usb_debugfs_init(ports[i].port, i);
  1194. pr_debug("%s: registered %d ttyGS* device%s\n", __func__,
  1195. count, (count == 1) ? "" : "s");
  1196. return status;
  1197. fail:
  1198. while (count--)
  1199. kfree(ports[count].port);
  1200. if (gserial_wq)
  1201. destroy_workqueue(gserial_wq);
  1202. put_tty_driver(gs_tty_driver);
  1203. gs_tty_driver = NULL;
  1204. return status;
  1205. }
  1206. static int gs_closed(struct gs_port *port)
  1207. {
  1208. int cond;
  1209. spin_lock_irq(&port->port_lock);
  1210. cond = (port->open_count == 0) && !port->openclose;
  1211. spin_unlock_irq(&port->port_lock);
  1212. return cond;
  1213. }
  1214. /**
  1215. * gserial_cleanup - remove TTY-over-USB driver and devices
  1216. * Context: may sleep
  1217. *
  1218. * This is called to free all resources allocated by @gserial_setup().
  1219. * Accordingly, it may need to wait until some open /dev/ files have
  1220. * closed.
  1221. *
  1222. * The caller must have issued @gserial_disconnect() for any ports
  1223. * that had previously been connected, so that there is never any
  1224. * I/O pending when it's called.
  1225. */
  1226. void gserial_cleanup(void)
  1227. {
  1228. unsigned i;
  1229. struct gs_port *port;
  1230. if (!gs_tty_driver)
  1231. return;
  1232. /* start sysfs and /dev/ttyGS* node removal */
  1233. for (i = 0; i < n_ports; i++)
  1234. tty_unregister_device(gs_tty_driver, i);
  1235. for (i = 0; i < n_ports; i++) {
  1236. /* prevent new opens */
  1237. mutex_lock(&ports[i].lock);
  1238. port = ports[i].port;
  1239. ports[i].port = NULL;
  1240. mutex_unlock(&ports[i].lock);
  1241. cancel_work_sync(&port->push);
  1242. /* wait for old opens to finish */
  1243. wait_event(port->close_wait, gs_closed(port));
  1244. WARN_ON(port->port_usb != NULL);
  1245. kfree(port);
  1246. }
  1247. n_ports = 0;
  1248. usb_debugfs_remove();
  1249. destroy_workqueue(gserial_wq);
  1250. tty_unregister_driver(gs_tty_driver);
  1251. put_tty_driver(gs_tty_driver);
  1252. gs_tty_driver = NULL;
  1253. pr_debug("%s: cleaned up ttyGS* support\n", __func__);
  1254. }
  1255. /**
  1256. * gserial_connect - notify TTY I/O glue that USB link is active
  1257. * @gser: the function, set up with endpoints and descriptors
  1258. * @port_num: which port is active
  1259. * Context: any (usually from irq)
  1260. *
  1261. * This is called activate endpoints and let the TTY layer know that
  1262. * the connection is active ... not unlike "carrier detect". It won't
  1263. * necessarily start I/O queues; unless the TTY is held open by any
  1264. * task, there would be no point. However, the endpoints will be
  1265. * activated so the USB host can perform I/O, subject to basic USB
  1266. * hardware flow control.
  1267. *
  1268. * Caller needs to have set up the endpoints and USB function in @dev
  1269. * before calling this, as well as the appropriate (speed-specific)
  1270. * endpoint descriptors, and also have set up the TTY driver by calling
  1271. * @gserial_setup().
  1272. *
  1273. * Returns negative errno or zero.
  1274. * On success, ep->driver_data will be overwritten.
  1275. */
  1276. int gserial_connect(struct gserial *gser, u8 port_num)
  1277. {
  1278. struct gs_port *port;
  1279. unsigned long flags;
  1280. int status;
  1281. if (!gs_tty_driver || port_num >= n_ports)
  1282. return -ENXIO;
  1283. /* we "know" gserial_cleanup() hasn't been called */
  1284. port = ports[port_num].port;
  1285. /* activate the endpoints */
  1286. status = usb_ep_enable(gser->in);
  1287. if (status < 0)
  1288. return status;
  1289. gser->in->driver_data = port;
  1290. status = usb_ep_enable(gser->out);
  1291. if (status < 0)
  1292. goto fail_out;
  1293. gser->out->driver_data = port;
  1294. /* then tell the tty glue that I/O can work */
  1295. spin_lock_irqsave(&port->port_lock, flags);
  1296. gser->ioport = port;
  1297. port->port_usb = gser;
  1298. /* REVISIT unclear how best to handle this state...
  1299. * we don't really couple it with the Linux TTY.
  1300. */
  1301. gser->port_line_coding = port->port_line_coding;
  1302. /* REVISIT if waiting on "carrier detect", signal. */
  1303. /* if it's already open, start I/O ... and notify the serial
  1304. * protocol about open/close status (connect/disconnect).
  1305. */
  1306. if (port->open_count) {
  1307. pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
  1308. gs_start_io(port);
  1309. if (gser->connect)
  1310. gser->connect(gser);
  1311. } else {
  1312. if (gser->disconnect)
  1313. gser->disconnect(gser);
  1314. }
  1315. spin_unlock_irqrestore(&port->port_lock, flags);
  1316. return status;
  1317. fail_out:
  1318. usb_ep_disable(gser->in);
  1319. gser->in->driver_data = NULL;
  1320. return status;
  1321. }
  1322. /**
  1323. * gserial_disconnect - notify TTY I/O glue that USB link is inactive
  1324. * @gser: the function, on which gserial_connect() was called
  1325. * Context: any (usually from irq)
  1326. *
  1327. * This is called to deactivate endpoints and let the TTY layer know
  1328. * that the connection went inactive ... not unlike "hangup".
  1329. *
  1330. * On return, the state is as if gserial_connect() had never been called;
  1331. * there is no active USB I/O on these endpoints.
  1332. */
  1333. void gserial_disconnect(struct gserial *gser)
  1334. {
  1335. struct gs_port *port = gser->ioport;
  1336. unsigned long flags;
  1337. if (!port)
  1338. return;
  1339. /* tell the TTY glue not to do I/O here any more */
  1340. spin_lock_irqsave(&port->port_lock, flags);
  1341. /* REVISIT as above: how best to track this? */
  1342. port->port_line_coding = gser->port_line_coding;
  1343. port->port_usb = NULL;
  1344. gser->ioport = NULL;
  1345. if (port->open_count > 0 || port->openclose) {
  1346. wake_up_interruptible(&port->drain_wait);
  1347. if (port->port_tty)
  1348. tty_hangup(port->port_tty);
  1349. }
  1350. spin_unlock_irqrestore(&port->port_lock, flags);
  1351. /* disable endpoints, aborting down any active I/O */
  1352. usb_ep_disable(gser->out);
  1353. gser->out->driver_data = NULL;
  1354. usb_ep_disable(gser->in);
  1355. gser->in->driver_data = NULL;
  1356. /* finally, free any unused/unusable I/O buffers */
  1357. spin_lock_irqsave(&port->port_lock, flags);
  1358. if (port->open_count == 0 && !port->openclose)
  1359. gs_buf_free(&port->port_write_buf);
  1360. gs_free_requests(gser->out, &port->read_pool, NULL);
  1361. gs_free_requests(gser->out, &port->read_queue, NULL);
  1362. gs_free_requests(gser->in, &port->write_pool, NULL);
  1363. port->read_allocated = port->read_started =
  1364. port->write_allocated = port->write_started = 0;
  1365. port->nbytes_from_host = port->nbytes_to_tty =
  1366. port->nbytes_from_tty = port->nbytes_to_host = 0;
  1367. spin_unlock_irqrestore(&port->port_lock, flags);
  1368. }