blob: bcb8ad84a74a653fd20af92377aae24f53557e7d [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 {
43 struct usb_device *udev;
44 struct usb_serial *serial;
45 struct usb_serial_port *port;
46 unsigned char *bulk_in_buffer;
47 struct urb *bulk_read_urb;
48 int buffer_size;
49 u8 bulk_address;
50 spinlock_t lock; /* protects the following flags */
51 bool throttled;
52 bool actually_throttled;
53 bool rts;
Martin Jansen309a0572011-02-24 14:50:16 +010054 bool cts;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080055 int outstanding_urbs;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080056};
57
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080058
Martin Jansen309a0572011-02-24 14:50:16 +010059
60static void opticon_read_bulk_callback(struct urb *urb)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080061{
62 struct opticon_private *priv = urb->context;
63 unsigned char *data = urb->transfer_buffer;
64 struct usb_serial_port *port = priv->port;
65 int status = urb->status;
66 struct tty_struct *tty;
67 int result;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080068 int data_length;
Martin Jansen309a0572011-02-24 14:50:16 +010069 unsigned long flags;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080070
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080071 switch (status) {
72 case 0:
73 /* success */
74 break;
75 case -ECONNRESET:
76 case -ENOENT:
77 case -ESHUTDOWN:
78 /* this urb is terminated, clean up */
Johan Hovolde29a7732012-11-18 13:23:23 +010079 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -070080 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080081 return;
82 default:
Johan Hovolde29a7732012-11-18 13:23:23 +010083 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -070084 __func__, status);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080085 goto exit;
86 }
87
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +010088 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080089
90 if (urb->actual_length > 2) {
91 data_length = urb->actual_length - 2;
92
93 /*
94 * Data from the device comes with a 2 byte header:
95 *
96 * <0x00><0x00>data...
Martin Jansen309a0572011-02-24 14:50:16 +010097 * This is real data to be sent to the tty layer
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080098 * <0x00><0x01)level
Martin Jansen309a0572011-02-24 14:50:16 +010099 * This is a CTS level change, the third byte is the CTS
100 * value (0 for low, 1 for high).
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800101 */
102 if ((data[0] == 0x00) && (data[1] == 0x00)) {
103 /* real data, send it to the tty layer */
104 tty = tty_port_tty_get(&port->port);
105 if (tty) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200106 tty_insert_flip_string(tty, data + 2,
107 data_length);
Alan Coxa108bfc2010-02-18 16:44:01 +0000108 tty_flip_buffer_push(tty);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800109 tty_kref_put(tty);
110 }
111 } else {
112 if ((data[0] == 0x00) && (data[1] == 0x01)) {
Martin Jansen309a0572011-02-24 14:50:16 +0100113 spin_lock_irqsave(&priv->lock, flags);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300114 /* CTS status information package */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800115 if (data[2] == 0x00)
Martin Jansen309a0572011-02-24 14:50:16 +0100116 priv->cts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800117 else
Martin Jansen309a0572011-02-24 14:50:16 +0100118 priv->cts = true;
119 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800120 } else {
Johan Hovolde29a7732012-11-18 13:23:23 +0100121 dev_dbg(&port->dev,
Alon Zivc6f694a2010-10-10 08:32:20 +0200122 "Unknown data packet received from the device:"
123 " %2x %2x\n",
124 data[0], data[1]);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800125 }
126 }
127 } else {
Johan Hovolde29a7732012-11-18 13:23:23 +0100128 dev_dbg(&port->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100129 "Improper amount of data received from the device, "
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800130 "%d bytes", urb->actual_length);
131 }
132
133exit:
134 spin_lock(&priv->lock);
135
136 /* Continue trying to always read if we should */
137 if (!priv->throttled) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200138 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800139 if (result)
140 dev_err(&port->dev,
141 "%s - failed resubmitting read urb, error %d\n",
142 __func__, result);
143 } else
144 priv->actually_throttled = true;
145 spin_unlock(&priv->lock);
146}
147
Martin Jansen309a0572011-02-24 14:50:16 +0100148static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
149 u8 val)
150{
151 struct usb_serial *serial = port->serial;
152 int retval;
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200153 u8 *buffer;
154
155 buffer = kzalloc(1, GFP_KERNEL);
156 if (!buffer)
157 return -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100158
159 buffer[0] = val;
160 /* Send the message to the vendor control endpoint
161 * of the connected device */
162 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
163 requesttype,
164 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
165 0, 0, buffer, 1, 0);
Johan Hovoldea0dbeb2012-10-25 10:29:11 +0200166 kfree(buffer);
Martin Jansen309a0572011-02-24 14:50:16 +0100167
168 return retval;
169}
170
Alan Coxa509a7e2009-09-19 13:13:26 -0700171static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800172{
173 struct opticon_private *priv = usb_get_serial_data(port->serial);
174 unsigned long flags;
175 int result = 0;
176
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800177 spin_lock_irqsave(&priv->lock, flags);
178 priv->throttled = false;
179 priv->actually_throttled = false;
180 priv->port = port;
Martin Jansen309a0572011-02-24 14:50:16 +0100181 priv->rts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800182 spin_unlock_irqrestore(&priv->lock, flags);
183
Martin Jansen309a0572011-02-24 14:50:16 +0100184 /* Clear RTS line */
185 send_control_msg(port, CONTROL_RTS, 0);
186
Martin Jansen309a0572011-02-24 14:50:16 +0100187 /* clear the halt status of the enpoint */
188 usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
189
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800190 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
191 if (result)
192 dev_err(&port->dev,
193 "%s - failed resubmitting read urb, error %d\n",
194 __func__, result);
Martin Jansen309a0572011-02-24 14:50:16 +0100195 /* Request CTS line state, sometimes during opening the current
196 * CTS state can be missed. */
197 send_control_msg(port, RESEND_CTS_STATE, 1);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800198 return result;
199}
200
Alan Cox335f8512009-06-11 12:26:29 +0100201static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800202{
203 struct opticon_private *priv = usb_get_serial_data(port->serial);
204
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800205 /* shutdown our urbs */
206 usb_kill_urb(priv->bulk_read_urb);
207}
208
Martin Jansen309a0572011-02-24 14:50:16 +0100209static void opticon_write_control_callback(struct urb *urb)
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800210{
211 struct opticon_private *priv = urb->context;
212 int status = urb->status;
213 unsigned long flags;
214
215 /* free up the transfer buffer, as usb_free_urb() does not do this */
216 kfree(urb->transfer_buffer);
217
Alon Ziv0d930e52010-10-10 08:32:19 +0200218 /* setup packet may be set if we're using it for writing */
219 kfree(urb->setup_packet);
220
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800221 if (status)
Johan Hovolde29a7732012-11-18 13:23:23 +0100222 dev_dbg(&priv->port->dev,
223 "%s - non-zero urb status received: %d\n",
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700224 __func__, status);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800225
226 spin_lock_irqsave(&priv->lock, flags);
227 --priv->outstanding_urbs;
228 spin_unlock_irqrestore(&priv->lock, flags);
229
230 usb_serial_port_softint(priv->port);
231}
232
233static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
234 const unsigned char *buf, int count)
235{
236 struct opticon_private *priv = usb_get_serial_data(port->serial);
237 struct usb_serial *serial = port->serial;
238 struct urb *urb;
239 unsigned char *buffer;
240 unsigned long flags;
241 int status;
Martin Jansen309a0572011-02-24 14:50:16 +0100242 struct usb_ctrlrequest *dr;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800243
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800244 spin_lock_irqsave(&priv->lock, flags);
245 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
246 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700247 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800248 return 0;
249 }
250 priv->outstanding_urbs++;
251 spin_unlock_irqrestore(&priv->lock, flags);
252
253 buffer = kmalloc(count, GFP_ATOMIC);
254 if (!buffer) {
255 dev_err(&port->dev, "out of memory\n");
256 count = -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100257
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800258 goto error_no_buffer;
259 }
260
261 urb = usb_alloc_urb(0, GFP_ATOMIC);
262 if (!urb) {
263 dev_err(&port->dev, "no more free urbs\n");
264 count = -ENOMEM;
265 goto error_no_urb;
266 }
267
268 memcpy(buffer, buf, count);
269
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100270 usb_serial_debug_data(&port->dev, __func__, count, buffer);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800271
Martin Jansen309a0572011-02-24 14:50:16 +0100272 /* The conncected devices do not have a bulk write endpoint,
273 * to transmit data to de barcode device the control endpoint is used */
274 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Julia Lawallb0795bb2011-05-13 17:30:46 +0200275 if (!dr) {
276 dev_err(&port->dev, "out of memory\n");
277 count = -ENOMEM;
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200278 goto error_no_dr;
Julia Lawallb0795bb2011-05-13 17:30:46 +0200279 }
Alon Ziv0d930e52010-10-10 08:32:19 +0200280
Martin Jansen309a0572011-02-24 14:50:16 +0100281 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
282 dr->bRequest = 0x01;
283 dr->wValue = 0;
284 dr->wIndex = 0;
285 dr->wLength = cpu_to_le16(count);
Alon Ziv0d930e52010-10-10 08:32:19 +0200286
Martin Jansen309a0572011-02-24 14:50:16 +0100287 usb_fill_control_urb(urb, serial->dev,
288 usb_sndctrlpipe(serial->dev, 0),
289 (unsigned char *)dr, buffer, count,
290 opticon_write_control_callback, priv);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800291
292 /* send it down the pipe */
293 status = usb_submit_urb(urb, GFP_ATOMIC);
294 if (status) {
295 dev_err(&port->dev,
Martin Jansen309a0572011-02-24 14:50:16 +0100296 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800297 __func__, status);
298 count = status;
299 goto error;
300 }
301
302 /* we are done with this urb, so let the host driver
303 * really free it when it is finished with it */
304 usb_free_urb(urb);
305
306 return count;
307error:
Johan Hovoldacbf0e52012-10-25 10:29:12 +0200308 kfree(dr);
309error_no_dr:
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800310 usb_free_urb(urb);
311error_no_urb:
312 kfree(buffer);
313error_no_buffer:
314 spin_lock_irqsave(&priv->lock, flags);
315 --priv->outstanding_urbs;
316 spin_unlock_irqrestore(&priv->lock, flags);
317 return count;
318}
319
320static int opticon_write_room(struct tty_struct *tty)
321{
322 struct usb_serial_port *port = tty->driver_data;
323 struct opticon_private *priv = usb_get_serial_data(port->serial);
324 unsigned long flags;
325
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800326 /*
327 * We really can take almost anything the user throws at us
328 * but let's pick a nice big number to tell the tty
329 * layer that we have lots of free space, unless we don't.
330 */
331 spin_lock_irqsave(&priv->lock, flags);
332 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
333 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700334 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800335 return 0;
336 }
337 spin_unlock_irqrestore(&priv->lock, flags);
338
339 return 2048;
340}
341
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800342static void opticon_throttle(struct tty_struct *tty)
343{
344 struct usb_serial_port *port = tty->driver_data;
345 struct opticon_private *priv = usb_get_serial_data(port->serial);
346 unsigned long flags;
347
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800348 spin_lock_irqsave(&priv->lock, flags);
349 priv->throttled = true;
350 spin_unlock_irqrestore(&priv->lock, flags);
351}
352
353
354static void opticon_unthrottle(struct tty_struct *tty)
355{
356 struct usb_serial_port *port = tty->driver_data;
357 struct opticon_private *priv = usb_get_serial_data(port->serial);
358 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200359 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800360
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800361 spin_lock_irqsave(&priv->lock, flags);
362 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200363 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800364 priv->actually_throttled = false;
365 spin_unlock_irqrestore(&priv->lock, flags);
366
Oliver Neukum88fa6592009-10-07 09:25:10 +0200367 if (was_throttled) {
368 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
369 if (result)
370 dev_err(&port->dev,
371 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800372 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200373 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800374}
375
Alan Cox60b33c12011-02-14 16:26:14 +0000376static int opticon_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800377{
378 struct usb_serial_port *port = tty->driver_data;
379 struct opticon_private *priv = usb_get_serial_data(port->serial);
380 unsigned long flags;
381 int result = 0;
382
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800383 spin_lock_irqsave(&priv->lock, flags);
384 if (priv->rts)
Martin Jansen309a0572011-02-24 14:50:16 +0100385 result |= TIOCM_RTS;
386 if (priv->cts)
387 result |= TIOCM_CTS;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800388 spin_unlock_irqrestore(&priv->lock, flags);
389
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700390 dev_dbg(&port->dev, "%s - %x\n", __func__, result);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800391 return result;
392}
393
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700394static int opticon_tiocmset(struct tty_struct *tty,
Martin Jansen309a0572011-02-24 14:50:16 +0100395 unsigned int set, unsigned int clear)
396{
397 struct usb_serial_port *port = tty->driver_data;
Johan Hovold81d5a672012-04-25 15:56:29 +0200398 struct usb_serial *serial = port->serial;
Martin Jansen309a0572011-02-24 14:50:16 +0100399 struct opticon_private *priv = usb_get_serial_data(port->serial);
400 unsigned long flags;
401 bool rts;
402 bool changed = false;
Johan Hovold81d5a672012-04-25 15:56:29 +0200403 int ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100404
Martin Jansen309a0572011-02-24 14:50:16 +0100405 /* We only support RTS so we only handle that */
406 spin_lock_irqsave(&priv->lock, flags);
407
408 rts = priv->rts;
409 if (set & TIOCM_RTS)
410 priv->rts = true;
411 if (clear & TIOCM_RTS)
412 priv->rts = false;
413 changed = rts ^ priv->rts;
414 spin_unlock_irqrestore(&priv->lock, flags);
415
416 if (!changed)
417 return 0;
418
419 /* Send the new RTS state to the connected device */
Johan Hovold81d5a672012-04-25 15:56:29 +0200420 mutex_lock(&serial->disc_mutex);
421 if (!serial->disconnected)
422 ret = send_control_msg(port, CONTROL_RTS, !rts);
423 else
424 ret = -ENODEV;
425 mutex_unlock(&serial->disc_mutex);
426
427 return ret;
Martin Jansen309a0572011-02-24 14:50:16 +0100428}
429
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800430static int get_serial_info(struct opticon_private *priv,
431 struct serial_struct __user *serial)
432{
433 struct serial_struct tmp;
434
435 if (!serial)
436 return -EFAULT;
437
438 memset(&tmp, 0x00, sizeof(tmp));
439
440 /* fake emulate a 16550 uart to make userspace code happy */
441 tmp.type = PORT_16550A;
442 tmp.line = priv->serial->minor;
443 tmp.port = 0;
444 tmp.irq = 0;
445 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
446 tmp.xmit_fifo_size = 1024;
447 tmp.baud_base = 9600;
448 tmp.close_delay = 5*HZ;
449 tmp.closing_wait = 30*HZ;
450
451 if (copy_to_user(serial, &tmp, sizeof(*serial)))
452 return -EFAULT;
453 return 0;
454}
455
Alan Cox00a0d0d2011-02-14 16:27:06 +0000456static int opticon_ioctl(struct tty_struct *tty,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800457 unsigned int cmd, unsigned long arg)
458{
459 struct usb_serial_port *port = tty->driver_data;
460 struct opticon_private *priv = usb_get_serial_data(port->serial);
461
Greg Kroah-Hartmand44d9ab2012-09-13 17:18:19 -0700462 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800463
464 switch (cmd) {
465 case TIOCGSERIAL:
466 return get_serial_info(priv,
467 (struct serial_struct __user *)arg);
468 }
469
470 return -ENOIOCTLCMD;
471}
472
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800473static int opticon_startup(struct usb_serial *serial)
474{
475 struct opticon_private *priv;
476 struct usb_host_interface *intf;
477 int i;
478 int retval = -ENOMEM;
479 bool bulk_in_found = false;
480
481 /* create our private serial structure */
482 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
483 if (priv == NULL) {
484 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
485 return -ENOMEM;
486 }
487 spin_lock_init(&priv->lock);
488 priv->serial = serial;
489 priv->port = serial->port[0];
490 priv->udev = serial->dev;
Martin Jansen309a0572011-02-24 14:50:16 +0100491 priv->outstanding_urbs = 0; /* Init the outstanding urbs */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800492
493 /* find our bulk endpoint */
494 intf = serial->interface->altsetting;
495 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
496 struct usb_endpoint_descriptor *endpoint;
497
498 endpoint = &intf->endpoint[i].desc;
499 if (!usb_endpoint_is_bulk_in(endpoint))
500 continue;
501
502 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
503 if (!priv->bulk_read_urb) {
504 dev_err(&priv->udev->dev, "out of memory\n");
505 goto error;
506 }
507
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700508 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800509 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
510 if (!priv->bulk_in_buffer) {
511 dev_err(&priv->udev->dev, "out of memory\n");
512 goto error;
513 }
514
515 priv->bulk_address = endpoint->bEndpointAddress;
516
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800517 bulk_in_found = true;
518 break;
519 }
520
521 if (!bulk_in_found) {
522 dev_err(&priv->udev->dev,
523 "Error - the proper endpoints were not found!\n");
524 goto error;
525 }
526
Johan Hovold0b8718a2012-11-18 13:23:22 +0100527 usb_fill_bulk_urb(priv->bulk_read_urb, serial->dev,
528 usb_rcvbulkpipe(serial->dev,
529 priv->bulk_address),
530 priv->bulk_in_buffer, priv->buffer_size,
531 opticon_read_bulk_callback, priv);
532
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800533 usb_set_serial_data(serial, priv);
534 return 0;
535
536error:
537 usb_free_urb(priv->bulk_read_urb);
538 kfree(priv->bulk_in_buffer);
539 kfree(priv);
540 return retval;
541}
542
Alan Sternf9c99bb2009-06-02 11:53:55 -0400543static void opticon_disconnect(struct usb_serial *serial)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800544{
545 struct opticon_private *priv = usb_get_serial_data(serial);
546
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800547 usb_kill_urb(priv->bulk_read_urb);
548 usb_free_urb(priv->bulk_read_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400549}
550
551static void opticon_release(struct usb_serial *serial)
552{
553 struct opticon_private *priv = usb_get_serial_data(serial);
554
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800555 kfree(priv->bulk_in_buffer);
556 kfree(priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800557}
558
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700559static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100560{
Oliver Neukumd1c07132009-01-14 18:34:06 +0100561 struct opticon_private *priv = usb_get_serial_data(serial);
562
563 usb_kill_urb(priv->bulk_read_urb);
564 return 0;
565}
566
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700567static int opticon_resume(struct usb_serial *serial)
Oliver Neukumd1c07132009-01-14 18:34:06 +0100568{
Oliver Neukumd1c07132009-01-14 18:34:06 +0100569 struct opticon_private *priv = usb_get_serial_data(serial);
570 struct usb_serial_port *port = serial->port[0];
571 int result;
572
Alan Cox82fc5942009-10-06 16:06:46 +0100573 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100574 /* This is protected by the port mutex against close/open */
575 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100576 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
577 else
578 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100579 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100580 return result;
581}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800582
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800583static struct usb_serial_driver opticon_device = {
584 .driver = {
585 .owner = THIS_MODULE,
586 .name = "opticon",
587 },
588 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800589 .num_ports = 1,
590 .attach = opticon_startup,
591 .open = opticon_open,
592 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800593 .write = opticon_write,
594 .write_room = opticon_write_room,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400595 .disconnect = opticon_disconnect,
596 .release = opticon_release,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800597 .throttle = opticon_throttle,
598 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800599 .ioctl = opticon_ioctl,
600 .tiocmget = opticon_tiocmget,
Martin Jansen309a0572011-02-24 14:50:16 +0100601 .tiocmset = opticon_tiocmset,
Greg Kroah-Hartmand5302962012-05-10 14:35:21 -0700602 .suspend = opticon_suspend,
603 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800604};
605
Alan Sternf667dda2012-02-23 14:57:18 -0500606static struct usb_serial_driver * const serial_drivers[] = {
607 &opticon_device, NULL
608};
609
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700610module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800611
Martin Jansen309a0572011-02-24 14:50:16 +0100612MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800613MODULE_LICENSE("GPL");