blob: be52c748bccb1c569799975824916095883e5e65 [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
28#ifdef CONFIG_USB_SERIAL_GENERIC
David Brownellb46d60f2007-03-23 12:51:55 -070029
30static int generic_probe(struct usb_interface *interface,
31 const struct usb_device_id *id);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static __u16 vendor = 0x05f9;
34static __u16 product = 0xffff;
35
36module_param(vendor, ushort, 0);
37MODULE_PARM_DESC(vendor, "User specified USB idVendor");
38
39module_param(product, ushort, 0);
40MODULE_PARM_DESC(product, "User specified USB idProduct");
41
42static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
43
Johannes Hölzld9b1b782006-12-17 21:50:24 +010044/* we want to look at all devices, as the vendor/product id can change
45 * depending on the command line argument */
Németh Márton7d40d7e2010-01-10 15:34:24 +010046static const struct usb_device_id generic_serial_ids[] = {
Johannes Hölzld9b1b782006-12-17 21:50:24 +010047 {.driver_info = 42},
48 {}
49};
50
51static struct usb_driver generic_driver = {
52 .name = "usbserial_generic",
53 .probe = generic_probe,
54 .disconnect = usb_serial_disconnect,
55 .id_table = generic_serial_ids,
56 .no_dynamic_id = 1,
57};
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,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010066 .usb_driver = &generic_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040068 .disconnect = usb_serial_generic_disconnect,
69 .release = usb_serial_generic_release,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010070 .throttle = usb_serial_generic_throttle,
71 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020072 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int generic_probe(struct usb_interface *interface,
76 const struct usb_device_id *id)
77{
78 const struct usb_device_id *id_pattern;
79
80 id_pattern = usb_match_id(interface, generic_device_ids);
81 if (id_pattern != NULL)
82 return usb_serial_probe(interface, id);
83 return -ENODEV;
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#endif
86
Alan Coxae643872008-07-22 11:11:55 +010087int usb_serial_generic_register(int _debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int retval = 0;
90
91 debug = _debug;
92#ifdef CONFIG_USB_SERIAL_GENERIC
93 generic_device_ids[0].idVendor = vendor;
94 generic_device_ids[0].idProduct = product;
Alan Coxae643872008-07-22 11:11:55 +010095 generic_device_ids[0].match_flags =
96 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* register our generic driver with ourselves */
Alan Coxae643872008-07-22 11:11:55 +010099 retval = usb_serial_register(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (retval)
101 goto exit;
102 retval = usb_register(&generic_driver);
103 if (retval)
104 usb_serial_deregister(&usb_serial_generic_device);
105exit:
106#endif
107 return retval;
108}
109
Alan Coxae643872008-07-22 11:11:55 +0100110void usb_serial_generic_deregister(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112#ifdef CONFIG_USB_SERIAL_GENERIC
113 /* remove our generic driver */
114 usb_deregister(&generic_driver);
Alan Coxae643872008-07-22 11:11:55 +0100115 usb_serial_deregister(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#endif
117}
118
Alan Coxa509a7e2009-09-19 13:13:26 -0700119int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100122 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Harvey Harrison441b62c2008-03-03 16:08:34 -0800124 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100126 /* clear the throttle flags */
127 spin_lock_irqsave(&port->lock, flags);
128 port->throttled = 0;
129 port->throttle_req = 0;
130 spin_unlock_irqrestore(&port->lock, flags);
131
132 /* if we have a bulk endpoint, start reading from it */
Johan Hovold41bd72f2010-03-17 23:05:53 +0100133 if (port->bulk_in_size)
134 result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 return result;
137}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700138EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Alan Cox95da3102008-07-22 11:09:07 +0100140static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct usb_serial *serial = port->serial;
Johan Hovoldec3ee502010-03-17 23:00:44 +0100143 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Harvey Harrison441b62c2008-03-03 16:08:34 -0800145 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 if (serial->dev) {
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100148 /* shutdown any bulk transfers that might be going on */
Johan Hovoldec3ee502010-03-17 23:00:44 +0100149 if (port->bulk_out_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 usb_kill_urb(port->write_urb);
Johan Hovoldec3ee502010-03-17 23:00:44 +0100151
152 spin_lock_irqsave(&port->lock, flags);
153 kfifo_reset_out(&port->write_fifo);
154 spin_unlock_irqrestore(&port->lock, flags);
155 }
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100156 if (port->bulk_in_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 usb_kill_urb(port->read_urb);
158 }
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
Jason Wessel715b1dc2009-05-11 15:24:07 -0500168static int usb_serial_multi_urb_write(struct tty_struct *tty,
169 struct usb_serial_port *port, const unsigned char *buf, int count)
170{
171 unsigned long flags;
172 struct urb *urb;
173 unsigned char *buffer;
174 int status;
175 int towrite;
176 int bwrite = 0;
177
178 dbg("%s - port %d", __func__, port->number);
179
180 if (count == 0)
181 dbg("%s - write request of 0 bytes", __func__);
182
183 while (count > 0) {
184 towrite = (count > port->bulk_out_size) ?
185 port->bulk_out_size : count;
186 spin_lock_irqsave(&port->lock, flags);
187 if (port->urbs_in_flight >
188 port->serial->type->max_in_flight_urbs) {
189 spin_unlock_irqrestore(&port->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800190 dbg("%s - write limit hit", __func__);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500191 return bwrite;
192 }
193 port->tx_bytes_flight += towrite;
194 port->urbs_in_flight++;
195 spin_unlock_irqrestore(&port->lock, flags);
196
197 buffer = kmalloc(towrite, GFP_ATOMIC);
198 if (!buffer) {
199 dev_err(&port->dev,
200 "%s ran out of kernel memory for urb ...\n", __func__);
201 goto error_no_buffer;
202 }
203
204 urb = usb_alloc_urb(0, GFP_ATOMIC);
205 if (!urb) {
206 dev_err(&port->dev, "%s - no more free urbs\n",
207 __func__);
208 goto error_no_urb;
209 }
210
211 /* Copy data */
212 memcpy(buffer, buf + bwrite, towrite);
213 usb_serial_debug_data(debug, &port->dev, __func__,
214 towrite, buffer);
215 /* fill the buffer and send it */
216 usb_fill_bulk_urb(urb, port->serial->dev,
217 usb_sndbulkpipe(port->serial->dev,
218 port->bulk_out_endpointAddress),
219 buffer, towrite,
Johan Hovold40f92f02010-03-17 23:06:06 +0100220 port->serial->type->write_bulk_callback, port);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500221
222 status = usb_submit_urb(urb, GFP_ATOMIC);
223 if (status) {
Johan Hovold1a1405e2010-03-17 23:06:01 +0100224 dev_err(&port->dev, "%s - error submitting urb: %d\n",
Jason Wessel715b1dc2009-05-11 15:24:07 -0500225 __func__, status);
226 goto error;
227 }
228
229 /* This urb is the responsibility of the host driver now */
230 usb_free_urb(urb);
231 dbg("%s write: %d", __func__, towrite);
232 count -= towrite;
233 bwrite += towrite;
234 }
235 return bwrite;
236
237error:
238 usb_free_urb(urb);
239error_no_urb:
240 kfree(buffer);
241error_no_buffer:
242 spin_lock_irqsave(&port->lock, flags);
243 port->urbs_in_flight--;
244 port->tx_bytes_flight -= towrite;
245 spin_unlock_irqrestore(&port->lock, flags);
246 return bwrite;
247}
248
David VomLehn8e8dce02009-08-28 12:54:27 -0700249/**
250 * usb_serial_generic_write_start - kick off an URB write
251 * @port: Pointer to the &struct usb_serial_port data
252 *
253 * Returns the number of bytes queued on success. This will be zero if there
254 * was nothing to send. Otherwise, it returns a negative errno value
255 */
256static int usb_serial_generic_write_start(struct usb_serial_port *port)
257{
David VomLehn8e8dce02009-08-28 12:54:27 -0700258 unsigned char *data;
259 int result;
260 int count;
261 unsigned long flags;
David VomLehn8e8dce02009-08-28 12:54:27 -0700262
David VomLehn8e8dce02009-08-28 12:54:27 -0700263 spin_lock_irqsave(&port->lock, flags);
Johan Hovold50a5f702010-03-17 23:06:02 +0100264 if (port->write_urb_busy || !kfifo_len(&port->write_fifo)) {
265 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700266 return 0;
Johan Hovold50a5f702010-03-17 23:06:02 +0100267 }
268 port->write_urb_busy = 1;
269 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700270
271 data = port->write_urb->transfer_buffer;
Stefani Seibold119eecc2009-12-23 09:10:48 +0100272 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700273 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
274
Johan Hovold056afc02010-03-17 23:05:54 +0100275 port->write_urb->transfer_buffer_length = count;
David VomLehn8e8dce02009-08-28 12:54:27 -0700276
277 /* send the data out the bulk port */
278 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
279 if (result) {
Johan Hovold1a1405e2010-03-17 23:06:01 +0100280 dev_err(&port->dev, "%s - error submitting urb: %d\n",
David VomLehn8e8dce02009-08-28 12:54:27 -0700281 __func__, result);
282 /* don't have to grab the lock here, as we will
283 retry if != 0 */
284 port->write_urb_busy = 0;
Johan Hovold30af7fb2010-03-17 23:00:42 +0100285 return result;
286 }
David VomLehn8e8dce02009-08-28 12:54:27 -0700287
Johan Hovold30af7fb2010-03-17 23:00:42 +0100288 spin_lock_irqsave(&port->lock, flags);
289 port->tx_bytes_flight += count;
290 spin_unlock_irqrestore(&port->lock, flags);
291
292 return count;
David VomLehn8e8dce02009-08-28 12:54:27 -0700293}
294
295/**
296 * usb_serial_generic_write - generic write function for serial USB devices
297 * @tty: Pointer to &struct tty_struct for the device
298 * @port: Pointer to the &usb_serial_port structure for the device
299 * @buf: Pointer to the data to write
300 * @count: Number of bytes to write
301 *
302 * Returns the number of characters actually written, which may be anything
303 * from zero to @count. If an error occurs, it returns the negative errno
304 * value.
305 */
Alan Cox95da3102008-07-22 11:09:07 +0100306int usb_serial_generic_write(struct tty_struct *tty,
307 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct usb_serial *serial = port->serial;
310 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Harvey Harrison441b62c2008-03-03 16:08:34 -0800312 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100314 /* only do something if we have a bulk out endpoint */
315 if (!port->bulk_out_size)
316 return -ENODEV;
317
Johan Hovold1a1405e2010-03-17 23:06:01 +0100318 if (!count)
Alan Coxae643872008-07-22 11:11:55 +0100319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
David VomLehn8e8dce02009-08-28 12:54:27 -0700321 if (serial->type->max_in_flight_urbs)
322 return usb_serial_multi_urb_write(tty, port,
323 buf, count);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500324
Stefani Seibold119eecc2009-12-23 09:10:48 +0100325 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700326 result = usb_serial_generic_write_start(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
David VomLehn8e8dce02009-08-28 12:54:27 -0700328 if (result >= 0)
329 result = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
David VomLehn8e8dce02009-08-28 12:54:27 -0700331 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500333EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Alan Coxae643872008-07-22 11:11:55 +0100335int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Alan Cox95da3102008-07-22 11:09:07 +0100337 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500339 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int room = 0;
341
Harvey Harrison441b62c2008-03-03 16:08:34 -0800342 dbg("%s - port %d", __func__, port->number);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100343
344 if (!port->bulk_out_size)
345 return 0;
346
Jason Wessel715b1dc2009-05-11 15:24:07 -0500347 spin_lock_irqsave(&port->lock, flags);
348 if (serial->type->max_in_flight_urbs) {
349 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500350 room = port->bulk_out_size *
351 (serial->type->max_in_flight_urbs -
352 port->urbs_in_flight);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100353 } else {
Stefani Seibold119eecc2009-12-23 09:10:48 +0100354 room = kfifo_avail(&port->write_fifo);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100355 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500356 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Harvey Harrison441b62c2008-03-03 16:08:34 -0800358 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100359 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Alan Cox95da3102008-07-22 11:09:07 +0100362int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Alan Cox95da3102008-07-22 11:09:07 +0100364 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500366 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100367 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Harvey Harrison441b62c2008-03-03 16:08:34 -0800369 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100371 if (!port->bulk_out_size)
372 return 0;
373
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100374 spin_lock_irqsave(&port->lock, flags);
375 if (serial->type->max_in_flight_urbs)
Jason Wessel715b1dc2009-05-11 15:24:07 -0500376 chars = port->tx_bytes_flight;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100377 else
Johan Hovold30af7fb2010-03-17 23:00:42 +0100378 chars = kfifo_len(&port->write_fifo) + port->tx_bytes_flight;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100379 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Harvey Harrison441b62c2008-03-03 16:08:34 -0800381 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100382 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Johan Hovold41bd72f2010-03-17 23:05:53 +0100385int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
386 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int result;
389
Johan Hovold056afc02010-03-17 23:05:54 +0100390 result = usb_submit_urb(port->read_urb, mem_flags);
Johan Hovold0ae14742010-02-27 14:05:46 +0100391 if (result && result != -EPERM) {
Johan Hovold1a1405e2010-03-17 23:06:01 +0100392 dev_err(&port->dev, "%s - error submitting urb: %d\n",
Alan Coxae643872008-07-22 11:11:55 +0100393 __func__, result);
Johan Hovold0ae14742010-02-27 14:05:46 +0100394 }
Johan Hovold41bd72f2010-03-17 23:05:53 +0100395 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Johan Hovold41bd72f2010-03-17 23:05:53 +0100397EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100398
Johan Hovold23154322010-03-17 23:05:57 +0100399void usb_serial_generic_process_read_urb(struct urb *urb)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200400{
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100401 struct usb_serial_port *port = urb->context;
402 struct tty_struct *tty;
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500403 char *ch = (char *)urb->transfer_buffer;
404 int i;
405
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100406 tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500407 if (!tty)
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100408 return;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200409
Alan Cox4cd1de02009-07-09 13:35:52 +0100410 /* The per character mucking around with sysrq path it too slow for
411 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
412 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600413 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de02009-07-09 13:35:52 +0100414 tty_insert_flip_string(tty, ch, urb->actual_length);
415 else {
Alan Cox4cd1de02009-07-09 13:35:52 +0100416 for (i = 0; i < urb->actual_length; i++, ch++) {
Alan Cox24a15a62009-07-09 13:36:22 +0100417 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
Alan Cox4cd1de02009-07-09 13:35:52 +0100418 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
419 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200420 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500421 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100422 tty_kref_put(tty);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200423}
Johan Hovold23154322010-03-17 23:05:57 +0100424EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200425
Alan Cox95da3102008-07-22 11:09:07 +0100426void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100427{
Ming Leicdc97792008-02-24 18:41:47 +0800428 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100429 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700430 int status = urb->status;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100431 unsigned long flags;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100432
Harvey Harrison441b62c2008-03-03 16:08:34 -0800433 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100434
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700435 if (unlikely(status != 0)) {
436 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800437 __func__, status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100438 return;
439 }
440
Alan Coxae643872008-07-22 11:11:55 +0100441 usb_serial_debug_data(debug, &port->dev, __func__,
442 urb->actual_length, data);
Johan Hovold23154322010-03-17 23:05:57 +0100443 port->serial->type->process_read_urb(urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100444
445 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100446 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100447 port->throttled = port->throttle_req;
448 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800449 spin_unlock_irqrestore(&port->lock, flags);
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100450 usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
Alan Coxae643872008-07-22 11:11:55 +0100451 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800452 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100453}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300454EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Alan Cox95da3102008-07-22 11:09:07 +0100456void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500458 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800459 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700460 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Harvey Harrison441b62c2008-03-03 16:08:34 -0800462 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Jason Wessel715b1dc2009-05-11 15:24:07 -0500464 if (port->serial->type->max_in_flight_urbs) {
Johan Hovold25915302010-01-06 15:48:42 -0800465 kfree(urb->transfer_buffer);
466
Jason Wessel715b1dc2009-05-11 15:24:07 -0500467 spin_lock_irqsave(&port->lock, flags);
468 --port->urbs_in_flight;
469 port->tx_bytes_flight -= urb->transfer_buffer_length;
470 if (port->urbs_in_flight < 0)
471 port->urbs_in_flight = 0;
472 spin_unlock_irqrestore(&port->lock, flags);
473 } else {
Johan Hovold30af7fb2010-03-17 23:00:42 +0100474 spin_lock_irqsave(&port->lock, flags);
475 port->tx_bytes_flight -= urb->transfer_buffer_length;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500476 port->write_urb_busy = 0;
Johan Hovold30af7fb2010-03-17 23:00:42 +0100477 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700478
Johan Hovold50dbb852010-03-17 23:00:43 +0100479 if (status) {
480 spin_lock_irqsave(&port->lock, flags);
Stefani Seibold119eecc2009-12-23 09:10:48 +0100481 kfifo_reset_out(&port->write_fifo);
Johan Hovold50dbb852010-03-17 23:00:43 +0100482 spin_unlock_irqrestore(&port->lock, flags);
483 } else {
David VomLehn8e8dce02009-08-28 12:54:27 -0700484 usb_serial_generic_write_start(port);
Johan Hovold50dbb852010-03-17 23:00:43 +0100485 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500486 }
487
Johan Hovold63136202010-02-27 14:06:07 +0100488 if (status)
489 dbg("%s - non-zero urb status: %d", __func__, status);
490
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700491 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800493EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Alan Cox95da3102008-07-22 11:09:07 +0100495void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100496{
Alan Cox95da3102008-07-22 11:09:07 +0100497 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100498 unsigned long flags;
499
Harvey Harrison441b62c2008-03-03 16:08:34 -0800500 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100501
502 /* Set the throttle request flag. It will be picked up
503 * by usb_serial_generic_read_bulk_callback(). */
504 spin_lock_irqsave(&port->lock, flags);
505 port->throttle_req = 1;
506 spin_unlock_irqrestore(&port->lock, flags);
507}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100508EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100509
Alan Cox95da3102008-07-22 11:09:07 +0100510void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100511{
Alan Cox95da3102008-07-22 11:09:07 +0100512 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100513 int was_throttled;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100514
Harvey Harrison441b62c2008-03-03 16:08:34 -0800515 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100516
517 /* Clear the throttle flags */
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100518 spin_lock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100519 was_throttled = port->throttled;
520 port->throttled = port->throttle_req = 0;
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100521 spin_unlock_irq(&port->lock);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100522
Johan Hovold0f3d5ba2010-03-17 23:05:56 +0100523 if (was_throttled)
524 usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100525}
Johan Hovoldf1e949a2010-03-17 23:05:59 +0100526EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100527
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700528#ifdef CONFIG_MAGIC_SYSRQ
Alan Cox24a15a62009-07-09 13:36:22 +0100529int usb_serial_handle_sysrq_char(struct tty_struct *tty,
530 struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500531{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600532 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500533 if (ch && time_before(jiffies, port->sysrq)) {
Alan Cox24a15a62009-07-09 13:36:22 +0100534 handle_sysrq(ch, tty);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500535 port->sysrq = 0;
536 return 1;
537 }
538 port->sysrq = 0;
539 }
540 return 0;
541}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700542#else
543int usb_serial_handle_sysrq_char(struct tty_struct *tty,
544 struct usb_serial_port *port, unsigned int ch)
545{
546 return 0;
547}
548#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500549EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
550
551int usb_serial_handle_break(struct usb_serial_port *port)
552{
553 if (!port->sysrq) {
554 port->sysrq = jiffies + HZ*5;
555 return 1;
556 }
557 port->sysrq = 0;
558 return 0;
559}
560EXPORT_SYMBOL_GPL(usb_serial_handle_break);
561
David VomLehn8e8dce02009-08-28 12:54:27 -0700562int usb_serial_generic_resume(struct usb_serial *serial)
563{
564 struct usb_serial_port *port;
565 int i, c = 0, r;
566
567 for (i = 0; i < serial->num_ports; i++) {
568 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500569 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700570 continue;
571
572 if (port->read_urb) {
573 r = usb_submit_urb(port->read_urb, GFP_NOIO);
574 if (r < 0)
575 c++;
576 }
577
578 if (port->write_urb) {
579 r = usb_serial_generic_write_start(port);
580 if (r < 0)
581 c++;
582 }
583 }
584
585 return c ? -EIO : 0;
586}
587EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
588
Alan Sternf9c99bb2009-06-02 11:53:55 -0400589void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 int i;
592
Harvey Harrison441b62c2008-03-03 16:08:34 -0800593 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100596 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Alan Sternf9c99bb2009-06-02 11:53:55 -0400600void usb_serial_generic_release(struct usb_serial *serial)
601{
602 dbg("%s", __func__);
603}