40 #include <linux/kernel.h>
41 #include <linux/errno.h>
43 #include <linux/slab.h>
44 #include <linux/tty.h>
47 #include <linux/serial.h>
48 #include <linux/module.h>
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"
70 #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
71 #define OTI6858_REQ_T_GET_STATUS 0x01
73 #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
74 #define OTI6858_REQ_T_SET_LINE 0x00
76 #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
77 #define OTI6858_REQ_T_CHECK_TXBUFF 0x00
82 #define OTI6858_MAX_BAUD_RATE 3000000
84 #define FMT_STOP_BITS_MASK 0xc0
85 #define FMT_STOP_BITS_1 0x00
86 #define FMT_STOP_BITS_2 0x40
87 #define FMT_PARITY_MASK 0x38
88 #define FMT_PARITY_NONE 0x00
89 #define FMT_PARITY_ODD 0x08
90 #define FMT_PARITY_EVEN 0x18
91 #define FMT_PARITY_MARK 0x28
92 #define FMT_PARITY_SPACE 0x38
93 #define FMT_DATA_BITS_MASK 0x03
94 #define FMT_DATA_BITS_5 0x00
95 #define FMT_DATA_BITS_6 0x01
96 #define FMT_DATA_BITS_7 0x02
97 #define FMT_DATA_BITS_8 0x03
100 #define CONTROL_MASK 0x0c
101 #define CONTROL_DTR_HIGH 0x08
102 #define CONTROL_RTS_HIGH 0x04
104 #define TX_BUFFER_EMPTIED 0x09
106 #define PIN_MASK 0x3f
116 #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
117 #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
118 (((a)->divisor == (priv)->pending_setup.divisor) \
119 && ((a)->control == (priv)->pending_setup.control) \
120 && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
125 static void oti6858_set_termios(
struct tty_struct *tty,
127 static void oti6858_init_termios(
struct tty_struct *tty);
128 static int oti6858_ioctl(
struct tty_struct *tty,
129 unsigned int cmd,
unsigned long arg);
130 static void oti6858_read_int_callback(
struct urb *
urb);
131 static void oti6858_read_bulk_callback(
struct urb *
urb);
132 static void oti6858_write_bulk_callback(
struct urb *
urb);
134 const unsigned char *
buf,
int count);
135 static int oti6858_write_room(
struct tty_struct *tty);
136 static int oti6858_chars_in_buffer(
struct tty_struct *tty);
137 static int oti6858_tiocmget(
struct tty_struct *tty);
138 static int oti6858_tiocmset(
struct tty_struct *tty,
139 unsigned int set,
unsigned int clear);
151 .open = oti6858_open,
152 .close = oti6858_close,
153 .write = oti6858_write,
154 .ioctl = oti6858_ioctl,
155 .set_termios = oti6858_set_termios,
156 .init_termios = oti6858_init_termios,
157 .tiocmget = oti6858_tiocmget,
158 .tiocmset = oti6858_tiocmset,
159 .read_bulk_callback = oti6858_read_bulk_callback,
160 .read_int_callback = oti6858_read_int_callback,
161 .write_bulk_callback = oti6858_write_bulk_callback,
162 .write_room = oti6858_write_room,
163 .chars_in_buffer = oti6858_chars_in_buffer,
164 .port_probe = oti6858_port_probe,
165 .port_remove = oti6858_port_remove,
169 &oti6858_device,
NULL
206 if (new_setup ==
NULL) {
207 dev_err(&port->
dev,
"%s(): out of memory!\n", __func__);
215 usb_rcvctrlpipe(port->
serial->dev, 0),
223 dev_err(&port->
dev,
"%s(): error reading status\n", __func__);
237 spin_unlock_irqrestore(&priv->
lock, flags);
239 usb_sndctrlpipe(port->
serial->dev, 0),
246 spin_unlock_irqrestore(&priv->
lock, flags);
255 spin_unlock_irqrestore(&priv->
lock, flags);
257 dev_dbg(&port->
dev,
"%s(): submitting interrupt urb\n", __func__);
260 dev_err(&port->
dev,
"%s(): usb_submit_urb() failed with error %d\n",
275 if (priv->
flags.write_urb_in_use) {
276 spin_unlock_irqrestore(&priv->
lock, flags);
281 priv->
flags.write_urb_in_use = 1;
282 spin_unlock_irqrestore(&priv->
lock, flags);
286 spin_unlock_irqrestore(&port->
lock, flags);
299 usb_rcvctrlpipe(port->
serial->dev, 0),
302 count, 0, allow, 1, 100);
303 if (result != 1 || *allow != 0)
309 priv->
flags.write_urb_in_use = 0;
311 dev_dbg(&port->
dev,
"%s(): submitting interrupt urb\n", __func__);
314 dev_err(&port->
dev,
"%s(): usb_submit_urb() failed with error %d\n",
328 priv->
flags.write_urb_in_use = 0;
348 usb_set_serial_port_data(port, priv);
357 priv = usb_get_serial_port_data(port);
364 const unsigned char *
buf,
int count)
374 static int oti6858_write_room(
struct tty_struct *tty)
382 spin_unlock_irqrestore(&port->
lock, flags);
387 static int oti6858_chars_in_buffer(
struct tty_struct *tty)
395 spin_unlock_irqrestore(&port->
lock, flags);
400 static void oti6858_init_termios(
struct tty_struct *tty)
404 tty->termios.c_ispeed = 38400;
405 tty->termios.c_ospeed = 38400;
408 static void oti6858_set_termios(
struct tty_struct *tty,
421 cflag = tty->termios.c_cflag;
427 spin_unlock_irqrestore(&priv->
lock, flags);
430 switch (cflag &
CSIZE) {
459 new_divisor = (96000000 + 8 * br) / (16 * br);
460 real_br = 96000000 / (16 * new_divisor);
466 if ((cflag &
CSTOPB) != 0)
472 if ((cflag &
PARENB) != 0) {
473 if ((cflag &
PARODD) != 0)
510 spin_unlock_irqrestore(&priv->
lock, flags);
527 dev_err(&port->
dev,
"%s(): out of memory!\n", __func__);
553 spin_unlock_irqrestore(&priv->
lock, flags);
556 dev_dbg(&port->
dev,
"%s(): submitting interrupt urb\n", __func__);
559 dev_err(&port->
dev,
"%s(): usb_submit_urb() failed with error %d\n",
567 oti6858_set_termios(tty, port, &tmp_termios);
568 port->
port.drain_delay = 256;
580 spin_unlock_irqrestore(&port->
lock, flags);
582 dev_dbg(&port->
dev,
"%s(): after buf_clear()\n", __func__);
589 dev_dbg(&port->
dev,
"%s(): shutting down urbs\n", __func__);
595 static int oti6858_tiocmset(
struct tty_struct *tty,
596 unsigned int set,
unsigned int clear)
603 dev_dbg(&port->
dev,
"%s(set = 0x%08x, clear = 0x%08x)\n",
604 __func__,
set, clear);
621 spin_unlock_irqrestore(&priv->
lock, flags);
625 static int oti6858_tiocmget(
struct tty_struct *tty)
635 spin_unlock_irqrestore(&priv->
lock, flags);
638 if ((pin_state &
PIN_RTS) != 0)
640 if ((pin_state &
PIN_CTS) != 0)
642 if ((pin_state &
PIN_DSR) != 0)
644 if ((pin_state &
PIN_DTR) != 0)
646 if ((pin_state &
PIN_RI) != 0)
648 if ((pin_state &
PIN_DCD) != 0)
651 dev_dbg(&port->
dev,
"%s() = 0x%08x\n", __func__, result);
664 prev = priv->
status.pin_state;
665 spin_unlock_irqrestore(&priv->
lock, flags);
669 priv->
status.pin_state != prev);
675 spin_unlock_irqrestore(&priv->
lock, flags);
679 if (((arg &
TIOCM_RNG) && (changed & PIN_RI)) ||
680 ((arg &
TIOCM_DSR) && (changed & PIN_DSR)) ||
682 ((arg &
TIOCM_CTS) && (changed & PIN_CTS)))
691 static int oti6858_ioctl(
struct tty_struct *tty,
692 unsigned int cmd,
unsigned long arg)
696 dev_dbg(&port->
dev,
"%s(cmd = 0x%04x, arg = 0x%08lx)\n", __func__, cmd, arg);
700 dev_dbg(&port->
dev,
"%s(): TIOCMIWAIT\n", __func__);
701 return wait_modem_info(port, arg);
703 dev_dbg(&port->
dev,
"%s(): 0x%04x not supported\n", __func__, cmd);
709 static void oti6858_read_int_callback(
struct urb *
urb)
713 int transient = 0, can_recv = 0, resubmit = 1;
714 int status = urb->status;
724 dev_dbg(&urb->dev->dev,
"%s(): urb shutting down with status: %d\n",
728 dev_dbg(&urb->dev->dev,
"%s(): nonzero urb status received: %d\n",
745 dev_dbg(&port->
dev,
"%s(): scheduling setup_line()\n", __func__);
759 dev_dbg(&port->
dev,
"%s(): scheduling setup_line()\n", __func__);
773 priv->
flags.read_urb_in_use = 1;
777 spin_unlock_irqrestore(&priv->
lock, flags);
785 priv->
flags.read_urb_in_use = 0;
786 dev_err(&port->
dev,
"%s(): usb_submit_urb() failed,"
787 " error %d\n", __func__, result);
791 }
else if (!
transient) {
797 spin_unlock_irqrestore(&port->
lock, flags);
800 if (priv->
flags.write_urb_in_use == 0 && count != 0) {
804 spin_unlock_irqrestore(&priv->
lock, flags);
814 "%s(): usb_submit_urb() failed with"
815 " error %d\n", __func__, result);
820 static void oti6858_read_bulk_callback(
struct urb *urb)
825 unsigned char *
data = urb->transfer_buffer;
827 int status = urb->status;
831 priv->
flags.read_urb_in_use = 0;
832 spin_unlock_irqrestore(&priv->
lock, flags);
835 dev_dbg(&urb->dev->dev,
"%s(): unable to handle the error, exiting\n", __func__);
840 if (tty !=
NULL && urb->actual_length > 0) {
841 tty_insert_flip_string(tty, data, urb->actual_length);
848 if (result != 0 && result != -
EPERM) {
849 dev_err(&port->
dev,
"%s(): usb_submit_urb() failed,"
850 " error %d\n", __func__, result);
854 static void oti6858_write_bulk_callback(
struct urb *urb)
858 int status = urb->status;
869 dev_dbg(&urb->dev->dev,
"%s(): urb shutting down with status: %d\n", __func__, status);
870 priv->
flags.write_urb_in_use = 0;
874 dev_dbg(&urb->dev->dev,
"%s(): nonzero write bulk status received: %d\n", __func__, status);
875 dev_dbg(&urb->dev->dev,
"%s(): overflow in write\n", __func__);
877 port->
write_urb->transfer_buffer_length = 1;
881 " error %d\n", __func__, result);
887 priv->
flags.write_urb_in_use = 0;
890 dev_dbg(&port->
dev,
"%s(): submitting interrupt urb\n", __func__);
893 dev_err(&port->
dev,
"%s(): failed submitting int urb,"
894 " error %d\n", __func__, result);