ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何将/ proc / bus / usb / devices条目映射到/ dev / sdX设备?

2019-06-26 05:39:06  阅读:222  来源: 互联网

标签:c-3 linux usb-drive


我需要知道如何确定/ proc / bus / usb / devices / / dev / sdX设备映射到哪个条目.基本上,我需要知道给定USB记忆棒的供应商ID和产品ID(可能没有序列号).

在我的情况下,我在/ proc / bus / usb / devices中有我的闪存驱动器的这个条目:

T:  Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  6 Spd=480 MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=0781 ProdID=5530 Rev= 2.00
S:  Manufacturer=SanDisk
S:  Product=Cruzer
S:  SerialNumber=0765400A1BD05BEE
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=200mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E:  Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms

我碰巧知道在我的情况下它是/ dev / sda,但我不知道如何在代码中解决这个问题.我的第一种方法是遍历所有/ dev / sdXX设备并发出SCSI_IOCTL_GET_BUS_NUMBER和/或SCSI_IOCTL_GET_IDLUN请求,但返回的信息无法帮我匹配:

/tmp # ./getscsiinfo /dev/sda
SCSI bus number: 8
ID: 00
LUN: 00
Channel: 00
Host#: 08
four_in_one: 08000000
host_unique_id: 0

我不确定如何使用SCSI总线编号或ID,LUN,Channel,Host将其映射到/ proc / bus / usb / devices中的条目.或者我如何从/ proc / bus / usb / 001/006设备获取SCSI总线编号,这是一个usbfs设备,似乎不喜欢相同的ioctl:

/tmp # ./getscsiinfo /proc/bus/usb/001/006
Could not get bus number: Inappropriate ioctl for device

这是我的little getscsiinfo测试工具的测试代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>

struct scsi_idlun
{
    int four_in_one;
    int host_unique_id;
};

int main(int argc, char** argv) {
    if (argc != 2)
        return 1;

    int fd = open(argv[1], O_RDONLY | O_NONBLOCK);
    if (fd < 0)
    {
        printf("Error opening device: %m\n");
        return 1;
    }

    int busNumber = -1;
    if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &busNumber) < 0)
    {
        printf("Could not get bus number: %m\n");
        close(fd);
        return 1;
    }

    printf("SCSI bus number: %d\n", busNumber);

    struct scsi_idlun argid;
    if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &argid) < 0)
    {
        printf("Could not get id: %m\n");
        close(fd);
        return 1;
    }

    printf("ID: %02x\n", argid.four_in_one & 0xFF);
    printf("LUN: %02x\n", (argid.four_in_one >> 8) & 0xFF);
    printf("Channel: %02x\n", (argid.four_in_one >> 16) & 0xFF);
    printf("Host#: %02x\n", (argid.four_in_one >> 24) & 0xFF);
    printf("four_in_one: %08x\n", (unsigned int)argid.four_in_one);
    printf("host_unique_id: %d\n", argid.host_unique_id);

    close(fd);
    return 0;
}

有谁有想法吗?

解决方法:

我相信你可以使用libudev库收集这些信息.

以下是有关它的一些细节:http://www.signal11.us/oss/udev/

我在上面的网站上发现了类似的东西:

.. Using libudev, we’ll be able to inspect the devices, including their Vendor ID (VID), Product ID (PID), serial number, and device strings, without ever opening the device. Further, libudev will tell us exactly where inside /dev the device’s node is located, giving the application a robust and distribution-independent way of accessing the device. …

标签:c-3,linux,usb-drive
来源: https://codeday.me/bug/20190626/1290439.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有