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

📄 adutux.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * adutux - driver for ADU devices from Ontrak Control Systems * This is an experimental driver. Use at your own risk. * This driver is not supported by Ontrak Control Systems. * * Copyright (c) 2003 John Homppi (SCO, leave this notice here) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * derived from the Lego USB Tower driver 0.56: * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net> *               2001 Juergen Stuber <stuber@loria.fr> * that was derived from USB Skeleton driver - 0.5 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com) * */#include <linux/kernel.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/usb.h>#include <linux/mutex.h>#include <asm/uaccess.h>#ifdef CONFIG_USB_DEBUGstatic int debug = 5;#elsestatic int debug = 1;#endif/* Use our own dbg macro */#undef dbg#define dbg(lvl, format, arg...) 					\do { 									\	if (debug >= lvl)						\		printk(KERN_DEBUG __FILE__ " : " format " \n", ## arg);	\} while (0)/* Version Information */#define DRIVER_VERSION "v0.0.13"#define DRIVER_AUTHOR "John Homppi"#define DRIVER_DESC "adutux (see www.ontrak.net)"/* Module parameters */module_param(debug, int, S_IRUGO | S_IWUSR);MODULE_PARM_DESC(debug, "Debug enabled or not");/* Define these values to match your device */#define ADU_VENDOR_ID 0x0a07#define ADU_PRODUCT_ID 0x0064/* table of devices that work with this driver */static struct usb_device_id device_table [] = {	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) },		/* ADU100 */	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, 	/* ADU120 */	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, 	/* ADU130 */	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) },	/* ADU200 */	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) },	/* ADU208 */	{ USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) },	/* ADU218 */	{ }/* Terminating entry */};MODULE_DEVICE_TABLE(usb, device_table);#ifdef CONFIG_USB_DYNAMIC_MINORS#define ADU_MINOR_BASE	0#else#define ADU_MINOR_BASE	67#endif/* we can have up to this number of device plugged in at once */#define MAX_DEVICES	16#define COMMAND_TIMEOUT	(2*HZ)	/* 60 second timeout for a command *//* * The locking scheme is a vanilla 3-lock: *   adu_device.buflock: A spinlock, covers what IRQs touch. *   adutux_mutex:       A Static lock to cover open_count. It would also cover *                       any globals, but we don't have them in 2.6. *   adu_device.mtx:     A mutex to hold across sleepers like copy_from_user. *                       It covers all of adu_device, except the open_count *                       and what .buflock covers. *//* Structure to hold all of our device specific stuff */struct adu_device {	struct mutex		mtx;	struct usb_device*	udev; /* save off the usb device pointer */	struct usb_interface*	interface;	unsigned int		minor; /* the starting minor number for this device */	char			serial_number[8];	int			open_count; /* number of times this port has been opened */	char*			read_buffer_primary;	int			read_buffer_length;	char*			read_buffer_secondary;	int			secondary_head;	int			secondary_tail;	spinlock_t		buflock;	wait_queue_head_t	read_wait;	wait_queue_head_t	write_wait;	char*			interrupt_in_buffer;	struct usb_endpoint_descriptor* interrupt_in_endpoint;	struct urb*		interrupt_in_urb;	int			read_urb_finished;	char*			interrupt_out_buffer;	struct usb_endpoint_descriptor* interrupt_out_endpoint;	struct urb*		interrupt_out_urb;	int			out_urb_finished;};static DEFINE_MUTEX(adutux_mutex);static struct usb_driver adu_driver;static void adu_debug_data(int level, const char *function, int size,			   const unsigned char *data){	int i;	if (debug < level)		return;	printk(KERN_DEBUG __FILE__": %s - length = %d, data = ",	       function, size);	for (i = 0; i < size; ++i)		printk("%.2x ", data[i]);	printk("\n");}/** * adu_abort_transfers *      aborts transfers and frees associated data structures */static void adu_abort_transfers(struct adu_device *dev){	unsigned long flags;	dbg(2," %s : enter", __FUNCTION__);	if (dev->udev == NULL) {		dbg(1," %s : udev is null", __FUNCTION__);		goto exit;	}	/* shutdown transfer */	/* XXX Anchor these instead */	spin_lock_irqsave(&dev->buflock, flags);	if (!dev->read_urb_finished) {		spin_unlock_irqrestore(&dev->buflock, flags);		usb_kill_urb(dev->interrupt_in_urb);	} else		spin_unlock_irqrestore(&dev->buflock, flags);	spin_lock_irqsave(&dev->buflock, flags);	if (!dev->out_urb_finished) {		spin_unlock_irqrestore(&dev->buflock, flags);		usb_kill_urb(dev->interrupt_out_urb);	} else		spin_unlock_irqrestore(&dev->buflock, flags);exit:	dbg(2," %s : leave", __FUNCTION__);}static void adu_delete(struct adu_device *dev){	dbg(2, "%s enter", __FUNCTION__);	/* free data structures */	usb_free_urb(dev->interrupt_in_urb);	usb_free_urb(dev->interrupt_out_urb);	kfree(dev->read_buffer_primary);	kfree(dev->read_buffer_secondary);	kfree(dev->interrupt_in_buffer);	kfree(dev->interrupt_out_buffer);	kfree(dev);	dbg(2, "%s : leave", __FUNCTION__);}static void adu_interrupt_in_callback(struct urb *urb){	struct adu_device *dev = urb->context;	int status = urb->status;	dbg(4," %s : enter, status %d", __FUNCTION__, status);	adu_debug_data(5, __FUNCTION__, urb->actual_length,		       urb->transfer_buffer);	spin_lock(&dev->buflock);	if (status != 0) {		if ((status != -ENOENT) && (status != -ECONNRESET) &&			(status != -ESHUTDOWN)) {			dbg(1," %s : nonzero status received: %d",			    __FUNCTION__, status);		}		goto exit;	}	if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {		if (dev->read_buffer_length <		    (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) -		     (urb->actual_length)) {			memcpy (dev->read_buffer_primary +				dev->read_buffer_length,				dev->interrupt_in_buffer, urb->actual_length);			dev->read_buffer_length += urb->actual_length;			dbg(2," %s reading  %d ", __FUNCTION__,			    urb->actual_length);		} else {			dbg(1," %s : read_buffer overflow", __FUNCTION__);		}	}exit:	dev->read_urb_finished = 1;	spin_unlock(&dev->buflock);	/* always wake up so we recover from errors */	wake_up_interruptible(&dev->read_wait);	adu_debug_data(5, __FUNCTION__, urb->actual_length,		       urb->transfer_buffer);	dbg(4," %s : leave, status %d", __FUNCTION__, status);}static void adu_interrupt_out_callback(struct urb *urb){	struct adu_device *dev = urb->context;	int status = urb->status;	dbg(4," %s : enter, status %d", __FUNCTION__, status);	adu_debug_data(5,__FUNCTION__, urb->actual_length, urb->transfer_buffer);	if (status != 0) {		if ((status != -ENOENT) &&		    (status != -ECONNRESET)) {			dbg(1, " %s :nonzero status received: %d",			    __FUNCTION__, status);		}		goto exit;	}	spin_lock(&dev->buflock);	dev->out_urb_finished = 1;	wake_up(&dev->write_wait);	spin_unlock(&dev->buflock);exit:	adu_debug_data(5, __FUNCTION__, urb->actual_length,		       urb->transfer_buffer);	dbg(4," %s : leave, status %d", __FUNCTION__, status);}static int adu_open(struct inode *inode, struct file *file){	struct adu_device *dev = NULL;	struct usb_interface *interface;	int subminor;	int retval;	dbg(2,"%s : enter", __FUNCTION__);	subminor = iminor(inode);	if ((retval = mutex_lock_interruptible(&adutux_mutex))) {		dbg(2, "%s : mutex lock failed", __FUNCTION__);		goto exit_no_lock;	}	interface = usb_find_interface(&adu_driver, subminor);	if (!interface) {		err("%s - error, can't find device for minor %d",		    __FUNCTION__, subminor);		retval = -ENODEV;		goto exit_no_device;	}	dev = usb_get_intfdata(interface);	if (!dev || !dev->udev) {		retval = -ENODEV;		goto exit_no_device;	}	/* check that nobody else is using the device */	if (dev->open_count) {		retval = -EBUSY;		goto exit_no_device;	}	++dev->open_count;	dbg(2,"%s : open count %d", __FUNCTION__, dev->open_count);	/* save device in the file's private structure */	file->private_data = dev;	/* initialize in direction */	dev->read_buffer_length = 0;	/* fixup first read by having urb waiting for it */	usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,			 usb_rcvintpipe(dev->udev,					dev->interrupt_in_endpoint->bEndpointAddress),			 dev->interrupt_in_buffer,			 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),			 adu_interrupt_in_callback, dev,			 dev->interrupt_in_endpoint->bInterval);	dev->read_urb_finished = 0;	if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))		dev->read_urb_finished = 1;	/* we ignore failure */	/* end of fixup for first read */	/* initialize out direction */	dev->out_urb_finished = 1;	retval = 0;exit_no_device:	mutex_unlock(&adutux_mutex);exit_no_lock:	dbg(2,"%s : leave, return value %d ", __FUNCTION__, retval);	return retval;}static void adu_release_internal(struct adu_device *dev){	dbg(2," %s : enter", __FUNCTION__);	/* decrement our usage count for the device */	--dev->open_count;	dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);	if (dev->open_count <= 0) {		adu_abort_transfers(dev);		dev->open_count = 0;	}	dbg(2," %s : leave", __FUNCTION__);}static int adu_release(struct inode *inode, struct file *file){	struct adu_device *dev;	int retval = 0;	dbg(2," %s : enter", __FUNCTION__);	if (file == NULL) { 		dbg(1," %s : file is NULL", __FUNCTION__);		retval = -ENODEV;		goto exit;	}	dev = file->private_data;	if (dev == NULL) { 		dbg(1," %s : object is NULL", __FUNCTION__);		retval = -ENODEV;		goto exit;	}	mutex_lock(&adutux_mutex); /* not interruptible */	if (dev->open_count <= 0) {		dbg(1," %s : device not opened", __FUNCTION__);		retval = -ENODEV;		goto exit;	}	adu_release_internal(dev);	if (dev->udev == NULL) {		/* the device was unplugged before the file was released */		if (!dev->open_count)	/* ... and we're the last user */			adu_delete(dev);	}exit:	mutex_unlock(&adutux_mutex);	dbg(2," %s : leave, return value %d", __FUNCTION__, retval);	return retval;}static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,			loff_t *ppos){	struct adu_device *dev;	size_t bytes_read = 0;	size_t bytes_to_read = count;	int i;	int retval = 0;	int timeout = 0;	int should_submit = 0;	unsigned long flags;	DECLARE_WAITQUEUE(wait, current);	dbg(2," %s : enter, count = %Zd, file=%p", __FUNCTION__, count, file);	dev = file->private_data;	dbg(2," %s : dev=%p", __FUNCTION__, dev);	if (mutex_lock_interruptible(&dev->mtx))		return -ERESTARTSYS;	/* verify that the device wasn't unplugged */	if (dev->udev == NULL) {		retval = -ENODEV;		err("No device or device unplugged %d", retval);		goto exit;	}	/* verify that some data was requested */	if (count == 0) {		dbg(1," %s : read request of 0 bytes", __FUNCTION__);		goto exit;	}	timeout = COMMAND_TIMEOUT;	dbg(2," %s : about to start looping", __FUNCTION__);	while (bytes_to_read) {		int data_in_secondary = dev->secondary_tail - dev->secondary_head;		dbg(2," %s : while, data_in_secondary=%d, status=%d",		    __FUNCTION__, data_in_secondary,		    dev->interrupt_in_urb->status);		if (data_in_secondary) {			/* drain secondary buffer */			int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;			i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);			if (i < 0) {				retval = -EFAULT;				goto exit;			}			dev->secondary_head += (amount - i);			bytes_read += (amount - i);			bytes_to_read -= (amount - i);			if (i) {				retval = bytes_read ? bytes_read : -EFAULT;				goto exit;			}		} else {			/* we check the primary buffer */			spin_lock_irqsave (&dev->buflock, flags);			if (dev->read_buffer_length) {				/* we secure access to the primary */				char *tmp;				dbg(2," %s : swap, read_buffer_length = %d",				    __FUNCTION__, dev->read_buffer_length);				tmp = dev->read_buffer_secondary;				dev->read_buffer_secondary = dev->read_buffer_primary;

⌨️ 快捷键说明

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