Scsi - Index of
Scsi - Index of Scsi - Index of
一下子进程,稍候马上去看父进程. 301 行,一个for 语句死循环,尽管外面的世界很精彩,但是咱们去看看for 里面的世界也不妨. 303 行,down_interruptible()函数,事实上302 行的注释已经告诉咱们,thread 将进入睡眠了...,也许 她累了.down_interruptible 的参数是&us->sema,不陌生吧,我们之前讲信号量讲互斥锁的时候就已经 提过了us->sema.所以这里很简单,就是想获得这把锁,但是别忘了,我们当初就介绍过,这把锁一开始就被 初始化为0 了,也就是说它属于那种指腹为婚的情形,一到这个世界来就告诉别人自己已经是名花有主了. 因 114 此,这里只能进入睡眠,等待一个up()函数去释放锁.谁会调用up()函数呢?暂时先不管它,我们先关注一下 父进程,毕竟我们自己进入了睡眠,而之前我们把父进程唤醒了. 彼岸花的传说(三) 遥想公瑾当年,小乔出嫁了,雄姿英发. 羽扇纶巾,谈笑间,樯橹灰飞烟灭. 故国神游,多情应笑我,早生华发, 人生如梦,一樽还酹江月. 的确,人生如梦,设计Linux 代码的人想必非常认可这种观点,因为他们已然把这种思想融入到了代码中去, 所以在代码里我们常看到睡眠,唤醒,睡眠,唤醒...而作为当年的大学生,Linus 想必也很认同大学生活的现 状,即大学生活就是睡觉,只是有的人两个人睡,有的人一个人睡. 前面已经说了,父进程在函数usb_stor_acquire_resources()里边,810 行,wait_for_completion(&(us->notify)),进入睡眠,而刚才咱们在子进程里已经看 到,complete(&(us->notify))被调用,于是父进程被唤醒,回到usb_stor_acquire_resources()函数中, 往下走,812 行,无它,唯返回耳.返回了0.于是咱们终于回到了storage_probe()函数中来. 1001 行,scsi_add_host()函数被执行,之前申请的us->host 被作为参数传递给她,同时,intf->dev 也 被传递给她,这个冬冬是被用来注册sysfs 的.在scsi_host_alloc 之后,必须执行scsi_add_host(),这 样,scsi 核心层才能够知道有这么一个host 存在.scsi_add_host()成功则返回0,否则返回出错代码.如果 一切顺利,咱们将走到1009 行,别急,先把代码贴出来,这就是storage_probe()函数的最后一小段了: 1007 1008 /* Start up the thread for delayed SCSI-device scanning */ 1009 result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM); 1010 if (result < 0) { 1011 printk(KERN_WARNING USB_STORAGE 1012 "Unable to start the device-scanning thread\n"); 1013 scsi_remove_host(us->host); 1014 goto BadDevice; 1015 } 1016 1017 return 0; 1018 1019 /* We come here if there are any problems */ 1020 BadDevice: 1021 US_DEBUGP("storage_probe() failed\n"); 1022 usb_stor_release_resources(us);
1023 dissociate_dev(us); 1024 return result; 1025 } 1026 115 又一次见到了kernel_thread,不需要更多解释,这里自然还是创建一个内核守护进程,只不过这次是 usb_stor_scan_thread,而上次是usb_stor_control_thread.usb_stor_scan_thread()函数也是定 义于drivers/usb/storage/usb.c 中: 886 /* Thread to carry out delayed SCSI-device scanning */ 887 static int usb_stor_scan_thread(void * __us) 888 { 889 struct us_data *us = (struct us_data *)__us; 890 891 /* 892 * This thread doesn't need any user-level access, 893 * so get rid of all our resources. 894 */ 895 lock_kernel(); 896 daemonize("usb-stor-scan"); 897 unlock_kernel(); 898 899 printk(KERN_DEBUG 900 "usb-storage: device found at %d\n", us->pusb_dev->devnum); 901 902 /* Wait for the timeout to expire or for a disconnect */ 903 if (delay_use > 0) { 904 printk(KERN_DEBUG "usb-storage: waiting for device " 905 "to settle before scanning\n"); 906 retry: 907 wait_event_interruptible_timeout(us->scsi_scan_wait, 908 test_bit(US_FLIDX_DISCONNECTING, &us->flags), 909 delay_use * HZ); 910 if (current->flags & PF_FREEZE) { 911 refrigerator(PF_FREEZE); 912 goto retry; 913 } 914 } 915 916 /* If the device is still connected, perform the scanning */ 917 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 918 scsi_scan_host(us->host); 919 printk(KERN_DEBUG "usb-storage: device scan complete\n"); 920 } 921
- Page 65 and 66: 782 us->unusual_dev->initFunction(u
- Page 67 and 68: 630 * each frame is in the fields o
- Page 69 and 70: 716 * Bulk URBs may 717 * use the U
- Page 71 and 72: 802 int number_of_packets; /* (in)
- Page 73 and 74: 先说互斥锁.它诞生于这样
- Page 75 and 76: 920 US_DEBUGP("GetMaxLUN command re
- Page 77 and 78: 148 /* set up data structures for t
- Page 79 and 80: 90 * @wValue: matches the USB wValu
- Page 81 and 82: 830 void *transfer_buffer, 831 int
- Page 83 and 84: 25 { 26 x->done = 0; 27 init_waitqu
- Page 85 and 86: 都会过期,我开始怀疑,在这
- Page 87 and 88: 是实际长度,要么就是不成
- Page 89 and 90: 87 258 行,usb_pipeendpoint,定义
- Page 91 and 92: 对爱你的人掘了一条无法
- Page 93 and 94: Type: Direct-Access ANSI SCSI revis
- Page 95 and 96: 416 /* 417 * this defines our host
- Page 97 and 98: 69 * Ioctl interface 70 * 71 * Stat
- Page 99 and 100: 155 * "is this a new device" checks
- Page 101 and 102: 241 char *proc_name; 242 243 /* 244
- Page 103 and 104: 328 * True if the low-level driver
- Page 105 and 106: 可以看到这个函数的参数
- Page 107 and 108: 451 short unsigned int max_sectors;
- Page 109 and 110: 地 说明了为啥咱们要显式
- Page 111 and 112: usb_stor_acquire_resources()函数
- Page 113 and 114: 341 } 342 343 /* reject if target !
- Page 115: 内核的核心位置,kernel/exit.c
- Page 119 and 120: 917 行,再次判断设备有没有
- Page 121 and 122: 181 182 /* fail the command if we a
- Page 123 and 124: 83 struct timer_list eh_timeout; /*
- Page 125 and 126: 123 两个scsi_done,一个是struct
- Page 127 and 128: 56 jiffie count on our counter, the
- Page 129 and 130: 中 看到如下的定义: UNUSUAL_
- Page 131 and 132: 269 memcpy(data+16, us->unusual_dev
- Page 133 and 134: 一切准备好了之后,我们就
- Page 135 and 136: 238 (sg->offset + *offset) & (PAGE_
- Page 137 and 138: 所以对于那些不能相应INQUI
- Page 139 and 140: 239 行对unsigned int sglen 赋值
- Page 141 and 142: 请求,所以这里就判断abort
- Page 143 and 144: 112 case GPCMD_READ_HEADER: what =
- Page 145 and 146: 到北京,每每只有在夜深人
- Page 147 and 148: 580 /* 581 * If we have a failure,
- Page 149 and 150: 666 memcpy(srb->cmnd, old_cmnd, MAX
- Page 151 and 152: 950 struct bulk_cb_wrap *bcb = (str
- Page 153 and 154: 1034 /* get the status again */ 103
- Page 155 and 156: 426 /* store the actual length of t
- Page 157 and 158: 后她会给host 返回一个CSW.CB
- Page 159 and 160: 296 } 297 298 US_DEBUGP("-- transfe
- Page 161 and 162: 491 * scatter-gather or not, and ac
- Page 163 and 164: 迷雾重重的 Bulk 传输(五) us
- Page 165 and 166: 然后把她赋给了result.而us->
1023 dissociate_dev(us);<br />
1024 return result;<br />
1025 }<br />
1026<br />
115<br />
又一次见到了kernel_thread,不需要更多解释,这里自然还是创建一个内核守护进程,只不过这次是<br />
usb_stor_scan_thread,而上次是usb_stor_control_thread.usb_stor_scan_thread()函数也是定<br />
义于drivers/usb/storage/usb.c 中:<br />
886 /* Thread to carry out delayed SCSI-device scanning */<br />
887 static int usb_stor_scan_thread(void * __us)<br />
888 {<br />
889 struct us_data *us = (struct us_data *)__us;<br />
890<br />
891 /*<br />
892 * This thread doesn't need any user-level access,<br />
893 * so get rid <strong>of</strong> all our resources.<br />
894 */<br />
895 lock_kernel();<br />
896 daemonize("usb-stor-scan");<br />
897 unlock_kernel();<br />
898<br />
899 printk(KERN_DEBUG<br />
900 "usb-storage: device found at %d\n", us->pusb_dev->devnum);<br />
901<br />
902 /* Wait for the timeout to expire or for a disconnect */<br />
903 if (delay_use > 0) {<br />
904 printk(KERN_DEBUG "usb-storage: waiting for device "<br />
905 "to settle before scanning\n");<br />
906 retry:<br />
907 wait_event_interruptible_timeout(us->scsi_scan_wait,<br />
908 test_bit(US_FLIDX_DISCONNECTING, &us->flags),<br />
909 delay_use * HZ);<br />
910 if (current->flags & PF_FREEZE) {<br />
911 refrigerator(PF_FREEZE);<br />
912 goto retry;<br />
913 }<br />
914 }<br />
915<br />
916 /* If the device is still connected, perform the scanning */<br />
917 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {<br />
918 scsi_scan_host(us->host);<br />
919 printk(KERN_DEBUG "usb-storage: device scan complete\n");<br />
920 }<br />
921