关于linux-2.6.11内核USB驱动问题

ljf112621 2006-07-26 04:30:41
我想请问一下 ,在LINUX-2.6.11的USB 驱动程序中, 当你插入一个U盘进行识别时,会调用usb/storage/usb.c中usb_stor_acquire_resource 函数,该函数定义如下:
/* Initialize all the dynamic resources we need */
static int usb_stor_acquire_resources(struct us_data *us)
{
int p;

us->current_urb = usb_alloc_urb(0, GFP_KERNEL); //分配URB结构对象空间
if (!us->current_urb) {
US_DEBUGP("URB allocation failed\n");
return -ENOMEM;
}

/* Lock the device while we carry out the next two operations */
down(&us->dev_semaphore);

/* For bulk-only devices, determine the max LUN value */
if (us->protocol == US_PR_BULK) {
p = usb_stor_Bulk_max_lun(us);
if (p < 0) {
up(&us->dev_semaphore);
return p;
}
us->max_lun = p;
}

/* Just before we start our control thread, initialize
* the device if it needs initialization */
if (us->unusual_dev->initFunction)
us->unusual_dev->initFunction(us);

up(&us->dev_semaphore);

/*
* Since this is a new device, we need to register a SCSI
* host definition with the higher SCSI layers.
*/
us->host = scsi_host_alloc(&usb_stor_host_template, sizeof(us));
if (!us->host) {
printk(KERN_WARNING USB_STORAGE
"Unable to allocate the scsi host\n");
return -EBUSY;
}

/* Set the hostdata to prepare for scanning */
us->host->hostdata[0] = (unsigned long) us;

/* Start up our control thread */
p = kernel_thread(usb_stor_control_thread, us, CLONE_VM);
if (p < 0) {
printk(KERN_WARNING USB_STORAGE
"Unable to start control thread\n");
return p;
}
us->pid = p;

/* Wait for the thread to start */
wait_for_completion(&(us->notify));

return 0;
}

这个函数定义中有这么一步:
p = usb_stor_Bulk_max_lun(us);
注释说这主要是对批量的设备(U盘等),得到最大逻辑单元值.我查了一下,好象在SCSI协议模型中,每个逻辑单元用来操作SCSI设备,但还是不是很懂.

哪位高人能够详细得解释一下什么是LUN吗?在USB识别过程中获取LUN值有什么用啊?
为什么我多数U盘获得的LUN值都是0呢?

usb_stor_Bulk_max_lun函数在usb/storage/transport.c中定义为
/*
* Bulk only transport
*/

/* Determine what the maximum LUN supported is */
int usb_stor_Bulk_max_lun(struct us_data *us)
{
int result;

/* issue the command */
result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
US_BULK_GET_MAX_LUN,
USB_DIR_IN | USB_TYPE_CLASS |
USB_RECIP_INTERFACE,
0, us->ifnum, us->iobuf, 1, HZ);

US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
result, us->iobuf[0]);

/* if we have a successful request, return the result */
if (result > 0)
return us->iobuf[0];

/*
* Some devices (i.e. Iomega Zip100) need this -- apparently
* the bulk pipes get STALLed when the GetMaxLUN request is
* processed. This is, in theory, harmless to all other devices
* (regardless of if they stall or not).
*/
if (result == -EPIPE) {
usb_stor_clear_halt(us, us->recv_bulk_pipe);
usb_stor_clear_halt(us, us->send_bulk_pipe);
}

/*
* Some devices don't like GetMaxLUN. They may STALL the control
* pipe, they may return a zero-length result, they may do nothing at
* all and timeout, or they may fail in even more bizarrely creative
* ways. In these cases the best approach is to use the default
* value: only one LUN.
*/
return 0;
}
...全文
355 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljf112621 2006-08-08
  • 打赏
  • 举报
回复
楼上的说得对,LUN值应该是SCSI总线分配的.
但是,根据以上的注释,好象最大的LUN值又是从U盘获取的,而且有些U盘的最大LUN值可以是0.这我就不懂了.注意,这里的最大LUN值,不是LUN值.
ljf112621 2006-08-07
  • 打赏
  • 举报
回复
如jammuxu所言,我一次插了两个U盘,但显示的LUN值都是0啊,具体信息如下:
(none) # cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: Model: @ Rev:
Type: Direct-Access ANSI SCSI revision: 02
Host: scsi1 Channel: 00 Id: 00 Lun: 00
Vendor: USB Model: Flash Disk Rev: 2.00
Type: Direct-Access ANSI SCSI revision: 02
ljf112621 2006-08-07
  • 打赏
  • 举报
回复
谢谢jammyxu和missile提供的资料,但是我看了还是不是很明白,结合USB驱动,如我帖子所言,为什么获取的最大LUN值是0的时候,还是能识别呢?
根据资料所言,LUN是为了区分SCSI总线上不同设备而分配的逻辑单元值,但是对USB驱动来说,当最大LUN值是0的时候,应该表示USB存储设备没有获得LUN值才对啊,为什么也可以识别?
mmxopq 2006-08-07
  • 打赏
  • 举报
回复
*GetMaxLun——取得设备支持的逻辑单元数目。

Some devices don't like GetMaxLUN. They may STALL the control
* pipe, they may return a zero-length result, they may do nothing at
* all and timeout, or they may fail in even more bizarrely creative
* ways. In these cases the best approach is to use the default
* value: only one LUN.

据此,lun是自己在linux里面设置的,而不是根据实际接的设备数读出来的
missile8 2006-08-04
  • 打赏
  • 举报
回复
see see

http://www.webopedia.com/TERM/L/LUN.html
jammyxu 2006-08-02
  • 打赏
  • 举报
回复
“再插一下”應該是再插一個
jammyxu 2006-08-02
  • 打赏
  • 举报
回复
低分的難題﹗

$ cd /proc/scsi/
$ ls
device_info scsi sg usb-storage
$ cat scsi
Attached devices:
Host: scsi1 Channel: 00 Id: 00 Lun: 00

Vendor: CDWRITER Model: IDE4816 Rev: 0A2G
Type: CD-ROM ANSI SCSI revision: 02
Host: scsi2 Channel: 00 Id: 00 Lun: 00
Vendor: USB 2.0 Model: Storage Device Rev: 0100
Type: Direct-Access ANSI SCSI revision: 02

lun 為 0很正常﹐ 代碼中異常條件不是小于0嗎﹖
再插一下U盤應該就不一樣了
ljf112621 2006-08-02
  • 打赏
  • 举报
回复
来个高手帮忙解释一下啊,就是提供个找资料的地方也可以啊!
nwcow 2006-07-28
  • 打赏
  • 举报
回复
它的意思是不是block size?
alaiyeshi 2006-07-28
  • 打赏
  • 举报
回复
我没做过
以前的老大做过,可惜走了
cryincold 2006-07-28
  • 打赏
  • 举报
回复
55555555555~~~
我的电脑的USB坏了.
ljf112621 2006-07-28
  • 打赏
  • 举报
回复
难道论坛上这么多人就没有做过USB驱动的吗?
91program 2006-07-27
  • 打赏
  • 举报
回复
没有做过USB驱动,GZ。
pemstin 2006-07-27
  • 打赏
  • 举报
回复
LINUX菜鸟
关注ing...
ljf112621 2006-07-27
  • 打赏
  • 举报
回复
没人知道吗?

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