blob: 6db0e561f6805c25a032797ffd1e25bf57c5fa18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * KOBIL USB Smart Card Terminal Driver
3 *
Alan Coxdee0a7c2008-07-22 11:14:10 +01004 * Copyright (C) 2002 KOBIL Systems GmbH
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author: Thomas Wahrenbruch
6 *
7 * Contact: linuxusb@kobil.de
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 * Supported readers: USB TWIN, KAAN Standard Plus and SecOVID Reader Plus
22 * (Adapter K), B1 Professional and KAAN Professional (Adapter B)
Alan Coxdee0a7c2008-07-22 11:14:10 +010023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * (21/05/2004) tw
25 * Fix bug with P'n'P readers
26 *
27 * (28/05/2003) tw
28 * Add support for KAAN SIM
29 *
30 * (12/09/2002) tw
31 * Adapted to 2.5.
32 *
33 * (11/08/2002) tw
34 * Initial version.
35 */
36
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
Alan Coxdee0a7c2008-07-22 11:14:10 +010047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070049#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "kobil_sct.h"
52
53static int debug;
54
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)"
59
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
65
66#define KOBIL_TIMEOUT 500
67#define KOBIL_BUF_LENGTH 300
68
69
70/* Function prototypes */
Alan Coxdee0a7c2008-07-22 11:14:10 +010071static int kobil_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040072static void kobil_release(struct usb_serial *serial);
Alan Coxdee0a7c2008-07-22 11:14:10 +010073static int kobil_open(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +010074 struct usb_serial_port *port, struct file *filp);
Alan Cox335f8512009-06-11 12:26:29 +010075static void kobil_close(struct usb_serial_port *port);
Alan Coxdee0a7c2008-07-22 11:14:10 +010076static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 const unsigned char *buf, int count);
Alan Cox95da3102008-07-22 11:09:07 +010078static int kobil_write_room(struct tty_struct *tty);
79static int kobil_ioctl(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned int cmd, unsigned long arg);
Alan Cox95da3102008-07-22 11:09:07 +010081static int kobil_tiocmget(struct tty_struct *tty, struct file *file);
82static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned int set, unsigned int clear);
Alan Coxdee0a7c2008-07-22 11:14:10 +010084static void kobil_read_int_callback(struct urb *urb);
85static void kobil_write_callback(struct urb *purb);
86static void kobil_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +010087 struct usb_serial_port *port, struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89
90static struct usb_device_id id_table [] = {
91 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
92 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
93 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
94 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
95 { } /* Terminating entry */
96};
97
98
Alan Coxdee0a7c2008-07-22 11:14:10 +010099MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101static struct usb_driver kobil_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .name = "kobil",
103 .probe = usb_serial_probe,
104 .disconnect = usb_serial_disconnect,
105 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800106 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700110static struct usb_serial_driver kobil_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700111 .driver = {
112 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700113 .name = "kobil",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700114 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700115 .description = "KOBIL USB smart card terminal",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100116 .usb_driver = &kobil_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .num_ports = 1,
119 .attach = kobil_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400120 .release = kobil_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .ioctl = kobil_ioctl,
Alan Cox94d0f7e2007-08-22 23:09:16 +0100122 .set_termios = kobil_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .tiocmget = kobil_tiocmget,
124 .tiocmset = kobil_tiocmset,
125 .open = kobil_open,
126 .close = kobil_close,
127 .write = kobil_write,
128 .write_room = kobil_write_room,
129 .read_int_callback = kobil_read_int_callback,
130};
131
132
133struct kobil_private {
134 int write_int_endpoint_address;
135 int read_int_endpoint_address;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100136 unsigned char buf[KOBIL_BUF_LENGTH]; /* buffer for the APDU to send */
137 int filled; /* index of the last char in buf */
138 int cur_pos; /* index of the next char to send in buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 __u16 device_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
142
Alan Coxdee0a7c2008-07-22 11:14:10 +0100143static int kobil_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int i;
146 struct kobil_private *priv;
147 struct usb_device *pdev;
148 struct usb_host_config *actconfig;
149 struct usb_interface *interface;
150 struct usb_host_interface *altsetting;
151 struct usb_host_endpoint *endpoint;
152
153 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100154 if (!priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 priv->filled = 0;
158 priv->cur_pos = 0;
159 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Alan Coxdee0a7c2008-07-22 11:14:10 +0100161 switch (priv->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 case KOBIL_ADAPTER_B_PRODUCT_ID:
163 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
164 break;
165 case KOBIL_ADAPTER_K_PRODUCT_ID:
Alan Coxdee0a7c2008-07-22 11:14:10 +0100166 printk(KERN_DEBUG
167 "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 break;
169 case KOBIL_USBTWIN_PRODUCT_ID:
170 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
171 break;
172 case KOBIL_KAAN_SIM_PRODUCT_ID:
173 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
174 break;
175 }
176 usb_set_serial_port_data(serial->port[0], priv);
177
Alan Coxdee0a7c2008-07-22 11:14:10 +0100178 /* search for the necessary endpoints */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 pdev = serial->dev;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100180 actconfig = pdev->actconfig;
181 interface = actconfig->interface[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 altsetting = interface->cur_altsetting;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100183 endpoint = altsetting->endpoint;
184
185 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 endpoint = &altsetting->endpoint[i];
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300187 if (usb_endpoint_is_int_out(&endpoint->desc)) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100188 dbg("%s Found interrupt out endpoint. Address: %d",
189 __func__, endpoint->desc.bEndpointAddress);
190 priv->write_int_endpoint_address =
191 endpoint->desc.bEndpointAddress;
192 }
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300193 if (usb_endpoint_is_int_in(&endpoint->desc)) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100194 dbg("%s Found interrupt in endpoint. Address: %d",
195 __func__, endpoint->desc.bEndpointAddress);
196 priv->read_int_endpoint_address =
197 endpoint->desc.bEndpointAddress;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 return 0;
201}
202
203
Alan Sternf9c99bb2009-06-02 11:53:55 -0400204static void kobil_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int i;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800207 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Alan Sternf9c99bb2009-06-02 11:53:55 -0400209 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 kfree(usb_get_serial_port_data(serial->port[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213
Alan Cox95da3102008-07-22 11:09:07 +0100214static int kobil_open(struct tty_struct *tty,
215 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Alan Cox94d0f7e2007-08-22 23:09:16 +0100217 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct kobil_private *priv;
219 unsigned char *transfer_buffer;
220 int transfer_buffer_length = 8;
221 int write_urb_transfer_buffer_length = 8;
222
Harvey Harrison441b62c2008-03-03 16:08:34 -0800223 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Alan Coxdee0a7c2008-07-22 11:14:10 +0100226 /* someone sets the dev to 0 if the close method has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 port->interrupt_in_urb->dev = port->serial->dev;
228
Alan Cox95da3102008-07-22 11:09:07 +0100229 if (tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alan Cox95da3102008-07-22 11:09:07 +0100231 /* Default to echo off and other sane device settings */
232 tty->termios->c_lflag = 0;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100233 tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN |
234 XCASE);
Alan Cox95da3102008-07-22 11:09:07 +0100235 tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100236 /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
237 tty->termios->c_oflag &= ~ONLCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100239 /* allocate memory for transfer buffer */
240 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
241 if (!transfer_buffer)
242 return -ENOMEM;
243
244 /* allocate write_urb */
245 if (!port->write_urb) {
246 dbg("%s - port %d Allocating port->write_urb",
247 __func__, port->number);
248 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!port->write_urb) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100250 dbg("%s - port %d usb_alloc_urb failed",
251 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 kfree(transfer_buffer);
253 return -ENOMEM;
254 }
255 }
256
Alan Coxdee0a7c2008-07-22 11:14:10 +0100257 /* allocate memory for write_urb transfer buffer */
258 port->write_urb->transfer_buffer =
259 kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
260 if (!port->write_urb->transfer_buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 kfree(transfer_buffer);
262 usb_free_urb(port->write_urb);
263 port->write_urb = NULL;
264 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100266
267 /* get hardware version */
268 result = usb_control_msg(port->serial->dev,
269 usb_rcvctrlpipe(port->serial->dev, 0),
270 SUSBCRequest_GetMisc,
271 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
272 SUSBCR_MSC_GetHWVersion,
273 0,
274 transfer_buffer,
275 transfer_buffer_length,
276 KOBIL_TIMEOUT
277 );
278 dbg("%s - port %d Send get_HW_version URB returns: %i",
279 __func__, port->number, result);
280 dbg("Harware version: %i.%i.%i",
281 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
282
283 /* get firmware version */
284 result = usb_control_msg(port->serial->dev,
285 usb_rcvctrlpipe(port->serial->dev, 0),
286 SUSBCRequest_GetMisc,
287 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
288 SUSBCR_MSC_GetFWVersion,
289 0,
290 transfer_buffer,
291 transfer_buffer_length,
292 KOBIL_TIMEOUT
293 );
294 dbg("%s - port %d Send get_FW_version URB returns: %i",
295 __func__, port->number, result);
296 dbg("Firmware version: %i.%i.%i",
297 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
298
299 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
300 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 |
307 SUSBCR_SPASB_1StopBit,
308 0,
309 transfer_buffer,
310 0,
311 KOBIL_TIMEOUT
312 );
313 dbg("%s - port %d Send set_baudrate URB returns: %i",
314 __func__, port->number, result);
315
316 /* reset all queues */
317 result = usb_control_msg(port->serial->dev,
318 usb_rcvctrlpipe(port->serial->dev, 0),
319 SUSBCRequest_Misc,
320 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
321 SUSBCR_MSC_ResetAllQueues,
322 0,
323 transfer_buffer,
324 0,
325 KOBIL_TIMEOUT
326 );
327 dbg("%s - port %d Send reset_all_queues URB returns: %i",
328 __func__, port->number, result);
329 }
330 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
331 priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100333 /* start reading (Adapter B 'cause PNP string) */
334 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
335 dbg("%s - port %d Send read URB returns: %i",
336 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 kfree(transfer_buffer);
340 return 0;
341}
342
343
Alan Cox335f8512009-06-11 12:26:29 +0100344static void kobil_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800346 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Alan Cox335f8512009-06-11 12:26:29 +0100348 /* FIXME: Add rts/dtr methods */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (port->write_urb) {
350 usb_kill_urb(port->write_urb);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100351 usb_free_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 port->write_urb = NULL;
353 }
Mariusz Kozlowski5505c222006-11-08 15:36:38 +0100354 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700358static void kobil_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int result;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700361 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct tty_struct *tty;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700363 unsigned char *data = urb->transfer_buffer;
364 int status = urb->status;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100365/* char *dbg_data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Harvey Harrison441b62c2008-03-03 16:08:34 -0800367 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700369 if (status) {
370 dbg("%s - port %d Read int status not zero: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800371 __func__, port->number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return;
373 }
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700374
Alan Cox4a90f092008-10-13 10:39:46 +0100375 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700376 if (urb->actual_length) {
377
Alan Coxdee0a7c2008-07-22 11:14:10 +0100378 /* BEGIN DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
Alan Coxdee0a7c2008-07-22 11:14:10 +0100380 dbg_data = kzalloc((3 * purb->actual_length + 10)
381 * sizeof(char), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (! dbg_data) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100383 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100385 for (i = 0; i < purb->actual_length; i++) {
386 sprintf(dbg_data +3*i, "%02X ", data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100388 dbg(" <-- %s", dbg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 kfree(dbg_data);
390 */
Alan Coxdee0a7c2008-07-22 11:14:10 +0100391 /* END DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700393 tty_buffer_request_room(tty, urb->actual_length);
394 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 tty_flip_buffer_push(tty);
396 }
Alan Cox4a90f092008-10-13 10:39:46 +0100397 tty_kref_put(tty);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100398 /* someone sets the dev to 0 if the close method has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 port->interrupt_in_urb->dev = port->serial->dev;
400
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700401 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100402 dbg("%s - port %d Send read URB returns: %i",
403 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406
Alan Coxdee0a7c2008-07-22 11:14:10 +0100407static void kobil_write_callback(struct urb *purb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409}
410
411
Alan Coxdee0a7c2008-07-22 11:14:10 +0100412static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 const unsigned char *buf, int count)
414{
415 int length = 0;
416 int result = 0;
417 int todo = 0;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100418 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (count == 0) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100421 dbg("%s - port %d write request of 0 bytes",
422 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424 }
425
426 priv = usb_get_serial_port_data(port);
427
428 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800429 dbg("%s - port %d Error: write request bigger than buffer size", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -ENOMEM;
431 }
432
Alan Coxdee0a7c2008-07-22 11:14:10 +0100433 /* Copy data to buffer */
434 memcpy(priv->buf + priv->filled, buf, count);
435 usb_serial_debug_data(debug, &port->dev, __func__, count,
436 priv->buf + priv->filled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 priv->filled = priv->filled + count;
438
Alan Coxdee0a7c2008-07-22 11:14:10 +0100439 /* only send complete block. TWIN, KAAN SIM and adapter K
440 use the same protocol. */
441 if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
442 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
443 /* stop reading (except TWIN and KAAN SIM) */
444 if ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID)
445 || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 usb_kill_urb(port->interrupt_in_urb);
447
448 todo = priv->filled - priv->cur_pos;
449
Alan Coxdee0a7c2008-07-22 11:14:10 +0100450 while (todo > 0) {
451 /* max 8 byte in one urb (endpoint size) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 length = (todo < 8) ? todo : 8;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100453 /* copy data to transfer buffer */
454 memcpy(port->write_urb->transfer_buffer,
455 priv->buf + priv->cur_pos, length);
456 usb_fill_int_urb(port->write_urb,
457 port->serial->dev,
458 usb_sndintpipe(port->serial->dev,
459 priv->write_int_endpoint_address),
460 port->write_urb->transfer_buffer,
461 length,
462 kobil_write_callback,
463 port,
464 8
465 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 priv->cur_pos = priv->cur_pos + length;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100468 result = usb_submit_urb(port->write_urb, GFP_NOIO);
469 dbg("%s - port %d Send write URB returns: %i",
470 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 todo = priv->filled - priv->cur_pos;
472
Alan Coxdee0a7c2008-07-22 11:14:10 +0100473 if (todo > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 msleep(24);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 priv->filled = 0;
478 priv->cur_pos = 0;
479
Alan Coxdee0a7c2008-07-22 11:14:10 +0100480 /* someone sets the dev to 0 if the close method
481 has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100483
484 /* start reading (except TWIN and KAAN SIM) */
485 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
486 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
487 /* someone sets the dev to 0 if the close method has
488 been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100490
491 result = usb_submit_urb(port->interrupt_in_urb,
492 GFP_NOIO);
493 dbg("%s - port %d Send read URB returns: %i",
494 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 }
497 return count;
498}
499
500
Alan Coxdee0a7c2008-07-22 11:14:10 +0100501static int kobil_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100503 /* dbg("%s - port %d", __func__, port->number); */
Alan Cox95da3102008-07-22 11:09:07 +0100504 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 8;
506}
507
508
Alan Cox95da3102008-07-22 11:09:07 +0100509static int kobil_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Alan Cox95da3102008-07-22 11:09:07 +0100511 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100512 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int result;
514 unsigned char *transfer_buffer;
515 int transfer_buffer_length = 8;
516
517 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100518 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
519 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
520 /* This device doesn't support ioctl calls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return -EINVAL;
522 }
523
Alan Coxdee0a7c2008-07-22 11:14:10 +0100524 /* allocate memory for transfer buffer */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100525 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100526 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Alan Coxdee0a7c2008-07-22 11:14:10 +0100529 result = usb_control_msg(port->serial->dev,
530 usb_rcvctrlpipe(port->serial->dev, 0),
531 SUSBCRequest_GetStatusLineState,
532 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
533 0,
534 0,
535 transfer_buffer,
536 transfer_buffer_length,
537 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Alan Coxdee0a7c2008-07-22 11:14:10 +0100539 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800540 __func__, port->number, result, transfer_buffer[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Alan Coxa40d8542008-02-20 21:40:34 +0000542 result = 0;
543 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
544 result = TIOCM_DSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 kfree(transfer_buffer);
Alan Coxa40d8542008-02-20 21:40:34 +0000546 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Alan Cox95da3102008-07-22 11:09:07 +0100549static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 unsigned int set, unsigned int clear)
551{
Alan Cox95da3102008-07-22 11:09:07 +0100552 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100553 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int result;
555 int dtr = 0;
556 int rts = 0;
557 unsigned char *transfer_buffer;
558 int transfer_buffer_length = 8;
559
Alan Coxa40d8542008-02-20 21:40:34 +0000560 /* FIXME: locking ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100562 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
563 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
564 /* This device doesn't support ioctl calls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EINVAL;
566 }
567
Alan Coxdee0a7c2008-07-22 11:14:10 +0100568 /* allocate memory for transfer buffer */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100569 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100570 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (set & TIOCM_RTS)
574 rts = 1;
575 if (set & TIOCM_DTR)
576 dtr = 1;
577 if (clear & TIOCM_RTS)
578 rts = 0;
579 if (clear & TIOCM_DTR)
580 dtr = 0;
581
582 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
583 if (dtr != 0)
Alan Coxdee0a7c2008-07-22 11:14:10 +0100584 dbg("%s - port %d Setting DTR",
585 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 else
Alan Coxdee0a7c2008-07-22 11:14:10 +0100587 dbg("%s - port %d Clearing DTR",
588 __func__, port->number);
589 result = usb_control_msg(port->serial->dev,
590 usb_rcvctrlpipe(port->serial->dev, 0),
591 SUSBCRequest_SetStatusLinesOrQueues,
592 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
593 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
594 0,
595 transfer_buffer,
596 0,
597 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 } else {
599 if (rts != 0)
Alan Coxdee0a7c2008-07-22 11:14:10 +0100600 dbg("%s - port %d Setting RTS",
601 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 else
Alan Coxdee0a7c2008-07-22 11:14:10 +0100603 dbg("%s - port %d Clearing RTS",
604 __func__, port->number);
605 result = usb_control_msg(port->serial->dev,
606 usb_rcvctrlpipe(port->serial->dev, 0),
607 SUSBCRequest_SetStatusLinesOrQueues,
608 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
609 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
610 0,
611 transfer_buffer,
612 0,
613 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100615 dbg("%s - port %d Send set_status_line URB returns: %i",
616 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 kfree(transfer_buffer);
618 return (result < 0) ? result : 0;
619}
620
Alan Cox95da3102008-07-22 11:09:07 +0100621static void kobil_set_termios(struct tty_struct *tty,
622 struct usb_serial_port *port, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100624 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int result;
626 unsigned short urb_val = 0;
Alan Cox95da3102008-07-22 11:09:07 +0100627 int c_cflag = tty->termios->c_cflag;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100628 speed_t speed;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100629 void *settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100632 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
Alan Coxb31f6582008-07-22 11:14:22 +0100633 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100634 /* This device doesn't support ioctl calls */
Alan Coxb31f6582008-07-22 11:14:22 +0100635 *tty->termios = *old;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100636 return;
Alan Coxb31f6582008-07-22 11:14:22 +0100637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Alan Coxdee0a7c2008-07-22 11:14:10 +0100639 speed = tty_get_baud_rate(tty);
640 switch (speed) {
641 case 1200:
642 urb_val = SUSBCR_SBR_1200;
643 break;
644 default:
645 speed = 9600;
646 case 9600:
647 urb_val = SUSBCR_SBR_9600;
648 break;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100649 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100650 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits :
651 SUSBCR_SPASB_1StopBit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Alan Cox94d0f7e2007-08-22 23:09:16 +0100653 settings = kzalloc(50, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100654 if (!settings)
Alan Cox94d0f7e2007-08-22 23:09:16 +0100655 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Alan Cox94d0f7e2007-08-22 23:09:16 +0100657 sprintf(settings, "%d ", speed);
658
659 if (c_cflag & PARENB) {
660 if (c_cflag & PARODD) {
661 urb_val |= SUSBCR_SPASB_OddParity;
662 strcat(settings, "Odd Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 } else {
Alan Cox94d0f7e2007-08-22 23:09:16 +0100664 urb_val |= SUSBCR_SPASB_EvenParity;
665 strcat(settings, "Even Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Alan Cox94d0f7e2007-08-22 23:09:16 +0100667 } else {
668 urb_val |= SUSBCR_SPASB_NoParity;
669 strcat(settings, "No Parity");
670 }
Alan Cox95da3102008-07-22 11:09:07 +0100671 tty->termios->c_cflag &= ~CMSPAR;
672 tty_encode_baud_rate(tty, speed, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Alan Coxdee0a7c2008-07-22 11:14:10 +0100674 result = usb_control_msg(port->serial->dev,
675 usb_rcvctrlpipe(port->serial->dev, 0),
676 SUSBCRequest_SetBaudRateParityAndStopBits,
677 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
678 urb_val,
679 0,
680 settings,
681 0,
682 KOBIL_TIMEOUT
Alan Cox94d0f7e2007-08-22 23:09:16 +0100683 );
684 kfree(settings);
685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Alan Coxdee0a7c2008-07-22 11:14:10 +0100687static int kobil_ioctl(struct tty_struct *tty, struct file *file,
688 unsigned int cmd, unsigned long arg)
Alan Cox94d0f7e2007-08-22 23:09:16 +0100689{
Alan Cox95da3102008-07-22 11:09:07 +0100690 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100691 struct kobil_private *priv = usb_get_serial_port_data(port);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100692 unsigned char *transfer_buffer;
693 int transfer_buffer_length = 8;
694 int result;
695
Alan Coxdee0a7c2008-07-22 11:14:10 +0100696 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
697 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
698 /* This device doesn't support ioctl calls */
Alan Coxb31f6582008-07-22 11:14:22 +0100699 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Alan Cox94d0f7e2007-08-22 23:09:16 +0100701 switch (cmd) {
Alan Cox95da3102008-07-22 11:09:07 +0100702 case TCFLSH:
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800703 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100704 if (!transfer_buffer)
705 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Alan Coxdee0a7c2008-07-22 11:14:10 +0100707 result = usb_control_msg(port->serial->dev,
708 usb_rcvctrlpipe(port->serial->dev, 0),
709 SUSBCRequest_Misc,
710 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
711 SUSBCR_MSC_ResetAllQueues,
712 0,
713 NULL, /* transfer_buffer, */
714 0,
715 KOBIL_TIMEOUT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 );
Alan Coxdee0a7c2008-07-22 11:14:10 +0100717
Harvey Harrison441b62c2008-03-03 16:08:34 -0800718 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 kfree(transfer_buffer);
Alan Cox95da3102008-07-22 11:09:07 +0100720 return (result < 0) ? -EIO: 0;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100721 default:
722 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Alan Coxdee0a7c2008-07-22 11:14:10 +0100726static int __init kobil_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 int retval;
729 retval = usb_serial_register(&kobil_device);
730 if (retval)
731 goto failed_usb_serial_register;
732 retval = usb_register(&kobil_driver);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100733 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goto failed_usb_register;
735
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700736 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
737 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 return 0;
740failed_usb_register:
741 usb_serial_deregister(&kobil_device);
742failed_usb_serial_register:
743 return retval;
744}
745
746
Alan Coxdee0a7c2008-07-22 11:14:10 +0100747static void __exit kobil_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100749 usb_deregister(&kobil_driver);
750 usb_serial_deregister(&kobil_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753module_init(kobil_init);
754module_exit(kobil_exit);
755
Alan Coxdee0a7c2008-07-22 11:14:10 +0100756MODULE_AUTHOR(DRIVER_AUTHOR);
757MODULE_DESCRIPTION(DRIVER_DESC);
758MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760module_param(debug, bool, S_IRUGO | S_IWUSR);
761MODULE_PARM_DESC(debug, "Debug enabled or not");