arm+linux+usb驱动开发,linux 2.6下编译usb驱动和arm板进行数据通信

linux 2.6下编译usb驱动和arm板进行数据通信

文件原型drivers/usb/misc/usblcd.c

#include

#include

#include

#include

#include

#include

#include

#define DRIVER_VERSION "USBLCD Driver Version 1.05"

#define USBLCD_MINOR        144

#define IOCTL_GET_HARD_VERSION    1

#define IOCTL_GET_DRV_VERSION    2

static struct usb_device_id id_table [] = {

{ .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },

{ .idVendor = 0x8086, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },//gliethttp_20080527添加自己的vid

{ .idVendor = 0x1286, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },//gliethttp_20080527添加自己的vid

{ },

};

MODULE_DEVICE_TABLE (usb, id_table);

struct usb_lcd {

struct usb_device *    udev;            /* init: probe_lcd */

struct usb_interface * interface;        /* the interface for this device */

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 */

struct kref        kref;

struct semaphore    limit_sem;        /* to stop writes at full throttle from

* using up all RAM */

};

#define to_lcd_dev(d) container_of(d, struct usb_lcd, kref)

#define USB_LCD_CONCURRENT_WRITES    1 //gliethttp_20080527同时允许1个task控制写函数,其他task将阻塞

static struct usb_driver lcd_driver;

static DEFINE_MUTEX(usb_lcd_open_mutex);

static void lcd_delete(struct kref *kref)

{

struct usb_lcd *dev = to_lcd_dev(kref);

usb_put_dev(dev->udev);

kfree (dev->bulk_in_buffer);

kfree (dev);

}

static int lcd_open(struct inode *inode, struct file *file)

{

struct usb_lcd *dev;

struct usb_interface *interface;

int subminor;

int retval = 0;

subminor = iminor(inode);

mutex_lock(&usb_lcd_open_mutex);

interface = usb_find_interface(&lcd_driver, subminor);

if (!interface) {

err ("USBLCD: %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);

/* save our object in the file's private structure */

file->private_data = dev;

exit:

mutex_unlock(&usb_lcd_open_mutex);

return retval;

}

static int lcd_release(struct inode *inode, struct file *file)

{

struct usb_lcd *dev;

dev = (struct usb_lcd *)file->private_data;

if (dev == NULL)

return -ENODEV;

/* decrement the count on our device */

kref_put(&dev->kref, lcd_delete);

return 0;

}

static ssize_t lcd_read(struct file *file, char __user * buffer, size_t count, loff_t *ppos)

{

struct usb_lcd *dev;

int retval = 0;

int bytes_read;

dev = (struct usb_lcd *)file->private_data;

/* 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, 30*60*1000);//gliethttp_20080527 修改超时时间

/* 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;

}

return retval;

}

static int lcd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)

{

struct usb_lcd *dev;

u16 bcdDevice;

char buf[30];

dev = (struct usb_lcd *)file->private_data;

if (dev == NULL)

return -ENODEV;

switch (cmd) {

case IOCTL_GET_HARD_VERSION:

bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);

sprintf(buf,"%1d%1d.%1d%1d",

(bcdDevice & 0xF000)>>12,

(bcdDevice & 0xF00)>>8,

(bcdDevice & 0xF0)>>4,

(bcdDevice & 0xF));

if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)

return -EFAULT;

break;

case IOCTL_GET_DRV_VERSION:

sprintf(buf,DRIVER_VERSION);

if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)

return -EFAULT;

break;

default:

return -ENOTTY;

break;

}

return 0;

}

static void lcd_write_bulk_callback(struct urb *urb)

{

struct usb_lcd *dev;

dev = (struct usb_lcd *)urb->context;

/* sync/async unlink faults aren't errors */

if (urb->status &&

!(urb->status == -ENOENT ||

urb->status == -ECONNRESET ||

urb->status == -ESHUTDOWN)) {

dbg("USBLCD: %s - nonzero write bulk status received: %d",

__FUNCTION__, urb->status);

}

/* 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 lcd_write(struct file *file, const char __user * user_buffer, size_t count, loff_t *ppos)

{

struct usb_lcd *dev;

int retval = 0, r;

struct urb *urb = NULL;

char *buf = NULL;

dev = (struct usb_lcd *)file->private_data;

/* verify that we actually have some data to write */

if (count == 0)

goto exit;

r = down_interruptible(&dev->limit_sem);

if (r < 0)

return -EINTR;

/* 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 err_no_buf;

}

buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);

if (!buf) {

retval = -ENOMEM;

goto error;

}

if (copy_from_user(buf, user_buffer, count)) {

retval = -EFAULT;

goto error;

}

/* initialize the urb properly */

usb_fill_bulk_urb(urb, dev->udev,

usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),

buf, count, lcd_write_bulk_callback, dev);

urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

/* send the data out the bulk port */

retval = usb_submit_urb(urb, GFP_KERNEL);

if (retval) {

err("USBLCD: %s - failed submitting write urb, error %d", __FUNCTION__, retval);

goto error;

}

/* release our reference to this urb, the USB core will eventually free it entirely */

usb_free_urb(urb);

r = down_interruptible(&dev->limit_sem);//等待,直到数据完全发送完毕

up(&dev->limit_sem);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值