generic.c 14 KB

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