]> err.no Git - linux-2.6/blob - drivers/usb/serial/kobil_sct.c
USB: Remove EXPERIMENTAL designation from USB serial/ Kconfig entries
[linux-2.6] / drivers / usb / serial / kobil_sct.c
1 /*
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
38 #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>
49 #include <linux/usb/serial.h>
50 #include <linux/ioctl.h>
51 #include "kobil_sct.h"
52
53 static 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 */
71 static int  kobil_startup (struct usb_serial *serial);
72 static void kobil_shutdown (struct usb_serial *serial);
73 static int  kobil_open (struct usb_serial_port *port, struct file *filp);
74 static void kobil_close (struct usb_serial_port *port, struct file *filp);
75 static int  kobil_write (struct usb_serial_port *port, 
76                          const unsigned char *buf, int count);
77 static int  kobil_write_room(struct usb_serial_port *port);
78 static int  kobil_ioctl(struct usb_serial_port *port, struct file *file,
79                         unsigned int cmd, unsigned long arg);
80 static int  kobil_tiocmget(struct usb_serial_port *port, struct file *file);
81 static int  kobil_tiocmset(struct usb_serial_port *port, struct file *file,
82                            unsigned int set, unsigned int clear);
83 static void kobil_read_int_callback( struct urb *urb );
84 static void kobil_write_callback( struct urb *purb );
85 static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old);
86
87
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 */
94 };
95
96
97 MODULE_DEVICE_TABLE (usb, id_table);
98
99 static struct usb_driver kobil_driver = {
100         .name =         "kobil",
101         .probe =        usb_serial_probe,
102         .disconnect =   usb_serial_disconnect,
103         .id_table =     id_table,
104         .no_dynamic_id =        1,
105 };
106
107
108 static struct usb_serial_driver kobil_device = {
109         .driver = {
110                 .owner =        THIS_MODULE,
111                 .name =         "kobil",
112         },
113         .description =          "KOBIL USB smart card terminal",
114         .usb_driver =           &kobil_driver,
115         .id_table =             id_table,
116         .num_interrupt_in =     NUM_DONT_CARE,
117         .num_interrupt_out =    NUM_DONT_CARE,
118         .num_bulk_in =          0,
119         .num_bulk_out =         0,
120         .num_ports =            1,
121         .attach =               kobil_startup,
122         .shutdown =             kobil_shutdown,
123         .ioctl =                kobil_ioctl,
124         .set_termios =          kobil_set_termios,
125         .tiocmget =             kobil_tiocmget,
126         .tiocmset =             kobil_tiocmset,
127         .open =                 kobil_open,
128         .close =                kobil_close,
129         .write =                kobil_write,
130         .write_room =           kobil_write_room,
131         .read_int_callback =    kobil_read_int_callback,
132 };
133
134
135 struct kobil_private {
136         int write_int_endpoint_address;
137         int read_int_endpoint_address;
138         unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
139         int filled;  // index of the last char in buf
140         int cur_pos; // index of the next char to send in buf
141         __u16 device_type;
142 };
143
144
145 static 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
164         switch (priv->device_type){
165         case KOBIL_ADAPTER_B_PRODUCT_ID:
166                 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
167                 break;
168         case KOBIL_ADAPTER_K_PRODUCT_ID:
169                 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
170                 break;
171         case KOBIL_USBTWIN_PRODUCT_ID:
172                 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
173                 break;
174         case KOBIL_KAAN_SIM_PRODUCT_ID:
175                 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
176                 break;
177         }
178         usb_set_serial_port_data(serial->port[0], priv);
179
180         // search for the necessary endpoints
181         pdev = serial->dev;
182         actconfig = pdev->actconfig;
183         interface = actconfig->interface[0];
184         altsetting = interface->cur_altsetting;
185         endpoint = altsetting->endpoint;
186   
187         for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
188                 endpoint = &altsetting->endpoint[i];
189                 if (usb_endpoint_is_int_out(&endpoint->desc)) {
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 (usb_endpoint_is_int_in(&endpoint->desc)) {
194                         dbg("%s Found interrupt in  endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
195                         priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
196                 }
197         }
198         return 0;
199 }
200
201
202 static void kobil_shutdown (struct usb_serial *serial)
203 {
204         int i;
205         dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
206
207         for (i=0; i < serial->num_ports; ++i) {
208                 while (serial->port[i]->open_count > 0) {
209                         kobil_close (serial->port[i], NULL);
210                 }
211                 kfree(usb_get_serial_port_data(serial->port[i]));
212                 usb_set_serial_port_data(serial->port[i], NULL);
213         }
214 }
215
216
217 static int kobil_open (struct usb_serial_port *port, struct file *filp)
218 {
219         int result = 0;
220         struct kobil_private *priv;
221         unsigned char *transfer_buffer;
222         int transfer_buffer_length = 8;
223         int write_urb_transfer_buffer_length = 8;
224
225         dbg("%s - port %d", __FUNCTION__, port->number);
226         priv = usb_get_serial_port_data(port);
227
228         // someone sets the dev to 0 if the close method has been called
229         port->interrupt_in_urb->dev = port->serial->dev;
230
231
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.
235          */
236         port->tty->low_latency = 1;
237
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)
243         
244         // allocate memory for transfer buffer
245         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
246         if (! transfer_buffer) {
247                 return -ENOMEM;
248         }
249         
250         // allocate write_urb
251         if (!port->write_urb) { 
252                 dbg("%s - port %d  Allocating port->write_urb", __FUNCTION__, port->number);
253                 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);  
254                 if (!port->write_urb) {
255                         dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
256                         kfree(transfer_buffer);
257                         return -ENOMEM;
258                 }
259         }
260
261         // allocate memory for write_urb transfer buffer
262         port->write_urb->transfer_buffer = kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
263         if (! port->write_urb->transfer_buffer) {
264                 kfree(transfer_buffer);
265                 usb_free_urb(port->write_urb);
266                 port->write_urb = NULL;
267                 return -ENOMEM;
268         } 
269
270         // get hardware version
271         result = usb_control_msg( port->serial->dev, 
272                                   usb_rcvctrlpipe(port->serial->dev, 0 ), 
273                                   SUSBCRequest_GetMisc,
274                                   USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
275                                   SUSBCR_MSC_GetHWVersion,
276                                   0,
277                                   transfer_buffer,
278                                   transfer_buffer_length,
279                                   KOBIL_TIMEOUT
280                 );
281         dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
282         dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
283         
284         // get firmware version
285         result = usb_control_msg( port->serial->dev, 
286                                   usb_rcvctrlpipe(port->serial->dev, 0 ), 
287                                   SUSBCRequest_GetMisc,
288                                   USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
289                                   SUSBCR_MSC_GetFWVersion,
290                                   0,
291                                   transfer_buffer,
292                                   transfer_buffer_length,
293                                   KOBIL_TIMEOUT
294                 );
295         dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
296         dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
297
298         if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
299                 // Setting Baudrate, Parity and Stopbits
300                 result = usb_control_msg( port->serial->dev, 
301                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
302                                           SUSBCRequest_SetBaudRateParityAndStopBits,
303                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
304                                           SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
305                                           0,
306                                           transfer_buffer,
307                                           0,
308                                           KOBIL_TIMEOUT
309                         );
310                 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
311                 
312                 // reset all queues
313                 result = usb_control_msg( port->serial->dev, 
314                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
315                                           SUSBCRequest_Misc,
316                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
317                                           SUSBCR_MSC_ResetAllQueues,
318                                           0,
319                                           transfer_buffer,
320                                           0,
321                                           KOBIL_TIMEOUT
322                         );
323                 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
324         }
325         if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
326             priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
327                 // start reading (Adapter B 'cause PNP string)
328                 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC  ); 
329                 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
330         }
331
332         kfree(transfer_buffer);
333         return 0;
334 }
335
336
337 static void kobil_close (struct usb_serial_port *port, struct file *filp)
338 {
339         dbg("%s - port %d", __FUNCTION__, port->number);
340
341         if (port->write_urb) {
342                 usb_kill_urb(port->write_urb);
343                 usb_free_urb( port->write_urb );
344                 port->write_urb = NULL;
345         }
346         usb_kill_urb(port->interrupt_in_urb);
347 }
348
349
350 static void kobil_read_int_callback(struct urb *urb)
351 {
352         int result;
353         struct usb_serial_port *port = urb->context;
354         struct tty_struct *tty;
355         unsigned char *data = urb->transfer_buffer;
356         int status = urb->status;
357 //      char *dbg_data;
358
359         dbg("%s - port %d", __FUNCTION__, port->number);
360
361         if (status) {
362                 dbg("%s - port %d Read int status not zero: %d",
363                     __FUNCTION__, port->number, status);
364                 return;
365         }
366
367         tty = port->tty;
368         if (urb->actual_length) {
369
370                 // BEGIN DEBUG
371                 /*
372                   dbg_data = kzalloc((3 *  purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
373                   if (! dbg_data) {
374                   return;
375                   }
376                   for (i = 0; i < purb->actual_length; i++) { 
377                   sprintf(dbg_data +3*i, "%02X ", data[i]); 
378                   }
379                   dbg(" <-- %s", dbg_data );
380                   kfree(dbg_data);
381                 */
382                 // END DEBUG
383
384                 tty_buffer_request_room(tty, urb->actual_length);
385                 tty_insert_flip_string(tty, data, urb->actual_length);
386                 tty_flip_buffer_push(tty);
387         }
388
389         // someone sets the dev to 0 if the close method has been called
390         port->interrupt_in_urb->dev = port->serial->dev;
391
392         result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
393         dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
394 }
395
396
397 static void kobil_write_callback( struct urb *purb )
398 {
399 }
400
401
402 static int kobil_write (struct usb_serial_port *port, 
403                         const unsigned char *buf, int count)
404 {
405         int length = 0;
406         int result = 0;
407         int todo = 0;
408         struct kobil_private * priv;
409
410         if (count == 0) {
411                 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
412                 return 0;
413         }
414
415         priv = usb_get_serial_port_data(port);
416
417         if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
418                 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
419                 return -ENOMEM;
420         }
421
422         // Copy data to buffer
423         memcpy (priv->buf + priv->filled, buf, count);
424
425         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
426
427         priv->filled = priv->filled + count;
428
429
430         // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
431         if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) || 
432              ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
433                 
434                 // stop reading (except TWIN and KAAN SIM)
435                 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
436                         usb_kill_urb(port->interrupt_in_urb);
437
438                 todo = priv->filled - priv->cur_pos;
439
440                 while(todo > 0) {
441                         // max 8 byte in one urb (endpoint size)
442                         length = (todo < 8) ? todo : 8;
443                         // copy data to transfer buffer
444                         memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
445                         usb_fill_int_urb( port->write_urb,
446                                           port->serial->dev,
447                                           usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
448                                           port->write_urb->transfer_buffer,
449                                           length,
450                                           kobil_write_callback,
451                                           port,
452                                           8
453                                 );
454
455                         priv->cur_pos = priv->cur_pos + length;
456                         result = usb_submit_urb( port->write_urb, GFP_NOIO );
457                         dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
458                         todo = priv->filled - priv->cur_pos;
459
460                         if (todo > 0) {
461                                 msleep(24);
462                         }
463
464                 } // end while
465                 
466                 priv->filled = 0;
467                 priv->cur_pos = 0;
468
469                 // someone sets the dev to 0 if the close method has been called
470                 port->interrupt_in_urb->dev = port->serial->dev;
471                 
472                 // start reading (except TWIN and KAAN SIM)
473                 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
474                         // someone sets the dev to 0 if the close method has been called
475                         port->interrupt_in_urb->dev = port->serial->dev;
476                         
477                         result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO ); 
478                         dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
479                 }
480         }
481         return count;
482 }
483
484
485 static int kobil_write_room (struct usb_serial_port *port)
486 {
487         //dbg("%s - port %d", __FUNCTION__, port->number);
488         return 8;
489 }
490
491
492 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
493 {
494         struct kobil_private * priv;
495         int result;
496         unsigned char *transfer_buffer;
497         int transfer_buffer_length = 8;
498
499         priv = usb_get_serial_port_data(port);
500         if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
501                 // This device doesn't support ioctl calls
502                 return -EINVAL;
503         }
504
505         // allocate memory for transfer buffer
506         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
507         if (!transfer_buffer) {
508                 return -ENOMEM;
509         }
510
511         result = usb_control_msg( port->serial->dev, 
512                                   usb_rcvctrlpipe(port->serial->dev, 0 ), 
513                                   SUSBCRequest_GetStatusLineState,
514                                   USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
515                                   0,
516                                   0,
517                                   transfer_buffer,
518                                   transfer_buffer_length,
519                                   KOBIL_TIMEOUT);
520
521         dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x", 
522             __FUNCTION__, port->number, result, transfer_buffer[0]);
523
524         result = 0;
525         if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
526                 result = TIOCM_DSR;
527         kfree(transfer_buffer);
528         return result;
529 }
530
531 static int  kobil_tiocmset(struct usb_serial_port *port, struct file *file,
532                            unsigned int set, unsigned int clear)
533 {
534         struct kobil_private * priv;
535         int result;
536         int dtr = 0;
537         int rts = 0;
538         unsigned char *transfer_buffer;
539         int transfer_buffer_length = 8;
540
541         /* FIXME: locking ? */
542         priv = usb_get_serial_port_data(port);
543         if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
544                 // This device doesn't support ioctl calls
545                 return -EINVAL;
546         }
547
548         // allocate memory for transfer buffer
549         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
550         if (! transfer_buffer) {
551                 return -ENOMEM;
552         }
553
554         if (set & TIOCM_RTS)
555                 rts = 1;
556         if (set & TIOCM_DTR)
557                 dtr = 1;
558         if (clear & TIOCM_RTS)
559                 rts = 0;
560         if (clear & TIOCM_DTR)
561                 dtr = 0;
562
563         if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
564                 if (dtr != 0)
565                         dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
566                 else
567                         dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
568                 result = usb_control_msg( port->serial->dev, 
569                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
570                                           SUSBCRequest_SetStatusLinesOrQueues,
571                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
572                                           ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
573                                           0,
574                                           transfer_buffer,
575                                           0,
576                                           KOBIL_TIMEOUT);
577         } else {
578                 if (rts != 0)
579                         dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
580                 else
581                         dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
582                 result = usb_control_msg( port->serial->dev, 
583                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
584                                           SUSBCRequest_SetStatusLinesOrQueues,
585                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
586                                           ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
587                                           0,
588                                           transfer_buffer,
589                                           0,
590                                           KOBIL_TIMEOUT);
591         }
592         dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
593         kfree(transfer_buffer);
594         return (result < 0) ? result : 0;
595 }
596
597 static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old)
598 {
599         struct kobil_private * priv;
600         int result;
601         unsigned short urb_val = 0;
602         int c_cflag = port->tty->termios->c_cflag;
603         speed_t speed;
604         void * settings;
605
606         priv = usb_get_serial_port_data(port);
607         if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
608                 // This device doesn't support ioctl calls
609                 return;
610
611         switch (speed = tty_get_baud_rate(port->tty)) {
612                 case 1200:
613                         urb_val = SUSBCR_SBR_1200;
614                         break;
615                 default:
616                         speed = 9600;
617                 case 9600:
618                         urb_val = SUSBCR_SBR_9600;
619                         break;
620         }
621         urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
622
623         settings = kzalloc(50, GFP_KERNEL);
624         if (! settings)
625                 return;
626
627         sprintf(settings, "%d ", speed);
628
629         if (c_cflag & PARENB) {
630                 if  (c_cflag & PARODD) {
631                         urb_val |= SUSBCR_SPASB_OddParity;
632                         strcat(settings, "Odd Parity");
633                 } else {
634                         urb_val |= SUSBCR_SPASB_EvenParity;
635                         strcat(settings, "Even Parity");
636                 }
637         } else {
638                 urb_val |= SUSBCR_SPASB_NoParity;
639                 strcat(settings, "No Parity");
640         }
641         port->tty->termios->c_cflag &= ~CMSPAR;
642         tty_encode_baud_rate(port->tty, speed, speed);
643
644         result = usb_control_msg( port->serial->dev,
645                                   usb_rcvctrlpipe(port->serial->dev, 0 ),
646                                   SUSBCRequest_SetBaudRateParityAndStopBits,
647                                   USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
648                                   urb_val,
649                                   0,
650                                   settings,
651                                   0,
652                                   KOBIL_TIMEOUT
653                 );
654         kfree(settings);
655 }
656
657 static int kobil_ioctl(struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
658 {
659         struct kobil_private * priv = usb_get_serial_port_data(port);
660         unsigned char *transfer_buffer;
661         int transfer_buffer_length = 8;
662         int result;
663
664         if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
665                 // This device doesn't support ioctl calls
666                 return 0;
667
668         switch (cmd) {
669         case TCFLSH:   // 0x540B
670                 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
671                 if (! transfer_buffer)
672                         return -ENOBUFS;
673
674                 result = usb_control_msg( port->serial->dev, 
675                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
676                                           SUSBCRequest_Misc,
677                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
678                                           SUSBCR_MSC_ResetAllQueues,
679                                           0,
680                                           NULL,//transfer_buffer,
681                                           0,
682                                           KOBIL_TIMEOUT
683                         );
684                 
685                 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
686                 kfree(transfer_buffer);
687                 return (result < 0) ? -EFAULT : 0;
688         default:
689                 return -ENOIOCTLCMD;
690         }
691 }
692
693 static int __init kobil_init (void)
694 {
695         int retval;
696         retval = usb_serial_register(&kobil_device);
697         if (retval)
698                 goto failed_usb_serial_register;
699         retval = usb_register(&kobil_driver);
700         if (retval) 
701                 goto failed_usb_register;
702
703         info(DRIVER_VERSION " " DRIVER_AUTHOR);
704         info(DRIVER_DESC);
705
706         return 0;
707 failed_usb_register:
708         usb_serial_deregister(&kobil_device);
709 failed_usb_serial_register:
710         return retval;
711 }
712
713
714 static void __exit kobil_exit (void)
715 {
716         usb_deregister (&kobil_driver);
717         usb_serial_deregister (&kobil_device);
718 }
719
720 module_init(kobil_init);
721 module_exit(kobil_exit);
722
723 MODULE_AUTHOR( DRIVER_AUTHOR );
724 MODULE_DESCRIPTION( DRIVER_DESC );
725 MODULE_LICENSE( "GPL" );
726
727 module_param(debug, bool, S_IRUGO | S_IWUSR);
728 MODULE_PARM_DESC(debug, "Debug enabled or not");