blob: 583244bd84b19d8a1b0aeb02c7c91e8991e37e1b [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{
121 struct usb_serial *serial = port->serial;
122 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100123 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Harvey Harrison441b62c2008-03-03 16:08:34 -0800125 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100127 /* clear the throttle flags */
128 spin_lock_irqsave(&port->lock, flags);
129 port->throttled = 0;
130 port->throttle_req = 0;
131 spin_unlock_irqrestore(&port->lock, flags);
132
133 /* if we have a bulk endpoint, start reading from it */
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100134 if (port->bulk_in_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* Start reading from the device */
Alan Coxae643872008-07-22 11:11:55 +0100136 usb_fill_bulk_urb(port->read_urb, serial->dev,
137 usb_rcvbulkpipe(serial->dev,
138 port->bulk_in_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 port->read_urb->transfer_buffer,
140 port->read_urb->transfer_buffer_length,
141 ((serial->type->read_bulk_callback) ?
142 serial->type->read_bulk_callback :
143 usb_serial_generic_read_bulk_callback),
144 port);
145 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
146 if (result)
Alan Coxae643872008-07-22 11:11:55 +0100147 dev_err(&port->dev,
148 "%s - failed resubmitting read urb, error %d\n",
149 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
152 return result;
153}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700154EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Alan Cox95da3102008-07-22 11:09:07 +0100156static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct usb_serial *serial = port->serial;
Johan Hovoldec3ee502010-03-17 23:00:44 +0100159 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Harvey Harrison441b62c2008-03-03 16:08:34 -0800161 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (serial->dev) {
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100164 /* shutdown any bulk transfers that might be going on */
Johan Hovoldec3ee502010-03-17 23:00:44 +0100165 if (port->bulk_out_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 usb_kill_urb(port->write_urb);
Johan Hovoldec3ee502010-03-17 23:00:44 +0100167
168 spin_lock_irqsave(&port->lock, flags);
169 kfifo_reset_out(&port->write_fifo);
170 spin_unlock_irqrestore(&port->lock, flags);
171 }
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100172 if (port->bulk_in_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 usb_kill_urb(port->read_urb);
174 }
175}
176
Alan Cox335f8512009-06-11 12:26:29 +0100177void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800179 dbg("%s - port %d", __func__, port->number);
Alan Coxae643872008-07-22 11:11:55 +0100180 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Jason Wessel715b1dc2009-05-11 15:24:07 -0500183static int usb_serial_multi_urb_write(struct tty_struct *tty,
184 struct usb_serial_port *port, const unsigned char *buf, int count)
185{
186 unsigned long flags;
187 struct urb *urb;
188 unsigned char *buffer;
189 int status;
190 int towrite;
191 int bwrite = 0;
192
193 dbg("%s - port %d", __func__, port->number);
194
195 if (count == 0)
196 dbg("%s - write request of 0 bytes", __func__);
197
198 while (count > 0) {
199 towrite = (count > port->bulk_out_size) ?
200 port->bulk_out_size : count;
201 spin_lock_irqsave(&port->lock, flags);
202 if (port->urbs_in_flight >
203 port->serial->type->max_in_flight_urbs) {
204 spin_unlock_irqrestore(&port->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800205 dbg("%s - write limit hit", __func__);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500206 return bwrite;
207 }
208 port->tx_bytes_flight += towrite;
209 port->urbs_in_flight++;
210 spin_unlock_irqrestore(&port->lock, flags);
211
212 buffer = kmalloc(towrite, GFP_ATOMIC);
213 if (!buffer) {
214 dev_err(&port->dev,
215 "%s ran out of kernel memory for urb ...\n", __func__);
216 goto error_no_buffer;
217 }
218
219 urb = usb_alloc_urb(0, GFP_ATOMIC);
220 if (!urb) {
221 dev_err(&port->dev, "%s - no more free urbs\n",
222 __func__);
223 goto error_no_urb;
224 }
225
226 /* Copy data */
227 memcpy(buffer, buf + bwrite, towrite);
228 usb_serial_debug_data(debug, &port->dev, __func__,
229 towrite, buffer);
230 /* fill the buffer and send it */
231 usb_fill_bulk_urb(urb, port->serial->dev,
232 usb_sndbulkpipe(port->serial->dev,
233 port->bulk_out_endpointAddress),
234 buffer, towrite,
235 usb_serial_generic_write_bulk_callback, port);
236
237 status = usb_submit_urb(urb, GFP_ATOMIC);
238 if (status) {
239 dev_err(&port->dev,
240 "%s - failed submitting write urb, error %d\n",
241 __func__, status);
242 goto error;
243 }
244
245 /* This urb is the responsibility of the host driver now */
246 usb_free_urb(urb);
247 dbg("%s write: %d", __func__, towrite);
248 count -= towrite;
249 bwrite += towrite;
250 }
251 return bwrite;
252
253error:
254 usb_free_urb(urb);
255error_no_urb:
256 kfree(buffer);
257error_no_buffer:
258 spin_lock_irqsave(&port->lock, flags);
259 port->urbs_in_flight--;
260 port->tx_bytes_flight -= towrite;
261 spin_unlock_irqrestore(&port->lock, flags);
262 return bwrite;
263}
264
David VomLehn8e8dce02009-08-28 12:54:27 -0700265/**
266 * usb_serial_generic_write_start - kick off an URB write
267 * @port: Pointer to the &struct usb_serial_port data
268 *
269 * Returns the number of bytes queued on success. This will be zero if there
270 * was nothing to send. Otherwise, it returns a negative errno value
271 */
272static int usb_serial_generic_write_start(struct usb_serial_port *port)
273{
274 struct usb_serial *serial = port->serial;
275 unsigned char *data;
276 int result;
277 int count;
278 unsigned long flags;
279 bool start_io;
280
281 /* Atomically determine whether we can and need to start a USB
282 * operation. */
283 spin_lock_irqsave(&port->lock, flags);
284 if (port->write_urb_busy)
285 start_io = false;
286 else {
Stefani Seibold119eecc2009-12-23 09:10:48 +0100287 start_io = (kfifo_len(&port->write_fifo) != 0);
David VomLehn8e8dce02009-08-28 12:54:27 -0700288 port->write_urb_busy = start_io;
289 }
290 spin_unlock_irqrestore(&port->lock, flags);
291
292 if (!start_io)
293 return 0;
294
295 data = port->write_urb->transfer_buffer;
Stefani Seibold119eecc2009-12-23 09:10:48 +0100296 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700297 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
298
299 /* set up our urb */
300 usb_fill_bulk_urb(port->write_urb, serial->dev,
301 usb_sndbulkpipe(serial->dev,
302 port->bulk_out_endpointAddress),
303 port->write_urb->transfer_buffer, count,
304 ((serial->type->write_bulk_callback) ?
305 serial->type->write_bulk_callback :
306 usb_serial_generic_write_bulk_callback),
307 port);
308
309 /* send the data out the bulk port */
310 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
311 if (result) {
312 dev_err(&port->dev,
313 "%s - failed submitting write urb, error %d\n",
314 __func__, result);
315 /* don't have to grab the lock here, as we will
316 retry if != 0 */
317 port->write_urb_busy = 0;
Johan Hovold30af7fb2010-03-17 23:00:42 +0100318 return result;
319 }
David VomLehn8e8dce02009-08-28 12:54:27 -0700320
Johan Hovold30af7fb2010-03-17 23:00:42 +0100321 spin_lock_irqsave(&port->lock, flags);
322 port->tx_bytes_flight += count;
323 spin_unlock_irqrestore(&port->lock, flags);
324
325 return count;
David VomLehn8e8dce02009-08-28 12:54:27 -0700326}
327
328/**
329 * usb_serial_generic_write - generic write function for serial USB devices
330 * @tty: Pointer to &struct tty_struct for the device
331 * @port: Pointer to the &usb_serial_port structure for the device
332 * @buf: Pointer to the data to write
333 * @count: Number of bytes to write
334 *
335 * Returns the number of characters actually written, which may be anything
336 * from zero to @count. If an error occurs, it returns the negative errno
337 * value.
338 */
Alan Cox95da3102008-07-22 11:09:07 +0100339int usb_serial_generic_write(struct tty_struct *tty,
340 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct usb_serial *serial = port->serial;
343 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Harvey Harrison441b62c2008-03-03 16:08:34 -0800345 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100347 /* only do something if we have a bulk out endpoint */
348 if (!port->bulk_out_size)
349 return -ENODEV;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800352 dbg("%s - write request of 0 bytes", __func__);
Alan Coxae643872008-07-22 11:11:55 +0100353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
David VomLehn8e8dce02009-08-28 12:54:27 -0700356 if (serial->type->max_in_flight_urbs)
357 return usb_serial_multi_urb_write(tty, port,
358 buf, count);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500359
Stefani Seibold119eecc2009-12-23 09:10:48 +0100360 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
David VomLehn8e8dce02009-08-28 12:54:27 -0700361 result = usb_serial_generic_write_start(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David VomLehn8e8dce02009-08-28 12:54:27 -0700363 if (result >= 0)
364 result = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
David VomLehn8e8dce02009-08-28 12:54:27 -0700366 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500368EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Alan Coxae643872008-07-22 11:11:55 +0100370int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Alan Cox95da3102008-07-22 11:09:07 +0100372 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500374 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int room = 0;
376
Harvey Harrison441b62c2008-03-03 16:08:34 -0800377 dbg("%s - port %d", __func__, port->number);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100378
379 if (!port->bulk_out_size)
380 return 0;
381
Jason Wessel715b1dc2009-05-11 15:24:07 -0500382 spin_lock_irqsave(&port->lock, flags);
383 if (serial->type->max_in_flight_urbs) {
384 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500385 room = port->bulk_out_size *
386 (serial->type->max_in_flight_urbs -
387 port->urbs_in_flight);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100388 } else {
Stefani Seibold119eecc2009-12-23 09:10:48 +0100389 room = kfifo_avail(&port->write_fifo);
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100390 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500391 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Harvey Harrison441b62c2008-03-03 16:08:34 -0800393 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100394 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Alan Cox95da3102008-07-22 11:09:07 +0100397int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Alan Cox95da3102008-07-22 11:09:07 +0100399 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500401 unsigned long flags;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100402 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Harvey Harrison441b62c2008-03-03 16:08:34 -0800404 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100406 if (!port->bulk_out_size)
407 return 0;
408
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100409 spin_lock_irqsave(&port->lock, flags);
410 if (serial->type->max_in_flight_urbs)
Jason Wessel715b1dc2009-05-11 15:24:07 -0500411 chars = port->tx_bytes_flight;
Johan Hovoldeb8878a2010-02-27 16:24:49 +0100412 else
Johan Hovold30af7fb2010-03-17 23:00:42 +0100413 chars = kfifo_len(&port->write_fifo) + port->tx_bytes_flight;
Stefani Seibold25719e6b2010-01-05 14:30:31 +0100414 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Harvey Harrison441b62c2008-03-03 16:08:34 -0800416 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100417 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200420
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500421void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
422 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100424 struct urb *urb = port->read_urb;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200425 struct usb_serial *serial = port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int result;
427
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100428 /* Continue reading from device */
Alan Coxae643872008-07-22 11:11:55 +0100429 usb_fill_bulk_urb(urb, serial->dev,
430 usb_rcvbulkpipe(serial->dev,
431 port->bulk_in_endpointAddress),
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200432 urb->transfer_buffer,
433 urb->transfer_buffer_length,
Alan Coxae643872008-07-22 11:11:55 +0100434 ((serial->type->read_bulk_callback) ?
435 serial->type->read_bulk_callback :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 usb_serial_generic_read_bulk_callback), port);
Johan Hovold0ae14742010-02-27 14:05:46 +0100437
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200438 result = usb_submit_urb(urb, mem_flags);
Johan Hovold0ae14742010-02-27 14:05:46 +0100439 if (result && result != -EPERM) {
Alan Coxae643872008-07-22 11:11:55 +0100440 dev_err(&port->dev,
441 "%s - failed resubmitting read urb, error %d\n",
442 __func__, result);
Johan Hovold0ae14742010-02-27 14:05:46 +0100443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500445EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100446
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200447/* Push data to tty layer and resubmit the bulk read URB */
Alan Cox95da3102008-07-22 11:09:07 +0100448static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200449{
450 struct urb *urb = port->read_urb;
Alan Cox4a90f092008-10-13 10:39:46 +0100451 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500452 char *ch = (char *)urb->transfer_buffer;
453 int i;
454
455 if (!tty)
456 goto done;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200457
Alan Cox4cd1de02009-07-09 13:35:52 +0100458 /* The per character mucking around with sysrq path it too slow for
459 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
460 where the USB serial is not a console anyway */
Jason Wesselbd5afa92010-03-08 21:50:12 -0600461 if (!port->port.console || !port->sysrq)
Alan Cox4cd1de02009-07-09 13:35:52 +0100462 tty_insert_flip_string(tty, ch, urb->actual_length);
463 else {
464 /* Push data to tty */
465 for (i = 0; i < urb->actual_length; i++, ch++) {
Alan Cox24a15a62009-07-09 13:36:22 +0100466 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
Alan Cox4cd1de02009-07-09 13:35:52 +0100467 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
468 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200469 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500470 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100471 tty_kref_put(tty);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500472done:
473 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200474}
475
Alan Cox95da3102008-07-22 11:09:07 +0100476void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100477{
Ming Leicdc97792008-02-24 18:41:47 +0800478 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100479 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700480 int status = urb->status;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100481 unsigned long flags;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100482
Harvey Harrison441b62c2008-03-03 16:08:34 -0800483 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100484
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700485 if (unlikely(status != 0)) {
486 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800487 __func__, status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100488 return;
489 }
490
Alan Coxae643872008-07-22 11:11:55 +0100491 usb_serial_debug_data(debug, &port->dev, __func__,
492 urb->actual_length, data);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100493
494 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100495 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100496 port->throttled = port->throttle_req;
497 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800498 spin_unlock_irqrestore(&port->lock, flags);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200499 flush_and_resubmit_read_urb(port);
Alan Coxae643872008-07-22 11:11:55 +0100500 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800501 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100502}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300503EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Alan Cox95da3102008-07-22 11:09:07 +0100505void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500507 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800508 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700509 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Harvey Harrison441b62c2008-03-03 16:08:34 -0800511 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Jason Wessel715b1dc2009-05-11 15:24:07 -0500513 if (port->serial->type->max_in_flight_urbs) {
Johan Hovold25915302010-01-06 15:48:42 -0800514 kfree(urb->transfer_buffer);
515
Jason Wessel715b1dc2009-05-11 15:24:07 -0500516 spin_lock_irqsave(&port->lock, flags);
517 --port->urbs_in_flight;
518 port->tx_bytes_flight -= urb->transfer_buffer_length;
519 if (port->urbs_in_flight < 0)
520 port->urbs_in_flight = 0;
521 spin_unlock_irqrestore(&port->lock, flags);
522 } else {
Johan Hovold30af7fb2010-03-17 23:00:42 +0100523 spin_lock_irqsave(&port->lock, flags);
524 port->tx_bytes_flight -= urb->transfer_buffer_length;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500525 port->write_urb_busy = 0;
Johan Hovold30af7fb2010-03-17 23:00:42 +0100526 spin_unlock_irqrestore(&port->lock, flags);
David VomLehn8e8dce02009-08-28 12:54:27 -0700527
Johan Hovold50dbb852010-03-17 23:00:43 +0100528 if (status) {
529 spin_lock_irqsave(&port->lock, flags);
Stefani Seibold119eecc2009-12-23 09:10:48 +0100530 kfifo_reset_out(&port->write_fifo);
Johan Hovold50dbb852010-03-17 23:00:43 +0100531 spin_unlock_irqrestore(&port->lock, flags);
532 } else {
David VomLehn8e8dce02009-08-28 12:54:27 -0700533 usb_serial_generic_write_start(port);
Johan Hovold50dbb852010-03-17 23:00:43 +0100534 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500535 }
536
Johan Hovold63136202010-02-27 14:06:07 +0100537 if (status)
538 dbg("%s - non-zero urb status: %d", __func__, status);
539
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700540 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800542EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Alan Cox95da3102008-07-22 11:09:07 +0100544void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100545{
Alan Cox95da3102008-07-22 11:09:07 +0100546 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100547 unsigned long flags;
548
Harvey Harrison441b62c2008-03-03 16:08:34 -0800549 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100550
551 /* Set the throttle request flag. It will be picked up
552 * by usb_serial_generic_read_bulk_callback(). */
553 spin_lock_irqsave(&port->lock, flags);
554 port->throttle_req = 1;
555 spin_unlock_irqrestore(&port->lock, flags);
556}
557
Alan Cox95da3102008-07-22 11:09:07 +0100558void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100559{
Alan Cox95da3102008-07-22 11:09:07 +0100560 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100561 int was_throttled;
562 unsigned long flags;
563
Harvey Harrison441b62c2008-03-03 16:08:34 -0800564 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100565
566 /* Clear the throttle flags */
567 spin_lock_irqsave(&port->lock, flags);
568 was_throttled = port->throttled;
569 port->throttled = port->throttle_req = 0;
570 spin_unlock_irqrestore(&port->lock, flags);
571
572 if (was_throttled) {
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200573 /* Resume reading from device */
Joris van Rantwijk63a96092009-09-24 20:20:20 +0200574 flush_and_resubmit_read_urb(port);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100575 }
576}
577
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700578#ifdef CONFIG_MAGIC_SYSRQ
Alan Cox24a15a62009-07-09 13:36:22 +0100579int usb_serial_handle_sysrq_char(struct tty_struct *tty,
580 struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500581{
Jason Wesselbd5afa92010-03-08 21:50:12 -0600582 if (port->sysrq && port->port.console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500583 if (ch && time_before(jiffies, port->sysrq)) {
Alan Cox24a15a62009-07-09 13:36:22 +0100584 handle_sysrq(ch, tty);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500585 port->sysrq = 0;
586 return 1;
587 }
588 port->sysrq = 0;
589 }
590 return 0;
591}
Randy Dunlap7f0ae3a2010-03-25 11:29:16 -0700592#else
593int usb_serial_handle_sysrq_char(struct tty_struct *tty,
594 struct usb_serial_port *port, unsigned int ch)
595{
596 return 0;
597}
598#endif
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500599EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
600
601int usb_serial_handle_break(struct usb_serial_port *port)
602{
603 if (!port->sysrq) {
604 port->sysrq = jiffies + HZ*5;
605 return 1;
606 }
607 port->sysrq = 0;
608 return 0;
609}
610EXPORT_SYMBOL_GPL(usb_serial_handle_break);
611
David VomLehn8e8dce02009-08-28 12:54:27 -0700612int usb_serial_generic_resume(struct usb_serial *serial)
613{
614 struct usb_serial_port *port;
615 int i, c = 0, r;
616
617 for (i = 0; i < serial->num_ports; i++) {
618 port = serial->port[i];
Alan Stern1f871582010-02-17 10:05:47 -0500619 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
David VomLehn8e8dce02009-08-28 12:54:27 -0700620 continue;
621
622 if (port->read_urb) {
623 r = usb_submit_urb(port->read_urb, GFP_NOIO);
624 if (r < 0)
625 c++;
626 }
627
628 if (port->write_urb) {
629 r = usb_serial_generic_write_start(port);
630 if (r < 0)
631 c++;
632 }
633 }
634
635 return c ? -EIO : 0;
636}
637EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
638
Alan Sternf9c99bb2009-06-02 11:53:55 -0400639void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 int i;
642
Harvey Harrison441b62c2008-03-03 16:08:34 -0800643 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100646 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Alan Sternf9c99bb2009-06-02 11:53:55 -0400650void usb_serial_generic_release(struct usb_serial *serial)
651{
652 dbg("%s", __func__);
653}