Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
846 giacomo 1
/*
2
 * Prolific PL2303 USB to serial adaptor driver
3
 *
4
 * Copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
5
 * Copyright (C) 2003 IBM Corp.
6
 *
7
 * Original driver for 2.2.x by anonymous
8
 *
9
 *      This program is free software; you can redistribute it and/or modify
10
 *      it under the terms of the GNU General Public License as published by
11
 *      the Free Software Foundation; either version 2 of the License, or
12
 *      (at your option) any later version.
13
 *
14
 * See Documentation/usb/usb-serial.txt for more information on using this driver
15
 *
16
 * 2002_Mar_26 gkh
17
 *      allowed driver to work properly if there is no tty assigned to a port
18
 *      (this happens for serial console devices.)
19
 *
20
 * 2001_Oct_06 gkh
21
 *      Added RTS and DTR line control.  Thanks to joe@bndlg.de for parts of it.
22
 *
23
 * 2001_Sep_19 gkh
24
 *      Added break support.
25
 *
26
 * 2001_Aug_30 gkh
27
 *      fixed oops in write_bulk_callback.
28
 *
29
 * 2001_Aug_28 gkh
30
 *      reworked buffer logic to be like other usb-serial drivers.  Hopefully
31
 *      removing some reported problems.
32
 *
33
 * 2001_Jun_06 gkh
34
 *      finished porting to 2.4 format.
35
 *
36
 */
37
 
38
#include <linuxcomp.h>
39
 
40
#include <linux/config.h>
41
#include <linux/kernel.h>
42
#include <linux/errno.h>
43
#include <linux/init.h>
44
#include <linux/slab.h>
45
#include <linux/tty.h>
46
#include <linux/tty_driver.h>
47
#include <linux/tty_flip.h>
48
#include <linux/serial.h>
49
#include <linux/module.h>
50
#include <linux/spinlock.h>
51
#include <asm/uaccess.h>
52
#include <linux/usb.h>
53
 
54
#ifdef CONFIG_USB_SERIAL_DEBUG
55
        static int debug = 1;
56
#else
57
        static int debug;
58
#endif
59
 
60
#include "usb-serial.h"
61
#include "pl2303.h"
62
 
63
/*
64
 * Version Information
65
 */
66
#define DRIVER_VERSION "v0.10"
67
#define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
68
 
69
 
70
 
