xref: /linux/drivers/usb/serial/empeg.c (revision 776cfebb430c7b22c208b1b17add97f354d97cab)
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
34  *	Added port->tty->ldisc.set_termios(port->tty, NULL) to empeg_open()
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 
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>
66 #include "usb-serial.h"
67 
68 static int debug;
69 
70 /*
71  * Version Information
72  */
73 #define DRIVER_VERSION "v1.2"
74 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
75 #define DRIVER_DESC "USB Empeg Mark I/II Driver"
76 
77 #define EMPEG_VENDOR_ID			0x084f
78 #define EMPEG_PRODUCT_ID		0x0001
79 
80 /* function prototypes for an empeg-car player */
81 static int  empeg_open			(struct usb_serial_port *port, struct file *filp);
82 static void empeg_close			(struct usb_serial_port *port, struct file *filp);
83 static int  empeg_write			(struct usb_serial_port *port,
84 					const unsigned char *buf,
85 					int count);
86 static int  empeg_write_room		(struct usb_serial_port *port);
87 static int  empeg_chars_in_buffer	(struct usb_serial_port *port);
88 static void empeg_throttle		(struct usb_serial_port *port);
89 static void empeg_unthrottle		(struct usb_serial_port *port);
90 static int  empeg_startup		(struct usb_serial *serial);
91 static void empeg_shutdown		(struct usb_serial *serial);
92 static int  empeg_ioctl			(struct usb_serial_port *port,
93 					struct file * file,
94 					unsigned int cmd,
95 					unsigned long arg);
96 static void empeg_set_termios		(struct usb_serial_port *port, struct termios *old_termios);
97 static void empeg_write_bulk_callback	(struct urb *urb, struct pt_regs *regs);
98 static void empeg_read_bulk_callback	(struct urb *urb, struct pt_regs *regs);
99 
100 static struct usb_device_id id_table [] = {
101 	{ USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
102 	{ }					/* Terminating entry */
103 };
104 
105 MODULE_DEVICE_TABLE (usb, id_table);
106 
107 static struct usb_driver empeg_driver = {
108 	.owner =	THIS_MODULE,
109 	.name =		"empeg",
110 	.probe =	usb_serial_probe,
111 	.disconnect =	usb_serial_disconnect,
112 	.id_table =	id_table,
113 };
114 
115 static struct usb_serial_device_type empeg_device = {
116 	.owner =		THIS_MODULE,
117 	.name =			"Empeg",
118 	.id_table =		id_table,
119 	.num_interrupt_in =	0,
120 	.num_bulk_in =		1,
121 	.num_bulk_out =		1,
122 	.num_ports =		1,
123 	.open =			empeg_open,
124 	.close =		empeg_close,
125 	.throttle =		empeg_throttle,
126 	.unthrottle =		empeg_unthrottle,
127 	.attach =		empeg_startup,
128 	.shutdown =		empeg_shutdown,
129 	.ioctl =		empeg_ioctl,
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 
141 static struct urb	*write_urb_pool[NUM_URBS];
142 static spinlock_t	write_urb_pool_lock;
143 static int		bytes_in;
144 static int		bytes_out;
145 
146 /******************************************************************************
147  * Empeg specific driver functions
148  ******************************************************************************/
149 static int empeg_open (struct usb_serial_port *port, struct file *filp)
150 {
151 	struct usb_serial *serial = port->serial;
152 	int result = 0;
153 
154 	dbg("%s - port %d", __FUNCTION__, port->number);
155 
156 	/* Force default termio settings */
157 	empeg_set_termios (port, NULL) ;
158 
159 	bytes_in = 0;
160 	bytes_out = 0;
161 
162 	/* Start reading from the device */
163 	usb_fill_bulk_urb(
164 		port->read_urb,
165 		serial->dev,
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,
171 		port);
172 
173 	result = usb_submit_urb(port->read_urb, GFP_KERNEL);
174 
175 	if (result)
176 		dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
177 
178 	return result;
179 }
180 
181 
182 static void empeg_close (struct usb_serial_port *port, struct file * filp)
183 {
184 	dbg("%s - port %d", __FUNCTION__, port->number);
185 
186 	/* shutdown our bulk read */
187 	usb_kill_urb(port->read_urb);
188 	/* Uncomment the following line if you want to see some statistics in your syslog */
189 	/* dev_info (&port->dev, "Bytes In = %d  Bytes Out = %d\n", bytes_in, bytes_out); */
190 }
191 
192 
193 static int empeg_write (struct usb_serial_port *port, const unsigned char *buf, int count)
194 {
195 	struct usb_serial *serial = port->serial;
196 	struct urb *urb;
197 	const unsigned char *current_position = buf;
198 	unsigned long flags;
199 	int status;
200 	int i;
201 	int bytes_sent = 0;
202 	int transfer_size;
203 
204 	dbg("%s - port %d", __FUNCTION__, port->number);
205 
206 	while (count > 0) {
207 
208 		/* try to find a free urb in our list of them */
209 		urb = NULL;
210 
211 		spin_lock_irqsave (&write_urb_pool_lock, flags);
212 
213 		for (i = 0; i < NUM_URBS; ++i) {
214 			if (write_urb_pool[i]->status != -EINPROGRESS) {
215 				urb = write_urb_pool[i];
216 				break;
217 			}
218 		}
219 
220 		spin_unlock_irqrestore (&write_urb_pool_lock, flags);
221 
222 		if (urb == NULL) {
223 			dbg("%s - no more free urbs", __FUNCTION__);
224 			goto exit;
225 		}
226 
227 		if (urb->transfer_buffer == NULL) {
228 			urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
229 			if (urb->transfer_buffer == NULL) {
230 				dev_err(&port->dev, "%s no more kernel memory...\n", __FUNCTION__);
231 				goto exit;
232 			}
233 		}
234 
235 		transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
236 
237 		memcpy (urb->transfer_buffer, current_position, transfer_size);
238 
239 		usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size, urb->transfer_buffer);
240 
241 		/* build up our urb */
242 		usb_fill_bulk_urb (
243 			urb,
244 			serial->dev,
245 			usb_sndbulkpipe(serial->dev,
246 				port->bulk_out_endpointAddress),
247 			urb->transfer_buffer,
248 			transfer_size,
249 			empeg_write_bulk_callback,
250 			port);
251 
252 		/* send it down the pipe */
253 		status = usb_submit_urb(urb, GFP_ATOMIC);
254 		if (status) {
255 			dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __FUNCTION__, status);
256 			bytes_sent = status;
257 			break;
258 		}
259 
260 		current_position += transfer_size;
261 		bytes_sent += transfer_size;
262 		count -= transfer_size;
263 		bytes_out += transfer_size;
264 
265 	}
266 
267 exit:
268 	return bytes_sent;
269 
270 }
271 
272 
273 static int empeg_write_room (struct usb_serial_port *port)
274 {
275 	unsigned long flags;
276 	int i;
277 	int room = 0;
278 
279 	dbg("%s - port %d", __FUNCTION__, port->number);
280 
281 	spin_lock_irqsave (&write_urb_pool_lock, flags);
282 
283 	/* tally up the number of bytes available */
284 	for (i = 0; i < NUM_URBS; ++i) {
285 		if (write_urb_pool[i]->status != -EINPROGRESS) {
286 			room += URB_TRANSFER_BUFFER_SIZE;
287 		}
288 	}
289 
290 	spin_unlock_irqrestore (&write_urb_pool_lock, flags);
291 
292 	dbg("%s - returns %d", __FUNCTION__, room);
293 
294 	return (room);
295 
296 }
297 
298 
299 static int empeg_chars_in_buffer (struct usb_serial_port *port)
300 {
301 	unsigned long flags;
302 	int i;
303 	int chars = 0;
304 
305 	dbg("%s - port %d", __FUNCTION__, port->number);
306 
307 	spin_lock_irqsave (&write_urb_pool_lock, flags);
308 
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;
313 		}
314 	}
315 
316 	spin_unlock_irqrestore (&write_urb_pool_lock, flags);
317 
318 	dbg("%s - returns %d", __FUNCTION__, chars);
319 
320 	return (chars);
321 
322 }
323 
324 
325 static void empeg_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
326 {
327 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
328 
329 	dbg("%s - port %d", __FUNCTION__, port->number);
330 
331 	if (urb->status) {
332 		dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
333 		return;
334 	}
335 
336 	schedule_work(&port->work);
337 }
338 
339 
340 static void empeg_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
341 {
342 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
343 	struct tty_struct *tty;
344 	unsigned char *data = urb->transfer_buffer;
345 	int i;
346 	int result;
347 
348 	dbg("%s - port %d", __FUNCTION__, port->number);
349 
350 	if (urb->status) {
351 		dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
352 		return;
353 	}
354 
355 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
356 
357 	tty = port->tty;
358 
359 	if (urb->actual_length) {
360 		for (i = 0; i < urb->actual_length ; ++i) {
361 			/* gb - 2000/11/13
362 			 * If we insert too many characters we'll overflow the buffer.
363 			 * This means we'll lose bytes - Decidedly bad.
364 			 */
365 			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
366 				tty_flip_buffer_push(tty);
367 				}
368 			tty_insert_flip_char(tty, data[i], 0);
369 		}
370 		/* gb - 2000/11/13
371 		 * Goes straight through instead of scheduling - if tty->low_latency is set.
372 		 */
373 		tty_flip_buffer_push(tty);
374 		bytes_in += urb->actual_length;
375 	}
376 
377 	/* Continue trying to always read  */
378 	usb_fill_bulk_urb(
379 		port->read_urb,
380 		port->serial->dev,
381 		usb_rcvbulkpipe(port->serial->dev,
382 			port->bulk_in_endpointAddress),
383 		port->read_urb->transfer_buffer,
384 		port->read_urb->transfer_buffer_length,
385 		empeg_read_bulk_callback,
386 		port);
387 
388 	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
389 
390 	if (result)
391 		dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
392 
393 	return;
394 
395 }
396 
397 
398 static void empeg_throttle (struct usb_serial_port *port)
399 {
400 	dbg("%s - port %d", __FUNCTION__, port->number);
401 	usb_kill_urb(port->read_urb);
402 }
403 
404 
405 static void empeg_unthrottle (struct usb_serial_port *port)
406 {
407 	int result;
408 
409 	dbg("%s - port %d", __FUNCTION__, port->number);
410 
411 	port->read_urb->dev = port->serial->dev;
412 
413 	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
414 
415 	if (result)
416 		dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
417 
418 	return;
419 }
420 
421 
422 static int  empeg_startup (struct usb_serial *serial)
423 {
424 	int r;
425 
426 	dbg("%s", __FUNCTION__);
427 
428 	if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
429 		err("active config #%d != 1 ??",
430 			serial->dev->actconfig->desc.bConfigurationValue);
431 		return -ENODEV;
432 	}
433 	dbg("%s - reset config", __FUNCTION__);
434 	r = usb_reset_configuration (serial->dev);
435 
436 	/* continue on with initialization */
437 	return r;
438 
439 }
440 
441 
442 static void empeg_shutdown (struct usb_serial *serial)
443 {
444 	dbg ("%s", __FUNCTION__);
445 }
446 
447 
448 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
449 {
450 	dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
451 
452 	return -ENOIOCTLCMD;
453 }
454 
455 
456 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
457 {
458 
459 	dbg("%s - port %d", __FUNCTION__, port->number);
460 
461 	if ((!port->tty) || (!port->tty->termios)) {
462 		dbg("%s - no tty structures", __FUNCTION__);
463 		return;
464 	}
465 
466 	/*
467          * The empeg-car player wants these particular tty settings.
468          * You could, for example, change the baud rate, however the
469          * player only supports 115200 (currently), so there is really
470          * no point in support for changes to the tty settings.
471          * (at least for now)
472          *
473          * The default requirements for this device are:
474          */
475 	port->tty->termios->c_iflag
476 		&= ~(IGNBRK	/* disable ignore break */
477 		| BRKINT	/* disable break causes interrupt */
478 		| PARMRK	/* disable mark parity errors */
479 		| ISTRIP	/* disable clear high bit of input characters */
480 		| INLCR		/* disable translate NL to CR */
481 		| IGNCR		/* disable ignore CR */
482 		| ICRNL		/* disable translate CR to NL */
483 		| IXON);	/* disable enable XON/XOFF flow control */
484 
485 	port->tty->termios->c_oflag
486 		&= ~OPOST;	/* disable postprocess output characters */
487 
488 	port->tty->termios->c_lflag
489 		&= ~(ECHO	/* disable echo input characters */
490 		| ECHONL	/* disable echo new line */
491 		| ICANON	/* disable erase, kill, werase, and rprnt special characters */
492 		| ISIG		/* disable interrupt, quit, and suspend special characters */
493 		| IEXTEN);	/* disable non-POSIX special characters */
494 
495 	port->tty->termios->c_cflag
496 		&= ~(CSIZE	/* no size */
497 		| PARENB	/* disable parity bit */
498 		| CBAUD);	/* clear current baud rate */
499 
500 	port->tty->termios->c_cflag
501 		|= (CS8		/* character size 8 bits */
502 		| B115200);	/* baud rate 115200 */
503 
504 	/*
505 	 * Force low_latency on; otherwise the pushes are scheduled;
506 	 * this is bad as it opens up the possibility of dropping bytes
507 	 * on the floor.  We don't want to drop bytes on the floor. :)
508 	 */
509 	port->tty->low_latency = 1;
510 
511 	return;
512 }
513 
514 
515 static int __init empeg_init (void)
516 {
517 	struct urb *urb;
518 	int i, retval;
519 
520 	/* create our write urb pool and transfer buffers */
521 	spin_lock_init (&write_urb_pool_lock);
522 	for (i = 0; i < NUM_URBS; ++i) {
523 		urb = usb_alloc_urb(0, GFP_KERNEL);
524 		write_urb_pool[i] = urb;
525 		if (urb == NULL) {
526 			err("No more urbs???");
527 			continue;
528 		}
529 
530 		urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
531 		if (!urb->transfer_buffer) {
532 			err("%s - out of memory for urb buffers.",
533 			    __FUNCTION__);
534 			continue;
535 		}
536 	}
537 
538 	retval = usb_serial_register(&empeg_device);
539 	if (retval)
540 		goto failed_usb_serial_register;
541 	retval = usb_register(&empeg_driver);
542 	if (retval)
543 		goto failed_usb_register;
544 
545 	info(DRIVER_VERSION ":" DRIVER_DESC);
546 
547 	return 0;
548 failed_usb_register:
549 	usb_serial_deregister(&empeg_device);
550 failed_usb_serial_register:
551 	for (i = 0; i < NUM_URBS; ++i) {
552 		if (write_urb_pool[i]) {
553 			kfree(write_urb_pool[i]->transfer_buffer);
554 			usb_free_urb(write_urb_pool[i]);
555 		}
556 	}
557 	return retval;
558 }
559 
560 
561 static void __exit empeg_exit (void)
562 {
563 	int i;
564 	unsigned long flags;
565 
566 	usb_deregister(&empeg_driver);
567 	usb_serial_deregister (&empeg_device);
568 
569 	spin_lock_irqsave (&write_urb_pool_lock, flags);
570 
571 	for (i = 0; i < NUM_URBS; ++i) {
572 		if (write_urb_pool[i]) {
573 			/* FIXME - uncomment the following usb_kill_urb call when
574 			 * the host controllers get fixed to set urb->dev = NULL after
575 			 * the urb is finished.  Otherwise this call oopses. */
576 			/* usb_kill_urb(write_urb_pool[i]); */
577 			kfree(write_urb_pool[i]->transfer_buffer);
578 			usb_free_urb (write_urb_pool[i]);
579 		}
580 	}
581 
582 	spin_unlock_irqrestore (&write_urb_pool_lock, flags);
583 }
584 
585 
586 module_init(empeg_init);
587 module_exit(empeg_exit);
588 
589 MODULE_AUTHOR( DRIVER_AUTHOR );
590 MODULE_DESCRIPTION( DRIVER_DESC );
591 MODULE_LICENSE("GPL");
592 
593 module_param(debug, bool, S_IRUGO | S_IWUSR);
594 MODULE_PARM_DESC(debug, "Debug enabled or not");
595