Scsi - Index of

Scsi - Index of Scsi - Index of

07.06.2013 Views

至此,get_pipes函数结束了,信息都保存到了us里面.下面us该发挥她的作用了.回到storage_probe() 函数,998行,把us作为参数传递给了usb_stor_acquire_resources()函数.而这个函数才是故事的高潮. 每一个有识之士在看过这个函数之后就会豁然开朗,都会感慨,一下子就看到了春天,看到了光明,原来 Linux 中的设备驱动程序就是这么工作的啊! 996 997 /* Acquire all the other resources and add the host */ 998 result = usb_stor_acquire_resources(us); 999 if (result) 1000 goto BadDevice; 1001 result = scsi_add_host(us->host, &intf->dev); 1002 if (result) { 1003 printk(KERN_WARNING USB_STORAGE 1004 "Unable to add the scsi host\n"); 1005 goto BadDevice; 1006 } 我们来看usb_stor_acquire_resources 函数.它被定义于drivers/usb/storage/usb.c 中: 755 /* Initialize all the dynamic resources we need */ 756 static int usb_stor_acquire_resources(struct us_data *us) 757 { 758 int p; 759 760 us->current_urb = usb_alloc_urb(0, GFP_KERNEL); 761 if (!us->current_urb) { 762 US_DEBUGP("URB allocation failed\n"); 763 return -ENOMEM; 62 764 } 765 766 /* Lock the device while we carry out the next two operations */ 767 down(&us->dev_semaphore); 768 769 /* For bulk-only devices, determine the max LUN value */ 770 if (us->protocol == US_PR_BULK) { 771 p = usb_stor_Bulk_max_lun(us); 772 if (p < 0) { 773 up(&us->dev_semaphore); 774 return p; 775 } 776 us->max_lun = p; 777 } 778 779 /* Just before we start our control thread, initialize 780 * the device if it needs initialization */ 781 if (us->unusual_dev->initFunction)

782 us->unusual_dev->initFunction(us); 783 784 up(&us->dev_semaphore); 785 786 /* 787 * Since this is a new device, we need to register a SCSI 788 * host definition with the higher SCSI layers. 789 */ 790 us->host = scsi_host_alloc(&usb_stor_host_template, sizeof(us)); 791 if (!us->host) { 792 printk(KERN_WARNING USB_STORAGE 793 "Unable to allocate the scsi host\n"); 794 return -EBUSY; 795 } 796 797 /* Set the hostdata to prepare for scanning */ 798 us->host->hostdata[0] = (unsigned long) us; 799 800 /* Start up our control thread */ 801 p = kernel_thread(usb_stor_control_thread, us, CLONE_VM); 802 if (p < 0) { 803 printk(KERN_WARNING USB_STORAGE 804 "Unable to start control thread\n"); 805 return p; 806 } 807 us->pid = p; 63 808 809 /* Wait for the thread to start */ 810 wait_for_completion(&(us->notify)); 811 812 return 0; 813 } 待到山花浪漫时,她在丛中笑.一个悟性高的人应该一眼就能从这个函数中找出那行在丛中笑的代码来, 没错,她就是801 行,kernel_thread,这个函数造就了许多的经典的Linux 内核模块,正是因为她的存 在,Linux 中某些设备驱动程序的编写变得非常简单.可以说,对某些设备驱动程序来说,kernel_thread 几 乎是整个driver 的灵魂,或者说是该Linux 内核模块的灵魂.不管她隐藏的多么深,她总像漆黑中的萤火虫, 那样的鲜明,那样的出众.甚至不夸张的说,对于很多模块来说,只要找到kernel_thread 这一行,基本上你 就 知道这个模块是怎么工作的了.

至此,get_pipes函数结束了,信息都保存到了us里面.下面us该发挥她的作用了.回到storage_probe()<br />

函数,998行,把us作为参数传递给了usb_stor_acquire_resources()函数.而这个函数才是故事的高潮.<br />

每一个有识之士在看过这个函数之后就会豁然开朗,都会感慨,一下子就看到了春天,看到了光明,原来<br />

Linux 中的设备驱动程序就是这么工作的啊!<br />

996<br />

997 /* Acquire all the other resources and add the host */<br />

998 result = usb_stor_acquire_resources(us);<br />

999 if (result)<br />

1000 goto BadDevice;<br />

1001 result = scsi_add_host(us->host, &intf->dev);<br />

1002 if (result) {<br />

1003 printk(KERN_WARNING USB_STORAGE<br />

1004 "Unable to add the scsi host\n");<br />

1005 goto BadDevice;<br />

1006 }<br />

我们来看usb_stor_acquire_resources 函数.它被定义于drivers/usb/storage/usb.c 中:<br />

755 /* Initialize all the dynamic resources we need */<br />

756 static int usb_stor_acquire_resources(struct us_data *us)<br />

757 {<br />

758 int p;<br />

759<br />

760 us->current_urb = usb_alloc_urb(0, GFP_KERNEL);<br />

761 if (!us->current_urb) {<br />

762 US_DEBUGP("URB allocation failed\n");<br />

763 return -ENOMEM;<br />

62<br />

764 }<br />

765<br />

766 /* Lock the device while we carry out the next two operations */<br />

767 down(&us->dev_semaphore);<br />

768<br />

769 /* For bulk-only devices, determine the max LUN value */<br />

770 if (us->protocol == US_PR_BULK) {<br />

771 p = usb_stor_Bulk_max_lun(us);<br />

772 if (p < 0) {<br />

773 up(&us->dev_semaphore);<br />

774 return p;<br />

775 }<br />

776 us->max_lun = p;<br />

777 }<br />

778<br />

779 /* Just before we start our control thread, initialize<br />

780 * the device if it needs initialization */<br />

781 if (us->unusual_dev->initFunction)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!