|
楼主 |
发表于 2005-1-25 20:23:15
|
显示全部楼层
程序如下
#define MODULE
#define __NO_VERSION__
#define __KERNEL__
#include <linux/version.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
#define DRIVER_VERSION "MYUSB Driver"
#define USB_MINOR 16
#define USB_VENDOR_ID 0x1043
#define USB_PRODUCT_ID 0x8006
#define NAK_TIMEOUT (10*HZ)
#define IBUF_SIZE 0x1000
#define OBUF_SIZE 0x10000
struct my_usb_data {
struct usb_device *my_dev;
unsigned int ifnum;
int isopen;
int present;
char *obuf, *ibuf;
wait_queue_head_t wait_q;
};
static struct my_usb_data my_instance;
static int open_my(struct inode *inode, struct file *file)
{
struct my_usb_data *my = &my_instance;
if (my->isopen || !my->present) {
return -EBUSY;
}
my->isopen = 1;
init_waitqueue_head(&my->wait_q);
info("MYUSB opened.");
return 0;
}
static int close_my(struct inode *inode, struct file *file)
{
struct my_usb_data *my = &my_instance;
my->isopen = 0;
info("MYUSB closed.");
return 0;
}
static ssize_t
write_my(struct file *file, const char *buffer,
size_t count, loff_t * ppos)
{
struct my_usb_data *my = &my_instance;
unsigned long copy_size;
unsigned long bytes_written = 0;
unsigned int partial;
int result = 0;
int maxretry;
if (my == NULL || my->present == 0 || my->my_dev == NULL)
return -1;
do {
unsigned long thistime;
char *obuf = my->obuf;
thistime = copy_size =
(count >= OBUF_SIZE) ? OBUF_SIZE : count;
if (copy_from_user(my->obuf, buffer, copy_size))
return -EFAULT;
maxretry = 5;
while (thistime) {
if (!my->my_dev)
return -ENODEV;
if (signal_pending(current)) {
return bytes_written ? bytes_written : -EINTR;
}
result = usb_bulk_msg(my->my_dev,
usb_sndbulkpipe(my->my_dev, 0),
obuf, thistime, &partial, 10 * HZ);
if (result == USB_ST_TIMEOUT) {
if (!maxretry--) {
return -ETIME;
}
interruptible_sleep_on_timeout(&my-> wait_q, NAK_TIMEOUT);
continue;
} else if (!result & partial) {
obuf += partial;
thistime -= partial;
} else
break;
};
if (result) {
err("Write Whoops - %x", result);
return -EIO;
}
bytes_written += copy_size;
count -= copy_size;
buffer += copy_size;
} while (count > 0);
return bytes_written ? bytes_written : -EIO;
}
static ssize_t
read_my(struct file *file, char *buffer, size_t count, loff_t * ppos)
{
struct my_usb_data *my = &my_instance;
ssize_t read_count;
unsigned int partial;
int this_read;
int result;
int maxretry = 10;
char *ibuf = my->ibuf;
if (my == NULL || my->present == 0 || my->my_dev == NULL)
return -1;
read_count = 0;
while (count > 0) {
if (signal_pending(current)) {
return read_count ? read_count : -EINTR;
}
if (!my->my_dev)
return -ENODEV;
this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
result = usb_bulk_msg(my->my_dev,
usb_rcvbulkpipe(my->my_dev, 1),
ibuf, this_read, &partial,
(int) (HZ * 8));
if (partial) {
count = this_read = partial;
} else if (result == USB_ST_TIMEOUT || result == 15) {
if (!maxretry--) {
err("read_lcd: maxretry timeout");
return -ETIME;
}
interruptible_sleep_on_timeout(&my->wait_q,
NAK_TIMEOUT);
continue;
} else if (result != USB_ST_DATAUNDERRUN) {
err("Read Whoops - result:%u partial:%u this_read:%u",
result, partial, this_read);
return -EIO;
} else {
return (0);
}
if (this_read) {
if (copy_to_user(buffer, ibuf, this_read))
return -EFAULT;
count -= this_read;
read_count += this_read;
buffer += this_read;
}
}
return read_count;
}
static void *probe_my(struct usb_device *dev, unsigned int ifnum)
{
struct my_usb_data *my = &my_instance;
int i;
if (dev->descriptor.idProduct != 0x8006||dev->descriptor.idVendor!=0x1043 ) {
warn(KERN_INFO "MYUSB model not supported.");
return NULL;
}
if (my->present == 1) {
warn(KERN_INFO "Multiple MYUSBs are not supported!");
return NULL;
}
my->present = 1;
my->my_dev = dev;
if (!(my->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
err("probe_lcd: Not enough memory for the output buffer");
return NULL;
}
if (!(my->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
err("probe_my: Not enough memory for the input buffer");
kfree(my->obuf);
return NULL;
}
my_instance=*my;
return my;
}
static void disconnect_my(struct usb_device *dev, void *ptr)
{
struct my_usb_data *my = (struct my_usb_data *) ptr;
if (my->isopen) {
my->isopen = 0;
my->my_dev = NULL;
return;
}
kfree(my->ibuf);
kfree(my->obuf);
info("MYUSB disconnected.");
my->present = 0;
}
static struct usb_device_id id_table [] = {
{USB_DEVICE(USB_VENDOR_ID, USB_PRODUCT_ID) },
{},
};
MODULE_DEVICE_TABLE (usb, id_table);
static struct
file_operations usb_my_fops = {
read: read_my,
write: write_my,
open: open_my,
release: close_my,
};
static struct
usb_driver my_driver = {
name: "myusb",
probe: (void *)probe_my,
disconnect: disconnect_my,
fops: &usb_my_fops,
minor: USB_MINOR,
};
int usb_my_init(void)
{
if (usb_register(&my_driver) < 0)
return -1;
info("MYUSB support registered.");
return 0;
}
void usb_my_cleanup(void)
{
struct my_usb_data *my = &my_instance;
my->present = 0;
usb_deregister(&my_driver);
}
module_init(usb_my_init);
module_exit(usb_my_cleanup); |
|