OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [usb/] [serial/] [kl5kusb105.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * KLSI KL5KUSB105 chip RS232 converter driver
3
 *
4
 *   Copyright (C) 2001 Utz-Uwe Haus <haus@uuhaus.de>
5
 *
6
 *   This program is free software; you can redistribute it and/or modify
7
 *   it under the terms of the GNU General Public License as published by
8
 *   the Free Software Foundation; either version 2 of the License, or
9
 *   (at your option) any later version.
10
 *
11
 * All information about the device was acquired using SniffUSB ans snoopUSB
12
 * on Windows98.
13
 * It was written out of frustration with the PalmConnect USB Serial adapter
14
 * sold by Palm Inc.
15
 * Neither Palm, nor their contractor (MCCI) or their supplier (KLSI) provided
16
 * information that was not already available.
17
 *
18
 * It seems that KLSI bought some silicon-design information from ScanLogic,
19
 * whose SL11R processor is at the core of the KL5KUSB chipset from KLSI.
20
 * KLSI has firmware available for their devices; it is probable that the
21
 * firmware differs from that used by KLSI in their products. If you have an
22
 * original KLSI device and can provide some information on it, I would be
23
 * most interested in adding support for it here. If you have any information
24
 * on the protocol used (or find errors in my reverse-engineered stuff), please
25
 * let me know.
26
 *
27
 * The code was only tested with a PalmConnect USB adapter; if you
28
 * are adventurous, try it with any KLSI-based device and let me know how it
29
 * breaks so that I can fix it!
30
 */
31
 
32
/* TODO:
33
 *      check modem line signals
34
 *      implement handshaking or decide that we do not support it
35
 */
36
 
37
/* History:
38
 *   0.3a - implemented pools of write URBs
39
 *   0.3  - alpha version for public testing
40
 *   0.2  - TIOCMGET works, so autopilot(1) can be used!
41
 *   0.1  - can be used to to pilot-xfer -p /dev/ttyUSB0 -l
42
 *
43
 *   The driver skeleton is mainly based on mct_u232.c and various other
44
 *   pieces of code shamelessly copied from the drivers/usb/serial/ directory.
45
 */
46
 
47
 
48
#include <linux/config.h>
49
#include <linux/kernel.h>
50
#include <linux/errno.h>
51
#include <linux/init.h>
52
#include <linux/slab.h>
53
#include <linux/tty.h>
54
#include <linux/tty_driver.h>
55
#include <linux/tty_flip.h>
56
#include <linux/module.h>
57
#include <asm/uaccess.h>
58
#include <linux/usb.h>
59
 
60
#ifdef CONFIG_USB_SERIAL_DEBUG
61
        static int debug = 1;
62
#else
63
        static int debug;
64
#endif
65
 
66
#include "usb-serial.h"
67
#include "kl5kusb105.h"
68
 
69
 
70
/*
71
 * Version Information
72
 */
73
#define DRIVER_VERSION "v0.3a"
74
#define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>"
75
#define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver"
76
 
77
 
78
/*
79
 * Function prototypes
80
 */
81
static int  klsi_105_startup             (struct usb_serial *serial);
82
static void klsi_105_shutdown            (struct usb_serial *serial);
83
static int  klsi_105_open                (struct usb_serial_port *port,
84
                                          struct file *filp);
85
static void klsi_105_close               (struct usb_serial_port *port,
86
                                          struct file *filp);
87
static int  klsi_105_write               (struct usb_serial_port *port,
88
                                          int from_user,
89
                                          const unsigned char *buf,
90
                                          int count);
91
static void klsi_105_write_bulk_callback (struct urb *urb);
92
static int  klsi_105_chars_in_buffer     (struct usb_serial_port *port);
93
static int  klsi_105_write_room          (struct usb_serial_port *port);
94
 
95
static void klsi_105_read_bulk_callback  (struct urb *urb);
96
static void klsi_105_set_termios         (struct usb_serial_port *port,
97
                                          struct termios * old);
98
static int  klsi_105_ioctl               (struct usb_serial_port *port,
99
                                          struct file * file,
100
                                          unsigned int cmd,
101
                                          unsigned long arg);
102
static void klsi_105_throttle            (struct usb_serial_port *port);
103
static void klsi_105_unthrottle          (struct usb_serial_port *port);
104
/*
105
static void klsi_105_break_ctl           (struct usb_serial_port *port,
106
                                          int break_state );
107
 */
108
 
109
/*
110
 * All of the device info needed for the KLSI converters.
111
 */
112
static struct usb_device_id id_table [] = {
113
        { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) },
114
        { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) },
115
        { }             /* Terminating entry */
116
};
117
 
118
MODULE_DEVICE_TABLE (usb, id_table);
119
 
120
 
