generic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. #define MAX_TX_URBS 40
  27. #ifdef CONFIG_USB_SERIAL_GENERIC
  28. static int generic_probe(struct usb_interface *interface,
  29. const struct usb_device_id *id);
  30. static __u16 vendor = 0x05f9;
  31. static __u16 product = 0xffff;
  32. module_param(vendor, ushort, 0);
  33. MODULE_PARM_DESC(vendor, "User specified USB idVendor");
  34. module_param(product, ushort, 0);
  35. MODULE_PARM_DESC(product, "User specified USB idProduct");
  36. static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
  37. /* we want to look at all devices, as the vendor/product id can change
  38. * depending on the command line argument */
  39. static const struct usb_device_id generic_serial_ids[] = {
  40. {.driver_info = 42},
  41. {}
  42. };
  43. static struct usb_driver generic_driver = {
  44. .name = "usbserial_generic",
  45. .probe = generic_probe,
  46. .disconnect = usb_serial_disconnect,
  47. .id_table = generic_serial_ids,
  48. .no_dynamic_id = 1,
  49. };
  50. /* All of the device info needed for the Generic Serial Converter */
  51. struct usb_serial_driver usb_serial_generic_device = {
  52. .driver = {
  53. .owner = THIS_MODULE,
  54. .name = "generic",
  55. },
  56. .id_table = generic_device_ids,
  57. .usb_driver = &generic_driver,
  58. .num_ports = 1,
  59. .disconnect = usb_serial_generic_disconnect,
  60. .release = usb_serial_generic_release,
  61. .throttle = usb_serial_generic_throttle,
  62. .unthrottle = usb_serial_generic_unthrottle,
  63. .resume = usb_serial_generic_resume,
  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(&usb_serial_generic_device);
  86. if (retval)
  87. goto exit;
  88. retval = usb_register(&generic_driver);
  89. if (retval)
  90. usb_serial_deregister(&usb_serial_generic_device);
  91. exit:
  92. #endif
  93. return retval;
  94. }
  95. void usb_serial_generic_deregister(void)
  96. {
  97. #ifdef CONFIG_USB_SERIAL_GENERIC
  98. /* remove our generic driver */
  99. usb_deregister(&generic_driver);
  100. usb_serial_deregister(&usb_serial_generic_device);
  101. #endif
  102. }
  103. int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
  104. {
  105. int result = 0;
  106. unsigned long flags;
  107. dbg("%s - port %d", __func__, port->number);
  108. /* clear the throttle flags */
  109. spin_lock_irqsave(&port->lock, flags);
  110. port->throttled = 0;
  111. port->throttle_req = 0;
  112. spin_unlock_irqrestore(&port->lock, flags);
  113. /* if we have a bulk endpoint, start reading from it */
  114. if (port->bulk_in_size)
  115. result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
  116. return result;
  117. }
  118. EXPORT_SYMBOL_GPL(usb_serial_generic_open);
  119. static void generic_cleanup(struct usb_serial_port *port)
  120. {
  121. struct usb_serial *serial = port->serial;
  122. unsigned long flags;
  123. int i;
  124. dbg("%s - port %d", __func__, port->number);
  125. if (serial->dev) {
  126. /* shutdown any bulk transfers that might be going on */
  127. if (port->bulk_out_size) {
  128. usb_kill_urb(port->write_urb);
  129. for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
  130. usb_kill_urb(port->write_urbs[i]);
  131. spin_lock_irqsave(&port->lock, flags);
  132. kfifo_reset_out(&port->write_fifo);
  133. spin_unlock_irqrestore(&port->lock, flags);
  134. }
  135. if (port->bulk_in_size)
  136. usb_kill_urb(port->read_urb);
  137. }
  138. }
  139. void usb_serial_generic_close(struct usb_serial_port *port)
  140. {
  141. dbg("%s - port %d", __func__, port->number);
  142. generic_cleanup(port);
  143. }
  144. EXPORT_SYMBOL_GPL(usb_serial_generic_close);
  145. int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
  146. void **dest, size_t size, const void *src, size_t count)
  147. {
  148. if (!*dest) {
  149. size = count;
  150. *dest = kmalloc(count, GFP_ATOMIC);
  151. if (!*dest) {
  152. dev_err(&port->dev, "%s - could not allocate buffer\n",
  153. __func__);
  154. return -ENOMEM;
  155. }
  156. }
  157. if (src) {
  158. count = size;
  159. memcpy(*dest, src, size);
  160. } else {
  161. count = kfifo_out_locked(&port->write_fifo, *dest, size,
  162. &port->lock);
  163. }
  164. return count;
  165. }
  166. EXPORT_SYMBOL_GPL(usb_serial_generic_prepare_write_buffer);
  167. static int usb_serial_multi_urb_write(struct tty_struct *tty,
  168. struct usb_serial_port *port, const unsigned char *buf, int count)
  169. {
  170. unsigned long flags;
  171. struct urb *urb;
  172. void *buffer;
  173. int status;
  174. spin_lock_irqsave(&port->lock, flags);
  175. if (port->tx_urbs == MAX_TX_URBS) {
  176. spin_unlock_irqrestore(&port->lock, flags);
  177. dbg("%s - write limit hit", __func__);
  178. return 0;
  179. }
  180. port->tx_urbs++;
  181. spin_unlock_irqrestore(&port->lock, flags);
  182. urb = usb_alloc_urb(0, GFP_ATOMIC);
  183. if (!urb) {
  184. dev_err(&port->dev, "%s - no free urbs available\n", __func__);
  185. status = -ENOMEM;
  186. goto err_urb;
  187. }
  188. buffer = NULL;
  189. count = min_t(int, count, PAGE_SIZE);
  190. count = port->serial->type->prepare_write_buffer(port, &buffer, 0,
  191. buf, count);
  192. if (count < 0) {
  193. status = count;
  194. goto err_buf;
  195. }
  196. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  197. usb_fill_bulk_urb(urb, port->serial->dev,
  198. usb_sndbulkpipe(port->serial->dev,
  199. port->bulk_out_endpointAddress),
  200. buffer, count,
  201. port->serial->type->write_bulk_callback, port);
  202. status = usb_submit_urb(urb, GFP_ATOMIC);
  203. if (status) {
  204. dev_err(&port->dev, "%s - error submitting urb: %d\n",
  205. __func__, status);
  206. goto err;
  207. }
  208. spin_lock_irqsave(&port->lock, flags);
  209. port->tx_bytes += urb->transfer_buffer_length;
  210. spin_unlock_irqrestore(&port->lock, flags);
  211. usb_free_urb(urb);
  212. return count;
  213. err:
  214. kfree(buffer);
  215. err_buf:
  216. usb_free_urb(urb);
  217. err_urb:
  218. spin_lock_irqsave(&port->lock, flags);
  219. port->tx_urbs--;
  220. spin_unlock_irqrestore(&port->lock, flags);
  221. return status;
  222. }
  223. /**
  224. * usb_serial_generic_write_start - kick off an URB write
  225. * @port: Pointer to the &struct usb_serial_port data
  226. *
  227. * Returns zero on success, or a negative errno value
  228. */
  229. static int usb_serial_generic_write_start(struct usb_serial_port *port)
  230. {
  231. struct urb *urb;
  232. int count, result;
  233. unsigned long flags;
  234. int i;
  235. if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
  236. return 0;
  237. retry:
  238. spin_lock_irqsave(&port->lock, flags);
  239. if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
  240. clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
  241. spin_unlock_irqrestore(&port->lock, flags);
  242. return 0;
  243. }
  244. i = (int)find_first_bit(&port->write_urbs_free,
  245. ARRAY_SIZE(port->write_urbs));
  246. spin_unlock_irqrestore(&port->lock, flags);
  247. urb = port->write_urbs[i];
  248. count = port->serial->type->prepare_write_buffer(port,
  249. &urb->transfer_buffer,
  250. port->bulk_out_size, NULL, 0);
  251. urb->transfer_buffer_length = count;
  252. usb_serial_debug_data(debug, &port->dev, __func__, count,
  253. urb->transfer_buffer);
  254. result = usb_submit_urb(urb, GFP_ATOMIC);
  255. if (result) {
  256. dev_err(&port->dev, "%s - error submitting urb: %d\n",
  257. __func__, result);
  258. clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
  259. return result;
  260. }
  261. clear_bit(i, &port->write_urbs_free);
  262. spin_lock_irqsave(&port->lock, flags);
  263. port->tx_bytes += count;
  264. spin_unlock_irqrestore(&port->lock, flags);
  265. /* Try sending off another urb, unless in irq context (in which case
  266. * there will be no free urb). */
  267. if (!in_irq())
  268. goto retry;
  269. clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
  270. return 0;
  271. }
  272. /**
  273. * usb_serial_generic_write - generic write function for serial USB devices
  274. * @tty: Pointer to &struct tty_struct for the device
  275. * @port: Pointer to the &usb_serial_port structure for the device
  276. * @buf: Pointer to the data to write
  277. * @count: Number of bytes to write
  278. *
  279. * Returns the number of characters actually written, which may be anything
  280. * from zero to @count. If an error occurs, it returns the negative errno
  281. * value.
  282. */
  283. int usb_serial_generic_write(struct tty_struct *tty,
  284. struct usb_serial_port *port, const unsigned char *buf, int count)
  285. {
  286. struct usb_serial *serial = port->serial;
  287. int result;
  288. dbg("%s - port %d", __func__, port->number);
  289. /* only do something if we have a bulk out endpoint */
  290. if (!port->bulk_out_size)
  291. return -ENODEV;
  292. if (!count)
  293. return 0;
  294. if (serial->type->multi_urb_write)
  295. return usb_serial_multi_urb_write(tty, port, buf, count);
  296. count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
  297. result = usb_serial_generic_write_start(port);
  298. if (result >= 0)
  299. result = count;
  300. return result;
  301. }
  302. EXPORT_SYMBOL_GPL(usb_serial_generic_write);
  303. int usb_serial_generic_write_room(struct tty_struct *tty)
  304. {
  305. struct usb_serial_port *port = tty->driver_data;
  306. struct usb_serial *serial = port->serial;
  307. unsigned long flags;
  308. int room;
  309. dbg("%s - port %d", __func__, port->number);
  310. if (!port->bulk_out_size)
  311. return 0;
  312. spin_lock_irqsave(&port->lock, flags);
  313. if (serial->type->multi_urb_write)
  314. room = (MAX_TX_URBS - port->tx_urbs) * PAGE_SIZE;
  315. else
  316. room = kfifo_avail(&port->write_fifo);
  317. spin_unlock_irqrestore(&port->lock, flags);
  318. dbg("%s - returns %d", __func__, room);
  319. return room;
  320. }
  321. int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
  322. {
  323. struct usb_serial_port *port = tty->driver_data;
  324. struct usb_serial *serial = port->serial;
  325. unsigned long flags;
  326. int chars;
  327. dbg("%s - port %d", __func__, port->number);
  328. if (!port->bulk_out_size)
  329. return 0;
  330. spin_lock_irqsave(&port->lock, flags);
  331. if (serial->type->multi_urb_write)
  332. chars = port->tx_bytes;
  333. else
  334. chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
  335. spin_unlock_irqrestore(&port->lock, flags);
  336. dbg("%s - returns %d", __func__, chars);
  337. return chars;
  338. }
  339. int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
  340. gfp_t mem_flags)
  341. {
  342. int result;
  343. result = usb_submit_urb(port->read_urb, mem_flags);
  344. if (result && result != -EPERM) {
  345. dev_err(&port->dev, "%s - error submitting urb: %d\n",
  346. __func__, result);
  347. }
  348. return result;
  349. }
  350. EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
  351. void usb_serial_generic_process_read_urb(struct urb *urb)
  352. {
  353. struct usb_serial_port *port = urb->context;
  354. struct tty_struct *tty;
  355. char *ch = (char *)urb->transfer_buffer;
  356. int i;
  357. tty = tty_port_tty_get(&port->port);
  358. if (!tty)
  359. return;
  360. /* The per character mucking around with sysrq path it too slow for
  361. stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
  362. where the USB serial is not a console anyway */
  363. if (!port->port.console || !port->sysrq)
  364. tty_insert_flip_string(tty, ch, urb->actual_length);
  365. else {
  366. for (i = 0; i < urb->actual_length; i++, ch++) {
  367. if (!usb_serial_handle_sysrq_char(tty, port, *ch))
  368. tty_insert_flip_char(tty, *ch, TTY_NORMAL);
  369. }
  370. }
  371. tty_flip_buffer_push(tty);
  372. tty_kref_put(tty);
  373. }
  374. EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
  375. void usb_serial_generic_read_bulk_callback(struct urb *urb)
  376. {
  377. struct usb_serial_port *port = urb->context;
  378. unsigned char *data = urb->transfer_buffer;
  379. int status = urb->status;
  380. unsigned long flags;
  381. dbg("%s - port %d", __func__, port->number);
  382. if (unlikely(status != 0)) {
  383. dbg("%s - nonzero read bulk status received: %d",
  384. __func__, status);
  385. return;
  386. }
  387. usb_serial_debug_data(debug, &port->dev, __func__,
  388. urb->actual_length, data);
  389. port->serial->type->process_read_urb(urb);
  390. /* Throttle the device if requested by tty */
  391. spin_lock_irqsave(&port->lock, flags);
  392. port->throttled = port->throttle_req;
  393. if (!port->throttled) {
  394. spin_unlock_irqrestore(&port->lock, flags);
  395. usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
  396. } else
  397. spin_unlock_irqrestore(&port->lock, flags);
  398. }
  399. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  400. void usb_serial_generic_write_bulk_callback(struct urb *urb)
  401. {
  402. unsigned long flags;
  403. struct usb_serial_port *port = urb->context;
  404. int status = urb->status;
  405. int i;
  406. dbg("%s - port %d", __func__, port->number);
  407. if (port->serial->type->multi_urb_write) {
  408. kfree(urb->transfer_buffer);
  409. spin_lock_irqsave(&port->lock, flags);
  410. port->tx_bytes -= urb->transfer_buffer_length;
  411. port->tx_urbs--;
  412. spin_unlock_irqrestore(&port->lock, flags);
  413. } else {
  414. for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
  415. if (port->write_urbs[i] == urb)
  416. break;
  417. spin_lock_irqsave(&port->lock, flags);
  418. port->tx_bytes -= urb->transfer_buffer_length;
  419. set_bit(i, &port->write_urbs_free);
  420. spin_unlock_irqrestore(&port->lock, flags);
  421. if (status) {
  422. spin_lock_irqsave(&port->lock, flags);
  423. kfifo_reset_out(&port->write_fifo);
  424. spin_unlock_irqrestore(&port->lock, flags);
  425. } else {
  426. usb_serial_generic_write_start(port);
  427. }
  428. }
  429. if (status)
  430. dbg("%s - non-zero urb status: %d", __func__, status);
  431. usb_serial_port_softint(port);
  432. }
  433. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  434. void usb_serial_generic_throttle(struct tty_struct *tty)
  435. {
  436. struct usb_serial_port *port = tty->driver_data;
  437. unsigned long flags;
  438. dbg("%s - port %d", __func__, port->number);
  439. /* Set the throttle request flag. It will be picked up
  440. * by usb_serial_generic_read_bulk_callback(). */
  441. spin_lock_irqsave(&port->lock, flags);
  442. port->throttle_req = 1;
  443. spin_unlock_irqrestore(&port->lock, flags);
  444. }
  445. EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
  446. void usb_serial_generic_unthrottle(struct tty_struct *tty)
  447. {
  448. struct usb_serial_port *port = tty->driver_data;
  449. int was_throttled;
  450. dbg("%s - port %d", __func__, port->number);
  451. /* Clear the throttle flags */
  452. spin_lock_irq(&port->lock);
  453. was_throttled = port->throttled;
  454. port->throttled = port->throttle_req = 0;
  455. spin_unlock_irq(&port->lock);
  456. if (was_throttled)
  457. usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
  458. }
  459. EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
  460. #ifdef CONFIG_MAGIC_SYSRQ
  461. int usb_serial_handle_sysrq_char(struct tty_struct *tty,
  462. struct usb_serial_port *port, unsigned int ch)
  463. {
  464. if (port->sysrq && port->port.console) {
  465. if (ch && time_before(jiffies, port->sysrq)) {
  466. handle_sysrq(ch, tty);
  467. port->sysrq = 0;
  468. return 1;
  469. }
  470. port->sysrq = 0;
  471. }
  472. return 0;
  473. }
  474. #else
  475. int usb_serial_handle_sysrq_char(struct tty_struct *tty,
  476. struct usb_serial_port *port, unsigned int ch)
  477. {
  478. return 0;
  479. }
  480. #endif
  481. EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
  482. int usb_serial_handle_break(struct usb_serial_port *port)
  483. {
  484. if (!port->sysrq) {
  485. port->sysrq = jiffies + HZ*5;
  486. return 1;
  487. }
  488. port->sysrq = 0;
  489. return 0;
  490. }
  491. EXPORT_SYMBOL_GPL(usb_serial_handle_break);
  492. int usb_serial_generic_resume(struct usb_serial *serial)
  493. {
  494. struct usb_serial_port *port;
  495. int i, c = 0, r;
  496. for (i = 0; i < serial->num_ports; i++) {
  497. port = serial->port[i];
  498. if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  499. continue;
  500. if (port->read_urb) {
  501. r = usb_submit_urb(port->read_urb, GFP_NOIO);
  502. if (r < 0)
  503. c++;
  504. }
  505. if (port->bulk_out_size) {
  506. r = usb_serial_generic_write_start(port);
  507. if (r < 0)
  508. c++;
  509. }
  510. }
  511. return c ? -EIO : 0;
  512. }
  513. EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
  514. void usb_serial_generic_disconnect(struct usb_serial *serial)
  515. {
  516. int i;
  517. dbg("%s", __func__);
  518. /* stop reads and writes on all ports */
  519. for (i = 0; i < serial->num_ports; ++i)
  520. generic_cleanup(serial->port[i]);
  521. }
  522. void usb_serial_generic_release(struct usb_serial *serial)
  523. {
  524. dbg("%s", __func__);
  525. }