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 
67 #ifdef CONFIG_USB_SERIAL_DEBUG
68 	static int debug = 1;
69 #else
70 	static int debug;
71 #endif
72 
73 #include "usb-serial.h"
74 
75 /*
76  * Version Information
77  */
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"
81 
82 #define EMPEG_VENDOR_ID			0x084f
83 #define EMPEG_PRODUCT_ID		0x0001
84 
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,
89 					int from_user,
90 					const unsigned char *buf,
91 					int count);
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,
99 					struct file * file,
100 					unsigned int cmd,
101 					unsigned long arg);
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);
105 
106 static struct usb_device_id id_table [] = {
107 	{ USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
108 	{ }					/* Terminating entry */
109 };
110 
111 MODULE_DEVICE_TABLE (usb, id_table);
112 
113 static struct usb_serial_device_type empeg_device = {
114 	.owner =		THIS_MODULE,
115 	.name =			"Empeg",
116 	.id_table =		id_table,
117 	.num_interrupt_in =	0,
118 	.num_bulk_in =		1,
119 	.num_bulk_out =		1,
120 	.num_ports =		1,
121 	.open =			empeg_open,
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,
134 };
135 
136 #define NUM_URBS			16
137 #define URB_TRANSFER_BUFFER_SIZE	4096
138 
139 static struct urb	*write_urb_pool[NUM_URBS];
140 static spinlock_t	write_urb_pool_lock;
141 static int		bytes_in;
142 static int		bytes_out;
143 
144 /******************************************************************************
145  * Empeg specific driver functions
146  ******************************************************************************/
empeg_open(struct usb_serial_port * port,struct file * filp)147 static int empeg_open (struct usb_serial_port *port, struct file *filp)
148 {
149 	struct usb_serial *serial = port->serial;
150 	int result = 0;;
151 
152 	if (port_paranoia_check (port, __FUNCTION__))
153 		return -ENODEV;
154 
155 	dbg("%s - port %d", __FUNCTION__, port->number);
156 
157 	/* Force default termio settings */
158 	empeg_set_termios (port, NULL) ;
159 
160 	bytes_in = 0;
161 	bytes_out = 0;
162 
163 	/* Start reading from the device */
164 	FILL_BULK_URB(
165 		port->read_urb,
166 		serial->dev,
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 	port->read_urb->transfer_flags |= USB_QUEUE_BULK;
175 
176 	result = usb_submit_urb(port->read_urb);
177 
178 	if (result)
179 		err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
180 
181 	return result;
182 }
183 
184 
empeg_close(struct usb_serial_port * port,struct file * filp)185 static void empeg_close (struct usb_serial_port *port, struct file * filp)
186 {
187 	struct usb_serial *serial;
188 
189 	if (port_paranoia_check (port, __FUNCTION__))
190 		return;
191 
192 	dbg("%s - port %d", __FUNCTION__, port->number);
193 
194 	serial = get_usb_serial (port, __FUNCTION__);
195 	if (!serial)
196 		return;
197 
198 	if (serial->dev) {
199 		/* shutdown our bulk read */
200 		usb_unlink_urb (port->read_urb);
201 	}
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); */
204 }
205 
206 
empeg_write(struct usb_serial_port * port,int from_user,const unsigned char * buf,int count)207 static int empeg_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
208 {
209 	struct usb_serial *serial = port->serial;
210 	struct urb *urb;
211 	const unsigned char *current_position = buf;
212 	unsigned long flags;
213 	int status;
214 	int i;
215 	int bytes_sent = 0;
216 	int transfer_size;
217 
218 	dbg("%s - port %d", __FUNCTION__, port->number);
219 
220 	usb_serial_debug_data (__FILE__, __FUNCTION__, count, buf);
221 
222 	while (count > 0) {
223 
224 		/* try to find a free urb in our list of them */
225 		urb = NULL;
226 
227 		spin_lock_irqsave (&write_urb_pool_lock, flags);
228 
229 		for (i = 0; i < NUM_URBS; ++i) {
230 			if (write_urb_pool[i]->status != -EINPROGRESS) {
231 				urb = write_urb_pool[i];
232 				break;
233 			}
234 		}
235 
236 		spin_unlock_irqrestore (&write_urb_pool_lock, flags);
237 
238 		if (urb == NULL) {
239 			dbg("%s - no more free urbs", __FUNCTION__);
240 			goto exit;
241 		}
242 
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__);
247 				goto exit;
248 			}
249 		}
250 
251 		transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
252 
253 		if (from_user) {
254 			if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
255 				bytes_sent = -EFAULT;
256 				break;
257 			}
258 		} else {
259 			memcpy (urb->transfer_buffer, current_position, transfer_size);
260 		}
261 
262 		/* build up our urb */
263 		FILL_BULK_URB (
264 			urb,
265 			serial->dev,
266 			usb_sndbulkpipe(serial->dev,
267 				port->bulk_out_endpointAddress),
268 			urb->transfer_buffer,
269 			transfer_size,
270 			empeg_write_bulk_callback,
271 			port);
272 
273 		urb->transfer_flags |= USB_QUEUE_BULK;
274 
275 		/* send it down the pipe */
276 		status = usb_submit_urb(urb);
277 		if (status) {
278 			err("%s - usb_submit_urb(write bulk) failed with status = %d", __FUNCTION__, status);
279 			bytes_sent = status;
280 			break;
281 		}
282 
283 		current_position += transfer_size;
284 		bytes_sent += transfer_size;
285 		count -= transfer_size;
286 		bytes_out += transfer_size;
287 
288 	}
289 
290 exit:
291 	return bytes_sent;
292 
293 }
294 
295 
empeg_write_room(struct usb_serial_port * port)296 static int empeg_write_room (struct usb_serial_port *port)
297 {
298 	unsigned long flags;
299 	int i;
300 	int room = 0;
301 
302 	dbg("%s - port %d", __FUNCTION__, port->number);
303 
304 	spin_lock_irqsave (&write_urb_pool_lock, flags);
305 
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;
310 		}
311 	}
312 
313 	spin_unlock_irqrestore (&write_urb_pool_lock, flags);
314 
315 	dbg("%s - returns %d", __FUNCTION__, room);
316 
317 	return (room);
318 
319 }
320 
321 
empeg_chars_in_buffer(struct usb_serial_port * port)322 static int empeg_chars_in_buffer (struct usb_serial_port *port)
323 {
324 	unsigned long flags;
325 	int i;
326 	int chars = 0;
327 
328 	dbg("%s - port %d", __FUNCTION__, port->number);
329 
330 	spin_lock_irqsave (&write_urb_pool_lock, flags);
331 
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;
336 		}
337 	}
338 
339 	spin_unlock_irqrestore (&write_urb_pool_lock, flags);
340 
341 	dbg("%s - returns %d", __FUNCTION__, chars);
342 
343 	return (chars);
344 
345 }
346 
347 
empeg_write_bulk_callback(struct urb * urb)348 static void empeg_write_bulk_callback (struct urb *urb)
349 {
350 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
351 
352 	if (port_paranoia_check (port, __FUNCTION__))
353 		return;
354 
355 	dbg("%s - port %d", __FUNCTION__, port->number);
356 
357 	if (urb->status) {
358 		dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
359 		return;
360 	}
361 
362 	queue_task(&port->tqueue, &tq_immediate);
363 	mark_bh(IMMEDIATE_BH);
364 
365 	return;
366 
367 }
368 
369 
empeg_read_bulk_callback(struct urb * urb)370 static void empeg_read_bulk_callback (struct urb *urb)
371 {
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;
376 	int i;
377 	int result;
378 
379 	if (port_paranoia_check (port, __FUNCTION__))
380 		return;
381 
382 	dbg("%s - port %d", __FUNCTION__, port->number);
383 
384 	if (!serial) {
385 		dbg("%s - bad serial pointer, exiting", __FUNCTION__);
386 		return;
387 	}
388 
389 	if (urb->status) {
390 		dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
391 		return;
392 	}
393 
394 	usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
395 
396 	tty = port->tty;
397 
398 	if (urb->actual_length) {
399 		for (i = 0; i < urb->actual_length ; ++i) {
400 			/* gb - 2000/11/13
401 			 * If we insert too many characters we'll overflow the buffer.
402 			 * This means we'll lose bytes - Decidedly bad.
403 			 */
404 			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
405 				tty_flip_buffer_push(tty);
406 				}
407 			tty_insert_flip_char(tty, data[i], 0);
408 		}
409 		/* gb - 2000/11/13
410 		 * Goes straight through instead of scheduling - if tty->low_latency is set.
411 		 */
412 		tty_flip_buffer_push(tty);
413 		bytes_in += urb->actual_length;
414 	}
415 
416 	/* Continue trying to always read  */
417 	FILL_BULK_URB(
418 		port->read_urb,
419 		serial->dev,
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,
425 		port);
426 
427 	port->read_urb->transfer_flags |= USB_QUEUE_BULK;
428 
429 	result = usb_submit_urb(port->read_urb);
430 
431 	if (result)
432 		err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
433 
434 	return;
435 
436 }
437 
438 
empeg_throttle(struct usb_serial_port * port)439 static void empeg_throttle (struct usb_serial_port *port)
440 {
441 	dbg("%s - port %d", __FUNCTION__, port->number);
442 	usb_unlink_urb (port->read_urb);
443 }
444 
445 
empeg_unthrottle(struct usb_serial_port * port)446 static void empeg_unthrottle (struct usb_serial_port *port)
447 {
448 	int result;
449 
450 	dbg("%s - port %d", __FUNCTION__, port->number);
451 
452 	port->read_urb->dev = port->serial->dev;
453 
454 	result = usb_submit_urb(port->read_urb);
455 
456 	if (result)
457 		err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
458 
459 	return;
460 }
461 
462 
empeg_startup(struct usb_serial * serial)463 static int  empeg_startup (struct usb_serial *serial)
464 {
465 
466 	dbg("%s", __FUNCTION__);
467 
468 	dbg("%s - Set config to 1", __FUNCTION__);
469 	usb_set_configuration (serial->dev, 1);
470 
471 	/* continue on with initialization */
472 	return 0;
473 
474 }
475 
476 
empeg_shutdown(struct usb_serial * serial)477 static void empeg_shutdown (struct usb_serial *serial)
478 {
479 	dbg ("%s", __FUNCTION__);
480 }
481 
482 
empeg_ioctl(struct usb_serial_port * port,struct file * file,unsigned int cmd,unsigned long arg)483 static int empeg_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
484 {
485 	dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
486 
487 	return -ENOIOCTLCMD;
488 }
489 
490 
empeg_set_termios(struct usb_serial_port * port,struct termios * old_termios)491 static void empeg_set_termios (struct usb_serial_port *port, struct termios *old_termios)
492 {
493 
494 	dbg("%s - port %d", __FUNCTION__, port->number);
495 
496 	if ((!port->tty) || (!port->tty->termios)) {
497 		dbg("%s - no tty structures", __FUNCTION__);
498 		return;
499 	}
500 
501 	/*
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.
506          * (at least for now)
507          *
508          * The default requirements for this device are:
509          */
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 */
519 
520 	port->tty->termios->c_oflag
521 		&= ~OPOST;	/* disable postprocess output characters */
522 
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 */
529 
530 	port->tty->termios->c_cflag
531 		&= ~(CSIZE	/* no size */
532 		| PARENB	/* disable parity bit */
533 		| CBAUD);	/* clear current baud rate */
534 
535 	port->tty->termios->c_cflag
536 		|= (CS8		/* character size 8 bits */
537 		| B115200);	/* baud rate 115200 */
538 
539 	/*
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. :)
543 	 */
544 	port->tty->low_latency = 1;
545 
546 	/* Notify the tty driver that the termios have changed. */
547 	port->tty->ldisc.set_termios(port->tty, NULL);
548 
549 	return;
550 
551 }
552 
553 
empeg_init(void)554 static int __init empeg_init (void)
555 {
556 	struct urb *urb;
557 	int i;
558 
559 	usb_serial_register (&empeg_device);
560 
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;
566 		if (urb == NULL) {
567 			err("No more urbs???");
568 			continue;
569 		}
570 
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.",
575 			    __FUNCTION__);
576 			continue;
577 		}
578 	}
579 
580 	info(DRIVER_VERSION ":" DRIVER_DESC);
581 
582 	return 0;
583 
584 }
585 
586 
empeg_exit(void)587 static void __exit empeg_exit (void)
588 {
589 	int i;
590 	unsigned long flags;
591 
592 	usb_serial_deregister (&empeg_device);
593 
594 	spin_lock_irqsave (&write_urb_pool_lock, flags);
595 
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]);
605 		}
606 	}
607 
608 	spin_unlock_irqrestore (&write_urb_pool_lock, flags);
609 
610 }
611 
612 
613 module_init(empeg_init);
614 module_exit(empeg_exit);
615 
616 MODULE_AUTHOR( DRIVER_AUTHOR );
617 MODULE_DESCRIPTION( DRIVER_DESC );
618 MODULE_LICENSE("GPL");
619 
620 MODULE_PARM(debug, "i");
621 MODULE_PARM_DESC(debug, "Debug enabled or not");
622 
623