Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / usb / serial / cyberjack.c
blobbfec6f4b8f86f908369fe52e9f2086efdcb3ba75
1 /*
2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
4 * Copyright (C) 2001 REINER SCT
5 * Author: Matthias Bruestle
7 * Contact: support@reiner-sct.com (see MAINTAINERS)
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.
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.
18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
19 * patience.
21 * In case of problems, please write to the contact e-mail address
22 * mentioned above.
24 * Please note that later models of the cyberjack reader family are
25 * supported by a libusb-based userspace device driver.
27 * Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
31 #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>
40 #include <linux/uaccess.h>
41 #include <linux/usb.h>
42 #include <linux/usb/serial.h>
44 #define CYBERJACK_LOCAL_BUF_SIZE 32
46 static bool debug;
49 * Version Information
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"
56 #define CYBERJACK_VENDOR_ID 0x0C4B
57 #define CYBERJACK_PRODUCT_ID 0x0100
59 /* Function prototypes */
60 static void cyberjack_disconnect(struct usb_serial *serial);
61 static int cyberjack_port_probe(struct usb_serial_port *port);
62 static int cyberjack_port_remove(struct usb_serial_port *port);
63 static int cyberjack_open(struct tty_struct *tty,
64 struct usb_serial_port *port);
65 static void cyberjack_close(struct usb_serial_port *port);
66 static int cyberjack_write(struct tty_struct *tty,
67 struct usb_serial_port *port, const unsigned char *buf, int count);
68 static int cyberjack_write_room(struct tty_struct *tty);
69 static void cyberjack_read_int_callback(struct urb *urb);
70 static void cyberjack_read_bulk_callback(struct urb *urb);
71 static void cyberjack_write_bulk_callback(struct urb *urb);
73 static const struct usb_device_id id_table[] = {
74 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
75 { } /* Terminating entry */
78 MODULE_DEVICE_TABLE(usb, id_table);
80 static struct usb_serial_driver cyberjack_device = {
81 .driver = {
82 .owner = THIS_MODULE,
83 .name = "cyberjack",
85 .description = "Reiner SCT Cyberjack USB card reader",
86 .id_table = id_table,
87 .num_ports = 1,
88 .disconnect = cyberjack_disconnect,
89 .port_probe = cyberjack_port_probe,
90 .port_remove = cyberjack_port_remove,
91 .open = cyberjack_open,
92 .close = cyberjack_close,
93 .write = cyberjack_write,
94 .write_room = cyberjack_write_room,
95 .read_int_callback = cyberjack_read_int_callback,
96 .read_bulk_callback = cyberjack_read_bulk_callback,
97 .write_bulk_callback = cyberjack_write_bulk_callback,
100 static struct usb_serial_driver * const serial_drivers[] = {
101 &cyberjack_device, NULL
104 struct 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 */
112 static int cyberjack_port_probe(struct usb_serial_port *port)
114 struct cyberjack_private *priv;
115 int result;
117 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
118 if (!priv)
119 return -ENOMEM;
121 spin_lock_init(&priv->lock);
122 priv->rdtodo = 0;
123 priv->wrfilled = 0;
124 priv->wrsent = 0;
126 usb_set_serial_port_data(port, priv);
128 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
129 if (result)
130 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
132 return 0;
135 static int cyberjack_port_remove(struct usb_serial_port *port)
137 struct cyberjack_private *priv;
139 priv = usb_get_serial_port_data(port);
140 kfree(priv);
142 return 0;
145 static void cyberjack_disconnect(struct usb_serial *serial)
147 int i;
149 for (i = 0; i < serial->num_ports; ++i)
150 usb_kill_urb(serial->port[i]->interrupt_in_urb);
153 static int cyberjack_open(struct tty_struct *tty,
154 struct usb_serial_port *port)
156 struct cyberjack_private *priv;
157 unsigned long flags;
158 int result = 0;
160 dbg("%s - usb_clear_halt", __func__);
161 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
163 priv = usb_get_serial_port_data(port);
164 spin_lock_irqsave(&priv->lock, flags);
165 priv->rdtodo = 0;
166 priv->wrfilled = 0;
167 priv->wrsent = 0;
168 spin_unlock_irqrestore(&priv->lock, flags);
170 return result;
173 static void cyberjack_close(struct usb_serial_port *port)
175 if (port->serial->dev) {
176 /* shutdown any bulk reads that might be going on */
177 usb_kill_urb(port->write_urb);
178 usb_kill_urb(port->read_urb);
182 static int cyberjack_write(struct tty_struct *tty,
183 struct usb_serial_port *port, const unsigned char *buf, int count)
185 struct cyberjack_private *priv = usb_get_serial_port_data(port);
186 unsigned long flags;
187 int result;
188 int wrexpected;
190 if (count == 0) {
191 dbg("%s - write request of 0 bytes", __func__);
192 return 0;
195 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
196 dbg("%s - already writing", __func__);
197 return 0;
200 spin_lock_irqsave(&priv->lock, flags);
202 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
203 /* To much data for buffer. Reset buffer. */
204 priv->wrfilled = 0;
205 spin_unlock_irqrestore(&priv->lock, flags);
206 set_bit(0, &port->write_urbs_free);
207 return 0;
210 /* Copy data */
211 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
213 usb_serial_debug_data(debug, &port->dev, __func__, count,
214 priv->wrbuf + priv->wrfilled);
215 priv->wrfilled += count;
217 if (priv->wrfilled >= 3) {
218 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
219 dbg("%s - expected data: %d", __func__, wrexpected);
220 } else
221 wrexpected = sizeof(priv->wrbuf);
223 if (priv->wrfilled >= wrexpected) {
224 /* We have enough data to begin transmission */
225 int length;
227 dbg("%s - transmitting data (frame 1)", __func__);
228 length = (wrexpected > port->bulk_out_size) ?
229 port->bulk_out_size : wrexpected;
231 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
232 priv->wrsent = length;
234 /* set up our urb */
235 port->write_urb->transfer_buffer_length = length;
237 /* send the data out the bulk port */
238 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
239 if (result) {
240 dev_err(&port->dev,
241 "%s - failed submitting write urb, error %d",
242 __func__, result);
243 /* Throw away data. No better idea what to do with it. */
244 priv->wrfilled = 0;
245 priv->wrsent = 0;
246 spin_unlock_irqrestore(&priv->lock, flags);
247 set_bit(0, &port->write_urbs_free);
248 return 0;
251 dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
252 dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
254 if (priv->wrsent >= priv->wrfilled) {
255 dbg("%s - buffer cleaned", __func__);
256 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
257 priv->wrfilled = 0;
258 priv->wrsent = 0;
262 spin_unlock_irqrestore(&priv->lock, flags);
264 return count;
267 static int cyberjack_write_room(struct tty_struct *tty)
269 /* FIXME: .... */
270 return CYBERJACK_LOCAL_BUF_SIZE;
273 static void cyberjack_read_int_callback(struct urb *urb)
275 struct usb_serial_port *port = urb->context;
276 struct cyberjack_private *priv = usb_get_serial_port_data(port);
277 unsigned char *data = urb->transfer_buffer;
278 int status = urb->status;
279 int result;
281 /* the urb might have been killed. */
282 if (status)
283 return;
285 usb_serial_debug_data(debug, &port->dev, __func__,
286 urb->actual_length, data);
288 /* React only to interrupts signaling a bulk_in transfer */
289 if (urb->actual_length == 4 && data[0] == 0x01) {
290 short old_rdtodo;
292 /* This is a announcement of coming bulk_ins. */
293 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
295 spin_lock(&priv->lock);
297 old_rdtodo = priv->rdtodo;
299 if (old_rdtodo + size < old_rdtodo) {
300 dbg("To many bulk_in urbs to do.");
301 spin_unlock(&priv->lock);
302 goto resubmit;
305 /* "+=" is probably more fault tollerant than "=" */
306 priv->rdtodo += size;
308 dbg("%s - rdtodo: %d", __func__, priv->rdtodo);
310 spin_unlock(&priv->lock);
312 if (!old_rdtodo) {
313 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
314 if (result)
315 dev_err(&port->dev, "%s - failed resubmitting "
316 "read urb, error %d\n",
317 __func__, result);
318 dbg("%s - usb_submit_urb(read urb)", __func__);
322 resubmit:
323 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
324 if (result)
325 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
326 dbg("%s - usb_submit_urb(int urb)", __func__);
329 static void cyberjack_read_bulk_callback(struct urb *urb)
331 struct usb_serial_port *port = urb->context;
332 struct cyberjack_private *priv = usb_get_serial_port_data(port);
333 struct tty_struct *tty;
334 unsigned char *data = urb->transfer_buffer;
335 short todo;
336 int result;
337 int status = urb->status;
339 usb_serial_debug_data(debug, &port->dev, __func__,
340 urb->actual_length, data);
341 if (status) {
342 dbg("%s - nonzero read bulk status received: %d",
343 __func__, status);
344 return;
347 tty = tty_port_tty_get(&port->port);
348 if (!tty) {
349 dbg("%s - ignoring since device not open", __func__);
350 return;
352 if (urb->actual_length) {
353 tty_insert_flip_string(tty, data, urb->actual_length);
354 tty_flip_buffer_push(tty);
356 tty_kref_put(tty);
358 spin_lock(&priv->lock);
360 /* Reduce urbs to do by one. */
361 priv->rdtodo -= urb->actual_length;
362 /* Just to be sure */
363 if (priv->rdtodo < 0)
364 priv->rdtodo = 0;
365 todo = priv->rdtodo;
367 spin_unlock(&priv->lock);
369 dbg("%s - rdtodo: %d", __func__, todo);
371 /* Continue to read if we have still urbs to do. */
372 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
373 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
374 if (result)
375 dev_err(&port->dev, "%s - failed resubmitting read "
376 "urb, error %d\n", __func__, result);
377 dbg("%s - usb_submit_urb(read urb)", __func__);
381 static void cyberjack_write_bulk_callback(struct urb *urb)
383 struct usb_serial_port *port = urb->context;
384 struct cyberjack_private *priv = usb_get_serial_port_data(port);
385 int status = urb->status;
387 set_bit(0, &port->write_urbs_free);
388 if (status) {
389 dbg("%s - nonzero write bulk status received: %d",
390 __func__, status);
391 return;
394 spin_lock(&priv->lock);
396 /* only do something if we have more data to send */
397 if (priv->wrfilled) {
398 int length, blksize, result;
400 dbg("%s - transmitting data (frame n)", __func__);
402 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
403 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
405 memcpy(port->write_urb->transfer_buffer,
406 priv->wrbuf + priv->wrsent, length);
407 priv->wrsent += length;
409 /* set up our urb */
410 port->write_urb->transfer_buffer_length = length;
412 /* send the data out the bulk port */
413 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
414 if (result) {
415 dev_err(&port->dev,
416 "%s - failed submitting write urb, error %d\n",
417 __func__, result);
418 /* Throw away data. No better idea what to do with it. */
419 priv->wrfilled = 0;
420 priv->wrsent = 0;
421 goto exit;
424 dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
425 dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
427 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
429 if (priv->wrsent >= priv->wrfilled ||
430 priv->wrsent >= blksize) {
431 dbg("%s - buffer cleaned", __func__);
432 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
433 priv->wrfilled = 0;
434 priv->wrsent = 0;
438 exit:
439 spin_unlock(&priv->lock);
440 usb_serial_port_softint(port);
443 module_usb_serial_driver(serial_drivers, id_table);
445 MODULE_AUTHOR(DRIVER_AUTHOR);
446 MODULE_DESCRIPTION(DRIVER_DESC);
447 MODULE_VERSION(DRIVER_VERSION);
448 MODULE_LICENSE("GPL");
450 module_param(debug, bool, S_IRUGO | S_IWUSR);
451 MODULE_PARM_DESC(debug, "Debug enabled or not");