generic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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,
  190. "%s - failed submitting write urb, error %d\n",
  191. __func__, status);
  192. goto error;
  193. }
  194. /* This urb is the responsibility of the host driver now */
  195. usb_free_urb(urb);
  196. dbg("%s write: %d", __func__, towrite);
  197. count -= towrite;
  198. bwrite += towrite;
  199. }
  200. return bwrite;
  201. error:
  202. usb_free_urb(urb);
  203. error_no_urb:
  204. kfree(buffer);
  205. error_no_buffer:
  206. spin_lock_irqsave(&port->lock, flags);
  207. port->urbs_in_flight--;
  208. port->tx_bytes_flight -= towrite;
  209. spin_unlock_irqrestore(&port->lock, flags);
  210. return bwrite;
  211. }
  212. /**
  213. * usb_serial_generic_write_start - kick off an URB write
  214. * @port: Pointer to the &struct usb_serial_port data
  215. *
  216. * Returns the number of bytes queued on success. This will be zero if there
  217. * was nothing to send. Otherwise, it returns a negative errno value
  218. */
  219. static int usb_serial_generic_write_start(struct usb_serial_port *port)
  220. {
  221. unsigned char *data;
  222. int result;
  223. int count;
  224. unsigned long flags;
  225. bool start_io;
  226. /* Atomically determine whether we can and need to start a USB
  227. * operation. */
  228. spin_lock_irqsave(&port->lock, flags);
  229. if (port->write_urb_busy)
  230. start_io = false;
  231. else {
  232. start_io = (kfifo_len(&port->write_fifo) != 0);
  233. port->write_urb_busy = start_io;
  234. }
  235. spin_unlock_irqrestore(&port->lock, flags);
  236. if (!start_io)
  237. return 0;
  238. data = port->write_urb->transfer_buffer;
  239. count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
  240. usb_serial_debug_data(debug, &port->dev, __func__, count, data);
  241. port->write_urb->transfer_buffer_length = count;
  242. /* send the data out the bulk port */
  243. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  244. if (result) {
  245. dev_err(&port->dev,
  246. "%s - failed submitting write urb, error %d\n",
  247. __func__, result);
  248. /* don't have to grab the lock here, as we will
  249. retry if != 0 */
  250. port->write_urb_busy = 0;
  251. return result;
  252. }
  253. spin_lock_irqsave(&port->lock, flags);
  254. port->tx_bytes_flight += count;
  255. spin_unlock_irqrestore(&port->lock, flags);
  256. return count;
  257. }
  258. /**
  259. * usb_serial_generic_write - generic write function for serial USB devices
  260. * @tty: Pointer to &struct tty_struct for the device
  261. * @port: Pointer to the &usb_serial_port structure for the device
  262. * @buf: Pointer to the data to write
  263. * @count: Number of bytes to write
  264. *
  265. * Returns the number of characters actually written, which may be anything
  266. * from zero to @count. If an error occurs, it returns the negative errno
  267. * value.
  268. */
  269. int usb_serial_generic_write(struct tty_struct *tty,
  270. struct usb_serial_port *port, const unsigned char *buf, int count)
  271. {
  272. struct usb_serial *serial = port->serial;
  273. int result;
  274. dbg("%s - port %d", __func__, port->number);
  275. /* only do something if we have a bulk out endpoint */
  276. if (!port->bulk_out_size)
  277. return -ENODEV;
  278. if (count == 0) {
  279. dbg("%s - write request of 0 bytes", __func__);
  280. return 0;
  281. }
  282. if (serial->type->max_in_flight_urbs)
  283. return usb_serial_multi_urb_write(tty, port,
  284. buf, count);
  285. count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
  286. result = usb_serial_generic_write_start(port);
  287. if (result >= 0)
  288. result = count;
  289. return result;
  290. }
  291. EXPORT_SYMBOL_GPL(usb_serial_generic_write);
  292. int usb_serial_generic_write_room(struct tty_struct *tty)
  293. {
  294. struct usb_serial_port *port = tty->driver_data;
  295. struct usb_serial *serial = port->serial;
  296. unsigned long flags;
  297. int room = 0;
  298. dbg("%s - port %d", __func__, port->number);
  299. if (!port->bulk_out_size)
  300. return 0;
  301. spin_lock_irqsave(&port->lock, flags);
  302. if (serial->type->max_in_flight_urbs) {
  303. if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
  304. room = port->bulk_out_size *
  305. (serial->type->max_in_flight_urbs -
  306. port->urbs_in_flight);
  307. } else {
  308. room = kfifo_avail(&port->write_fifo);
  309. }
  310. spin_unlock_irqrestore(&port->lock, flags);
  311. dbg("%s - returns %d", __func__, room);
  312. return room;
  313. }
  314. int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
  315. {
  316. struct usb_serial_port *port = tty->driver_data;
  317. struct usb_serial *serial = port->serial;
  318. unsigned long flags;
  319. int chars;
  320. dbg("%s - port %d", __func__, port->number);
  321. if (!port->bulk_out_size)
  322. return 0;
  323. spin_lock_irqsave(&port->lock, flags);
  324. if (serial->type->max_in_flight_urbs)
  325. chars = port->tx_bytes_flight;
  326. else
  327. chars = kfifo_len(&port->write_fifo) + port->tx_bytes_flight;
  328. spin_unlock_irqrestore(&port->lock, flags);
  329. dbg("%s - returns %d", __func__, chars);
  330. return chars;
  331. }
  332. int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
  333. gfp_t mem_flags)
  334. {
  335. int result;
  336. result = usb_submit_urb(port->read_urb, mem_flags);
  337. if (result && result != -EPERM) {
  338. dev_err(&port->dev,
  339. "%s - failed submitting read urb, error %d\n",
  340. __func__, result);
  341. }
  342. return result;
  343. }
  344. EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
  345. static void usb_serial_generic_process_read_urb(struct urb *urb)
  346. {
  347. struct usb_serial_port *port = urb->context;
  348. struct tty_struct *tty;
  349. char *ch = (char *)urb->transfer_buffer;
  350. int i;
  351. tty = tty_port_tty_get(&port->port);
  352. if (!tty)
  353. return;
  354. /* The per character mucking around with sysrq path it too slow for
  355. stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
  356. where the USB serial is not a console anyway */
  357. if (!port->port.console || !port->sysrq)
  358. tty_insert_flip_string(tty, ch, urb->actual_length);
  359. else {
  360. for (i = 0; i < urb->actual_length; i++, ch++) {
  361. if (!usb_serial_handle_sysrq_char(tty, port, *ch))
  362. tty_insert_flip_char(tty, *ch, TTY_NORMAL);
  363. }
  364. }
  365. tty_flip_buffer_push(tty);
  366. tty_kref_put(tty);
  367. }
  368. void usb_serial_generic_read_bulk_callback(struct urb *urb)
  369. {
  370. struct usb_serial_port *port = urb->context;
  371. unsigned char *data = urb->transfer_buffer;
  372. int status = urb->status;
  373. unsigned long flags;
  374. dbg("%s - port %d", __func__, port->number);
  375. if (unlikely(status != 0)) {
  376. dbg("%s - nonzero read bulk status received: %d",
  377. __func__, status);
  378. return;
  379. }
  380. usb_serial_debug_data(debug, &port->dev, __func__,
  381. urb->actual_length, data);
  382. usb_serial_generic_process_read_urb(urb);
  383. /* Throttle the device if requested by tty */
  384. spin_lock_irqsave(&port->lock, flags);
  385. port->throttled = port->throttle_req;
  386. if (!port->throttled) {
  387. spin_unlock_irqrestore(&port->lock, flags);
  388. usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
  389. } else
  390. spin_unlock_irqrestore(&port->lock, flags);
  391. }
  392. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  393. void usb_serial_generic_write_bulk_callback(struct urb *urb)
  394. {
  395. unsigned long flags;
  396. struct usb_serial_port *port = urb->context;
  397. int status = urb->status;
  398. dbg("%s - port %d", __func__, port->number);
  399. if (port->serial->type->max_in_flight_urbs) {
  400. kfree(urb->transfer_buffer);
  401. spin_lock_irqsave(&port->lock, flags);
  402. --port->urbs_in_flight;
  403. port->tx_bytes_flight -= urb->transfer_buffer_length;
  404. if (port->urbs_in_flight < 0)
  405. port->urbs_in_flight = 0;
  406. spin_unlock_irqrestore(&port->lock, flags);
  407. } else {
  408. spin_lock_irqsave(&port->lock, flags);
  409. port->tx_bytes_flight -= urb->transfer_buffer_length;
  410. port->write_urb_busy = 0;
  411. spin_unlock_irqrestore(&port->lock, flags);
  412. if (status) {
  413. spin_lock_irqsave(&port->lock, flags);
  414. kfifo_reset_out(&port->write_fifo);
  415. spin_unlock_irqrestore(&port->lock, flags);
  416. } else {
  417. usb_serial_generic_write_start(port);
  418. }
  419. }
  420. if (status)
  421. dbg("%s - non-zero urb status: %d", __func__, status);
  422. usb_serial_port_softint(port);
  423. }
  424. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  425. void usb_serial_generic_throttle(struct tty_struct *tty)
  426. {
  427. struct usb_serial_port *port = tty->driver_data;
  428. unsigned long flags;
  429. dbg("%s - port %d", __func__, port->number);
  430. /* Set the throttle request flag. It will be picked up
  431. * by usb_serial_generic_read_bulk_callback(). */
  432. spin_lock_irqsave(&port->lock, flags);
  433. port->throttle_req = 1;
  434. spin_unlock_irqrestore(&port->lock, flags);
  435. }
  436. void usb_serial_generic_unthrottle(struct tty_struct *tty)
  437. {
  438. struct usb_serial_port *port = tty->driver_data;
  439. int was_throttled;
  440. dbg("%s - port %d", __func__, port->number);
  441. /* Clear the throttle flags */
  442. spin_lock_irq(&port->lock);
  443. was_throttled = port->throttled;
  444. port->throttled = port->throttle_req = 0;
  445. spin_unlock_irq(&port->lock);
  446. if (was_throttled)
  447. usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
  448. }
  449. #ifdef CONFIG_MAGIC_SYSRQ
  450. int usb_serial_handle_sysrq_char(struct tty_struct *tty,
  451. struct usb_serial_port *port, unsigned int ch)
  452. {
  453. if (port->sysrq && port->port.console) {
  454. if (ch && time_before(jiffies, port->sysrq)) {
  455. handle_sysrq(ch, tty);
  456. port->sysrq = 0;
  457. return 1;
  458. }
  459. port->sysrq = 0;
  460. }
  461. return 0;
  462. }
  463. #else
  464. int usb_serial_handle_sysrq_char(struct tty_struct *tty,
  465. struct usb_serial_port *port, unsigned int ch)
  466. {
  467. return 0;
  468. }
  469. #endif
  470. EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
  471. int usb_serial_handle_break(struct usb_serial_port *port)
  472. {
  473. if (!port->sysrq) {
  474. port->sysrq = jiffies + HZ*5;
  475. return 1;
  476. }
  477. port->sysrq = 0;
  478. return 0;
  479. }
  480. EXPORT_SYMBOL_GPL(usb_serial_handle_break);
  481. int usb_serial_generic_resume(struct usb_serial *serial)
  482. {
  483. struct usb_serial_port *port;
  484. int i, c = 0, r;
  485. for (i = 0; i < serial->num_ports; i++) {
  486. port = serial->port[i];
  487. if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  488. continue;
  489. if (port->read_urb) {
  490. r = usb_submit_urb(port->read_urb, GFP_NOIO);
  491. if (r < 0)
  492. c++;
  493. }
  494. if (port->write_urb) {
  495. r = usb_serial_generic_write_start(port);
  496. if (r < 0)
  497. c++;
  498. }
  499. }
  500. return c ? -EIO : 0;
  501. }
  502. EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
  503. void usb_serial_generic_disconnect(struct usb_serial *serial)
  504. {
  505. int i;
  506. dbg("%s", __func__);
  507. /* stop reads and writes on all ports */
  508. for (i = 0; i < serial->num_ports; ++i)
  509. generic_cleanup(serial->port[i]);
  510. }
  511. void usb_serial_generic_release(struct usb_serial *serial)
  512. {
  513. dbg("%s", __func__);
  514. }