generic.c 15 KB

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