blob: 7eee965542a5835638af67e1f3a4fe3bfad52e77 [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) {
Johan Hovold27c7acf2010-05-05 23:57:37 +0200147 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
148 usb_kill_urb(port->write_urbs[i]);
Johan Hovoldec3ee502010-03-17 23:00:44 +0100149
150 spin_lock_irqsave(&port->lock, flags);
151 kfifo_reset_out(&port->write_fifo);
152 spin_unlock_irqrestore(&port->lock, flags);
153 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100154 if (port->bulk_in_size) {
155 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
156 usb_kill_urb(port->read_urbs[i]);
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159}
160
Alan Cox335f8512009-06-11 12:26:29 +0100161void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800163 dbg("%s - port %d", __func__, port->number);
Alan Coxae643872008-07-22 11:11:55 +0100164 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
Johan Hovoldf26788d2010-03-17 23:00:45 +0100166EXPORT_SYMBOL_GPL(usb_serial_generic_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100168int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200169 void *dest, size_t size)
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100170{
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200171 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500172}
173
David VomLehn8e8dce02009-08-28 12:54:27 -0700174/**
175 * usb_serial_generic_write_start - kick off an URB write
176 * @port: Pointer to the &struct usb_serial_port data
177 *
Johan Hovold27c7acf2010-05-05 23:57:37 +0200178 * Returns zero on success, or a negative errno value
David VomLehn8e8dce02009-08-28 12:54:27 -0700179 */
180static int usb_serial_generic_write_start(struct usb_serial_port *port)
181{
Johan Hovold27c7acf2010-05-05 23:57:37 +0200182 struct urb *urb;
183 int count, result;
David VomLehn8e8dce02009-08-28 12:54:27 -0700184 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200185 int i;
David VomLehn8e8dce02009-08-28 12:54:27 -0700186
Johan Hovold27c7acf2010-05-05 23:57:37 +0200187 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
188 return 0;
189retry:
David VomLehn8e8dce02009-08-28 12:54:27 -0700190 spin_lock_irqsave(&port->lock, flags);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200191 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
192 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold50a5f702010-03-17 23:06:02 +0100193 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700194 return 0;
Johan Hovold50a5f702010-03-17 23:06:02 +0100195 }
Johan Hovold27c7acf2010-05-05 23:57:37 +0200196 i = (int)find_first_bit(&port->write_urbs_free,
197 ARRAY_SIZE(port->write_urbs));
Johan Hovold50a5f702010-03-17 23:06:02 +0100198 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700199
Johan Hovold27c7acf2010-05-05 23:57:37 +0200200 urb = port->write_urbs[i];
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100201 count = port->serial->type->prepare_write_buffer(port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200202 urb->transfer_buffer,
203 port->bulk_out_size);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200204 urb->transfer_buffer_length = count;
205 usb_serial_debug_data(debug, &port->dev, __func__, count,
206 urb->transfer_buffer);
Johan Hovoldb58af402010-08-04 15:45:57 +0200207 spin_lock_irqsave(&port->lock, flags);
208 port->tx_bytes += count;
209 spin_unlock_irqrestore(&port->lock, flags);
210
211 clear_bit(i, &port->write_urbs_free);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200212 result = usb_submit_urb(urb, GFP_ATOMIC);
David VomLehn8e8dce02009-08-28 12:54:27 -0700213 if (result) {
Johan Hovoldf1475a02012-02-10 13:20:50 +0100214 dev_err_console(port, "%s - error submitting urb: %d\n",
David VomLehn8e8dce02009-08-28 12:54:27 -0700215 __func__, result);
Johan Hovoldb58af402010-08-04 15:45:57 +0200216 set_bit(i, &port->write_urbs_free);
217 spin_lock_irqsave(&port->lock, flags);
218 port->tx_bytes -= count;
219 spin_unlock_irqrestore(&port->lock, flags);
220
Johan Hovold27c7acf2010-05-05 23:57:37 +0200221 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold30af7fb2010-03-17 23:00:42 +0100222 return result;
223 }
Johan Hovold30af7fb2010-03-17 23:00:42 +0100224
Johan Hovold27c7acf2010-05-05 23:57:37 +0200225 /* Try sending off another urb, unless in irq context (in which case
226 * there will be no free urb). */
227 if (!in_irq())
228 goto retry;
229
230 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
231
232 return 0;
David VomLehn8e8dce02009-08-28 12:54:27 -0700233}
234
235/**
236 * usb_serial_generic_write - generic write function for serial USB devices
237 * @tty: Pointer to &struct tty_struct for the device
238 * @port: Pointer to the &usb_serial_port structure for the device
239 * @buf: Pointer to the data to write
240 * @count: Number of bytes to write
241 *
242 * Returns the number of characters actually written, which may be anything
243 * from zero to @count. If an error occurs, it returns the negative errno
244 * value.
245 */
Alan Cox95da3102008-07-22 11:09:07 +0100246int usb_serial_generic_write(struct tty_struct *tty,
247 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Harvey Harrison441b62c2008-03-03 16:08:34 -0800251 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100253 /* only do something if we have a bulk out endpoint */
254 if (!port->bulk_out_size)
255 return -ENODEV;
256
Johan Hovold1a1405e2010-03-17 23:06:01 +0100257 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Stefani Seibold119eecc2009-12-23 09:10:48 +0100260 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700261 result = usb_serial_generic_write_start(port);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200262 if (result)
263 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200265 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500267EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Alan Coxae643872008-07-22 11:11:55 +0100269int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Alan Cox95da3102008-07-22 11:09:07 +0100271 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500272 unsigned long flags;
Johan Hovold25d514c2010-03-17 23:06:07 +0100273 int room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Harvey Harrison441b62c2008-03-03 16:08:34 -0800275 dbg("%s - port %d", __func__, port->number);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100276
277 if (!port->bulk_out_size)
278 return 0;
279
Jason Wessel715b1dc2009-05-11 15:24:07 -0500280 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200281 room = kfifo_avail(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500282 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Harvey Harrison441b62c2008-03-03 16:08:34 -0800284 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100285 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Alan Cox95da3102008-07-22 11:09:07 +0100288int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Alan Cox95da3102008-07-22 11:09:07 +0100290 struct usb_serial_port *port = tty->driver_data;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500291 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100292 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Harvey Harrison441b62c2008-03-03 16:08:34 -0800294 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100296 if (!port->bulk_out_size)
297 return 0;
298
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100299 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200300 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100301 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Harvey Harrison441b62c2008-03-03 16:08:34 -0800303 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100304 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Johan Hovoldd83b4052011-11-06 19:06:37 +0100307static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
308 int index, gfp_t mem_flags)
309{
310 int res;
311
312 if (!test_and_clear_bit(index, &port->read_urbs_free))
313 return 0;
314
Johan Hovoldc1d830c2012-04-25 16:12:08 +0200315 dbg("%s - port %d, urb %d", __func__, port->number, index);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100316
317 res = usb_submit_urb(port->read_urbs[index], mem_flags);
318 if (res) {
319 if (res != -EPERM) {
320 dev_err(&port->dev,
321 "%s - usb_submit_urb failed: %d\n",
322 __func__, res);
323 }
324 set_bit(index, &port->read_urbs_free);
325 return res;
326 }
327
328 return 0;
329}
330
331int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
Johan Hovold41bd72f2010-03-17 23:05:53 +0100332 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Johan Hovoldd83b4052011-11-06 19:06:37 +0100334 int res;
335 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Johan Hovoldd83b4052011-11-06 19:06:37 +0100337 dbg("%s - port %d", __func__, port->number);
338
339 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
340 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
341 if (res)
342 goto err;
Johan Hovold0ae14742010-02-27 14:05:46 +0100343 }
Johan Hovoldd83b4052011-11-06 19:06:37 +0100344
345 return 0;
346err:
347 for (; i >= 0; --i)
348 usb_kill_urb(port->read_urbs[i]);
349
350 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
Johan Hovoldd83b4052011-11-06 19:06:37 +0100352EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100353
Johan Hovold23154322010-03-17 23:05:57 +0100354void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200355{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100356 struct usb_serial_port *port = urb->context;
357 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500358 char *ch = (char *)urb->transfer_buffer;
359 int i;
360
Johan Hovold56a1df42010-05-15 17:53:44 +0200361 if (!urb->actual_length)
362 return;
363
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100364 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500365 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100366 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200367
Alan Cox4cd1de02009-07-09 13:35:52 +0100368 /* The per character mucking around with sysrq path it too slow for
369 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
370 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600371 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de02009-07-09 13:35:52 +0100372 tty_insert_flip_string(tty, ch, urb->actual_length);
373 else {
Alan Cox4cd1de02009-07-09 13:35:52 +0100374 for (i = 0; i < urb->actual_length; i++, ch++) {
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700375 if (!usb_serial_handle_sysrq_char(port, *ch))
Alan Cox4cd1de02009-07-09 13:35:52 +0100376 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
377 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200378 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500379 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100380 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200381}
Johan Hovold23154322010-03-17 23:05:57 +0100382EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200383
Alan Cox95da3102008-07-22 11:09:07 +0100384void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100385{
Ming Leicdc97792008-02-24 18:41:47 +0800386 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100387 unsigned char *data = urb->transfer_buffer;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100388 unsigned long flags;
Johan Hovoldd83b4052011-11-06 19:06:37 +0100389 int i;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100390
Johan Hovoldd83b4052011-11-06 19:06:37 +0100391 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
392 if (urb == port->read_urbs[i])
393 break;
394 }
395 set_bit(i, &port->read_urbs_free);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100396
Johan Hovoldc1d830c2012-04-25 16:12:08 +0200397 dbg("%s - port %d, urb %d, len %d", __func__, port->number, i,
Johan Hovoldd83b4052011-11-06 19:06:37 +0100398 urb->actual_length);
399 if (urb->status) {
Johan Hovoldc1d830c2012-04-25 16:12:08 +0200400 dbg("%s - non-zero urb status: %d", __func__, urb->status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100401 return;
402 }
403
Alan Coxae643872008-07-22 11:11:55 +0100404 usb_serial_debug_data(debug, &port->dev, __func__,
405 urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100406 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100407
408 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100409 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100410 port->throttled = port->throttle_req;
411 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800412 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovoldd83b4052011-11-06 19:06:37 +0100413 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100414 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800415 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100416}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300417EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alan Cox95da3102008-07-22 11:09:07 +0100419void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500421 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800422 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700423 int status = urb->status;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200424 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Harvey Harrison441b62c2008-03-03 16:08:34 -0800426 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200428 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
429 if (port->write_urbs[i] == urb)
430 break;
431
432 spin_lock_irqsave(&port->lock, flags);
433 port->tx_bytes -= urb->transfer_buffer_length;
434 set_bit(i, &port->write_urbs_free);
435 spin_unlock_irqrestore(&port->lock, flags);
436
437 if (status) {
438 dbg("%s - non-zero urb status: %d", __func__, status);
Johan Hovold25915302010-01-06 15:48:42 -0800439
Jason Wessel715b1dc2009-05-11 15:24:07 -0500440 spin_lock_irqsave(&port->lock, flags);
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200441 kfifo_reset_out(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500442 spin_unlock_irqrestore(&port->lock, flags);
443 } else {
Johan Hovoldc23e5fc2010-05-05 23:58:13 +0200444 usb_serial_generic_write_start(port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500445 }
446
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700447 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800449EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Alan Cox95da3102008-07-22 11:09:07 +0100451void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100452{
Alan Cox95da3102008-07-22 11:09:07 +0100453 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100454 unsigned long flags;
455
Harvey Harrison441b62c2008-03-03 16:08:34 -0800456 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100457
458 /* Set the throttle request flag. It will be picked up
459 * by usb_serial_generic_read_bulk_callback(). */
460 spin_lock_irqsave(&port->lock, flags);
461 port->throttle_req = 1;
462 spin_unlock_irqrestore(&port->lock, flags);
463}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100464EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100465
Alan Cox95da3102008-07-22 11:09:07 +0100466void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100467{
Alan Cox95da3102008-07-22 11:09:07 +0100468 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100469 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100470
Harvey Harrison441b62c2008-03-03 16:08:34 -0800471 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100472
473 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100474 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100475 was_throttled = port->throttled;
476 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100477 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100478
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100479 if (was_throttled)
Johan Hovoldd83b4052011-11-06 19:06:37 +0100480 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100481}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100482EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100483
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700484#ifdef CONFIG_MAGIC_SYSRQ
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700485int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500486{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600487 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500488 if (ch && time_before(jiffies, port->sysrq)) {
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700489 handle_sysrq(ch);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500490 port->sysrq = 0;
491 return 1;
492 }
493 port->sysrq = 0;
494 }
495 return 0;
496}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700497#else
Dmitry Torokhov6ee9f4b2010-08-17 21:15:47 -0700498int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700499{
500 return 0;
501}
502#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500503EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
504
505int usb_serial_handle_break(struct usb_serial_port *port)
506{
507 if (!port->sysrq) {
508 port->sysrq = jiffies + HZ*5;
509 return 1;
510 }
511 port->sysrq = 0;
512 return 0;
513}
514EXPORT_SYMBOL_GPL(usb_serial_handle_break);
515
Libor Pechacekd14fc1a2011-01-14 14:30:21 +0100516/**
517 * usb_serial_handle_dcd_change - handle a change of carrier detect state
518 * @port: usb_serial_port structure for the open port
519 * @tty: tty_struct structure for the port
520 * @status: new carrier detect status, nonzero if active
521 */
522void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
523 struct tty_struct *tty, unsigned int status)
524{
525 struct tty_port *port = &usb_port->port;
526
527 dbg("%s - port %d, status %d", __func__, usb_port->number, status);
528
529 if (status)
530 wake_up_interruptible(&port->open_wait);
531 else if (tty && !C_CLOCAL(tty))
532 tty_hangup(tty);
533}
534EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
535
David VomLehn8e8dce02009-08-28 12:54:27 -0700536int usb_serial_generic_resume(struct usb_serial *serial)
537{
538 struct usb_serial_port *port;
539 int i, c = 0, r;
540
541 for (i = 0; i < serial->num_ports; i++) {
542 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500543 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700544 continue;
545
Johan Hovoldd83b4052011-11-06 19:06:37 +0100546 if (port->bulk_in_size) {
547 r = usb_serial_generic_submit_read_urbs(port,
548 GFP_NOIO);
David VomLehn8e8dce02009-08-28 12:54:27 -0700549 if (r < 0)
550 c++;
551 }
552
Johan Hovold27c7acf2010-05-05 23:57:37 +0200553 if (port->bulk_out_size) {
David VomLehn8e8dce02009-08-28 12:54:27 -0700554 r = usb_serial_generic_write_start(port);
555 if (r < 0)
556 c++;
557 }
558 }
559
560 return c ? -EIO : 0;
561}
562EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
563
Alan Sternf9c99bb2009-06-02 11:53:55 -0400564void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 int i;
567
Harvey Harrison441b62c2008-03-03 16:08:34 -0800568 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100571 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Bill Pemberton5c7efeb2010-08-05 17:01:10 -0400574EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Alan Sternf9c99bb2009-06-02 11:53:55 -0400576void usb_serial_generic_release(struct usb_serial *serial)
577{
578 dbg("%s", __func__);
579}