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