blob: 3ae17840175cfa61969eabfba78f435e10910723 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Serial Converter Generic functions
3 *
Johan Hovold27c7acf2010-05-05 23:57:37 +02004 * Copyright (C) 2010 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
Johan Hovold25d514c2010-03-17 23:06:07 +010029#define MAX_TX_URBS 40
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#ifdef CONFIG_USB_SERIAL_GENERIC
David Brownellb46d60f2007-03-23 12:51:55 -070032
33static int generic_probe(struct usb_interface *interface,
34 const struct usb_device_id *id);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static __u16 vendor = 0x05f9;
37static __u16 product = 0xffff;
38
39module_param(vendor, ushort, 0);
40MODULE_PARM_DESC(vendor, "User specified USB idVendor");
41
42module_param(product, ushort, 0);
43MODULE_PARM_DESC(product, "User specified USB idProduct");
44
45static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
46
Johannes Hölzld9b1b782006-12-17 21:50:24 +010047/* we want to look at all devices, as the vendor/product id can change
48 * depending on the command line argument */
Németh Márton7d40d7e2010-01-10 15:34:24 +010049static const struct usb_device_id generic_serial_ids[] = {
Johannes Hölzld9b1b782006-12-17 21:50:24 +010050 {.driver_info = 42},
51 {}
52};
53
54static struct usb_driver generic_driver = {
55 .name = "usbserial_generic",
56 .probe = generic_probe,
57 .disconnect = usb_serial_disconnect,
58 .id_table = generic_serial_ids,
59 .no_dynamic_id = 1,
60};
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070063struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070064 .driver = {
65 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070066 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070067 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .id_table = generic_device_ids,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010069 .usb_driver = &generic_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040071 .disconnect = usb_serial_generic_disconnect,
72 .release = usb_serial_generic_release,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010073 .throttle = usb_serial_generic_throttle,
74 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020075 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
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 Coxae643872008-07-22 11:11:55 +0100102 retval = usb_serial_register(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (retval)
104 goto exit;
105 retval = usb_register(&generic_driver);
106 if (retval)
107 usb_serial_deregister(&usb_serial_generic_device);
108exit:
109#endif
110 return retval;
111}
112
Alan Coxae643872008-07-22 11:11:55 +0100113void usb_serial_generic_deregister(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115#ifdef CONFIG_USB_SERIAL_GENERIC
116 /* remove our generic driver */
117 usb_deregister(&generic_driver);
Alan Coxae643872008-07-22 11:11:55 +0100118 usb_serial_deregister(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#endif
120}
121
Alan Coxa509a7e2009-09-19 13:13:26 -0700122int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100125 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Harvey Harrison441b62c2008-03-03 16:08:34 -0800127 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100129 /* clear the throttle flags */
130 spin_lock_irqsave(&port->lock, flags);
131 port->throttled = 0;
132 port->throttle_req = 0;
133 spin_unlock_irqrestore(&port->lock, flags);
134
135 /* if we have a bulk endpoint, start reading from it */
Johan Hovold41bd72f2010-03-17 23:05:53 +0100136 if (port->bulk_in_size)
137 result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 return result;
140}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700141EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Alan Cox95da3102008-07-22 11:09:07 +0100143static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct usb_serial *serial = port->serial;
Johan Hovoldec3ee502010-03-17 23:00:44 +0100146 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200147 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Harvey Harrison441b62c2008-03-03 16:08:34 -0800149 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (serial->dev) {
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100152 /* shutdown any bulk transfers that might be going on */
Johan Hovoldec3ee502010-03-17 23:00:44 +0100153 if (port->bulk_out_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 usb_kill_urb(port->write_urb);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200155 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
156 usb_kill_urb(port->write_urbs[i]);
Johan Hovoldec3ee502010-03-17 23:00:44 +0100157
158 spin_lock_irqsave(&port->lock, flags);
159 kfifo_reset_out(&port->write_fifo);
160 spin_unlock_irqrestore(&port->lock, flags);
161 }
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100162 if (port->bulk_in_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 usb_kill_urb(port->read_urb);
164 }
165}
166
Alan Cox335f8512009-06-11 12:26:29 +0100167void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800169 dbg("%s - port %d", __func__, port->number);
Alan Coxae643872008-07-22 11:11:55 +0100170 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
Johan Hovoldf26788d2010-03-17 23:00:45 +0100172EXPORT_SYMBOL_GPL(usb_serial_generic_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100174int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
175 void **dest, size_t size, const void *src, size_t count)
176{
177 if (!*dest) {
178 size = count;
179 *dest = kmalloc(count, GFP_ATOMIC);
180 if (!*dest) {
181 dev_err(&port->dev, "%s - could not allocate buffer\n",
182 __func__);
183 return -ENOMEM;
184 }
185 }
186 if (src) {
187 count = size;
188 memcpy(*dest, src, size);
189 } else {
190 count = kfifo_out_locked(&port->write_fifo, *dest, size,
191 &port->lock);
192 }
193 return count;
194}
195EXPORT_SYMBOL_GPL(usb_serial_generic_prepare_write_buffer);
196
Jason Wessel715b1dc2009-05-11 15:24:07 -0500197static int usb_serial_multi_urb_write(struct tty_struct *tty,
198 struct usb_serial_port *port, const unsigned char *buf, int count)
199{
200 unsigned long flags;
201 struct urb *urb;
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100202 void *buffer;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500203 int status;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500204
Johan Hovold25d514c2010-03-17 23:06:07 +0100205 spin_lock_irqsave(&port->lock, flags);
206 if (port->tx_urbs == MAX_TX_URBS) {
Jason Wessel715b1dc2009-05-11 15:24:07 -0500207 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100208 dbg("%s - write limit hit", __func__);
209 return 0;
210 }
211 port->tx_urbs++;
212 spin_unlock_irqrestore(&port->lock, flags);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500213
Johan Hovold25d514c2010-03-17 23:06:07 +0100214 urb = usb_alloc_urb(0, GFP_ATOMIC);
215 if (!urb) {
216 dev_err(&port->dev, "%s - no free urbs available\n", __func__);
217 status = -ENOMEM;
218 goto err_urb;
219 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500220
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100221 buffer = NULL;
Johan Hovold25d514c2010-03-17 23:06:07 +0100222 count = min_t(int, count, PAGE_SIZE);
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100223 count = port->serial->type->prepare_write_buffer(port, &buffer, 0,
224 buf, count);
225 if (count < 0) {
226 status = count;
Johan Hovold25d514c2010-03-17 23:06:07 +0100227 goto err_buf;
228 }
Johan Hovold25d514c2010-03-17 23:06:07 +0100229 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
230 usb_fill_bulk_urb(urb, port->serial->dev,
Jason Wessel715b1dc2009-05-11 15:24:07 -0500231 usb_sndbulkpipe(port->serial->dev,
232 port->bulk_out_endpointAddress),
Johan Hovold25d514c2010-03-17 23:06:07 +0100233 buffer, count,
Johan Hovold40f92f02010-03-17 23:06:06 +0100234 port->serial->type->write_bulk_callback, port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500235
Johan Hovold25d514c2010-03-17 23:06:07 +0100236 status = usb_submit_urb(urb, GFP_ATOMIC);
237 if (status) {
238 dev_err(&port->dev, "%s - error submitting urb: %d\n",
Jason Wessel715b1dc2009-05-11 15:24:07 -0500239 __func__, status);
Johan Hovold25d514c2010-03-17 23:06:07 +0100240 goto err;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500241 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500242 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100243 port->tx_bytes += urb->transfer_buffer_length;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500244 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100245
246 usb_free_urb(urb);
247
248 return count;
249err:
250 kfree(buffer);
251err_buf:
252 usb_free_urb(urb);
253err_urb:
254 spin_lock_irqsave(&port->lock, flags);
255 port->tx_urbs--;
256 spin_unlock_irqrestore(&port->lock, flags);
257
258 return status;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500259}
260
David VomLehn8e8dce02009-08-28 12:54:27 -0700261/**
262 * usb_serial_generic_write_start - kick off an URB write
263 * @port: Pointer to the &struct usb_serial_port data
264 *
Johan Hovold27c7acf2010-05-05 23:57:37 +0200265 * Returns zero on success, or a negative errno value
David VomLehn8e8dce02009-08-28 12:54:27 -0700266 */
267static int usb_serial_generic_write_start(struct usb_serial_port *port)
268{
Johan Hovold27c7acf2010-05-05 23:57:37 +0200269 struct urb *urb;
270 int count, result;
David VomLehn8e8dce02009-08-28 12:54:27 -0700271 unsigned long flags;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200272 int i;
David VomLehn8e8dce02009-08-28 12:54:27 -0700273
Johan Hovold27c7acf2010-05-05 23:57:37 +0200274 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
275 return 0;
276retry:
David VomLehn8e8dce02009-08-28 12:54:27 -0700277 spin_lock_irqsave(&port->lock, flags);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200278 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
279 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold50a5f702010-03-17 23:06:02 +0100280 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700281 return 0;
Johan Hovold50a5f702010-03-17 23:06:02 +0100282 }
Johan Hovold27c7acf2010-05-05 23:57:37 +0200283 i = (int)find_first_bit(&port->write_urbs_free,
284 ARRAY_SIZE(port->write_urbs));
Johan Hovold50a5f702010-03-17 23:06:02 +0100285 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700286
Johan Hovold27c7acf2010-05-05 23:57:37 +0200287 urb = port->write_urbs[i];
Johan Hovoldeaa3bcb2010-03-17 23:06:08 +0100288 count = port->serial->type->prepare_write_buffer(port,
Johan Hovold27c7acf2010-05-05 23:57:37 +0200289 &urb->transfer_buffer,
290 port->bulk_out_size, NULL, 0);
291 urb->transfer_buffer_length = count;
292 usb_serial_debug_data(debug, &port->dev, __func__, count,
293 urb->transfer_buffer);
294 result = usb_submit_urb(urb, GFP_ATOMIC);
David VomLehn8e8dce02009-08-28 12:54:27 -0700295 if (result) {
Johan Hovold1a1405e2010-03-17 23:06:01 +0100296 dev_err(&port->dev, "%s - error submitting urb: %d\n",
David VomLehn8e8dce02009-08-28 12:54:27 -0700297 __func__, result);
Johan Hovold27c7acf2010-05-05 23:57:37 +0200298 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
Johan Hovold30af7fb2010-03-17 23:00:42 +0100299 return result;
300 }
Johan Hovold27c7acf2010-05-05 23:57:37 +0200301 clear_bit(i, &port->write_urbs_free);
David VomLehn8e8dce02009-08-28 12:54:27 -0700302
Johan Hovold30af7fb2010-03-17 23:00:42 +0100303 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100304 port->tx_bytes += count;
Johan Hovold30af7fb2010-03-17 23:00:42 +0100305 spin_unlock_irqrestore(&port->lock, flags);
306
Johan Hovold27c7acf2010-05-05 23:57:37 +0200307 /* Try sending off another urb, unless in irq context (in which case
308 * there will be no free urb). */
309 if (!in_irq())
310 goto retry;
311
312 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
313
314 return 0;
David VomLehn8e8dce02009-08-28 12:54:27 -0700315}
316
317/**
318 * usb_serial_generic_write - generic write function for serial USB devices
319 * @tty: Pointer to &struct tty_struct for the device
320 * @port: Pointer to the &usb_serial_port structure for the device
321 * @buf: Pointer to the data to write
322 * @count: Number of bytes to write
323 *
324 * Returns the number of characters actually written, which may be anything
325 * from zero to @count. If an error occurs, it returns the negative errno
326 * value.
327 */
Alan Cox95da3102008-07-22 11:09:07 +0100328int usb_serial_generic_write(struct tty_struct *tty,
329 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct usb_serial *serial = port->serial;
332 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Harvey Harrison441b62c2008-03-03 16:08:34 -0800334 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100336 /* only do something if we have a bulk out endpoint */
337 if (!port->bulk_out_size)
338 return -ENODEV;
339
Johan Hovold1a1405e2010-03-17 23:06:01 +0100340 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Johan Hovold25d514c2010-03-17 23:06:07 +0100343 if (serial->type->multi_urb_write)
344 return usb_serial_multi_urb_write(tty, port, buf, count);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500345
Stefani Seibold119eecc2009-12-23 09:10:48 +0100346 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700347 result = usb_serial_generic_write_start(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
David VomLehn8e8dce02009-08-28 12:54:27 -0700349 if (result >= 0)
350 result = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
David VomLehn8e8dce02009-08-28 12:54:27 -0700352 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500354EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Alan Coxae643872008-07-22 11:11:55 +0100356int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Alan Cox95da3102008-07-22 11:09:07 +0100358 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500360 unsigned long flags;
Johan Hovold25d514c2010-03-17 23:06:07 +0100361 int room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Harvey Harrison441b62c2008-03-03 16:08:34 -0800363 dbg("%s - port %d", __func__, port->number);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100364
365 if (!port->bulk_out_size)
366 return 0;
367
Jason Wessel715b1dc2009-05-11 15:24:07 -0500368 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100369 if (serial->type->multi_urb_write)
370 room = (MAX_TX_URBS - port->tx_urbs) * PAGE_SIZE;
371 else
Stefani Seibold119eecc2009-12-23 09:10:48 +0100372 room = kfifo_avail(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500373 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Harvey Harrison441b62c2008-03-03 16:08:34 -0800375 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100376 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Alan Cox95da3102008-07-22 11:09:07 +0100379int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Alan Cox95da3102008-07-22 11:09:07 +0100381 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500383 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100384 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Harvey Harrison441b62c2008-03-03 16:08:34 -0800386 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100388 if (!port->bulk_out_size)
389 return 0;
390
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100391 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100392 if (serial->type->multi_urb_write)
393 chars = port->tx_bytes;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100394 else
Johan Hovold25d514c2010-03-17 23:06:07 +0100395 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100396 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Harvey Harrison441b62c2008-03-03 16:08:34 -0800398 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100399 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Johan Hovold41bd72f2010-03-17 23:05:53 +0100402int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
403 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int result;
406
Johan Hovold056afc02010-03-17 23:05:54 +0100407 result = usb_submit_urb(port->read_urb, mem_flags);
Johan Hovold0ae14742010-02-27 14:05:46 +0100408 if (result && result != -EPERM) {
Johan Hovold1a1405e2010-03-17 23:06:01 +0100409 dev_err(&port->dev, "%s - error submitting urb: %d\n",
Alan Coxae643872008-07-22 11:11:55 +0100410 __func__, result);
Johan Hovold0ae14742010-02-27 14:05:46 +0100411 }
Johan Hovold41bd72f2010-03-17 23:05:53 +0100412 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
Johan Hovold41bd72f2010-03-17 23:05:53 +0100414EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100415
Johan Hovold23154322010-03-17 23:05:57 +0100416void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200417{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100418 struct usb_serial_port *port = urb->context;
419 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500420 char *ch = (char *)urb->transfer_buffer;
421 int i;
422
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100423 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500424 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100425 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200426
Alan Cox4cd1de02009-07-09 13:35:52 +0100427 /* The per character mucking around with sysrq path it too slow for
428 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
429 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600430 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de02009-07-09 13:35:52 +0100431 tty_insert_flip_string(tty, ch, urb->actual_length);
432 else {
Alan Cox4cd1de02009-07-09 13:35:52 +0100433 for (i = 0; i < urb->actual_length; i++, ch++) {
Alan Cox24a15a62009-07-09 13:36:22 +0100434 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
Alan Cox4cd1de02009-07-09 13:35:52 +0100435 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
436 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200437 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500438 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100439 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200440}
Johan Hovold23154322010-03-17 23:05:57 +0100441EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200442
Alan Cox95da3102008-07-22 11:09:07 +0100443void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100444{
Ming Leicdc97792008-02-24 18:41:47 +0800445 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100446 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700447 int status = urb->status;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100448 unsigned long flags;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100449
Harvey Harrison441b62c2008-03-03 16:08:34 -0800450 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100451
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700452 if (unlikely(status != 0)) {
453 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800454 __func__, status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100455 return;
456 }
457
Alan Coxae643872008-07-22 11:11:55 +0100458 usb_serial_debug_data(debug, &port->dev, __func__,
459 urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100460 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100461
462 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100463 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100464 port->throttled = port->throttle_req;
465 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800466 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100467 usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100468 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800469 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100470}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300471EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Alan Cox95da3102008-07-22 11:09:07 +0100473void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500475 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800476 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700477 int status = urb->status;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200478 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Harvey Harrison441b62c2008-03-03 16:08:34 -0800480 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Johan Hovold25d514c2010-03-17 23:06:07 +0100482 if (port->serial->type->multi_urb_write) {
Johan Hovold25915302010-01-06 15:48:42 -0800483 kfree(urb->transfer_buffer);
484
Jason Wessel715b1dc2009-05-11 15:24:07 -0500485 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100486 port->tx_bytes -= urb->transfer_buffer_length;
487 port->tx_urbs--;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500488 spin_unlock_irqrestore(&port->lock, flags);
489 } else {
Johan Hovold27c7acf2010-05-05 23:57:37 +0200490 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
491 if (port->write_urbs[i] == urb)
492 break;
493
Johan Hovold30af7fb2010-03-17 23:00:42 +0100494 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100495 port->tx_bytes -= urb->transfer_buffer_length;
Johan Hovold27c7acf2010-05-05 23:57:37 +0200496 set_bit(i, &port->write_urbs_free);
Johan Hovold30af7fb2010-03-17 23:00:42 +0100497 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700498
Johan Hovold50dbb852010-03-17 23:00:43 +0100499 if (status) {
500 spin_lock_irqsave(&port->lock, flags);
Stefani Seibold119eecc2009-12-23 09:10:48 +0100501 kfifo_reset_out(&port->write_fifo);
Johan Hovold50dbb852010-03-17 23:00:43 +0100502 spin_unlock_irqrestore(&port->lock, flags);
503 } else {
David VomLehn8e8dce02009-08-28 12:54:27 -0700504 usb_serial_generic_write_start(port);
Johan Hovold50dbb852010-03-17 23:00:43 +0100505 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500506 }
507
Johan Hovold63136202010-02-27 14:06:07 +0100508 if (status)
509 dbg("%s - non-zero urb status: %d", __func__, status);
510
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700511 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800513EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Alan Cox95da3102008-07-22 11:09:07 +0100515void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100516{
Alan Cox95da3102008-07-22 11:09:07 +0100517 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100518 unsigned long flags;
519
Harvey Harrison441b62c2008-03-03 16:08:34 -0800520 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100521
522 /* Set the throttle request flag. It will be picked up
523 * by usb_serial_generic_read_bulk_callback(). */
524 spin_lock_irqsave(&port->lock, flags);
525 port->throttle_req = 1;
526 spin_unlock_irqrestore(&port->lock, flags);
527}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100528EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100529
Alan Cox95da3102008-07-22 11:09:07 +0100530void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100531{
Alan Cox95da3102008-07-22 11:09:07 +0100532 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100533 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100534
Harvey Harrison441b62c2008-03-03 16:08:34 -0800535 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100536
537 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100538 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100539 was_throttled = port->throttled;
540 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100541 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100542
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100543 if (was_throttled)
544 usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100545}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100546EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100547
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700548#ifdef CONFIG_MAGIC_SYSRQ
Alan Cox24a15a62009-07-09 13:36:22 +0100549int usb_serial_handle_sysrq_char(struct tty_struct *tty,
550 struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500551{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600552 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500553 if (ch && time_before(jiffies, port->sysrq)) {
Alan Cox24a15a62009-07-09 13:36:22 +0100554 handle_sysrq(ch, tty);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500555 port->sysrq = 0;
556 return 1;
557 }
558 port->sysrq = 0;
559 }
560 return 0;
561}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700562#else
563int usb_serial_handle_sysrq_char(struct tty_struct *tty,
564 struct usb_serial_port *port, unsigned int ch)
565{
566 return 0;
567}
568#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500569EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
570
571int usb_serial_handle_break(struct usb_serial_port *port)
572{
573 if (!port->sysrq) {
574 port->sysrq = jiffies + HZ*5;
575 return 1;
576 }
577 port->sysrq = 0;
578 return 0;
579}
580EXPORT_SYMBOL_GPL(usb_serial_handle_break);
581
David VomLehn8e8dce02009-08-28 12:54:27 -0700582int usb_serial_generic_resume(struct usb_serial *serial)
583{
584 struct usb_serial_port *port;
585 int i, c = 0, r;
586
587 for (i = 0; i < serial->num_ports; i++) {
588 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500589 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700590 continue;
591
592 if (port->read_urb) {
593 r = usb_submit_urb(port->read_urb, GFP_NOIO);
594 if (r < 0)
595 c++;
596 }
597
Johan Hovold27c7acf2010-05-05 23:57:37 +0200598 if (port->bulk_out_size) {
David VomLehn8e8dce02009-08-28 12:54:27 -0700599 r = usb_serial_generic_write_start(port);
600 if (r < 0)
601 c++;
602 }
603 }
604
605 return c ? -EIO : 0;
606}
607EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
608
Alan Sternf9c99bb2009-06-02 11:53:55 -0400609void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 int i;
612
Harvey Harrison441b62c2008-03-03 16:08:34 -0800613 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100616 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Alan Sternf9c99bb2009-06-02 11:53:55 -0400620void usb_serial_generic_release(struct usb_serial *serial)
621{
622 dbg("%s", __func__);
623}