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