2 * KOBIL USB Smart Card Terminal Driver
4 * Copyright (C) 2002 KOBIL Systems GmbH
5 * Author: Thomas Wahrenbruch
7 * Contact: linuxusb@kobil.de
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
21 * Supported readers: USB TWIN, KAAN Standard Plus and SecOVID Reader Plus
22 * (Adapter K), B1 Professional and KAAN Professional (Adapter B)
25 * Fix bug with P'n'P readers
28 * Add support for KAAN SIM
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/tty.h>
43 #include <linux/tty_driver.h>
44 #include <linux/tty_flip.h>
45 #include <linux/module.h>
46 #include <linux/spinlock.h>
47 #include <asm/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/serial.h>
50 #include <linux/ioctl.h>
51 #include "kobil_sct.h"
55 /* Version Information */
56 #define DRIVER_VERSION "21/05/2004"
57 #define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
58 #define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
60 #define KOBIL_VENDOR_ID 0x0D46
61 #define KOBIL_ADAPTER_B_PRODUCT_ID 0x2011
62 #define KOBIL_ADAPTER_K_PRODUCT_ID 0x2012
63 #define KOBIL_USBTWIN_PRODUCT_ID 0x0078
64 #define KOBIL_KAAN_SIM_PRODUCT_ID 0x0081
66 #define KOBIL_TIMEOUT 500
67 #define KOBIL_BUF_LENGTH 300
70 /* Function prototypes */
71 static int kobil_startup (struct usb_serial *serial);
72 static void kobil_shutdown (struct usb_serial *serial);
73 static int kobil_open (struct usb_serial_port *port, struct file *filp);
74 static void kobil_close (struct usb_serial_port *port, struct file *filp);
75 static int kobil_write (struct usb_serial_port *port,
76 const unsigned char *buf, int count);
77 static int kobil_write_room(struct usb_serial_port *port);
78 static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
79 unsigned int cmd, unsigned long arg);
80 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file);
81 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
82 unsigned int set, unsigned int clear);
83 static void kobil_read_int_callback( struct urb *urb );
84 static void kobil_write_callback( struct urb *purb );
85 static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old);
88 static struct usb_device_id id_table [] = {
89 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
90 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
91 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
92 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
93 { } /* Terminating entry */
97 MODULE_DEVICE_TABLE (usb, id_table);
99 static struct usb_driver kobil_driver = {
101 .probe = usb_serial_probe,
102 .disconnect = usb_serial_disconnect,
103 .id_table = id_table,
108 static struct usb_serial_driver kobil_device = {
110 .owner = THIS_MODULE,
113 .description = "KOBIL USB smart card terminal",
114 .usb_driver = &kobil_driver,
115 .id_table = id_table,
116 .num_interrupt_in = NUM_DONT_CARE,
117 .num_interrupt_out = NUM_DONT_CARE,
121 .attach = kobil_startup,
122 .shutdown = kobil_shutdown,
123 .ioctl = kobil_ioctl,
124 .set_termios = kobil_set_termios,
125 .tiocmget = kobil_tiocmget,
126 .tiocmset = kobil_tiocmset,
128 .close = kobil_close,
129 .write = kobil_write,
130 .write_room = kobil_write_room,
131 .read_int_callback = kobil_read_int_callback,
135 struct kobil_private {
136 int write_int_endpoint_address;
137 int read_int_endpoint_address;
138 unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
139 int filled; // index of the last char in buf
140 int cur_pos; // index of the next char to send in buf
146 static int kobil_startup (struct usb_serial *serial)
149 struct kobil_private *priv;
150 struct usb_device *pdev;
151 struct usb_host_config *actconfig;
152 struct usb_interface *interface;
153 struct usb_host_interface *altsetting;
154 struct usb_host_endpoint *endpoint;
156 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
163 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
164 priv->line_state = 0;
166 switch (priv->device_type){
167 case KOBIL_ADAPTER_B_PRODUCT_ID:
168 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
170 case KOBIL_ADAPTER_K_PRODUCT_ID:
171 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
173 case KOBIL_USBTWIN_PRODUCT_ID:
174 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
176 case KOBIL_KAAN_SIM_PRODUCT_ID:
177 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
180 usb_set_serial_port_data(serial->port[0], priv);
182 // search for the necessary endpoints
184 actconfig = pdev->actconfig;
185 interface = actconfig->interface[0];
186 altsetting = interface->cur_altsetting;
187 endpoint = altsetting->endpoint;
189 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
190 endpoint = &altsetting->endpoint[i];
191 if (usb_endpoint_is_int_out(&endpoint->desc)) {
192 dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
193 priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
195 if (usb_endpoint_is_int_in(&endpoint->desc)) {
196 dbg("%s Found interrupt in endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
197 priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
204 static void kobil_shutdown (struct usb_serial *serial)
207 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
209 for (i=0; i < serial->num_ports; ++i) {
210 while (serial->port[i]->open_count > 0) {
211 kobil_close (serial->port[i], NULL);
213 kfree(usb_get_serial_port_data(serial->port[i]));
214 usb_set_serial_port_data(serial->port[i], NULL);
219 static int kobil_open (struct usb_serial_port *port, struct file *filp)
222 struct kobil_private *priv;
223 unsigned char *transfer_buffer;
224 int transfer_buffer_length = 8;
225 int write_urb_transfer_buffer_length = 8;
227 dbg("%s - port %d", __FUNCTION__, port->number);
228 priv = usb_get_serial_port_data(port);
229 priv->line_state = 0;
231 // someone sets the dev to 0 if the close method has been called
232 port->interrupt_in_urb->dev = port->serial->dev;
235 /* force low_latency on so that our tty_push actually forces
236 * the data through, otherwise it is scheduled, and with high
237 * data rates (like with OHCI) data can get lost.
239 port->tty->low_latency = 1;
241 // without this, every push_tty_char is echoed :-(
242 port->tty->termios->c_lflag = 0;
243 port->tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
244 port->tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
245 port->tty->termios->c_oflag &= ~ONLCR; // do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D)
247 // allocate memory for transfer buffer
248 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
249 if (! transfer_buffer) {
253 // allocate write_urb
254 if (!port->write_urb) {
255 dbg("%s - port %d Allocating port->write_urb", __FUNCTION__, port->number);
256 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
257 if (!port->write_urb) {
258 dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
259 kfree(transfer_buffer);
264 // allocate memory for write_urb transfer buffer
265 port->write_urb->transfer_buffer = kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
266 if (! port->write_urb->transfer_buffer) {
267 kfree(transfer_buffer);
268 usb_free_urb(port->write_urb);
269 port->write_urb = NULL;
273 // get hardware version
274 result = usb_control_msg( port->serial->dev,
275 usb_rcvctrlpipe(port->serial->dev, 0 ),
276 SUSBCRequest_GetMisc,
277 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
278 SUSBCR_MSC_GetHWVersion,
281 transfer_buffer_length,
284 dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
285 dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
287 // get firmware version
288 result = usb_control_msg( port->serial->dev,
289 usb_rcvctrlpipe(port->serial->dev, 0 ),
290 SUSBCRequest_GetMisc,
291 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
292 SUSBCR_MSC_GetFWVersion,
295 transfer_buffer_length,
298 dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
299 dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
301 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
302 // Setting Baudrate, Parity and Stopbits
303 result = usb_control_msg( port->serial->dev,
304 usb_rcvctrlpipe(port->serial->dev, 0 ),
305 SUSBCRequest_SetBaudRateParityAndStopBits,
306 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
307 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
313 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
316 result = usb_control_msg( port->serial->dev,
317 usb_rcvctrlpipe(port->serial->dev, 0 ),
319 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
320 SUSBCR_MSC_ResetAllQueues,
326 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
328 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
329 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
330 // start reading (Adapter B 'cause PNP string)
331 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
332 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
335 kfree(transfer_buffer);
340 static void kobil_close (struct usb_serial_port *port, struct file *filp)
342 dbg("%s - port %d", __FUNCTION__, port->number);
344 if (port->write_urb) {
345 usb_kill_urb(port->write_urb);
346 usb_free_urb( port->write_urb );
347 port->write_urb = NULL;
349 usb_kill_urb(port->interrupt_in_urb);
353 static void kobil_read_int_callback(struct urb *urb)
356 struct usb_serial_port *port = urb->context;
357 struct tty_struct *tty;
358 unsigned char *data = urb->transfer_buffer;
359 int status = urb->status;
362 dbg("%s - port %d", __FUNCTION__, port->number);
365 dbg("%s - port %d Read int status not zero: %d",
366 __FUNCTION__, port->number, status);
371 if (urb->actual_length) {
375 dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
379 for (i = 0; i < purb->actual_length; i++) {
380 sprintf(dbg_data +3*i, "%02X ", data[i]);
382 dbg(" <-- %s", dbg_data );
387 tty_buffer_request_room(tty, urb->actual_length);
388 tty_insert_flip_string(tty, data, urb->actual_length);
389 tty_flip_buffer_push(tty);
392 // someone sets the dev to 0 if the close method has been called
393 port->interrupt_in_urb->dev = port->serial->dev;
395 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
396 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
400 static void kobil_write_callback( struct urb *purb )
405 static int kobil_write (struct usb_serial_port *port,
406 const unsigned char *buf, int count)
411 struct kobil_private * priv;
414 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
418 priv = usb_get_serial_port_data(port);
420 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
421 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
425 // Copy data to buffer
426 memcpy (priv->buf + priv->filled, buf, count);
428 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
430 priv->filled = priv->filled + count;
433 // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
434 if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
435 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
437 // stop reading (except TWIN and KAAN SIM)
438 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
439 usb_kill_urb(port->interrupt_in_urb);
441 todo = priv->filled - priv->cur_pos;
444 // max 8 byte in one urb (endpoint size)
445 length = (todo < 8) ? todo : 8;
446 // copy data to transfer buffer
447 memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
448 usb_fill_int_urb( port->write_urb,
450 usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
451 port->write_urb->transfer_buffer,
453 kobil_write_callback,
458 priv->cur_pos = priv->cur_pos + length;
459 result = usb_submit_urb( port->write_urb, GFP_NOIO );
460 dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
461 todo = priv->filled - priv->cur_pos;
472 // someone sets the dev to 0 if the close method has been called
473 port->interrupt_in_urb->dev = port->serial->dev;
475 // start reading (except TWIN and KAAN SIM)
476 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
477 // someone sets the dev to 0 if the close method has been called
478 port->interrupt_in_urb->dev = port->serial->dev;
480 result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
481 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
488 static int kobil_write_room (struct usb_serial_port *port)
490 //dbg("%s - port %d", __FUNCTION__, port->number);
495 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
497 struct kobil_private * priv;
499 unsigned char *transfer_buffer;
500 int transfer_buffer_length = 8;
502 priv = usb_get_serial_port_data(port);
503 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
504 // This device doesn't support ioctl calls
508 // allocate memory for transfer buffer
509 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
510 if (!transfer_buffer) {
514 result = usb_control_msg( port->serial->dev,
515 usb_rcvctrlpipe(port->serial->dev, 0 ),
516 SUSBCRequest_GetStatusLineState,
517 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
521 transfer_buffer_length,
524 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
525 __FUNCTION__, port->number, result, transfer_buffer[0]);
527 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) {
528 priv->line_state |= TIOCM_DSR;
530 priv->line_state &= ~TIOCM_DSR;
533 kfree(transfer_buffer);
534 return priv->line_state;
537 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
538 unsigned int set, unsigned int clear)
540 struct kobil_private * priv;
544 unsigned char *transfer_buffer;
545 int transfer_buffer_length = 8;
547 priv = usb_get_serial_port_data(port);
548 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
549 // This device doesn't support ioctl calls
553 // allocate memory for transfer buffer
554 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
555 if (! transfer_buffer) {
563 if (clear & TIOCM_RTS)
565 if (clear & TIOCM_DTR)
568 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
570 dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
572 dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
573 result = usb_control_msg( port->serial->dev,
574 usb_rcvctrlpipe(port->serial->dev, 0 ),
575 SUSBCRequest_SetStatusLinesOrQueues,
576 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
577 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
584 dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
586 dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
587 result = usb_control_msg( port->serial->dev,
588 usb_rcvctrlpipe(port->serial->dev, 0 ),
589 SUSBCRequest_SetStatusLinesOrQueues,
590 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
591 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
597 dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
598 kfree(transfer_buffer);
599 return (result < 0) ? result : 0;
602 static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old)
604 struct kobil_private * priv;
606 unsigned short urb_val = 0;
607 int c_cflag = port->tty->termios->c_cflag;
611 priv = usb_get_serial_port_data(port);
612 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
613 // This device doesn't support ioctl calls
616 switch (speed = tty_get_baud_rate(port->tty)) {
618 urb_val = SUSBCR_SBR_1200;
623 urb_val = SUSBCR_SBR_9600;
626 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
628 settings = kzalloc(50, GFP_KERNEL);
632 sprintf(settings, "%d ", speed);
634 if (c_cflag & PARENB) {
635 if (c_cflag & PARODD) {
636 urb_val |= SUSBCR_SPASB_OddParity;
637 strcat(settings, "Odd Parity");
639 urb_val |= SUSBCR_SPASB_EvenParity;
640 strcat(settings, "Even Parity");
643 urb_val |= SUSBCR_SPASB_NoParity;
644 strcat(settings, "No Parity");
646 port->tty->termios->c_cflag &= ~CMSPAR;
647 tty_encode_baud_rate(port->tty, speed, speed);
649 result = usb_control_msg( port->serial->dev,
650 usb_rcvctrlpipe(port->serial->dev, 0 ),
651 SUSBCRequest_SetBaudRateParityAndStopBits,
652 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
662 static int kobil_ioctl(struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
664 struct kobil_private * priv = usb_get_serial_port_data(port);
665 unsigned char *transfer_buffer;
666 int transfer_buffer_length = 8;
669 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
670 // This device doesn't support ioctl calls
674 case TCFLSH: // 0x540B
675 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
676 if (! transfer_buffer)
679 result = usb_control_msg( port->serial->dev,
680 usb_rcvctrlpipe(port->serial->dev, 0 ),
682 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
683 SUSBCR_MSC_ResetAllQueues,
685 NULL,//transfer_buffer,
690 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
691 kfree(transfer_buffer);
692 return (result < 0) ? -EFAULT : 0;
698 static int __init kobil_init (void)
701 retval = usb_serial_register(&kobil_device);
703 goto failed_usb_serial_register;
704 retval = usb_register(&kobil_driver);
706 goto failed_usb_register;
708 info(DRIVER_VERSION " " DRIVER_AUTHOR);
713 usb_serial_deregister(&kobil_device);
714 failed_usb_serial_register:
719 static void __exit kobil_exit (void)
721 usb_deregister (&kobil_driver);
722 usb_serial_deregister (&kobil_device);
725 module_init(kobil_init);
726 module_exit(kobil_exit);
728 MODULE_AUTHOR( DRIVER_AUTHOR );
729 MODULE_DESCRIPTION( DRIVER_DESC );
730 MODULE_LICENSE( "GPL" );
732 module_param(debug, bool, S_IRUGO | S_IWUSR);
733 MODULE_PARM_DESC(debug, "Debug enabled or not");