generic.c 11 KB

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