USB: serial: remove bogus disconnect test in cleanup
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / serial / generic.c
CommitLineData
1da177e4
LT
1/*
2 * USB Serial Converter Generic functions
3 *
d83b4053 4 * Copyright (C) 2010 - 2011 Johan Hovold (jhovold@gmail.com)
1da177e4
LT
5 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 */
12
1da177e4
LT
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
7f0ae3a8 16#include <linux/sysrq.h>
1da177e4
LT
17#include <linux/tty.h>
18#include <linux/tty_flip.h>
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/usb.h>
a969888c 22#include <linux/usb/serial.h>
ae64387a 23#include <linux/uaccess.h>
8e8dce06 24#include <linux/kfifo.h>
1f87158e 25#include <linux/serial.h>
d9b1b787 26
1da177e4 27#ifdef CONFIG_USB_SERIAL_GENERIC
b46d60fc 28
1da177e4
LT
29static __u16 vendor = 0x05f9;
30static __u16 product = 0xffff;
31
32module_param(vendor, ushort, 0);
33MODULE_PARM_DESC(vendor, "User specified USB idVendor");
34
35module_param(product, ushort, 0);
36MODULE_PARM_DESC(product, "User specified USB idProduct");
37
38static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
39
40/* All of the device info needed for the Generic Serial Converter */
ea65370d 41struct usb_serial_driver usb_serial_generic_device = {
18fcac35
GKH
42 .driver = {
43 .owner = THIS_MODULE,
269bda1c 44 .name = "generic",
18fcac35 45 },
1da177e4 46 .id_table = generic_device_ids,
1da177e4 47 .num_ports = 1,
f9c99bb8
AS
48 .disconnect = usb_serial_generic_disconnect,
49 .release = usb_serial_generic_release,
253ca923
JR
50 .throttle = usb_serial_generic_throttle,
51 .unthrottle = usb_serial_generic_unthrottle,
ec22559e 52 .resume = usb_serial_generic_resume,
1da177e4
LT
53};
54
765e0ba6
AS
55static struct usb_serial_driver * const serial_drivers[] = {
56 &usb_serial_generic_device, NULL
57};
58
1da177e4
LT
59#endif
60
3033bc8d 61int usb_serial_generic_register(void)
1da177e4
LT
62{
63 int retval = 0;
64
1da177e4
LT
65#ifdef CONFIG_USB_SERIAL_GENERIC
66 generic_device_ids[0].idVendor = vendor;
67 generic_device_ids[0].idProduct = product;
ae64387a
AC
68 generic_device_ids[0].match_flags =
69 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
1da177e4
LT
70
71 /* register our generic driver with ourselves */
0b84704a
AS
72 retval = usb_serial_register_drivers(serial_drivers,
73 "usbserial_generic", generic_device_ids);
1da177e4
LT
74#endif
75 return retval;
76}
77
ae64387a 78void usb_serial_generic_deregister(void)
1da177e4
LT
79{
80#ifdef CONFIG_USB_SERIAL_GENERIC
81 /* remove our generic driver */
68e24113 82 usb_serial_deregister_drivers(serial_drivers);
1da177e4
LT
83#endif
84}
85
a509a7e4 86int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
1da177e4 87{
1da177e4 88 int result = 0;
253ca923 89 unsigned long flags;
1da177e4 90
253ca923
JR
91 /* clear the throttle flags */
92 spin_lock_irqsave(&port->lock, flags);
93 port->throttled = 0;
94 port->throttle_req = 0;
95 spin_unlock_irqrestore(&port->lock, flags);
96
97 /* if we have a bulk endpoint, start reading from it */
41bd72f9 98 if (port->bulk_in_size)
d83b4053 99 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
1da177e4
LT
100
101 return result;
102}
815ddc99 103EXPORT_SYMBOL_GPL(usb_serial_generic_open);
1da177e4 104
95da310e 105static void generic_cleanup(struct usb_serial_port *port)
1da177e4 106{
ec3ee508 107 unsigned long flags;
27c7acf2 108 int i;
1da177e4 109
19c61853
JH
110 if (port->bulk_out_size) {
111 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
112 usb_kill_urb(port->write_urbs[i]);
113
114 spin_lock_irqsave(&port->lock, flags);
115 kfifo_reset_out(&port->write_fifo);
116 spin_unlock_irqrestore(&port->lock, flags);
117 }
118 if (port->bulk_in_size) {
119 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
120 usb_kill_urb(port->read_urbs[i]);
1da177e4
LT
121 }
122}
123
335f8514 124void usb_serial_generic_close(struct usb_serial_port *port)
1da177e4 125{
ae64387a 126 generic_cleanup(port);
1da177e4 127}
f26788da 128EXPORT_SYMBOL_GPL(usb_serial_generic_close);
1da177e4 129
eaa3bcb0 130int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
c23e5fc1 131 void *dest, size_t size)
eaa3bcb0 132{
c23e5fc1 133 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
715b1dc0
JW
134}
135
8e8dce06
DV
136/**
137 * usb_serial_generic_write_start - kick off an URB write
138 * @port: Pointer to the &struct usb_serial_port data
139 *
27c7acf2 140 * Returns zero on success, or a negative errno value
8e8dce06
DV
141 */
142static int usb_serial_generic_write_start(struct usb_serial_port *port)
143{
27c7acf2
JH
144 struct urb *urb;
145 int count, result;
8e8dce06 146 unsigned long flags;
27c7acf2 147 int i;
8e8dce06 148
27c7acf2
JH
149 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
150 return 0;
151retry:
8e8dce06 152 spin_lock_irqsave(&port->lock, flags);
27c7acf2
JH
153 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
154 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
50a5f70c
JH
155 spin_unlock_irqrestore(&port->lock, flags);
156 return 0;
8e8dce06 157 }
27c7acf2
JH
158 i = (int)find_first_bit(&port->write_urbs_free,
159 ARRAY_SIZE(port->write_urbs));
8e8dce06
DV
160 spin_unlock_irqrestore(&port->lock, flags);
161
27c7acf2 162 urb = port->write_urbs[i];
eaa3bcb0 163 count = port->serial->type->prepare_write_buffer(port,
c23e5fc1
JH
164 urb->transfer_buffer,
165 port->bulk_out_size);
27c7acf2 166 urb->transfer_buffer_length = count;
59d33f2f 167 usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
b58af406
JH
168 spin_lock_irqsave(&port->lock, flags);
169 port->tx_bytes += count;
170 spin_unlock_irqrestore(&port->lock, flags);
171
172 clear_bit(i, &port->write_urbs_free);
27c7acf2 173 result = usb_submit_urb(urb, GFP_ATOMIC);
8e8dce06 174 if (result) {
f1475a00 175 dev_err_console(port, "%s - error submitting urb: %d\n",
8e8dce06 176 __func__, result);
b58af406
JH
177 set_bit(i, &port->write_urbs_free);
178 spin_lock_irqsave(&port->lock, flags);
179 port->tx_bytes -= count;
180 spin_unlock_irqrestore(&port->lock, flags);
181
27c7acf2 182 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
30af7fb5
JH
183 return result;
184 }
30af7fb5 185
27c7acf2
JH
186 /* Try sending off another urb, unless in irq context (in which case
187 * there will be no free urb). */
188 if (!in_irq())
189 goto retry;
190
191 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
192
193 return 0;
8e8dce06
DV
194}
195
196/**
197 * usb_serial_generic_write - generic write function for serial USB devices
198 * @tty: Pointer to &struct tty_struct for the device
199 * @port: Pointer to the &usb_serial_port structure for the device
200 * @buf: Pointer to the data to write
201 * @count: Number of bytes to write
202 *
203 * Returns the number of characters actually written, which may be anything
204 * from zero to @count. If an error occurs, it returns the negative errno
205 * value.
206 */
95da310e
AC
207int usb_serial_generic_write(struct tty_struct *tty,
208 struct usb_serial_port *port, const unsigned char *buf, int count)
1da177e4 209{
1da177e4 210 int result;
1da177e4 211
eb8878a8
JH
212 /* only do something if we have a bulk out endpoint */
213 if (!port->bulk_out_size)
214 return -ENODEV;
215
1a1405e2 216 if (!count)
ae64387a 217 return 0;
1da177e4 218
119eecc8 219 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
8e8dce06 220 result = usb_serial_generic_write_start(port);
c23e5fc1
JH
221 if (result)
222 return result;
1da177e4 223
c23e5fc1 224 return count;
1da177e4 225}
98fcb5f7 226EXPORT_SYMBOL_GPL(usb_serial_generic_write);
1da177e4 227
ae64387a 228int usb_serial_generic_write_room(struct tty_struct *tty)
1da177e4 229{
95da310e 230 struct usb_serial_port *port = tty->driver_data;
715b1dc0 231 unsigned long flags;
25d514ca 232 int room;
1da177e4 233
eb8878a8
JH
234 if (!port->bulk_out_size)
235 return 0;
236
715b1dc0 237 spin_lock_irqsave(&port->lock, flags);
c23e5fc1 238 room = kfifo_avail(&port->write_fifo);
715b1dc0 239 spin_unlock_irqrestore(&port->lock, flags);
1da177e4 240
689c2781 241 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
a5b6f60c 242 return room;
1da177e4
LT
243}
244
95da310e 245int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
1da177e4 246{
95da310e 247 struct usb_serial_port *port = tty->driver_data;
715b1dc0 248 unsigned long flags;
eb8878a8 249 int chars;
1da177e4 250
eb8878a8
JH
251 if (!port->bulk_out_size)
252 return 0;
253
25719e6b 254 spin_lock_irqsave(&port->lock, flags);
c23e5fc1 255 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
25719e6b 256 spin_unlock_irqrestore(&port->lock, flags);
1da177e4 257
689c2781 258 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
95da310e 259 return chars;
1da177e4 260}
428d9988 261EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
1da177e4 262
d83b4053
JH
263static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
264 int index, gfp_t mem_flags)
265{
266 int res;
267
268 if (!test_and_clear_bit(index, &port->read_urbs_free))
269 return 0;
270
49bd196d 271 dev_dbg(&port->dev, "%s - urb %d\n", __func__, index);
d83b4053
JH
272
273 res = usb_submit_urb(port->read_urbs[index], mem_flags);
274 if (res) {
275 if (res != -EPERM) {
276 dev_err(&port->dev,
277 "%s - usb_submit_urb failed: %d\n",
278 __func__, res);
279 }
280 set_bit(index, &port->read_urbs_free);
281 return res;
282 }
283
284 return 0;
285}
286
287int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
41bd72f9 288 gfp_t mem_flags)
1da177e4 289{
d83b4053
JH
290 int res;
291 int i;
1da177e4 292
d83b4053
JH
293 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
294 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
295 if (res)
296 goto err;
0ae14743 297 }
d83b4053
JH
298
299 return 0;
300err:
301 for (; i >= 0; --i)
302 usb_kill_urb(port->read_urbs[i]);
303
304 return res;
1da177e4 305}
d83b4053 306EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
253ca923 307
23154320 308void usb_serial_generic_process_read_urb(struct urb *urb)
1abdeeb1 309{
0f3d5bae 310 struct usb_serial_port *port = urb->context;
98fcb5f7
JW
311 char *ch = (char *)urb->transfer_buffer;
312 int i;
313
56a1df46
JH
314 if (!urb->actual_length)
315 return;
316
4cd1de0a
AC
317 /* The per character mucking around with sysrq path it too slow for
318 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
319 where the USB serial is not a console anyway */
bd5afa9e 320 if (!port->port.console || !port->sysrq)
05c7cd39 321 tty_insert_flip_string(&port->port, ch, urb->actual_length);
4cd1de0a 322 else {
4cd1de0a 323 for (i = 0; i < urb->actual_length; i++, ch++) {
6ee9f4b4 324 if (!usb_serial_handle_sysrq_char(port, *ch))
92a19f9c 325 tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
4cd1de0a 326 }
1abdeeb1 327 }
2e124b4a 328 tty_flip_buffer_push(&port->port);
1abdeeb1 329}
23154320 330EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
1abdeeb1 331
95da310e 332void usb_serial_generic_read_bulk_callback(struct urb *urb)
253ca923 333{
cdc97792 334 struct usb_serial_port *port = urb->context;
253ca923 335 unsigned char *data = urb->transfer_buffer;
bfaeafcf 336 unsigned long flags;
d83b4053 337 int i;
253ca923 338
d83b4053
JH
339 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
340 if (urb == port->read_urbs[i])
341 break;
342 }
343 set_bit(i, &port->read_urbs_free);
253ca923 344
49bd196d
JH
345 dev_dbg(&port->dev, "%s - urb %d, len %d\n", __func__, i,
346 urb->actual_length);
689c2781 347
d83b4053 348 if (urb->status) {
689c2781
GKH
349 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
350 __func__, urb->status);
253ca923
JR
351 return;
352 }
353
59d33f2f 354 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
23154320 355 port->serial->type->process_read_urb(urb);
253ca923
JR
356
357 /* Throttle the device if requested by tty */
bfaeafcf 358 spin_lock_irqsave(&port->lock, flags);
ae64387a
AC
359 port->throttled = port->throttle_req;
360 if (!port->throttled) {
b507cc97 361 spin_unlock_irqrestore(&port->lock, flags);
d83b4053 362 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
ae64387a 363 } else
b507cc97 364 spin_unlock_irqrestore(&port->lock, flags);
253ca923 365}
166ffccf 366EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
1da177e4 367
95da310e 368void usb_serial_generic_write_bulk_callback(struct urb *urb)
1da177e4 369{
715b1dc0 370 unsigned long flags;
cdc97792 371 struct usb_serial_port *port = urb->context;
fbd27225 372 int status = urb->status;
27c7acf2 373 int i;
1da177e4 374
c23e5fc1
JH
375 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
376 if (port->write_urbs[i] == urb)
377 break;
25915302 378
c23e5fc1
JH
379 spin_lock_irqsave(&port->lock, flags);
380 port->tx_bytes -= urb->transfer_buffer_length;
381 set_bit(i, &port->write_urbs_free);
382 spin_unlock_irqrestore(&port->lock, flags);
383
384 if (status) {
689c2781
GKH
385 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
386 __func__, status);
27c7acf2 387
30af7fb5 388 spin_lock_irqsave(&port->lock, flags);
c23e5fc1 389 kfifo_reset_out(&port->write_fifo);
30af7fb5 390 spin_unlock_irqrestore(&port->lock, flags);
c23e5fc1
JH
391 } else {
392 usb_serial_generic_write_start(port);
1da177e4 393 }
8e8dce06 394
cf2c7481 395 usb_serial_port_softint(port);
1da177e4 396}
bb833986 397EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
1da177e4 398
95da310e 399void usb_serial_generic_throttle(struct tty_struct *tty)
253ca923 400{
95da310e 401 struct usb_serial_port *port = tty->driver_data;
253ca923
JR
402 unsigned long flags;
403
253ca923
JR
404 /* Set the throttle request flag. It will be picked up
405 * by usb_serial_generic_read_bulk_callback(). */
406 spin_lock_irqsave(&port->lock, flags);
407 port->throttle_req = 1;
408 spin_unlock_irqrestore(&port->lock, flags);
409}
f1e949ac 410EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
253ca923 411
95da310e 412void usb_serial_generic_unthrottle(struct tty_struct *tty)
253ca923 413{
95da310e 414 struct usb_serial_port *port = tty->driver_data;
253ca923 415 int was_throttled;
253ca923 416
253ca923 417 /* Clear the throttle flags */
0f3d5bae 418 spin_lock_irq(&port->lock);
253ca923
JR
419 was_throttled = port->throttled;
420 port->throttled = port->throttle_req = 0;
0f3d5bae 421 spin_unlock_irq(&port->lock);
253ca923 422
0f3d5bae 423 if (was_throttled)
d83b4053 424 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
253ca923 425}
f1e949ac 426EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
253ca923 427
7f0ae3a8 428#ifdef CONFIG_MAGIC_SYSRQ
6ee9f4b4 429int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
98fcb5f7 430{
bd5afa9e 431 if (port->sysrq && port->port.console) {
98fcb5f7 432 if (ch && time_before(jiffies, port->sysrq)) {
f335397d 433 handle_sysrq(ch);
98fcb5f7
JW
434 port->sysrq = 0;
435 return 1;
436 }
437 port->sysrq = 0;
438 }
439 return 0;
440}
7f0ae3a8 441#else
6ee9f4b4 442int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
7f0ae3a8
RD
443{
444 return 0;
445}
446#endif
98fcb5f7
JW
447EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
448
449int usb_serial_handle_break(struct usb_serial_port *port)
450{
451 if (!port->sysrq) {
452 port->sysrq = jiffies + HZ*5;
453 return 1;
454 }
455 port->sysrq = 0;
456 return 0;
457}
458EXPORT_SYMBOL_GPL(usb_serial_handle_break);
459
d14fc1a7
LP
460/**
461 * usb_serial_handle_dcd_change - handle a change of carrier detect state
462 * @port: usb_serial_port structure for the open port
463 * @tty: tty_struct structure for the port
464 * @status: new carrier detect status, nonzero if active
465 */
466void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
467 struct tty_struct *tty, unsigned int status)
468{
469 struct tty_port *port = &usb_port->port;
470
49bd196d 471 dev_dbg(&usb_port->dev, "%s - status %d\n", __func__, status);
d14fc1a7
LP
472
473 if (status)
474 wake_up_interruptible(&port->open_wait);
475 else if (tty && !C_CLOCAL(tty))
476 tty_hangup(tty);
477}
478EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
479
8e8dce06
DV
480int usb_serial_generic_resume(struct usb_serial *serial)
481{
482 struct usb_serial_port *port;
483 int i, c = 0, r;
484
485 for (i = 0; i < serial->num_ports; i++) {
486 port = serial->port[i];
1f87158e 487 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
8e8dce06
DV
488 continue;
489
d83b4053
JH
490 if (port->bulk_in_size) {
491 r = usb_serial_generic_submit_read_urbs(port,
492 GFP_NOIO);
8e8dce06
DV
493 if (r < 0)
494 c++;
495 }
496
27c7acf2 497 if (port->bulk_out_size) {
8e8dce06
DV
498 r = usb_serial_generic_write_start(port);
499 if (r < 0)
500 c++;
501 }
502 }
503
504 return c ? -EIO : 0;
505}
506EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
507
f9c99bb8 508void usb_serial_generic_disconnect(struct usb_serial *serial)
1da177e4
LT
509{
510 int i;
511
1da177e4 512 /* stop reads and writes on all ports */
ae64387a 513 for (i = 0; i < serial->num_ports; ++i)
1da177e4 514 generic_cleanup(serial->port[i]);
1da177e4 515}
5c7efeb7 516EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
1da177e4 517
f9c99bb8
AS
518void usb_serial_generic_release(struct usb_serial *serial)
519{
f9c99bb8 520}