generic.c 16 KB

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