u_serial.c 39 KB

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