making a linux usb driver

http://www.kroah.com/linux/

http://matthias.vallentin.net/blog/2007/04/writing-a-linux-kernel-driver-for-an-unknown-usb-device/

http://www.linuxjournal.com/article/7353?page=0,2

sudo lsusb -v -d 16c0:05df                             可以查看某个usb device的所有描述信息,包括configure, interface, endpoint信息

http://stackoverflow.com/questions/5973425/writing-usb-device-driver-in-linux 

libusb is useful and easy to get up and running. I would suggest that you start there, especially if you haven't written Linux drivers in a while. Use libusb to understand what the signalling protocol is for the Roland GR-55 and do some experiments.

USB supports several types of logical connections over the same physical wire. There will likely be DATA and CONTROL pipes available from the device and you will need to map that out before starting on a proper driver.

As I said, libusb is easy to get going and you could have something useful in a few days if you just want to write a control interface and store raw data from the device. However, ALSA is the way to go if you want to use the device with existing music software. Also, by updating ALSA you would be supporting the larger community because you could merge your work in to the ALSA project.

Writing a kernel module from scratch could be fun, but USB is a bit of a beast and would probably not be an ideal one to start with. With ALSA you will have a framework to guide you and won't need to worry so much about defining your own APIs.

====

As commented above, check out Linux Device Drivers http://lwn.net/Kernel/LDD3/ chapter 13 talks specifically about USB drivers. For development it's easier to write your driver as a kernel module because then you don't need to recompile the kernel when you make a change.

====

Since you probably want to hear sound on your sound card, you should opt for ALSA. That way after you are done you are done. If you write libusb driver, then you would have to write your own user space tools for feeding that sound card sound for playing.

====

No need for writing a new driver - ALSA already has support for that since a while. See commit https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=0ef283247a0cf0fd2e8370ee467030292eb3129e

简单的说,libusb是通用的方便usb传输数据的c开源库; 而 alsa 是解决linux音频而开发的开源库。

OpenUSB 是一个C语言开发的平台无关的USB设备访问接口库,基于 libusb 项目开发,#可能#更好用,但实际选择需要自己确定,下面是对比:

http://www.libusb.org/wiki/libusb-1.0

OpenUSB

OpenUSB is a fork of a never-released development branch of libusb, confusingly also named libusb-1.0.

OpenUSB's Advantages compared to libusb-1.0

  • OpenUSB is working on Solaris whereas libusb-1.0 does not support Solaris now. Take note: both libusb-1.0 and OpenUSB have support of Linux and Mac OS X.
  • Support hotplug (using HAL and DBUS under Linux)

OpenUSB's Disadvantages compared to libusb-1.0

  • Does not expose pollable file descriptors (and it would not be realistic to offer this functionality without a lot of rework)
  • Creates a number of internal threads for each process that uses it
  • In Daniel Drake's opinion, more complex than it needs to be (largely due to the complexity of threading)
  • No work is being done yet for Windows support

libusb for android:   https://github.com/libusb/libusb/blob/master/android/README,       https://github.com/OpenNI/OpenNI2/tree/master/ThirdParty/PSCommon/XnLib/ThirdParty/libusb-1.0.9-Android

winusb for windows:      WinUSB

The Advanced Linux Sound Architecture (ALSA) provides audio and MIDI functionality to the Linux operating system. ALSA has the following significant features:

  • Efficient support for all types of audio interfaces, from consumer sound cards to professional multichannel audio interfaces.
  • Fully modularized sound drivers.
  • SMP and thread-safe design (PLEASE READ THIS).
  • User space library (alsa-lib) to simplify application programming and provide higher level functionality.
  • Support for the older Open Sound System (OSS) API, providing binary compatibility for most OSS programs.

================

以下是libusb简单使用示例:

http://www.dreamincode.net/forums/topic/148707-introduction-to-using-libusb-10/

Introduction to using LibUSB-1.0

Bulk Transfer
To do a bulk transfer to your device, you have to have a device handler
for your usb device, and you have to know which endpoint to use (get
from device specs above).
Refer Here for information on the syntax.

Now, here's an example to combine all the things I mentioned above:

#include <stdio.h>
#include <stdlib.h>
#include <libusb.h> int main() {
libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
libusb_device_handle *dev_handle; //a device handle
libusb_context *ctx = NULL; //a libusb session
int r; //for return values
ssize_t cnt; //holding number of devices in list
r = libusb_init(&ctx); //initialize the library for the session we just declared
if(r < ) {
printf("Init Error\n");
return ;
}
libusb_set_debug(ctx, ); //set verbosity level to 3, as suggested in the documentation cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
if(cnt < ) {
printf("Get Device Error\n"); //there was an error
return ;
}
printf("%d Devices in list\n", cnt); dev_handle = libusb_open_device_with_vid_pid(ctx, 0x046d, 0xc05a); //these are vendorID and productID I found for my usb device
if(dev_handle == NULL) {
printf("Cannot open device, exit ...\n");
return ;
}
else
printf("Device Opened\n");
libusb_free_device_list(devs, ); //free the list, unref the devices in it unsigned char data[];
data[]='a';data[]='b';data[]='c';data[]='d'; //some dummy values int actual; //used to find out how many bytes were written
if(libusb_kernel_driver_active(dev_handle, ) == ) { //find out if kernel driver is attached
printf("Kernel Driver Active\n");
if(libusb_detach_kernel_driver(dev_handle, ) == ) //detach it
printf("Kernel Driver Detached!\n");
}
r = libusb_claim_interface(dev_handle, ); //claim interface 0 (the first) of device (mine had jsut 1)
if(r < ) {
printf("Cannot Claim Interface\n");
return ;
}
printf("Claimed Interface\n"); //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129,
r = libusb_bulk_transfer(dev_handle, ( | LIBUSB_ENDPOINT_OUT), data, , &actual, );
if(r == && actual == ) //we wrote the 4 bytes successfully
printf("Writing Successful!\n");
else
printf("Write Error\n"); r = libusb_release_interface(dev_handle, ); //release the claimed interface
if(r!=) {
printf("Cannot Release Interface\n");
return ;
}
printf("Released Interface\n"); libusb_close(dev_handle); //close the device we opened
libusb_exit(ctx); //needs to be called to end the return ;
}

