generic.c 13 KB

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