u_serial.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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 = port->port_usb->in;
  331. int status = 0;
  332. bool do_tty_wake = false;
  333. while (!port->write_busy && !list_empty(pool)) {
  334. struct usb_request *req;
  335. int len;
  336. if (port->write_started >= QUEUE_SIZE)
  337. break;
  338. req = list_entry(pool->next, struct usb_request, list);
  339. len = gs_send_packet(port, req->buf, in->maxpacket);
  340. if (len == 0) {
  341. wake_up_interruptible(&port->drain_wait);
  342. break;
  343. }
  344. do_tty_wake = true;
  345. req->length = len;
  346. list_del(&req->list);
  347. req->zero = (gs_buf_data_avail(&port->port_write_buf) == 0);
  348. pr_vdebug("ttyGS%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n",
  349. port->port_num, len, *((u8 *)req->buf),
  350. *((u8 *)req->buf+1), *((u8 *)req->buf+2));
  351. /* Drop lock while we call out of driver; completions
  352. * could be issued while we do so. Disconnection may
  353. * happen too; maybe immediately before we queue this!
  354. *
  355. * NOTE that we may keep sending data for a while after
  356. * the TTY closed (dev->ioport->port_tty is NULL).
  357. */
  358. port->write_busy = true;
  359. spin_unlock(&port->port_lock);
  360. status = usb_ep_queue(in, req, GFP_ATOMIC);
  361. spin_lock(&port->port_lock);
  362. port->write_busy = false;
  363. if (status) {
  364. pr_debug("%s: %s %s err %d\n",
  365. __func__, "queue", in->name, status);
  366. list_add(&req->list, pool);
  367. break;
  368. }
  369. port->write_started++;
  370. /* abort immediately after disconnect */
  371. if (!port->port_usb)
  372. break;
  373. }
  374. if (do_tty_wake && port->port.tty)
  375. tty_wakeup(port->port.tty);
  376. return status;
  377. }
  378. /*
  379. * Context: caller owns port_lock, and port_usb is set
  380. */
  381. static unsigned gs_start_rx(struct gs_port *port)
  382. /*
  383. __releases(&port->port_lock)
  384. __acquires(&port->port_lock)
  385. */
  386. {
  387. struct list_head *pool = &port->read_pool;
  388. struct usb_ep *out = port->port_usb->out;
  389. while (!list_empty(pool)) {
  390. struct usb_request *req;
  391. int status;
  392. struct tty_struct *tty;
  393. /* no more rx if closed */
  394. tty = port->port.tty;
  395. if (!tty)
  396. break;
  397. if (port->read_started >= QUEUE_SIZE)
  398. break;
  399. req = list_entry(pool->next, struct usb_request, list);
  400. list_del(&req->list);
  401. req->length = out->maxpacket;
  402. /* drop lock while we call out; the controller driver
  403. * may need to call us back (e.g. for disconnect)
  404. */
  405. spin_unlock(&port->port_lock);
  406. status = usb_ep_queue(out, req, GFP_ATOMIC);
  407. spin_lock(&port->port_lock);
  408. if (status) {
  409. pr_debug("%s: %s %s err %d\n",
  410. __func__, "queue", out->name, status);
  411. list_add(&req->list, pool);
  412. break;
  413. }
  414. port->read_started++;
  415. /* abort immediately after disconnect */
  416. if (!port->port_usb)
  417. break;
  418. }
  419. return port->read_started;
  420. }
  421. /*
  422. * RX tasklet takes data out of the RX queue and hands it up to the TTY
  423. * layer until it refuses to take any more data (or is throttled back).
  424. * Then it issues reads for any further data.
  425. *
  426. * If the RX queue becomes full enough that no usb_request is queued,
  427. * the OUT endpoint may begin NAKing as soon as its FIFO fills up.
  428. * So QUEUE_SIZE packets plus however many the FIFO holds (usually two)
  429. * can be buffered before the TTY layer's buffers (currently 64 KB).
  430. */
  431. static void gs_rx_push(unsigned long _port)
  432. {
  433. struct gs_port *port = (void *)_port;
  434. struct tty_struct *tty;
  435. struct list_head *queue = &port->read_queue;
  436. bool disconnect = false;
  437. bool do_push = false;
  438. /* hand any queued data to the tty */
  439. spin_lock_irq(&port->port_lock);
  440. tty = port->port.tty;
  441. while (!list_empty(queue)) {
  442. struct usb_request *req;
  443. req = list_first_entry(queue, struct usb_request, list);
  444. /* leave data queued if tty was rx throttled */
  445. if (tty && test_bit(TTY_THROTTLED, &tty->flags))
  446. break;
  447. switch (req->status) {
  448. case -ESHUTDOWN:
  449. disconnect = true;
  450. pr_vdebug("ttyGS%d: shutdown\n", port->port_num);
  451. break;
  452. default:
  453. /* presumably a transient fault */
  454. pr_warn("ttyGS%d: unexpected RX status %d\n",
  455. port->port_num, req->status);
  456. /* FALLTHROUGH */
  457. case 0:
  458. /* normal completion */
  459. break;
  460. }
  461. /* push data to (open) tty */
  462. if (req->actual) {
  463. char *packet = req->buf;
  464. unsigned size = req->actual;
  465. unsigned n;
  466. int count;
  467. /* we may have pushed part of this packet already... */
  468. n = port->n_read;
  469. if (n) {
  470. packet += n;
  471. size -= n;
  472. }
  473. count = tty_insert_flip_string(&port->port, packet,
  474. size);
  475. if (count)
  476. do_push = true;
  477. if (count != size) {
  478. /* stop pushing; TTY layer can't handle more */
  479. port->n_read += count;
  480. pr_vdebug("ttyGS%d: rx block %d/%d\n",
  481. port->port_num, count, req->actual);
  482. break;
  483. }
  484. port->n_read = 0;
  485. }
  486. list_move(&req->list, &port->read_pool);
  487. port->read_started--;
  488. }
  489. /* Push from tty to ldisc; this is handled by a workqueue,
  490. * so we won't get callbacks and can hold port_lock
  491. */
  492. if (do_push)
  493. tty_flip_buffer_push(&port->port);
  494. /* We want our data queue to become empty ASAP, keeping data
  495. * in the tty and ldisc (not here). If we couldn't push any
  496. * this time around, there may be trouble unless there's an
  497. * implicit tty_unthrottle() call on its way...
  498. *
  499. * REVISIT we should probably add a timer to keep the tasklet
  500. * from starving ... but it's not clear that case ever happens.
  501. */
  502. if (!list_empty(queue) && tty) {
  503. if (!test_bit(TTY_THROTTLED, &tty->flags)) {
  504. if (do_push)
  505. tasklet_schedule(&port->push);
  506. else
  507. pr_warn("ttyGS%d: RX not scheduled?\n",
  508. port->port_num);
  509. }
  510. }
  511. /* If we're still connected, refill the USB RX queue. */
  512. if (!disconnect && port->port_usb)
  513. gs_start_rx(port);
  514. spin_unlock_irq(&port->port_lock);
  515. }
  516. static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
  517. {
  518. struct gs_port *port = ep->driver_data;
  519. /* Queue all received data until the tty layer is ready for it. */
  520. spin_lock(&port->port_lock);
  521. list_add_tail(&req->list, &port->read_queue);
  522. tasklet_schedule(&port->push);
  523. spin_unlock(&port->port_lock);
  524. }
  525. static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
  526. {
  527. struct gs_port *port = ep->driver_data;
  528. spin_lock(&port->port_lock);
  529. list_add(&req->list, &port->write_pool);
  530. port->write_started--;
  531. switch (req->status) {
  532. default:
  533. /* presumably a transient fault */
  534. pr_warning("%s: unexpected %s status %d\n",
  535. __func__, ep->name, req->status);
  536. /* FALL THROUGH */
  537. case 0:
  538. /* normal completion */
  539. gs_start_tx(port);
  540. break;
  541. case -ESHUTDOWN:
  542. /* disconnect */
  543. pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
  544. break;
  545. }
  546. spin_unlock(&port->port_lock);
  547. }
  548. static void gs_free_requests(struct usb_ep *ep, struct list_head *head,
  549. int *allocated)
  550. {
  551. struct usb_request *req;
  552. while (!list_empty(head)) {
  553. req = list_entry(head->next, struct usb_request, list);
  554. list_del(&req->list);
  555. gs_free_req(ep, req);
  556. if (allocated)
  557. (*allocated)--;
  558. }
  559. }
  560. static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head,
  561. void (*fn)(struct usb_ep *, struct usb_request *),
  562. int *allocated)
  563. {
  564. int i;
  565. struct usb_request *req;
  566. int n = allocated ? QUEUE_SIZE - *allocated : QUEUE_SIZE;
  567. /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't
  568. * do quite that many this time, don't fail ... we just won't
  569. * be as speedy as we might otherwise be.
  570. */
  571. for (i = 0; i < n; i++) {
  572. req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
  573. if (!req)
  574. return list_empty(head) ? -ENOMEM : 0;
  575. req->complete = fn;
  576. list_add_tail(&req->list, head);
  577. if (allocated)
  578. (*allocated)++;
  579. }
  580. return 0;
  581. }
  582. /**
  583. * gs_start_io - start USB I/O streams
  584. * @dev: encapsulates endpoints to use
  585. * Context: holding port_lock; port_tty and port_usb are non-null
  586. *
  587. * We only start I/O when something is connected to both sides of
  588. * this port. If nothing is listening on the host side, we may
  589. * be pointlessly filling up our TX buffers and FIFO.
  590. */
  591. static int gs_start_io(struct gs_port *port)
  592. {
  593. struct list_head *head = &port->read_pool;
  594. struct usb_ep *ep = port->port_usb->out;
  595. int status;
  596. unsigned started;
  597. /* Allocate RX and TX I/O buffers. We can't easily do this much
  598. * earlier (with GFP_KERNEL) because the requests are coupled to
  599. * endpoints, as are the packet sizes we'll be using. Different
  600. * configurations may use different endpoints with a given port;
  601. * and high speed vs full speed changes packet sizes too.
  602. */
  603. status = gs_alloc_requests(ep, head, gs_read_complete,
  604. &port->read_allocated);
  605. if (status)
  606. return status;
  607. status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
  608. gs_write_complete, &port->write_allocated);
  609. if (status) {
  610. gs_free_requests(ep, head, &port->read_allocated);
  611. return status;
  612. }
  613. /* queue read requests */
  614. port->n_read = 0;
  615. started = gs_start_rx(port);
  616. /* unblock any pending writes into our circular buffer */
  617. if (started) {
  618. tty_wakeup(port->port.tty);
  619. } else {
  620. gs_free_requests(ep, head, &port->read_allocated);
  621. gs_free_requests(port->port_usb->in, &port->write_pool,
  622. &port->write_allocated);
  623. status = -EIO;
  624. }
  625. return status;
  626. }
  627. /*-------------------------------------------------------------------------*/
  628. /* TTY Driver */
  629. /*
  630. * gs_open sets up the link between a gs_port and its associated TTY.
  631. * That link is broken *only* by TTY close(), and all driver methods
  632. * know that.
  633. */
  634. static int gs_open(struct tty_struct *tty, struct file *file)
  635. {
  636. int port_num = tty->index;
  637. struct gs_port *port;
  638. int status;
  639. do {
  640. mutex_lock(&ports[port_num].lock);
  641. port = ports[port_num].port;
  642. if (!port)
  643. status = -ENODEV;
  644. else {
  645. spin_lock_irq(&port->port_lock);
  646. /* already open? Great. */
  647. if (port->port.count) {
  648. status = 0;
  649. port->port.count++;
  650. /* currently opening/closing? wait ... */
  651. } else if (port->openclose) {
  652. status = -EBUSY;
  653. /* ... else we do the work */
  654. } else {
  655. status = -EAGAIN;
  656. port->openclose = true;
  657. }
  658. spin_unlock_irq(&port->port_lock);
  659. }
  660. mutex_unlock(&ports[port_num].lock);
  661. switch (status) {
  662. default:
  663. /* fully handled */
  664. return status;
  665. case -EAGAIN:
  666. /* must do the work */
  667. break;
  668. case -EBUSY:
  669. /* wait for EAGAIN task to finish */
  670. msleep(1);
  671. /* REVISIT could have a waitchannel here, if
  672. * concurrent open performance is important
  673. */
  674. break;
  675. }
  676. } while (status != -EAGAIN);
  677. /* Do the "real open" */
  678. spin_lock_irq(&port->port_lock);
  679. /* allocate circular buffer on first open */
  680. if (port->port_write_buf.buf_buf == NULL) {
  681. spin_unlock_irq(&port->port_lock);
  682. status = gs_buf_alloc(&port->port_write_buf, WRITE_BUF_SIZE);
  683. spin_lock_irq(&port->port_lock);
  684. if (status) {
  685. pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n",
  686. port->port_num, tty, file);
  687. port->openclose = false;
  688. goto exit_unlock_port;
  689. }
  690. }
  691. /* REVISIT if REMOVED (ports[].port NULL), abort the open
  692. * to let rmmod work faster (but this way isn't wrong).
  693. */
  694. /* REVISIT maybe wait for "carrier detect" */
  695. tty->driver_data = port;
  696. port->port.tty = tty;
  697. port->port.count = 1;
  698. port->openclose = false;
  699. /* if connected, start the I/O stream */
  700. if (port->port_usb) {
  701. struct gserial *gser = port->port_usb;
  702. pr_debug("gs_open: start ttyGS%d\n", port->port_num);
  703. gs_start_io(port);
  704. if (gser->connect)
  705. gser->connect(gser);
  706. }
  707. pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
  708. status = 0;
  709. exit_unlock_port:
  710. spin_unlock_irq(&port->port_lock);
  711. return status;
  712. }
  713. static int gs_writes_finished(struct gs_port *p)
  714. {
  715. int cond;
  716. /* return true on disconnect or empty buffer */
  717. spin_lock_irq(&p->port_lock);
  718. cond = (p->port_usb == NULL) || !gs_buf_data_avail(&p->port_write_buf);
  719. spin_unlock_irq(&p->port_lock);
  720. return cond;
  721. }
  722. static void gs_close(struct tty_struct *tty, struct file *file)
  723. {
  724. struct gs_port *port = tty->driver_data;
  725. struct gserial *gser;
  726. spin_lock_irq(&port->port_lock);
  727. if (port->port.count != 1) {
  728. if (port->port.count == 0)
  729. WARN_ON(1);
  730. else
  731. --port->port.count;
  732. goto exit;
  733. }
  734. pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
  735. /* mark port as closing but in use; we can drop port lock
  736. * and sleep if necessary
  737. */
  738. port->openclose = true;
  739. port->port.count = 0;
  740. gser = port->port_usb;
  741. if (gser && gser->disconnect)
  742. gser->disconnect(gser);
  743. /* wait for circular write buffer to drain, disconnect, or at
  744. * most GS_CLOSE_TIMEOUT seconds; then discard the rest
  745. */
  746. if (gs_buf_data_avail(&port->port_write_buf) > 0 && gser) {
  747. spin_unlock_irq(&port->port_lock);
  748. wait_event_interruptible_timeout(port->drain_wait,
  749. gs_writes_finished(port),
  750. GS_CLOSE_TIMEOUT * HZ);
  751. spin_lock_irq(&port->port_lock);
  752. gser = port->port_usb;
  753. }
  754. /* Iff we're disconnected, there can be no I/O in flight so it's
  755. * ok to free the circular buffer; else just scrub it. And don't
  756. * let the push tasklet fire again until we're re-opened.
  757. */
  758. if (gser == NULL)
  759. gs_buf_free(&port->port_write_buf);
  760. else
  761. gs_buf_clear(&port->port_write_buf);
  762. port->port.tty = NULL;
  763. port->openclose = false;
  764. pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
  765. port->port_num, tty, file);
  766. wake_up(&port->close_wait);
  767. exit:
  768. spin_unlock_irq(&port->port_lock);
  769. }
  770. static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
  771. {
  772. struct gs_port *port = tty->driver_data;
  773. unsigned long flags;
  774. int status;
  775. pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n",
  776. port->port_num, tty, count);
  777. spin_lock_irqsave(&port->port_lock, flags);
  778. if (count)
  779. count = gs_buf_put(&port->port_write_buf, buf, count);
  780. /* treat count == 0 as flush_chars() */
  781. if (port->port_usb)
  782. status = gs_start_tx(port);
  783. spin_unlock_irqrestore(&port->port_lock, flags);
  784. return count;
  785. }
  786. static int gs_put_char(struct tty_struct *tty, unsigned char ch)
  787. {
  788. struct gs_port *port = tty->driver_data;
  789. unsigned long flags;
  790. int status;
  791. pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %ps\n",
  792. port->port_num, tty, ch, __builtin_return_address(0));
  793. spin_lock_irqsave(&port->port_lock, flags);
  794. status = gs_buf_put(&port->port_write_buf, &ch, 1);
  795. spin_unlock_irqrestore(&port->port_lock, flags);
  796. return status;
  797. }
  798. static void gs_flush_chars(struct tty_struct *tty)
  799. {
  800. struct gs_port *port = tty->driver_data;
  801. unsigned long flags;
  802. pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
  803. spin_lock_irqsave(&port->port_lock, flags);
  804. if (port->port_usb)
  805. gs_start_tx(port);
  806. spin_unlock_irqrestore(&port->port_lock, flags);
  807. }
  808. static int gs_write_room(struct tty_struct *tty)
  809. {
  810. struct gs_port *port = tty->driver_data;
  811. unsigned long flags;
  812. int room = 0;
  813. spin_lock_irqsave(&port->port_lock, flags);
  814. if (port->port_usb)
  815. room = gs_buf_space_avail(&port->port_write_buf);
  816. spin_unlock_irqrestore(&port->port_lock, flags);
  817. pr_vdebug("gs_write_room: (%d,%p) room=%d\n",
  818. port->port_num, tty, room);
  819. return room;
  820. }
  821. static int gs_chars_in_buffer(struct tty_struct *tty)
  822. {
  823. struct gs_port *port = tty->driver_data;
  824. unsigned long flags;
  825. int chars = 0;
  826. spin_lock_irqsave(&port->port_lock, flags);
  827. chars = gs_buf_data_avail(&port->port_write_buf);
  828. spin_unlock_irqrestore(&port->port_lock, flags);
  829. pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
  830. port->port_num, tty, chars);
  831. return chars;
  832. }
  833. /* undo side effects of setting TTY_THROTTLED */
  834. static void gs_unthrottle(struct tty_struct *tty)
  835. {
  836. struct gs_port *port = tty->driver_data;
  837. unsigned long flags;
  838. spin_lock_irqsave(&port->port_lock, flags);
  839. if (port->port_usb) {
  840. /* Kickstart read queue processing. We don't do xon/xoff,
  841. * rts/cts, or other handshaking with the host, but if the
  842. * read queue backs up enough we'll be NAKing OUT packets.
  843. */
  844. tasklet_schedule(&port->push);
  845. pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
  846. }
  847. spin_unlock_irqrestore(&port->port_lock, flags);
  848. }
  849. static int gs_break_ctl(struct tty_struct *tty, int duration)
  850. {
  851. struct gs_port *port = tty->driver_data;
  852. int status = 0;
  853. struct gserial *gser;
  854. pr_vdebug("gs_break_ctl: ttyGS%d, send break (%d) \n",
  855. port->port_num, duration);
  856. spin_lock_irq(&port->port_lock);
  857. gser = port->port_usb;
  858. if (gser && gser->send_break)
  859. status = gser->send_break(gser, duration);
  860. spin_unlock_irq(&port->port_lock);
  861. return status;
  862. }
  863. static const struct tty_operations gs_tty_ops = {
  864. .open = gs_open,
  865. .close = gs_close,
  866. .write = gs_write,
  867. .put_char = gs_put_char,
  868. .flush_chars = gs_flush_chars,
  869. .write_room = gs_write_room,
  870. .chars_in_buffer = gs_chars_in_buffer,
  871. .unthrottle = gs_unthrottle,
  872. .break_ctl = gs_break_ctl,
  873. };
  874. /*-------------------------------------------------------------------------*/
  875. static struct tty_driver *gs_tty_driver;
  876. #ifdef CONFIG_U_SERIAL_CONSOLE
  877. static struct gscons_info gscons_info;
  878. static struct console gserial_cons;
  879. static struct usb_request *gs_request_new(struct usb_ep *ep)
  880. {
  881. struct usb_request *req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  882. if (!req)
  883. return NULL;
  884. req->buf = kmalloc(ep->maxpacket, GFP_ATOMIC);
  885. if (!req->buf) {
  886. usb_ep_free_request(ep, req);
  887. return NULL;
  888. }
  889. return req;
  890. }
  891. static void gs_request_free(struct usb_request *req, struct usb_ep *ep)
  892. {
  893. if (!req)
  894. return;
  895. kfree(req->buf);
  896. usb_ep_free_request(ep, req);
  897. }
  898. static void gs_complete_out(struct usb_ep *ep, struct usb_request *req)
  899. {
  900. struct gscons_info *info = &gscons_info;
  901. switch (req->status) {
  902. default:
  903. pr_warn("%s: unexpected %s status %d\n",
  904. __func__, ep->name, req->status);
  905. case 0:
  906. /* normal completion */
  907. spin_lock(&info->con_lock);
  908. info->req_busy = 0;
  909. spin_unlock(&info->con_lock);
  910. wake_up_process(info->console_thread);
  911. break;
  912. case -ESHUTDOWN:
  913. /* disconnect */
  914. pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
  915. break;
  916. }
  917. }
  918. static int gs_console_connect(int port_num)
  919. {
  920. struct gscons_info *info = &gscons_info;
  921. struct gs_port *port;
  922. struct usb_ep *ep;
  923. if (port_num != gserial_cons.index) {
  924. pr_err("%s: port num [%d] is not support console\n",
  925. __func__, port_num);
  926. return -ENXIO;
  927. }
  928. port = ports[port_num].port;
  929. ep = port->port_usb->in;
  930. if (!info->console_req) {
  931. info->console_req = gs_request_new(ep);
  932. if (!info->console_req)
  933. return -ENOMEM;
  934. info->console_req->complete = gs_complete_out;
  935. }
  936. info->port = port;
  937. spin_lock(&info->con_lock);
  938. info->req_busy = 0;
  939. spin_unlock(&info->con_lock);
  940. pr_vdebug("port[%d] console connect!\n", port_num);
  941. return 0;
  942. }
  943. static void gs_console_disconnect(struct usb_ep *ep)
  944. {
  945. struct gscons_info *info = &gscons_info;
  946. struct usb_request *req = info->console_req;
  947. gs_request_free(req, ep);
  948. info->console_req = NULL;
  949. }
  950. static int gs_console_thread(void *data)
  951. {
  952. struct gscons_info *info = &gscons_info;
  953. struct gs_port *port;
  954. struct usb_request *req;
  955. struct usb_ep *ep;
  956. int xfer, ret, count, size;
  957. do {
  958. port = info->port;
  959. set_current_state(TASK_INTERRUPTIBLE);
  960. if (!port || !port->port_usb
  961. || !port->port_usb->in || !info->console_req)
  962. goto sched;
  963. req = info->console_req;
  964. ep = port->port_usb->in;
  965. spin_lock_irq(&info->con_lock);
  966. count = gs_buf_data_avail(&info->con_buf);
  967. size = ep->maxpacket;
  968. if (count > 0 && !info->req_busy) {
  969. set_current_state(TASK_RUNNING);
  970. if (count < size)
  971. size = count;
  972. xfer = gs_buf_get(&info->con_buf, req->buf, size);
  973. req->length = xfer;
  974. spin_unlock(&info->con_lock);
  975. ret = usb_ep_queue(ep, req, GFP_ATOMIC);
  976. spin_lock(&info->con_lock);
  977. if (ret < 0)
  978. info->req_busy = 0;
  979. else
  980. info->req_busy = 1;
  981. spin_unlock_irq(&info->con_lock);
  982. } else {
  983. spin_unlock_irq(&info->con_lock);
  984. sched:
  985. if (kthread_should_stop()) {
  986. set_current_state(TASK_RUNNING);
  987. break;
  988. }
  989. schedule();
  990. }
  991. } while (1);
  992. return 0;
  993. }
  994. static int gs_console_setup(struct console *co, char *options)
  995. {
  996. struct gscons_info *info = &gscons_info;
  997. int status;
  998. info->port = NULL;
  999. info->console_req = NULL;
  1000. info->req_busy = 0;
  1001. spin_lock_init(&info->con_lock);
  1002. status = gs_buf_alloc(&info->con_buf, GS_CONSOLE_BUF_SIZE);
  1003. if (status) {
  1004. pr_err("%s: allocate console buffer failed\n", __func__);
  1005. return status;
  1006. }
  1007. info->console_thread = kthread_create(gs_console_thread,
  1008. co, "gs_console");
  1009. if (IS_ERR(info->console_thread)) {
  1010. pr_err("%s: cannot create console thread\n", __func__);
  1011. gs_buf_free(&info->con_buf);
  1012. return PTR_ERR(info->console_thread);
  1013. }
  1014. wake_up_process(info->console_thread);
  1015. return 0;
  1016. }
  1017. static void gs_console_write(struct console *co,
  1018. const char *buf, unsigned count)
  1019. {
  1020. struct gscons_info *info = &gscons_info;
  1021. unsigned long flags;
  1022. spin_lock_irqsave(&info->con_lock, flags);
  1023. gs_buf_put(&info->con_buf, buf, count);
  1024. spin_unlock_irqrestore(&info->con_lock, flags);
  1025. wake_up_process(info->console_thread);
  1026. }
  1027. static struct tty_driver *gs_console_device(struct console *co, int *index)
  1028. {
  1029. struct tty_driver **p = (struct tty_driver **)co->data;
  1030. if (!*p)
  1031. return NULL;
  1032. *index = co->index;
  1033. return *p;
  1034. }
  1035. static struct console gserial_cons = {
  1036. .name = "ttyGS",
  1037. .write = gs_console_write,
  1038. .device = gs_console_device,
  1039. .setup = gs_console_setup,
  1040. .flags = CON_PRINTBUFFER,
  1041. .index = -1,
  1042. .data = &gs_tty_driver,
  1043. };
  1044. static void gserial_console_init(void)
  1045. {
  1046. register_console(&gserial_cons);
  1047. }
  1048. static void gserial_console_exit(void)
  1049. {
  1050. struct gscons_info *info = &gscons_info;
  1051. unregister_console(&gserial_cons);
  1052. kthread_stop(info->console_thread);
  1053. gs_buf_free(&info->con_buf);
  1054. }
  1055. #else
  1056. static int gs_console_connect(int port_num)
  1057. {
  1058. return 0;
  1059. }
  1060. static void gs_console_disconnect(struct usb_ep *ep)
  1061. {
  1062. }
  1063. static void gserial_console_init(void)
  1064. {
  1065. }
  1066. static void gserial_console_exit(void)
  1067. {
  1068. }
  1069. #endif
  1070. static int
  1071. gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
  1072. {
  1073. struct gs_port *port;
  1074. int ret = 0;
  1075. mutex_lock(&ports[port_num].lock);
  1076. if (ports[port_num].port) {
  1077. ret = -EBUSY;
  1078. goto out;
  1079. }
  1080. port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
  1081. if (port == NULL) {
  1082. ret = -ENOMEM;
  1083. goto out;
  1084. }
  1085. tty_port_init(&port->port);
  1086. spin_lock_init(&port->port_lock);
  1087. init_waitqueue_head(&port->drain_wait);
  1088. init_waitqueue_head(&port->close_wait);
  1089. tasklet_init(&port->push, gs_rx_push, (unsigned long) port);
  1090. INIT_LIST_HEAD(&port->read_pool);
  1091. INIT_LIST_HEAD(&port->read_queue);
  1092. INIT_LIST_HEAD(&port->write_pool);
  1093. port->port_num = port_num;
  1094. port->port_line_coding = *coding;
  1095. ports[port_num].port = port;
  1096. out:
  1097. mutex_unlock(&ports[port_num].lock);
  1098. return ret;
  1099. }
  1100. static int gs_closed(struct gs_port *port)
  1101. {
  1102. int cond;
  1103. spin_lock_irq(&port->port_lock);
  1104. cond = (port->port.count == 0) && !port->openclose;
  1105. spin_unlock_irq(&port->port_lock);
  1106. return cond;
  1107. }
  1108. static void gserial_free_port(struct gs_port *port)
  1109. {
  1110. tasklet_kill(&port->push);
  1111. /* wait for old opens to finish */
  1112. wait_event(port->close_wait, gs_closed(port));
  1113. WARN_ON(port->port_usb != NULL);
  1114. tty_port_destroy(&port->port);
  1115. kfree(port);
  1116. }
  1117. void gserial_free_line(unsigned char port_num)
  1118. {
  1119. struct gs_port *port;
  1120. mutex_lock(&ports[port_num].lock);
  1121. if (WARN_ON(!ports[port_num].port)) {
  1122. mutex_unlock(&ports[port_num].lock);
  1123. return;
  1124. }
  1125. port = ports[port_num].port;
  1126. ports[port_num].port = NULL;
  1127. mutex_unlock(&ports[port_num].lock);
  1128. gserial_free_port(port);
  1129. tty_unregister_device(gs_tty_driver, port_num);
  1130. gserial_console_exit();
  1131. }
  1132. EXPORT_SYMBOL_GPL(gserial_free_line);
  1133. int gserial_alloc_line(unsigned char *line_num)
  1134. {
  1135. struct usb_cdc_line_coding coding;
  1136. struct device *tty_dev;
  1137. int ret;
  1138. int port_num;
  1139. coding.dwDTERate = cpu_to_le32(9600);
  1140. coding.bCharFormat = 8;
  1141. coding.bParityType = USB_CDC_NO_PARITY;
  1142. coding.bDataBits = USB_CDC_1_STOP_BITS;
  1143. for (port_num = 0; port_num < MAX_U_SERIAL_PORTS; port_num++) {
  1144. ret = gs_port_alloc(port_num, &coding);
  1145. if (ret == -EBUSY)
  1146. continue;
  1147. if (ret)
  1148. return ret;
  1149. break;
  1150. }
  1151. if (ret)
  1152. return ret;
  1153. /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
  1154. tty_dev = tty_port_register_device(&ports[port_num].port->port,
  1155. gs_tty_driver, port_num, NULL);
  1156. if (IS_ERR(tty_dev)) {
  1157. struct gs_port *port;
  1158. pr_err("%s: failed to register tty for port %d, err %ld\n",
  1159. __func__, port_num, PTR_ERR(tty_dev));
  1160. ret = PTR_ERR(tty_dev);
  1161. port = ports[port_num].port;
  1162. ports[port_num].port = NULL;
  1163. gserial_free_port(port);
  1164. goto err;
  1165. }
  1166. *line_num = port_num;
  1167. gserial_console_init();
  1168. err:
  1169. return ret;
  1170. }
  1171. EXPORT_SYMBOL_GPL(gserial_alloc_line);
  1172. /**
  1173. * gserial_connect - notify TTY I/O glue that USB link is active
  1174. * @gser: the function, set up with endpoints and descriptors
  1175. * @port_num: which port is active
  1176. * Context: any (usually from irq)
  1177. *
  1178. * This is called activate endpoints and let the TTY layer know that
  1179. * the connection is active ... not unlike "carrier detect". It won't
  1180. * necessarily start I/O queues; unless the TTY is held open by any
  1181. * task, there would be no point. However, the endpoints will be
  1182. * activated so the USB host can perform I/O, subject to basic USB
  1183. * hardware flow control.
  1184. *
  1185. * Caller needs to have set up the endpoints and USB function in @dev
  1186. * before calling this, as well as the appropriate (speed-specific)
  1187. * endpoint descriptors, and also have allocate @port_num by calling
  1188. * @gserial_alloc_line().
  1189. *
  1190. * Returns negative errno or zero.
  1191. * On success, ep->driver_data will be overwritten.
  1192. */
  1193. int gserial_connect(struct gserial *gser, u8 port_num)
  1194. {
  1195. struct gs_port *port;
  1196. unsigned long flags;
  1197. int status;
  1198. if (port_num >= MAX_U_SERIAL_PORTS)
  1199. return -ENXIO;
  1200. port = ports[port_num].port;
  1201. if (!port) {
  1202. pr_err("serial line %d not allocated.\n", port_num);
  1203. return -EINVAL;
  1204. }
  1205. if (port->port_usb) {
  1206. pr_err("serial line %d is in use.\n", port_num);
  1207. return -EBUSY;
  1208. }
  1209. /* activate the endpoints */
  1210. status = usb_ep_enable(gser->in);
  1211. if (status < 0)
  1212. return status;
  1213. gser->in->driver_data = port;
  1214. status = usb_ep_enable(gser->out);
  1215. if (status < 0)
  1216. goto fail_out;
  1217. gser->out->driver_data = port;
  1218. /* then tell the tty glue that I/O can work */
  1219. spin_lock_irqsave(&port->port_lock, flags);
  1220. gser->ioport = port;
  1221. port->port_usb = gser;
  1222. /* REVISIT unclear how best to handle this state...
  1223. * we don't really couple it with the Linux TTY.
  1224. */
  1225. gser->port_line_coding = port->port_line_coding;
  1226. /* REVISIT if waiting on "carrier detect", signal. */
  1227. /* if it's already open, start I/O ... and notify the serial
  1228. * protocol about open/close status (connect/disconnect).
  1229. */
  1230. if (port->port.count) {
  1231. pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
  1232. gs_start_io(port);
  1233. if (gser->connect)
  1234. gser->connect(gser);
  1235. } else {
  1236. if (gser->disconnect)
  1237. gser->disconnect(gser);
  1238. }
  1239. status = gs_console_connect(port_num);
  1240. spin_unlock_irqrestore(&port->port_lock, flags);
  1241. return status;
  1242. fail_out:
  1243. usb_ep_disable(gser->in);
  1244. return status;
  1245. }
  1246. EXPORT_SYMBOL_GPL(gserial_connect);
  1247. /**
  1248. * gserial_disconnect - notify TTY I/O glue that USB link is inactive
  1249. * @gser: the function, on which gserial_connect() was called
  1250. * Context: any (usually from irq)
  1251. *
  1252. * This is called to deactivate endpoints and let the TTY layer know
  1253. * that the connection went inactive ... not unlike "hangup".
  1254. *
  1255. * On return, the state is as if gserial_connect() had never been called;
  1256. * there is no active USB I/O on these endpoints.
  1257. */
  1258. void gserial_disconnect(struct gserial *gser)
  1259. {
  1260. struct gs_port *port = gser->ioport;
  1261. unsigned long flags;
  1262. if (!port)
  1263. return;
  1264. /* tell the TTY glue not to do I/O here any more */
  1265. spin_lock_irqsave(&port->port_lock, flags);
  1266. /* REVISIT as above: how best to track this? */
  1267. port->port_line_coding = gser->port_line_coding;
  1268. port->port_usb = NULL;
  1269. gser->ioport = NULL;
  1270. if (port->port.count > 0 || port->openclose) {
  1271. wake_up_interruptible(&port->drain_wait);
  1272. if (port->port.tty)
  1273. tty_hangup(port->port.tty);
  1274. }
  1275. spin_unlock_irqrestore(&port->port_lock, flags);
  1276. /* disable endpoints, aborting down any active I/O */
  1277. usb_ep_disable(gser->out);
  1278. usb_ep_disable(gser->in);
  1279. /* finally, free any unused/unusable I/O buffers */
  1280. spin_lock_irqsave(&port->port_lock, flags);
  1281. if (port->port.count == 0 && !port->openclose)
  1282. gs_buf_free(&port->port_write_buf);
  1283. gs_free_requests(gser->out, &port->read_pool, NULL);
  1284. gs_free_requests(gser->out, &port->read_queue, NULL);
  1285. gs_free_requests(gser->in, &port->write_pool, NULL);
  1286. port->read_allocated = port->read_started =
  1287. port->write_allocated = port->write_started = 0;
  1288. gs_console_disconnect(gser->in);
  1289. spin_unlock_irqrestore(&port->port_lock, flags);
  1290. }
  1291. EXPORT_SYMBOL_GPL(gserial_disconnect);
  1292. static int userial_init(void)
  1293. {
  1294. unsigned i;
  1295. int status;
  1296. gs_tty_driver = alloc_tty_driver(MAX_U_SERIAL_PORTS);
  1297. if (!gs_tty_driver)
  1298. return -ENOMEM;
  1299. gs_tty_driver->driver_name = "g_serial";
  1300. gs_tty_driver->name = "ttyGS";
  1301. /* uses dynamically assigned dev_t values */
  1302. gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1303. gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  1304. gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1305. gs_tty_driver->init_termios = tty_std_termios;
  1306. /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on
  1307. * MS-Windows. Otherwise, most of these flags shouldn't affect
  1308. * anything unless we were to actually hook up to a serial line.
  1309. */
  1310. gs_tty_driver->init_termios.c_cflag =
  1311. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1312. gs_tty_driver->init_termios.c_ispeed = 9600;
  1313. gs_tty_driver->init_termios.c_ospeed = 9600;
  1314. tty_set_operations(gs_tty_driver, &gs_tty_ops);
  1315. for (i = 0; i < MAX_U_SERIAL_PORTS; i++)
  1316. mutex_init(&ports[i].lock);
  1317. /* export the driver ... */
  1318. status = tty_register_driver(gs_tty_driver);
  1319. if (status) {
  1320. pr_err("%s: cannot register, err %d\n",
  1321. __func__, status);
  1322. goto fail;
  1323. }
  1324. pr_debug("%s: registered %d ttyGS* device%s\n", __func__,
  1325. MAX_U_SERIAL_PORTS,
  1326. (MAX_U_SERIAL_PORTS == 1) ? "" : "s");
  1327. return status;
  1328. fail:
  1329. put_tty_driver(gs_tty_driver);
  1330. gs_tty_driver = NULL;
  1331. return status;
  1332. }
  1333. module_init(userial_init);
  1334. static void userial_cleanup(void)
  1335. {
  1336. tty_unregister_driver(gs_tty_driver);
  1337. put_tty_driver(gs_tty_driver);
  1338. gs_tty_driver = NULL;
  1339. }
  1340. module_exit(userial_cleanup);
  1341. MODULE_LICENSE("GPL");