generic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. static int debug;
  22. #ifdef CONFIG_USB_SERIAL_GENERIC
  23. static int generic_probe(struct usb_interface *interface,
  24. const struct usb_device_id *id);
  25. static __u16 vendor = 0x05f9;
  26. static __u16 product = 0xffff;
  27. module_param(vendor, ushort, 0);
  28. MODULE_PARM_DESC(vendor, "User specified USB idVendor");
  29. module_param(product, ushort, 0);
  30. MODULE_PARM_DESC(product, "User specified USB idProduct");
  31. static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
  32. /* we want to look at all devices, as the vendor/product id can change
  33. * depending on the command line argument */
  34. static struct usb_device_id generic_serial_ids[] = {
  35. {.driver_info = 42},
  36. {}
  37. };
  38. static struct usb_driver generic_driver = {
  39. .name = "usbserial_generic",
  40. .probe = generic_probe,
  41. .disconnect = usb_serial_disconnect,
  42. .id_table = generic_serial_ids,
  43. .no_dynamic_id = 1,
  44. };
  45. /* All of the device info needed for the Generic Serial Converter */
  46. struct usb_serial_driver usb_serial_generic_device = {
  47. .driver = {
  48. .owner = THIS_MODULE,
  49. .name = "generic",
  50. },
  51. .id_table = generic_device_ids,
  52. .usb_driver = &generic_driver,
  53. .num_ports = 1,
  54. .shutdown = usb_serial_generic_shutdown,
  55. .throttle = usb_serial_generic_throttle,
  56. .unthrottle = usb_serial_generic_unthrottle,
  57. .resume = usb_serial_generic_resume,
  58. };
  59. static int generic_probe(struct usb_interface *interface,
  60. const struct usb_device_id *id)
  61. {
  62. const struct usb_device_id *id_pattern;
  63. id_pattern = usb_match_id(interface, generic_device_ids);
  64. if (id_pattern != NULL)
  65. return usb_serial_probe(interface, id);
  66. return -ENODEV;
  67. }
  68. #endif
  69. int usb_serial_generic_register(int _debug)
  70. {
  71. int retval = 0;
  72. debug = _debug;
  73. #ifdef CONFIG_USB_SERIAL_GENERIC
  74. generic_device_ids[0].idVendor = vendor;
  75. generic_device_ids[0].idProduct = product;
  76. generic_device_ids[0].match_flags =
  77. USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  78. /* register our generic driver with ourselves */
  79. retval = usb_serial_register(&usb_serial_generic_device);
  80. if (retval)
  81. goto exit;
  82. retval = usb_register(&generic_driver);
  83. if (retval)
  84. usb_serial_deregister(&usb_serial_generic_device);
  85. exit:
  86. #endif
  87. return retval;
  88. }
  89. void usb_serial_generic_deregister(void)
  90. {
  91. #ifdef CONFIG_USB_SERIAL_GENERIC
  92. /* remove our generic driver */
  93. usb_deregister(&generic_driver);
  94. usb_serial_deregister(&usb_serial_generic_device);
  95. #endif
  96. }
  97. int usb_serial_generic_open(struct tty_struct *tty,
  98. struct usb_serial_port *port, struct file *filp)
  99. {
  100. struct usb_serial *serial = port->serial;
  101. int result = 0;
  102. unsigned long flags;
  103. dbg("%s - port %d", __func__, port->number);
  104. /* force low_latency on so that our tty_push actually forces the data
  105. through, otherwise it is scheduled, and with high data rates (like
  106. with OHCI) data can get lost. */
  107. if (tty)
  108. tty->low_latency = 1;
  109. /* clear the throttle flags */
  110. spin_lock_irqsave(&port->lock, flags);
  111. port->throttled = 0;
  112. port->throttle_req = 0;
  113. spin_unlock_irqrestore(&port->lock, flags);
  114. /* if we have a bulk endpoint, start reading from it */
  115. if (serial->num_bulk_in) {
  116. /* Start reading from the device */
  117. usb_fill_bulk_urb(port->read_urb, serial->dev,
  118. usb_rcvbulkpipe(serial->dev,
  119. port->bulk_in_endpointAddress),
  120. port->read_urb->transfer_buffer,
  121. port->read_urb->transfer_buffer_length,
  122. ((serial->type->read_bulk_callback) ?
  123. serial->type->read_bulk_callback :
  124. usb_serial_generic_read_bulk_callback),
  125. port);
  126. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  127. if (result)
  128. dev_err(&port->dev,
  129. "%s - failed resubmitting read urb, error %d\n",
  130. __func__, result);
  131. }
  132. return result;
  133. }
  134. EXPORT_SYMBOL_GPL(usb_serial_generic_open);
  135. static void generic_cleanup(struct usb_serial_port *port)
  136. {
  137. struct usb_serial *serial = port->serial;
  138. dbg("%s - port %d", __func__, port->number);
  139. if (serial->dev) {
  140. /* shutdown any bulk reads that might be going on */
  141. if (serial->num_bulk_out)
  142. usb_kill_urb(port->write_urb);
  143. if (serial->num_bulk_in)
  144. usb_kill_urb(port->read_urb);
  145. }
  146. }
  147. int usb_serial_generic_resume(struct usb_serial *serial)
  148. {
  149. struct usb_serial_port *port;
  150. int i, c = 0, r;
  151. for (i = 0; i < serial->num_ports; i++) {
  152. port = serial->port[i];
  153. if (port->port.count && port->read_urb) {
  154. r = usb_submit_urb(port->read_urb, GFP_NOIO);
  155. if (r < 0)
  156. c++;
  157. }
  158. }
  159. return c ? -EIO : 0;
  160. }
  161. void usb_serial_generic_close(struct tty_struct *tty,
  162. struct usb_serial_port *port, struct file *filp)
  163. {
  164. dbg("%s - port %d", __func__, port->number);
  165. generic_cleanup(port);
  166. }
  167. int usb_serial_generic_write(struct tty_struct *tty,
  168. struct usb_serial_port *port, const unsigned char *buf, int count)
  169. {
  170. struct usb_serial *serial = port->serial;
  171. int result;
  172. unsigned char *data;
  173. dbg("%s - port %d", __func__, port->number);
  174. if (count == 0) {
  175. dbg("%s - write request of 0 bytes", __func__);
  176. return 0;
  177. }
  178. /* only do something if we have a bulk out endpoint */
  179. if (serial->num_bulk_out) {
  180. unsigned long flags;
  181. spin_lock_irqsave(&port->lock, flags);
  182. if (port->write_urb_busy) {
  183. spin_unlock_irqrestore(&port->lock, flags);
  184. dbg("%s - already writing", __func__);
  185. return 0;
  186. }
  187. port->write_urb_busy = 1;
  188. spin_unlock_irqrestore(&port->lock, flags);
  189. count = (count > port->bulk_out_size) ?
  190. port->bulk_out_size : count;
  191. memcpy(port->write_urb->transfer_buffer, buf, count);
  192. data = port->write_urb->transfer_buffer;
  193. usb_serial_debug_data(debug, &port->dev, __func__, count, data);
  194. /* set up our urb */
  195. usb_fill_bulk_urb(port->write_urb, serial->dev,
  196. usb_sndbulkpipe(serial->dev,
  197. port->bulk_out_endpointAddress),
  198. port->write_urb->transfer_buffer, count,
  199. ((serial->type->write_bulk_callback) ?
  200. serial->type->write_bulk_callback :
  201. usb_serial_generic_write_bulk_callback),
  202. port);
  203. /* send the data out the bulk port */
  204. port->write_urb_busy = 1;
  205. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  206. if (result) {
  207. dev_err(&port->dev,
  208. "%s - failed submitting write urb, error %d\n",
  209. __func__, result);
  210. /* don't have to grab the lock here, as we will
  211. retry if != 0 */
  212. port->write_urb_busy = 0;
  213. } else
  214. result = count;
  215. return result;
  216. }
  217. /* no bulk out, so return 0 bytes written */
  218. return 0;
  219. }
  220. int usb_serial_generic_write_room(struct tty_struct *tty)
  221. {
  222. struct usb_serial_port *port = tty->driver_data;
  223. struct usb_serial *serial = port->serial;
  224. int room = 0;
  225. dbg("%s - port %d", __func__, port->number);
  226. /* FIXME: Locking */
  227. if (serial->num_bulk_out) {
  228. if (!(port->write_urb_busy))
  229. room = port->bulk_out_size;
  230. }
  231. dbg("%s - returns %d", __func__, room);
  232. return room;
  233. }
  234. int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
  235. {
  236. struct usb_serial_port *port = tty->driver_data;
  237. struct usb_serial *serial = port->serial;
  238. int chars = 0;
  239. dbg("%s - port %d", __func__, port->number);
  240. /* FIXME: Locking */
  241. if (serial->num_bulk_out) {
  242. if (port->write_urb_busy)
  243. chars = port->write_urb->transfer_buffer_length;
  244. }
  245. dbg("%s - returns %d", __func__, chars);
  246. return chars;
  247. }
  248. static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
  249. {
  250. struct urb *urb = port->read_urb;
  251. struct usb_serial *serial = port->serial;
  252. int result;
  253. /* Continue reading from device */
  254. usb_fill_bulk_urb(urb, serial->dev,
  255. usb_rcvbulkpipe(serial->dev,
  256. port->bulk_in_endpointAddress),
  257. urb->transfer_buffer,
  258. urb->transfer_buffer_length,
  259. ((serial->type->read_bulk_callback) ?
  260. serial->type->read_bulk_callback :
  261. usb_serial_generic_read_bulk_callback), port);
  262. result = usb_submit_urb(urb, mem_flags);
  263. if (result)
  264. dev_err(&port->dev,
  265. "%s - failed resubmitting read urb, error %d\n",
  266. __func__, result);
  267. }
  268. /* Push data to tty layer and resubmit the bulk read URB */
  269. static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
  270. {
  271. struct urb *urb = port->read_urb;
  272. struct tty_struct *tty = tty_port_tty_get(&port->port);
  273. int room;
  274. /* Push data to tty */
  275. if (tty && urb->actual_length) {
  276. room = tty_buffer_request_room(tty, urb->actual_length);
  277. if (room) {
  278. tty_insert_flip_string(tty, urb->transfer_buffer, room);
  279. tty_flip_buffer_push(tty);
  280. }
  281. }
  282. tty_kref_put(tty);
  283. resubmit_read_urb(port, GFP_ATOMIC);
  284. }
  285. void usb_serial_generic_read_bulk_callback(struct urb *urb)
  286. {
  287. struct usb_serial_port *port = urb->context;
  288. unsigned char *data = urb->transfer_buffer;
  289. int status = urb->status;
  290. unsigned long flags;
  291. dbg("%s - port %d", __func__, port->number);
  292. if (unlikely(status != 0)) {
  293. dbg("%s - nonzero read bulk status received: %d",
  294. __func__, status);
  295. return;
  296. }
  297. usb_serial_debug_data(debug, &port->dev, __func__,
  298. urb->actual_length, data);
  299. /* Throttle the device if requested by tty */
  300. spin_lock_irqsave(&port->lock, flags);
  301. port->throttled = port->throttle_req;
  302. if (!port->throttled) {
  303. spin_unlock_irqrestore(&port->lock, flags);
  304. flush_and_resubmit_read_urb(port);
  305. } else
  306. spin_unlock_irqrestore(&port->lock, flags);
  307. }
  308. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  309. void usb_serial_generic_write_bulk_callback(struct urb *urb)
  310. {
  311. struct usb_serial_port *port = urb->context;
  312. int status = urb->status;
  313. dbg("%s - port %d", __func__, port->number);
  314. port->write_urb_busy = 0;
  315. if (status) {
  316. dbg("%s - nonzero write bulk status received: %d",
  317. __func__, status);
  318. return;
  319. }
  320. usb_serial_port_softint(port);
  321. }
  322. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  323. void usb_serial_generic_throttle(struct tty_struct *tty)
  324. {
  325. struct usb_serial_port *port = tty->driver_data;
  326. unsigned long flags;
  327. dbg("%s - port %d", __func__, port->number);
  328. /* Set the throttle request flag. It will be picked up
  329. * by usb_serial_generic_read_bulk_callback(). */
  330. spin_lock_irqsave(&port->lock, flags);
  331. port->throttle_req = 1;
  332. spin_unlock_irqrestore(&port->lock, flags);
  333. }
  334. void usb_serial_generic_unthrottle(struct tty_struct *tty)
  335. {
  336. struct usb_serial_port *port = tty->driver_data;
  337. int was_throttled;
  338. unsigned long flags;
  339. dbg("%s - port %d", __func__, port->number);
  340. /* Clear the throttle flags */
  341. spin_lock_irqsave(&port->lock, flags);
  342. was_throttled = port->throttled;
  343. port->throttled = port->throttle_req = 0;
  344. spin_unlock_irqrestore(&port->lock, flags);
  345. if (was_throttled) {
  346. /* Resume reading from device */
  347. resubmit_read_urb(port, GFP_KERNEL);
  348. }
  349. }
  350. void usb_serial_generic_shutdown(struct usb_serial *serial)
  351. {
  352. int i;
  353. dbg("%s", __func__);
  354. /* stop reads and writes on all ports */
  355. for (i = 0; i < serial->num_ports; ++i)
  356. generic_cleanup(serial->port[i]);
  357. }