Subversion Repositories shark

Rev

Rev 846 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1049 mauro 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
                *(port->tty->termios) = tty_std_termios;
263
    port->tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
264
                priv->termios_initialized = 1;
265
        }
266
 
267
        spin_unlock_irqrestore(&priv->lock, flags);
268
 
269
        cflag = port->tty->termios->c_cflag;
270
        /* check that they really want us to change something */
271
        if (old_termios) {
272
                if ((cflag == old_termios->c_cflag) &&
273
                    (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
274
                    dbg("%s - nothing to change...", __FUNCTION__);
275
                    return;
276
                }
277
        }
278
 
279
        buf = kmalloc (7, GFP_KERNEL);
280
        if (!buf) {
281
                dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
282
                return;
283
        }
284
        memset (buf, 0x00, 0x07);
285
 
286
        i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
287
                             GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
288
                             0, 0, buf, 7, 100);
289
        dbg ("0xa1:0x21:0:0  %d - %x %x %x %x %x %x %x", i,
290
             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
291
 
292
 
293
        if (cflag & CSIZE) {
294
                switch (cflag & CSIZE) {
295
                        case CS5:       buf[6] = 5;     break;
296
                        case CS6:       buf[6] = 6;     break;
297
                        case CS7:       buf[6] = 7;     break;
298
                        default:
299
                        case CS8:       buf[6] = 8;     break;
300
                }
301
                dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
302
        }
303
 
304
        baud = 0;
305
        switch (cflag & CBAUD) {
306
                case B0:        baud = 0;       break;
307
                case B75:       baud = 75;      break;
308
                case B150:      baud = 150;     break;
309
                case B300:      baud = 300;     break;
310
                case B600:      baud = 600;     break;
311
                case B1200:     baud = 1200;    break;
312
                case B1800:     baud = 1800;    break;
313
                case B2400:     baud = 2400;    break;
314
                case B4800:     baud = 4800;    break;
315
                case B9600:     baud = 9600;    break;
316
                case B19200:    baud = 19200;   break;
317
                case B38400:    baud = 38400;   break;
318
                case B57600:    baud = 57600;   break;
319
                case B115200:   baud = 115200;  break;
320
                case B230400:   baud = 230400;  break;
321
                case B460800:   baud = 460800;  break;
322
                default:
323
                        dev_err(&port->dev, "pl2303 driver does not support the baudrate requested (fix it)\n");
324
                        break;
325
        }
326
        dbg("%s - baud = %d", __FUNCTION__, baud);
327
        if (baud) {
328
                buf[0] = baud & 0xff;
329
                buf[1] = (baud >> 8) & 0xff;
330
                buf[2] = (baud >> 16) & 0xff;
331
                buf[3] = (baud >> 24) & 0xff;
332
        }
333
 
334
        /* For reference buf[4]=0 is 1 stop bits */
335
        /* For reference buf[4]=1 is 1.5 stop bits */
336
        /* For reference buf[4]=2 is 2 stop bits */
337
        if (cflag & CSTOPB) {
338
                buf[4] = 2;
339
                dbg("%s - stop bits = 2", __FUNCTION__);
340
        } else {
341
                buf[4] = 0;
342
                dbg("%s - stop bits = 1", __FUNCTION__);
343
        }
344
 
345
        if (cflag & PARENB) {
346
                /* For reference buf[5]=0 is none parity */
347
                /* For reference buf[5]=1 is odd parity */
348
                /* For reference buf[5]=2 is even parity */
349
                /* For reference buf[5]=3 is mark parity */
350
                /* For reference buf[5]=4 is space parity */
351
                if (cflag & PARODD) {
352
                        buf[5] = 1;
353
                        dbg("%s - parity = odd", __FUNCTION__);
354
                } else {
355
                        buf[5] = 2;
356
                        dbg("%s - parity = even", __FUNCTION__);
357
                }
358
        } else {
359
                buf[5] = 0;
360
                dbg("%s - parity = none", __FUNCTION__);
361
        }
362
 
363
        i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
364
                             SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
365
                             0, 0, buf, 7, 100);
366
        dbg ("0x21:0x20:0:0  %d", i);
367
 
368
        /* change control lines if we are switching to or from B0 */
369
        spin_lock_irqsave(&priv->lock, flags);
370
        control = priv->line_control;
371
        if ((cflag & CBAUD) == B0)
372
                priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
373
        else
374
                priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
375
        if (control != priv->line_control) {
376
                control = priv->line_control;
377
                spin_unlock_irqrestore(&priv->lock, flags);
378
                set_control_lines(serial->dev, control);
379
        } else {
380
                spin_unlock_irqrestore(&priv->lock, flags);
381
        }
382
 
383
        buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
384
 
385
        i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
386
                             GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