71
static struct usb_device_id id_table [] = {
72
        { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
73
        { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
74
        { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
75
        { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
76
        { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
77
        { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
78
        { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
79
        { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
80
        { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
81
        { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
82
        { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
83
        { }                                     /* Terminating entry */
84
};
85
 
86
MODULE_DEVICE_TABLE (usb, id_table);
87
 
88
static struct usb_driver pl2303_driver = {
89
        .owner =        THIS_MODULE,
90
        .name =         "pl2303",
91
        .probe =        usb_serial_probe,
92
        .disconnect =   usb_serial_disconnect,
93
        .id_table =     id_table,
94
};
95
 
96
#define SET_LINE_REQUEST_TYPE           0x21
97
#define SET_LINE_REQUEST                0x20
98
 
99
#define SET_CONTROL_REQUEST_TYPE        0x21
100
#define SET_CONTROL_REQUEST             0x22
101
#define CONTROL_DTR                     0x01
102
#define CONTROL_RTS                     0x02
103
 
104
#define BREAK_REQUEST_TYPE              0x21
105
#define BREAK_REQUEST                   0x23    
106
#define BREAK_ON                        0xffff
107
#define BREAK_OFF                       0x0000
108
 
109
#define GET_LINE_REQUEST_TYPE           0xa1
110
#define GET_LINE_REQUEST                0x21
111
 
112
#define VENDOR_WRITE_REQUEST_TYPE       0x40
113
#define VENDOR_WRITE_REQUEST            0x01
114
 
115
#define VENDOR_READ_REQUEST_TYPE        0xc0
116
#define VENDOR_READ_REQUEST             0x01
117
 
118
#define UART_STATE                      0x08
119
#define UART_DCD                        0x01
120
#define UART_DSR                        0x02
121
#define UART_BREAK_ERROR                0x04
122
#define UART_RING                       0x08
123
#define UART_FRAME_ERROR                0x10
124
#define UART_PARITY_ERROR               0x20
125
#define UART_OVERRUN_ERROR              0x40
126
#define UART_CTS                        0x80
127
 
128
/* function prototypes for a PL2303 serial converter */
129
static int pl2303_open (struct usb_serial_port *port, struct file *filp);
130
static void pl2303_close (struct usb_serial_port *port, struct file *filp);
131
static void pl2303_set_termios (struct usb_serial_port *port,
132
                                struct termios *old);
133
static int pl2303_ioctl (struct usb_serial_port *port, struct file *file,
134
                         unsigned int cmd, unsigned long arg);
135
static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs);
136
static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
137
static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
138
static int pl2303_write (struct usb_serial_port *port, int from_user,
139
                         const unsigned char *buf, int count);
140
static void pl2303_break_ctl(struct usb_serial_port *port,int break_state);
141
static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file);
142
static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
143
                            unsigned int set, unsigned int clear);
144
static int pl2303_startup (struct usb_serial *serial);
145
static void pl2303_shutdown (struct usb_serial *serial);
146
 
147
 
148
/* All of the device info needed for the PL2303 SIO serial converter */
149
static struct usb_serial_device_type pl2303_device = {
150
        .owner =                THIS_MODULE,
151
        .name =                 "PL-2303",
152
        .id_table =             id_table,
153
        .num_interrupt_in =     NUM_DONT_CARE,
154
        .num_bulk_in =          1,
155
        .num_bulk_out =         1,
156
        .num_ports =            1,
157
        .open =                 pl2303_open,
158
        .close =                pl2303_close,
159
        .write =                pl2303_write,
160
        .ioctl =                pl2303_ioctl,
161
        .break_ctl =            pl2303_break_ctl,
162
        .set_termios =          pl2303_set_termios,
163
        .tiocmget =             pl2303_tiocmget,
164
        .tiocmset =             pl2303_tiocmset,
165
        .read_bulk_callback =   pl2303_read_bulk_callback,
166
        .read_int_callback =    pl2303_read_int_callback,
167
        .write_bulk_callback =  pl2303_write_bulk_callback,
168
        .attach =               pl2303_startup,
169
        .shutdown =             pl2303_shutdown,
170
};
171
 
172
struct pl2303_private {
173
        spinlock_t lock;
174
        u8 line_control;
175
        u8 line_status;
176
        u8 termios_initialized;
177
};
178
 
179
 
180
static int pl2303_startup (struct usb_serial *serial)
181
{
182
        struct pl2303_private *priv;
183
        int i;
184
 
185
        for (i = 0; i < serial->num_ports; ++i) {
186
                priv = kmalloc (sizeof (struct pl2303_private), GFP_KERNEL);
187
                if (!priv)
188
                        return -ENOMEM;
189
                memset (priv, 0x00, sizeof (struct pl2303_private));
190
                spin_lock_init(&priv->lock);
191
                usb_set_serial_port_data(serial->port[i], priv);
192
        }
193
        return 0;
194
}
195
 
196
static int set_control_lines (struct usb_device *dev, u8 value)
197
{
198
        int retval;
199
 
200
        retval = usb_control_msg (dev, usb_sndctrlpipe (dev, 0),
201
                                  SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
202
                                  value, 0, NULL, 0, 100);
203
        dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
204
        return retval;
205
}
206
 
207
static int pl2303_write (struct usb_serial_port *port, int from_user,  const unsigned char *buf, int count)
208
{
209
        int result;
210
 
211
        dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
212
 
213
        if (port->write_urb->status == -EINPROGRESS) {
214
                dbg("%s - already writing", __FUNCTION__);
215
                return 0;
216
        }
217
 
218
        count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
219
        if (from_user) {
220
                if (copy_from_user (port->write_urb->transfer_buffer, buf, count))
221
                        return -EFAULT;
222
        } else {
223
                memcpy (port->write_urb->transfer_buffer, buf, count);
224
        }
225
 
226
        usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
227
 
228
        port->write_urb->transfer_buffer_length = count;
229
        port->write_urb->dev = port->serial->dev;
230
        result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
231
        if (result)
232
                dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
233
        else
234
                result = count;
235
 
236
        return result;
237
}
238
 
239
 
240
 
241
static void pl2303_set_termios (struct usb_serial_port *port, struct termios *old_termios)
242
{
243
        struct usb_serial *serial = port->serial;
244
        struct pl2303_private *priv = usb_get_serial_port_data(port);
245
        unsigned long flags;
246
        unsigned int cflag;
247
        unsigned char *buf;
248
        int baud;
249
        int i;
250
        u8 control;
251
 
252
        dbg("%s -  port %d", __FUNCTION__, port->number);
253
 
254
        if ((!port->tty) || (!port->tty->termios)) {
255
                dbg("%s - no tty structures", __FUNCTION__);
256
                return;
257
        }
258
 
259
  spin_lock_irqsave(&priv->lock, flags);
260
 
261
        if (!priv->termios_initialized) {
262
    printk(KERN_INFO "@####### termios initializated\n");
263
                *(port->tty->termios) = tty_std_termios;
264
    port->tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
265
                priv->termios_initialized = 1;
266
        }
267
 
268
        spin_unlock_irqrestore(&priv->lock, flags);
269
 
270
        cflag = port->tty->termios->c_cflag;
271
        /* check that they really want us to change something */
272
        if (old_termios) {
273
                if ((cflag == old_termios->c_cflag) &&
274
                    (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
275
                    dbg("%s - nothing to change...", __FUNCTION__);
276
                    return;
277
                }
278
        }
279
 
280
        buf = kmalloc (7, GFP_KERNEL);
281
        if (!buf) {
282
                dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
283
                return;
284
        }
285
        memset (buf, 0x00, 0x07);
286
 
287
        i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
288
                             GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
289
                             0, 0, buf, 7, 100);
290
        dbg ("0xa1:0x21:0:0  %d - %x %x %x %x %x %x %x", i,
291
             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
292
 
293
 
294
        if (cflag & CSIZE) {
295
                switch (cflag & CSIZE) {
296
                        case CS5:       buf[6] = 5;     break;
297
                        case CS6:       buf[6] = 6;     break;
298
                        case CS7:       buf[6] = 7;     break;
299
                        default:
300
                        case CS8:       buf[6] = 8;     break;
301
                }
302
                dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
303
        }
304
 
305
        baud = 0;
306
        switch (cflag & CBAUD) {
307
                case B0:        baud = 0;       break;
308
                case B75:       baud = 75;      break;
309
                case B150:      baud = 150;     break;
310
                case B300:      baud = 300;     break;
311
                case B600:      baud = 600;     break;
312
                case B1200:     baud = 1200;    break;
313
                case B1800:     baud = 1800;    break;
314
                case B2400:     baud = 2400;    break;
315
                case B4800:     baud = 4800;    break;
316
                case B9600:     baud = 9600;    break;
317
                case B19200:    baud = 19200;   break;
318
                case B38400:    baud = 38400;   break;
319
                case B57600:    baud = 57600;   break;
320
                case B115200:   baud = 115200;  break;
321
                case B230400:   baud = 230400;  break;
322
                case B460800:   baud = 460800;  break;
323
                default:
324
                        dev_err(&port->dev, "pl2303 driver does not support the baudrate requested (fix it)\n");
325
                        break;
326
        }
327
        dbg("%s - baud = %d", __FUNCTION__, baud);
328
        if (baud) {
329
                buf[0] = baud & 0xff;
330
                buf[1] = (baud >> 8) & 0xff;
331
                buf[2] = (baud >> 16) & 0xff;
332
                buf[3] = (baud >> 24) & 0xff;
333
        }
334
 
335
        /* For reference buf[4]=0 is 1 stop bits */
336
        /* For reference buf[4]=1 is 1.5 stop bits */
337
        /* For reference buf[4]=2 is 2 stop bits */
338
        if (cflag & CSTOPB) {
339
                buf[4] = 2;
340
                dbg("%s - stop bits = 2", __FUNCTION__);
341
        } else {
342
                buf[4] = 0;
343
                dbg("%s - stop bits = 1", __FUNCTION__);
344
        }
345
 
346
        if (cflag & PARENB) {
347
                /* For reference buf[5]=0 is none parity */
348
                /* For reference buf[5]=1 is odd parity */
349
                /* For reference buf[5]=2 is even parity */
350
                /* For reference buf[5]=3 is mark parity */
351
                /* For reference buf[5]=4 is space parity */
352
                if (cflag & PARODD) {
353
                        buf[5] = 1;
354
                        dbg("%s - parity = odd", __FUNCTION__);
355
                } else {
356
                        buf[5] = 2;
357
                        dbg("%s - parity = even", __FUNCTION__);
358
                }
359
        } else {
360
                buf[5] = 0;
361
                dbg("%s - parity = none", __FUNCTION__);
362
        }
363
 
364
        i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
365
                             SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
366
                             0, 0, buf, 7, 100);
367
        dbg ("0x21:0x20:0:0  %d", i);
368
 
369
        /* change control lines if we are switching to or from B0 */
370
        spin_lock_irqsave(&priv->lock, flags);
371
        control = priv->line_control;
372
        if ((cflag & CBAUD) == B0)
373
                priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
374
        else
375
                priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
376
        if (control != priv->line_control) {
377
                control = priv->line_control;
378
                spin_unlock_irqrestore(&priv->lock, flags);
379
                set_control_lines(serial->dev, control);
380
        } else {
381
                spin_unlock_irqrestore(&priv->lock, flags);
382
        }
383
 
384
        buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
385
 
386
        i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
387
                             GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
388
                             0, 0, buf, 7, 100);
389
        dbg ("0xa1:0x21:0:0  %d - %x %x %x %x %x %x %x", i,
390
             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
391
 
392
        if (cflag & CRTSCTS) {
393
                i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
394
                                     VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
395
                                     0x0, 0x41, NULL, 0, 100);
396
                dbg ("0x40:0x1:0x0:0x41  %d", i);
397
        }
398
 
399
        kfree (buf);
400
}      
401
 
402
 
403
static int pl2303_open (struct usb_serial_port *port, struct file *filp)
404
{
405
        struct termios tmp_termios;
406
        struct usb_serial *serial = port->serial;
407
        unsigned char buf[10];
408
        int result;
409
 
410
        if (port_paranoia_check (port, __FUNCTION__))
411
                return -ENODEV;
412
 
413
        dbg("%s -  port %d", __FUNCTION__, port->number);
414
 
415
        usb_clear_halt(serial->dev, port->write_urb->pipe);
416
        usb_clear_halt(serial->dev, port->read_urb->pipe);
417
 
418
#define FISH(a,b,c,d)                                                           \
419
        result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0),     \
420
                               b, a, c, d, buf, 1, 100);                        \
421
        dbg("0x%x:0x%x:0x%x:0x%x  %d - %x",a,b,c,d,result,buf[0]);
422
 
423
#define SOUP(a,b,c,d)                                                           \
424
        result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0),     \
425
                               b, a, c, d, NULL, 0, 100);                       \
426
        dbg("0x%x:0x%x:0x%x:0x%x  %d",a,b,c,d,result);
427
 
428
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
429
        SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
430
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
431
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
432
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
433
        SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
434
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
435
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
436
 
437
        /* Setup termios */
438
        if (port->tty) {
439
    printk(KERN_INFO "#####@ set termios");
440
                pl2303_set_termios (port, &tmp_termios);
441
        }
442
 
443
        //FIXME: need to assert RTS and DTR if CRTSCTS off
444
 
445
        dbg("%s - submitting read urb", __FUNCTION__);
446
        port->read_urb->dev = serial->dev;
447
        result = usb_submit_urb (port->read_urb, GFP_KERNEL);
448
        if (result) {
449
                dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
450
                pl2303_close (port, NULL);
451
                return -EPROTO;
452
        }
453
 
454
        dbg("%s - submitting interrupt urb", __FUNCTION__);
455
        port->interrupt_in_urb->dev = serial->dev;
456
        result = usb_submit_urb (port->interrupt_in_urb, GFP_KERNEL);
457
        if (result) {
458
                dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n", __FUNCTION__, result);
459
                pl2303_close (port, NULL);
460
                return -EPROTO;
461
        }
462
        return 0;
463
}
464
 
465
 
466
static void pl2303_close (struct usb_serial_port *port, struct file *filp)
467
{
468
        struct usb_serial *serial;
469
        struct pl2303_private *priv;
470
        unsigned long flags;
471
        unsigned int c_cflag;
472
        int result;
473
 
474
        if (port_paranoia_check (port, __FUNCTION__))
475
                return;
476
        serial = get_usb_serial (port, __FUNCTION__);
477
        if (!serial)
478
                return;
479
 
480
        dbg("%s - port %d", __FUNCTION__, port->number);
481
 
482
        /* shutdown our urbs */
483
        dbg("%s - shutting down urbs", __FUNCTION__);
484
        result = usb_unlink_urb (port->write_urb);
485
        if (result)
486
                dbg("%s - usb_unlink_urb (write_urb)"
487
                    " failed with reason: %d", __FUNCTION__,
488
                     result);
489
 
490
        result = usb_unlink_urb (port->read_urb);
491
        if (result)
492
                dbg("%s - usb_unlink_urb (read_urb) "
493
                    "failed with reason: %d", __FUNCTION__,
494
                     result);
495
 
496
        result = usb_unlink_urb (port->interrupt_in_urb);
497
        if (result)
498
                dbg("%s - usb_unlink_urb (interrupt_in_urb)"
499
                    " failed with reason: %d", __FUNCTION__,
500
                     result);
501
 
502
        if (port->tty) {
503
                c_cflag = port->tty->termios->c_cflag;
504
                if (c_cflag & HUPCL) {
505
                        /* drop DTR and RTS */
506
                        priv = usb_get_serial_port_data(port);
507
                        spin_lock_irqsave(&priv->lock, flags);
508
                        priv->line_control = 0;
509
                        spin_unlock_irqrestore (&priv->lock, flags);
510
                        set_control_lines (port->serial->dev, 0);
511
                }
512
        }
513
 
514
}
515
 
516
static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
517
                            unsigned int set, unsigned int clear)
518
{
519
        struct pl2303_private *priv = usb_get_serial_port_data(port);
520
        unsigned long flags;
521
        u8 control;
522
 
523
        spin_lock_irqsave (&priv->lock, flags);
524
        if (set & TIOCM_RTS)
525
                priv->line_control |= CONTROL_RTS;
526
        if (set & TIOCM_DTR)
527
                priv->line_control |= CONTROL_DTR;
528
        if (clear & TIOCM_RTS)
529
                priv->line_control &= ~CONTROL_RTS;
530
        if (clear & TIOCM_DTR)
531
                priv->line_control &= ~CONTROL_DTR;
532
        control = priv->line_control;
533
        spin_unlock_irqrestore (&priv->lock, flags);
534
 
535
        return set_control_lines (port->serial->dev, control);
536
}
537
 
538
static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file)
539
{
540
        struct pl2303_private *priv = usb_get_serial_port_data(port);
541
        unsigned long flags;
542
        unsigned int mcr;
543
        unsigned int status;
544
        unsigned int result;
545
 
546
        dbg("%s (%d)", __FUNCTION__, port->number);
547
 
548
        spin_lock_irqsave (&priv->lock, flags);
549
        mcr = priv->line_control;
550
        status = priv->line_status;
551
        spin_unlock_irqrestore (&priv->lock, flags);
552
 
553
        result = ((mcr & CONTROL_DTR)           ? TIOCM_DTR : 0)
554
                  | ((mcr & CONTROL_RTS)        ? TIOCM_RTS : 0)
555
                  | ((status & UART_CTS)        ? TIOCM_CTS : 0)
556
                  | ((status & UART_DSR)        ? TIOCM_DSR : 0)
557
                  | ((status & UART_RING)       ? TIOCM_RI  : 0)
558
                  | ((status & UART_DCD)        ? TIOCM_CD  : 0);
559
 
560
        dbg("%s - result = %x", __FUNCTION__, result);
561
 
562
        return result;
563
}
564
 
565
static int pl2303_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
566
{
567
        dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
568
 
569
        switch (cmd) {
570
                default:
571
                        dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
572
                        break;
573
        }
574
 
575
        return -ENOIOCTLCMD;
576
}
577
 
578
static void pl2303_break_ctl (struct usb_serial_port *port, int break_state)
579
{
580
        struct usb_serial *serial = port->serial;
581
        u16 state;
582
        int result;
583
 
584
        dbg("%s - port %d", __FUNCTION__, port->number);
585
 
586
        if (break_state == 0)
587
                state = BREAK_OFF;
588
        else
589
                state = BREAK_ON;
590
        dbg("%s - turning break %s", state==BREAK_OFF ? "off" : "on", __FUNCTION__);
591
 
592
        result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
593
                                  BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
594
                                  0, NULL, 0, 100);
595
        if (result)
596
                dbg("%s - error sending break = %d", __FUNCTION__, result);
597
}
598
 
