generic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * USB Serial Converter Generic functions
  3. *
  4. * Copyright (C) 2010 - 2013 Johan Hovold (jhovold@gmail.com)
  5. * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/sysrq.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/serial.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/kfifo.h>
  24. #include <linux/serial.h>
  25. #ifdef CONFIG_USB_SERIAL_GENERIC
  26. static __u16 vendor = 0x05f9;
  27. static __u16 product = 0xffff;
  28. module_param(vendor, ushort, 0);
  29. MODULE_PARM_DESC(vendor, "User specified USB idVendor");
  30. module_param(product, ushort, 0);
  31. MODULE_PARM_DESC(product, "User specified USB idProduct");
  32. static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
  33. struct usb_serial_driver usb_serial_generic_device = {
  34. .driver = {
  35. .owner = THIS_MODULE,
  36. .name = "generic",
  37. },
  38. .id_table = generic_device_ids,
  39. .num_ports = 1,
  40. .throttle = usb_serial_generic_throttle,
  41. .unthrottle = usb_serial_generic_unthrottle,
  42. .resume = usb_serial_generic_resume,
  43. };
  44. static struct usb_serial_driver * const serial_drivers[] = {
  45. &usb_serial_generic_device, NULL
  46. };
  47. #endif
  48. int usb_serial_generic_register(void)
  49. {
  50. int retval = 0;
  51. #ifdef CONFIG_USB_SERIAL_GENERIC
  52. generic_device_ids[0].idVendor = vendor;
  53. generic_device_ids[0].idProduct = product;
  54. generic_device_ids[0].match_flags =
  55. USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  56. retval = usb_serial_register_drivers(serial_drivers,
  57. "usbserial_generic", generic_device_ids);
  58. #endif
  59. return retval;
  60. }
  61. void usb_serial_generic_deregister(void)
  62. {
  63. #ifdef CONFIG_USB_SERIAL_GENERIC
  64. usb_serial_deregister_drivers(serial_drivers);
  65. #endif
  66. }
  67. int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
  68. {
  69. int result = 0;
  70. unsigned long flags;
  71. spin_lock_irqsave(&port->lock, flags);
  72. port->throttled = 0;
  73. port->throttle_req = 0;
  74. spin_unlock_irqrestore(&port->lock, flags);
  75. if (port->bulk_in_size)
  76. result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
  77. return result;
  78. }
  79. EXPORT_SYMBOL_GPL(usb_serial_generic_open);
  80. void usb_serial_generic_close(struct usb_serial_port *port)
  81. {
  82. unsigned long flags;
  83. int i;
  84. if (port->bulk_out_size) {
  85. for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
  86. usb_kill_urb(port->write_urbs[i]);
  87. spin_lock_irqsave(&port->lock, flags);
  88. kfifo_reset_out(&port->write_fifo);
  89. spin_unlock_irqrestore(&port->lock, flags);
  90. }
  91. if (port->bulk_in_size) {
  92. for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
  93. usb_kill_urb(port->read_urbs[i]);
  94. }
  95. }
  96. EXPORT_SYMBOL_GPL(usb_serial_generic_close);
  97. int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
  98. void *dest, size_t size)
  99. {
  100. return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
  101. }
  102. /**
  103. * usb_serial_generic_write_start - start writing buffered data
  104. * @port: usb-serial port
  105. * @mem_flags: flags to use for memory allocations
  106. *
  107. * Serialised using USB_SERIAL_WRITE_BUSY flag.
  108. *
  109. * Return: Zero on success or if busy, otherwise a negative errno value.
  110. */
  111. int usb_serial_generic_write_start(struct usb_serial_port *port,
  112. gfp_t mem_flags)
  113. {
  114. struct urb *urb;
  115. int count, result;
  116. unsigned long flags;
  117. int i;
  118. if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
  119. return 0;
  120. retry:
  121. spin_lock_irqsave(&port->lock, flags);
  122. if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
  123. clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
  124. spin_unlock_irqrestore(&port->lock, flags);
  125. return 0;
  126. }
  127. i = (int)find_first_bit(&port->write_urbs_free,
  128. ARRAY_SIZE(port->write_urbs));
  129. spin_unlock_irqrestore(&port->lock, flags);
  130. urb = port->write_urbs[i];
  131. count = port->serial->type->prepare_write_buffer(port,
  132. urb->transfer_buffer,
  133. port->bulk_out_size);
  134. urb->transfer_buffer_length = count;
  135. usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
  136. spin_lock_irqsave(&port->lock, flags);
  137. port->tx_bytes += count;
  138. spin_unlock_irqrestore(&port->lock, flags);
  139. clear_bit(i, &port->write_urbs_free);
  140. result = usb_submit_urb(urb, mem_flags);
  141. if (result) {
  142. dev_err_console(port, "%s - error submitting urb: %d\n",
  143. __func__, result);
  144. set_bit(i, &port->write_urbs_free);
  145. spin_lock_irqsave(&port->lock, flags);
  146. port->tx_bytes -= count;
  147. spin_unlock_irqrestore(&port->lock, flags);
  148. clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
  149. return result;
  150. }
  151. goto retry; /* try sending off another urb */
  152. }
  153. EXPORT_SYMBOL_GPL(usb_serial_generic_write_start);
  154. /**
  155. * usb_serial_generic_write - generic write function
  156. * @tty: tty for the port
  157. * @port: usb-serial port
  158. * @buf: data to write
  159. * @count: number of bytes to write
  160. *
  161. * Return: The number of characters buffered, which may be anything from
  162. * zero to @count, or a negative errno value.
  163. */
  164. int usb_serial_generic_write(struct tty_struct *tty,
  165. struct usb_serial_port *port, const unsigned char *buf, int count)
  166. {
  167. int result;
  168. if (!port->bulk_out_size)
  169. return -ENODEV;
  170. if (!count)
  171. return 0;
  172. count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
  173. result = usb_serial_generic_write_start(port, GFP_ATOMIC);
  174. if (result)
  175. return result;
  176. return count;
  177. }
  178. EXPORT_SYMBOL_GPL(usb_serial_generic_write);
  179. int usb_serial_generic_write_room(struct tty_struct *tty)
  180. {
  181. struct usb_serial_port *port = tty->driver_data;
  182. unsigned long flags;
  183. int room;
  184. if (!port->bulk_out_size)
  185. return 0;
  186. spin_lock_irqsave(&port->lock, flags);
  187. room = kfifo_avail(&port->write_fifo);
  188. spin_unlock_irqrestore(&port->lock, flags);
  189. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  190. return room;
  191. }
  192. int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
  193. {
  194. struct usb_serial_port *port = tty->driver_data;
  195. unsigned long flags;
  196. int chars;
  197. if (!port->bulk_out_size)
  198. return 0;
  199. spin_lock_irqsave(&port->lock, flags);
  200. chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
  201. spin_unlock_irqrestore(&port->lock, flags);
  202. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  203. return chars;
  204. }
  205. EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
  206. void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout)
  207. {
  208. struct usb_serial_port *port = tty->driver_data;
  209. unsigned int bps;
  210. unsigned long period;
  211. unsigned long expire;
  212. bps = tty_get_baud_rate(tty);
  213. if (!bps)
  214. bps = 9600; /* B0 */
  215. /*
  216. * Use a poll-period of roughly the time it takes to send one
  217. * character or at least one jiffy.
  218. */
  219. period = max_t(unsigned long, (10 * HZ / bps), 1);
  220. if (timeout)
  221. period = min_t(unsigned long, period, timeout);
  222. dev_dbg(&port->dev, "%s - timeout = %u ms, period = %u ms\n",
  223. __func__, jiffies_to_msecs(timeout),
  224. jiffies_to_msecs(period));
  225. expire = jiffies + timeout;
  226. while (!port->serial->type->tx_empty(port)) {
  227. schedule_timeout_interruptible(period);
  228. if (signal_pending(current))
  229. break;
  230. if (timeout && time_after(jiffies, expire))
  231. break;
  232. }
  233. }
  234. EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent);
  235. static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
  236. int index, gfp_t mem_flags)
  237. {
  238. int res;
  239. if (!test_and_clear_bit(index, &port->read_urbs_free))
  240. return 0;
  241. dev_dbg(&port->dev, "%s - urb %d\n", __func__, index);
  242. res = usb_submit_urb(port->read_urbs[index], mem_flags);
  243. if (res) {
  244. if (res != -EPERM && res != -ENODEV) {
  245. dev_err(&port->dev,
  246. "%s - usb_submit_urb failed: %d\n",
  247. __func__, res);
  248. }
  249. set_bit(index, &port->read_urbs_free);
  250. return res;
  251. }
  252. return 0;
  253. }
  254. int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
  255. gfp_t mem_flags)
  256. {
  257. int res;
  258. int i;
  259. for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
  260. res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
  261. if (res)
  262. goto err;
  263. }
  264. return 0;
  265. err:
  266. for (; i >= 0; --i)
  267. usb_kill_urb(port->read_urbs[i]);
  268. return res;
  269. }
  270. EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
  271. void usb_serial_generic_process_read_urb(struct urb *urb)
  272. {
  273. struct usb_serial_port *port = urb->context;
  274. char *ch = (char *)urb->transfer_buffer;
  275. int i;
  276. if (!urb->actual_length)
  277. return;
  278. /*
  279. * The per character mucking around with sysrq path it too slow for
  280. * stuff like 3G modems, so shortcircuit it in the 99.9999999% of
  281. * cases where the USB serial is not a console anyway.
  282. */
  283. if (!port->port.console || !port->sysrq) {
  284. tty_insert_flip_string(&port->port, ch, urb->actual_length);
  285. } else {
  286. for (i = 0; i < urb->actual_length; i++, ch++) {
  287. if (!usb_serial_handle_sysrq_char(port, *ch))
  288. tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
  289. }
  290. }
  291. tty_flip_buffer_push(&port->port);
  292. }
  293. EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
  294. void usb_serial_generic_read_bulk_callback(struct urb *urb)
  295. {
  296. struct usb_serial_port *port = urb->context;
  297. unsigned char *data = urb->transfer_buffer;
  298. unsigned long flags;
  299. int status = urb->status;
  300. int i;
  301. for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
  302. if (urb == port->read_urbs[i])
  303. break;
  304. }
  305. set_bit(i, &port->read_urbs_free);
  306. dev_dbg(&port->dev, "%s - urb %d, len %d\n", __func__, i,
  307. urb->actual_length);
  308. switch (status) {
  309. case 0:
  310. break;
  311. case -ENOENT:
  312. case -ECONNRESET:
  313. case -ESHUTDOWN:
  314. dev_dbg(&port->dev, "%s - urb stopped: %d\n",
  315. __func__, status);
  316. return;
  317. case -EPIPE:
  318. dev_err(&port->dev, "%s - urb stopped: %d\n",
  319. __func__, status);
  320. return;
  321. default:
  322. dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
  323. __func__, status);
  324. goto resubmit;
  325. }
  326. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  327. port->serial->type->process_read_urb(urb);
  328. resubmit:
  329. /* Throttle the device if requested by tty */
  330. spin_lock_irqsave(&port->lock, flags);
  331. port->throttled = port->throttle_req;
  332. if (!port->throttled) {
  333. spin_unlock_irqrestore(&port->lock, flags);
  334. usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
  335. } else {
  336. spin_unlock_irqrestore(&port->lock, flags);
  337. }
  338. }
  339. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  340. void usb_serial_generic_write_bulk_callback(struct urb *urb)
  341. {
  342. unsigned long flags;
  343. struct usb_serial_port *port = urb->context;
  344. int status = urb->status;
  345. int i;
  346. for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
  347. if (port->write_urbs[i] == urb)
  348. break;
  349. }
  350. spin_lock_irqsave(&port->lock, flags);
  351. port->tx_bytes -= urb->transfer_buffer_length;
  352. set_bit(i, &port->write_urbs_free);
  353. spin_unlock_irqrestore(&port->lock, flags);
  354. switch (status) {
  355. case 0:
  356. break;
  357. case -ENOENT:
  358. case -ECONNRESET:
  359. case -ESHUTDOWN:
  360. dev_dbg(&port->dev, "%s - urb stopped: %d\n",
  361. __func__, status);
  362. return;
  363. case -EPIPE:
  364. dev_err_console(port, "%s - urb stopped: %d\n",
  365. __func__, status);
  366. return;
  367. default:
  368. dev_err_console(port, "%s - nonzero urb status: %d\n",
  369. __func__, status);
  370. goto resubmit;
  371. }
  372. resubmit:
  373. usb_serial_generic_write_start(port, GFP_ATOMIC);
  374. usb_serial_port_softint(port);
  375. }
  376. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  377. void usb_serial_generic_throttle(struct tty_struct *tty)
  378. {
  379. struct usb_serial_port *port = tty->driver_data;
  380. unsigned long flags;
  381. spin_lock_irqsave(&port->lock, flags);
  382. port->throttle_req = 1;
  383. spin_unlock_irqrestore(&port->lock, flags);
  384. }
  385. EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
  386. void usb_serial_generic_unthrottle(struct tty_struct *tty)
  387. {
  388. struct usb_serial_port *port = tty->driver_data;
  389. int was_throttled;
  390. spin_lock_irq(&port->lock);
  391. was_throttled = port->throttled;
  392. port->throttled = port->throttle_req = 0;
  393. spin_unlock_irq(&port->lock);
  394. if (was_throttled)
  395. usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
  396. }
  397. EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
  398. static bool usb_serial_generic_msr_changed(struct tty_struct *tty,
  399. unsigned long arg, struct async_icount *cprev)
  400. {
  401. struct usb_serial_port *port = tty->driver_data;
  402. struct async_icount cnow;
  403. unsigned long flags;
  404. bool ret;
  405. /*
  406. * Use tty-port initialised flag to detect all hangups including the
  407. * one generated at USB-device disconnect.
  408. */
  409. if (!tty_port_initialized(&port->port))
  410. return true;
  411. spin_lock_irqsave(&port->lock, flags);
  412. cnow = port->icount; /* atomic copy*/
  413. spin_unlock_irqrestore(&port->lock, flags);
  414. ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  415. ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  416. ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
  417. ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
  418. *cprev = cnow;
  419. return ret;
  420. }
  421. int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg)
  422. {
  423. struct usb_serial_port *port = tty->driver_data;
  424. struct async_icount cnow;
  425. unsigned long flags;
  426. int ret;
  427. spin_lock_irqsave(&port->lock, flags);
  428. cnow = port->icount; /* atomic copy */
  429. spin_unlock_irqrestore(&port->lock, flags);
  430. ret = wait_event_interruptible(port->port.delta_msr_wait,
  431. usb_serial_generic_msr_changed(tty, arg, &cnow));
  432. if (!ret && !tty_port_initialized(&port->port))
  433. ret = -EIO;
  434. return ret;
  435. }
  436. EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait);
  437. int usb_serial_generic_get_icount(struct tty_struct *tty,
  438. struct serial_icounter_struct *icount)
  439. {
  440. struct usb_serial_port *port = tty->driver_data;
  441. struct async_icount cnow;
  442. unsigned long flags;
  443. spin_lock_irqsave(&port->lock, flags);
  444. cnow = port->icount; /* atomic copy */
  445. spin_unlock_irqrestore(&port->lock, flags);
  446. icount->cts = cnow.cts;
  447. icount->dsr = cnow.dsr;
  448. icount->rng = cnow.rng;
  449. icount->dcd = cnow.dcd;
  450. icount->tx = cnow.tx;
  451. icount->rx = cnow.rx;
  452. icount->frame = cnow.frame;
  453. icount->parity = cnow.parity;
  454. icount->overrun = cnow.overrun;
  455. icount->brk = cnow.brk;
  456. icount->buf_overrun = cnow.buf_overrun;
  457. return 0;
  458. }
  459. EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount);
  460. #ifdef CONFIG_MAGIC_SYSRQ
  461. int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
  462. {
  463. if (port->sysrq && port->port.console) {
  464. if (ch && time_before(jiffies, port->sysrq)) {
  465. handle_sysrq(ch);
  466. port->sysrq = 0;
  467. return 1;
  468. }
  469. port->sysrq = 0;
  470. }
  471. return 0;
  472. }
  473. #else
  474. int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
  475. {
  476. return 0;
  477. }
  478. #endif
  479. EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
  480. int usb_serial_handle_break(struct usb_serial_port *port)
  481. {
  482. if (!port->sysrq) {
  483. port->sysrq = jiffies + HZ*5;
  484. return 1;
  485. }
  486. port->sysrq = 0;
  487. return 0;
  488. }
  489. EXPORT_SYMBOL_GPL(usb_serial_handle_break);
  490. /**
  491. * usb_serial_handle_dcd_change - handle a change of carrier detect state
  492. * @port: usb-serial port
  493. * @tty: tty for the port
  494. * @status: new carrier detect status, nonzero if active
  495. */
  496. void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
  497. struct tty_struct *tty, unsigned int status)
  498. {
  499. struct tty_port *port = &usb_port->port;
  500. dev_dbg(&usb_port->dev, "%s - status %d\n", __func__, status);
  501. if (tty) {
  502. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  503. if (ld) {
  504. if (ld->ops->dcd_change)
  505. ld->ops->dcd_change(tty, status);
  506. tty_ldisc_deref(ld);
  507. }
  508. }
  509. if (status)
  510. wake_up_interruptible(&port->open_wait);
  511. else if (tty && !C_CLOCAL(tty))
  512. tty_hangup(tty);
  513. }
  514. EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
  515. int usb_serial_generic_resume(struct usb_serial *serial)
  516. {
  517. struct usb_serial_port *port;
  518. int i, c = 0, r;
  519. for (i = 0; i < serial->num_ports; i++) {
  520. port = serial->port[i];
  521. if (!tty_port_initialized(&port->port))
  522. continue;
  523. if (port->bulk_in_size) {
  524. r = usb_serial_generic_submit_read_urbs(port,
  525. GFP_NOIO);
  526. if (r < 0)
  527. c++;
  528. }
  529. if (port->bulk_out_size) {
  530. r = usb_serial_generic_write_start(port, GFP_NOIO);
  531. if (r < 0)
  532. c++;
  533. }
  534. }
  535. return c ? -EIO : 0;
  536. }
  537. EXPORT_SYMBOL_GPL(usb_serial_generic_resume);