⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbserial.c

📁 是关于linux2.5.1的完全源码
💻 C
📖 第 1 页 / 共 4 页
字号:
{	int i;	dbg(__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(__FUNCTION__ " - no physical device present, failing.");		return -ENODEV;	}	transfer_buffer =  kmalloc (length, GFP_KERNEL);	if (!transfer_buffer) {		err(__FUNCTION__ " - kmalloc(%d) failed.", 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, 300);	kfree (transfer_buffer);	return result;}int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit){	int	response;	dbg(__FUNCTION__ " - %d", reset_bit);	response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0);	if (response < 0) {		err(__FUNCTION__ "- %d failed", 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(__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 (__FUNCTION__ " - port not opened");		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(__FUNCTION__ " - port %d", 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(__FUNCTION__ " - port %d, %d byte(s)", port->number, count);	if (!port->open_count) {		dbg (__FUNCTION__ " - port not opened");		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(__FUNCTION__ " - port %d", port->number);	if (!port->open_count) {		dbg (__FUNCTION__ " - port not open");		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(__FUNCTION__ " - port %d", port->number);	if (!port->open_count) {		dbg (__FUNCTION__ " - port not open");		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(__FUNCTION__ " - port %d", port->number);	if (!port->open_count) {		dbg (__FUNCTION__ " - port not open");		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(__FUNCTION__ " - port %d", port->number);	if (!port->open_count) {		dbg (__FUNCTION__ " - port not open");		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(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);	if (!port->open_count) {		dbg (__FUNCTION__ " - port not open");		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(__FUNCTION__ " - port %d", port->number);	if (!port->open_count) {		dbg (__FUNCTION__ " - port not open");		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(__FUNCTION__ " - port %d", port->number);	if (!port->open_count) {		dbg (__FUNCTION__ " - port not open");		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(__FUNCTION__);	if (serial->type->shutdown)		serial->type->shutdown(serial);	else		generic_shutdown(serial);}static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data){	struct usb_serial *serial;	int length = 0;	int i;	off_t begin = 0;	char tmp[40];	dbg(__FUNCTION__);	length += sprintf (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);	for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {		serial = get_serial_by_minor(i);		if (serial == NULL)			continue;		length += sprintf (page+length, "%d:", i);		if (serial->type->owner)			length += sprintf (page+length, " module:%s", serial->type->owner->name);		length += sprintf (page+length, " name:\"%s\"", serial->type->name);		length += sprintf (page+length, " vendor:%04x product:%04x", serial->vendor, serial->product);		length += sprintf (page+length, " num_ports:%d", serial->num_ports);		length += sprintf (page+length, " port:%d", i - serial->minor + 1);		usb_make_path(serial->dev, tmp, sizeof(tmp));		length += sprintf (page+length, " path:%s", tmp);					length += sprintf (page+length, "\n");		if ((length + begin) > (off + count))			goto done;		if ((length + begin) < off) {			begin += length;			length = 0;		}	}	*eof = 1;done:	if (off >= (length + begin))		return 0;	*start = page + (off-begin);	return ((count < begin+length-off) ? count : begin+length-off);}/***************************************************************************** * generic devices specific driver functions *****************************************************************************/static int generic_open (struct usb_serial_port *port, struct file *filp){	struct usb_serial *serial = port->serial;	int result = 0;	if (port_paranoia_check (port, __FUNCTION__))		return -ENODEV;	dbg(__FUNCTION__ " - port %d", port->number);	/* force low_latency on so that our tty_push actually forces the data through, 	   otherwise it is scheduled, and with high data rates (like with OHCI) data	   can get lost. */	if (port->tty)		port->tty->low_latency = 1;	/* if we have a bulk interrupt, start reading from it */	if (serial->num_bulk_in) {		/* Start reading from the device */		usb_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,				   ((serial->type->read_bulk_callback) ?				     serial->type->read_bulk_callback :				     generic_read_bulk_callback),				   port);		result = usb_submit_urb(port->read_urb, GFP_KERNEL);		if (result)			err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);	}	return result;}static void generic_cleanup (struct usb_serial_port *port){	struct usb_serial *serial = port->serial;	dbg(__FUNCTION__ " - port %d", port->number);	if (serial->dev) {		/* shutdown any bulk reads that might be going on */		if (serial->num_bulk_out)			usb_unlink_urb (port->write_urb);		if (serial->num_bulk_in)			usb_unlink_urb (port->read_urb);	}}static void generic_close (struct usb_serial_port *port, struct file * filp){	dbg(__FUNCTION__ " - port %d", port->number);	generic_cleanup (port);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -