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)
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/tty.h>
31 #include <linux/tty_driver.h>
32 #include <linux/tty_flip.h>
33 #include <linux/module.h>
34 #include <linux/spinlock.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
38 #include <linux/ioctl.h>
39 #include "kobil_sct.h"
43 /* Version Information */
44 #define DRIVER_VERSION "21/05/2004"
45 #define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
46 #define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
48 #define KOBIL_VENDOR_ID 0x0D46
49 #define KOBIL_ADAPTER_B_PRODUCT_ID 0x2011
50 #define KOBIL_ADAPTER_K_PRODUCT_ID 0x2012
51 #define KOBIL_USBTWIN_PRODUCT_ID 0x0078
52 #define KOBIL_KAAN_SIM_PRODUCT_ID 0x0081
54 #define KOBIL_TIMEOUT 500
55 #define KOBIL_BUF_LENGTH 300
58 /* Function prototypes */
59 static int kobil_port_probe(struct usb_serial_port
*probe
);
60 static int kobil_port_remove(struct usb_serial_port
*probe
);
61 static int kobil_open(struct tty_struct
*tty
, struct usb_serial_port
*port
);
62 static void kobil_close(struct usb_serial_port
*port
);
63 static int kobil_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
64 const unsigned char *buf
, int count
);
65 static int kobil_write_room(struct tty_struct
*tty
);
66 static int kobil_ioctl(struct tty_struct
*tty
,
67 unsigned int cmd
, unsigned long arg
);
68 static int kobil_tiocmget(struct tty_struct
*tty
);
69 static int kobil_tiocmset(struct tty_struct
*tty
,
70 unsigned int set
, unsigned int clear
);
71 static void kobil_read_int_callback(struct urb
*urb
);
72 static void kobil_write_callback(struct urb
*purb
);
73 static void kobil_set_termios(struct tty_struct
*tty
,
74 struct usb_serial_port
*port
, struct ktermios
*old
);
75 static void kobil_init_termios(struct tty_struct
*tty
);
77 static const struct usb_device_id id_table
[] = {
78 { USB_DEVICE(KOBIL_VENDOR_ID
, KOBIL_ADAPTER_B_PRODUCT_ID
) },
79 { USB_DEVICE(KOBIL_VENDOR_ID
, KOBIL_ADAPTER_K_PRODUCT_ID
) },
80 { USB_DEVICE(KOBIL_VENDOR_ID
, KOBIL_USBTWIN_PRODUCT_ID
) },
81 { USB_DEVICE(KOBIL_VENDOR_ID
, KOBIL_KAAN_SIM_PRODUCT_ID
) },
82 { } /* Terminating entry */
84 MODULE_DEVICE_TABLE(usb
, id_table
);
86 static struct usb_serial_driver kobil_device
= {
91 .description
= "KOBIL USB smart card terminal",
94 .port_probe
= kobil_port_probe
,
95 .port_remove
= kobil_port_remove
,
97 .set_termios
= kobil_set_termios
,
98 .init_termios
= kobil_init_termios
,
99 .tiocmget
= kobil_tiocmget
,
100 .tiocmset
= kobil_tiocmset
,
102 .close
= kobil_close
,
103 .write
= kobil_write
,
104 .write_room
= kobil_write_room
,
105 .read_int_callback
= kobil_read_int_callback
,
108 static struct usb_serial_driver
* const serial_drivers
[] = {
112 struct kobil_private
{
113 int write_int_endpoint_address
;
114 int read_int_endpoint_address
;
115 unsigned char buf
[KOBIL_BUF_LENGTH
]; /* buffer for the APDU to send */
116 int filled
; /* index of the last char in buf */
117 int cur_pos
; /* index of the next char to send in buf */
122 static int kobil_port_probe(struct usb_serial_port
*port
)
125 struct usb_serial
*serial
= port
->serial
;
126 struct kobil_private
*priv
;
127 struct usb_device
*pdev
;
128 struct usb_host_config
*actconfig
;
129 struct usb_interface
*interface
;
130 struct usb_host_interface
*altsetting
;
131 struct usb_host_endpoint
*endpoint
;
133 priv
= kmalloc(sizeof(struct kobil_private
), GFP_KERNEL
);
139 priv
->device_type
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
141 switch (priv
->device_type
) {
142 case KOBIL_ADAPTER_B_PRODUCT_ID
:
143 printk(KERN_DEBUG
"KOBIL B1 PRO / KAAN PRO detected\n");
145 case KOBIL_ADAPTER_K_PRODUCT_ID
:
147 "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
149 case KOBIL_USBTWIN_PRODUCT_ID
:
150 printk(KERN_DEBUG
"KOBIL USBTWIN detected\n");
152 case KOBIL_KAAN_SIM_PRODUCT_ID
:
153 printk(KERN_DEBUG
"KOBIL KAAN SIM detected\n");
156 usb_set_serial_port_data(port
, priv
);
158 /* search for the necessary endpoints */
160 actconfig
= pdev
->actconfig
;
161 interface
= actconfig
->interface
[0];
162 altsetting
= interface
->cur_altsetting
;
163 endpoint
= altsetting
->endpoint
;
165 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
166 endpoint
= &altsetting
->endpoint
[i
];
167 if (usb_endpoint_is_int_out(&endpoint
->desc
)) {
168 dbg("%s Found interrupt out endpoint. Address: %d",
169 __func__
, endpoint
->desc
.bEndpointAddress
);
170 priv
->write_int_endpoint_address
=
171 endpoint
->desc
.bEndpointAddress
;
173 if (usb_endpoint_is_int_in(&endpoint
->desc
)) {
174 dbg("%s Found interrupt in endpoint. Address: %d",
175 __func__
, endpoint
->desc
.bEndpointAddress
);
176 priv
->read_int_endpoint_address
=
177 endpoint
->desc
.bEndpointAddress
;
184 static int kobil_port_remove(struct usb_serial_port
*port
)
186 struct kobil_private
*priv
;
188 priv
= usb_get_serial_port_data(port
);
194 static void kobil_init_termios(struct tty_struct
*tty
)
196 /* Default to echo off and other sane device settings */
197 tty
->termios
->c_lflag
= 0;
198 tty
->termios
->c_lflag
&= ~(ISIG
| ICANON
| ECHO
| IEXTEN
| XCASE
);
199 tty
->termios
->c_iflag
= IGNBRK
| IGNPAR
| IXOFF
;
200 /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
201 tty
->termios
->c_oflag
&= ~ONLCR
;
204 static int kobil_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
207 struct kobil_private
*priv
;
208 unsigned char *transfer_buffer
;
209 int transfer_buffer_length
= 8;
210 int write_urb_transfer_buffer_length
= 8;
212 priv
= usb_get_serial_port_data(port
);
214 /* allocate memory for transfer buffer */
215 transfer_buffer
= kzalloc(transfer_buffer_length
, GFP_KERNEL
);
216 if (!transfer_buffer
)
219 /* allocate write_urb */
220 if (!port
->write_urb
) {
221 dbg("%s - port %d Allocating port->write_urb",
222 __func__
, port
->number
);
223 port
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
224 if (!port
->write_urb
) {
225 dbg("%s - port %d usb_alloc_urb failed",
226 __func__
, port
->number
);
227 kfree(transfer_buffer
);
232 /* allocate memory for write_urb transfer buffer */
233 port
->write_urb
->transfer_buffer
=
234 kmalloc(write_urb_transfer_buffer_length
, GFP_KERNEL
);
235 if (!port
->write_urb
->transfer_buffer
) {
236 kfree(transfer_buffer
);
237 usb_free_urb(port
->write_urb
);
238 port
->write_urb
= NULL
;
242 /* get hardware version */
243 result
= usb_control_msg(port
->serial
->dev
,
244 usb_rcvctrlpipe(port
->serial
->dev
, 0),
245 SUSBCRequest_GetMisc
,
246 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_IN
,
247 SUSBCR_MSC_GetHWVersion
,
250 transfer_buffer_length
,
253 dbg("%s - port %d Send get_HW_version URB returns: %i",
254 __func__
, port
->number
, result
);
255 dbg("Harware version: %i.%i.%i",
256 transfer_buffer
[0], transfer_buffer
[1], transfer_buffer
[2]);
258 /* get firmware version */
259 result
= usb_control_msg(port
->serial
->dev
,
260 usb_rcvctrlpipe(port
->serial
->dev
, 0),
261 SUSBCRequest_GetMisc
,
262 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_IN
,
263 SUSBCR_MSC_GetFWVersion
,
266 transfer_buffer_length
,
269 dbg("%s - port %d Send get_FW_version URB returns: %i",
270 __func__
, port
->number
, result
);
271 dbg("Firmware version: %i.%i.%i",
272 transfer_buffer
[0], transfer_buffer
[1], transfer_buffer
[2]);
274 if (priv
->device_type
== KOBIL_ADAPTER_B_PRODUCT_ID
||
275 priv
->device_type
== KOBIL_ADAPTER_K_PRODUCT_ID
) {
276 /* Setting Baudrate, Parity and Stopbits */
277 result
= usb_control_msg(port
->serial
->dev
,
278 usb_rcvctrlpipe(port
->serial
->dev
, 0),
279 SUSBCRequest_SetBaudRateParityAndStopBits
,
280 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
281 SUSBCR_SBR_9600
| SUSBCR_SPASB_EvenParity
|
282 SUSBCR_SPASB_1StopBit
,
288 dbg("%s - port %d Send set_baudrate URB returns: %i",
289 __func__
, port
->number
, result
);
291 /* reset all queues */
292 result
= usb_control_msg(port
->serial
->dev
,
293 usb_rcvctrlpipe(port
->serial
->dev
, 0),
295 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
296 SUSBCR_MSC_ResetAllQueues
,
302 dbg("%s - port %d Send reset_all_queues URB returns: %i",
303 __func__
, port
->number
, result
);
305 if (priv
->device_type
== KOBIL_USBTWIN_PRODUCT_ID
||
306 priv
->device_type
== KOBIL_ADAPTER_B_PRODUCT_ID
||
307 priv
->device_type
== KOBIL_KAAN_SIM_PRODUCT_ID
) {
308 /* start reading (Adapter B 'cause PNP string) */
309 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
310 dbg("%s - port %d Send read URB returns: %i",
311 __func__
, port
->number
, result
);
314 kfree(transfer_buffer
);
319 static void kobil_close(struct usb_serial_port
*port
)
321 /* FIXME: Add rts/dtr methods */
322 if (port
->write_urb
) {
323 usb_poison_urb(port
->write_urb
);
324 kfree(port
->write_urb
->transfer_buffer
);
325 usb_free_urb(port
->write_urb
);
326 port
->write_urb
= NULL
;
328 usb_kill_urb(port
->interrupt_in_urb
);
332 static void kobil_read_int_callback(struct urb
*urb
)
335 struct usb_serial_port
*port
= urb
->context
;
336 struct tty_struct
*tty
;
337 unsigned char *data
= urb
->transfer_buffer
;
338 int status
= urb
->status
;
339 /* char *dbg_data; */
342 dbg("%s - port %d Read int status not zero: %d",
343 __func__
, port
->number
, status
);
347 tty
= tty_port_tty_get(&port
->port
);
348 if (tty
&& urb
->actual_length
) {
352 dbg_data = kzalloc((3 * purb->actual_length + 10)
353 * sizeof(char), GFP_KERNEL);
357 for (i = 0; i < purb->actual_length; i++) {
358 sprintf(dbg_data +3*i, "%02X ", data[i]);
360 dbg(" <-- %s", dbg_data);
365 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
366 tty_flip_buffer_push(tty
);
370 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
371 dbg("%s - port %d Send read URB returns: %i",
372 __func__
, port
->number
, result
);
376 static void kobil_write_callback(struct urb
*purb
)
381 static int kobil_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
382 const unsigned char *buf
, int count
)
387 struct kobil_private
*priv
;
390 dbg("%s - port %d write request of 0 bytes",
391 __func__
, port
->number
);
395 priv
= usb_get_serial_port_data(port
);
397 if (count
> (KOBIL_BUF_LENGTH
- priv
->filled
)) {
398 dbg("%s - port %d Error: write request bigger than buffer size", __func__
, port
->number
);
402 /* Copy data to buffer */
403 memcpy(priv
->buf
+ priv
->filled
, buf
, count
);
404 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
,
405 priv
->buf
+ priv
->filled
);
406 priv
->filled
= priv
->filled
+ count
;
408 /* only send complete block. TWIN, KAAN SIM and adapter K
409 use the same protocol. */
410 if (((priv
->device_type
!= KOBIL_ADAPTER_B_PRODUCT_ID
) && (priv
->filled
> 2) && (priv
->filled
>= (priv
->buf
[1] + 3))) ||
411 ((priv
->device_type
== KOBIL_ADAPTER_B_PRODUCT_ID
) && (priv
->filled
> 3) && (priv
->filled
>= (priv
->buf
[2] + 4)))) {
412 /* stop reading (except TWIN and KAAN SIM) */
413 if ((priv
->device_type
== KOBIL_ADAPTER_B_PRODUCT_ID
)
414 || (priv
->device_type
== KOBIL_ADAPTER_K_PRODUCT_ID
))
415 usb_kill_urb(port
->interrupt_in_urb
);
417 todo
= priv
->filled
- priv
->cur_pos
;
420 /* max 8 byte in one urb (endpoint size) */
421 length
= (todo
< 8) ? todo
: 8;
422 /* copy data to transfer buffer */
423 memcpy(port
->write_urb
->transfer_buffer
,
424 priv
->buf
+ priv
->cur_pos
, length
);
425 usb_fill_int_urb(port
->write_urb
,
427 usb_sndintpipe(port
->serial
->dev
,
428 priv
->write_int_endpoint_address
),
429 port
->write_urb
->transfer_buffer
,
431 kobil_write_callback
,
436 priv
->cur_pos
= priv
->cur_pos
+ length
;
437 result
= usb_submit_urb(port
->write_urb
, GFP_NOIO
);
438 dbg("%s - port %d Send write URB returns: %i",
439 __func__
, port
->number
, result
);
440 todo
= priv
->filled
- priv
->cur_pos
;
449 /* start reading (except TWIN and KAAN SIM) */
450 if (priv
->device_type
== KOBIL_ADAPTER_B_PRODUCT_ID
||
451 priv
->device_type
== KOBIL_ADAPTER_K_PRODUCT_ID
) {
452 result
= usb_submit_urb(port
->interrupt_in_urb
,
454 dbg("%s - port %d Send read URB returns: %i",
455 __func__
, port
->number
, result
);
462 static int kobil_write_room(struct tty_struct
*tty
)
469 static int kobil_tiocmget(struct tty_struct
*tty
)
471 struct usb_serial_port
*port
= tty
->driver_data
;
472 struct kobil_private
*priv
;
474 unsigned char *transfer_buffer
;
475 int transfer_buffer_length
= 8;
477 priv
= usb_get_serial_port_data(port
);
478 if (priv
->device_type
== KOBIL_USBTWIN_PRODUCT_ID
479 || priv
->device_type
== KOBIL_KAAN_SIM_PRODUCT_ID
) {
480 /* This device doesn't support ioctl calls */
484 /* allocate memory for transfer buffer */
485 transfer_buffer
= kzalloc(transfer_buffer_length
, GFP_KERNEL
);
486 if (!transfer_buffer
)
489 result
= usb_control_msg(port
->serial
->dev
,
490 usb_rcvctrlpipe(port
->serial
->dev
, 0),
491 SUSBCRequest_GetStatusLineState
,
492 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_IN
,
496 transfer_buffer_length
,
499 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
500 __func__
, port
->number
, result
, transfer_buffer
[0]);
503 if ((transfer_buffer
[0] & SUSBCR_GSL_DSR
) != 0)
505 kfree(transfer_buffer
);
509 static int kobil_tiocmset(struct tty_struct
*tty
,
510 unsigned int set
, unsigned int clear
)
512 struct usb_serial_port
*port
= tty
->driver_data
;
513 struct kobil_private
*priv
;
517 unsigned char *transfer_buffer
;
518 int transfer_buffer_length
= 8;
520 /* FIXME: locking ? */
521 priv
= usb_get_serial_port_data(port
);
522 if (priv
->device_type
== KOBIL_USBTWIN_PRODUCT_ID
523 || priv
->device_type
== KOBIL_KAAN_SIM_PRODUCT_ID
) {
524 /* This device doesn't support ioctl calls */
528 /* allocate memory for transfer buffer */
529 transfer_buffer
= kzalloc(transfer_buffer_length
, GFP_KERNEL
);
530 if (!transfer_buffer
)
537 if (clear
& TIOCM_RTS
)
539 if (clear
& TIOCM_DTR
)
542 if (priv
->device_type
== KOBIL_ADAPTER_B_PRODUCT_ID
) {
544 dbg("%s - port %d Setting DTR",
545 __func__
, port
->number
);
547 dbg("%s - port %d Clearing DTR",
548 __func__
, port
->number
);
549 result
= usb_control_msg(port
->serial
->dev
,
550 usb_rcvctrlpipe(port
->serial
->dev
, 0),
551 SUSBCRequest_SetStatusLinesOrQueues
,
552 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
553 ((dtr
!= 0) ? SUSBCR_SSL_SETDTR
: SUSBCR_SSL_CLRDTR
),
560 dbg("%s - port %d Setting RTS",
561 __func__
, port
->number
);
563 dbg("%s - port %d Clearing RTS",
564 __func__
, port
->number
);
565 result
= usb_control_msg(port
->serial
->dev
,
566 usb_rcvctrlpipe(port
->serial
->dev
, 0),
567 SUSBCRequest_SetStatusLinesOrQueues
,
568 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
569 ((rts
!= 0) ? SUSBCR_SSL_SETRTS
: SUSBCR_SSL_CLRRTS
),
575 dbg("%s - port %d Send set_status_line URB returns: %i",
576 __func__
, port
->number
, result
);
577 kfree(transfer_buffer
);
578 return (result
< 0) ? result
: 0;
581 static void kobil_set_termios(struct tty_struct
*tty
,
582 struct usb_serial_port
*port
, struct ktermios
*old
)
584 struct kobil_private
*priv
;
586 unsigned short urb_val
= 0;
587 int c_cflag
= tty
->termios
->c_cflag
;
590 priv
= usb_get_serial_port_data(port
);
591 if (priv
->device_type
== KOBIL_USBTWIN_PRODUCT_ID
||
592 priv
->device_type
== KOBIL_KAAN_SIM_PRODUCT_ID
) {
593 /* This device doesn't support ioctl calls */
594 *tty
->termios
= *old
;
598 speed
= tty_get_baud_rate(tty
);
601 urb_val
= SUSBCR_SBR_1200
;
606 urb_val
= SUSBCR_SBR_9600
;
609 urb_val
|= (c_cflag
& CSTOPB
) ? SUSBCR_SPASB_2StopBits
:
610 SUSBCR_SPASB_1StopBit
;
611 if (c_cflag
& PARENB
) {
612 if (c_cflag
& PARODD
)
613 urb_val
|= SUSBCR_SPASB_OddParity
;
615 urb_val
|= SUSBCR_SPASB_EvenParity
;
617 urb_val
|= SUSBCR_SPASB_NoParity
;
618 tty
->termios
->c_cflag
&= ~CMSPAR
;
619 tty_encode_baud_rate(tty
, speed
, speed
);
621 result
= usb_control_msg(port
->serial
->dev
,
622 usb_rcvctrlpipe(port
->serial
->dev
, 0),
623 SUSBCRequest_SetBaudRateParityAndStopBits
,
624 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
633 static int kobil_ioctl(struct tty_struct
*tty
,
634 unsigned int cmd
, unsigned long arg
)
636 struct usb_serial_port
*port
= tty
->driver_data
;
637 struct kobil_private
*priv
= usb_get_serial_port_data(port
);
638 unsigned char *transfer_buffer
;
639 int transfer_buffer_length
= 8;
642 if (priv
->device_type
== KOBIL_USBTWIN_PRODUCT_ID
||
643 priv
->device_type
== KOBIL_KAAN_SIM_PRODUCT_ID
)
644 /* This device doesn't support ioctl calls */
649 transfer_buffer
= kmalloc(transfer_buffer_length
, GFP_KERNEL
);
650 if (!transfer_buffer
)
653 result
= usb_control_msg(port
->serial
->dev
,
654 usb_rcvctrlpipe(port
->serial
->dev
, 0),
656 USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
657 SUSBCR_MSC_ResetAllQueues
,
659 NULL
, /* transfer_buffer, */
664 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__
, port
->number
, result
);
665 kfree(transfer_buffer
);
666 return (result
< 0) ? -EIO
: 0;
672 module_usb_serial_driver(serial_drivers
, id_table
);
674 MODULE_AUTHOR(DRIVER_AUTHOR
);
675 MODULE_DESCRIPTION(DRIVER_DESC
);
676 MODULE_LICENSE("GPL");
678 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
679 MODULE_PARM_DESC(debug
, "Debug enabled or not");