generic.c 14 KB

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