blob: 8dabac1929b04b2d09cdf7f62e3f3742bc9be8ce [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,
66 .shutdown = usb_serial_generic_shutdown,
Joris van Rantwijk253ca922007-02-01 20:08:18 +010067 .throttle = usb_serial_generic_throttle,
68 .unthrottle = usb_serial_generic_unthrottle,
Oliver Neukumec225592007-04-27 20:54:57 +020069 .resume = usb_serial_generic_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int generic_probe(struct usb_interface *interface,
73 const struct usb_device_id *id)
74{
75 const struct usb_device_id *id_pattern;
76
77 id_pattern = usb_match_id(interface, generic_device_ids);
78 if (id_pattern != NULL)
79 return usb_serial_probe(interface, id);
80 return -ENODEV;
81}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif
83
Alan Coxae643872008-07-22 11:11:55 +010084int usb_serial_generic_register(int _debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 int retval = 0;
87
88 debug = _debug;
89#ifdef CONFIG_USB_SERIAL_GENERIC
90 generic_device_ids[0].idVendor = vendor;
91 generic_device_ids[0].idProduct = product;
Alan Coxae643872008-07-22 11:11:55 +010092 generic_device_ids[0].match_flags =
93 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /* register our generic driver with ourselves */
Alan Coxae643872008-07-22 11:11:55 +010096 retval = usb_serial_register(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (retval)
98 goto exit;
99 retval = usb_register(&generic_driver);
100 if (retval)
101 usb_serial_deregister(&usb_serial_generic_device);
102exit:
103#endif
104 return retval;
105}
106
Alan Coxae643872008-07-22 11:11:55 +0100107void usb_serial_generic_deregister(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109#ifdef CONFIG_USB_SERIAL_GENERIC
110 /* remove our generic driver */
111 usb_deregister(&generic_driver);
Alan Coxae643872008-07-22 11:11:55 +0100112 usb_serial_deregister(&usb_serial_generic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif
114}
115
Alan Cox95da3102008-07-22 11:09:07 +0100116int usb_serial_generic_open(struct tty_struct *tty,
117 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct usb_serial *serial = port->serial;
120 int result = 0;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100121 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Harvey Harrison441b62c2008-03-03 16:08:34 -0800123 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100125 /* clear the throttle flags */
126 spin_lock_irqsave(&port->lock, flags);
127 port->throttled = 0;
128 port->throttle_req = 0;
129 spin_unlock_irqrestore(&port->lock, flags);
130
131 /* if we have a bulk endpoint, start reading from it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (serial->num_bulk_in) {
133 /* Start reading from the device */
Alan Coxae643872008-07-22 11:11:55 +0100134 usb_fill_bulk_urb(port->read_urb, serial->dev,
135 usb_rcvbulkpipe(serial->dev,
136 port->bulk_in_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 port->read_urb->transfer_buffer,
138 port->read_urb->transfer_buffer_length,
139 ((serial->type->read_bulk_callback) ?
140 serial->type->read_bulk_callback :
141 usb_serial_generic_read_bulk_callback),
142 port);
143 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
144 if (result)
Alan Coxae643872008-07-22 11:11:55 +0100145 dev_err(&port->dev,
146 "%s - failed resubmitting read urb, error %d\n",
147 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
150 return result;
151}
Greg Kroah-Hartman815ddc92006-05-12 11:05:29 -0700152EXPORT_SYMBOL_GPL(usb_serial_generic_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Alan Cox95da3102008-07-22 11:09:07 +0100154static void generic_cleanup(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct usb_serial *serial = port->serial;
157
Harvey Harrison441b62c2008-03-03 16:08:34 -0800158 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 if (serial->dev) {
161 /* shutdown any bulk reads that might be going on */
162 if (serial->num_bulk_out)
163 usb_kill_urb(port->write_urb);
164 if (serial->num_bulk_in)
165 usb_kill_urb(port->read_urb);
166 }
167}
168
Oliver Neukumec225592007-04-27 20:54:57 +0200169int usb_serial_generic_resume(struct usb_serial *serial)
170{
171 struct usb_serial_port *port;
172 int i, c = 0, r;
173
174 for (i = 0; i < serial->num_ports; i++) {
175 port = serial->port[i];
Alan Cox95da3102008-07-22 11:09:07 +0100176 if (port->port.count && port->read_urb) {
Oliver Neukumec225592007-04-27 20:54:57 +0200177 r = usb_submit_urb(port->read_urb, GFP_NOIO);
178 if (r < 0)
179 c++;
180 }
181 }
182
183 return c ? -EIO : 0;
184}
Oliver Neukum81d043c2009-02-06 15:37:14 +0100185EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
Oliver Neukumec225592007-04-27 20:54:57 +0200186
Alan Cox335f8512009-06-11 12:26:29 +0100187void usb_serial_generic_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800189 dbg("%s - port %d", __func__, port->number);
Alan Coxae643872008-07-22 11:11:55 +0100190 generic_cleanup(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Jason Wessel715b1dc2009-05-11 15:24:07 -0500193static int usb_serial_multi_urb_write(struct tty_struct *tty,
194 struct usb_serial_port *port, const unsigned char *buf, int count)
195{
196 unsigned long flags;
197 struct urb *urb;
198 unsigned char *buffer;
199 int status;
200 int towrite;
201 int bwrite = 0;
202
203 dbg("%s - port %d", __func__, port->number);
204
205 if (count == 0)
206 dbg("%s - write request of 0 bytes", __func__);
207
208 while (count > 0) {
209 towrite = (count > port->bulk_out_size) ?
210 port->bulk_out_size : count;
211 spin_lock_irqsave(&port->lock, flags);
212 if (port->urbs_in_flight >
213 port->serial->type->max_in_flight_urbs) {
214 spin_unlock_irqrestore(&port->lock, flags);
215 dbg("%s - write limit hit\n", __func__);
216 return bwrite;
217 }
218 port->tx_bytes_flight += towrite;
219 port->urbs_in_flight++;
220 spin_unlock_irqrestore(&port->lock, flags);
221
222 buffer = kmalloc(towrite, GFP_ATOMIC);
223 if (!buffer) {
224 dev_err(&port->dev,
225 "%s ran out of kernel memory for urb ...\n", __func__);
226 goto error_no_buffer;
227 }
228
229 urb = usb_alloc_urb(0, GFP_ATOMIC);
230 if (!urb) {
231 dev_err(&port->dev, "%s - no more free urbs\n",
232 __func__);
233 goto error_no_urb;
234 }
235
236 /* Copy data */
237 memcpy(buffer, buf + bwrite, towrite);
238 usb_serial_debug_data(debug, &port->dev, __func__,
239 towrite, buffer);
240 /* fill the buffer and send it */
241 usb_fill_bulk_urb(urb, port->serial->dev,
242 usb_sndbulkpipe(port->serial->dev,
243 port->bulk_out_endpointAddress),
244 buffer, towrite,
245 usb_serial_generic_write_bulk_callback, port);
246
247 status = usb_submit_urb(urb, GFP_ATOMIC);
248 if (status) {
249 dev_err(&port->dev,
250 "%s - failed submitting write urb, error %d\n",
251 __func__, status);
252 goto error;
253 }
254
255 /* This urb is the responsibility of the host driver now */
256 usb_free_urb(urb);
257 dbg("%s write: %d", __func__, towrite);
258 count -= towrite;
259 bwrite += towrite;
260 }
261 return bwrite;
262
263error:
264 usb_free_urb(urb);
265error_no_urb:
266 kfree(buffer);
267error_no_buffer:
268 spin_lock_irqsave(&port->lock, flags);
269 port->urbs_in_flight--;
270 port->tx_bytes_flight -= towrite;
271 spin_unlock_irqrestore(&port->lock, flags);
272 return bwrite;
273}
274
Alan Cox95da3102008-07-22 11:09:07 +0100275int usb_serial_generic_write(struct tty_struct *tty,
276 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct usb_serial *serial = port->serial;
279 int result;
280 unsigned char *data;
281
Harvey Harrison441b62c2008-03-03 16:08:34 -0800282 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800285 dbg("%s - write request of 0 bytes", __func__);
Alan Coxae643872008-07-22 11:11:55 +0100286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
289 /* only do something if we have a bulk out endpoint */
290 if (serial->num_bulk_out) {
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200291 unsigned long flags;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500292
293 if (serial->type->max_in_flight_urbs)
294 return usb_serial_multi_urb_write(tty, port,
295 buf, count);
296
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200297 spin_lock_irqsave(&port->lock, flags);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700298 if (port->write_urb_busy) {
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200299 spin_unlock_irqrestore(&port->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800300 dbg("%s - already writing", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700303 port->write_urb_busy = 1;
Jiri Kosinaacd2a842007-10-20 00:05:19 +0200304 spin_unlock_irqrestore(&port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Alan Coxae643872008-07-22 11:11:55 +0100306 count = (count > port->bulk_out_size) ?
307 port->bulk_out_size : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Alan Coxae643872008-07-22 11:11:55 +0100309 memcpy(port->write_urb->transfer_buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 data = port->write_urb->transfer_buffer;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800311 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* set up our urb */
Alan Coxae643872008-07-22 11:11:55 +0100314 usb_fill_bulk_urb(port->write_urb, serial->dev,
315 usb_sndbulkpipe(serial->dev,
316 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 port->write_urb->transfer_buffer, count,
Alan Coxae643872008-07-22 11:11:55 +0100318 ((serial->type->write_bulk_callback) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 serial->type->write_bulk_callback :
Alan Coxae643872008-07-22 11:11:55 +0100320 usb_serial_generic_write_bulk_callback),
321 port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* send the data out the bulk port */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700324 port->write_urb_busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700326 if (result) {
Alan Coxae643872008-07-22 11:11:55 +0100327 dev_err(&port->dev,
328 "%s - failed submitting write urb, error %d\n",
329 __func__, result);
330 /* don't have to grab the lock here, as we will
331 retry if != 0 */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700332 port->write_urb_busy = 0;
333 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 result = count;
335
336 return result;
337 }
338
339 /* no bulk out, so return 0 bytes written */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500342EXPORT_SYMBOL_GPL(usb_serial_generic_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Alan Coxae643872008-07-22 11:11:55 +0100344int usb_serial_generic_write_room(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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int room = 0;
350
Harvey Harrison441b62c2008-03-03 16:08:34 -0800351 dbg("%s - port %d", __func__, port->number);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500352 spin_lock_irqsave(&port->lock, flags);
353 if (serial->type->max_in_flight_urbs) {
354 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500355 room = port->bulk_out_size *
356 (serial->type->max_in_flight_urbs -
357 port->urbs_in_flight);
Jason Wessel715b1dc2009-05-11 15:24:07 -0500358 } else if (serial->num_bulk_out && !(port->write_urb_busy)) {
359 room = port->bulk_out_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Jason Wessel715b1dc2009-05-11 15:24:07 -0500361 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__, room);
Alan Coxa5b6f602008-04-08 17:16:06 +0100364 return room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Alan Cox95da3102008-07-22 11:09:07 +0100367int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Alan Cox95da3102008-07-22 11:09:07 +0100369 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct usb_serial *serial = port->serial;
371 int chars = 0;
Jason Wessel715b1dc2009-05-11 15:24:07 -0500372 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Harvey Harrison441b62c2008-03-03 16:08:34 -0800374 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jason Wessel715b1dc2009-05-11 15:24:07 -0500376 if (serial->type->max_in_flight_urbs) {
377 spin_lock_irqsave(&port->lock, flags);
378 chars = port->tx_bytes_flight;
379 spin_unlock_irqrestore(&port->lock, flags);
380 } else if (serial->num_bulk_out) {
381 /* FIXME: Locking */
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700382 if (port->write_urb_busy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 chars = port->write_urb->transfer_buffer_length;
384 }
385
Harvey Harrison441b62c2008-03-03 16:08:34 -0800386 dbg("%s - returns %d", __func__, chars);
Alan Cox95da3102008-07-22 11:09:07 +0100387 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200390
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500391void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
392 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100394 struct urb *urb = port->read_urb;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200395 struct usb_serial *serial = port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int result;
397
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100398 /* Continue reading from device */
Alan Coxae643872008-07-22 11:11:55 +0100399 usb_fill_bulk_urb(urb, serial->dev,
400 usb_rcvbulkpipe(serial->dev,
401 port->bulk_in_endpointAddress),
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200402 urb->transfer_buffer,
403 urb->transfer_buffer_length,
Alan Coxae643872008-07-22 11:11:55 +0100404 ((serial->type->read_bulk_callback) ?
405 serial->type->read_bulk_callback :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 usb_serial_generic_read_bulk_callback), port);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200407 result = usb_submit_urb(urb, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (result)
Alan Coxae643872008-07-22 11:11:55 +0100409 dev_err(&port->dev,
410 "%s - failed resubmitting read urb, error %d\n",
411 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500413EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100414
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200415/* Push data to tty layer and resubmit the bulk read URB */
Alan Cox95da3102008-07-22 11:09:07 +0100416static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200417{
418 struct urb *urb = port->read_urb;
Alan Cox4a90f092008-10-13 10:39:46 +0100419 struct tty_struct *tty = tty_port_tty_get(&port->port);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500420 char *ch = (char *)urb->transfer_buffer;
421 int i;
422
423 if (!tty)
424 goto done;
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200425
426 /* Push data to tty */
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500427 for (i = 0; i < urb->actual_length; i++, ch++) {
428 if (!usb_serial_handle_sysrq_char(port, *ch))
429 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200430 }
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500431 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100432 tty_kref_put(tty);
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500433done:
434 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200435}
436
Alan Cox95da3102008-07-22 11:09:07 +0100437void usb_serial_generic_read_bulk_callback(struct urb *urb)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100438{
Ming Leicdc97792008-02-24 18:41:47 +0800439 struct usb_serial_port *port = urb->context;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100440 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700441 int status = urb->status;
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100442 unsigned long flags;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100443
Harvey Harrison441b62c2008-03-03 16:08:34 -0800444 dbg("%s - port %d", __func__, port->number);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100445
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700446 if (unlikely(status != 0)) {
447 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800448 __func__, status);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100449 return;
450 }
451
Alan Coxae643872008-07-22 11:11:55 +0100452 usb_serial_debug_data(debug, &port->dev, __func__,
453 urb->actual_length, data);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100454
455 /* Throttle the device if requested by tty */
Borislav Petkovbfaeafc2007-10-28 13:24:16 +0100456 spin_lock_irqsave(&port->lock, flags);
Alan Coxae643872008-07-22 11:11:55 +0100457 port->throttled = port->throttle_req;
458 if (!port->throttled) {
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800459 spin_unlock_irqrestore(&port->lock, flags);
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200460 flush_and_resubmit_read_urb(port);
Alan Coxae643872008-07-22 11:11:55 +0100461 } else
Pete Zaitcevb507cc92008-03-04 23:28:42 -0800462 spin_unlock_irqrestore(&port->lock, flags);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100463}
Luiz Fernando N. Capitulino166ffcc2006-07-11 14:19:25 -0300464EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Alan Cox95da3102008-07-22 11:09:07 +0100466void usb_serial_generic_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jason Wessel715b1dc2009-05-11 15:24:07 -0500468 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +0800469 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700470 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Harvey Harrison441b62c2008-03-03 16:08:34 -0800472 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jason Wessel715b1dc2009-05-11 15:24:07 -0500474 if (port->serial->type->max_in_flight_urbs) {
475 spin_lock_irqsave(&port->lock, flags);
476 --port->urbs_in_flight;
477 port->tx_bytes_flight -= urb->transfer_buffer_length;
478 if (port->urbs_in_flight < 0)
479 port->urbs_in_flight = 0;
480 spin_unlock_irqrestore(&port->lock, flags);
481 } else {
482 /* Handle the case for single urb mode */
483 port->write_urb_busy = 0;
484 }
485
Greg Kroah-Hartmanfbd272252007-06-15 15:44:13 -0700486 if (status) {
487 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800488 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return;
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}
508
Alan Cox95da3102008-07-22 11:09:07 +0100509void usb_serial_generic_unthrottle(struct tty_struct *tty)
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100510{
Alan Cox95da3102008-07-22 11:09:07 +0100511 struct usb_serial_port *port = tty->driver_data;
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100512 int was_throttled;
513 unsigned long flags;
514
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 */
518 spin_lock_irqsave(&port->lock, flags);
519 was_throttled = port->throttled;
520 port->throttled = port->throttle_req = 0;
521 spin_unlock_irqrestore(&port->lock, flags);
522
523 if (was_throttled) {
Oliver Neukum1abdeeb2007-05-07 12:09:33 +0200524 /* Resume reading from device */
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500525 usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL);
Joris van Rantwijk253ca922007-02-01 20:08:18 +0100526 }
527}
528
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500529int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
530{
Jason Wessel568d4222009-05-29 13:34:17 -0500531 if (port->sysrq && port->console) {
Jason Wessel98fcb5f2009-05-11 15:24:09 -0500532 if (ch && time_before(jiffies, port->sysrq)) {
533 handle_sysrq(ch, tty_port_tty_get(&port->port));
534 port->sysrq = 0;
535 return 1;
536 }
537 port->sysrq = 0;
538 }
539 return 0;
540}
541EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
542
543int usb_serial_handle_break(struct usb_serial_port *port)
544{
545 if (!port->sysrq) {
546 port->sysrq = jiffies + HZ*5;
547 return 1;
548 }
549 port->sysrq = 0;
550 return 0;
551}
552EXPORT_SYMBOL_GPL(usb_serial_handle_break);
553
Alan Coxae643872008-07-22 11:11:55 +0100554void usb_serial_generic_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 int i;
557
Harvey Harrison441b62c2008-03-03 16:08:34 -0800558 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* stop reads and writes on all ports */
Alan Coxae643872008-07-22 11:11:55 +0100561 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 generic_cleanup(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564