u_serial.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * u_serial.c - utilities for USB gadget "serial port"/TTY support
  4. *
  5. * Copyright (C) 2003 Al Borchers ([email protected])
  6. * Copyright (C) 2008 David Brownell
  7. * Copyright (C) 2008 by Nokia Corporation
  8. *
  9. * This code also borrows from usbserial.c, which is
  10. * Copyright (C) 1999 - 2002 Greg Kroah-Hartman ([email protected])
  11. * Copyright (C) 2000 Peter Berger ([email protected])
  12. * Copyright (C) 2000 Al Borchers ([email protected])
  13. */
  14. /* #define VERBOSE_DEBUG */
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/device.h>
  18. #include <linux/delay.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/slab.h>
  22. #include <linux/export.h>
  23. #include <linux/module.h>
  24. #include <linux/console.h>
  25. #include <linux/kthread.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/kfifo.h>
  28. #include "u_serial.h"
  29. /*
  30. * This component encapsulates the TTY layer glue needed to provide basic
  31. * "serial port" functionality through the USB gadget stack. Each such
  32. * port is exposed through a /dev/ttyGS* node.
  33. *
  34. * After this module has been loaded, the individual TTY port can be requested
  35. * (gserial_alloc_line()) and it will stay available until they are removed
  36. * (gserial_free_line()). Each one may be connected to a USB function
  37. * (gserial_connect), or disconnected (with gserial_disconnect) when the USB
  38. * host issues a config change event. Data can only flow when the port is
  39. * 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. *
  55. * gserial is the lifecycle interface, used by USB functions
  56. * gs_port is the I/O nexus, used by the tty driver
  57. * tty_struct links to the tty/filesystem framework
  58. *
  59. * gserial <---> gs_port ... links will be null when the USB link is
  60. * inactive; managed by gserial_{connect,disconnect}(). each gserial
  61. * instance can wrap its own USB control protocol.
  62. * gserial->ioport == usb_ep->driver_data ... gs_port
  63. * gs_port->port_usb ... gserial
  64. *
  65. * gs_port <---> tty_struct ... links will be null when the TTY file
  66. * isn't opened; managed by gs_open()/gs_close()
  67. * gserial->port_tty ... tty_struct
  68. * tty_struct->driver_data ... gserial
  69. */
  70. /* RX and TX queues can buffer QUEUE_SIZE packets before they hit the
  71. * next layer of buffering. For TX that's a circular buffer; for RX
  72. * consider it a NOP. A third layer is provided by the TTY code.
  73. */
  74. #define QUEUE_SIZE 16
  75. #define WRITE_BUF_SIZE 8192 /* TX only */
  76. #define GS_CONSOLE_BUF_SIZE 8192
  77. /* Prevents race conditions while accessing gser->ioport */
  78. static DEFINE_SPINLOCK(serial_port_lock);
  79. /* console info */
  80. struct gs_console {
  81. struct console console;
  82. struct work_struct work;
  83. spinlock_t lock;
  84. struct usb_request *req;
  85. struct kfifo buf;
  86. size_t missed;
  87. };
  88. /*
  89. * The port structure holds info for each port, one for each minor number
  90. * (and thus for each /dev/ node).
  91. */
  92. struct gs_port {
  93. struct tty_port port;
  94. spinlock_t port_lock; /* guard port_* access */
  95. struct gserial *port_usb;
  96. #ifdef CONFIG_U_SERIAL_CONSOLE
  97. struct gs_console *console;
  98. #endif
  99. u8 port_num;
  100. struct list_head read_pool;
  101. int read_started;
  102. int read_allocated;
  103. struct list_head read_queue;
  104. unsigned n_read;
  105. struct delayed_work push;
  106. struct list_head write_pool;
  107. int write_started;
  108. int write_allocated;
  109. struct kfifo port_write_buf;
  110. wait_queue_head_t drain_wait; /* wait while writes drain */
  111. bool write_busy;
  112. wait_queue_head_t close_wait;
  113. bool suspended; /* port suspended */
  114. bool start_delayed; /* delay start when suspended */
  115. /* REVISIT this state ... */
  116. struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
  117. };
  118. static struct portmaster {
  119. struct mutex lock; /* protect open/close */
  120. struct gs_port *port;
  121. } ports[MAX_U_SERIAL_PORTS];
  122. #define GS_CLOSE_TIMEOUT 15 /* seconds */
  123. #ifdef VERBOSE_DEBUG
  124. #ifndef pr_vdebug
  125. #define pr_vdebug(fmt, arg...) \
  126. pr_debug(fmt, ##arg)
  127. #endif /* pr_vdebug */
  128. #else
  129. #ifndef pr_vdebug
  130. #define pr_vdebug(fmt, arg...) \
  131. ({ if (0) pr_debug(fmt, ##arg); })
  132. #endif /* pr_vdebug */
  133. #endif
  134. /*-------------------------------------------------------------------------*/
  135. /* I/O glue between TTY (upper) and USB function (lower) driver layers */
  136. /*
  137. * gs_alloc_req
  138. *
  139. * Allocate a usb_request and its buffer. Returns a pointer to the
  140. * usb_request or NULL if there is an error.
  141. */
  142. struct usb_request *
  143. gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
  144. {
  145. struct usb_request *req;
  146. req = usb_ep_alloc_request(ep, kmalloc_flags);
  147. if (req != NULL) {
  148. req->length = len;
  149. req->buf = kmalloc(len, kmalloc_flags);
  150. if (req->buf == NULL) {
  151. usb_ep_free_request(ep, req);
  152. return NULL;
  153. }
  154. }
  155. return req;
  156. }
  157. EXPORT_SYMBOL_GPL(gs_alloc_req);
  158. /*
  159. * gs_free_req
  160. *
  161. * Free a usb_request and its buffer.
  162. */
  163. void gs_free_req(struct usb_ep *ep, struct usb_request *req)
  164. {
  165. kfree(req->buf);
  166. usb_ep_free_request(ep, req);
  167. }
  168. EXPORT_SYMBOL_GPL(gs_free_req);
  169. /*
  170. * gs_send_packet
  171. *
  172. * If there is data to send, a packet is built in the given
  173. * buffer and the size is returned. If there is no data to
  174. * send, 0 is returned.
  175. *
  176. * Called with port_lock held.
  177. */
  178. static unsigned
  179. gs_send_packet(struct gs_port *port, char *packet, unsigned size)
  180. {
  181. unsigned len;
  182. len = kfifo_len(&port->port_write_buf);
  183. if (len < size)
  184. size = len;
  185. if (size != 0)
  186. size = kfifo_out(&port->port_write_buf, packet, size);
  187. return size;
  188. }
  189. /*
  190. * gs_start_tx
  191. *
  192. * This function finds available write requests, calls
  193. * gs_send_packet to fill these packets with data, and
  194. * continues until either there are no more write requests
  195. * available or no more data to send. This function is
  196. * run whenever data arrives or write requests are available.
  197. *
  198. * Context: caller owns port_lock; port_usb is non-null.
  199. */
  200. static int gs_start_tx(struct gs_port *port)
  201. /*
  202. __releases(&port->port_lock)
  203. __acquires(&port->port_lock)
  204. */
  205. {
  206. struct list_head *pool = &port->write_pool;
  207. struct usb_ep *in;
  208. int status = 0;
  209. bool do_tty_wake = false;
  210. if (!port->port_usb)
  211. return status;
  212. in = port->port_usb->in;
  213. while (!port->write_busy && !list_empty(pool)) {
  214. struct usb_request *req;
  215. int len;
  216. if (port->write_started >= QUEUE_SIZE)
  217. break;
  218. req = list_entry(pool->next, struct usb_request, list);
  219. len = gs_send_packet(port, req->buf, in->maxpacket);
  220. if (len == 0) {
  221. wake_up_interruptible(&port->drain_wait);
  222. break;
  223. }
  224. do_tty_wake = true;
  225. req->length = len;
  226. list_del(&req->list);
  227. req->zero = kfifo_is_empty(&port->port_write_buf);
  228. pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf);
  229. /* Drop lock while we call out of driver; completions
  230. * could be issued while we do so. Disconnection may
  231. * happen too; maybe immediately before we queue this!
  232. *
  233. * NOTE that we may keep sending data for a while after
  234. * the TTY closed (dev->ioport->port_tty is NULL).
  235. */
  236. port->write_busy = true;
  237. spin_unlock(&port->port_lock);
  238. status = usb_ep_queue(in, req, GFP_ATOMIC);
  239. spin_lock(&port->port_lock);
  240. port->write_busy = false;
  241. if (status) {
  242. pr_debug("%s: %s %s err %d\n",
  243. __func__, "queue", in->name, status);
  244. list_add(&req->list, pool);
  245. break;
  246. }
  247. port->write_started++;
  248. /* abort immediately after disconnect */
  249. if (!port->port_usb)
  250. break;
  251. }
  252. if (do_tty_wake && port->port.tty)
  253. tty_wakeup(port->port.tty);
  254. return status;
  255. }
  256. /*
  257. * Context: caller owns port_lock, and port_usb is set
  258. */
  259. static unsigned gs_start_rx(struct gs_port *port)
  260. /*
  261. __releases(&port->port_lock)
  262. __acquires(&port->port_lock)
  263. */
  264. {
  265. struct list_head *pool = &port->read_pool;
  266. struct usb_ep *out = port->port_usb->out;
  267. while (!list_empty(pool)) {
  268. struct usb_request *req;
  269. int status;
  270. struct tty_struct *tty;
  271. /* no more rx if closed */
  272. tty = port->port.tty;
  273. if (!tty)
  274. break;
  275. if (port->read_started >= QUEUE_SIZE)
  276. break;
  277. req = list_entry(pool->next, struct usb_request, list);
  278. list_del(&req->list);
  279. req->length = out->maxpacket;
  280. /* drop lock while we call out; the controller driver
  281. * may need to call us back (e.g. for disconnect)
  282. */
  283. spin_unlock(&port->port_lock);
  284. status = usb_ep_queue(out, req, GFP_ATOMIC);
  285. spin_lock(&port->port_lock);
  286. if (status) {
  287. pr_debug("%s: %s %s err %d\n",
  288. __func__, "queue", out->name, status);
  289. list_add(&req->list, pool);
  290. break;
  291. }
  292. port->read_started++;
  293. /* abort immediately after disconnect */
  294. if (!port->port_usb)
  295. break;
  296. }
  297. return port->read_started;
  298. }
  299. /*
  300. * RX work takes data out of the RX queue and hands it up to the TTY
  301. * layer until it refuses to take any more data (or is throttled back).
  302. * Then it issues reads for any further data.
  303. *
  304. * If the RX queue becomes full enough that no usb_request is queued,
  305. * the OUT endpoint may begin NAKing as soon as its FIFO fills up.
  306. * So QUEUE_SIZE packets plus however many the FIFO holds (usually two)
  307. * can be buffered before the TTY layer's buffers (currently 64 KB).
  308. */
  309. static void gs_rx_push(struct work_struct *work)
  310. {
  311. struct delayed_work *w = to_delayed_work(work);
  312. struct gs_port *port = container_of(w, struct gs_port, push);
  313. struct tty_struct *tty;
  314. struct list_head *queue = &port->read_queue;
  315. bool disconnect = false;
  316. bool do_push = false;
  317. /* hand any queued data to the tty */
  318. spin_lock_irq(&port->port_lock);
  319. tty = port->port.tty;
  320. while (!list_empty(queue)) {
  321. struct usb_request *req;
  322. req = list_first_entry(queue, struct usb_request, list);
  323. /* leave data queued if tty was rx throttled */
  324. if (tty && tty_throttled(tty))
  325. break;
  326. switch (req->status) {
  327. case -ESHUTDOWN:
  328. disconnect = true;
  329. pr_vdebug("ttyGS%d: shutdown\n", port->port_num);
  330. break;
  331. default:
  332. /* presumably a transient fault */
  333. pr_debug("ttyGS%d: unexpected RX status %d\n",
  334. port->port_num, req->status);
  335. fallthrough;
  336. case 0:
  337. /* normal completion */
  338. break;
  339. }
  340. /* push data to (open) tty */
  341. if (req->actual && tty) {
  342. char *packet = req->buf;
  343. unsigned size = req->actual;
  344. unsigned n;
  345. int count;
  346. /* we may have pushed part of this packet already... */
  347. n = port->n_read;
  348. if (n) {
  349. packet += n;
  350. size -= n;
  351. }
  352. count = tty_insert_flip_string(&port->port, packet,
  353. size);
  354. if (count)
  355. do_push = true;
  356. if (count != size) {
  357. /* stop pushing; TTY layer can't handle more */
  358. port->n_read += count;
  359. pr_vdebug("ttyGS%d: rx block %d/%d\n",
  360. port->port_num, count, req->actual);
  361. break;
  362. }
  363. port->n_read = 0;
  364. }
  365. list_move(&req->list, &port->read_pool);
  366. port->read_started--;
  367. }
  368. /* Push from tty to ldisc; this is handled by a workqueue,
  369. * so we won't get callbacks and can hold port_lock
  370. */
  371. if (do_push)
  372. tty_flip_buffer_push(&port->port);
  373. /* We want our data queue to become empty ASAP, keeping data
  374. * in the tty and ldisc (not here). If we couldn't push any
  375. * this time around, RX may be starved, so wait until next jiffy.
  376. *
  377. * We may leave non-empty queue only when there is a tty, and
  378. * either it is throttled or there is no more room in flip buffer.
  379. */
  380. if (!list_empty(queue) && !tty_throttled(tty))
  381. schedule_delayed_work(&port->push, 1);
  382. /* If we're still connected, refill the USB RX queue. */
  383. if (!disconnect && port->port_usb)
  384. gs_start_rx(port);
  385. spin_unlock_irq(&port->port_lock);
  386. }
  387. static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
  388. {
  389. struct gs_port *port = ep->driver_data;
  390. /* Queue all received data until the tty layer is ready for it. */
  391. spin_lock(&port->port_lock);
  392. list_add_tail(&req->list, &port->read_queue);
  393. schedule_delayed_work(&port->push, 0);
  394. spin_unlock(&port->port_lock);
  395. }
  396. static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
  397. {
  398. struct gs_port *port = ep->driver_data;
  399. spin_lock(&port->port_lock);
  400. list_add(&req->list, &port->write_pool);
  401. port->write_started--;
  402. switch (req->status) {
  403. default:
  404. /* presumably a transient fault */
  405. pr_warn("%s: unexpected %s status %d\n",
  406. __func__, ep->name, req->status);
  407. fallthrough;
  408. case 0:
  409. /* normal completion */
  410. gs_start_tx(port);
  411. break;
  412. case -ESHUTDOWN:
  413. /* disconnect */
  414. pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
  415. break;
  416. }
  417. spin_unlock(&port->port_lock);
  418. }
  419. static void gs_free_requests(struct usb_ep *ep, struct list_head *head,
  420. int *allocated)
  421. {
  422. struct usb_request *req;
  423. while (!list_empty(head)) {
  424. req = list_entry(head->next, struct usb_request, list);
  425. list_del(&req->list);
  426. gs_free_req(ep, req);
  427. if (allocated)
  428. (*allocated)--;
  429. }
  430. }
  431. static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head,
  432. void (*fn)(struct usb_ep *, struct usb_request *),
  433. int *allocated)
  434. {
  435. int i;
  436. struct usb_request *req;
  437. int n = allocated ? QUEUE_SIZE - *allocated : QUEUE_SIZE;
  438. /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't
  439. * do quite that many this time, don't fail ... we just won't
  440. * be as speedy as we might otherwise be.
  441. */
  442. for (i = 0; i < n; i++) {
  443. req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
  444. if (!req)
  445. return list_empty(head) ? -ENOMEM : 0;
  446. req->complete = fn;
  447. list_add_tail(&req->list, head);
  448. if (allocated)
  449. (*allocated)++;
  450. }
  451. return 0;
  452. }
  453. /**
  454. * gs_start_io - start USB I/O streams
  455. * @port: port to use
  456. * Context: holding port_lock; port_tty and port_usb are non-null
  457. *
  458. * We only start I/O when something is connected to both sides of
  459. * this port. If nothing is listening on the host side, we may
  460. * be pointlessly filling up our TX buffers and FIFO.
  461. */
  462. static int gs_start_io(struct gs_port *port)
  463. {
  464. struct list_head *head = &port->read_pool;
  465. struct usb_ep *ep;
  466. int status;
  467. unsigned started;
  468. if (!port->port_usb || !port->port.tty)
  469. return -EIO;
  470. /* Allocate RX and TX I/O buffers. We can't easily do this much
  471. * earlier (with GFP_KERNEL) because the requests are coupled to
  472. * endpoints, as are the packet sizes we'll be using. Different
  473. * configurations may use different endpoints with a given port;
  474. * and high speed vs full speed changes packet sizes too.
  475. */
  476. ep = port->port_usb->out;
  477. status = gs_alloc_requests(ep, head, gs_read_complete,
  478. &port->read_allocated);
  479. if (status)
  480. return status;
  481. status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
  482. gs_write_complete, &port->write_allocated);
  483. if (status) {
  484. gs_free_requests(ep, head, &port->read_allocated);
  485. return status;
  486. }
  487. /* queue read requests */
  488. port->n_read = 0;
  489. started = gs_start_rx(port);
  490. if (started && port->port.tty) {
  491. gs_start_tx(port);
  492. /* Unblock any pending writes into our circular buffer, in case
  493. * we didn't in gs_start_tx() */
  494. tty_wakeup(port->port.tty);
  495. } else {
  496. gs_free_requests(ep, head, &port->read_allocated);
  497. gs_free_requests(port->port_usb->in, &port->write_pool,
  498. &port->write_allocated);
  499. status = -EIO;
  500. }
  501. return status;
  502. }
  503. /*-------------------------------------------------------------------------*/
  504. /* TTY Driver */
  505. /*
  506. * gs_open sets up the link between a gs_port and its associated TTY.
  507. * That link is broken *only* by TTY close(), and all driver methods
  508. * know that.
  509. */
  510. static int gs_open(struct tty_struct *tty, struct file *file)
  511. {
  512. int port_num = tty->index;
  513. struct gs_port *port;
  514. int status = 0;
  515. mutex_lock(&ports[port_num].lock);
  516. port = ports[port_num].port;
  517. if (!port) {
  518. status = -ENODEV;
  519. goto out;
  520. }
  521. spin_lock_irq(&port->port_lock);
  522. /* allocate circular buffer on first open */
  523. if (!kfifo_initialized(&port->port_write_buf)) {
  524. spin_unlock_irq(&port->port_lock);
  525. /*
  526. * portmaster's mutex still protects from simultaneous open(),
  527. * and close() can't happen, yet.
  528. */
  529. status = kfifo_alloc(&port->port_write_buf,
  530. WRITE_BUF_SIZE, GFP_KERNEL);
  531. if (status) {
  532. pr_info("gs_open: ttyGS%d (%p,%p) no buffer\n",
  533. port_num, tty, file);
  534. goto out;
  535. }
  536. spin_lock_irq(&port->port_lock);
  537. }
  538. /* already open? Great. */
  539. if (port->port.count++)
  540. goto exit_unlock_port;
  541. tty->driver_data = port;
  542. port->port.tty = tty;
  543. /* if connected, start the I/O stream */
  544. if (port->port_usb) {
  545. /* if port is suspended, wait resume to start I/0 stream */
  546. if (!port->suspended) {
  547. struct gserial *gser = port->port_usb;
  548. pr_info("gs_open: start ttyGS%d\n", port->port_num);
  549. gs_start_io(port);
  550. if (gser->connect)
  551. gser->connect(gser);
  552. } else {
  553. pr_info("delay start of ttyGS%d\n", port->port_num);
  554. port->start_delayed = true;
  555. }
  556. }
  557. pr_info("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
  558. exit_unlock_port:
  559. spin_unlock_irq(&port->port_lock);
  560. out:
  561. mutex_unlock(&ports[port_num].lock);
  562. return status;
  563. }
  564. static int gs_close_flush_done(struct gs_port *p)
  565. {
  566. int cond;
  567. /* return true on disconnect or empty buffer or if raced with open() */
  568. spin_lock_irq(&p->port_lock);
  569. cond = p->port_usb == NULL || !kfifo_len(&p->port_write_buf) ||
  570. p->port.count > 1;
  571. spin_unlock_irq(&p->port_lock);
  572. return cond;
  573. }
  574. static void gs_close(struct tty_struct *tty, struct file *file)
  575. {
  576. struct gs_port *port = tty->driver_data;
  577. struct gserial *gser;
  578. spin_lock_irq(&port->port_lock);
  579. if (port->port.count != 1) {
  580. raced_with_open:
  581. if (port->port.count == 0)
  582. WARN_ON(1);
  583. else
  584. --port->port.count;
  585. goto exit;
  586. }
  587. pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
  588. gser = port->port_usb;
  589. if (gser && !port->suspended && gser->disconnect)
  590. gser->disconnect(gser);
  591. /* wait for circular write buffer to drain, disconnect, or at
  592. * most GS_CLOSE_TIMEOUT seconds; then discard the rest
  593. */
  594. if (kfifo_len(&port->port_write_buf) > 0 && gser) {
  595. spin_unlock_irq(&port->port_lock);
  596. wait_event_interruptible_timeout(port->drain_wait,
  597. gs_close_flush_done(port),
  598. GS_CLOSE_TIMEOUT * HZ);
  599. spin_lock_irq(&port->port_lock);
  600. if (port->port.count != 1)
  601. goto raced_with_open;
  602. gser = port->port_usb;
  603. }
  604. /* Iff we're disconnected, there can be no I/O in flight so it's
  605. * ok to free the circular buffer; else just scrub it. And don't
  606. * let the push async work fire again until we're re-opened.
  607. */
  608. if (gser == NULL)
  609. kfifo_free(&port->port_write_buf);
  610. else
  611. kfifo_reset(&port->port_write_buf);
  612. port->start_delayed = false;
  613. port->port.count = 0;
  614. port->port.tty = NULL;
  615. pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
  616. port->port_num, tty, file);
  617. wake_up(&port->close_wait);
  618. exit:
  619. spin_unlock_irq(&port->port_lock);
  620. }
  621. static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
  622. {
  623. struct gs_port *port = tty->driver_data;
  624. unsigned long flags;
  625. pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n",
  626. port->port_num, tty, count);
  627. spin_lock_irqsave(&port->port_lock, flags);
  628. if (count)
  629. count = kfifo_in(&port->port_write_buf, buf, count);
  630. /* treat count == 0 as flush_chars() */
  631. if (port->port_usb)
  632. gs_start_tx(port);
  633. spin_unlock_irqrestore(&port->port_lock, flags);
  634. return count;
  635. }
  636. static int gs_put_char(struct tty_struct *tty, unsigned char ch)
  637. {
  638. struct gs_port *port = tty->driver_data;
  639. unsigned long flags;
  640. int status;
  641. pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %ps\n",
  642. port->port_num, tty, ch, __builtin_return_address(0));
  643. spin_lock_irqsave(&port->port_lock, flags);
  644. status = kfifo_put(&port->port_write_buf, ch);
  645. spin_unlock_irqrestore(&port->port_lock, flags);
  646. return status;
  647. }
  648. static void gs_flush_chars(struct tty_struct *tty)
  649. {
  650. struct gs_port *port = tty->driver_data;
  651. unsigned long flags;
  652. pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
  653. spin_lock_irqsave(&port->port_lock, flags);
  654. if (port->port_usb)
  655. gs_start_tx(port);
  656. spin_unlock_irqrestore(&port->port_lock, flags);
  657. }
  658. static unsigned int gs_write_room(struct tty_struct *tty)
  659. {
  660. struct gs_port *port = tty->driver_data;
  661. unsigned long flags;
  662. unsigned int room = 0;
  663. spin_lock_irqsave(&port->port_lock, flags);
  664. if (port->port_usb)
  665. room = kfifo_avail(&port->port_write_buf);
  666. spin_unlock_irqrestore(&port->port_lock, flags);
  667. pr_vdebug("gs_write_room: (%d,%p) room=%u\n",
  668. port->port_num, tty, room);
  669. return room;
  670. }
  671. static unsigned int gs_chars_in_buffer(struct tty_struct *tty)
  672. {
  673. struct gs_port *port = tty->driver_data;
  674. unsigned long flags;
  675. unsigned int chars;
  676. spin_lock_irqsave(&port->port_lock, flags);
  677. chars = kfifo_len(&port->port_write_buf);
  678. spin_unlock_irqrestore(&port->port_lock, flags);
  679. pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%u\n",
  680. port->port_num, tty, chars);
  681. return chars;
  682. }
  683. /* undo side effects of setting TTY_THROTTLED */
  684. static void gs_unthrottle(struct tty_struct *tty)
  685. {
  686. struct gs_port *port = tty->driver_data;
  687. unsigned long flags;
  688. spin_lock_irqsave(&port->port_lock, flags);
  689. if (port->port_usb) {
  690. /* Kickstart read queue processing. We don't do xon/xoff,
  691. * rts/cts, or other handshaking with the host, but if the
  692. * read queue backs up enough we'll be NAKing OUT packets.
  693. */
  694. pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
  695. schedule_delayed_work(&port->push, 0);
  696. }
  697. spin_unlock_irqrestore(&port->port_lock, flags);
  698. }
  699. static int gs_break_ctl(struct tty_struct *tty, int duration)
  700. {
  701. struct gs_port *port = tty->driver_data;
  702. int status = 0;
  703. struct gserial *gser;
  704. pr_vdebug("gs_break_ctl: ttyGS%d, send break (%d) \n",
  705. port->port_num, duration);
  706. spin_lock_irq(&port->port_lock);
  707. gser = port->port_usb;
  708. if (gser && gser->send_break)
  709. status = gser->send_break(gser, duration);
  710. spin_unlock_irq(&port->port_lock);
  711. return status;
  712. }
  713. static const struct tty_operations gs_tty_ops = {
  714. .open = gs_open,
  715. .close = gs_close,
  716. .write = gs_write,
  717. .put_char = gs_put_char,
  718. .flush_chars = gs_flush_chars,
  719. .write_room = gs_write_room,
  720. .chars_in_buffer = gs_chars_in_buffer,
  721. .unthrottle = gs_unthrottle,
  722. .break_ctl = gs_break_ctl,
  723. };
  724. /*-------------------------------------------------------------------------*/
  725. static struct tty_driver *gs_tty_driver;
  726. #ifdef CONFIG_U_SERIAL_CONSOLE
  727. static void gs_console_complete_out(struct usb_ep *ep, struct usb_request *req)
  728. {
  729. struct gs_console *cons = req->context;
  730. switch (req->status) {
  731. default:
  732. pr_warn("%s: unexpected %s status %d\n",
  733. __func__, ep->name, req->status);
  734. fallthrough;
  735. case 0:
  736. /* normal completion */
  737. spin_lock(&cons->lock);
  738. req->length = 0;
  739. schedule_work(&cons->work);
  740. spin_unlock(&cons->lock);
  741. break;
  742. case -ECONNRESET:
  743. case -ESHUTDOWN:
  744. /* disconnect */
  745. pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
  746. break;
  747. }
  748. }
  749. static void __gs_console_push(struct gs_console *cons)
  750. {
  751. struct usb_request *req = cons->req;
  752. struct usb_ep *ep;
  753. size_t size;
  754. if (!req)
  755. return; /* disconnected */
  756. if (req->length)
  757. return; /* busy */
  758. ep = cons->console.data;
  759. size = kfifo_out(&cons->buf, req->buf, ep->maxpacket);
  760. if (!size)
  761. return;
  762. if (cons->missed && ep->maxpacket >= 64) {
  763. char buf[64];
  764. size_t len;
  765. len = sprintf(buf, "\n[missed %zu bytes]\n", cons->missed);
  766. kfifo_in(&cons->buf, buf, len);
  767. cons->missed = 0;
  768. }
  769. req->length = size;
  770. spin_unlock_irq(&cons->lock);
  771. if (usb_ep_queue(ep, req, GFP_ATOMIC))
  772. req->length = 0;
  773. spin_lock_irq(&cons->lock);
  774. }
  775. static void gs_console_work(struct work_struct *work)
  776. {
  777. struct gs_console *cons = container_of(work, struct gs_console, work);
  778. spin_lock_irq(&cons->lock);
  779. __gs_console_push(cons);
  780. spin_unlock_irq(&cons->lock);
  781. }
  782. static void gs_console_write(struct console *co,
  783. const char *buf, unsigned count)
  784. {
  785. struct gs_console *cons = container_of(co, struct gs_console, console);
  786. unsigned long flags;
  787. size_t n;
  788. spin_lock_irqsave(&cons->lock, flags);
  789. n = kfifo_in(&cons->buf, buf, count);
  790. if (n < count)
  791. cons->missed += count - n;
  792. if (cons->req && !cons->req->length)
  793. schedule_work(&cons->work);
  794. spin_unlock_irqrestore(&cons->lock, flags);
  795. }
  796. static struct tty_driver *gs_console_device(struct console *co, int *index)
  797. {
  798. *index = co->index;
  799. return gs_tty_driver;
  800. }
  801. static int gs_console_connect(struct gs_port *port)
  802. {
  803. struct gs_console *cons = port->console;
  804. struct usb_request *req;
  805. struct usb_ep *ep;
  806. if (!cons)
  807. return 0;
  808. ep = port->port_usb->in;
  809. req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
  810. if (!req)
  811. return -ENOMEM;
  812. req->complete = gs_console_complete_out;
  813. req->context = cons;
  814. req->length = 0;
  815. spin_lock(&cons->lock);
  816. cons->req = req;
  817. cons->console.data = ep;
  818. spin_unlock(&cons->lock);
  819. pr_debug("ttyGS%d: console connected!\n", port->port_num);
  820. schedule_work(&cons->work);
  821. return 0;
  822. }
  823. static void gs_console_disconnect(struct gs_port *port)
  824. {
  825. struct gs_console *cons = port->console;
  826. struct usb_request *req;
  827. struct usb_ep *ep;
  828. if (!cons)
  829. return;
  830. spin_lock(&cons->lock);
  831. req = cons->req;
  832. ep = cons->console.data;
  833. cons->req = NULL;
  834. spin_unlock(&cons->lock);
  835. if (!req)
  836. return;
  837. usb_ep_dequeue(ep, req);
  838. gs_free_req(ep, req);
  839. }
  840. static int gs_console_init(struct gs_port *port)
  841. {
  842. struct gs_console *cons;
  843. int err;
  844. if (port->console)
  845. return 0;
  846. cons = kzalloc(sizeof(*port->console), GFP_KERNEL);
  847. if (!cons)
  848. return -ENOMEM;
  849. strcpy(cons->console.name, "ttyGS");
  850. cons->console.write = gs_console_write;
  851. cons->console.device = gs_console_device;
  852. cons->console.flags = CON_PRINTBUFFER;
  853. cons->console.index = port->port_num;
  854. INIT_WORK(&cons->work, gs_console_work);
  855. spin_lock_init(&cons->lock);
  856. err = kfifo_alloc(&cons->buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL);
  857. if (err) {
  858. pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num);
  859. kfree(cons);
  860. return err;
  861. }
  862. port->console = cons;
  863. register_console(&cons->console);
  864. spin_lock_irq(&port->port_lock);
  865. if (port->port_usb)
  866. gs_console_connect(port);
  867. spin_unlock_irq(&port->port_lock);
  868. return 0;
  869. }
  870. static void gs_console_exit(struct gs_port *port)
  871. {
  872. struct gs_console *cons = port->console;
  873. if (!cons)
  874. return;
  875. unregister_console(&cons->console);
  876. spin_lock_irq(&port->port_lock);
  877. if (cons->req)
  878. gs_console_disconnect(port);
  879. spin_unlock_irq(&port->port_lock);
  880. cancel_work_sync(&cons->work);
  881. kfifo_free(&cons->buf);
  882. kfree(cons);
  883. port->console = NULL;
  884. }
  885. ssize_t gserial_set_console(unsigned char port_num, const char *page, size_t count)
  886. {
  887. struct gs_port *port;
  888. bool enable;
  889. int ret;
  890. ret = strtobool(page, &enable);
  891. if (ret)
  892. return ret;
  893. mutex_lock(&ports[port_num].lock);
  894. port = ports[port_num].port;
  895. if (WARN_ON(port == NULL)) {
  896. ret = -ENXIO;
  897. goto out;
  898. }
  899. if (enable)
  900. ret = gs_console_init(port);
  901. else
  902. gs_console_exit(port);
  903. out:
  904. mutex_unlock(&ports[port_num].lock);
  905. return ret < 0 ? ret : count;
  906. }
  907. EXPORT_SYMBOL_GPL(gserial_set_console);
  908. ssize_t gserial_get_console(unsigned char port_num, char *page)
  909. {
  910. struct gs_port *port;
  911. ssize_t ret;
  912. mutex_lock(&ports[port_num].lock);
  913. port = ports[port_num].port;
  914. if (WARN_ON(port == NULL))
  915. ret = -ENXIO;
  916. else
  917. ret = sprintf(page, "%u\n", !!port->console);
  918. mutex_unlock(&ports[port_num].lock);
  919. return ret;
  920. }
  921. EXPORT_SYMBOL_GPL(gserial_get_console);
  922. #else
  923. static int gs_console_connect(struct gs_port *port)
  924. {
  925. return 0;
  926. }
  927. static void gs_console_disconnect(struct gs_port *port)
  928. {
  929. }
  930. static int gs_console_init(struct gs_port *port)
  931. {
  932. return -ENOSYS;
  933. }
  934. static void gs_console_exit(struct gs_port *port)
  935. {
  936. }
  937. #endif
  938. static int
  939. gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
  940. {
  941. struct gs_port *port;
  942. int ret = 0;
  943. mutex_lock(&ports[port_num].lock);
  944. if (ports[port_num].port) {
  945. ret = -EBUSY;
  946. goto out;
  947. }
  948. port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
  949. if (port == NULL) {
  950. ret = -ENOMEM;
  951. goto out;
  952. }
  953. tty_port_init(&port->port);
  954. spin_lock_init(&port->port_lock);
  955. init_waitqueue_head(&port->drain_wait);
  956. init_waitqueue_head(&port->close_wait);
  957. INIT_DELAYED_WORK(&port->push, gs_rx_push);
  958. INIT_LIST_HEAD(&port->read_pool);
  959. INIT_LIST_HEAD(&port->read_queue);
  960. INIT_LIST_HEAD(&port->write_pool);
  961. port->port_num = port_num;
  962. port->port_line_coding = *coding;
  963. ports[port_num].port = port;
  964. out:
  965. mutex_unlock(&ports[port_num].lock);
  966. return ret;
  967. }
  968. static int gs_closed(struct gs_port *port)
  969. {
  970. int cond;
  971. spin_lock_irq(&port->port_lock);
  972. cond = port->port.count == 0;
  973. spin_unlock_irq(&port->port_lock);
  974. return cond;
  975. }
  976. static void gserial_free_port(struct gs_port *port)
  977. {
  978. cancel_delayed_work_sync(&port->push);
  979. /* wait for old opens to finish */
  980. wait_event(port->close_wait, gs_closed(port));
  981. WARN_ON(port->port_usb != NULL);
  982. tty_port_destroy(&port->port);
  983. kfree(port);
  984. }
  985. void gserial_free_line(unsigned char port_num)
  986. {
  987. struct gs_port *port;
  988. mutex_lock(&ports[port_num].lock);
  989. if (!ports[port_num].port) {
  990. mutex_unlock(&ports[port_num].lock);
  991. return;
  992. }
  993. port = ports[port_num].port;
  994. gs_console_exit(port);
  995. ports[port_num].port = NULL;
  996. mutex_unlock(&ports[port_num].lock);
  997. gserial_free_port(port);
  998. tty_unregister_device(gs_tty_driver, port_num);
  999. }
  1000. EXPORT_SYMBOL_GPL(gserial_free_line);
  1001. int gserial_alloc_line_no_console(unsigned char *line_num)
  1002. {
  1003. struct usb_cdc_line_coding coding;
  1004. struct gs_port *port;
  1005. struct device *tty_dev;
  1006. int ret;
  1007. int port_num;
  1008. coding.dwDTERate = cpu_to_le32(9600);
  1009. coding.bCharFormat = 8;
  1010. coding.bParityType = USB_CDC_NO_PARITY;
  1011. coding.bDataBits = USB_CDC_1_STOP_BITS;
  1012. for (port_num = 0; port_num < MAX_U_SERIAL_PORTS; port_num++) {
  1013. ret = gs_port_alloc(port_num, &coding);
  1014. if (ret == -EBUSY)
  1015. continue;
  1016. if (ret)
  1017. return ret;
  1018. break;
  1019. }
  1020. if (ret)
  1021. return ret;
  1022. /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
  1023. port = ports[port_num].port;
  1024. tty_dev = tty_port_register_device(&port->port,
  1025. gs_tty_driver, port_num, NULL);
  1026. if (IS_ERR(tty_dev)) {
  1027. pr_err("%s: failed to register tty for port %d, err %ld\n",
  1028. __func__, port_num, PTR_ERR(tty_dev));
  1029. ret = PTR_ERR(tty_dev);
  1030. mutex_lock(&ports[port_num].lock);
  1031. ports[port_num].port = NULL;
  1032. mutex_unlock(&ports[port_num].lock);
  1033. gserial_free_port(port);
  1034. goto err;
  1035. }
  1036. *line_num = port_num;
  1037. err:
  1038. return ret;
  1039. }
  1040. EXPORT_SYMBOL_GPL(gserial_alloc_line_no_console);
  1041. int gserial_alloc_line(unsigned char *line_num)
  1042. {
  1043. int ret = gserial_alloc_line_no_console(line_num);
  1044. if (!ret && !*line_num)
  1045. gs_console_init(ports[*line_num].port);
  1046. return ret;
  1047. }
  1048. EXPORT_SYMBOL_GPL(gserial_alloc_line);
  1049. /**
  1050. * gserial_connect - notify TTY I/O glue that USB link is active
  1051. * @gser: the function, set up with endpoints and descriptors
  1052. * @port_num: which port is active
  1053. * Context: any (usually from irq)
  1054. *
  1055. * This is called activate endpoints and let the TTY layer know that
  1056. * the connection is active ... not unlike "carrier detect". It won't
  1057. * necessarily start I/O queues; unless the TTY is held open by any
  1058. * task, there would be no point. However, the endpoints will be
  1059. * activated so the USB host can perform I/O, subject to basic USB
  1060. * hardware flow control.
  1061. *
  1062. * Caller needs to have set up the endpoints and USB function in @dev
  1063. * before calling this, as well as the appropriate (speed-specific)
  1064. * endpoint descriptors, and also have allocate @port_num by calling
  1065. * @gserial_alloc_line().
  1066. *
  1067. * Returns negative errno or zero.
  1068. * On success, ep->driver_data will be overwritten.
  1069. */
  1070. int gserial_connect(struct gserial *gser, u8 port_num)
  1071. {
  1072. struct gs_port *port;
  1073. unsigned long flags;
  1074. int status;
  1075. pr_info("%s +++\n", __func__);
  1076. if (port_num >= MAX_U_SERIAL_PORTS)
  1077. return -ENXIO;
  1078. port = ports[port_num].port;
  1079. if (!port) {
  1080. pr_err("serial line %d not allocated.\n", port_num);
  1081. return -EINVAL;
  1082. }
  1083. if (port->port_usb) {
  1084. pr_err("serial line %d is in use.\n", port_num);
  1085. return -EBUSY;
  1086. }
  1087. /* activate the endpoints */
  1088. status = usb_ep_enable(gser->in);
  1089. if (status < 0)
  1090. return status;
  1091. gser->in->driver_data = port;
  1092. status = usb_ep_enable(gser->out);
  1093. if (status < 0)
  1094. goto fail_out;
  1095. gser->out->driver_data = port;
  1096. /* then tell the tty glue that I/O can work */
  1097. spin_lock_irqsave(&port->port_lock, flags);
  1098. gser->ioport = port;
  1099. port->port_usb = gser;
  1100. /* REVISIT unclear how best to handle this state...
  1101. * we don't really couple it with the Linux TTY.
  1102. */
  1103. gser->port_line_coding = port->port_line_coding;
  1104. /* REVISIT if waiting on "carrier detect", signal. */
  1105. /* if it's already open, start I/O ... and notify the serial
  1106. * protocol about open/close status (connect/disconnect).
  1107. */
  1108. if (port->port.count) {
  1109. pr_info("gserial_connect: start ttyGS%d\n", port->port_num);
  1110. gs_start_io(port);
  1111. if (gser->connect)
  1112. gser->connect(gser);
  1113. } else {
  1114. if (gser->disconnect)
  1115. gser->disconnect(gser);
  1116. }
  1117. status = gs_console_connect(port);
  1118. spin_unlock_irqrestore(&port->port_lock, flags);
  1119. return status;
  1120. fail_out:
  1121. usb_ep_disable(gser->in);
  1122. return status;
  1123. }
  1124. EXPORT_SYMBOL_GPL(gserial_connect);
  1125. /**
  1126. * gserial_disconnect - notify TTY I/O glue that USB link is inactive
  1127. * @gser: the function, on which gserial_connect() was called
  1128. * Context: any (usually from irq)
  1129. *
  1130. * This is called to deactivate endpoints and let the TTY layer know
  1131. * that the connection went inactive ... not unlike "hangup".
  1132. *
  1133. * On return, the state is as if gserial_connect() had never been called;
  1134. * there is no active USB I/O on these endpoints.
  1135. */
  1136. void gserial_disconnect(struct gserial *gser)
  1137. {
  1138. struct gs_port *port = gser->ioport;
  1139. unsigned long flags;
  1140. pr_info("%s +++ \n", __func__);
  1141. if (!port) {
  1142. pr_err("%s: port is NULL\n", __func__);
  1143. return;
  1144. }
  1145. spin_lock_irqsave(&serial_port_lock, flags);
  1146. /* tell the TTY glue not to do I/O here any more */
  1147. spin_lock(&port->port_lock);
  1148. gs_console_disconnect(port);
  1149. /* REVISIT as above: how best to track this? */
  1150. port->port_line_coding = gser->port_line_coding;
  1151. port->port_usb = NULL;
  1152. gser->ioport = NULL;
  1153. if (port->port.count > 0) {
  1154. wake_up_interruptible(&port->drain_wait);
  1155. if (port->port.tty)
  1156. tty_hangup(port->port.tty);
  1157. }
  1158. port->suspended = false;
  1159. spin_unlock(&port->port_lock);
  1160. spin_unlock_irqrestore(&serial_port_lock, flags);
  1161. /* disable endpoints, aborting down any active I/O */
  1162. usb_ep_disable(gser->out);
  1163. usb_ep_disable(gser->in);
  1164. /* finally, free any unused/unusable I/O buffers */
  1165. spin_lock_irqsave(&port->port_lock, flags);
  1166. if (port->port.count == 0)
  1167. kfifo_free(&port->port_write_buf);
  1168. gs_free_requests(gser->out, &port->read_pool, NULL);
  1169. gs_free_requests(gser->out, &port->read_queue, NULL);
  1170. gs_free_requests(gser->in, &port->write_pool, NULL);
  1171. port->read_allocated = port->read_started =
  1172. port->write_allocated = port->write_started = 0;
  1173. spin_unlock_irqrestore(&port->port_lock, flags);
  1174. }
  1175. EXPORT_SYMBOL_GPL(gserial_disconnect);
  1176. void gserial_suspend(struct gserial *gser)
  1177. {
  1178. struct gs_port *port;
  1179. unsigned long flags;
  1180. spin_lock_irqsave(&serial_port_lock, flags);
  1181. port = gser->ioport;
  1182. if (!port) {
  1183. pr_err("%s: port is NULL\n", __func__);
  1184. spin_unlock_irqrestore(&serial_port_lock, flags);
  1185. return;
  1186. }
  1187. spin_lock(&port->port_lock);
  1188. spin_unlock(&serial_port_lock);
  1189. port->suspended = true;
  1190. spin_unlock_irqrestore(&port->port_lock, flags);
  1191. }
  1192. EXPORT_SYMBOL_GPL(gserial_suspend);
  1193. void gserial_resume(struct gserial *gser)
  1194. {
  1195. struct gs_port *port;
  1196. unsigned long flags;
  1197. spin_lock_irqsave(&serial_port_lock, flags);
  1198. port = gser->ioport;
  1199. if (!port) {
  1200. spin_unlock_irqrestore(&serial_port_lock, flags);
  1201. return;
  1202. }
  1203. spin_lock(&port->port_lock);
  1204. spin_unlock(&serial_port_lock);
  1205. port->suspended = false;
  1206. if (!port->start_delayed) {
  1207. spin_unlock_irqrestore(&port->port_lock, flags);
  1208. return;
  1209. }
  1210. pr_debug("delayed start ttyGS%d\n", port->port_num);
  1211. gs_start_io(port);
  1212. if (gser->connect)
  1213. gser->connect(gser);
  1214. port->start_delayed = false;
  1215. spin_unlock_irqrestore(&port->port_lock, flags);
  1216. }
  1217. EXPORT_SYMBOL_GPL(gserial_resume);
  1218. static int __init userial_init(void)
  1219. {
  1220. struct tty_driver *driver;
  1221. unsigned i;
  1222. int status;
  1223. driver = tty_alloc_driver(MAX_U_SERIAL_PORTS, TTY_DRIVER_REAL_RAW |
  1224. TTY_DRIVER_DYNAMIC_DEV);
  1225. if (IS_ERR(driver))
  1226. return PTR_ERR(driver);
  1227. driver->driver_name = "g_serial";
  1228. driver->name = "ttyGS";
  1229. /* uses dynamically assigned dev_t values */
  1230. driver->type = TTY_DRIVER_TYPE_SERIAL;
  1231. driver->subtype = SERIAL_TYPE_NORMAL;
  1232. driver->init_termios = tty_std_termios;
  1233. /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on
  1234. * MS-Windows. Otherwise, most of these flags shouldn't affect
  1235. * anything unless we were to actually hook up to a serial line.
  1236. */
  1237. driver->init_termios.c_cflag =
  1238. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1239. driver->init_termios.c_ispeed = 9600;
  1240. driver->init_termios.c_ospeed = 9600;
  1241. tty_set_operations(driver, &gs_tty_ops);
  1242. for (i = 0; i < MAX_U_SERIAL_PORTS; i++)
  1243. mutex_init(&ports[i].lock);
  1244. /* export the driver ... */
  1245. status = tty_register_driver(driver);
  1246. if (status) {
  1247. pr_err("%s: cannot register, err %d\n",
  1248. __func__, status);
  1249. goto fail;
  1250. }
  1251. gs_tty_driver = driver;
  1252. pr_debug("%s: registered %d ttyGS* device%s\n", __func__,
  1253. MAX_U_SERIAL_PORTS,
  1254. (MAX_U_SERIAL_PORTS == 1) ? "" : "s");
  1255. return status;
  1256. fail:
  1257. tty_driver_kref_put(driver);
  1258. return status;
  1259. }
  1260. module_init(userial_init);
  1261. static void __exit userial_cleanup(void)
  1262. {
  1263. tty_unregister_driver(gs_tty_driver);
  1264. tty_driver_kref_put(gs_tty_driver);
  1265. gs_tty_driver = NULL;
  1266. }
  1267. module_exit(userial_cleanup);
  1268. MODULE_LICENSE("GPL");