早早的就買了Linux程序設計和Linux設備驅動程序兩本書,一直沒什麼時間仔細閱讀,總是偶爾拿來翻翻。
最近終於有個機會學習USB的驅動程序,手邊沒有現成的環境,先找找資料學習一下吧。
爲Linux USB寫驅動程序,要先了解現有的USB子系統。寫驅動程序,一般先要分析usb-skeleton.c。我分析的內核代碼版本是2.6.23。
文件中定義了設備結構體usb_skel:
struct usb_skel {
struct usb_device *udev; /* the usb device for this device */
struct usb_interface *interface; /* the interface for this device */
struct semaphore limit_sem; /* limiting the number of writes in progress */
struct usb_anchor submitted; /* in case we need to retract our submissions */
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 */
int errors; /* the last request tanked */
int open_count; /* count the number of openers */
spinlock_t err_lock; /* lock for errors */
struct kref kref;
struct mutex io_mutex; /* synchronize I/O with disconnect */
}
struct usb_device *udev; /* the usb device for this device */
struct usb_interface *interface; /* the interface for this device */
struct semaphore limit_sem; /* limiting the number of writes in progress */
struct usb_anchor submitted; /* in case we need to retract our submissions */
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 */
int errors; /* the last request tanked */
int open_count; /* count the number of openers */
spinlock_t err_lock; /* lock for errors */
struct kref kref;
struct mutex io_mutex; /* synchronize I/O with disconnect */
}
第一個見到的數據結構就是 struct usb_device,在這裏實際就代表這個驅動程序所對應的設備信息。然後將會看到struct usb_interface 結構體,代表了這個設備的接口信息。limit_sem是進行併發控制的,bulk_in_buffer是接收數據的緩衝區,bulk_in_size是緩衝區的大小。bulk_in_endpointAddr和bulk_out_endpointAddr是批量輸入輸出端口地址。open_count是打開設備的計數器。kref是一個內核使用的引用計數器。io_mutex是I/O的互斥量。err_lock是錯誤旋鎖,errors是錯誤代碼。