📄 ftdi_sio.c
字号:
if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) return -EFAULT; return 0;} /* get_serial_info */static int set_serial_info(struct usb_serial_port * port, struct serial_struct * newinfo){ /* set_serial_info */ struct ftdi_private * priv = (struct ftdi_private *) port->private; struct serial_struct new_serial; struct ftdi_private old_priv; if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) return -EFAULT; old_priv = * priv; /* Do error checking and permission checking */ if (!capable(CAP_SYS_ADMIN)) { if (((new_serial.flags & ~ASYNC_USR_MASK) != (priv->flags & ~ASYNC_USR_MASK))) return -EPERM; priv->flags = ((priv->flags & ~ASYNC_USR_MASK) | (new_serial.flags & ASYNC_USR_MASK)); priv->custom_divisor = new_serial.custom_divisor; goto check_and_exit; } if ((new_serial.baud_base != priv->baud_base) || (new_serial.baud_base < 9600)) return -EINVAL; /* Make the changes - these are privileged changes! */ priv->flags = ((priv->flags & ~ASYNC_FLAGS) | (new_serial.flags & ASYNC_FLAGS)); priv->custom_divisor = new_serial.custom_divisor; port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;check_and_exit: if (((old_priv.flags & ASYNC_SPD_MASK) != (priv->flags & ASYNC_SPD_MASK)) || (old_priv.custom_divisor != priv->custom_divisor)) { if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) port->tty->alt_speed = 57600; if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) port->tty->alt_speed = 115200; if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) port->tty->alt_speed = 230400; if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) port->tty->alt_speed = 460800; change_speed(port); } return (0);} /* set_serial_info *//* * *************************************************************************** * FTDI driver specific functions * *************************************************************************** *//* Startup for the SIO chip */static int ftdi_SIO_startup (struct usb_serial *serial){ struct ftdi_private *priv; priv = serial->port->private = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL); if (!priv){ err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private)); return -ENOMEM; } priv->chip_type = SIO; priv->baud_base = 12000000 / 16; priv->custom_divisor = 0; priv->write_offset = 1; priv->prev_status = priv->diff_status = 0; /* This will push the characters through immediately rather than queue a task to deliver them */ priv->flags = ASYNC_LOW_LATENCY; return (0);}/* Startup for the 8U232AM chip */static int ftdi_8U232AM_startup (struct usb_serial *serial){ struct ftdi_private *priv; priv = serial->port->private = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL); if (!priv){ err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private)); return -ENOMEM; } priv->chip_type = FT8U232AM; priv->baud_base = 48000000 / 2; /* Would be / 16, but FTDI supports 0.125, 0.25 and 0.5 divisor fractions! */ priv->custom_divisor = 0; priv->write_offset = 0; init_waitqueue_head(&priv->delta_msr_wait); /* This will push the characters through immediately rather than queue a task to deliver them */ priv->flags = ASYNC_LOW_LATENCY; return (0);}static void ftdi_shutdown (struct usb_serial *serial){ dbg("%s", __FUNCTION__); /* stop reads and writes on all ports */ while (serial->port[0].open_count > 0) { ftdi_close (&serial->port[0], NULL); } if (serial->port[0].private){ kfree(serial->port[0].private); serial->port[0].private = NULL; }}static int ftdi_open (struct usb_serial_port *port, struct file *filp){ /* ftdi_open */ struct termios tmp_termios; struct usb_serial *serial = port->serial; struct ftdi_private *priv = port->private; int result = 0; char buf[1]; /* Needed for the usb_control_msg I think */ dbg("%s", __FUNCTION__); port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0; /* No error checking for this (will get errors later anyway) */ /* See ftdi_sio.h for description of what is reset */ usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), FTDI_SIO_RESET_REQUEST, FTDI_SIO_RESET_REQUEST_TYPE, FTDI_SIO_RESET_SIO, 0, buf, 0, WDR_TIMEOUT); /* Termios defaults are set by usb_serial_init. We don't change port->tty->termios - this would loose speed settings, etc. This is same behaviour as serial.c/rs_open() - Kuba */ /* ftdi_set_termios will send usb control messages */ ftdi_set_termios(port, &tmp_termios); /* FIXME: Flow control might be enabled, so it should be checked - we have no control of defaults! */ /* Turn on RTS and DTR since we are not flow controlling by default */ if (set_dtr(serial->dev, usb_sndctrlpipe(serial->dev, 0),HIGH) < 0) { err("%s Error from DTR HIGH urb", __FUNCTION__); } if (set_rts(serial->dev, usb_sndctrlpipe(serial->dev, 0),HIGH) < 0){ err("%s Error from RTS HIGH urb", __FUNCTION__); } /* Start reading from the device */ FILL_BULK_URB(port->read_urb, serial->dev, usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress), port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length, ftdi_read_bulk_callback, port); result = usb_submit_urb(port->read_urb); if (result) err("%s - failed submitting read urb, error %d", __FUNCTION__, result); return result;} /* ftdi_open */static void ftdi_close (struct usb_serial_port *port, struct file *filp){ /* ftdi_close */ struct usb_serial *serial = port->serial; /* Checked in usbserial.c */ unsigned int c_cflag = port->tty->termios->c_cflag; char buf[1]; dbg("%s", __FUNCTION__); if (serial->dev) { if (c_cflag & HUPCL){ /* Disable flow control */ if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), FTDI_SIO_SET_FLOW_CTRL_REQUEST, FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE, 0, 0, buf, 0, WDR_TIMEOUT) < 0) { err("error from flowcontrol urb"); } /* drop DTR */ if (set_dtr(serial->dev, usb_sndctrlpipe(serial->dev, 0), LOW) < 0){ err("Error from DTR LOW urb"); } /* drop RTS */ if (set_rts(serial->dev, usb_sndctrlpipe(serial->dev, 0),LOW) < 0) { err("Error from RTS LOW urb"); } } /* Note change no line is hupcl is off */ /* shutdown our bulk reads and writes */ /* ***CHECK*** behaviour when there is nothing queued */ usb_unlink_urb (port->write_urb); usb_unlink_urb (port->read_urb); }} /* ftdi_close */ /* The ftdi_sio requires the first byte to have: * B0 1 * B1 0 * B2..7 length of message excluding byte 0 */static int ftdi_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count){ /* ftdi_write */ struct usb_serial *serial = port->serial; struct ftdi_private *priv = (struct ftdi_private *)port->private; unsigned char *first_byte = port->write_urb->transfer_buffer; int data_offset ; int result; dbg("%s port %d, %d bytes", __FUNCTION__, port->number, count); if (count == 0) { err("write request of 0 bytes"); return 0; } data_offset = priv->write_offset; dbg("data_offset set to %d",data_offset); if (port->write_urb->status == -EINPROGRESS) { dbg("%s - already writing", __FUNCTION__); return (0); } count += data_offset; count = (count > port->bulk_out_size) ? port->bulk_out_size : count; /* Copy in the data to send */ if (from_user) { if (copy_from_user(port->write_urb->transfer_buffer + data_offset, buf, count - data_offset )){ return -EFAULT; } } else { memcpy(port->write_urb->transfer_buffer + data_offset, buf, count - data_offset ); } first_byte = port->write_urb->transfer_buffer; if (data_offset > 0){ /* Write the control byte at the front of the packet*/ *first_byte = 1 | ((count-data_offset) << 2) ; } dbg("%s Bytes: %d, First Byte: 0x%02x", __FUNCTION__,count, first_byte[0]); usb_serial_debug_data (__FILE__, __FUNCTION__, count, first_byte); /* send the data out the bulk port */ FILL_BULK_URB(port->write_urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress), port->write_urb->transfer_buffer, count, ftdi_write_bulk_callback, port); result = usb_submit_urb(port->write_urb); if (result) { err("%s - failed submitting write urb, error %d", __FUNCTION__, result); return 0; } dbg("%s write returning: %d", __FUNCTION__, count - data_offset); return (count - data_offset);} /* ftdi_write */static void ftdi_write_bulk_callback (struct urb *urb){ struct usb_serial_port *port = (struct usb_serial_port *)urb->context; struct usb_serial *serial; dbg("%s", __FUNCTION__); if (port_paranoia_check (port, "ftdi_write_bulk_callback")) { return; } serial = port->serial; if (serial_paranoia_check (serial, "ftdi_write_bulk_callback")) { return; } if (urb->status) { dbg("nonzero write bulk status received: %d", urb->status); return; } queue_task(&port->tqueue, &tq_immediate); mark_bh(IMMEDIATE_BH); return;} /* ftdi_write_bulk_callback */static int ftdi_write_room( struct usb_serial_port *port ){ struct ftdi_private *priv = (struct ftdi_private *)port->private; int room; if ( port->write_urb->status == -EINPROGRESS) { /* There is a race here with the _write routines but it won't hurt */ room = 0; } else { room = port->bulk_out_size - priv->write_offset; } return(room);} /* ftdi_write_room */static void ftdi_read_bulk_callback (struct urb *urb){ /* ftdi_read_bulk_callback */ struct usb_serial_port *port = (struct usb_serial_port *)urb->context; struct usb_serial *serial; struct tty_struct *tty = port->tty ; struct ftdi_private *priv = (struct ftdi_private *) port->private; char error_flag; unsigned char *data = urb->transfer_buffer; const int data_offset = 2; int i; int result; dbg("%s - port %d", __FUNCTION__, port->number); if (port_paranoia_check (port, "ftdi_sio_read_bulk_callback")) { return; } serial = port->serial; if (serial_paranoia_check (serial, "ftdi_sio_read_bulk_callback")) { return; } if (urb->status) { /* This will happen at close every time so it is a dbg not an err */ dbg("nonzero read bulk status received: %d", urb->status); return; } if (urb->actual_length > 2) { usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data); } else { dbg("Just status 0o%03o0o%03o",data[0],data[1]); } /* TO DO -- check for hung up line and handle appropriately: */ /* send hangup */ /* See acm.c - you do a tty_hangup - eg tty_hangup(tty) */ /* if CD is dropped and the line is not CLOCAL then we should hangup */ /* Compare new line status to the old one, signal if different */ if (priv != NULL) { char new_status = data[0] & FTDI_STATUS_B0_MASK; if (new_status != priv->prev_status) { priv->diff_status |= new_status ^ priv->prev_status; wake_up_interruptible(&priv->delta_msr_wait); priv->prev_status = new_status; } } /* Handle errors and break */ error_flag = TTY_NORMAL; /* Although the device uses a bitmask and hence can have multiple */ /* errors on a packet - the order here sets the priority the */ /* error is returned to the tty layer */ if ( data[1] & FTDI_RS_OE ) { error_flag = TTY_OVERRUN; dbg("OVERRRUN error"); } if ( data[1] & FTDI_RS_BI ) { error_flag = TTY_BREAK; dbg("BREAK received"); } if ( data[1] & FTDI_RS_PE ) { error_flag = TTY_PARITY; dbg("PARITY error"); } if ( data[1] & FTDI_RS_FE ) { error_flag = TTY_FRAME; dbg("FRAMING error"); } if (urb->actual_length > data_offset) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -