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