xref: /openbmc/linux/drivers/usb/serial/empeg.c (revision 78c99ba1)
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
15  * driver
16  *
17  * (07/16/2001) gb
18  *	remove unused code in empeg_close() (thanks to Oliver Neukum for
19  *	pointing this out) and rewrote empeg_set_termios().
20  *
21  * (05/30/2001) gkh
22  *	switched from using spinlock to a semaphore, which fixes lots of
23  * problems.
24  *
25  * (04/08/2001) gb
26  *      Identify version on module load.
27  *
28  * (01/22/2001) gb
29  *	Added write_room() and chars_in_buffer() support.
30  *
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().
34  *
35  * (12/03/2000) gb
36  *	Added tty->ldisc.set_termios(port, tty, NULL) to empeg_open().
37  *	This notifies the tty driver that the termios have changed.
38  *
39  * (11/13/2000) gb
40  *	Moved tty->low_latency = 1 from empeg_read_bulk_callback() to
41  *	empeg_open() (It only needs to be set once - Doh!)
42  *
43  * (11/11/2000) gb
44  *	Updated to work with id_table structure.
45  *
46  * (11/04/2000) gb
47  *	Forked this from visor.c, and hacked it up to work with an
48  *	Empeg ltd. empeg-car player.  Constructive criticism welcomed.
49  *	I would like to say, 'Thank You' to Greg Kroah-Hartman for the
50  *	use of his code, and for his guidance, advice and patience. :)
51  *	A 'Thank You' is in order for John Ripley of Empeg ltd for his
52  *	advice, and patience too.
53  *
54  */
55 
56 #include <linux/kernel.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
60 #include <linux/tty.h>
61 #include <linux/tty_driver.h>
62 #include <linux/tty_flip.h>
63 #include <linux/module.h>
64 #include <linux/spinlock.h>
65 #include <linux/uaccess.h>
66 #include <linux/usb.h>
67 #include <linux/usb/serial.h>
68 
69 static int debug;
70 
71 /*
72  * Version Information
73  */
74 #define DRIVER_VERSION "v1.2"
75 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
76 #define DRIVER_DESC "USB Empeg Mark I/II Driver"
77 
78 #define EMPEG_VENDOR_ID			0x084f
79 #define EMPEG_PRODUCT_ID		0x0001
80 
81 /* function prototypes for an empeg-car player */
82 static int  empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
83 						struct file *filp);
84 static void empeg_close(struct usb_serial_port *port);
85 static int  empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
86 						const unsigned char *buf,
87 						int count);
88 static int  empeg_write_room(struct tty_struct *tty);
89 static int  empeg_chars_in_buffer(struct tty_struct *tty);
90 static void empeg_throttle(struct tty_struct *tty);
91 static void empeg_unthrottle(struct tty_struct *tty);
92 static int  empeg_startup(struct usb_serial *serial);
93 static void empeg_shutdown(struct usb_serial *serial);
94 static void empeg_set_termios(struct tty_struct *tty,
95 		struct usb_serial_port *port, struct ktermios *old_termios);
96 static void empeg_write_bulk_callback(struct urb *urb);
97 static void empeg_read_bulk_callback(struct urb *urb);
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 	.usb_driver = 		&empeg_driver,
121 	.num_ports =		1,
122 	.open =			empeg_open,
123 	.close =		empeg_close,
124 	.throttle =		empeg_throttle,
125 	.unthrottle =		empeg_unthrottle,
126 	.attach =		empeg_startup,
127 	.shutdown =		empeg_shutdown,
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  ******************************************************************************/
147 static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
148 				struct file *filp)
149 {
150 	struct usb_serial *serial = port->serial;
151 	int result = 0;
152 
153 	dbg("%s - port %d", __func__, port->number);
154 
155 	/* Force default termio settings */
156 	empeg_set_termios(tty, port, NULL) ;
157 
158 	bytes_in = 0;
159 	bytes_out = 0;
160 
161 	/* Start reading from the device */
162 	usb_fill_bulk_urb(
163 		port->read_urb,
164 		serial->dev,
165 		usb_rcvbulkpipe(serial->dev,
166 			port->bulk_in_endpointAddress),
167 		port->read_urb->transfer_buffer,
168 		port->read_urb->transfer_buffer_length,
169 		empeg_read_bulk_callback,
170 		port);
171 
172 	result = usb_submit_urb(port->read_urb, GFP_KERNEL);
173 
174 	if (result)
175 		dev_err(&port->dev,
176 			"%s - failed submitting read urb, error %d\n",
177 							__func__, result);
178 
179 	return result;
180 }
181 
182 
183 static void empeg_close(struct usb_serial_port *port)
184 {
185 	dbg("%s - port %d", __func__, 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 tty_struct *tty, struct usb_serial_port *port,
195 					const unsigned char *buf, int count)
196 {
197 	struct usb_serial *serial = port->serial;
198 	struct urb *urb;
199 	const unsigned char *current_position = buf;
200 	unsigned long flags;
201 	int status;
202 	int i;
203 	int bytes_sent = 0;
204 	int transfer_size;
205 
206 	dbg("%s - port %d", __func__, port->number);
207 
208 	while (count > 0) {
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", __func__);
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,
232 					"%s no more kernel memory...\n",
233 								__func__);
234 				goto exit;
235 			}
236 		}
237 
238 		transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
239 
240 		memcpy(urb->transfer_buffer, current_position, transfer_size);
241 
242 		usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer);
243 
244 		/* build up our urb */
245 		usb_fill_bulk_urb(
246 			urb,
247 			serial->dev,
248 			usb_sndbulkpipe(serial->dev,
249 					port->bulk_out_endpointAddress),
250 			urb->transfer_buffer,
251 			transfer_size,
252 			empeg_write_bulk_callback,
253 			port);
254 
255 		/* send it down the pipe */
256 		status = usb_submit_urb(urb, GFP_ATOMIC);
257 		if (status) {
258 			dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
259 			bytes_sent = status;
260 			break;
261 		}
262 
263 		current_position += transfer_size;
264 		bytes_sent += transfer_size;
265 		count -= transfer_size;
266 		bytes_out += transfer_size;
267 
268 	}
269 exit:
270 	return bytes_sent;
271 }
272 
273 
274 static int empeg_write_room(struct tty_struct *tty)
275 {
276 	struct usb_serial_port *port = tty->driver_data;
277 	unsigned long flags;
278 	int i;
279 	int room = 0;
280 
281 	dbg("%s - port %d", __func__, port->number);
282 
283 	spin_lock_irqsave(&write_urb_pool_lock, flags);
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 	spin_unlock_irqrestore(&write_urb_pool_lock, flags);
290 	dbg("%s - returns %d", __func__, room);
291 	return room;
292 
293 }
294 
295 
296 static int empeg_chars_in_buffer(struct tty_struct *tty)
297 {
298 	struct usb_serial_port *port = tty->driver_data;
299 	unsigned long flags;
300 	int i;
301 	int chars = 0;
302 
303 	dbg("%s - port %d", __func__, port->number);
304 
305 	spin_lock_irqsave(&write_urb_pool_lock, flags);
306 
307 	/* tally up the number of bytes waiting */
308 	for (i = 0; i < NUM_URBS; ++i) {
309 		if (write_urb_pool[i]->status == -EINPROGRESS)
310 			chars += URB_TRANSFER_BUFFER_SIZE;
311 	}
312 
313 	spin_unlock_irqrestore(&write_urb_pool_lock, flags);
314 	dbg("%s - returns %d", __func__, chars);
315 	return chars;
316 }
317 
318 
319 static void empeg_write_bulk_callback(struct urb *urb)
320 {
321 	struct usb_serial_port *port = urb->context;
322 	int status = urb->status;
323 
324 	dbg("%s - port %d", __func__, port->number);
325 
326 	if (status) {
327 		dbg("%s - nonzero write bulk status received: %d",
328 		    __func__, status);
329 		return;
330 	}
331 
332 	usb_serial_port_softint(port);
333 }
334 
335 
336 static void empeg_read_bulk_callback(struct urb *urb)
337 {
338 	struct usb_serial_port *port = urb->context;
339 	struct tty_struct *tty;
340 	unsigned char *data = urb->transfer_buffer;
341 	int result;
342 	int status = urb->status;
343 
344 	dbg("%s - port %d", __func__, port->number);
345 
346 	if (status) {
347 		dbg("%s - nonzero read bulk status received: %d",
348 		    __func__, status);
349 		return;
350 	}
351 
352 	usb_serial_debug_data(debug, &port->dev, __func__,
353 						urb->actual_length, data);
354 	tty = tty_port_tty_get(&port->port);
355 
356 	if (urb->actual_length) {
357 		tty_buffer_request_room(tty, urb->actual_length);
358 		tty_insert_flip_string(tty, data, urb->actual_length);
359 		tty_flip_buffer_push(tty);
360 		bytes_in += urb->actual_length;
361 	}
362 	tty_kref_put(tty);
363 
364 	/* Continue trying to always read  */
365 	usb_fill_bulk_urb(
366 		port->read_urb,
367 		port->serial->dev,
368 		usb_rcvbulkpipe(port->serial->dev,
369 			port->bulk_in_endpointAddress),
370 		port->read_urb->transfer_buffer,
371 		port->read_urb->transfer_buffer_length,
372 		empeg_read_bulk_callback,
373 		port);
374 
375 	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
376 
377 	if (result)
378 		dev_err(&urb->dev->dev,
379 			"%s - failed resubmitting read urb, error %d\n",
380 							__func__, result);
381 
382 	return;
383 
384 }
385 
386 
387 static void empeg_throttle(struct tty_struct *tty)
388 {
389 	struct usb_serial_port *port = tty->driver_data;
390 	dbg("%s - port %d", __func__, port->number);
391 	usb_kill_urb(port->read_urb);
392 }
393 
394 
395 static void empeg_unthrottle(struct tty_struct *tty)
396 {
397 	struct usb_serial_port *port = tty->driver_data;
398 	int result;
399 	dbg("%s - port %d", __func__, port->number);
400 
401 	port->read_urb->dev = port->serial->dev;
402 	result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
403 	if (result)
404 		dev_err(&port->dev,
405 			"%s - failed submitting read urb, error %d\n",
406 							__func__, result);
407 }
408 
409 
410 static int  empeg_startup(struct usb_serial *serial)
411 {
412 	int r;
413 
414 	dbg("%s", __func__);
415 
416 	if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
417 		dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
418 			serial->dev->actconfig->desc.bConfigurationValue);
419 		return -ENODEV;
420 	}
421 	dbg("%s - reset config", __func__);
422 	r = usb_reset_configuration(serial->dev);
423 
424 	/* continue on with initialization */
425 	return r;
426 
427 }
428 
429 
430 static void empeg_shutdown(struct usb_serial *serial)
431 {
432 	dbg("%s", __func__);
433 }
434 
435 
436 static void empeg_set_termios(struct tty_struct *tty,
437 		struct usb_serial_port *port, struct ktermios *old_termios)
438 {
439 	struct ktermios *termios = tty->termios;
440 	dbg("%s - port %d", __func__, port->number);
441 
442 	/*
443 	 * The empeg-car player wants these particular tty settings.
444 	 * You could, for example, change the baud rate, however the
445 	 * player only supports 115200 (currently), so there is really
446 	 * no point in support for changes to the tty settings.
447 	 * (at least for now)
448 	 *
449 	 * The default requirements for this device are:
450 	 */
451 	termios->c_iflag
452 		&= ~(IGNBRK	/* disable ignore break */
453 		| BRKINT	/* disable break causes interrupt */
454 		| PARMRK	/* disable mark parity errors */
455 		| ISTRIP	/* disable clear high bit of input characters */
456 		| INLCR		/* disable translate NL to CR */
457 		| IGNCR		/* disable ignore CR */
458 		| ICRNL		/* disable translate CR to NL */
459 		| IXON);	/* disable enable XON/XOFF flow control */
460 
461 	termios->c_oflag
462 		&= ~OPOST;	/* disable postprocess output characters */
463 
464 	termios->c_lflag
465 		&= ~(ECHO	/* disable echo input characters */
466 		| ECHONL	/* disable echo new line */
467 		| ICANON	/* disable erase, kill, werase, and rprnt special characters */
468 		| ISIG		/* disable interrupt, quit, and suspend special characters */
469 		| IEXTEN);	/* disable non-POSIX special characters */
470 
471 	termios->c_cflag
472 		&= ~(CSIZE	/* no size */
473 		| PARENB	/* disable parity bit */
474 		| CBAUD);	/* clear current baud rate */
475 
476 	termios->c_cflag
477 		|= CS8;		/* character size 8 bits */
478 
479 	tty_encode_baud_rate(tty, 115200, 115200);
480 }
481 
482 
483 static int __init empeg_init(void)
484 {
485 	struct urb *urb;
486 	int i, retval;
487 
488 	/* create our write urb pool and transfer buffers */
489 	spin_lock_init(&write_urb_pool_lock);
490 	for (i = 0; i < NUM_URBS; ++i) {
491 		urb = usb_alloc_urb(0, GFP_KERNEL);
492 		write_urb_pool[i] = urb;
493 		if (urb == NULL) {
494 			printk(KERN_ERR "empeg: No more urbs???\n");
495 			continue;
496 		}
497 
498 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
499 								GFP_KERNEL);
500 		if (!urb->transfer_buffer) {
501 			printk(KERN_ERR "empeg: %s - out of memory for urb "
502 			       "buffers.", __func__);
503 			continue;
504 		}
505 	}
506 
507 	retval = usb_serial_register(&empeg_device);
508 	if (retval)
509 		goto failed_usb_serial_register;
510 	retval = usb_register(&empeg_driver);
511 	if (retval)
512 		goto failed_usb_register;
513 
514 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
515 	       DRIVER_DESC "\n");
516 
517 	return 0;
518 failed_usb_register:
519 	usb_serial_deregister(&empeg_device);
520 failed_usb_serial_register:
521 	for (i = 0; i < NUM_URBS; ++i) {
522 		if (write_urb_pool[i]) {
523 			kfree(write_urb_pool[i]->transfer_buffer);
524 			usb_free_urb(write_urb_pool[i]);
525 		}
526 	}
527 	return retval;
528 }
529 
530 
531 static void __exit empeg_exit(void)
532 {
533 	int i;
534 	unsigned long flags;
535 
536 	usb_deregister(&empeg_driver);
537 	usb_serial_deregister(&empeg_device);
538 
539 	spin_lock_irqsave(&write_urb_pool_lock, flags);
540 
541 	for (i = 0; i < NUM_URBS; ++i) {
542 		if (write_urb_pool[i]) {
543 			/* FIXME - uncomment the following usb_kill_urb call
544 			 * when the host controllers get fixed to set urb->dev
545 			 * = NULL after the urb is finished.  Otherwise this
546 			 * call oopses. */
547 			/* usb_kill_urb(write_urb_pool[i]); */
548 			kfree(write_urb_pool[i]->transfer_buffer);
549 			usb_free_urb(write_urb_pool[i]);
550 		}
551 	}
552 	spin_unlock_irqrestore(&write_urb_pool_lock, flags);
553 }
554 
555 
556 module_init(empeg_init);
557 module_exit(empeg_exit);
558 
559 MODULE_AUTHOR(DRIVER_AUTHOR);
560 MODULE_DESCRIPTION(DRIVER_DESC);
561 MODULE_LICENSE("GPL");
562 
563 module_param(debug, bool, S_IRUGO | S_IWUSR);
564 MODULE_PARM_DESC(debug, "Debug enabled or not");
565