how to learn device driver

摘要:
制作linuxusbdriverhttp://www.kroah.com/linux/http://matthias.vallentin.net/blog/2007/04/writing-alinux-kernel-driver-for-an-unknown-usb-device/http://www.linuxjournal.com/article/7353?page=0,2苏多

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 < 0) {
        printf("Init Error
");
        return 1;
    }
    libusb_set_debug(ctx, 3);               //set verbosity level to 3, as suggested in the documentation

    cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
    if(cnt < 0) {
        printf("Get Device Error
");       //there was an error
        return 1;
    }
    printf("%d Devices in list
", 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 ...
");
        return 1;
    }
    else
        printf("Device Opened
");
    libusb_free_device_list(devs, 1);       //free the list, unref the devices in it

    unsigned char data[4];
    data[0]='a';data[1]='b';data[2]='c';data[3]='d';    //some dummy values

    int actual;                             //used to find out how many bytes were written
    if(libusb_kernel_driver_active(dev_handle, 0) == 1) {           //find out if kernel driver is attached
        printf("Kernel Driver Active
");
        if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
            printf("Kernel Driver Detached!
");
    }
    r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1)
    if(r < 0) {
        printf("Cannot Claim Interface
");
        return 1;
    }
    printf("Claimed Interface
");

    //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129, 
    r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0); 
    if(r == 0 && actual == 4) //we wrote the 4 bytes successfully
        printf("Writing Successful!
");
    else
        printf("Write Error
");

    r = libusb_release_interface(dev_handle, 0); //release the claimed interface
    if(r!=0) {
        printf("Cannot Release Interface
");
        return 1;
    }
    printf("Released Interface
");

    libusb_close(dev_handle); //close the device we opened
    libusb_exit(ctx); //needs to be called to end the

    return 0;
}

另外,有一个叫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》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇HTTP协议中GET请求方法中的请求头内容 : If-Modified-Sincevue-element-admin基础模板(极简版)的Refresh跳转404页面下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

libusb函数

一、libusb常用数据结构 libusb_device *list:设备列表 libusb_device_handle *handle:设备句柄 struct libusb_device_descriptor desc:设备描述符 struct libusb_config_descriptor conf:配置描述符 二、libusb常用API函数 1...

libusb-win32 在visual studio2008中成功编译回忆录

关于这个项目不用多说 介绍 libusb是一个针对usb通讯的库. 使用它, 你不需要知道操作系统的细节, 你只需要对USB有足够的了解即可. 它也不需要你写驱动, 所有的工作都可以在用户态完成. 使用方法很简单, 这里有一个示例:http://sourceforge.net/apps/trac/libusb-win32/wiki/libusbwin32...

libusb阻塞

这两天发现手头一个usb指纹头出现了一点状况,若libusb以同步方式发送bulk transfer出现阻塞。经过测试发现跟timeout有些关系:若timeout为0(无timeout),不会阻塞;若timeout为1000或者2000,则会。另外,若采用异步方式传送bulk transfer,则不会阻塞。 同步方式 libusb_bulk_transf...