printk removal, receive reordering bugfix
[cor_2_6_31.git] / drivers / usb / serial / empeg.c
blob80cb3471adbe0fe64cae662184aef149180a942b
1 /*
2 * USB Empeg empeg-car player driver
4 * Copyright (C) 2000, 2001
5 * Gary Brubaker (xavyer@ix.netcom.com)
7 * Copyright (C) 1999 - 2001
8 * Greg Kroah-Hartman (greg@kroah.com)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License, as published by
12 * the Free Software Foundation, version 2.
14 * See Documentation/usb/usb-serial.txt for more information on using this
15 * driver
17 * (07/16/2001) gb
18 * remove unused code in empeg_close() (thanks to Oliver Neukum for
19 * pointing this out) and rewrote empeg_set_termios().
21 * (05/30/2001) gkh
22 * switched from using spinlock to a semaphore, which fixes lots of
23 * problems.
25 * (04/08/2001) gb
26 * Identify version on module load.
28 * (01/22/2001) gb
29 * Added write_room() and chars_in_buffer() support.
31 * (12/21/2000) gb
32 * Moved termio stuff inside the port->active check.
33 * Moved MOD_DEC_USE_COUNT to end of empeg_close().
35 * (12/03/2000) gb
36 * Added tty->ldisc.set_termios(port, tty, NULL) to empeg_open().
37 * This notifies the tty driver that the termios have changed.
39 * (11/13/2000) gb
40 * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to
41 * empeg_open() (It only needs to be set once - Doh!)
43 * (11/11/2000) gb
44 * Updated to work with id_table structure.
46 * (11/04/2000) gb
47 * Forked this from visor.c, and hacked it up to work with an
48 * Empeg ltd. empeg-car player. Constructive criticism welcomed.
49 * I would like to say, 'Thank You' to Greg Kroah-Hartman for the
50 * use of his code, and for his guidance, advice and patience. :)
51 * A 'Thank You' is in order for John Ripley of Empeg ltd for his
52 * advice, and patience too.
56 #include <linux/kernel.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
60 #include <linux/tty.h>
61 #include <linux/tty_driver.h>
62 #include <linux/tty_flip.h>
63 #include <linux/module.h>
64 #include <linux/spinlock.h>
65 #include <linux/uaccess.h>
66 #include <linux/usb.h>
67 #include <linux/usb/serial.h>
69 static int debug;
72 * Version Information
74 #define DRIVER_VERSION "v1.2"
75 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
76 #define DRIVER_DESC "USB Empeg Mark I/II Driver"
78 #define EMPEG_VENDOR_ID 0x084f
79 #define EMPEG_PRODUCT_ID 0x0001
81 /* function prototypes for an empeg-car player */
82 static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
83 struct file *filp);
84 static void empeg_close(struct usb_serial_port *port);
85 static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
86 const unsigned char *buf,
87 int count);
88 static int empeg_write_room(struct tty_struct *tty);
89 static int empeg_chars_in_buffer(struct tty_struct *tty);
90 static void empeg_throttle(struct tty_struct *tty);
91 static void empeg_unthrottle(struct tty_struct *tty);
92 static int empeg_startup(struct usb_serial *serial);
93 static void empeg_set_termios(struct tty_struct *tty,
94 struct usb_serial_port *port, struct ktermios *old_termios);
95 static void empeg_write_bulk_callback(struct urb *urb);
96 static void empeg_read_bulk_callback(struct urb *urb);
98 static struct usb_device_id id_table [] = {
99 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
100 { } /* Terminating entry */
103 MODULE_DEVICE_TABLE(usb, id_table);
105 static struct usb_driver empeg_driver = {
106 .name = "empeg",
107 .probe = usb_serial_probe,
108 .disconnect = usb_serial_disconnect,
109 .id_table = id_table,
110 .no_dynamic_id = 1,
113 static struct usb_serial_driver empeg_device = {
114 .driver = {
115 .owner = THIS_MODULE,
116 .name = "empeg",
118 .id_table = id_table,
119 .usb_driver = &empeg_driver,
120 .num_ports = 1,
121 .open = empeg_open,
122 .close = empeg_close,
123 .throttle = empeg_throttle,
124 .unthrottle = empeg_unthrottle,
125 .attach = empeg_startup,
126 .set_termios = empeg_set_termios,
127 .write = empeg_write,
128 .write_room = empeg_write_room,
129 .chars_in_buffer = empeg_chars_in_buffer,
130 .write_bulk_callback = empeg_write_bulk_callback,
131 .read_bulk_callback = empeg_read_bulk_callback,
134 #define NUM_URBS 16
135 #define URB_TRANSFER_BUFFER_SIZE 4096
137 static struct urb *write_urb_pool[NUM_URBS];
138 static spinlock_t write_urb_pool_lock;
139 static int bytes_in;
140 static int bytes_out;
142 /******************************************************************************
143 * Empeg specific driver functions
144 ******************************************************************************/
145 static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
146 struct file *filp)
148 struct usb_serial *serial = port->serial;
149 int result = 0;
151 dbg("%s - port %d", __func__, port->number);
153 /* Force default termio settings */
154 empeg_set_termios(tty, port, NULL) ;
156 bytes_in = 0;
157 bytes_out = 0;
159 /* Start reading from the device */
160 usb_fill_bulk_urb(
161 port->read_urb,
162 serial->dev,
163 usb_rcvbulkpipe(serial->dev,
164 port->bulk_in_endpointAddress),
165 port->read_urb->transfer_buffer,
166 port->read_urb->transfer_buffer_length,
167 empeg_read_bulk_callback,
168 port);
170 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
172 if (result)
173 dev_err(&port->dev,
174 "%s - failed submitting read urb, error %d\n",
175 __func__, result);
177 return result;
181 static void empeg_close(struct usb_serial_port *port)
183 dbg("%s - port %d", __func__, port->number);
185 /* shutdown our bulk read */
186 usb_kill_urb(port->read_urb);
187 /* Uncomment the following line if you want to see some statistics in your syslog */
188 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
192 static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
193 const unsigned char *buf, int count)
195 struct usb_serial *serial = port->serial;
196 struct urb *urb;
197 const unsigned char *current_position = buf;
198 unsigned long flags;
199 int status;
200 int i;
201 int bytes_sent = 0;
202 int transfer_size;
204 dbg("%s - port %d", __func__, port->number);
206 while (count > 0) {
207 /* try to find a free urb in our list of them */
208 urb = NULL;
210 spin_lock_irqsave(&write_urb_pool_lock, flags);
212 for (i = 0; i < NUM_URBS; ++i) {
213 if (write_urb_pool[i]->status != -EINPROGRESS) {
214 urb = write_urb_pool[i];
215 break;
219 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
221 if (urb == NULL) {
222 dbg("%s - no more free urbs", __func__);
223 goto exit;
226 if (urb->transfer_buffer == NULL) {
227 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
228 if (urb->transfer_buffer == NULL) {
229 dev_err(&port->dev,
230 "%s no more kernel memory...\n",
231 __func__);
232 goto exit;
236 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
238 memcpy(urb->transfer_buffer, current_position, transfer_size);
240 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer);
242 /* build up our urb */
243 usb_fill_bulk_urb(
244 urb,
245 serial->dev,
246 usb_sndbulkpipe(serial->dev,
247 port->bulk_out_endpointAddress),
248 urb->transfer_buffer,
249 transfer_size,
250 empeg_write_bulk_callback,
251 port);
253 /* send it down the pipe */
254 status = usb_submit_urb(urb, GFP_ATOMIC);
255 if (status) {
256 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
257 bytes_sent = status;
258 break;
261 current_position += transfer_size;
262 bytes_sent += transfer_size;
263 count -= transfer_size;
264 bytes_out += transfer_size;
267 exit:
268 return bytes_sent;
272 static int empeg_write_room(struct tty_struct *tty)
274 struct usb_serial_port *port = tty->driver_data;
275 unsigned long flags;
276 int i;
277 int room = 0;
279 dbg("%s - port %d", __func__, port->number);
281 spin_lock_irqsave(&write_urb_pool_lock, flags);
282 /* tally up the number of bytes available */
283 for (i = 0; i < NUM_URBS; ++i) {
284 if (write_urb_pool[i]->status != -EINPROGRESS)
285 room += URB_TRANSFER_BUFFER_SIZE;
287 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
288 dbg("%s - returns %d", __func__, room);
289 return room;
294 static int empeg_chars_in_buffer(struct tty_struct *tty)
296 struct usb_serial_port *port = tty->driver_data;
297 unsigned long flags;
298 int i;
299 int chars = 0;
301 dbg("%s - port %d", __func__, port->number);
303 spin_lock_irqsave(&write_urb_pool_lock, flags);
305 /* tally up the number of bytes waiting */
306 for (i = 0; i < NUM_URBS; ++i) {
307 if (write_urb_pool[i]->status == -EINPROGRESS)
308 chars += URB_TRANSFER_BUFFER_SIZE;
311 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
312 dbg("%s - returns %d", __func__, chars);
313 return chars;
317 static void empeg_write_bulk_callback(struct urb *urb)
319 struct usb_serial_port *port = urb->context;
320 int status = urb->status;
322 dbg("%s - port %d", __func__, port->number);
324 if (status) {
325 dbg("%s - nonzero write bulk status received: %d",
326 __func__, status);
327 return;
330 usb_serial_port_softint(port);
334 static void empeg_read_bulk_callback(struct urb *urb)
336 struct usb_serial_port *port = urb->context;
337 struct tty_struct *tty;
338 unsigned char *data = urb->transfer_buffer;
339 int result;
340 int status = urb->status;
342 dbg("%s - port %d", __func__, port->number);
344 if (status) {
345 dbg("%s - nonzero read bulk status received: %d",
346 __func__, status);
347 return;
350 usb_serial_debug_data(debug, &port->dev, __func__,
351 urb->actual_length, data);
352 tty = tty_port_tty_get(&port->port);
354 if (urb->actual_length) {
355 tty_buffer_request_room(tty, urb->actual_length);
356 tty_insert_flip_string(tty, data, urb->actual_length);
357 tty_flip_buffer_push(tty);
358 bytes_in += urb->actual_length;
360 tty_kref_put(tty);
362 /* Continue trying to always read */
363 usb_fill_bulk_urb(
364 port->read_urb,
365 port->serial->dev,
366 usb_rcvbulkpipe(port->serial->dev,
367 port->bulk_in_endpointAddress),
368 port->read_urb->transfer_buffer,
369 port->read_urb->transfer_buffer_length,
370 empeg_read_bulk_callback,
371 port);
373 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
375 if (result)
376 dev_err(&urb->dev->dev,
377 "%s - failed resubmitting read urb, error %d\n",
378 __func__, result);
380 return;
385 static void empeg_throttle(struct tty_struct *tty)
387 struct usb_serial_port *port = tty->driver_data;
388 dbg("%s - port %d", __func__, port->number);
389 usb_kill_urb(port->read_urb);
393 static void empeg_unthrottle(struct tty_struct *tty)
395 struct usb_serial_port *port = tty->driver_data;
396 int result;
397 dbg("%s - port %d", __func__, port->number);
399 port->read_urb->dev = port->serial->dev;
400 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
401 if (result)
402 dev_err(&port->dev,
403 "%s - failed submitting read urb, error %d\n",
404 __func__, result);
408 static int empeg_startup(struct usb_serial *serial)
410 int r;
412 dbg("%s", __func__);
414 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
415 dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
416 serial->dev->actconfig->desc.bConfigurationValue);
417 return -ENODEV;
419 dbg("%s - reset config", __func__);
420 r = usb_reset_configuration(serial->dev);
422 /* continue on with initialization */
423 return r;
428 static void empeg_set_termios(struct tty_struct *tty,
429 struct usb_serial_port *port, struct ktermios *old_termios)
431 struct ktermios *termios = tty->termios;
432 dbg("%s - port %d", __func__, port->number);
435 * The empeg-car player wants these particular tty settings.
436 * You could, for example, change the baud rate, however the
437 * player only supports 115200 (currently), so there is really
438 * no point in support for changes to the tty settings.
439 * (at least for now)
441 * The default requirements for this device are:
443 termios->c_iflag
444 &= ~(IGNBRK /* disable ignore break */
445 | BRKINT /* disable break causes interrupt */
446 | PARMRK /* disable mark parity errors */
447 | ISTRIP /* disable clear high bit of input characters */
448 | INLCR /* disable translate NL to CR */
449 | IGNCR /* disable ignore CR */
450 | ICRNL /* disable translate CR to NL */
451 | IXON); /* disable enable XON/XOFF flow control */
453 termios->c_oflag
454 &= ~OPOST; /* disable postprocess output characters */
456 termios->c_lflag
457 &= ~(ECHO /* disable echo input characters */
458 | ECHONL /* disable echo new line */
459 | ICANON /* disable erase, kill, werase, and rprnt special characters */
460 | ISIG /* disable interrupt, quit, and suspend special characters */
461 | IEXTEN); /* disable non-POSIX special characters */
463 termios->c_cflag
464 &= ~(CSIZE /* no size */
465 | PARENB /* disable parity bit */
466 | CBAUD); /* clear current baud rate */
468 termios->c_cflag
469 |= CS8; /* character size 8 bits */
471 tty_encode_baud_rate(tty, 115200, 115200);
475 static int __init empeg_init(void)
477 struct urb *urb;
478 int i, retval;
480 /* create our write urb pool and transfer buffers */
481 spin_lock_init(&write_urb_pool_lock);
482 for (i = 0; i < NUM_URBS; ++i) {
483 urb = usb_alloc_urb(0, GFP_KERNEL);
484 write_urb_pool[i] = urb;
485 if (urb == NULL) {
486 printk(KERN_ERR "empeg: No more urbs???\n");
487 continue;
490 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
491 GFP_KERNEL);
492 if (!urb->transfer_buffer) {
493 printk(KERN_ERR "empeg: %s - out of memory for urb "
494 "buffers.", __func__);
495 continue;
499 retval = usb_serial_register(&empeg_device);
500 if (retval)
501 goto failed_usb_serial_register;
502 retval = usb_register(&empeg_driver);
503 if (retval)
504 goto failed_usb_register;
506 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
507 DRIVER_DESC "\n");
509 return 0;
510 failed_usb_register:
511 usb_serial_deregister(&empeg_device);
512 failed_usb_serial_register:
513 for (i = 0; i < NUM_URBS; ++i) {
514 if (write_urb_pool[i]) {
515 kfree(write_urb_pool[i]->transfer_buffer);
516 usb_free_urb(write_urb_pool[i]);
519 return retval;
523 static void __exit empeg_exit(void)
525 int i;
526 unsigned long flags;
528 usb_deregister(&empeg_driver);
529 usb_serial_deregister(&empeg_device);
531 spin_lock_irqsave(&write_urb_pool_lock, flags);
533 for (i = 0; i < NUM_URBS; ++i) {
534 if (write_urb_pool[i]) {
535 /* FIXME - uncomment the following usb_kill_urb call
536 * when the host controllers get fixed to set urb->dev
537 * = NULL after the urb is finished. Otherwise this
538 * call oopses. */
539 /* usb_kill_urb(write_urb_pool[i]); */
540 kfree(write_urb_pool[i]->transfer_buffer);
541 usb_free_urb(write_urb_pool[i]);
544 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
548 module_init(empeg_init);
549 module_exit(empeg_exit);
551 MODULE_AUTHOR(DRIVER_AUTHOR);
552 MODULE_DESCRIPTION(DRIVER_DESC);
553 MODULE_LICENSE("GPL");
555 module_param(debug, bool, S_IRUGO | S_IWUSR);
556 MODULE_PARM_DESC(debug, "Debug enabled or not");