blob: 546177fa7cfec3e219458d01b1a628295046c86b [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
31static int generic_probe(struct usb_interface *interface,
32 const struct usb_device_id *id);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static __u16 vendor = 0x05f9;
35static __u16 product = 0xffff;
36
37module_param(vendor, ushort, 0);
38MODULE_PARM_DESC(vendor, "User specified USB idVendor");
39
40module_param(product, ushort, 0);
41MODULE_PARM_DESC(product, "User specified USB idProduct");
42
43static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
44
Johannes Hölzld9b1b782006-12-17 21:50:24 +010045/* we want to look at all devices, as the vendor/product id can change
46 * depending on the command line argument */
Németh Márton7d40d7e2010-01-10 15:34:24 +010047static const struct usb_device_id generic_serial_ids[] = {
Johannes Hölzld9b1b782006-12-17 21:50:24 +010048 {.driver_info = 42},
49 {}
50};
51
52static struct usb_driver generic_driver = {
53 .name = "usbserial_generic",
54 .probe = generic_probe,
55 .disconnect = usb_serial_disconnect,
56 .id_table = generic_serial_ids,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010057};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070060struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070061 .driver = {
62 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070063 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070064 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .id_table = generic_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040067 .disconnect = usb_serial_generic_disconnect,
68 .release = usb_serial_generic_release,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010069 .throttle = usb_serial_generic_throttle,
70 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020071 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Alan Stern765e0ba2012-02-23 14:55:59 -050074static struct usb_serial_driver * const serial_drivers[] = {
75 &usb_serial_generic_device, NULL
76};
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int generic_probe(struct usb_interface *interface,
79 const struct usb_device_id *id)
80{
81 const struct usb_device_id *id_pattern;
82
83 id_pattern = usb_match_id(interface, generic_device_ids);
84 if (id_pattern != NULL)
85 return usb_serial_probe(interface, id);
86 return -ENODEV;
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89
Alan Coxae643872008-07-22 11:11:55 +010090int usb_serial_generic_register(int _debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int retval = 0;
93
94 debug = _debug;
95#ifdef CONFIG_USB_SERIAL_GENERIC
96 generic_device_ids[0].idVendor = vendor;
97 generic_device_ids[0].idProduct = product;
Alan Coxae643872008-07-22 11:11:55 +010098 generic_device_ids[0].match_flags =
99 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* register our generic driver with ourselves */
Alan Stern765e0ba2012-02-23 14:55:59 -0500102 retval = usb_serial_register_drivers(&generic_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
104 return retval;
105}
106
Alan Coxae643872008-07-22 11:11:55 +0100107void usb_serial_generic_deregister(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109#ifdef CONFIG_USB_SERIAL_GENERIC
110 /* remove our generic driver */
Alan Stern765e0ba2012-02-23 14:55:59 -0500111 usb_serial_deregister_drivers(&generic_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113}
114
Alan Coxa509a7e2009-09-19 13:13:26 -0700115int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100118 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Harvey Harrison441b62c2008-03-03 16:08:34 -0800120 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100122 /* clear the throttle flags */
123 spin_lock_irqsave(&port->lock, flags);
124 port->throttled = 0;
125 port->throttle_req = 0;
126 spin_unlock_irqrestore(&port->lock, flags);
127
128 /* if we have a bulk endpoint, start reading from it */
Johan Hovold41bd72f2010-03-17 23:05:53 +0100129 if (port->bulk_in_size)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100130 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return result;
133}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700134EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Alan Cox95da3102008-07-22 11:09:07 +0100136static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 struct usb_serial *serial = port->serial;
Johan Hovoldec3ee502010-03-17 23:00:44 +0100139 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200140 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Harvey Harrison441b62c2008-03-03 16:08:34 -0800142 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (serial->dev) {
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100145 /* shutdown any bulk transfers that might be going on */
Johan Hovoldec3ee502010-03-17 23:00:44 +0100146 if (port->bulk_out_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 usb_kill_urb(port->write_urb);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200148 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
149 usb_kill_urb(port->write_urbs[i]);
Johan Hovoldec3ee502010-03-17 23:00:44 +0100150
151 spin_lock_irqsave(&port->lock, flags);
152 kfifo_reset_out(&port->write_fifo);
153 spin_unlock_irqrestore(&port->lock, flags);
154 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100155 if (port->bulk_in_size) {
156 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
157 usb_kill_urb(port->read_urbs[i]);
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160}
161
Alan Cox335f8512009-06-11 12:26:29 +0100162void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800164 dbg("%s - port %d", __func__, port->number);
Alan Coxae643872008-07-22 11:11:55 +0100165 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Johan Hovoldf26788d2010-03-17 23:00:45 +0100167EXPORT_SYMBOL_GPL(usb_serial_generic_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100169int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200170 void *dest, size_t size)
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100171{
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200172 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500173}
174
David VomLehn8e8dce02009-08-28 12:54:27 -0700175/**
176 * usb_serial_generic_write_start - kick off an URB write
177 * @port: Pointer to the &struct usb_serial_port data
178 *
Johan Hovold27c7acf2010-05-05 23:57:37 +0200179 * Returns zero on success, or a negative errno value
David VomLehn8e8dce02009-08-28 12:54:27 -0700180 */
181static int usb_serial_generic_write_start(struct usb_serial_port *port)
182{
Johan Hovold27c7acf2010-05-05 23:57:37 +0200183 struct urb *urb;
184 int count, result;
David VomLehn8e8dce02009-08-28 12:54:27 -0700185 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200186 int i;
David VomLehn8e8dce02009-08-28 12:54:27 -0700187
Johan Hovold27c7acf2010-05-05 23:57:37 +0200188 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
189 return 0;
190retry:
David VomLehn8e8dce02009-08-28 12:54:27 -0700191 spin_lock_irqsave(&port->lock, flags);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200192 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
193 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold50a5f702010-03-17 23:06:02 +0100194 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700195 return 0;
Johan Hovold50a5f702010-03-17 23:06:02 +0100196 }
Johan Hovold27c7acf2010-05-05 23:57:37 +0200197 i = (int)find_first_bit(&port->write_urbs_free,
198 ARRAY_SIZE(port->write_urbs));
Johan Hovold50a5f702010-03-17 23:06:02 +0100199 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700200
Johan Hovold27c7acf2010-05-05 23:57:37 +0200201 urb = port->write_urbs[i];
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100202 count = port->serial->type->prepare_write_buffer(port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200203 urb->transfer_buffer,
204 port->bulk_out_size);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200205 urb->transfer_buffer_length = count;
206 usb_serial_debug_data(debug, &port->dev, __func__, count,
207 urb->transfer_buffer);
Johan Hovoldb58af402010-08-04 15:45:57 +0200208 spin_lock_irqsave(&port->lock, flags);
209 port->tx_bytes += count;
210 spin_unlock_irqrestore(&port->lock, flags);
211
212 clear_bit(i, &port->write_urbs_free);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200213 result = usb_submit_urb(urb, GFP_ATOMIC);
David VomLehn8e8dce02009-08-28 12:54:27 -0700214 if (result) {
Johan Hovoldf1475a02012-02-10 13:20:50 +0100215 dev_err_console(port, "%s - error submitting urb: %d\n",
David VomLehn8e8dce02009-08-28 12:54:27 -0700216 __func__, result);
Johan Hovoldb58af402010-08-04 15:45:57 +0200217 set_bit(i, &port->write_urbs_free);
218 spin_lock_irqsave(&port->lock, flags);
219 port->tx_bytes -= count;
220 spin_unlock_irqrestore(&port->lock, flags);
221
Johan Hovold27c7acf2010-05-05 23:57:37 +0200222 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold30af7fb2010-03-17 23:00:42 +0100223 return result;
224 }
Johan Hovold30af7fb2010-03-17 23:00:42 +0100225
Johan Hovoldbc845e52013-11-09 12:38:09 +0100226 goto retry; /* try sending off another urb */
David VomLehn8e8dce02009-08-28 12:54:27 -0700227}
228
229/**
230 * usb_serial_generic_write - generic write function for serial USB devices
231 * @tty: Pointer to &struct tty_struct for the device
232 * @port: Pointer to the &usb_serial_port structure for the device
233 * @buf: Pointer to the data to write
234 * @count: Number of bytes to write
235 *
236 * Returns the number of characters actually written, which may be anything
237 * from zero to @count. If an error occurs, it returns the negative errno
238 * value.
239 */
Alan Cox95da3102008-07-22 11:09:07 +0100240int usb_serial_generic_write(struct tty_struct *tty,
241 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Harvey Harrison441b62c2008-03-03 16:08:34 -0800245 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100247 /* only do something if we have a bulk out endpoint */
248 if (!port->bulk_out_size)
249 return -ENODEV;
250
Johan Hovold1a1405e2010-03-17 23:06:01 +0100251 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Stefani Seibold119eecc2009-12-23 09:10:48 +0100254 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700255 result = usb_serial_generic_write_start(port);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200256 if (result)
257 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200259 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500261EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alan Coxae643872008-07-22 11:11:55 +0100263int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Alan Cox95da3102008-07-22 11:09:07 +0100265 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500266 unsigned long flags;
Johan Hovold25d514c2010-03-17 23:06:07 +0100267 int room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Harvey Harrison441b62c2008-03-03 16:08:34 -0800269 dbg("%s - port %d", __func__, port->number);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100270
271 if (!port->bulk_out_size)
272 return 0;
273
Jason Wessel715b1dc2009-05-11 15:24:07 -0500274 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200275 room = kfifo_avail(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500276 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Harvey Harrison441b62c2008-03-03 16:08:34 -0800278 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100279 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Alan Cox95da3102008-07-22 11:09:07 +0100282int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Alan Cox95da3102008-07-22 11:09:07 +0100284 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500285 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100286 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Harvey Harrison441b62c2008-03-03 16:08:34 -0800288 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100290 if (!port->bulk_out_size)
291 return 0;
292
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100293 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200294 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100295 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Harvey Harrison441b62c2008-03-03 16:08:34 -0800297 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100298 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Johan Hovoldd83b4052011-11-06 19:06:37 +0100301static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
302 int index, gfp_t mem_flags)
303{
304 int res;
305
306 if (!test_and_clear_bit(index, &port->read_urbs_free))
307 return 0;
308
309 dbg("%s - port %d, urb %d\n", __func__, port->number, index);
310
311 res = usb_submit_urb(port->read_urbs[index], mem_flags);
312 if (res) {
313 if (res != -EPERM) {
314 dev_err(&port->dev,
315 "%s - usb_submit_urb failed: %d\n",
316 __func__, res);
317 }
318 set_bit(index, &port->read_urbs_free);
319 return res;
320 }
321
322 return 0;
323}
324
325int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
Johan Hovold41bd72f2010-03-17 23:05:53 +0100326 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Johan Hovoldd83b4052011-11-06 19:06:37 +0100328 int res;
329 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Johan Hovoldd83b4052011-11-06 19:06:37 +0100331 dbg("%s - port %d", __func__, port->number);
332
333 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
334 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
335 if (res)
336 goto err;
Johan Hovold0ae14742010-02-27 14:05:46 +0100337 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100338
339 return 0;
340err:
341 for (; i >= 0; --i)
342 usb_kill_urb(port->read_urbs[i]);
343
344 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Johan Hovoldd83b4052011-11-06 19:06:37 +0100346EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100347
Johan Hovold23154322010-03-17 23:05:57 +0100348void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200349{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100350 struct usb_serial_port *port = urb->context;
351 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500352 char *ch = (char *)urb->transfer_buffer;
353 int i;
354
Johan Hovold56a1df42010-05-15 17:53:44 +0200355 if (!urb->actual_length)
356 return;
357
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100358 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500359 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100360 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200361
Alan Cox4cd1de02009-07-09 13:35:52 +0100362 /* The per character mucking around with sysrq path it too slow for
363 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
364 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600365 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de02009-07-09 13:35:52 +0100366 tty_insert_flip_string(tty, ch, urb->actual_length);
367 else {
Alan Cox4cd1de02009-07-09 13:35:52 +0100368 for (i = 0; i < urb->actual_length; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700369 if (!usb_serial_handle_sysrq_char(port, *ch))
Alan Cox4cd1de02009-07-09 13:35:52 +0100370 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
371 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200372 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500373 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100374 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200375}
Johan Hovold23154322010-03-17 23:05:57 +0100376EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200377
Alan Cox95da3102008-07-22 11:09:07 +0100378void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100379{
Ming Leicdc97792008-02-24 18:41:47 +0800380 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100381 unsigned char *data = urb->transfer_buffer;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100382 unsigned long flags;
Johan Hovoldd83b4052011-11-06 19:06:37 +0100383 int i;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100384
Johan Hovoldd83b4052011-11-06 19:06:37 +0100385 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
386 if (urb == port->read_urbs[i])
387 break;
388 }
389 set_bit(i, &port->read_urbs_free);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100390
Johan Hovoldd83b4052011-11-06 19:06:37 +0100391 dbg("%s - port %d, urb %d, len %d\n", __func__, port->number, i,
392 urb->actual_length);
393 if (urb->status) {
394 dbg("%s - non-zero urb status: %d\n", __func__, urb->status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100395 return;
396 }
397
Alan Coxae643872008-07-22 11:11:55 +0100398 usb_serial_debug_data(debug, &port->dev, __func__,
399 urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100400 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100401
402 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100403 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100404 port->throttled = port->throttle_req;
405 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800406 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100407 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100408 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800409 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100410}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300411EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Alan Cox95da3102008-07-22 11:09:07 +0100413void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500415 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800416 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700417 int status = urb->status;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200418 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Harvey Harrison441b62c2008-03-03 16:08:34 -0800420 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200422 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
423 if (port->write_urbs[i] == urb)
424 break;
425
426 spin_lock_irqsave(&port->lock, flags);
427 port->tx_bytes -= urb->transfer_buffer_length;
428 set_bit(i, &port->write_urbs_free);
429 spin_unlock_irqrestore(&port->lock, flags);
430
431 if (status) {
432 dbg("%s - non-zero urb status: %d", __func__, status);
Johan Hovold25915302010-01-06 15:48:42 -0800433
Jason Wessel715b1dc2009-05-11 15:24:07 -0500434 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200435 kfifo_reset_out(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500436 spin_unlock_irqrestore(&port->lock, flags);
437 } else {
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200438 usb_serial_generic_write_start(port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500439 }
440
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700441 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800443EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Alan Cox95da3102008-07-22 11:09:07 +0100445void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100446{
Alan Cox95da3102008-07-22 11:09:07 +0100447 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100448 unsigned long flags;
449
Harvey Harrison441b62c2008-03-03 16:08:34 -0800450 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100451
452 /* Set the throttle request flag. It will be picked up
453 * by usb_serial_generic_read_bulk_callback(). */
454 spin_lock_irqsave(&port->lock, flags);
455 port->throttle_req = 1;
456 spin_unlock_irqrestore(&port->lock, flags);
457}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100458EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100459
Alan Cox95da3102008-07-22 11:09:07 +0100460void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100461{
Alan Cox95da3102008-07-22 11:09:07 +0100462 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100463 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100464
Harvey Harrison441b62c2008-03-03 16:08:34 -0800465 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100466
467 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100468 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100469 was_throttled = port->throttled;
470 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100471 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100472
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100473 if (was_throttled)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100474 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100475}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100476EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100477
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700478#ifdef CONFIG_MAGIC_SYSRQ
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700479int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500480{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600481 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500482 if (ch && time_before(jiffies, port->sysrq)) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700483 handle_sysrq(ch);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500484 port->sysrq = 0;
485 return 1;
486 }
487 port->sysrq = 0;
488 }
489 return 0;
490}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700491#else
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700492int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700493{
494 return 0;
495}
496#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500497EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
498
499int usb_serial_handle_break(struct usb_serial_port *port)
500{
501 if (!port->sysrq) {
502 port->sysrq = jiffies + HZ*5;
503 return 1;
504 }
505 port->sysrq = 0;
506 return 0;
507}
508EXPORT_SYMBOL_GPL(usb_serial_handle_break);
509
Libor Pechacekd14fc1a2011-01-14 14:30:21 +0100510/**
511 * usb_serial_handle_dcd_change - handle a change of carrier detect state
512 * @port: usb_serial_port structure for the open port
513 * @tty: tty_struct structure for the port
514 * @status: new carrier detect status, nonzero if active
515 */
516void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
517 struct tty_struct *tty, unsigned int status)
518{
519 struct tty_port *port = &usb_port->port;
520
521 dbg("%s - port %d, status %d", __func__, usb_port->number, status);
522
523 if (status)
524 wake_up_interruptible(&port->open_wait);
525 else if (tty && !C_CLOCAL(tty))
526 tty_hangup(tty);
527}
528EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
529
David VomLehn8e8dce02009-08-28 12:54:27 -0700530int usb_serial_generic_resume(struct usb_serial *serial)
531{
532 struct usb_serial_port *port;
533 int i, c = 0, r;
534
535 for (i = 0; i < serial->num_ports; i++) {
536 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500537 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700538 continue;
539
Johan Hovoldd83b4052011-11-06 19:06:37 +0100540 if (port->bulk_in_size) {
541 r = usb_serial_generic_submit_read_urbs(port,
542 GFP_NOIO);
David VomLehn8e8dce02009-08-28 12:54:27 -0700543 if (r < 0)
544 c++;
545 }
546
Johan Hovold27c7acf2010-05-05 23:57:37 +0200547 if (port->bulk_out_size) {
David VomLehn8e8dce02009-08-28 12:54:27 -0700548 r = usb_serial_generic_write_start(port);
549 if (r < 0)
550 c++;
551 }
552 }
553
554 return c ? -EIO : 0;
555}
556EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
557
Alan Sternf9c99bb2009-06-02 11:53:55 -0400558void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 int i;
561
Harvey Harrison441b62c2008-03-03 16:08:34 -0800562 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100565 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Bill Pemberton5c7efeb2010-08-05 17:01:10 -0400568EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Alan Sternf9c99bb2009-06-02 11:53:55 -0400570void usb_serial_generic_release(struct usb_serial *serial)
571{
572 dbg("%s", __func__);
573}