ftdi_sio.c
来自「优龙2410linux2.6.8内核源代码」· C语言 代码 · 共 1,815 行 · 第 1/5 页
C
1,815 行
.unthrottle = ftdi_unthrottle, .write = ftdi_write, .write_room = ftdi_write_room, .chars_in_buffer = ftdi_chars_in_buffer, .read_bulk_callback = ftdi_read_bulk_callback, .write_bulk_callback = ftdi_write_bulk_callback, .tiocmget = ftdi_tiocmget, .tiocmset = ftdi_tiocmset, .ioctl = ftdi_ioctl, .set_termios = ftdi_set_termios, .break_ctl = ftdi_break_ctl, .attach = ftdi_FT232BM_startup, .shutdown = ftdi_shutdown,};static struct usb_serial_device_type ftdi_USB_UIRT_device = { .owner = THIS_MODULE, .name = "USB-UIRT Infrared Tranceiver", .id_table = id_table_USB_UIRT, .num_interrupt_in = 0, .num_bulk_in = 1, .num_bulk_out = 1, .num_ports = 1, .open = ftdi_open, .close = ftdi_close, .throttle = ftdi_throttle, .unthrottle = ftdi_unthrottle, .write = ftdi_write, .write_room = ftdi_write_room, .chars_in_buffer = ftdi_chars_in_buffer, .read_bulk_callback = ftdi_read_bulk_callback, .write_bulk_callback = ftdi_write_bulk_callback, .tiocmget = ftdi_tiocmget, .tiocmset = ftdi_tiocmset, .ioctl = ftdi_ioctl, .set_termios = ftdi_set_termios, .break_ctl = ftdi_break_ctl, .attach = ftdi_USB_UIRT_startup, .shutdown = ftdi_shutdown,};/* The TIRA1 is based on a FT232BM which requires a fixed baud rate of 100000 * and which requires RTS-CTS to be enabled. */static struct usb_serial_device_type ftdi_HE_TIRA1_device = { .owner = THIS_MODULE, .name = "Home-Electronics TIRA-1 IR Transceiver", .id_table = id_table_HE_TIRA1, .num_interrupt_in = 0, .num_bulk_in = 1, .num_bulk_out = 1, .num_ports = 1, .open = ftdi_open, .close = ftdi_close, .throttle = ftdi_throttle, .unthrottle = ftdi_unthrottle, .write = ftdi_write, .write_room = ftdi_write_room, .chars_in_buffer = ftdi_chars_in_buffer, .read_bulk_callback = ftdi_read_bulk_callback, .write_bulk_callback = ftdi_write_bulk_callback, .tiocmget = ftdi_tiocmget, .tiocmset = ftdi_tiocmset, .ioctl = ftdi_ioctl, .set_termios = ftdi_set_termios, .break_ctl = ftdi_break_ctl, .attach = ftdi_HE_TIRA1_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 = usb_get_serial_port_data(port); char *buf; unsigned ftdi_high_or_low; int rv; buf = kmalloc(1, GFP_NOIO); if (!buf) return -ENOMEM; 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; } rv = 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); kfree(buf); return rv;}static int set_dtr(struct usb_serial_port *port, int high_or_low){ struct ftdi_private *priv = usb_get_serial_port_data(port); char *buf; unsigned ftdi_high_or_low; int rv; buf = kmalloc(1, GFP_NOIO); if (!buf) return -ENOMEM; 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; } rv = 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); kfree(buf); return rv;}static __u32 get_ftdi_divisor(struct usb_serial_port * port);static int change_speed(struct usb_serial_port *port){ char *buf; __u16 urb_value; __u16 urb_index; __u32 urb_index_value; int rv; buf = kmalloc(1, GFP_NOIO); if (!buf) return -ENOMEM; urb_index_value = get_ftdi_divisor(port); urb_value = (__u16)urb_index_value; urb_index = (__u16)(urb_index_value >> 16); rv = 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); kfree(buf); return rv;}static __u32 get_ftdi_divisor(struct usb_serial_port * port){ /* get_ftdi_divisor */ struct ftdi_private *priv = usb_get_serial_port_data(port); __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 __user * retinfo){ struct ftdi_private *priv = usb_get_serial_port_data(port); 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 __user * newinfo){ /* set_serial_info */ struct ftdi_private *priv = usb_get_serial_port_data(port); 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:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?