blob: 543d8c79b026a5b5fb5151dedcc41802a29a4e44 [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 unsigned char *bulk_in_buffer;
44 struct urb *bulk_read_urb;
45 int buffer_size;
46 u8 bulk_address;
47 spinlock_t lock; /* protects the following flags */
48 bool throttled;
49 bool actually_throttled;
50 bool rts;
Martin Jansen309a0572011-02-24 14:50:16 +010051 bool cts;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080052 int outstanding_urbs;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080053};
54
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080055
Martin Jansen309a0572011-02-24 14:50:16 +010056
57static void opticon_read_bulk_callback(struct urb *urb)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080058{
Johan Hovolde32d82bc2012-11-18 13:23:32 +010059 struct usb_serial_port *port = urb->context;
60 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080061 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080062 int status = urb->status;
63 struct tty_struct *tty;
64 int result;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080065 int data_length;
Martin Jansen309a0572011-02-24 14:50:16 +010066 unsigned long flags;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080067
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080068 switch (status) {
69 case 0:
70 /* success */
71 break;
72 case -ECONNRESET:
73 case -ENOENT:
74 case -ESHUTDOWN:
75 /* this urb is terminated, clean up */
Johan Hovolde29a7732012-11-18 13:23:23 +010076 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -070077 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080078 return;
79 default:
Johan Hovolde29a7732012-11-18 13:23:23 +010080 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -070081 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080082 goto exit;
83 }
84
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010085 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080086
87 if (urb->actual_length > 2) {
88 data_length = urb->actual_length - 2;
89
90 /*
91 * Data from the device comes with a 2 byte header:
92 *
93 * <0x00><0x00>data...
Martin Jansen309a0572011-02-24 14:50:16 +010094 * This is real data to be sent to the tty layer
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080095 * <0x00><0x01)level
Martin Jansen309a0572011-02-24 14:50:16 +010096 * This is a CTS level change, the third byte is the CTS
97 * value (0 for low, 1 for high).
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080098 */
99 if ((data[0] == 0x00) && (data[1] == 0x00)) {
100 /* real data, send it to the tty layer */
101 tty = tty_port_tty_get(&port->port);
102 if (tty) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200103 tty_insert_flip_string(tty, data + 2,
104 data_length);
Alan Coxa108bfc2010-02-18 16:44:01 +0000105 tty_flip_buffer_push(tty);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800106 tty_kref_put(tty);
107 }
108 } else {
109 if ((data[0] == 0x00) && (data[1] == 0x01)) {
Martin Jansen309a0572011-02-24 14:50:16 +0100110 spin_lock_irqsave(&priv->lock, flags);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300111 /* CTS status information package */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800112 if (data[2] == 0x00)
Martin Jansen309a0572011-02-24 14:50:16 +0100113 priv->cts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800114 else
Martin Jansen309a0572011-02-24 14:50:16 +0100115 priv->cts = true;
116 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800117 } else {
Johan Hovolde29a7732012-11-18 13:23:23 +0100118 dev_dbg(&port->dev,
Alon Zivc6f694a2010-10-10 08:32:20 +0200119 "Unknown data packet received from the device:"
120 " %2x %2x\n",
121 data[0], data[1]);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800122 }
123 }
124 } else {
Johan Hovolde29a7732012-11-18 13:23:23 +0100125 dev_dbg(&port->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100126 "Improper amount of data received from the device, "
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800127 "%d bytes", urb->actual_length);
128 }
129
130exit:
131 spin_lock(&priv->lock);
132
133 /* Continue trying to always read if we should */
134 if (!priv->throttled) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200135 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800136 if (result)
137 dev_err(&port->dev,
138 "%s - failed resubmitting read urb, error %d\n",
139 __func__, result);
140 } else
141 priv->actually_throttled = true;
142 spin_unlock(&priv->lock);
143}
144
Martin Jansen309a0572011-02-24 14:50:16 +0100145static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
146 u8 val)
147{
148 struct usb_serial *serial = port->serial;
149 int retval;
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200150 u8 *buffer;
151
152 buffer = kzalloc(1, GFP_KERNEL);
153 if (!buffer)
154 return -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100155
156 buffer[0] = val;
157 /* Send the message to the vendor control endpoint
158 * of the connected device */
159 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
160 requesttype,
161 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
162 0, 0, buffer, 1, 0);
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200163 kfree(buffer);
Martin Jansen309a0572011-02-24 14:50:16 +0100164
165 return retval;
166}
167
Alan Coxa509a7e2009-09-19 13:13:26 -0700168static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800169{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100170 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800171 unsigned long flags;
172 int result = 0;
173
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800174 spin_lock_irqsave(&priv->lock, flags);
175 priv->throttled = false;
176 priv->actually_throttled = false;
Martin Jansen309a0572011-02-24 14:50:16 +0100177 priv->rts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800178 spin_unlock_irqrestore(&priv->lock, flags);
179
Martin Jansen309a0572011-02-24 14:50:16 +0100180 /* Clear RTS line */
181 send_control_msg(port, CONTROL_RTS, 0);
182
Martin Jansen309a0572011-02-24 14:50:16 +0100183 /* clear the halt status of the enpoint */
Johan Hovold3157fad2012-11-18 13:23:24 +0100184 usb_clear_halt(port->serial->dev, priv->bulk_read_urb->pipe);
Martin Jansen309a0572011-02-24 14:50:16 +0100185
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800186 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
187 if (result)
188 dev_err(&port->dev,
189 "%s - failed resubmitting read urb, error %d\n",
190 __func__, result);
Martin Jansen309a0572011-02-24 14:50:16 +0100191 /* Request CTS line state, sometimes during opening the current
192 * CTS state can be missed. */
193 send_control_msg(port, RESEND_CTS_STATE, 1);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800194 return result;
195}
196
Alan Cox335f8512009-06-11 12:26:29 +0100197static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800198{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100199 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800200
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800201 /* shutdown our urbs */
202 usb_kill_urb(priv->bulk_read_urb);
203}
204
Martin Jansen309a0572011-02-24 14:50:16 +0100205static void opticon_write_control_callback(struct urb *urb)
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800206{
Johan Hovolde32d82bc2012-11-18 13:23:32 +0100207 struct usb_serial_port *port = urb->context;
208 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800209 int status = urb->status;
210 unsigned long flags;
211
212 /* free up the transfer buffer, as usb_free_urb() does not do this */
213 kfree(urb->transfer_buffer);
214
Alon Ziv0d930e52010-10-10 08:32:19 +0200215 /* setup packet may be set if we're using it for writing */
216 kfree(urb->setup_packet);
217
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800218 if (status)
Johan Hovolde32d82bc2012-11-18 13:23:32 +0100219 dev_dbg(&port->dev,
Johan Hovolde29a7732012-11-18 13:23:23 +0100220 "%s - non-zero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700221 __func__, status);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800222
223 spin_lock_irqsave(&priv->lock, flags);
224 --priv->outstanding_urbs;
225 spin_unlock_irqrestore(&priv->lock, flags);
226
Johan Hovolde32d82bc2012-11-18 13:23:32 +0100227 usb_serial_port_softint(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800228}
229
230static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
231 const unsigned char *buf, int count)
232{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100233 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800234 struct usb_serial *serial = port->serial;
235 struct urb *urb;
236 unsigned char *buffer;
237 unsigned long flags;
238 int status;
Martin Jansen309a0572011-02-24 14:50:16 +0100239 struct usb_ctrlrequest *dr;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800240
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800241 spin_lock_irqsave(&priv->lock, flags);
242 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
243 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700244 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800245 return 0;
246 }
247 priv->outstanding_urbs++;
248 spin_unlock_irqrestore(&priv->lock, flags);
249
250 buffer = kmalloc(count, GFP_ATOMIC);
251 if (!buffer) {
252 dev_err(&port->dev, "out of memory\n");
253 count = -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100254
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800255 goto error_no_buffer;
256 }
257
258 urb = usb_alloc_urb(0, GFP_ATOMIC);
259 if (!urb) {
260 dev_err(&port->dev, "no more free urbs\n");
261 count = -ENOMEM;
262 goto error_no_urb;
263 }
264
265 memcpy(buffer, buf, count);
266
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100267 usb_serial_debug_data(&port->dev, __func__, count, buffer);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800268
Martin Jansen309a0572011-02-24 14:50:16 +0100269 /* The conncected devices do not have a bulk write endpoint,
270 * to transmit data to de barcode device the control endpoint is used */
271 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Julia Lawallb0795bb2011-05-13 17:30:46 +0200272 if (!dr) {
273 dev_err(&port->dev, "out of memory\n");
274 count = -ENOMEM;
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200275 goto error_no_dr;
Julia Lawallb0795bb2011-05-13 17:30:46 +0200276 }
Alon Ziv0d930e52010-10-10 08:32:19 +0200277
Martin Jansen309a0572011-02-24 14:50:16 +0100278 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
279 dr->bRequest = 0x01;
280 dr->wValue = 0;
281 dr->wIndex = 0;
282 dr->wLength = cpu_to_le16(count);
Alon Ziv0d930e52010-10-10 08:32:19 +0200283
Martin Jansen309a0572011-02-24 14:50:16 +0100284 usb_fill_control_urb(urb, serial->dev,
285 usb_sndctrlpipe(serial->dev, 0),
286 (unsigned char *)dr, buffer, count,
Johan Hovolde32d82bc2012-11-18 13:23:32 +0100287 opticon_write_control_callback, port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800288
289 /* send it down the pipe */
290 status = usb_submit_urb(urb, GFP_ATOMIC);
291 if (status) {
292 dev_err(&port->dev,
Martin Jansen309a0572011-02-24 14:50:16 +0100293 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800294 __func__, status);
295 count = status;
296 goto error;
297 }
298
299 /* we are done with this urb, so let the host driver
300 * really free it when it is finished with it */
301 usb_free_urb(urb);
302
303 return count;
304error:
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200305 kfree(dr);
306error_no_dr:
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800307 usb_free_urb(urb);
308error_no_urb:
309 kfree(buffer);
310error_no_buffer:
311 spin_lock_irqsave(&priv->lock, flags);
312 --priv->outstanding_urbs;
313 spin_unlock_irqrestore(&priv->lock, flags);
314 return count;
315}
316
317static int opticon_write_room(struct tty_struct *tty)
318{
319 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100320 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800321 unsigned long flags;
322
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800323 /*
324 * We really can take almost anything the user throws at us
325 * but let's pick a nice big number to tell the tty
326 * layer that we have lots of free space, unless we don't.
327 */
328 spin_lock_irqsave(&priv->lock, flags);
329 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
330 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700331 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800332 return 0;
333 }
334 spin_unlock_irqrestore(&priv->lock, flags);
335
336 return 2048;
337}
338
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800339static void opticon_throttle(struct tty_struct *tty)
340{
341 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100342 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800343 unsigned long flags;
344
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800345 spin_lock_irqsave(&priv->lock, flags);
346 priv->throttled = true;
347 spin_unlock_irqrestore(&priv->lock, flags);
348}
349
350
351static void opticon_unthrottle(struct tty_struct *tty)
352{
353 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100354 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800355 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200356 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800357
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800358 spin_lock_irqsave(&priv->lock, flags);
359 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200360 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800361 priv->actually_throttled = false;
362 spin_unlock_irqrestore(&priv->lock, flags);
363
Oliver Neukum88fa6592009-10-07 09:25:10 +0200364 if (was_throttled) {
365 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
366 if (result)
367 dev_err(&port->dev,
368 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800369 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200370 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800371}
372
Alan Cox60b33c12011-02-14 16:26:14 +0000373static int opticon_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800374{
375 struct usb_serial_port *port = tty->driver_data;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100376 struct opticon_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800377 unsigned long flags;
378 int result = 0;
379
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800380 spin_lock_irqsave(&priv->lock, flags);
381 if (priv->rts)
Martin Jansen309a0572011-02-24 14:50:16 +0100382 result |= TIOCM_RTS;
383 if (priv->cts)
384 result |= TIOCM_CTS;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800385 spin_unlock_irqrestore(&priv->lock, flags);
386
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700387 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800388 return result;
389}
390
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700391static int opticon_tiocmset(struct tty_struct *tty,
Martin Jansen309a0572011-02-24 14:50:16 +0100392 unsigned int set, unsigned int clear)
393{
394 struct usb_serial_port *port = tty->driver_data;
Johan Hovold81d5a672012-04-25 15:56:29 +0200395 struct usb_serial *serial = port->serial;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100396 struct opticon_private *priv = usb_get_serial_port_data(port);
Martin Jansen309a0572011-02-24 14:50:16 +0100397 unsigned long flags;
398 bool rts;
399 bool changed = false;
Johan Hovold81d5a672012-04-25 15:56:29 +0200400 int ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100401
Martin Jansen309a0572011-02-24 14:50:16 +0100402 /* We only support RTS so we only handle that */
403 spin_lock_irqsave(&priv->lock, flags);
404
405 rts = priv->rts;
406 if (set & TIOCM_RTS)
407 priv->rts = true;
408 if (clear & TIOCM_RTS)
409 priv->rts = false;
410 changed = rts ^ priv->rts;
411 spin_unlock_irqrestore(&priv->lock, flags);
412
413 if (!changed)
414 return 0;
415
416 /* Send the new RTS state to the connected device */
Johan Hovold81d5a672012-04-25 15:56:29 +0200417 mutex_lock(&serial->disc_mutex);
418 if (!serial->disconnected)
419 ret = send_control_msg(port, CONTROL_RTS, !rts);
420 else
421 ret = -ENODEV;
422 mutex_unlock(&serial->disc_mutex);
423
424 return ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100425}
426
Johan Hovold56be1a12012-11-18 13:23:31 +0100427static int get_serial_info(struct usb_serial_port *port,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800428 struct serial_struct __user *serial)
429{
430 struct serial_struct tmp;
431
432 if (!serial)
433 return -EFAULT;
434
435 memset(&tmp, 0x00, sizeof(tmp));
436
437 /* fake emulate a 16550 uart to make userspace code happy */
438 tmp.type = PORT_16550A;
Johan Hovold56be1a12012-11-18 13:23:31 +0100439 tmp.line = port->serial->minor;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800440 tmp.port = 0;
441 tmp.irq = 0;
442 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
443 tmp.xmit_fifo_size = 1024;
444 tmp.baud_base = 9600;
445 tmp.close_delay = 5*HZ;
446 tmp.closing_wait = 30*HZ;
447
448 if (copy_to_user(serial, &tmp, sizeof(*serial)))
449 return -EFAULT;
450 return 0;
451}
452
Alan Cox00a0d0d2011-02-14 16:27:06 +0000453static int opticon_ioctl(struct tty_struct *tty,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800454 unsigned int cmd, unsigned long arg)
455{
456 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800457
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700458 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800459
460 switch (cmd) {
461 case TIOCGSERIAL:
Johan Hovold56be1a12012-11-18 13:23:31 +0100462 return get_serial_info(port,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800463 (struct serial_struct __user *)arg);
464 }
465
466 return -ENOIOCTLCMD;
467}
468
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800469static int opticon_startup(struct usb_serial *serial)
470{
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100471 if (!serial->num_bulk_in) {
472 dev_err(&serial->dev->dev, "no bulk in endpoint\n");
473 return -ENODEV;
474 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800475
Johan Hovold70d25ee2012-11-18 13:23:30 +0100476 return 0;
477}
478
479static int opticon_port_probe(struct usb_serial_port *port)
480{
481 struct usb_serial *serial = port->serial;
482 struct opticon_private *priv;
483 int retval = -ENOMEM;
484
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800485 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100486 if (!priv)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800487 return -ENOMEM;
Johan Hovold70d25ee2012-11-18 13:23:30 +0100488
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800489 spin_lock_init(&priv->lock);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800490
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100491 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100492 if (!priv->bulk_read_urb)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800493 goto error;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800494
Johan Hovold333396f2012-11-18 13:23:33 +0100495 priv->buffer_size = port->bulk_in_size;
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100496 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100497 if (!priv->bulk_in_buffer)
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100498 goto error;
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100499
Johan Hovold70d25ee2012-11-18 13:23:30 +0100500 priv->bulk_address = port->bulk_in_endpointAddress;
Johan Hovolda0a5fd92012-11-18 13:23:27 +0100501
Johan Hovold0b8718a2012-11-18 13:23:22 +0100502 usb_fill_bulk_urb(priv->bulk_read_urb, serial->dev,
503 usb_rcvbulkpipe(serial->dev,
504 priv->bulk_address),
505 priv->bulk_in_buffer, priv->buffer_size,
Johan Hovolde32d82bc2012-11-18 13:23:32 +0100506 opticon_read_bulk_callback, port);
Johan Hovold0b8718a2012-11-18 13:23:22 +0100507
Johan Hovold70d25ee2012-11-18 13:23:30 +0100508 usb_set_serial_port_data(port, priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800509
Johan Hovold70d25ee2012-11-18 13:23:30 +0100510 return 0;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800511error:
512 usb_free_urb(priv->bulk_read_urb);
513 kfree(priv->bulk_in_buffer);
514 kfree(priv);
515 return retval;
516}
517
Johan Hovold70d25ee2012-11-18 13:23:30 +0100518static int opticon_port_remove(struct usb_serial_port *port)
Alan Sternf9c99bb2009-06-02 11:53:55 -0400519{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100520 struct opticon_private *priv = usb_get_serial_port_data(port);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400521
Johan Hovold70f9bf62012-11-18 13:23:28 +0100522 usb_free_urb(priv->bulk_read_urb);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800523 kfree(priv->bulk_in_buffer);
524 kfree(priv);
Johan Hovold70d25ee2012-11-18 13:23:30 +0100525
526 return 0;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800527}
528
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700529static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100530{
Johan Hovold70d25ee2012-11-18 13:23:30 +0100531 struct opticon_private *priv;
532
533 priv = usb_get_serial_port_data(serial->port[0]);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100534
535 usb_kill_urb(priv->bulk_read_urb);
536 return 0;
537}
538
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700539static int opticon_resume(struct usb_serial *serial)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100540{
Oliver Neukumd1c07132009-01-14 18:34:06 +0100541 struct usb_serial_port *port = serial->port[0];
Johan Hovold70d25ee2012-11-18 13:23:30 +0100542 struct opticon_private *priv = usb_get_serial_port_data(port);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100543 int result;
544
Alan Cox82fc5942009-10-06 16:06:46 +0100545 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100546 /* This is protected by the port mutex against close/open */
547 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100548 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
549 else
550 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100551 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100552 return result;
553}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800554
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800555static struct usb_serial_driver opticon_device = {
556 .driver = {
557 .owner = THIS_MODULE,
558 .name = "opticon",
559 },
560 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800561 .num_ports = 1,
Johan Hovold333396f2012-11-18 13:23:33 +0100562 .bulk_in_size = 256,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800563 .attach = opticon_startup,
Johan Hovold70d25ee2012-11-18 13:23:30 +0100564 .port_probe = opticon_port_probe,
565 .port_remove = opticon_port_remove,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800566 .open = opticon_open,
567 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800568 .write = opticon_write,
569 .write_room = opticon_write_room,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800570 .throttle = opticon_throttle,
571 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800572 .ioctl = opticon_ioctl,
573 .tiocmget = opticon_tiocmget,
Martin Jansen309a0572011-02-24 14:50:16 +0100574 .tiocmset = opticon_tiocmset,
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700575 .suspend = opticon_suspend,
576 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800577};
578
Alan Sternf667dda2012-02-23 14:57:18 -0500579static struct usb_serial_driver * const serial_drivers[] = {
580 &opticon_device, NULL
581};
582
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700583module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800584
Martin Jansen309a0572011-02-24 14:50:16 +0100585MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800586MODULE_LICENSE("GPL");