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
18 * remove unused code in empeg_close() (thanks to Oliver Neukum for
19 * pointing this out) and rewrote empeg_set_termios().
22 * switched from using spinlock to a semaphore, which fixes lots of
26 * Identify version on module load.
29 * Added write_room() and chars_in_buffer() support.
32 * Moved termio stuff inside the port->active check.
33 * Moved MOD_DEC_USE_COUNT to end of empeg_close().
36 * Added tty->ldisc.set_termios(port, tty, NULL) to empeg_open().
37 * This notifies the tty driver that the termios have changed.
40 * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to
41 * empeg_open() (It only needs to be set once - Doh!)
44 * Updated to work with id_table structure.
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>
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
,
84 static void empeg_close(struct tty_struct
*tty
, struct usb_serial_port
*port
,
86 static int empeg_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
87 const unsigned char *buf
,
89 static int empeg_write_room(struct tty_struct
*tty
);
90 static int empeg_chars_in_buffer(struct tty_struct
*tty
);
91 static void empeg_throttle(struct tty_struct
*tty
);
92 static void empeg_unthrottle(struct tty_struct
*tty
);
93 static int empeg_startup(struct usb_serial
*serial
);
94 static void empeg_shutdown(struct usb_serial
*serial
);
95 static void empeg_set_termios(struct tty_struct
*tty
,
96 struct usb_serial_port
*port
, struct ktermios
*old_termios
);
97 static void empeg_write_bulk_callback(struct urb
*urb
);
98 static void empeg_read_bulk_callback(struct urb
*urb
);
100 static struct usb_device_id id_table
[] = {
101 { USB_DEVICE(EMPEG_VENDOR_ID
, EMPEG_PRODUCT_ID
) },
102 { } /* Terminating entry */
105 MODULE_DEVICE_TABLE(usb
, id_table
);
107 static struct usb_driver empeg_driver
= {
109 .probe
= usb_serial_probe
,
110 .disconnect
= usb_serial_disconnect
,
111 .id_table
= id_table
,
115 static struct usb_serial_driver empeg_device
= {
117 .owner
= THIS_MODULE
,
120 .id_table
= id_table
,
121 .usb_driver
= &empeg_driver
,
124 .close
= empeg_close
,
125 .throttle
= empeg_throttle
,
126 .unthrottle
= empeg_unthrottle
,
127 .attach
= empeg_startup
,
128 .shutdown
= empeg_shutdown
,
129 .set_termios
= empeg_set_termios
,
130 .write
= empeg_write
,
131 .write_room
= empeg_write_room
,
132 .chars_in_buffer
= empeg_chars_in_buffer
,
133 .write_bulk_callback
= empeg_write_bulk_callback
,
134 .read_bulk_callback
= empeg_read_bulk_callback
,
138 #define URB_TRANSFER_BUFFER_SIZE 4096
140 static struct urb
*write_urb_pool
[NUM_URBS
];
141 static spinlock_t write_urb_pool_lock
;
143 static int bytes_out
;
145 /******************************************************************************
146 * Empeg specific driver functions
147 ******************************************************************************/
148 static int empeg_open(struct tty_struct
*tty
, struct usb_serial_port
*port
,
151 struct usb_serial
*serial
= port
->serial
;
154 dbg("%s - port %d", __func__
, port
->number
);
156 /* Force default termio settings */
157 empeg_set_termios(tty
, port
, NULL
) ;
162 /* Start reading from the device */
166 usb_rcvbulkpipe(serial
->dev
,
167 port
->bulk_in_endpointAddress
),
168 port
->read_urb
->transfer_buffer
,
169 port
->read_urb
->transfer_buffer_length
,
170 empeg_read_bulk_callback
,
173 result
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
177 "%s - failed submitting read urb, error %d\n",
184 static void empeg_close(struct tty_struct
*tty
, struct usb_serial_port
*port
,
187 dbg("%s - port %d", __func__
, port
->number
);
189 /* shutdown our bulk read */
190 usb_kill_urb(port
->read_urb
);
191 /* Uncomment the following line if you want to see some statistics in your syslog */
192 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
196 static int empeg_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
197 const unsigned char *buf
, int count
)
199 struct usb_serial
*serial
= port
->serial
;
201 const unsigned char *current_position
= buf
;
208 dbg("%s - port %d", __func__
, port
->number
);
211 /* try to find a free urb in our list of them */
214 spin_lock_irqsave(&write_urb_pool_lock
, flags
);
216 for (i
= 0; i
< NUM_URBS
; ++i
) {
217 if (write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
218 urb
= write_urb_pool
[i
];
223 spin_unlock_irqrestore(&write_urb_pool_lock
, flags
);
226 dbg("%s - no more free urbs", __func__
);
230 if (urb
->transfer_buffer
== NULL
) {
231 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_ATOMIC
);
232 if (urb
->transfer_buffer
== NULL
) {
234 "%s no more kernel memory...\n",
240 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
242 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
244 usb_serial_debug_data(debug
, &port
->dev
, __func__
, transfer_size
, urb
->transfer_buffer
);
246 /* build up our urb */
250 usb_sndbulkpipe(serial
->dev
,
251 port
->bulk_out_endpointAddress
),
252 urb
->transfer_buffer
,
254 empeg_write_bulk_callback
,
257 /* send it down the pipe */
258 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
260 dev_err(&port
->dev
, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__
, status
);
265 current_position
+= transfer_size
;
266 bytes_sent
+= transfer_size
;
267 count
-= transfer_size
;
268 bytes_out
+= transfer_size
;
276 static int empeg_write_room(struct tty_struct
*tty
)
278 struct usb_serial_port
*port
= tty
->driver_data
;
283 dbg("%s - port %d", __func__
, port
->number
);
285 spin_lock_irqsave(&write_urb_pool_lock
, flags
);
286 /* tally up the number of bytes available */
287 for (i
= 0; i
< NUM_URBS
; ++i
) {
288 if (write_urb_pool
[i
]->status
!= -EINPROGRESS
)
289 room
+= URB_TRANSFER_BUFFER_SIZE
;
291 spin_unlock_irqrestore(&write_urb_pool_lock
, flags
);
292 dbg("%s - returns %d", __func__
, room
);
298 static int empeg_chars_in_buffer(struct tty_struct
*tty
)
300 struct usb_serial_port
*port
= tty
->driver_data
;
305 dbg("%s - port %d", __func__
, port
->number
);
307 spin_lock_irqsave(&write_urb_pool_lock
, flags
);
309 /* tally up the number of bytes waiting */
310 for (i
= 0; i
< NUM_URBS
; ++i
) {
311 if (write_urb_pool
[i
]->status
== -EINPROGRESS
)
312 chars
+= URB_TRANSFER_BUFFER_SIZE
;
315 spin_unlock_irqrestore(&write_urb_pool_lock
, flags
);
316 dbg("%s - returns %d", __func__
, chars
);
321 static void empeg_write_bulk_callback(struct urb
*urb
)
323 struct usb_serial_port
*port
= urb
->context
;
324 int status
= urb
->status
;
326 dbg("%s - port %d", __func__
, port
->number
);
329 dbg("%s - nonzero write bulk status received: %d",
334 usb_serial_port_softint(port
);
338 static void empeg_read_bulk_callback(struct urb
*urb
)
340 struct usb_serial_port
*port
= urb
->context
;
341 struct tty_struct
*tty
;
342 unsigned char *data
= urb
->transfer_buffer
;
344 int status
= urb
->status
;
346 dbg("%s - port %d", __func__
, port
->number
);
349 dbg("%s - nonzero read bulk status received: %d",
354 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
355 urb
->actual_length
, data
);
356 tty
= tty_port_tty_get(&port
->port
);
358 if (urb
->actual_length
) {
359 tty_buffer_request_room(tty
, urb
->actual_length
);
360 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
361 tty_flip_buffer_push(tty
);
362 bytes_in
+= urb
->actual_length
;
366 /* Continue trying to always read */
370 usb_rcvbulkpipe(port
->serial
->dev
,
371 port
->bulk_in_endpointAddress
),
372 port
->read_urb
->transfer_buffer
,
373 port
->read_urb
->transfer_buffer_length
,
374 empeg_read_bulk_callback
,
377 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
380 dev_err(&urb
->dev
->dev
,
381 "%s - failed resubmitting read urb, error %d\n",
389 static void empeg_throttle(struct tty_struct
*tty
)
391 struct usb_serial_port
*port
= tty
->driver_data
;
392 dbg("%s - port %d", __func__
, port
->number
);
393 usb_kill_urb(port
->read_urb
);
397 static void empeg_unthrottle(struct tty_struct
*tty
)
399 struct usb_serial_port
*port
= tty
->driver_data
;
401 dbg("%s - port %d", __func__
, port
->number
);
403 port
->read_urb
->dev
= port
->serial
->dev
;
404 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
407 "%s - failed submitting read urb, error %d\n",
412 static int empeg_startup(struct usb_serial
*serial
)
418 if (serial
->dev
->actconfig
->desc
.bConfigurationValue
!= 1) {
419 err("active config #%d != 1 ??",
420 serial
->dev
->actconfig
->desc
.bConfigurationValue
);
423 dbg("%s - reset config", __func__
);
424 r
= usb_reset_configuration(serial
->dev
);
426 /* continue on with initialization */
432 static void empeg_shutdown(struct usb_serial
*serial
)
438 static void empeg_set_termios(struct tty_struct
*tty
,
439 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
441 struct ktermios
*termios
= tty
->termios
;
442 dbg("%s - port %d", __func__
, port
->number
);
445 * The empeg-car player wants these particular tty settings.
446 * You could, for example, change the baud rate, however the
447 * player only supports 115200 (currently), so there is really
448 * no point in support for changes to the tty settings.
451 * The default requirements for this device are:
454 &= ~(IGNBRK
/* disable ignore break */
455 | BRKINT
/* disable break causes interrupt */
456 | PARMRK
/* disable mark parity errors */
457 | ISTRIP
/* disable clear high bit of input characters */
458 | INLCR
/* disable translate NL to CR */
459 | IGNCR
/* disable ignore CR */
460 | ICRNL
/* disable translate CR to NL */
461 | IXON
); /* disable enable XON/XOFF flow control */
464 &= ~OPOST
; /* disable postprocess output characters */
467 &= ~(ECHO
/* disable echo input characters */
468 | ECHONL
/* disable echo new line */
469 | ICANON
/* disable erase, kill, werase, and rprnt special characters */
470 | ISIG
/* disable interrupt, quit, and suspend special characters */
471 | IEXTEN
); /* disable non-POSIX special characters */
474 &= ~(CSIZE
/* no size */
475 | PARENB
/* disable parity bit */
476 | CBAUD
); /* clear current baud rate */
479 |= CS8
; /* character size 8 bits */
482 * Force low_latency on; otherwise the pushes are scheduled;
483 * this is bad as it opens up the possibility of dropping bytes
484 * on the floor. We don't want to drop bytes on the floor. :)
486 tty
->low_latency
= 1;
487 tty_encode_baud_rate(tty
, 115200, 115200);
491 static int __init
empeg_init(void)
496 /* create our write urb pool and transfer buffers */
497 spin_lock_init(&write_urb_pool_lock
);
498 for (i
= 0; i
< NUM_URBS
; ++i
) {
499 urb
= usb_alloc_urb(0, GFP_KERNEL
);
500 write_urb_pool
[i
] = urb
;
502 err("No more urbs???");
506 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
508 if (!urb
->transfer_buffer
) {
509 err("%s - out of memory for urb buffers.",
515 retval
= usb_serial_register(&empeg_device
);
517 goto failed_usb_serial_register
;
518 retval
= usb_register(&empeg_driver
);
520 goto failed_usb_register
;
522 info(DRIVER_VERSION
":" DRIVER_DESC
);
526 usb_serial_deregister(&empeg_device
);
527 failed_usb_serial_register
:
528 for (i
= 0; i
< NUM_URBS
; ++i
) {
529 if (write_urb_pool
[i
]) {
530 kfree(write_urb_pool
[i
]->transfer_buffer
);
531 usb_free_urb(write_urb_pool
[i
]);
538 static void __exit
empeg_exit(void)
543 usb_deregister(&empeg_driver
);
544 usb_serial_deregister(&empeg_device
);
546 spin_lock_irqsave(&write_urb_pool_lock
, flags
);
548 for (i
= 0; i
< NUM_URBS
; ++i
) {
549 if (write_urb_pool
[i
]) {
550 /* FIXME - uncomment the following usb_kill_urb call
551 * when the host controllers get fixed to set urb->dev
552 * = NULL after the urb is finished. Otherwise this
554 /* usb_kill_urb(write_urb_pool[i]); */
555 kfree(write_urb_pool
[i
]->transfer_buffer
);
556 usb_free_urb(write_urb_pool
[i
]);
559 spin_unlock_irqrestore(&write_urb_pool_lock
, flags
);
563 module_init(empeg_init
);
564 module_exit(empeg_exit
);
566 MODULE_AUTHOR(DRIVER_AUTHOR
);
567 MODULE_DESCRIPTION(DRIVER_DESC
);
568 MODULE_LICENSE("GPL");
570 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
571 MODULE_PARM_DESC(debug
, "Debug enabled or not");