USB: oti6858: use kfifo to implement write buffering
[safe/jmp/linux-2.6] / drivers / usb / serial / oti6858.c
1 /*
2  * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
3  *
4  * Copyleft  (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
5  * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
6  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
7  * Copyright (C) 2003 IBM Corp.
8  *
9  * Many thanks to the authors of pl2303 driver: all functions in this file
10  * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
11  *
12  * Warning! You use this driver on your own risk! The only official
13  * description of this device I have is datasheet from manufacturer,
14  * and it doesn't contain almost any information needed to write a driver.
15  * Almost all knowlegde used while writing this driver was gathered by:
16  *  - analyzing traffic between device and the M$ Windows 2000 driver,
17  *  - trying different bit combinations and checking pin states
18  *    with a voltmeter,
19  *  - receiving malformed frames and producing buffer overflows
20  *    to learn how errors are reported,
21  * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
22  * CONNECTED TO IT!
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License.
27  *
28  * See Documentation/usb/usb-serial.txt for more information on using this
29  * driver
30  *
31  * TODO:
32  *  - implement correct flushing for ioctls and oti6858_close()
33  *  - check how errors (rx overflow, parity error, framing error) are reported
34  *  - implement oti6858_break_ctl()
35  *  - implement more ioctls
36  *  - test/implement flow control
37  *  - allow setting custom baud rates
38  */
39
40 #include <linux/kernel.h>
41 #include <linux/errno.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/tty.h>
45 #include <linux/tty_driver.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial.h>
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/spinlock.h>
51 #include <linux/usb.h>
52 #include <linux/usb/serial.h>
53 #include <linux/uaccess.h>
54 #include <linux/kfifo.h>
55 #include "oti6858.h"
56
57 #define OTI6858_DESCRIPTION \
58         "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
59 #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
60 #define OTI6858_VERSION "0.2"
61
62 static const struct usb_device_id id_table[] = {
63         { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
64         { }
65 };
66
67 MODULE_DEVICE_TABLE(usb, id_table);
68
69 static struct usb_driver oti6858_driver = {
70         .name =         "oti6858",
71         .probe =        usb_serial_probe,
72         .disconnect =   usb_serial_disconnect,
73         .id_table =     id_table,
74         .no_dynamic_id =        1,
75 };
76
77 static int debug;
78
79 #define OTI6858_FIFO_SIZE               1024
80
81 /* requests */
82 #define OTI6858_REQ_GET_STATUS          (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
83 #define OTI6858_REQ_T_GET_STATUS        0x01
84
85 #define OTI6858_REQ_SET_LINE            (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
86 #define OTI6858_REQ_T_SET_LINE          0x00
87
88 #define OTI6858_REQ_CHECK_TXBUFF        (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
89 #define OTI6858_REQ_T_CHECK_TXBUFF      0x00
90
91 /* format of the control packet */
92 struct oti6858_control_pkt {
93         __le16  divisor;        /* baud rate = 96000000 / (16 * divisor), LE */
94 #define OTI6858_MAX_BAUD_RATE   3000000
95         u8      frame_fmt;
96 #define FMT_STOP_BITS_MASK      0xc0
97 #define FMT_STOP_BITS_1         0x00
98 #define FMT_STOP_BITS_2         0x40    /* 1.5 stop bits if FMT_DATA_BITS_5 */
99 #define FMT_PARITY_MASK         0x38
100 #define FMT_PARITY_NONE         0x00
101 #define FMT_PARITY_ODD          0x08
102 #define FMT_PARITY_EVEN         0x18
103 #define FMT_PARITY_MARK         0x28
104 #define FMT_PARITY_SPACE        0x38
105 #define FMT_DATA_BITS_MASK      0x03
106 #define FMT_DATA_BITS_5         0x00
107 #define FMT_DATA_BITS_6         0x01
108 #define FMT_DATA_BITS_7         0x02
109 #define FMT_DATA_BITS_8         0x03
110         u8      something;      /* always equals 0x43 */
111         u8      control;        /* settings of flow control lines */
112 #define CONTROL_MASK            0x0c
113 #define CONTROL_DTR_HIGH        0x08
114 #define CONTROL_RTS_HIGH        0x04
115         u8      tx_status;
116 #define TX_BUFFER_EMPTIED       0x09
117         u8      pin_state;
118 #define PIN_MASK                0x3f
119 #define PIN_RTS                 0x20    /* output pin */
120 #define PIN_CTS                 0x10    /* input pin, active low */
121 #define PIN_DSR                 0x08    /* input pin, active low */
122 #define PIN_DTR                 0x04    /* output pin */
123 #define PIN_RI                  0x02    /* input pin, active low */
124 #define PIN_DCD                 0x01    /* input pin, active low */
125         u8      rx_bytes_avail;         /* number of bytes in rx buffer */;
126 };
127
128 #define OTI6858_CTRL_PKT_SIZE   sizeof(struct oti6858_control_pkt)
129 #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
130         (((a)->divisor == (priv)->pending_setup.divisor) \
131           && ((a)->control == (priv)->pending_setup.control) \
132           && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
133
134 /* function prototypes */
135 static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
136 static void oti6858_close(struct usb_serial_port *port);
137 static void oti6858_set_termios(struct tty_struct *tty,
138                         struct usb_serial_port *port, struct ktermios *old);
139 static void oti6858_init_termios(struct tty_struct *tty);
140 static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
141                         unsigned int cmd, unsigned long arg);
142 static void oti6858_read_int_callback(struct urb *urb);
143 static void oti6858_read_bulk_callback(struct urb *urb);
144 static void oti6858_write_bulk_callback(struct urb *urb);
145 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
146                         const unsigned char *buf, int count);
147 static int oti6858_write_room(struct tty_struct *tty);
148 static int oti6858_chars_in_buffer(struct tty_struct *tty);
149 static int oti6858_tiocmget(struct tty_struct *tty, struct file *file);
150 static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
151                                 unsigned int set, unsigned int clear);
152 static int oti6858_startup(struct usb_serial *serial);
153 static void oti6858_release(struct usb_serial *serial);
154
155 /* device info */
156 static struct usb_serial_driver oti6858_device = {
157         .driver = {
158                 .owner =        THIS_MODULE,
159                 .name =         "oti6858",
160         },
161         .id_table =             id_table,
162         .num_ports =            1,
163         .open =                 oti6858_open,
164         .close =                oti6858_close,
165         .write =                oti6858_write,
166         .ioctl =                oti6858_ioctl,
167         .set_termios =          oti6858_set_termios,
168         .init_termios =         oti6858_init_termios,
169         .tiocmget =             oti6858_tiocmget,
170         .tiocmset =             oti6858_tiocmset,
171         .read_bulk_callback =   oti6858_read_bulk_callback,
172         .read_int_callback =    oti6858_read_int_callback,
173         .write_bulk_callback =  oti6858_write_bulk_callback,
174         .write_room =           oti6858_write_room,
175         .chars_in_buffer =      oti6858_chars_in_buffer,
176         .attach =               oti6858_startup,
177         .release =              oti6858_release,
178 };
179
180 struct oti6858_private {
181         spinlock_t lock;
182
183         struct kfifo write_fifo;
184         struct oti6858_control_pkt status;
185
186         struct {
187                 u8 read_urb_in_use;
188                 u8 write_urb_in_use;
189         } flags;
190         struct delayed_work delayed_write_work;
191
192         struct {
193                 __le16 divisor;
194                 u8 frame_fmt;
195                 u8 control;
196         } pending_setup;
197         u8 transient;
198         u8 setup_done;
199         struct delayed_work delayed_setup_work;
200
201         wait_queue_head_t intr_wait;
202         struct usb_serial_port *port;   /* USB port with which associated */
203 };
204
205 static void setup_line(struct work_struct *work)
206 {
207         struct oti6858_private *priv = container_of(work,
208                         struct oti6858_private, delayed_setup_work.work);
209         struct usb_serial_port *port = priv->port;
210         struct oti6858_control_pkt *new_setup;
211         unsigned long flags;
212         int result;
213
214         dbg("%s(port = %d)", __func__, port->number);
215
216         new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
217         if (new_setup == NULL) {
218                 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
219                 /* we will try again */
220                 schedule_delayed_work(&priv->delayed_setup_work,
221                                                 msecs_to_jiffies(2));
222                 return;
223         }
224
225         result = usb_control_msg(port->serial->dev,
226                                 usb_rcvctrlpipe(port->serial->dev, 0),
227                                 OTI6858_REQ_T_GET_STATUS,
228                                 OTI6858_REQ_GET_STATUS,
229                                 0, 0,
230                                 new_setup, OTI6858_CTRL_PKT_SIZE,
231                                 100);
232
233         if (result != OTI6858_CTRL_PKT_SIZE) {
234                 dev_err(&port->dev, "%s(): error reading status\n", __func__);
235                 kfree(new_setup);
236                 /* we will try again */
237                 schedule_delayed_work(&priv->delayed_setup_work,
238                                                         msecs_to_jiffies(2));
239                 return;
240         }
241
242         spin_lock_irqsave(&priv->lock, flags);
243         if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
244                 new_setup->divisor = priv->pending_setup.divisor;
245                 new_setup->control = priv->pending_setup.control;
246                 new_setup->frame_fmt = priv->pending_setup.frame_fmt;
247
248                 spin_unlock_irqrestore(&priv->lock, flags);
249                 result = usb_control_msg(port->serial->dev,
250                                         usb_sndctrlpipe(port->serial->dev, 0),
251                                         OTI6858_REQ_T_SET_LINE,
252                                         OTI6858_REQ_SET_LINE,
253                                         0, 0,
254                                         new_setup, OTI6858_CTRL_PKT_SIZE,
255                                         100);
256         } else {
257                 spin_unlock_irqrestore(&priv->lock, flags);
258                 result = 0;
259         }
260         kfree(new_setup);
261
262         spin_lock_irqsave(&priv->lock, flags);
263         if (result != OTI6858_CTRL_PKT_SIZE)
264                 priv->transient = 0;
265         priv->setup_done = 1;
266         spin_unlock_irqrestore(&priv->lock, flags);
267
268         dbg("%s(): submitting interrupt urb", __func__);
269         port->interrupt_in_urb->dev = port->serial->dev;
270         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
271         if (result != 0) {
272                 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
273                                 " with error %d\n", __func__, result);
274         }
275 }
276
277 static void send_data(struct work_struct *work)
278 {
279         struct oti6858_private *priv = container_of(work,
280                         struct oti6858_private, delayed_write_work.work);
281         struct usb_serial_port *port = priv->port;
282         int count = 0, result;
283         unsigned long flags;
284         u8 *allow;
285
286         dbg("%s(port = %d)", __func__, port->number);
287
288         spin_lock_irqsave(&priv->lock, flags);
289         if (priv->flags.write_urb_in_use) {
290                 spin_unlock_irqrestore(&priv->lock, flags);
291                 schedule_delayed_work(&priv->delayed_write_work,
292                                                 msecs_to_jiffies(2));
293                 return;
294         }
295         priv->flags.write_urb_in_use = 1;
296
297         count = kfifo_len(&priv->write_fifo);
298         spin_unlock_irqrestore(&priv->lock, flags);
299         if (count > port->bulk_out_size)
300                 count = port->bulk_out_size;
301
302         if (count != 0) {
303                 allow = kmalloc(1, GFP_KERNEL);
304                 if (!allow) {
305                         dev_err(&port->dev, "%s(): kmalloc failed\n",
306                                         __func__);
307                         return;
308                 }
309                 result = usb_control_msg(port->serial->dev,
310                                 usb_rcvctrlpipe(port->serial->dev, 0),
311                                 OTI6858_REQ_T_CHECK_TXBUFF,
312                                 OTI6858_REQ_CHECK_TXBUFF,
313                                 count, 0, allow, 1, 100);
314                 if (result != 1 || *allow != 0)
315                         count = 0;
316                 kfree(allow);
317         }
318
319         if (count == 0) {
320                 priv->flags.write_urb_in_use = 0;
321
322                 dbg("%s(): submitting interrupt urb", __func__);
323                 port->interrupt_in_urb->dev = port->serial->dev;
324                 result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
325                 if (result != 0) {
326                         dev_err(&port->dev, "%s(): usb_submit_urb() failed"
327                                 " with error %d\n", __func__, result);
328                 }
329                 return;
330         }
331
332         count = kfifo_out_locked(&priv->write_fifo,
333                                         port->write_urb->transfer_buffer,
334                                         count, &priv->lock);
335         port->write_urb->transfer_buffer_length = count;
336         port->write_urb->dev = port->serial->dev;
337         result = usb_submit_urb(port->write_urb, GFP_NOIO);
338         if (result != 0) {
339                 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
340                                " with error %d\n", __func__, result);
341                 priv->flags.write_urb_in_use = 0;
342         }
343
344         usb_serial_port_softint(port);
345 }
346
347 static int oti6858_startup(struct usb_serial *serial)
348 {
349         struct usb_serial_port *port = serial->port[0];
350         struct oti6858_private *priv;
351         int i;
352
353         for (i = 0; i < serial->num_ports; ++i) {
354                 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
355                 if (!priv)
356                         break;
357                 if (kfifo_alloc(&priv->write_fifo, OTI6858_FIFO_SIZE,
358                                                                 GFP_KERNEL)) {
359                         kfree(priv);
360                         break;
361                 }
362
363                 spin_lock_init(&priv->lock);
364                 init_waitqueue_head(&priv->intr_wait);
365 /*              INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
366 /*              INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
367                 priv->port = port;
368                 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
369                 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
370
371                 usb_set_serial_port_data(serial->port[i], priv);
372         }
373         if (i == serial->num_ports)
374                 return 0;
375
376         for (--i; i >= 0; --i) {
377                 priv = usb_get_serial_port_data(serial->port[i]);
378                 kfifo_free(&priv->write_fifo);
379                 kfree(priv);
380                 usb_set_serial_port_data(serial->port[i], NULL);
381         }
382         return -ENOMEM;
383 }
384
385 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
386                         const unsigned char *buf, int count)
387 {
388         struct oti6858_private *priv = usb_get_serial_port_data(port);
389
390         dbg("%s(port = %d, count = %d)", __func__, port->number, count);
391
392         if (!count)
393                 return count;
394
395         count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
396
397         return count;
398 }
399
400 static int oti6858_write_room(struct tty_struct *tty)
401 {
402         struct usb_serial_port *port = tty->driver_data;
403         struct oti6858_private *priv = usb_get_serial_port_data(port);
404         int room = 0;
405         unsigned long flags;
406
407         dbg("%s(port = %d)", __func__, port->number);
408
409         spin_lock_irqsave(&priv->lock, flags);
410         room = kfifo_avail(&priv->write_fifo);
411         spin_unlock_irqrestore(&priv->lock, flags);
412
413         return room;
414 }
415
416 static int oti6858_chars_in_buffer(struct tty_struct *tty)
417 {
418         struct usb_serial_port *port = tty->driver_data;
419         struct oti6858_private *priv = usb_get_serial_port_data(port);
420         int chars = 0;
421         unsigned long flags;
422
423         dbg("%s(port = %d)", __func__, port->number);
424
425         spin_lock_irqsave(&priv->lock, flags);
426         chars = kfifo_len(&priv->write_fifo);
427         spin_unlock_irqrestore(&priv->lock, flags);
428
429         return chars;
430 }
431
432 static void oti6858_init_termios(struct tty_struct *tty)
433 {
434         *(tty->termios) = tty_std_termios;
435         tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
436         tty->termios->c_ispeed = 38400;
437         tty->termios->c_ospeed = 38400;
438 }
439
440 static void oti6858_set_termios(struct tty_struct *tty,
441                 struct usb_serial_port *port, struct ktermios *old_termios)
442 {
443         struct oti6858_private *priv = usb_get_serial_port_data(port);
444         unsigned long flags;
445         unsigned int cflag;
446         u8 frame_fmt, control;
447         __le16 divisor;
448         int br;
449
450         dbg("%s(port = %d)", __func__, port->number);
451
452         if (!tty) {
453                 dbg("%s(): no tty structures", __func__);
454                 return;
455         }
456
457         cflag = tty->termios->c_cflag;
458
459         spin_lock_irqsave(&priv->lock, flags);
460         divisor = priv->pending_setup.divisor;
461         frame_fmt = priv->pending_setup.frame_fmt;
462         control = priv->pending_setup.control;
463         spin_unlock_irqrestore(&priv->lock, flags);
464
465         frame_fmt &= ~FMT_DATA_BITS_MASK;
466         switch (cflag & CSIZE) {
467         case CS5:
468                 frame_fmt |= FMT_DATA_BITS_5;
469                 break;
470         case CS6:
471                 frame_fmt |= FMT_DATA_BITS_6;
472                 break;
473         case CS7:
474                 frame_fmt |= FMT_DATA_BITS_7;
475                 break;
476         default:
477         case CS8:
478                 frame_fmt |= FMT_DATA_BITS_8;
479                 break;
480         }
481
482         /* manufacturer claims that this device can work with baud rates
483          * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
484          * guarantee that any other baud rate will work (especially
485          * the higher ones)
486          */
487         br = tty_get_baud_rate(tty);
488         if (br == 0) {
489                 divisor = 0;
490         } else {
491                 int real_br;
492                 int new_divisor;
493                 br = min(br, OTI6858_MAX_BAUD_RATE);
494
495                 new_divisor = (96000000 + 8 * br) / (16 * br);
496                 real_br = 96000000 / (16 * new_divisor);
497                 divisor = cpu_to_le16(new_divisor);
498                 tty_encode_baud_rate(tty, real_br, real_br);
499         }
500
501         frame_fmt &= ~FMT_STOP_BITS_MASK;
502         if ((cflag & CSTOPB) != 0)
503                 frame_fmt |= FMT_STOP_BITS_2;
504         else
505                 frame_fmt |= FMT_STOP_BITS_1;
506
507         frame_fmt &= ~FMT_PARITY_MASK;
508         if ((cflag & PARENB) != 0) {
509                 if ((cflag & PARODD) != 0)
510                         frame_fmt |= FMT_PARITY_ODD;
511                 else
512                         frame_fmt |= FMT_PARITY_EVEN;
513         } else {
514                 frame_fmt |= FMT_PARITY_NONE;
515         }
516
517         control &= ~CONTROL_MASK;
518         if ((cflag & CRTSCTS) != 0)
519                 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
520
521         /* change control lines if we are switching to or from B0 */
522         /* FIXME:
523         spin_lock_irqsave(&priv->lock, flags);
524         control = priv->line_control;
525         if ((cflag & CBAUD) == B0)
526                 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
527         else
528                 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
529         if (control != priv->line_control) {
530                 control = priv->line_control;
531                 spin_unlock_irqrestore(&priv->lock, flags);
532                 set_control_lines(serial->dev, control);
533         } else {
534                 spin_unlock_irqrestore(&priv->lock, flags);
535         }
536         */
537
538         spin_lock_irqsave(&priv->lock, flags);
539         if (divisor != priv->pending_setup.divisor
540                         || control != priv->pending_setup.control
541                         || frame_fmt != priv->pending_setup.frame_fmt) {
542                 priv->pending_setup.divisor = divisor;
543                 priv->pending_setup.control = control;
544                 priv->pending_setup.frame_fmt = frame_fmt;
545         }
546         spin_unlock_irqrestore(&priv->lock, flags);
547 }
548
549 static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
550 {
551         struct oti6858_private *priv = usb_get_serial_port_data(port);
552         struct ktermios tmp_termios;
553         struct usb_serial *serial = port->serial;
554         struct oti6858_control_pkt *buf;
555         unsigned long flags;
556         int result;
557
558         dbg("%s(port = %d)", __func__, port->number);
559
560         usb_clear_halt(serial->dev, port->write_urb->pipe);
561         usb_clear_halt(serial->dev, port->read_urb->pipe);
562
563         buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
564         if (buf == NULL) {
565                 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
566                 return -ENOMEM;
567         }
568
569         result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
570                                 OTI6858_REQ_T_GET_STATUS,
571                                 OTI6858_REQ_GET_STATUS,
572                                 0, 0,
573                                 buf, OTI6858_CTRL_PKT_SIZE,
574                                 100);
575         if (result != OTI6858_CTRL_PKT_SIZE) {
576                 /* assume default (after power-on reset) values */
577                 buf->divisor = cpu_to_le16(0x009c);     /* 38400 bps */
578                 buf->frame_fmt = 0x03;  /* 8N1 */
579                 buf->something = 0x43;
580                 buf->control = 0x4c;    /* DTR, RTS */
581                 buf->tx_status = 0x00;
582                 buf->pin_state = 0x5b;  /* RTS, CTS, DSR, DTR, RI, DCD */
583                 buf->rx_bytes_avail = 0x00;
584         }
585
586         spin_lock_irqsave(&priv->lock, flags);
587         memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
588         priv->pending_setup.divisor = buf->divisor;
589         priv->pending_setup.frame_fmt = buf->frame_fmt;
590         priv->pending_setup.control = buf->control;
591         spin_unlock_irqrestore(&priv->lock, flags);
592         kfree(buf);
593
594         dbg("%s(): submitting interrupt urb", __func__);
595         port->interrupt_in_urb->dev = serial->dev;
596         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
597         if (result != 0) {
598                 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
599                                " with error %d\n", __func__, result);
600                 oti6858_close(port);
601                 return -EPROTO;
602         }
603
604         /* setup termios */
605         if (tty)
606                 oti6858_set_termios(tty, port, &tmp_termios);
607         port->port.drain_delay = 256;   /* FIXME: check the FIFO length */
608         return 0;
609 }
610
611 static void oti6858_close(struct usb_serial_port *port)
612 {
613         struct oti6858_private *priv = usb_get_serial_port_data(port);
614         unsigned long flags;
615
616         dbg("%s(port = %d)", __func__, port->number);
617
618         spin_lock_irqsave(&priv->lock, flags);
619         /* clear out any remaining data in the buffer */
620         kfifo_reset_out(&priv->write_fifo);
621         spin_unlock_irqrestore(&priv->lock, flags);
622
623         dbg("%s(): after buf_clear()", __func__);
624
625         /* cancel scheduled setup */
626         cancel_delayed_work(&priv->delayed_setup_work);
627         cancel_delayed_work(&priv->delayed_write_work);
628         flush_scheduled_work();
629
630         /* shutdown our urbs */
631         dbg("%s(): shutting down urbs", __func__);
632         usb_kill_urb(port->write_urb);
633         usb_kill_urb(port->read_urb);
634         usb_kill_urb(port->interrupt_in_urb);
635 }
636
637 static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
638                                 unsigned int set, unsigned int clear)
639 {
640         struct usb_serial_port *port = tty->driver_data;
641         struct oti6858_private *priv = usb_get_serial_port_data(port);
642         unsigned long flags;
643         u8 control;
644
645         dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
646                                 __func__, port->number, set, clear);
647
648         if (!usb_get_intfdata(port->serial->interface))
649                 return -ENODEV;
650
651         /* FIXME: check if this is correct (active high/low) */
652         spin_lock_irqsave(&priv->lock, flags);
653         control = priv->pending_setup.control;
654         if ((set & TIOCM_RTS) != 0)
655                 control |= CONTROL_RTS_HIGH;
656         if ((set & TIOCM_DTR) != 0)
657                 control |= CONTROL_DTR_HIGH;
658         if ((clear & TIOCM_RTS) != 0)
659                 control &= ~CONTROL_RTS_HIGH;
660         if ((clear & TIOCM_DTR) != 0)
661                 control &= ~CONTROL_DTR_HIGH;
662
663         if (control != priv->pending_setup.control)
664                 priv->pending_setup.control = control;
665
666         spin_unlock_irqrestore(&priv->lock, flags);
667         return 0;
668 }
669
670 static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
671 {
672         struct usb_serial_port *port = tty->driver_data;
673         struct oti6858_private *priv = usb_get_serial_port_data(port);
674         unsigned long flags;
675         unsigned pin_state;
676         unsigned result = 0;
677
678         dbg("%s(port = %d)", __func__, port->number);
679
680         if (!usb_get_intfdata(port->serial->interface))
681                 return -ENODEV;
682
683         spin_lock_irqsave(&priv->lock, flags);
684         pin_state = priv->status.pin_state & PIN_MASK;
685         spin_unlock_irqrestore(&priv->lock, flags);
686
687         /* FIXME: check if this is correct (active high/low) */
688         if ((pin_state & PIN_RTS) != 0)
689                 result |= TIOCM_RTS;
690         if ((pin_state & PIN_CTS) != 0)
691                 result |= TIOCM_CTS;
692         if ((pin_state & PIN_DSR) != 0)
693                 result |= TIOCM_DSR;
694         if ((pin_state & PIN_DTR) != 0)
695                 result |= TIOCM_DTR;
696         if ((pin_state & PIN_RI) != 0)
697                 result |= TIOCM_RI;
698         if ((pin_state & PIN_DCD) != 0)
699                 result |= TIOCM_CD;
700
701         dbg("%s() = 0x%08x", __func__, result);
702
703         return result;
704 }
705
706 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
707 {
708         struct oti6858_private *priv = usb_get_serial_port_data(port);
709         unsigned long flags;
710         unsigned int prev, status;
711         unsigned int changed;
712
713         spin_lock_irqsave(&priv->lock, flags);
714         prev = priv->status.pin_state;
715         spin_unlock_irqrestore(&priv->lock, flags);
716
717         while (1) {
718                 wait_event_interruptible(priv->intr_wait,
719                                         priv->status.pin_state != prev);
720                 if (signal_pending(current))
721                         return -ERESTARTSYS;
722
723                 spin_lock_irqsave(&priv->lock, flags);
724                 status = priv->status.pin_state & PIN_MASK;
725                 spin_unlock_irqrestore(&priv->lock, flags);
726
727                 changed = prev ^ status;
728                 /* FIXME: check if this is correct (active high/low) */
729                 if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
730                     ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
731                     ((arg & TIOCM_CD)  && (changed & PIN_DCD)) ||
732                     ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
733                         return 0;
734                 prev = status;
735         }
736
737         /* NOTREACHED */
738         return 0;
739 }
740
741 static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
742                         unsigned int cmd, unsigned long arg)
743 {
744         struct usb_serial_port *port = tty->driver_data;
745
746         dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
747                                 __func__, port->number, cmd, arg);
748
749         switch (cmd) {
750         case TIOCMIWAIT:
751                 dbg("%s(): TIOCMIWAIT", __func__);
752                 return wait_modem_info(port, arg);
753         default:
754                 dbg("%s(): 0x%04x not supported", __func__, cmd);
755                 break;
756         }
757         return -ENOIOCTLCMD;
758 }
759
760
761 static void oti6858_release(struct usb_serial *serial)
762 {
763         struct oti6858_private *priv;
764         int i;
765
766         dbg("%s()", __func__);
767
768         for (i = 0; i < serial->num_ports; ++i) {
769                 priv = usb_get_serial_port_data(serial->port[i]);
770                 if (priv) {
771                         kfifo_free(&priv->write_fifo);
772                         kfree(priv);
773                 }
774         }
775 }
776
777 static void oti6858_read_int_callback(struct urb *urb)
778 {
779         struct usb_serial_port *port =  urb->context;
780         struct oti6858_private *priv = usb_get_serial_port_data(port);
781         int transient = 0, can_recv = 0, resubmit = 1;
782         int status = urb->status;
783
784         dbg("%s(port = %d, status = %d)",
785                                 __func__, port->number, status);
786
787         switch (status) {
788         case 0:
789                 /* success */
790                 break;
791         case -ECONNRESET:
792         case -ENOENT:
793         case -ESHUTDOWN:
794                 /* this urb is terminated, clean up */
795                 dbg("%s(): urb shutting down with status: %d",
796                                         __func__, status);
797                 return;
798         default:
799                 dbg("%s(): nonzero urb status received: %d",
800                                         __func__, status);
801                 break;
802         }
803
804         if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
805                 struct oti6858_control_pkt *xs = urb->transfer_buffer;
806                 unsigned long flags;
807
808                 spin_lock_irqsave(&priv->lock, flags);
809
810                 if (!priv->transient) {
811                         if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
812                                 if (xs->rx_bytes_avail == 0) {
813                                         priv->transient = 4;
814                                         priv->setup_done = 0;
815                                         resubmit = 0;
816                                         dbg("%s(): scheduling setup_line()",
817                                             __func__);
818                                         schedule_delayed_work(&priv->delayed_setup_work, 0);
819                                 }
820                         }
821                 } else {
822                         if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
823                                 priv->transient = 0;
824                         } else if (!priv->setup_done) {
825                                 resubmit = 0;
826                         } else if (--priv->transient == 0) {
827                                 if (xs->rx_bytes_avail == 0) {
828                                         priv->transient = 4;
829                                         priv->setup_done = 0;
830                                         resubmit = 0;
831                                         dbg("%s(): scheduling setup_line()",
832                                             __func__);
833                                         schedule_delayed_work(&priv->delayed_setup_work, 0);
834                                 }
835                         }
836                 }
837
838                 if (!priv->transient) {
839                         if (xs->pin_state != priv->status.pin_state)
840                                 wake_up_interruptible(&priv->intr_wait);
841                         memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
842                 }
843
844                 if (!priv->transient && xs->rx_bytes_avail != 0) {
845                         can_recv = xs->rx_bytes_avail;
846                         priv->flags.read_urb_in_use = 1;
847                 }
848
849                 transient = priv->transient;
850                 spin_unlock_irqrestore(&priv->lock, flags);
851         }
852
853         if (can_recv) {
854                 int result;
855
856                 port->read_urb->dev = port->serial->dev;
857                 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
858                 if (result != 0) {
859                         priv->flags.read_urb_in_use = 0;
860                         dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
861                                         " error %d\n", __func__, result);
862                 } else {
863                         resubmit = 0;
864                 }
865         } else if (!transient) {
866                 unsigned long flags;
867
868                 spin_lock_irqsave(&priv->lock, flags);
869                 if (priv->flags.write_urb_in_use == 0
870                                 && kfifo_len(&priv->write_fifo) != 0) {
871                         schedule_delayed_work(&priv->delayed_write_work, 0);
872                         resubmit = 0;
873                 }
874                 spin_unlock_irqrestore(&priv->lock, flags);
875         }
876
877         if (resubmit) {
878                 int result;
879
880 /*              dbg("%s(): submitting interrupt urb", __func__); */
881                 urb->dev = port->serial->dev;
882                 result = usb_submit_urb(urb, GFP_ATOMIC);
883                 if (result != 0) {
884                         dev_err(&urb->dev->dev,
885                                         "%s(): usb_submit_urb() failed with"
886                                         " error %d\n", __func__, result);
887                 }
888         }
889 }
890
891 static void oti6858_read_bulk_callback(struct urb *urb)
892 {
893         struct usb_serial_port *port =  urb->context;
894         struct oti6858_private *priv = usb_get_serial_port_data(port);
895         struct tty_struct *tty;
896         unsigned char *data = urb->transfer_buffer;
897         unsigned long flags;
898         int status = urb->status;
899         int result;
900
901         dbg("%s(port = %d, status = %d)",
902                                 __func__, port->number, status);
903
904         spin_lock_irqsave(&priv->lock, flags);
905         priv->flags.read_urb_in_use = 0;
906         spin_unlock_irqrestore(&priv->lock, flags);
907
908         if (status != 0) {
909                 /*
910                 if (status == -EPROTO) {
911                         * PL2303 mysteriously fails with -EPROTO reschedule
912                            the read *
913                         dbg("%s - caught -EPROTO, resubmitting the urb",
914                                                                 __func__);
915                         result = usb_submit_urb(urb, GFP_ATOMIC);
916                         if (result)
917                                 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
918                         return;
919                 }
920                 */
921                 dbg("%s(): unable to handle the error, exiting", __func__);
922                 return;
923         }
924
925         tty = tty_port_tty_get(&port->port);
926         if (tty != NULL && urb->actual_length > 0) {
927                 tty_insert_flip_string(tty, data, urb->actual_length);
928                 tty_flip_buffer_push(tty);
929         }
930         tty_kref_put(tty);
931
932         /* schedule the interrupt urb */
933         port->interrupt_in_urb->dev = port->serial->dev;
934         result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
935         if (result != 0 && result != -EPERM) {
936                 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
937                                 " error %d\n", __func__, result);
938         }
939 }
940
941 static void oti6858_write_bulk_callback(struct urb *urb)
942 {
943         struct usb_serial_port *port =  urb->context;
944         struct oti6858_private *priv = usb_get_serial_port_data(port);
945         int status = urb->status;
946         int result;
947
948         dbg("%s(port = %d, status = %d)",
949                                 __func__, port->number, status);
950
951         switch (status) {
952         case 0:
953                 /* success */
954                 break;
955         case -ECONNRESET:
956         case -ENOENT:
957         case -ESHUTDOWN:
958                 /* this urb is terminated, clean up */
959                 dbg("%s(): urb shutting down with status: %d",
960                                         __func__, status);
961                 priv->flags.write_urb_in_use = 0;
962                 return;
963         default:
964                 /* error in the urb, so we have to resubmit it */
965                 dbg("%s(): nonzero write bulk status received: %d",
966                                         __func__, status);
967                 dbg("%s(): overflow in write", __func__);
968
969                 port->write_urb->transfer_buffer_length = 1;
970                 port->write_urb->dev = port->serial->dev;
971                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
972                 if (result) {
973                         dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
974                                         " error %d\n", __func__, result);
975                 } else {
976                         return;
977                 }
978         }
979
980         priv->flags.write_urb_in_use = 0;
981
982         /* schedule the interrupt urb if we are still open */
983         port->interrupt_in_urb->dev = port->serial->dev;
984         dbg("%s(): submitting interrupt urb", __func__);
985         result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
986         if (result != 0) {
987                 dev_err(&port->dev, "%s(): failed submitting int urb,"
988                                         " error %d\n", __func__, result);
989         }
990 }
991
992 /* module description and (de)initialization */
993
994 static int __init oti6858_init(void)
995 {
996         int retval;
997
998         retval = usb_serial_register(&oti6858_device);
999         if (retval == 0) {
1000                 retval = usb_register(&oti6858_driver);
1001                 if (retval)
1002                         usb_serial_deregister(&oti6858_device);
1003         }
1004         return retval;
1005 }
1006
1007 static void __exit oti6858_exit(void)
1008 {
1009         usb_deregister(&oti6858_driver);
1010         usb_serial_deregister(&oti6858_device);
1011 }
1012
1013 module_init(oti6858_init);
1014 module_exit(oti6858_exit);
1015
1016 MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1017 MODULE_AUTHOR(OTI6858_AUTHOR);
1018 MODULE_VERSION(OTI6858_VERSION);
1019 MODULE_LICENSE("GPL");
1020
1021 module_param(debug, bool, S_IRUGO | S_IWUSR);
1022 MODULE_PARM_DESC(debug, "enable debug output");
1023