599
 
600
static void pl2303_shutdown (struct usb_serial *serial)
601
{
602
        int i;
603
 
604
        dbg("%s", __FUNCTION__);
605
 
606
        for (i = 0; i < serial->num_ports; ++i) {
607
                kfree (usb_get_serial_port_data(serial->port[i]));
608
                usb_set_serial_port_data(serial->port[i], NULL);
609
        }              
610
}
611
 
612
 
613
static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs)
614
{
615
        struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
616
        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
617
        struct pl2303_private *priv = usb_get_serial_port_data(port);
618
        unsigned char *data = urb->transfer_buffer;
619
        unsigned long flags;
620
        int status;
621
 
622
        dbg("%s (%d)", __FUNCTION__, port->number);
623
 
624
        switch (urb->status) {
625
        case 0:
626
                /* success */
627
                break;
628
        case -ECONNRESET:
629
        case -ENOENT:
630
        case -ESHUTDOWN:
631
                /* this urb is terminated, clean up */
632
                dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
633
                return;
634
        default:
635
                dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
636
                goto exit;
637
        }
638
 
639
        if (!serial) {
640
                return;
641
        }
642
 
643
        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
644
 
645
        if (urb->actual_length < UART_STATE)
646
                goto exit;
647
 
648
        /* Save off the uart status for others to look at */
649
        spin_lock_irqsave(&priv->lock, flags);
650
        priv->line_status = data[UART_STATE];
651
        spin_unlock_irqrestore(&priv->lock, flags);
652
 
653
exit:
654
        status = usb_submit_urb (urb, GFP_ATOMIC);
655
        if (status)
656
                dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
657
                        __FUNCTION__, status);
