blob: 1f850065d159d31e312c8629520ba2805eca6865 [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
Rusty Russell90ab5ee2012-01-13 09:32:20 +103035static bool debug;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080036
Németh Márton7d40d7e2010-01-10 15:34:24 +010037static const struct usb_device_id id_table[] = {
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080038 { USB_DEVICE(0x065a, 0x0009) },
39 { },
40};
41MODULE_DEVICE_TABLE(usb, id_table);
42
43/* This structure holds all of the individual device information */
44struct opticon_private {
45 struct usb_device *udev;
46 struct usb_serial *serial;
47 struct usb_serial_port *port;
48 unsigned char *bulk_in_buffer;
49 struct urb *bulk_read_urb;
50 int buffer_size;
51 u8 bulk_address;
52 spinlock_t lock; /* protects the following flags */
53 bool throttled;
54 bool actually_throttled;
55 bool rts;
Martin Jansen309a0572011-02-24 14:50:16 +010056 bool cts;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080057 int outstanding_urbs;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080058};
59
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -080060
Martin Jansen309a0572011-02-24 14:50:16 +010061
62static void opticon_read_bulk_callback(struct urb *urb)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080063{
64 struct opticon_private *priv = urb->context;
65 unsigned char *data = urb->transfer_buffer;
66 struct usb_serial_port *port = priv->port;
67 int status = urb->status;
68 struct tty_struct *tty;
69 int result;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080070 int data_length;
Martin Jansen309a0572011-02-24 14:50:16 +010071 unsigned long flags;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -080072
73 dbg("%s - port %d", __func__, port->number);
74
75 switch (status) {
76 case 0:
77 /* success */
78 break;
79 case -ECONNRESET:
80 case -ENOENT:
81 case -ESHUTDOWN:
82 /* this urb is terminated, clean up */
83 dbg("%s - urb shutting down with status: %d",
84 __func__, status);
85 return;
86 default:
87 dbg("%s - nonzero urb status received: %d",
88 __func__, status);
89 goto exit;
90 }
91
92 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
93 data);
94
95 if (urb->actual_length > 2) {
96 data_length = urb->actual_length - 2;
97
98 /*
99 * Data from the device comes with a 2 byte header:
100 *
101 * <0x00><0x00>data...
Martin Jansen309a0572011-02-24 14:50:16 +0100102 * This is real data to be sent to the tty layer
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800103 * <0x00><0x01)level
Martin Jansen309a0572011-02-24 14:50:16 +0100104 * This is a CTS level change, the third byte is the CTS
105 * value (0 for low, 1 for high).
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800106 */
107 if ((data[0] == 0x00) && (data[1] == 0x00)) {
108 /* real data, send it to the tty layer */
109 tty = tty_port_tty_get(&port->port);
110 if (tty) {
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200111 tty_insert_flip_string(tty, data + 2,
112 data_length);
Alan Coxa108bfc2010-02-18 16:44:01 +0000113 tty_flip_buffer_push(tty);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800114 tty_kref_put(tty);
115 }
116 } else {
117 if ((data[0] == 0x00) && (data[1] == 0x01)) {
Martin Jansen309a0572011-02-24 14:50:16 +0100118 spin_lock_irqsave(&priv->lock, flags);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300119 /* CTS status information package */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800120 if (data[2] == 0x00)
Martin Jansen309a0572011-02-24 14:50:16 +0100121 priv->cts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800122 else
Martin Jansen309a0572011-02-24 14:50:16 +0100123 priv->cts = true;
124 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800125 } else {
Alon Zivc6f694a2010-10-10 08:32:20 +0200126 dev_dbg(&priv->udev->dev,
127 "Unknown data packet received from the device:"
128 " %2x %2x\n",
129 data[0], data[1]);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800130 }
131 }
132 } else {
133 dev_dbg(&priv->udev->dev,
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +0100134 "Improper amount of data received from the device, "
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800135 "%d bytes", urb->actual_length);
136 }
137
138exit:
139 spin_lock(&priv->lock);
140
141 /* Continue trying to always read if we should */
142 if (!priv->throttled) {
143 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
144 usb_rcvbulkpipe(priv->udev,
145 priv->bulk_address),
146 priv->bulk_in_buffer, priv->buffer_size,
Martin Jansen309a0572011-02-24 14:50:16 +0100147 opticon_read_bulk_callback, priv);
Alon Ziv97cd8dc2010-10-10 08:32:18 +0200148 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800149 if (result)
150 dev_err(&port->dev,
151 "%s - failed resubmitting read urb, error %d\n",
152 __func__, result);
153 } else
154 priv->actually_throttled = true;
155 spin_unlock(&priv->lock);
156}
157
Martin Jansen309a0572011-02-24 14:50:16 +0100158static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
159 u8 val)
160{
161 struct usb_serial *serial = port->serial;
162 int retval;
Johan Hovoldce119582012-10-25 10:29:11 +0200163 u8 *buffer;
164
165 buffer = kzalloc(1, GFP_KERNEL);
166 if (!buffer)
167 return -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100168
169 buffer[0] = val;
170 /* Send the message to the vendor control endpoint
171 * of the connected device */
172 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
173 requesttype,
174 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
175 0, 0, buffer, 1, 0);
Johan Hovoldce119582012-10-25 10:29:11 +0200176 kfree(buffer);
Martin Jansen309a0572011-02-24 14:50:16 +0100177
178 return retval;
179}
180
Alan Coxa509a7e2009-09-19 13:13:26 -0700181static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800182{
183 struct opticon_private *priv = usb_get_serial_data(port->serial);
184 unsigned long flags;
185 int result = 0;
186
187 dbg("%s - port %d", __func__, port->number);
188
189 spin_lock_irqsave(&priv->lock, flags);
190 priv->throttled = false;
191 priv->actually_throttled = false;
192 priv->port = port;
Martin Jansen309a0572011-02-24 14:50:16 +0100193 priv->rts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800194 spin_unlock_irqrestore(&priv->lock, flags);
195
Martin Jansen309a0572011-02-24 14:50:16 +0100196 /* Clear RTS line */
197 send_control_msg(port, CONTROL_RTS, 0);
198
199 /* Setup the read URB and start reading from the device */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800200 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
201 usb_rcvbulkpipe(priv->udev,
202 priv->bulk_address),
203 priv->bulk_in_buffer, priv->buffer_size,
Martin Jansen309a0572011-02-24 14:50:16 +0100204 opticon_read_bulk_callback, priv);
205
206 /* clear the halt status of the enpoint */
207 usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
208
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800209 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
210 if (result)
211 dev_err(&port->dev,
212 "%s - failed resubmitting read urb, error %d\n",
213 __func__, result);
Martin Jansen309a0572011-02-24 14:50:16 +0100214 /* Request CTS line state, sometimes during opening the current
215 * CTS state can be missed. */
216 send_control_msg(port, RESEND_CTS_STATE, 1);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800217 return result;
218}
219
Alan Cox335f8512009-06-11 12:26:29 +0100220static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800221{
222 struct opticon_private *priv = usb_get_serial_data(port->serial);
223
224 dbg("%s - port %d", __func__, port->number);
225
226 /* shutdown our urbs */
227 usb_kill_urb(priv->bulk_read_urb);
228}
229
Martin Jansen309a0572011-02-24 14:50:16 +0100230static void opticon_write_control_callback(struct urb *urb)
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800231{
232 struct opticon_private *priv = urb->context;
233 int status = urb->status;
234 unsigned long flags;
235
236 /* free up the transfer buffer, as usb_free_urb() does not do this */
237 kfree(urb->transfer_buffer);
238
Alon Ziv0d930e52010-10-10 08:32:19 +0200239 /* setup packet may be set if we're using it for writing */
240 kfree(urb->setup_packet);
241
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800242 if (status)
243 dbg("%s - nonzero write bulk status received: %d",
244 __func__, status);
245
246 spin_lock_irqsave(&priv->lock, flags);
247 --priv->outstanding_urbs;
248 spin_unlock_irqrestore(&priv->lock, flags);
249
250 usb_serial_port_softint(priv->port);
251}
252
253static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
254 const unsigned char *buf, int count)
255{
256 struct opticon_private *priv = usb_get_serial_data(port->serial);
257 struct usb_serial *serial = port->serial;
258 struct urb *urb;
259 unsigned char *buffer;
260 unsigned long flags;
261 int status;
Martin Jansen309a0572011-02-24 14:50:16 +0100262 struct usb_ctrlrequest *dr;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800263
264 dbg("%s - port %d", __func__, port->number);
265
266 spin_lock_irqsave(&priv->lock, flags);
267 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
268 spin_unlock_irqrestore(&priv->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800269 dbg("%s - write limit hit", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800270 return 0;
271 }
272 priv->outstanding_urbs++;
273 spin_unlock_irqrestore(&priv->lock, flags);
274
275 buffer = kmalloc(count, GFP_ATOMIC);
276 if (!buffer) {
277 dev_err(&port->dev, "out of memory\n");
278 count = -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100279
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800280 goto error_no_buffer;
281 }
282
283 urb = usb_alloc_urb(0, GFP_ATOMIC);
284 if (!urb) {
285 dev_err(&port->dev, "no more free urbs\n");
286 count = -ENOMEM;
287 goto error_no_urb;
288 }
289
290 memcpy(buffer, buf, count);
291
292 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
293
Martin Jansen309a0572011-02-24 14:50:16 +0100294 /* The conncected devices do not have a bulk write endpoint,
295 * to transmit data to de barcode device the control endpoint is used */
296 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Julia Lawallb0795bb2011-05-13 17:30:46 +0200297 if (!dr) {
298 dev_err(&port->dev, "out of memory\n");
299 count = -ENOMEM;
Johan Hovolded7d2722012-10-25 10:29:12 +0200300 goto error_no_dr;
Julia Lawallb0795bb2011-05-13 17:30:46 +0200301 }
Alon Ziv0d930e52010-10-10 08:32:19 +0200302
Martin Jansen309a0572011-02-24 14:50:16 +0100303 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
304 dr->bRequest = 0x01;
305 dr->wValue = 0;
306 dr->wIndex = 0;
307 dr->wLength = cpu_to_le16(count);
Alon Ziv0d930e52010-10-10 08:32:19 +0200308
Martin Jansen309a0572011-02-24 14:50:16 +0100309 usb_fill_control_urb(urb, serial->dev,
310 usb_sndctrlpipe(serial->dev, 0),
311 (unsigned char *)dr, buffer, count,
312 opticon_write_control_callback, priv);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800313
314 /* send it down the pipe */
315 status = usb_submit_urb(urb, GFP_ATOMIC);
316 if (status) {
317 dev_err(&port->dev,
Martin Jansen309a0572011-02-24 14:50:16 +0100318 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800319 __func__, status);
320 count = status;
321 goto error;
322 }
323
324 /* we are done with this urb, so let the host driver
325 * really free it when it is finished with it */
326 usb_free_urb(urb);
327
328 return count;
329error:
Johan Hovolded7d2722012-10-25 10:29:12 +0200330 kfree(dr);
331error_no_dr:
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800332 usb_free_urb(urb);
333error_no_urb:
334 kfree(buffer);
335error_no_buffer:
336 spin_lock_irqsave(&priv->lock, flags);
337 --priv->outstanding_urbs;
338 spin_unlock_irqrestore(&priv->lock, flags);
339 return count;
340}
341
342static int opticon_write_room(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
348 dbg("%s - port %d", __func__, port->number);
349
350 /*
351 * We really can take almost anything the user throws at us
352 * but let's pick a nice big number to tell the tty
353 * layer that we have lots of free space, unless we don't.
354 */
355 spin_lock_irqsave(&priv->lock, flags);
356 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
357 spin_unlock_irqrestore(&priv->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800358 dbg("%s - write limit hit", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800359 return 0;
360 }
361 spin_unlock_irqrestore(&priv->lock, flags);
362
363 return 2048;
364}
365
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800366static void opticon_throttle(struct tty_struct *tty)
367{
368 struct usb_serial_port *port = tty->driver_data;
369 struct opticon_private *priv = usb_get_serial_data(port->serial);
370 unsigned long flags;
371
372 dbg("%s - port %d", __func__, port->number);
373 spin_lock_irqsave(&priv->lock, flags);
374 priv->throttled = true;
375 spin_unlock_irqrestore(&priv->lock, flags);
376}
377
378
379static void opticon_unthrottle(struct tty_struct *tty)
380{
381 struct usb_serial_port *port = tty->driver_data;
382 struct opticon_private *priv = usb_get_serial_data(port->serial);
383 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200384 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800385
386 dbg("%s - port %d", __func__, port->number);
387
388 spin_lock_irqsave(&priv->lock, flags);
389 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200390 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800391 priv->actually_throttled = false;
392 spin_unlock_irqrestore(&priv->lock, flags);
393
Oliver Neukum88fa6592009-10-07 09:25:10 +0200394 if (was_throttled) {
395 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
396 if (result)
397 dev_err(&port->dev,
398 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800399 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200400 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800401}
402
Alan Cox60b33c12011-02-14 16:26:14 +0000403static int opticon_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800404{
405 struct usb_serial_port *port = tty->driver_data;
406 struct opticon_private *priv = usb_get_serial_data(port->serial);
407 unsigned long flags;
408 int result = 0;
409
410 dbg("%s - port %d", __func__, port->number);
Martin Jansen309a0572011-02-24 14:50:16 +0100411 if (!usb_get_intfdata(port->serial->interface))
412 return -ENODEV;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800413
414 spin_lock_irqsave(&priv->lock, flags);
415 if (priv->rts)
Martin Jansen309a0572011-02-24 14:50:16 +0100416 result |= TIOCM_RTS;
417 if (priv->cts)
418 result |= TIOCM_CTS;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800419 spin_unlock_irqrestore(&priv->lock, flags);
420
421 dbg("%s - %x", __func__, result);
422 return result;
423}
424
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700425static int opticon_tiocmset(struct tty_struct *tty,
Martin Jansen309a0572011-02-24 14:50:16 +0100426 unsigned int set, unsigned int clear)
427{
428 struct usb_serial_port *port = tty->driver_data;
429 struct opticon_private *priv = usb_get_serial_data(port->serial);
430 unsigned long flags;
431 bool rts;
432 bool changed = false;
433
434 if (!usb_get_intfdata(port->serial->interface))
435 return -ENODEV;
436 /* We only support RTS so we only handle that */
437 spin_lock_irqsave(&priv->lock, flags);
438
439 rts = priv->rts;
440 if (set & TIOCM_RTS)
441 priv->rts = true;
442 if (clear & TIOCM_RTS)
443 priv->rts = false;
444 changed = rts ^ priv->rts;
445 spin_unlock_irqrestore(&priv->lock, flags);
446
447 if (!changed)
448 return 0;
449
450 /* Send the new RTS state to the connected device */
451 return send_control_msg(port, CONTROL_RTS, !rts);
452}
453
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800454static int get_serial_info(struct opticon_private *priv,
455 struct serial_struct __user *serial)
456{
457 struct serial_struct tmp;
458
459 if (!serial)
460 return -EFAULT;
461
462 memset(&tmp, 0x00, sizeof(tmp));
463
464 /* fake emulate a 16550 uart to make userspace code happy */
465 tmp.type = PORT_16550A;
466 tmp.line = priv->serial->minor;
467 tmp.port = 0;
468 tmp.irq = 0;
469 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
470 tmp.xmit_fifo_size = 1024;
471 tmp.baud_base = 9600;
472 tmp.close_delay = 5*HZ;
473 tmp.closing_wait = 30*HZ;
474
475 if (copy_to_user(serial, &tmp, sizeof(*serial)))
476 return -EFAULT;
477 return 0;
478}
479
Alan Cox00a0d0d2011-02-14 16:27:06 +0000480static int opticon_ioctl(struct tty_struct *tty,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800481 unsigned int cmd, unsigned long arg)
482{
483 struct usb_serial_port *port = tty->driver_data;
484 struct opticon_private *priv = usb_get_serial_data(port->serial);
485
486 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
487
488 switch (cmd) {
489 case TIOCGSERIAL:
490 return get_serial_info(priv,
491 (struct serial_struct __user *)arg);
492 }
493
494 return -ENOIOCTLCMD;
495}
496
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800497static int opticon_startup(struct usb_serial *serial)
498{
499 struct opticon_private *priv;
500 struct usb_host_interface *intf;
501 int i;
502 int retval = -ENOMEM;
503 bool bulk_in_found = false;
504
505 /* create our private serial structure */
506 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
507 if (priv == NULL) {
508 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
509 return -ENOMEM;
510 }
511 spin_lock_init(&priv->lock);
512 priv->serial = serial;
513 priv->port = serial->port[0];
514 priv->udev = serial->dev;
Martin Jansen309a0572011-02-24 14:50:16 +0100515 priv->outstanding_urbs = 0; /* Init the outstanding urbs */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800516
517 /* find our bulk endpoint */
518 intf = serial->interface->altsetting;
519 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
520 struct usb_endpoint_descriptor *endpoint;
521
522 endpoint = &intf->endpoint[i].desc;
523 if (!usb_endpoint_is_bulk_in(endpoint))
524 continue;
525
526 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
527 if (!priv->bulk_read_urb) {
528 dev_err(&priv->udev->dev, "out of memory\n");
529 goto error;
530 }
531
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700532 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800533 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
534 if (!priv->bulk_in_buffer) {
535 dev_err(&priv->udev->dev, "out of memory\n");
536 goto error;
537 }
538
539 priv->bulk_address = endpoint->bEndpointAddress;
540
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800541 bulk_in_found = true;
542 break;
543 }
544
545 if (!bulk_in_found) {
546 dev_err(&priv->udev->dev,
547 "Error - the proper endpoints were not found!\n");
548 goto error;
549 }
550
551 usb_set_serial_data(serial, priv);
552 return 0;
553
554error:
555 usb_free_urb(priv->bulk_read_urb);
556 kfree(priv->bulk_in_buffer);
557 kfree(priv);
558 return retval;
559}
560
Alan Sternf9c99bb2009-06-02 11:53:55 -0400561static void opticon_disconnect(struct usb_serial *serial)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800562{
563 struct opticon_private *priv = usb_get_serial_data(serial);
564
565 dbg("%s", __func__);
566
567 usb_kill_urb(priv->bulk_read_urb);
568 usb_free_urb(priv->bulk_read_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400569}
570
571static void opticon_release(struct usb_serial *serial)
572{
573 struct opticon_private *priv = usb_get_serial_data(serial);
574
575 dbg("%s", __func__);
576
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800577 kfree(priv->bulk_in_buffer);
578 kfree(priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800579}
580
Oliver Neukumd1c07132009-01-14 18:34:06 +0100581static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
582{
583 struct usb_serial *serial = usb_get_intfdata(intf);
584 struct opticon_private *priv = usb_get_serial_data(serial);
585
586 usb_kill_urb(priv->bulk_read_urb);
587 return 0;
588}
589
590static int opticon_resume(struct usb_interface *intf)
591{
592 struct usb_serial *serial = usb_get_intfdata(intf);
593 struct opticon_private *priv = usb_get_serial_data(serial);
594 struct usb_serial_port *port = serial->port[0];
595 int result;
596
Alan Cox82fc5942009-10-06 16:06:46 +0100597 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100598 /* This is protected by the port mutex against close/open */
599 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100600 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
601 else
602 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100603 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100604 return result;
605}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800606
607static struct usb_driver opticon_driver = {
608 .name = "opticon",
609 .probe = usb_serial_probe,
610 .disconnect = usb_serial_disconnect,
Oliver Neukumd1c07132009-01-14 18:34:06 +0100611 .suspend = opticon_suspend,
612 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800613 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800614};
615
616static struct usb_serial_driver opticon_device = {
617 .driver = {
618 .owner = THIS_MODULE,
619 .name = "opticon",
620 },
621 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800622 .num_ports = 1,
623 .attach = opticon_startup,
624 .open = opticon_open,
625 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800626 .write = opticon_write,
627 .write_room = opticon_write_room,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400628 .disconnect = opticon_disconnect,
629 .release = opticon_release,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800630 .throttle = opticon_throttle,
631 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800632 .ioctl = opticon_ioctl,
633 .tiocmget = opticon_tiocmget,
Martin Jansen309a0572011-02-24 14:50:16 +0100634 .tiocmset = opticon_tiocmset,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800635};
636
Alan Sternf667dda2012-02-23 14:57:18 -0500637static struct usb_serial_driver * const serial_drivers[] = {
638 &opticon_device, NULL
639};
640
Greg Kroah-Hartmane206b7f2012-02-28 13:12:19 -0800641module_usb_serial_driver(opticon_driver, serial_drivers);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800642
Martin Jansen309a0572011-02-24 14:50:16 +0100643MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800644MODULE_LICENSE("GPL");
645
646module_param(debug, bool, S_IRUGO | S_IWUSR);
647MODULE_PARM_DESC(debug, "Debug enabled or not");