]>
Commit | Line | Data |
---|---|---|
57262b82 GKH |
1 | /* |
2 | * Opticon USB barcode to serial driver | |
3 | * | |
7a6ee2b0 | 4 | * Copyright (C) 2011 - 2012 Johan Hovold <jhovold@gmail.com> |
309a0579 | 5 | * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com> |
648d4e16 GKH |
6 | * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de> |
7 | * Copyright (C) 2008 - 2009 Novell Inc. | |
57262b82 GKH |
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> | |
5a0e3ad6 | 18 | #include <linux/slab.h> |
57262b82 | 19 | #include <linux/tty_flip.h> |
faac64ad | 20 | #include <linux/serial.h> |
57262b82 GKH |
21 | #include <linux/module.h> |
22 | #include <linux/usb.h> | |
23 | #include <linux/usb/serial.h> | |
24 | #include <linux/uaccess.h> | |
25 | ||
309a0579 MJ |
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 | ||
7d40d7e8 | 36 | static const struct usb_device_id id_table[] = { |
57262b82 GKH |
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 { | |
57262b82 | 44 | spinlock_t lock; /* protects the following flags */ |
57262b82 | 45 | bool rts; |
309a0579 | 46 | bool cts; |
648d4e16 | 47 | int outstanding_urbs; |
57262b82 GKH |
48 | }; |
49 | ||
648d4e16 | 50 | |
32802077 JH |
51 | static void opticon_process_data_packet(struct usb_serial_port *port, |
52 | const unsigned char *buf, size_t len) | |
53 | { | |
05c7cd39 | 54 | tty_insert_flip_string(&port->port, buf, len); |
2e124b4a | 55 | tty_flip_buffer_push(&port->port); |
32802077 JH |
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 | } | |
309a0579 | 102 | |
309a0579 MJ |
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; | |
ea0dbebf JH |
108 | u8 *buffer; |
109 | ||
110 | buffer = kzalloc(1, GFP_KERNEL); | |
111 | if (!buffer) | |
112 | return -ENOMEM; | |
309a0579 MJ |
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); | |
ea0dbebf | 121 | kfree(buffer); |
309a0579 | 122 | |
94c51dca JH |
123 | if (retval < 0) |
124 | return retval; | |
125 | ||
126 | return 0; | |
309a0579 MJ |
127 | } |
128 | ||
a509a7e4 | 129 | static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port) |
57262b82 | 130 | { |
70d25eee | 131 | struct opticon_private *priv = usb_get_serial_port_data(port); |
57262b82 | 132 | unsigned long flags; |
7a6ee2b0 | 133 | int res; |
57262b82 | 134 | |
57262b82 | 135 | spin_lock_irqsave(&priv->lock, flags); |
309a0579 | 136 | priv->rts = false; |
57262b82 GKH |
137 | spin_unlock_irqrestore(&priv->lock, flags); |
138 | ||
309a0579 MJ |
139 | /* Clear RTS line */ |
140 | send_control_msg(port, CONTROL_RTS, 0); | |
141 | ||
309a0579 | 142 | /* clear the halt status of the enpoint */ |
7a6ee2b0 JH |
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; | |
309a0579 | 148 | |
309a0579 MJ |
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); | |
57262b82 | 152 | |
7a6ee2b0 | 153 | return res; |
57262b82 GKH |
154 | } |
155 | ||
309a0579 | 156 | static void opticon_write_control_callback(struct urb *urb) |
648d4e16 | 157 | { |
e32d82bc JH |
158 | struct usb_serial_port *port = urb->context; |
159 | struct opticon_private *priv = usb_get_serial_port_data(port); | |
648d4e16 GKH |
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 | ||
0d930e51 AZ |
166 | /* setup packet may be set if we're using it for writing */ |
167 | kfree(urb->setup_packet); | |
168 | ||
648d4e16 | 169 | if (status) |
e32d82bc | 170 | dev_dbg(&port->dev, |
e29a7738 | 171 | "%s - non-zero urb status received: %d\n", |
d44d9ab7 | 172 | __func__, status); |
648d4e16 GKH |
173 | |
174 | spin_lock_irqsave(&priv->lock, flags); | |
175 | --priv->outstanding_urbs; | |
176 | spin_unlock_irqrestore(&priv->lock, flags); | |
177 | ||
e32d82bc | 178 | usb_serial_port_softint(port); |
648d4e16 GKH |
179 | } |
180 | ||
181 | static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port, | |
182 | const unsigned char *buf, int count) | |
183 | { | |
70d25eee | 184 | struct opticon_private *priv = usb_get_serial_port_data(port); |
648d4e16 GKH |
185 | struct usb_serial *serial = port->serial; |
186 | struct urb *urb; | |
187 | unsigned char *buffer; | |
188 | unsigned long flags; | |
189 | int status; | |
309a0579 | 190 | struct usb_ctrlrequest *dr; |
648d4e16 | 191 | |
648d4e16 GKH |
192 | spin_lock_irqsave(&priv->lock, flags); |
193 | if (priv->outstanding_urbs > URB_UPPER_LIMIT) { | |
194 | spin_unlock_irqrestore(&priv->lock, flags); | |
d44d9ab7 | 195 | dev_dbg(&port->dev, "%s - write limit hit\n", __func__); |
648d4e16 GKH |
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 | dev_err(&port->dev, "out of memory\n"); | |
204 | count = -ENOMEM; | |
309a0579 | 205 | |
648d4e16 GKH |
206 | goto error_no_buffer; |
207 | } | |
208 | ||
209 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
210 | if (!urb) { | |
211 | dev_err(&port->dev, "no more free urbs\n"); | |
212 | count = -ENOMEM; | |
213 | goto error_no_urb; | |
214 | } | |
215 | ||
216 | memcpy(buffer, buf, count); | |
217 | ||
59d33f2f | 218 | usb_serial_debug_data(&port->dev, __func__, count, buffer); |
648d4e16 | 219 | |
309a0579 MJ |
220 | /* The conncected devices do not have a bulk write endpoint, |
221 | * to transmit data to de barcode device the control endpoint is used */ | |
222 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO); | |
b0795bbf JL |
223 | if (!dr) { |
224 | dev_err(&port->dev, "out of memory\n"); | |
225 | count = -ENOMEM; | |
acbf0e52 | 226 | goto error_no_dr; |
b0795bbf | 227 | } |
309a0579 MJ |
228 | |
229 | dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT; | |
230 | dr->bRequest = 0x01; | |
231 | dr->wValue = 0; | |
232 | dr->wIndex = 0; | |
233 | dr->wLength = cpu_to_le16(count); | |
234 | ||
235 | usb_fill_control_urb(urb, serial->dev, | |
236 | usb_sndctrlpipe(serial->dev, 0), | |
237 | (unsigned char *)dr, buffer, count, | |
e32d82bc | 238 | opticon_write_control_callback, port); |
648d4e16 GKH |
239 | |
240 | /* send it down the pipe */ | |
241 | status = usb_submit_urb(urb, GFP_ATOMIC); | |
242 | if (status) { | |
243 | dev_err(&port->dev, | |
309a0579 | 244 | "%s - usb_submit_urb(write endpoint) failed status = %d\n", |
648d4e16 GKH |
245 | __func__, status); |
246 | count = status; | |
247 | goto error; | |
248 | } | |
249 | ||
250 | /* we are done with this urb, so let the host driver | |
251 | * really free it when it is finished with it */ | |
252 | usb_free_urb(urb); | |
253 | ||
254 | return count; | |
255 | error: | |
acbf0e52 JH |
256 | kfree(dr); |
257 | error_no_dr: | |
648d4e16 GKH |
258 | usb_free_urb(urb); |
259 | error_no_urb: | |
260 | kfree(buffer); | |
261 | error_no_buffer: | |
262 | spin_lock_irqsave(&priv->lock, flags); | |
263 | --priv->outstanding_urbs; | |
264 | spin_unlock_irqrestore(&priv->lock, flags); | |
265 | return count; | |
266 | } | |
267 | ||
268 | static int opticon_write_room(struct tty_struct *tty) | |
269 | { | |
270 | struct usb_serial_port *port = tty->driver_data; | |
70d25eee | 271 | struct opticon_private *priv = usb_get_serial_port_data(port); |
648d4e16 GKH |
272 | unsigned long flags; |
273 | ||
648d4e16 GKH |
274 | /* |
275 | * We really can take almost anything the user throws at us | |
276 | * but let's pick a nice big number to tell the tty | |
277 | * layer that we have lots of free space, unless we don't. | |
278 | */ | |
279 | spin_lock_irqsave(&priv->lock, flags); | |
280 | if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) { | |
281 | spin_unlock_irqrestore(&priv->lock, flags); | |
d44d9ab7 | 282 | dev_dbg(&port->dev, "%s - write limit hit\n", __func__); |
648d4e16 GKH |
283 | return 0; |
284 | } | |
285 | spin_unlock_irqrestore(&priv->lock, flags); | |
286 | ||
287 | return 2048; | |
288 | } | |
289 | ||
60b33c13 | 290 | static int opticon_tiocmget(struct tty_struct *tty) |
faac64ad GKH |
291 | { |
292 | struct usb_serial_port *port = tty->driver_data; | |
70d25eee | 293 | struct opticon_private *priv = usb_get_serial_port_data(port); |
faac64ad GKH |
294 | unsigned long flags; |
295 | int result = 0; | |
296 | ||
faac64ad GKH |
297 | spin_lock_irqsave(&priv->lock, flags); |
298 | if (priv->rts) | |
309a0579 MJ |
299 | result |= TIOCM_RTS; |
300 | if (priv->cts) | |
301 | result |= TIOCM_CTS; | |
faac64ad GKH |
302 | spin_unlock_irqrestore(&priv->lock, flags); |
303 | ||
d44d9ab7 | 304 | dev_dbg(&port->dev, "%s - %x\n", __func__, result); |
faac64ad GKH |
305 | return result; |
306 | } | |
307 | ||
4acfaf82 | 308 | static int opticon_tiocmset(struct tty_struct *tty, |
309a0579 MJ |
309 | unsigned int set, unsigned int clear) |
310 | { | |
311 | struct usb_serial_port *port = tty->driver_data; | |
70d25eee | 312 | struct opticon_private *priv = usb_get_serial_port_data(port); |
309a0579 MJ |
313 | unsigned long flags; |
314 | bool rts; | |
315 | bool changed = false; | |
81d5a672 | 316 | int ret; |
309a0579 | 317 | |
309a0579 MJ |
318 | /* We only support RTS so we only handle that */ |
319 | spin_lock_irqsave(&priv->lock, flags); | |
320 | ||
321 | rts = priv->rts; | |
322 | if (set & TIOCM_RTS) | |
323 | priv->rts = true; | |
324 | if (clear & TIOCM_RTS) | |
325 | priv->rts = false; | |
326 | changed = rts ^ priv->rts; | |
327 | spin_unlock_irqrestore(&priv->lock, flags); | |
328 | ||
329 | if (!changed) | |
330 | return 0; | |
331 | ||
94bcef62 JH |
332 | ret = send_control_msg(port, CONTROL_RTS, !rts); |
333 | if (ret) | |
334 | return usb_translate_errors(ret); | |
81d5a672 | 335 | |
94bcef62 | 336 | return 0; |
309a0579 MJ |
337 | } |
338 | ||
56be1a17 | 339 | static int get_serial_info(struct usb_serial_port *port, |
faac64ad GKH |
340 | struct serial_struct __user *serial) |
341 | { | |
342 | struct serial_struct tmp; | |
343 | ||
344 | if (!serial) | |
345 | return -EFAULT; | |
346 | ||
347 | memset(&tmp, 0x00, sizeof(tmp)); | |
348 | ||
349 | /* fake emulate a 16550 uart to make userspace code happy */ | |
350 | tmp.type = PORT_16550A; | |
e5b1e206 | 351 | tmp.line = port->minor; |
faac64ad GKH |
352 | tmp.port = 0; |
353 | tmp.irq = 0; | |
354 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; | |
355 | tmp.xmit_fifo_size = 1024; | |
356 | tmp.baud_base = 9600; | |
357 | tmp.close_delay = 5*HZ; | |
358 | tmp.closing_wait = 30*HZ; | |
359 | ||
360 | if (copy_to_user(serial, &tmp, sizeof(*serial))) | |
361 | return -EFAULT; | |
362 | return 0; | |
363 | } | |
364 | ||
00a0d0d6 | 365 | static int opticon_ioctl(struct tty_struct *tty, |
faac64ad GKH |
366 | unsigned int cmd, unsigned long arg) |
367 | { | |
368 | struct usb_serial_port *port = tty->driver_data; | |
faac64ad | 369 | |
1143832e | 370 | dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd); |
faac64ad GKH |
371 | |
372 | switch (cmd) { | |
373 | case TIOCGSERIAL: | |
56be1a17 | 374 | return get_serial_info(port, |
faac64ad GKH |
375 | (struct serial_struct __user *)arg); |
376 | } | |
377 | ||
378 | return -ENOIOCTLCMD; | |
379 | } | |
380 | ||
57262b82 GKH |
381 | static int opticon_startup(struct usb_serial *serial) |
382 | { | |
a0a5fd92 JH |
383 | if (!serial->num_bulk_in) { |
384 | dev_err(&serial->dev->dev, "no bulk in endpoint\n"); | |
385 | return -ENODEV; | |
386 | } | |
57262b82 | 387 | |
70d25eee JH |
388 | return 0; |
389 | } | |
390 | ||
391 | static int opticon_port_probe(struct usb_serial_port *port) | |
392 | { | |
70d25eee | 393 | struct opticon_private *priv; |
70d25eee | 394 | |
57262b82 | 395 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
70d25eee | 396 | if (!priv) |
57262b82 | 397 | return -ENOMEM; |
70d25eee | 398 | |
57262b82 | 399 | spin_lock_init(&priv->lock); |
0b8718a2 | 400 | |
70d25eee | 401 | usb_set_serial_port_data(port, priv); |
57262b82 | 402 | |
70d25eee | 403 | return 0; |
57262b82 GKH |
404 | } |
405 | ||
70d25eee | 406 | static int opticon_port_remove(struct usb_serial_port *port) |
f9c99bb8 | 407 | { |
70d25eee | 408 | struct opticon_private *priv = usb_get_serial_port_data(port); |
f9c99bb8 | 409 | |
57262b82 | 410 | kfree(priv); |
70d25eee JH |
411 | |
412 | return 0; | |
57262b82 GKH |
413 | } |
414 | ||
57262b82 GKH |
415 | static struct usb_serial_driver opticon_device = { |
416 | .driver = { | |
417 | .owner = THIS_MODULE, | |
418 | .name = "opticon", | |
419 | }, | |
420 | .id_table = id_table, | |
57262b82 | 421 | .num_ports = 1, |
333396fc | 422 | .bulk_in_size = 256, |
57262b82 | 423 | .attach = opticon_startup, |
70d25eee JH |
424 | .port_probe = opticon_port_probe, |
425 | .port_remove = opticon_port_remove, | |
57262b82 | 426 | .open = opticon_open, |
648d4e16 GKH |
427 | .write = opticon_write, |
428 | .write_room = opticon_write_room, | |
7a6ee2b0 JH |
429 | .throttle = usb_serial_generic_throttle, |
430 | .unthrottle = usb_serial_generic_unthrottle, | |
faac64ad GKH |
431 | .ioctl = opticon_ioctl, |
432 | .tiocmget = opticon_tiocmget, | |
309a0579 | 433 | .tiocmset = opticon_tiocmset, |
7a6ee2b0 | 434 | .process_read_urb = opticon_process_read_urb, |
57262b82 GKH |
435 | }; |
436 | ||
f667ddad AS |
437 | static struct usb_serial_driver * const serial_drivers[] = { |
438 | &opticon_device, NULL | |
439 | }; | |
440 | ||
68e24113 | 441 | module_usb_serial_driver(serial_drivers, id_table); |
57262b82 | 442 | |
309a0579 | 443 | MODULE_DESCRIPTION(DRIVER_DESC); |
57262b82 | 444 | MODULE_LICENSE("GPL"); |