387
                             0, 0, buf, 7, 100);
388
        dbg ("0xa1:0x21:0:0  %d - %x %x %x %x %x %x %x", i,
389
             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
390
 
391
        if (cflag & CRTSCTS) {
392
                i = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
393
                                     VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
394
                                     0x0, 0x41, NULL, 0, 100);
395
                dbg ("0x40:0x1:0x0:0x41  %d", i);
396
        }
397
 
398
        kfree (buf);
399
}      
400
 
401
 
402
static int pl2303_open (struct usb_serial_port *port, struct file *filp)
403
{
404
        struct termios tmp_termios;
405
        struct usb_serial *serial = port->serial;
406
        unsigned char buf[10];
407
        int result;
408
 
409
        if (port_paranoia_check (port, __FUNCTION__))
410
                return -ENODEV;
411
 
412
        dbg("%s -  port %d", __FUNCTION__, port->number);
413
 
414
        usb_clear_halt(serial->dev, port->write_urb->pipe);
415
        usb_clear_halt(serial->dev, port->read_urb->pipe);
416
 
417
#define FISH(a,b,c,d)                                                           \
418
        result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0),     \
419
                               b, a, c, d, buf, 1, 100);                        \
420
        dbg("0x%x:0x%x:0x%x:0x%x  %d - %x",a,b,c,d,result,buf[0]);
421
 
422
#define SOUP(a,b,c,d)                                                           \
423
        result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0),     \
424
                               b, a, c, d, NULL, 0, 100);                       \
425
        dbg("0x%x:0x%x:0x%x:0x%x  %d",a,b,c,d,result);
426
 
427
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
428
        SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
429
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
430
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
431
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
432
        SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
433
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
434
        FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
435
 
436
        /* Setup termios */
437
        if (port->tty) {
438
                pl2303_set_termios (port, &tmp_termios);
439
        }
440
 
441
        //FIXME: need to assert RTS and DTR if CRTSCTS off
442
 
443
        dbg("%s - submitting read urb", __FUNCTION__);
444
        port->read_urb->dev = serial->dev;
445
        result = usb_submit_urb (port->read_urb, GFP_KERNEL);
446
        if (result) {
447
                dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
448
                pl2303_close (port, NULL);
449
                return -EPROTO;
450
        }
451
 
452
        dbg("%s - submitting interrupt urb", __FUNCTION__);
453
        port->interrupt_in_urb->dev = serial->dev;
454
        result = usb_submit_urb (port->interrupt_in_urb, GFP_KERNEL);
455
        if (result) {
456
                dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n", __FUNCTION__, result);
457
                pl2303_close (port, NULL);
458
                return -EPROTO;
459
        }
460
        return 0;
461
}
462
 
463
 
464
static void pl2303_close (struct usb_serial_port *port, struct file *filp)
465
{
466
        struct usb_serial *serial;
467
        struct pl2303_private *priv;
468
        unsigned long flags;
469
        unsigned int c_cflag;
470
        int result;
471
 
472
        if (port_paranoia_check (port, __FUNCTION__))
473
                return;
474
        serial = get_usb_serial (port, __FUNCTION__);
475
        if (!serial)
476
                return;
477
 
478
        dbg("%s - port %d", __FUNCTION__, port->number);
479
 
480
        /* shutdown our urbs */
481
        dbg("%s - shutting down urbs", __FUNCTION__);
482
        result = usb_unlink_urb (port->write_urb);
483
        if (result)
484
                dbg("%s - usb_unlink_urb (write_urb)"
485
                    " failed with reason: %d", __FUNCTION__,
486
                     result);
487
 
488
        result = usb_unlink_urb (port->read_urb);
489
        if (result)
490
                dbg("%s - usb_unlink_urb (read_urb) "
491
                    "failed with reason: %d", __FUNCTION__,
492
                     result);
493
 
494
        result = usb_unlink_urb (port->interrupt_in_urb);
495
        if (result)
496
                dbg("%s - usb_unlink_urb (interrupt_in_urb)"
497
                    " failed with reason: %d", __FUNCTION__,
498
                     result);
499
 
500
        if (port->tty) {
501
                c_cflag = port->tty->termios->c_cflag;
502
                if (c_cflag & HUPCL) {
503
                        /* drop DTR and RTS */
504
                        priv = usb_get_serial_port_data(port);
505
                        spin_lock_irqsave(&priv->lock, flags);
506
                        priv->line_control = 0;
507
                        spin_unlock_irqrestore (&priv->lock, flags);
508
                        set_control_lines (port->serial->dev, 0);
509
                }
510
        }
511
 
512
}
513
 
514
static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
515
                            unsigned int set, unsigned int clear)
