📄 usbserial.c
字号:
dbg("%s %d", __FUNCTION__, num_ports); *minor = 0; for (i = 0; i < SERIAL_TTY_MINORS; ++i) { if (serial_table[i]) continue; good_spot = 1; for (j = 1; j <= num_ports-1; ++j) if (serial_table[i+j]) good_spot = 0; if (good_spot == 0) continue; if (!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) { err("%s - Out of memory", __FUNCTION__); return NULL; } memset(serial, 0, sizeof(struct usb_serial)); serial->magic = USB_SERIAL_MAGIC; serial_table[i] = serial; *minor = i; dbg("%s - minor base = %d", __FUNCTION__, *minor); for (i = *minor+1; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) serial_table[i] = serial; return serial; } return NULL;}static void return_serial (struct usb_serial *serial){ int i; dbg("%s", __FUNCTION__); if (serial == NULL) return; for (i = 0; i < serial->num_ports; ++i) { serial_table[serial->minor + i] = NULL; } return;}#ifdef USES_EZUSB_FUNCTIONS/* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */#define CPUCS_REG 0x7F92int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest){ int result; unsigned char *transfer_buffer; /* dbg("ezusb_writememory %x, %d", address, length); */ if (!serial->dev) { dbg("%s - no physical device present, failing.", __FUNCTION__); return -ENODEV; } transfer_buffer = kmalloc (length, GFP_KERNEL); if (!transfer_buffer) { err("%s - kmalloc(%d) failed.", __FUNCTION__, length); return -ENOMEM; } memcpy (transfer_buffer, data, length); result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), bRequest, 0x40, address, 0, transfer_buffer, length, 3*HZ); kfree (transfer_buffer); return result;}int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit){ int response; dbg("%s - %d", __FUNCTION__, reset_bit); response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0); if (response < 0) { err("%s- %d failed", __FUNCTION__, reset_bit); } return response;}#endif /* USES_EZUSB_FUNCTIONS *//***************************************************************************** * Driver tty interface functions *****************************************************************************/static int serial_open (struct tty_struct *tty, struct file * filp){ struct usb_serial *serial; struct usb_serial_port *port; unsigned int portNumber; int retval = 0; dbg("%s", __FUNCTION__); /* initialize the pointer incase something fails */ tty->driver_data = NULL; /* get the serial object associated with this tty pointer */ serial = get_serial_by_minor (MINOR(tty->device)); if (serial_paranoia_check (serial, __FUNCTION__)) return -ENODEV; /* set up our port structure making the tty driver remember our port object, and us it */ portNumber = MINOR(tty->device) - serial->minor; port = &serial->port[portNumber]; tty->driver_data = port; down (&port->sem); port->tty = tty; /* lock this module before we call it */ if (serial->type->owner) __MOD_INC_USE_COUNT(serial->type->owner); ++port->open_count; if (port->open_count == 1) { /* only call the device specific open if this * is the first time the port is opened */ if (serial->type->open) retval = serial->type->open(port, filp); else retval = generic_open(port, filp); } if (retval) { port->open_count = 0; if (serial->type->owner) __MOD_DEC_USE_COUNT(serial->type->owner); } up (&port->sem); return retval;}static void __serial_close(struct usb_serial_port *port, struct file *filp){ if (!port->open_count) { dbg ("%s - port not opened", __FUNCTION__); return; } --port->open_count; if (port->open_count <= 0) { /* only call the device specific close if this * port is being closed by the last owner */ if (port->serial->type->close) port->serial->type->close(port, filp); else generic_close(port, filp); port->open_count = 0; } if (port->serial->type->owner) __MOD_DEC_USE_COUNT(port->serial->type->owner);}static void serial_close(struct tty_struct *tty, struct file * filp){ struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); if (!serial) return; down (&port->sem); dbg("%s - port %d", __FUNCTION__, port->number); /* if disconnect beat us to the punch here, there's nothing to do */ if (tty->driver_data) { __serial_close(port, filp); } up (&port->sem);}static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count){ struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); int retval = -EINVAL; if (!serial) return -ENODEV; down (&port->sem); dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count); if (!port->open_count) { dbg("%s - port not opened", __FUNCTION__); goto exit; } /* pass on to the driver specific version of this function if it is available */ if (serial->type->write) retval = serial->type->write(port, from_user, buf, count); else retval = generic_write(port, from_user, buf, count);exit: up (&port->sem); return retval;}static int serial_write_room (struct tty_struct *tty) { struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); int retval = -EINVAL; if (!serial) return -ENODEV; down (&port->sem); dbg("%s - port %d", __FUNCTION__, port->number); if (!port->open_count) { dbg("%s - port not open", __FUNCTION__); goto exit; } /* pass on to the driver specific version of this function if it is available */ if (serial->type->write_room) retval = serial->type->write_room(port); else retval = generic_write_room(port);exit: up (&port->sem); return retval;}static int serial_chars_in_buffer (struct tty_struct *tty) { struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); int retval = -EINVAL; if (!serial) return -ENODEV; down (&port->sem); dbg("%s = port %d", __FUNCTION__, port->number); if (!port->open_count) { dbg("%s - port not open", __FUNCTION__); goto exit; } /* pass on to the driver specific version of this function if it is available */ if (serial->type->chars_in_buffer) retval = serial->type->chars_in_buffer(port); else retval = generic_chars_in_buffer(port);exit: up (&port->sem); return retval;}static void serial_throttle (struct tty_struct * tty){ struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); if (!serial) return; down (&port->sem); dbg("%s - port %d", __FUNCTION__, port->number); if (!port->open_count) { dbg ("%s - port not open", __FUNCTION__); goto exit; } /* pass on to the driver specific version of this function */ if (serial->type->throttle) serial->type->throttle(port);exit: up (&port->sem);}static void serial_unthrottle (struct tty_struct * tty){ struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); if (!serial) return; down (&port->sem); dbg("%s - port %d", __FUNCTION__, port->number); if (!port->open_count) { dbg("%s - port not open", __FUNCTION__); goto exit; } /* pass on to the driver specific version of this function */ if (serial->type->unthrottle) serial->type->unthrottle(port);exit: up (&port->sem);}static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg){ struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); int retval = -ENODEV; if (!serial) return -ENODEV; down (&port->sem); dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd); if (!port->open_count) { dbg ("%s - port not open", __FUNCTION__); goto exit; } /* pass on to the driver specific version of this function if it is available */ if (serial->type->ioctl) retval = serial->type->ioctl(port, file, cmd, arg); else retval = -ENOIOCTLCMD;exit: up (&port->sem); return retval;}static void serial_set_termios (struct tty_struct *tty, struct termios * old){ struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); if (!serial) return; down (&port->sem); dbg("%s - port %d", __FUNCTION__, port->number); if (!port->open_count) { dbg("%s - port not open", __FUNCTION__); goto exit; } /* pass on to the driver specific version of this function if it is available */ if (serial->type->set_termios) serial->type->set_termios(port, old);exit: up (&port->sem);}static void serial_break (struct tty_struct *tty, int break_state){ struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); if (!serial) return; down (&port->sem); dbg("%s - port %d", __FUNCTION__, port->number); if (!port->open_count) { dbg("%s - port not open", __FUNCTION__); goto exit; } /* pass on to the driver specific version of this function if it is available */ if (serial->type->break_ctl) serial->type->break_ctl(port, break_state);exit: up (&port->sem);}static void serial_shutdown (struct usb_serial *serial){ dbg ("%s", __FUNCTION__); if (serial->type->shutdown) serial->type->shutdown(serial);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -