blob: 69a4fa1cee25b4cdcf7cb8673e7b5b21548a9913 [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#define DRIVER_AUTHOR "Matthias Bruestle"
47#define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
48
49
50#define CYBERJACK_VENDOR_ID 0x0C4B
51#define CYBERJACK_PRODUCT_ID 0x0100
52
53/* Function prototypes */
Alan Sternf9c99bb2009-06-02 11:53:55 -040054static void cyberjack_disconnect(struct usb_serial *serial);
Johan Hovolda9556042012-10-15 18:20:54 +020055static int cyberjack_port_probe(struct usb_serial_port *port);
56static int cyberjack_port_remove(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010057static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -070058 struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010059static void cyberjack_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010060static int cyberjack_write(struct tty_struct *tty,
61 struct usb_serial_port *port, const unsigned char *buf, int count);
Alan Coxb9c52f12008-07-22 11:10:27 +010062static int cyberjack_write_room(struct tty_struct *tty);
Alan Cox95da3102008-07-22 11:09:07 +010063static void cyberjack_read_int_callback(struct urb *urb);
64static void cyberjack_read_bulk_callback(struct urb *urb);
65static void cyberjack_write_bulk_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Németh Márton7d40d7e2010-01-10 15:34:24 +010067static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
69 { } /* Terminating entry */
70};
71
Alan Coxb9c52f12008-07-22 11:10:27 +010072MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070074static struct usb_serial_driver cyberjack_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070075 .driver = {
76 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070077 .name = "cyberjack",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070078 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070079 .description = "Reiner SCT Cyberjack USB card reader",
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040082 .disconnect = cyberjack_disconnect,
Johan Hovolda9556042012-10-15 18:20:54 +020083 .port_probe = cyberjack_port_probe,
84 .port_remove = cyberjack_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .open = cyberjack_open,
86 .close = cyberjack_close,
87 .write = cyberjack_write,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010088 .write_room = cyberjack_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .read_int_callback = cyberjack_read_int_callback,
90 .read_bulk_callback = cyberjack_read_bulk_callback,
91 .write_bulk_callback = cyberjack_write_bulk_callback,
92};
93
Alan Stern08a4f6b2012-02-23 14:56:17 -050094static struct usb_serial_driver * const serial_drivers[] = {
95 &cyberjack_device, NULL
96};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct cyberjack_private {
99 spinlock_t lock; /* Lock for SMP */
100 short rdtodo; /* Bytes still to read */
101 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
102 short wrfilled; /* Overall data size we already got */
103 short wrsent; /* Data already sent */
104};
105
Johan Hovolda9556042012-10-15 18:20:54 +0200106static int cyberjack_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct cyberjack_private *priv;
Johan Hovolda9556042012-10-15 18:20:54 +0200109 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
112 if (!priv)
113 return -ENOMEM;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 spin_lock_init(&priv->lock);
116 priv->rdtodo = 0;
117 priv->wrfilled = 0;
118 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Johan Hovolda9556042012-10-15 18:20:54 +0200120 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Johan Hovolda9556042012-10-15 18:20:54 +0200122 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
123 if (result)
124 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
125
126 return 0;
127}
128
129static int cyberjack_port_remove(struct usb_serial_port *port)
130{
131 struct cyberjack_private *priv;
132
133 priv = usb_get_serial_port_data(port);
134 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Alan Coxb9c52f12008-07-22 11:10:27 +0100136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Alan Sternf9c99bb2009-06-02 11:53:55 -0400139static void cyberjack_disconnect(struct usb_serial *serial)
140{
141 int i;
142
Alan Sternf9c99bb2009-06-02 11:53:55 -0400143 for (i = 0; i < serial->num_ports; ++i)
144 usb_kill_urb(serial->port[i]->interrupt_in_urb);
145}
146
Alan Cox95da3102008-07-22 11:09:07 +0100147static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700148 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct cyberjack_private *priv;
151 unsigned long flags;
152 int result = 0;
153
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700154 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 priv = usb_get_serial_port_data(port);
158 spin_lock_irqsave(&priv->lock, flags);
159 priv->rdtodo = 0;
160 priv->wrfilled = 0;
161 priv->wrsent = 0;
162 spin_unlock_irqrestore(&priv->lock, flags);
163
164 return result;
165}
166
Alan Cox335f8512009-06-11 12:26:29 +0100167static void cyberjack_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (port->serial->dev) {
170 /* shutdown any bulk reads that might be going on */
171 usb_kill_urb(port->write_urb);
172 usb_kill_urb(port->read_urb);
173 }
174}
175
Alan Cox95da3102008-07-22 11:09:07 +0100176static int cyberjack_write(struct tty_struct *tty,
177 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700179 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct cyberjack_private *priv = usb_get_serial_port_data(port);
181 unsigned long flags;
182 int result;
183 int wrexpected;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (count == 0) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700186 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxa5b6f602008-04-08 17:16:06 +0100187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
Johan Hovoldc1cac102011-11-06 19:06:23 +0100190 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700191 dev_dbg(dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
195 spin_lock_irqsave(&priv->lock, flags);
196
Alan Coxb9c52f12008-07-22 11:10:27 +0100197 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* To much data for buffer. Reset buffer. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100199 priv->wrfilled = 0;
Alan Coxa5b6f602008-04-08 17:16:06 +0100200 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100201 set_bit(0, &port->write_urbs_free);
Alan Coxa5b6f602008-04-08 17:16:06 +0100202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
205 /* Copy data */
Alan Coxb9c52f12008-07-22 11:10:27 +0100206 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100208 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 priv->wrfilled += count;
210
Alan Coxb9c52f12008-07-22 11:10:27 +0100211 if (priv->wrfilled >= 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700213 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
Alan Coxb9c52f12008-07-22 11:10:27 +0100214 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 wrexpected = sizeof(priv->wrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Alan Coxb9c52f12008-07-22 11:10:27 +0100217 if (priv->wrfilled >= wrexpected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /* We have enough data to begin transmission */
219 int length;
220
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700221 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100222 length = (wrexpected > port->bulk_out_size) ?
223 port->bulk_out_size : wrexpected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Alan Coxb9c52f12008-07-22 11:10:27 +0100225 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
226 priv->wrsent = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100229 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* send the data out the bulk port */
232 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
233 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700234 dev_err(&port->dev,
235 "%s - failed submitting write urb, error %d",
236 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100238 priv->wrfilled = 0;
239 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100241 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243 }
244
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700245 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
246 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Alan Coxb9c52f12008-07-22 11:10:27 +0100248 if (priv->wrsent >= priv->wrfilled) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700249 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100250 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100251 priv->wrfilled = 0;
252 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 }
255
256 spin_unlock_irqrestore(&priv->lock, flags);
257
Alan Coxb9c52f12008-07-22 11:10:27 +0100258 return count;
259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Alan Cox95da3102008-07-22 11:09:07 +0100261static int cyberjack_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Alan Coxa5b6f602008-04-08 17:16:06 +0100263 /* FIXME: .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return CYBERJACK_LOCAL_BUF_SIZE;
265}
266
Alan Cox95da3102008-07-22 11:09:07 +0100267static void cyberjack_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Ming Leicdc97792008-02-24 18:41:47 +0800269 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700271 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700273 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int result;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* the urb might have been killed. */
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700277 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return;
279
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100280 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* React only to interrupts signaling a bulk_in transfer */
Alan Coxb9c52f12008-07-22 11:10:27 +0100283 if (urb->actual_length == 4 && data[0] == 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 short old_rdtodo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* This is a announcement of coming bulk_ins. */
287 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
288
289 spin_lock(&priv->lock);
290
291 old_rdtodo = priv->rdtodo;
292
Alan Coxb9c52f12008-07-22 11:10:27 +0100293 if (old_rdtodo + size < old_rdtodo) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700294 dev_dbg(dev, "To many bulk_in urbs to do.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 spin_unlock(&priv->lock);
296 goto resubmit;
297 }
298
299 /* "+=" is probably more fault tollerant than "=" */
300 priv->rdtodo += size;
301
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700302 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 spin_unlock(&priv->lock);
305
Alan Coxb9c52f12008-07-22 11:10:27 +0100306 if (!old_rdtodo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Alan Coxb9c52f12008-07-22 11:10:27 +0100308 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700309 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700310 __func__, result);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700311 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
314
315resubmit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
317 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700318 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700319 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Alan Cox95da3102008-07-22 11:09:07 +0100322static void cyberjack_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Ming Leicdc97792008-02-24 18:41:47 +0800324 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700326 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct tty_struct *tty;
328 unsigned char *data = urb->transfer_buffer;
329 short todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int result;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700331 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100333 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700334 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700335 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
336 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return;
338 }
339
Alan Cox4a90f092008-10-13 10:39:46 +0100340 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!tty) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700342 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return;
344 }
345 if (urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -0800346 tty_insert_flip_string(tty, data, urb->actual_length);
Alan Coxb9c52f12008-07-22 11:10:27 +0100347 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Alan Cox4a90f092008-10-13 10:39:46 +0100349 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 spin_lock(&priv->lock);
352
353 /* Reduce urbs to do by one. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100354 priv->rdtodo -= urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* Just to be sure */
Alan Coxb9c52f12008-07-22 11:10:27 +0100356 if (priv->rdtodo < 0)
357 priv->rdtodo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 todo = priv->rdtodo;
359
360 spin_unlock(&priv->lock);
361
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700362 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* Continue to read if we have still urbs to do. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100365 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
367 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700368 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
369 __func__, result);
370 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372}
373
Alan Cox95da3102008-07-22 11:09:07 +0100374static void cyberjack_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Ming Leicdc97792008-02-24 18:41:47 +0800376 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700378 struct device *dev = &port->dev;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700379 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Johan Hovoldc1cac102011-11-06 19:06:23 +0100381 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700382 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700383 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
384 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return;
386 }
387
388 spin_lock(&priv->lock);
389
390 /* only do something if we have more data to send */
Alan Coxb9c52f12008-07-22 11:10:27 +0100391 if (priv->wrfilled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int length, blksize, result;
393
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700394 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
397 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
398
Alan Coxb9c52f12008-07-22 11:10:27 +0100399 memcpy(port->write_urb->transfer_buffer,
400 priv->wrbuf + priv->wrsent, length);
401 priv->wrsent += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100404 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* send the data out the bulk port */
407 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
408 if (result) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700409 dev_err(dev, "%s - failed submitting write urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700410 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100412 priv->wrfilled = 0;
413 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto exit;
415 }
416
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700417 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
418 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
421
Alan Coxb9c52f12008-07-22 11:10:27 +0100422 if (priv->wrsent >= priv->wrfilled ||
423 priv->wrsent >= blksize) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700424 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100425 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100426 priv->wrfilled = 0;
427 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 }
430
431exit:
432 spin_unlock(&priv->lock);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700433 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700436module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Alan Coxb9c52f12008-07-22 11:10:27 +0100438MODULE_AUTHOR(DRIVER_AUTHOR);
439MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440MODULE_LICENSE("GPL");