]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/serial/cyberjack.c
USB: serial: remove debug parameter from usb_serial_debug_data()
[karo-tx-linux.git] / drivers / usb / serial / cyberjack.c
1 /*
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
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>
43
44 #define CYBERJACK_LOCAL_BUF_SIZE 32
45
46 static bool debug;
47
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 */
60 static int cyberjack_startup(struct usb_serial *serial);
61 static void cyberjack_disconnect(struct usb_serial *serial);
62 static void cyberjack_release(struct usb_serial *serial);
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);
72
73 static const struct usb_device_id id_table[] = {
74         { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
75         { }                     /* Terminating entry */
76 };
77
78 MODULE_DEVICE_TABLE(usb, id_table);
79
80 static struct usb_serial_driver cyberjack_device = {
81         .driver = {
82                 .owner =        THIS_MODULE,
83                 .name =         "cyberjack",
84         },
85         .description =          "Reiner SCT Cyberjack USB card reader",
86         .id_table =             id_table,
87         .num_ports =            1,
88         .attach =               cyberjack_startup,
89         .disconnect =           cyberjack_disconnect,
90         .release =              cyberjack_release,
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,
98 };
99
100 static struct usb_serial_driver * const serial_drivers[] = {
101         &cyberjack_device, NULL
102 };
103
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 */
110 };
111
112 /* do some startup allocations not currently performed by usb_serial_probe() */
113 static int cyberjack_startup(struct usb_serial *serial)
114 {
115         struct cyberjack_private *priv;
116         int i;
117
118         /* 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;
134                 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
135                                         GFP_KERNEL);
136                 if (result)
137                         dev_err(&serial->dev->dev,
138                                 "usb_submit_urb(read int) failed\n");
139                 dev_dbg(&serial->dev->dev, "%s - usb_submit_urb(int urb)\n",
140                         __func__);
141         }
142
143         return 0;
144 }
145
146 static void cyberjack_disconnect(struct usb_serial *serial)
147 {
148         int i;
149
150         for (i = 0; i < serial->num_ports; ++i)
151                 usb_kill_urb(serial->port[i]->interrupt_in_urb);
152 }
153
154 static void cyberjack_release(struct usb_serial *serial)
155 {
156         int i;
157
158         for (i = 0; i < serial->num_ports; ++i) {
159                 /* My special items, the standard routines free my urbs */
160                 kfree(usb_get_serial_port_data(serial->port[i]));
161         }
162 }
163
164 static int  cyberjack_open(struct tty_struct *tty,
165                                         struct usb_serial_port *port)
166 {
167         struct cyberjack_private *priv;
168         unsigned long flags;
169         int result = 0;
170
171         dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
172         usb_clear_halt(port->serial->dev, port->write_urb->pipe);
173
174         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
184 static void cyberjack_close(struct usb_serial_port *port)
185 {
186         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
193 static int cyberjack_write(struct tty_struct *tty,
194         struct usb_serial_port *port, const unsigned char *buf, int count)
195 {
196         struct device *dev = &port->dev;
197         struct cyberjack_private *priv = usb_get_serial_port_data(port);
198         unsigned long flags;
199         int result;
200         int wrexpected;
201
202         if (count == 0) {
203                 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
204                 return 0;
205         }
206
207         if (!test_and_clear_bit(0, &port->write_urbs_free)) {
208                 dev_dbg(dev, "%s - already writing\n", __func__);
209                 return 0;
210         }
211
212         spin_lock_irqsave(&priv->lock, flags);
213
214         if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
215                 /* To much data for buffer. Reset buffer. */
216                 priv->wrfilled = 0;
217                 spin_unlock_irqrestore(&priv->lock, flags);
218                 set_bit(0, &port->write_urbs_free);
219                 return 0;
220         }
221
222         /* Copy data */
223         memcpy(priv->wrbuf + priv->wrfilled, buf, count);
224
225         usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
226         priv->wrfilled += count;
227
228         if (priv->wrfilled >= 3) {
229                 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
230                 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
231         } else
232                 wrexpected = sizeof(priv->wrbuf);
233
234         if (priv->wrfilled >= wrexpected) {
235                 /* We have enough data to begin transmission */
236                 int length;
237
238                 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
239                 length = (wrexpected > port->bulk_out_size) ?
240                                         port->bulk_out_size : wrexpected;
241
242                 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
243                 priv->wrsent = length;
244
245                 /* set up our urb */
246                 port->write_urb->transfer_buffer_length = length;
247
248                 /* send the data out the bulk port */
249                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
250                 if (result) {
251                         dev_err(&port->dev,
252                                 "%s - failed submitting write urb, error %d",
253                                 __func__, result);
254                         /* Throw away data. No better idea what to do with it. */
255                         priv->wrfilled = 0;
256                         priv->wrsent = 0;
257                         spin_unlock_irqrestore(&priv->lock, flags);
258                         set_bit(0, &port->write_urbs_free);
259                         return 0;
260                 }
261
262                 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
263                 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
264
265                 if (priv->wrsent >= priv->wrfilled) {
266                         dev_dbg(dev, "%s - buffer cleaned\n", __func__);
267                         memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
268                         priv->wrfilled = 0;
269                         priv->wrsent = 0;
270                 }
271         }
272
273         spin_unlock_irqrestore(&priv->lock, flags);
274
275         return count;
276 }
277
278 static int cyberjack_write_room(struct tty_struct *tty)
279 {
280         /* FIXME: .... */
281         return CYBERJACK_LOCAL_BUF_SIZE;
282 }
283
284 static void cyberjack_read_int_callback(struct urb *urb)
285 {
286         struct usb_serial_port *port = urb->context;
287         struct cyberjack_private *priv = usb_get_serial_port_data(port);
288         struct device *dev = &port->dev;
289         unsigned char *data = urb->transfer_buffer;
290         int status = urb->status;
291         int result;
292
293         /* the urb might have been killed. */
294         if (status)
295                 return;
296
297         usb_serial_debug_data(dev, __func__, urb->actual_length, data);
298
299         /* React only to interrupts signaling a bulk_in transfer */
300         if (urb->actual_length == 4 && data[0] == 0x01) {
301                 short old_rdtodo;
302
303                 /* This is a announcement of coming bulk_ins. */
304                 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
305
306                 spin_lock(&priv->lock);
307
308                 old_rdtodo = priv->rdtodo;
309
310                 if (old_rdtodo + size < old_rdtodo) {
311                         dev_dbg(dev, "To many bulk_in urbs to do.\n");
312                         spin_unlock(&priv->lock);
313                         goto resubmit;
314                 }
315
316                 /* "+=" is probably more fault tollerant than "=" */
317                 priv->rdtodo += size;
318
319                 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
320
321                 spin_unlock(&priv->lock);
322
323                 if (!old_rdtodo) {
324                         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
325                         if (result)
326                                 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
327                                         __func__, result);
328                         dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
329                 }
330         }
331
332 resubmit:
333         result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
334         if (result)
335                 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
336         dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
337 }
338
339 static void cyberjack_read_bulk_callback(struct urb *urb)
340 {
341         struct usb_serial_port *port = urb->context;
342         struct cyberjack_private *priv = usb_get_serial_port_data(port);
343         struct device *dev = &port->dev;
344         struct tty_struct *tty;
345         unsigned char *data = urb->transfer_buffer;
346         short todo;
347         int result;
348         int status = urb->status;
349
350         usb_serial_debug_data(dev, __func__, urb->actual_length, data);
351         if (status) {
352                 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
353                         __func__, status);
354                 return;
355         }
356
357         tty = tty_port_tty_get(&port->port);
358         if (!tty) {
359                 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
360                 return;
361         }
362         if (urb->actual_length) {
363                 tty_insert_flip_string(tty, data, urb->actual_length);
364                 tty_flip_buffer_push(tty);
365         }
366         tty_kref_put(tty);
367
368         spin_lock(&priv->lock);
369
370         /* Reduce urbs to do by one. */
371         priv->rdtodo -= urb->actual_length;
372         /* Just to be sure */
373         if (priv->rdtodo < 0)
374                 priv->rdtodo = 0;
375         todo = priv->rdtodo;
376
377         spin_unlock(&priv->lock);
378
379         dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
380
381         /* Continue to read if we have still urbs to do. */
382         if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
383                 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
384                 if (result)
385                         dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
386                                 __func__, result);
387                 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
388         }
389 }
390
391 static void cyberjack_write_bulk_callback(struct urb *urb)
392 {
393         struct usb_serial_port *port = urb->context;
394         struct cyberjack_private *priv = usb_get_serial_port_data(port);
395         struct device *dev = &port->dev;
396         int status = urb->status;
397
398         set_bit(0, &port->write_urbs_free);
399         if (status) {
400                 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
401                         __func__, status);
402                 return;
403         }
404
405         spin_lock(&priv->lock);
406
407         /* only do something if we have more data to send */
408         if (priv->wrfilled) {
409                 int length, blksize, result;
410
411                 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
412
413                 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
414                         port->bulk_out_size : (priv->wrfilled - priv->wrsent);
415
416                 memcpy(port->write_urb->transfer_buffer,
417                                         priv->wrbuf + priv->wrsent, length);
418                 priv->wrsent += length;
419
420                 /* set up our urb */
421                 port->write_urb->transfer_buffer_length = length;
422
423                 /* send the data out the bulk port */
424                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
425                 if (result) {
426                         dev_err(dev, "%s - failed submitting write urb, error %d\n",
427                                 __func__, result);
428                         /* Throw away data. No better idea what to do with it. */
429                         priv->wrfilled = 0;
430                         priv->wrsent = 0;
431                         goto exit;
432                 }
433
434                 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
435                 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
436
437                 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
438
439                 if (priv->wrsent >= priv->wrfilled ||
440                                         priv->wrsent >= blksize) {
441                         dev_dbg(dev, "%s - buffer cleaned\n", __func__);
442                         memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
443                         priv->wrfilled = 0;
444                         priv->wrsent = 0;
445                 }
446         }
447
448 exit:
449         spin_unlock(&priv->lock);
450         usb_serial_port_softint(port);
451 }
452
453 module_usb_serial_driver(serial_drivers, id_table);
454
455 MODULE_AUTHOR(DRIVER_AUTHOR);
456 MODULE_DESCRIPTION(DRIVER_DESC);
457 MODULE_VERSION(DRIVER_VERSION);
458 MODULE_LICENSE("GPL");
459
460 module_param(debug, bool, S_IRUGO | S_IWUSR);
461 MODULE_PARM_DESC(debug, "Debug enabled or not");