u_serial.c 36 KB

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