generic.c 16 KB

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