xref: /openbmc/linux/drivers/usb/serial/opticon.c (revision b627b4ed)
1 /*
2  * Opticon USB barcode to serial driver
3  *
4  * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
5  * Copyright (C) 2008 - 2009 Novell Inc.
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License version
9  *	2 as published by the Free Software Foundation.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/tty.h>
15 #include <linux/tty_driver.h>
16 #include <linux/tty_flip.h>
17 #include <linux/serial.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/uaccess.h>
22 
23 static int debug;
24 
25 static struct usb_device_id id_table[] = {
26 	{ USB_DEVICE(0x065a, 0x0009) },
27 	{ },
28 };
29 MODULE_DEVICE_TABLE(usb, id_table);
30 
31 /* This structure holds all of the individual device information */
32 struct opticon_private {
33 	struct usb_device *udev;
34 	struct usb_serial *serial;
35 	struct usb_serial_port *port;
36 	unsigned char *bulk_in_buffer;
37 	struct urb *bulk_read_urb;
38 	int buffer_size;
39 	u8 bulk_address;
40 	spinlock_t lock;	/* protects the following flags */
41 	bool throttled;
42 	bool actually_throttled;
43 	bool rts;
44 	int outstanding_urbs;
45 };
46 
47 /* max number of write urbs in flight */
48 #define URB_UPPER_LIMIT	4
49 
50 static void opticon_bulk_callback(struct urb *urb)
51 {
52 	struct opticon_private *priv = urb->context;
53 	unsigned char *data = urb->transfer_buffer;
54 	struct usb_serial_port *port = priv->port;
55 	int status = urb->status;
56 	struct tty_struct *tty;
57 	int result;
58 	int available_room = 0;
59 	int data_length;
60 
61 	dbg("%s - port %d", __func__, port->number);
62 
63 	switch (status) {
64 	case 0:
65 		/* success */
66 		break;
67 	case -ECONNRESET:
68 	case -ENOENT:
69 	case -ESHUTDOWN:
70 		/* this urb is terminated, clean up */
71 		dbg("%s - urb shutting down with status: %d",
72 		    __func__, status);
73 		return;
74 	default:
75 		dbg("%s - nonzero urb status received: %d",
76 		    __func__, status);
77 		goto exit;
78 	}
79 
80 	usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
81 			      data);
82 
83 	if (urb->actual_length > 2) {
84 		data_length = urb->actual_length - 2;
85 
86 		/*
87 		 * Data from the device comes with a 2 byte header:
88 		 *
89 		 * <0x00><0x00>data...
90 		 * 	This is real data to be sent to the tty layer
91 		 * <0x00><0x01)level
92 		 * 	This is a RTS level change, the third byte is the RTS
93 		 * 	value (0 for low, 1 for high).
94 		 */
95 		if ((data[0] == 0x00) && (data[1] == 0x00)) {
96 			/* real data, send it to the tty layer */
97 			tty = tty_port_tty_get(&port->port);
98 			if (tty) {
99 				available_room = tty_buffer_request_room(tty,
100 								data_length);
101 				if (available_room) {
102 					tty_insert_flip_string(tty, data,
103 							       available_room);
104 					tty_flip_buffer_push(tty);
105 				}
106 				tty_kref_put(tty);
107 			}
108 		} else {
109 			if ((data[0] == 0x00) && (data[1] == 0x01)) {
110 				if (data[2] == 0x00)
111 					priv->rts = false;
112 				else
113 					priv->rts = true;
114 			} else {
115 			dev_dbg(&priv->udev->dev,
116 				"Unknown data packet received from the device:"
117 				" %2x %2x\n",
118 				data[0], data[1]);
119 			}
120 		}
121 	} else {
122 		dev_dbg(&priv->udev->dev,
123 			"Improper ammount of data received from the device, "
124 			"%d bytes", urb->actual_length);
125 	}
126 
127 exit:
128 	spin_lock(&priv->lock);
129 
130 	/* Continue trying to always read if we should */
131 	if (!priv->throttled) {
132 		usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
133 				  usb_rcvbulkpipe(priv->udev,
134 						  priv->bulk_address),
135 				  priv->bulk_in_buffer, priv->buffer_size,
136 				  opticon_bulk_callback, priv);
137 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
138 		if (result)
139 			dev_err(&port->dev,
140 			    "%s - failed resubmitting read urb, error %d\n",
141 							__func__, result);
142 	} else
143 		priv->actually_throttled = true;
144 	spin_unlock(&priv->lock);
145 }
146 
147 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port,
148 			struct file *filp)
149 {
150 	struct opticon_private *priv = usb_get_serial_data(port->serial);
151 	unsigned long flags;
152 	int result = 0;
153 
154 	dbg("%s - port %d", __func__, port->number);
155 
156 	spin_lock_irqsave(&priv->lock, flags);
157 	priv->throttled = false;
158 	priv->actually_throttled = false;
159 	priv->port = port;
160 	spin_unlock_irqrestore(&priv->lock, flags);
161 
162 	/*
163 	 * Force low_latency on so that our tty_push actually forces the data
164 	 * through, otherwise it is scheduled, and with high data rates (like
165 	 * with OHCI) data can get lost.
166 	 */
167 	if (tty)
168 		tty->low_latency = 1;
169 
170 	/* Start reading from the device */
171 	usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
172 			  usb_rcvbulkpipe(priv->udev,
173 					  priv->bulk_address),
174 			  priv->bulk_in_buffer, priv->buffer_size,
175 			  opticon_bulk_callback, priv);
176 	result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
177 	if (result)
178 		dev_err(&port->dev,
179 			"%s - failed resubmitting read urb, error %d\n",
180 			__func__, result);
181 	return result;
182 }
183 
184 static void opticon_close(struct tty_struct *tty, struct usb_serial_port *port,
185 			  struct file *filp)
186 {
187 	struct opticon_private *priv = usb_get_serial_data(port->serial);
188 
189 	dbg("%s - port %d", __func__, port->number);
190 
191 	/* shutdown our urbs */
192 	usb_kill_urb(priv->bulk_read_urb);
193 }
194 
195 static void opticon_write_bulk_callback(struct urb *urb)
196 {
197 	struct opticon_private *priv = urb->context;
198 	int status = urb->status;
199 	unsigned long flags;
200 
201 	/* free up the transfer buffer, as usb_free_urb() does not do this */
202 	kfree(urb->transfer_buffer);
203 
204 	if (status)
205 		dbg("%s - nonzero write bulk status received: %d",
206 		    __func__, status);
207 
208 	spin_lock_irqsave(&priv->lock, flags);
209 	--priv->outstanding_urbs;
210 	spin_unlock_irqrestore(&priv->lock, flags);
211 
212 	usb_serial_port_softint(priv->port);
213 }
214 
215 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
216 			 const unsigned char *buf, int count)
217 {
218 	struct opticon_private *priv = usb_get_serial_data(port->serial);
219 	struct usb_serial *serial = port->serial;
220 	struct urb *urb;
221 	unsigned char *buffer;
222 	unsigned long flags;
223 	int status;
224 
225 	dbg("%s - port %d", __func__, port->number);
226 
227 	spin_lock_irqsave(&priv->lock, flags);
228 	if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
229 		spin_unlock_irqrestore(&priv->lock, flags);
230 		dbg("%s - write limit hit\n", __func__);
231 		return 0;
232 	}
233 	priv->outstanding_urbs++;
234 	spin_unlock_irqrestore(&priv->lock, flags);
235 
236 	buffer = kmalloc(count, GFP_ATOMIC);
237 	if (!buffer) {
238 		dev_err(&port->dev, "out of memory\n");
239 		count = -ENOMEM;
240 		goto error_no_buffer;
241 	}
242 
243 	urb = usb_alloc_urb(0, GFP_ATOMIC);
244 	if (!urb) {
245 		dev_err(&port->dev, "no more free urbs\n");
246 		count = -ENOMEM;
247 		goto error_no_urb;
248 	}
249 
250 	memcpy(buffer, buf, count);
251 
252 	usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
253 
254 	usb_fill_bulk_urb(urb, serial->dev,
255 			  usb_sndbulkpipe(serial->dev,
256 					  port->bulk_out_endpointAddress),
257 			  buffer, count, opticon_write_bulk_callback, priv);
258 
259 	/* send it down the pipe */
260 	status = usb_submit_urb(urb, GFP_ATOMIC);
261 	if (status) {
262 		dev_err(&port->dev,
263 		   "%s - usb_submit_urb(write bulk) failed with status = %d\n",
264 							__func__, status);
265 		count = status;
266 		goto error;
267 	}
268 
269 	/* we are done with this urb, so let the host driver
270 	 * really free it when it is finished with it */
271 	usb_free_urb(urb);
272 
273 	return count;
274 error:
275 	usb_free_urb(urb);
276 error_no_urb:
277 	kfree(buffer);
278 error_no_buffer:
279 	spin_lock_irqsave(&priv->lock, flags);
280 	--priv->outstanding_urbs;
281 	spin_unlock_irqrestore(&priv->lock, flags);
282 	return count;
283 }
284 
285 static int opticon_write_room(struct tty_struct *tty)
286 {
287 	struct usb_serial_port *port = tty->driver_data;
288 	struct opticon_private *priv = usb_get_serial_data(port->serial);
289 	unsigned long flags;
290 
291 	dbg("%s - port %d", __func__, port->number);
292 
293 	/*
294 	 * We really can take almost anything the user throws at us
295 	 * but let's pick a nice big number to tell the tty
296 	 * layer that we have lots of free space, unless we don't.
297 	 */
298 	spin_lock_irqsave(&priv->lock, flags);
299 	if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
300 		spin_unlock_irqrestore(&priv->lock, flags);
301 		dbg("%s - write limit hit\n", __func__);
302 		return 0;
303 	}
304 	spin_unlock_irqrestore(&priv->lock, flags);
305 
306 	return 2048;
307 }
308 
309 static void opticon_throttle(struct tty_struct *tty)
310 {
311 	struct usb_serial_port *port = tty->driver_data;
312 	struct opticon_private *priv = usb_get_serial_data(port->serial);
313 	unsigned long flags;
314 
315 	dbg("%s - port %d", __func__, port->number);
316 	spin_lock_irqsave(&priv->lock, flags);
317 	priv->throttled = true;
318 	spin_unlock_irqrestore(&priv->lock, flags);
319 }
320 
321 
322 static void opticon_unthrottle(struct tty_struct *tty)
323 {
324 	struct usb_serial_port *port = tty->driver_data;
325 	struct opticon_private *priv = usb_get_serial_data(port->serial);
326 	unsigned long flags;
327 	int result;
328 
329 	dbg("%s - port %d", __func__, port->number);
330 
331 	spin_lock_irqsave(&priv->lock, flags);
332 	priv->throttled = false;
333 	priv->actually_throttled = false;
334 	spin_unlock_irqrestore(&priv->lock, flags);
335 
336 	priv->bulk_read_urb->dev = port->serial->dev;
337 	result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
338 	if (result)
339 		dev_err(&port->dev,
340 			"%s - failed submitting read urb, error %d\n",
341 							__func__, result);
342 }
343 
344 static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
345 {
346 	struct usb_serial_port *port = tty->driver_data;
347 	struct opticon_private *priv = usb_get_serial_data(port->serial);
348 	unsigned long flags;
349 	int result = 0;
350 
351 	dbg("%s - port %d", __func__, port->number);
352 
353 	spin_lock_irqsave(&priv->lock, flags);
354 	if (priv->rts)
355 		result = TIOCM_RTS;
356 	spin_unlock_irqrestore(&priv->lock, flags);
357 
358 	dbg("%s - %x", __func__, result);
359 	return result;
360 }
361 
362 static int get_serial_info(struct opticon_private *priv,
363 			   struct serial_struct __user *serial)
364 {
365 	struct serial_struct tmp;
366 
367 	if (!serial)
368 		return -EFAULT;
369 
370 	memset(&tmp, 0x00, sizeof(tmp));
371 
372 	/* fake emulate a 16550 uart to make userspace code happy */
373 	tmp.type		= PORT_16550A;
374 	tmp.line		= priv->serial->minor;
375 	tmp.port		= 0;
376 	tmp.irq			= 0;
377 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
378 	tmp.xmit_fifo_size	= 1024;
379 	tmp.baud_base		= 9600;
380 	tmp.close_delay		= 5*HZ;
381 	tmp.closing_wait	= 30*HZ;
382 
383 	if (copy_to_user(serial, &tmp, sizeof(*serial)))
384 		return -EFAULT;
385 	return 0;
386 }
387 
388 static int opticon_ioctl(struct tty_struct *tty, struct file *file,
389 			 unsigned int cmd, unsigned long arg)
390 {
391 	struct usb_serial_port *port = tty->driver_data;
392 	struct opticon_private *priv = usb_get_serial_data(port->serial);
393 
394 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
395 
396 	switch (cmd) {
397 	case TIOCGSERIAL:
398 		return get_serial_info(priv,
399 				       (struct serial_struct __user *)arg);
400 	}
401 
402 	return -ENOIOCTLCMD;
403 }
404 
405 static int opticon_startup(struct usb_serial *serial)
406 {
407 	struct opticon_private *priv;
408 	struct usb_host_interface *intf;
409 	int i;
410 	int retval = -ENOMEM;
411 	bool bulk_in_found = false;
412 
413 	/* create our private serial structure */
414 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
415 	if (priv == NULL) {
416 		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
417 		return -ENOMEM;
418 	}
419 	spin_lock_init(&priv->lock);
420 	priv->serial = serial;
421 	priv->port = serial->port[0];
422 	priv->udev = serial->dev;
423 
424 	/* find our bulk endpoint */
425 	intf = serial->interface->altsetting;
426 	for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
427 		struct usb_endpoint_descriptor *endpoint;
428 
429 		endpoint = &intf->endpoint[i].desc;
430 		if (!usb_endpoint_is_bulk_in(endpoint))
431 			continue;
432 
433 		priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
434 		if (!priv->bulk_read_urb) {
435 			dev_err(&priv->udev->dev, "out of memory\n");
436 			goto error;
437 		}
438 
439 		priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
440 		priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
441 		if (!priv->bulk_in_buffer) {
442 			dev_err(&priv->udev->dev, "out of memory\n");
443 			goto error;
444 		}
445 
446 		priv->bulk_address = endpoint->bEndpointAddress;
447 
448 		/* set up our bulk urb */
449 		usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
450 				  usb_rcvbulkpipe(priv->udev,
451 						  endpoint->bEndpointAddress),
452 				  priv->bulk_in_buffer, priv->buffer_size,
453 				  opticon_bulk_callback, priv);
454 
455 		bulk_in_found = true;
456 		break;
457 		}
458 
459 	if (!bulk_in_found) {
460 		dev_err(&priv->udev->dev,
461 			"Error - the proper endpoints were not found!\n");
462 		goto error;
463 	}
464 
465 	usb_set_serial_data(serial, priv);
466 	return 0;
467 
468 error:
469 	usb_free_urb(priv->bulk_read_urb);
470 	kfree(priv->bulk_in_buffer);
471 	kfree(priv);
472 	return retval;
473 }
474 
475 static void opticon_shutdown(struct usb_serial *serial)
476 {
477 	struct opticon_private *priv = usb_get_serial_data(serial);
478 
479 	dbg("%s", __func__);
480 
481 	usb_kill_urb(priv->bulk_read_urb);
482 	usb_free_urb(priv->bulk_read_urb);
483 	kfree(priv->bulk_in_buffer);
484 	kfree(priv);
485 	usb_set_serial_data(serial, NULL);
486 }
487 
488 static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
489 {
490 	struct usb_serial *serial = usb_get_intfdata(intf);
491 	struct opticon_private *priv = usb_get_serial_data(serial);
492 
493 	usb_kill_urb(priv->bulk_read_urb);
494 	return 0;
495 }
496 
497 static int opticon_resume(struct usb_interface *intf)
498 {
499 	struct usb_serial *serial = usb_get_intfdata(intf);
500 	struct opticon_private *priv = usb_get_serial_data(serial);
501 	struct usb_serial_port *port = serial->port[0];
502 	int result;
503 
504 	mutex_lock(&port->mutex);
505 	if (port->port.count)
506 		result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
507 	else
508 		result = 0;
509 	mutex_unlock(&port->mutex);
510 	return result;
511 }
512 
513 static struct usb_driver opticon_driver = {
514 	.name =		"opticon",
515 	.probe =	usb_serial_probe,
516 	.disconnect =	usb_serial_disconnect,
517 	.suspend =	opticon_suspend,
518 	.resume =	opticon_resume,
519 	.id_table =	id_table,
520 	.no_dynamic_id = 	1,
521 };
522 
523 static struct usb_serial_driver opticon_device = {
524 	.driver = {
525 		.owner =	THIS_MODULE,
526 		.name =		"opticon",
527 	},
528 	.id_table =		id_table,
529 	.usb_driver = 		&opticon_driver,
530 	.num_ports =		1,
531 	.attach =		opticon_startup,
532 	.open =			opticon_open,
533 	.close =		opticon_close,
534 	.write =		opticon_write,
535 	.write_room = 		opticon_write_room,
536 	.shutdown =		opticon_shutdown,
537 	.throttle = 		opticon_throttle,
538 	.unthrottle =		opticon_unthrottle,
539 	.ioctl =		opticon_ioctl,
540 	.tiocmget =		opticon_tiocmget,
541 };
542 
543 static int __init opticon_init(void)
544 {
545 	int retval;
546 
547 	retval = usb_serial_register(&opticon_device);
548 	if (retval)
549 		return retval;
550 	retval = usb_register(&opticon_driver);
551 	if (retval)
552 		usb_serial_deregister(&opticon_device);
553 	return retval;
554 }
555 
556 static void __exit opticon_exit(void)
557 {
558 	usb_deregister(&opticon_driver);
559 	usb_serial_deregister(&opticon_device);
560 }
561 
562 module_init(opticon_init);
563 module_exit(opticon_exit);
564 MODULE_LICENSE("GPL");
565 
566 module_param(debug, bool, S_IRUGO | S_IWUSR);
567 MODULE_PARM_DESC(debug, "Debug enabled or not");
568