121
static struct usb_serial_device_type kl5kusb105d_device = {
122
        .owner =             THIS_MODULE,
123
        .name =              "KL5KUSB105D / PalmConnect",
124
        .id_table =          id_table,
125
        .num_interrupt_in =  1,
126
        .num_bulk_in =       1,
127
        .num_bulk_out =      1,
128
        .num_ports =         1,
129
        .open =              klsi_105_open,
130
        .close =             klsi_105_close,
131
        .write =             klsi_105_write,
132
        .write_bulk_callback = klsi_105_write_bulk_callback,
133
        .chars_in_buffer =   klsi_105_chars_in_buffer,
134
        .write_room =        klsi_105_write_room,
135
        .read_bulk_callback =klsi_105_read_bulk_callback,
136
        .ioctl =             klsi_105_ioctl,
137
        .set_termios =       klsi_105_set_termios,
138
        /*.break_ctl =       klsi_105_break_ctl,*/
139
        .startup =           klsi_105_startup,
140
        .shutdown =          klsi_105_shutdown,
141
        .throttle =          klsi_105_throttle,
142
        .unthrottle =        klsi_105_unthrottle,
143
};
144
 
145
struct klsi_105_port_settings {
146
        __u8    pktlen;         /* always 5, it seems */
147
        __u8    baudrate;
148
        __u8    databits;
149
        __u8    unknown1;
150
        __u8    unknown2;
151
} __attribute__ ((packed));
152
 
153
/* we implement a pool of NUM_URBS urbs per usb_serial */
154
#define NUM_URBS                        1
155
#define URB_TRANSFER_BUFFER_SIZE        64
156
struct klsi_105_private {
157
        struct klsi_105_port_settings   cfg;
158
        struct termios                  termios;
159
        unsigned long                   line_state; /* modem line settings */
160
        /* write pool */
161
        struct urb *                    write_urb_pool[NUM_URBS];
162
        spinlock_t                      write_urb_pool_lock;
163
        unsigned long                   bytes_in;
164
        unsigned long                   bytes_out;
165
};
166
 
167
 
168
/*
169
 * Handle vendor specific USB requests
170
 */
171
 
172
 
173
#define KLSI_TIMEOUT     (HZ * 5 ) /* default urb timeout */
174
 
175
static int klsi_105_chg_port_settings(struct usb_serial *serial,
176
                                      struct klsi_105_port_settings *settings)
177
{
178
        int rc;
179
 
180
        rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
181
                             KL5KUSB105A_SIO_SET_DATA,
182
                             USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE,
183
                             0, /* value */
184
                             0, /* index */
185
                             settings,
186
                             sizeof(struct klsi_105_port_settings),
187
                             KLSI_TIMEOUT);
188
        if (rc < 0)
189
                err("Change port settings failed (error = %d)", rc);
190
        info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d",
191
            __FUNCTION__,
192
            settings->pktlen,
193
            settings->baudrate, settings->databits,
194
            settings->unknown1, settings->unknown2);
195
        return rc;
196
} /* klsi_105_chg_port_settings */
197
 
198
/* translate a 16-bit status value from the device to linux's TIO bits */
199
static unsigned long klsi_105_status2linestate(const __u16 status)
200
{
201
        unsigned long res = 0;
202
 
203
        res =   ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0)
204
              | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0)
205
              ;
206
 
207
        return res;
208
}
209
/*
210
 * Read line control via vendor command and return result through
211
 * *line_state_p
212
 */
213
/* It seems that the status buffer has always only 2 bytes length */
214
#define KLSI_STATUSBUF_LEN      2
215
static int klsi_105_get_line_state(struct usb_serial *serial,
216
                                   unsigned long *line_state_p)
217
{
218
        int rc;
219
        __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1};
220
        __u16 status;
221
 
222
        info("%s - sending SIO Poll request", __FUNCTION__);
223
        rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
224
                             KL5KUSB105A_SIO_POLL,
225
                             USB_TYPE_VENDOR | USB_DIR_IN,
226
                             0, /* value */
227
                             0, /* index */
228
                             status_buf, KLSI_STATUSBUF_LEN,
229
                             10*HZ
230
                             );
231
        if (rc < 0)
232
                err("Reading line status failed (error = %d)", rc);
233
        else {
234
                status = status_buf[0] + (status_buf[1]<<8);
235
 
236
                info("%s - read status %x %x", __FUNCTION__,
237
                     status_buf[0], status_buf[1]);
238
 
239
                *line_state_p = klsi_105_status2linestate(status);
240
        }
241
 
242
        return rc;
243
}
244
 
245
 
246
/*
247
 * Driver's tty interface functions
248
 */
249
 
