generic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * USB Serial Converter Generic functions
  3. *
  4. * Copyright (C) 2010 - 2011 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. static int debug;
  26. #ifdef CONFIG_USB_SERIAL_GENERIC
  27. static __u16 vendor = 0x05f9;
  28. static __u16 product = 0xffff;
  29. module_param(vendor, ushort, 0);
  30. MODULE_PARM_DESC(vendor, "User specified USB idVendor");
  31. module_param(product, ushort, 0);
  32. MODULE_PARM_DESC(product, "User specified USB idProduct");
  33. static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
  34. /* we want to look at all devices, as the vendor/product id can change
  35. * depending on the command line argument */
  36. static const struct usb_device_id generic_serial_ids[] = {
  37. {.driver_info = 42},
  38. {}
  39. };
  40. static struct usb_driver generic_driver = {
  41. .name = "usbserial_generic",
  42. .id_table = generic_serial_ids,
  43. };
  44. /* All of the device info needed for the Generic Serial Converter */
  45. struct usb_serial_driver usb_serial_generic_device = {
  46. .driver = {
  47. .owner = THIS_MODULE,
  48. .name = "generic",
  49. },
  50. .id_table = generic_device_ids,
  51. .num_ports = 1,
  52. .disconnect = usb_serial_generic_disconnect,
  53. .release = usb_serial_generic_release,
  54. .throttle = usb_serial_generic_throttle,
  55. .unthrottle = usb_serial_generic_unthrottle,
  56. .resume = usb_serial_generic_resume,
  57. };
  58. static struct usb_serial_driver * const serial_drivers[] = {
  59. &usb_serial_generic_device, NULL
  60. };
  61. #endif
  62. int usb_serial_generic_register(int _debug)
  63. {
  64. int retval = 0;
  65. debug = _debug;
  66. #ifdef CONFIG_USB_SERIAL_GENERIC
  67. generic_device_ids[0].idVendor = vendor;
  68. generic_device_ids[0].idProduct = product;
  69. generic_device_ids[0].match_flags =
  70. USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  71. /* register our generic driver with ourselves */
  72. retval = usb_serial_register_drivers(&generic_driver, serial_drivers);
  73. #endif
  74. return retval;
  75. }
  76. void usb_serial_generic_deregister(void)
  77. {
  78. #ifdef CONFIG_USB_SERIAL_GENERIC
  79. /* remove our generic driver */
  80. usb_serial_deregister_drivers(&generic_driver, serial_drivers);
  81. #endif
  82. }
  83. int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
  84. {
  85. int result = 0;
  86. unsigned long flags;
  87. /* clear the throttle flags */
  88. spin_lock_irqsave(&port->lock, flags);
  89. port->throttled = 0;
  90. port->throttle_req = 0;
  91. spin_unlock_irqrestore(&port->lock, flags);
  92. /* if we have a bulk endpoint, start reading from it */
  93. if (port->bulk_in_size)
  94. result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
  95. return result;
  96. }
  97. EXPORT_SYMBOL_GPL(usb_serial_generic_open);
  98. static void generic_cleanup(struct usb_serial_port *port)
  99. {
  100. struct usb_serial *serial = port->serial;
  101. unsigned long flags;
  102. int i;
  103. if (serial->dev) {
  104. /* shutdown any bulk transfers that might be going on */
  105. if (port->bulk_out_size) {
  106. for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
  107. usb_kill_urb(port->write_urbs[i]);
  108. spin_lock_irqsave(&port->lock, flags);
  109. kfifo_reset_out(&port->write_fifo);
  110. spin_unlock_irqrestore(&port->lock, flags);
  111. }
  112. if (port->bulk_in_size) {
  113. for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
  114. usb_kill_urb(port->read_urbs[i]);
  115. }
  116. }
  117. }
  118. void usb_serial_generic_close(struct usb_serial_port *port)
  119. {
  120. generic_cleanup(port);
  121. }
  122. EXPORT_SYMBOL_GPL(usb_serial_generic_close);
  123. int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
  124. void *dest, size_t size)
  125. {
  126. return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
  127. }
  128. /**
  129. * usb_serial_generic_write_start - kick off an URB write
  130. * @port: Pointer to the &struct usb_serial_port data
  131. *
  132. * Returns zero on success, or a negative errno value
  133. */
  134. static int usb_serial_generic_write_start(struct usb_serial_port *port)
  135. {
  136. struct urb *urb;
  137. int count, result;
  138. unsigned long flags;
  139. int i;
  140. if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
  141. return 0;
  142. retry:
  143. spin_lock_irqsave(&port->lock, flags);
  144. if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
  145. clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
  146. spin_unlock_irqrestore(&port->lock, flags);
  147. return 0;
  148. }
  149. i = (int)find_first_bit(&port->write_urbs_free,
  150. ARRAY_SIZE(port->write_urbs));
  151. spin_unlock_irqrestore(&port->lock, flags);
  152. urb = port->write_urbs[i];
  153. count = port->serial->type->prepare_write_buffer(port,
  154. urb->transfer_buffer,
  155. port->bulk_out_size);
  156. urb->transfer_buffer_length = count;
  157. usb_serial_debug_data(debug, &port->dev, __func__, count,
  158. urb->transfer_buffer);
  159. spin_lock_irqsave(&port->lock, flags);
  160. port->tx_bytes += count;
  161. spin_unlock_irqrestore(&port->lock, flags);
  162. clear_bit(i, &port->write_urbs_free);
  163. result = usb_submit_urb(urb, GFP_ATOMIC);
  164. if (result) {
  165. dev_err_console(port, "%s - error submitting urb: %d\n",
  166. __func__, result);
  167. set_bit(i, &port->write_urbs_free);
  168. spin_lock_irqsave(&port->lock, flags);
  169. port->tx_bytes -= count;
  170. spin_unlock_irqrestore(&port->lock, flags);
  171. clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
  172. return result;
  173. }
  174. /* Try sending off another urb, unless in irq context (in which case
  175. * there will be no free urb). */
  176. if (!in_irq())
  177. goto retry;
  178. clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
  179. return 0;
  180. }
  181. /**
  182. * usb_serial_generic_write - generic write function for serial USB devices
  183. * @tty: Pointer to &struct tty_struct for the device
  184. * @port: Pointer to the &usb_serial_port structure for the device
  185. * @buf: Pointer to the data to write
  186. * @count: Number of bytes to write
  187. *
  188. * Returns the number of characters actually written, which may be anything
  189. * from zero to @count. If an error occurs, it returns the negative errno
  190. * value.
  191. */
  192. int usb_serial_generic_write(struct tty_struct *tty,
  193. struct usb_serial_port *port, const unsigned char *buf, int count)
  194. {
  195. int result;
  196. /* only do something if we have a bulk out endpoint */
  197. if (!port->bulk_out_size)
  198. return -ENODEV;
  199. if (!count)
  200. return 0;
  201. count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
  202. result = usb_serial_generic_write_start(port);
  203. if (result)
  204. return result;
  205. return count;
  206. }
  207. EXPORT_SYMBOL_GPL(usb_serial_generic_write);
  208. int usb_serial_generic_write_room(struct tty_struct *tty)
  209. {
  210. struct usb_serial_port *port = tty->driver_data;
  211. unsigned long flags;
  212. int room;
  213. if (!port->bulk_out_size)
  214. return 0;
  215. spin_lock_irqsave(&port->lock, flags);
  216. room = kfifo_avail(&port->write_fifo);
  217. spin_unlock_irqrestore(&port->lock, flags);
  218. dbg("%s - returns %d", __func__, room);
  219. return room;
  220. }
  221. int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
  222. {
  223. struct usb_serial_port *port = tty->driver_data;
  224. unsigned long flags;
  225. int chars;
  226. if (!port->bulk_out_size)
  227. return 0;
  228. spin_lock_irqsave(&port->lock, flags);
  229. chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
  230. spin_unlock_irqrestore(&port->lock, flags);
  231. dbg("%s - returns %d", __func__, chars);
  232. return chars;
  233. }
  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. dbg("%s - port %d, urb %d", __func__, port->number, index);
  241. res = usb_submit_urb(port->read_urbs[index], mem_flags);
  242. if (res) {
  243. if (res != -EPERM) {
  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. struct tty_struct *tty;
  274. char *ch = (char *)urb->transfer_buffer;
  275. int i;
  276. if (!urb->actual_length)
  277. return;
  278. tty = tty_port_tty_get(&port->port);
  279. if (!tty)
  280. return;
  281. /* The per character mucking around with sysrq path it too slow for
  282. stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
  283. where the USB serial is not a console anyway */
  284. if (!port->port.console || !port->sysrq)
  285. tty_insert_flip_string(tty, ch, urb->actual_length);
  286. else {
  287. for (i = 0; i < urb->actual_length; i++, ch++) {
  288. if (!usb_serial_handle_sysrq_char(port, *ch))
  289. tty_insert_flip_char(tty, *ch, TTY_NORMAL);
  290. }
  291. }
  292. tty_flip_buffer_push(tty);
  293. tty_kref_put(tty);
  294. }
  295. EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
  296. void usb_serial_generic_read_bulk_callback(struct urb *urb)
  297. {
  298. struct usb_serial_port *port = urb->context;
  299. unsigned char *data = urb->transfer_buffer;
  300. unsigned long flags;
  301. int i;
  302. for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
  303. if (urb == port->read_urbs[i])
  304. break;
  305. }
  306. set_bit(i, &port->read_urbs_free);
  307. dbg("%s - port %d, urb %d, len %d", __func__, port->number, i,
  308. urb->actual_length);
  309. if (urb->status) {
  310. dbg("%s - non-zero urb status: %d", __func__, urb->status);
  311. return;
  312. }
  313. usb_serial_debug_data(debug, &port->dev, __func__,
  314. urb->actual_length, data);
  315. port->serial->type->process_read_urb(urb);
  316. /* Throttle the device if requested by tty */
  317. spin_lock_irqsave(&port->lock, flags);
  318. port->throttled = port->throttle_req;
  319. if (!port->throttled) {
  320. spin_unlock_irqrestore(&port->lock, flags);
  321. usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
  322. } else
  323. spin_unlock_irqrestore(&port->lock, flags);
  324. }
  325. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  326. void usb_serial_generic_write_bulk_callback(struct urb *urb)
  327. {
  328. unsigned long flags;
  329. struct usb_serial_port *port = urb->context;
  330. int status = urb->status;
  331. int i;
  332. for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
  333. if (port->write_urbs[i] == urb)
  334. break;
  335. spin_lock_irqsave(&port->lock, flags);
  336. port->tx_bytes -= urb->transfer_buffer_length;
  337. set_bit(i, &port->write_urbs_free);
  338. spin_unlock_irqrestore(&port->lock, flags);
  339. if (status) {
  340. dbg("%s - non-zero urb status: %d", __func__, status);
  341. spin_lock_irqsave(&port->lock, flags);
  342. kfifo_reset_out(&port->write_fifo);
  343. spin_unlock_irqrestore(&port->lock, flags);
  344. } else {
  345. usb_serial_generic_write_start(port);
  346. }
  347. usb_serial_port_softint(port);
  348. }
  349. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  350. void usb_serial_generic_throttle(struct tty_struct *tty)
  351. {
  352. struct usb_serial_port *port = tty->driver_data;
  353. unsigned long flags;
  354. /* Set the throttle request flag. It will be picked up
  355. * by usb_serial_generic_read_bulk_callback(). */
  356. spin_lock_irqsave(&port->lock, flags);
  357. port->throttle_req = 1;
  358. spin_unlock_irqrestore(&port->lock, flags);
  359. }
  360. EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
  361. void usb_serial_generic_unthrottle(struct tty_struct *tty)
  362. {
  363. struct usb_serial_port *port = tty->driver_data;
  364. int was_throttled;
  365. /* Clear the throttle flags */
  366. spin_lock_irq(&port->lock);
  367. was_throttled = port->throttled;
  368. port->throttled = port->throttle_req = 0;
  369. spin_unlock_irq(&port->lock);
  370. if (was_throttled)
  371. usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
  372. }
  373. EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
  374. #ifdef CONFIG_MAGIC_SYSRQ
  375. int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
  376. {
  377. if (port->sysrq && port->port.console) {
  378. if (ch && time_before(jiffies, port->sysrq)) {
  379. handle_sysrq(ch);
  380. port->sysrq = 0;
  381. return 1;
  382. }
  383. port->sysrq = 0;
  384. }
  385. return 0;
  386. }
  387. #else
  388. int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
  389. {
  390. return 0;
  391. }
  392. #endif
  393. EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
  394. int usb_serial_handle_break(struct usb_serial_port *port)
  395. {
  396. if (!port->sysrq) {
  397. port->sysrq = jiffies + HZ*5;
  398. return 1;
  399. }
  400. port->sysrq = 0;
  401. return 0;
  402. }
  403. EXPORT_SYMBOL_GPL(usb_serial_handle_break);
  404. /**
  405. * usb_serial_handle_dcd_change - handle a change of carrier detect state
  406. * @port: usb_serial_port structure for the open port
  407. * @tty: tty_struct structure for the port
  408. * @status: new carrier detect status, nonzero if active
  409. */
  410. void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
  411. struct tty_struct *tty, unsigned int status)
  412. {
  413. struct tty_port *port = &usb_port->port;
  414. dbg("%s - port %d, status %d", __func__, usb_port->number, status);
  415. if (status)
  416. wake_up_interruptible(&port->open_wait);
  417. else if (tty && !C_CLOCAL(tty))
  418. tty_hangup(tty);
  419. }
  420. EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
  421. int usb_serial_generic_resume(struct usb_serial *serial)
  422. {
  423. struct usb_serial_port *port;
  424. int i, c = 0, r;
  425. for (i = 0; i < serial->num_ports; i++) {
  426. port = serial->port[i];
  427. if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  428. continue;
  429. if (port->bulk_in_size) {
  430. r = usb_serial_generic_submit_read_urbs(port,
  431. GFP_NOIO);
  432. if (r < 0)
  433. c++;
  434. }
  435. if (port->bulk_out_size) {
  436. r = usb_serial_generic_write_start(port);
  437. if (r < 0)
  438. c++;
  439. }
  440. }
  441. return c ? -EIO : 0;
  442. }
  443. EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
  444. void usb_serial_generic_disconnect(struct usb_serial *serial)
  445. {
  446. int i;
  447. /* stop reads and writes on all ports */
  448. for (i = 0; i < serial->num_ports; ++i)
  449. generic_cleanup(serial->port[i]);
  450. }
  451. EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
  452. void usb_serial_generic_release(struct usb_serial *serial)
  453. {
  454. }