blob: 6286baad9392a7c13dbb9a40d4d770156cee7cb0 [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);
72static void kobil_shutdown(struct usb_serial *serial);
73static int kobil_open(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +010074 struct usb_serial_port *port, struct file *filp);
Alan Coxdee0a7c2008-07-22 11:14:10 +010075static void kobil_close(struct tty_struct *tty, struct usb_serial_port *port,
Alan Cox95da3102008-07-22 11:09:07 +010076 struct file *filp);
Alan Coxdee0a7c2008-07-22 11:14:10 +010077static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 const unsigned char *buf, int count);
Alan Cox95da3102008-07-22 11:09:07 +010079static int kobil_write_room(struct tty_struct *tty);
80static int kobil_ioctl(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned int cmd, unsigned long arg);
Alan Cox95da3102008-07-22 11:09:07 +010082static int kobil_tiocmget(struct tty_struct *tty, struct file *file);
83static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned int set, unsigned int clear);
Alan Coxdee0a7c2008-07-22 11:14:10 +010085static void kobil_read_int_callback(struct urb *urb);
86static void kobil_write_callback(struct urb *purb);
87static void kobil_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +010088 struct usb_serial_port *port, struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90
91static struct usb_device_id id_table [] = {
92 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
93 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
94 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
95 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
96 { } /* Terminating entry */
97};
98
99
Alan Coxdee0a7c2008-07-22 11:14:10 +0100100MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102static struct usb_driver kobil_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .name = "kobil",
104 .probe = usb_serial_probe,
105 .disconnect = usb_serial_disconnect,
106 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800107 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700111static struct usb_serial_driver kobil_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700112 .driver = {
113 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700114 .name = "kobil",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700115 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700116 .description = "KOBIL USB smart card terminal",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100117 .usb_driver = &kobil_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .num_ports = 1,
120 .attach = kobil_startup,
121 .shutdown = kobil_shutdown,
122 .ioctl = kobil_ioctl,
Alan Cox94d0f7e2007-08-22 23:09:16 +0100123 .set_termios = kobil_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .tiocmget = kobil_tiocmget,
125 .tiocmset = kobil_tiocmset,
126 .open = kobil_open,
127 .close = kobil_close,
128 .write = kobil_write,
129 .write_room = kobil_write_room,
130 .read_int_callback = kobil_read_int_callback,
131};
132
133
134struct kobil_private {
135 int write_int_endpoint_address;
136 int read_int_endpoint_address;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100137 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 __u16 device_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143
Alan Coxdee0a7c2008-07-22 11:14:10 +0100144static int kobil_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int i;
147 struct kobil_private *priv;
148 struct usb_device *pdev;
149 struct usb_host_config *actconfig;
150 struct usb_interface *interface;
151 struct usb_host_interface *altsetting;
152 struct usb_host_endpoint *endpoint;
153
154 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100155 if (!priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 priv->filled = 0;
159 priv->cur_pos = 0;
160 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Alan Coxdee0a7c2008-07-22 11:14:10 +0100162 switch (priv->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 case KOBIL_ADAPTER_B_PRODUCT_ID:
164 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
165 break;
166 case KOBIL_ADAPTER_K_PRODUCT_ID:
Alan Coxdee0a7c2008-07-22 11:14:10 +0100167 printk(KERN_DEBUG
168 "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 case KOBIL_USBTWIN_PRODUCT_ID:
171 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
172 break;
173 case KOBIL_KAAN_SIM_PRODUCT_ID:
174 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
175 break;
176 }
177 usb_set_serial_port_data(serial->port[0], priv);
178
Alan Coxdee0a7c2008-07-22 11:14:10 +0100179 /* search for the necessary endpoints */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 pdev = serial->dev;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100181 actconfig = pdev->actconfig;
182 interface = actconfig->interface[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 altsetting = interface->cur_altsetting;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100184 endpoint = altsetting->endpoint;
185
186 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 endpoint = &altsetting->endpoint[i];
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300188 if (usb_endpoint_is_int_out(&endpoint->desc)) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100189 dbg("%s Found interrupt out endpoint. Address: %d",
190 __func__, endpoint->desc.bEndpointAddress);
191 priv->write_int_endpoint_address =
192 endpoint->desc.bEndpointAddress;
193 }
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300194 if (usb_endpoint_is_int_in(&endpoint->desc)) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100195 dbg("%s Found interrupt in endpoint. Address: %d",
196 __func__, endpoint->desc.bEndpointAddress);
197 priv->read_int_endpoint_address =
198 endpoint->desc.bEndpointAddress;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 return 0;
202}
203
204
Alan Coxdee0a7c2008-07-22 11:14:10 +0100205static void kobil_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 int i;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800208 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Alan Coxdee0a7c2008-07-22 11:14:10 +0100210 for (i = 0; i < serial->num_ports; ++i) {
211 while (serial->port[i]->port.count > 0)
212 kobil_close(NULL, serial->port[i], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 kfree(usb_get_serial_port_data(serial->port[i]));
214 usb_set_serial_port_data(serial->port[i], NULL);
215 }
216}
217
218
Alan Cox95da3102008-07-22 11:09:07 +0100219static int kobil_open(struct tty_struct *tty,
220 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Alan Cox94d0f7e2007-08-22 23:09:16 +0100222 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct kobil_private *priv;
224 unsigned char *transfer_buffer;
225 int transfer_buffer_length = 8;
226 int write_urb_transfer_buffer_length = 8;
227
Harvey Harrison441b62c2008-03-03 16:08:34 -0800228 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alan Coxdee0a7c2008-07-22 11:14:10 +0100231 /* someone sets the dev to 0 if the close method has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 port->interrupt_in_urb->dev = port->serial->dev;
233
234
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.
238 */
Alan Cox95da3102008-07-22 11:09:07 +0100239 if (tty) {
240 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Alan Cox95da3102008-07-22 11:09:07 +0100242 /* Default to echo off and other sane device settings */
243 tty->termios->c_lflag = 0;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100244 tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN |
245 XCASE);
Alan Cox95da3102008-07-22 11:09:07 +0100246 tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100247 /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
248 tty->termios->c_oflag &= ~ONLCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100250 /* allocate memory for transfer buffer */
251 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
252 if (!transfer_buffer)
253 return -ENOMEM;
254
255 /* allocate write_urb */
256 if (!port->write_urb) {
257 dbg("%s - port %d Allocating port->write_urb",
258 __func__, port->number);
259 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!port->write_urb) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100261 dbg("%s - port %d usb_alloc_urb failed",
262 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kfree(transfer_buffer);
264 return -ENOMEM;
265 }
266 }
267
Alan Coxdee0a7c2008-07-22 11:14:10 +0100268 /* allocate memory for write_urb transfer buffer */
269 port->write_urb->transfer_buffer =
270 kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
271 if (!port->write_urb->transfer_buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 kfree(transfer_buffer);
273 usb_free_urb(port->write_urb);
274 port->write_urb = NULL;
275 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100277
278 /* get hardware version */
279 result = usb_control_msg(port->serial->dev,
280 usb_rcvctrlpipe(port->serial->dev, 0),
281 SUSBCRequest_GetMisc,
282 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
283 SUSBCR_MSC_GetHWVersion,
284 0,
285 transfer_buffer,
286 transfer_buffer_length,
287 KOBIL_TIMEOUT
288 );
289 dbg("%s - port %d Send get_HW_version URB returns: %i",
290 __func__, port->number, result);
291 dbg("Harware version: %i.%i.%i",
292 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
293
294 /* get firmware version */
295 result = usb_control_msg(port->serial->dev,
296 usb_rcvctrlpipe(port->serial->dev, 0),
297 SUSBCRequest_GetMisc,
298 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
299 SUSBCR_MSC_GetFWVersion,
300 0,
301 transfer_buffer,
302 transfer_buffer_length,
303 KOBIL_TIMEOUT
304 );
305 dbg("%s - port %d Send get_FW_version URB returns: %i",
306 __func__, port->number, result);
307 dbg("Firmware version: %i.%i.%i",
308 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
309
310 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
311 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
312 /* Setting Baudrate, Parity and Stopbits */
313 result = usb_control_msg(port->serial->dev,
314 usb_rcvctrlpipe(port->serial->dev, 0),
315 SUSBCRequest_SetBaudRateParityAndStopBits,
316 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
317 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity |
318 SUSBCR_SPASB_1StopBit,
319 0,
320 transfer_buffer,
321 0,
322 KOBIL_TIMEOUT
323 );
324 dbg("%s - port %d Send set_baudrate URB returns: %i",
325 __func__, port->number, result);
326
327 /* reset all queues */
328 result = usb_control_msg(port->serial->dev,
329 usb_rcvctrlpipe(port->serial->dev, 0),
330 SUSBCRequest_Misc,
331 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
332 SUSBCR_MSC_ResetAllQueues,
333 0,
334 transfer_buffer,
335 0,
336 KOBIL_TIMEOUT
337 );
338 dbg("%s - port %d Send reset_all_queues URB returns: %i",
339 __func__, port->number, result);
340 }
341 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
342 priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100344 /* start reading (Adapter B 'cause PNP string) */
345 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
346 dbg("%s - port %d Send read URB returns: %i",
347 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 kfree(transfer_buffer);
351 return 0;
352}
353
354
Alan Cox95da3102008-07-22 11:09:07 +0100355static void kobil_close(struct tty_struct *tty,
356 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800358 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (port->write_urb) {
361 usb_kill_urb(port->write_urb);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100362 usb_free_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 port->write_urb = NULL;
364 }
Mariusz Kozlowski5505c222006-11-08 15:36:38 +0100365 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700369static void kobil_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int result;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700372 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct tty_struct *tty;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700374 unsigned char *data = urb->transfer_buffer;
375 int status = urb->status;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100376/* char *dbg_data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Harvey Harrison441b62c2008-03-03 16:08:34 -0800378 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700380 if (status) {
381 dbg("%s - port %d Read int status not zero: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800382 __func__, port->number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return;
384 }
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700385
Alan Cox4a90f092008-10-13 10:39:46 +0100386 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700387 if (urb->actual_length) {
388
Alan Coxdee0a7c2008-07-22 11:14:10 +0100389 /* BEGIN DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
Alan Coxdee0a7c2008-07-22 11:14:10 +0100391 dbg_data = kzalloc((3 * purb->actual_length + 10)
392 * sizeof(char), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (! dbg_data) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100394 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100396 for (i = 0; i < purb->actual_length; i++) {
397 sprintf(dbg_data +3*i, "%02X ", data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100399 dbg(" <-- %s", dbg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 kfree(dbg_data);
401 */
Alan Coxdee0a7c2008-07-22 11:14:10 +0100402 /* END DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700404 tty_buffer_request_room(tty, urb->actual_length);
405 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 tty_flip_buffer_push(tty);
407 }
Alan Cox4a90f092008-10-13 10:39:46 +0100408 tty_kref_put(tty);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100409 /* someone sets the dev to 0 if the close method has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 port->interrupt_in_urb->dev = port->serial->dev;
411
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700412 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100413 dbg("%s - port %d Send read URB returns: %i",
414 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417
Alan Coxdee0a7c2008-07-22 11:14:10 +0100418static void kobil_write_callback(struct urb *purb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420}
421
422
Alan Coxdee0a7c2008-07-22 11:14:10 +0100423static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 const unsigned char *buf, int count)
425{
426 int length = 0;
427 int result = 0;
428 int todo = 0;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100429 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (count == 0) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100432 dbg("%s - port %d write request of 0 bytes",
433 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435 }
436
437 priv = usb_get_serial_port_data(port);
438
439 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800440 dbg("%s - port %d Error: write request bigger than buffer size", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -ENOMEM;
442 }
443
Alan Coxdee0a7c2008-07-22 11:14:10 +0100444 /* Copy data to buffer */
445 memcpy(priv->buf + priv->filled, buf, count);
446 usb_serial_debug_data(debug, &port->dev, __func__, count,
447 priv->buf + priv->filled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 priv->filled = priv->filled + count;
449
Alan Coxdee0a7c2008-07-22 11:14:10 +0100450 /* only send complete block. TWIN, KAAN SIM and adapter K
451 use the same protocol. */
452 if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
453 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
454 /* stop reading (except TWIN and KAAN SIM) */
455 if ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID)
456 || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 usb_kill_urb(port->interrupt_in_urb);
458
459 todo = priv->filled - priv->cur_pos;
460
Alan Coxdee0a7c2008-07-22 11:14:10 +0100461 while (todo > 0) {
462 /* max 8 byte in one urb (endpoint size) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 length = (todo < 8) ? todo : 8;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100464 /* copy data to transfer buffer */
465 memcpy(port->write_urb->transfer_buffer,
466 priv->buf + priv->cur_pos, length);
467 usb_fill_int_urb(port->write_urb,
468 port->serial->dev,
469 usb_sndintpipe(port->serial->dev,
470 priv->write_int_endpoint_address),
471 port->write_urb->transfer_buffer,
472 length,
473 kobil_write_callback,
474 port,
475 8
476 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 priv->cur_pos = priv->cur_pos + length;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100479 result = usb_submit_urb(port->write_urb, GFP_NOIO);
480 dbg("%s - port %d Send write URB returns: %i",
481 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 todo = priv->filled - priv->cur_pos;
483
Alan Coxdee0a7c2008-07-22 11:14:10 +0100484 if (todo > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 msleep(24);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 priv->filled = 0;
489 priv->cur_pos = 0;
490
Alan Coxdee0a7c2008-07-22 11:14:10 +0100491 /* someone sets the dev to 0 if the close method
492 has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100494
495 /* start reading (except TWIN and KAAN SIM) */
496 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
497 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
498 /* someone sets the dev to 0 if the close method has
499 been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100501
502 result = usb_submit_urb(port->interrupt_in_urb,
503 GFP_NOIO);
504 dbg("%s - port %d Send read URB returns: %i",
505 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 }
508 return count;
509}
510
511
Alan Coxdee0a7c2008-07-22 11:14:10 +0100512static int kobil_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100514 /* dbg("%s - port %d", __func__, port->number); */
Alan Cox95da3102008-07-22 11:09:07 +0100515 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return 8;
517}
518
519
Alan Cox95da3102008-07-22 11:09:07 +0100520static int kobil_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Alan Cox95da3102008-07-22 11:09:07 +0100522 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100523 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int result;
525 unsigned char *transfer_buffer;
526 int transfer_buffer_length = 8;
527
528 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100529 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
530 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
531 /* This device doesn't support ioctl calls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -EINVAL;
533 }
534
Alan Coxdee0a7c2008-07-22 11:14:10 +0100535 /* allocate memory for transfer buffer */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100536 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100537 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Alan Coxdee0a7c2008-07-22 11:14:10 +0100540 result = usb_control_msg(port->serial->dev,
541 usb_rcvctrlpipe(port->serial->dev, 0),
542 SUSBCRequest_GetStatusLineState,
543 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
544 0,
545 0,
546 transfer_buffer,
547 transfer_buffer_length,
548 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Alan Coxdee0a7c2008-07-22 11:14:10 +0100550 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800551 __func__, port->number, result, transfer_buffer[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Alan Coxa40d8542008-02-20 21:40:34 +0000553 result = 0;
554 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
555 result = TIOCM_DSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 kfree(transfer_buffer);
Alan Coxa40d8542008-02-20 21:40:34 +0000557 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Alan Cox95da3102008-07-22 11:09:07 +0100560static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 unsigned int set, unsigned int clear)
562{
Alan Cox95da3102008-07-22 11:09:07 +0100563 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100564 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int result;
566 int dtr = 0;
567 int rts = 0;
568 unsigned char *transfer_buffer;
569 int transfer_buffer_length = 8;
570
Alan Coxa40d8542008-02-20 21:40:34 +0000571 /* FIXME: locking ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100573 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
574 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
575 /* This device doesn't support ioctl calls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -EINVAL;
577 }
578
Alan Coxdee0a7c2008-07-22 11:14:10 +0100579 /* allocate memory for transfer buffer */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100580 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100581 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (set & TIOCM_RTS)
585 rts = 1;
586 if (set & TIOCM_DTR)
587 dtr = 1;
588 if (clear & TIOCM_RTS)
589 rts = 0;
590 if (clear & TIOCM_DTR)
591 dtr = 0;
592
593 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
594 if (dtr != 0)
Alan Coxdee0a7c2008-07-22 11:14:10 +0100595 dbg("%s - port %d Setting DTR",
596 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 else
Alan Coxdee0a7c2008-07-22 11:14:10 +0100598 dbg("%s - port %d Clearing DTR",
599 __func__, port->number);
600 result = usb_control_msg(port->serial->dev,
601 usb_rcvctrlpipe(port->serial->dev, 0),
602 SUSBCRequest_SetStatusLinesOrQueues,
603 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
604 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
605 0,
606 transfer_buffer,
607 0,
608 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 } else {
610 if (rts != 0)
Alan Coxdee0a7c2008-07-22 11:14:10 +0100611 dbg("%s - port %d Setting RTS",
612 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 else
Alan Coxdee0a7c2008-07-22 11:14:10 +0100614 dbg("%s - port %d Clearing RTS",
615 __func__, port->number);
616 result = usb_control_msg(port->serial->dev,
617 usb_rcvctrlpipe(port->serial->dev, 0),
618 SUSBCRequest_SetStatusLinesOrQueues,
619 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
620 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
621 0,
622 transfer_buffer,
623 0,
624 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100626 dbg("%s - port %d Send set_status_line URB returns: %i",
627 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 kfree(transfer_buffer);
629 return (result < 0) ? result : 0;
630}
631
Alan Cox95da3102008-07-22 11:09:07 +0100632static void kobil_set_termios(struct tty_struct *tty,
633 struct usb_serial_port *port, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100635 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int result;
637 unsigned short urb_val = 0;
Alan Cox95da3102008-07-22 11:09:07 +0100638 int c_cflag = tty->termios->c_cflag;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100639 speed_t speed;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100640 void *settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100643 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
Alan Coxb31f6582008-07-22 11:14:22 +0100644 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100645 /* This device doesn't support ioctl calls */
Alan Coxb31f6582008-07-22 11:14:22 +0100646 *tty->termios = *old;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100647 return;
Alan Coxb31f6582008-07-22 11:14:22 +0100648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Alan Coxdee0a7c2008-07-22 11:14:10 +0100650 speed = tty_get_baud_rate(tty);
651 switch (speed) {
652 case 1200:
653 urb_val = SUSBCR_SBR_1200;
654 break;
655 default:
656 speed = 9600;
657 case 9600:
658 urb_val = SUSBCR_SBR_9600;
659 break;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100660 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100661 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits :
662 SUSBCR_SPASB_1StopBit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Alan Cox94d0f7e2007-08-22 23:09:16 +0100664 settings = kzalloc(50, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100665 if (!settings)
Alan Cox94d0f7e2007-08-22 23:09:16 +0100666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Alan Cox94d0f7e2007-08-22 23:09:16 +0100668 sprintf(settings, "%d ", speed);
669
670 if (c_cflag & PARENB) {
671 if (c_cflag & PARODD) {
672 urb_val |= SUSBCR_SPASB_OddParity;
673 strcat(settings, "Odd Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 } else {
Alan Cox94d0f7e2007-08-22 23:09:16 +0100675 urb_val |= SUSBCR_SPASB_EvenParity;
676 strcat(settings, "Even Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Alan Cox94d0f7e2007-08-22 23:09:16 +0100678 } else {
679 urb_val |= SUSBCR_SPASB_NoParity;
680 strcat(settings, "No Parity");
681 }
Alan Cox95da3102008-07-22 11:09:07 +0100682 tty->termios->c_cflag &= ~CMSPAR;
683 tty_encode_baud_rate(tty, speed, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Alan Coxdee0a7c2008-07-22 11:14:10 +0100685 result = usb_control_msg(port->serial->dev,
686 usb_rcvctrlpipe(port->serial->dev, 0),
687 SUSBCRequest_SetBaudRateParityAndStopBits,
688 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
689 urb_val,
690 0,
691 settings,
692 0,
693 KOBIL_TIMEOUT
Alan Cox94d0f7e2007-08-22 23:09:16 +0100694 );
695 kfree(settings);
696}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Alan Coxdee0a7c2008-07-22 11:14:10 +0100698static int kobil_ioctl(struct tty_struct *tty, struct file *file,
699 unsigned int cmd, unsigned long arg)
Alan Cox94d0f7e2007-08-22 23:09:16 +0100700{
Alan Cox95da3102008-07-22 11:09:07 +0100701 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100702 struct kobil_private *priv = usb_get_serial_port_data(port);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100703 unsigned char *transfer_buffer;
704 int transfer_buffer_length = 8;
705 int result;
706
Alan Coxdee0a7c2008-07-22 11:14:10 +0100707 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
708 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
709 /* This device doesn't support ioctl calls */
Alan Coxb31f6582008-07-22 11:14:22 +0100710 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Alan Cox94d0f7e2007-08-22 23:09:16 +0100712 switch (cmd) {
Alan Cox95da3102008-07-22 11:09:07 +0100713 case TCFLSH:
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800714 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100715 if (!transfer_buffer)
716 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Alan Coxdee0a7c2008-07-22 11:14:10 +0100718 result = usb_control_msg(port->serial->dev,
719 usb_rcvctrlpipe(port->serial->dev, 0),
720 SUSBCRequest_Misc,
721 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
722 SUSBCR_MSC_ResetAllQueues,
723 0,
724 NULL, /* transfer_buffer, */
725 0,
726 KOBIL_TIMEOUT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 );
Alan Coxdee0a7c2008-07-22 11:14:10 +0100728
Harvey Harrison441b62c2008-03-03 16:08:34 -0800729 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 kfree(transfer_buffer);
Alan Cox95da3102008-07-22 11:09:07 +0100731 return (result < 0) ? -EIO: 0;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100732 default:
733 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Alan Coxdee0a7c2008-07-22 11:14:10 +0100737static int __init kobil_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 int retval;
740 retval = usb_serial_register(&kobil_device);
741 if (retval)
742 goto failed_usb_serial_register;
743 retval = usb_register(&kobil_driver);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100744 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 goto failed_usb_register;
746
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700747 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
748 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 return 0;
751failed_usb_register:
752 usb_serial_deregister(&kobil_device);
753failed_usb_serial_register:
754 return retval;
755}
756
757
Alan Coxdee0a7c2008-07-22 11:14:10 +0100758static void __exit kobil_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100760 usb_deregister(&kobil_driver);
761 usb_serial_deregister(&kobil_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764module_init(kobil_init);
765module_exit(kobil_exit);
766
Alan Coxdee0a7c2008-07-22 11:14:10 +0100767MODULE_AUTHOR(DRIVER_AUTHOR);
768MODULE_DESCRIPTION(DRIVER_DESC);
769MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771module_param(debug, bool, S_IRUGO | S_IWUSR);
772MODULE_PARM_DESC(debug, "Debug enabled or not");