516
{
517
        struct pl2303_private *priv = usb_get_serial_port_data(port);
518
        unsigned long flags;
519
        u8 control;
520
 
521
        spin_lock_irqsave (&priv->lock, flags);
522
        if (set & TIOCM_RTS)
523
                priv->line_control |= CONTROL_RTS;
524
        if (set & TIOCM_DTR)
525
                priv->line_control |= CONTROL_DTR;
526
        if (clear & TIOCM_RTS)
527
                priv->line_control &= ~CONTROL_RTS;
528
        if (clear & TIOCM_DTR)
529
                priv->line_control &= ~CONTROL_DTR;
530
        control = priv->line_control;
531
        spin_unlock_irqrestore (&priv->lock, flags);
532
 
533
        return set_control_lines (port->serial->dev, control);
534
}
535
 
536
static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file)
537
{
538
        struct pl2303_private *priv = usb_get_serial_port_data(port);
539
        unsigned long flags;
540
        unsigned int mcr;
541
        unsigned int status;
542
        unsigned int result;
543
 
544
        dbg("%s (%d)", __FUNCTION__, port->number);
545
 
546
        spin_lock_irqsave (&priv->lock, flags);
547
        mcr = priv->line_control;
548
        status = priv->line_status;
549
        spin_unlock_irqrestore (&priv->lock, flags);
550
 
551
        result = ((mcr & CONTROL_DTR)           ? TIOCM_DTR : 0)
552
                  | ((mcr & CONTROL_RTS)        ? TIOCM_RTS : 0)
553
                  | ((status & UART_CTS)        ? TIOCM_CTS : 0)
554
                  | ((status & UART_DSR)        ? TIOCM_DSR : 0)
555
                  | ((status & UART_RING)       ? TIOCM_RI  : 0)
556
                  | ((status & UART_DCD)        ? TIOCM_CD  : 0);
557
 
558
        dbg("%s - result = %x", __FUNCTION__, result);
559
 
560
        return result;
561
}
562
 
563
static int pl2303_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
564
{
565
        dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
566
 
567
        switch (cmd) {
568
                default:
569
                        dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
570
                        break;
571
        }
572
 
573
        return -ENOIOCTLCMD;
574
}
575
 
576
static void pl2303_break_ctl (struct usb_serial_port *port, int break_state)
577
{
578
        struct usb_serial *serial = port->serial;
579
        u16 state;
580
        int result;
581
 
582
        dbg("%s - port %d", __FUNCTION__, port->number);
583
 
584
        if (break_state == 0)
585
                state = BREAK_OFF;
586
        else
587
                state = BREAK_ON;
588
        dbg("%s - turning break %s", state==BREAK_OFF ? "off" : "on", __FUNCTION__);
589
 
590
        result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
591
                                  BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
592
                                  0, NULL, 0, 100);
593
        if (result)
594
                dbg("%s - error sending break = %d", __FUNCTION__, result);
595
}
596
 
597
 
598
static void pl2303_shutdown (struct usb_serial *serial)
599
{
600
        int i;
601
 
602
        dbg("%s", __FUNCTION__);
603
 
604
        for (i = 0; i < serial->num_ports; ++i) {
605
                kfree (usb_get_serial_port_data(serial->port[i]));
606
                usb_set_serial_port_data(serial->port[i], NULL);
607
        }              
608
}
609
 
610
 
611
static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs)
612
{
613
        struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
614
        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
615
        struct pl2303_private *priv = usb_get_serial_port_data(port);
616
        unsigned char *data = urb->transfer_buffer;
617
        unsigned long flags;
618
        int status;
619
 
620
        dbg("%s (%d)", __FUNCTION__, port->number);
621
 
622
        switch (urb->status) {
623
        case 0:
624
                /* success */
625
                break;
626
        case -ECONNRESET:
627
        case -ENOENT:
628
        case -ESHUTDOWN:
629
                /* this urb is terminated, clean up */
630
                dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
631
                return;
632
        default:
633
                dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
634
                goto exit;
635
        }
636
 
637
        if (!serial) {
638
                return;
639
        }
640
 
641
        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
642
 
643
        if (urb->actual_length < UART_STATE)
644
                goto exit;
645
 
646
        /* Save off the uart status for others to look at */
647
        spin_lock_irqsave(&priv->lock, flags);
648
        priv->line_status = data[UART_STATE];
649
        spin_unlock_irqrestore(&priv->lock, flags);
650
 
651
exit:
652
        status = usb_submit_urb (urb, GFP_ATOMIC);
653
        if (status)
654
                dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
655
                        __FUNCTION__, status);
656
}
657
 
658
 