另外,有一个叫usb4java的开源项目,它包含有怎么使用的example,可以作为学习怎么使用libusb,地址如下:

http://usb4java.org/index.html

https://github.com/usb4java/

https://github.com/usb4java/libusb4java

https://github.com/usb4java/usb4java

https://github.com/usb4java/usb4java-javax-examples

https://github.com/usb4java/usb4java-examples

how to learn device driver的更多相关文章

  1. How to learn linux device driver

    To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...

  2. [platform]linux platform device/driver(二)--Platform Device和Platform_driver注册过程之详细代码

    转自:http://www.cnblogs.com/haimeng2010/p/3582403.html 目录: 1.platform_device注册过程 2.platform_driver注册过程 ...

  3. Architecture of Device I/O Drivers, Device Driver Design

    http://www.kalinskyassociates.com/Wpaper4.html Architecture of Device I/O Drivers Many embedded syst ...

  4. linux下bus,device,driver三者关系

    linux下bus,device,driver三者关系 1.bus: 总线作为主机和外设的连接通道,有些总线是比较规范的,形成了很多协议.如 PCI,USB,1394,IIC等.任何设备都可以选择合适 ...

  5. linux device driver —— 环形缓冲区的实现

    还是没有接触到怎么控制硬件,但是在书里看到了一个挺巧妙的环形缓冲区实现. 此环形缓冲区实际为一个大小为bufsize的一维数组,有一个rp的读指针,一个wp的写指针. 在数据满时写进程会等待读进程读取 ...

  6. 阅读 Device Driver Programmer Guide 笔记

    阅读 Device Driver Programmer Guide 笔记 xilinx驱动命名规则 以X开头 源文件命名规则 以x打头 底层头文件与高级头文件 重点来了,关于指针的使用 其中 XDev ...

  7. Samsung_tiny4412(驱动笔记10)----mdev,bus,device,driver,platform

    /*********************************************************************************** * * mdev,bus,de ...

  8. I.MX6 ar1020 SPI device driver hacking

    /************************************************************************************ * I.MX6 ar1020 ...

  9. I.MX6 Linux I2C device& driver hacking

    /******************************************************************************************* * I.MX6 ...

随机推荐

  1. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...

  2. [笨木头FireFly 02]入门篇2_客户端发送请求,服务器处理请求

    原地址:http://www.9miao.com/question-15-53940.html 好,经过上一篇不权威的讲解,大家已经能轻易地让客户端和服务端连接起来了. 但是,仅仅是连接了,可它们俩不 ...

  3. 找出程序cpu使用率高的原因

    确定是CPU过高 使用top观察是否存在CPU使用率过高现象 找出线程 对CPU使用率过高的进程的所有线程进行排序 ps H -e -o pid,tid,pcpu,cmd --sort=pcpu |g ...

  4. C++:在程序中获取全球唯一标识号(GUID或UUID)

    Windows:使用CoCreateGuid函数(GUID) #include <objbase.h> #include <stdio.h> #define GUID_LEN  ...

  5. nginx 详解

    #运行用户 #user  nobody;   #启动进程,通常设置成和cpu的数量相等或者2倍于cpu的个数(具体结合cpu和内存).默认为1 worker_processes  1;   #全局的错 ...

  6. 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!

    启动 Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误的解决方法 http://blog.csdn.net/z ...

  7. 面试大总结之二:Java搞定面试中的二叉树题目

    package BinaryTreeSummary; import java.util.ArrayList; import java.util.Iterator; import java.util.L ...

  8. *J2EE中乱码处理

    发生中文乱码有三种情况 表单form (1)post 首先确定浏览器的编码方式,比如说utf-8,请求发给web服务器,web服务器以编码方式iso-9959-1来接收数据(服务器是外国人编写的),服 ...

  9. Django admin site(一)ModelAdmin Options

    Admin管理界面是django的杀手级应用.它读取你模式中的元数据,然后提供给你一个强大而且可以使用的界面,网站管理者可以用它立即向网站中添加内容. 要使用admin,可以按照下面的步骤: 将'dj ...

  10. python学习笔记四--元组

    一.元组: 1. 不可变更的列表 2. 从语法上,她们是编写在小括号里,不是方括号里,列表是编写在方括号里的 3. 圆括号也同时用于表达式,如果想说明这是一个元组,不是表达式,可以在value后,关闭 ...