250
static int klsi_105_startup (struct usb_serial *serial)
251
{
252
        struct klsi_105_private *priv;
253
        int i;
254
 
255
        /* check if we support the product id (see keyspan.c)
256
         * FIXME
257
         */
258
 
259
        /* allocate the private data structure */
260
        for (i=0; i<serial->num_ports; i++) {
261
                serial->port[i].private = kmalloc(sizeof(struct klsi_105_private),
262
                                                   GFP_KERNEL);
263
                if (!serial->port[i].private) {
264
                        dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__);
265
                        return (-1); /* error */
266
                }
267
                priv = (struct klsi_105_private *)serial->port[i].private;
268
                /* set initial values for control structures */
269
                priv->cfg.pktlen    = 5;
270
                priv->cfg.baudrate  = kl5kusb105a_sio_b9600;
271
                priv->cfg.databits  = kl5kusb105a_dtb_8;
272
                priv->cfg.unknown1  = 0;
273
                priv->cfg.unknown2  = 1;
274
 
275
                priv->line_state    = 0;
276
 
277
                priv->bytes_in      = 0;
278
                priv->bytes_out     = 0;
279
 
280
                spin_lock_init (&priv->write_urb_pool_lock);
281
                for (i=0; i<NUM_URBS; i++) {
282
                        struct urb* urb = usb_alloc_urb(0);
283
 
284
                        priv->write_urb_pool[i] = urb;
285
                        if (urb == NULL) {
286
                                err("No more urbs???");
287
                                continue;
288
                        }
289
 
290
                        urb->transfer_buffer = NULL;
291
                        urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE,
292
                                                        GFP_KERNEL);
293
                        if (!urb->transfer_buffer) {
294
                                err("%s - out of memory for urb buffers.", __FUNCTION__);
295
                                continue;
296
                        }
297
                }
298
 
299
                /* priv->termios is left uninitalized until port opening */
300
                init_waitqueue_head(&serial->port[i].write_wait);
301
        }
302
 
303
        return (0);
304
} /* klsi_105_startup */
305
 
306
 
307
static void klsi_105_shutdown (struct usb_serial *serial)
308
{
309
        int i;
310
 
311
        dbg("%s", __FUNCTION__);
312
 
313
        /* stop reads and writes on all ports */
314
        for (i=0; i < serial->num_ports; ++i) {
315
                struct klsi_105_private *priv =
316
                        (struct klsi_105_private*) serial->port[i].private;
317
                unsigned long flags;
318
 
319
                if (priv) {
320
                        /* kill our write urb pool */
321
                        int j;
322
                        struct urb **write_urbs = priv->write_urb_pool;
323
                        spin_lock_irqsave(&priv->write_urb_pool_lock,flags);
324
 
325
                        for (j = 0; j < NUM_URBS; j++) {
326
                                if (write_urbs[j]) {
327
                                        /* FIXME - uncomment the following
328
                                         * usb_unlink_urb call when the host
329
                                         * controllers get fixed to set
330
                                         * urb->dev = NULL after the urb is
331
                                         * finished.  Otherwise this call
332
                                         * oopses. */
333
                                        /* usb_unlink_urb(write_urbs[j]); */
334
                                        if (write_urbs[j]->transfer_buffer)
335
                                                    kfree(write_urbs[j]->transfer_buffer);
336
                                        usb_free_urb (write_urbs[j]);
337
                                }
338
                        }
339
 
340
                        spin_unlock_irqrestore (&priv->write_urb_pool_lock,
341
                                                flags);
342
 
343
                        kfree(serial->port[i].private);
344
                }
345
        }
346
} /* klsi_105_shutdown */
347
 
348
static int  klsi_105_open (struct usb_serial_port *port, struct file *filp)
349
{
350
        struct usb_serial *serial = port->serial;
351
        struct klsi_105_private *priv = (struct klsi_105_private *)port->private;
352
        int retval = 0;
353
        int rc;
354
        int i;
355
        unsigned long line_state;
356
 
357
        dbg("%s port %d", __FUNCTION__, port->number);
358
 
359
        /* force low_latency on so that our tty_push actually forces
360
         * the data through
361
         * port->tty->low_latency = 1; */
362
 
363
        /* Do a defined restart:
364
         * Set up sane default baud rate and send the 'READ_ON'
365
         * vendor command.
366
         * FIXME: set modem line control (how?)
367
         * Then read the modem line control and store values in
368
         * priv->line_state.
369
         */
370
        priv->cfg.pktlen   = 5;
371
        priv->cfg.baudrate = kl5kusb105a_sio_b9600;
372
        priv->cfg.databits = kl5kusb105a_dtb_8;
373
        priv->cfg.unknown1 = 0;
374
        priv->cfg.unknown2 = 1;
375
        klsi_105_chg_port_settings(serial, &(priv->cfg));
376
 
377
        /* set up termios structure */
378
        priv->termios.c_iflag = port->tty->termios->c_iflag;
379
        priv->termios.c_oflag = port->tty->termios->c_oflag;
380
        priv->termios.c_cflag = port->tty->termios->c_cflag;
381
        priv->termios.c_lflag = port->tty->termios->c_lflag;
382
        for (i=0; i<NCCS; i++)
383
                priv->termios.c_cc[i] = port->tty->termios->c_cc[i];
384
 
385
 
386
        /* READ_ON and urb submission */
387
        FILL_BULK_URB(port->read_urb, serial->dev,
388
                      usb_rcvbulkpipe(serial->dev,
389
                                      port->bulk_in_endpointAddress),
390
                      port->read_urb->transfer_buffer,
391
                      port->read_urb->transfer_buffer_length,
392
                      klsi_105_read_bulk_callback,
393
                      port);
394
        port->read_urb->transfer_flags |= USB_QUEUE_BULK;
395
 
396
        rc = usb_submit_urb(port->read_urb);
397
        if (rc) {
398
                err("%s - failed submitting read urb, error %d", __FUNCTION__, rc);
399
                retval = rc;
400
                goto exit;
401
        }
402
 
403
        rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0),
404
                             KL5KUSB105A_SIO_CONFIGURE,
405
                             USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE,
406
                             KL5KUSB105A_SIO_CONFIGURE_READ_ON,
407
                             0, /* index */
408
                             NULL,
409
                             0,
410
                             KLSI_TIMEOUT);
411
        if (rc < 0) {
412
                err("Enabling read failed (error = %d)", rc);
413
                retval = rc;
414
        } else
415
                dbg("%s - enabled reading", __FUNCTION__);
416
 
417
        rc = klsi_105_get_line_state(serial, &line_state);
418
        if (rc >= 0) {
419
                priv->line_state = line_state;
420
                dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
421
                retval = 0;
422
        } else
423
                retval = rc;
424
 
425
exit:
426
        return retval;
427
} /* klsi_105_open */
428
 
429
 
430
static void klsi_105_close (struct usb_serial_port *port, struct file *filp)
431
{
432
        struct usb_serial *serial;
433
        struct klsi_105_private *priv
434
                = (struct klsi_105_private *)port->private;
435
        int rc;
436
 
437
        dbg("%s port %d", __FUNCTION__, port->number);
438
 
439
        serial = get_usb_serial (port, __FUNCTION__);
440
 
441
        if(!serial)
442
                return;
443
 
444
        /* send READ_OFF */
445
        rc = usb_control_msg (serial->dev,
446
                              usb_sndctrlpipe(serial->dev, 0),
447
                              KL5KUSB105A_SIO_CONFIGURE,
448
                              USB_TYPE_VENDOR | USB_DIR_OUT,
449
                              KL5KUSB105A_SIO_CONFIGURE_READ_OFF,
450
                              0, /* index */
451
                              NULL, 0,
452
                              KLSI_TIMEOUT);
453
        if (rc < 0)
454
                    err("Disabling read failed (error = %d)", rc);
455
 
456
        /* shutdown our bulk reads and writes */
457
        usb_unlink_urb (port->write_urb);
458
        usb_unlink_urb (port->read_urb);
459
        /* unlink our write pool */
460
        /* FIXME */
461
        /* wgg - do I need this? I think so. */
462
        usb_unlink_urb (port->interrupt_in_urb);
463
        info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out);
464
} /* klsi_105_close */
465
 
466
 
467
/* We need to write a complete 64-byte data block and encode the
468
 * number actually sent in the first double-byte, LSB-order. That
469
 * leaves at most 62 bytes of payload.
470
 */
471
#define KLSI_105_DATA_OFFSET    2   /* in the bulk urb data block */
472
 
473
 
474
static int klsi_105_write (struct usb_serial_port *port, int from_user,
475
                           const unsigned char *buf, int count)
476
{
477
        struct usb_serial *serial = port->serial;
478
        struct klsi_105_private *priv =
479
                (struct klsi_105_private*) port->private;
480
        int result, size;
481
        int bytes_sent=0;
482
 
483
        dbg("%s - port %d", __FUNCTION__, port->number);
484
 
485
        while (count > 0) {
486
                /* try to find a free urb (write 0 bytes if none) */
487
                struct urb *urb = NULL;
488
                unsigned long flags;
489
                int i;
490
                /* since the pool is per-port we might not need the spin lock !? */
491
                spin_lock_irqsave (&priv->write_urb_pool_lock, flags);
492
                for (i=0; i<NUM_URBS; i++) {
493
                        if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
494
                                urb = priv->write_urb_pool[i];
495
                                dbg("%s - using pool URB %d", __FUNCTION__, i);
496
                                break;
497
                        }
498
                }
499
                spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);
500
 
501
                if (urb==NULL) {
502
                        dbg("%s - no more free urbs", __FUNCTION__);
503
                        goto exit;
504
                }
505
 
506
                if (urb->transfer_buffer == NULL) {
507
                        urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
508
                        if (urb->transfer_buffer == NULL) {
509
                                err("%s - no more kernel memory...", __FUNCTION__);
510
                                goto exit;
511
                        }
512
                }
513
 
514
                size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET);
515
                size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET);
516
 
517
                if (from_user) {
518
                        if (copy_from_user(urb->transfer_buffer
519
                                           + KLSI_105_DATA_OFFSET, buf, size)) {
520
                                return -EFAULT;
521
                        }
522
                } else {
523
                        memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET,
524
                                buf, size);
525
                }
526
 
527
                /* write payload size into transfer buffer */
528
                ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF);
529
                ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8);
530
 
531
                /* set up our urb */
532
                FILL_BULK_URB(urb, serial->dev,
533
                              usb_sndbulkpipe(serial->dev,
534
                                              port->bulk_out_endpointAddress),
535
                              urb->transfer_buffer,
536
                              URB_TRANSFER_BUFFER_SIZE,
537
                              klsi_105_write_bulk_callback,
538
                              port);
539
                urb->transfer_flags |= USB_QUEUE_BULK;
540
 
541
 
542
                /* send the data out the bulk port */
543
                result = usb_submit_urb(urb);
544
                if (result) {
545
                        err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
546
                        goto exit;
547
                }
548
                buf += size;
549
                bytes_sent += size;
550
                count -= size;
551
        }
552
exit:
553
        priv->bytes_out+=bytes_sent;
554
 
555
        return bytes_sent;      /* that's how much we wrote */
556
} /* klsi_105_write */
557
 
558
static void klsi_105_write_bulk_callback ( struct urb *urb)
559
{
560
        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
561
        struct usb_serial *serial = port->serial;
562
 
563
        dbg("%s - port %d", __FUNCTION__, port->number);
564
 
565
        if (!serial) {
566
                dbg("%s - bad serial pointer, exiting", __FUNCTION__);
567
                return;
568
        }
569
 
570
        if (urb->status) {
571
                dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
572
                    urb->status);
573
                return;
574
        }
575
 
576
        /* from generic_write_bulk_callback */
577
        queue_task(&port->tqueue, &tq_immediate);
578
        mark_bh(IMMEDIATE_BH);
579
 
580
        return;
581
} /* klsi_105_write_bulk_completion_callback */
582
 
583
 
584
/* return number of characters currently in the writing process */
585
static int klsi_105_chars_in_buffer (struct usb_serial_port *port)
586
{
587
        int chars = 0;
588
        int i;
589
        unsigned long flags;
590
        struct klsi_105_private *priv =
591
                (struct klsi_105_private*) port->private;
592
 
593
        spin_lock_irqsave (&priv->write_urb_pool_lock, flags);
594
 
595
        for (i = 0; i < NUM_URBS; ++i) {
596
                if (priv->write_urb_pool[i]->status == -EINPROGRESS) {
597
                        chars += URB_TRANSFER_BUFFER_SIZE;
598
                }
599
        }
600
 
601
        spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);
602
 
603
        dbg("%s - returns %d", __FUNCTION__, chars);
604
        return (chars);
605
}
606
 
607
static int klsi_105_write_room (struct usb_serial_port *port)
608
{
609
        unsigned long flags;
610
        int i;
611
        int room = 0;
612
        struct klsi_105_private *priv =
613
                (struct klsi_105_private*) port->private;
614
 
615
        spin_lock_irqsave (&priv->write_urb_pool_lock, flags);
616
        for (i = 0; i < NUM_URBS; ++i) {
617
                if (priv->write_urb_pool[i]->status != -EINPROGRESS) {
618
                        room += URB_TRANSFER_BUFFER_SIZE;
619
                }
620
        }
621
 
622
        spin_unlock_irqrestore (&priv->write_urb_pool_lock, flags);
623
 
624
        dbg("%s - returns %d", __FUNCTION__, room);
625
        return (room);
626
}
627
 
628
 
629
 
630
static void klsi_105_read_bulk_callback (struct urb *urb)
631
{
632
        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
633
        struct usb_serial *serial = port->serial;
634
        struct klsi_105_private *priv =
635
                (struct klsi_105_private*) port->private;
636
        struct tty_struct *tty;
637
        unsigned char *data = urb->transfer_buffer;
638
        int rc;
639
 
640
        dbg("%s - port %d", __FUNCTION__, port->number);
641
 
642
        /* The urb might have been killed. */
643
        if (urb->status) {
644
                dbg("%s - nonzero read bulk status received: %d", __FUNCTION__,
645
                    urb->status);
646
                return;
647
        }
648
        if (!serial) {
649
                dbg("%s - bad serial pointer, exiting", __FUNCTION__);
650
                return;
651
        }
652
 
653
        /* The data received is again preceded by a length double-byte in LSB-
654
         * first order (see klsi_105_write() )
655
         */
656
        if (urb->actual_length == 0) {
657
                /* empty urbs seem to happen, we ignore them */
658
                /* dbg("%s - emtpy URB", __FUNCTION__); */
659
               ;
660
        } else if (urb->actual_length <= 2) {
661
                dbg("%s - size %d URB not understood", __FUNCTION__,
662
                    urb->actual_length);
663
                usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
664
        } else {
665
                int i;
666
                int bytes_sent = ((__u8 *) data)[0] +
667
                                 ((unsigned int) ((__u8 *) data)[1] << 8);
668
                tty = port->tty;
669
                /* we should immediately resubmit the URB, before attempting
670
                 * to pass the data on to the tty layer. But that needs locking
671
                 * against re-entry an then mixed-up data because of
672
                 * intermixed tty_flip_buffer_push()s
673
                 * FIXME
674
                 */
675
                usb_serial_debug_data (__FILE__, __FUNCTION__,
676
                                       urb->actual_length, data);
677
 
678
                if (bytes_sent + 2 > urb->actual_length) {
679
                        dbg("%s - trying to read more data than available"
680
                            " (%d vs. %d)", __FUNCTION__,
681
                            bytes_sent+2, urb->actual_length);
682
                        /* cap at implied limit */
683
                        bytes_sent = urb->actual_length - 2;
684
                }
685
 
686
                for (i = 2; i < 2+bytes_sent; i++) {
687
                        /* if we insert more than TTY_FLIPBUF_SIZE characters,
688
                         * we drop them. */
689
                        if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
690
                                tty_flip_buffer_push(tty);
691
                        }
692
                        /* this doesn't actually push the data through unless
693
                         * tty->low_latency is set */
694
                        tty_insert_flip_char(tty, ((__u8*) data)[i], 0);
695
                }
696
                tty_flip_buffer_push(tty);
697
                priv->bytes_in += bytes_sent;
698
        }
