blob: 8d6ece048f07f6231ad5563f27d10c740e630cc2 [file] [log] [blame]
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -08001/*
2 * Opticon USB barcode to serial driver
3 *
Martin Jansen309a0572011-02-24 14:50:16 +01004 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -08005 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
6 * Copyright (C) 2008 - 2009 Novell Inc.
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -08007 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080018#include <linux/tty_flip.h>
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -080019#include <linux/serial.h>
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080020#include <linux/module.h>
21#include <linux/usb.h>
22#include <linux/usb/serial.h>
23#include <linux/uaccess.h>
24
Martin Jansen309a0572011-02-24 14:50:16 +010025#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
Németh Márton7d40d7e2010-01-10 15:34:24 +010035static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080036 { USB_DEVICE(0x065a, 0x0009) },
37 { },
38};
39MODULE_DEVICE_TABLE(usb, id_table);
40
41/* This structure holds all of the individual device information */
42struct opticon_private {
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080043 struct urb *bulk_read_urb;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080044 spinlock_t lock; /* protects the following flags */
45 bool throttled;
46 bool actually_throttled;
47 bool rts;
Martin Jansen309a0572011-02-24 14:50:16 +010048 bool cts;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080049 int outstanding_urbs;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080050};
51
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080052
Johan Hovold32802072012-11-18 13:23:35 +010053static void opticon_process_data_packet(struct usb_serial_port *port,
54 const unsigned char *buf, size_t len)
55{
56 struct tty_struct *tty;
57
58 tty = tty_port_tty_get(&port->port);
59 if (!tty)
60 return;
61
62 tty_insert_flip_string(tty, buf, len);
63 tty_flip_buffer_push(tty);
64 tty_kref_put(tty);
65}
66
67static void opticon_process_status_packet(struct usb_serial_port *port,
68 const unsigned char *buf, size_t len)
69{
70 struct opticon_private *priv = usb_get_serial_port_data(port);
71 unsigned long flags;
72
73 spin_lock_irqsave(&priv->lock, flags);
74 if (buf[0] == 0x00)
75 priv->cts = false;
76 else
77 priv->cts = true;
78 spin_unlock_irqrestore(&priv->lock, flags);
79}
80
81static void opticon_process_read_urb(struct urb *urb)
82{
83 struct usb_serial_port *port = urb->context;
84 const unsigned char *hdr = urb->transfer_buffer;
85 const unsigned char *data = hdr + 2;
86 size_t data_len = urb->actual_length - 2;
87
88 if (urb->actual_length <= 2) {
89 dev_dbg(&port->dev, "malformed packet received: %d bytes\n",
90 urb->actual_length);
91 return;
92 }
93 /*
94 * Data from the device comes with a 2 byte header:
95 *
96 * <0x00><0x00>data...
97 * This is real data to be sent to the tty layer
98 * <0x00><0x01>level
99 * This is a CTS level change, the third byte is the CTS
100 * value (0 for low, 1 for high).
101 */
102 if ((hdr[0] == 0x00) && (hdr[1] == 0x00)) {
103 opticon_process_data_packet(port, data, data_len);
104 } else if ((hdr[0] == 0x00) && (hdr[1] == 0x01)) {
105 opticon_process_status_packet(port, data, data_len);
106 } else {
107 dev_dbg(&port->dev, "unknown packet received: %02x %02x\n",
108 hdr[0], hdr[1]);
109 }
110}
Martin Jansen309a0572011-02-24 14:50:16 +0100111
112static void opticon_read_bulk_callback(struct urb *urb)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800113{
Johan Hovolde32d82b2012-11-18 13:23:32 +0100114 struct usb_serial_port *port = urb->context;
115 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800116 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800117 int status = urb->status;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800118 int result;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800119
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800120 switch (status) {
121 case 0:
122 /* success */
123 break;
124 case -ECONNRESET:
125 case -ENOENT:
126 case -ESHUTDOWN:
127 /* this urb is terminated, clean up */
Johan Hovolde29a7732012-11-18 13:23:23 +0100128 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700129 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800130 return;
131 default:
Johan Hovolde29a7732012-11-18 13:23:23 +0100132 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700133 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800134 goto exit;
135 }
136
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100137 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800138
Johan Hovold32802072012-11-18 13:23:35 +0100139 opticon_process_read_urb(urb);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800140exit:
141 spin_lock(&priv->lock);
142
143 /* Continue trying to always read if we should */
144 if (!priv->throttled) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200145 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800146 if (result)
147 dev_err(&port->dev,
148 "%s - failed resubmitting read urb, error %d\n",
149 __func__, result);
150 } else
151 priv->actually_throttled = true;
152 spin_unlock(&priv->lock);
153}
154
Martin Jansen309a0572011-02-24 14:50:16 +0100155static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
156 u8 val)
157{
158 struct usb_serial *serial = port->serial;
159 int retval;
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200160 u8 *buffer;
161
162 buffer = kzalloc(1, GFP_KERNEL);
163 if (!buffer)
164 return -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100165
166 buffer[0] = val;
167 /* Send the message to the vendor control endpoint
168 * of the connected device */
169 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
170 requesttype,
171 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
172 0, 0, buffer, 1, 0);
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200173 kfree(buffer);
Martin Jansen309a0572011-02-24 14:50:16 +0100174
175 return retval;
176}
177
Alan Coxa509a7e2009-09-19 13:13:26 -0700178static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800179{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100180 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800181 unsigned long flags;
182 int result = 0;
183
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800184 spin_lock_irqsave(&priv->lock, flags);
185 priv->throttled = false;
186 priv->actually_throttled = false;
Martin Jansen309a0572011-02-24 14:50:16 +0100187 priv->rts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800188 spin_unlock_irqrestore(&priv->lock, flags);
189
Martin Jansen309a0572011-02-24 14:50:16 +0100190 /* Clear RTS line */
191 send_control_msg(port, CONTROL_RTS, 0);
192
Martin Jansen309a0572011-02-24 14:50:16 +0100193 /* clear the halt status of the enpoint */
Johan Hovold3157fad2012-11-18 13:23:24 +0100194 usb_clear_halt(port->serial->dev, priv->bulk_read_urb->pipe);
Martin Jansen309a0572011-02-24 14:50:16 +0100195
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800196 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
197 if (result)
198 dev_err(&port->dev,
199 "%s - failed resubmitting read urb, error %d\n",
200 __func__, result);
Martin Jansen309a0572011-02-24 14:50:16 +0100201 /* Request CTS line state, sometimes during opening the current
202 * CTS state can be missed. */
203 send_control_msg(port, RESEND_CTS_STATE, 1);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800204 return result;
205}
206
Alan Cox335f8512009-06-11 12:26:29 +0100207static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800208{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100209 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800210
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800211 /* shutdown our urbs */
212 usb_kill_urb(priv->bulk_read_urb);
213}
214
Martin Jansen309a0572011-02-24 14:50:16 +0100215static void opticon_write_control_callback(struct urb *urb)
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800216{
Johan Hovolde32d82b2012-11-18 13:23:32 +0100217 struct usb_serial_port *port = urb->context;
218 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800219 int status = urb->status;
220 unsigned long flags;
221
222 /* free up the transfer buffer, as usb_free_urb() does not do this */
223 kfree(urb->transfer_buffer);
224
Alon Ziv0d930e52010-10-10 08:32:19 +0200225 /* setup packet may be set if we're using it for writing */
226 kfree(urb->setup_packet);
227
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800228 if (status)
Johan Hovolde32d82b2012-11-18 13:23:32 +0100229 dev_dbg(&port->dev,
Johan Hovolde29a7732012-11-18 13:23:23 +0100230 "%s - non-zero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700231 __func__, status);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800232
233 spin_lock_irqsave(&priv->lock, flags);
234 --priv->outstanding_urbs;
235 spin_unlock_irqrestore(&priv->lock, flags);
236
Johan Hovolde32d82b2012-11-18 13:23:32 +0100237 usb_serial_port_softint(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800238}
239
240static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
241 const unsigned char *buf, int count)
242{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100243 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800244 struct usb_serial *serial = port->serial;
245 struct urb *urb;
246 unsigned char *buffer;
247 unsigned long flags;
248 int status;
Martin Jansen309a0572011-02-24 14:50:16 +0100249 struct usb_ctrlrequest *dr;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800250
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800251 spin_lock_irqsave(&priv->lock, flags);
252 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
253 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700254 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800255 return 0;
256 }
257 priv->outstanding_urbs++;
258 spin_unlock_irqrestore(&priv->lock, flags);
259
260 buffer = kmalloc(count, GFP_ATOMIC);
261 if (!buffer) {
262 dev_err(&port->dev, "out of memory\n");
263 count = -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100264
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800265 goto error_no_buffer;
266 }
267
268 urb = usb_alloc_urb(0, GFP_ATOMIC);
269 if (!urb) {
270 dev_err(&port->dev, "no more free urbs\n");
271 count = -ENOMEM;
272 goto error_no_urb;
273 }
274
275 memcpy(buffer, buf, count);
276
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100277 usb_serial_debug_data(&port->dev, __func__, count, buffer);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800278
Martin Jansen309a0572011-02-24 14:50:16 +0100279 /* The conncected devices do not have a bulk write endpoint,
280 * to transmit data to de barcode device the control endpoint is used */
281 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Julia Lawallb0795bb2011-05-13 17:30:46 +0200282 if (!dr) {
283 dev_err(&port->dev, "out of memory\n");
284 count = -ENOMEM;
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200285 goto error_no_dr;
Julia Lawallb0795bb2011-05-13 17:30:46 +0200286 }
Alon Ziv0d930e52010-10-10 08:32:19 +0200287
Martin Jansen309a0572011-02-24 14:50:16 +0100288 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
289 dr->bRequest = 0x01;
290 dr->wValue = 0;
291 dr->wIndex = 0;
292 dr->wLength = cpu_to_le16(count);
Alon Ziv0d930e52010-10-10 08:32:19 +0200293
Martin Jansen309a0572011-02-24 14:50:16 +0100294 usb_fill_control_urb(urb, serial->dev,
295 usb_sndctrlpipe(serial->dev, 0),
296 (unsigned char *)dr, buffer, count,
Johan Hovolde32d82b2012-11-18 13:23:32 +0100297 opticon_write_control_callback, port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800298
299 /* send it down the pipe */
300 status = usb_submit_urb(urb, GFP_ATOMIC);
301 if (status) {
302 dev_err(&port->dev,
Martin Jansen309a0572011-02-24 14:50:16 +0100303 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800304 __func__, status);
305 count = status;
306 goto error;
307 }
308
309 /* we are done with this urb, so let the host driver
310 * really free it when it is finished with it */
311 usb_free_urb(urb);
312
313 return count;
314error:
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200315 kfree(dr);
316error_no_dr:
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800317 usb_free_urb(urb);
318error_no_urb:
319 kfree(buffer);
320error_no_buffer:
321 spin_lock_irqsave(&priv->lock, flags);
322 --priv->outstanding_urbs;
323 spin_unlock_irqrestore(&priv->lock, flags);
324 return count;
325}
326
327static int opticon_write_room(struct tty_struct *tty)
328{
329 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100330 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800331 unsigned long flags;
332
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800333 /*
334 * We really can take almost anything the user throws at us
335 * but let's pick a nice big number to tell the tty
336 * layer that we have lots of free space, unless we don't.
337 */
338 spin_lock_irqsave(&priv->lock, flags);
339 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
340 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700341 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800342 return 0;
343 }
344 spin_unlock_irqrestore(&priv->lock, flags);
345
346 return 2048;
347}
348
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800349static void opticon_throttle(struct tty_struct *tty)
350{
351 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100352 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800353 unsigned long flags;
354
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800355 spin_lock_irqsave(&priv->lock, flags);
356 priv->throttled = true;
357 spin_unlock_irqrestore(&priv->lock, flags);
358}
359
360
361static void opticon_unthrottle(struct tty_struct *tty)
362{
363 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100364 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800365 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200366 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800367
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800368 spin_lock_irqsave(&priv->lock, flags);
369 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200370 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800371 priv->actually_throttled = false;
372 spin_unlock_irqrestore(&priv->lock, flags);
373
Oliver Neukum88fa6592009-10-07 09:25:10 +0200374 if (was_throttled) {
375 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
376 if (result)
377 dev_err(&port->dev,
378 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800379 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200380 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800381}
382
Alan Cox60b33c12011-02-14 16:26:14 +0000383static int opticon_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800384{
385 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100386 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800387 unsigned long flags;
388 int result = 0;
389
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800390 spin_lock_irqsave(&priv->lock, flags);
391 if (priv->rts)
Martin Jansen309a0572011-02-24 14:50:16 +0100392 result |= TIOCM_RTS;
393 if (priv->cts)
394 result |= TIOCM_CTS;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800395 spin_unlock_irqrestore(&priv->lock, flags);
396
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700397 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800398 return result;
399}
400
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700401static int opticon_tiocmset(struct tty_struct *tty,
Martin Jansen309a0572011-02-24 14:50:16 +0100402 unsigned int set, unsigned int clear)
403{
404 struct usb_serial_port *port = tty->driver_data;
Johan Hovold81d5a672012-04-25 15:56:29 +0200405 struct usb_serial *serial = port->serial;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100406 struct opticon_private *priv = usb_get_serial_port_data(port);
Martin Jansen309a0572011-02-24 14:50:16 +0100407 unsigned long flags;
408 bool rts;
409 bool changed = false;
Johan Hovold81d5a672012-04-25 15:56:29 +0200410 int ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100411
Martin Jansen309a0572011-02-24 14:50:16 +0100412 /* We only support RTS so we only handle that */
413 spin_lock_irqsave(&priv->lock, flags);
414
415 rts = priv->rts;
416 if (set & TIOCM_RTS)
417 priv->rts = true;
418 if (clear & TIOCM_RTS)
419 priv->rts = false;
420 changed = rts ^ priv->rts;
421 spin_unlock_irqrestore(&priv->lock, flags);
422
423 if (!changed)
424 return 0;
425
426 /* Send the new RTS state to the connected device */
Johan Hovold81d5a672012-04-25 15:56:29 +0200427 mutex_lock(&serial->disc_mutex);
428 if (!serial->disconnected)
429 ret = send_control_msg(port, CONTROL_RTS, !rts);
430 else
431 ret = -ENODEV;
432 mutex_unlock(&serial->disc_mutex);
433
434 return ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100435}
436
Johan Hovold56be1a12012-11-18 13:23:31 +0100437static int get_serial_info(struct usb_serial_port *port,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800438 struct serial_struct __user *serial)
439{
440 struct serial_struct tmp;
441
442 if (!serial)
443 return -EFAULT;
444
445 memset(&tmp, 0x00, sizeof(tmp));
446
447 /* fake emulate a 16550 uart to make userspace code happy */
448 tmp.type = PORT_16550A;
Johan Hovold56be1a12012-11-18 13:23:31 +0100449 tmp.line = port->serial->minor;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800450 tmp.port = 0;
451 tmp.irq = 0;
452 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
453 tmp.xmit_fifo_size = 1024;
454 tmp.baud_base = 9600;
455 tmp.close_delay = 5*HZ;
456 tmp.closing_wait = 30*HZ;
457
458 if (copy_to_user(serial, &tmp, sizeof(*serial)))
459 return -EFAULT;
460 return 0;
461}
462
Alan Cox00a0d0d2011-02-14 16:27:06 +0000463static int opticon_ioctl(struct tty_struct *tty,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800464 unsigned int cmd, unsigned long arg)
465{
466 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800467
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700468 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800469
470 switch (cmd) {
471 case TIOCGSERIAL:
Johan Hovold56be1a12012-11-18 13:23:31 +0100472 return get_serial_info(port,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800473 (struct serial_struct __user *)arg);
474 }
475
476 return -ENOIOCTLCMD;
477}
478
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800479static int opticon_startup(struct usb_serial *serial)
480{
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100481 if (!serial->num_bulk_in) {
482 dev_err(&serial->dev->dev, "no bulk in endpoint\n");
483 return -ENODEV;
484 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800485
Johan Hovold70d25ee2012-11-18 13:23:30 +0100486 return 0;
487}
488
489static int opticon_port_probe(struct usb_serial_port *port)
490{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100491 struct opticon_private *priv;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100492
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800493 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100494 if (!priv)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800495 return -ENOMEM;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100496
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800497 spin_lock_init(&priv->lock);
Johan Hovold5ad47342012-11-18 13:23:34 +0100498 priv->bulk_read_urb = port->read_urbs[0];
Johan Hovold0b8718a2012-11-18 13:23:22 +0100499
Johan Hovold70d25ee2012-11-18 13:23:30 +0100500 usb_set_serial_port_data(port, priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800501
Johan Hovold70d25ee2012-11-18 13:23:30 +0100502 return 0;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800503}
504
Johan Hovold70d25ee2012-11-18 13:23:30 +0100505static int opticon_port_remove(struct usb_serial_port *port)
Alan Sternf9c99bb2009-06-02 11:53:55 -0400506{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100507 struct opticon_private *priv = usb_get_serial_port_data(port);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400508
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800509 kfree(priv);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100510
511 return 0;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800512}
513
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700514static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100515{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100516 struct opticon_private *priv;
517
518 priv = usb_get_serial_port_data(serial->port[0]);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100519
520 usb_kill_urb(priv->bulk_read_urb);
521 return 0;
522}
523
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700524static int opticon_resume(struct usb_serial *serial)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100525{
Oliver Neukumd1c07132009-01-14 18:34:06 +0100526 struct usb_serial_port *port = serial->port[0];
Johan Hovold70d25ee2012-11-18 13:23:30 +0100527 struct opticon_private *priv = usb_get_serial_port_data(port);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100528 int result;
529
Alan Cox82fc5942009-10-06 16:06:46 +0100530 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100531 /* This is protected by the port mutex against close/open */
532 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100533 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
534 else
535 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100536 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100537 return result;
538}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800539
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800540static struct usb_serial_driver opticon_device = {
541 .driver = {
542 .owner = THIS_MODULE,
543 .name = "opticon",
544 },
545 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800546 .num_ports = 1,
Johan Hovold333396f2012-11-18 13:23:33 +0100547 .bulk_in_size = 256,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800548 .attach = opticon_startup,
Johan Hovold70d25ee2012-11-18 13:23:30 +0100549 .port_probe = opticon_port_probe,
550 .port_remove = opticon_port_remove,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800551 .open = opticon_open,
552 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800553 .write = opticon_write,
554 .write_room = opticon_write_room,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800555 .throttle = opticon_throttle,
556 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800557 .ioctl = opticon_ioctl,
558 .tiocmget = opticon_tiocmget,
Martin Jansen309a0572011-02-24 14:50:16 +0100559 .tiocmset = opticon_tiocmset,
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700560 .suspend = opticon_suspend,
561 .resume = opticon_resume,
Johan Hovold5ad47342012-11-18 13:23:34 +0100562 .read_bulk_callback = opticon_read_bulk_callback,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800563};
564
Alan Sternf667dda2012-02-23 14:57:18 -0500565static struct usb_serial_driver * const serial_drivers[] = {
566 &opticon_device, NULL
567};
568
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700569module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800570
Martin Jansen309a0572011-02-24 14:50:16 +0100571MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800572MODULE_LICENSE("GPL");