Lines Matching +full:1 +full:br +full:- +full:100
1 // SPDX-License-Identifier: GPL-2.0
3 * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
6 * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
7 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
11 * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
17 * - analyzing traffic between device and the M$ Windows 2000 driver,
18 * - trying different bit combinations and checking pin states
20 * - receiving malformed frames and producing buffer overflows
22 * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
25 * See Documentation/usb/usb-serial.rst for more information on using this
29 * - implement correct flushing for ioctls and oti6858_close()
30 * - check how errors (rx overflow, parity error, framing error) are reported
31 * - implement oti6858_break_ctl()
32 * - implement more ioctls
33 * - test/implement flow control
34 * - allow setting custom baud rates
54 "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
114 (((a)->divisor == (priv)->pending_setup.divisor) \
115 && ((a)->control == (priv)->pending_setup.control) \
116 && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
144 .num_ports = 1,
145 .num_bulk_in = 1,
146 .num_bulk_out = 1,
147 .num_interrupt_in = 1,
196 struct usb_serial_port *port = priv->port; in setup_line()
204 schedule_delayed_work(&priv->delayed_setup_work, in setup_line()
209 result = usb_control_msg(port->serial->dev, in setup_line()
210 usb_rcvctrlpipe(port->serial->dev, 0), in setup_line()
215 100); in setup_line()
218 dev_err(&port->dev, "%s(): error reading status\n", __func__); in setup_line()
221 schedule_delayed_work(&priv->delayed_setup_work, in setup_line()
226 spin_lock_irqsave(&priv->lock, flags); in setup_line()
228 new_setup->divisor = priv->pending_setup.divisor; in setup_line()
229 new_setup->control = priv->pending_setup.control; in setup_line()
230 new_setup->frame_fmt = priv->pending_setup.frame_fmt; in setup_line()
232 spin_unlock_irqrestore(&priv->lock, flags); in setup_line()
233 result = usb_control_msg(port->serial->dev, in setup_line()
234 usb_sndctrlpipe(port->serial->dev, 0), in setup_line()
239 100); in setup_line()
241 spin_unlock_irqrestore(&priv->lock, flags); in setup_line()
246 spin_lock_irqsave(&priv->lock, flags); in setup_line()
248 priv->transient = 0; in setup_line()
249 priv->setup_done = 1; in setup_line()
250 spin_unlock_irqrestore(&priv->lock, flags); in setup_line()
252 dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__); in setup_line()
253 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); in setup_line()
255 dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n", in setup_line()
264 struct usb_serial_port *port = priv->port; in send_data()
269 spin_lock_irqsave(&priv->lock, flags); in send_data()
270 if (priv->flags.write_urb_in_use) { in send_data()
271 spin_unlock_irqrestore(&priv->lock, flags); in send_data()
272 schedule_delayed_work(&priv->delayed_write_work, in send_data()
276 priv->flags.write_urb_in_use = 1; in send_data()
277 spin_unlock_irqrestore(&priv->lock, flags); in send_data()
279 spin_lock_irqsave(&port->lock, flags); in send_data()
280 count = kfifo_len(&port->write_fifo); in send_data()
281 spin_unlock_irqrestore(&port->lock, flags); in send_data()
283 if (count > port->bulk_out_size) in send_data()
284 count = port->bulk_out_size; in send_data()
287 allow = kmalloc(1, GFP_KERNEL); in send_data()
291 result = usb_control_msg(port->serial->dev, in send_data()
292 usb_rcvctrlpipe(port->serial->dev, 0), in send_data()
295 count, 0, allow, 1, 100); in send_data()
296 if (result != 1 || *allow != 0) in send_data()
302 priv->flags.write_urb_in_use = 0; in send_data()
304 dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__); in send_data()
305 result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); in send_data()
307 dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n", in send_data()
313 count = kfifo_out_locked(&port->write_fifo, in send_data()
314 port->write_urb->transfer_buffer, in send_data()
315 count, &port->lock); in send_data()
316 port->write_urb->transfer_buffer_length = count; in send_data()
317 result = usb_submit_urb(port->write_urb, GFP_NOIO); in send_data()
321 priv->flags.write_urb_in_use = 0; in send_data()
333 return -ENOMEM; in oti6858_port_probe()
335 spin_lock_init(&priv->lock); in oti6858_port_probe()
336 priv->port = port; in oti6858_port_probe()
337 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line); in oti6858_port_probe()
338 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data); in oti6858_port_probe()
342 port->port.drain_delay = 256; /* FIXME: check the FIFO length */ in oti6858_port_probe()
361 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock); in oti6858_write()
368 struct usb_serial_port *port = tty->driver_data; in oti6858_write_room()
372 spin_lock_irqsave(&port->lock, flags); in oti6858_write_room()
373 room = kfifo_avail(&port->write_fifo); in oti6858_write_room()
374 spin_unlock_irqrestore(&port->lock, flags); in oti6858_write_room()
381 struct usb_serial_port *port = tty->driver_data; in oti6858_chars_in_buffer()
385 spin_lock_irqsave(&port->lock, flags); in oti6858_chars_in_buffer()
386 chars = kfifo_len(&port->write_fifo); in oti6858_chars_in_buffer()
387 spin_unlock_irqrestore(&port->lock, flags); in oti6858_chars_in_buffer()
406 int br; in oti6858_set_termios() local
408 cflag = tty->termios.c_cflag; in oti6858_set_termios()
410 spin_lock_irqsave(&priv->lock, flags); in oti6858_set_termios()
411 frame_fmt = priv->pending_setup.frame_fmt; in oti6858_set_termios()
412 control = priv->pending_setup.control; in oti6858_set_termios()
413 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_set_termios()
437 br = tty_get_baud_rate(tty); in oti6858_set_termios()
438 if (br == 0) { in oti6858_set_termios()
443 br = min(br, OTI6858_MAX_BAUD_RATE); in oti6858_set_termios()
445 new_divisor = (96000000 + 8 * br) / (16 * br); in oti6858_set_termios()
473 spin_lock_irqsave(&priv->lock, flags); in oti6858_set_termios()
474 control = priv->line_control; in oti6858_set_termios()
476 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); in oti6858_set_termios()
478 priv->line_control |= (CONTROL_DTR | CONTROL_RTS); in oti6858_set_termios()
479 if (control != priv->line_control) { in oti6858_set_termios()
480 control = priv->line_control; in oti6858_set_termios()
481 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_set_termios()
482 set_control_lines(serial->dev, control); in oti6858_set_termios()
484 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_set_termios()
488 spin_lock_irqsave(&priv->lock, flags); in oti6858_set_termios()
489 if (divisor != priv->pending_setup.divisor in oti6858_set_termios()
490 || control != priv->pending_setup.control in oti6858_set_termios()
491 || frame_fmt != priv->pending_setup.frame_fmt) { in oti6858_set_termios()
492 priv->pending_setup.divisor = divisor; in oti6858_set_termios()
493 priv->pending_setup.control = control; in oti6858_set_termios()
494 priv->pending_setup.frame_fmt = frame_fmt; in oti6858_set_termios()
496 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_set_termios()
502 struct usb_serial *serial = port->serial; in oti6858_open()
507 usb_clear_halt(serial->dev, port->write_urb->pipe); in oti6858_open()
508 usb_clear_halt(serial->dev, port->read_urb->pipe); in oti6858_open()
512 return -ENOMEM; in oti6858_open()
514 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), in oti6858_open()
519 100); in oti6858_open()
521 /* assume default (after power-on reset) values */ in oti6858_open()
522 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */ in oti6858_open()
523 buf->frame_fmt = 0x03; /* 8N1 */ in oti6858_open()
524 buf->something = 0x43; in oti6858_open()
525 buf->control = 0x4c; /* DTR, RTS */ in oti6858_open()
526 buf->tx_status = 0x00; in oti6858_open()
527 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */ in oti6858_open()
528 buf->rx_bytes_avail = 0x00; in oti6858_open()
531 spin_lock_irqsave(&priv->lock, flags); in oti6858_open()
532 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE); in oti6858_open()
533 priv->pending_setup.divisor = buf->divisor; in oti6858_open()
534 priv->pending_setup.frame_fmt = buf->frame_fmt; in oti6858_open()
535 priv->pending_setup.control = buf->control; in oti6858_open()
536 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_open()
539 dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__); in oti6858_open()
540 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); in oti6858_open()
542 dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n", in oti6858_open()
560 spin_lock_irqsave(&port->lock, flags); in oti6858_close()
562 kfifo_reset_out(&port->write_fifo); in oti6858_close()
563 spin_unlock_irqrestore(&port->lock, flags); in oti6858_close()
565 dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__); in oti6858_close()
568 cancel_delayed_work_sync(&priv->delayed_setup_work); in oti6858_close()
569 cancel_delayed_work_sync(&priv->delayed_write_work); in oti6858_close()
572 dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__); in oti6858_close()
573 usb_kill_urb(port->write_urb); in oti6858_close()
574 usb_kill_urb(port->read_urb); in oti6858_close()
575 usb_kill_urb(port->interrupt_in_urb); in oti6858_close()
581 struct usb_serial_port *port = tty->driver_data; in oti6858_tiocmset()
586 dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n", in oti6858_tiocmset()
590 spin_lock_irqsave(&priv->lock, flags); in oti6858_tiocmset()
591 control = priv->pending_setup.control; in oti6858_tiocmset()
601 if (control != priv->pending_setup.control) in oti6858_tiocmset()
602 priv->pending_setup.control = control; in oti6858_tiocmset()
604 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_tiocmset()
610 struct usb_serial_port *port = tty->driver_data; in oti6858_tiocmget()
616 spin_lock_irqsave(&priv->lock, flags); in oti6858_tiocmget()
617 pin_state = priv->status.pin_state & PIN_MASK; in oti6858_tiocmget()
618 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_tiocmget()
634 dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result); in oti6858_tiocmget()
641 struct usb_serial_port *port = urb->context; in oti6858_read_int_callback()
643 int transient = 0, can_recv = 0, resubmit = 1; in oti6858_read_int_callback()
644 int status = urb->status; in oti6858_read_int_callback()
650 case -ECONNRESET: in oti6858_read_int_callback()
651 case -ENOENT: in oti6858_read_int_callback()
652 case -ESHUTDOWN: in oti6858_read_int_callback()
654 dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", in oti6858_read_int_callback()
658 dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n", in oti6858_read_int_callback()
663 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) { in oti6858_read_int_callback()
664 struct oti6858_control_pkt *xs = urb->transfer_buffer; in oti6858_read_int_callback()
667 spin_lock_irqsave(&priv->lock, flags); in oti6858_read_int_callback()
669 if (!priv->transient) { in oti6858_read_int_callback()
671 if (xs->rx_bytes_avail == 0) { in oti6858_read_int_callback()
672 priv->transient = 4; in oti6858_read_int_callback()
673 priv->setup_done = 0; in oti6858_read_int_callback()
675 dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__); in oti6858_read_int_callback()
676 schedule_delayed_work(&priv->delayed_setup_work, 0); in oti6858_read_int_callback()
681 priv->transient = 0; in oti6858_read_int_callback()
682 } else if (!priv->setup_done) { in oti6858_read_int_callback()
684 } else if (--priv->transient == 0) { in oti6858_read_int_callback()
685 if (xs->rx_bytes_avail == 0) { in oti6858_read_int_callback()
686 priv->transient = 4; in oti6858_read_int_callback()
687 priv->setup_done = 0; in oti6858_read_int_callback()
689 dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__); in oti6858_read_int_callback()
690 schedule_delayed_work(&priv->delayed_setup_work, 0); in oti6858_read_int_callback()
695 if (!priv->transient) { in oti6858_read_int_callback()
696 u8 delta = xs->pin_state ^ priv->status.pin_state; in oti6858_read_int_callback()
700 port->icount.cts++; in oti6858_read_int_callback()
702 port->icount.dsr++; in oti6858_read_int_callback()
704 port->icount.rng++; in oti6858_read_int_callback()
706 port->icount.dcd++; in oti6858_read_int_callback()
708 wake_up_interruptible(&port->port.delta_msr_wait); in oti6858_read_int_callback()
711 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE); in oti6858_read_int_callback()
714 if (!priv->transient && xs->rx_bytes_avail != 0) { in oti6858_read_int_callback()
715 can_recv = xs->rx_bytes_avail; in oti6858_read_int_callback()
716 priv->flags.read_urb_in_use = 1; in oti6858_read_int_callback()
719 transient = priv->transient; in oti6858_read_int_callback()
720 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_read_int_callback()
726 result = usb_submit_urb(port->read_urb, GFP_ATOMIC); in oti6858_read_int_callback()
728 priv->flags.read_urb_in_use = 0; in oti6858_read_int_callback()
729 dev_err(&port->dev, "%s(): usb_submit_urb() failed," in oti6858_read_int_callback()
738 spin_lock_irqsave(&port->lock, flags); in oti6858_read_int_callback()
739 count = kfifo_len(&port->write_fifo); in oti6858_read_int_callback()
740 spin_unlock_irqrestore(&port->lock, flags); in oti6858_read_int_callback()
742 spin_lock_irqsave(&priv->lock, flags); in oti6858_read_int_callback()
743 if (priv->flags.write_urb_in_use == 0 && count != 0) { in oti6858_read_int_callback()
744 schedule_delayed_work(&priv->delayed_write_work, 0); in oti6858_read_int_callback()
747 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_read_int_callback()
753 /* dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */ in oti6858_read_int_callback()
756 dev_err(&urb->dev->dev, in oti6858_read_int_callback()
765 struct usb_serial_port *port = urb->context; in oti6858_read_bulk_callback()
767 unsigned char *data = urb->transfer_buffer; in oti6858_read_bulk_callback()
769 int status = urb->status; in oti6858_read_bulk_callback()
772 spin_lock_irqsave(&priv->lock, flags); in oti6858_read_bulk_callback()
773 priv->flags.read_urb_in_use = 0; in oti6858_read_bulk_callback()
774 spin_unlock_irqrestore(&priv->lock, flags); in oti6858_read_bulk_callback()
777 dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__); in oti6858_read_bulk_callback()
781 if (urb->actual_length > 0) { in oti6858_read_bulk_callback()
782 tty_insert_flip_string(&port->port, data, urb->actual_length); in oti6858_read_bulk_callback()
783 tty_flip_buffer_push(&port->port); in oti6858_read_bulk_callback()
787 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); in oti6858_read_bulk_callback()
788 if (result != 0 && result != -EPERM) { in oti6858_read_bulk_callback()
789 dev_err(&port->dev, "%s(): usb_submit_urb() failed," in oti6858_read_bulk_callback()
796 struct usb_serial_port *port = urb->context; in oti6858_write_bulk_callback()
798 int status = urb->status; in oti6858_write_bulk_callback()
805 case -ECONNRESET: in oti6858_write_bulk_callback()
806 case -ENOENT: in oti6858_write_bulk_callback()
807 case -ESHUTDOWN: in oti6858_write_bulk_callback()
809 dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status); in oti6858_write_bulk_callback()
810 priv->flags.write_urb_in_use = 0; in oti6858_write_bulk_callback()
814 dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status); in oti6858_write_bulk_callback()
815 dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__); in oti6858_write_bulk_callback()
817 port->write_urb->transfer_buffer_length = 1; in oti6858_write_bulk_callback()
818 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); in oti6858_write_bulk_callback()
827 priv->flags.write_urb_in_use = 0; in oti6858_write_bulk_callback()
830 dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__); in oti6858_write_bulk_callback()
831 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); in oti6858_write_bulk_callback()
833 dev_err(&port->dev, "%s(): failed submitting int urb," in oti6858_write_bulk_callback()