blob: aee450246bfd8b0befa879ca9613415ad649d577 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * KOBIL USB Smart Card Terminal Driver
3 *
4 * Copyright (C) 2002 KOBIL Systems GmbH
5 * 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)
23 *
24 * (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>
47#include <asm/uaccess.h>
48#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 */
71static int kobil_startup (struct usb_serial *serial);
72static void kobil_shutdown (struct usb_serial *serial);
73static int kobil_open (struct usb_serial_port *port, struct file *filp);
74static void kobil_close (struct usb_serial_port *port, struct file *filp);
75static int kobil_write (struct usb_serial_port *port,
76 const unsigned char *buf, int count);
77static int kobil_write_room(struct usb_serial_port *port);
78static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
79 unsigned int cmd, unsigned long arg);
80static int kobil_tiocmget(struct usb_serial_port *port, struct file *file);
81static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
82 unsigned int set, unsigned int clear);
David Howells7d12e782006-10-05 14:55:46 +010083static void kobil_read_int_callback( struct urb *urb );
84static void kobil_write_callback( struct urb *purb );
Alan Cox94d0f7e2007-08-22 23:09:16 +010085static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87
88static struct usb_device_id id_table [] = {
89 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
90 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
91 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
92 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
93 { } /* Terminating entry */
94};
95
96
97MODULE_DEVICE_TABLE (usb, id_table);
98
99static struct usb_driver kobil_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .name = "kobil",
101 .probe = usb_serial_probe,
102 .disconnect = usb_serial_disconnect,
103 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800104 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
107
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700108static struct usb_serial_driver kobil_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700109 .driver = {
110 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700111 .name = "kobil",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700112 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700113 .description = "KOBIL USB smart card terminal",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100114 .usb_driver = &kobil_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .id_table = id_table,
116 .num_interrupt_in = NUM_DONT_CARE,
117 .num_bulk_in = 0,
118 .num_bulk_out = 0,
119 .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;
137 unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
138 int filled; // index of the last char in buf
139 int cur_pos; // index of the next char to send in buf
140 __u16 device_type;
141 int line_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
144
145static int kobil_startup (struct usb_serial *serial)
146{
147 int i;
148 struct kobil_private *priv;
149 struct usb_device *pdev;
150 struct usb_host_config *actconfig;
151 struct usb_interface *interface;
152 struct usb_host_interface *altsetting;
153 struct usb_host_endpoint *endpoint;
154
155 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
156 if (!priv){
157 return -ENOMEM;
158 }
159
160 priv->filled = 0;
161 priv->cur_pos = 0;
162 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
163 priv->line_state = 0;
164
165 switch (priv->device_type){
166 case KOBIL_ADAPTER_B_PRODUCT_ID:
167 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
168 break;
169 case KOBIL_ADAPTER_K_PRODUCT_ID:
170 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
171 break;
172 case KOBIL_USBTWIN_PRODUCT_ID:
173 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
174 break;
175 case KOBIL_KAAN_SIM_PRODUCT_ID:
176 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
177 break;
178 }
179 usb_set_serial_port_data(serial->port[0], priv);
180
181 // search for the necessary endpoints
182 pdev = serial->dev;
183 actconfig = pdev->actconfig;
184 interface = actconfig->interface[0];
185 altsetting = interface->cur_altsetting;
186 endpoint = altsetting->endpoint;
187
188 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
189 endpoint = &altsetting->endpoint[i];
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300190 if (usb_endpoint_is_int_out(&endpoint->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
192 priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
193 }
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300194 if (usb_endpoint_is_int_in(&endpoint->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 dbg("%s Found interrupt in endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
196 priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
197 }
198 }
199 return 0;
200}
201
202
203static void kobil_shutdown (struct usb_serial *serial)
204{
205 int i;
206 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
207
208 for (i=0; i < serial->num_ports; ++i) {
209 while (serial->port[i]->open_count > 0) {
210 kobil_close (serial->port[i], NULL);
211 }
212 kfree(usb_get_serial_port_data(serial->port[i]));
213 usb_set_serial_port_data(serial->port[i], NULL);
214 }
215}
216
217
218static int kobil_open (struct usb_serial_port *port, struct file *filp)
219{
Alan Cox94d0f7e2007-08-22 23:09:16 +0100220 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct kobil_private *priv;
222 unsigned char *transfer_buffer;
223 int transfer_buffer_length = 8;
224 int write_urb_transfer_buffer_length = 8;
225
226 dbg("%s - port %d", __FUNCTION__, port->number);
227 priv = usb_get_serial_port_data(port);
228 priv->line_state = 0;
229
230 // someone sets the dev to 0 if the close method has been called
231 port->interrupt_in_urb->dev = port->serial->dev;
232
233
234 /* force low_latency on so that our tty_push actually forces
235 * the data through, otherwise it is scheduled, and with high
236 * data rates (like with OHCI) data can get lost.
237 */
238 port->tty->low_latency = 1;
239
240 // without this, every push_tty_char is echoed :-(
241 port->tty->termios->c_lflag = 0;
242 port->tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
243 port->tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
244 port->tty->termios->c_oflag &= ~ONLCR; // do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D)
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100247 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (! transfer_buffer) {
249 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 // allocate write_urb
253 if (!port->write_urb) {
254 dbg("%s - port %d Allocating port->write_urb", __FUNCTION__, port->number);
255 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
256 if (!port->write_urb) {
257 dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
258 kfree(transfer_buffer);
259 return -ENOMEM;
260 }
261 }
262
263 // allocate memory for write_urb transfer buffer
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800264 port->write_urb->transfer_buffer = kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (! port->write_urb->transfer_buffer) {
266 kfree(transfer_buffer);
267 usb_free_urb(port->write_urb);
268 port->write_urb = NULL;
269 return -ENOMEM;
270 }
271
272 // get hardware version
273 result = usb_control_msg( port->serial->dev,
274 usb_rcvctrlpipe(port->serial->dev, 0 ),
275 SUSBCRequest_GetMisc,
276 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
277 SUSBCR_MSC_GetHWVersion,
278 0,
279 transfer_buffer,
280 transfer_buffer_length,
281 KOBIL_TIMEOUT
282 );
283 dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
284 dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
285
286 // get firmware version
287 result = usb_control_msg( port->serial->dev,
288 usb_rcvctrlpipe(port->serial->dev, 0 ),
289 SUSBCRequest_GetMisc,
290 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
291 SUSBCR_MSC_GetFWVersion,
292 0,
293 transfer_buffer,
294 transfer_buffer_length,
295 KOBIL_TIMEOUT
296 );
297 dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
298 dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
299
300 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
301 // Setting Baudrate, Parity and Stopbits
302 result = usb_control_msg( port->serial->dev,
303 usb_rcvctrlpipe(port->serial->dev, 0 ),
304 SUSBCRequest_SetBaudRateParityAndStopBits,
305 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
306 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
307 0,
308 transfer_buffer,
309 0,
310 KOBIL_TIMEOUT
311 );
312 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
313
314 // reset all queues
315 result = usb_control_msg( port->serial->dev,
316 usb_rcvctrlpipe(port->serial->dev, 0 ),
317 SUSBCRequest_Misc,
318 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
319 SUSBCR_MSC_ResetAllQueues,
320 0,
321 transfer_buffer,
322 0,
323 KOBIL_TIMEOUT
324 );
325 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
326 }
327 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
328 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
329 // start reading (Adapter B 'cause PNP string)
330 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
331 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
332 }
333
334 kfree(transfer_buffer);
335 return 0;
336}
337
338
339static void kobil_close (struct usb_serial_port *port, struct file *filp)
340{
341 dbg("%s - port %d", __FUNCTION__, port->number);
342
343 if (port->write_urb) {
344 usb_kill_urb(port->write_urb);
345 usb_free_urb( port->write_urb );
346 port->write_urb = NULL;
347 }
Mariusz Kozlowski5505c222006-11-08 15:36:38 +0100348 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700352static void kobil_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int result;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700355 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct tty_struct *tty;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700357 unsigned char *data = urb->transfer_buffer;
358 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359// char *dbg_data;
360
361 dbg("%s - port %d", __FUNCTION__, port->number);
362
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700363 if (status) {
364 dbg("%s - port %d Read int status not zero: %d",
365 __FUNCTION__, port->number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return;
367 }
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700368
369 tty = port->tty;
370 if (urb->actual_length) {
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 // BEGIN DEBUG
373 /*
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100374 dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (! dbg_data) {
376 return;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 for (i = 0; i < purb->actual_length; i++) {
379 sprintf(dbg_data +3*i, "%02X ", data[i]);
380 }
381 dbg(" <-- %s", dbg_data );
382 kfree(dbg_data);
383 */
384 // END DEBUG
385
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700386 tty_buffer_request_room(tty, urb->actual_length);
387 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 tty_flip_buffer_push(tty);
389 }
390
391 // someone sets the dev to 0 if the close method has been called
392 port->interrupt_in_urb->dev = port->serial->dev;
393
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700394 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
396}
397
398
David Howells7d12e782006-10-05 14:55:46 +0100399static void kobil_write_callback( struct urb *purb )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401}
402
403
404static int kobil_write (struct usb_serial_port *port,
405 const unsigned char *buf, int count)
406{
407 int length = 0;
408 int result = 0;
409 int todo = 0;
410 struct kobil_private * priv;
411
412 if (count == 0) {
413 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
414 return 0;
415 }
416
417 priv = usb_get_serial_port_data(port);
418
419 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
420 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
421 return -ENOMEM;
422 }
423
424 // Copy data to buffer
425 memcpy (priv->buf + priv->filled, buf, count);
426
427 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
428
429 priv->filled = priv->filled + count;
430
431
432 // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
433 if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
434 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
435
436 // stop reading (except TWIN and KAAN SIM)
437 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
438 usb_kill_urb(port->interrupt_in_urb);
439
440 todo = priv->filled - priv->cur_pos;
441
442 while(todo > 0) {
443 // max 8 byte in one urb (endpoint size)
444 length = (todo < 8) ? todo : 8;
445 // copy data to transfer buffer
446 memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
447 usb_fill_int_urb( port->write_urb,
448 port->serial->dev,
449 usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
450 port->write_urb->transfer_buffer,
451 length,
452 kobil_write_callback,
453 port,
454 8
455 );
456
457 priv->cur_pos = priv->cur_pos + length;
458 result = usb_submit_urb( port->write_urb, GFP_NOIO );
459 dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
460 todo = priv->filled - priv->cur_pos;
461
462 if (todo > 0) {
463 msleep(24);
464 }
465
466 } // end while
467
468 priv->filled = 0;
469 priv->cur_pos = 0;
470
471 // someone sets the dev to 0 if the close method has been called
472 port->interrupt_in_urb->dev = port->serial->dev;
473
474 // start reading (except TWIN and KAAN SIM)
475 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
476 // someone sets the dev to 0 if the close method has been called
477 port->interrupt_in_urb->dev = port->serial->dev;
478
479 result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
480 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
481 }
482 }
483 return count;
484}
485
486
487static int kobil_write_room (struct usb_serial_port *port)
488{
489 //dbg("%s - port %d", __FUNCTION__, port->number);
490 return 8;
491}
492
493
494static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
495{
496 struct kobil_private * priv;
497 int result;
498 unsigned char *transfer_buffer;
499 int transfer_buffer_length = 8;
500
501 priv = usb_get_serial_port_data(port);
502 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
503 // This device doesn't support ioctl calls
504 return -EINVAL;
505 }
506
507 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100508 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!transfer_buffer) {
510 return -ENOMEM;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 result = usb_control_msg( port->serial->dev,
514 usb_rcvctrlpipe(port->serial->dev, 0 ),
515 SUSBCRequest_GetStatusLineState,
516 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
517 0,
518 0,
519 transfer_buffer,
520 transfer_buffer_length,
521 KOBIL_TIMEOUT);
522
523 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
524 __FUNCTION__, port->number, result, transfer_buffer[0]);
525
526 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) {
527 priv->line_state |= TIOCM_DSR;
528 } else {
529 priv->line_state &= ~TIOCM_DSR;
530 }
531
532 kfree(transfer_buffer);
533 return priv->line_state;
534}
535
536static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
537 unsigned int set, unsigned int clear)
538{
539 struct kobil_private * priv;
540 int result;
541 int dtr = 0;
542 int rts = 0;
543 unsigned char *transfer_buffer;
544 int transfer_buffer_length = 8;
545
546 priv = usb_get_serial_port_data(port);
547 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
548 // This device doesn't support ioctl calls
549 return -EINVAL;
550 }
551
552 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100553 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (! transfer_buffer) {
555 return -ENOMEM;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (set & TIOCM_RTS)
559 rts = 1;
560 if (set & TIOCM_DTR)
561 dtr = 1;
562 if (clear & TIOCM_RTS)
563 rts = 0;
564 if (clear & TIOCM_DTR)
565 dtr = 0;
566
567 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
568 if (dtr != 0)
569 dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
570 else
571 dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
572 result = usb_control_msg( port->serial->dev,
573 usb_rcvctrlpipe(port->serial->dev, 0 ),
574 SUSBCRequest_SetStatusLinesOrQueues,
575 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
576 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
577 0,
578 transfer_buffer,
579 0,
580 KOBIL_TIMEOUT);
581 } else {
582 if (rts != 0)
583 dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
584 else
585 dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
586 result = usb_control_msg( port->serial->dev,
587 usb_rcvctrlpipe(port->serial->dev, 0 ),
588 SUSBCRequest_SetStatusLinesOrQueues,
589 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
590 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
591 0,
592 transfer_buffer,
593 0,
594 KOBIL_TIMEOUT);
595 }
596 dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
597 kfree(transfer_buffer);
598 return (result < 0) ? result : 0;
599}
600
Alan Cox94d0f7e2007-08-22 23:09:16 +0100601static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct kobil_private * priv;
604 int result;
605 unsigned short urb_val = 0;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100606 int c_cflag = port->tty->termios->c_cflag;
607 speed_t speed;
608 void * settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 priv = usb_get_serial_port_data(port);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100611 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 // This device doesn't support ioctl calls
Alan Cox94d0f7e2007-08-22 23:09:16 +0100613 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Alan Cox94d0f7e2007-08-22 23:09:16 +0100615 switch (speed = tty_get_baud_rate(port->tty)) {
616 case 1200:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 urb_val = SUSBCR_SBR_1200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 default:
Alan Coxa6ebf802007-10-18 01:24:21 -0700620 speed = 9600;
621 case 9600:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 urb_val = SUSBCR_SBR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100624 }
625 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Alan Cox94d0f7e2007-08-22 23:09:16 +0100627 settings = kzalloc(50, GFP_KERNEL);
628 if (! settings)
629 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Alan Cox94d0f7e2007-08-22 23:09:16 +0100631 sprintf(settings, "%d ", speed);
632
633 if (c_cflag & PARENB) {
634 if (c_cflag & PARODD) {
635 urb_val |= SUSBCR_SPASB_OddParity;
636 strcat(settings, "Odd Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 } else {
Alan Cox94d0f7e2007-08-22 23:09:16 +0100638 urb_val |= SUSBCR_SPASB_EvenParity;
639 strcat(settings, "Even Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Alan Cox94d0f7e2007-08-22 23:09:16 +0100641 } else {
642 urb_val |= SUSBCR_SPASB_NoParity;
643 strcat(settings, "No Parity");
644 }
Alan Coxa6ebf802007-10-18 01:24:21 -0700645 port->tty->termios->c_cflag &= ~CMSPAR;
646 tty_encode_baud_rate(port->tty, speed, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Alan Cox94d0f7e2007-08-22 23:09:16 +0100648 result = usb_control_msg( port->serial->dev,
649 usb_rcvctrlpipe(port->serial->dev, 0 ),
650 SUSBCRequest_SetBaudRateParityAndStopBits,
651 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
652 urb_val,
653 0,
654 settings,
655 0,
656 KOBIL_TIMEOUT
657 );
658 kfree(settings);
659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Alan Cox94d0f7e2007-08-22 23:09:16 +0100661static int kobil_ioctl(struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
662{
663 struct kobil_private * priv = usb_get_serial_port_data(port);
664 unsigned char *transfer_buffer;
665 int transfer_buffer_length = 8;
666 int result;
667
668 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
669 // This device doesn't support ioctl calls
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
671
Alan Cox94d0f7e2007-08-22 23:09:16 +0100672 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case TCFLSH: // 0x540B
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800674 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100675 if (! transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 result = usb_control_msg( port->serial->dev,
679 usb_rcvctrlpipe(port->serial->dev, 0 ),
680 SUSBCRequest_Misc,
681 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
682 SUSBCR_MSC_ResetAllQueues,
683 0,
684 NULL,//transfer_buffer,
685 0,
686 KOBIL_TIMEOUT
687 );
688
689 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 kfree(transfer_buffer);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100691 return (result < 0) ? -EFAULT : 0;
692 default:
693 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697static int __init kobil_init (void)
698{
699 int retval;
700 retval = usb_serial_register(&kobil_device);
701 if (retval)
702 goto failed_usb_serial_register;
703 retval = usb_register(&kobil_driver);
704 if (retval)
705 goto failed_usb_register;
706
707 info(DRIVER_VERSION " " DRIVER_AUTHOR);
708 info(DRIVER_DESC);
709
710 return 0;
711failed_usb_register:
712 usb_serial_deregister(&kobil_device);
713failed_usb_serial_register:
714 return retval;
715}
716
717
718static void __exit kobil_exit (void)
719{
720 usb_deregister (&kobil_driver);
721 usb_serial_deregister (&kobil_device);
722}
723
724module_init(kobil_init);
725module_exit(kobil_exit);
726
727MODULE_AUTHOR( DRIVER_AUTHOR );
728MODULE_DESCRIPTION( DRIVER_DESC );
729MODULE_LICENSE( "GPL" );
730
731module_param(debug, bool, S_IRUGO | S_IWUSR);
732MODULE_PARM_DESC(debug, "Debug enabled or not");