blob: 7001ad779b9440fc72f7cc650c1b11f447369623 [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;
163 u8 buffer[2];
164
165 buffer[0] = val;
166 /* Send the message to the vendor control endpoint
167 * of the connected device */
168 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
169 requesttype,
170 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
171 0, 0, buffer, 1, 0);
172
173 return retval;
174}
175
Alan Coxa509a7e2009-09-19 13:13:26 -0700176static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800177{
178 struct opticon_private *priv = usb_get_serial_data(port->serial);
179 unsigned long flags;
180 int result = 0;
181
182 dbg("%s - port %d", __func__, port->number);
183
184 spin_lock_irqsave(&priv->lock, flags);
185 priv->throttled = false;
186 priv->actually_throttled = false;
187 priv->port = port;
Martin Jansen309a0572011-02-24 14:50:16 +0100188 priv->rts = false;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800189 spin_unlock_irqrestore(&priv->lock, flags);
190
Martin Jansen309a0572011-02-24 14:50:16 +0100191 /* Clear RTS line */
192 send_control_msg(port, CONTROL_RTS, 0);
193
194 /* Setup the read URB and start reading from the device */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800195 usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
196 usb_rcvbulkpipe(priv->udev,
197 priv->bulk_address),
198 priv->bulk_in_buffer, priv->buffer_size,
Martin Jansen309a0572011-02-24 14:50:16 +0100199 opticon_read_bulk_callback, priv);
200
201 /* clear the halt status of the enpoint */
202 usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
203
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800204 result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
205 if (result)
206 dev_err(&port->dev,
207 "%s - failed resubmitting read urb, error %d\n",
208 __func__, result);
Martin Jansen309a0572011-02-24 14:50:16 +0100209 /* Request CTS line state, sometimes during opening the current
210 * CTS state can be missed. */
211 send_control_msg(port, RESEND_CTS_STATE, 1);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800212 return result;
213}
214
Alan Cox335f8512009-06-11 12:26:29 +0100215static void opticon_close(struct usb_serial_port *port)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800216{
217 struct opticon_private *priv = usb_get_serial_data(port->serial);
218
219 dbg("%s - port %d", __func__, port->number);
220
221 /* shutdown our urbs */
222 usb_kill_urb(priv->bulk_read_urb);
223}
224
Martin Jansen309a0572011-02-24 14:50:16 +0100225static void opticon_write_control_callback(struct urb *urb)
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800226{
227 struct opticon_private *priv = urb->context;
228 int status = urb->status;
229 unsigned long flags;
230
231 /* free up the transfer buffer, as usb_free_urb() does not do this */
232 kfree(urb->transfer_buffer);
233
Alon Ziv0d930e52010-10-10 08:32:19 +0200234 /* setup packet may be set if we're using it for writing */
235 kfree(urb->setup_packet);
236
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800237 if (status)
238 dbg("%s - nonzero write bulk status received: %d",
239 __func__, status);
240
241 spin_lock_irqsave(&priv->lock, flags);
242 --priv->outstanding_urbs;
243 spin_unlock_irqrestore(&priv->lock, flags);
244
245 usb_serial_port_softint(priv->port);
246}
247
248static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
249 const unsigned char *buf, int count)
250{
251 struct opticon_private *priv = usb_get_serial_data(port->serial);
252 struct usb_serial *serial = port->serial;
253 struct urb *urb;
254 unsigned char *buffer;
255 unsigned long flags;
256 int status;
Martin Jansen309a0572011-02-24 14:50:16 +0100257 struct usb_ctrlrequest *dr;
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800258
259 dbg("%s - port %d", __func__, port->number);
260
261 spin_lock_irqsave(&priv->lock, flags);
262 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
263 spin_unlock_irqrestore(&priv->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800264 dbg("%s - write limit hit", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800265 return 0;
266 }
267 priv->outstanding_urbs++;
268 spin_unlock_irqrestore(&priv->lock, flags);
269
270 buffer = kmalloc(count, GFP_ATOMIC);
271 if (!buffer) {
272 dev_err(&port->dev, "out of memory\n");
273 count = -ENOMEM;
Martin Jansen309a0572011-02-24 14:50:16 +0100274
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800275 goto error_no_buffer;
276 }
277
278 urb = usb_alloc_urb(0, GFP_ATOMIC);
279 if (!urb) {
280 dev_err(&port->dev, "no more free urbs\n");
281 count = -ENOMEM;
282 goto error_no_urb;
283 }
284
285 memcpy(buffer, buf, count);
286
287 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
288
Martin Jansen309a0572011-02-24 14:50:16 +0100289 /* The conncected devices do not have a bulk write endpoint,
290 * to transmit data to de barcode device the control endpoint is used */
291 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Julia Lawallb0795bb2011-05-13 17:30:46 +0200292 if (!dr) {
293 dev_err(&port->dev, "out of memory\n");
294 count = -ENOMEM;
295 goto error;
296 }
Alon Ziv0d930e52010-10-10 08:32:19 +0200297
Martin Jansen309a0572011-02-24 14:50:16 +0100298 dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
299 dr->bRequest = 0x01;
300 dr->wValue = 0;
301 dr->wIndex = 0;
302 dr->wLength = cpu_to_le16(count);
Alon Ziv0d930e52010-10-10 08:32:19 +0200303
Martin Jansen309a0572011-02-24 14:50:16 +0100304 usb_fill_control_urb(urb, serial->dev,
305 usb_sndctrlpipe(serial->dev, 0),
306 (unsigned char *)dr, buffer, count,
307 opticon_write_control_callback, priv);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800308
309 /* send it down the pipe */
310 status = usb_submit_urb(urb, GFP_ATOMIC);
311 if (status) {
312 dev_err(&port->dev,
Martin Jansen309a0572011-02-24 14:50:16 +0100313 "%s - usb_submit_urb(write endpoint) failed status = %d\n",
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800314 __func__, status);
315 count = status;
316 goto error;
317 }
318
319 /* we are done with this urb, so let the host driver
320 * really free it when it is finished with it */
321 usb_free_urb(urb);
322
323 return count;
324error:
325 usb_free_urb(urb);
326error_no_urb:
327 kfree(buffer);
328error_no_buffer:
329 spin_lock_irqsave(&priv->lock, flags);
330 --priv->outstanding_urbs;
331 spin_unlock_irqrestore(&priv->lock, flags);
332 return count;
333}
334
335static int opticon_write_room(struct tty_struct *tty)
336{
337 struct usb_serial_port *port = tty->driver_data;
338 struct opticon_private *priv = usb_get_serial_data(port->serial);
339 unsigned long flags;
340
341 dbg("%s - port %d", __func__, port->number);
342
343 /*
344 * We really can take almost anything the user throws at us
345 * but let's pick a nice big number to tell the tty
346 * layer that we have lots of free space, unless we don't.
347 */
348 spin_lock_irqsave(&priv->lock, flags);
349 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
350 spin_unlock_irqrestore(&priv->lock, flags);
Joe Perches759f3632010-02-05 16:50:08 -0800351 dbg("%s - write limit hit", __func__);
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800352 return 0;
353 }
354 spin_unlock_irqrestore(&priv->lock, flags);
355
356 return 2048;
357}
358
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800359static void opticon_throttle(struct tty_struct *tty)
360{
361 struct usb_serial_port *port = tty->driver_data;
362 struct opticon_private *priv = usb_get_serial_data(port->serial);
363 unsigned long flags;
364
365 dbg("%s - port %d", __func__, port->number);
366 spin_lock_irqsave(&priv->lock, flags);
367 priv->throttled = true;
368 spin_unlock_irqrestore(&priv->lock, flags);
369}
370
371
372static void opticon_unthrottle(struct tty_struct *tty)
373{
374 struct usb_serial_port *port = tty->driver_data;
375 struct opticon_private *priv = usb_get_serial_data(port->serial);
376 unsigned long flags;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200377 int result, was_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800378
379 dbg("%s - port %d", __func__, port->number);
380
381 spin_lock_irqsave(&priv->lock, flags);
382 priv->throttled = false;
Oliver Neukum88fa6592009-10-07 09:25:10 +0200383 was_throttled = priv->actually_throttled;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800384 priv->actually_throttled = false;
385 spin_unlock_irqrestore(&priv->lock, flags);
386
Oliver Neukum88fa6592009-10-07 09:25:10 +0200387 if (was_throttled) {
388 result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
389 if (result)
390 dev_err(&port->dev,
391 "%s - failed submitting read urb, error %d\n",
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800392 __func__, result);
Oliver Neukum88fa6592009-10-07 09:25:10 +0200393 }
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800394}
395
Alan Cox60b33c12011-02-14 16:26:14 +0000396static int opticon_tiocmget(struct tty_struct *tty)
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800397{
398 struct usb_serial_port *port = tty->driver_data;
399 struct opticon_private *priv = usb_get_serial_data(port->serial);
400 unsigned long flags;
401 int result = 0;
402
403 dbg("%s - port %d", __func__, port->number);
Martin Jansen309a0572011-02-24 14:50:16 +0100404 if (!usb_get_intfdata(port->serial->interface))
405 return -ENODEV;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800406
407 spin_lock_irqsave(&priv->lock, flags);
408 if (priv->rts)
Martin Jansen309a0572011-02-24 14:50:16 +0100409 result |= TIOCM_RTS;
410 if (priv->cts)
411 result |= TIOCM_CTS;
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800412 spin_unlock_irqrestore(&priv->lock, flags);
413
414 dbg("%s - %x", __func__, result);
415 return result;
416}
417
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700418static int opticon_tiocmset(struct tty_struct *tty,
Martin Jansen309a0572011-02-24 14:50:16 +0100419 unsigned int set, unsigned int clear)
420{
421 struct usb_serial_port *port = tty->driver_data;
422 struct opticon_private *priv = usb_get_serial_data(port->serial);
423 unsigned long flags;
424 bool rts;
425 bool changed = false;
426
427 if (!usb_get_intfdata(port->serial->interface))
428 return -ENODEV;
429 /* We only support RTS so we only handle that */
430 spin_lock_irqsave(&priv->lock, flags);
431
432 rts = priv->rts;
433 if (set & TIOCM_RTS)
434 priv->rts = true;
435 if (clear & TIOCM_RTS)
436 priv->rts = false;
437 changed = rts ^ priv->rts;
438 spin_unlock_irqrestore(&priv->lock, flags);
439
440 if (!changed)
441 return 0;
442
443 /* Send the new RTS state to the connected device */
444 return send_control_msg(port, CONTROL_RTS, !rts);
445}
446
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800447static int get_serial_info(struct opticon_private *priv,
448 struct serial_struct __user *serial)
449{
450 struct serial_struct tmp;
451
452 if (!serial)
453 return -EFAULT;
454
455 memset(&tmp, 0x00, sizeof(tmp));
456
457 /* fake emulate a 16550 uart to make userspace code happy */
458 tmp.type = PORT_16550A;
459 tmp.line = priv->serial->minor;
460 tmp.port = 0;
461 tmp.irq = 0;
462 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
463 tmp.xmit_fifo_size = 1024;
464 tmp.baud_base = 9600;
465 tmp.close_delay = 5*HZ;
466 tmp.closing_wait = 30*HZ;
467
468 if (copy_to_user(serial, &tmp, sizeof(*serial)))
469 return -EFAULT;
470 return 0;
471}
472
Alan Cox00a0d0d2011-02-14 16:27:06 +0000473static int opticon_ioctl(struct tty_struct *tty,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800474 unsigned int cmd, unsigned long arg)
475{
476 struct usb_serial_port *port = tty->driver_data;
477 struct opticon_private *priv = usb_get_serial_data(port->serial);
478
479 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
480
481 switch (cmd) {
482 case TIOCGSERIAL:
483 return get_serial_info(priv,
484 (struct serial_struct __user *)arg);
485 }
486
487 return -ENOIOCTLCMD;
488}
489
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800490static int opticon_startup(struct usb_serial *serial)
491{
492 struct opticon_private *priv;
493 struct usb_host_interface *intf;
494 int i;
495 int retval = -ENOMEM;
496 bool bulk_in_found = false;
497
498 /* create our private serial structure */
499 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
500 if (priv == NULL) {
501 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
502 return -ENOMEM;
503 }
504 spin_lock_init(&priv->lock);
505 priv->serial = serial;
506 priv->port = serial->port[0];
507 priv->udev = serial->dev;
Martin Jansen309a0572011-02-24 14:50:16 +0100508 priv->outstanding_urbs = 0; /* Init the outstanding urbs */
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800509
510 /* find our bulk endpoint */
511 intf = serial->interface->altsetting;
512 for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
513 struct usb_endpoint_descriptor *endpoint;
514
515 endpoint = &intf->endpoint[i].desc;
516 if (!usb_endpoint_is_bulk_in(endpoint))
517 continue;
518
519 priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
520 if (!priv->bulk_read_urb) {
521 dev_err(&priv->udev->dev, "out of memory\n");
522 goto error;
523 }
524
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700525 priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800526 priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
527 if (!priv->bulk_in_buffer) {
528 dev_err(&priv->udev->dev, "out of memory\n");
529 goto error;
530 }
531
532 priv->bulk_address = endpoint->bEndpointAddress;
533
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800534 bulk_in_found = true;
535 break;
536 }
537
538 if (!bulk_in_found) {
539 dev_err(&priv->udev->dev,
540 "Error - the proper endpoints were not found!\n");
541 goto error;
542 }
543
544 usb_set_serial_data(serial, priv);
545 return 0;
546
547error:
548 usb_free_urb(priv->bulk_read_urb);
549 kfree(priv->bulk_in_buffer);
550 kfree(priv);
551 return retval;
552}
553
Alan Sternf9c99bb2009-06-02 11:53:55 -0400554static void opticon_disconnect(struct usb_serial *serial)
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800555{
556 struct opticon_private *priv = usb_get_serial_data(serial);
557
558 dbg("%s", __func__);
559
560 usb_kill_urb(priv->bulk_read_urb);
561 usb_free_urb(priv->bulk_read_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400562}
563
564static void opticon_release(struct usb_serial *serial)
565{
566 struct opticon_private *priv = usb_get_serial_data(serial);
567
568 dbg("%s", __func__);
569
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800570 kfree(priv->bulk_in_buffer);
571 kfree(priv);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800572}
573
Oliver Neukumd1c07132009-01-14 18:34:06 +0100574static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
575{
576 struct usb_serial *serial = usb_get_intfdata(intf);
577 struct opticon_private *priv = usb_get_serial_data(serial);
578
579 usb_kill_urb(priv->bulk_read_urb);
580 return 0;
581}
582
583static int opticon_resume(struct usb_interface *intf)
584{
585 struct usb_serial *serial = usb_get_intfdata(intf);
586 struct opticon_private *priv = usb_get_serial_data(serial);
587 struct usb_serial_port *port = serial->port[0];
588 int result;
589
Alan Cox82fc5942009-10-06 16:06:46 +0100590 mutex_lock(&port->port.mutex);
Alan Cox2a0785e2009-10-06 16:06:57 +0100591 /* This is protected by the port mutex against close/open */
592 if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
Oliver Neukumd1c07132009-01-14 18:34:06 +0100593 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
594 else
595 result = 0;
Alan Cox82fc5942009-10-06 16:06:46 +0100596 mutex_unlock(&port->port.mutex);
Oliver Neukumd1c07132009-01-14 18:34:06 +0100597 return result;
598}
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800599
600static struct usb_driver opticon_driver = {
601 .name = "opticon",
602 .probe = usb_serial_probe,
603 .disconnect = usb_serial_disconnect,
Oliver Neukumd1c07132009-01-14 18:34:06 +0100604 .suspend = opticon_suspend,
605 .resume = opticon_resume,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800606 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800607};
608
609static struct usb_serial_driver opticon_device = {
610 .driver = {
611 .owner = THIS_MODULE,
612 .name = "opticon",
613 },
614 .id_table = id_table,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800615 .num_ports = 1,
616 .attach = opticon_startup,
617 .open = opticon_open,
618 .close = opticon_close,
Greg Kroah-Hartman648d4e12009-02-06 18:30:56 -0800619 .write = opticon_write,
620 .write_room = opticon_write_room,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400621 .disconnect = opticon_disconnect,
622 .release = opticon_release,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800623 .throttle = opticon_throttle,
624 .unthrottle = opticon_unthrottle,
Greg Kroah-Hartmanfaac64a2009-02-06 18:31:46 -0800625 .ioctl = opticon_ioctl,
626 .tiocmget = opticon_tiocmget,
Martin Jansen309a0572011-02-24 14:50:16 +0100627 .tiocmset = opticon_tiocmset,
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800628};
629
Alan Sternf667dda2012-02-23 14:57:18 -0500630static struct usb_serial_driver * const serial_drivers[] = {
631 &opticon_device, NULL
632};
633
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800634static int __init opticon_init(void)
635{
Alan Sternf667dda2012-02-23 14:57:18 -0500636 return usb_serial_register_drivers(&opticon_driver, serial_drivers);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800637}
638
639static void __exit opticon_exit(void)
640{
Alan Sternf667dda2012-02-23 14:57:18 -0500641 usb_serial_deregister_drivers(&opticon_driver, serial_drivers);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800642}
643
644module_init(opticon_init);
645module_exit(opticon_exit);
Martin Jansen309a0572011-02-24 14:50:16 +0100646MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman57262b82008-11-03 13:27:03 -0800647MODULE_LICENSE("GPL");
648
649module_param(debug, bool, S_IRUGO | S_IWUSR);
650MODULE_PARM_DESC(debug, "Debug enabled or not");