blob: 4ee77dcbe690dc73e95f79ebf2d3d75e2784326b [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 Sternf9c99bb2009-06-02 11:53:55 -040058static void cyberjack_disconnect(struct usb_serial *serial);
Johan Hovolda9556042012-10-15 18:20:54 +020059static int cyberjack_port_probe(struct usb_serial_port *port);
60static int cyberjack_port_remove(struct usb_serial_port *port);
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,
Alan Sternf9c99bb2009-06-02 11:53:55 -040086 .disconnect = cyberjack_disconnect,
Johan Hovolda9556042012-10-15 18:20:54 +020087 .port_probe = cyberjack_port_probe,
88 .port_remove = cyberjack_port_remove,
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
Johan Hovolda9556042012-10-15 18:20:54 +0200110static int cyberjack_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct cyberjack_private *priv;
Johan Hovolda9556042012-10-15 18:20:54 +0200113 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
116 if (!priv)
117 return -ENOMEM;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 spin_lock_init(&priv->lock);
120 priv->rdtodo = 0;
121 priv->wrfilled = 0;
122 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Johan Hovolda9556042012-10-15 18:20:54 +0200124 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Johan Hovolda9556042012-10-15 18:20:54 +0200126 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
127 if (result)
128 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
129
130 return 0;
131}
132
133static int cyberjack_port_remove(struct usb_serial_port *port)
134{
135 struct cyberjack_private *priv;
136
137 priv = usb_get_serial_port_data(port);
138 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Alan Coxb9c52f12008-07-22 11:10:27 +0100140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Alan Sternf9c99bb2009-06-02 11:53:55 -0400143static void cyberjack_disconnect(struct usb_serial *serial)
144{
145 int i;
146
Alan Sternf9c99bb2009-06-02 11:53:55 -0400147 for (i = 0; i < serial->num_ports; ++i)
148 usb_kill_urb(serial->port[i]->interrupt_in_urb);
149}
150
Alan Cox95da3102008-07-22 11:09:07 +0100151static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700152 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct cyberjack_private *priv;
155 unsigned long flags;
156 int result = 0;
157
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700158 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 priv = usb_get_serial_port_data(port);
162 spin_lock_irqsave(&priv->lock, flags);
163 priv->rdtodo = 0;
164 priv->wrfilled = 0;
165 priv->wrsent = 0;
166 spin_unlock_irqrestore(&priv->lock, flags);
167
168 return result;
169}
170
Alan Cox335f8512009-06-11 12:26:29 +0100171static void cyberjack_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (port->serial->dev) {
174 /* shutdown any bulk reads that might be going on */
175 usb_kill_urb(port->write_urb);
176 usb_kill_urb(port->read_urb);
177 }
178}
179
Alan Cox95da3102008-07-22 11:09:07 +0100180static int cyberjack_write(struct tty_struct *tty,
181 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700183 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct cyberjack_private *priv = usb_get_serial_port_data(port);
185 unsigned long flags;
186 int result;
187 int wrexpected;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (count == 0) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700190 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxa5b6f602008-04-08 17:16:06 +0100191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
Johan Hovoldc1cac102011-11-06 19:06:23 +0100194 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700195 dev_dbg(dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
199 spin_lock_irqsave(&priv->lock, flags);
200
Alan Coxb9c52f12008-07-22 11:10:27 +0100201 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* To much data for buffer. Reset buffer. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100203 priv->wrfilled = 0;
Alan Coxa5b6f602008-04-08 17:16:06 +0100204 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100205 set_bit(0, &port->write_urbs_free);
Alan Coxa5b6f602008-04-08 17:16:06 +0100206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 /* Copy data */
Alan Coxb9c52f12008-07-22 11:10:27 +0100210 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100212 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 priv->wrfilled += count;
214
Alan Coxb9c52f12008-07-22 11:10:27 +0100215 if (priv->wrfilled >= 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700217 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
Alan Coxb9c52f12008-07-22 11:10:27 +0100218 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 wrexpected = sizeof(priv->wrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Alan Coxb9c52f12008-07-22 11:10:27 +0100221 if (priv->wrfilled >= wrexpected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* We have enough data to begin transmission */
223 int length;
224
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700225 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100226 length = (wrexpected > port->bulk_out_size) ?
227 port->bulk_out_size : wrexpected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Alan Coxb9c52f12008-07-22 11:10:27 +0100229 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
230 priv->wrsent = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100233 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /* send the data out the bulk port */
236 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
237 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700238 dev_err(&port->dev,
239 "%s - failed submitting write urb, error %d",
240 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100242 priv->wrfilled = 0;
243 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100245 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
247 }
248
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700249 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
250 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Alan Coxb9c52f12008-07-22 11:10:27 +0100252 if (priv->wrsent >= priv->wrfilled) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700253 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100254 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100255 priv->wrfilled = 0;
256 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 }
259
260 spin_unlock_irqrestore(&priv->lock, flags);
261
Alan Coxb9c52f12008-07-22 11:10:27 +0100262 return count;
263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Alan Cox95da3102008-07-22 11:09:07 +0100265static int cyberjack_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Alan Coxa5b6f602008-04-08 17:16:06 +0100267 /* FIXME: .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return CYBERJACK_LOCAL_BUF_SIZE;
269}
270
Alan Cox95da3102008-07-22 11:09:07 +0100271static void cyberjack_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Ming Leicdc97792008-02-24 18:41:47 +0800273 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700275 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700277 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int result;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* the urb might have been killed. */
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700281 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return;
283
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100284 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* React only to interrupts signaling a bulk_in transfer */
Alan Coxb9c52f12008-07-22 11:10:27 +0100287 if (urb->actual_length == 4 && data[0] == 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 short old_rdtodo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* This is a announcement of coming bulk_ins. */
291 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
292
293 spin_lock(&priv->lock);
294
295 old_rdtodo = priv->rdtodo;
296
Alan Coxb9c52f12008-07-22 11:10:27 +0100297 if (old_rdtodo + size < old_rdtodo) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700298 dev_dbg(dev, "To many bulk_in urbs to do.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 spin_unlock(&priv->lock);
300 goto resubmit;
301 }
302
303 /* "+=" is probably more fault tollerant than "=" */
304 priv->rdtodo += size;
305
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700306 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 spin_unlock(&priv->lock);
309
Alan Coxb9c52f12008-07-22 11:10:27 +0100310 if (!old_rdtodo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Alan Coxb9c52f12008-07-22 11:10:27 +0100312 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700313 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700314 __func__, result);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700315 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 }
318
319resubmit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
321 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700322 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700323 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Alan Cox95da3102008-07-22 11:09:07 +0100326static void cyberjack_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Ming Leicdc97792008-02-24 18:41:47 +0800328 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700330 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct tty_struct *tty;
332 unsigned char *data = urb->transfer_buffer;
333 short todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int result;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700335 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100337 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700338 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700339 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
340 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return;
342 }
343
Alan Cox4a90f092008-10-13 10:39:46 +0100344 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!tty) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700346 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return;
348 }
349 if (urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800350 tty_insert_flip_string(tty, data, urb->actual_length);
Alan Coxb9c52f12008-07-22 11:10:27 +0100351 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Alan Cox4a90f092008-10-13 10:39:46 +0100353 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 spin_lock(&priv->lock);
356
357 /* Reduce urbs to do by one. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100358 priv->rdtodo -= urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Just to be sure */
Alan Coxb9c52f12008-07-22 11:10:27 +0100360 if (priv->rdtodo < 0)
361 priv->rdtodo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 todo = priv->rdtodo;
363
364 spin_unlock(&priv->lock);
365
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700366 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* Continue to read if we have still urbs to do. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100369 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
371 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700372 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
373 __func__, result);
374 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376}
377
Alan Cox95da3102008-07-22 11:09:07 +0100378static void cyberjack_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Ming Leicdc97792008-02-24 18:41:47 +0800380 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700382 struct device *dev = &port->dev;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700383 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Johan Hovoldc1cac102011-11-06 19:06:23 +0100385 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700386 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700387 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
388 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return;
390 }
391
392 spin_lock(&priv->lock);
393
394 /* only do something if we have more data to send */
Alan Coxb9c52f12008-07-22 11:10:27 +0100395 if (priv->wrfilled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int length, blksize, result;
397
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700398 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
401 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
402
Alan Coxb9c52f12008-07-22 11:10:27 +0100403 memcpy(port->write_urb->transfer_buffer,
404 priv->wrbuf + priv->wrsent, length);
405 priv->wrsent += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100408 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* send the data out the bulk port */
411 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
412 if (result) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700413 dev_err(dev, "%s - failed submitting write urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700414 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100416 priv->wrfilled = 0;
417 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 goto exit;
419 }
420
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700421 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
422 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
425
Alan Coxb9c52f12008-07-22 11:10:27 +0100426 if (priv->wrsent >= priv->wrfilled ||
427 priv->wrsent >= blksize) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700428 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100429 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100430 priv->wrfilled = 0;
431 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 }
434
435exit:
436 spin_unlock(&priv->lock);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700437 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700440module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Alan Coxb9c52f12008-07-22 11:10:27 +0100442MODULE_AUTHOR(DRIVER_AUTHOR);
443MODULE_DESCRIPTION(DRIVER_DESC);
444MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445MODULE_LICENSE("GPL");