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,
120 .attach = kobil_startup,
121 .shutdown = kobil_shutdown,
122 .ioctl = kobil_ioctl,
123 .set_termios = kobil_set_termios,
124 .tiocmget = kobil_tiocmget,
125 .tiocmset = kobil_tiocmset,
127 .close = kobil_close,
128 .write = kobil_write,
129 .write_room = kobil_write_room,
130 .read_int_callback = kobil_read_int_callback,
134 struct kobil_private {
135 int write_int_endpoint_address;
136 int read_int_endpoint_address;
137 unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
138 int filled; // index of the last char in buf
139 int cur_pos; // index of the next char to send in buf
145 static int kobil_startup (struct usb_serial *serial)
148 struct kobil_private *priv;
149 struct usb_device *pdev;
150 struct usb_host_config *actconfig;
151 struct usb_interface *interface;
152 struct usb_host_interface *altsetting;
153 struct usb_host_endpoint *endpoint;
155 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
162 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
163 priv->line_state = 0;
165 switch (priv->device_type){
166 case KOBIL_ADAPTER_B_PRODUCT_ID:
167 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
169 case KOBIL_ADAPTER_K_PRODUCT_ID:
170 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
172 case KOBIL_USBTWIN_PRODUCT_ID:
173 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
175 case KOBIL_KAAN_SIM_PRODUCT_ID:
176 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
179 usb_set_serial_port_data(serial->port[0], priv);
181 // search for the necessary endpoints
183 actconfig = pdev->actconfig;
184 interface = actconfig->interface[0];
185 altsetting = interface->cur_altsetting;
186 endpoint = altsetting->endpoint;
188 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
189 endpoint = &altsetting->endpoint[i];
190 if (usb_endpoint_is_int_out(&endpoint->desc)) {
191 dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
192 priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
194 if (usb_endpoint_is_int_in(&endpoint->desc)) {
195 dbg("%s Found interrupt in endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
196 priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
203 static void kobil_shutdown (struct usb_serial *serial)
206 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
208 for (i=0; i < serial->num_ports; ++i) {
209 while (serial->port[i]->open_count > 0) {
210 kobil_close (serial->port[i], NULL);
212 kfree(usb_get_serial_port_data(serial->port[i]));
213 usb_set_serial_port_data(serial->port[i], NULL);
218 static int kobil_open (struct usb_serial_port *port, struct file *filp)
221 struct kobil_private *priv;
222 unsigned char *transfer_buffer;
223 int transfer_buffer_length = 8;
224 int write_urb_transfer_buffer_length = 8;
226 dbg("%s - port %d", __FUNCTION__, port->number);
227 priv = usb_get_serial_port_data(port);
228 priv->line_state = 0;
230 // someone sets the dev to 0 if the close method has been called
231 port->interrupt_in_urb->dev = port->serial->dev;
234 /* force low_latency on so that our tty_push actually forces
235 * the data through, otherwise it is scheduled, and with high
236 * data rates (like with OHCI) data can get lost.
238 port->tty->low_latency = 1;
240 // without this, every push_tty_char is echoed :-(
241 port->tty->termios->c_lflag = 0;
242 port->tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
243 port->tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
244 port->tty->termios->c_oflag &= ~ONLCR; // do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D)
246 // allocate memory for transfer buffer
247 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
248 if (! transfer_buffer) {
252 // allocate write_urb
253 if (!port->write_urb) {
254 dbg("%s - port %d Allocating port->write_urb", __FUNCTION__, port->number);
255 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
256 if (!port->write_urb) {
257 dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
258 kfree(transfer_buffer);
263 // allocate memory for write_urb transfer buffer
264 port->write_urb->transfer_buffer = kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
265 if (! port->write_urb->transfer_buffer) {
266 kfree(transfer_buffer);
267 usb_free_urb(port->write_urb);
268 port->write_urb = NULL;
272 // get hardware version
273 result = usb_control_msg( port->serial->dev,
274 usb_rcvctrlpipe(port->serial->dev, 0 ),
275 SUSBCRequest_GetMisc,
276 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
277 SUSBCR_MSC_GetHWVersion,
280 transfer_buffer_length,
283 dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
284 dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
286 // get firmware version
287 result = usb_control_msg( port->serial->dev,
288 usb_rcvctrlpipe(port->serial->dev, 0 ),
289 SUSBCRequest_GetMisc,
290 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
291 SUSBCR_MSC_GetFWVersion,
294 transfer_buffer_length,
297 dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
298 dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
300 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
301 // Setting Baudrate, Parity and Stopbits
302 result = usb_control_msg( port->serial->dev,
303 usb_rcvctrlpipe(port->serial->dev, 0 ),
304 SUSBCRequest_SetBaudRateParityAndStopBits,
305 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
306 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
312 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
315 result = usb_control_msg( port->serial->dev,
316 usb_rcvctrlpipe(port->serial->dev, 0 ),
318 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
319 SUSBCR_MSC_ResetAllQueues,
325 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
327 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
328 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
329 // start reading (Adapter B 'cause PNP string)
330 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
331 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
334 kfree(transfer_buffer);
339 static void kobil_close (struct usb_serial_port *port, struct file *filp)
341 dbg("%s - port %d", __FUNCTION__, port->number);
343 if (port->write_urb) {
344 usb_kill_urb(port->write_urb);
345 usb_free_urb( port->write_urb );
346 port->write_urb = NULL;
348 usb_kill_urb(port->interrupt_in_urb);
352 static void kobil_read_int_callback(struct urb *urb)
355 struct usb_serial_port *port = urb->context;
356 struct tty_struct *tty;
357 unsigned char *data = urb->transfer_buffer;
358 int status = urb->status;
361 dbg("%s - port %d", __FUNCTION__, port->number);
364 dbg("%s - port %d Read int status not zero: %d",
365 __FUNCTION__, port->number, status);
370 if (urb->actual_length) {
374 dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
378 for (i = 0; i < purb->actual_length; i++) {
379 sprintf(dbg_data +3*i, "%02X ", data[i]);
381 dbg(" <-- %s", dbg_data );
386 tty_buffer_request_room(tty, urb->actual_length);
387 tty_insert_flip_string(tty, data, urb->actual_length);
388 tty_flip_buffer_push(tty);
391 // someone sets the dev to 0 if the close method has been called
392 port->interrupt_in_urb->dev = port->serial->dev;
394 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
395 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
399 static void kobil_write_callback( struct urb *purb )
404 static int kobil_write (struct usb_serial_port *port,
405 const unsigned char *buf, int count)
410 struct kobil_private * priv;
413 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
417 priv = usb_get_serial_port_data(port);
419 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
420 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
424 // Copy data to buffer
425 memcpy (priv->buf + priv->filled, buf, count);
427 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
429 priv->filled = priv->filled + count;
432 // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
433 if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
434 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
436 // stop reading (except TWIN and KAAN SIM)
437 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
438 usb_kill_urb(port->interrupt_in_urb);
440 todo = priv->filled - priv->cur_pos;
443 // max 8 byte in one urb (endpoint size)
444 length = (todo < 8) ? todo : 8;
445 // copy data to transfer buffer
446 memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
447 usb_fill_int_urb( port->write_urb,
449 usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
450 port->write_urb->transfer_buffer,
452 kobil_write_callback,
457 priv->cur_pos = priv->cur_pos + length;
458 result = usb_submit_urb( port->write_urb, GFP_NOIO );
459 dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
460 todo = priv->filled - priv->cur_pos;
471 // someone sets the dev to 0 if the close method has been called
472 port->interrupt_in_urb->dev = port->serial->dev;
474 // start reading (except TWIN and KAAN SIM)
475 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
476 // someone sets the dev to 0 if the close method has been called
477 port->interrupt_in_urb->dev = port->serial->dev;
479 result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
480 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
487 static int kobil_write_room (struct usb_serial_port *port)
489 //dbg("%s - port %d", __FUNCTION__, port->number);
494 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
496 struct kobil_private * priv;
498 unsigned char *transfer_buffer;
499 int transfer_buffer_length = 8;
501 priv = usb_get_serial_port_data(port);
502 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
503 // This device doesn't support ioctl calls
507 // allocate memory for transfer buffer
508 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
509 if (!transfer_buffer) {
513 result = usb_control_msg( port->serial->dev,
514 usb_rcvctrlpipe(port->serial->dev, 0 ),
515 SUSBCRequest_GetStatusLineState,
516 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
520 transfer_buffer_length,
523 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
524 __FUNCTION__, port->number, result, transfer_buffer[0]);
526 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) {
527 priv->line_state |= TIOCM_DSR;
529 priv->line_state &= ~TIOCM_DSR;
532 kfree(transfer_buffer);
533 return priv->line_state;
536 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
537 unsigned int set, unsigned int clear)
539 struct kobil_private * priv;
543 unsigned char *transfer_buffer;
544 int transfer_buffer_length = 8;
546 priv = usb_get_serial_port_data(port);
547 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
548 // This device doesn't support ioctl calls
552 // allocate memory for transfer buffer
553 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
554 if (! transfer_buffer) {
562 if (clear & TIOCM_RTS)
564 if (clear & TIOCM_DTR)
567 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
569 dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
571 dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
572 result = usb_control_msg( port->serial->dev,
573 usb_rcvctrlpipe(port->serial->dev, 0 ),
574 SUSBCRequest_SetStatusLinesOrQueues,
575 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
576 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
583 dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
585 dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
586 result = usb_control_msg( port->serial->dev,
587 usb_rcvctrlpipe(port->serial->dev, 0 ),
588 SUSBCRequest_SetStatusLinesOrQueues,
589 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
590 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
596 dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
597 kfree(transfer_buffer);
598 return (result < 0) ? result : 0;
601 static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old)
603 struct kobil_private * priv;
605 unsigned short urb_val = 0;
606 int c_cflag = port->tty->termios->c_cflag;
610 priv = usb_get_serial_port_data(port);
611 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
612 // This device doesn't support ioctl calls
615 switch (speed = tty_get_baud_rate(port->tty)) {
617 urb_val = SUSBCR_SBR_1200;
622 urb_val = SUSBCR_SBR_9600;
625 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
627 settings = kzalloc(50, GFP_KERNEL);
631 sprintf(settings, "%d ", speed);
633 if (c_cflag & PARENB) {
634 if (c_cflag & PARODD) {
635 urb_val |= SUSBCR_SPASB_OddParity;
636 strcat(settings, "Odd Parity");
638 urb_val |= SUSBCR_SPASB_EvenParity;
639 strcat(settings, "Even Parity");
642 urb_val |= SUSBCR_SPASB_NoParity;
643 strcat(settings, "No Parity");
645 port->tty->termios->c_cflag &= ~CMSPAR;
646 tty_encode_baud_rate(port->tty, speed, speed);
648 result = usb_control_msg( port->serial->dev,
649 usb_rcvctrlpipe(port->serial->dev, 0 ),
650 SUSBCRequest_SetBaudRateParityAndStopBits,
651 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
661 static int kobil_ioctl(struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
663 struct kobil_private * priv = usb_get_serial_port_data(port);
664 unsigned char *transfer_buffer;
665 int transfer_buffer_length = 8;
668 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
669 // This device doesn't support ioctl calls
673 case TCFLSH: // 0x540B
674 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
675 if (! transfer_buffer)
678 result = usb_control_msg( port->serial->dev,
679 usb_rcvctrlpipe(port->serial->dev, 0 ),
681 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
682 SUSBCR_MSC_ResetAllQueues,
684 NULL,//transfer_buffer,
689 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
690 kfree(transfer_buffer);
691 return (result < 0) ? -EFAULT : 0;
697 static int __init kobil_init (void)
700 retval = usb_serial_register(&kobil_device);
702 goto failed_usb_serial_register;
703 retval = usb_register(&kobil_driver);
705 goto failed_usb_register;
707 info(DRIVER_VERSION " " DRIVER_AUTHOR);
712 usb_serial_deregister(&kobil_device);
713 failed_usb_serial_register:
718 static void __exit kobil_exit (void)
720 usb_deregister (&kobil_driver);
721 usb_serial_deregister (&kobil_device);
724 module_init(kobil_init);
725 module_exit(kobil_exit);
727 MODULE_AUTHOR( DRIVER_AUTHOR );
728 MODULE_DESCRIPTION( DRIVER_DESC );
729 MODULE_LICENSE( "GPL" );
731 module_param(debug, bool, S_IRUGO | S_IWUSR);
732 MODULE_PARM_DESC(debug, "Debug enabled or not");