699
        /* Continue trying to always read  */
700
        FILL_BULK_URB(port->read_urb, serial->dev,
701
                      usb_rcvbulkpipe(serial->dev,
702
                                      port->bulk_in_endpointAddress),
703
                      port->read_urb->transfer_buffer,
704
                      port->read_urb->transfer_buffer_length,
705
                      klsi_105_read_bulk_callback,
706
                      port);
707
        rc = usb_submit_urb(port->read_urb);
708
        if (rc)
709
                err("%s - failed resubmitting read urb, error %d", __FUNCTION__, rc);
710
} /* klsi_105_read_bulk_callback */
711
 
712
 
713
static void klsi_105_set_termios (struct usb_serial_port *port,
714
                                  struct termios *old_termios)
715
{
716
        struct usb_serial *serial = port->serial;
717
        struct klsi_105_private *priv = (struct klsi_105_private *)port->private;
718
        unsigned int iflag = port->tty->termios->c_iflag;
719
        unsigned int old_iflag = old_termios->c_iflag;
720
        unsigned int cflag = port->tty->termios->c_cflag;
721
        unsigned int old_cflag = old_termios->c_cflag;
722
 
723
        /*
724
         * Update baud rate
725
         */
726
        if( (cflag & CBAUD) != (old_cflag & CBAUD) ) {
727
                /* reassert DTR and (maybe) RTS on transition from B0 */
728
                if( (old_cflag & CBAUD) == B0 ) {
729
                        dbg("%s: baud was B0", __FUNCTION__);
730
#if 0
731
                        priv->control_state |= TIOCM_DTR;
732
                        /* don't set RTS if using hardware flow control */
733
                        if (!(old_cflag & CRTSCTS)) {
734
                                priv->control_state |= TIOCM_RTS;
735
                        }
736
                        mct_u232_set_modem_ctrl(serial, priv->control_state);
737
#endif
738
                }
739
 
740
                switch(cflag & CBAUD) {
741
                case B0: /* handled below */
742
                        break;
743
                case B1200: priv->cfg.baudrate = kl5kusb105a_sio_b1200;
744
                        break;
745
                case B2400: priv->cfg.baudrate = kl5kusb105a_sio_b2400;
746
                        break;
747
                case B4800: priv->cfg.baudrate = kl5kusb105a_sio_b4800;
748
                        break;
749
                case B9600: priv->cfg.baudrate = kl5kusb105a_sio_b9600;
750
                        break;
751
                case B19200: priv->cfg.baudrate = kl5kusb105a_sio_b19200;
752
                        break;
753
                case B38400: priv->cfg.baudrate = kl5kusb105a_sio_b38400;
754
                        break;
755
                case B57600: priv->cfg.baudrate = kl5kusb105a_sio_b57600;
756
                        break;
757
                case B115200: priv->cfg.baudrate = kl5kusb105a_sio_b115200;
758
                        break;
759
                default:
760
                        err("KLSI USB->Serial converter:"
761
                            " unsupported baudrate request, using default"
762
                            " of 9600");
763
                        priv->cfg.baudrate = kl5kusb105a_sio_b9600;
764
                        break;
765
                }
766
                if ((cflag & CBAUD) == B0 ) {
767
                        dbg("%s: baud is B0", __FUNCTION__);
768
                        /* Drop RTS and DTR */
769
                        /* maybe this should be simulated by sending read
770
                         * disable and read enable messages?
771
                         */
772
                        ;
773
#if 0
774
                        priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
775
                        mct_u232_set_modem_ctrl(serial, priv->control_state);
776
#endif
777
                }
778
        }
779
 
780
        if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
781
                /* set the number of data bits */
782
                switch (cflag & CSIZE) {
783
                case CS5:
784
                        dbg("%s - 5 bits/byte not supported", __FUNCTION__);
785
                        return ;
786
                case CS6:
787
                        dbg("%s - 6 bits/byte not supported", __FUNCTION__);
788
                        return ;
789
                case CS7:
790
                        priv->cfg.databits = kl5kusb105a_dtb_7;
791
                        break;
792
                case CS8:
793
                        priv->cfg.databits = kl5kusb105a_dtb_8;
794
                        break;
795
                default:
796
                        err("CSIZE was not CS5-CS8, using default of 8");
797
                        priv->cfg.databits = kl5kusb105a_dtb_8;
798
                        break;
799
                }
800
        }
