blob: 9b026bf7afefe7002a4baf106e059d8d63d5b891 [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;
174 usb_serial_debug_data(debug, &port->dev, __func__, count,
175 urb->transfer_buffer);
Johan Hovoldb58af402010-08-04 15:45:57 +0200176 spin_lock_irqsave(&port->lock, flags);
177 port->tx_bytes += count;
178 spin_unlock_irqrestore(&port->lock, flags);
179
180 clear_bit(i, &port->write_urbs_free);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200181 result = usb_submit_urb(urb, GFP_ATOMIC);
David VomLehn8e8dce02009-08-28 12:54:27 -0700182 if (result) {
Johan Hovoldf1475a02012-02-10 13:20:50 +0100183 dev_err_console(port, "%s - error submitting urb: %d\n",
David VomLehn8e8dce02009-08-28 12:54:27 -0700184 __func__, result);
Johan Hovoldb58af402010-08-04 15:45:57 +0200185 set_bit(i, &port->write_urbs_free);
186 spin_lock_irqsave(&port->lock, flags);
187 port->tx_bytes -= count;
188 spin_unlock_irqrestore(&port->lock, flags);
189
Johan Hovold27c7acf2010-05-05 23:57:37 +0200190 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold30af7fb2010-03-17 23:00:42 +0100191 return result;
192 }
Johan Hovold30af7fb2010-03-17 23:00:42 +0100193
Johan Hovold27c7acf2010-05-05 23:57:37 +0200194 /* Try sending off another urb, unless in irq context (in which case
195 * there will be no free urb). */
196 if (!in_irq())
197 goto retry;
198
199 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
200
201 return 0;
David VomLehn8e8dce02009-08-28 12:54:27 -0700202}
203
204/**
205 * usb_serial_generic_write - generic write function for serial USB devices
206 * @tty: Pointer to &struct tty_struct for the device
207 * @port: Pointer to the &usb_serial_port structure for the device
208 * @buf: Pointer to the data to write
209 * @count: Number of bytes to write
210 *
211 * Returns the number of characters actually written, which may be anything
212 * from zero to @count. If an error occurs, it returns the negative errno
213 * value.
214 */
Alan Cox95da3102008-07-22 11:09:07 +0100215int usb_serial_generic_write(struct tty_struct *tty,
216 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100220 /* only do something if we have a bulk out endpoint */
221 if (!port->bulk_out_size)
222 return -ENODEV;
223
Johan Hovold1a1405e2010-03-17 23:06:01 +0100224 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Stefani Seibold119eecc2009-12-23 09:10:48 +0100227 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700228 result = usb_serial_generic_write_start(port);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200229 if (result)
230 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200232 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500234EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Alan Coxae643872008-07-22 11:11:55 +0100236int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Alan Cox95da3102008-07-22 11:09:07 +0100238 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500239 unsigned long flags;
Johan Hovold25d514c2010-03-17 23:06:07 +0100240 int room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100242 if (!port->bulk_out_size)
243 return 0;
244
Jason Wessel715b1dc2009-05-11 15:24:07 -0500245 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200246 room = kfifo_avail(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500247 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700249 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100250 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Alan Cox95da3102008-07-22 11:09:07 +0100253int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Alan Cox95da3102008-07-22 11:09:07 +0100255 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500256 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100257 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100259 if (!port->bulk_out_size)
260 return 0;
261
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100262 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200263 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100264 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700266 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100267 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Johan Hovoldd83b4052011-11-06 19:06:37 +0100270static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
271 int index, gfp_t mem_flags)
272{
273 int res;
274
275 if (!test_and_clear_bit(index, &port->read_urbs_free))
276 return 0;
277
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700278 dev_dbg(&port->dev, "%s - port %d, urb %d\n", __func__,
279 port->number, index);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100280
281 res = usb_submit_urb(port->read_urbs[index], mem_flags);
282 if (res) {
283 if (res != -EPERM) {
284 dev_err(&port->dev,
285 "%s - usb_submit_urb failed: %d\n",
286 __func__, res);
287 }
288 set_bit(index, &port->read_urbs_free);
289 return res;
290 }
291
292 return 0;
293}
294
295int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
Johan Hovold41bd72f2010-03-17 23:05:53 +0100296 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Johan Hovoldd83b4052011-11-06 19:06:37 +0100298 int res;
299 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Johan Hovoldd83b4052011-11-06 19:06:37 +0100301 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
302 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
303 if (res)
304 goto err;
Johan Hovold0ae14742010-02-27 14:05:46 +0100305 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100306
307 return 0;
308err:
309 for (; i >= 0; --i)
310 usb_kill_urb(port->read_urbs[i]);
311
312 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
Johan Hovoldd83b4052011-11-06 19:06:37 +0100314EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100315
Johan Hovold23154322010-03-17 23:05:57 +0100316void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200317{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100318 struct usb_serial_port *port = urb->context;
319 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500320 char *ch = (char *)urb->transfer_buffer;
321 int i;
322
Johan Hovold56a1df42010-05-15 17:53:44 +0200323 if (!urb->actual_length)
324 return;
325
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100326 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500327 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100328 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200329
Alan Cox4cd1de02009-07-09 13:35:52 +0100330 /* The per character mucking around with sysrq path it too slow for
331 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
332 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600333 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de02009-07-09 13:35:52 +0100334 tty_insert_flip_string(tty, ch, urb->actual_length);
335 else {
Alan Cox4cd1de02009-07-09 13:35:52 +0100336 for (i = 0; i < urb->actual_length; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700337 if (!usb_serial_handle_sysrq_char(port, *ch))
Alan Cox4cd1de02009-07-09 13:35:52 +0100338 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
339 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200340 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500341 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100342 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200343}
Johan Hovold23154322010-03-17 23:05:57 +0100344EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200345
Alan Cox95da3102008-07-22 11:09:07 +0100346void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100347{
Ming Leicdc97792008-02-24 18:41:47 +0800348 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100349 unsigned char *data = urb->transfer_buffer;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100350 unsigned long flags;
Johan Hovoldd83b4052011-11-06 19:06:37 +0100351 int i;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100352
Johan Hovoldd83b4052011-11-06 19:06:37 +0100353 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
354 if (urb == port->read_urbs[i])
355 break;
356 }
357 set_bit(i, &port->read_urbs_free);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100358
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700359 dev_dbg(&port->dev, "%s - port %d, urb %d, len %d\n",
360 __func__, port->number, i, urb->actual_length);
361
Johan Hovoldd83b4052011-11-06 19:06:37 +0100362 if (urb->status) {
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700363 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
364 __func__, urb->status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100365 return;
366 }
367
Alan Coxae643872008-07-22 11:11:55 +0100368 usb_serial_debug_data(debug, &port->dev, __func__,
369 urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100370 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100371
372 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100373 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100374 port->throttled = port->throttle_req;
375 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800376 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100377 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100378 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800379 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100380}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300381EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Alan Cox95da3102008-07-22 11:09:07 +0100383void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500385 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800386 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700387 int status = urb->status;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200388 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200390 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
391 if (port->write_urbs[i] == urb)
392 break;
393
394 spin_lock_irqsave(&port->lock, flags);
395 port->tx_bytes -= urb->transfer_buffer_length;
396 set_bit(i, &port->write_urbs_free);
397 spin_unlock_irqrestore(&port->lock, flags);
398
399 if (status) {
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700400 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
401 __func__, status);
Johan Hovold25915302010-01-06 15:48:42 -0800402
Jason Wessel715b1dc2009-05-11 15:24:07 -0500403 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200404 kfifo_reset_out(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500405 spin_unlock_irqrestore(&port->lock, flags);
406 } else {
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200407 usb_serial_generic_write_start(port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500408 }
409
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700410 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800412EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Alan Cox95da3102008-07-22 11:09:07 +0100414void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100415{
Alan Cox95da3102008-07-22 11:09:07 +0100416 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100417 unsigned long flags;
418
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100419 /* Set the throttle request flag. It will be picked up
420 * by usb_serial_generic_read_bulk_callback(). */
421 spin_lock_irqsave(&port->lock, flags);
422 port->throttle_req = 1;
423 spin_unlock_irqrestore(&port->lock, flags);
424}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100425EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100426
Alan Cox95da3102008-07-22 11:09:07 +0100427void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100428{
Alan Cox95da3102008-07-22 11:09:07 +0100429 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100430 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100431
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100432 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100433 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100434 was_throttled = port->throttled;
435 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100436 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100437
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100438 if (was_throttled)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100439 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100440}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100441EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100442
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700443#ifdef CONFIG_MAGIC_SYSRQ
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700444int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500445{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600446 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500447 if (ch && time_before(jiffies, port->sysrq)) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700448 handle_sysrq(ch);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500449 port->sysrq = 0;
450 return 1;
451 }
452 port->sysrq = 0;
453 }
454 return 0;
455}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700456#else
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700457int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700458{
459 return 0;
460}
461#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500462EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
463
464int usb_serial_handle_break(struct usb_serial_port *port)
465{
466 if (!port->sysrq) {
467 port->sysrq = jiffies + HZ*5;
468 return 1;
469 }
470 port->sysrq = 0;
471 return 0;
472}
473EXPORT_SYMBOL_GPL(usb_serial_handle_break);
474
Libor Pechacekd14fc1a2011-01-14 14:30:21 +0100475/**
476 * usb_serial_handle_dcd_change - handle a change of carrier detect state
477 * @port: usb_serial_port structure for the open port
478 * @tty: tty_struct structure for the port
479 * @status: new carrier detect status, nonzero if active
480 */
481void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
482 struct tty_struct *tty, unsigned int status)
483{
484 struct tty_port *port = &usb_port->port;
485
Greg Kroah-Hartman689c2782012-05-15 16:27:18 -0700486 dev_dbg(&usb_port->dev, "%s - port %d, status %d\n", __func__,
487 usb_port->number, status);
Libor Pechacekd14fc1a2011-01-14 14:30:21 +0100488
489 if (status)
490 wake_up_interruptible(&port->open_wait);
491 else if (tty && !C_CLOCAL(tty))
492 tty_hangup(tty);
493}
494EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
495
David VomLehn8e8dce02009-08-28 12:54:27 -0700496int usb_serial_generic_resume(struct usb_serial *serial)
497{
498 struct usb_serial_port *port;
499 int i, c = 0, r;
500
501 for (i = 0; i < serial->num_ports; i++) {
502 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500503 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700504 continue;
505
Johan Hovoldd83b4052011-11-06 19:06:37 +0100506 if (port->bulk_in_size) {
507 r = usb_serial_generic_submit_read_urbs(port,
508 GFP_NOIO);
David VomLehn8e8dce02009-08-28 12:54:27 -0700509 if (r < 0)
510 c++;
511 }
512
Johan Hovold27c7acf2010-05-05 23:57:37 +0200513 if (port->bulk_out_size) {
David VomLehn8e8dce02009-08-28 12:54:27 -0700514 r = usb_serial_generic_write_start(port);
515 if (r < 0)
516 c++;
517 }
518 }
519
520 return c ? -EIO : 0;
521}
522EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
523
Alan Sternf9c99bb2009-06-02 11:53:55 -0400524void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 int i;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100529 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
Bill Pemberton5c7efeb2010-08-05 17:01:10 -0400532EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Alan Sternf9c99bb2009-06-02 11:53:55 -0400534void usb_serial_generic_release(struct usb_serial *serial)
535{
Alan Sternf9c99bb2009-06-02 11:53:55 -0400536}