Program for Linux USB-devices driver

開始啃硬骨头~ 这里我打算一步步给出USB device driver 的demo。希望有心能可以共同交流学习. 希望认识很多其它对Linux有兴趣的geek.

眼下因为环境和自身能力方面原因还没能做实物的測试,篇章的最后打算给出一个在x86上模拟USB读写的driver。以后可以做实物測试之后再更新this blog

我的联系方式:

jasonleaster@gmail.com(因为偶不能登QQ,所以thunder bird的邮件仅仅要电脑开着就一直在线~)

測试环境:

Ubuntu 14.0

kernel: 3.13.0

以下我比較具体的对代码进行了凝视,要点都在代码中啦~

这段代码不过个样例,相当于第一个驱动设备hello world一样,力求最简单化概括性的给出USB设备插入,拔出相关的处理过程,后面慢慢的写的更深入的时候。代码也就越来越”复杂“

我整理了USB device driver 代码相关的结构体。做了文件夹便于查阅

/**************************************************************
code writer : EOF
code date : 2014.08.18
code file : ./USB_1/usb_1.c
e-mail : jasonleaster@gmail.com code purpose:
This is a demo for how to code for a USB device driver. If there is something wrong with my code, please touch
me by e-mail. Thank you. *************************************************************/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/usb.h>
#include <linux/module.h>
#include <linux/moduleparam.h> /*
** This two Macro used to create a structure --
** 'usb_device_id' by 'USB_DEVICE'
*/
#define VENDOR_ID 0x08f7
#define PRODUCT_ID 0x0002 #define DEVICE_NAME "usb_1" MODULE_AUTHOR("EOF");
MODULE_LICENSE("Dual BSD/GPL"); /*
** id_table stored many USB device's 'usb_device_id' structure
*/
static struct usb_device_id id_table[] =
{
{ USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
{ },
}; /*
** The 'MODULE_DEVICE_TABLE' Macro marks device in the
** module image so that the module can be loaded on demand if
** the device is hotplugged.
*/ MODULE_DEVICE_TABLE(usb,id_table); /*
** The probe() method is invoked by khubd after device enumeration.
**
** @interface :contains information gleaned during the
** enumeration process.
**
** @id : the entry in the driver's 'usb_device_id' table
** that matches the values read from the USB-device.
**
*/
static int usb_probe(struct usb_interface * interface,
const struct usb_device_id *id)
{
dev_info(&interface->dev,"USB device now attached\n"); return 0;
} /*
** disconnect() method is called when the device is unplugged or
** when the module is unloaded.
*/
static void usb_disconnect(struct usb_interface* interface)
{
dev_info(&interface->dev,"USB device now disconnect\n");
} /*
** Each USB device has a 'usb_driver' data structure.
*/
static struct usb_driver usb_driver =
{
.name = DEVICE_NAME,
.probe = usb_probe,
.disconnect = usb_disconnect,
.id_table = id_table,
}; static int usb_init(void)
{
return usb_register(&usb_driver);
} static void usb_exit(void)
{
usb_deregister(&usb_driver);
} module_init(usb_init);
module_exit(usb_exit);

update: 2014.08.18 14:25

逐步完好

这里旨在说明

 'usb_get_dev()' and 'usb_set_intfdata'

http://blog.csdn.net/cinmyheart/article/details/38628387#t21

上面的link。按文件夹查找相关函数定义就可以

/**************************************************************
code writer : EOF
code date : 2014.08.18
code file : ./USB_2/usb_2.c
e-mail : jasonleaster@gmail.com code purpose:
This is a demo for how to code for a USB device driver.
I emphasize to demo for function 'usb_get_dev()' and 'usb_set_intfdata' If there is something wrong with my code, please touch
me by e-mail. Thank you. *************************************************************/ #include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/usb.h>
#include <linux/slab.h> #define VENDOR_ID 0x08f7
#define PRODUCT_ID 0x0002 static struct usb_device_id id_table[] =
{
{USB_DEVICE(VENDOR_ID,PRODUCT_ID)},
{ },
}; MODULE_DEVICE_TABLE(usb,id_table); struct my_usb
{
struct usb_device *udev;
int temp;
}; static int usb_probe(struct usb_interface* interface,const struct usb_device_id*id)
{
struct usb_device* udev = interface_to_usbdev(interface);
struct my_usb * my_dev; my_dev = kzalloc(sizeof(struct my_usb),GFP_KERNEL); if(!my_dev)
{
dev_err(&interface->dev,"Out of memory\n"); return -ENOMEM;
} /*
** increase the reference about probe() method
*/
my_usb->udev = usb_get_dev(udev); usb_set_intfdata(interface,my_usb); dev_info(&interface->dev,"USB device now attached\n"); return 0;
} static void usb_disconnect(struct usb_interface * interface)
{
struct my_usb *my_dev; /*
** Get 'struct my_usb' from interface and then free
** free 'my_dev->udev'.
*/
my_dev = usb_get_intfdata(interface); /*
** decrease the reference about probe() method
*/
usb_put_dev(my_dev->udev); kfree(my_dev); dev_info(&interface->dev,"USB device now disconnected]n");
} static struct usb_driver my_usb_driver =
{
.name = "usb_2",
.probe = usb_probe,
.disconnect = usb_disconnect,
.id_table = id_table,
}; static int usb_init(void)
{
return usb_register(&my_usb_driver);
} static void usb_exit(void)
{
usb_deregister(&my_usb_driver);
} MODULE_AUTHOR("EOF");
MODULE_LICENSE("Dual BSD/GPL"); module_init(usb_init);
module_exit(usb_exit);

衡山日出

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Program for Linux USB-devices driver step by step (ONE)的更多相关文章

  1. Linux USB Project

    转自:http://www.linux-usb.org/ Welcome to the home of the Linux USB Project This web site was created ...

  2. Linux usb子系统(三):通过usbfs操作设备的用户空间驱动

    内核中提供了USB设备文件系统(usbdevfs,Linux 2.6改为usbfs,即USB文件系统),它和/proc类似,都是动态产生的.通过在/etc/fstab文件中添加如下一行:none /p ...

  3. Linux usb子系统(二):USB设备驱动usb-skeleton.c

    usb驱动分为通过usbfs操作设备的用户空间驱动,内核空间的内核驱动.两者不能同时进行,否则容易引发对共享资源访问的问题,死锁!使用了内核驱动,就不能在usbfs里驱动该设备. 下面转载的一篇分析u ...

  4. Linux usb子系统(一):子系统架构

    一.USB协议基础知识   前序:USB概念概述 USB1.0版本速度1.5Mbps(低速USB) USB1.1版本速度12Mbps(全速USB)  USB2.0版本速度480Mbps(高速USB). ...

  5. Linux usb子系统(一) _写一个usb鼠标驱动

    USB总线是一种典型的热插拔的总线标准,由于其优异的性能几乎成为了当下大小设备中的标配. USB的驱动可以分为3类:SoC的USB控制器的驱动,主机端USB设备的驱动,设备上的USB Gadget驱动 ...

  6. Linux usb 驱动程序范例

                     linxu_usb驱动之框架 USB骨架程序可以被看做一个最简单的USB设备驱动的实例. 首先看看USB骨架程序的usb_driver的定义 [cpp] view p ...

  7. AT91 USB Composite Driver Implementation

    AT91 USB Composite Driver Implementation 1. Introduction The USB Composite Device is a general way t ...

  8. Linux USB驱动框架分析(2)【转】

    转自:http://blog.chinaunix.net/uid-23046336-id-3243543.html   看了http://blog.chinaunix.net/uid-11848011 ...

  9. Linux USB驱动框架分析 【转】

    转自:http://blog.chinaunix.net/uid-11848011-id-96188.html 初次接触与OS相关的设备驱动编写,感觉还挺有意思的,为了不至于忘掉看过的东西,笔记跟总结 ...

随机推荐

  1. Havel-Hakimi定理 hdu2454 / poj1695 Havel-Hakimi定理

    Havel-Hakimi定理 当年一度热门出如今ACM赛场上的算法. 算法定义: Havel-Hakimi定理主要用来判定一个给定的序列是否是可图的. 2.首先介绍一下度序列:若把图 G 全部顶点的度 ...

  2. js导出报表

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78376156 需求:项目中有一个学生签到模块需要导出每天的签到数据,一开始用poi在后 ...

  3. 【t053】整数去位

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 键盘输入一个高精度的正整数N,去掉其中任意M个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对 ...

  4. USB 3.0规范中译本 第4章 超高速数据流模型

    本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 本章展示数据和信息如何在超高速上通过的一种高层次的描述.请阅读协议层一章关于低层次协议的细节.本章提供设备架 ...

  5. 用SQL找出前N名

    业务系统中常常会有排名的需求,考试和比赛中则更普遍了.Excel 中也有个 Rank 函数供排名之用,数据库中更不例外了. 如果须要找出工资最高的前三个员工工资(及其员工号). 只是."前三 ...

  6. 枚举系统磁盘驱动器(使用GetLogicalDriveStrings API函数,system("pause"); 很实用,还用到wcslen等函数)

    代码如下: #include "stdafx.h" #include <vector> #include <string> #include <Win ...

  7. MQ选型对比RabbitMQ RocketMQ ActiveMQ

    原文:MQ选型对比RabbitMQ RocketMQ ActiveMQ 几种MQ产品说明:     ZeroMQ :  扩展性好,开发比较灵活,采用C语言实现,实际上他只是一个socket库的重新封装 ...

  8. js如何实现动态点击改变单元格颜色?

    js如何实现动态点击改变单元格颜色? 一.总结 1.通过table的rows属性,遍历表格所有行,然后通过cells属性,遍历每一行中的单元格. 2.遍历的过程中,动态的为每一个单元格定义单击事件,改 ...

  9. Linux中特别要注意用户与文件权限的问题

    1.在使用Linux中,肯定会涉及不同用户的切换,但是如果不合理切换的话,会造成很多应用启动不了,所以这时候要多多使用ll看一下文件目录的权限问题,因为如果习惯用root启动程序,然后切换普通用户继续 ...

  10. oracle常规任务

    # su - oracle  oracle> sqlplus "/as sysdba"  SQL> exec dbms_scheduler.disable('MONDA ...