generic.c 16 KB

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