blob: 47e228030922343e17de9b35b25bf475d73a9b64 [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
42/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070043struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070044 .driver = {
45 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070046 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070047 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 .id_table = generic_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040050 .disconnect = usb_serial_generic_disconnect,
51 .release = usb_serial_generic_release,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010052 .throttle = usb_serial_generic_throttle,
53 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020054 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
Alan Stern765e0ba2012-02-23 14:55:59 -050057static struct usb_serial_driver * const serial_drivers[] = {
58 &usb_serial_generic_device, NULL
59};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
Alan Coxae643872008-07-22 11:11:55 +010063int usb_serial_generic_register(int _debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 int retval = 0;
66
67 debug = _debug;
68#ifdef CONFIG_USB_SERIAL_GENERIC
69 generic_device_ids[0].idVendor = vendor;
70 generic_device_ids[0].idProduct = product;
Alan Coxae643872008-07-22 11:11:55 +010071 generic_device_ids[0].match_flags =
72 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /* register our generic driver with ourselves */
Alan Stern0b847042012-05-31 17:03:52 -040075 retval = usb_serial_register_drivers(serial_drivers,
76 "usbserial_generic", generic_device_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78 return retval;
79}
80
Alan Coxae643872008-07-22 11:11:55 +010081void usb_serial_generic_deregister(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83#ifdef CONFIG_USB_SERIAL_GENERIC
84 /* remove our generic driver */
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070085 usb_serial_deregister_drivers(serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#endif
87}
88
Alan Coxa509a7e2009-09-19 13:13:26 -070089int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +010092 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Joris van Rantwijk253ca922007-02-01 20:08:18 +010094 /* clear the throttle flags */
95 spin_lock_irqsave(&port->lock, flags);
96 port->throttled = 0;
97 port->throttle_req = 0;
98 spin_unlock_irqrestore(&port->lock, flags);
99
100 /* if we have a bulk endpoint, start reading from it */
Johan Hovold41bd72f2010-03-17 23:05:53 +0100101 if (port->bulk_in_size)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100102 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 return result;
105}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700106EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Alan Cox95da3102008-07-22 11:09:07 +0100108static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct usb_serial *serial = port->serial;
Johan Hovoldec3ee502010-03-17 23:00:44 +0100111 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200112 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (serial->dev) {
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100115 /* shutdown any bulk transfers that might be going on */
Johan Hovoldec3ee502010-03-17 23:00:44 +0100116 if (port->bulk_out_size) {
Johan Hovold27c7acf2010-05-05 23:57:37 +0200117 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
118 usb_kill_urb(port->write_urbs[i]);
Johan Hovoldec3ee502010-03-17 23:00:44 +0100119
120 spin_lock_irqsave(&port->lock, flags);
121 kfifo_reset_out(&port->write_fifo);
122 spin_unlock_irqrestore(&port->lock, flags);
123 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100124 if (port->bulk_in_size) {
125 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
126 usb_kill_urb(port->read_urbs[i]);
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129}
130
Alan Cox335f8512009-06-11 12:26:29 +0100131void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Alan Coxae643872008-07-22 11:11:55 +0100133 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
Johan Hovoldf26788d2010-03-17 23:00:45 +0100135EXPORT_SYMBOL_GPL(usb_serial_generic_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100137int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200138 void *dest, size_t size)
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100139{
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200140 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500141}
142
David VomLehn8e8dce02009-08-28 12:54:27 -0700143/**
144 * usb_serial_generic_write_start - kick off an URB write
145 * @port: Pointer to the &struct usb_serial_port data
146 *
Johan Hovold27c7acf2010-05-05 23:57:37 +0200147 * Returns zero on success, or a negative errno value
David VomLehn8e8dce02009-08-28 12:54:27 -0700148 */
149static int usb_serial_generic_write_start(struct usb_serial_port *port)
150{
Johan Hovold27c7acf2010-05-05 23:57:37 +0200151 struct urb *urb;
152 int count, result;
David VomLehn8e8dce02009-08-28 12:54:27 -0700153 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200154 int i;
David VomLehn8e8dce02009-08-28 12:54:27 -0700155
Johan Hovold27c7acf2010-05-05 23:57:37 +0200156 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
157 return 0;
158retry:
David VomLehn8e8dce02009-08-28 12:54:27 -0700159 spin_lock_irqsave(&port->lock, flags);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200160 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
161 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold50a5f702010-03-17 23:06:02 +0100162 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700163 return 0;
Johan Hovold50a5f702010-03-17 23:06:02 +0100164 }
Johan Hovold27c7acf2010-05-05 23:57:37 +0200165 i = (int)find_first_bit(&port->write_urbs_free,
166 ARRAY_SIZE(port->write_urbs));
Johan Hovold50a5f702010-03-17 23:06:02 +0100167 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700168
Johan Hovold27c7acf2010-05-05 23:57:37 +0200169 urb = port->write_urbs[i];
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100170 count = port->serial->type->prepare_write_buffer(port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200171 urb->transfer_buffer,
172 port->bulk_out_size);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200173 urb->transfer_buffer_length = count;
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100174 usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
Johan Hovoldb58af402010-08-04 15:45:57 +0200175 spin_lock_irqsave(&port->lock, flags);
176 port->tx_bytes += count;
177 spin_unlock_irqrestore(&port->lock, flags);
178
179 clear_bit(i, &port->write_urbs_free);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200180 result = usb_submit_urb(urb, GFP_ATOMIC);
David VomLehn8e8dce02009-08-28 12:54:27 -0700181 if (result) {
Johan Hovoldf1475a02012-02-10 13:20:50 +0100182 dev_err_console(port, "%s - error submitting urb: %d\n",
David VomLehn8e8dce02009-08-28 12:54:27 -0700183 __func__, result);
Johan Hovoldb58af402010-08-04 15:45:57 +0200184 set_bit(i, &port->write_urbs_free);
185 spin_lock_irqsave(&port->lock, flags);
186 port->tx_bytes -= count;
187 spin_unlock_irqrestore(&port->lock, flags);
188
Johan Hovold27c7acf2010-05-05 23:57:37 +0200189 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold30af7fb2010-03-17 23:00:42 +0100190 return result;
191 }
Johan Hovold30af7fb2010-03-17 23:00:42 +0100192
Johan Hovold27c7acf2010-05-05 23:57:37 +0200193 /* Try sending off another urb, unless in irq context (in which case
194 * there will be no free urb). */
195 if (!in_irq())
196 goto retry;
197
198 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
199
200 return 0;
David VomLehn8e8dce02009-08-28 12:54:27 -0700201}
202
203/**
204 * usb_serial_generic_write - generic write function for serial USB devices
205 * @tty: Pointer to &struct tty_struct for the device
206 * @port: Pointer to the &usb_serial_port structure for the device
207 * @buf: Pointer to the data to write
208 * @count: Number of bytes to write
209 *
210 * Returns the number of characters actually written, which may be anything
211 * from zero to @count. If an error occurs, it returns the negative errno
212 * value.
213 */
Alan Cox95da3102008-07-22 11:09:07 +0100214int usb_serial_generic_write(struct tty_struct *tty,
215 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100219 /* only do something if we have a bulk out endpoint */
220 if (!port->bulk_out_size)
221 return -ENODEV;
222
Johan Hovold1a1405e2010-03-17 23:06:01 +0100223 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Stefani Seibold119eecc2009-12-23 09:10:48 +0100226 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700227 result = usb_serial_generic_write_start(port);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200228 if (result)
229 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200231 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500233EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Alan Coxae643872008-07-22 11:11:55 +0100235int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Alan Cox95da3102008-07-22 11:09:07 +0100237 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500238 unsigned long flags;
Johan Hovold25d514c2010-03-17 23:06:07 +0100239 int room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100241 if (!port->bulk_out_size)
242 return 0;
243
Jason Wessel715b1dc2009-05-11 15:24:07 -0500244 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200245 room = kfifo_avail(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500246 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700248 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100249 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Alan Cox95da3102008-07-22 11:09:07 +0100252int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Alan Cox95da3102008-07-22 11:09:07 +0100254 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500255 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100256 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100258 if (!port->bulk_out_size)
259 return 0;
260
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100261 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200262 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100263 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700265 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100266 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Johan Hovoldd83b4052011-11-06 19:06:37 +0100269static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
270 int index, gfp_t mem_flags)
271{
272 int res;
273
274 if (!test_and_clear_bit(index, &port->read_urbs_free))
275 return 0;
276
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700277 dev_dbg(&port->dev, "%s - port %d, urb %d\n", __func__,
278 port->number, index);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100279
280 res = usb_submit_urb(port->read_urbs[index], mem_flags);
281 if (res) {
282 if (res != -EPERM) {
283 dev_err(&port->dev,
284 "%s - usb_submit_urb failed: %d\n",
285 __func__, res);
286 }
287 set_bit(index, &port->read_urbs_free);
288 return res;
289 }
290
291 return 0;
292}
293
294int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
Johan Hovold41bd72f2010-03-17 23:05:53 +0100295 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Johan Hovoldd83b4052011-11-06 19:06:37 +0100297 int res;
298 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Johan Hovoldd83b4052011-11-06 19:06:37 +0100300 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
301 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
302 if (res)
303 goto err;
Johan Hovold0ae14742010-02-27 14:05:46 +0100304 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100305
306 return 0;
307err:
308 for (; i >= 0; --i)
309 usb_kill_urb(port->read_urbs[i]);
310
311 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
Johan Hovoldd83b4052011-11-06 19:06:37 +0100313EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100314
Johan Hovold23154322010-03-17 23:05:57 +0100315void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200316{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100317 struct usb_serial_port *port = urb->context;
318 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500319 char *ch = (char *)urb->transfer_buffer;
320 int i;
321
Johan Hovold56a1df42010-05-15 17:53:44 +0200322 if (!urb->actual_length)
323 return;
324
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100325 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500326 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100327 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200328
Alan Cox4cd1de02009-07-09 13:35:52 +0100329 /* The per character mucking around with sysrq path it too slow for
330 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
331 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600332 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de02009-07-09 13:35:52 +0100333 tty_insert_flip_string(tty, ch, urb->actual_length);
334 else {
Alan Cox4cd1de02009-07-09 13:35:52 +0100335 for (i = 0; i < urb->actual_length; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700336 if (!usb_serial_handle_sysrq_char(port, *ch))
Alan Cox4cd1de02009-07-09 13:35:52 +0100337 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
338 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200339 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500340 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100341 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200342}
Johan Hovold23154322010-03-17 23:05:57 +0100343EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200344
Alan Cox95da3102008-07-22 11:09:07 +0100345void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100346{
Ming Leicdc97792008-02-24 18:41:47 +0800347 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100348 unsigned char *data = urb->transfer_buffer;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100349 unsigned long flags;
Johan Hovoldd83b4052011-11-06 19:06:37 +0100350 int i;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100351
Johan Hovoldd83b4052011-11-06 19:06:37 +0100352 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
353 if (urb == port->read_urbs[i])
354 break;
355 }
356 set_bit(i, &port->read_urbs_free);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100357
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700358 dev_dbg(&port->dev, "%s - port %d, urb %d, len %d\n",
359 __func__, port->number, i, urb->actual_length);
360
Johan Hovoldd83b4052011-11-06 19:06:37 +0100361 if (urb->status) {
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700362 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
363 __func__, urb->status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100364 return;
365 }
366
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100367 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100368 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100369
370 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100371 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100372 port->throttled = port->throttle_req;
373 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800374 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100375 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100376 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800377 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100378}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300379EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Alan Cox95da3102008-07-22 11:09:07 +0100381void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500383 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800384 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700385 int status = urb->status;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200386 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200388 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
389 if (port->write_urbs[i] == urb)
390 break;
391
392 spin_lock_irqsave(&port->lock, flags);
393 port->tx_bytes -= urb->transfer_buffer_length;
394 set_bit(i, &port->write_urbs_free);
395 spin_unlock_irqrestore(&port->lock, flags);
396
397 if (status) {
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700398 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
399 __func__, status);
Johan Hovold25915302010-01-06 15:48:42 -0800400
Jason Wessel715b1dc2009-05-11 15:24:07 -0500401 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200402 kfifo_reset_out(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500403 spin_unlock_irqrestore(&port->lock, flags);
404 } else {
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200405 usb_serial_generic_write_start(port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500406 }
407
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700408 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800410EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Alan Cox95da3102008-07-22 11:09:07 +0100412void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100413{
Alan Cox95da3102008-07-22 11:09:07 +0100414 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100415 unsigned long flags;
416
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100417 /* Set the throttle request flag. It will be picked up
418 * by usb_serial_generic_read_bulk_callback(). */
419 spin_lock_irqsave(&port->lock, flags);
420 port->throttle_req = 1;
421 spin_unlock_irqrestore(&port->lock, flags);
422}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100423EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100424
Alan Cox95da3102008-07-22 11:09:07 +0100425void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100426{
Alan Cox95da3102008-07-22 11:09:07 +0100427 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100428 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100429
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100430 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100431 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100432 was_throttled = port->throttled;
433 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100434 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100435
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100436 if (was_throttled)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100437 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100438}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100439EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100440
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700441#ifdef CONFIG_MAGIC_SYSRQ
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700442int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500443{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600444 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500445 if (ch && time_before(jiffies, port->sysrq)) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700446 handle_sysrq(ch);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500447 port->sysrq = 0;
448 return 1;
449 }
450 port->sysrq = 0;
451 }
452 return 0;
453}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700454#else
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700455int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700456{
457 return 0;
458}
459#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500460EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
461
462int usb_serial_handle_break(struct usb_serial_port *port)
463{
464 if (!port->sysrq) {
465 port->sysrq = jiffies + HZ*5;
466 return 1;
467 }
468 port->sysrq = 0;
469 return 0;
470}
471EXPORT_SYMBOL_GPL(usb_serial_handle_break);
472
Libor Pechacekd14fc1a2011-01-14 14:30:21 +0100473/**
474 * usb_serial_handle_dcd_change - handle a change of carrier detect state
475 * @port: usb_serial_port structure for the open port
476 * @tty: tty_struct structure for the port
477 * @status: new carrier detect status, nonzero if active
478 */
479void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
480 struct tty_struct *tty, unsigned int status)
481{
482 struct tty_port *port = &usb_port->port;
483
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700484 dev_dbg(&usb_port->dev, "%s - port %d, status %d\n", __func__,
485 usb_port->number, status);
Libor Pechacekd14fc1a2011-01-14 14:30:21 +0100486
487 if (status)
488 wake_up_interruptible(&port->open_wait);
489 else if (tty && !C_CLOCAL(tty))
490 tty_hangup(tty);
491}
492EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
493
David VomLehn8e8dce02009-08-28 12:54:27 -0700494int usb_serial_generic_resume(struct usb_serial *serial)
495{
496 struct usb_serial_port *port;
497 int i, c = 0, r;
498
499 for (i = 0; i < serial->num_ports; i++) {
500 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500501 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700502 continue;
503
Johan Hovoldd83b4052011-11-06 19:06:37 +0100504 if (port->bulk_in_size) {
505 r = usb_serial_generic_submit_read_urbs(port,
506 GFP_NOIO);
David VomLehn8e8dce02009-08-28 12:54:27 -0700507 if (r < 0)
508 c++;
509 }
510
Johan Hovold27c7acf2010-05-05 23:57:37 +0200511 if (port->bulk_out_size) {
David VomLehn8e8dce02009-08-28 12:54:27 -0700512 r = usb_serial_generic_write_start(port);
513 if (r < 0)
514 c++;
515 }
516 }
517
518 return c ? -EIO : 0;
519}
520EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
521
Alan Sternf9c99bb2009-06-02 11:53:55 -0400522void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 int i;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100527 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
Bill Pemberton5c7efeb2010-08-05 17:01:10 -0400530EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Alan Sternf9c99bb2009-06-02 11:53:55 -0400532void usb_serial_generic_release(struct usb_serial *serial)
533{
Alan Sternf9c99bb2009-06-02 11:53:55 -0400534}