blob: 2a7aecc72237ccaac68b2c922093182c878acbda [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
3 *
4 * Copyright (C) 2001 REINER SCT
5 * Author: Matthias Bruestle
6 *
7 * Contact: support@reiner-sct.com (see MAINTAINERS)
8 *
9 * This program is largely derived from work by the linux-usb group
10 * and associated source files. Please see the usb/serial files for
11 * individual credits and copyrights.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
19 * patience.
20 *
21 * In case of problems, please write to the contact e-mail address
22 * mentioned above.
23 *
24 * Please note that later models of the cyberjack reader family are
25 * supported by a libusb-based userspace device driver.
26 *
27 * Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
28 */
29
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
32#include <linux/errno.h>
33#include <linux/init.h>
34#include <linux/slab.h>
35#include <linux/tty.h>
36#include <linux/tty_driver.h>
37#include <linux/tty_flip.h>
38#include <linux/module.h>
39#include <linux/spinlock.h>
Alan Coxb9c52f12008-07-22 11:10:27 +010040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070042#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define CYBERJACK_LOCAL_BUF_SIZE 32
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * Version Information
48 */
49#define DRIVER_VERSION "v1.01"
50#define DRIVER_AUTHOR "Matthias Bruestle"
51#define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
52
53
54#define CYBERJACK_VENDOR_ID 0x0C4B
55#define CYBERJACK_PRODUCT_ID 0x0100
56
57/* Function prototypes */
Alan Cox95da3102008-07-22 11:09:07 +010058static int cyberjack_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040059static void cyberjack_disconnect(struct usb_serial *serial);
60static void cyberjack_release(struct usb_serial *serial);
Alan Cox95da3102008-07-22 11:09:07 +010061static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -070062 struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010063static void cyberjack_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010064static int cyberjack_write(struct tty_struct *tty,
65 struct usb_serial_port *port, const unsigned char *buf, int count);
Alan Coxb9c52f12008-07-22 11:10:27 +010066static int cyberjack_write_room(struct tty_struct *tty);
Alan Cox95da3102008-07-22 11:09:07 +010067static void cyberjack_read_int_callback(struct urb *urb);
68static void cyberjack_read_bulk_callback(struct urb *urb);
69static void cyberjack_write_bulk_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Németh Márton7d40d7e2010-01-10 15:34:24 +010071static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
73 { } /* Terminating entry */
74};
75
Alan Coxb9c52f12008-07-22 11:10:27 +010076MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070078static struct usb_serial_driver cyberjack_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070079 .driver = {
80 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070081 .name = "cyberjack",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070082 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070083 .description = "Reiner SCT Cyberjack USB card reader",
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .num_ports = 1,
86 .attach = cyberjack_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -040087 .disconnect = cyberjack_disconnect,
88 .release = cyberjack_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .open = cyberjack_open,
90 .close = cyberjack_close,
91 .write = cyberjack_write,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010092 .write_room = cyberjack_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .read_int_callback = cyberjack_read_int_callback,
94 .read_bulk_callback = cyberjack_read_bulk_callback,
95 .write_bulk_callback = cyberjack_write_bulk_callback,
96};
97
Alan Stern08a4f6b2012-02-23 14:56:17 -050098static struct usb_serial_driver * const serial_drivers[] = {
99 &cyberjack_device, NULL
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102struct cyberjack_private {
103 spinlock_t lock; /* Lock for SMP */
104 short rdtodo; /* Bytes still to read */
105 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
106 short wrfilled; /* Overall data size we already got */
107 short wrsent; /* Data already sent */
108};
109
110/* do some startup allocations not currently performed by usb_serial_probe() */
Alan Cox95da3102008-07-22 11:09:07 +0100111static int cyberjack_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct cyberjack_private *priv;
114 int i;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* allocate the private data structure */
117 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
118 if (!priv)
119 return -ENOMEM;
120
121 /* set initial values */
122 spin_lock_init(&priv->lock);
123 priv->rdtodo = 0;
124 priv->wrfilled = 0;
125 priv->wrsent = 0;
126 usb_set_serial_port_data(serial->port[0], priv);
127
128 init_waitqueue_head(&serial->port[0]->write_wait);
129
130 for (i = 0; i < serial->num_ports; ++i) {
131 int result;
Alan Coxb9c52f12008-07-22 11:10:27 +0100132 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 GFP_KERNEL);
134 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700135 dev_err(&serial->dev->dev,
136 "usb_submit_urb(read int) failed\n");
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700137 dev_dbg(&serial->dev->dev, "%s - usb_submit_urb(int urb)\n",
138 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140
Alan Coxb9c52f12008-07-22 11:10:27 +0100141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Alan Sternf9c99bb2009-06-02 11:53:55 -0400144static void cyberjack_disconnect(struct usb_serial *serial)
145{
146 int i;
147
Alan Sternf9c99bb2009-06-02 11:53:55 -0400148 for (i = 0; i < serial->num_ports; ++i)
149 usb_kill_urb(serial->port[i]->interrupt_in_urb);
150}
151
152static void cyberjack_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int i;
Alan Coxb9c52f12008-07-22 11:10:27 +0100155
Alan Coxa5b6f602008-04-08 17:16:06 +0100156 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* My special items, the standard routines free my urbs */
158 kfree(usb_get_serial_port_data(serial->port[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160}
Alan Coxb9c52f12008-07-22 11:10:27 +0100161
Alan Cox95da3102008-07-22 11:09:07 +0100162static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700163 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 struct cyberjack_private *priv;
166 unsigned long flags;
167 int result = 0;
168
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700169 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 priv = usb_get_serial_port_data(port);
173 spin_lock_irqsave(&priv->lock, flags);
174 priv->rdtodo = 0;
175 priv->wrfilled = 0;
176 priv->wrsent = 0;
177 spin_unlock_irqrestore(&priv->lock, flags);
178
179 return result;
180}
181
Alan Cox335f8512009-06-11 12:26:29 +0100182static void cyberjack_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (port->serial->dev) {
185 /* shutdown any bulk reads that might be going on */
186 usb_kill_urb(port->write_urb);
187 usb_kill_urb(port->read_urb);
188 }
189}
190
Alan Cox95da3102008-07-22 11:09:07 +0100191static int cyberjack_write(struct tty_struct *tty,
192 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700194 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct cyberjack_private *priv = usb_get_serial_port_data(port);
196 unsigned long flags;
197 int result;
198 int wrexpected;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (count == 0) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700201 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxa5b6f602008-04-08 17:16:06 +0100202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
Johan Hovoldc1cac102011-11-06 19:06:23 +0100205 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700206 dev_dbg(dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
210 spin_lock_irqsave(&priv->lock, flags);
211
Alan Coxb9c52f12008-07-22 11:10:27 +0100212 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* To much data for buffer. Reset buffer. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100214 priv->wrfilled = 0;
Alan Coxa5b6f602008-04-08 17:16:06 +0100215 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100216 set_bit(0, &port->write_urbs_free);
Alan Coxa5b6f602008-04-08 17:16:06 +0100217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
220 /* Copy data */
Alan Coxb9c52f12008-07-22 11:10:27 +0100221 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100223 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 priv->wrfilled += count;
225
Alan Coxb9c52f12008-07-22 11:10:27 +0100226 if (priv->wrfilled >= 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700228 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
Alan Coxb9c52f12008-07-22 11:10:27 +0100229 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 wrexpected = sizeof(priv->wrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Alan Coxb9c52f12008-07-22 11:10:27 +0100232 if (priv->wrfilled >= wrexpected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* We have enough data to begin transmission */
234 int length;
235
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700236 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100237 length = (wrexpected > port->bulk_out_size) ?
238 port->bulk_out_size : wrexpected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Alan Coxb9c52f12008-07-22 11:10:27 +0100240 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
241 priv->wrsent = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100244 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* send the data out the bulk port */
247 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
248 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700249 dev_err(&port->dev,
250 "%s - failed submitting write urb, error %d",
251 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100253 priv->wrfilled = 0;
254 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100256 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258 }
259
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700260 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
261 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alan Coxb9c52f12008-07-22 11:10:27 +0100263 if (priv->wrsent >= priv->wrfilled) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700264 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100265 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100266 priv->wrfilled = 0;
267 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 }
270
271 spin_unlock_irqrestore(&priv->lock, flags);
272
Alan Coxb9c52f12008-07-22 11:10:27 +0100273 return count;
274}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Alan Cox95da3102008-07-22 11:09:07 +0100276static int cyberjack_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Alan Coxa5b6f602008-04-08 17:16:06 +0100278 /* FIXME: .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return CYBERJACK_LOCAL_BUF_SIZE;
280}
281
Alan Cox95da3102008-07-22 11:09:07 +0100282static void cyberjack_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Ming Leicdc97792008-02-24 18:41:47 +0800284 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700286 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700288 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int result;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* the urb might have been killed. */
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700292 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return;
294
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100295 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 /* React only to interrupts signaling a bulk_in transfer */
Alan Coxb9c52f12008-07-22 11:10:27 +0100298 if (urb->actual_length == 4 && data[0] == 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 short old_rdtodo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* This is a announcement of coming bulk_ins. */
302 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
303
304 spin_lock(&priv->lock);
305
306 old_rdtodo = priv->rdtodo;
307
Alan Coxb9c52f12008-07-22 11:10:27 +0100308 if (old_rdtodo + size < old_rdtodo) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700309 dev_dbg(dev, "To many bulk_in urbs to do.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 spin_unlock(&priv->lock);
311 goto resubmit;
312 }
313
314 /* "+=" is probably more fault tollerant than "=" */
315 priv->rdtodo += size;
316
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700317 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 spin_unlock(&priv->lock);
320
Alan Coxb9c52f12008-07-22 11:10:27 +0100321 if (!old_rdtodo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Alan Coxb9c52f12008-07-22 11:10:27 +0100323 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700324 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700325 __func__, result);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700326 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 }
329
330resubmit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
332 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700333 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700334 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Alan Cox95da3102008-07-22 11:09:07 +0100337static void cyberjack_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Ming Leicdc97792008-02-24 18:41:47 +0800339 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700341 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct tty_struct *tty;
343 unsigned char *data = urb->transfer_buffer;
344 short todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int result;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700346 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100348 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700349 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700350 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
351 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return;
353 }
354
Alan Cox4a90f092008-10-13 10:39:46 +0100355 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!tty) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700357 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return;
359 }
360 if (urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800361 tty_insert_flip_string(tty, data, urb->actual_length);
Alan Coxb9c52f12008-07-22 11:10:27 +0100362 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Alan Cox4a90f092008-10-13 10:39:46 +0100364 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 spin_lock(&priv->lock);
367
368 /* Reduce urbs to do by one. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100369 priv->rdtodo -= urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Just to be sure */
Alan Coxb9c52f12008-07-22 11:10:27 +0100371 if (priv->rdtodo < 0)
372 priv->rdtodo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 todo = priv->rdtodo;
374
375 spin_unlock(&priv->lock);
376
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700377 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* Continue to read if we have still urbs to do. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100380 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
382 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700383 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
384 __func__, result);
385 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387}
388
Alan Cox95da3102008-07-22 11:09:07 +0100389static void cyberjack_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Ming Leicdc97792008-02-24 18:41:47 +0800391 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700393 struct device *dev = &port->dev;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700394 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Johan Hovoldc1cac102011-11-06 19:06:23 +0100396 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700397 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700398 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
399 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return;
401 }
402
403 spin_lock(&priv->lock);
404
405 /* only do something if we have more data to send */
Alan Coxb9c52f12008-07-22 11:10:27 +0100406 if (priv->wrfilled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int length, blksize, result;
408
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700409 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
412 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
413
Alan Coxb9c52f12008-07-22 11:10:27 +0100414 memcpy(port->write_urb->transfer_buffer,
415 priv->wrbuf + priv->wrsent, length);
416 priv->wrsent += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100419 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* send the data out the bulk port */
422 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
423 if (result) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700424 dev_err(dev, "%s - failed submitting write urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700425 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100427 priv->wrfilled = 0;
428 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto exit;
430 }
431
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700432 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
433 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
436
Alan Coxb9c52f12008-07-22 11:10:27 +0100437 if (priv->wrsent >= priv->wrfilled ||
438 priv->wrsent >= blksize) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700439 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100440 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100441 priv->wrfilled = 0;
442 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444 }
445
446exit:
447 spin_unlock(&priv->lock);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700448 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700451module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Alan Coxb9c52f12008-07-22 11:10:27 +0100453MODULE_AUTHOR(DRIVER_AUTHOR);
454MODULE_DESCRIPTION(DRIVER_DESC);
455MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456MODULE_LICENSE("GPL");