generic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. };
  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 = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  79. /* register our generic driver with ourselves */
  80. retval = usb_serial_register (&usb_serial_generic_device);
  81. if (retval)
  82. goto exit;
  83. retval = usb_register(&generic_driver);
  84. if (retval)
  85. usb_serial_deregister(&usb_serial_generic_device);
  86. exit:
  87. #endif
  88. return retval;
  89. }
  90. void usb_serial_generic_deregister (void)
  91. {
  92. #ifdef CONFIG_USB_SERIAL_GENERIC
  93. /* remove our generic driver */
  94. usb_deregister(&generic_driver);
  95. usb_serial_deregister (&usb_serial_generic_device);
  96. #endif
  97. }
  98. int usb_serial_generic_open (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", __FUNCTION__, port->number);
  104. /* force low_latency on so that our tty_push actually forces the data through,
  105. otherwise it is scheduled, and with high data rates (like with OHCI) data
  106. can get lost. */
  107. if (port->tty)
  108. port->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, port->bulk_in_endpointAddress),
  119. port->read_urb->transfer_buffer,
  120. port->read_urb->transfer_buffer_length,
  121. ((serial->type->read_bulk_callback) ?
  122. serial->type->read_bulk_callback :
  123. usb_serial_generic_read_bulk_callback),
  124. port);
  125. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  126. if (result)
  127. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, 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", __FUNCTION__, port->number);
  136. if (serial->dev) {
  137. /* shutdown any bulk reads that might be going on */
  138. if (serial->num_bulk_out)
  139. usb_kill_urb(port->write_urb);
  140. if (serial->num_bulk_in)
  141. usb_kill_urb(port->read_urb);
  142. }
  143. }
  144. void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
  145. {
  146. dbg("%s - port %d", __FUNCTION__, port->number);
  147. generic_cleanup (port);
  148. }
  149. int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  150. {
  151. struct usb_serial *serial = port->serial;
  152. int result;
  153. unsigned char *data;
  154. dbg("%s - port %d", __FUNCTION__, port->number);
  155. if (count == 0) {
  156. dbg("%s - write request of 0 bytes", __FUNCTION__);
  157. return (0);
  158. }
  159. /* only do something if we have a bulk out endpoint */
  160. if (serial->num_bulk_out) {
  161. spin_lock_bh(&port->lock);
  162. if (port->write_urb_busy) {
  163. spin_unlock_bh(&port->lock);
  164. dbg("%s - already writing", __FUNCTION__);
  165. return 0;
  166. }
  167. port->write_urb_busy = 1;
  168. spin_unlock_bh(&port->lock);
  169. count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  170. memcpy (port->write_urb->transfer_buffer, buf, count);
  171. data = port->write_urb->transfer_buffer;
  172. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
  173. /* set up our urb */
  174. usb_fill_bulk_urb (port->write_urb, serial->dev,
  175. usb_sndbulkpipe (serial->dev,
  176. port->bulk_out_endpointAddress),
  177. port->write_urb->transfer_buffer, count,
  178. ((serial->type->write_bulk_callback) ?
  179. serial->type->write_bulk_callback :
  180. usb_serial_generic_write_bulk_callback), port);
  181. /* send the data out the bulk port */
  182. port->write_urb_busy = 1;
  183. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  184. if (result) {
  185. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  186. /* don't have to grab the lock here, as we will retry if != 0 */
  187. port->write_urb_busy = 0;
  188. } else
  189. result = count;
  190. return result;
  191. }
  192. /* no bulk out, so return 0 bytes written */
  193. return 0;
  194. }
  195. int usb_serial_generic_write_room (struct usb_serial_port *port)
  196. {
  197. struct usb_serial *serial = port->serial;
  198. int room = 0;
  199. dbg("%s - port %d", __FUNCTION__, port->number);
  200. if (serial->num_bulk_out) {
  201. if (!(port->write_urb_busy))
  202. room = port->bulk_out_size;
  203. }
  204. dbg("%s - returns %d", __FUNCTION__, room);
  205. return (room);
  206. }
  207. int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
  208. {
  209. struct usb_serial *serial = port->serial;
  210. int chars = 0;
  211. dbg("%s - port %d", __FUNCTION__, port->number);
  212. if (serial->num_bulk_out) {
  213. if (port->write_urb_busy)
  214. chars = port->write_urb->transfer_buffer_length;
  215. }
  216. dbg("%s - returns %d", __FUNCTION__, chars);
  217. return (chars);
  218. }
  219. /* Push data to tty layer and resubmit the bulk read URB */
  220. static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
  221. {
  222. struct usb_serial *serial = port->serial;
  223. struct urb *urb = port->read_urb;
  224. struct tty_struct *tty = port->tty;
  225. int result;
  226. /* Push data to tty */
  227. if (tty && urb->actual_length) {
  228. tty_buffer_request_room(tty, urb->actual_length);
  229. tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
  230. tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
  231. }
  232. /* Continue reading from device */
  233. usb_fill_bulk_urb (port->read_urb, serial->dev,
  234. usb_rcvbulkpipe (serial->dev,
  235. port->bulk_in_endpointAddress),
  236. port->read_urb->transfer_buffer,
  237. port->read_urb->transfer_buffer_length,
  238. ((serial->type->read_bulk_callback) ?
  239. serial->type->read_bulk_callback :
  240. usb_serial_generic_read_bulk_callback), port);
  241. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  242. if (result)
  243. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  244. }
  245. void usb_serial_generic_read_bulk_callback (struct urb *urb)
  246. {
  247. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  248. unsigned char *data = urb->transfer_buffer;
  249. int is_throttled;
  250. unsigned long flags;
  251. dbg("%s - port %d", __FUNCTION__, port->number);
  252. if (urb->status) {
  253. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  254. return;
  255. }
  256. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  257. /* Throttle the device if requested by tty */
  258. if (urb->actual_length) {
  259. spin_lock_irqsave(&port->lock, flags);
  260. is_throttled = port->throttled = port->throttle_req;
  261. spin_unlock_irqrestore(&port->lock, flags);
  262. if (is_throttled) {
  263. /* Let the received data linger in the read URB;
  264. * usb_serial_generic_unthrottle() will pick it
  265. * up later. */
  266. dbg("%s - throttling device", __FUNCTION__);
  267. return;
  268. }
  269. }
  270. /* Handle data and continue reading from device */
  271. flush_and_resubmit_read_urb(port);
  272. }
  273. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  274. void usb_serial_generic_write_bulk_callback (struct urb *urb)
  275. {
  276. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  277. dbg("%s - port %d", __FUNCTION__, port->number);
  278. port->write_urb_busy = 0;
  279. if (urb->status) {
  280. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  281. return;
  282. }
  283. usb_serial_port_softint(port);
  284. }
  285. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  286. void usb_serial_generic_throttle (struct usb_serial_port *port)
  287. {
  288. unsigned long flags;
  289. dbg("%s - port %d", __FUNCTION__, port->number);
  290. /* Set the throttle request flag. It will be picked up
  291. * by usb_serial_generic_read_bulk_callback(). */
  292. spin_lock_irqsave(&port->lock, flags);
  293. port->throttle_req = 1;
  294. spin_unlock_irqrestore(&port->lock, flags);
  295. }
  296. void usb_serial_generic_unthrottle (struct usb_serial_port *port)
  297. {
  298. int was_throttled;
  299. unsigned long flags;
  300. dbg("%s - port %d", __FUNCTION__, port->number);
  301. /* Clear the throttle flags */
  302. spin_lock_irqsave(&port->lock, flags);
  303. was_throttled = port->throttled;
  304. port->throttled = port->throttle_req = 0;
  305. spin_unlock_irqrestore(&port->lock, flags);
  306. if (was_throttled) {
  307. /* Handle pending data and resume reading from device */
  308. flush_and_resubmit_read_urb(port);
  309. }
  310. }
  311. void usb_serial_generic_shutdown (struct usb_serial *serial)
  312. {
  313. int i;
  314. dbg("%s", __FUNCTION__);
  315. /* stop reads and writes on all ports */
  316. for (i=0; i < serial->num_ports; ++i) {
  317. generic_cleanup(serial->port[i]);
  318. }
  319. }