digi_acceleport: coding style
[deliverable/linux.git] / 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 *
14 * See Documentation/usb/usb-serial.txt for more information on using this driver
15 *
16 * (07/16/2001) gb
17 * remove unused code in empeg_close() (thanks to Oliver Neukum for pointing this
18 * out) and rewrote empeg_set_termios().
19 *
20 * (05/30/2001) gkh
21 * switched from using spinlock to a semaphore, which fixes lots of problems.
22 *
23 * (04/08/2001) gb
24 * Identify version on module load.
25 *
26 * (01/22/2001) gb
27 * Added write_room() and chars_in_buffer() support.
28 *
29 * (12/21/2000) gb
30 * Moved termio stuff inside the port->active check.
31 * Moved MOD_DEC_USE_COUNT to end of empeg_close().
32 *
33 * (12/03/2000) gb
95da310e 34 * Added port->port.tty->ldisc.set_termios(port->port.tty, NULL) to empeg_open()
1da177e4
LT
35 * This notifies the tty driver that the termios have changed.
36 *
37 * (11/13/2000) gb
38 * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to empeg_open()
39 * (It only needs to be set once - Doh!)
40 *
41 * (11/11/2000) gb
42 * Updated to work with id_table structure.
43 *
44 * (11/04/2000) gb
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.
51 *
52 */
53
1da177e4
LT
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>
a969888c 65#include <linux/usb/serial.h>
1da177e4
LT
66
67static int debug;
68
69/*
70 * Version Information
71 */
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"
75
76#define EMPEG_VENDOR_ID 0x084f
77#define EMPEG_PRODUCT_ID 0x0001
78
79/* function prototypes for an empeg-car player */
95da310e
AC
80static int empeg_open (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
81static void empeg_close (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
82static int empeg_write (struct tty_struct *tty, struct usb_serial_port *port,
1da177e4
LT
83 const unsigned char *buf,
84 int count);
95da310e
AC
85static int empeg_write_room (struct tty_struct *tty);
86static int empeg_chars_in_buffer (struct tty_struct *tty);
87static void empeg_throttle (struct tty_struct *tty);
88static void empeg_unthrottle (struct tty_struct *tty);
1da177e4
LT
89static int empeg_startup (struct usb_serial *serial);
90static void empeg_shutdown (struct usb_serial *serial);
95da310e 91static void empeg_set_termios (struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios);
7d12e780
DH
92static void empeg_write_bulk_callback (struct urb *urb);
93static void empeg_read_bulk_callback (struct urb *urb);
1da177e4
LT
94
95static struct usb_device_id id_table [] = {
96 { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
97 { } /* Terminating entry */
98};
99
100MODULE_DEVICE_TABLE (usb, id_table);
101
102static struct usb_driver empeg_driver = {
1da177e4
LT
103 .name = "empeg",
104 .probe = usb_serial_probe,
105 .disconnect = usb_serial_disconnect,
106 .id_table = id_table,
ba9dc657 107 .no_dynamic_id = 1,
1da177e4
LT
108};
109
ea65370d 110static struct usb_serial_driver empeg_device = {
18fcac35
GKH
111 .driver = {
112 .owner = THIS_MODULE,
269bda1c 113 .name = "empeg",
18fcac35 114 },
1da177e4 115 .id_table = id_table,
d9b1b787 116 .usb_driver = &empeg_driver,
1da177e4
LT
117 .num_ports = 1,
118 .open = empeg_open,
119 .close = empeg_close,
120 .throttle = empeg_throttle,
121 .unthrottle = empeg_unthrottle,
122 .attach = empeg_startup,
123 .shutdown = empeg_shutdown,
1da177e4
LT
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,
130};
131
132#define NUM_URBS 16
133#define URB_TRANSFER_BUFFER_SIZE 4096
134
135static struct urb *write_urb_pool[NUM_URBS];
136static spinlock_t write_urb_pool_lock;
137static int bytes_in;
138static int bytes_out;
139
140/******************************************************************************
141 * Empeg specific driver functions
142 ******************************************************************************/
95da310e
AC
143static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
144 struct file *filp)
1da177e4
LT
145{
146 struct usb_serial *serial = port->serial;
147 int result = 0;
148
441b62c1 149 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
150
151 /* Force default termio settings */
95da310e 152 empeg_set_termios (tty, port, NULL) ;
1da177e4
LT
153
154 bytes_in = 0;
155 bytes_out = 0;
156
157 /* Start reading from the device */
158 usb_fill_bulk_urb(
159 port->read_urb,
160 serial->dev,
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,
166 port);
167
168 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
169
170 if (result)
441b62c1 171 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1da177e4
LT
172
173 return result;
174}
175
176
95da310e
AC
177static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port,
178 struct file * filp)
1da177e4 179{
441b62c1 180 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
181
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); */
186}
187
188
95da310e 189static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count)
1da177e4
LT
190{
191 struct usb_serial *serial = port->serial;
192 struct urb *urb;
193 const unsigned char *current_position = buf;
194 unsigned long flags;
195 int status;
196 int i;
197 int bytes_sent = 0;
198 int transfer_size;
199
441b62c1 200 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
201
202 while (count > 0) {
1da177e4
LT
203 /* try to find a free urb in our list of them */
204 urb = NULL;
205
206 spin_lock_irqsave (&write_urb_pool_lock, flags);
207
208 for (i = 0; i < NUM_URBS; ++i) {
209 if (write_urb_pool[i]->status != -EINPROGRESS) {
210 urb = write_urb_pool[i];
211 break;
212 }
213 }
214
215 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
216
217 if (urb == NULL) {
441b62c1 218 dbg("%s - no more free urbs", __func__);
1da177e4
LT
219 goto exit;
220 }
221
222 if (urb->transfer_buffer == NULL) {
223 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
224 if (urb->transfer_buffer == NULL) {
441b62c1 225 dev_err(&port->dev, "%s no more kernel memory...\n", __func__);
1da177e4
LT
226 goto exit;
227 }
228 }
229
230 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
231
232 memcpy (urb->transfer_buffer, current_position, transfer_size);
233
441b62c1 234 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer);
1da177e4
LT
235
236 /* build up our urb */
237 usb_fill_bulk_urb (
238 urb,
239 serial->dev,
240 usb_sndbulkpipe(serial->dev,
241 port->bulk_out_endpointAddress),
242 urb->transfer_buffer,
243 transfer_size,
244 empeg_write_bulk_callback,
245 port);
246
247 /* send it down the pipe */
248 status = usb_submit_urb(urb, GFP_ATOMIC);
249 if (status) {
441b62c1 250 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
1da177e4
LT
251 bytes_sent = status;
252 break;
253 }
254
255 current_position += transfer_size;
256 bytes_sent += transfer_size;
257 count -= transfer_size;
258 bytes_out += transfer_size;
259
260 }
1da177e4
LT
261exit:
262 return bytes_sent;
1da177e4
LT
263}
264
265
95da310e 266static int empeg_write_room(struct tty_struct *tty)
1da177e4 267{
95da310e 268 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
269 unsigned long flags;
270 int i;
271 int room = 0;
272
441b62c1 273 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
274
275 spin_lock_irqsave (&write_urb_pool_lock, flags);
1da177e4
LT
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;
280 }
281 }
1da177e4 282 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
441b62c1 283 dbg("%s - returns %d", __func__, room);
95da310e 284 return room;
1da177e4
LT
285
286}
287
288
95da310e 289static int empeg_chars_in_buffer(struct tty_struct *tty)
1da177e4 290{
95da310e 291 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
292 unsigned long flags;
293 int i;
294 int chars = 0;
295
441b62c1 296 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
297
298 spin_lock_irqsave (&write_urb_pool_lock, flags);
299
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;
304 }
305 }
306
307 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
308
441b62c1 309 dbg("%s - returns %d", __func__, chars);
1da177e4
LT
310
311 return (chars);
312
313}
314
315
7d12e780 316static void empeg_write_bulk_callback (struct urb *urb)
1da177e4 317{
335202f4
GKH
318 struct usb_serial_port *port = urb->context;
319 int status = urb->status;
1da177e4 320
441b62c1 321 dbg("%s - port %d", __func__, port->number);
1da177e4 322
335202f4
GKH
323 if (status) {
324 dbg("%s - nonzero write bulk status received: %d",
441b62c1 325 __func__, status);
1da177e4
LT
326 return;
327 }
328
cf2c7481 329 usb_serial_port_softint(port);
1da177e4
LT
330}
331
332
7d12e780 333static void empeg_read_bulk_callback (struct urb *urb)
1da177e4 334{
cdc97792 335 struct usb_serial_port *port = urb->context;
1da177e4
LT
336 struct tty_struct *tty;
337 unsigned char *data = urb->transfer_buffer;
1da177e4 338 int result;
335202f4 339 int status = urb->status;
1da177e4 340
441b62c1 341 dbg("%s - port %d", __func__, port->number);
1da177e4 342
335202f4
GKH
343 if (status) {
344 dbg("%s - nonzero read bulk status received: %d",
441b62c1 345 __func__, status);
1da177e4
LT
346 return;
347 }
348
441b62c1 349 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
1da177e4 350
95da310e 351 tty = port->port.tty;
1da177e4
LT
352
353 if (urb->actual_length) {
33f0f88f
AC
354 tty_buffer_request_room(tty, urb->actual_length);
355 tty_insert_flip_string(tty, data, urb->actual_length);
1da177e4
LT
356 tty_flip_buffer_push(tty);
357 bytes_in += urb->actual_length;
358 }
359
360 /* Continue trying to always read */
361 usb_fill_bulk_urb(
362 port->read_urb,
363 port->serial->dev,
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,
369 port);
370
371 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
372
373 if (result)
441b62c1 374 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
1da177e4
LT
375
376 return;
377
378}
379
380
95da310e 381static void empeg_throttle(struct tty_struct *tty)
1da177e4 382{
95da310e 383 struct usb_serial_port *port = tty->driver_data;
441b62c1 384 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
385 usb_kill_urb(port->read_urb);
386}
387
388
95da310e 389static void empeg_unthrottle(struct tty_struct *tty)
1da177e4 390{
95da310e 391 struct usb_serial_port *port = tty->driver_data;
1da177e4 392 int result;
441b62c1 393 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
394
395 port->read_urb->dev = port->serial->dev;
1da177e4 396 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1da177e4 397 if (result)
441b62c1 398 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __func__, result);
1da177e4
LT
399}
400
401
402static int empeg_startup (struct usb_serial *serial)
403{
404 int r;
405
441b62c1 406 dbg("%s", __func__);
1da177e4
LT
407
408 if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
409 err("active config #%d != 1 ??",
410 serial->dev->actconfig->desc.bConfigurationValue);
411 return -ENODEV;
412 }
441b62c1 413 dbg("%s - reset config", __func__);
1da177e4
LT
414 r = usb_reset_configuration (serial->dev);
415
416 /* continue on with initialization */
417 return r;
418
419}
420
421
422static void empeg_shutdown (struct usb_serial *serial)
423{
441b62c1 424 dbg ("%s", __func__);
1da177e4
LT
425}
426
427
95da310e
AC
428static void empeg_set_termios(struct tty_struct *tty,
429 struct usb_serial_port *port, struct ktermios *old_termios)
1da177e4 430{
95da310e 431 struct ktermios *termios = tty->termios;
441b62c1 432 dbg("%s - port %d", __func__, port->number);
1da177e4 433
1da177e4
LT
434 /*
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)
440 *
441 * The default requirements for this device are:
442 */
998e8638 443 termios->c_iflag
1da177e4
LT
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 */
452
998e8638 453 termios->c_oflag
1da177e4
LT
454 &= ~OPOST; /* disable postprocess output characters */
455
998e8638 456 termios->c_lflag
1da177e4
LT
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 */
462
998e8638 463 termios->c_cflag
1da177e4
LT
464 &= ~(CSIZE /* no size */
465 | PARENB /* disable parity bit */
466 | CBAUD); /* clear current baud rate */
467
998e8638
AC
468 termios->c_cflag
469 |= CS8; /* character size 8 bits */
1da177e4
LT
470
471 /*
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. :)
475 */
95da310e
AC
476 tty->low_latency = 1;
477 tty_encode_baud_rate(tty, 115200, 115200);
1da177e4
LT
478}
479
480
481static int __init empeg_init (void)
482{
483 struct urb *urb;
484 int i, retval;
485
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;
491 if (urb == NULL) {
492 err("No more urbs???");
493 continue;
494 }
495
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.",
441b62c1 499 __func__);
1da177e4
LT
500 continue;
501 }
502 }
503
504 retval = usb_serial_register(&empeg_device);
505 if (retval)
506 goto failed_usb_serial_register;
507 retval = usb_register(&empeg_driver);
508 if (retval)
509 goto failed_usb_register;
510
511 info(DRIVER_VERSION ":" DRIVER_DESC);
512
513 return 0;
514failed_usb_register:
515 usb_serial_deregister(&empeg_device);
516failed_usb_serial_register:
517 for (i = 0; i < NUM_URBS; ++i) {
518 if (write_urb_pool[i]) {
1bc3c9e1 519 kfree(write_urb_pool[i]->transfer_buffer);
1da177e4
LT
520 usb_free_urb(write_urb_pool[i]);
521 }
522 }
523 return retval;
524}
525
526
527static void __exit empeg_exit (void)
528{
529 int i;
530 unsigned long flags;
531
532 usb_deregister(&empeg_driver);
533 usb_serial_deregister (&empeg_device);
534
535 spin_lock_irqsave (&write_urb_pool_lock, flags);
536
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]); */
1bc3c9e1 543 kfree(write_urb_pool[i]->transfer_buffer);
1da177e4
LT
544 usb_free_urb (write_urb_pool[i]);
545 }
546 }
547
548 spin_unlock_irqrestore (&write_urb_pool_lock, flags);
549}
550
551
552module_init(empeg_init);
553module_exit(empeg_exit);
554
555MODULE_AUTHOR( DRIVER_AUTHOR );
556MODULE_DESCRIPTION( DRIVER_DESC );
557MODULE_LICENSE("GPL");
558
559module_param(debug, bool, S_IRUGO | S_IWUSR);
560MODULE_PARM_DESC(debug, "Debug enabled or not");
This page took 0.401222 seconds and 5 git commands to generate.