generic.c 15 KB

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