u_serial.c 36 KB

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