u_serial.c 37 KB

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