blob: 664deb63807c0d3ec3d7f6cf359a1cec4ce652de [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 Hovold27c7acf2010-05-05 23:57:37 +0200226 /* Try sending off another urb, unless in irq context (in which case
227 * there will be no free urb). */
228 if (!in_irq())
229 goto retry;
230
231 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
232
233 return 0;
David VomLehn8e8dce02009-08-28 12:54:27 -0700234}
235
236/**
237 * usb_serial_generic_write - generic write function for serial USB devices
238 * @tty: Pointer to &struct tty_struct for the device
239 * @port: Pointer to the &usb_serial_port structure for the device
240 * @buf: Pointer to the data to write
241 * @count: Number of bytes to write
242 *
243 * Returns the number of characters actually written, which may be anything
244 * from zero to @count. If an error occurs, it returns the negative errno
245 * value.
246 */
Alan Cox95da3102008-07-22 11:09:07 +0100247int usb_serial_generic_write(struct tty_struct *tty,
248 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Harvey Harrison441b62c2008-03-03 16:08:34 -0800252 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100254 /* only do something if we have a bulk out endpoint */
255 if (!port->bulk_out_size)
256 return -ENODEV;
257
Johan Hovold1a1405e2010-03-17 23:06:01 +0100258 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Stefani Seibold119eecc2009-12-23 09:10:48 +0100261 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700262 result = usb_serial_generic_write_start(port);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200263 if (result)
264 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200266 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500268EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Alan Coxae643872008-07-22 11:11:55 +0100270int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Alan Cox95da3102008-07-22 11:09:07 +0100272 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500273 unsigned long flags;
Johan Hovold25d514c2010-03-17 23:06:07 +0100274 int room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Harvey Harrison441b62c2008-03-03 16:08:34 -0800276 dbg("%s - port %d", __func__, port->number);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100277
278 if (!port->bulk_out_size)
279 return 0;
280
Jason Wessel715b1dc2009-05-11 15:24:07 -0500281 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200282 room = kfifo_avail(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500283 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Harvey Harrison441b62c2008-03-03 16:08:34 -0800285 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100286 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Alan Cox95da3102008-07-22 11:09:07 +0100289int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Alan Cox95da3102008-07-22 11:09:07 +0100291 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500292 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100293 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Harvey Harrison441b62c2008-03-03 16:08:34 -0800295 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100297 if (!port->bulk_out_size)
298 return 0;
299
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100300 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200301 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100302 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Harvey Harrison441b62c2008-03-03 16:08:34 -0800304 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100305 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Johan Hovoldd83b4052011-11-06 19:06:37 +0100308static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
309 int index, gfp_t mem_flags)
310{
311 int res;
312
313 if (!test_and_clear_bit(index, &port->read_urbs_free))
314 return 0;
315
316 dbg("%s - port %d, urb %d\n", __func__, port->number, index);
317
318 res = usb_submit_urb(port->read_urbs[index], mem_flags);
319 if (res) {
320 if (res != -EPERM) {
321 dev_err(&port->dev,
322 "%s - usb_submit_urb failed: %d\n",
323 __func__, res);
324 }
325 set_bit(index, &port->read_urbs_free);
326 return res;
327 }
328
329 return 0;
330}
331
332int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
Johan Hovold41bd72f2010-03-17 23:05:53 +0100333 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Johan Hovoldd83b4052011-11-06 19:06:37 +0100335 int res;
336 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Johan Hovoldd83b4052011-11-06 19:06:37 +0100338 dbg("%s - port %d", __func__, port->number);
339
340 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
341 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
342 if (res)
343 goto err;
Johan Hovold0ae14742010-02-27 14:05:46 +0100344 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100345
346 return 0;
347err:
348 for (; i >= 0; --i)
349 usb_kill_urb(port->read_urbs[i]);
350
351 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
Johan Hovoldd83b4052011-11-06 19:06:37 +0100353EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100354
Johan Hovold23154322010-03-17 23:05:57 +0100355void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200356{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100357 struct usb_serial_port *port = urb->context;
358 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500359 char *ch = (char *)urb->transfer_buffer;
360 int i;
361
Johan Hovold56a1df42010-05-15 17:53:44 +0200362 if (!urb->actual_length)
363 return;
364
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100365 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500366 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100367 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200368
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100369 /* The per character mucking around with sysrq path it too slow for
370 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
371 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600372 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100373 tty_insert_flip_string(tty, ch, urb->actual_length);
374 else {
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100375 for (i = 0; i < urb->actual_length; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700376 if (!usb_serial_handle_sysrq_char(port, *ch))
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100377 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
378 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200379 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500380 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100381 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200382}
Johan Hovold23154322010-03-17 23:05:57 +0100383EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200384
Alan Cox95da3102008-07-22 11:09:07 +0100385void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100386{
Ming Leicdc97792008-02-24 18:41:47 +0800387 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100388 unsigned char *data = urb->transfer_buffer;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100389 unsigned long flags;
Johan Hovoldd83b4052011-11-06 19:06:37 +0100390 int i;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100391
Johan Hovoldd83b4052011-11-06 19:06:37 +0100392 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
393 if (urb == port->read_urbs[i])
394 break;
395 }
396 set_bit(i, &port->read_urbs_free);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100397
Johan Hovoldd83b4052011-11-06 19:06:37 +0100398 dbg("%s - port %d, urb %d, len %d\n", __func__, port->number, i,
399 urb->actual_length);
400 if (urb->status) {
401 dbg("%s - non-zero urb status: %d\n", __func__, urb->status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100402 return;
403 }
404
Alan Coxae643872008-07-22 11:11:55 +0100405 usb_serial_debug_data(debug, &port->dev, __func__,
406 urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100407 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100408
409 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100410 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100411 port->throttled = port->throttle_req;
412 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800413 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100414 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100415 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800416 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100417}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300418EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Alan Cox95da3102008-07-22 11:09:07 +0100420void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500422 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800423 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700424 int status = urb->status;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200425 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Harvey Harrison441b62c2008-03-03 16:08:34 -0800427 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200429 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
430 if (port->write_urbs[i] == urb)
431 break;
432
433 spin_lock_irqsave(&port->lock, flags);
434 port->tx_bytes -= urb->transfer_buffer_length;
435 set_bit(i, &port->write_urbs_free);
436 spin_unlock_irqrestore(&port->lock, flags);
437
438 if (status) {
439 dbg("%s - non-zero urb status: %d", __func__, status);
Johan Hovold25915302010-01-06 15:48:42 -0800440
Jason Wessel715b1dc2009-05-11 15:24:07 -0500441 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200442 kfifo_reset_out(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500443 spin_unlock_irqrestore(&port->lock, flags);
444 } else {
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200445 usb_serial_generic_write_start(port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500446 }
447
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700448 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800450EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Alan Cox95da3102008-07-22 11:09:07 +0100452void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100453{
Alan Cox95da3102008-07-22 11:09:07 +0100454 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100455 unsigned long flags;
456
Harvey Harrison441b62c2008-03-03 16:08:34 -0800457 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100458
459 /* Set the throttle request flag. It will be picked up
460 * by usb_serial_generic_read_bulk_callback(). */
461 spin_lock_irqsave(&port->lock, flags);
462 port->throttle_req = 1;
463 spin_unlock_irqrestore(&port->lock, flags);
464}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100465EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100466
Alan Cox95da3102008-07-22 11:09:07 +0100467void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100468{
Alan Cox95da3102008-07-22 11:09:07 +0100469 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100470 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100471
Harvey Harrison441b62c2008-03-03 16:08:34 -0800472 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100473
474 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100475 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100476 was_throttled = port->throttled;
477 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100478 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100479
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100480 if (was_throttled)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100481 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100482}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100483EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100484
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700485#ifdef CONFIG_MAGIC_SYSRQ
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700486int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500487{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600488 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500489 if (ch && time_before(jiffies, port->sysrq)) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700490 handle_sysrq(ch);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500491 port->sysrq = 0;
492 return 1;
493 }
494 port->sysrq = 0;
495 }
496 return 0;
497}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700498#else
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700499int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700500{
501 return 0;
502}
503#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500504EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
505
506int usb_serial_handle_break(struct usb_serial_port *port)
507{
508 if (!port->sysrq) {
509 port->sysrq = jiffies + HZ*5;
510 return 1;
511 }
512 port->sysrq = 0;
513 return 0;
514}
515EXPORT_SYMBOL_GPL(usb_serial_handle_break);
516
Libor Pechacekd14fc1a2011-01-14 14:30:21 +0100517/**
518 * usb_serial_handle_dcd_change - handle a change of carrier detect state
519 * @port: usb_serial_port structure for the open port
520 * @tty: tty_struct structure for the port
521 * @status: new carrier detect status, nonzero if active
522 */
523void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
524 struct tty_struct *tty, unsigned int status)
525{
526 struct tty_port *port = &usb_port->port;
527
528 dbg("%s - port %d, status %d", __func__, usb_port->number, status);
529
530 if (status)
531 wake_up_interruptible(&port->open_wait);
532 else if (tty && !C_CLOCAL(tty))
533 tty_hangup(tty);
534}
535EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
536
David VomLehn8e8dce02009-08-28 12:54:27 -0700537int usb_serial_generic_resume(struct usb_serial *serial)
538{
539 struct usb_serial_port *port;
540 int i, c = 0, r;
541
542 for (i = 0; i < serial->num_ports; i++) {
543 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500544 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700545 continue;
546
Johan Hovoldd83b4052011-11-06 19:06:37 +0100547 if (port->bulk_in_size) {
548 r = usb_serial_generic_submit_read_urbs(port,
549 GFP_NOIO);
David VomLehn8e8dce02009-08-28 12:54:27 -0700550 if (r < 0)
551 c++;
552 }
553
Johan Hovold27c7acf2010-05-05 23:57:37 +0200554 if (port->bulk_out_size) {
David VomLehn8e8dce02009-08-28 12:54:27 -0700555 r = usb_serial_generic_write_start(port);
556 if (r < 0)
557 c++;
558 }
559 }
560
561 return c ? -EIO : 0;
562}
563EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
564
Alan Sternf9c99bb2009-06-02 11:53:55 -0400565void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 int i;
568
Harvey Harrison441b62c2008-03-03 16:08:34 -0800569 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100572 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Bill Pemberton5c7efeb2010-08-05 17:01:10 -0400575EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Alan Sternf9c99bb2009-06-02 11:53:55 -0400577void usb_serial_generic_release(struct usb_serial *serial)
578{
579 dbg("%s", __func__);
580}