Chinaunix首页 | 论坛 | 博客
  • 博客访问: 479454
  • 博文数量: 100
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-21 09:30
文章分类

全部博文(100)

文章存档

2017年(1)

2016年(16)

2015年(83)

我的朋友

分类: 嵌入式

2016-01-17 21:07:59

驱动层:
注意一定要用ubuntu原始版本的gcc编译,因为内核符号是使用原始gcc建立的,比如ubuntu 12.04的gcc为gcc4.6。 编译前gcc -v确认一下
Makefile

  1. ifneq ($(KERNELRELEASE),)
  2.  
  3. obj-m := secbulk.o
  4.  
  5. else

  6. KERNELDIR ?=/lib/modules/$(shell uname -r)/build
  7.  
  8. PWD := $(shell pwd)
  9.  
  10. modules:
  11.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
  12.  
  13. endif

  14. clean:
  15.     rm -rf *.o *.order *.depend .*.cmd *.ko *.mod.c .tmp_versions *.symvers

secbulk.c

点击(此处)折叠或打开

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/usb.h>
  4. #include <linux/fs.h>
  5. #include <linux/mutex.h>
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>

  8. #define SECBULK_MAJOR    102
  9. #define SECBULK_MINOR    0
  10. #define DRIVER_NAME    "secbulk"

  11. #define BULKOUT_BUFFER_SIZE    512

  12. struct secbulk_dev
  13. {
  14.     struct usb_device *udev;
  15.     struct mutex io_mutex;
  16.     char*    bulkout_buffer;
  17.     unsigned char    bulk_out_endpointAddr;
  18. };

  19. static struct usb_class_driver secbulk_class;

  20. static struct usb_device_id secbulk_table[]= {
  21.     { USB_DEVICE(0x04e8, 0x1234)},
  22.     { }
  23. };

  24. static struct usb_driver secbulk_driver;
  25. static void secbulk_disconnect(struct usb_interface *interface)
  26. {
  27.     struct secbulk_dev *dev = NULL;
  28.     printk(KERN_INFO "secbulk:secbulk disconnected!\n");
  29.     dev = usb_get_intfdata(interface);
  30.     if( NULL != dev )
  31.         kfree(dev);
  32.     usb_deregister_dev(interface, &secbulk_class);
  33.     return;
  34. }

  35. static ssize_t secbulk_read(struct file *file, char __user *buf, size_t len, loff_t *loff)
  36. {
  37.     return -EPERM;
  38. }

  39. static ssize_t secbulk_write(struct file *file, const char __user *buf, size_t len, loff_t *loff)
  40. {
  41.     size_t to_write;
  42.     struct secbulk_dev *dev = file->private_data;
  43.     int ret;
  44.     int actual_length;
  45.     size_t total_writed;
  46.     
  47.     total_writed = 0;
  48.     while(len > 0)
  49.     {
  50.         to_write = min(len, BULKOUT_BUFFER_SIZE);
  51.     
  52.         if(copy_from_user(dev->bulkout_buffer, buf+total_writed, to_write))
  53.         {
  54.             printk(KERN_ERR "secbulk:copy_from_user failed!\n");
  55.             return -EFAULT;    
  56.         }    

  57.         ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
  58.                 dev->bulkout_buffer,
  59.                 to_write,
  60.                 &actual_length,
  61.                 3*HZ);
  62.         if(ret || actual_length!=to_write)
  63.         {    
  64.             printk(KERN_ERR "secbulk:usb_bulk_msg failed!\n");
  65.             return -EFAULT;
  66.         }    
  67.         len -= to_write;
  68.         total_writed += to_write;
  69.     }    
  70.     return total_writed;
  71. }

  72. static int secbulk_open(struct inode *node, struct file *file)
  73. {
  74.     struct usb_interface *interface;
  75.     struct secbulk_dev *dev;
  76.     
  77.     interface = usb_find_interface(&secbulk_driver, iminor(node));
  78.     if(!interface)
  79.         return -ENODEV;

  80.     dev = usb_get_intfdata(interface);
  81.     dev->bulkout_buffer = kzalloc(BULKOUT_BUFFER_SIZE, GFP_KERNEL);
  82.     if(!(dev->bulkout_buffer))
  83.         return -ENOMEM;
  84.     if(!mutex_trylock(&dev->io_mutex))
  85.         return -EBUSY;
  86.     file->private_data = dev;
  87.     return 0;
  88. }

  89. static int secbulk_release(struct inode *node, struct file *file)
  90. {
  91.     struct secbulk_dev *dev;

  92.     dev = (struct secbulk_dev*)(file->private_data);
  93.     kfree(dev->bulkout_buffer);
  94.     mutex_unlock(&dev->io_mutex);
  95.     return 0;
  96. }

  97. static struct file_operations secbulk_fops = {
  98.     .owner     =    THIS_MODULE,
  99.     .read     =    secbulk_read,
  100.     .write    =    secbulk_write,
  101.     .open =    secbulk_open,
  102.     .release=    secbulk_release,
  103. };

  104. static struct usb_class_driver secbulk_class = {
  105.     .name =     "secbulk%d",
  106.     .fops =        &secbulk_fops,
  107.     .minor_base=    100,
  108. };

  109. static int secbulk_probe(struct usb_interface *interface, const struct usb_device_id *id)
  110. {
  111.     int ret;
  112.     struct secbulk_dev *dev;
  113.     struct usb_host_interface *iface_desc;
  114.     struct usb_endpoint_descriptor *endpoint;
  115.     int i;

  116.     printk(KERN_INFO "secbulk:secbulk probing...\n");
  117.     
  118.     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  119.     if(!dev)
  120.     {
  121.         ret = -ENOMEM;
  122.         goto error;
  123.     }    

  124.     iface_desc = interface->cur_altsetting;
  125.     for(i=0; i < iface_desc->desc.bNumEndpoints; i++)
  126.     {
  127.         endpoint = &(iface_desc->endpoint[i].desc);
  128.         if(!dev->bulk_out_endpointAddr
  129.         && usb_endpoint_is_bulk_out(endpoint))
  130.         {
  131.             printk(KERN_INFO "secbulk:bulk out endpoint found!\n");
  132.             dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
  133.             break;
  134.         }
  135.     }
  136.     
  137.     if(!(dev->bulk_out_endpointAddr))
  138.     {
  139.         ret = -EBUSY;
  140.         goto error;
  141.     }

  142.     ret = usb_register_dev(interface, &secbulk_class);
  143.     if(ret)
  144.     {
  145.         printk(KERN_ERR "secbulk: usb_register_dev failed!\n");
  146.         return ret;
  147.     }        
  148.     dev->udev = usb_get_dev(interface_to_usbdev(interface));

  149.     usb_set_intfdata(interface, dev);
  150.     
  151.     mutex_init(&dev->io_mutex);
  152.     return 0;
  153. error:
  154.     if(!dev)
  155.         kfree(dev);
  156.     return ret;
  157. }

  158. static struct usb_driver secbulk_driver= {
  159.     .name=        "secbulk",
  160.     .probe=        secbulk_probe,
  161.     .disconnect=    secbulk_disconnect,
  162.     .id_table=    secbulk_table,
  163.     .supports_autosuspend=0,
  164. };
  165. static int __init secbulk_init(void)
  166. {
  167.     int result;
  168.     printk(KERN_INFO "secbulk:secbulk loaded\n");
  169.     result = usb_register(&secbulk_driver);
  170.     if(result)
  171.     {    printk(KERN_ERR "secbulk:usb_register failed: %d", result);
  172.         return result;
  173.     }        
  174.     return 0;
  175. }

  176. static void __exit secbulk_exit(void)
  177. {
  178.     usb_deregister(&secbulk_driver);
  179.     printk(KERN_INFO "secbulk:secbulk unloaded\n");
  180. }

  181. module_init(secbulk_init);
  182. module_exit(secbulk_exit);
  183. MODULE_LICENSE("GPL");


应用层:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>

  8. const char* dev = "/dev/secbulk0";


  9. int main(int argc, char* argv[])
  10. {
  11.     unsigned char* file_buffer = NULL;
  12.     if( 2 != argc )
  13.     {
  14.         printf("Usage: dwn \n");
  15.         return 1;
  16.     }

  17.     int fd = open(argv[1], O_RDONLY);
  18.     if(-1 == fd)
  19.     {
  20.         printf("Can not open file - %s\n", argv[1]);
  21.         return 1;
  22.     }

  23.     struct stat file_stat;
  24.     if( -1 == fstat(fd, &file_stat) )
  25.     {
  26.         printf("Get file size filed!\n");
  27.         return 1;
  28.     }    
  29.     
  30.     file_buffer = (char*)malloc(file_stat.st_size+10);
  31.     if(NULL == file_buffer)
  32.     {
  33.         printf("malloc failed!\n");
  34.         goto error;
  35.     }
  36.     if( file_stat.st_size != read(fd, file_buffer+8, file_stat.st_size))
  37.     {
  38.         printf("Read file failed!\n");
  39.         goto error;
  40.     }

  41.     printf("file name : %s\n", argv[1]);
  42.     printf("file size : %d bytes\n", file_stat.st_size);

  43.     int fd_dev = open(dev, O_WRONLY);
  44.     if( -1 == fd_dev)
  45.     {
  46.         printf("Can not open %s\n", dev);
  47.         goto error;
  48.     }
  49.     
  50.     *((unsigned long*)file_buffer) = 0x32000000;     //load address
  51.     *((unsigned long*)file_buffer+1) = file_stat.st_size+10;    //file size
  52.     unsigned short sum = 0;
  53.     int i;
  54.     for(i=8; i<file_stat.st_size+8; i++)
  55.     {
  56.         sum += file_buffer[i];
  57.     }
  58.     /*****************************/
  59.     file_buffer [file_stat.st_size + 8] = sum & 0xff;
  60.     file_buffer [file_stat.st_size + 9] = sum >> 8;
  61.     /*****************************/
  62.     printf("Writing data...\n");
  63.     size_t remain_size = file_stat.st_size+10;
  64.     size_t block_size = remain_size / 100;
  65.     size_t writed = 0;
  66.     while(remain_size>0)
  67.     {
  68.         size_t to_write = remain_size > block_size ? block_size:remain_size;
  69.         if( to_write != write(fd_dev, file_buffer+writed, to_write))
  70.         {
  71.             printf("failed!\n");
  72.             return 1;
  73.         }
  74.         remain_size -= to_write;
  75.         writed += to_write;
  76.         printf("\r%d%\t %d bytes ", writed*100/(file_stat.st_size+10), writed);
  77.         fflush(stdout);
  78.         
  79.     }    

  80.     printf("OK\n");
  81.     return 0;

  82. error:
  83.     if(-1!=fd_dev) close(fd_dev);
  84.     if(fd != -1) close(fd);
  85.     if( NULL!=file_buffer )
  86.         free(file_buffer);
  87.     return -1;
  88. }

参考:
http://www.cnblogs.com/qiaoge/archive/2012/03/29/2423776.html
http://blog.csdn.net/david_xtd/article/details/26088299

阅读(2363) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~