[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / usb / serial / kobil_sct.c
blobfe4c98a7517147717932cf69a0fb88f78141bfa9
1 /*
2 * KOBIL USB Smart Card Terminal Driver
4 * Copyright (C) 2002 KOBIL Systems GmbH
5 * Author: Thomas Wahrenbruch
7 * Contact: linuxusb@kobil.de
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.
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.
18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
19 * patience.
21 * Supported readers: USB TWIN, KAAN Standard Plus and SecOVID Reader Plus
22 * (Adapter K), B1 Professional and KAAN Professional (Adapter B)
24 * (21/05/2004) tw
25 * Fix bug with P'n'P readers
27 * (28/05/2003) tw
28 * Add support for KAAN SIM
30 * (12/09/2002) tw
31 * Adapted to 2.5.
33 * (11/08/2002) tw
34 * Initial version.
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/tty.h>
44 #include <linux/tty_driver.h>
45 #include <linux/tty_flip.h>
46 #include <linux/module.h>
47 #include <linux/spinlock.h>
48 #include <asm/uaccess.h>
49 #include <linux/usb.h>
50 #include <linux/ioctl.h>
51 #include "usb-serial.h"
52 #include "kobil_sct.h"
54 static int debug;
56 /* Version Information */
57 #define DRIVER_VERSION "21/05/2004"
58 #define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
59 #define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
61 #define KOBIL_VENDOR_ID 0x0D46
62 #define KOBIL_ADAPTER_B_PRODUCT_ID 0x2011
63 #define KOBIL_ADAPTER_K_PRODUCT_ID 0x2012
64 #define KOBIL_USBTWIN_PRODUCT_ID 0x0078
65 #define KOBIL_KAAN_SIM_PRODUCT_ID 0x0081
67 #define KOBIL_TIMEOUT 500
68 #define KOBIL_BUF_LENGTH 300
71 /* Function prototypes */
72 static int kobil_startup (struct usb_serial *serial);
73 static void kobil_shutdown (struct usb_serial *serial);
74 static int kobil_open (struct usb_serial_port *port, struct file *filp);
75 static void kobil_close (struct usb_serial_port *port, struct file *filp);
76 static int kobil_write (struct usb_serial_port *port,
77 const unsigned char *buf, int count);
78 static int kobil_write_room(struct usb_serial_port *port);
79 static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
80 unsigned int cmd, unsigned long arg);
81 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file);
82 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
83 unsigned int set, unsigned int clear);
84 static void kobil_read_int_callback( struct urb *urb, struct pt_regs *regs );
85 static void kobil_write_callback( struct urb *purb, struct pt_regs *regs );
88 static 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 */
97 MODULE_DEVICE_TABLE (usb, id_table);
99 static struct usb_driver kobil_driver = {
100 .owner = THIS_MODULE,
101 .name = "kobil",
102 .probe = usb_serial_probe,
103 .disconnect = usb_serial_disconnect,
104 .id_table = id_table,
108 static struct usb_serial_device_type kobil_device = {
109 .owner = THIS_MODULE,
110 .name = "KOBIL USB smart card terminal",
111 .id_table = id_table,
112 .num_interrupt_in = NUM_DONT_CARE,
113 .num_bulk_in = 0,
114 .num_bulk_out = 0,
115 .num_ports = 1,
116 .attach = kobil_startup,
117 .shutdown = kobil_shutdown,
118 .ioctl = kobil_ioctl,
119 .tiocmget = kobil_tiocmget,
120 .tiocmset = kobil_tiocmset,
121 .open = kobil_open,
122 .close = kobil_close,
123 .write = kobil_write,
124 .write_room = kobil_write_room,
125 .read_int_callback = kobil_read_int_callback,
129 struct kobil_private {
130 int write_int_endpoint_address;
131 int read_int_endpoint_address;
132 unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
133 int filled; // index of the last char in buf
134 int cur_pos; // index of the next char to send in buf
135 __u16 device_type;
136 int line_state;
137 struct termios internal_termios;
141 static int kobil_startup (struct usb_serial *serial)
143 int i;
144 struct kobil_private *priv;
145 struct usb_device *pdev;
146 struct usb_host_config *actconfig;
147 struct usb_interface *interface;
148 struct usb_host_interface *altsetting;
149 struct usb_host_endpoint *endpoint;
151 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
152 if (!priv){
153 return -ENOMEM;
156 priv->filled = 0;
157 priv->cur_pos = 0;
158 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
159 priv->line_state = 0;
161 switch (priv->device_type){
162 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:
166 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
167 break;
168 case KOBIL_USBTWIN_PRODUCT_ID:
169 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
170 break;
171 case KOBIL_KAAN_SIM_PRODUCT_ID:
172 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
173 break;
175 usb_set_serial_port_data(serial->port[0], priv);
177 // search for the necessary endpoints
178 pdev = serial->dev;
179 actconfig = pdev->actconfig;
180 interface = actconfig->interface[0];
181 altsetting = interface->cur_altsetting;
182 endpoint = altsetting->endpoint;
184 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
185 endpoint = &altsetting->endpoint[i];
186 if (((endpoint->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
187 ((endpoint->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
188 dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
189 priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
191 if (((endpoint->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
192 ((endpoint->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
193 dbg("%s Found interrupt in endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
194 priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
197 return 0;
201 static void kobil_shutdown (struct usb_serial *serial)
203 int i;
204 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
206 for (i=0; i < serial->num_ports; ++i) {
207 while (serial->port[i]->open_count > 0) {
208 kobil_close (serial->port[i], NULL);
210 kfree(usb_get_serial_port_data(serial->port[i]));
211 usb_set_serial_port_data(serial->port[i], NULL);
216 static int kobil_open (struct usb_serial_port *port, struct file *filp)
218 int i, result = 0;
219 struct kobil_private *priv;
220 unsigned char *transfer_buffer;
221 int transfer_buffer_length = 8;
222 int write_urb_transfer_buffer_length = 8;
224 dbg("%s - port %d", __FUNCTION__, port->number);
225 priv = usb_get_serial_port_data(port);
226 priv->line_state = 0;
228 // someone sets the dev to 0 if the close method has been called
229 port->interrupt_in_urb->dev = port->serial->dev;
232 /* force low_latency on so that our tty_push actually forces
233 * the data through, otherwise it is scheduled, and with high
234 * data rates (like with OHCI) data can get lost.
236 port->tty->low_latency = 1;
238 // without this, every push_tty_char is echoed :-(
239 port->tty->termios->c_lflag = 0;
240 port->tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
241 port->tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
242 port->tty->termios->c_oflag &= ~ONLCR; // do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D)
244 // set up internal termios structure
245 priv->internal_termios.c_iflag = port->tty->termios->c_iflag;
246 priv->internal_termios.c_oflag = port->tty->termios->c_oflag;
247 priv->internal_termios.c_cflag = port->tty->termios->c_cflag;
248 priv->internal_termios.c_lflag = port->tty->termios->c_lflag;
250 for (i=0; i<NCCS; i++) {
251 priv->internal_termios.c_cc[i] = port->tty->termios->c_cc[i];
254 // allocate memory for transfer buffer
255 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
256 if (! transfer_buffer) {
257 return -ENOMEM;
258 } else {
259 memset(transfer_buffer, 0, transfer_buffer_length);
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;
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;
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,
289 transfer_buffer,
290 transfer_buffer_length,
291 KOBIL_TIMEOUT
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] );
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,
303 transfer_buffer,
304 transfer_buffer_length,
305 KOBIL_TIMEOUT
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] );
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,
318 transfer_buffer,
320 KOBIL_TIMEOUT
322 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
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,
331 transfer_buffer,
333 KOBIL_TIMEOUT
335 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
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);
344 kfree(transfer_buffer);
345 return 0;
349 static void kobil_close (struct usb_serial_port *port, struct file *filp)
351 dbg("%s - port %d", __FUNCTION__, port->number);
353 if (port->write_urb) {
354 usb_kill_urb(port->write_urb);
355 usb_free_urb( port->write_urb );
356 port->write_urb = NULL;
358 if (port->interrupt_in_urb)
359 usb_kill_urb(port->interrupt_in_urb);
363 static void kobil_read_int_callback( struct urb *purb, struct pt_regs *regs)
365 int i;
366 int result;
367 struct usb_serial_port *port = (struct usb_serial_port *) purb->context;
368 struct tty_struct *tty;
369 unsigned char *data = purb->transfer_buffer;
370 // char *dbg_data;
372 dbg("%s - port %d", __FUNCTION__, port->number);
374 if (purb->status) {
375 dbg("%s - port %d Read int status not zero: %d", __FUNCTION__, port->number, purb->status);
376 return;
379 tty = port->tty;
380 if (purb->actual_length) {
382 // BEGIN DEBUG
384 dbg_data = (unsigned char *) kmalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
385 if (! dbg_data) {
386 return;
388 memset(dbg_data, 0, (3 * purb->actual_length + 10));
389 for (i = 0; i < purb->actual_length; i++) {
390 sprintf(dbg_data +3*i, "%02X ", data[i]);
392 dbg(" <-- %s", dbg_data );
393 kfree(dbg_data);
395 // END DEBUG
397 for (i = 0; i < purb->actual_length; ++i) {
398 // if we insert more than TTY_FLIPBUF_SIZE characters, we drop them.
399 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
400 tty_flip_buffer_push(tty);
402 // this doesn't actually push the data through unless tty->low_latency is set
403 tty_insert_flip_char(tty, data[i], 0);
405 tty_flip_buffer_push(tty);
408 // someone sets the dev to 0 if the close method has been called
409 port->interrupt_in_urb->dev = port->serial->dev;
411 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
412 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
416 static void kobil_write_callback( struct urb *purb, struct pt_regs *regs )
421 static int kobil_write (struct usb_serial_port *port,
422 const unsigned char *buf, int count)
424 int length = 0;
425 int result = 0;
426 int todo = 0;
427 struct kobil_private * priv;
429 if (count == 0) {
430 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
431 return 0;
434 priv = usb_get_serial_port_data(port);
436 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
437 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
438 return -ENOMEM;
441 // Copy data to buffer
442 memcpy (priv->buf + priv->filled, buf, count);
444 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
446 priv->filled = priv->filled + count;
449 // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
450 if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
451 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
453 // stop reading (except TWIN and KAAN SIM)
454 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
455 usb_kill_urb(port->interrupt_in_urb);
457 todo = priv->filled - priv->cur_pos;
459 while(todo > 0) {
460 // max 8 byte in one urb (endpoint size)
461 length = (todo < 8) ? todo : 8;
462 // copy data to transfer buffer
463 memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
464 usb_fill_int_urb( port->write_urb,
465 port->serial->dev,
466 usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
467 port->write_urb->transfer_buffer,
468 length,
469 kobil_write_callback,
470 port,
474 priv->cur_pos = priv->cur_pos + length;
475 result = usb_submit_urb( port->write_urb, GFP_NOIO );
476 dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
477 todo = priv->filled - priv->cur_pos;
479 if (todo > 0) {
480 msleep(24);
483 } // end while
485 priv->filled = 0;
486 priv->cur_pos = 0;
488 // someone sets the dev to 0 if the close method has been called
489 port->interrupt_in_urb->dev = port->serial->dev;
491 // start reading (except TWIN and KAAN SIM)
492 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
493 // someone sets the dev to 0 if the close method has been called
494 port->interrupt_in_urb->dev = port->serial->dev;
496 result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
497 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
500 return count;
504 static int kobil_write_room (struct usb_serial_port *port)
506 //dbg("%s - port %d", __FUNCTION__, port->number);
507 return 8;
511 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
513 struct kobil_private * priv;
514 int result;
515 unsigned char *transfer_buffer;
516 int transfer_buffer_length = 8;
518 priv = usb_get_serial_port_data(port);
519 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
520 // This device doesn't support ioctl calls
521 return -EINVAL;
524 // allocate memory for transfer buffer
525 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
526 if (!transfer_buffer) {
527 return -ENOMEM;
529 memset(transfer_buffer, 0, transfer_buffer_length);
531 result = usb_control_msg( port->serial->dev,
532 usb_rcvctrlpipe(port->serial->dev, 0 ),
533 SUSBCRequest_GetStatusLineState,
534 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
537 transfer_buffer,
538 transfer_buffer_length,
539 KOBIL_TIMEOUT);
541 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
542 __FUNCTION__, port->number, result, transfer_buffer[0]);
544 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) {
545 priv->line_state |= TIOCM_DSR;
546 } else {
547 priv->line_state &= ~TIOCM_DSR;
550 kfree(transfer_buffer);
551 return priv->line_state;
554 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
555 unsigned int set, unsigned int clear)
557 struct kobil_private * priv;
558 int result;
559 int dtr = 0;
560 int rts = 0;
561 unsigned char *transfer_buffer;
562 int transfer_buffer_length = 8;
564 priv = usb_get_serial_port_data(port);
565 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
566 // This device doesn't support ioctl calls
567 return -EINVAL;
570 // allocate memory for transfer buffer
571 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
572 if (! transfer_buffer) {
573 return -ENOMEM;
575 memset(transfer_buffer, 0, transfer_buffer_length);
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;
586 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
587 if (dtr != 0)
588 dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
589 else
590 dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
591 result = usb_control_msg( port->serial->dev,
592 usb_rcvctrlpipe(port->serial->dev, 0 ),
593 SUSBCRequest_SetStatusLinesOrQueues,
594 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
595 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
597 transfer_buffer,
599 KOBIL_TIMEOUT);
600 } else {
601 if (rts != 0)
602 dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
603 else
604 dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
605 result = usb_control_msg( port->serial->dev,
606 usb_rcvctrlpipe(port->serial->dev, 0 ),
607 SUSBCRequest_SetStatusLinesOrQueues,
608 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
609 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
611 transfer_buffer,
613 KOBIL_TIMEOUT);
615 dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
616 kfree(transfer_buffer);
617 return (result < 0) ? result : 0;
621 static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
622 unsigned int cmd, unsigned long arg)
624 struct kobil_private * priv;
625 int result;
626 unsigned short urb_val = 0;
627 unsigned char *transfer_buffer;
628 int transfer_buffer_length = 8;
629 char *settings;
630 void __user *user_arg = (void __user *)arg;
632 priv = usb_get_serial_port_data(port);
633 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
634 // This device doesn't support ioctl calls
635 return 0;
638 switch (cmd) {
639 case TCGETS: // 0x5401
640 if (!access_ok(VERIFY_WRITE, user_arg, sizeof(struct termios))) {
641 dbg("%s - port %d Error in access_ok", __FUNCTION__, port->number);
642 return -EFAULT;
644 if (kernel_termios_to_user_termios((struct termios __user *)arg,
645 &priv->internal_termios))
646 return -EFAULT;
647 return 0;
649 case TCSETS: // 0x5402
650 if (!(port->tty->termios)) {
651 dbg("%s - port %d Error: port->tty->termios is NULL", __FUNCTION__, port->number);
652 return -ENOTTY;
654 if (!access_ok(VERIFY_READ, user_arg, sizeof(struct termios))) {
655 dbg("%s - port %d Error in access_ok", __FUNCTION__, port->number);
656 return -EFAULT;
658 if (user_termios_to_kernel_termios(&priv->internal_termios,
659 (struct termios __user *)arg))
660 return -EFAULT;
662 settings = (unsigned char *) kmalloc(50, GFP_KERNEL);
663 if (! settings) {
664 return -ENOBUFS;
666 memset(settings, 0, 50);
668 switch (priv->internal_termios.c_cflag & CBAUD) {
669 case B1200:
670 urb_val = SUSBCR_SBR_1200;
671 strcat(settings, "1200 ");
672 break;
673 case B9600:
674 default:
675 urb_val = SUSBCR_SBR_9600;
676 strcat(settings, "9600 ");
677 break;
680 urb_val |= (priv->internal_termios.c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
681 strcat(settings, (priv->internal_termios.c_cflag & CSTOPB) ? "2 StopBits " : "1 StopBit ");
683 if (priv->internal_termios.c_cflag & PARENB) {
684 if (priv->internal_termios.c_cflag & PARODD) {
685 urb_val |= SUSBCR_SPASB_OddParity;
686 strcat(settings, "Odd Parity");
687 } else {
688 urb_val |= SUSBCR_SPASB_EvenParity;
689 strcat(settings, "Even Parity");
691 } else {
692 urb_val |= SUSBCR_SPASB_NoParity;
693 strcat(settings, "No Parity");
695 dbg("%s - port %d setting port to: %s", __FUNCTION__, port->number, settings );
697 result = usb_control_msg( port->serial->dev,
698 usb_rcvctrlpipe(port->serial->dev, 0 ),
699 SUSBCRequest_SetBaudRateParityAndStopBits,
700 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
701 urb_val,
703 settings,
705 KOBIL_TIMEOUT
708 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
709 kfree(settings);
710 return 0;
712 case TCFLSH: // 0x540B
713 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
714 if (! transfer_buffer) {
715 return -ENOBUFS;
718 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,
724 NULL,//transfer_buffer,
726 KOBIL_TIMEOUT
729 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
731 kfree(transfer_buffer);
732 return ((result < 0) ? -EFAULT : 0);
735 return -ENOIOCTLCMD;
739 static int __init kobil_init (void)
741 int retval;
742 retval = usb_serial_register(&kobil_device);
743 if (retval)
744 goto failed_usb_serial_register;
745 retval = usb_register(&kobil_driver);
746 if (retval)
747 goto failed_usb_register;
749 info(DRIVER_VERSION " " DRIVER_AUTHOR);
750 info(DRIVER_DESC);
752 return 0;
753 failed_usb_register:
754 usb_serial_deregister(&kobil_device);
755 failed_usb_serial_register:
756 return retval;
760 static void __exit kobil_exit (void)
762 usb_deregister (&kobil_driver);
763 usb_serial_deregister (&kobil_device);
766 module_init(kobil_init);
767 module_exit(kobil_exit);
769 MODULE_AUTHOR( DRIVER_AUTHOR );
770 MODULE_DESCRIPTION( DRIVER_DESC );
771 MODULE_LICENSE( "GPL" );
773 module_param(debug, bool, S_IRUGO | S_IWUSR);
774 MODULE_PARM_DESC(debug, "Debug enabled or not");