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->tty->ldisc.set_termios(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/config.h>
55 #include <linux/kernel.h>
56 #include <linux/errno.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #include <linux/tty.h>
60 #include <linux/tty_driver.h>
61 #include <linux/tty_flip.h>
62 #include <linux/module.h>
63 #include <linux/spinlock.h>
64 #include <asm/uaccess.h>
65 #include <linux/usb.h>
67 #ifdef CONFIG_USB_SERIAL_DEBUG
73 #include "usb-serial.h"
78 #define DRIVER_VERSION "v1.2"
79 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
80 #define DRIVER_DESC "USB Empeg Mark I/II Driver"
82 #define EMPEG_VENDOR_ID 0x084f
83 #define EMPEG_PRODUCT_ID 0x0001
85 /* function prototypes for an empeg-car player */
86 static int empeg_open (struct usb_serial_port *port, struct file *filp);
87 static void empeg_close (struct usb_serial_port *port, struct file *filp);
88 static int empeg_write (struct usb_serial_port *port,
90 const unsigned char *buf,
92 static int empeg_write_room (struct usb_serial_port *port);
93 static int empeg_chars_in_buffer (struct usb_serial_port *port);
94 static void empeg_throttle (struct usb_serial_port *port);
95 static void empeg_unthrottle (struct usb_serial_port *port);
96 static int empeg_startup (struct usb_serial *serial);
97 static void empeg_shutdown (struct usb_serial *serial);
98 static int empeg_ioctl (struct usb_serial_port *port,
102 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios);
103 static void empeg_write_bulk_callback (struct urb *urb);
104 static void empeg_read_bulk_callback (struct urb *urb);
106 static struct usb_device_id id_table [] = {
107 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
108 { } /* Terminating entry */
111 MODULE_DEVICE_TABLE (usb, id_table);
113 static struct usb_serial_device_type empeg_device = {
114 .owner = THIS_MODULE,
116 .id_table = id_table,
117 .num_interrupt_in = 0,
122 .close = empeg_close,
123 .throttle = empeg_throttle,
124 .unthrottle = empeg_unthrottle,
125 .startup = empeg_startup,
126 .shutdown = empeg_shutdown,
127 .ioctl = empeg_ioctl,
128 .set_termios = empeg_set_termios,
129 .write = empeg_write,
130 .write_room = empeg_write_room,
131 .chars_in_buffer = empeg_chars_in_buffer,
132 .write_bulk_callback = empeg_write_bulk_callback,
133 .read_bulk_callback = empeg_read_bulk_callback,
137 #define URB_TRANSFER_BUFFER_SIZE 4096
139 static struct urb *write_urb_pool[NUM_URBS];
140 static spinlock_t write_urb_pool_lock;
142 static int bytes_out;
144 /******************************************************************************
145 * Empeg specific driver functions
146 ******************************************************************************/
147 static int empeg_open (struct usb_serial_port *port, struct file *filp)
149 struct usb_serial *serial = port->serial;
152 if (port_paranoia_check (port, __FUNCTION__))
155 dbg("%s - port %d", __FUNCTION__, port->number);
157 /* Force default termio settings */
158 empeg_set_termios (port, NULL) ;
163 /* Start reading from the device */
167 usb_rcvbulkpipe(serial->dev,
168 port->bulk_in_endpointAddress),
169 port->read_urb->transfer_buffer,
170 port->read_urb->transfer_buffer_length,
171 empeg_read_bulk_callback,
174 port->read_urb->transfer_flags |= USB_QUEUE_BULK;
176 result = usb_submit_urb(port->read_urb);
179 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
185 static void empeg_close (struct usb_serial_port *port, struct file * filp)
187 struct usb_serial *serial;
189 if (port_paranoia_check (port, __FUNCTION__))
192 dbg("%s - port %d", __FUNCTION__, port->number);
194 serial = get_usb_serial (port, __FUNCTION__);
199 /* shutdown our bulk read */
200 usb_unlink_urb (port->read_urb);
202 /* Uncomment the following line if you want to see some statistics in your syslog */
203 /* info ("Bytes In = %d Bytes Out = %d", bytes_in, bytes_out); */
207 static int empeg_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
209 struct usb_serial *serial = port->serial;
211 const unsigned char *current_position = buf;
218 dbg("%s - port %d", __FUNCTION__, port->number);
220 usb_serial_debug_data (__FILE__, __FUNCTION__, count, buf);
224 /* try to find a free urb in our list of them */
227 spin_lock_irqsave (&write_urb_pool_lock, flags);
229 for (i = 0; i < NUM_URBS; ++i) {
230 if (write_urb_pool[i]->status != -EINPROGRESS) {
231 urb = write_urb_pool[i];
236 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
239 dbg("%s - no more free urbs", __FUNCTION__);
243 if (urb->transfer_buffer == NULL) {
244 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
245 if (urb->transfer_buffer == NULL) {
246 err("%s no more kernel memory...", __FUNCTION__);
251 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
254 if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
255 bytes_sent = -EFAULT;
259 memcpy (urb->transfer_buffer, current_position, transfer_size);
262 /* build up our urb */
266 usb_sndbulkpipe(serial->dev,
267 port->bulk_out_endpointAddress),
268 urb->transfer_buffer,
270 empeg_write_bulk_callback,
273 urb->transfer_flags |= USB_QUEUE_BULK;
275 /* send it down the pipe */
276 status = usb_submit_urb(urb);
278 err("%s - usb_submit_urb(write bulk) failed with status = %d", __FUNCTION__, status);
283 current_position += transfer_size;
284 bytes_sent += transfer_size;
285 count -= transfer_size;
286 bytes_out += transfer_size;
296 static int empeg_write_room (struct usb_serial_port *port)
302 dbg("%s - port %d", __FUNCTION__, port->number);
304 spin_lock_irqsave (&write_urb_pool_lock, flags);
306 /* tally up the number of bytes available */
307 for (i = 0; i < NUM_URBS; ++i) {
308 if (write_urb_pool[i]->status != -EINPROGRESS) {
309 room += URB_TRANSFER_BUFFER_SIZE;
313 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
315 dbg("%s - returns %d", __FUNCTION__, room);
322 static int empeg_chars_in_buffer (struct usb_serial_port *port)
328 dbg("%s - port %d", __FUNCTION__, port->number);
330 spin_lock_irqsave (&write_urb_pool_lock, flags);
332 /* tally up the number of bytes waiting */
333 for (i = 0; i < NUM_URBS; ++i) {
334 if (write_urb_pool[i]->status == -EINPROGRESS) {
335 chars += URB_TRANSFER_BUFFER_SIZE;
339 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
341 dbg("%s - returns %d", __FUNCTION__, chars);
348 static void empeg_write_bulk_callback (struct urb *urb)
350 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
352 if (port_paranoia_check (port, __FUNCTION__))
355 dbg("%s - port %d", __FUNCTION__, port->number);
358 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
362 queue_task(&port->tqueue, &tq_immediate);
363 mark_bh(IMMEDIATE_BH);
370 static void empeg_read_bulk_callback (struct urb *urb)
372 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
373 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
374 struct tty_struct *tty;
375 unsigned char *data = urb->transfer_buffer;
379 if (port_paranoia_check (port, __FUNCTION__))
382 dbg("%s - port %d", __FUNCTION__, port->number);
385 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
390 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
394 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
398 if (urb->actual_length) {
399 for (i = 0; i < urb->actual_length ; ++i) {
401 * If we insert too many characters we'll overflow the buffer.
402 * This means we'll lose bytes - Decidedly bad.
404 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
405 tty_flip_buffer_push(tty);
407 tty_insert_flip_char(tty, data[i], 0);
410 * Goes straight through instead of scheduling - if tty->low_latency is set.
412 tty_flip_buffer_push(tty);
413 bytes_in += urb->actual_length;
416 /* Continue trying to always read */
420 usb_rcvbulkpipe(serial->dev,
421 port->bulk_in_endpointAddress),
422 port->read_urb->transfer_buffer,
423 port->read_urb->transfer_buffer_length,
424 empeg_read_bulk_callback,
427 port->read_urb->transfer_flags |= USB_QUEUE_BULK;
429 result = usb_submit_urb(port->read_urb);
432 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
439 static void empeg_throttle (struct usb_serial_port *port)
441 dbg("%s - port %d", __FUNCTION__, port->number);
442 usb_unlink_urb (port->read_urb);
446 static void empeg_unthrottle (struct usb_serial_port *port)
450 dbg("%s - port %d", __FUNCTION__, port->number);
452 port->read_urb->dev = port->serial->dev;
454 result = usb_submit_urb(port->read_urb);
457 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
463 static int empeg_startup (struct usb_serial *serial)
466 dbg("%s", __FUNCTION__);
468 dbg("%s - Set config to 1", __FUNCTION__);
469 usb_set_configuration (serial->dev, 1);
471 /* continue on with initialization */
477 static void empeg_shutdown (struct usb_serial *serial)
479 dbg ("%s", __FUNCTION__);
483 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
485 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
491 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
494 dbg("%s - port %d", __FUNCTION__, port->number);
496 if ((!port->tty) || (!port->tty->termios)) {
497 dbg("%s - no tty structures", __FUNCTION__);
502 * The empeg-car player wants these particular tty settings.
503 * You could, for example, change the baud rate, however the
504 * player only supports 115200 (currently), so there is really
505 * no point in support for changes to the tty settings.
508 * The default requirements for this device are:
510 port->tty->termios->c_iflag
511 &= ~(IGNBRK /* disable ignore break */
512 | BRKINT /* disable break causes interrupt */
513 | PARMRK /* disable mark parity errors */
514 | ISTRIP /* disable clear high bit of input characters */
515 | INLCR /* disable translate NL to CR */
516 | IGNCR /* disable ignore CR */
517 | ICRNL /* disable translate CR to NL */
518 | IXON); /* disable enable XON/XOFF flow control */
520 port->tty->termios->c_oflag
521 &= ~OPOST; /* disable postprocess output characters */
523 port->tty->termios->c_lflag
524 &= ~(ECHO /* disable echo input characters */
525 | ECHONL /* disable echo new line */
526 | ICANON /* disable erase, kill, werase, and rprnt special characters */
527 | ISIG /* disable interrupt, quit, and suspend special characters */
528 | IEXTEN); /* disable non-POSIX special characters */
530 port->tty->termios->c_cflag
531 &= ~(CSIZE /* no size */
532 | PARENB /* disable parity bit */
533 | CBAUD); /* clear current baud rate */
535 port->tty->termios->c_cflag
536 |= (CS8 /* character size 8 bits */
537 | B115200); /* baud rate 115200 */
540 * Force low_latency on; otherwise the pushes are scheduled;
541 * this is bad as it opens up the possibility of dropping bytes
542 * on the floor. We don't want to drop bytes on the floor. :)
544 port->tty->low_latency = 1;
546 /* Notify the tty driver that the termios have changed. */
547 port->tty->ldisc.set_termios(port->tty, NULL);
554 static int __init empeg_init (void)
559 usb_serial_register (&empeg_device);
561 /* create our write urb pool and transfer buffers */
562 spin_lock_init (&write_urb_pool_lock);
563 for (i = 0; i < NUM_URBS; ++i) {
564 urb = usb_alloc_urb(0);
565 write_urb_pool[i] = urb;
567 err("No more urbs???");
571 urb->transfer_buffer = NULL;
572 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
573 if (!urb->transfer_buffer) {
574 err("%s - out of memory for urb buffers.",
580 info(DRIVER_VERSION ":" DRIVER_DESC);
587 static void __exit empeg_exit (void)
592 usb_serial_deregister (&empeg_device);
594 spin_lock_irqsave (&write_urb_pool_lock, flags);
596 for (i = 0; i < NUM_URBS; ++i) {
597 if (write_urb_pool[i]) {
598 /* FIXME - uncomment the following usb_unlink_urb call when
599 * the host controllers get fixed to set urb->dev = NULL after
600 * the urb is finished. Otherwise this call oopses. */
601 /* usb_unlink_urb(write_urb_pool[i]); */
602 if (write_urb_pool[i]->transfer_buffer)
603 kfree(write_urb_pool[i]->transfer_buffer);
604 usb_free_urb (write_urb_pool[i]);
608 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
613 module_init(empeg_init);
614 module_exit(empeg_exit);
616 MODULE_AUTHOR( DRIVER_AUTHOR );
617 MODULE_DESCRIPTION( DRIVER_DESC );
618 MODULE_LICENSE("GPL");
620 MODULE_PARM(debug, "i");
621 MODULE_PARM_DESC(debug, "Debug enabled or not");