blob: 9c90575cf3542d6b0315e5ffe010060e54f7db00 [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
Rusty Russell90ab5ee2012-01-13 09:32:20 +103046static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * Version Information
50 */
51#define DRIVER_VERSION "v1.01"
52#define DRIVER_AUTHOR "Matthias Bruestle"
53#define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
54
55
56#define CYBERJACK_VENDOR_ID 0x0C4B
57#define CYBERJACK_PRODUCT_ID 0x0100
58
59/* Function prototypes */
Alan Cox95da3102008-07-22 11:09:07 +010060static int cyberjack_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040061static void cyberjack_disconnect(struct usb_serial *serial);
62static void cyberjack_release(struct usb_serial *serial);
Alan Cox95da3102008-07-22 11:09:07 +010063static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -070064 struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010065static void cyberjack_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010066static int cyberjack_write(struct tty_struct *tty,
67 struct usb_serial_port *port, const unsigned char *buf, int count);
Alan Coxb9c52f12008-07-22 11:10:27 +010068static int cyberjack_write_room(struct tty_struct *tty);
Alan Cox95da3102008-07-22 11:09:07 +010069static void cyberjack_read_int_callback(struct urb *urb);
70static void cyberjack_read_bulk_callback(struct urb *urb);
71static void cyberjack_write_bulk_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Németh Márton7d40d7e2010-01-10 15:34:24 +010073static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
75 { } /* Terminating entry */
76};
77
Alan Coxb9c52f12008-07-22 11:10:27 +010078MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070080static struct usb_serial_driver cyberjack_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070081 .driver = {
82 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070083 .name = "cyberjack",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070084 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070085 .description = "Reiner SCT Cyberjack USB card reader",
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .num_ports = 1,
88 .attach = cyberjack_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -040089 .disconnect = cyberjack_disconnect,
90 .release = cyberjack_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .open = cyberjack_open,
92 .close = cyberjack_close,
93 .write = cyberjack_write,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010094 .write_room = cyberjack_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .read_int_callback = cyberjack_read_int_callback,
96 .read_bulk_callback = cyberjack_read_bulk_callback,
97 .write_bulk_callback = cyberjack_write_bulk_callback,
98};
99
Alan Stern08a4f6b2012-02-23 14:56:17 -0500100static struct usb_serial_driver * const serial_drivers[] = {
101 &cyberjack_device, NULL
102};
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104struct cyberjack_private {
105 spinlock_t lock; /* Lock for SMP */
106 short rdtodo; /* Bytes still to read */
107 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
108 short wrfilled; /* Overall data size we already got */
109 short wrsent; /* Data already sent */
110};
111
112/* do some startup allocations not currently performed by usb_serial_probe() */
Alan Cox95da3102008-07-22 11:09:07 +0100113static int cyberjack_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct cyberjack_private *priv;
116 int i;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* allocate the private data structure */
119 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
120 if (!priv)
121 return -ENOMEM;
122
123 /* set initial values */
124 spin_lock_init(&priv->lock);
125 priv->rdtodo = 0;
126 priv->wrfilled = 0;
127 priv->wrsent = 0;
128 usb_set_serial_port_data(serial->port[0], priv);
129
130 init_waitqueue_head(&serial->port[0]->write_wait);
131
132 for (i = 0; i < serial->num_ports; ++i) {
133 int result;
Alan Coxb9c52f12008-07-22 11:10:27 +0100134 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 GFP_KERNEL);
136 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700137 dev_err(&serial->dev->dev,
138 "usb_submit_urb(read int) failed\n");
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700139 dev_dbg(&serial->dev->dev, "%s - usb_submit_urb(int urb)\n",
140 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
Alan Coxb9c52f12008-07-22 11:10:27 +0100143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Alan Sternf9c99bb2009-06-02 11:53:55 -0400146static void cyberjack_disconnect(struct usb_serial *serial)
147{
148 int i;
149
Alan Sternf9c99bb2009-06-02 11:53:55 -0400150 for (i = 0; i < serial->num_ports; ++i)
151 usb_kill_urb(serial->port[i]->interrupt_in_urb);
152}
153
154static void cyberjack_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 int i;
Alan Coxb9c52f12008-07-22 11:10:27 +0100157
Alan Coxa5b6f602008-04-08 17:16:06 +0100158 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* My special items, the standard routines free my urbs */
160 kfree(usb_get_serial_port_data(serial->port[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162}
Alan Coxb9c52f12008-07-22 11:10:27 +0100163
Alan Cox95da3102008-07-22 11:09:07 +0100164static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700165 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct cyberjack_private *priv;
168 unsigned long flags;
169 int result = 0;
170
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700171 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 priv = usb_get_serial_port_data(port);
175 spin_lock_irqsave(&priv->lock, flags);
176 priv->rdtodo = 0;
177 priv->wrfilled = 0;
178 priv->wrsent = 0;
179 spin_unlock_irqrestore(&priv->lock, flags);
180
181 return result;
182}
183
Alan Cox335f8512009-06-11 12:26:29 +0100184static void cyberjack_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (port->serial->dev) {
187 /* shutdown any bulk reads that might be going on */
188 usb_kill_urb(port->write_urb);
189 usb_kill_urb(port->read_urb);
190 }
191}
192
Alan Cox95da3102008-07-22 11:09:07 +0100193static int cyberjack_write(struct tty_struct *tty,
194 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700196 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct cyberjack_private *priv = usb_get_serial_port_data(port);
198 unsigned long flags;
199 int result;
200 int wrexpected;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (count == 0) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700203 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxa5b6f602008-04-08 17:16:06 +0100204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
Johan Hovoldc1cac102011-11-06 19:06:23 +0100207 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700208 dev_dbg(dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 spin_lock_irqsave(&priv->lock, flags);
213
Alan Coxb9c52f12008-07-22 11:10:27 +0100214 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* To much data for buffer. Reset buffer. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100216 priv->wrfilled = 0;
Alan Coxa5b6f602008-04-08 17:16:06 +0100217 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100218 set_bit(0, &port->write_urbs_free);
Alan Coxa5b6f602008-04-08 17:16:06 +0100219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
222 /* Copy data */
Alan Coxb9c52f12008-07-22 11:10:27 +0100223 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700225 usb_serial_debug_data(debug, dev, __func__, count,
Alan Coxb9c52f12008-07-22 11:10:27 +0100226 priv->wrbuf + priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 priv->wrfilled += count;
228
Alan Coxb9c52f12008-07-22 11:10:27 +0100229 if (priv->wrfilled >= 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700231 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
Alan Coxb9c52f12008-07-22 11:10:27 +0100232 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 wrexpected = sizeof(priv->wrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Alan Coxb9c52f12008-07-22 11:10:27 +0100235 if (priv->wrfilled >= wrexpected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* We have enough data to begin transmission */
237 int length;
238
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700239 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100240 length = (wrexpected > port->bulk_out_size) ?
241 port->bulk_out_size : wrexpected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Alan Coxb9c52f12008-07-22 11:10:27 +0100243 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
244 priv->wrsent = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100247 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* send the data out the bulk port */
250 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
251 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700252 dev_err(&port->dev,
253 "%s - failed submitting write urb, error %d",
254 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100256 priv->wrfilled = 0;
257 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100259 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261 }
262
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700263 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
264 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Alan Coxb9c52f12008-07-22 11:10:27 +0100266 if (priv->wrsent >= priv->wrfilled) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700267 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100268 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100269 priv->wrfilled = 0;
270 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 }
273
274 spin_unlock_irqrestore(&priv->lock, flags);
275
Alan Coxb9c52f12008-07-22 11:10:27 +0100276 return count;
277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Alan Cox95da3102008-07-22 11:09:07 +0100279static int cyberjack_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Alan Coxa5b6f602008-04-08 17:16:06 +0100281 /* FIXME: .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return CYBERJACK_LOCAL_BUF_SIZE;
283}
284
Alan Cox95da3102008-07-22 11:09:07 +0100285static void cyberjack_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Ming Leicdc97792008-02-24 18:41:47 +0800287 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700289 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700291 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int result;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* the urb might have been killed. */
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700295 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return;
297
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700298 usb_serial_debug_data(debug, dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* React only to interrupts signaling a bulk_in transfer */
Alan Coxb9c52f12008-07-22 11:10:27 +0100301 if (urb->actual_length == 4 && data[0] == 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 short old_rdtodo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* This is a announcement of coming bulk_ins. */
305 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
306
307 spin_lock(&priv->lock);
308
309 old_rdtodo = priv->rdtodo;
310
Alan Coxb9c52f12008-07-22 11:10:27 +0100311 if (old_rdtodo + size < old_rdtodo) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700312 dev_dbg(dev, "To many bulk_in urbs to do.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 spin_unlock(&priv->lock);
314 goto resubmit;
315 }
316
317 /* "+=" is probably more fault tollerant than "=" */
318 priv->rdtodo += size;
319
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700320 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 spin_unlock(&priv->lock);
323
Alan Coxb9c52f12008-07-22 11:10:27 +0100324 if (!old_rdtodo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Alan Coxb9c52f12008-07-22 11:10:27 +0100326 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700327 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700328 __func__, result);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700329 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 }
332
333resubmit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
335 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700336 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700337 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Alan Cox95da3102008-07-22 11:09:07 +0100340static void cyberjack_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Ming Leicdc97792008-02-24 18:41:47 +0800342 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700344 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct tty_struct *tty;
346 unsigned char *data = urb->transfer_buffer;
347 short todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int result;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700349 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Alan Coxb9c52f12008-07-22 11:10:27 +0100351 usb_serial_debug_data(debug, &port->dev, __func__,
352 urb->actual_length, data);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700353 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700354 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
355 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
357 }
358
Alan Cox4a90f092008-10-13 10:39:46 +0100359 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!tty) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700361 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return;
363 }
364 if (urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800365 tty_insert_flip_string(tty, data, urb->actual_length);
Alan Coxb9c52f12008-07-22 11:10:27 +0100366 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Alan Cox4a90f092008-10-13 10:39:46 +0100368 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 spin_lock(&priv->lock);
371
372 /* Reduce urbs to do by one. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100373 priv->rdtodo -= urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* Just to be sure */
Alan Coxb9c52f12008-07-22 11:10:27 +0100375 if (priv->rdtodo < 0)
376 priv->rdtodo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 todo = priv->rdtodo;
378
379 spin_unlock(&priv->lock);
380
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700381 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* Continue to read if we have still urbs to do. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100384 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
386 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700387 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
388 __func__, result);
389 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391}
392
Alan Cox95da3102008-07-22 11:09:07 +0100393static void cyberjack_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Ming Leicdc97792008-02-24 18:41:47 +0800395 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700397 struct device *dev = &port->dev;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700398 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Johan Hovoldc1cac102011-11-06 19:06:23 +0100400 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700401 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700402 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
403 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return;
405 }
406
407 spin_lock(&priv->lock);
408
409 /* only do something if we have more data to send */
Alan Coxb9c52f12008-07-22 11:10:27 +0100410 if (priv->wrfilled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int length, blksize, result;
412
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700413 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
416 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
417
Alan Coxb9c52f12008-07-22 11:10:27 +0100418 memcpy(port->write_urb->transfer_buffer,
419 priv->wrbuf + priv->wrsent, length);
420 priv->wrsent += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100423 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* send the data out the bulk port */
426 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
427 if (result) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700428 dev_err(dev, "%s - failed submitting write urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700429 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100431 priv->wrfilled = 0;
432 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto exit;
434 }
435
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700436 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
437 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
440
Alan Coxb9c52f12008-07-22 11:10:27 +0100441 if (priv->wrsent >= priv->wrfilled ||
442 priv->wrsent >= blksize) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700443 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100444 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100445 priv->wrfilled = 0;
446 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 }
449
450exit:
451 spin_unlock(&priv->lock);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700452 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700455module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Alan Coxb9c52f12008-07-22 11:10:27 +0100457MODULE_AUTHOR(DRIVER_AUTHOR);
458MODULE_DESCRIPTION(DRIVER_DESC);
459MODULE_VERSION(DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460MODULE_LICENSE("GPL");
461
462module_param(debug, bool, S_IRUGO | S_IWUSR);
463MODULE_PARM_DESC(debug, "Debug enabled or not");