📄 fs2410-usbcon.1.1.c
字号:
/* * USB FS2410usbcon driver - 1.0 * * 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, version 2. * * This driver is based on the the USB Skeleton driver in the 2.6.24.7 version of linux kenerel. * */#include <linux/kernel.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/module.h>#include <linux/kref.h>#include <asm/uaccess.h>#include <linux/usb.h>#include <linux/mutex.h>/* ioctl commands */#define IOCTL_SET_USER_INPUT (0x79)#define IOCTL_GET_ARM_OUTPUT (0x80)#define IOCTL_SEND_BULKDATA (0x79)#define IOCTL_RECV_BULKDATA (0x80)/* USB request commands in pipe0 on arm */#define PIPO0_SET_USER_INPUT (79)#define PIPO0_GET_ARM_OUTPUT_LENGTH (80)#define PIPO0_GET_ARM_OUTPUT (81)/* Define these values to match your devices */#define USB_FS2410USBCON_VENDOR_ID 0x5345#define USB_FS2410USBCON_PRODUCT_ID 0x1234struct IOCTL_DATA_IO{ size_t length; char *data;};/* table of devices that work with this driver */static struct usb_device_id fs2410usbcon_table[] = { { USB_DEVICE(USB_FS2410USBCON_VENDOR_ID, USB_FS2410USBCON_PRODUCT_ID) }, { } /* Terminating entry */};MODULE_DEVICE_TABLE(usb, fs2410usbcon_table);/* Get a minor range for your devices from the usb maintainer */#define USB_FS2410USBCON_MINOR_BASE 192/* our private defines. if this grows any larger, use your own .h file */#define MAX_TRANSFER (PAGE_SIZE - 512)/* MAX_TRANSFER is chosen so that the VM is not stressed by allocations > PAGE_SIZE and the number of packets in a page is an integer 512 is the largest possible packet on EHCI */#define WRITES_IN_FLIGHT 8/* arbitrarily chosen *//* Structure to hold all of our device specific stuff */struct usb_fs2410usbcon { struct usb_device *udev; /* the usb device for this device */ struct usb_interface *interface; /* the interface for this device */ struct semaphore limit_sem; /* limiting the number of writes in progress */ struct usb_anchor submitted; /* in case we need to retract our submissions */ unsigned char *bulk_in_buffer; /* the buffer to receive data */ size_t bulk_in_size; /* the size of the receive buffer */ __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */ int errors; /* the last request tanked */ int open_count; /* count the number of openers */ spinlock_t err_lock; /* lock for errors */ struct kref kref; struct mutex io_mutex; /* synchronize I/O with disconnect */};#define to_fs2410usbcon_dev(d) container_of(d, struct usb_fs2410usbcon, kref)static struct usb_driver fs2410usbcon_driver;static void fs2410usbcon_draw_down(struct usb_fs2410usbcon *dev);static void fs2410usbcon_delete(struct kref *kref){ struct usb_fs2410usbcon *dev = to_fs2410usbcon_dev(kref); usb_put_dev(dev->udev); kfree(dev->bulk_in_buffer); kfree(dev);}static int fs2410usbcon_open(struct inode *inode, struct file *file){ struct usb_fs2410usbcon *dev; struct usb_interface *interface; int subminor; int retval = 0; subminor = iminor(inode); interface = usb_find_interface(&fs2410usbcon_driver, subminor); if (!interface) { err ("%s - error, can't find device for minor %d", __FUNCTION__, subminor); retval = -ENODEV; goto exit; } dev = usb_get_intfdata(interface); if (!dev) { retval = -ENODEV; goto exit; } /* increment our usage count for the device */ kref_get(&dev->kref); /* lock the device to allow correctly handling errors * in resumption */ mutex_lock(&dev->io_mutex); if (!dev->open_count++) { retval = usb_autopm_get_interface(interface); if (retval) { dev->open_count--; mutex_unlock(&dev->io_mutex); kref_put(&dev->kref, fs2410usbcon_delete); goto exit; } } /* else { //uncomment this block if you want exclusive open retval = -EBUSY; dev->open_count--; mutex_unlock(&dev->io_mutex); kref_put(&dev->kref, fs2410usbcon_delete); goto exit; } */ /* prevent the device from being autosuspended */ /* save our object in the file's private structure */ file->private_data = dev; mutex_unlock(&dev->io_mutex);exit: return retval;}static int fs2410usbcon_release(struct inode *inode, struct file *file){ struct usb_fs2410usbcon *dev; dev = (struct usb_fs2410usbcon *)file->private_data; if (dev == NULL) return -ENODEV; /* allow the device to be autosuspended */ mutex_lock(&dev->io_mutex); if (!--dev->open_count && dev->interface) usb_autopm_put_interface(dev->interface); mutex_unlock(&dev->io_mutex); /* decrement the count on our device */ kref_put(&dev->kref, fs2410usbcon_delete); return 0;}static int fs2410usbcon_flush(struct file *file, fl_owner_t id){ struct usb_fs2410usbcon *dev; int res; dev = (struct usb_fs2410usbcon *)file->private_data; if (dev == NULL) return -ENODEV; /* wait for io to stop */ mutex_lock(&dev->io_mutex); fs2410usbcon_draw_down(dev); /* read out errors, leave subsequent opens a clean slate */ spin_lock_irq(&dev->err_lock); res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0; dev->errors = 0; spin_unlock_irq(&dev->err_lock); mutex_unlock(&dev->io_mutex); return res;}static ssize_t fs2410usbcon_read2(struct file *filp, char *buffer, size_t count, loff_t * ppos){ struct usb_fs2410usbcon *dev = (struct usb_fs2410usbcon *)filp->private_data; char *buf = NULL; size_t iosize; int retval; unsigned char ucLen[4]; int len; /* how many bytes can be readed out in the device ? */ if (count < 0) return -EFAULT; if (count == 0) return 0; iosize = min(count, (size_t)MAX_TRANSFER); mutex_lock(&dev->io_mutex); if (!dev->interface) /* disconnect() was called */ { retval = -ENODEV; goto L_READ_EXIT; } /* get the device data length */ retval = usb_control_msg( dev->udev, usb_rcvctrlpipe(dev->udev, 0), PIPO0_GET_ARM_OUTPUT_LENGTH, USB_DIR_IN | USB_TYPE_VENDOR, 0, 0, ucLen, 4, 10000); if (retval < 0) { err("%s - failed controling urb to get buffer lenth, error %d", __FUNCTION__, retval); retval = -EFAULT; goto L_READ_EXIT; } len = ((int)ucLen[0] << 24) + ((int)ucLen[1] << 16) + ((int)ucLen[2] << 8) + (int)ucLen[3]; if (iosize > len) iosize = len; /* allocate memory */ buf = kzalloc(iosize, GFP_KERNEL); if (!buf) { err("Out of memory"); retval = -ENOMEM; goto L_READ_EXIT; } /* get the device data */ retval = usb_control_msg( dev->udev, usb_rcvctrlpipe(dev->udev, 0), PIPO0_GET_ARM_OUTPUT, USB_DIR_IN | USB_TYPE_VENDOR, 0, 0, buf, iosize, 10000); if (retval < 0) { err("%s - failed controling urb to get buffer, error %d", __FUNCTION__, retval); retval = -EFAULT; goto L_READ_EXIT; } if (copy_to_user((void *)buffer, buf, iosize)) { retval = -EFAULT; goto L_READ_EXIT; }L_READ_EXIT: if (buf) { kfree(buf); } mutex_unlock(&dev->io_mutex); return retval;}static ssize_t fs2410usbcon_read(struct file *file, char *buffer, size_t count, loff_t *ppos){ struct usb_fs2410usbcon *dev; int retval; int bytes_read; dev = (struct usb_fs2410usbcon *)file->private_data; mutex_lock(&dev->io_mutex); if (!dev->interface) { /* disconnect() was called */ retval = -ENODEV; goto exit; } /* do a blocking bulk read to get data from the device */ retval = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr), dev->bulk_in_buffer, min(dev->bulk_in_size, count), &bytes_read, 10000); /* if the read was successful, copy the data to userspace */ if (!retval) { if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read)) retval = -EFAULT; else retval = bytes_read; }exit: mutex_unlock(&dev->io_mutex); return retval;}static ssize_t fs2410usbcon_write2(struct file *filp, const char *user_buffer, size_t count, loff_t *ppos){ struct usb_fs2410usbcon *dev = (struct usb_fs2410usbcon *)filp->private_data; char *buf = NULL; size_t iosize; int retval; if (count < 0) return -EFAULT; if (count == 0) return 0; iosize = min(count, (size_t)MAX_TRANSFER); mutex_lock(&dev->io_mutex); if (!dev->interface) /* disconnect() was called */ { retval = -ENODEV; goto L_WRITE_EXIT; } /* allocate memory */ buf = kzalloc(iosize, GFP_KERNEL); if (!buf) { err("Out of memory"); retval = -ENOMEM; goto L_WRITE_EXIT; } if (copy_from_user(buf, user_buffer, iosize)) { retval = -EFAULT; goto L_WRITE_EXIT; } /* send the data out */ retval = usb_control_msg( dev->udev, usb_sndctrlpipe(dev->udev, 0), PIPO0_SET_USER_INPUT, USB_DIR_OUT | USB_TYPE_VENDOR, 0, 0, buf, iosize, 10000); if (retval < 0) { err("%s - failed controling urb to get buffer, error %d", __FUNCTION__, retval); retval = -EFAULT; goto L_WRITE_EXIT; }L_WRITE_EXIT: if (buf) { kfree(buf); } mutex_unlock(&dev->io_mutex); return retval;}static void fs2410usbcon_write_bulk_callback(struct urb *urb){ struct usb_fs2410usbcon *dev; dev = (struct usb_fs2410usbcon *)urb->context; /* sync/async unlink faults aren't errors */ if (urb->status) { if(!(urb->status == -ENOENT || urb->status == -ECONNRESET || urb->status == -ESHUTDOWN)) err("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status); spin_lock(&dev->err_lock); dev->errors = urb->status; spin_unlock(&dev->err_lock); } /* free up our allocated buffer */ usb_buffer_free(urb->dev, urb->transfer_buffer_length, urb->transfer_buffer, urb->transfer_dma); up(&dev->limit_sem);}static ssize_t fs2410usbcon_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos){ struct usb_fs2410usbcon *dev; int retval = 0; struct urb *urb = NULL; char *buf = NULL; size_t writesize = min(count, (size_t)MAX_TRANSFER); dev = (struct usb_fs2410usbcon *)file->private_data; /* verify that we actually have some data to write */ if (count == 0) goto exit; /* limit the number of URBs in flight to stop a user from using up all RAM */ if (down_interruptible(&dev->limit_sem)) { retval = -ERESTARTSYS; goto exit; } spin_lock_irq(&dev->err_lock); if ((retval = dev->errors) < 0) { /* any error is reported once */ dev->errors = 0; /* to preserve notifications about reset */ retval = (retval == -EPIPE) ? retval : -EIO; } spin_unlock_irq(&dev->err_lock); if (retval < 0) goto error; /* create a urb, and a buffer for it, and copy the data to the urb */ urb = usb_alloc_urb(0, GFP_KERNEL); if (!urb) { retval = -ENOMEM; goto error; } buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma); if (!buf) { retval = -ENOMEM; goto error; } if (copy_from_user(buf, user_buffer, writesize)) { retval = -EFAULT; goto error; } /* this lock makes sure we don't submit URBs to gone devices */ mutex_lock(&dev->io_mutex); if (!dev->interface) { /* disconnect() was called */ mutex_unlock(&dev->io_mutex); retval = -ENODEV; goto error; } /* initialize the urb properly */ usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), buf, writesize, fs2410usbcon_write_bulk_callback, dev); urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; usb_anchor_urb(urb, &dev->submitted); /* send the data out the bulk port */ retval = usb_submit_urb(urb, GFP_KERNEL); mutex_unlock(&dev->io_mutex); if (retval) { err("%s - failed submitting write urb, error %d", __FUNCTION__, retval); goto error_unanchor; } /* release our reference to this urb, the USB core will eventually free it entirely */ usb_free_urb(urb); return writesize;error_unanchor: usb_unanchor_urb(urb);error: if (urb) { usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma); usb_free_urb(urb); } up(&dev->limit_sem);exit: return retval;}int fs2410usbcon_ioctl2(struct inode * inode_, struct file * filp, unsigned int cmd, unsigned long arg){ struct usb_fs2410usbcon *dev = (struct usb_fs2410usbcon *)filp->private_data; int retval = 0; char *buf = NULL; size_t iosize = 0; unsigned char ucLen[4]; int len; /* how many bytes can be readed out in the device ? */ struct IOCTL_DATA_IO ctlData; switch (cmd) { case IOCTL_SEND_BULKDATA: { if (copy_from_user(&ctlData, (const void *)arg, sizeof(struct IOCTL_DATA_IO))) { retval = -EFAULT; return -1; } if (ctlData.length > 0) { iosize = min(ctlData.length, (size_t)MAX_TRANSFER); /* limit the number of URBs in flight to stop a user from using up all RAM */ if (down_interruptible(&dev->limit_sem)) { retval = -ERESTARTSYS; goto L_SEND_BULKDATA_ERR; } spin_lock_irq(&dev->err_lock); if ((retval = dev->errors) < 0) { /* any error is reported once */ dev->errors = 0; /* to preserve notifications about reset */ retval = (retval == -EPIPE) ? retval : -EIO; } spin_unlock_irq(&dev->err_lock); if (retval < 0) goto L_SEND_BULKDATA_ERR; /* allocate memory */ buf = kzalloc(iosize, GFP_KERNEL); if (!buf) { err("Out of memory"); retval = -ENOMEM; goto L_SEND_BULKDATA_ERR; } if (copy_from_user(buf, ctlData.data, iosize)) { retval = -EFAULT; goto L_SEND_BULKDATA_ERR; } /* this lock makes sure we don't submit URBs to gone devices */ mutex_lock(&dev->io_mutex); if (!dev->interface) {/* disconnect() was called */ mutex_unlock(&dev->io_mutex); retval = -ENODEV; goto L_SEND_BULKDATA_ERR; } /* send the data out */ retval = usb_control_msg( dev->udev, usb_sndctrlpipe(dev->udev, 0), PIPO0_SET_USER_INPUT, USB_DIR_OUT | USB_TYPE_VENDOR, 0, 0, buf, iosize, 10000); mutex_unlock(&dev->io_mutex); if (retval < 0) { err("%s - failed controling urb, error %d", __FUNCTION__, retval); goto L_SEND_BULKDATA_ERR; } } ctlData.length = iosize; if (copy_to_user((void *)arg, &ctlData, sizeof(struct IOCTL_DATA_IO))) { retval = -EFAULT; goto L_SEND_BULKDATA_ERR; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -