blob: ce57f6a32bdfb4cc7a5e30f78c1e8eca21a9364a [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>
15#include <linux/tty.h>
16#include <linux/tty_flip.h>
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070020#include <linux/usb/serial.h>
Alan Coxae643872008-07-22 11:11:55 +010021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Johannes Hölzld9b1b782006-12-17 21:50:24 +010023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static int debug;
25
26#ifdef CONFIG_USB_SERIAL_GENERIC
David Brownellb46d60f2007-03-23 12:51:55 -070027
28static int generic_probe(struct usb_interface *interface,
29 const struct usb_device_id *id);
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static __u16 vendor = 0x05f9;
32static __u16 product = 0xffff;
33
34module_param(vendor, ushort, 0);
35MODULE_PARM_DESC(vendor, "User specified USB idVendor");
36
37module_param(product, ushort, 0);
38MODULE_PARM_DESC(product, "User specified USB idProduct");
39
40static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
41
Johannes Hölzld9b1b782006-12-17 21:50:24 +010042/* we want to look at all devices, as the vendor/product id can change
43 * depending on the command line argument */
44static struct usb_device_id generic_serial_ids[] = {
45 {.driver_info = 42},
46 {}
47};
48
49static struct usb_driver generic_driver = {
50 .name = "usbserial_generic",
51 .probe = generic_probe,
52 .disconnect = usb_serial_disconnect,
53 .id_table = generic_serial_ids,
54 .no_dynamic_id = 1,
55};
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* All of the device info needed for the Generic Serial Converter */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070058struct usb_serial_driver usb_serial_generic_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070059 .driver = {
60 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070061 .name = "generic",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070062 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .id_table = generic_device_ids,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010064 .usb_driver = &generic_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040066 .disconnect = usb_serial_generic_disconnect,
67 .release = usb_serial_generic_release,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010068 .throttle = usb_serial_generic_throttle,
69 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020070 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int generic_probe(struct usb_interface *interface,
74 const struct usb_device_id *id)
75{
76 const struct usb_device_id *id_pattern;
77
78 id_pattern = usb_match_id(interface, generic_device_ids);
79 if (id_pattern != NULL)
80 return usb_serial_probe(interface, id);
81 return -ENODEV;
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
Alan Coxae643872008-07-22 11:11:55 +010085int usb_serial_generic_register(int _debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 int retval = 0;
88
89 debug = _debug;
90#ifdef CONFIG_USB_SERIAL_GENERIC
91 generic_device_ids[0].idVendor = vendor;
92 generic_device_ids[0].idProduct = product;
Alan Coxae643872008-07-22 11:11:55 +010093 generic_device_ids[0].match_flags =
94 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /* register our generic driver with ourselves */
Alan Coxae643872008-07-22 11:11:55 +010097 retval = usb_serial_register(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (retval)
99 goto exit;
100 retval = usb_register(&generic_driver);
101 if (retval)
102 usb_serial_deregister(&usb_serial_generic_device);
103exit:
104#endif
105 return retval;
106}
107
Alan Coxae643872008-07-22 11:11:55 +0100108void usb_serial_generic_deregister(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110#ifdef CONFIG_USB_SERIAL_GENERIC
111 /* remove our generic driver */
112 usb_deregister(&generic_driver);
Alan Coxae643872008-07-22 11:11:55 +0100113 usb_serial_deregister(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115}
116
Alan Cox95da3102008-07-22 11:09:07 +0100117int usb_serial_generic_open(struct tty_struct *tty,
118 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 struct usb_serial *serial = port->serial;
121 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (serial->num_bulk_in) {
134 /* Start reading from the device */
Alan Coxae643872008-07-22 11:11:55 +0100135 usb_fill_bulk_urb(port->read_urb, serial->dev,
136 usb_rcvbulkpipe(serial->dev,
137 port->bulk_in_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 port->read_urb->transfer_buffer,
139 port->read_urb->transfer_buffer_length,
140 ((serial->type->read_bulk_callback) ?
141 serial->type->read_bulk_callback :
142 usb_serial_generic_read_bulk_callback),
143 port);
144 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
145 if (result)
Alan Coxae643872008-07-22 11:11:55 +0100146 dev_err(&port->dev,
147 "%s - failed resubmitting read urb, error %d\n",
148 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
151 return result;
152}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700153EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Alan Cox95da3102008-07-22 11:09:07 +0100155static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct usb_serial *serial = port->serial;
158
Harvey Harrison441b62c2008-03-03 16:08:34 -0800159 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (serial->dev) {
162 /* shutdown any bulk reads that might be going on */
163 if (serial->num_bulk_out)
164 usb_kill_urb(port->write_urb);
165 if (serial->num_bulk_in)
166 usb_kill_urb(port->read_urb);
167 }
168}
169
Oliver Neukumec225592007-04-27 20:54:57 +0200170int usb_serial_generic_resume(struct usb_serial *serial)
171{
172 struct usb_serial_port *port;
173 int i, c = 0, r;
174
175 for (i = 0; i < serial->num_ports; i++) {
176 port = serial->port[i];
Alan Cox95da3102008-07-22 11:09:07 +0100177 if (port->port.count && port->read_urb) {
Oliver Neukumec225592007-04-27 20:54:57 +0200178 r = usb_submit_urb(port->read_urb, GFP_NOIO);
179 if (r < 0)
180 c++;
181 }
182 }
183
184 return c ? -EIO : 0;
185}
Oliver Neukum81d043c2009-02-06 15:37:14 +0100186EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
Oliver Neukumec225592007-04-27 20:54:57 +0200187
Alan Cox335f8512009-06-11 12:26:29 +0100188void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800190 dbg("%s - port %d", __func__, port->number);
Alan Coxae643872008-07-22 11:11:55 +0100191 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Jason Wessel715b1dc2009-05-11 15:24:07 -0500194static int usb_serial_multi_urb_write(struct tty_struct *tty,
195 struct usb_serial_port *port, const unsigned char *buf, int count)
196{
197 unsigned long flags;
198 struct urb *urb;
199 unsigned char *buffer;
200 int status;
201 int towrite;
202 int bwrite = 0;
203
204 dbg("%s - port %d", __func__, port->number);
205
206 if (count == 0)
207 dbg("%s - write request of 0 bytes", __func__);
208
209 while (count > 0) {
210 towrite = (count > port->bulk_out_size) ?
211 port->bulk_out_size : count;
212 spin_lock_irqsave(&port->lock, flags);
213 if (port->urbs_in_flight >
214 port->serial->type->max_in_flight_urbs) {
215 spin_unlock_irqrestore(&port->lock, flags);
216 dbg("%s - write limit hit\n", __func__);
217 return bwrite;
218 }
219 port->tx_bytes_flight += towrite;
220 port->urbs_in_flight++;
221 spin_unlock_irqrestore(&port->lock, flags);
222
223 buffer = kmalloc(towrite, GFP_ATOMIC);
224 if (!buffer) {
225 dev_err(&port->dev,
226 "%s ran out of kernel memory for urb ...\n", __func__);
227 goto error_no_buffer;
228 }
229
230 urb = usb_alloc_urb(0, GFP_ATOMIC);
231 if (!urb) {
232 dev_err(&port->dev, "%s - no more free urbs\n",
233 __func__);
234 goto error_no_urb;
235 }
236
237 /* Copy data */
238 memcpy(buffer, buf + bwrite, towrite);
239 usb_serial_debug_data(debug, &port->dev, __func__,
240 towrite, buffer);
241 /* fill the buffer and send it */
242 usb_fill_bulk_urb(urb, port->serial->dev,
243 usb_sndbulkpipe(port->serial->dev,
244 port->bulk_out_endpointAddress),
245 buffer, towrite,
246 usb_serial_generic_write_bulk_callback, port);
247
248 status = usb_submit_urb(urb, GFP_ATOMIC);
249 if (status) {
250 dev_err(&port->dev,
251 "%s - failed submitting write urb, error %d\n",
252 __func__, status);
253 goto error;
254 }
255
256 /* This urb is the responsibility of the host driver now */
257 usb_free_urb(urb);
258 dbg("%s write: %d", __func__, towrite);
259 count -= towrite;
260 bwrite += towrite;
261 }
262 return bwrite;
263
264error:
265 usb_free_urb(urb);
266error_no_urb:
267 kfree(buffer);
268error_no_buffer:
269 spin_lock_irqsave(&port->lock, flags);
270 port->urbs_in_flight--;
271 port->tx_bytes_flight -= towrite;
272 spin_unlock_irqrestore(&port->lock, flags);
273 return bwrite;
274}
275
Alan Cox95da3102008-07-22 11:09:07 +0100276int usb_serial_generic_write(struct tty_struct *tty,
277 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct usb_serial *serial = port->serial;
280 int result;
281 unsigned char *data;
282
Harvey Harrison441b62c2008-03-03 16:08:34 -0800283 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800286 dbg("%s - write request of 0 bytes", __func__);
Alan Coxae643872008-07-22 11:11:55 +0100287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 /* only do something if we have a bulk out endpoint */
291 if (serial->num_bulk_out) {
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200292 unsigned long flags;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500293
294 if (serial->type->max_in_flight_urbs)
295 return usb_serial_multi_urb_write(tty, port,
296 buf, count);
297
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200298 spin_lock_irqsave(&port->lock, flags);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700299 if (port->write_urb_busy) {
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200300 spin_unlock_irqrestore(&port->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800301 dbg("%s - already writing", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700304 port->write_urb_busy = 1;
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200305 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Alan Coxae643872008-07-22 11:11:55 +0100307 count = (count > port->bulk_out_size) ?
308 port->bulk_out_size : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Alan Coxae643872008-07-22 11:11:55 +0100310 memcpy(port->write_urb->transfer_buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 data = port->write_urb->transfer_buffer;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800312 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* set up our urb */
Alan Coxae643872008-07-22 11:11:55 +0100315 usb_fill_bulk_urb(port->write_urb, serial->dev,
316 usb_sndbulkpipe(serial->dev,
317 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 port->write_urb->transfer_buffer, count,
Alan Coxae643872008-07-22 11:11:55 +0100319 ((serial->type->write_bulk_callback) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 serial->type->write_bulk_callback :
Alan Coxae643872008-07-22 11:11:55 +0100321 usb_serial_generic_write_bulk_callback),
322 port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700325 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700327 if (result) {
Alan Coxae643872008-07-22 11:11:55 +0100328 dev_err(&port->dev,
329 "%s - failed submitting write urb, error %d\n",
330 __func__, result);
331 /* don't have to grab the lock here, as we will
332 retry if != 0 */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700333 port->write_urb_busy = 0;
334 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 result = count;
336
337 return result;
338 }
339
340 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500343EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Alan Coxae643872008-07-22 11:11:55 +0100345int usb_serial_generic_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Alan Cox95da3102008-07-22 11:09:07 +0100347 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct usb_serial *serial = port->serial;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500349 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int room = 0;
351
Harvey Harrison441b62c2008-03-03 16:08:34 -0800352 dbg("%s - port %d", __func__, port->number);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500353 spin_lock_irqsave(&port->lock, flags);
354 if (serial->type->max_in_flight_urbs) {
355 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500356 room = port->bulk_out_size *
357 (serial->type->max_in_flight_urbs -
358 port->urbs_in_flight);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500359 } else if (serial->num_bulk_out && !(port->write_urb_busy)) {
360 room = port->bulk_out_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500362 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Harvey Harrison441b62c2008-03-03 16:08:34 -0800364 dbg("%s - returns %d", __func__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100365 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Alan Cox95da3102008-07-22 11:09:07 +0100368int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Alan Cox95da3102008-07-22 11:09:07 +0100370 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct usb_serial *serial = port->serial;
372 int chars = 0;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500373 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Harvey Harrison441b62c2008-03-03 16:08:34 -0800375 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jason Wessel715b1dc2009-05-11 15:24:07 -0500377 if (serial->type->max_in_flight_urbs) {
378 spin_lock_irqsave(&port->lock, flags);
379 chars = port->tx_bytes_flight;
380 spin_unlock_irqrestore(&port->lock, flags);
381 } else if (serial->num_bulk_out) {
382 /* FIXME: Locking */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700383 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 chars = port->write_urb->transfer_buffer_length;
385 }
386
Harvey Harrison441b62c2008-03-03 16:08:34 -0800387 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100388 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200391
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500392void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
393 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100395 struct urb *urb = port->read_urb;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200396 struct usb_serial *serial = port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int result;
398
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100399 /* Continue reading from device */
Alan Coxae643872008-07-22 11:11:55 +0100400 usb_fill_bulk_urb(urb, serial->dev,
401 usb_rcvbulkpipe(serial->dev,
402 port->bulk_in_endpointAddress),
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200403 urb->transfer_buffer,
404 urb->transfer_buffer_length,
Alan Coxae643872008-07-22 11:11:55 +0100405 ((serial->type->read_bulk_callback) ?
406 serial->type->read_bulk_callback :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 usb_serial_generic_read_bulk_callback), port);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200408 result = usb_submit_urb(urb, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (result)
Alan Coxae643872008-07-22 11:11:55 +0100410 dev_err(&port->dev,
411 "%s - failed resubmitting read urb, error %d\n",
412 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500414EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100415
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200416/* Push data to tty layer and resubmit the bulk read URB */
Alan Cox95da3102008-07-22 11:09:07 +0100417static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200418{
419 struct urb *urb = port->read_urb;
Alan Cox4a90f092008-10-13 10:39:46 +0100420 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500421 char *ch = (char *)urb->transfer_buffer;
422 int i;
423
424 if (!tty)
425 goto done;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200426
Alan Cox4cd1de0a2009-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 */
430 if (!port->console || !port->sysrq)
431 tty_insert_flip_string(tty, ch, urb->actual_length);
432 else {
433 /* Push data to tty */
434 for (i = 0; i < urb->actual_length; i++, ch++) {
Alan Cox24a15a62009-07-09 13:36:22 +0100435 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
Alan Cox4cd1de0a2009-07-09 13:35:52 +0100436 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
437 }
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200438 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500439 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100440 tty_kref_put(tty);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500441done:
442 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200443}
444
Alan Cox95da3102008-07-22 11:09:07 +0100445void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100446{
Ming Leicdc97792008-02-24 18:41:47 +0800447 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100448 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700449 int status = urb->status;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100450 unsigned long flags;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100451
Harvey Harrison441b62c2008-03-03 16:08:34 -0800452 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100453
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700454 if (unlikely(status != 0)) {
455 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800456 __func__, status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100457 return;
458 }
459
Alan Coxae643872008-07-22 11:11:55 +0100460 usb_serial_debug_data(debug, &port->dev, __func__,
461 urb->actual_length, data);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100462
463 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100464 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100465 port->throttled = port->throttle_req;
466 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800467 spin_unlock_irqrestore(&port->lock, flags);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200468 flush_and_resubmit_read_urb(port);
Alan Coxae643872008-07-22 11:11:55 +0100469 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800470 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100471}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300472EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Alan Cox95da3102008-07-22 11:09:07 +0100474void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500476 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800477 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700478 int status = urb->status;
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
Jason Wessel715b1dc2009-05-11 15:24:07 -0500482 if (port->serial->type->max_in_flight_urbs) {
483 spin_lock_irqsave(&port->lock, flags);
484 --port->urbs_in_flight;
485 port->tx_bytes_flight -= urb->transfer_buffer_length;
486 if (port->urbs_in_flight < 0)
487 port->urbs_in_flight = 0;
488 spin_unlock_irqrestore(&port->lock, flags);
489 } else {
490 /* Handle the case for single urb mode */
491 port->write_urb_busy = 0;
492 }
493
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700494 if (status) {
495 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800496 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return;
498 }
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700499 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
Greg Kroah-Hartmanbb833982005-11-17 09:48:18 -0800501EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Alan Cox95da3102008-07-22 11:09:07 +0100503void usb_serial_generic_throttle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100504{
Alan Cox95da3102008-07-22 11:09:07 +0100505 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100506 unsigned long flags;
507
Harvey Harrison441b62c2008-03-03 16:08:34 -0800508 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100509
510 /* Set the throttle request flag. It will be picked up
511 * by usb_serial_generic_read_bulk_callback(). */
512 spin_lock_irqsave(&port->lock, flags);
513 port->throttle_req = 1;
514 spin_unlock_irqrestore(&port->lock, flags);
515}
516
Alan Cox95da3102008-07-22 11:09:07 +0100517void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100518{
Alan Cox95da3102008-07-22 11:09:07 +0100519 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100520 int was_throttled;
521 unsigned long flags;
522
Harvey Harrison441b62c2008-03-03 16:08:34 -0800523 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100524
525 /* Clear the throttle flags */
526 spin_lock_irqsave(&port->lock, flags);
527 was_throttled = port->throttled;
528 port->throttled = port->throttle_req = 0;
529 spin_unlock_irqrestore(&port->lock, flags);
530
531 if (was_throttled) {
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200532 /* Resume reading from device */
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500533 usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100534 }
535}
536
Alan Cox24a15a62009-07-09 13:36:22 +0100537int usb_serial_handle_sysrq_char(struct tty_struct *tty,
538 struct usb_serial_port *port, unsigned int ch)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500539{
Jason Wessel568d4222009-05-29 13:34:17 -0500540 if (port->sysrq && port->console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500541 if (ch && time_before(jiffies, port->sysrq)) {
Alan Cox24a15a62009-07-09 13:36:22 +0100542 handle_sysrq(ch, tty);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500543 port->sysrq = 0;
544 return 1;
545 }
546 port->sysrq = 0;
547 }
548 return 0;
549}
550EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
551
552int usb_serial_handle_break(struct usb_serial_port *port)
553{
554 if (!port->sysrq) {
555 port->sysrq = jiffies + HZ*5;
556 return 1;
557 }
558 port->sysrq = 0;
559 return 0;
560}
561EXPORT_SYMBOL_GPL(usb_serial_handle_break);
562
Alan Sternf9c99bb2009-06-02 11:53:55 -0400563void usb_serial_generic_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 int i;
566
Harvey Harrison441b62c2008-03-03 16:08:34 -0800567 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100570 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Alan Sternf9c99bb2009-06-02 11:53:55 -0400574void usb_serial_generic_release(struct usb_serial *serial)
575{
576 dbg("%s", __func__);
577}