658
}
659
 
660
 
661
static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
662
{
663
        struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
664
        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
665
        struct pl2303_private *priv = usb_get_serial_port_data(port);
666
        struct tty_struct *tty;
667
        unsigned char *data = urb->transfer_buffer;
668
        unsigned long flags;
669
        int i;
670
        int result;
671
        u8 status;
672
        char tty_flag;
673
 
674
        if (port_paranoia_check (port, __FUNCTION__))
675
                return;
676
 
677
        dbg("%s - port %d", __FUNCTION__, port->number);
678
 
679
        if (!serial) {
680
                dbg("%s - bad serial pointer, exiting", __FUNCTION__);
681
                return;
682
        }
683
 
684
 
685
        if (urb->status) {
686
                dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
687
                if (!port->open_count) {
688
                        dbg("%s - port is closed, exiting.", __FUNCTION__);
689
                        return;
690
                }
691
                if (urb->status == -EPROTO) {
692
                        /* PL2303 mysteriously fails with -EPROTO reschedule the read */
693
                        dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
694
                        urb->status = 0;
695
                        urb->dev = serial->dev;
696
                        result = usb_submit_urb(urb, GFP_ATOMIC);
697
                        if (result)
698
                                dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
699
                        return;
700
                }
701
                dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
702
                return;
703
        }
704
 
705
        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
706
 
707
        /* get tty_flag from status */
708
        tty_flag = TTY_NORMAL;
709
 
710
        spin_lock_irqsave(&priv->lock, flags);
711
        status = priv->line_status;
712
        spin_unlock_irqrestore(&priv->lock, flags);
713
 
714
        /* break takes precedence over parity, */
715
        /* which takes precedence over framing errors */
716
        if (status & UART_BREAK_ERROR )
717
                tty_flag = TTY_BREAK;
718
        else if (status & UART_PARITY_ERROR)
719
                tty_flag = TTY_PARITY;
720
        else if (status & UART_FRAME_ERROR)
721
                tty_flag = TTY_FRAME;
722
        dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
723
 
724
        tty = port->tty;
725
        if (tty && urb->actual_length) {
726
                /* overrun is special, not associated with a char */
727
                if (status & UART_OVERRUN_ERROR)
728
                        tty_insert_flip_char(tty, 0, TTY_OVERRUN);
729
                for (i = 0; i < urb->actual_length; ++i) {
730
                        if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
731
                                tty_flip_buffer_push(tty);
732
                        }
733
//**#ifdef DEBUG_ME
734
                        tty_insert_flip_char (tty, data[i], tty_flag);
735
//**#endif /* DEBUG_ME */
736
                }
737
                tty_flip_buffer_push (tty);
738
        }
