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