blob: 92f56e476f2836a88ca0ecceac81112fd079748a [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 usb_serial_port *port;
44 unsigned char *bulk_in_buffer;
45 struct urb *bulk_read_urb;
46 int buffer_size;
47 u8 bulk_address;
48 spinlock_t lock; /* protects the following flags */
49 bool throttled;
50 bool actually_throttled;
51 bool rts;
Martin Jansen309a0572011-02-24 14:50:16 +010052 bool cts;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080053 int outstanding_urbs;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080054};
55
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080056
Martin Jansen309a0572011-02-24 14:50:16 +010057
58static void opticon_read_bulk_callback(struct urb *urb)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080059{
60 struct opticon_private *priv = urb->context;
61 unsigned char *data = urb->transfer_buffer;
62 struct usb_serial_port *port = priv->port;
63 int status = urb->status;
64 struct tty_struct *tty;
65 int result;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080066 int data_length;
Martin Jansen309a0572011-02-24 14:50:16 +010067 unsigned long flags;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080068
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080069 switch (status) {
70 case 0:
71 /* success */
72 break;
73 case -ECONNRESET:
74 case -ENOENT:
75 case -ESHUTDOWN:
76 /* this urb is terminated, clean up */
Johan Hovolde29a7732012-11-18 13:23:23 +010077 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -070078 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080079 return;
80 default:
Johan Hovolde29a7732012-11-18 13:23:23 +010081 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -070082 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080083 goto exit;
84 }
85
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010086 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080087
88 if (urb->actual_length > 2) {
89 data_length = urb->actual_length - 2;
90
91 /*
92 * Data from the device comes with a 2 byte header:
93 *
94 * <0x00><0x00>data...
Martin Jansen309a0572011-02-24 14:50:16 +010095 * This is real data to be sent to the tty layer
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080096 * <0x00><0x01)level
Martin Jansen309a0572011-02-24 14:50:16 +010097 * This is a CTS level change, the third byte is the CTS
98 * value (0 for low, 1 for high).
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080099 */
100 if ((data[0] == 0x00) && (data[1] == 0x00)) {
101 /* real data, send it to the tty layer */
102 tty = tty_port_tty_get(&port->port);
103 if (tty) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200104 tty_insert_flip_string(tty, data + 2,
105 data_length);
Alan Coxa108bfc2010-02-18 16:44:01 +0000106 tty_flip_buffer_push(tty);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800107 tty_kref_put(tty);
108 }
109 } else {
110 if ((data[0] == 0x00) && (data[1] == 0x01)) {
Martin Jansen309a0572011-02-24 14:50:16 +0100111 spin_lock_irqsave(&priv->lock, flags);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300112 /* CTS status information package */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800113 if (data[2] == 0x00)
Martin Jansen309a0572011-02-24 14:50:16 +0100114 priv->cts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800115 else
Martin Jansen309a0572011-02-24 14:50:16 +0100116 priv->cts = true;
117 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800118 } else {
Johan Hovolde29a7732012-11-18 13:23:23 +0100119 dev_dbg(&port->dev,
Alon Zivc6f694a2010-10-10 08:32:20 +0200120 "Unknown data packet received from the device:"
121 " %2x %2x\n",
122 data[0], data[1]);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800123 }
124 }
125 } else {
Johan Hovolde29a7732012-11-18 13:23:23 +0100126 dev_dbg(&port->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100127 "Improper amount of data received from the device, "
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800128 "%d bytes", urb->actual_length);
129 }
130
131exit:
132 spin_lock(&priv->lock);
133
134 /* Continue trying to always read if we should */
135 if (!priv->throttled) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200136 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800137 if (result)
138 dev_err(&port->dev,
139 "%s - failed resubmitting read urb, error %d\n",
140 __func__, result);
141 } else
142 priv->actually_throttled = true;
143 spin_unlock(&priv->lock);
144}
145
Martin Jansen309a0572011-02-24 14:50:16 +0100146static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
147 u8 val)
148{
149 struct usb_serial *serial = port->serial;
150 int retval;
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200151 u8 *buffer;
152
153 buffer = kzalloc(1, GFP_KERNEL);
154 if (!buffer)
155 return -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100156
157 buffer[0] = val;
158 /* Send the message to the vendor control endpoint
159 * of the connected device */
160 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
161 requesttype,
162 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
163 0, 0, buffer, 1, 0);
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200164 kfree(buffer);
Martin Jansen309a0572011-02-24 14:50:16 +0100165
166 return retval;
167}
168
Alan Coxa509a7e2009-09-19 13:13:26 -0700169static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800170{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100171 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800172 unsigned long flags;
173 int result = 0;
174
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800175 spin_lock_irqsave(&priv->lock, flags);
176 priv->throttled = false;
177 priv->actually_throttled = false;
178 priv->port = port;
Martin Jansen309a0572011-02-24 14:50:16 +0100179 priv->rts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800180 spin_unlock_irqrestore(&priv->lock, flags);
181
Martin Jansen309a0572011-02-24 14:50:16 +0100182 /* Clear RTS line */
183 send_control_msg(port, CONTROL_RTS, 0);
184
Martin Jansen309a0572011-02-24 14:50:16 +0100185 /* clear the halt status of the enpoint */
Johan Hovold3157fad2012-11-18 13:23:24 +0100186 usb_clear_halt(port->serial->dev, priv->bulk_read_urb->pipe);
Martin Jansen309a0572011-02-24 14:50:16 +0100187
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800188 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
189 if (result)
190 dev_err(&port->dev,
191 "%s - failed resubmitting read urb, error %d\n",
192 __func__, result);
Martin Jansen309a0572011-02-24 14:50:16 +0100193 /* Request CTS line state, sometimes during opening the current
194 * CTS state can be missed. */
195 send_control_msg(port, RESEND_CTS_STATE, 1);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800196 return result;
197}
198
Alan Cox335f8512009-06-11 12:26:29 +0100199static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800200{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100201 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800202
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800203 /* shutdown our urbs */
204 usb_kill_urb(priv->bulk_read_urb);
205}
206
Martin Jansen309a0572011-02-24 14:50:16 +0100207static void opticon_write_control_callback(struct urb *urb)
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800208{
209 struct opticon_private *priv = urb->context;
210 int status = urb->status;
211 unsigned long flags;
212
213 /* free up the transfer buffer, as usb_free_urb() does not do this */
214 kfree(urb->transfer_buffer);
215
Alon Ziv0d930e52010-10-10 08:32:19 +0200216 /* setup packet may be set if we're using it for writing */
217 kfree(urb->setup_packet);
218
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800219 if (status)
Johan Hovolde29a7732012-11-18 13:23:23 +0100220 dev_dbg(&priv->port->dev,
221 "%s - non-zero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700222 __func__, status);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800223
224 spin_lock_irqsave(&priv->lock, flags);
225 --priv->outstanding_urbs;
226 spin_unlock_irqrestore(&priv->lock, flags);
227
228 usb_serial_port_softint(priv->port);
229}
230
231static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
232 const unsigned char *buf, int count)
233{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100234 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800235 struct usb_serial *serial = port->serial;
236 struct urb *urb;
237 unsigned char *buffer;
238 unsigned long flags;
239 int status;
Martin Jansen309a0572011-02-24 14:50:16 +0100240 struct usb_ctrlrequest *dr;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800241
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800242 spin_lock_irqsave(&priv->lock, flags);
243 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
244 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700245 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800246 return 0;
247 }
248 priv->outstanding_urbs++;
249 spin_unlock_irqrestore(&priv->lock, flags);
250
251 buffer = kmalloc(count, GFP_ATOMIC);
252 if (!buffer) {
253 dev_err(&port->dev, "out of memory\n");
254 count = -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100255
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800256 goto error_no_buffer;
257 }
258
259 urb = usb_alloc_urb(0, GFP_ATOMIC);
260 if (!urb) {
261 dev_err(&port->dev, "no more free urbs\n");
262 count = -ENOMEM;
263 goto error_no_urb;
264 }
265
266 memcpy(buffer, buf, count);
267
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100268 usb_serial_debug_data(&port->dev, __func__, count, buffer);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800269
Martin Jansen309a0572011-02-24 14:50:16 +0100270 /* The conncected devices do not have a bulk write endpoint,
271 * to transmit data to de barcode device the control endpoint is used */
272 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Julia Lawallb0795bb2011-05-13 17:30:46 +0200273 if (!dr) {
274 dev_err(&port->dev, "out of memory\n");
275 count = -ENOMEM;
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200276 goto error_no_dr;
Julia Lawallb0795bb2011-05-13 17:30:46 +0200277 }
Alon Ziv0d930e52010-10-10 08:32:19 +0200278
Martin Jansen309a0572011-02-24 14:50:16 +0100279 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
280 dr->bRequest = 0x01;
281 dr->wValue = 0;
282 dr->wIndex = 0;
283 dr->wLength = cpu_to_le16(count);
Alon Ziv0d930e52010-10-10 08:32:19 +0200284
Martin Jansen309a0572011-02-24 14:50:16 +0100285 usb_fill_control_urb(urb, serial->dev,
286 usb_sndctrlpipe(serial->dev, 0),
287 (unsigned char *)dr, buffer, count,
288 opticon_write_control_callback, priv);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800289
290 /* send it down the pipe */
291 status = usb_submit_urb(urb, GFP_ATOMIC);
292 if (status) {
293 dev_err(&port->dev,
Martin Jansen309a0572011-02-24 14:50:16 +0100294 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800295 __func__, status);
296 count = status;
297 goto error;
298 }
299
300 /* we are done with this urb, so let the host driver
301 * really free it when it is finished with it */
302 usb_free_urb(urb);
303
304 return count;
305error:
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200306 kfree(dr);
307error_no_dr:
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800308 usb_free_urb(urb);
309error_no_urb:
310 kfree(buffer);
311error_no_buffer:
312 spin_lock_irqsave(&priv->lock, flags);
313 --priv->outstanding_urbs;
314 spin_unlock_irqrestore(&priv->lock, flags);
315 return count;
316}
317
318static int opticon_write_room(struct tty_struct *tty)
319{
320 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100321 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800322 unsigned long flags;
323
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800324 /*
325 * We really can take almost anything the user throws at us
326 * but let's pick a nice big number to tell the tty
327 * layer that we have lots of free space, unless we don't.
328 */
329 spin_lock_irqsave(&priv->lock, flags);
330 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
331 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700332 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800333 return 0;
334 }
335 spin_unlock_irqrestore(&priv->lock, flags);
336
337 return 2048;
338}
339
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800340static void opticon_throttle(struct tty_struct *tty)
341{
342 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100343 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800344 unsigned long flags;
345
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800346 spin_lock_irqsave(&priv->lock, flags);
347 priv->throttled = true;
348 spin_unlock_irqrestore(&priv->lock, flags);
349}
350
351
352static void opticon_unthrottle(struct tty_struct *tty)
353{
354 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100355 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800356 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200357 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800358
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800359 spin_lock_irqsave(&priv->lock, flags);
360 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200361 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800362 priv->actually_throttled = false;
363 spin_unlock_irqrestore(&priv->lock, flags);
364
Oliver Neukum88fa6592009-10-07 09:25:10 +0200365 if (was_throttled) {
366 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
367 if (result)
368 dev_err(&port->dev,
369 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800370 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200371 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800372}
373
Alan Cox60b33c12011-02-14 16:26:14 +0000374static int opticon_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800375{
376 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100377 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800378 unsigned long flags;
379 int result = 0;
380
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800381 spin_lock_irqsave(&priv->lock, flags);
382 if (priv->rts)
Martin Jansen309a0572011-02-24 14:50:16 +0100383 result |= TIOCM_RTS;
384 if (priv->cts)
385 result |= TIOCM_CTS;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800386 spin_unlock_irqrestore(&priv->lock, flags);
387
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700388 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800389 return result;
390}
391
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700392static int opticon_tiocmset(struct tty_struct *tty,
Martin Jansen309a0572011-02-24 14:50:16 +0100393 unsigned int set, unsigned int clear)
394{
395 struct usb_serial_port *port = tty->driver_data;
Johan Hovold81d5a672012-04-25 15:56:29 +0200396 struct usb_serial *serial = port->serial;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100397 struct opticon_private *priv = usb_get_serial_port_data(port);
Martin Jansen309a0572011-02-24 14:50:16 +0100398 unsigned long flags;
399 bool rts;
400 bool changed = false;
Johan Hovold81d5a672012-04-25 15:56:29 +0200401 int ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100402
Martin Jansen309a0572011-02-24 14:50:16 +0100403 /* We only support RTS so we only handle that */
404 spin_lock_irqsave(&priv->lock, flags);
405
406 rts = priv->rts;
407 if (set & TIOCM_RTS)
408 priv->rts = true;
409 if (clear & TIOCM_RTS)
410 priv->rts = false;
411 changed = rts ^ priv->rts;
412 spin_unlock_irqrestore(&priv->lock, flags);
413
414 if (!changed)
415 return 0;
416
417 /* Send the new RTS state to the connected device */
Johan Hovold81d5a672012-04-25 15:56:29 +0200418 mutex_lock(&serial->disc_mutex);
419 if (!serial->disconnected)
420 ret = send_control_msg(port, CONTROL_RTS, !rts);
421 else
422 ret = -ENODEV;
423 mutex_unlock(&serial->disc_mutex);
424
425 return ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100426}
427
Johan Hovold56be1a12012-11-18 13:23:31 +0100428static int get_serial_info(struct usb_serial_port *port,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800429 struct serial_struct __user *serial)
430{
431 struct serial_struct tmp;
432
433 if (!serial)
434 return -EFAULT;
435
436 memset(&tmp, 0x00, sizeof(tmp));
437
438 /* fake emulate a 16550 uart to make userspace code happy */
439 tmp.type = PORT_16550A;
Johan Hovold56be1a12012-11-18 13:23:31 +0100440 tmp.line = port->serial->minor;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800441 tmp.port = 0;
442 tmp.irq = 0;
443 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
444 tmp.xmit_fifo_size = 1024;
445 tmp.baud_base = 9600;
446 tmp.close_delay = 5*HZ;
447 tmp.closing_wait = 30*HZ;
448
449 if (copy_to_user(serial, &tmp, sizeof(*serial)))
450 return -EFAULT;
451 return 0;
452}
453
Alan Cox00a0d0d2011-02-14 16:27:06 +0000454static int opticon_ioctl(struct tty_struct *tty,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800455 unsigned int cmd, unsigned long arg)
456{
457 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800458
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700459 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800460
461 switch (cmd) {
462 case TIOCGSERIAL:
Johan Hovold56be1a12012-11-18 13:23:31 +0100463 return get_serial_info(port,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800464 (struct serial_struct __user *)arg);
465 }
466
467 return -ENOIOCTLCMD;
468}
469
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800470static int opticon_startup(struct usb_serial *serial)
471{
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100472 if (!serial->num_bulk_in) {
473 dev_err(&serial->dev->dev, "no bulk in endpoint\n");
474 return -ENODEV;
475 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800476
Johan Hovold70d25ee2012-11-18 13:23:30 +0100477 return 0;
478}
479
480static int opticon_port_probe(struct usb_serial_port *port)
481{
482 struct usb_serial *serial = port->serial;
483 struct opticon_private *priv;
484 int retval = -ENOMEM;
485
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800486 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100487 if (!priv)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800488 return -ENOMEM;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100489
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800490 spin_lock_init(&priv->lock);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100491 priv->port = port;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800492
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100493 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100494 if (!priv->bulk_read_urb)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800495 goto error;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800496
Johan Hovold70d25ee2012-11-18 13:23:30 +0100497 priv->buffer_size = 2 * port->bulk_in_size;
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100498 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100499 if (!priv->bulk_in_buffer)
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100500 goto error;
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100501
Johan Hovold70d25ee2012-11-18 13:23:30 +0100502 priv->bulk_address = port->bulk_in_endpointAddress;
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100503
Johan Hovold0b8718a2012-11-18 13:23:22 +0100504 usb_fill_bulk_urb(priv->bulk_read_urb, serial->dev,
505 usb_rcvbulkpipe(serial->dev,
506 priv->bulk_address),
507 priv->bulk_in_buffer, priv->buffer_size,
508 opticon_read_bulk_callback, priv);
509
Johan Hovold70d25ee2012-11-18 13:23:30 +0100510 usb_set_serial_port_data(port, priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800511
Johan Hovold70d25ee2012-11-18 13:23:30 +0100512 return 0;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800513error:
514 usb_free_urb(priv->bulk_read_urb);
515 kfree(priv->bulk_in_buffer);
516 kfree(priv);
517 return retval;
518}
519
Johan Hovold70d25ee2012-11-18 13:23:30 +0100520static int opticon_port_remove(struct usb_serial_port *port)
Alan Sternf9c99bb2009-06-02 11:53:55 -0400521{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100522 struct opticon_private *priv = usb_get_serial_port_data(port);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400523
Johan Hovold70f9bf62012-11-18 13:23:28 +0100524 usb_free_urb(priv->bulk_read_urb);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800525 kfree(priv->bulk_in_buffer);
526 kfree(priv);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100527
528 return 0;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800529}
530
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700531static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100532{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100533 struct opticon_private *priv;
534
535 priv = usb_get_serial_port_data(serial->port[0]);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100536
537 usb_kill_urb(priv->bulk_read_urb);
538 return 0;
539}
540
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700541static int opticon_resume(struct usb_serial *serial)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100542{
Oliver Neukumd1c07132009-01-14 18:34:06 +0100543 struct usb_serial_port *port = serial->port[0];
Johan Hovold70d25ee2012-11-18 13:23:30 +0100544 struct opticon_private *priv = usb_get_serial_port_data(port);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100545 int result;
546
Alan Cox82fc5942009-10-06 16:06:46 +0100547 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100548 /* This is protected by the port mutex against close/open */
549 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100550 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
551 else
552 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100553 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100554 return result;
555}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800556
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800557static struct usb_serial_driver opticon_device = {
558 .driver = {
559 .owner = THIS_MODULE,
560 .name = "opticon",
561 },
562 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800563 .num_ports = 1,
564 .attach = opticon_startup,
Johan Hovold70d25ee2012-11-18 13:23:30 +0100565 .port_probe = opticon_port_probe,
566 .port_remove = opticon_port_remove,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800567 .open = opticon_open,
568 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800569 .write = opticon_write,
570 .write_room = opticon_write_room,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800571 .throttle = opticon_throttle,
572 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800573 .ioctl = opticon_ioctl,
574 .tiocmget = opticon_tiocmget,
Martin Jansen309a0572011-02-24 14:50:16 +0100575 .tiocmset = opticon_tiocmset,
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700576 .suspend = opticon_suspend,
577 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800578};
579
Alan Sternf667dda2012-02-23 14:57:18 -0500580static struct usb_serial_driver * const serial_drivers[] = {
581 &opticon_device, NULL
582};
583
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700584module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800585
Martin Jansen309a0572011-02-24 14:50:16 +0100586MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800587MODULE_LICENSE("GPL");