659
static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
660
{
661
        struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
662
        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
663
        struct pl2303_private *priv = usb_get_serial_port_data(port);
664
        struct tty_struct *tty;
665
        unsigned char *data = urb->transfer_buffer;
666
        unsigned long flags;
667
        int i;
668
        int result;
669
        u8 status;
670
        char tty_flag;
671
 
672
        if (port_paranoia_check (port, __FUNCTION__))
673
                return;
674
 
675
        dbg("%s - port %d", __FUNCTION__, port->number);
676
 
677
        if (!serial) {
678
                dbg("%s - bad serial pointer, exiting", __FUNCTION__);
679
                return;
680
        }
681
 
682
 
683
        if (urb->status) {
684
                dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
685
                if (!port->open_count) {
686
                        dbg("%s - port is closed, exiting.", __FUNCTION__);
687
                        return;
688
                }
689
                if (urb->status == -EPROTO) {
690
                        /* PL2303 mysteriously fails with -EPROTO reschedule the read */
691
                        dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
692
                        urb->status = 0;
693
                        urb->dev = serial->dev;
694
                        result = usb_submit_urb(urb, GFP_ATOMIC);
695
                        if (result)
696
                                dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
697
                        return;
698
                }
699
                dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
700
                return;
701
        }
702
 
703
        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
704
 
705
        /* get tty_flag from status */
706
        tty_flag = TTY_NORMAL;
707
 
708
        spin_lock_irqsave(&priv->lock, flags);
709
        status = priv->line_status;
710
        spin_unlock_irqrestore(&priv->lock, flags);
711
 
712
        /* break takes precedence over parity, */
713
        /* which takes precedence over framing errors */
714
        if (status & UART_BREAK_ERROR )
715
                tty_flag = TTY_BREAK;
716
        else if (status & UART_PARITY_ERROR)
717
                tty_flag = TTY_PARITY;
718
        else if (status & UART_FRAME_ERROR)
719
                tty_flag = TTY_FRAME;
720
        dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
721
 
722
        tty = port->tty;
723
        if (tty && urb->actual_length) {
724
                /* overrun is special, not associated with a char */
725
                if (status & UART_OVERRUN_ERROR)
726
      tty_insert_flip_char(tty, 0, TTY_OVERRUN);
727
                for (i = 0; i < urb->actual_length; ++i) {
728
                        if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
729
                                tty_flip_buffer_push(tty);
730
                        }
731
//**#ifdef DEBUG_ME
732
      tty_insert_flip_char (tty, data[i], tty_flag);
733
//**#endif /* DEBUG_ME */
734
                }
735
                tty_flip_buffer_push (tty);
736
        }
737
 
738
 
739
        /* Schedule the next read _if_ we are still open */
740
        if (port->open_count) {
741
                urb->dev = serial->dev;
742
                result = usb_submit_urb(urb, GFP_ATOMIC);
743
                if (result)
744
                        dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
745
        }
746
 
747
 
748
        return;
749
}
750
 
751
 
752
 
753
static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
754
{
755
        struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
756
        int result;
757
 
758
        if (port_paranoia_check (port, __FUNCTION__))
759
                return;
760
 
761
        dbg("%s - port %d", __FUNCTION__, port->number);
762
 
763
        if (urb->status) {
764
                /* error in the urb, so we have to resubmit it */
765
                if (serial_paranoia_check (port->serial, __FUNCTION__)) {
766
                        return;
767
                }
768
                dbg("%s - Overflow in write", __FUNCTION__);
769
                dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
770
                port->write_urb->transfer_buffer_length = 1;
771
                port->write_urb->dev = port->serial->dev;
772
                result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
773
                if (result)
774
                        dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n", __FUNCTION__, result);
775
 
776
                return;
777
        }
778
 
779
        schedule_work(&port->work);
780
}
781
 
782
 
783
/*static*/ int __init pl2303_init (void)
784
{
785
        int retval;
786
        retval = usb_serial_register(&pl2303_device);
787
        if (retval)
788
                goto failed_usb_serial_register;
789
        retval = usb_register(&pl2303_driver);
790
        if (retval)
791
                goto failed_usb_register;
792
        info(DRIVER_DESC " " DRIVER_VERSION);
793
        return 0;
794
failed_usb_register:
795
        usb_serial_deregister(&pl2303_device);
796
failed_usb_serial_register:
797
        return retval;
798
}
799
 
800
 
801
/*static*/ void __exit pl2303_exit (void)
802
{
803
        usb_deregister (&pl2303_driver);
804
        usb_serial_deregister (&pl2303_device);
805
}
806
 
807
 
808
module_init(pl2303_init);
809
module_exit(pl2303_exit);
810
 
811
MODULE_DESCRIPTION(DRIVER_DESC);
812
MODULE_LICENSE("GPL");
813
 
814
MODULE_PARM(debug, "i");
815
MODULE_PARM_DESC(debug, "Debug enabled or not");
816