Merge branch 'upstream-jeff' of git://git.kernel.org/pub/scm/linux/kernel/git/romieu...
[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_bulk_in =          0,
118         .num_bulk_out =         0,
119         .num_ports =            1,
120         .attach =               kobil_startup,
121         .shutdown =             kobil_shutdown,
122         .ioctl =                kobil_ioctl,
123         .set_termios =          kobil_set_termios,
124         .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
134 struct 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;
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         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];
190                 if (usb_endpoint_is_int_out(&endpoint->desc)) {
191                         dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
192                         priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
193                 }
194                 if (usb_endpoint_is_int_in(&endpoint->desc)) {
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
203 static 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
218 static int kobil_open (struct usb_serial_port *port, struct file *filp)
219 {
220         int 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         // allocate memory for transfer buffer
247         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
248         if (! transfer_buffer) {
249                 return -ENOMEM;
250         }
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
264         port->write_urb->transfer_buffer = kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
265         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
339 static 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         }
348         usb_kill_urb(port->interrupt_in_urb);
349 }
350
351
352 static void kobil_read_int_callback(struct urb *urb)
353 {
354         int result;
355         struct usb_serial_port *port = urb->context;
356         struct tty_struct *tty;
357         unsigned char *data = urb->transfer_buffer;
358         int status = urb->status;
359 //      char *dbg_data;
360
361         dbg("%s - port %d", __FUNCTION__, port->number);
362
363         if (status) {
364                 dbg("%s - port %d Read int status not zero: %d",
365                     __FUNCTION__, port->number, status);
366                 return;
367         }
368
369         tty = port->tty;
370         if (urb->actual_length) {
371
372                 // BEGIN DEBUG
373                 /*
374                   dbg_data = kzalloc((3 *  purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
375                   if (! dbg_data) {
376                   return;
377                   }
378                   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
386                 tty_buffer_request_room(tty, urb->actual_length);
387                 tty_insert_flip_string(tty, data, urb->actual_length);
388                 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
394         result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
395         dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
396 }
397
398
399 static void kobil_write_callback( struct urb *purb )
400 {
401 }
402
403
404 static 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
487 static int kobil_write_room (struct usb_serial_port *port)
488 {
489         //dbg("%s - port %d", __FUNCTION__, port->number);
490         return 8;
491 }
492
493
494 static 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
508         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
509         if (!transfer_buffer) {
510                 return -ENOMEM;
511         }
512
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
536 static 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
553         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
554         if (! transfer_buffer) {
555                 return -ENOMEM;
556         }
557
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
601 static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old)
602 {
603         struct kobil_private * priv;
604         int result;
605         unsigned short urb_val = 0;
606         int c_cflag = port->tty->termios->c_cflag;
607         speed_t speed;
608         void * settings;
609
610         priv = usb_get_serial_port_data(port);
611         if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
612                 // This device doesn't support ioctl calls
613                 return;
614
615         switch (speed = tty_get_baud_rate(port->tty)) {
616                 case 1200:
617                         urb_val = SUSBCR_SBR_1200;
618                         break;
619                 default:
620                         speed = 9600;
621                 case 9600:
622                         urb_val = SUSBCR_SBR_9600;
623                         break;
624         }
625         urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
626
627         settings = kzalloc(50, GFP_KERNEL);
628         if (! settings)
629                 return;
630
631         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");
637                 } else {
638                         urb_val |= SUSBCR_SPASB_EvenParity;
639                         strcat(settings, "Even Parity");
640                 }
641         } else {
642                 urb_val |= SUSBCR_SPASB_NoParity;
643                 strcat(settings, "No Parity");
644         }
645         port->tty->termios->c_cflag &= ~CMSPAR;
646         tty_encode_baud_rate(port->tty, speed, speed);
647
648         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 }
660
661 static 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
670                 return 0;
671
672         switch (cmd) {
673         case TCFLSH:   // 0x540B
674                 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
675                 if (! transfer_buffer)
676                         return -ENOBUFS;
677
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);
690                 kfree(transfer_buffer);
691                 return (result < 0) ? -EFAULT : 0;
692         default:
693                 return -ENOIOCTLCMD;
694         }
695 }
696
697 static 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;
711 failed_usb_register:
712         usb_serial_deregister(&kobil_device);
713 failed_usb_serial_register:
714         return retval;
715 }
716
717
718 static void __exit kobil_exit (void)
719 {
720         usb_deregister (&kobil_driver);
721         usb_serial_deregister (&kobil_device);
722 }
723
724 module_init(kobil_init);
725 module_exit(kobil_exit);
726
727 MODULE_AUTHOR( DRIVER_AUTHOR );
728 MODULE_DESCRIPTION( DRIVER_DESC );
729 MODULE_LICENSE( "GPL" );
730
731 module_param(debug, bool, S_IRUGO | S_IWUSR);
732 MODULE_PARM_DESC(debug, "Debug enabled or not");