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 driver
17 * remove unused code in empeg_close() (thanks to Oliver Neukum for pointing this
18 * out) and rewrote empeg_set_termios().
21 * switched from using spinlock to a semaphore, which fixes lots of problems.
24 * Identify version on module load.
27 * Added write_room() and chars_in_buffer() support.
30 * Moved termio stuff inside the port->active check.
31 * Moved MOD_DEC_USE_COUNT to end of empeg_close().
34 * Added port->port.tty->ldisc.set_termios(port->port.tty, NULL) to empeg_open()
35 * This notifies the tty driver that the termios have changed.
38 * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to empeg_open()
39 * (It only needs to be set once - Doh!)
42 * Updated to work with id_table structure.
45 * Forked this from visor.c, and hacked it up to work with an
46 * Empeg ltd. empeg-car player. Constructive criticism welcomed.
47 * I would like to say, 'Thank You' to Greg Kroah-Hartman for the
48 * use of his code, and for his guidance, advice and patience. :)
49 * A 'Thank You' is in order for John Ripley of Empeg ltd for his
50 * advice, and patience too.
54 #include <linux/kernel.h>
55 #include <linux/errno.h>
56 #include <linux/init.h>
57 #include <linux/slab.h>
58 #include <linux/tty.h>
59 #include <linux/tty_driver.h>
60 #include <linux/tty_flip.h>
61 #include <linux/module.h>
62 #include <linux/spinlock.h>
63 #include <asm/uaccess.h>
64 #include <linux/usb.h>
65 #include <linux/usb/serial.h>
72 #define DRIVER_VERSION "v1.2"
73 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
74 #define DRIVER_DESC "USB Empeg Mark I/II Driver"
76 #define EMPEG_VENDOR_ID 0x084f
77 #define EMPEG_PRODUCT_ID 0x0001
79 /* function prototypes for an empeg-car player */
80 static int empeg_open (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
81 static void empeg_close (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
82 static int empeg_write (struct tty_struct *tty, struct usb_serial_port *port,
83 const unsigned char *buf,
85 static int empeg_write_room (struct tty_struct *tty);
86 static int empeg_chars_in_buffer (struct tty_struct *tty);
87 static void empeg_throttle (struct tty_struct *tty);
88 static void empeg_unthrottle (struct tty_struct *tty);
89 static int empeg_startup (struct usb_serial *serial);
90 static void empeg_shutdown (struct usb_serial *serial);
91 static void empeg_set_termios (struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios);
92 static void empeg_write_bulk_callback (struct urb *urb);
93 static void empeg_read_bulk_callback (struct urb *urb);
95 static struct usb_device_id id_table [] = {
96 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
97 { } /* Terminating entry */
100 MODULE_DEVICE_TABLE (usb, id_table);
102 static struct usb_driver empeg_driver = {
104 .probe = usb_serial_probe,
105 .disconnect = usb_serial_disconnect,
106 .id_table = id_table,
110 static struct usb_serial_driver empeg_device = {
112 .owner = THIS_MODULE,
115 .id_table = id_table,
116 .usb_driver = &empeg_driver,
119 .close = empeg_close,
120 .throttle = empeg_throttle,
121 .unthrottle = empeg_unthrottle,
122 .attach = empeg_startup,
123 .shutdown = empeg_shutdown,
124 .set_termios = empeg_set_termios,
125 .write = empeg_write,
126 .write_room = empeg_write_room,
127 .chars_in_buffer = empeg_chars_in_buffer,
128 .write_bulk_callback = empeg_write_bulk_callback,
129 .read_bulk_callback = empeg_read_bulk_callback,
133 #define URB_TRANSFER_BUFFER_SIZE 4096
135 static struct urb *write_urb_pool[NUM_URBS];
136 static spinlock_t write_urb_pool_lock;
138 static int bytes_out;
140 /******************************************************************************
141 * Empeg specific driver functions
142 ******************************************************************************/
143 static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
146 struct usb_serial *serial = port->serial;
149 dbg("%s - port %d", __func__, port->number);
151 /* Force default termio settings */
152 empeg_set_termios (tty, port, NULL) ;
157 /* Start reading from the device */
161 usb_rcvbulkpipe(serial->dev,
162 port->bulk_in_endpointAddress),
163 port->read_urb->transfer_buffer,
164 port->read_urb->transfer_buffer_length,
165 empeg_read_bulk_callback,
168 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
171 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __func__, result);
177 static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port,
180 dbg("%s - port %d", __func__, port->number);
182 /* shutdown our bulk read */
183 usb_kill_urb(port->read_urb);
184 /* Uncomment the following line if you want to see some statistics in your syslog */
185 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
189 static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count)
191 struct usb_serial *serial = port->serial;
193 const unsigned char *current_position = buf;
200 dbg("%s - port %d", __func__, port->number);
203 /* try to find a free urb in our list of them */
206 spin_lock_irqsave (&write_urb_pool_lock, flags);
208 for (i = 0; i < NUM_URBS; ++i) {
209 if (write_urb_pool[i]->status != -EINPROGRESS) {
210 urb = write_urb_pool[i];
215 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
218 dbg("%s - no more free urbs", __func__);
222 if (urb->transfer_buffer == NULL) {
223 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
224 if (urb->transfer_buffer == NULL) {
225 dev_err(&port->dev, "%s no more kernel memory...\n", __func__);
230 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
232 memcpy (urb->transfer_buffer, current_position, transfer_size);
234 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer);
236 /* build up our urb */
240 usb_sndbulkpipe(serial->dev,
241 port->bulk_out_endpointAddress),
242 urb->transfer_buffer,
244 empeg_write_bulk_callback,
247 /* send it down the pipe */
248 status = usb_submit_urb(urb, GFP_ATOMIC);
250 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
255 current_position += transfer_size;
256 bytes_sent += transfer_size;
257 count -= transfer_size;
258 bytes_out += transfer_size;
266 static int empeg_write_room(struct tty_struct *tty)
268 struct usb_serial_port *port = tty->driver_data;
273 dbg("%s - port %d", __func__, port->number);
275 spin_lock_irqsave (&write_urb_pool_lock, flags);
276 /* tally up the number of bytes available */
277 for (i = 0; i < NUM_URBS; ++i) {
278 if (write_urb_pool[i]->status != -EINPROGRESS) {
279 room += URB_TRANSFER_BUFFER_SIZE;
282 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
283 dbg("%s - returns %d", __func__, room);
289 static int empeg_chars_in_buffer(struct tty_struct *tty)
291 struct usb_serial_port *port = tty->driver_data;
296 dbg("%s - port %d", __func__, port->number);
298 spin_lock_irqsave (&write_urb_pool_lock, flags);
300 /* tally up the number of bytes waiting */
301 for (i = 0; i < NUM_URBS; ++i) {
302 if (write_urb_pool[i]->status == -EINPROGRESS) {
303 chars += URB_TRANSFER_BUFFER_SIZE;
307 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
309 dbg("%s - returns %d", __func__, chars);
316 static void empeg_write_bulk_callback (struct urb *urb)
318 struct usb_serial_port *port = urb->context;
319 int status = urb->status;
321 dbg("%s - port %d", __func__, port->number);
324 dbg("%s - nonzero write bulk status received: %d",
329 usb_serial_port_softint(port);
333 static void empeg_read_bulk_callback (struct urb *urb)
335 struct usb_serial_port *port = urb->context;
336 struct tty_struct *tty;
337 unsigned char *data = urb->transfer_buffer;
339 int status = urb->status;
341 dbg("%s - port %d", __func__, port->number);
344 dbg("%s - nonzero read bulk status received: %d",
349 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
351 tty = port->port.tty;
353 if (urb->actual_length) {
354 tty_buffer_request_room(tty, urb->actual_length);
355 tty_insert_flip_string(tty, data, urb->actual_length);
356 tty_flip_buffer_push(tty);
357 bytes_in += urb->actual_length;
360 /* Continue trying to always read */
364 usb_rcvbulkpipe(port->serial->dev,
365 port->bulk_in_endpointAddress),
366 port->read_urb->transfer_buffer,
367 port->read_urb->transfer_buffer_length,
368 empeg_read_bulk_callback,
371 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
374 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
381 static void empeg_throttle(struct tty_struct *tty)
383 struct usb_serial_port *port = tty->driver_data;
384 dbg("%s - port %d", __func__, port->number);
385 usb_kill_urb(port->read_urb);
389 static void empeg_unthrottle(struct tty_struct *tty)
391 struct usb_serial_port *port = tty->driver_data;
393 dbg("%s - port %d", __func__, port->number);
395 port->read_urb->dev = port->serial->dev;
396 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
398 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __func__, result);
402 static int empeg_startup (struct usb_serial *serial)
408 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
409 err("active config #%d != 1 ??",
410 serial->dev->actconfig->desc.bConfigurationValue);
413 dbg("%s - reset config", __func__);
414 r = usb_reset_configuration (serial->dev);
416 /* continue on with initialization */
422 static void empeg_shutdown (struct usb_serial *serial)
424 dbg ("%s", __func__);
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.
441 * The default requirements for this device are:
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 */
454 &= ~OPOST; /* disable postprocess output characters */
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 */
464 &= ~(CSIZE /* no size */
465 | PARENB /* disable parity bit */
466 | CBAUD); /* clear current baud rate */
469 |= CS8; /* character size 8 bits */
472 * Force low_latency on; otherwise the pushes are scheduled;
473 * this is bad as it opens up the possibility of dropping bytes
474 * on the floor. We don't want to drop bytes on the floor. :)
476 tty->low_latency = 1;
477 tty_encode_baud_rate(tty, 115200, 115200);
481 static int __init empeg_init (void)
486 /* create our write urb pool and transfer buffers */
487 spin_lock_init (&write_urb_pool_lock);
488 for (i = 0; i < NUM_URBS; ++i) {
489 urb = usb_alloc_urb(0, GFP_KERNEL);
490 write_urb_pool[i] = urb;
492 err("No more urbs???");
496 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
497 if (!urb->transfer_buffer) {
498 err("%s - out of memory for urb buffers.",
504 retval = usb_serial_register(&empeg_device);
506 goto failed_usb_serial_register;
507 retval = usb_register(&empeg_driver);
509 goto failed_usb_register;
511 info(DRIVER_VERSION ":" DRIVER_DESC);
515 usb_serial_deregister(&empeg_device);
516 failed_usb_serial_register:
517 for (i = 0; i < NUM_URBS; ++i) {
518 if (write_urb_pool[i]) {
519 kfree(write_urb_pool[i]->transfer_buffer);
520 usb_free_urb(write_urb_pool[i]);
527 static void __exit empeg_exit (void)
532 usb_deregister(&empeg_driver);
533 usb_serial_deregister (&empeg_device);
535 spin_lock_irqsave (&write_urb_pool_lock, flags);
537 for (i = 0; i < NUM_URBS; ++i) {
538 if (write_urb_pool[i]) {
539 /* FIXME - uncomment the following usb_kill_urb call when
540 * the host controllers get fixed to set urb->dev = NULL after
541 * the urb is finished. Otherwise this call oopses. */
542 /* usb_kill_urb(write_urb_pool[i]); */
543 kfree(write_urb_pool[i]->transfer_buffer);
544 usb_free_urb (write_urb_pool[i]);
548 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
552 module_init(empeg_init);
553 module_exit(empeg_exit);
555 MODULE_AUTHOR( DRIVER_AUTHOR );
556 MODULE_DESCRIPTION( DRIVER_DESC );
557 MODULE_LICENSE("GPL");
559 module_param(debug, bool, S_IRUGO | S_IWUSR);
560 MODULE_PARM_DESC(debug, "Debug enabled or not");