blob: 6b570498287f3f5d62d337e6a347945101b638c3 [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 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,
120 .shutdown = kobil_shutdown,
121 .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 Coxdee0a7c2008-07-22 11:14:10 +0100204static void kobil_shutdown(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 Coxdee0a7c2008-07-22 11:14:10 +0100209 for (i = 0; i < serial->num_ports; ++i) {
210 while (serial->port[i]->port.count > 0)
Alan Cox335f8512009-06-11 12:26:29 +0100211 kobil_close(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 kfree(usb_get_serial_port_data(serial->port[i]));
213 usb_set_serial_port_data(serial->port[i], NULL);
214 }
215}
216
217
Alan Cox95da3102008-07-22 11:09:07 +0100218static int kobil_open(struct tty_struct *tty,
219 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Alan Cox94d0f7e2007-08-22 23:09:16 +0100221 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct kobil_private *priv;
223 unsigned char *transfer_buffer;
224 int transfer_buffer_length = 8;
225 int write_urb_transfer_buffer_length = 8;
226
Harvey Harrison441b62c2008-03-03 16:08:34 -0800227 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Alan Coxdee0a7c2008-07-22 11:14:10 +0100230 /* someone sets the dev to 0 if the close method has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 port->interrupt_in_urb->dev = port->serial->dev;
232
Alan Cox95da3102008-07-22 11:09:07 +0100233 if (tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Alan Cox95da3102008-07-22 11:09:07 +0100235 /* Default to echo off and other sane device settings */
236 tty->termios->c_lflag = 0;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100237 tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN |
238 XCASE);
Alan Cox95da3102008-07-22 11:09:07 +0100239 tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100240 /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
241 tty->termios->c_oflag &= ~ONLCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100243 /* allocate memory for transfer buffer */
244 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
245 if (!transfer_buffer)
246 return -ENOMEM;
247
248 /* allocate write_urb */
249 if (!port->write_urb) {
250 dbg("%s - port %d Allocating port->write_urb",
251 __func__, port->number);
252 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!port->write_urb) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100254 dbg("%s - port %d usb_alloc_urb failed",
255 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 kfree(transfer_buffer);
257 return -ENOMEM;
258 }
259 }
260
Alan Coxdee0a7c2008-07-22 11:14:10 +0100261 /* allocate memory for write_urb transfer buffer */
262 port->write_urb->transfer_buffer =
263 kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
264 if (!port->write_urb->transfer_buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 kfree(transfer_buffer);
266 usb_free_urb(port->write_urb);
267 port->write_urb = NULL;
268 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100270
271 /* get hardware version */
272 result = usb_control_msg(port->serial->dev,
273 usb_rcvctrlpipe(port->serial->dev, 0),
274 SUSBCRequest_GetMisc,
275 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
276 SUSBCR_MSC_GetHWVersion,
277 0,
278 transfer_buffer,
279 transfer_buffer_length,
280 KOBIL_TIMEOUT
281 );
282 dbg("%s - port %d Send get_HW_version URB returns: %i",
283 __func__, port->number, result);
284 dbg("Harware version: %i.%i.%i",
285 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
286
287 /* get firmware version */
288 result = usb_control_msg(port->serial->dev,
289 usb_rcvctrlpipe(port->serial->dev, 0),
290 SUSBCRequest_GetMisc,
291 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
292 SUSBCR_MSC_GetFWVersion,
293 0,
294 transfer_buffer,
295 transfer_buffer_length,
296 KOBIL_TIMEOUT
297 );
298 dbg("%s - port %d Send get_FW_version URB returns: %i",
299 __func__, port->number, result);
300 dbg("Firmware version: %i.%i.%i",
301 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
302
303 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
304 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
305 /* Setting Baudrate, Parity and Stopbits */
306 result = usb_control_msg(port->serial->dev,
307 usb_rcvctrlpipe(port->serial->dev, 0),
308 SUSBCRequest_SetBaudRateParityAndStopBits,
309 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
310 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity |
311 SUSBCR_SPASB_1StopBit,
312 0,
313 transfer_buffer,
314 0,
315 KOBIL_TIMEOUT
316 );
317 dbg("%s - port %d Send set_baudrate URB returns: %i",
318 __func__, port->number, result);
319
320 /* reset all queues */
321 result = usb_control_msg(port->serial->dev,
322 usb_rcvctrlpipe(port->serial->dev, 0),
323 SUSBCRequest_Misc,
324 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
325 SUSBCR_MSC_ResetAllQueues,
326 0,
327 transfer_buffer,
328 0,
329 KOBIL_TIMEOUT
330 );
331 dbg("%s - port %d Send reset_all_queues URB returns: %i",
332 __func__, port->number, result);
333 }
334 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
335 priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100337 /* start reading (Adapter B 'cause PNP string) */
338 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
339 dbg("%s - port %d Send read URB returns: %i",
340 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 kfree(transfer_buffer);
344 return 0;
345}
346
347
Alan Cox335f8512009-06-11 12:26:29 +0100348static void kobil_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800350 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Alan Cox335f8512009-06-11 12:26:29 +0100352 /* FIXME: Add rts/dtr methods */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (port->write_urb) {
354 usb_kill_urb(port->write_urb);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100355 usb_free_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 port->write_urb = NULL;
357 }
Mariusz Kozlowski5505c222006-11-08 15:36:38 +0100358 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700362static void kobil_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int result;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700365 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct tty_struct *tty;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700367 unsigned char *data = urb->transfer_buffer;
368 int status = urb->status;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100369/* char *dbg_data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Harvey Harrison441b62c2008-03-03 16:08:34 -0800371 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700373 if (status) {
374 dbg("%s - port %d Read int status not zero: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800375 __func__, port->number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return;
377 }
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700378
Alan Cox4a90f092008-10-13 10:39:46 +0100379 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700380 if (urb->actual_length) {
381
Alan Coxdee0a7c2008-07-22 11:14:10 +0100382 /* BEGIN DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
Alan Coxdee0a7c2008-07-22 11:14:10 +0100384 dbg_data = kzalloc((3 * purb->actual_length + 10)
385 * sizeof(char), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (! dbg_data) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100387 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100389 for (i = 0; i < purb->actual_length; i++) {
390 sprintf(dbg_data +3*i, "%02X ", data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100392 dbg(" <-- %s", dbg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 kfree(dbg_data);
394 */
Alan Coxdee0a7c2008-07-22 11:14:10 +0100395 /* END DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700397 tty_buffer_request_room(tty, urb->actual_length);
398 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 tty_flip_buffer_push(tty);
400 }
Alan Cox4a90f092008-10-13 10:39:46 +0100401 tty_kref_put(tty);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100402 /* someone sets the dev to 0 if the close method has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 port->interrupt_in_urb->dev = port->serial->dev;
404
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700405 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100406 dbg("%s - port %d Send read URB returns: %i",
407 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410
Alan Coxdee0a7c2008-07-22 11:14:10 +0100411static void kobil_write_callback(struct urb *purb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413}
414
415
Alan Coxdee0a7c2008-07-22 11:14:10 +0100416static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 const unsigned char *buf, int count)
418{
419 int length = 0;
420 int result = 0;
421 int todo = 0;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100422 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (count == 0) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100425 dbg("%s - port %d write request of 0 bytes",
426 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428 }
429
430 priv = usb_get_serial_port_data(port);
431
432 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800433 dbg("%s - port %d Error: write request bigger than buffer size", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -ENOMEM;
435 }
436
Alan Coxdee0a7c2008-07-22 11:14:10 +0100437 /* Copy data to buffer */
438 memcpy(priv->buf + priv->filled, buf, count);
439 usb_serial_debug_data(debug, &port->dev, __func__, count,
440 priv->buf + priv->filled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 priv->filled = priv->filled + count;
442
Alan Coxdee0a7c2008-07-22 11:14:10 +0100443 /* only send complete block. TWIN, KAAN SIM and adapter K
444 use the same protocol. */
445 if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
446 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
447 /* stop reading (except TWIN and KAAN SIM) */
448 if ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID)
449 || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 usb_kill_urb(port->interrupt_in_urb);
451
452 todo = priv->filled - priv->cur_pos;
453
Alan Coxdee0a7c2008-07-22 11:14:10 +0100454 while (todo > 0) {
455 /* max 8 byte in one urb (endpoint size) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 length = (todo < 8) ? todo : 8;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100457 /* copy data to transfer buffer */
458 memcpy(port->write_urb->transfer_buffer,
459 priv->buf + priv->cur_pos, length);
460 usb_fill_int_urb(port->write_urb,
461 port->serial->dev,
462 usb_sndintpipe(port->serial->dev,
463 priv->write_int_endpoint_address),
464 port->write_urb->transfer_buffer,
465 length,
466 kobil_write_callback,
467 port,
468 8
469 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 priv->cur_pos = priv->cur_pos + length;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100472 result = usb_submit_urb(port->write_urb, GFP_NOIO);
473 dbg("%s - port %d Send write URB returns: %i",
474 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 todo = priv->filled - priv->cur_pos;
476
Alan Coxdee0a7c2008-07-22 11:14:10 +0100477 if (todo > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 msleep(24);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 priv->filled = 0;
482 priv->cur_pos = 0;
483
Alan Coxdee0a7c2008-07-22 11:14:10 +0100484 /* someone sets the dev to 0 if the close method
485 has been called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100487
488 /* start reading (except TWIN and KAAN SIM) */
489 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
490 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
491 /* someone sets the dev to 0 if the close method has
492 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 result = usb_submit_urb(port->interrupt_in_urb,
496 GFP_NOIO);
497 dbg("%s - port %d Send read URB returns: %i",
498 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 }
501 return count;
502}
503
504
Alan Coxdee0a7c2008-07-22 11:14:10 +0100505static int kobil_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100507 /* dbg("%s - port %d", __func__, port->number); */
Alan Cox95da3102008-07-22 11:09:07 +0100508 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 8;
510}
511
512
Alan Cox95da3102008-07-22 11:09:07 +0100513static int kobil_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Alan Cox95da3102008-07-22 11:09:07 +0100515 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100516 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int result;
518 unsigned char *transfer_buffer;
519 int transfer_buffer_length = 8;
520
521 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100522 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EINVAL;
526 }
527
Alan Coxdee0a7c2008-07-22 11:14:10 +0100528 /* allocate memory for transfer buffer */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100529 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100530 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Alan Coxdee0a7c2008-07-22 11:14:10 +0100533 result = usb_control_msg(port->serial->dev,
534 usb_rcvctrlpipe(port->serial->dev, 0),
535 SUSBCRequest_GetStatusLineState,
536 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
537 0,
538 0,
539 transfer_buffer,
540 transfer_buffer_length,
541 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Alan Coxdee0a7c2008-07-22 11:14:10 +0100543 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800544 __func__, port->number, result, transfer_buffer[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Alan Coxa40d8542008-02-20 21:40:34 +0000546 result = 0;
547 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
548 result = TIOCM_DSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 kfree(transfer_buffer);
Alan Coxa40d8542008-02-20 21:40:34 +0000550 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Alan Cox95da3102008-07-22 11:09:07 +0100553static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 unsigned int set, unsigned int clear)
555{
Alan Cox95da3102008-07-22 11:09:07 +0100556 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100557 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int result;
559 int dtr = 0;
560 int rts = 0;
561 unsigned char *transfer_buffer;
562 int transfer_buffer_length = 8;
563
Alan Coxa40d8542008-02-20 21:40:34 +0000564 /* FIXME: locking ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100566 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
567 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
568 /* This device doesn't support ioctl calls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EINVAL;
570 }
571
Alan Coxdee0a7c2008-07-22 11:14:10 +0100572 /* allocate memory for transfer buffer */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100573 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100574 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (set & TIOCM_RTS)
578 rts = 1;
579 if (set & TIOCM_DTR)
580 dtr = 1;
581 if (clear & TIOCM_RTS)
582 rts = 0;
583 if (clear & TIOCM_DTR)
584 dtr = 0;
585
586 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
587 if (dtr != 0)
Alan Coxdee0a7c2008-07-22 11:14:10 +0100588 dbg("%s - port %d Setting DTR",
589 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 else
Alan Coxdee0a7c2008-07-22 11:14:10 +0100591 dbg("%s - port %d Clearing DTR",
592 __func__, port->number);
593 result = usb_control_msg(port->serial->dev,
594 usb_rcvctrlpipe(port->serial->dev, 0),
595 SUSBCRequest_SetStatusLinesOrQueues,
596 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
597 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
598 0,
599 transfer_buffer,
600 0,
601 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 } else {
603 if (rts != 0)
Alan Coxdee0a7c2008-07-22 11:14:10 +0100604 dbg("%s - port %d Setting RTS",
605 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 else
Alan Coxdee0a7c2008-07-22 11:14:10 +0100607 dbg("%s - port %d Clearing RTS",
608 __func__, port->number);
609 result = usb_control_msg(port->serial->dev,
610 usb_rcvctrlpipe(port->serial->dev, 0),
611 SUSBCRequest_SetStatusLinesOrQueues,
612 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
613 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
614 0,
615 transfer_buffer,
616 0,
617 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100619 dbg("%s - port %d Send set_status_line URB returns: %i",
620 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 kfree(transfer_buffer);
622 return (result < 0) ? result : 0;
623}
624
Alan Cox95da3102008-07-22 11:09:07 +0100625static void kobil_set_termios(struct tty_struct *tty,
626 struct usb_serial_port *port, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100628 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int result;
630 unsigned short urb_val = 0;
Alan Cox95da3102008-07-22 11:09:07 +0100631 int c_cflag = tty->termios->c_cflag;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100632 speed_t speed;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100633 void *settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100636 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
Alan Coxb31f6582008-07-22 11:14:22 +0100637 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100638 /* This device doesn't support ioctl calls */
Alan Coxb31f6582008-07-22 11:14:22 +0100639 *tty->termios = *old;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100640 return;
Alan Coxb31f6582008-07-22 11:14:22 +0100641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Alan Coxdee0a7c2008-07-22 11:14:10 +0100643 speed = tty_get_baud_rate(tty);
644 switch (speed) {
645 case 1200:
646 urb_val = SUSBCR_SBR_1200;
647 break;
648 default:
649 speed = 9600;
650 case 9600:
651 urb_val = SUSBCR_SBR_9600;
652 break;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100653 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100654 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits :
655 SUSBCR_SPASB_1StopBit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Alan Cox94d0f7e2007-08-22 23:09:16 +0100657 settings = kzalloc(50, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100658 if (!settings)
Alan Cox94d0f7e2007-08-22 23:09:16 +0100659 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Alan Cox94d0f7e2007-08-22 23:09:16 +0100661 sprintf(settings, "%d ", speed);
662
663 if (c_cflag & PARENB) {
664 if (c_cflag & PARODD) {
665 urb_val |= SUSBCR_SPASB_OddParity;
666 strcat(settings, "Odd Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 } else {
Alan Cox94d0f7e2007-08-22 23:09:16 +0100668 urb_val |= SUSBCR_SPASB_EvenParity;
669 strcat(settings, "Even Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Alan Cox94d0f7e2007-08-22 23:09:16 +0100671 } else {
672 urb_val |= SUSBCR_SPASB_NoParity;
673 strcat(settings, "No Parity");
674 }
Alan Cox95da3102008-07-22 11:09:07 +0100675 tty->termios->c_cflag &= ~CMSPAR;
676 tty_encode_baud_rate(tty, speed, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Alan Coxdee0a7c2008-07-22 11:14:10 +0100678 result = usb_control_msg(port->serial->dev,
679 usb_rcvctrlpipe(port->serial->dev, 0),
680 SUSBCRequest_SetBaudRateParityAndStopBits,
681 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
682 urb_val,
683 0,
684 settings,
685 0,
686 KOBIL_TIMEOUT
Alan Cox94d0f7e2007-08-22 23:09:16 +0100687 );
688 kfree(settings);
689}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Alan Coxdee0a7c2008-07-22 11:14:10 +0100691static int kobil_ioctl(struct tty_struct *tty, struct file *file,
692 unsigned int cmd, unsigned long arg)
Alan Cox94d0f7e2007-08-22 23:09:16 +0100693{
Alan Cox95da3102008-07-22 11:09:07 +0100694 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100695 struct kobil_private *priv = usb_get_serial_port_data(port);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100696 unsigned char *transfer_buffer;
697 int transfer_buffer_length = 8;
698 int result;
699
Alan Coxdee0a7c2008-07-22 11:14:10 +0100700 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
701 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
702 /* This device doesn't support ioctl calls */
Alan Coxb31f6582008-07-22 11:14:22 +0100703 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Alan Cox94d0f7e2007-08-22 23:09:16 +0100705 switch (cmd) {
Alan Cox95da3102008-07-22 11:09:07 +0100706 case TCFLSH:
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800707 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100708 if (!transfer_buffer)
709 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Alan Coxdee0a7c2008-07-22 11:14:10 +0100711 result = usb_control_msg(port->serial->dev,
712 usb_rcvctrlpipe(port->serial->dev, 0),
713 SUSBCRequest_Misc,
714 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
715 SUSBCR_MSC_ResetAllQueues,
716 0,
717 NULL, /* transfer_buffer, */
718 0,
719 KOBIL_TIMEOUT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 );
Alan Coxdee0a7c2008-07-22 11:14:10 +0100721
Harvey Harrison441b62c2008-03-03 16:08:34 -0800722 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 kfree(transfer_buffer);
Alan Cox95da3102008-07-22 11:09:07 +0100724 return (result < 0) ? -EIO: 0;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100725 default:
726 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Alan Coxdee0a7c2008-07-22 11:14:10 +0100730static int __init kobil_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 int retval;
733 retval = usb_serial_register(&kobil_device);
734 if (retval)
735 goto failed_usb_serial_register;
736 retval = usb_register(&kobil_driver);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100737 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto failed_usb_register;
739
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700740 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
741 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 return 0;
744failed_usb_register:
745 usb_serial_deregister(&kobil_device);
746failed_usb_serial_register:
747 return retval;
748}
749
750
Alan Coxdee0a7c2008-07-22 11:14:10 +0100751static void __exit kobil_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100753 usb_deregister(&kobil_driver);
754 usb_serial_deregister(&kobil_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757module_init(kobil_init);
758module_exit(kobil_exit);
759
Alan Coxdee0a7c2008-07-22 11:14:10 +0100760MODULE_AUTHOR(DRIVER_AUTHOR);
761MODULE_DESCRIPTION(DRIVER_DESC);
762MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764module_param(debug, bool, S_IRUGO | S_IWUSR);
765MODULE_PARM_DESC(debug, "Debug enabled or not");