generic.c 15 KB

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