generic.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/usb.h>
  20. #include <asm/uaccess.h>
  21. #include "usb-serial.h"
  22. static int debug;
  23. #ifdef CONFIG_USB_SERIAL_GENERIC
  24. static __u16 vendor = 0x05f9;
  25. static __u16 product = 0xffff;
  26. module_param(vendor, ushort, 0);
  27. MODULE_PARM_DESC(vendor, "User specified USB idVendor");
  28. module_param(product, ushort, 0);
  29. MODULE_PARM_DESC(product, "User specified USB idProduct");
  30. static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
  31. /* All of the device info needed for the Generic Serial Converter */
  32. struct usb_serial_device_type usb_serial_generic_device = {
  33. .owner = THIS_MODULE,
  34. .name = "Generic",
  35. .short_name = "generic",
  36. .id_table = generic_device_ids,
  37. .num_interrupt_in = NUM_DONT_CARE,
  38. .num_bulk_in = NUM_DONT_CARE,
  39. .num_bulk_out = NUM_DONT_CARE,
  40. .num_ports = 1,
  41. .shutdown = usb_serial_generic_shutdown,
  42. };
  43. /* we want to look at all devices, as the vendor/product id can change
  44. * depending on the command line argument */
  45. static struct usb_device_id generic_serial_ids[] = {
  46. {.driver_info = 42},
  47. {}
  48. };
  49. static int generic_probe(struct usb_interface *interface,
  50. const struct usb_device_id *id)
  51. {
  52. const struct usb_device_id *id_pattern;
  53. id_pattern = usb_match_id(interface, generic_device_ids);
  54. if (id_pattern != NULL)
  55. return usb_serial_probe(interface, id);
  56. return -ENODEV;
  57. }
  58. static struct usb_driver generic_driver = {
  59. .owner = THIS_MODULE,
  60. .name = "usbserial_generic",
  61. .probe = generic_probe,
  62. .disconnect = usb_serial_disconnect,
  63. .id_table = generic_serial_ids,
  64. };
  65. #endif
  66. int usb_serial_generic_register (int _debug)
  67. {
  68. int retval = 0;
  69. debug = _debug;
  70. #ifdef CONFIG_USB_SERIAL_GENERIC
  71. generic_device_ids[0].idVendor = vendor;
  72. generic_device_ids[0].idProduct = product;
  73. generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  74. /* register our generic driver with ourselves */
  75. retval = usb_serial_register (&usb_serial_generic_device);
  76. if (retval)
  77. goto exit;
  78. retval = usb_register(&generic_driver);
  79. if (retval)
  80. usb_serial_deregister(&usb_serial_generic_device);
  81. exit:
  82. #endif
  83. return retval;
  84. }
  85. void usb_serial_generic_deregister (void)
  86. {
  87. #ifdef CONFIG_USB_SERIAL_GENERIC
  88. /* remove our generic driver */
  89. usb_deregister(&generic_driver);
  90. usb_serial_deregister (&usb_serial_generic_device);
  91. #endif
  92. }
  93. int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
  94. {
  95. struct usb_serial *serial = port->serial;
  96. int result = 0;
  97. dbg("%s - port %d", __FUNCTION__, port->number);
  98. /* force low_latency on so that our tty_push actually forces the data through,
  99. otherwise it is scheduled, and with high data rates (like with OHCI) data
  100. can get lost. */
  101. if (port->tty)
  102. port->tty->low_latency = 1;
  103. /* if we have a bulk interrupt, start reading from it */
  104. if (serial->num_bulk_in) {
  105. /* Start reading from the device */
  106. usb_fill_bulk_urb (port->read_urb, serial->dev,
  107. usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  108. port->read_urb->transfer_buffer,
  109. port->read_urb->transfer_buffer_length,
  110. ((serial->type->read_bulk_callback) ?
  111. serial->type->read_bulk_callback :
  112. usb_serial_generic_read_bulk_callback),
  113. port);
  114. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  115. if (result)
  116. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  117. }
  118. return result;
  119. }
  120. static void generic_cleanup (struct usb_serial_port *port)
  121. {
  122. struct usb_serial *serial = port->serial;
  123. dbg("%s - port %d", __FUNCTION__, port->number);
  124. if (serial->dev) {
  125. /* shutdown any bulk reads that might be going on */
  126. if (serial->num_bulk_out)
  127. usb_kill_urb(port->write_urb);
  128. if (serial->num_bulk_in)
  129. usb_kill_urb(port->read_urb);
  130. }
  131. }
  132. void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
  133. {
  134. dbg("%s - port %d", __FUNCTION__, port->number);
  135. generic_cleanup (port);
  136. }
  137. int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  138. {
  139. struct usb_serial *serial = port->serial;
  140. int result;
  141. unsigned char *data;
  142. dbg("%s - port %d", __FUNCTION__, port->number);
  143. if (count == 0) {
  144. dbg("%s - write request of 0 bytes", __FUNCTION__);
  145. return (0);
  146. }
  147. /* only do something if we have a bulk out endpoint */
  148. if (serial->num_bulk_out) {
  149. spin_lock(&port->lock);
  150. if (port->write_urb_busy) {
  151. spin_unlock(&port->lock);
  152. dbg("%s - already writing", __FUNCTION__);
  153. return 0;
  154. }
  155. port->write_urb_busy = 1;
  156. spin_unlock(&port->lock);
  157. count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  158. memcpy (port->write_urb->transfer_buffer, buf, count);
  159. data = port->write_urb->transfer_buffer;
  160. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
  161. /* set up our urb */
  162. usb_fill_bulk_urb (port->write_urb, serial->dev,
  163. usb_sndbulkpipe (serial->dev,
  164. port->bulk_out_endpointAddress),
  165. port->write_urb->transfer_buffer, count,
  166. ((serial->type->write_bulk_callback) ?
  167. serial->type->write_bulk_callback :
  168. usb_serial_generic_write_bulk_callback), port);
  169. /* send the data out the bulk port */
  170. port->write_urb_busy = 1;
  171. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  172. if (result) {
  173. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  174. /* don't have to grab the lock here, as we will retry if != 0 */
  175. port->write_urb_busy = 0;
  176. } else
  177. result = count;
  178. return result;
  179. }
  180. /* no bulk out, so return 0 bytes written */
  181. return 0;
  182. }
  183. int usb_serial_generic_write_room (struct usb_serial_port *port)
  184. {
  185. struct usb_serial *serial = port->serial;
  186. int room = 0;
  187. dbg("%s - port %d", __FUNCTION__, port->number);
  188. if (serial->num_bulk_out) {
  189. if (port->write_urb_busy)
  190. room = port->bulk_out_size;
  191. }
  192. dbg("%s - returns %d", __FUNCTION__, room);
  193. return (room);
  194. }
  195. int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
  196. {
  197. struct usb_serial *serial = port->serial;
  198. int chars = 0;
  199. dbg("%s - port %d", __FUNCTION__, port->number);
  200. if (serial->num_bulk_out) {
  201. if (port->write_urb_busy)
  202. chars = port->write_urb->transfer_buffer_length;
  203. }
  204. dbg("%s - returns %d", __FUNCTION__, chars);
  205. return (chars);
  206. }
  207. void usb_serial_generic_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  208. {
  209. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  210. struct usb_serial *serial = port->serial;
  211. struct tty_struct *tty;
  212. unsigned char *data = urb->transfer_buffer;
  213. int i;
  214. int result;
  215. dbg("%s - port %d", __FUNCTION__, port->number);
  216. if (urb->status) {
  217. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  218. return;
  219. }
  220. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  221. tty = port->tty;
  222. if (tty && urb->actual_length) {
  223. for (i = 0; i < urb->actual_length ; ++i) {
  224. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  225. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  226. tty_flip_buffer_push(tty);
  227. }
  228. /* this doesn't actually push the data through unless tty->low_latency is set */
  229. tty_insert_flip_char(tty, data[i], 0);
  230. }
  231. tty_flip_buffer_push(tty);
  232. }
  233. /* Continue trying to always read */
  234. usb_fill_bulk_urb (port->read_urb, serial->dev,
  235. usb_rcvbulkpipe (serial->dev,
  236. port->bulk_in_endpointAddress),
  237. port->read_urb->transfer_buffer,
  238. port->read_urb->transfer_buffer_length,
  239. ((serial->type->read_bulk_callback) ?
  240. serial->type->read_bulk_callback :
  241. usb_serial_generic_read_bulk_callback), port);
  242. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  243. if (result)
  244. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  245. }
  246. void usb_serial_generic_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
  247. {
  248. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  249. dbg("%s - port %d", __FUNCTION__, port->number);
  250. port->write_urb_busy = 0;
  251. if (urb->status) {
  252. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  253. return;
  254. }
  255. usb_serial_port_softint((void *)port);
  256. schedule_work(&port->work);
  257. }
  258. void usb_serial_generic_shutdown (struct usb_serial *serial)
  259. {
  260. int i;
  261. dbg("%s", __FUNCTION__);
  262. /* stop reads and writes on all ports */
  263. for (i=0; i < serial->num_ports; ++i) {
  264. generic_cleanup(serial->port[i]);
  265. }
  266. }