📄 whiteheat.c
字号:
/* Firmware likely not running */ err("%s: Unable to retrieve firmware version, try replugging\n", serial->type->description); err("%s: If the firmware is not running (status led not blinking)\n", serial->type->description); err("%s: please contact support@connecttech.com\n", serial->type->description); return -ENODEV;no_command_private: for (i = serial->num_ports - 1; i >= 0; i--) { port = serial->port[i]; info = usb_get_serial_port_data(port); for (j = urb_pool_size - 1; j >= 0; j--) { tmp = list_first(&info->tx_urbs_free); list_del(tmp); wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); urb = wrap->urb; kfree(wrap);no_tx_wrap: kfree(urb->transfer_buffer);no_tx_buf: usb_free_urb(urb);no_tx_urb: tmp = list_first(&info->rx_urbs_free); list_del(tmp); wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); urb = wrap->urb; kfree(wrap);no_rx_wrap: kfree(urb->transfer_buffer);no_rx_buf: usb_free_urb(urb);no_rx_urb: ; } kfree(info);no_private: ; } kfree(result);no_result_buffer: kfree(command);no_command_buffer: return -ENOMEM;}static void whiteheat_shutdown (struct usb_serial *serial){ struct usb_serial_port *command_port; struct usb_serial_port *port; struct whiteheat_private *info; struct whiteheat_urb_wrap *wrap; struct urb *urb; struct list_head *tmp; struct list_head *tmp2; int i; dbg("%s", __FUNCTION__); /* free up our private data for our command port */ command_port = serial->port[COMMAND_PORT]; kfree (usb_get_serial_port_data(command_port)); for (i = 0; i < serial->num_ports; i++) { port = serial->port[i]; info = usb_get_serial_port_data(port); list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) { list_del(tmp); wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); urb = wrap->urb; kfree(wrap); kfree(urb->transfer_buffer); usb_free_urb(urb); } list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) { list_del(tmp); wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); urb = wrap->urb; kfree(wrap); kfree(urb->transfer_buffer); usb_free_urb(urb); } kfree(info); } return;}static int whiteheat_open (struct usb_serial_port *port, struct file *filp){ int retval = 0; struct termios old_term; dbg("%s - port %d", __FUNCTION__, port->number); retval = start_command_port(port->serial); if (retval) goto exit; if (port->tty) port->tty->low_latency = 1; /* send an open port command */ retval = firm_open(port); if (retval) { stop_command_port(port->serial); goto exit; } retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX); if (retval) { firm_close(port); stop_command_port(port->serial); goto exit; } old_term.c_cflag = ~port->tty->termios->c_cflag; old_term.c_iflag = ~port->tty->termios->c_iflag; whiteheat_set_termios(port, &old_term); /* Work around HCD bugs */ usb_clear_halt(port->serial->dev, port->read_urb->pipe); usb_clear_halt(port->serial->dev, port->write_urb->pipe); /* Start reading from the device */ retval = start_port_read(port); if (retval) { err("%s - failed submitting read urb, error %d", __FUNCTION__, retval); firm_close(port); stop_command_port(port->serial); goto exit; }exit: dbg("%s - exit, retval = %d", __FUNCTION__, retval); return retval;}static void whiteheat_close(struct usb_serial_port *port, struct file * filp){ struct whiteheat_private *info = usb_get_serial_port_data(port); struct whiteheat_urb_wrap *wrap; struct urb *urb; struct list_head *tmp; struct list_head *tmp2; unsigned long flags; dbg("%s - port %d", __FUNCTION__, port->number); /* filp is NULL when called from usb_serial_disconnect */ if (filp && (tty_hung_up_p(filp))) { return; } port->tty->closing = 1;/* * Not currently in use; tty_wait_until_sent() calls * serial_chars_in_buffer() which deadlocks on the second semaphore * acquisition. This should be fixed at some point. Greg's been * notified. if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) { tty_wait_until_sent(port->tty, CLOSING_DELAY); }*/ if (port->tty->driver->flush_buffer) port->tty->driver->flush_buffer(port->tty); tty_ldisc_flush(port->tty); firm_report_tx_done(port); firm_close(port); /* shutdown our bulk reads and writes */ spin_lock_irqsave(&info->lock, flags); list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) { wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); urb = wrap->urb; usb_kill_urb(urb); list_del(tmp); list_add(tmp, &info->rx_urbs_free); } list_for_each_safe(tmp, tmp2, &info->rx_urb_q) { list_del(tmp); list_add(tmp, &info->rx_urbs_free); } list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) { wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); urb = wrap->urb; usb_kill_urb(urb); list_del(tmp); list_add(tmp, &info->tx_urbs_free); } spin_unlock_irqrestore(&info->lock, flags); stop_command_port(port->serial); port->tty->closing = 0;}static int whiteheat_write(struct usb_serial_port *port, const unsigned char *buf, int count){ struct usb_serial *serial = port->serial; struct whiteheat_private *info = usb_get_serial_port_data(port); struct whiteheat_urb_wrap *wrap; struct urb *urb; int result; int bytes; int sent = 0; unsigned long flags; struct list_head *tmp; dbg("%s - port %d", __FUNCTION__, port->number); if (count == 0) { dbg("%s - write request of 0 bytes", __FUNCTION__); return (0); } while (count) { spin_lock_irqsave(&info->lock, flags); if (list_empty(&info->tx_urbs_free)) { spin_unlock_irqrestore(&info->lock, flags); break; } tmp = list_first(&info->tx_urbs_free); list_del(tmp); spin_unlock_irqrestore(&info->lock, flags); wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); urb = wrap->urb; bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count; memcpy (urb->transfer_buffer, buf + sent, bytes); usb_serial_debug_data(debug, &port->dev, __FUNCTION__, bytes, urb->transfer_buffer); urb->dev = serial->dev; urb->transfer_buffer_length = bytes; result = usb_submit_urb(urb, GFP_ATOMIC); if (result) { err("%s - failed submitting write urb, error %d", __FUNCTION__, result); sent = result; spin_lock_irqsave(&info->lock, flags); list_add(tmp, &info->tx_urbs_free); spin_unlock_irqrestore(&info->lock, flags); break; } else { sent += bytes; count -= bytes; spin_lock_irqsave(&info->lock, flags); list_add(tmp, &info->tx_urbs_submitted); spin_unlock_irqrestore(&info->lock, flags); } } return sent;}static int whiteheat_write_room(struct usb_serial_port *port){ struct whiteheat_private *info = usb_get_serial_port_data(port); struct list_head *tmp; int room = 0; unsigned long flags; dbg("%s - port %d", __FUNCTION__, port->number); spin_lock_irqsave(&info->lock, flags); list_for_each(tmp, &info->tx_urbs_free) room++; spin_unlock_irqrestore(&info->lock, flags); room *= port->bulk_out_size; dbg("%s - returns %d", __FUNCTION__, room); return (room);}static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file){ struct whiteheat_private *info = usb_get_serial_port_data(port); unsigned int modem_signals = 0; dbg("%s - port %d", __FUNCTION__, port->number); firm_get_dtr_rts(port); if (info->mcr & UART_MCR_DTR) modem_signals |= TIOCM_DTR; if (info->mcr & UART_MCR_RTS) modem_signals |= TIOCM_RTS; return modem_signals;}static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear){ struct whiteheat_private *info = usb_get_serial_port_data(port); dbg("%s - port %d", __FUNCTION__, port->number); if (set & TIOCM_RTS) info->mcr |= UART_MCR_RTS; if (set & TIOCM_DTR) info->mcr |= UART_MCR_DTR; if (clear & TIOCM_RTS) info->mcr &= ~UART_MCR_RTS; if (clear & TIOCM_DTR) info->mcr &= ~UART_MCR_DTR; firm_set_dtr(port, info->mcr & UART_MCR_DTR); firm_set_rts(port, info->mcr & UART_MCR_RTS); return 0;}static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg){ struct serial_struct serstruct; void __user *user_arg = (void __user *)arg; dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd); switch (cmd) { case TIOCGSERIAL: memset(&serstruct, 0, sizeof(serstruct)); serstruct.type = PORT_16654; serstruct.line = port->serial->minor; serstruct.port = port->number; serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; serstruct.xmit_fifo_size = port->bulk_out_size; serstruct.custom_divisor = 0; serstruct.baud_base = 460800; serstruct.close_delay = CLOSING_DELAY; serstruct.closing_wait = CLOSING_DELAY; if (copy_to_user(user_arg, &serstruct, sizeof(serstruct))) return -EFAULT; break; case TIOCSSERIAL: if (copy_from_user(&serstruct, user_arg, sizeof(serstruct))) return -EFAULT; /* * For now this is ignored. dip sets the ASYNC_[V]HI flags * but this isn't used by us at all. Maybe someone somewhere * will need the custom_divisor setting. */ break; default: break; } return -ENOIOCTLCMD;}static void whiteheat_set_termios (struct usb_serial_port *port, struct termios *old_termios){ dbg("%s -port %d", __FUNCTION__, port->number); if ((!port->tty) || (!port->tty->termios)) { dbg("%s - no tty structures", __FUNCTION__); goto exit; } /* check that they really want us to change something */ if (old_termios) { if ((port->tty->termios->c_cflag == old_termios->c_cflag) && (port->tty->termios->c_iflag == old_termios->c_iflag)) { dbg("%s - nothing to change...", __FUNCTION__); goto exit; } } firm_setup_port(port);exit: return;}static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) { firm_set_break(port, break_state);}static int whiteheat_chars_in_buffer(struct usb_serial_port *port){ struct whiteheat_private *info = usb_get_serial_port_data(port); struct list_head *tmp; struct whiteheat_urb_wrap *wrap; int chars = 0; unsigned long flags; dbg("%s - port %d", __FUNCTION__, port->number); spin_lock_irqsave(&info->lock, flags); list_for_each(tmp, &info->tx_urbs_submitted) { wrap = list_entry(tmp, struct whiteheat_urb_wrap, list); chars += wrap->urb->transfer_buffer_length; } spin_unlock_irqrestore(&info->lock, flags); dbg ("%s - returns %d", __FUNCTION__, chars); return (chars);}static void whiteheat_throttle (struct usb_serial_port *port){ struct whiteheat_private *info = usb_get_serial_port_data(port); unsigned long flags; dbg("%s - port %d", __FUNCTION__, port->number); spin_lock_irqsave(&info->lock, flags); info->flags |= THROTTLED; spin_unlock_irqrestore(&info->lock, flags); return;}static void whiteheat_unthrottle (struct usb_serial_port *port){ struct whiteheat_private *info = usb_get_serial_port_data(port); int actually_throttled; unsigned long flags; dbg("%s - port %d", __FUNCTION__, port->number); spin_lock_irqsave(&info->lock, flags); actually_throttled = info->flags & ACTUALLY_THROTTLED; info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED); spin_unlock_irqrestore(&info->lock, flags); if (actually_throttled) rx_data_softint(port); return;}/***************************************************************************** * Connect Tech's White Heat callback routines *****************************************************************************/static void command_port_write_callback (struct urb *urb, struct pt_regs *regs){ dbg("%s", __FUNCTION__); if (urb->status) { dbg ("nonzero urb status: %d", urb->status); return; }}static void command_port_read_callback (struct urb *urb, struct pt_regs *regs){ struct usb_serial_port *command_port = (struct usb_serial_port *)urb->context; struct whiteheat_command_private *command_info; unsigned char *data = urb->transfer_buffer; int result; unsigned long flags; dbg("%s", __FUNCTION__); if (urb->status) { dbg("%s - nonzero urb status: %d", __FUNCTION__, urb->status); return; } usb_serial_debug_data(debug, &command_port->dev, __FUNCTION__, urb->actual_length, data); command_info = usb_get_serial_port_data(command_port); if (!command_info) { dbg ("%s - command_info is NULL, exiting.", __FUNCTION__); return; } spin_lock_irqsave(&command_info->lock, flags); if (data[0] == WHITEHEAT_CMD_COMPLETE) { command_info->command_finished = WHITEHEAT_CMD_COMPLETE; wake_up_interruptible(&command_info->wait_command); } else if (data[0] == WHITEHEAT_CMD_FAILURE) { command_info->command_finished = WHITEHEAT_CMD_FAILURE; wake_up_interruptible(&command_info->wait_command); } else if (data[0] == WHITEHEAT_EVENT) { /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */ dbg("%s - event received", __FUNCTION__); } else if (data[0] == WHITEHEAT_GET_DTR_RTS) { memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1); command_info->command_finished = WHITEHEAT_CMD_COMPLETE; wake_up_interruptible(&command_info->wait_command); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -