blob: ad4823bbfa19c8d73058360319ec3050ff362f2e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Serial Converter Generic functions
3 *
4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/slab.h>
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -070015#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/tty.h>
17#include <linux/tty_flip.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070021#include <linux/usb/serial.h>
Alan Coxae643872008-07-22 11:11:55 +010022#include <linux/uaccess.h>
David VomLehn8e8dce02009-08-28 12:54:27 -070023#include <linux/kfifo.h>
Alan Stern1f871582010-02-17 10:05:47 -050024#include <linux/serial.h>
Johannes Hölzld9b1b782006-12-17 21:50:24 +010025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int debug;
27
Johan Hovold25d514c2010-03-17 23:06:07 +010028#define MAX_TX_URBS 40
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#ifdef CONFIG_USB_SERIAL_GENERIC
David Brownellb46d60f2007-03-23 12:51:55 -070031
32static int generic_probe(struct usb_interface *interface,
33 const struct usb_device_id *id);
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static __u16 vendor = 0x05f9;
36static __u16 product = 0xffff;
37
38module_param(vendor, ushort, 0);
39MODULE_PARM_DESC(vendor, "User specified USB idVendor");
40
41module_param(product, ushort, 0);
42MODULE_PARM_DESC(product, "User specified USB idProduct");
43
44static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
45
Johannes Hölzld9b1b782006-12-17 21:50:24 +010046/* we want to look at all devices, as the vendor/product id can change
47 * depending on the command line argument */
Németh Márton7d40d7e2010-01-10 15:34:24 +010048static const struct usb_device_id generic_serial_ids[] = {
Johannes Hölzld9b1b782006-12-17 21:50:24 +010049 {.driver_info = 42},
50 {}
51};
52
53static struct usb_driver generic_driver = {
54 .name = "usbserial_generic",
55 .probe = generic_probe,
56 .disconnect = usb_serial_disconnect,
57 .id_table = generic_serial_ids,
58 .no_dynamic_id = 1,
59};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070062struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070063 .driver = {
64 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070065 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070066 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .id_table = generic_device_ids,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010068 .usb_driver = &generic_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040070 .disconnect = usb_serial_generic_disconnect,
71 .release = usb_serial_generic_release,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010072 .throttle = usb_serial_generic_throttle,
73 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020074 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int generic_probe(struct usb_interface *interface,
78 const struct usb_device_id *id)
79{
80 const struct usb_device_id *id_pattern;
81
82 id_pattern = usb_match_id(interface, generic_device_ids);
83 if (id_pattern != NULL)
84 return usb_serial_probe(interface, id);
85 return -ENODEV;
86}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#endif
88
Alan Coxae643872008-07-22 11:11:55 +010089int usb_serial_generic_register(int _debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int retval = 0;
92
93 debug = _debug;
94#ifdef CONFIG_USB_SERIAL_GENERIC
95 generic_device_ids[0].idVendor = vendor;
96 generic_device_ids[0].idProduct = product;
Alan Coxae643872008-07-22 11:11:55 +010097 generic_device_ids[0].match_flags =
98 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* register our generic driver with ourselves */
Alan Coxae643872008-07-22 11:11:55 +0100101 retval = usb_serial_register(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (retval)
103 goto exit;
104 retval = usb_register(&generic_driver);
105 if (retval)
106 usb_serial_deregister(&usb_serial_generic_device);
107exit:
108#endif
109 return retval;
110}
111
Alan Coxae643872008-07-22 11:11:55 +0100112void usb_serial_generic_deregister(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114#ifdef CONFIG_USB_SERIAL_GENERIC
115 /* remove our generic driver */
116 usb_deregister(&generic_driver);
Alan Coxae643872008-07-22 11:11:55 +0100117 usb_serial_deregister(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#endif
119}
120
Alan Coxa509a7e2009-09-19 13:13:26 -0700121int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100124 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Harvey Harrison441b62c2008-03-03 16:08:34 -0800126 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100128 /* clear the throttle flags */
129 spin_lock_irqsave(&port->lock, flags);
130 port->throttled = 0;
131 port->throttle_req = 0;
132 spin_unlock_irqrestore(&port->lock, flags);
133
134 /* if we have a bulk endpoint, start reading from it */
Johan Hovold41bd72f2010-03-17 23:05:53 +0100135 if (port->bulk_in_size)
136 result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 return result;
139}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700140EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Alan Cox95da3102008-07-22 11:09:07 +0100142static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct usb_serial *serial = port->serial;
Johan Hovoldec3ee502010-03-17 23:00:44 +0100145 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Harvey Harrison441b62c2008-03-03 16:08:34 -0800147 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (serial->dev) {
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100150 /* shutdown any bulk transfers that might be going on */
Johan Hovoldec3ee502010-03-17 23:00:44 +0100151 if (port->bulk_out_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 usb_kill_urb(port->write_urb);
Johan Hovoldec3ee502010-03-17 23:00:44 +0100153
154 spin_lock_irqsave(&port->lock, flags);
155 kfifo_reset_out(&port->write_fifo);
156 spin_unlock_irqrestore(&port->lock, flags);
157 }
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100158 if (port->bulk_in_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 usb_kill_urb(port->read_urb);
160 }
161}
162
Alan Cox335f8512009-06-11 12:26:29 +0100163void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800165 dbg("%s - port %d", __func__, port->number);
Alan Coxae643872008-07-22 11:11:55 +0100166 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Johan Hovoldf26788d2010-03-17 23:00:45 +0100168EXPORT_SYMBOL_GPL(usb_serial_generic_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jason Wessel715b1dc2009-05-11 15:24:07 -0500170static int usb_serial_multi_urb_write(struct tty_struct *tty,
171 struct usb_serial_port *port, const unsigned char *buf, int count)
172{
173 unsigned long flags;
174 struct urb *urb;
175 unsigned char *buffer;
176 int status;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500177
Johan Hovold25d514c2010-03-17 23:06:07 +0100178 spin_lock_irqsave(&port->lock, flags);
179 if (port->tx_urbs == MAX_TX_URBS) {
Jason Wessel715b1dc2009-05-11 15:24:07 -0500180 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100181 dbg("%s - write limit hit", __func__);
182 return 0;
183 }
184 port->tx_urbs++;
185 spin_unlock_irqrestore(&port->lock, flags);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500186
Johan Hovold25d514c2010-03-17 23:06:07 +0100187 urb = usb_alloc_urb(0, GFP_ATOMIC);
188 if (!urb) {
189 dev_err(&port->dev, "%s - no free urbs available\n", __func__);
190 status = -ENOMEM;
191 goto err_urb;
192 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500193
Johan Hovold25d514c2010-03-17 23:06:07 +0100194 count = min_t(int, count, PAGE_SIZE);
195 buffer = kmalloc(count, GFP_ATOMIC);
196 if (!buffer) {
197 dev_err(&port->dev, "%s - could not allocate buffer\n",
Jason Wessel715b1dc2009-05-11 15:24:07 -0500198 __func__);
Johan Hovold25d514c2010-03-17 23:06:07 +0100199 status = -ENOMEM;
200 goto err_buf;
201 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500202
Johan Hovold25d514c2010-03-17 23:06:07 +0100203 memcpy(buffer, buf, count);
204 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
205 usb_fill_bulk_urb(urb, port->serial->dev,
Jason Wessel715b1dc2009-05-11 15:24:07 -0500206 usb_sndbulkpipe(port->serial->dev,
207 port->bulk_out_endpointAddress),
Johan Hovold25d514c2010-03-17 23:06:07 +0100208 buffer, count,
Johan Hovold40f92f02010-03-17 23:06:06 +0100209 port->serial->type->write_bulk_callback, port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500210
Johan Hovold25d514c2010-03-17 23:06:07 +0100211 status = usb_submit_urb(urb, GFP_ATOMIC);
212 if (status) {
213 dev_err(&port->dev, "%s - error submitting urb: %d\n",
Jason Wessel715b1dc2009-05-11 15:24:07 -0500214 __func__, status);
Johan Hovold25d514c2010-03-17 23:06:07 +0100215 goto err;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500216 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500217 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100218 port->tx_bytes += urb->transfer_buffer_length;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500219 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100220
221 usb_free_urb(urb);
222
223 return count;
224err:
225 kfree(buffer);
226err_buf:
227 usb_free_urb(urb);
228err_urb:
229 spin_lock_irqsave(&port->lock, flags);
230 port->tx_urbs--;
231 spin_unlock_irqrestore(&port->lock, flags);
232
233 return status;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500234}
235
David VomLehn8e8dce02009-08-28 12:54:27 -0700236/**
237 * usb_serial_generic_write_start - kick off an URB write
238 * @port: Pointer to the &struct usb_serial_port data
239 *
240 * Returns the number of bytes queued on success. This will be zero if there
241 * was nothing to send. Otherwise, it returns a negative errno value
242 */
243static int usb_serial_generic_write_start(struct usb_serial_port *port)
244{
David VomLehn8e8dce02009-08-28 12:54:27 -0700245 unsigned char *data;
246 int result;
247 int count;
248 unsigned long flags;
David VomLehn8e8dce02009-08-28 12:54:27 -0700249
David VomLehn8e8dce02009-08-28 12:54:27 -0700250 spin_lock_irqsave(&port->lock, flags);
Johan Hovold50a5f702010-03-17 23:06:02 +0100251 if (port->write_urb_busy || !kfifo_len(&port->write_fifo)) {
252 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700253 return 0;
Johan Hovold50a5f702010-03-17 23:06:02 +0100254 }
255 port->write_urb_busy = 1;
256 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700257
258 data = port->write_urb->transfer_buffer;
Stefani Seibold119eecc2009-12-23 09:10:48 +0100259 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700260 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
261
Johan Hovold056afc02010-03-17 23:05:54 +0100262 port->write_urb->transfer_buffer_length = count;
David VomLehn8e8dce02009-08-28 12:54:27 -0700263
264 /* send the data out the bulk port */
265 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
266 if (result) {
Johan Hovold1a1405e2010-03-17 23:06:01 +0100267 dev_err(&port->dev, "%s - error submitting urb: %d\n",
David VomLehn8e8dce02009-08-28 12:54:27 -0700268 __func__, result);
269 /* don't have to grab the lock here, as we will
270 retry if != 0 */
271 port->write_urb_busy = 0;
Johan Hovold30af7fb2010-03-17 23:00:42 +0100272 return result;
273 }
David VomLehn8e8dce02009-08-28 12:54:27 -0700274
Johan Hovold30af7fb2010-03-17 23:00:42 +0100275 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100276 port->tx_bytes += count;
Johan Hovold30af7fb2010-03-17 23:00:42 +0100277 spin_unlock_irqrestore(&port->lock, flags);
278
279 return count;
David VomLehn8e8dce02009-08-28 12:54:27 -0700280}
281
282/**
283 * usb_serial_generic_write - generic write function for serial USB devices
284 * @tty: Pointer to &struct tty_struct for the device
285 * @port: Pointer to the &usb_serial_port structure for the device
286 * @buf: Pointer to the data to write
287 * @count: Number of bytes to write
288 *
289 * Returns the number of characters actually written, which may be anything
290 * from zero to @count. If an error occurs, it returns the negative errno
291 * value.
292 */
Alan Cox95da3102008-07-22 11:09:07 +0100293int usb_serial_generic_write(struct tty_struct *tty,
294 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct usb_serial *serial = port->serial;
297 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Harvey Harrison441b62c2008-03-03 16:08:34 -0800299 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100301 /* only do something if we have a bulk out endpoint */
302 if (!port->bulk_out_size)
303 return -ENODEV;
304
Johan Hovold1a1405e2010-03-17 23:06:01 +0100305 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Johan Hovold25d514c2010-03-17 23:06:07 +0100308 if (serial->type->multi_urb_write)
309 return usb_serial_multi_urb_write(tty, port, buf, count);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500310
Stefani Seibold119eecc2009-12-23 09:10:48 +0100311 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700312 result = usb_serial_generic_write_start(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
David VomLehn8e8dce02009-08-28 12:54:27 -0700314 if (result >= 0)
315 result = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
David VomLehn8e8dce02009-08-28 12:54:27 -0700317 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500319EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Alan Coxae643872008-07-22 11:11:55 +0100321int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Alan Cox95da3102008-07-22 11:09:07 +0100323 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500325 unsigned long flags;
Johan Hovold25d514c2010-03-17 23:06:07 +0100326 int room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Harvey Harrison441b62c2008-03-03 16:08:34 -0800328 dbg("%s - port %d", __func__, port->number);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100329
330 if (!port->bulk_out_size)
331 return 0;
332
Jason Wessel715b1dc2009-05-11 15:24:07 -0500333 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100334 if (serial->type->multi_urb_write)
335 room = (MAX_TX_URBS - port->tx_urbs) * PAGE_SIZE;
336 else
Stefani Seibold119eecc2009-12-23 09:10:48 +0100337 room = kfifo_avail(&port->write_fifo);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500338 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Harvey Harrison441b62c2008-03-03 16:08:34 -0800340 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100341 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Alan Cox95da3102008-07-22 11:09:07 +0100344int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Alan Cox95da3102008-07-22 11:09:07 +0100346 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500348 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100349 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Harvey Harrison441b62c2008-03-03 16:08:34 -0800351 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100353 if (!port->bulk_out_size)
354 return 0;
355
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100356 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100357 if (serial->type->multi_urb_write)
358 chars = port->tx_bytes;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100359 else
Johan Hovold25d514c2010-03-17 23:06:07 +0100360 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100361 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Harvey Harrison441b62c2008-03-03 16:08:34 -0800363 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100364 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Johan Hovold41bd72f2010-03-17 23:05:53 +0100367int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
368 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int result;
371
Johan Hovold056afc02010-03-17 23:05:54 +0100372 result = usb_submit_urb(port->read_urb, mem_flags);
Johan Hovold0ae14742010-02-27 14:05:46 +0100373 if (result && result != -EPERM) {
Johan Hovold1a1405e2010-03-17 23:06:01 +0100374 dev_err(&port->dev, "%s - error submitting urb: %d\n",
Alan Coxae643872008-07-22 11:11:55 +0100375 __func__, result);
Johan Hovold0ae14742010-02-27 14:05:46 +0100376 }
Johan Hovold41bd72f2010-03-17 23:05:53 +0100377 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Johan Hovold41bd72f2010-03-17 23:05:53 +0100379EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100380
Johan Hovold23154322010-03-17 23:05:57 +0100381void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200382{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100383 struct usb_serial_port *port = urb->context;
384 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500385 char *ch = (char *)urb->transfer_buffer;
386 int i;
387
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100388 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500389 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100390 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200391
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100392 /* The per character mucking around with sysrq path it too slow for
393 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
394 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600395 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100396 tty_insert_flip_string(tty, ch, urb->actual_length);
397 else {
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100398 for (i = 0; i < urb->actual_length; i++, ch++) {
Alan Cox24a15a62009-07-09 13:36:22 +0100399 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100400 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
401 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200402 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500403 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100404 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200405}
Johan Hovold23154322010-03-17 23:05:57 +0100406EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200407
Alan Cox95da3102008-07-22 11:09:07 +0100408void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100409{
Ming Leicdc97792008-02-24 18:41:47 +0800410 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100411 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700412 int status = urb->status;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100413 unsigned long flags;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100414
Harvey Harrison441b62c2008-03-03 16:08:34 -0800415 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100416
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700417 if (unlikely(status != 0)) {
418 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800419 __func__, status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100420 return;
421 }
422
Alan Coxae643872008-07-22 11:11:55 +0100423 usb_serial_debug_data(debug, &port->dev, __func__,
424 urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100425 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100426
427 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100428 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100429 port->throttled = port->throttle_req;
430 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800431 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100432 usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100433 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800434 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100435}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300436EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Alan Cox95da3102008-07-22 11:09:07 +0100438void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500440 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800441 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700442 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Harvey Harrison441b62c2008-03-03 16:08:34 -0800444 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Johan Hovold25d514c2010-03-17 23:06:07 +0100446 if (port->serial->type->multi_urb_write) {
Johan Hovold25915302010-01-06 15:48:42 -0800447 kfree(urb->transfer_buffer);
448
Jason Wessel715b1dc2009-05-11 15:24:07 -0500449 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100450 port->tx_bytes -= urb->transfer_buffer_length;
451 port->tx_urbs--;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500452 spin_unlock_irqrestore(&port->lock, flags);
453 } else {
Johan Hovold30af7fb2010-03-17 23:00:42 +0100454 spin_lock_irqsave(&port->lock, flags);
Johan Hovold25d514c2010-03-17 23:06:07 +0100455 port->tx_bytes -= urb->transfer_buffer_length;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500456 port->write_urb_busy = 0;
Johan Hovold30af7fb2010-03-17 23:00:42 +0100457 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700458
Johan Hovold50dbb852010-03-17 23:00:43 +0100459 if (status) {
460 spin_lock_irqsave(&port->lock, flags);
Stefani Seibold119eecc2009-12-23 09:10:48 +0100461 kfifo_reset_out(&port->write_fifo);
Johan Hovold50dbb852010-03-17 23:00:43 +0100462 spin_unlock_irqrestore(&port->lock, flags);
463 } else {
David VomLehn8e8dce02009-08-28 12:54:27 -0700464 usb_serial_generic_write_start(port);
Johan Hovold50dbb852010-03-17 23:00:43 +0100465 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500466 }
467
Johan Hovold63136202010-02-27 14:06:07 +0100468 if (status)
469 dbg("%s - non-zero urb status: %d", __func__, status);
470
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700471 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800473EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Alan Cox95da3102008-07-22 11:09:07 +0100475void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100476{
Alan Cox95da3102008-07-22 11:09:07 +0100477 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100478 unsigned long flags;
479
Harvey Harrison441b62c2008-03-03 16:08:34 -0800480 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100481
482 /* Set the throttle request flag. It will be picked up
483 * by usb_serial_generic_read_bulk_callback(). */
484 spin_lock_irqsave(&port->lock, flags);
485 port->throttle_req = 1;
486 spin_unlock_irqrestore(&port->lock, flags);
487}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100488EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100489
Alan Cox95da3102008-07-22 11:09:07 +0100490void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100491{
Alan Cox95da3102008-07-22 11:09:07 +0100492 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100493 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100494
Harvey Harrison441b62c2008-03-03 16:08:34 -0800495 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100496
497 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100498 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100499 was_throttled = port->throttled;
500 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100501 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100502
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100503 if (was_throttled)
504 usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100505}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100506EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100507
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700508#ifdef CONFIG_MAGIC_SYSRQ
Alan Cox24a15a62009-07-09 13:36:22 +0100509int usb_serial_handle_sysrq_char(struct tty_struct *tty,
510 struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500511{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600512 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500513 if (ch && time_before(jiffies, port->sysrq)) {
Alan Cox24a15a62009-07-09 13:36:22 +0100514 handle_sysrq(ch, tty);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500515 port->sysrq = 0;
516 return 1;
517 }
518 port->sysrq = 0;
519 }
520 return 0;
521}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700522#else
523int usb_serial_handle_sysrq_char(struct tty_struct *tty,
524 struct usb_serial_port *port, unsigned int ch)
525{
526 return 0;
527}
528#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500529EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
530
531int usb_serial_handle_break(struct usb_serial_port *port)
532{
533 if (!port->sysrq) {
534 port->sysrq = jiffies + HZ*5;
535 return 1;
536 }
537 port->sysrq = 0;
538 return 0;
539}
540EXPORT_SYMBOL_GPL(usb_serial_handle_break);
541
David VomLehn8e8dce02009-08-28 12:54:27 -0700542int usb_serial_generic_resume(struct usb_serial *serial)
543{
544 struct usb_serial_port *port;
545 int i, c = 0, r;
546
547 for (i = 0; i < serial->num_ports; i++) {
548 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500549 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700550 continue;
551
552 if (port->read_urb) {
553 r = usb_submit_urb(port->read_urb, GFP_NOIO);
554 if (r < 0)
555 c++;
556 }
557
558 if (port->write_urb) {
559 r = usb_serial_generic_write_start(port);
560 if (r < 0)
561 c++;
562 }
563 }
564
565 return c ? -EIO : 0;
566}
567EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
568
Alan Sternf9c99bb2009-06-02 11:53:55 -0400569void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 int i;
572
Harvey Harrison441b62c2008-03-03 16:08:34 -0800573 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100576 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Alan Sternf9c99bb2009-06-02 11:53:55 -0400580void usb_serial_generic_release(struct usb_serial *serial)
581{
582 dbg("%s", __func__);
583}