generic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 (port->bulk_in_size) {
  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 transfers that might be going on */
  138. if (port->bulk_out_size)
  139. usb_kill_urb(port->write_urb);
  140. if (port->bulk_in_size)
  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. /* only do something if we have a bulk out endpoint */
  291. if (!port->bulk_out_size)
  292. return -ENODEV;
  293. if (count == 0) {
  294. dbg("%s - write request of 0 bytes", __func__);
  295. return 0;
  296. }
  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. if (!port->bulk_out_size)
  315. return 0;
  316. spin_lock_irqsave(&port->lock, flags);
  317. if (serial->type->max_in_flight_urbs) {
  318. if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
  319. room = port->bulk_out_size *
  320. (serial->type->max_in_flight_urbs -
  321. port->urbs_in_flight);
  322. } else {
  323. room = kfifo_avail(&port->write_fifo);
  324. }
  325. spin_unlock_irqrestore(&port->lock, flags);
  326. dbg("%s - returns %d", __func__, room);
  327. return room;
  328. }
  329. int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
  330. {
  331. struct usb_serial_port *port = tty->driver_data;
  332. struct usb_serial *serial = port->serial;
  333. unsigned long flags;
  334. int chars;
  335. dbg("%s - port %d", __func__, port->number);
  336. if (!port->bulk_out_size)
  337. return 0;
  338. spin_lock_irqsave(&port->lock, flags);
  339. if (serial->type->max_in_flight_urbs)
  340. chars = port->tx_bytes_flight;
  341. else
  342. chars = kfifo_len(&port->write_fifo);
  343. spin_unlock_irqrestore(&port->lock, flags);
  344. dbg("%s - returns %d", __func__, chars);
  345. return chars;
  346. }
  347. void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
  348. gfp_t mem_flags)
  349. {
  350. struct urb *urb = port->read_urb;
  351. struct usb_serial *serial = port->serial;
  352. int result;
  353. /* Continue reading from device */
  354. usb_fill_bulk_urb(urb, serial->dev,
  355. usb_rcvbulkpipe(serial->dev,
  356. port->bulk_in_endpointAddress),
  357. urb->transfer_buffer,
  358. urb->transfer_buffer_length,
  359. ((serial->type->read_bulk_callback) ?
  360. serial->type->read_bulk_callback :
  361. usb_serial_generic_read_bulk_callback), port);
  362. result = usb_submit_urb(urb, mem_flags);
  363. if (result && result != -EPERM) {
  364. dev_err(&port->dev,
  365. "%s - failed resubmitting read urb, error %d\n",
  366. __func__, result);
  367. }
  368. }
  369. EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
  370. /* Push data to tty layer and resubmit the bulk read URB */
  371. static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
  372. {
  373. struct urb *urb = port->read_urb;
  374. struct tty_struct *tty = tty_port_tty_get(&port->port);
  375. char *ch = (char *)urb->transfer_buffer;
  376. int i;
  377. if (!tty)
  378. goto done;
  379. /* The per character mucking around with sysrq path it too slow for
  380. stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
  381. where the USB serial is not a console anyway */
  382. if (!port->console || !port->sysrq)
  383. tty_insert_flip_string(tty, ch, urb->actual_length);
  384. else {
  385. /* Push data to tty */
  386. for (i = 0; i < urb->actual_length; i++, ch++) {
  387. if (!usb_serial_handle_sysrq_char(tty, port, *ch))
  388. tty_insert_flip_char(tty, *ch, TTY_NORMAL);
  389. }
  390. }
  391. tty_flip_buffer_push(tty);
  392. tty_kref_put(tty);
  393. done:
  394. usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
  395. }
  396. void usb_serial_generic_read_bulk_callback(struct urb *urb)
  397. {
  398. struct usb_serial_port *port = urb->context;
  399. unsigned char *data = urb->transfer_buffer;
  400. int status = urb->status;
  401. unsigned long flags;
  402. dbg("%s - port %d", __func__, port->number);
  403. if (unlikely(status != 0)) {
  404. dbg("%s - nonzero read bulk status received: %d",
  405. __func__, status);
  406. return;
  407. }
  408. usb_serial_debug_data(debug, &port->dev, __func__,
  409. urb->actual_length, data);
  410. /* Throttle the device if requested by tty */
  411. spin_lock_irqsave(&port->lock, flags);
  412. port->throttled = port->throttle_req;
  413. if (!port->throttled) {
  414. spin_unlock_irqrestore(&port->lock, flags);
  415. flush_and_resubmit_read_urb(port);
  416. } else
  417. spin_unlock_irqrestore(&port->lock, flags);
  418. }
  419. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  420. void usb_serial_generic_write_bulk_callback(struct urb *urb)
  421. {
  422. unsigned long flags;
  423. struct usb_serial_port *port = urb->context;
  424. int status = urb->status;
  425. dbg("%s - port %d", __func__, port->number);
  426. if (port->serial->type->max_in_flight_urbs) {
  427. kfree(urb->transfer_buffer);
  428. spin_lock_irqsave(&port->lock, flags);
  429. --port->urbs_in_flight;
  430. port->tx_bytes_flight -= urb->transfer_buffer_length;
  431. if (port->urbs_in_flight < 0)
  432. port->urbs_in_flight = 0;
  433. spin_unlock_irqrestore(&port->lock, flags);
  434. } else {
  435. port->write_urb_busy = 0;
  436. if (status)
  437. kfifo_reset_out(&port->write_fifo);
  438. else
  439. usb_serial_generic_write_start(port);
  440. }
  441. if (status)
  442. dbg("%s - non-zero urb status: %d", __func__, status);
  443. usb_serial_port_softint(port);
  444. }
  445. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  446. void usb_serial_generic_throttle(struct tty_struct *tty)
  447. {
  448. struct usb_serial_port *port = tty->driver_data;
  449. unsigned long flags;
  450. dbg("%s - port %d", __func__, port->number);
  451. /* Set the throttle request flag. It will be picked up
  452. * by usb_serial_generic_read_bulk_callback(). */
  453. spin_lock_irqsave(&port->lock, flags);
  454. port->throttle_req = 1;
  455. spin_unlock_irqrestore(&port->lock, flags);
  456. }
  457. void usb_serial_generic_unthrottle(struct tty_struct *tty)
  458. {
  459. struct usb_serial_port *port = tty->driver_data;
  460. int was_throttled;
  461. unsigned long flags;
  462. dbg("%s - port %d", __func__, port->number);
  463. /* Clear the throttle flags */
  464. spin_lock_irqsave(&port->lock, flags);
  465. was_throttled = port->throttled;
  466. port->throttled = port->throttle_req = 0;
  467. spin_unlock_irqrestore(&port->lock, flags);
  468. if (was_throttled) {
  469. /* Resume reading from device */
  470. flush_and_resubmit_read_urb(port);
  471. }
  472. }
  473. int usb_serial_handle_sysrq_char(struct tty_struct *tty,
  474. struct usb_serial_port *port, unsigned int ch)
  475. {
  476. if (port->sysrq && port->console) {
  477. if (ch && time_before(jiffies, port->sysrq)) {
  478. handle_sysrq(ch, tty);
  479. port->sysrq = 0;
  480. return 1;
  481. }
  482. port->sysrq = 0;
  483. }
  484. return 0;
  485. }
  486. EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
  487. int usb_serial_handle_break(struct usb_serial_port *port)
  488. {
  489. if (!port->sysrq) {
  490. port->sysrq = jiffies + HZ*5;
  491. return 1;
  492. }
  493. port->sysrq = 0;
  494. return 0;
  495. }
  496. EXPORT_SYMBOL_GPL(usb_serial_handle_break);
  497. int usb_serial_generic_resume(struct usb_serial *serial)
  498. {
  499. struct usb_serial_port *port;
  500. int i, c = 0, r;
  501. for (i = 0; i < serial->num_ports; i++) {
  502. port = serial->port[i];
  503. if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  504. continue;
  505. if (port->read_urb) {
  506. r = usb_submit_urb(port->read_urb, GFP_NOIO);
  507. if (r < 0)
  508. c++;
  509. }
  510. if (port->write_urb) {
  511. r = usb_serial_generic_write_start(port);
  512. if (r < 0)
  513. c++;
  514. }
  515. }
  516. return c ? -EIO : 0;
  517. }
  518. EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
  519. void usb_serial_generic_disconnect(struct usb_serial *serial)
  520. {
  521. int i;
  522. dbg("%s", __func__);
  523. /* stop reads and writes on all ports */
  524. for (i = 0; i < serial->num_ports; ++i)
  525. generic_cleanup(serial->port[i]);
  526. }
  527. void usb_serial_generic_release(struct usb_serial *serial)
  528. {
  529. dbg("%s", __func__);
  530. }