Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / drivers / usb / serial / empeg.c
CommitLineData
1da177e4
LT
1/*
2 * USB Empeg empeg-car player driver
3 *
4 * Copyright (C) 2000, 2001
5 * Gary Brubaker (xavyer@ix.netcom.com)
6 *
7 * Copyright (C) 1999 - 2001
8 * Greg Kroah-Hartman (greg@kroah.com)
9 *
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.
13 *
93c46795
AC
14 * See Documentation/usb/usb-serial.txt for more information on using this
15 * driver
16 *
1da177e4 17 * (07/16/2001) gb
93c46795
AC
18 * remove unused code in empeg_close() (thanks to Oliver Neukum for
19 * pointing this out) and rewrote empeg_set_termios().
20 *
1da177e4 21 * (05/30/2001) gkh
93c46795
AC
22 * switched from using spinlock to a semaphore, which fixes lots of
23 * problems.
1da177e4
LT
24 *
25 * (04/08/2001) gb
26 * Identify version on module load.
93c46795 27 *
1da177e4 28 * (01/22/2001) gb
93c46795
AC
29 * Added write_room() and chars_in_buffer() support.
30 *
1da177e4
LT
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().
93c46795 34 *
1da177e4 35 * (12/03/2000) gb
93c46795
AC
36 * Added port->port.tty->ldisc.set_termios(port->port.tty, NULL) to
37 * empeg_open(). This notifies the tty driver that the termios have
38 * changed.
39 *
1da177e4 40 * (11/13/2000) gb
93c46795
AC
41 * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to
42 * empeg_open() (It only needs to be set once - Doh!)
43 *
1da177e4
LT
44 * (11/11/2000) gb
45 * Updated to work with id_table structure.
93c46795 46 *
1da177e4
LT
47 * (11/04/2000) gb
48 * Forked this from visor.c, and hacked it up to work with an
49 * Empeg ltd. empeg-car player. Constructive criticism welcomed.
50 * I would like to say, 'Thank You' to Greg Kroah-Hartman for the
51 * use of his code, and for his guidance, advice and patience. :)
52 * A 'Thank You' is in order for John Ripley of Empeg ltd for his
53 * advice, and patience too.
93c46795 54 *
1da177e4
LT
55 */
56
1da177e4
LT
57#include <linux/kernel.h>
58#include <linux/errno.h>
59#include <linux/init.h>
60#include <linux/slab.h>
61#include <linux/tty.h>
62#include <linux/tty_driver.h>
63#include <linux/tty_flip.h>
64#include <linux/module.h>
65#include <linux/spinlock.h>
93c46795 66#include <linux/uaccess.h>
1da177e4 67#include <linux/usb.h>
a969888c 68#include <linux/usb/serial.h>
1da177e4
LT
69
70static int debug;
71
72/*
73 * Version Information
74 */
75#define DRIVER_VERSION "v1.2"
76#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
77#define DRIVER_DESC "USB Empeg Mark I/II Driver"
78
79#define EMPEG_VENDOR_ID 0x084f
80#define EMPEG_PRODUCT_ID 0x0001
81
82/* function prototypes for an empeg-car player */
93c46795
AC
83static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
84 struct file *filp);
85static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port,
86 struct file *filp);
87static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
88 const unsigned char *buf,
89 int count);
90static int empeg_write_room(struct tty_struct *tty);
91static int empeg_chars_in_buffer(struct tty_struct *tty);
92static void empeg_throttle(struct tty_struct *tty);
93static void empeg_unthrottle(struct tty_struct *tty);
94static int empeg_startup(struct usb_serial *serial);
95static void empeg_shutdown(struct usb_serial *serial);
96static void empeg_set_termios(struct tty_struct *tty,
97 struct usb_serial_port *port, struct ktermios *old_termios);
98static void empeg_write_bulk_callback(struct urb *urb);
99static void empeg_read_bulk_callback(struct urb *urb);
1da177e4
LT
100
101static struct usb_device_id id_table [] = {
102 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
103 { } /* Terminating entry */
104};
105
93c46795 106MODULE_DEVICE_TABLE(usb, id_table);
1da177e4
LT
107
108static struct usb_driver empeg_driver = {
1da177e4
LT
109 .name = "empeg",
110 .probe = usb_serial_probe,
111 .disconnect = usb_serial_disconnect,
112 .id_table = id_table,
ba9dc657 113 .no_dynamic_id = 1,
1da177e4
LT
114};
115
ea65370d 116static struct usb_serial_driver empeg_device = {
18fcac35
GKH
117 .driver = {
118 .owner = THIS_MODULE,
269bda1c 119 .name = "empeg",
18fcac35 120 },
1da177e4 121 .id_table = id_table,
d9b1b787 122 .usb_driver = &empeg_driver,
1da177e4
LT
123 .num_ports = 1,
124 .open = empeg_open,
125 .close = empeg_close,
126 .throttle = empeg_throttle,
127 .unthrottle = empeg_unthrottle,
128 .attach = empeg_startup,
129 .shutdown = empeg_shutdown,
1da177e4
LT
130 .set_termios = empeg_set_termios,
131 .write = empeg_write,
132 .write_room = empeg_write_room,
133 .chars_in_buffer = empeg_chars_in_buffer,
134 .write_bulk_callback = empeg_write_bulk_callback,
135 .read_bulk_callback = empeg_read_bulk_callback,
136};
137
138#define NUM_URBS 16
139#define URB_TRANSFER_BUFFER_SIZE 4096
140
141static struct urb *write_urb_pool[NUM_URBS];
142static spinlock_t write_urb_pool_lock;
143static int bytes_in;
144static int bytes_out;
145
146/******************************************************************************
147 * Empeg specific driver functions
148 ******************************************************************************/
95da310e
AC
149static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
150 struct file *filp)
1da177e4
LT
151{
152 struct usb_serial *serial = port->serial;
153 int result = 0;
154
441b62c1 155 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
156
157 /* Force default termio settings */
93c46795 158 empeg_set_termios(tty, port, NULL) ;
1da177e4
LT
159
160 bytes_in = 0;
161 bytes_out = 0;
162
163 /* Start reading from the device */
164 usb_fill_bulk_urb(
165 port->read_urb,
93c46795 166 serial->dev,
1da177e4
LT
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,
172 port);
173
174 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
175
176 if (result)
93c46795
AC
177 dev_err(&port->dev,
178 "%s - failed submitting read urb, error %d\n",
179 __func__, result);
1da177e4
LT
180
181 return result;
182}
183
184
95da310e 185static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port,
93c46795 186 struct file *filp)
1da177e4 187{
441b62c1 188 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
189
190 /* shutdown our bulk read */
191 usb_kill_urb(port->read_urb);
192 /* Uncomment the following line if you want to see some statistics in your syslog */
193 /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
194}
195
196
93c46795
AC
197static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
198 const unsigned char *buf, int count)
1da177e4
LT
199{
200 struct usb_serial *serial = port->serial;
201 struct urb *urb;
202 const unsigned char *current_position = buf;
203 unsigned long flags;
204 int status;
205 int i;
206 int bytes_sent = 0;
207 int transfer_size;
208
441b62c1 209 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
210
211 while (count > 0) {
1da177e4
LT
212 /* try to find a free urb in our list of them */
213 urb = NULL;
214
93c46795 215 spin_lock_irqsave(&write_urb_pool_lock, flags);
1da177e4
LT
216
217 for (i = 0; i < NUM_URBS; ++i) {
218 if (write_urb_pool[i]->status != -EINPROGRESS) {
219 urb = write_urb_pool[i];
220 break;
221 }
222 }
223
93c46795 224 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
1da177e4
LT
225
226 if (urb == NULL) {
441b62c1 227 dbg("%s - no more free urbs", __func__);
1da177e4
LT
228 goto exit;
229 }
230
231 if (urb->transfer_buffer == NULL) {
93c46795 232 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
1da177e4 233 if (urb->transfer_buffer == NULL) {
93c46795
AC
234 dev_err(&port->dev,
235 "%s no more kernel memory...\n",
236 __func__);
1da177e4
LT
237 goto exit;
238 }
239 }
240
93c46795 241 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1da177e4 242
93c46795 243 memcpy(urb->transfer_buffer, current_position, transfer_size);
1da177e4 244
441b62c1 245 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer);
1da177e4
LT
246
247 /* build up our urb */
93c46795 248 usb_fill_bulk_urb(
1da177e4
LT
249 urb,
250 serial->dev,
251 usb_sndbulkpipe(serial->dev,
93c46795 252 port->bulk_out_endpointAddress),
1da177e4
LT
253 urb->transfer_buffer,
254 transfer_size,
255 empeg_write_bulk_callback,
256 port);
257
258 /* send it down the pipe */
259 status = usb_submit_urb(urb, GFP_ATOMIC);
260 if (status) {
441b62c1 261 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
1da177e4
LT
262 bytes_sent = status;
263 break;
264 }
265
266 current_position += transfer_size;
267 bytes_sent += transfer_size;
268 count -= transfer_size;
269 bytes_out += transfer_size;
270
271 }
1da177e4
LT
272exit:
273 return bytes_sent;
93c46795 274}
1da177e4
LT
275
276
95da310e 277static int empeg_write_room(struct tty_struct *tty)
1da177e4 278{
95da310e 279 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
280 unsigned long flags;
281 int i;
282 int room = 0;
283
441b62c1 284 dbg("%s - port %d", __func__, port->number);
1da177e4 285
93c46795 286 spin_lock_irqsave(&write_urb_pool_lock, flags);
1da177e4
LT
287 /* tally up the number of bytes available */
288 for (i = 0; i < NUM_URBS; ++i) {
93c46795 289 if (write_urb_pool[i]->status != -EINPROGRESS)
1da177e4 290 room += URB_TRANSFER_BUFFER_SIZE;
93c46795
AC
291 }
292 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
441b62c1 293 dbg("%s - returns %d", __func__, room);
95da310e 294 return room;
1da177e4
LT
295
296}
297
298
95da310e 299static int empeg_chars_in_buffer(struct tty_struct *tty)
1da177e4 300{
95da310e 301 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
302 unsigned long flags;
303 int i;
304 int chars = 0;
305
441b62c1 306 dbg("%s - port %d", __func__, port->number);
1da177e4 307
93c46795 308 spin_lock_irqsave(&write_urb_pool_lock, flags);
1da177e4
LT
309
310 /* tally up the number of bytes waiting */
311 for (i = 0; i < NUM_URBS; ++i) {
93c46795 312 if (write_urb_pool[i]->status == -EINPROGRESS)
1da177e4 313 chars += URB_TRANSFER_BUFFER_SIZE;
1da177e4
LT
314 }
315
93c46795 316 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
441b62c1 317 dbg("%s - returns %d", __func__, chars);
93c46795 318 return chars;
1da177e4
LT
319}
320
321
93c46795 322static void empeg_write_bulk_callback(struct urb *urb)
1da177e4 323{
335202f4
GKH
324 struct usb_serial_port *port = urb->context;
325 int status = urb->status;
1da177e4 326
441b62c1 327 dbg("%s - port %d", __func__, port->number);
1da177e4 328
335202f4
GKH
329 if (status) {
330 dbg("%s - nonzero write bulk status received: %d",
441b62c1 331 __func__, status);
1da177e4
LT
332 return;
333 }
334
cf2c7481 335 usb_serial_port_softint(port);
1da177e4
LT
336}
337
338
93c46795 339static void empeg_read_bulk_callback(struct urb *urb)
1da177e4 340{
cdc97792 341 struct usb_serial_port *port = urb->context;
1da177e4
LT
342 struct tty_struct *tty;
343 unsigned char *data = urb->transfer_buffer;
1da177e4 344 int result;
335202f4 345 int status = urb->status;
1da177e4 346
441b62c1 347 dbg("%s - port %d", __func__, port->number);
1da177e4 348
335202f4
GKH
349 if (status) {
350 dbg("%s - nonzero read bulk status received: %d",
441b62c1 351 __func__, status);
1da177e4
LT
352 return;
353 }
354
93c46795
AC
355 usb_serial_debug_data(debug, &port->dev, __func__,
356 urb->actual_length, data);
95da310e 357 tty = port->port.tty;
1da177e4
LT
358
359 if (urb->actual_length) {
33f0f88f
AC
360 tty_buffer_request_room(tty, urb->actual_length);
361 tty_insert_flip_string(tty, data, urb->actual_length);
1da177e4
LT
362 tty_flip_buffer_push(tty);
363 bytes_in += urb->actual_length;
364 }
365
366 /* Continue trying to always read */
367 usb_fill_bulk_urb(
368 port->read_urb,
93c46795 369 port->serial->dev,
1da177e4
LT
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,
375 port);
376
377 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
378
379 if (result)
93c46795
AC
380 dev_err(&urb->dev->dev,
381 "%s - failed resubmitting read urb, error %d\n",
382 __func__, result);
1da177e4
LT
383
384 return;
385
386}
387
388
95da310e 389static void empeg_throttle(struct tty_struct *tty)
1da177e4 390{
95da310e 391 struct usb_serial_port *port = tty->driver_data;
441b62c1 392 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
393 usb_kill_urb(port->read_urb);
394}
395
396
95da310e 397static void empeg_unthrottle(struct tty_struct *tty)
1da177e4 398{
95da310e 399 struct usb_serial_port *port = tty->driver_data;
1da177e4 400 int result;
441b62c1 401 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
402
403 port->read_urb->dev = port->serial->dev;
1da177e4 404 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1da177e4 405 if (result)
93c46795
AC
406 dev_err(&port->dev,
407 "%s - failed submitting read urb, error %d\n",
408 __func__, result);
1da177e4
LT
409}
410
411
93c46795 412static int empeg_startup(struct usb_serial *serial)
1da177e4
LT
413{
414 int r;
415
441b62c1 416 dbg("%s", __func__);
1da177e4
LT
417
418 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
419 err("active config #%d != 1 ??",
420 serial->dev->actconfig->desc.bConfigurationValue);
421 return -ENODEV;
422 }
441b62c1 423 dbg("%s - reset config", __func__);
93c46795 424 r = usb_reset_configuration(serial->dev);
1da177e4
LT
425
426 /* continue on with initialization */
427 return r;
428
429}
430
431
93c46795 432static void empeg_shutdown(struct usb_serial *serial)
1da177e4 433{
93c46795 434 dbg("%s", __func__);
1da177e4
LT
435}
436
437
95da310e
AC
438static void empeg_set_termios(struct tty_struct *tty,
439 struct usb_serial_port *port, struct ktermios *old_termios)
1da177e4 440{
95da310e 441 struct ktermios *termios = tty->termios;
441b62c1 442 dbg("%s - port %d", __func__, port->number);
1da177e4 443
1da177e4 444 /*
93c46795
AC
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.
449 * (at least for now)
450 *
451 * The default requirements for this device are:
452 */
998e8638 453 termios->c_iflag
1da177e4
LT
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 */
462
998e8638 463 termios->c_oflag
1da177e4
LT
464 &= ~OPOST; /* disable postprocess output characters */
465
998e8638 466 termios->c_lflag
1da177e4
LT
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 */
472
998e8638 473 termios->c_cflag
1da177e4
LT
474 &= ~(CSIZE /* no size */
475 | PARENB /* disable parity bit */
476 | CBAUD); /* clear current baud rate */
477
998e8638
AC
478 termios->c_cflag
479 |= CS8; /* character size 8 bits */
1da177e4
LT
480
481 /*
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. :)
485 */
95da310e
AC
486 tty->low_latency = 1;
487 tty_encode_baud_rate(tty, 115200, 115200);
1da177e4
LT
488}
489
490
93c46795 491static int __init empeg_init(void)
1da177e4
LT
492{
493 struct urb *urb;
494 int i, retval;
495
93c46795
AC
496 /* create our write urb pool and transfer buffers */
497 spin_lock_init(&write_urb_pool_lock);
1da177e4
LT
498 for (i = 0; i < NUM_URBS; ++i) {
499 urb = usb_alloc_urb(0, GFP_KERNEL);
500 write_urb_pool[i] = urb;
501 if (urb == NULL) {
502 err("No more urbs???");
503 continue;
504 }
505
93c46795
AC
506 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
507 GFP_KERNEL);
1da177e4 508 if (!urb->transfer_buffer) {
93c46795 509 err("%s - out of memory for urb buffers.",
441b62c1 510 __func__);
1da177e4
LT
511 continue;
512 }
513 }
514
515 retval = usb_serial_register(&empeg_device);
516 if (retval)
517 goto failed_usb_serial_register;
518 retval = usb_register(&empeg_driver);
519 if (retval)
520 goto failed_usb_register;
521
522 info(DRIVER_VERSION ":" DRIVER_DESC);
523
524 return 0;
525failed_usb_register:
526 usb_serial_deregister(&empeg_device);
527failed_usb_serial_register:
528 for (i = 0; i < NUM_URBS; ++i) {
529 if (write_urb_pool[i]) {
1bc3c9e1 530 kfree(write_urb_pool[i]->transfer_buffer);
1da177e4
LT
531 usb_free_urb(write_urb_pool[i]);
532 }
533 }
534 return retval;
535}
536
537
93c46795 538static void __exit empeg_exit(void)
1da177e4
LT
539{
540 int i;
541 unsigned long flags;
542
543 usb_deregister(&empeg_driver);
93c46795 544 usb_serial_deregister(&empeg_device);
1da177e4 545
93c46795 546 spin_lock_irqsave(&write_urb_pool_lock, flags);
1da177e4
LT
547
548 for (i = 0; i < NUM_URBS; ++i) {
549 if (write_urb_pool[i]) {
93c46795
AC
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
553 * call oopses. */
1da177e4 554 /* usb_kill_urb(write_urb_pool[i]); */
1bc3c9e1 555 kfree(write_urb_pool[i]->transfer_buffer);
93c46795 556 usb_free_urb(write_urb_pool[i]);
1da177e4
LT
557 }
558 }
93c46795 559 spin_unlock_irqrestore(&write_urb_pool_lock, flags);
1da177e4
LT
560}
561
562
563module_init(empeg_init);
564module_exit(empeg_exit);
565
93c46795
AC
566MODULE_AUTHOR(DRIVER_AUTHOR);
567MODULE_DESCRIPTION(DRIVER_DESC);
1da177e4
LT
568MODULE_LICENSE("GPL");
569
570module_param(debug, bool, S_IRUGO | S_IWUSR);
571MODULE_PARM_DESC(debug, "Debug enabled or not");