blob: f81ffb019314cf9ffb5bc95f37f8e2cb8f9305a9 [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 *serial;
44 struct usb_serial_port *port;
45 unsigned char *bulk_in_buffer;
46 struct urb *bulk_read_urb;
47 int buffer_size;
48 u8 bulk_address;
49 spinlock_t lock; /* protects the following flags */
50 bool throttled;
51 bool actually_throttled;
52 bool rts;
Martin Jansen309a0572011-02-24 14:50:16 +010053 bool cts;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080054 int outstanding_urbs;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080055};
56
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080057
Martin Jansen309a0572011-02-24 14:50:16 +010058
59static void opticon_read_bulk_callback(struct urb *urb)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080060{
61 struct opticon_private *priv = urb->context;
62 unsigned char *data = urb->transfer_buffer;
63 struct usb_serial_port *port = priv->port;
64 int status = urb->status;
65 struct tty_struct *tty;
66 int result;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080067 int data_length;
Martin Jansen309a0572011-02-24 14:50:16 +010068 unsigned long flags;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080069
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080070 switch (status) {
71 case 0:
72 /* success */
73 break;
74 case -ECONNRESET:
75 case -ENOENT:
76 case -ESHUTDOWN:
77 /* this urb is terminated, clean up */
Johan Hovolde29a7732012-11-18 13:23:23 +010078 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -070079 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080080 return;
81 default:
Johan Hovolde29a7732012-11-18 13:23:23 +010082 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -070083 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080084 goto exit;
85 }
86
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010087 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080088
89 if (urb->actual_length > 2) {
90 data_length = urb->actual_length - 2;
91
92 /*
93 * Data from the device comes with a 2 byte header:
94 *
95 * <0x00><0x00>data...
Martin Jansen309a0572011-02-24 14:50:16 +010096 * This is real data to be sent to the tty layer
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080097 * <0x00><0x01)level
Martin Jansen309a0572011-02-24 14:50:16 +010098 * This is a CTS level change, the third byte is the CTS
99 * value (0 for low, 1 for high).
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800100 */
101 if ((data[0] == 0x00) && (data[1] == 0x00)) {
102 /* real data, send it to the tty layer */
103 tty = tty_port_tty_get(&port->port);
104 if (tty) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200105 tty_insert_flip_string(tty, data + 2,
106 data_length);
Alan Coxa108bfc2010-02-18 16:44:01 +0000107 tty_flip_buffer_push(tty);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800108 tty_kref_put(tty);
109 }
110 } else {
111 if ((data[0] == 0x00) && (data[1] == 0x01)) {
Martin Jansen309a0572011-02-24 14:50:16 +0100112 spin_lock_irqsave(&priv->lock, flags);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300113 /* CTS status information package */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800114 if (data[2] == 0x00)
Martin Jansen309a0572011-02-24 14:50:16 +0100115 priv->cts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800116 else
Martin Jansen309a0572011-02-24 14:50:16 +0100117 priv->cts = true;
118 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800119 } else {
Johan Hovolde29a7732012-11-18 13:23:23 +0100120 dev_dbg(&port->dev,
Alon Zivc6f694a2010-10-10 08:32:20 +0200121 "Unknown data packet received from the device:"
122 " %2x %2x\n",
123 data[0], data[1]);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800124 }
125 }
126 } else {
Johan Hovolde29a7732012-11-18 13:23:23 +0100127 dev_dbg(&port->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100128 "Improper amount of data received from the device, "
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800129 "%d bytes", urb->actual_length);
130 }
131
132exit:
133 spin_lock(&priv->lock);
134
135 /* Continue trying to always read if we should */
136 if (!priv->throttled) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200137 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800138 if (result)
139 dev_err(&port->dev,
140 "%s - failed resubmitting read urb, error %d\n",
141 __func__, result);
142 } else
143 priv->actually_throttled = true;
144 spin_unlock(&priv->lock);
145}
146
Martin Jansen309a0572011-02-24 14:50:16 +0100147static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
148 u8 val)
149{
150 struct usb_serial *serial = port->serial;
151 int retval;
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200152 u8 *buffer;
153
154 buffer = kzalloc(1, GFP_KERNEL);
155 if (!buffer)
156 return -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100157
158 buffer[0] = val;
159 /* Send the message to the vendor control endpoint
160 * of the connected device */
161 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
162 requesttype,
163 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
164 0, 0, buffer, 1, 0);
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200165 kfree(buffer);
Martin Jansen309a0572011-02-24 14:50:16 +0100166
167 return retval;
168}
169
Alan Coxa509a7e2009-09-19 13:13:26 -0700170static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800171{
172 struct opticon_private *priv = usb_get_serial_data(port->serial);
173 unsigned long flags;
174 int result = 0;
175
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800176 spin_lock_irqsave(&priv->lock, flags);
177 priv->throttled = false;
178 priv->actually_throttled = false;
179 priv->port = port;
Martin Jansen309a0572011-02-24 14:50:16 +0100180 priv->rts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800181 spin_unlock_irqrestore(&priv->lock, flags);
182
Martin Jansen309a0572011-02-24 14:50:16 +0100183 /* Clear RTS line */
184 send_control_msg(port, CONTROL_RTS, 0);
185
Martin Jansen309a0572011-02-24 14:50:16 +0100186 /* clear the halt status of the enpoint */
Johan Hovold3157fad2012-11-18 13:23:24 +0100187 usb_clear_halt(port->serial->dev, priv->bulk_read_urb->pipe);
Martin Jansen309a0572011-02-24 14:50:16 +0100188
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800189 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
190 if (result)
191 dev_err(&port->dev,
192 "%s - failed resubmitting read urb, error %d\n",
193 __func__, result);
Martin Jansen309a0572011-02-24 14:50:16 +0100194 /* Request CTS line state, sometimes during opening the current
195 * CTS state can be missed. */
196 send_control_msg(port, RESEND_CTS_STATE, 1);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800197 return result;
198}
199
Alan Cox335f8512009-06-11 12:26:29 +0100200static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800201{
202 struct opticon_private *priv = usb_get_serial_data(port->serial);
203
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800204 /* shutdown our urbs */
205 usb_kill_urb(priv->bulk_read_urb);
206}
207
Martin Jansen309a0572011-02-24 14:50:16 +0100208static void opticon_write_control_callback(struct urb *urb)
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800209{
210 struct opticon_private *priv = urb->context;
211 int status = urb->status;
212 unsigned long flags;
213
214 /* free up the transfer buffer, as usb_free_urb() does not do this */
215 kfree(urb->transfer_buffer);
216
Alon Ziv0d930e52010-10-10 08:32:19 +0200217 /* setup packet may be set if we're using it for writing */
218 kfree(urb->setup_packet);
219
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800220 if (status)
Johan Hovolde29a7732012-11-18 13:23:23 +0100221 dev_dbg(&priv->port->dev,
222 "%s - non-zero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700223 __func__, status);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800224
225 spin_lock_irqsave(&priv->lock, flags);
226 --priv->outstanding_urbs;
227 spin_unlock_irqrestore(&priv->lock, flags);
228
229 usb_serial_port_softint(priv->port);
230}
231
232static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
233 const unsigned char *buf, int count)
234{
235 struct opticon_private *priv = usb_get_serial_data(port->serial);
236 struct usb_serial *serial = port->serial;
237 struct urb *urb;
238 unsigned char *buffer;
239 unsigned long flags;
240 int status;
Martin Jansen309a0572011-02-24 14:50:16 +0100241 struct usb_ctrlrequest *dr;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800242
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800243 spin_lock_irqsave(&priv->lock, flags);
244 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
245 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700246 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800247 return 0;
248 }
249 priv->outstanding_urbs++;
250 spin_unlock_irqrestore(&priv->lock, flags);
251
252 buffer = kmalloc(count, GFP_ATOMIC);
253 if (!buffer) {
254 dev_err(&port->dev, "out of memory\n");
255 count = -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100256
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800257 goto error_no_buffer;
258 }
259
260 urb = usb_alloc_urb(0, GFP_ATOMIC);
261 if (!urb) {
262 dev_err(&port->dev, "no more free urbs\n");
263 count = -ENOMEM;
264 goto error_no_urb;
265 }
266
267 memcpy(buffer, buf, count);
268
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100269 usb_serial_debug_data(&port->dev, __func__, count, buffer);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800270
Martin Jansen309a0572011-02-24 14:50:16 +0100271 /* The conncected devices do not have a bulk write endpoint,
272 * to transmit data to de barcode device the control endpoint is used */
273 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Julia Lawallb0795bb2011-05-13 17:30:46 +0200274 if (!dr) {
275 dev_err(&port->dev, "out of memory\n");
276 count = -ENOMEM;
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200277 goto error_no_dr;
Julia Lawallb0795bb2011-05-13 17:30:46 +0200278 }
Alon Ziv0d930e52010-10-10 08:32:19 +0200279
Martin Jansen309a0572011-02-24 14:50:16 +0100280 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
281 dr->bRequest = 0x01;
282 dr->wValue = 0;
283 dr->wIndex = 0;
284 dr->wLength = cpu_to_le16(count);
Alon Ziv0d930e52010-10-10 08:32:19 +0200285
Martin Jansen309a0572011-02-24 14:50:16 +0100286 usb_fill_control_urb(urb, serial->dev,
287 usb_sndctrlpipe(serial->dev, 0),
288 (unsigned char *)dr, buffer, count,
289 opticon_write_control_callback, priv);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800290
291 /* send it down the pipe */
292 status = usb_submit_urb(urb, GFP_ATOMIC);
293 if (status) {
294 dev_err(&port->dev,
Martin Jansen309a0572011-02-24 14:50:16 +0100295 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800296 __func__, status);
297 count = status;
298 goto error;
299 }
300
301 /* we are done with this urb, so let the host driver
302 * really free it when it is finished with it */
303 usb_free_urb(urb);
304
305 return count;
306error:
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200307 kfree(dr);
308error_no_dr:
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800309 usb_free_urb(urb);
310error_no_urb:
311 kfree(buffer);
312error_no_buffer:
313 spin_lock_irqsave(&priv->lock, flags);
314 --priv->outstanding_urbs;
315 spin_unlock_irqrestore(&priv->lock, flags);
316 return count;
317}
318
319static int opticon_write_room(struct tty_struct *tty)
320{
321 struct usb_serial_port *port = tty->driver_data;
322 struct opticon_private *priv = usb_get_serial_data(port->serial);
323 unsigned long flags;
324
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800325 /*
326 * We really can take almost anything the user throws at us
327 * but let's pick a nice big number to tell the tty
328 * layer that we have lots of free space, unless we don't.
329 */
330 spin_lock_irqsave(&priv->lock, flags);
331 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
332 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700333 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800334 return 0;
335 }
336 spin_unlock_irqrestore(&priv->lock, flags);
337
338 return 2048;
339}
340
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800341static void opticon_throttle(struct tty_struct *tty)
342{
343 struct usb_serial_port *port = tty->driver_data;
344 struct opticon_private *priv = usb_get_serial_data(port->serial);
345 unsigned long flags;
346
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800347 spin_lock_irqsave(&priv->lock, flags);
348 priv->throttled = true;
349 spin_unlock_irqrestore(&priv->lock, flags);
350}
351
352
353static void opticon_unthrottle(struct tty_struct *tty)
354{
355 struct usb_serial_port *port = tty->driver_data;
356 struct opticon_private *priv = usb_get_serial_data(port->serial);
357 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200358 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800359
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800360 spin_lock_irqsave(&priv->lock, flags);
361 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200362 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800363 priv->actually_throttled = false;
364 spin_unlock_irqrestore(&priv->lock, flags);
365
Oliver Neukum88fa6592009-10-07 09:25:10 +0200366 if (was_throttled) {
367 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
368 if (result)
369 dev_err(&port->dev,
370 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800371 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200372 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800373}
374
Alan Cox60b33c12011-02-14 16:26:14 +0000375static int opticon_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800376{
377 struct usb_serial_port *port = tty->driver_data;
378 struct opticon_private *priv = usb_get_serial_data(port->serial);
379 unsigned long flags;
380 int result = 0;
381
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800382 spin_lock_irqsave(&priv->lock, flags);
383 if (priv->rts)
Martin Jansen309a0572011-02-24 14:50:16 +0100384 result |= TIOCM_RTS;
385 if (priv->cts)
386 result |= TIOCM_CTS;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800387 spin_unlock_irqrestore(&priv->lock, flags);
388
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700389 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800390 return result;
391}
392
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700393static int opticon_tiocmset(struct tty_struct *tty,
Martin Jansen309a0572011-02-24 14:50:16 +0100394 unsigned int set, unsigned int clear)
395{
396 struct usb_serial_port *port = tty->driver_data;
Johan Hovold81d5a672012-04-25 15:56:29 +0200397 struct usb_serial *serial = port->serial;
Martin Jansen309a0572011-02-24 14:50:16 +0100398 struct opticon_private *priv = usb_get_serial_data(port->serial);
399 unsigned long flags;
400 bool rts;
401 bool changed = false;
Johan Hovold81d5a672012-04-25 15:56:29 +0200402 int ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100403
Martin Jansen309a0572011-02-24 14:50:16 +0100404 /* We only support RTS so we only handle that */
405 spin_lock_irqsave(&priv->lock, flags);
406
407 rts = priv->rts;
408 if (set & TIOCM_RTS)
409 priv->rts = true;
410 if (clear & TIOCM_RTS)
411 priv->rts = false;
412 changed = rts ^ priv->rts;
413 spin_unlock_irqrestore(&priv->lock, flags);
414
415 if (!changed)
416 return 0;
417
418 /* Send the new RTS state to the connected device */
Johan Hovold81d5a672012-04-25 15:56:29 +0200419 mutex_lock(&serial->disc_mutex);
420 if (!serial->disconnected)
421 ret = send_control_msg(port, CONTROL_RTS, !rts);
422 else
423 ret = -ENODEV;
424 mutex_unlock(&serial->disc_mutex);
425
426 return ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100427}
428
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800429static int get_serial_info(struct opticon_private *priv,
430 struct serial_struct __user *serial)
431{
432 struct serial_struct tmp;
433
434 if (!serial)
435 return -EFAULT;
436
437 memset(&tmp, 0x00, sizeof(tmp));
438
439 /* fake emulate a 16550 uart to make userspace code happy */
440 tmp.type = PORT_16550A;
441 tmp.line = priv->serial->minor;
442 tmp.port = 0;
443 tmp.irq = 0;
444 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
445 tmp.xmit_fifo_size = 1024;
446 tmp.baud_base = 9600;
447 tmp.close_delay = 5*HZ;
448 tmp.closing_wait = 30*HZ;
449
450 if (copy_to_user(serial, &tmp, sizeof(*serial)))
451 return -EFAULT;
452 return 0;
453}
454
Alan Cox00a0d0d2011-02-14 16:27:06 +0000455static int opticon_ioctl(struct tty_struct *tty,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800456 unsigned int cmd, unsigned long arg)
457{
458 struct usb_serial_port *port = tty->driver_data;
459 struct opticon_private *priv = usb_get_serial_data(port->serial);
460
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700461 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800462
463 switch (cmd) {
464 case TIOCGSERIAL:
465 return get_serial_info(priv,
466 (struct serial_struct __user *)arg);
467 }
468
469 return -ENOIOCTLCMD;
470}
471
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800472static int opticon_startup(struct usb_serial *serial)
473{
474 struct opticon_private *priv;
475 struct usb_host_interface *intf;
476 int i;
477 int retval = -ENOMEM;
478 bool bulk_in_found = false;
479
480 /* create our private serial structure */
481 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
482 if (priv == NULL) {
483 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
484 return -ENOMEM;
485 }
486 spin_lock_init(&priv->lock);
487 priv->serial = serial;
488 priv->port = serial->port[0];
Martin Jansen309a0572011-02-24 14:50:16 +0100489 priv->outstanding_urbs = 0; /* Init the outstanding urbs */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800490
491 /* find our bulk endpoint */
492 intf = serial->interface->altsetting;
493 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
494 struct usb_endpoint_descriptor *endpoint;
495
496 endpoint = &intf->endpoint[i].desc;
497 if (!usb_endpoint_is_bulk_in(endpoint))
498 continue;
499
500 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
501 if (!priv->bulk_read_urb) {
Johan Hovold3157fad2012-11-18 13:23:24 +0100502 dev_err(&serial->dev->dev, "out of memory\n");
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800503 goto error;
504 }
505
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700506 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800507 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
508 if (!priv->bulk_in_buffer) {
Johan Hovold3157fad2012-11-18 13:23:24 +0100509 dev_err(&serial->dev->dev, "out of memory\n");
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800510 goto error;
511 }
512
513 priv->bulk_address = endpoint->bEndpointAddress;
514
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800515 bulk_in_found = true;
516 break;
517 }
518
519 if (!bulk_in_found) {
Johan Hovold3157fad2012-11-18 13:23:24 +0100520 dev_err(&serial->dev->dev,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800521 "Error - the proper endpoints were not found!\n");
522 goto error;
523 }
524
Johan Hovold0b8718a2012-11-18 13:23:22 +0100525 usb_fill_bulk_urb(priv->bulk_read_urb, serial->dev,
526 usb_rcvbulkpipe(serial->dev,
527 priv->bulk_address),
528 priv->bulk_in_buffer, priv->buffer_size,
529 opticon_read_bulk_callback, priv);
530
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800531 usb_set_serial_data(serial, priv);
532 return 0;
533
534error:
535 usb_free_urb(priv->bulk_read_urb);
536 kfree(priv->bulk_in_buffer);
537 kfree(priv);
538 return retval;
539}
540
Alan Sternf9c99bb2009-06-02 11:53:55 -0400541static void opticon_disconnect(struct usb_serial *serial)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800542{
543 struct opticon_private *priv = usb_get_serial_data(serial);
544
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800545 usb_kill_urb(priv->bulk_read_urb);
546 usb_free_urb(priv->bulk_read_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400547}
548
549static void opticon_release(struct usb_serial *serial)
550{
551 struct opticon_private *priv = usb_get_serial_data(serial);
552
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800553 kfree(priv->bulk_in_buffer);
554 kfree(priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800555}
556
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700557static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100558{
Oliver Neukumd1c07132009-01-14 18:34:06 +0100559 struct opticon_private *priv = usb_get_serial_data(serial);
560
561 usb_kill_urb(priv->bulk_read_urb);
562 return 0;
563}
564
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700565static int opticon_resume(struct usb_serial *serial)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100566{
Oliver Neukumd1c07132009-01-14 18:34:06 +0100567 struct opticon_private *priv = usb_get_serial_data(serial);
568 struct usb_serial_port *port = serial->port[0];
569 int result;
570
Alan Cox82fc5942009-10-06 16:06:46 +0100571 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100572 /* This is protected by the port mutex against close/open */
573 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100574 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
575 else
576 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100577 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100578 return result;
579}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800580
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800581static struct usb_serial_driver opticon_device = {
582 .driver = {
583 .owner = THIS_MODULE,
584 .name = "opticon",
585 },
586 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800587 .num_ports = 1,
588 .attach = opticon_startup,
589 .open = opticon_open,
590 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800591 .write = opticon_write,
592 .write_room = opticon_write_room,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400593 .disconnect = opticon_disconnect,
594 .release = opticon_release,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800595 .throttle = opticon_throttle,
596 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800597 .ioctl = opticon_ioctl,
598 .tiocmget = opticon_tiocmget,
Martin Jansen309a0572011-02-24 14:50:16 +0100599 .tiocmset = opticon_tiocmset,
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700600 .suspend = opticon_suspend,
601 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800602};
603
Alan Sternf667dda2012-02-23 14:57:18 -0500604static struct usb_serial_driver * const serial_drivers[] = {
605 &opticon_device, NULL
606};
607
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700608module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800609
Martin Jansen309a0572011-02-24 14:50:16 +0100610MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800611MODULE_LICENSE("GPL");