xref: /openbmc/linux/drivers/usb/serial/opticon.c (revision 840ef8b7cc584a23c4f9d05352f4dbaf8e56e5ab)
1 /*
2  * Opticon USB barcode to serial driver
3  *
4  * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com>
5  * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
6  * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
7  * Copyright (C) 2008 - 2009 Novell Inc.
8  *
9  *	This program is free software; you can redistribute it and/or
10  *	modify it under the terms of the GNU General Public License version
11  *	2 as published by the Free Software Foundation.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/tty.h>
17 #include <linux/tty_driver.h>
18 #include <linux/slab.h>
19 #include <linux/tty_flip.h>
20 #include <linux/serial.h>
21 #include <linux/module.h>
22 #include <linux/usb.h>
23 #include <linux/usb/serial.h>
24 #include <linux/uaccess.h>
25 
26 #define CONTROL_RTS			0x02
27 #define RESEND_CTS_STATE	0x03
28 
29 /* max number of write urbs in flight */
30 #define URB_UPPER_LIMIT	8
31 
32 /* This driver works for the Opticon 1D barcode reader
33  * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
34 #define DRIVER_DESC	"Opticon USB barcode to serial driver (1D)"
35 
36 static const struct usb_device_id id_table[] = {
37 	{ USB_DEVICE(0x065a, 0x0009) },
38 	{ },
39 };
40 MODULE_DEVICE_TABLE(usb, id_table);
41 
42 /* This structure holds all of the individual device information */
43 struct opticon_private {
44 	spinlock_t lock;	/* protects the following flags */
45 	bool rts;
46 	bool cts;
47 	int outstanding_urbs;
48 };
49 
50 
51 static void opticon_process_data_packet(struct usb_serial_port *port,
52 					const unsigned char *buf, size_t len)
53 {
54 	tty_insert_flip_string(&port->port, buf, len);
55 	tty_flip_buffer_push(&port->port);
56 }
57 
58 static void opticon_process_status_packet(struct usb_serial_port *port,
59 					const unsigned char *buf, size_t len)
60 {
61 	struct opticon_private *priv = usb_get_serial_port_data(port);
62 	unsigned long flags;
63 
64 	spin_lock_irqsave(&priv->lock, flags);
65 	if (buf[0] == 0x00)
66 		priv->cts = false;
67 	else
68 		priv->cts = true;
69 	spin_unlock_irqrestore(&priv->lock, flags);
70 }
71 
72 static void opticon_process_read_urb(struct urb *urb)
73 {
74 	struct usb_serial_port *port = urb->context;
75 	const unsigned char *hdr = urb->transfer_buffer;
76 	const unsigned char *data = hdr + 2;
77 	size_t data_len = urb->actual_length - 2;
78 
79 	if (urb->actual_length <= 2) {
80 		dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
81 							urb->actual_length);
82 		return;
83 	}
84 	/*
85 	 * Data from the device comes with a 2 byte header:
86 	 *
87 	 * <0x00><0x00>data...
88 	 *      This is real data to be sent to the tty layer
89 	 * <0x00><0x01>level
90 	 *      This is a CTS level change, the third byte is the CTS
91 	 *      value (0 for low, 1 for high).
92 	 */
93 	if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
94 		opticon_process_data_packet(port, data, data_len);
95 	} else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
96 		opticon_process_status_packet(port, data, data_len);
97 	} else {
98 		dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
99 							hdr[0], hdr[1]);
100 	}
101 }
102 
103 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
104 				u8 val)
105 {
106 	struct usb_serial *serial = port->serial;
107 	int retval;
108 	u8 *buffer;
109 
110 	buffer = kzalloc(1, GFP_KERNEL);
111 	if (!buffer)
112 		return -ENOMEM;
113 
114 	buffer[0] = val;
115 	/* Send the message to the vendor control endpoint
116 	 * of the connected device */
117 	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
118 				requesttype,
119 				USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
120 				0, 0, buffer, 1, 0);
121 	kfree(buffer);
122 
123 	return retval;
124 }
125 
126 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
127 {
128 	struct opticon_private *priv = usb_get_serial_port_data(port);
129 	unsigned long flags;
130 	int res;
131 
132 	spin_lock_irqsave(&priv->lock, flags);
133 	priv->rts = false;
134 	spin_unlock_irqrestore(&priv->lock, flags);
135 
136 	/* Clear RTS line */
137 	send_control_msg(port, CONTROL_RTS, 0);
138 
139 	/* clear the halt status of the enpoint */
140 	usb_clear_halt(port->serial->dev, port->read_urb->pipe);
141 
142 	res = usb_serial_generic_open(tty, port);
143 	if (!res)
144 		return res;
145 
146 	/* Request CTS line state, sometimes during opening the current
147 	 * CTS state can be missed. */
148 	send_control_msg(port, RESEND_CTS_STATE, 1);
149 
150 	return res;
151 }
152 
153 static void opticon_write_control_callback(struct urb *urb)
154 {
155 	struct usb_serial_port *port = urb->context;
156 	struct opticon_private *priv = usb_get_serial_port_data(port);
157 	int status = urb->status;
158 	unsigned long flags;
159 
160 	/* free up the transfer buffer, as usb_free_urb() does not do this */
161 	kfree(urb->transfer_buffer);
162 
163 	/* setup packet may be set if we're using it for writing */
164 	kfree(urb->setup_packet);
165 
166 	if (status)
167 		dev_dbg(&port->dev,
168 			"%s - non-zero urb status received: %d\n",
169 			__func__, status);
170 
171 	spin_lock_irqsave(&priv->lock, flags);
172 	--priv->outstanding_urbs;
173 	spin_unlock_irqrestore(&priv->lock, flags);
174 
175 	usb_serial_port_softint(port);
176 }
177 
178 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
179 			 const unsigned char *buf, int count)
180 {
181 	struct opticon_private *priv = usb_get_serial_port_data(port);
182 	struct usb_serial *serial = port->serial;
183 	struct urb *urb;
184 	unsigned char *buffer;
185 	unsigned long flags;
186 	int status;
187 	struct usb_ctrlrequest *dr;
188 
189 	spin_lock_irqsave(&priv->lock, flags);
190 	if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
191 		spin_unlock_irqrestore(&priv->lock, flags);
192 		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
193 		return 0;
194 	}
195 	priv->outstanding_urbs++;
196 	spin_unlock_irqrestore(&priv->lock, flags);
197 
198 	buffer = kmalloc(count, GFP_ATOMIC);
199 	if (!buffer) {
200 		dev_err(&port->dev, "out of memory\n");
201 		count = -ENOMEM;
202 
203 		goto error_no_buffer;
204 	}
205 
206 	urb = usb_alloc_urb(0, GFP_ATOMIC);
207 	if (!urb) {
208 		dev_err(&port->dev, "no more free urbs\n");
209 		count = -ENOMEM;
210 		goto error_no_urb;
211 	}
212 
213 	memcpy(buffer, buf, count);
214 
215 	usb_serial_debug_data(&port->dev, __func__, count, buffer);
216 
217 	/* The conncected devices do not have a bulk write endpoint,
218 	 * to transmit data to de barcode device the control endpoint is used */
219 	dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
220 	if (!dr) {
221 		dev_err(&port->dev, "out of memory\n");
222 		count = -ENOMEM;
223 		goto error_no_dr;
224 	}
225 
226 	dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
227 	dr->bRequest = 0x01;
228 	dr->wValue = 0;
229 	dr->wIndex = 0;
230 	dr->wLength = cpu_to_le16(count);
231 
232 	usb_fill_control_urb(urb, serial->dev,
233 		usb_sndctrlpipe(serial->dev, 0),
234 		(unsigned char *)dr, buffer, count,
235 		opticon_write_control_callback, port);
236 
237 	/* send it down the pipe */
238 	status = usb_submit_urb(urb, GFP_ATOMIC);
239 	if (status) {
240 		dev_err(&port->dev,
241 		"%s - usb_submit_urb(write endpoint) failed status = %d\n",
242 							__func__, status);
243 		count = status;
244 		goto error;
245 	}
246 
247 	/* we are done with this urb, so let the host driver
248 	 * really free it when it is finished with it */
249 	usb_free_urb(urb);
250 
251 	return count;
252 error:
253 	kfree(dr);
254 error_no_dr:
255 	usb_free_urb(urb);
256 error_no_urb:
257 	kfree(buffer);
258 error_no_buffer:
259 	spin_lock_irqsave(&priv->lock, flags);
260 	--priv->outstanding_urbs;
261 	spin_unlock_irqrestore(&priv->lock, flags);
262 	return count;
263 }
264 
265 static int opticon_write_room(struct tty_struct *tty)
266 {
267 	struct usb_serial_port *port = tty->driver_data;
268 	struct opticon_private *priv = usb_get_serial_port_data(port);
269 	unsigned long flags;
270 
271 	/*
272 	 * We really can take almost anything the user throws at us
273 	 * but let's pick a nice big number to tell the tty
274 	 * layer that we have lots of free space, unless we don't.
275 	 */
276 	spin_lock_irqsave(&priv->lock, flags);
277 	if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
278 		spin_unlock_irqrestore(&priv->lock, flags);
279 		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
280 		return 0;
281 	}
282 	spin_unlock_irqrestore(&priv->lock, flags);
283 
284 	return 2048;
285 }
286 
287 static int opticon_tiocmget(struct tty_struct *tty)
288 {
289 	struct usb_serial_port *port = tty->driver_data;
290 	struct opticon_private *priv = usb_get_serial_port_data(port);
291 	unsigned long flags;
292 	int result = 0;
293 
294 	spin_lock_irqsave(&priv->lock, flags);
295 	if (priv->rts)
296 		result |= TIOCM_RTS;
297 	if (priv->cts)
298 		result |= TIOCM_CTS;
299 	spin_unlock_irqrestore(&priv->lock, flags);
300 
301 	dev_dbg(&port->dev, "%s - %x\n", __func__, result);
302 	return result;
303 }
304 
305 static int opticon_tiocmset(struct tty_struct *tty,
306 			   unsigned int set, unsigned int clear)
307 {
308 	struct usb_serial_port *port = tty->driver_data;
309 	struct usb_serial *serial = port->serial;
310 	struct opticon_private *priv = usb_get_serial_port_data(port);
311 	unsigned long flags;
312 	bool rts;
313 	bool changed = false;
314 	int ret;
315 
316 	/* We only support RTS so we only handle that */
317 	spin_lock_irqsave(&priv->lock, flags);
318 
319 	rts = priv->rts;
320 	if (set & TIOCM_RTS)
321 		priv->rts = true;
322 	if (clear & TIOCM_RTS)
323 		priv->rts = false;
324 	changed = rts ^ priv->rts;
325 	spin_unlock_irqrestore(&priv->lock, flags);
326 
327 	if (!changed)
328 		return 0;
329 
330 	/* Send the new RTS state to the connected device */
331 	mutex_lock(&serial->disc_mutex);
332 	if (!serial->disconnected)
333 		ret = send_control_msg(port, CONTROL_RTS, !rts);
334 	else
335 		ret = -ENODEV;
336 	mutex_unlock(&serial->disc_mutex);
337 
338 	return ret;
339 }
340 
341 static int get_serial_info(struct usb_serial_port *port,
342 			   struct serial_struct __user *serial)
343 {
344 	struct serial_struct tmp;
345 
346 	if (!serial)
347 		return -EFAULT;
348 
349 	memset(&tmp, 0x00, sizeof(tmp));
350 
351 	/* fake emulate a 16550 uart to make userspace code happy */
352 	tmp.type		= PORT_16550A;
353 	tmp.line		= port->serial->minor;
354 	tmp.port		= 0;
355 	tmp.irq			= 0;
356 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
357 	tmp.xmit_fifo_size	= 1024;
358 	tmp.baud_base		= 9600;
359 	tmp.close_delay		= 5*HZ;
360 	tmp.closing_wait	= 30*HZ;
361 
362 	if (copy_to_user(serial, &tmp, sizeof(*serial)))
363 		return -EFAULT;
364 	return 0;
365 }
366 
367 static int opticon_ioctl(struct tty_struct *tty,
368 			 unsigned int cmd, unsigned long arg)
369 {
370 	struct usb_serial_port *port = tty->driver_data;
371 
372 	dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
373 
374 	switch (cmd) {
375 	case TIOCGSERIAL:
376 		return get_serial_info(port,
377 				       (struct serial_struct __user *)arg);
378 	}
379 
380 	return -ENOIOCTLCMD;
381 }
382 
383 static int opticon_startup(struct usb_serial *serial)
384 {
385 	if (!serial->num_bulk_in) {
386 		dev_err(&serial->dev->dev, "no bulk in endpoint\n");
387 		return -ENODEV;
388 	}
389 
390 	return 0;
391 }
392 
393 static int opticon_port_probe(struct usb_serial_port *port)
394 {
395 	struct opticon_private *priv;
396 
397 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
398 	if (!priv)
399 		return -ENOMEM;
400 
401 	spin_lock_init(&priv->lock);
402 
403 	usb_set_serial_port_data(port, priv);
404 
405 	return 0;
406 }
407 
408 static int opticon_port_remove(struct usb_serial_port *port)
409 {
410 	struct opticon_private *priv = usb_get_serial_port_data(port);
411 
412 	kfree(priv);
413 
414 	return 0;
415 }
416 
417 static struct usb_serial_driver opticon_device = {
418 	.driver = {
419 		.owner =	THIS_MODULE,
420 		.name =		"opticon",
421 	},
422 	.id_table =		id_table,
423 	.num_ports =		1,
424 	.bulk_in_size =		256,
425 	.attach =		opticon_startup,
426 	.port_probe =		opticon_port_probe,
427 	.port_remove =		opticon_port_remove,
428 	.open =			opticon_open,
429 	.write =		opticon_write,
430 	.write_room = 		opticon_write_room,
431 	.throttle =		usb_serial_generic_throttle,
432 	.unthrottle =		usb_serial_generic_unthrottle,
433 	.ioctl =		opticon_ioctl,
434 	.tiocmget =		opticon_tiocmget,
435 	.tiocmset =		opticon_tiocmset,
436 	.process_read_urb =	opticon_process_read_urb,
437 };
438 
439 static struct usb_serial_driver * const serial_drivers[] = {
440 	&opticon_device, NULL
441 };
442 
443 module_usb_serial_driver(serial_drivers, id_table);
444 
445 MODULE_DESCRIPTION(DRIVER_DESC);
446 MODULE_LICENSE("GPL");
447