generic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <linux/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_ports = 1,
  54. .shutdown = usb_serial_generic_shutdown,
  55. .throttle = usb_serial_generic_throttle,
  56. .unthrottle = usb_serial_generic_unthrottle,
  57. .resume = usb_serial_generic_resume,
  58. };
  59. static int generic_probe(struct usb_interface *interface,
  60. const struct usb_device_id *id)
  61. {
  62. const struct usb_device_id *id_pattern;
  63. id_pattern = usb_match_id(interface, generic_device_ids);
  64. if (id_pattern != NULL)
  65. return usb_serial_probe(interface, id);
  66. return -ENODEV;
  67. }
  68. #endif
  69. int usb_serial_generic_register(int _debug)
  70. {
  71. int retval = 0;
  72. debug = _debug;
  73. #ifdef CONFIG_USB_SERIAL_GENERIC
  74. generic_device_ids[0].idVendor = vendor;
  75. generic_device_ids[0].idProduct = product;
  76. generic_device_ids[0].match_flags =
  77. USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  78. /* register our generic driver with ourselves */
  79. retval = usb_serial_register(&usb_serial_generic_device);
  80. if (retval)
  81. goto exit;
  82. retval = usb_register(&generic_driver);
  83. if (retval)
  84. usb_serial_deregister(&usb_serial_generic_device);
  85. exit:
  86. #endif
  87. return retval;
  88. }
  89. void usb_serial_generic_deregister(void)
  90. {
  91. #ifdef CONFIG_USB_SERIAL_GENERIC
  92. /* remove our generic driver */
  93. usb_deregister(&generic_driver);
  94. usb_serial_deregister(&usb_serial_generic_device);
  95. #endif
  96. }
  97. int usb_serial_generic_open(struct tty_struct *tty,
  98. 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", __func__, port->number);
  104. /* force low_latency on so that our tty_push actually forces the data
  105. through, otherwise it is scheduled, and with high data rates (like
  106. with OHCI) data can get lost. */
  107. if (tty)
  108. 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,
  119. 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,
  129. "%s - failed resubmitting read urb, error %d\n",
  130. __func__, result);
  131. }
  132. return result;
  133. }
  134. EXPORT_SYMBOL_GPL(usb_serial_generic_open);
  135. static void generic_cleanup(struct usb_serial_port *port)
  136. {
  137. struct usb_serial *serial = port->serial;
  138. dbg("%s - port %d", __func__, port->number);
  139. if (serial->dev) {
  140. /* shutdown any bulk reads that might be going on */
  141. if (serial->num_bulk_out)
  142. usb_kill_urb(port->write_urb);
  143. if (serial->num_bulk_in)
  144. usb_kill_urb(port->read_urb);
  145. }
  146. }
  147. int usb_serial_generic_resume(struct usb_serial *serial)
  148. {
  149. struct usb_serial_port *port;
  150. int i, c = 0, r;
  151. #ifdef CONFIG_PM
  152. /*
  153. * If this is an autoresume, don't submit URBs.
  154. * They will be submitted in the open function instead.
  155. */
  156. if (serial->dev->auto_pm)
  157. return 0;
  158. #endif
  159. for (i = 0; i < serial->num_ports; i++) {
  160. port = serial->port[i];
  161. if (port->port.count && port->read_urb) {
  162. r = usb_submit_urb(port->read_urb, GFP_NOIO);
  163. if (r < 0)
  164. c++;
  165. }
  166. }
  167. return c ? -EIO : 0;
  168. }
  169. void usb_serial_generic_close(struct tty_struct *tty,
  170. struct usb_serial_port *port, struct file *filp)
  171. {
  172. dbg("%s - port %d", __func__, port->number);
  173. generic_cleanup(port);
  174. }
  175. int usb_serial_generic_write(struct tty_struct *tty,
  176. struct usb_serial_port *port, const unsigned char *buf, int count)
  177. {
  178. struct usb_serial *serial = port->serial;
  179. int result;
  180. unsigned char *data;
  181. dbg("%s - port %d", __func__, port->number);
  182. if (count == 0) {
  183. dbg("%s - write request of 0 bytes", __func__);
  184. return 0;
  185. }
  186. /* only do something if we have a bulk out endpoint */
  187. if (serial->num_bulk_out) {
  188. unsigned long flags;
  189. spin_lock_irqsave(&port->lock, flags);
  190. if (port->write_urb_busy) {
  191. spin_unlock_irqrestore(&port->lock, flags);
  192. dbg("%s - already writing", __func__);
  193. return 0;
  194. }
  195. port->write_urb_busy = 1;
  196. spin_unlock_irqrestore(&port->lock, flags);
  197. count = (count > port->bulk_out_size) ?
  198. port->bulk_out_size : count;
  199. memcpy(port->write_urb->transfer_buffer, buf, count);
  200. data = port->write_urb->transfer_buffer;
  201. usb_serial_debug_data(debug, &port->dev, __func__, count, data);
  202. /* set up our urb */
  203. usb_fill_bulk_urb(port->write_urb, serial->dev,
  204. usb_sndbulkpipe(serial->dev,
  205. port->bulk_out_endpointAddress),
  206. port->write_urb->transfer_buffer, count,
  207. ((serial->type->write_bulk_callback) ?
  208. serial->type->write_bulk_callback :
  209. usb_serial_generic_write_bulk_callback),
  210. port);
  211. /* send the data out the bulk port */
  212. port->write_urb_busy = 1;
  213. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  214. if (result) {
  215. dev_err(&port->dev,
  216. "%s - failed submitting write urb, error %d\n",
  217. __func__, result);
  218. /* don't have to grab the lock here, as we will
  219. retry if != 0 */
  220. port->write_urb_busy = 0;
  221. } else
  222. result = count;
  223. return result;
  224. }
  225. /* no bulk out, so return 0 bytes written */
  226. return 0;
  227. }
  228. int usb_serial_generic_write_room(struct tty_struct *tty)
  229. {
  230. struct usb_serial_port *port = tty->driver_data;
  231. struct usb_serial *serial = port->serial;
  232. int room = 0;
  233. dbg("%s - port %d", __func__, port->number);
  234. /* FIXME: Locking */
  235. if (serial->num_bulk_out) {
  236. if (!(port->write_urb_busy))
  237. room = port->bulk_out_size;
  238. }
  239. dbg("%s - returns %d", __func__, room);
  240. return room;
  241. }
  242. int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
  243. {
  244. struct usb_serial_port *port = tty->driver_data;
  245. struct usb_serial *serial = port->serial;
  246. int chars = 0;
  247. dbg("%s - port %d", __func__, port->number);
  248. /* FIXME: Locking */
  249. if (serial->num_bulk_out) {
  250. if (port->write_urb_busy)
  251. chars = port->write_urb->transfer_buffer_length;
  252. }
  253. dbg("%s - returns %d", __func__, chars);
  254. return chars;
  255. }
  256. static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
  257. {
  258. struct urb *urb = port->read_urb;
  259. struct usb_serial *serial = port->serial;
  260. int result;
  261. /* Continue reading from device */
  262. usb_fill_bulk_urb(urb, serial->dev,
  263. usb_rcvbulkpipe(serial->dev,
  264. port->bulk_in_endpointAddress),
  265. urb->transfer_buffer,
  266. urb->transfer_buffer_length,
  267. ((serial->type->read_bulk_callback) ?
  268. serial->type->read_bulk_callback :
  269. usb_serial_generic_read_bulk_callback), port);
  270. result = usb_submit_urb(urb, mem_flags);
  271. if (result)
  272. dev_err(&port->dev,
  273. "%s - failed resubmitting read urb, error %d\n",
  274. __func__, result);
  275. }
  276. /* Push data to tty layer and resubmit the bulk read URB */
  277. static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
  278. {
  279. struct urb *urb = port->read_urb;
  280. struct tty_struct *tty = tty_port_tty_get(&port->port);
  281. int room;
  282. /* Push data to tty */
  283. if (tty && urb->actual_length) {
  284. room = tty_buffer_request_room(tty, urb->actual_length);
  285. if (room) {
  286. tty_insert_flip_string(tty, urb->transfer_buffer, room);
  287. tty_flip_buffer_push(tty);
  288. }
  289. }
  290. tty_kref_put(tty);
  291. resubmit_read_urb(port, GFP_ATOMIC);
  292. }
  293. void usb_serial_generic_read_bulk_callback(struct urb *urb)
  294. {
  295. struct usb_serial_port *port = urb->context;
  296. unsigned char *data = urb->transfer_buffer;
  297. int status = urb->status;
  298. unsigned long flags;
  299. dbg("%s - port %d", __func__, port->number);
  300. if (unlikely(status != 0)) {
  301. dbg("%s - nonzero read bulk status received: %d",
  302. __func__, status);
  303. return;
  304. }
  305. usb_serial_debug_data(debug, &port->dev, __func__,
  306. urb->actual_length, data);
  307. /* Throttle the device if requested by tty */
  308. spin_lock_irqsave(&port->lock, flags);
  309. port->throttled = port->throttle_req;
  310. if (!port->throttled) {
  311. spin_unlock_irqrestore(&port->lock, flags);
  312. flush_and_resubmit_read_urb(port);
  313. } else
  314. spin_unlock_irqrestore(&port->lock, flags);
  315. }
  316. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  317. void usb_serial_generic_write_bulk_callback(struct urb *urb)
  318. {
  319. struct usb_serial_port *port = urb->context;
  320. int status = urb->status;
  321. dbg("%s - port %d", __func__, port->number);
  322. port->write_urb_busy = 0;
  323. if (status) {
  324. dbg("%s - nonzero write bulk status received: %d",
  325. __func__, status);
  326. return;
  327. }
  328. usb_serial_port_softint(port);
  329. }
  330. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  331. void usb_serial_generic_throttle(struct tty_struct *tty)
  332. {
  333. struct usb_serial_port *port = tty->driver_data;
  334. unsigned long flags;
  335. dbg("%s - port %d", __func__, port->number);
  336. /* Set the throttle request flag. It will be picked up
  337. * by usb_serial_generic_read_bulk_callback(). */
  338. spin_lock_irqsave(&port->lock, flags);
  339. port->throttle_req = 1;
  340. spin_unlock_irqrestore(&port->lock, flags);
  341. }
  342. void usb_serial_generic_unthrottle(struct tty_struct *tty)
  343. {
  344. struct usb_serial_port *port = tty->driver_data;
  345. int was_throttled;
  346. unsigned long flags;
  347. dbg("%s - port %d", __func__, port->number);
  348. /* Clear the throttle flags */
  349. spin_lock_irqsave(&port->lock, flags);
  350. was_throttled = port->throttled;
  351. port->throttled = port->throttle_req = 0;
  352. spin_unlock_irqrestore(&port->lock, flags);
  353. if (was_throttled) {
  354. /* Resume reading from device */
  355. resubmit_read_urb(port, GFP_KERNEL);
  356. }
  357. }
  358. void usb_serial_generic_shutdown(struct usb_serial *serial)
  359. {
  360. int i;
  361. dbg("%s", __func__);
  362. /* stop reads and writes on all ports */
  363. for (i = 0; i < serial->num_ports; ++i)
  364. generic_cleanup(serial->port[i]);
  365. }