generic.c 16 KB

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