739
 
740
 
741
        /* Schedule the next read _if_ we are still open */
742
        if (port->open_count) {
743
                urb->dev = serial->dev;
744
                result = usb_submit_urb(urb, GFP_ATOMIC);
745
                if (result)
746
                        dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
747
        }
748
 
749
 
750
        return;
751
}
752
 
753
 
754
 
755
static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
756
{
757
        struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
758
        int result;
759
 
760
        if (port_paranoia_check (port, __FUNCTION__))
761
                return;
762
 
763
        dbg("%s - port %d", __FUNCTION__, port->number);
764
 
765
        if (urb->status) {
766
                /* error in the urb, so we have to resubmit it */
767
                if (serial_paranoia_check (port->serial, __FUNCTION__)) {
768
                        return;
769
                }
770
                dbg("%s - Overflow in write", __FUNCTION__);
771
                dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
772
                port->write_urb->transfer_buffer_length = 1;
773
                port->write_urb->dev = port->serial->dev;
774
                result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
775
                if (result)
776
                        dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", __FUNCTION__, result);
777
 
778
                return;
779
        }
780
 
781
        schedule_work(&port->work);
782
}
783
 
784
 
785
/*static*/ int __init pl2303_init (void)
786
{
787
        int retval;
788
        retval = usb_serial_register(&pl2303_device);
789
        if (retval)
790
                goto failed_usb_serial_register;
791
        retval = usb_register(&pl2303_driver);
792
        if (retval)
793
                goto failed_usb_register;
794
        info(DRIVER_DESC " " DRIVER_VERSION);
795
        return 0;
796
failed_usb_register:
797
        usb_serial_deregister(&pl2303_device);
798
failed_usb_serial_register:
799
        return retval;
800
}
801
 
802
 
803
/*static*/ void __exit pl2303_exit (void)
804
{
805
        usb_deregister (&pl2303_driver);
806
        usb_serial_deregister (&pl2303_device);
807
}
808
 
809
 
810
module_init(pl2303_init);
811
module_exit(pl2303_exit);
812
 
813
MODULE_DESCRIPTION(DRIVER_DESC);
814
MODULE_LICENSE("GPL");
815
 
816
MODULE_PARM(debug, "i");
817
MODULE_PARM_DESC(debug, "Debug enabled or not");
818