blob: 4e5e993771e9bc6cc8a3b6325c7e3868211f9ad4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Serial Converter Generic functions
3 *
Johan Hovoldd83b4052011-11-06 19:06:37 +01004 * Copyright (C) 2010 - 2011 Johan Hovold (jhovold@gmail.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -070016#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/tty.h>
18#include <linux/tty_flip.h>
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070022#include <linux/usb/serial.h>
Alan Coxae643872008-07-22 11:11:55 +010023#include <linux/uaccess.h>
David VomLehn8e8dce02009-08-28 12:54:27 -070024#include <linux/kfifo.h>
Alan Stern1f871582010-02-17 10:05:47 -050025#include <linux/serial.h>
Johannes Hölzld9b1b782006-12-17 21:50:24 +010026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int debug;
28
29#ifdef CONFIG_USB_SERIAL_GENERIC
David Brownellb46d60f2007-03-23 12:51:55 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static __u16 vendor = 0x05f9;
32static __u16 product = 0xffff;
33
34module_param(vendor, ushort, 0);
35MODULE_PARM_DESC(vendor, "User specified USB idVendor");
36
37module_param(product, ushort, 0);
38MODULE_PARM_DESC(product, "User specified USB idProduct");
39
40static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
41
Johannes Hölzld9b1b782006-12-17 21:50:24 +010042/* we want to look at all devices, as the vendor/product id can change
43 * depending on the command line argument */
Németh Márton7d40d7e2010-01-10 15:34:24 +010044static const struct usb_device_id generic_serial_ids[] = {
Johannes Hölzld9b1b782006-12-17 21:50:24 +010045 {.driver_info = 42},
46 {}
47};
48
49static struct usb_driver generic_driver = {
50 .name = "usbserial_generic",
Johannes Hölzld9b1b782006-12-17 21:50:24 +010051 .id_table = generic_serial_ids,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010052};
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070055struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070056 .driver = {
57 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070058 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070059 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .id_table = generic_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040062 .disconnect = usb_serial_generic_disconnect,
63 .release = usb_serial_generic_release,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010064 .throttle = usb_serial_generic_throttle,
65 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020066 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Alan Stern765e0ba2012-02-23 14:55:59 -050069static struct usb_serial_driver * const serial_drivers[] = {
70 &usb_serial_generic_device, NULL
71};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74
Alan Coxae643872008-07-22 11:11:55 +010075int usb_serial_generic_register(int _debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int retval = 0;
78
79 debug = _debug;
80#ifdef CONFIG_USB_SERIAL_GENERIC
81 generic_device_ids[0].idVendor = vendor;
82 generic_device_ids[0].idProduct = product;
Alan Coxae643872008-07-22 11:11:55 +010083 generic_device_ids[0].match_flags =
84 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* register our generic driver with ourselves */
Alan Stern765e0ba2012-02-23 14:55:59 -050087 retval = usb_serial_register_drivers(&generic_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89 return retval;
90}
91
Alan Coxae643872008-07-22 11:11:55 +010092void usb_serial_generic_deregister(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94#ifdef CONFIG_USB_SERIAL_GENERIC
95 /* remove our generic driver */
Alan Stern765e0ba2012-02-23 14:55:59 -050096 usb_serial_deregister_drivers(&generic_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#endif
98}
99
Alan Coxa509a7e2009-09-19 13:13:26 -0700100int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100103 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100105 /* clear the throttle flags */
106 spin_lock_irqsave(&port->lock, flags);
107 port->throttled = 0;
108 port->throttle_req = 0;
109 spin_unlock_irqrestore(&port->lock, flags);
110
111 /* if we have a bulk endpoint, start reading from it */
Johan Hovold41bd72f2010-03-17 23:05:53 +0100112 if (port->bulk_in_size)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100113 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return result;
116}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700117EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Alan Cox95da3102008-07-22 11:09:07 +0100119static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct usb_serial *serial = port->serial;
Johan Hovoldec3ee502010-03-17 23:00:44 +0100122 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200123 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (serial->dev) {
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100126 /* shutdown any bulk transfers that might be going on */
Johan Hovoldec3ee502010-03-17 23:00:44 +0100127 if (port->bulk_out_size) {
Johan Hovold27c7acf2010-05-05 23:57:37 +0200128 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
129 usb_kill_urb(port->write_urbs[i]);
Johan Hovoldec3ee502010-03-17 23:00:44 +0100130
131 spin_lock_irqsave(&port->lock, flags);
132 kfifo_reset_out(&port->write_fifo);
133 spin_unlock_irqrestore(&port->lock, flags);
134 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100135 if (port->bulk_in_size) {
136 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
137 usb_kill_urb(port->read_urbs[i]);
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140}
141
Alan Cox335f8512009-06-11 12:26:29 +0100142void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Alan Coxae643872008-07-22 11:11:55 +0100144 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
Johan Hovoldf26788d2010-03-17 23:00:45 +0100146EXPORT_SYMBOL_GPL(usb_serial_generic_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100148int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200149 void *dest, size_t size)
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100150{
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200151 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500152}
153
David VomLehn8e8dce02009-08-28 12:54:27 -0700154/**
155 * usb_serial_generic_write_start - kick off an URB write
156 * @port: Pointer to the &struct usb_serial_port data
157 *
Johan Hovold27c7acf2010-05-05 23:57:37 +0200158 * Returns zero on success, or a negative errno value
David VomLehn8e8dce02009-08-28 12:54:27 -0700159 */
160static int usb_serial_generic_write_start(struct usb_serial_port *port)
161{
Johan Hovold27c7acf2010-05-05 23:57:37 +0200162 struct urb *urb;
163 int count, result;
David VomLehn8e8dce02009-08-28 12:54:27 -0700164 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200165 int i;
David VomLehn8e8dce02009-08-28 12:54:27 -0700166
Johan Hovold27c7acf2010-05-05 23:57:37 +0200167 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
168 return 0;
169retry:
David VomLehn8e8dce02009-08-28 12:54:27 -0700170 spin_lock_irqsave(&port->lock, flags);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200171 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
172 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold50a5f702010-03-17 23:06:02 +0100173 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700174 return 0;
Johan Hovold50a5f702010-03-17 23:06:02 +0100175 }
Johan Hovold27c7acf2010-05-05 23:57:37 +0200176 i = (int)find_first_bit(&port->write_urbs_free,
177 ARRAY_SIZE(port->write_urbs));
Johan Hovold50a5f702010-03-17 23:06:02 +0100178 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700179
Johan Hovold27c7acf2010-05-05 23:57:37 +0200180 urb = port->write_urbs[i];
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100181 count = port->serial->type->prepare_write_buffer(port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200182 urb->transfer_buffer,
183 port->bulk_out_size);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200184 urb->transfer_buffer_length = count;
185 usb_serial_debug_data(debug, &port->dev, __func__, count,
186 urb->transfer_buffer);
Johan Hovoldb58af402010-08-04 15:45:57 +0200187 spin_lock_irqsave(&port->lock, flags);
188 port->tx_bytes += count;
189 spin_unlock_irqrestore(&port->lock, flags);
190
191 clear_bit(i, &port->write_urbs_free);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200192 result = usb_submit_urb(urb, GFP_ATOMIC);
David VomLehn8e8dce02009-08-28 12:54:27 -0700193 if (result) {
Johan Hovoldf1475a02012-02-10 13:20:50 +0100194 dev_err_console(port, "%s - error submitting urb: %d\n",
David VomLehn8e8dce02009-08-28 12:54:27 -0700195 __func__, result);
Johan Hovoldb58af402010-08-04 15:45:57 +0200196 set_bit(i, &port->write_urbs_free);
197 spin_lock_irqsave(&port->lock, flags);
198 port->tx_bytes -= count;
199 spin_unlock_irqrestore(&port->lock, flags);
200
Johan Hovold27c7acf2010-05-05 23:57:37 +0200201 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold30af7fb2010-03-17 23:00:42 +0100202 return result;
203 }
Johan Hovold30af7fb2010-03-17 23:00:42 +0100204
Johan Hovold27c7acf2010-05-05 23:57:37 +0200205 /* Try sending off another urb, unless in irq context (in which case
206 * there will be no free urb). */
207 if (!in_irq())
208 goto retry;
209
210 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
211
212 return 0;
David VomLehn8e8dce02009-08-28 12:54:27 -0700213}
214
215/**
216 * usb_serial_generic_write - generic write function for serial USB devices
217 * @tty: Pointer to &struct tty_struct for the device
218 * @port: Pointer to the &usb_serial_port structure for the device
219 * @buf: Pointer to the data to write
220 * @count: Number of bytes to write
221 *
222 * Returns the number of characters actually written, which may be anything
223 * from zero to @count. If an error occurs, it returns the negative errno
224 * value.
225 */
Alan Cox95da3102008-07-22 11:09:07 +0100226int usb_serial_generic_write(struct tty_struct *tty,
227 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100231 /* only do something if we have a bulk out endpoint */
232 if (!port->bulk_out_size)
233 return -ENODEV;
234
Johan Hovold1a1405e2010-03-17 23:06:01 +0100235 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Stefani Seibold119eecc2009-12-23 09:10:48 +0100238 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700239 result = usb_serial_generic_write_start(port);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200240 if (result)
241 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200243 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500245EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Alan Coxae643872008-07-22 11:11:55 +0100247int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Alan Cox95da3102008-07-22 11:09:07 +0100249 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500250 unsigned long flags;
Johan Hovold25d514c2010-03-17 23:06:07 +0100251 int room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100253 if (!port->bulk_out_size)
254 return 0;
255
Jason Wessel715b1dc2009-05-11 15:24:07 -0500256 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200257 room = kfifo_avail(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500258 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Harvey Harrison441b62c2008-03-03 16:08:34 -0800260 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100261 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Alan Cox95da3102008-07-22 11:09:07 +0100264int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Alan Cox95da3102008-07-22 11:09:07 +0100266 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500267 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100268 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100270 if (!port->bulk_out_size)
271 return 0;
272
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100273 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200274 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100275 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Harvey Harrison441b62c2008-03-03 16:08:34 -0800277 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100278 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Johan Hovoldd83b4052011-11-06 19:06:37 +0100281static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
282 int index, gfp_t mem_flags)
283{
284 int res;
285
286 if (!test_and_clear_bit(index, &port->read_urbs_free))
287 return 0;
288
Johan Hovoldc1d830c2012-04-25 16:12:08 +0200289 dbg("%s - port %d, urb %d", __func__, port->number, index);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100290
291 res = usb_submit_urb(port->read_urbs[index], mem_flags);
292 if (res) {
293 if (res != -EPERM) {
294 dev_err(&port->dev,
295 "%s - usb_submit_urb failed: %d\n",
296 __func__, res);
297 }
298 set_bit(index, &port->read_urbs_free);
299 return res;
300 }
301
302 return 0;
303}
304
305int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
Johan Hovold41bd72f2010-03-17 23:05:53 +0100306 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Johan Hovoldd83b4052011-11-06 19:06:37 +0100308 int res;
309 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Johan Hovoldd83b4052011-11-06 19:06:37 +0100311 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
312 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
313 if (res)
314 goto err;
Johan Hovold0ae14742010-02-27 14:05:46 +0100315 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100316
317 return 0;
318err:
319 for (; i >= 0; --i)
320 usb_kill_urb(port->read_urbs[i]);
321
322 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
Johan Hovoldd83b4052011-11-06 19:06:37 +0100324EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100325
Johan Hovold23154322010-03-17 23:05:57 +0100326void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200327{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100328 struct usb_serial_port *port = urb->context;
329 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500330 char *ch = (char *)urb->transfer_buffer;
331 int i;
332
Johan Hovold56a1df42010-05-15 17:53:44 +0200333 if (!urb->actual_length)
334 return;
335
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100336 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500337 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100338 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200339
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100340 /* The per character mucking around with sysrq path it too slow for
341 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
342 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600343 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100344 tty_insert_flip_string(tty, ch, urb->actual_length);
345 else {
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100346 for (i = 0; i < urb->actual_length; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700347 if (!usb_serial_handle_sysrq_char(port, *ch))
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100348 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
349 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200350 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500351 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100352 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200353}
Johan Hovold23154322010-03-17 23:05:57 +0100354EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200355
Alan Cox95da3102008-07-22 11:09:07 +0100356void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100357{
Ming Leicdc97792008-02-24 18:41:47 +0800358 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100359 unsigned char *data = urb->transfer_buffer;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100360 unsigned long flags;
Johan Hovoldd83b4052011-11-06 19:06:37 +0100361 int i;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100362
Johan Hovoldd83b4052011-11-06 19:06:37 +0100363 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
364 if (urb == port->read_urbs[i])
365 break;
366 }
367 set_bit(i, &port->read_urbs_free);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100368
Johan Hovoldc1d830c2012-04-25 16:12:08 +0200369 dbg("%s - port %d, urb %d, len %d", __func__, port->number, i,
Johan Hovoldd83b4052011-11-06 19:06:37 +0100370 urb->actual_length);
371 if (urb->status) {
Johan Hovoldc1d830c2012-04-25 16:12:08 +0200372 dbg("%s - non-zero urb status: %d", __func__, urb->status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100373 return;
374 }
375
Alan Coxae643872008-07-22 11:11:55 +0100376 usb_serial_debug_data(debug, &port->dev, __func__,
377 urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100378 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100379
380 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100381 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100382 port->throttled = port->throttle_req;
383 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800384 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100385 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100386 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800387 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100388}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300389EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Alan Cox95da3102008-07-22 11:09:07 +0100391void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500393 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800394 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700395 int status = urb->status;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200396 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200398 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
399 if (port->write_urbs[i] == urb)
400 break;
401
402 spin_lock_irqsave(&port->lock, flags);
403 port->tx_bytes -= urb->transfer_buffer_length;
404 set_bit(i, &port->write_urbs_free);
405 spin_unlock_irqrestore(&port->lock, flags);
406
407 if (status) {
408 dbg("%s - non-zero urb status: %d", __func__, status);
Johan Hovold25915302010-01-06 15:48:42 -0800409
Jason Wessel715b1dc2009-05-11 15:24:07 -0500410 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200411 kfifo_reset_out(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500412 spin_unlock_irqrestore(&port->lock, flags);
413 } else {
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200414 usb_serial_generic_write_start(port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500415 }
416
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700417 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800419EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Alan Cox95da3102008-07-22 11:09:07 +0100421void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100422{
Alan Cox95da3102008-07-22 11:09:07 +0100423 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100424 unsigned long flags;
425
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100426 /* Set the throttle request flag. It will be picked up
427 * by usb_serial_generic_read_bulk_callback(). */
428 spin_lock_irqsave(&port->lock, flags);
429 port->throttle_req = 1;
430 spin_unlock_irqrestore(&port->lock, flags);
431}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100432EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100433
Alan Cox95da3102008-07-22 11:09:07 +0100434void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100435{
Alan Cox95da3102008-07-22 11:09:07 +0100436 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100437 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100438
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100439 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100440 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100441 was_throttled = port->throttled;
442 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100443 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100444
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100445 if (was_throttled)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100446 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100447}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100448EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100449
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700450#ifdef CONFIG_MAGIC_SYSRQ
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700451int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500452{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600453 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500454 if (ch && time_before(jiffies, port->sysrq)) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700455 handle_sysrq(ch);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500456 port->sysrq = 0;
457 return 1;
458 }
459 port->sysrq = 0;
460 }
461 return 0;
462}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700463#else
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700464int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700465{
466 return 0;
467}
468#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500469EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
470
471int usb_serial_handle_break(struct usb_serial_port *port)
472{
473 if (!port->sysrq) {
474 port->sysrq = jiffies + HZ*5;
475 return 1;
476 }
477 port->sysrq = 0;
478 return 0;
479}
480EXPORT_SYMBOL_GPL(usb_serial_handle_break);
481
Libor Pechacekd14fc1a2011-01-14 14:30:21 +0100482/**
483 * usb_serial_handle_dcd_change - handle a change of carrier detect state
484 * @port: usb_serial_port structure for the open port
485 * @tty: tty_struct structure for the port
486 * @status: new carrier detect status, nonzero if active
487 */
488void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
489 struct tty_struct *tty, unsigned int status)
490{
491 struct tty_port *port = &usb_port->port;
492
493 dbg("%s - port %d, status %d", __func__, usb_port->number, status);
494
495 if (status)
496 wake_up_interruptible(&port->open_wait);
497 else if (tty && !C_CLOCAL(tty))
498 tty_hangup(tty);
499}
500EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
501
David VomLehn8e8dce02009-08-28 12:54:27 -0700502int usb_serial_generic_resume(struct usb_serial *serial)
503{
504 struct usb_serial_port *port;
505 int i, c = 0, r;
506
507 for (i = 0; i < serial->num_ports; i++) {
508 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500509 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700510 continue;
511
Johan Hovoldd83b4052011-11-06 19:06:37 +0100512 if (port->bulk_in_size) {
513 r = usb_serial_generic_submit_read_urbs(port,
514 GFP_NOIO);
David VomLehn8e8dce02009-08-28 12:54:27 -0700515 if (r < 0)
516 c++;
517 }
518
Johan Hovold27c7acf2010-05-05 23:57:37 +0200519 if (port->bulk_out_size) {
David VomLehn8e8dce02009-08-28 12:54:27 -0700520 r = usb_serial_generic_write_start(port);
521 if (r < 0)
522 c++;
523 }
524 }
525
526 return c ? -EIO : 0;
527}
528EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
529
Alan Sternf9c99bb2009-06-02 11:53:55 -0400530void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 int i;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100535 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
Bill Pemberton5c7efeb2010-08-05 17:01:10 -0400538EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Alan Sternf9c99bb2009-06-02 11:53:55 -0400540void usb_serial_generic_release(struct usb_serial *serial)
541{
Alan Sternf9c99bb2009-06-02 11:53:55 -0400542}