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

📄 usbserial.c

📁 讲述linux的初始化过程
💻 C
📖 第 1 页 / 共 3 页
字号:
	 	/* pass on to the driver specific version of this function if it is available */	if (serial->type->open) {		return (serial->type->open(port, filp));	} else {		return (generic_open(port, filp));	}}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;	}	dbg(__FUNCTION__ " - port %d", port->number);		if (!port->active) {		dbg (__FUNCTION__ " - port not opened");		return;	}	/* pass on to the driver specific version of this function if it is available */	if (serial->type->close) {		serial->type->close(port, filp);	} else {		generic_close(port, filp);	}}	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__);		if (!serial) {		return -ENODEV;	}		dbg(__FUNCTION__ " - port %d, %d byte(s)", port->number, count);	if (!port->active) {		dbg (__FUNCTION__ " - port not opened");		return -EINVAL;	}		/* pass on to the driver specific version of this function if it is available */	if (serial->type->write) {		return (serial->type->write(port, from_user, buf, count));	} else {		return (generic_write(port, from_user, buf, count));	}}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__);	if (!serial) {		return -ENODEV;	}	dbg(__FUNCTION__ " - port %d", port->number);		if (!port->active) {		dbg (__FUNCTION__ " - port not open");		return -EINVAL;	}	/* pass on to the driver specific version of this function if it is available */	if (serial->type->write_room) {		return (serial->type->write_room(port));	} else {		return (generic_write_room(port));	}}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__);	if (!serial) {		return -ENODEV;	}	if (!port->active) {		dbg (__FUNCTION__ " - port not open");		return -EINVAL;	}	/* pass on to the driver specific version of this function if it is available */	if (serial->type->chars_in_buffer) {		return (serial->type->chars_in_buffer(port));	} else {		return (generic_chars_in_buffer(port));	}}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;	}	dbg(__FUNCTION__ " - port %d", port->number);	if (!port->active) {		dbg (__FUNCTION__ " - port not open");		return;	}	/* pass on to the driver specific version of this function */	if (serial->type->throttle) {		serial->type->throttle(port);	}	return;}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;	}	dbg(__FUNCTION__ " - port %d", port->number);	if (!port->active) {		dbg (__FUNCTION__ " - port not open");		return;	}	/* pass on to the driver specific version of this function */	if (serial->type->unthrottle) {		serial->type->unthrottle(port);	}	return;}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__);	if (!serial) {		return -ENODEV;	}	dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);	if (!port->active) {		dbg (__FUNCTION__ " - port not open");		return -ENODEV;	}	/* pass on to the driver specific version of this function if it is available */	if (serial->type->ioctl) {		return (serial->type->ioctl(port, file, cmd, arg));	} else {		return -ENOIOCTLCMD;	}}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;	}	dbg(__FUNCTION__ " - port %d", port->number);	if (!port->active) {		dbg (__FUNCTION__ " - port not open");		return;	}	/* 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);	}		return;}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;	}	dbg(__FUNCTION__ " - port %d", port->number);	if (!port->active) {		dbg (__FUNCTION__ " - port not open");		return;	}	/* 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);	}}/***************************************************************************** * generic devices specific driver functions *****************************************************************************/static int generic_open (struct usb_serial_port *port, struct file *filp){	struct usb_serial *serial = port->serial;	unsigned long flags;	int result;	if (port_paranoia_check (port, __FUNCTION__))		return -ENODEV;	dbg(__FUNCTION__ " - port %d", port->number);	spin_lock_irqsave (&port->port_lock, flags);		++port->open_count;	MOD_INC_USE_COUNT;		if (!port->active) {		port->active = 1;		/* if we have a bulk interrupt, start reading from it */		if (serial->num_bulk_in) {			/* 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,				      ((serial->type->read_bulk_callback) ?				       serial->type->read_bulk_callback :				       generic_read_bulk_callback), 				      port);			result = usb_submit_urb(port->read_urb);			if (result)				err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);		}	}		spin_unlock_irqrestore (&port->port_lock, flags);		return 0;}static void generic_close (struct usb_serial_port *port, struct file * filp){	struct usb_serial *serial = port->serial;	unsigned long flags;	dbg(__FUNCTION__ " - port %d", port->number);	spin_lock_irqsave (&port->port_lock, flags);	--port->open_count;	if (port->open_count <= 0) {		/* 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);				port->active = 0;		port->open_count = 0;	}	spin_unlock_irqrestore (&port->port_lock, flags);}static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count){	struct usb_serial *serial = port->serial;	unsigned long flags;	int result;	dbg(__FUNCTION__ " - port %d", port->number);	if (count == 0) {		dbg(__FUNCTION__ " - write request of 0 bytes");		return (0);	}	/* only do something if we have a bulk out endpoint */	if (serial->num_bulk_out) {		if (port->write_urb->status == -EINPROGRESS) {			dbg (__FUNCTION__ " - already writing");			return (0);		}		spin_lock_irqsave (&port->port_lock, flags);		count = (count > port->bulk_out_size) ? port->bulk_out_size : count;		usb_serial_debug_data (__FILE__, __FUNCTION__, count, buf);		if (from_user) {			copy_from_user(port->write_urb->transfer_buffer, buf, count);		}		else {			memcpy (port->write_urb->transfer_buffer, buf, count);		}  		/* set up our urb */		FILL_BULK_URB(port->write_urb, serial->dev, 			      usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),			      port->write_urb->transfer_buffer, count,			      ((serial->type->write_bulk_callback) ? 			       serial->type->write_bulk_callback : 			       generic_write_bulk_callback), 			      port);		/* send the data out the bulk port */		result = usb_submit_urb(port->write_urb);		if (result) {			err(__FUNCTION__ " - failed submitting write urb, error %d", result);			spin_unlock_irqrestore (&port->port_lock, flags);			return 0;		}		spin_unlock_irqrestore (&port->port_lock, flags);		return (count);	}		/* no bulk out, so return 0 bytes written */	return (0);} static int generic_write_room (struct usb_serial_port *port){	struct usb_serial *serial = port->serial;	int room = 0;	dbg(__FUNCTION__ " - port %d", port->number);		if (serial->num_bulk_out)		if (port->write_urb->status != -EINPROGRESS)			room = port->bulk_out_size;		dbg(__FUNCTION__ " - returns %d", room);	return (room);}static int generic_chars_in_buffer (struct usb_serial_port *port){	struct usb_serial *serial = port->serial;	int chars = 0;	dbg(__FUNCTION__ " - port %d", port->number);		if (serial->num_bulk_out)		if (port->write_urb->status == -EINPROGRESS)			chars = port->write_urb->transfer_buffer_length;	dbg (__FUNCTION__ " - returns %d", chars);	return (chars);}static void generic_read_bulk_callback (struct urb *urb){	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;	struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);	struct tty_struct *tty;	unsigned char *data = urb->transfer_buffer;	int i;	int result;	dbg(__FUNCTION__ " - port %d", port->number);		if (!serial) {		dbg(__FUNCTION__ " - bad serial pointer, exiting");		return;	}	if (urb->status) {		dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);		return;	}	usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);	tty = port->tty;	if (urb->actual_length) {		for (i = 0; i < urb->actual_length ; ++i) {			 tty_insert_flip_char(tty, data[i], 0);	  	}	  	tty_flip_buffer_push(tty);	}	/* Continue trying to always read  */	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);	if (result)		err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);}static void generic_write_bulk_callback (struct urb *urb){	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;	struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);	dbg(__FUNCTION__ " - port %d", port->number);		if (!serial) {		dbg(__FUNCTION__ " - bad serial pointer, exiting");		return;	}	if (urb->status) {		dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);		return;	}	queue_task(&port->tqueue, &tq_immediate);	mark_bh(IMMEDIATE_BH);		return;}static void generic_shutdown (struct usb_serial *serial){	int i;	dbg (__FUNCTION__);	/* stop reads and writes on all ports */	for (i=0; i < serial->num_ports; ++i) {		while (serial->port[i].open_count > 0) {			generic_close (&serial->port[i], NULL);		}	}}static void port_softint(void *private)

⌨️ 快捷键说明

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