blob: d50dce03495819f4280f815285a89214be2a3e88 [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);
83static void kobil_read_int_callback( struct urb *urb, struct pt_regs *regs );
84static void kobil_write_callback( struct urb *purb, struct pt_regs *regs );
85
86
87static struct usb_device_id id_table [] = {
88 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
89 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
90 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
91 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
92 { } /* Terminating entry */
93};
94
95
96MODULE_DEVICE_TABLE (usb, id_table);
97
98static struct usb_driver kobil_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .name = "kobil",
100 .probe = usb_serial_probe,
101 .disconnect = usb_serial_disconnect,
102 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800103 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700107static struct usb_serial_driver kobil_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700108 .driver = {
109 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700110 .name = "kobil",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700111 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700112 .description = "KOBIL USB smart card terminal",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .id_table = id_table,
114 .num_interrupt_in = NUM_DONT_CARE,
115 .num_bulk_in = 0,
116 .num_bulk_out = 0,
117 .num_ports = 1,
118 .attach = kobil_startup,
119 .shutdown = kobil_shutdown,
120 .ioctl = kobil_ioctl,
121 .tiocmget = kobil_tiocmget,
122 .tiocmset = kobil_tiocmset,
123 .open = kobil_open,
124 .close = kobil_close,
125 .write = kobil_write,
126 .write_room = kobil_write_room,
127 .read_int_callback = kobil_read_int_callback,
128};
129
130
131struct kobil_private {
132 int write_int_endpoint_address;
133 int read_int_endpoint_address;
134 unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
135 int filled; // index of the last char in buf
136 int cur_pos; // index of the next char to send in buf
137 __u16 device_type;
138 int line_state;
139 struct termios internal_termios;
140};
141
142
143static int kobil_startup (struct usb_serial *serial)
144{
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);
154 if (!priv){
155 return -ENOMEM;
156 }
157
158 priv->filled = 0;
159 priv->cur_pos = 0;
160 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
161 priv->line_state = 0;
162
163 switch (priv->device_type){
164 case KOBIL_ADAPTER_B_PRODUCT_ID:
165 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
166 break;
167 case KOBIL_ADAPTER_K_PRODUCT_ID:
168 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
169 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
179 // search for the necessary endpoints
180 pdev = serial->dev;
181 actconfig = pdev->actconfig;
182 interface = actconfig->interface[0];
183 altsetting = interface->cur_altsetting;
184 endpoint = altsetting->endpoint;
185
186 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
187 endpoint = &altsetting->endpoint[i];
188 if (((endpoint->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
189 ((endpoint->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
190 dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
191 priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
192 }
193 if (((endpoint->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
194 ((endpoint->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
195 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{
220 int i, result = 0;
221 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
246 // set up internal termios structure
247 priv->internal_termios.c_iflag = port->tty->termios->c_iflag;
248 priv->internal_termios.c_oflag = port->tty->termios->c_oflag;
249 priv->internal_termios.c_cflag = port->tty->termios->c_cflag;
250 priv->internal_termios.c_lflag = port->tty->termios->c_lflag;
251
252 for (i=0; i<NCCS; i++) {
253 priv->internal_termios.c_cc[i] = port->tty->termios->c_cc[i];
254 }
255
256 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100257 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (! transfer_buffer) {
259 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
262 // allocate write_urb
263 if (!port->write_urb) {
264 dbg("%s - port %d Allocating port->write_urb", __FUNCTION__, port->number);
265 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
266 if (!port->write_urb) {
267 dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
268 kfree(transfer_buffer);
269 return -ENOMEM;
270 }
271 }
272
273 // allocate memory for write_urb transfer buffer
274 port->write_urb->transfer_buffer = (unsigned char *) kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
275 if (! port->write_urb->transfer_buffer) {
276 kfree(transfer_buffer);
277 usb_free_urb(port->write_urb);
278 port->write_urb = NULL;
279 return -ENOMEM;
280 }
281
282 // get hardware version
283 result = usb_control_msg( port->serial->dev,
284 usb_rcvctrlpipe(port->serial->dev, 0 ),
285 SUSBCRequest_GetMisc,
286 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
287 SUSBCR_MSC_GetHWVersion,
288 0,
289 transfer_buffer,
290 transfer_buffer_length,
291 KOBIL_TIMEOUT
292 );
293 dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
294 dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
295
296 // get firmware version
297 result = usb_control_msg( port->serial->dev,
298 usb_rcvctrlpipe(port->serial->dev, 0 ),
299 SUSBCRequest_GetMisc,
300 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
301 SUSBCR_MSC_GetFWVersion,
302 0,
303 transfer_buffer,
304 transfer_buffer_length,
305 KOBIL_TIMEOUT
306 );
307 dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
308 dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
309
310 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
311 // Setting Baudrate, Parity and Stopbits
312 result = usb_control_msg( port->serial->dev,
313 usb_rcvctrlpipe(port->serial->dev, 0 ),
314 SUSBCRequest_SetBaudRateParityAndStopBits,
315 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
316 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
317 0,
318 transfer_buffer,
319 0,
320 KOBIL_TIMEOUT
321 );
322 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
323
324 // reset all queues
325 result = usb_control_msg( port->serial->dev,
326 usb_rcvctrlpipe(port->serial->dev, 0 ),
327 SUSBCRequest_Misc,
328 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
329 SUSBCR_MSC_ResetAllQueues,
330 0,
331 transfer_buffer,
332 0,
333 KOBIL_TIMEOUT
334 );
335 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
336 }
337 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
338 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
339 // start reading (Adapter B 'cause PNP string)
340 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
341 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
342 }
343
344 kfree(transfer_buffer);
345 return 0;
346}
347
348
349static void kobil_close (struct usb_serial_port *port, struct file *filp)
350{
351 dbg("%s - port %d", __FUNCTION__, port->number);
352
353 if (port->write_urb) {
354 usb_kill_urb(port->write_urb);
355 usb_free_urb( port->write_urb );
356 port->write_urb = NULL;
357 }
358 if (port->interrupt_in_urb)
359 usb_kill_urb(port->interrupt_in_urb);
360}
361
362
363static void kobil_read_int_callback( struct urb *purb, struct pt_regs *regs)
364{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int result;
366 struct usb_serial_port *port = (struct usb_serial_port *) purb->context;
367 struct tty_struct *tty;
368 unsigned char *data = purb->transfer_buffer;
369// char *dbg_data;
370
371 dbg("%s - port %d", __FUNCTION__, port->number);
372
373 if (purb->status) {
374 dbg("%s - port %d Read int status not zero: %d", __FUNCTION__, port->number, purb->status);
375 return;
376 }
377
378 tty = port->tty;
379 if (purb->actual_length) {
380
381 // BEGIN DEBUG
382 /*
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100383 dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (! dbg_data) {
385 return;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 for (i = 0; i < purb->actual_length; i++) {
388 sprintf(dbg_data +3*i, "%02X ", data[i]);
389 }
390 dbg(" <-- %s", dbg_data );
391 kfree(dbg_data);
392 */
393 // END DEBUG
394
Alan Cox33f0f882006-01-09 20:54:13 -0800395 tty_buffer_request_room(tty, purb->actual_length);
396 tty_insert_flip_string(tty, data, purb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 tty_flip_buffer_push(tty);
398 }
399
400 // someone sets the dev to 0 if the close method has been called
401 port->interrupt_in_urb->dev = port->serial->dev;
402
403 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
404 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
405}
406
407
408static void kobil_write_callback( struct urb *purb, struct pt_regs *regs )
409{
410}
411
412
413static int kobil_write (struct usb_serial_port *port,
414 const unsigned char *buf, int count)
415{
416 int length = 0;
417 int result = 0;
418 int todo = 0;
419 struct kobil_private * priv;
420
421 if (count == 0) {
422 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
423 return 0;
424 }
425
426 priv = usb_get_serial_port_data(port);
427
428 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
429 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
430 return -ENOMEM;
431 }
432
433 // Copy data to buffer
434 memcpy (priv->buf + priv->filled, buf, count);
435
436 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
437
438 priv->filled = priv->filled + count;
439
440
441 // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
442 if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
443 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
444
445 // stop reading (except TWIN and KAAN SIM)
446 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
447 usb_kill_urb(port->interrupt_in_urb);
448
449 todo = priv->filled - priv->cur_pos;
450
451 while(todo > 0) {
452 // max 8 byte in one urb (endpoint size)
453 length = (todo < 8) ? todo : 8;
454 // copy data to transfer buffer
455 memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
456 usb_fill_int_urb( port->write_urb,
457 port->serial->dev,
458 usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
459 port->write_urb->transfer_buffer,
460 length,
461 kobil_write_callback,
462 port,
463 8
464 );
465
466 priv->cur_pos = priv->cur_pos + length;
467 result = usb_submit_urb( port->write_urb, GFP_NOIO );
468 dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
469 todo = priv->filled - priv->cur_pos;
470
471 if (todo > 0) {
472 msleep(24);
473 }
474
475 } // end while
476
477 priv->filled = 0;
478 priv->cur_pos = 0;
479
480 // someone sets the dev to 0 if the close method has been called
481 port->interrupt_in_urb->dev = port->serial->dev;
482
483 // start reading (except TWIN and KAAN SIM)
484 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
485 // someone sets the dev to 0 if the close method has been called
486 port->interrupt_in_urb->dev = port->serial->dev;
487
488 result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
489 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
490 }
491 }
492 return count;
493}
494
495
496static int kobil_write_room (struct usb_serial_port *port)
497{
498 //dbg("%s - port %d", __FUNCTION__, port->number);
499 return 8;
500}
501
502
503static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
504{
505 struct kobil_private * priv;
506 int result;
507 unsigned char *transfer_buffer;
508 int transfer_buffer_length = 8;
509
510 priv = usb_get_serial_port_data(port);
511 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
512 // This device doesn't support ioctl calls
513 return -EINVAL;
514 }
515
516 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100517 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!transfer_buffer) {
519 return -ENOMEM;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 result = usb_control_msg( port->serial->dev,
523 usb_rcvctrlpipe(port->serial->dev, 0 ),
524 SUSBCRequest_GetStatusLineState,
525 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
526 0,
527 0,
528 transfer_buffer,
529 transfer_buffer_length,
530 KOBIL_TIMEOUT);
531
532 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
533 __FUNCTION__, port->number, result, transfer_buffer[0]);
534
535 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) {
536 priv->line_state |= TIOCM_DSR;
537 } else {
538 priv->line_state &= ~TIOCM_DSR;
539 }
540
541 kfree(transfer_buffer);
542 return priv->line_state;
543}
544
545static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
546 unsigned int set, unsigned int clear)
547{
548 struct kobil_private * priv;
549 int result;
550 int dtr = 0;
551 int rts = 0;
552 unsigned char *transfer_buffer;
553 int transfer_buffer_length = 8;
554
555 priv = usb_get_serial_port_data(port);
556 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
557 // This device doesn't support ioctl calls
558 return -EINVAL;
559 }
560
561 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100562 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (! transfer_buffer) {
564 return -ENOMEM;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (set & TIOCM_RTS)
568 rts = 1;
569 if (set & TIOCM_DTR)
570 dtr = 1;
571 if (clear & TIOCM_RTS)
572 rts = 0;
573 if (clear & TIOCM_DTR)
574 dtr = 0;
575
576 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
577 if (dtr != 0)
578 dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
579 else
580 dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
581 result = usb_control_msg( port->serial->dev,
582 usb_rcvctrlpipe(port->serial->dev, 0 ),
583 SUSBCRequest_SetStatusLinesOrQueues,
584 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
585 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
586 0,
587 transfer_buffer,
588 0,
589 KOBIL_TIMEOUT);
590 } else {
591 if (rts != 0)
592 dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
593 else
594 dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
595 result = usb_control_msg( port->serial->dev,
596 usb_rcvctrlpipe(port->serial->dev, 0 ),
597 SUSBCRequest_SetStatusLinesOrQueues,
598 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
599 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
600 0,
601 transfer_buffer,
602 0,
603 KOBIL_TIMEOUT);
604 }
605 dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
606 kfree(transfer_buffer);
607 return (result < 0) ? result : 0;
608}
609
610
611static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
612 unsigned int cmd, unsigned long arg)
613{
614 struct kobil_private * priv;
615 int result;
616 unsigned short urb_val = 0;
617 unsigned char *transfer_buffer;
618 int transfer_buffer_length = 8;
619 char *settings;
620 void __user *user_arg = (void __user *)arg;
621
622 priv = usb_get_serial_port_data(port);
623 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
624 // This device doesn't support ioctl calls
625 return 0;
626 }
627
628 switch (cmd) {
629 case TCGETS: // 0x5401
630 if (!access_ok(VERIFY_WRITE, user_arg, sizeof(struct termios))) {
631 dbg("%s - port %d Error in access_ok", __FUNCTION__, port->number);
632 return -EFAULT;
633 }
634 if (kernel_termios_to_user_termios((struct termios __user *)arg,
635 &priv->internal_termios))
636 return -EFAULT;
637 return 0;
638
639 case TCSETS: // 0x5402
640 if (!(port->tty->termios)) {
641 dbg("%s - port %d Error: port->tty->termios is NULL", __FUNCTION__, port->number);
642 return -ENOTTY;
643 }
644 if (!access_ok(VERIFY_READ, user_arg, sizeof(struct termios))) {
645 dbg("%s - port %d Error in access_ok", __FUNCTION__, port->number);
646 return -EFAULT;
647 }
648 if (user_termios_to_kernel_termios(&priv->internal_termios,
649 (struct termios __user *)arg))
650 return -EFAULT;
651
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100652 settings = kzalloc(50, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (! settings) {
654 return -ENOBUFS;
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 switch (priv->internal_termios.c_cflag & CBAUD) {
658 case B1200:
659 urb_val = SUSBCR_SBR_1200;
660 strcat(settings, "1200 ");
661 break;
662 case B9600:
663 default:
664 urb_val = SUSBCR_SBR_9600;
665 strcat(settings, "9600 ");
666 break;
667 }
668
669 urb_val |= (priv->internal_termios.c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
670 strcat(settings, (priv->internal_termios.c_cflag & CSTOPB) ? "2 StopBits " : "1 StopBit ");
671
672 if (priv->internal_termios.c_cflag & PARENB) {
673 if (priv->internal_termios.c_cflag & PARODD) {
674 urb_val |= SUSBCR_SPASB_OddParity;
675 strcat(settings, "Odd Parity");
676 } else {
677 urb_val |= SUSBCR_SPASB_EvenParity;
678 strcat(settings, "Even Parity");
679 }
680 } else {
681 urb_val |= SUSBCR_SPASB_NoParity;
682 strcat(settings, "No Parity");
683 }
684 dbg("%s - port %d setting port to: %s", __FUNCTION__, port->number, settings );
685
686 result = usb_control_msg( port->serial->dev,
687 usb_rcvctrlpipe(port->serial->dev, 0 ),
688 SUSBCRequest_SetBaudRateParityAndStopBits,
689 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
690 urb_val,
691 0,
692 settings,
693 0,
694 KOBIL_TIMEOUT
695 );
696
697 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
698 kfree(settings);
699 return 0;
700
701 case TCFLSH: // 0x540B
702 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
703 if (! transfer_buffer) {
704 return -ENOBUFS;
705 }
706
707 result = usb_control_msg( port->serial->dev,
708 usb_rcvctrlpipe(port->serial->dev, 0 ),
709 SUSBCRequest_Misc,
710 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
711 SUSBCR_MSC_ResetAllQueues,
712 0,
713 NULL,//transfer_buffer,
714 0,
715 KOBIL_TIMEOUT
716 );
717
718 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
719
720 kfree(transfer_buffer);
721 return ((result < 0) ? -EFAULT : 0);
722
723 }
724 return -ENOIOCTLCMD;
725}
726
727
728static int __init kobil_init (void)
729{
730 int retval;
731 retval = usb_serial_register(&kobil_device);
732 if (retval)
733 goto failed_usb_serial_register;
734 retval = usb_register(&kobil_driver);
735 if (retval)
736 goto failed_usb_register;
737
738 info(DRIVER_VERSION " " DRIVER_AUTHOR);
739 info(DRIVER_DESC);
740
741 return 0;
742failed_usb_register:
743 usb_serial_deregister(&kobil_device);
744failed_usb_serial_register:
745 return retval;
746}
747
748
749static void __exit kobil_exit (void)
750{
751 usb_deregister (&kobil_driver);
752 usb_serial_deregister (&kobil_device);
753}
754
755module_init(kobil_init);
756module_exit(kobil_exit);
757
758MODULE_AUTHOR( DRIVER_AUTHOR );
759MODULE_DESCRIPTION( DRIVER_DESC );
760MODULE_LICENSE( "GPL" );
761
762module_param(debug, bool, S_IRUGO | S_IWUSR);
763MODULE_PARM_DESC(debug, "Debug enabled or not");