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

📄 usbserial.c

📁 linux-2.4.29操作系统的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
 * * Our component drivers are hideously buggy and written by people * who have difficulty understanding the concept of spinlocks. * There were so many races and lockups that Greg K-H made a watershed * decision to provide what is essentially a single-threaded sandbox * for component drivers, protected by a semaphore. It helped a lot, but * for one little problem: when tty->low_latency is set, line disciplines * can call ->write from an interrupt, where the semaphore oopses. * * Rather than open the whole can of worms again, we just post writes * into a helper which can sleep. * * Kernel 2.6 has a proper fix. It replaces semaphores with proper locking. */static void post_helper(void *arg){	struct list_head *pos;	struct usb_serial_post_job *job;	struct usb_serial_port *port;	struct usb_serial *serial;	unsigned int flags;	spin_lock_irqsave(&post_lock, flags);	pos = post_list.next;	while (pos != &post_list) {		job = list_entry(pos, struct usb_serial_post_job, link);		port = job->port;		/* get_usb_serial checks port->tty, so cannot be used */		serial = port->serial;		if (port->write_busy) {			dbg("%s - port %d busy", __FUNCTION__, port->number);			pos = pos->next;			continue;		}		list_del(&job->link);		spin_unlock_irqrestore(&post_lock, flags);		down(&port->sem);		dbg("%s - port %d len %d backlog %d", __FUNCTION__,		    port->number, job->len, port->write_backlog);		if (port->tty != NULL) {			int rc;			int sent = 0;			while (sent < job->len) {				rc = __serial_write(port, 0, job->buff + sent, job->len - sent);				if ((rc < 0) || signal_pending(current))					break;				sent += rc;				if ((sent < job->len) && current->need_resched)					schedule();			}		}		up(&port->sem);		spin_lock_irqsave(&post_lock, flags);		port->write_backlog -= job->len;		kfree(job);		if (--serial->ref == 0)			kfree(serial);		/* Have to reset because we dropped spinlock */		pos = post_list.next;	}	spin_unlock_irqrestore(&post_lock, flags);}#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->tty) {			port->tty->driver_data = NULL;			port->tty = NULL;		}	}	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) {		/*		 * XXX The right thing would be to wait for the output to drain.		 * But we are not sufficiently daring to experiment in 2.4.		 * N.B. If we do wait, no need to run post_helper here.		 * Normall callback mechanism wakes it up just fine.		 */#if I_AM_A_DARING_HACKER		tty->closing = 1;		up (&port->sem);		if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)			tty_wait_until_sent(tty, info->closing_wait);		down (&port->sem);		if (!tty->driver_data) /* woopsie, disconnect, now what */ ;#endif		__serial_close(port, filp);	}	up (&port->sem);}static int __serial_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count){	struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);	int retval = -EINVAL;	if (!serial)		return -ENODEV;	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:	return retval;}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;	int rc;	if (!in_interrupt()) {		/*		 * Run post_list to reduce a possiblity of reordered writes.		 * Tasks can make keventd to sleep, sometimes for a long time.		 */		post_helper(NULL);		down(&port->sem);		/*		 * This happens when a line discipline asks how much room		 * we have, gets 64, then tries to perform two writes		 * for a byte each. First write takes whole URB, second		 * write hits this check.		 */		if (port->write_busy) {			up(&port->sem);			return serial_post_job(port, from_user, GFP_KERNEL,			    buf, count);		}		rc = __serial_write(port, from_user, buf, count);		up(&port->sem);		return rc;	}	if (from_user) {		/*		 * This is a BUG-able offense because we cannot		 * pagefault while in_interrupt, but we want to see		 * something in dmesg rather than just blinking LEDs.		 */		err("user data in interrupt write");		return -EINVAL;	}	return serial_post_job(port, 0, GFP_ATOMIC, buf, count);}static int serial_post_job(struct usb_serial_port *port, int from_user,    int gfp, const unsigned char *buf, int count){	int done = 0, length;	int rc;	if (port == NULL)		return -EPIPE;	if (count >= 512) {		static int rate = 0;		/*		 * Data loss due to extreme circumstances.		 * It's a ususal thing on serial to lose characters, isn't it?		 * Neener, neener! Actually, it's probably an echo loop anyway.		 * Only happens when getty starts talking to Visor.		 */		if (++rate % 1000 < 3) {			err("too much data (%d) from %s", count,			    from_user? "user": "kernel");		}		count = 512;	}	while (done < count) {		length = count - done;		if (length > POST_BSIZE)			length = POST_BSIZE;		if (length > port->bulk_out_size)			length = port->bulk_out_size;		rc = serial_post_one(port, from_user, gfp, buf + done, length);		if (rc <= 0) {			if (done != 0)				return done;			return rc;		}		done += rc;	}	return done;}static int serial_post_one(struct usb_serial_port *port, int from_user,    int gfp, const unsigned char *buf, int count){	struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);	struct usb_serial_post_job *job;	unsigned long flags;	dbg("%s - port %d user %d count %d", __FUNCTION__, port->number, from_user, count);	job = kmalloc(sizeof(struct usb_serial_post_job), gfp);	if (job == NULL)		return -ENOMEM;	job->port = port;	if (count >= POST_BSIZE)		count = POST_BSIZE;	job->len = count;	if (from_user) {		if (copy_from_user(job->buff, buf, count)) {			kfree(job);			return -EFAULT;		}	} else {		memcpy(job->buff, buf, count);	}	spin_lock_irqsave(&post_lock, flags);	port->write_backlog += count;	list_add_tail(&job->link, &post_list);	serial->ref++;		/* Protect the port->sem from kfree() */	schedule_task(&post_task);	spin_unlock_irqrestore(&post_lock, flags);	return 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__);	int retval = -EINVAL;	if (!serial)		return -ENODEV;	if (in_interrupt()) {		retval = 0;		if (!port->write_busy && port->write_backlog == 0)			retval = port->bulk_out_size;		dbg("%s - returns %d", __FUNCTION__, retval);		return retval;	}	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);	if (!port->open_count) {		dbg("%s - port %d: not open", __FUNCTION__, port->number);		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;	}

⌨️ 快捷键说明

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