????probe是usb子系統(tǒng)自動(dòng)調(diào)用的一個(gè)函數(shù),有USB設(shè)備接到硬件集線器時(shí),usb子系統(tǒng)會(huì)根據(jù)production ID和vendor ID的組合或者設(shè)備的class、subclass跟protocol的組合來識(shí)別設(shè)備調(diào)用相應(yīng)驅(qū)動(dòng)程序的probe(探測)函數(shù),對(duì)于skeleton來說,就是skel_probe。系統(tǒng)會(huì)傳遞給探測函數(shù)一個(gè)usb_interface *跟一個(gè)struct usb_device_id *作為參數(shù)。他們分別是該USB設(shè)備的接口描述(一般會(huì)是該設(shè)備的第0號(hào)接口,該接口的默認(rèn)設(shè)置也是第0號(hào)設(shè)置)跟它的設(shè)備ID描述(包括Vendor ID、Production ID等)。probe函數(shù)比較長,我們分段來分析這個(gè)函數(shù):
dev->udev = usb_get_dev(interface_to_usbdev(interface));
dev->interface = interface;
????在初始化了一些資源之后,可以看到第一個(gè)關(guān)鍵的函數(shù)調(diào)用——interface_to_usbdev。他同uo一個(gè)usb_interface來得到該接口所在設(shè)備的設(shè)備描述結(jié)構(gòu)。本來,要得到一個(gè)usb_device只要用interface_to_usbdev就夠了,但因?yàn)橐黾訉?duì)該usb_device的引用計(jì)數(shù),我們應(yīng)該在做一個(gè)usb_get_dev的操作,來增加引用計(jì)數(shù),并在釋放設(shè)備時(shí)用usb_put_dev來減少引用計(jì)數(shù)。這里要解釋的是,該引用計(jì)數(shù)值是對(duì)該usb_device的計(jì)數(shù),并不是對(duì)本模塊的計(jì)數(shù),本模塊的計(jì)數(shù)要由kref來維護(hù)。所以,probe一開始就有初始化kref。事實(shí)上,kref_init操作不單只初始化kref,還將其置設(shè)成1。所以在出錯(cuò)處理代碼中有kref_put,它把kref的計(jì)數(shù)減1,如果kref計(jì)數(shù)已經(jīng)為0,那么kref會(huì)被釋放。kref_put的第二個(gè)參數(shù)是一個(gè)函數(shù)指針,指向一個(gè)清理函數(shù)。注意,該指針不能為空,或者kfree。該函數(shù)會(huì)在最后一個(gè)對(duì)kref的引用釋放時(shí)被調(diào)用(如果我的理解不準(zhǔn)確,請(qǐng)指正)。下面是內(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í)行打開操作時(shí),我們要增加kref的計(jì)數(shù),我們可以用kref_get,來完成。所有對(duì)struct kref的操作都有內(nèi)核代碼確保其原子性。
????得到了該usb_device之后,我們要對(duì)我們自定義的usb_skel各個(gè)狀態(tài)跟資源作初始化。這部分工作的任務(wù)主要是向usb_skel注冊(cè)該usb設(shè)備的端點(diǎn)。這里可能要補(bǔ)充以下一些關(guān)于usb_interface_descriptor的知識(shí),但因?yàn)閮?nèi)核源碼對(duì)該結(jié)構(gòu)體的注釋不多,所以只能靠個(gè)人猜測。在一個(gè)usb_host_interface結(jié)構(gòu)里面有一個(gè)usb_interface_descriptor叫做desc的成員,他應(yīng)該是用于描述該interface的一些屬性,其中bNumEndpoints是一個(gè)8位(b for byte)的數(shù)字,他代表了該接口的端點(diǎn)數(shù)。probe然后遍歷所有的端點(diǎn),檢查他們的類型跟方向,注冊(cè)到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;
???? }
評(píng)論
查看更多