u_serial.c 38 KB

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