xref: /linux/drivers/usb/serial/opticon.c (revision 1e578ec51cace9dcef961225b72b8da46ade13ae)
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 
11 #include <linux/kernel.h>
12 #include <linux/tty.h>
13 #include <linux/slab.h>
14 #include <linux/tty_flip.h>
15 #include <linux/serial.h>
16 #include <linux/module.h>
17 #include <linux/usb.h>
18 #include <linux/usb/serial.h>
19 
20 #define CONTROL_RTS			0x02
21 #define RESEND_CTS_STATE	0x03
22 
23 /* max number of write urbs in flight */
24 #define URB_UPPER_LIMIT	8
25 
26 /* This driver works for the Opticon 1D barcode reader
27  * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
28 #define DRIVER_DESC	"Opticon USB barcode to serial driver (1D)"
29 
30 static const struct usb_device_id id_table[] = {
31 	{ USB_DEVICE(0x065a, 0x0009) },
32 	{ },
33 };
34 MODULE_DEVICE_TABLE(usb, id_table);
35 
36 /* This structure holds all of the individual device information */
37 struct opticon_private {
38 	spinlock_t lock;	/* protects the following flags */
39 	bool rts;
40 	bool cts;
41 	int outstanding_urbs;
42 	int outstanding_bytes;
43 
44 	struct usb_anchor anchor;
45 };
46 
47 
48 static void opticon_process_data_packet(struct usb_serial_port *port,
49 					const unsigned char *buf, size_t len)
50 {
51 	tty_insert_flip_string(&port->port, buf, len);
52 	tty_flip_buffer_push(&port->port);
53 }
54 
55 static void opticon_process_status_packet(struct usb_serial_port *port,
56 					const unsigned char *buf, size_t len)
57 {
58 	struct opticon_private *priv = usb_get_serial_port_data(port);
59 	unsigned long flags;
60 
61 	spin_lock_irqsave(&priv->lock, flags);
62 	if (buf[0] == 0x00)
63 		priv->cts = false;
64 	else
65 		priv->cts = true;
66 	spin_unlock_irqrestore(&priv->lock, flags);
67 }
68 
69 static void opticon_process_read_urb(struct urb *urb)
70 {
71 	struct usb_serial_port *port = urb->context;
72 	const unsigned char *hdr = urb->transfer_buffer;
73 	const unsigned char *data = hdr + 2;
74 	size_t data_len = urb->actual_length - 2;
75 
76 	if (urb->actual_length <= 2) {
77 		dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
78 							urb->actual_length);
79 		return;
80 	}
81 	/*
82 	 * Data from the device comes with a 2 byte header:
83 	 *
84 	 * <0x00><0x00>data...
85 	 *      This is real data to be sent to the tty layer
86 	 * <0x00><0x01>level
87 	 *      This is a CTS level change, the third byte is the CTS
88 	 *      value (0 for low, 1 for high).
89 	 */
90 	if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
91 		opticon_process_data_packet(port, data, data_len);
92 	} else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
93 		opticon_process_status_packet(port, data, data_len);
94 	} else {
95 		dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
96 							hdr[0], hdr[1]);
97 	}
98 }
99 
100 static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
101 				u8 val)
102 {
103 	struct usb_serial *serial = port->serial;
104 	int retval;
105 	u8 *buffer;
106 
107 	buffer = kzalloc(1, GFP_KERNEL);
108 	if (!buffer)
109 		return -ENOMEM;
110 
111 	buffer[0] = val;
112 	/* Send the message to the vendor control endpoint
113 	 * of the connected device */
114 	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
115 				requesttype,
116 				USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
117 				0, 0, buffer, 1, USB_CTRL_SET_TIMEOUT);
118 	kfree(buffer);
119 
120 	if (retval < 0)
121 		return retval;
122 
123 	return 0;
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 endpoint */
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_close(struct usb_serial_port *port)
154 {
155 	struct opticon_private *priv = usb_get_serial_port_data(port);
156 
157 	usb_kill_anchored_urbs(&priv->anchor);
158 
159 	usb_serial_generic_close(port);
160 }
161 
162 static void opticon_write_control_callback(struct urb *urb)
163 {
164 	struct usb_serial_port *port = urb->context;
165 	struct opticon_private *priv = usb_get_serial_port_data(port);
166 	int status = urb->status;
167 	unsigned long flags;
168 
169 	/* free up the transfer buffer, as usb_free_urb() does not do this */
170 	kfree(urb->transfer_buffer);
171 
172 	/* setup packet may be set if we're using it for writing */
173 	kfree(urb->setup_packet);
174 
175 	if (status)
176 		dev_dbg(&port->dev,
177 			"%s - non-zero urb status received: %d\n",
178 			__func__, status);
179 
180 	spin_lock_irqsave(&priv->lock, flags);
181 	--priv->outstanding_urbs;
182 	priv->outstanding_bytes -= urb->transfer_buffer_length;
183 	spin_unlock_irqrestore(&priv->lock, flags);
184 
185 	usb_serial_port_softint(port);
186 }
187 
188 static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
189 			 const unsigned char *buf, int count)
190 {
191 	struct opticon_private *priv = usb_get_serial_port_data(port);
192 	struct usb_serial *serial = port->serial;
193 	struct urb *urb;
194 	unsigned char *buffer;
195 	unsigned long flags;
196 	struct usb_ctrlrequest *dr;
197 	int ret = -ENOMEM;
198 
199 	spin_lock_irqsave(&priv->lock, flags);
200 	if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
201 		spin_unlock_irqrestore(&priv->lock, flags);
202 		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
203 		return 0;
204 	}
205 	priv->outstanding_urbs++;
206 	priv->outstanding_bytes += count;
207 	spin_unlock_irqrestore(&priv->lock, flags);
208 
209 	buffer = kmemdup(buf, count, GFP_ATOMIC);
210 	if (!buffer)
211 		goto error_no_buffer;
212 
213 	urb = usb_alloc_urb(0, GFP_ATOMIC);
214 	if (!urb)
215 		goto error_no_urb;
216 
217 	usb_serial_debug_data(&port->dev, __func__, count, buffer);
218 
219 	/* The connected devices do not have a bulk write endpoint,
220 	 * to transmit data to de barcode device the control endpoint is used */
221 	dr = kmalloc_obj(struct usb_ctrlrequest, GFP_ATOMIC);
222 	if (!dr)
223 		goto error_no_dr;
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 	usb_anchor_urb(urb, &priv->anchor);
237 
238 	/* send it down the pipe */
239 	ret = usb_submit_urb(urb, GFP_ATOMIC);
240 	if (ret) {
241 		dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
242 		usb_unanchor_urb(urb);
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 	priv->outstanding_bytes -= count;
261 	spin_unlock_irqrestore(&priv->lock, flags);
262 
263 	return ret;
264 }
265 
266 static unsigned int opticon_write_room(struct tty_struct *tty)
267 {
268 	struct usb_serial_port *port = tty->driver_data;
269 	struct opticon_private *priv = usb_get_serial_port_data(port);
270 	unsigned long flags;
271 
272 	/*
273 	 * We really can take almost anything the user throws at us
274 	 * but let's pick a nice big number to tell the tty
275 	 * layer that we have lots of free space, unless we don't.
276 	 */
277 	spin_lock_irqsave(&priv->lock, flags);
278 	if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
279 		spin_unlock_irqrestore(&priv->lock, flags);
280 		dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
281 		return 0;
282 	}
283 	spin_unlock_irqrestore(&priv->lock, flags);
284 
285 	return 2048;
286 }
287 
288 static unsigned int opticon_chars_in_buffer(struct tty_struct *tty)
289 {
290 	struct usb_serial_port *port = tty->driver_data;
291 	struct opticon_private *priv = usb_get_serial_port_data(port);
292 	unsigned long flags;
293 	unsigned int count;
294 
295 	spin_lock_irqsave(&priv->lock, flags);
296 	count = priv->outstanding_bytes;
297 	spin_unlock_irqrestore(&priv->lock, flags);
298 
299 	return count;
300 }
301 
302 static int opticon_tiocmget(struct tty_struct *tty)
303 {
304 	struct usb_serial_port *port = tty->driver_data;
305 	struct opticon_private *priv = usb_get_serial_port_data(port);
306 	unsigned long flags;
307 	int result = 0;
308 
309 	spin_lock_irqsave(&priv->lock, flags);
310 	if (priv->rts)
311 		result |= TIOCM_RTS;
312 	if (priv->cts)
313 		result |= TIOCM_CTS;
314 	spin_unlock_irqrestore(&priv->lock, flags);
315 
316 	dev_dbg(&port->dev, "%s - %x\n", __func__, result);
317 	return result;
318 }
319 
320 static int opticon_tiocmset(struct tty_struct *tty,
321 			   unsigned int set, unsigned int clear)
322 {
323 	struct usb_serial_port *port = tty->driver_data;
324 	struct opticon_private *priv = usb_get_serial_port_data(port);
325 	unsigned long flags;
326 	bool rts;
327 	bool changed = false;
328 	int ret;
329 
330 	/* We only support RTS so we only handle that */
331 	spin_lock_irqsave(&priv->lock, flags);
332 
333 	rts = priv->rts;
334 	if (set & TIOCM_RTS)
335 		priv->rts = true;
336 	if (clear & TIOCM_RTS)
337 		priv->rts = false;
338 	changed = rts ^ priv->rts;
339 	spin_unlock_irqrestore(&priv->lock, flags);
340 
341 	if (!changed)
342 		return 0;
343 
344 	ret = send_control_msg(port, CONTROL_RTS, !rts);
345 	if (ret)
346 		return usb_translate_errors(ret);
347 
348 	return 0;
349 }
350 
351 static int opticon_port_probe(struct usb_serial_port *port)
352 {
353 	struct opticon_private *priv;
354 
355 	priv = kzalloc_obj(*priv);
356 	if (!priv)
357 		return -ENOMEM;
358 
359 	spin_lock_init(&priv->lock);
360 	init_usb_anchor(&priv->anchor);
361 
362 	usb_set_serial_port_data(port, priv);
363 
364 	return 0;
365 }
366 
367 static void opticon_port_remove(struct usb_serial_port *port)
368 {
369 	struct opticon_private *priv = usb_get_serial_port_data(port);
370 
371 	kfree(priv);
372 }
373 
374 static struct usb_serial_driver opticon_device = {
375 	.driver = {
376 		.name =		"opticon",
377 	},
378 	.id_table =		id_table,
379 	.num_ports =		1,
380 	.num_bulk_in =		1,
381 	.bulk_in_size =		256,
382 	.port_probe =		opticon_port_probe,
383 	.port_remove =		opticon_port_remove,
384 	.open =			opticon_open,
385 	.close =		opticon_close,
386 	.write =		opticon_write,
387 	.write_room = 		opticon_write_room,
388 	.chars_in_buffer =	opticon_chars_in_buffer,
389 	.throttle =		usb_serial_generic_throttle,
390 	.unthrottle =		usb_serial_generic_unthrottle,
391 	.tiocmget =		opticon_tiocmget,
392 	.tiocmset =		opticon_tiocmset,
393 	.process_read_urb =	opticon_process_read_urb,
394 };
395 
396 static struct usb_serial_driver * const serial_drivers[] = {
397 	&opticon_device, NULL
398 };
399 
400 module_usb_serial_driver(serial_drivers, id_table);
401 
402 MODULE_DESCRIPTION(DRIVER_DESC);
403 MODULE_LICENSE("GPL v2");
404