xref: /openbmc/linux/drivers/usb/serial/opticon.c (revision 5fd54ace)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Opticon USB barcode to serial driver
4  *
5  * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com>
6  * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
7  * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
8  * Copyright (C) 2008 - 2009 Novell Inc.
9  *
10  *	This program is free software; you can redistribute it and/or
11  *	modify it under the terms of the GNU General Public License version
12  *	2 as published by the Free Software Foundation.
13  */
14 
15 #include <linux/kernel.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 	if (retval < 0)
124 		return retval;
125 
126 	return 0;
127 }
128 
129 static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
130 {
131 	struct opticon_private *priv = usb_get_serial_port_data(port);
132 	unsigned long flags;
133 	int res;
134 
135 	spin_lock_irqsave(&priv->lock, flags);
136 	priv->rts = false;
137 	spin_unlock_irqrestore(&priv->lock, flags);
138 
139 	/* Clear RTS line */
140 	send_control_msg(port, CONTROL_RTS, 0);
141 
142 	/* clear the halt status of the endpoint */
143 	usb_clear_halt(port->serial->dev, port->read_urb->pipe);
144 
145 	res = usb_serial_generic_open(tty, port);
146 	if (res)
147 		return res;
148 
149 	/* Request CTS line state, sometimes during opening the current
150 	 * CTS state can be missed. */
151 	send_control_msg(port, RESEND_CTS_STATE, 1);
152 
153 	return res;
154 }
155 
156 static void opticon_write_control_callback(struct urb *urb)
157 {
158 	struct usb_serial_port *port = urb->context;
159 	struct opticon_private *priv = usb_get_serial_port_data(port);
160 	int status = urb->status;
161 	unsigned long flags;
162 
163 	/* free up the transfer buffer, as usb_free_urb() does not do this */
164 	kfree(urb->transfer_buffer);
165 
166 	/* setup packet may be set if we're using it for writing */
167 	kfree(urb->setup_packet);
168 
169 	if (status)
170 		dev_dbg(&port->dev,
171 			"%s - non-zero urb status received: %d\n",
172 			__func__, status);
173 
174 	spin_lock_irqsave(&priv->lock, flags);
175 	--priv->outstanding_urbs;
176 	spin_unlock_irqrestore(&priv->lock, flags);
177 
178 	usb_serial_port_softint(port);
179 }
180 
181 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
182 			 const unsigned char *buf, int count)
183 {
184 	struct opticon_private *priv = usb_get_serial_port_data(port);
185 	struct usb_serial *serial = port->serial;
186 	struct urb *urb;
187 	unsigned char *buffer;
188 	unsigned long flags;
189 	int status;
190 	struct usb_ctrlrequest *dr;
191 
192 	spin_lock_irqsave(&priv->lock, flags);
193 	if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
194 		spin_unlock_irqrestore(&priv->lock, flags);
195 		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
196 		return 0;
197 	}
198 	priv->outstanding_urbs++;
199 	spin_unlock_irqrestore(&priv->lock, flags);
200 
201 	buffer = kmalloc(count, GFP_ATOMIC);
202 	if (!buffer) {
203 		count = -ENOMEM;
204 		goto error_no_buffer;
205 	}
206 
207 	urb = usb_alloc_urb(0, GFP_ATOMIC);
208 	if (!urb) {
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 connected 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_ATOMIC);
220 	if (!dr) {
221 		count = -ENOMEM;
222 		goto error_no_dr;
223 	}
224 
225 	dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
226 	dr->bRequest = 0x01;
227 	dr->wValue = 0;
228 	dr->wIndex = 0;
229 	dr->wLength = cpu_to_le16(count);
230 
231 	usb_fill_control_urb(urb, serial->dev,
232 		usb_sndctrlpipe(serial->dev, 0),
233 		(unsigned char *)dr, buffer, count,
234 		opticon_write_control_callback, port);
235 
236 	/* send it down the pipe */
237 	status = usb_submit_urb(urb, GFP_ATOMIC);
238 	if (status) {
239 		dev_err(&port->dev,
240 		"%s - usb_submit_urb(write endpoint) failed status = %d\n",
241 							__func__, status);
242 		count = status;
243 		goto error;
244 	}
245 
246 	/* we are done with this urb, so let the host driver
247 	 * really free it when it is finished with it */
248 	usb_free_urb(urb);
249 
250 	return count;
251 error:
252 	kfree(dr);
253 error_no_dr:
254 	usb_free_urb(urb);
255 error_no_urb:
256 	kfree(buffer);
257 error_no_buffer:
258 	spin_lock_irqsave(&priv->lock, flags);
259 	--priv->outstanding_urbs;
260 	spin_unlock_irqrestore(&priv->lock, flags);
261 	return count;
262 }
263 
264 static int opticon_write_room(struct tty_struct *tty)
265 {
266 	struct usb_serial_port *port = tty->driver_data;
267 	struct opticon_private *priv = usb_get_serial_port_data(port);
268 	unsigned long flags;
269 
270 	/*
271 	 * We really can take almost anything the user throws at us
272 	 * but let's pick a nice big number to tell the tty
273 	 * layer that we have lots of free space, unless we don't.
274 	 */
275 	spin_lock_irqsave(&priv->lock, flags);
276 	if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
277 		spin_unlock_irqrestore(&priv->lock, flags);
278 		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
279 		return 0;
280 	}
281 	spin_unlock_irqrestore(&priv->lock, flags);
282 
283 	return 2048;
284 }
285 
286 static int opticon_tiocmget(struct tty_struct *tty)
287 {
288 	struct usb_serial_port *port = tty->driver_data;
289 	struct opticon_private *priv = usb_get_serial_port_data(port);
290 	unsigned long flags;
291 	int result = 0;
292 
293 	spin_lock_irqsave(&priv->lock, flags);
294 	if (priv->rts)
295 		result |= TIOCM_RTS;
296 	if (priv->cts)
297 		result |= TIOCM_CTS;
298 	spin_unlock_irqrestore(&priv->lock, flags);
299 
300 	dev_dbg(&port->dev, "%s - %x\n", __func__, result);
301 	return result;
302 }
303 
304 static int opticon_tiocmset(struct tty_struct *tty,
305 			   unsigned int set, unsigned int clear)
306 {
307 	struct usb_serial_port *port = tty->driver_data;
308 	struct opticon_private *priv = usb_get_serial_port_data(port);
309 	unsigned long flags;
310 	bool rts;
311 	bool changed = false;
312 	int ret;
313 
314 	/* We only support RTS so we only handle that */
315 	spin_lock_irqsave(&priv->lock, flags);
316 
317 	rts = priv->rts;
318 	if (set & TIOCM_RTS)
319 		priv->rts = true;
320 	if (clear & TIOCM_RTS)
321 		priv->rts = false;
322 	changed = rts ^ priv->rts;
323 	spin_unlock_irqrestore(&priv->lock, flags);
324 
325 	if (!changed)
326 		return 0;
327 
328 	ret = send_control_msg(port, CONTROL_RTS, !rts);
329 	if (ret)
330 		return usb_translate_errors(ret);
331 
332 	return 0;
333 }
334 
335 static int get_serial_info(struct usb_serial_port *port,
336 			   struct serial_struct __user *serial)
337 {
338 	struct serial_struct tmp;
339 
340 	memset(&tmp, 0x00, sizeof(tmp));
341 
342 	/* fake emulate a 16550 uart to make userspace code happy */
343 	tmp.type		= PORT_16550A;
344 	tmp.line		= port->minor;
345 	tmp.port		= 0;
346 	tmp.irq			= 0;
347 	tmp.xmit_fifo_size	= 1024;
348 	tmp.baud_base		= 9600;
349 	tmp.close_delay		= 5*HZ;
350 	tmp.closing_wait	= 30*HZ;
351 
352 	if (copy_to_user(serial, &tmp, sizeof(*serial)))
353 		return -EFAULT;
354 	return 0;
355 }
356 
357 static int opticon_ioctl(struct tty_struct *tty,
358 			 unsigned int cmd, unsigned long arg)
359 {
360 	struct usb_serial_port *port = tty->driver_data;
361 
362 	switch (cmd) {
363 	case TIOCGSERIAL:
364 		return get_serial_info(port,
365 				       (struct serial_struct __user *)arg);
366 	}
367 
368 	return -ENOIOCTLCMD;
369 }
370 
371 static int opticon_port_probe(struct usb_serial_port *port)
372 {
373 	struct opticon_private *priv;
374 
375 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
376 	if (!priv)
377 		return -ENOMEM;
378 
379 	spin_lock_init(&priv->lock);
380 
381 	usb_set_serial_port_data(port, priv);
382 
383 	return 0;
384 }
385 
386 static int opticon_port_remove(struct usb_serial_port *port)
387 {
388 	struct opticon_private *priv = usb_get_serial_port_data(port);
389 
390 	kfree(priv);
391 
392 	return 0;
393 }
394 
395 static struct usb_serial_driver opticon_device = {
396 	.driver = {
397 		.owner =	THIS_MODULE,
398 		.name =		"opticon",
399 	},
400 	.id_table =		id_table,
401 	.num_ports =		1,
402 	.num_bulk_in =		1,
403 	.bulk_in_size =		256,
404 	.port_probe =		opticon_port_probe,
405 	.port_remove =		opticon_port_remove,
406 	.open =			opticon_open,
407 	.write =		opticon_write,
408 	.write_room = 		opticon_write_room,
409 	.throttle =		usb_serial_generic_throttle,
410 	.unthrottle =		usb_serial_generic_unthrottle,
411 	.ioctl =		opticon_ioctl,
412 	.tiocmget =		opticon_tiocmget,
413 	.tiocmset =		opticon_tiocmset,
414 	.process_read_urb =	opticon_process_read_urb,
415 };
416 
417 static struct usb_serial_driver * const serial_drivers[] = {
418 	&opticon_device, NULL
419 };
420 
421 module_usb_serial_driver(serial_drivers, id_table);
422 
423 MODULE_DESCRIPTION(DRIVER_DESC);
424 MODULE_LICENSE("GPL");
425