probe是usb子系統(tǒng)自動調(diào)用的一個函數(shù),有USB設(shè)備接到硬件集線器時,usb子系統(tǒng)會根據(jù)production ID和vendorID的組合或者設(shè)備的class、subclass跟protocol的組合來識別設(shè)備調(diào)用相應(yīng)驅(qū)動程序的probe(探測)函數(shù),對于skeleton來說,就是skel_probe。系統(tǒng)會傳遞給探測函數(shù)一個usb_interface *跟一個struct usb_device_id*作為參數(shù)。他們分別是該USB設(shè)備的接口描述(一般會是該設(shè)備的第0號接口,該接口的默認(rèn)設(shè)置也是第0號設(shè)置)跟它的設(shè)備ID描述(包括VendorID、Production ID等)。Probe函數(shù)比較長,我們分段來分析這個函數(shù):
???? dev->udev = usb_get_dev(interface_to_usbdev(interface));
???? dev->interface = interface;
在初始化了一些資源之后,我們可以看到第一個關(guān)鍵的函數(shù)調(diào)用——interface_to_usbdev。他同uo一個usb_interface來得到該接口所在設(shè)備的設(shè)備描述結(jié)構(gòu)。本來,要得到一個usb_device只要用interface_to_usbdev就夠了,但因為要增加對該usb_device的引用計數(shù),我們應(yīng)該在做一個usb_get_dev的操作,來增加引用計數(shù),并在釋放設(shè)備時用usb_put_dev來減少引用計數(shù)。這里要解釋的是,該引用計數(shù)值是對該usb_device的計數(shù),并不是對本模塊的計數(shù),本模塊的計數(shù)要由kref來維護。所以,probe一開始就有初始化kref。事實上,kref_init操作不單只初始化kref,還將其置設(shè)成1。所以在出錯處理代碼中有kref_put,它把kref的計數(shù)減1,如果kref計數(shù)已經(jīng)為0,那么kref會被釋放。Kref_put的第二個參數(shù)是一個函數(shù)指針,指向一個清理函數(shù)。注意,該指針不能位空,或者kfree。該函數(shù)會在最后一個對kref的引用釋放時被調(diào)用(如果我的理解不準(zhǔn)確,請指正)。下面是內(nèi)核源碼中的一段注釋及代碼:
/**
?* kref_put - decrement refcount for object.
?* @kref: object.
?* @release: pointer to the function that will clean up the object when the
?*?? ???? last reference to the object is released.
?*?? ???? This pointer is required, and it is not acceptable to pass kfree
?*?? ???? in as this function.
?*
?* Decrement the refcount, and if 0, call release().
?* Return 1 if the object was removed, otherwise return 0.? Beware, if this
?* function returns 0, you still can not count on the kref from remaining in
?* memory.? Only use the return value if you want to see if the kref is now
?* gone, not present.
?*/
int kref_put(struct kref *kref, void (*release)(struct kref *kref))
{
???? WARN_ON(release == NULL);
???? WARN_ON(release == (void (*)(struct kref *))kfree);
?
???? /*
???? ?* if current count is one, we are the last user and can release object
???? ?* right now, avoiding an atomic operation on 'refcount'
???? ?*/
???? if ((atomic_read(&kref->refcount) == 1) ||
???? ??? (atomic_dec_and_test(&kref->refcount))) {
???????? release(kref);
???????? return 1;
???? }
???? return 0;
}
當(dāng)我們執(zhí)行打開操作時,我們要增加kref的計數(shù),我們可以用kref_get,來完成。所有對struct kref的操作都有內(nèi)核代碼確保其原子性。
得到了該usb_device之后,我們要對我們自定義的usb_skel各個狀態(tài)跟資源作初始化。這部分工作的任務(wù)主要是向usb_skel注冊該usb設(shè)備的端點。這里可能要補充以下一些關(guān)于usb_interface_descriptor的知識,但因為內(nèi)核源碼對該結(jié)構(gòu)體的注釋不多,所以只能靠個人猜測。在一個usb_host_interface結(jié)構(gòu)里面有一個usb_interface_descriptor叫做desc的成員,他應(yīng)該是用于描述該interface的一些屬性,其中bNubEndpoints一個8位(b forbyte)的數(shù)字,他代表了該接口的端點數(shù)。Probe然后遍歷所有的端點,檢查他們的類型跟方向,注冊到usb_skel中。
???? /* set up the endpoint information */
???? /* use only the first bulk-in and bulk-out endpoints */
???? iface_desc = interface->cur_altsetting;
???? for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
???????? endpoint = &iface_desc->endpoint[i].desc;
?
???????? if (!dev->bulk_in_endpointAddr &&
???????? ??? ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
?????????????????????? == USB_DIR_IN) &&
???????? ??? ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
?????????????????????? == USB_ENDPOINT_XFER_BULK)) {
????????????? /* we found a bulk in endpoint */
????????????? buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
????????????? dev->bulk_in_size = buffer_size;
????????????? dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
????????????? dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
????????????? if (!dev->bulk_in_buffer) {
?????????????????? err("Could not allocate bulk_in_buffer");
?????????????????? goto error;
????????????? }
???????? }
?
???????? if (!dev->bulk_out_endpointAddr &&
???????? ??? ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
?????????????????????? == USB_DIR_OUT) &&
???????? ??? ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
?????????????????????? == USB_ENDPOINT_XFER_BULK)) {
????????????? /* we found a bulk out endpoint */
????????????? dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
???????? }
???? }
???? if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
???????? err("Could not find both bulk-in and bulk-out endpoints");
???????? goto error;
???? }