801
 
802
        /*
803
         * Update line control register (LCR)
804
         */
805
        if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))
806
            || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) {
807
 
808
#if 0
809
                priv->last_lcr = 0;
810
 
811
                /* set the parity */
812
                if (cflag & PARENB)
813
                        priv->last_lcr |= (cflag & PARODD) ?
814
                                MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
815
                else
816
                        priv->last_lcr |= MCT_U232_PARITY_NONE;
817
 
818
                /* set the number of stop bits */
819
                priv->last_lcr |= (cflag & CSTOPB) ?
820
                        MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
821
 
822
                mct_u232_set_line_ctrl(serial, priv->last_lcr);
823
#endif
824
                ;
825
        }
826
 
827
        /*
828
         * Set flow control: well, I do not really now how to handle DTR/RTS.
829
         * Just do what we have seen with SniffUSB on Win98.
830
         */
831
        if( (iflag & IXOFF) != (old_iflag & IXOFF)
832
            || (iflag & IXON) != (old_iflag & IXON)
833
            ||  (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) {
834
 
835
                /* Drop DTR/RTS if no flow control otherwise assert */
836
#if 0
837
                if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) )
838
                        priv->control_state |= TIOCM_DTR | TIOCM_RTS;
839
                else
840
                        priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
841
                mct_u232_set_modem_ctrl(serial, priv->control_state);
842
#endif
843
                ;
844
        }
845
 
846
        /* now commit changes to device */
847
        klsi_105_chg_port_settings(serial, &(priv->cfg));
848
} /* klsi_105_set_termios */
849
 
850
 
851
#if 0
852
static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
853
{
854
        struct usb_serial *serial = port->serial;
855
        struct mct_u232_private *priv = (struct mct_u232_private *)port->private;
856
        unsigned char lcr = priv->last_lcr;
857
 
858
        dbg("%sstate=%d", __FUNCTION__, break_state);
859
 
860
        if (break_state)
861
                lcr |= MCT_U232_SET_BREAK;
862
 
863
        mct_u232_set_line_ctrl(serial, lcr);
864
} /* mct_u232_break_ctl */
865
#endif
866
 
867
static int klsi_105_ioctl (struct usb_serial_port *port, struct file * file,
868
                           unsigned int cmd, unsigned long arg)
