generic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 <asm/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_interrupt_in = NUM_DONT_CARE,
  54. .num_bulk_in = NUM_DONT_CARE,
  55. .num_bulk_out = NUM_DONT_CARE,
  56. .num_ports = 1,
  57. .shutdown = usb_serial_generic_shutdown,
  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 = 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 usb_serial_port *port, struct file *filp)
  100. {
  101. struct usb_serial *serial = port->serial;
  102. int result = 0;
  103. unsigned long flags;
  104. dbg("%s - port %d", __FUNCTION__, port->number);
  105. /* force low_latency on so that our tty_push actually forces the data through,
  106. otherwise it is scheduled, and with high data rates (like with OHCI) data
  107. can get lost. */
  108. if (port->tty)
  109. port->tty->low_latency = 1;
  110. /* clear the throttle flags */
  111. spin_lock_irqsave(&port->lock, flags);
  112. port->throttled = 0;
  113. port->throttle_req = 0;
  114. spin_unlock_irqrestore(&port->lock, flags);
  115. /* if we have a bulk endpoint, start reading from it */
  116. if (serial->num_bulk_in) {
  117. /* Start reading from the device */
  118. usb_fill_bulk_urb (port->read_urb, serial->dev,
  119. usb_rcvbulkpipe(serial->dev, 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, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  129. }
  130. return result;
  131. }
  132. EXPORT_SYMBOL_GPL(usb_serial_generic_open);
  133. static void generic_cleanup (struct usb_serial_port *port)
  134. {
  135. struct usb_serial *serial = port->serial;
  136. dbg("%s - port %d", __FUNCTION__, port->number);
  137. if (serial->dev) {
  138. /* shutdown any bulk reads that might be going on */
  139. if (serial->num_bulk_out)
  140. usb_kill_urb(port->write_urb);
  141. if (serial->num_bulk_in)
  142. usb_kill_urb(port->read_urb);
  143. }
  144. }
  145. int usb_serial_generic_resume(struct usb_serial *serial)
  146. {
  147. struct usb_serial_port *port;
  148. int i, c = 0, r;
  149. for (i = 0; i < serial->num_ports; i++) {
  150. port = serial->port[i];
  151. if (port->open_count && port->read_urb) {
  152. r = usb_submit_urb(port->read_urb, GFP_NOIO);
  153. if (r < 0)
  154. c++;
  155. }
  156. }
  157. return c ? -EIO : 0;
  158. }
  159. void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
  160. {
  161. dbg("%s - port %d", __FUNCTION__, port->number);
  162. generic_cleanup (port);
  163. }
  164. int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  165. {
  166. struct usb_serial *serial = port->serial;
  167. int result;
  168. unsigned char *data;
  169. dbg("%s - port %d", __FUNCTION__, port->number);
  170. if (count == 0) {
  171. dbg("%s - write request of 0 bytes", __FUNCTION__);
  172. return (0);
  173. }
  174. /* only do something if we have a bulk out endpoint */
  175. if (serial->num_bulk_out) {
  176. spin_lock_bh(&port->lock);
  177. if (port->write_urb_busy) {
  178. spin_unlock_bh(&port->lock);
  179. dbg("%s - already writing", __FUNCTION__);
  180. return 0;
  181. }
  182. port->write_urb_busy = 1;
  183. spin_unlock_bh(&port->lock);
  184. count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  185. memcpy (port->write_urb->transfer_buffer, buf, count);
  186. data = port->write_urb->transfer_buffer;
  187. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
  188. /* set up our urb */
  189. usb_fill_bulk_urb (port->write_urb, serial->dev,
  190. usb_sndbulkpipe (serial->dev,
  191. port->bulk_out_endpointAddress),
  192. port->write_urb->transfer_buffer, count,
  193. ((serial->type->write_bulk_callback) ?
  194. serial->type->write_bulk_callback :
  195. usb_serial_generic_write_bulk_callback), port);
  196. /* send the data out the bulk port */
  197. port->write_urb_busy = 1;
  198. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  199. if (result) {
  200. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  201. /* don't have to grab the lock here, as we will retry if != 0 */
  202. port->write_urb_busy = 0;
  203. } else
  204. result = count;
  205. return result;
  206. }
  207. /* no bulk out, so return 0 bytes written */
  208. return 0;
  209. }
  210. int usb_serial_generic_write_room (struct usb_serial_port *port)
  211. {
  212. struct usb_serial *serial = port->serial;
  213. int room = 0;
  214. dbg("%s - port %d", __FUNCTION__, port->number);
  215. if (serial->num_bulk_out) {
  216. if (!(port->write_urb_busy))
  217. room = port->bulk_out_size;
  218. }
  219. dbg("%s - returns %d", __FUNCTION__, room);
  220. return (room);
  221. }
  222. int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
  223. {
  224. struct usb_serial *serial = port->serial;
  225. int chars = 0;
  226. dbg("%s - port %d", __FUNCTION__, port->number);
  227. if (serial->num_bulk_out) {
  228. if (port->write_urb_busy)
  229. chars = port->write_urb->transfer_buffer_length;
  230. }
  231. dbg("%s - returns %d", __FUNCTION__, chars);
  232. return (chars);
  233. }
  234. static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
  235. {
  236. struct urb *urb = port->read_urb;
  237. struct usb_serial *serial = port->serial;
  238. int result;
  239. /* Continue reading from device */
  240. usb_fill_bulk_urb (urb, serial->dev,
  241. usb_rcvbulkpipe (serial->dev,
  242. port->bulk_in_endpointAddress),
  243. urb->transfer_buffer,
  244. urb->transfer_buffer_length,
  245. ((serial->type->read_bulk_callback) ?
  246. serial->type->read_bulk_callback :
  247. usb_serial_generic_read_bulk_callback), port);
  248. result = usb_submit_urb(urb, mem_flags);
  249. if (result)
  250. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  251. }
  252. /* Push data to tty layer and resubmit the bulk read URB */
  253. static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
  254. {
  255. struct urb *urb = port->read_urb;
  256. struct tty_struct *tty = port->tty;
  257. int room;
  258. /* Push data to tty */
  259. if (tty && urb->actual_length) {
  260. room = tty_buffer_request_room(tty, urb->actual_length);
  261. if (room) {
  262. tty_insert_flip_string(tty, urb->transfer_buffer, room);
  263. tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
  264. }
  265. }
  266. resubmit_read_urb(port, GFP_ATOMIC);
  267. }
  268. void usb_serial_generic_read_bulk_callback (struct urb *urb)
  269. {
  270. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  271. unsigned char *data = urb->transfer_buffer;
  272. int status = urb->status;
  273. dbg("%s - port %d", __FUNCTION__, port->number);
  274. if (unlikely(status != 0)) {
  275. dbg("%s - nonzero read bulk status received: %d",
  276. __FUNCTION__, status);
  277. return;
  278. }
  279. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  280. /* Throttle the device if requested by tty */
  281. spin_lock(&port->lock);
  282. if (!(port->throttled = port->throttle_req))
  283. /* Handle data and continue reading from device */
  284. flush_and_resubmit_read_urb(port);
  285. spin_unlock(&port->lock);
  286. }
  287. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  288. void usb_serial_generic_write_bulk_callback (struct urb *urb)
  289. {
  290. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  291. int status = urb->status;
  292. dbg("%s - port %d", __FUNCTION__, port->number);
  293. port->write_urb_busy = 0;
  294. if (status) {
  295. dbg("%s - nonzero write bulk status received: %d",
  296. __FUNCTION__, status);
  297. return;
  298. }
  299. usb_serial_port_softint(port);
  300. }
  301. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  302. void usb_serial_generic_throttle (struct usb_serial_port *port)
  303. {
  304. unsigned long flags;
  305. dbg("%s - port %d", __FUNCTION__, port->number);
  306. /* Set the throttle request flag. It will be picked up
  307. * by usb_serial_generic_read_bulk_callback(). */
  308. spin_lock_irqsave(&port->lock, flags);
  309. port->throttle_req = 1;
  310. spin_unlock_irqrestore(&port->lock, flags);
  311. }
  312. void usb_serial_generic_unthrottle (struct usb_serial_port *port)
  313. {
  314. int was_throttled;
  315. unsigned long flags;
  316. dbg("%s - port %d", __FUNCTION__, port->number);
  317. /* Clear the throttle flags */
  318. spin_lock_irqsave(&port->lock, flags);
  319. was_throttled = port->throttled;
  320. port->throttled = port->throttle_req = 0;
  321. spin_unlock_irqrestore(&port->lock, flags);
  322. if (was_throttled) {
  323. /* Resume reading from device */
  324. resubmit_read_urb(port, GFP_KERNEL);
  325. }
  326. }
  327. void usb_serial_generic_shutdown (struct usb_serial *serial)
  328. {
  329. int i;
  330. dbg("%s", __FUNCTION__);
  331. /* stop reads and writes on all ports */
  332. for (i=0; i < serial->num_ports; ++i) {
  333. generic_cleanup(serial->port[i]);
  334. }
  335. }