📄 ftdi_sio.c
字号:
.owner = THIS_MODULE, .name = "FTDI SIO compatible", .id_table = id_table_userdev, .num_interrupt_in = 0, .num_bulk_in = 1, .num_bulk_out = 1, .num_ports = 1, .open = ftdi_open, .close = ftdi_close, .write = ftdi_write, .write_room = ftdi_write_room, .read_bulk_callback = ftdi_read_bulk_callback, .write_bulk_callback = ftdi_write_bulk_callback, .ioctl = ftdi_ioctl, .set_termios = ftdi_set_termios, .break_ctl = ftdi_break_ctl, .startup = ftdi_userdev_startup, .shutdown = ftdi_shutdown,};#define WDR_TIMEOUT (HZ * 5 ) /* default urb timeout *//* High and low are for DTR, RTS etc etc */#define HIGH 1#define LOW 0/* * *************************************************************************** * Utlity functions * *************************************************************************** */static unsigned short int ftdi_232am_baud_base_to_divisor(int baud, int base){ unsigned short int divisor; int divisor3 = base / 2 / baud; // divisor shifted 3 bits to the left if ((divisor3 & 0x7) == 7) divisor3 ++; // round x.7/8 up to x+1 divisor = divisor3 >> 3; divisor3 &= 0x7; if (divisor3 == 1) divisor |= 0xc000; else // 0.125 if (divisor3 >= 4) divisor |= 0x4000; else // 0.5 if (divisor3 != 0) divisor |= 0x8000; // 0.25 if (divisor == 1) divisor = 0; /* special case for maximum baud rate */ return divisor;}static unsigned short int ftdi_232am_baud_to_divisor(int baud){ return(ftdi_232am_baud_base_to_divisor(baud, 48000000));}static __u32 ftdi_232bm_baud_base_to_divisor(int baud, int base){ static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 }; __u32 divisor; int divisor3 = base / 2 / baud; // divisor shifted 3 bits to the left divisor = divisor3 >> 3; divisor |= (__u32)divfrac[divisor3 & 0x7] << 14; /* Deal with special cases for highest baud rates. */ if (divisor == 1) divisor = 0; else // 1.0 if (divisor == 0x4001) divisor = 1; // 1.5 return divisor;}static __u32 ftdi_232bm_baud_to_divisor(int baud){ return(ftdi_232bm_baud_base_to_divisor(baud, 48000000));}static int set_rts(struct usb_serial_port *port, int high_or_low){ struct ftdi_private * priv = (struct ftdi_private *)port->private; char buf[1]; unsigned ftdi_high_or_low; if (high_or_low) { ftdi_high_or_low = FTDI_SIO_SET_RTS_HIGH; priv->last_dtr_rts |= TIOCM_RTS; } else { ftdi_high_or_low = FTDI_SIO_SET_RTS_LOW; priv->last_dtr_rts &= ~TIOCM_RTS; } return(usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0), FTDI_SIO_SET_MODEM_CTRL_REQUEST, FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE, ftdi_high_or_low, 0, buf, 0, WDR_TIMEOUT));}static int set_dtr(struct usb_serial_port *port, int high_or_low){ struct ftdi_private * priv = (struct ftdi_private *)port->private; char buf[1]; unsigned ftdi_high_or_low; if (high_or_low) { ftdi_high_or_low = FTDI_SIO_SET_DTR_HIGH; priv->last_dtr_rts |= TIOCM_DTR; } else { ftdi_high_or_low = FTDI_SIO_SET_DTR_LOW; priv->last_dtr_rts &= ~TIOCM_DTR; } return(usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0), FTDI_SIO_SET_MODEM_CTRL_REQUEST, FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE, ftdi_high_or_low, 0, buf, 0, WDR_TIMEOUT));}static __u32 get_ftdi_divisor(struct usb_serial_port * port);static int change_speed(struct usb_serial_port *port){ char buf[1]; __u16 urb_value; __u16 urb_index; __u32 urb_index_value; urb_index_value = get_ftdi_divisor(port); urb_value = (__u16)urb_index_value; urb_index = (__u16)(urb_index_value >> 16); return (usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0), FTDI_SIO_SET_BAUDRATE_REQUEST, FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE, urb_value, urb_index, buf, 0, 100) < 0);}static __u32 get_ftdi_divisor(struct usb_serial_port * port){ /* get_ftdi_divisor */ struct ftdi_private * priv = (struct ftdi_private *)port->private; __u32 div_value = 0; int div_okay = 1; char *chip_name = ""; int baud; /* * The logic involved in setting the baudrate can be cleanly split in 3 steps. * Obtaining the actual baud rate is a little tricky since unix traditionally * somehow ignored the possibility to set non-standard baud rates. * 1. Standard baud rates are set in tty->termios->c_cflag * 2. If these are not enough, you can set any speed using alt_speed as follows: * - set tty->termios->c_cflag speed to B38400 * - set your real speed in tty->alt_speed; it gets ignored when * alt_speed==0, (or) * - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows: * flags & ASYNC_SPD_MASK == ASYNC_SPD_[HI, VHI, SHI, WARP], this just * sets alt_speed to (HI: 57600, VHI: 115200, SHI: 230400, WARP: 460800) * ** Steps 1, 2 are done courtesy of tty_get_baud_rate * 3. You can also set baud rate by setting custom divisor as follows * - set tty->termios->c_cflag speed to B38400 * - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows: * o flags & ASYNC_SPD_MASK == ASYNC_SPD_CUST * o custom_divisor set to baud_base / your_new_baudrate * ** Step 3 is done courtesy of code borrowed from serial.c - I should really * spend some time and separate+move this common code to serial.c, it is * replicated in nearly every serial driver you see. */ /* 1. Get the baud rate from the tty settings, this observes alt_speed hack */ baud = tty_get_baud_rate(port->tty); dbg("%s - tty_get_baud_rate reports speed %d", __FUNCTION__, baud); /* 2. Observe async-compatible custom_divisor hack, update baudrate if needed */ if (baud == 38400 && ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) && (priv->custom_divisor)) { baud = priv->baud_base / priv->custom_divisor; dbg("%s - custom divisor %d sets baud rate to %d", __FUNCTION__, priv->custom_divisor, baud); } /* 3. Convert baudrate to device-specific divisor */ if (!baud) baud = 9600; switch(priv->chip_type) { case SIO: /* SIO chip */ chip_name = "SIO"; switch(baud) { case 300: div_value = ftdi_sio_b300; break; case 600: div_value = ftdi_sio_b600; break; case 1200: div_value = ftdi_sio_b1200; break; case 2400: div_value = ftdi_sio_b2400; break; case 4800: div_value = ftdi_sio_b4800; break; case 9600: div_value = ftdi_sio_b9600; break; case 19200: div_value = ftdi_sio_b19200; break; case 38400: div_value = ftdi_sio_b38400; break; case 57600: div_value = ftdi_sio_b57600; break; case 115200: div_value = ftdi_sio_b115200; break; } /* baud */ if (div_value == 0) { dbg("%s - Baudrate (%d) requested is not supported", __FUNCTION__, baud); div_value = ftdi_sio_b9600; div_okay = 0; } break; case FT8U232AM: /* 8U232AM chip */ chip_name = "FT8U232AM"; if (baud <= 3000000) { div_value = ftdi_232am_baud_to_divisor(baud); } else { dbg("%s - Baud rate too high!", __FUNCTION__); div_value = ftdi_232am_baud_to_divisor(9600); div_okay = 0; } break; case FT232BM: /* FT232BM chip */ chip_name = "FT232BM"; if (baud <= 3000000) { div_value = ftdi_232bm_baud_to_divisor(baud); } else { dbg("%s - Baud rate too high!", __FUNCTION__); div_value = ftdi_232bm_baud_to_divisor(9600); div_okay = 0; } break; } /* priv->chip_type */ if (div_okay) { dbg("%s - Baud rate set to %d (divisor 0x%lX) on chip %s", __FUNCTION__, baud, (unsigned long)div_value, chip_name); } return(div_value);}static int get_serial_info(struct usb_serial_port * port, struct serial_struct * retinfo){ struct ftdi_private * priv = (struct ftdi_private*) port->private; struct serial_struct tmp; if (!retinfo) return -EFAULT; memset(&tmp, 0, sizeof(tmp)); tmp.flags = priv->flags; tmp.baud_base = priv->baud_base; tmp.custom_divisor = priv->custom_divisor; 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)) { if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) port->tty->alt_speed = 57600; else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) port->tty->alt_speed = 115200; else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) port->tty->alt_speed = 230400; else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) port->tty->alt_speed = 460800; else port->tty->alt_speed = 0; } if (((old_priv.flags & ASYNC_SPD_MASK) != (priv->flags & ASYNC_SPD_MASK)) || (((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) && (old_priv.custom_divisor != priv->custom_divisor))) { change_speed(port); } return (0);} /* set_serial_info *//* * *************************************************************************** * FTDI driver specific functions * *************************************************************************** *//* Common startup subroutine *//* Called from ftdi_SIO_startup, etc. */static int ftdi_common_startup (struct usb_serial *serial){ struct usb_serial_port *port = &serial->port[0]; struct ftdi_private *priv; int i ; struct urb *urb; dbg("%s",__FUNCTION__); priv = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL); if (!priv){ err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private)); return -ENOMEM; } memset(priv, 0, sizeof(*priv)); 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; /* Increase the size of read buffers */ if (port->bulk_in_buffer) { kfree (port->bulk_in_buffer); } port->bulk_in_buffer = kmalloc (BUFSZ, GFP_KERNEL); if (!port->bulk_in_buffer) { kfree (priv); return -ENOMEM; } if (port->read_urb) { port->read_urb->transfer_buffer = port->bulk_in_buffer; port->read_urb->transfer_buffer_length = BUFSZ; } /* Free port's existing write urb and transfer buffer. */ if (port->write_urb) { usb_free_urb (port->write_urb); port->write_urb = NULL; } if (port->bulk_out_buffer) { kfree (port->bulk_out_buffer); port->bulk_out_buffer = NULL; } /* create our write urb pool and transfer buffers */ spin_lock_init (&priv->write_urb_pool_lock); for (i = 0; i < NUM_URBS; ++i) { urb = usb_alloc_urb(0); priv->write_urb_pool[i] = urb; if (urb == NULL) { err("Unable to create new urb in urb pool");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -