USB: serial: clean up generic write start busy test
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / serial / generic.c
blob9756b5c22d9460cf5a476f306fffdf31f56ef0b4
1 /*
2 * USB Serial Converter Generic functions
4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
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.
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/sysrq.h>
16 #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>
21 #include <linux/usb/serial.h>
22 #include <linux/uaccess.h>
23 #include <linux/kfifo.h>
24 #include <linux/serial.h>
26 static int debug;
28 #ifdef CONFIG_USB_SERIAL_GENERIC
30 static int generic_probe(struct usb_interface *interface,
31 const struct usb_device_id *id);
33 static __u16 vendor = 0x05f9;
34 static __u16 product = 0xffff;
36 module_param(vendor, ushort, 0);
37 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
39 module_param(product, ushort, 0);
40 MODULE_PARM_DESC(product, "User specified USB idProduct");
42 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
44 /* we want to look at all devices, as the vendor/product id can change
45 * depending on the command line argument */
46 static const struct usb_device_id generic_serial_ids[] = {
47 {.driver_info = 42},
51 static 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,
59 /* All of the device info needed for the Generic Serial Converter */
60 struct usb_serial_driver usb_serial_generic_device = {
61 .driver = {
62 .owner = THIS_MODULE,
63 .name = "generic",
65 .id_table = generic_device_ids,
66 .usb_driver = &generic_driver,
67 .num_ports = 1,
68 .disconnect = usb_serial_generic_disconnect,
69 .release = usb_serial_generic_release,
70 .throttle = usb_serial_generic_throttle,
71 .unthrottle = usb_serial_generic_unthrottle,
72 .resume = usb_serial_generic_resume,
75 static int generic_probe(struct usb_interface *interface,
76 const struct usb_device_id *id)
78 const struct usb_device_id *id_pattern;
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;
85 #endif
87 int usb_serial_generic_register(int _debug)
89 int retval = 0;
91 debug = _debug;
92 #ifdef CONFIG_USB_SERIAL_GENERIC
93 generic_device_ids[0].idVendor = vendor;
94 generic_device_ids[0].idProduct = product;
95 generic_device_ids[0].match_flags =
96 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
98 /* register our generic driver with ourselves */
99 retval = usb_serial_register(&usb_serial_generic_device);
100 if (retval)
101 goto exit;
102 retval = usb_register(&generic_driver);
103 if (retval)
104 usb_serial_deregister(&usb_serial_generic_device);
105 exit:
106 #endif
107 return retval;
110 void usb_serial_generic_deregister(void)
112 #ifdef CONFIG_USB_SERIAL_GENERIC
113 /* remove our generic driver */
114 usb_deregister(&generic_driver);
115 usb_serial_deregister(&usb_serial_generic_device);
116 #endif
119 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
121 int result = 0;
122 unsigned long flags;
124 dbg("%s - port %d", __func__, port->number);
126 /* 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);
132 /* if we have a bulk endpoint, start reading from it */
133 if (port->bulk_in_size)
134 result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
136 return result;
138 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
140 static void generic_cleanup(struct usb_serial_port *port)
142 struct usb_serial *serial = port->serial;
143 unsigned long flags;
145 dbg("%s - port %d", __func__, port->number);
147 if (serial->dev) {
148 /* shutdown any bulk transfers that might be going on */
149 if (port->bulk_out_size) {
150 usb_kill_urb(port->write_urb);
152 spin_lock_irqsave(&port->lock, flags);
153 kfifo_reset_out(&port->write_fifo);
154 spin_unlock_irqrestore(&port->lock, flags);
156 if (port->bulk_in_size)
157 usb_kill_urb(port->read_urb);
161 void usb_serial_generic_close(struct usb_serial_port *port)
163 dbg("%s - port %d", __func__, port->number);
164 generic_cleanup(port);
166 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
168 static int usb_serial_multi_urb_write(struct tty_struct *tty,
169 struct usb_serial_port *port, const unsigned char *buf, int count)
171 unsigned long flags;
172 struct urb *urb;
173 unsigned char *buffer;
174 int status;
175 int towrite;
176 int bwrite = 0;
178 dbg("%s - port %d", __func__, port->number);
180 if (count == 0)
181 dbg("%s - write request of 0 bytes", __func__);
183 while (count > 0) {
184 towrite = (count > port->bulk_out_size) ?
185 port->bulk_out_size : count;
186 spin_lock_irqsave(&port->lock, flags);
187 if (port->urbs_in_flight >
188 port->serial->type->max_in_flight_urbs) {
189 spin_unlock_irqrestore(&port->lock, flags);
190 dbg("%s - write limit hit", __func__);
191 return bwrite;
193 port->tx_bytes_flight += towrite;
194 port->urbs_in_flight++;
195 spin_unlock_irqrestore(&port->lock, flags);
197 buffer = kmalloc(towrite, GFP_ATOMIC);
198 if (!buffer) {
199 dev_err(&port->dev,
200 "%s ran out of kernel memory for urb ...\n", __func__);
201 goto error_no_buffer;
204 urb = usb_alloc_urb(0, GFP_ATOMIC);
205 if (!urb) {
206 dev_err(&port->dev, "%s - no more free urbs\n",
207 __func__);
208 goto error_no_urb;
211 /* Copy data */
212 memcpy(buffer, buf + bwrite, towrite);
213 usb_serial_debug_data(debug, &port->dev, __func__,
214 towrite, buffer);
215 /* fill the buffer and send it */
216 usb_fill_bulk_urb(urb, port->serial->dev,
217 usb_sndbulkpipe(port->serial->dev,
218 port->bulk_out_endpointAddress),
219 buffer, towrite,
220 usb_serial_generic_write_bulk_callback, port);
222 status = usb_submit_urb(urb, GFP_ATOMIC);
223 if (status) {
224 dev_err(&port->dev, "%s - error submitting urb: %d\n",
225 __func__, status);
226 goto error;
229 /* This urb is the responsibility of the host driver now */
230 usb_free_urb(urb);
231 dbg("%s write: %d", __func__, towrite);
232 count -= towrite;
233 bwrite += towrite;
235 return bwrite;
237 error:
238 usb_free_urb(urb);
239 error_no_urb:
240 kfree(buffer);
241 error_no_buffer:
242 spin_lock_irqsave(&port->lock, flags);
243 port->urbs_in_flight--;
244 port->tx_bytes_flight -= towrite;
245 spin_unlock_irqrestore(&port->lock, flags);
246 return bwrite;
250 * usb_serial_generic_write_start - kick off an URB write
251 * @port: Pointer to the &struct usb_serial_port data
253 * Returns the number of bytes queued on success. This will be zero if there
254 * was nothing to send. Otherwise, it returns a negative errno value
256 static int usb_serial_generic_write_start(struct usb_serial_port *port)
258 unsigned char *data;
259 int result;
260 int count;
261 unsigned long flags;
263 spin_lock_irqsave(&port->lock, flags);
264 if (port->write_urb_busy || !kfifo_len(&port->write_fifo)) {
265 spin_unlock_irqrestore(&port->lock, flags);
266 return 0;
268 port->write_urb_busy = 1;
269 spin_unlock_irqrestore(&port->lock, flags);
271 data = port->write_urb->transfer_buffer;
272 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock);
273 usb_serial_debug_data(debug, &port->dev, __func__, count, data);
275 port->write_urb->transfer_buffer_length = count;
277 /* send the data out the bulk port */
278 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
279 if (result) {
280 dev_err(&port->dev, "%s - error submitting urb: %d\n",
281 __func__, result);
282 /* don't have to grab the lock here, as we will
283 retry if != 0 */
284 port->write_urb_busy = 0;
285 return result;
288 spin_lock_irqsave(&port->lock, flags);
289 port->tx_bytes_flight += count;
290 spin_unlock_irqrestore(&port->lock, flags);
292 return count;
296 * usb_serial_generic_write - generic write function for serial USB devices
297 * @tty: Pointer to &struct tty_struct for the device
298 * @port: Pointer to the &usb_serial_port structure for the device
299 * @buf: Pointer to the data to write
300 * @count: Number of bytes to write
302 * Returns the number of characters actually written, which may be anything
303 * from zero to @count. If an error occurs, it returns the negative errno
304 * value.
306 int usb_serial_generic_write(struct tty_struct *tty,
307 struct usb_serial_port *port, const unsigned char *buf, int count)
309 struct usb_serial *serial = port->serial;
310 int result;
312 dbg("%s - port %d", __func__, port->number);
314 /* only do something if we have a bulk out endpoint */
315 if (!port->bulk_out_size)
316 return -ENODEV;
318 if (!count)
319 return 0;
321 if (serial->type->max_in_flight_urbs)
322 return usb_serial_multi_urb_write(tty, port,
323 buf, count);
325 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
326 result = usb_serial_generic_write_start(port);
328 if (result >= 0)
329 result = count;
331 return result;
333 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
335 int usb_serial_generic_write_room(struct tty_struct *tty)
337 struct usb_serial_port *port = tty->driver_data;
338 struct usb_serial *serial = port->serial;
339 unsigned long flags;
340 int room = 0;
342 dbg("%s - port %d", __func__, port->number);
344 if (!port->bulk_out_size)
345 return 0;
347 spin_lock_irqsave(&port->lock, flags);
348 if (serial->type->max_in_flight_urbs) {
349 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
350 room = port->bulk_out_size *
351 (serial->type->max_in_flight_urbs -
352 port->urbs_in_flight);
353 } else {
354 room = kfifo_avail(&port->write_fifo);
356 spin_unlock_irqrestore(&port->lock, flags);
358 dbg("%s - returns %d", __func__, room);
359 return room;
362 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
364 struct usb_serial_port *port = tty->driver_data;
365 struct usb_serial *serial = port->serial;
366 unsigned long flags;
367 int chars;
369 dbg("%s - port %d", __func__, port->number);
371 if (!port->bulk_out_size)
372 return 0;
374 spin_lock_irqsave(&port->lock, flags);
375 if (serial->type->max_in_flight_urbs)
376 chars = port->tx_bytes_flight;
377 else
378 chars = kfifo_len(&port->write_fifo) + port->tx_bytes_flight;
379 spin_unlock_irqrestore(&port->lock, flags);
381 dbg("%s - returns %d", __func__, chars);
382 return chars;
385 int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
386 gfp_t mem_flags)
388 int result;
390 result = usb_submit_urb(port->read_urb, mem_flags);
391 if (result && result != -EPERM) {
392 dev_err(&port->dev, "%s - error submitting urb: %d\n",
393 __func__, result);
395 return result;
397 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
399 void usb_serial_generic_process_read_urb(struct urb *urb)
401 struct usb_serial_port *port = urb->context;
402 struct tty_struct *tty;
403 char *ch = (char *)urb->transfer_buffer;
404 int i;
406 tty = tty_port_tty_get(&port->port);
407 if (!tty)
408 return;
410 /* The per character mucking around with sysrq path it too slow for
411 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
412 where the USB serial is not a console anyway */
413 if (!port->port.console || !port->sysrq)
414 tty_insert_flip_string(tty, ch, urb->actual_length);
415 else {
416 for (i = 0; i < urb->actual_length; i++, ch++) {
417 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
418 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
421 tty_flip_buffer_push(tty);
422 tty_kref_put(tty);
424 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
426 void usb_serial_generic_read_bulk_callback(struct urb *urb)
428 struct usb_serial_port *port = urb->context;
429 unsigned char *data = urb->transfer_buffer;
430 int status = urb->status;
431 unsigned long flags;
433 dbg("%s - port %d", __func__, port->number);
435 if (unlikely(status != 0)) {
436 dbg("%s - nonzero read bulk status received: %d",
437 __func__, status);
438 return;
441 usb_serial_debug_data(debug, &port->dev, __func__,
442 urb->actual_length, data);
443 port->serial->type->process_read_urb(urb);
445 /* Throttle the device if requested by tty */
446 spin_lock_irqsave(&port->lock, flags);
447 port->throttled = port->throttle_req;
448 if (!port->throttled) {
449 spin_unlock_irqrestore(&port->lock, flags);
450 usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
451 } else
452 spin_unlock_irqrestore(&port->lock, flags);
454 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
456 void usb_serial_generic_write_bulk_callback(struct urb *urb)
458 unsigned long flags;
459 struct usb_serial_port *port = urb->context;
460 int status = urb->status;
462 dbg("%s - port %d", __func__, port->number);
464 if (port->serial->type->max_in_flight_urbs) {
465 kfree(urb->transfer_buffer);
467 spin_lock_irqsave(&port->lock, flags);
468 --port->urbs_in_flight;
469 port->tx_bytes_flight -= urb->transfer_buffer_length;
470 if (port->urbs_in_flight < 0)
471 port->urbs_in_flight = 0;
472 spin_unlock_irqrestore(&port->lock, flags);
473 } else {
474 spin_lock_irqsave(&port->lock, flags);
475 port->tx_bytes_flight -= urb->transfer_buffer_length;
476 port->write_urb_busy = 0;
477 spin_unlock_irqrestore(&port->lock, flags);
479 if (status) {
480 spin_lock_irqsave(&port->lock, flags);
481 kfifo_reset_out(&port->write_fifo);
482 spin_unlock_irqrestore(&port->lock, flags);
483 } else {
484 usb_serial_generic_write_start(port);
488 if (status)
489 dbg("%s - non-zero urb status: %d", __func__, status);
491 usb_serial_port_softint(port);
493 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
495 void usb_serial_generic_throttle(struct tty_struct *tty)
497 struct usb_serial_port *port = tty->driver_data;
498 unsigned long flags;
500 dbg("%s - port %d", __func__, port->number);
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);
508 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
510 void usb_serial_generic_unthrottle(struct tty_struct *tty)
512 struct usb_serial_port *port = tty->driver_data;
513 int was_throttled;
515 dbg("%s - port %d", __func__, port->number);
517 /* Clear the throttle flags */
518 spin_lock_irq(&port->lock);
519 was_throttled = port->throttled;
520 port->throttled = port->throttle_req = 0;
521 spin_unlock_irq(&port->lock);
523 if (was_throttled)
524 usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
526 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
528 #ifdef CONFIG_MAGIC_SYSRQ
529 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
530 struct usb_serial_port *port, unsigned int ch)
532 if (port->sysrq && port->port.console) {
533 if (ch && time_before(jiffies, port->sysrq)) {
534 handle_sysrq(ch, tty);
535 port->sysrq = 0;
536 return 1;
538 port->sysrq = 0;
540 return 0;
542 #else
543 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
544 struct usb_serial_port *port, unsigned int ch)
546 return 0;
548 #endif
549 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
551 int usb_serial_handle_break(struct usb_serial_port *port)
553 if (!port->sysrq) {
554 port->sysrq = jiffies + HZ*5;
555 return 1;
557 port->sysrq = 0;
558 return 0;
560 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
562 int usb_serial_generic_resume(struct usb_serial *serial)
564 struct usb_serial_port *port;
565 int i, c = 0, r;
567 for (i = 0; i < serial->num_ports; i++) {
568 port = serial->port[i];
569 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
570 continue;
572 if (port->read_urb) {
573 r = usb_submit_urb(port->read_urb, GFP_NOIO);
574 if (r < 0)
575 c++;
578 if (port->write_urb) {
579 r = usb_serial_generic_write_start(port);
580 if (r < 0)
581 c++;
585 return c ? -EIO : 0;
587 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
589 void usb_serial_generic_disconnect(struct usb_serial *serial)
591 int i;
593 dbg("%s", __func__);
595 /* stop reads and writes on all ports */
596 for (i = 0; i < serial->num_ports; ++i)
597 generic_cleanup(serial->port[i]);
600 void usb_serial_generic_release(struct usb_serial *serial)
602 dbg("%s", __func__);