869
{
870
        struct usb_serial *serial = port->serial;
871
        struct klsi_105_private *priv = (struct klsi_105_private *)port->private;
872
        int mask;
873
 
874
        dbg("%scmd=0x%x", __FUNCTION__, cmd);
875
 
876
        /* Based on code from acm.c and others */
877
        switch (cmd) {
878
        case TIOCMGET: {
879
                int rc;
880
                unsigned long line_state;
881
                dbg("%s - TIOCMGET request, just guessing", __FUNCTION__);
882
 
883
                rc = klsi_105_get_line_state(serial, &line_state);
884
                if (rc < 0) {
885
                        err("Reading line control failed (error = %d)", rc);
886
                        /* better return value? EAGAIN? */
887
                        return -ENOIOCTLCMD;
888
                } else {
889
                        priv->line_state = line_state;
890
                        dbg("%s - read line state 0x%lx", __FUNCTION__, line_state);
891
                }
892
                return put_user(priv->line_state, (unsigned long *) arg);
893
               };
894
 
895
        case TIOCMSET: /* Turns on and off the lines as specified by the mask */
896
        case TIOCMBIS: /* turns on (Sets) the lines as specified by the mask */
897
        case TIOCMBIC: /* turns off (Clears) the lines as specified by the mask */
898
                if (get_user(mask, (unsigned long *) arg))
899
                        return -EFAULT;
900
 
901
                if ((cmd == TIOCMSET) || (mask & TIOCM_RTS)) {
902
                        /* RTS needs set */
903
                        if( ((cmd == TIOCMSET) && (mask & TIOCM_RTS)) ||
904
                            (cmd == TIOCMBIS) )
905
                                dbg("%s - set RTS not handled", __FUNCTION__);
906
                                /* priv->control_state |=  TIOCM_RTS; */
907
                        else
908
                                dbg("%s - clear RTS not handled", __FUNCTION__);
909
                                /* priv->control_state &= ~TIOCM_RTS; */
910
                }
911
 
912
                if ((cmd == TIOCMSET) || (mask & TIOCM_DTR)) {
913
                        /* DTR needs set */
914
                        if( ((cmd == TIOCMSET) && (mask & TIOCM_DTR)) ||
915
                            (cmd == TIOCMBIS) )
916
                                dbg("%s - set DTR not handled", __FUNCTION__);
917
                        /*      priv->control_state |=  TIOCM_DTR; */
918
                        else
919
                                dbg("%s - clear DTR not handled", __FUNCTION__);
920
                                /* priv->control_state &= ~TIOCM_DTR; */
921
                }
922
                /*
923
                mct_u232_set_modem_ctrl(serial, priv->control_state);
924
                */
925
                break;
926
 
927
        case TIOCMIWAIT:
928
                /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
929
                /* TODO */
930
                dbg("%s - TIOCMIWAIT not handled", __FUNCTION__);
931
                return -ENOIOCTLCMD;
932
 
933
        case TIOCGICOUNT:
934
                /* return count of modemline transitions */
935
                /* TODO */
936
                dbg("%s - TIOCGICOUNT not handled", __FUNCTION__);
937
                return -ENOIOCTLCMD;
938
        case TCGETS: {
939
             /* return current info to caller */
940
             int retval;
941
 
942
             dbg("%s - TCGETS data faked/incomplete", __FUNCTION__);
943
 
944
             retval = verify_area(VERIFY_WRITE, (void *)arg,
945
                                  sizeof(struct termios));
946
 
947
             if (retval)
948
                         return(retval);
949
 
950
             kernel_termios_to_user_termios((struct termios *)arg,
951
                                            &priv->termios);
952
             return(0);
953
             }
954
        case TCSETS: {
955
                /* set port termios to the one given by the user */
956
                int retval;
957
 
958
                dbg("%s - TCSETS not handled", __FUNCTION__);
959
 
960
                retval = verify_area(VERIFY_READ, (void *)arg,
961
                                     sizeof(struct termios));
962
 
963
                if (retval)
964
                            return(retval);
965
 
966
                user_termios_to_kernel_termios(&priv->termios,
967
                                               (struct termios *)arg);
968
                klsi_105_set_termios(port, &priv->termios);
969
                return(0);
970
             }
971
        case TCSETSW: {
972
                /* set port termios and try to wait for completion of last
973
                 * write operation */
974
                /* We guess here. If there are not too many write urbs
975
                 * outstanding, we lie. */
976
                /* what is the right way to wait here? schedule() ? */
977
                /*
978
                while (klsi_105_chars_in_buffer(port) > (NUM_URBS / 4 ) * URB_TRANSFER_BUFFER_SIZE)
979
                            schedule();
980
                 */
981
                return -ENOIOCTLCMD;
982
                      }
983
        default:
984
                dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd);
985
                return(-ENOIOCTLCMD);
986
                break;
987
        }
988
        return 0;
989
} /* klsi_105_ioctl */
990
 
991
static void klsi_105_throttle (struct usb_serial_port *port)
992
{
993
        dbg("%s - port %d", __FUNCTION__, port->number);
994
        usb_unlink_urb (port->read_urb);
995
}
996
 
997
static void klsi_105_unthrottle (struct usb_serial_port *port)
998
{
999
        int result;
1000
 
1001
        dbg("%s - port %d", __FUNCTION__, port->number);
1002
 
1003
        port->read_urb->dev = port->serial->dev;
1004
        result = usb_submit_urb(port->read_urb);
1005
        if (result)
1006
                err("%s - failed submitting read urb, error %d", __FUNCTION__,
1007
                    result);
1008
}
1009
 
1010
 
1011
 
1012
static int __init klsi_105_init (void)
1013
{
1014
        usb_serial_register (&kl5kusb105d_device);
1015
 
1016
        info(DRIVER_DESC " " DRIVER_VERSION);
1017
        return 0;
1018
}
1019
 
1020
 
1021
static void __exit klsi_105_exit (void)
1022
{
1023
        usb_serial_deregister (&kl5kusb105d_device);
1024
}
1025
 
1026
 
1027
module_init (klsi_105_init);
1028
module_exit (klsi_105_exit);
1029
 
1030
MODULE_AUTHOR( DRIVER_AUTHOR );
1031
MODULE_DESCRIPTION( DRIVER_DESC );
1032
MODULE_LICENSE("GPL");
1033
 
1034
 
1035
MODULE_PARM(debug, "i");
1036
MODULE_PARM_DESC(debug, "enable extensive debugging messages");
1037
/* FIXME: implement
1038
MODULE_PARM(num_urbs, "i");
1039
MODULE_PARM_DESC(num_urbs, "number of URBs to use in write pool");
1040
*/
1041
 
1042
/* vim: set sts=8 ts=8 sw=8: */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.