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