generic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. unsigned long flags;
  177. spin_lock_irqsave(&port->lock, flags);
  178. if (port->write_urb_busy) {
  179. spin_unlock_irqrestore(&port->lock, flags);
  180. dbg("%s - already writing", __FUNCTION__);
  181. return 0;
  182. }
  183. port->write_urb_busy = 1;
  184. spin_unlock_irqrestore(&port->lock, flags);
  185. count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  186. memcpy (port->write_urb->transfer_buffer, buf, count);
  187. data = port->write_urb->transfer_buffer;
  188. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
  189. /* set up our urb */
  190. usb_fill_bulk_urb (port->write_urb, serial->dev,
  191. usb_sndbulkpipe (serial->dev,
  192. port->bulk_out_endpointAddress),
  193. port->write_urb->transfer_buffer, count,
  194. ((serial->type->write_bulk_callback) ?
  195. serial->type->write_bulk_callback :
  196. usb_serial_generic_write_bulk_callback), port);
  197. /* send the data out the bulk port */
  198. port->write_urb_busy = 1;
  199. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  200. if (result) {
  201. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  202. /* don't have to grab the lock here, as we will retry if != 0 */
  203. port->write_urb_busy = 0;
  204. } else
  205. result = count;
  206. return result;
  207. }
  208. /* no bulk out, so return 0 bytes written */
  209. return 0;
  210. }
  211. int usb_serial_generic_write_room (struct usb_serial_port *port)
  212. {
  213. struct usb_serial *serial = port->serial;
  214. int room = 0;
  215. dbg("%s - port %d", __FUNCTION__, port->number);
  216. if (serial->num_bulk_out) {
  217. if (!(port->write_urb_busy))
  218. room = port->bulk_out_size;
  219. }
  220. dbg("%s - returns %d", __FUNCTION__, room);
  221. return (room);
  222. }
  223. int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
  224. {
  225. struct usb_serial *serial = port->serial;
  226. int chars = 0;
  227. dbg("%s - port %d", __FUNCTION__, port->number);
  228. if (serial->num_bulk_out) {
  229. if (port->write_urb_busy)
  230. chars = port->write_urb->transfer_buffer_length;
  231. }
  232. dbg("%s - returns %d", __FUNCTION__, chars);
  233. return (chars);
  234. }
  235. static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
  236. {
  237. struct urb *urb = port->read_urb;
  238. struct usb_serial *serial = port->serial;
  239. int result;
  240. /* Continue reading from device */
  241. usb_fill_bulk_urb (urb, serial->dev,
  242. usb_rcvbulkpipe (serial->dev,
  243. port->bulk_in_endpointAddress),
  244. urb->transfer_buffer,
  245. urb->transfer_buffer_length,
  246. ((serial->type->read_bulk_callback) ?
  247. serial->type->read_bulk_callback :
  248. usb_serial_generic_read_bulk_callback), port);
  249. result = usb_submit_urb(urb, mem_flags);
  250. if (result)
  251. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  252. }
  253. /* Push data to tty layer and resubmit the bulk read URB */
  254. static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
  255. {
  256. struct urb *urb = port->read_urb;
  257. struct tty_struct *tty = port->tty;
  258. int room;
  259. /* Push data to tty */
  260. if (tty && urb->actual_length) {
  261. room = tty_buffer_request_room(tty, urb->actual_length);
  262. if (room) {
  263. tty_insert_flip_string(tty, urb->transfer_buffer, room);
  264. tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
  265. }
  266. }
  267. resubmit_read_urb(port, GFP_ATOMIC);
  268. }
  269. void usb_serial_generic_read_bulk_callback (struct urb *urb)
  270. {
  271. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  272. unsigned char *data = urb->transfer_buffer;
  273. int status = urb->status;
  274. dbg("%s - port %d", __FUNCTION__, port->number);
  275. if (unlikely(status != 0)) {
  276. dbg("%s - nonzero read bulk status received: %d",
  277. __FUNCTION__, status);
  278. return;
  279. }
  280. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  281. /* Throttle the device if requested by tty */
  282. spin_lock(&port->lock);
  283. if (!(port->throttled = port->throttle_req))
  284. /* Handle data and continue reading from device */
  285. flush_and_resubmit_read_urb(port);
  286. spin_unlock(&port->lock);
  287. }
  288. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  289. void usb_serial_generic_write_bulk_callback (struct urb *urb)
  290. {
  291. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  292. int status = urb->status;
  293. dbg("%s - port %d", __FUNCTION__, port->number);
  294. port->write_urb_busy = 0;
  295. if (status) {
  296. dbg("%s - nonzero write bulk status received: %d",
  297. __FUNCTION__, status);
  298. return;
  299. }
  300. usb_serial_port_softint(port);
  301. }
  302. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  303. void usb_serial_generic_throttle (struct usb_serial_port *port)
  304. {
  305. unsigned long flags;
  306. dbg("%s - port %d", __FUNCTION__, port->number);
  307. /* Set the throttle request flag. It will be picked up
  308. * by usb_serial_generic_read_bulk_callback(). */
  309. spin_lock_irqsave(&port->lock, flags);
  310. port->throttle_req = 1;
  311. spin_unlock_irqrestore(&port->lock, flags);
  312. }
  313. void usb_serial_generic_unthrottle (struct usb_serial_port *port)
  314. {
  315. int was_throttled;
  316. unsigned long flags;
  317. dbg("%s - port %d", __FUNCTION__, port->number);
  318. /* Clear the throttle flags */
  319. spin_lock_irqsave(&port->lock, flags);
  320. was_throttled = port->throttled;
  321. port->throttled = port->throttle_req = 0;
  322. spin_unlock_irqrestore(&port->lock, flags);
  323. if (was_throttled) {
  324. /* Resume reading from device */
  325. resubmit_read_urb(port, GFP_KERNEL);
  326. }
  327. }
  328. void usb_serial_generic_shutdown (struct usb_serial *serial)
  329. {
  330. int i;
  331. dbg("%s", __FUNCTION__);
  332. /* stop reads and writes on all ports */
  333. for (i=0; i < serial->num_ports; ++i) {
  334. generic_cleanup(serial->port[i]);
  335. }
  336. }