• Linux摄像头驱动学习之:(五)UVC-分析设备描述符


      linux系统上插上USB摄像头设备后,内存就会有相应的设备描述符信息,后期可以根据这些信息进一步写驱动程序。

    流程:Device(设备) -> Configuration(配置) -> IAD I/F(接口联合体描述符-对接口的管理,比如数量和调用顺序等)

    查看UVC 1.5 Cloass Specification 规范手册
    框架调用流程:IT(01) -> PU(03) -> EU(04) -> OT(02)

    <Video Control Interface> 处理函数 parse_videocontrol_interface()--------------------------------------------

    VideoControl Interface的自定义描述符:
    extra buffer of interface 0:
    extra desc 0: 0d 24 01 00 01 4d 00 80 c3 c9 01 01 01
    VC_HEADER
    extra desc 1: 12 24 02 01 01 02 00 00 00 00 00 00 00 00 03 0e 00 00
    VC_INPUT_TERMINAL ID
    extra desc 2: 09 24 03 02 01 01 00 04 00
    VC_OUTPUT_TERMINAL ID wTerminalType bAssocTerminal bSourceID
    extra desc 3: 0b 24 05 03 01 00 00 02 7f 14 00
    VC_PROCESSING_UNIT ID bSourceID wMaxMultiplier bControlSize bmControls
    extra desc 4: 1a 24 06 04 ad cc b1 c2 f6 ab b8 48 8e 37 32 d4 f3 a3 fe ec 08 01 03 01 3f 00
    VC_EXTENSION_UNIT ID GUID bNumControls bNrInPins baSourceID

    VC_DESCRIPTOR_UNDEFINED 0x00
    VC_HEADER 0x01
    VC_INPUT_TERMINAL 0x02
    VC_OUTPUT_TERMINAL 0x03
    VC_SELECTOR_UNIT 0x04
    VC_PROCESSING_UNIT 0x05
    VC_EXTENSION_UNIT 0x06
    VC_ENCODING_UNIT 0x07

    <Video Streaming Interface> 处理函数 parse_videostreaming_interface()--------------------------------------

    VideoStreaming Interface的自定义描述符:
    extra buffer of interface 1:
    extra desc 0: 0e 24 01 01 df 00 81 00 02 02 01 01 01 00
    VS_INPUT_HEADER bNumFormats
    extra desc 1: 1b 24 04 01 05 59 55 59 32 00 00 10 00 80 00 00 aa 00 38 9b 71 10 01 00 00 00 00
    VS_FORMAT_UNCOMPRESSED bFormatIndex bNumFrameDescriptors GUID bBitsPerPixel
    extra desc 2: 1e 24 05 01 00 80 02 e0 01 00 00 ca 08 00 00 ca 08 00 60 09 00 15 16 05 00 01 15 16 05 00
    VS_FRAME_UNCOMPRESSED bFrameIndex bmCapabilities wWidth wHeight
    640x480
    extra desc 3: 1e 24 05 02 00 60 01 20 01 00 80 e6 02 00 80 e6 02 00 18 03 00 15 16 05 00 01 15 16 05 00
    VS_FRAME_UNCOMPRESSED
    extra desc 4: 1e 24 05 03 00 40 01 f0 00 00 80 32 02 00 80 32 02 00 58 02 00 15 16 05 00 01 15 16 05 00
    extra desc 5: 1e 24 05 04 00 b0 00 90 00 00 a0 b9 00 00 a0 b9 00 00 c6 00 00 15 16 05 00 01 15 16 05 00
    extra desc 6: 1e 24 05 05 00 a0 00 78 00 00 a0 8c 00 00 a0 8c 00 00 96 00 00 15 16 05 00 01 15 16 05 00

    extra desc 7: 1a 24 03 00 05 80 02 e0 01 60 01 20 01 40 01 f0 00 b0 00 90 00 a0 00 78 00 00
    VS_STILL_IMAGE_FRAME
    extra desc 8: 06 24 0d 01 01 04

    VS_INPUT_HEADER 0x01
    VS_STILL_IMAGE_FRAME 0x03
    VS_FORMAT_UNCOMPRESSED 0x04
    VS_FRAME_UNCOMPRESSED 0x05
    VS_COLORFORMAT 0x0D

    //参考 lsusb 源码得知如何去实现获取设备描述符:
    main
      ->list_devices
       ->dumpdev(libusb_device *dev)
         ->dump_device(udev, &desc);
        ->dump_config(udev, config); //打印出配置描述符
           ->for (i = 0 ; i < config->bNumInterfaces ; i++) //每个设置可能对应多个接口
           dump_interface(dev, &config->interface[i]);
            ->for (i = 0; i < interface->num_altsetting; i++) //每个接口设置描述符
            dump_altsetting(dev, &interface->altsetting[i]);


    //lsusb 命令打印出设备ID
    Bus 002 Device 006: ID 1b3b:2977

    //lsusb -v -d 0x1b3b : 命令打印出设备详细描述符
    Bus 002 Device 007: ID 1b3b:2977
    Device Descriptor: //设备描述符
    bLength 18
    bDescriptorType 1
    bcdUSB 2.00
    bDeviceClass 239 Miscellaneous Device
    bDeviceSubClass 2 ?
    bDeviceProtocol 1 Interface Association
    bMaxPacketSize0 64
    idVendor 0x1b3b
    idProduct 0x2977
    bcdDevice 1.0a
    iManufacturer 0
    iProduct 0
    iSerial 0
    bNumConfigurations 1
    Configuration Descriptor: //配置描述符
    bLength 9
    bDescriptorType 2
    wTotalLength 492
    bNumInterfaces 4
    bConfigurationValue 1
    iConfiguration 0
    bmAttributes 0x80
    (Bus Powered)
    MaxPower 500mA
    Interface Association: //接口
    bLength 8
    bDescriptorType 11
    bFirstInterface 0
    bInterfaceCount 2
    bFunctionClass 14 Video
    bFunctionSubClass 3 Video Interface Collection
    bFunctionProtocol 0
    iFunction 0
    .....................................

    /*--------------------------------------------以下代码实现获取通用usb摄像头设备描述符信息-----------------------------------------------------*/

    参考:lsusb - 源代码《libusb-1.0.16-rc10》 《usbutils-006》

    //注:本份代码仅实现设备描述符的获取和解析
    #include <linux/kernel.h> #include <linux/list.h> #include <linux/module.h> #include <linux/usb.h> #include <linux/videodev2.h> #include <linux/vmalloc.h> #include <linux/wait.h> #include <asm/atomic.h> #include <asm/unaligned.h> #include <media/v4l2-common.h> //支持的设备类型信息 static struct usb_device_id sheldon_uvc_ids[] = { /* Generic USB Video Class */ { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },/*1-视频控制接口*/ { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 2, 0) },/*2-视频流控制接口(被1包含)*/ {} }; static const char *get_guid(const unsigned char *buf) { static char guid[39]; /* NOTE: see RFC 4122 for more information about GUID/UUID * structure. The first fields fields are historically big * endian numbers, dating from Apollo mc68000 workstations. */ sprintf(guid, "{%02x%02x%02x%02x" "-%02x%02x" "-%02x%02x" "-%02x%02x" "-%02x%02x%02x%02x%02x%02x}", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]); return guid; } static void parse_videocontrol_interface(struct usb_interface *intf, unsigned char *buf, int buflen) { static const char * const ctrlnames[] = { "Brightness", "Contrast", "Hue", "Saturation", "Sharpness", "Gamma", "White Balance Temperature", "White Balance Component", "Backlight Compensation", "Gain", "Power Line Frequency", "Hue, Auto", "White Balance Temperature, Auto", "White Balance Component, Auto", "Digital Multiplier", "Digital Multiplier Limit", "Analog Video Standard", "Analog Video Lock Status" }; static const char * const camctrlnames[] = { "Scanning Mode", "Auto-Exposure Mode", "Auto-Exposure Priority", "Exposure Time (Absolute)", "Exposure Time (Relative)", "Focus (Absolute)", "Focus (Relative)", "Iris (Absolute)", "Iris (Relative)", "Zoom (Absolute)", "Zoom (Relative)", "PanTilt (Absolute)", "PanTilt (Relative)", "Roll (Absolute)", "Roll (Relative)", "Reserved", "Reserved", "Focus, Auto", "Privacy" }; static const char * const stdnames[] = { "None", "NTSC - 525/60", "PAL - 625/50", "SECAM - 625/50", "NTSC - 625/50", "PAL - 525/60" }; unsigned int i, ctrls, stds, n, p, termt, freq; while (buflen > 0) { if (buf[1] != USB_DT_CS_INTERFACE) printk(" Warning: Invalid descriptor "); else if (buf[0] < 3) printk(" Warning: Descriptor too short "); printk(" VideoControl Interface Descriptor: " " bLength %5u " " bDescriptorType %5u " " bDescriptorSubtype %5u ", buf[0], buf[1], buf[2]); switch (buf[2]) { case 0x01: /* HEADER */ printk("(HEADER) "); n = buf[11]; if (buf[0] < 12+n) printk(" Warning: Descriptor too short "); freq = buf[7] | (buf[8] << 8) | (buf[9] << 16) | (buf[10] << 24); printk(" bcdUVC %2x.%02x " " wTotalLength %5u " " dwClockFrequency %5u.%06uMHz " " bInCollection %5u ", buf[4], buf[3], buf[5] | (buf[6] << 8), freq / 1000000, freq % 1000000, n); for (i = 0; i < n; i++) printk(" baInterfaceNr(%2u) %5u ", i, buf[12+i]); break; case 0x02: /* INPUT_TERMINAL */ printk("(INPUT_TERMINAL) "); termt = buf[4] | (buf[5] << 8); n = termt == 0x0201 ? 7 : 0; if (buf[0] < 8 + n) printk(" Warning: Descriptor too short "); printk(" bTerminalID %5u " " wTerminalType 0x%04x " " bAssocTerminal %5u ", buf[3], termt, buf[6]); printk(" iTerminal %5u ", buf[7]); if (termt == 0x0201) { n += buf[14]; printk(" wObjectiveFocalLengthMin %5u " " wObjectiveFocalLengthMax %5u " " wOcularFocalLength %5u " " bControlSize %5u ", buf[8] | (buf[9] << 8), buf[10] | (buf[11] << 8), buf[12] | (buf[13] << 8), buf[14]); ctrls = 0; for (i = 0; i < 3 && i < buf[14]; i++) ctrls = (ctrls << 8) | buf[8+n-i-1]; printk(" bmControls 0x%08x ", ctrls); for (i = 0; i < 19; i++) if ((ctrls >> i) & 1) printk(" %s ", camctrlnames[i]); } break; case 0x03: /* OUTPUT_TERMINAL */ printk("(OUTPUT_TERMINAL) "); termt = buf[4] | (buf[5] << 8); if (buf[0] < 9) printk(" Warning: Descriptor too short "); printk(" bTerminalID %5u " " wTerminalType 0x%04x " " bAssocTerminal %5u " " bSourceID %5u " " iTerminal %5u ", buf[3], termt, buf[6], buf[7], buf[8]); break; case 0x04: /* SELECTOR_UNIT */ printk("(SELECTOR_UNIT) "); p = buf[4]; if (buf[0] < 6+p) printk(" Warning: Descriptor too short "); printk(" bUnitID %5u " " bNrInPins %5u ", buf[3], p); for (i = 0; i < p; i++) printk(" baSource(%2u) %5u ", i, buf[5+i]); printk(" iSelector %5u ", buf[5+p]); break; case 0x05: /* PROCESSING_UNIT */ printk("(PROCESSING_UNIT) "); n = buf[7]; if (buf[0] < 10+n) printk(" Warning: Descriptor too short "); printk(" bUnitID %5u " " bSourceID %5u " " wMaxMultiplier %5u " " bControlSize %5u ", buf[3], buf[4], buf[5] | (buf[6] << 8), n); ctrls = 0; for (i = 0; i < 3 && i < n; i++) ctrls = (ctrls << 8) | buf[8+n-i-1]; printk(" bmControls 0x%08x ", ctrls); for (i = 0; i < 18; i++) if ((ctrls >> i) & 1) printk(" %s ", ctrlnames[i]); stds = buf[9+n]; printk(" iProcessing %5u " " bmVideoStandards 0x%2x ", buf[8+n], stds); for (i = 0; i < 6; i++) if ((stds >> i) & 1) printk(" %s ", stdnames[i]); break; case 0x06: /* EXTENSION_UNIT */ printk("(EXTENSION_UNIT) "); p = buf[21]; n = buf[22+p]; if (buf[0] < 24+p+n) printk(" Warning: Descriptor too short "); printk(" bUnitID %5u " " guidExtensionCode %s " " bNumControl %5u " " bNrPins %5u ", buf[3], get_guid(&buf[4]), buf[20], buf[21]); for (i = 0; i < p; i++) printk(" baSourceID(%2u) %5u ", i, buf[22+i]); printk(" bControlSize %5u ", buf[22+p]); for (i = 0; i < n; i++) printk(" bmControls(%2u) 0x%02x ", i, buf[23+p+i]); printk(" iExtension %5u ", buf[23+p+n]); break; default: printk("(unknown) " " Invalid desc subtype:"); break; } buflen -= buf[0]; buf += buf[0]; } } static void parse_videostreaming_interface(struct usb_interface *intf, unsigned char *buf, int buflen) { static const char * const colorPrims[] = { "Unspecified", "BT.709,sRGB", "BT.470-2 (M)", "BT.470-2 (B,G)", "SMPTE 170M", "SMPTE 240M" }; static const char * const transferChars[] = { "Unspecified", "BT.709", "BT.470-2 (M)", "BT.470-2 (B,G)", "SMPTE 170M", "SMPTE 240M", "Linear", "sRGB"}; static const char * const matrixCoeffs[] = { "Unspecified", "BT.709", "FCC", "BT.470-2 (B,G)", "SMPTE 170M (BT.601)", "SMPTE 240M" }; unsigned int i, m, n, p, flags, len; while (buflen > 0) { if (buf[1] != USB_DT_CS_INTERFACE) printk(" Warning: Invalid descriptor "); else if (buf[0] < 3) printk(" Warning: Descriptor too short "); printk(" VideoStreaming Interface Descriptor: " " bLength %5u " " bDescriptorType %5u " " bDescriptorSubtype %5u ", buf[0], buf[1], buf[2]); switch (buf[2]) { case 0x01: /* INPUT_HEADER */ printk("(INPUT_HEADER) "); p = buf[3]; n = buf[12]; if (buf[0] < 13+p*n) printk(" Warning: Descriptor too short "); printk(" bNumFormats %5u " " wTotalLength %5u " " bEndPointAddress %5u " " bmInfo %5u " " bTerminalLink %5u " " bStillCaptureMethod %5u " " bTriggerSupport %5u " " bTriggerUsage %5u " " bControlSize %5u ", p, buf[4] | (buf[5] << 8), buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], n); for (i = 0; i < p; i++) printk( " bmaControls(%2u) %5u ", i, buf[13+p*n]); break; case 0x02: /* OUTPUT_HEADER */ printk("(OUTPUT_HEADER) "); p = buf[3]; n = buf[8]; if (buf[0] < 9+p*n) printk(" Warning: Descriptor too short "); printk(" bNumFormats %5u " " wTotalLength %5u " " bEndpointAddress %5u " " bTerminalLink %5u " " bControlSize %5u ", p, buf[4] | (buf[5] << 8), buf[6], buf[7], n); for (i = 0; i < p; i++) printk( " bmaControls(%2u) %5u ", i, buf[9+p*n]); break; case 0x03: /* STILL_IMAGE_FRAME */ printk("(STILL_IMAGE_FRAME) "); n = buf[4]; m = buf[5+4*n]; if (buf[0] < 6+4*n+m) printk(" Warning: Descriptor too short "); printk(" bEndpointAddress %5u " " bNumImageSizePatterns %3u ", buf[3], n); for (i = 0; i < n; i++) printk(" wWidth(%2u) %5u " " wHeight(%2u) %5u ", i, buf[5+4*i] | (buf[6+4*i] << 8), i, buf[7+4*i] | (buf[8+4*i] << 8)); printk(" bNumCompressionPatterns %3u ", n); for (i = 0; i < m; i++) printk(" bCompression(%2u) %5u ", i, buf[6+4*n+i]); break; case 0x04: /* FORMAT_UNCOMPRESSED */ case 0x10: /* FORMAT_FRAME_BASED */ if (buf[2] == 0x04) { printk("(FORMAT_UNCOMPRESSED) "); len = 27; } else { printk("(FORMAT_FRAME_BASED) "); len = 28; } if (buf[0] < len) printk(" Warning: Descriptor too short "); flags = buf[25]; printk(" bFormatIndex %5u " " bNumFrameDescriptors %5u " " guidFormat %s " " bBitsPerPixel %5u " " bDefaultFrameIndex %5u " " bAspectRatioX %5u " " bAspectRatioY %5u " " bmInterlaceFlags 0x%02x ", buf[3], buf[4], get_guid(&buf[5]), buf[21], buf[22], buf[23], buf[24], flags); printk(" Interlaced stream or variable: %s ", (flags & (1 << 0)) ? "Yes" : "No"); printk(" Fields per frame: %u fields ", (flags & (1 << 1)) ? 1 : 2); printk(" Field 1 first: %s ", (flags & (1 << 2)) ? "Yes" : "No"); printk(" Field pattern: "); switch ((flags >> 4) & 0x03) { case 0: printk("Field 1 only "); break; case 1: printk("Field 2 only "); break; case 2: printk("Regular pattern of fields 1 and 2 "); break; case 3: printk("Random pattern of fields 1 and 2 "); break; } printk(" bCopyProtect %5u ", buf[26]); if (buf[2] == 0x10) printk(" bVariableSize %5u ", buf[27]); break; case 0x05: /* FRAME UNCOMPRESSED */ case 0x07: /* FRAME_MJPEG */ case 0x11: /* FRAME_FRAME_BASED */ if (buf[2] == 0x05) { printk("(FRAME_UNCOMPRESSED) "); n = 25; } else if (buf[2] == 0x07) { printk("(FRAME_MJPEG) "); n = 25; } else { printk("(FRAME_FRAME_BASED) "); n = 21; } len = (buf[n] != 0) ? (26+buf[n]*4) : 38; if (buf[0] < len) printk(" Warning: Descriptor too short "); flags = buf[4]; printk(" bFrameIndex %5u " " bmCapabilities 0x%02x ", buf[3], flags); printk(" Still image %ssupported ", (flags & (1 << 0)) ? "" : "un"); if (flags & (1 << 1)) printk(" Fixed frame-rate "); printk(" wWidth %5u " " wHeight %5u " " dwMinBitRate %9u " " dwMaxBitRate %9u ", buf[5] | (buf[6] << 8), buf[7] | (buf[8] << 8), buf[9] | (buf[10] << 8) | (buf[11] << 16) | (buf[12] << 24), buf[13] | (buf[14] << 8) | (buf[15] << 16) | (buf[16] << 24)); if (buf[2] == 0x11) printk(" dwDefaultFrameInterval %9u " " bFrameIntervalType %5u " " dwBytesPerLine %9u ", buf[17] | (buf[18] << 8) | (buf[19] << 16) | (buf[20] << 24), buf[21], buf[22] | (buf[23] << 8) | (buf[24] << 16) | (buf[25] << 24)); else printk(" dwMaxVideoFrameBufferSize %9u " " dwDefaultFrameInterval %9u " " bFrameIntervalType %5u ", buf[17] | (buf[18] << 8) | (buf[19] << 16) | (buf[20] << 24), buf[21] | (buf[22] << 8) | (buf[23] << 16) | (buf[24] << 24), buf[25]); if (buf[n] == 0) printk(" dwMinFrameInterval %9u " " dwMaxFrameInterval %9u " " dwFrameIntervalStep %9u ", buf[26] | (buf[27] << 8) | (buf[28] << 16) | (buf[29] << 24), buf[30] | (buf[31] << 8) | (buf[32] << 16) | (buf[33] << 24), buf[34] | (buf[35] << 8) | (buf[36] << 16) | (buf[37] << 24)); else for (i = 0; i < buf[n]; i++) printk(" dwFrameInterval(%2u) %9u ", i, buf[26+4*i] | (buf[27+4*i] << 8) | (buf[28+4*i] << 16) | (buf[29+4*i] << 24)); break; case 0x06: /* FORMAT_MJPEG */ printk("(FORMAT_MJPEG) "); if (buf[0] < 11) printk(" Warning: Descriptor too short "); flags = buf[5]; printk(" bFormatIndex %5u " " bNumFrameDescriptors %5u " " bFlags %5u ", buf[3], buf[4], flags); printk(" Fixed-size samples: %s ", (flags & (1 << 0)) ? "Yes" : "No"); flags = buf[9]; printk(" bDefaultFrameIndex %5u " " bAspectRatioX %5u " " bAspectRatioY %5u " " bmInterlaceFlags 0x%02x ", buf[6], buf[7], buf[8], flags); printk(" Interlaced stream or variable: %s ", (flags & (1 << 0)) ? "Yes" : "No"); printk(" Fields per frame: %u fields ", (flags & (1 << 1)) ? 2 : 1); printk(" Field 1 first: %s ", (flags & (1 << 2)) ? "Yes" : "No"); printk(" Field pattern: "); switch ((flags >> 4) & 0x03) { case 0: printk("Field 1 only "); break; case 1: printk("Field 2 only "); break; case 2: printk("Regular pattern of fields 1 and 2 "); break; case 3: printk("Random pattern of fields 1 and 2 "); break; } printk(" bCopyProtect %5u ", buf[10]); break; case 0x0a: /* FORMAT_MPEG2TS */ printk("(FORMAT_MPEG2TS) "); len = buf[0] < 23 ? 7 : 23; if (buf[0] < len) printk(" Warning: Descriptor too short "); printk(" bFormatIndex %5u " " bDataOffset %5u " " bPacketLength %5u " " bStrideLength %5u ", buf[3], buf[4], buf[5], buf[6]); if (len > 7) printk(" guidStrideFormat %s ", get_guid(&buf[7])); break; case 0x0d: /* COLORFORMAT */ printk("(COLORFORMAT) "); if (buf[0] < 6) printk(" Warning: Descriptor too short "); printk(" bColorPrimaries %5u (%s) ", buf[3], (buf[3] <= 5) ? colorPrims[buf[3]] : "Unknown"); printk(" bTransferCharacteristics %5u (%s) ", buf[4], (buf[4] <= 7) ? transferChars[buf[4]] : "Unknown"); printk(" bMatrixCoefficients %5u (%s) ", buf[5], (buf[5] <= 5) ? matrixCoeffs[buf[5]] : "Unknown"); break; default: printk(" Invalid desc subtype:"); break; } buflen -= buf[0]; buf += buf[0]; } } //打印端点描述符 static void dump_endpoint(const struct usb_endpoint_descriptor *endpoint) { static const char * const typeattr[] = { "Control", "Isochronous", "Bulk", "Interrupt" }; static const char * const syncattr[] = { "None", "Asynchronous", "Adaptive", "Synchronous" }; static const char * const usage[] = { "Data", "Feedback", "Implicit feedback Data", "(reserved)" }; static const char * const hb[] = { "1x", "2x", "3x", "(??)" }; unsigned wmax = le16_to_cpu(endpoint->wMaxPacketSize); printk(" Endpoint Descriptor: " " bLength %5u " " bDescriptorType %5u " " bEndpointAddress 0x%02x EP %u %s " " bmAttributes %5u " " Transfer Type %s " " Synch Type %s " " Usage Type %s " " wMaxPacketSize 0x%04x %s %d bytes " " bInterval %5u ", endpoint->bLength, endpoint->bDescriptorType, endpoint->bEndpointAddress, endpoint->bEndpointAddress & 0x0f, (endpoint->bEndpointAddress & 0x80) ? "IN" : "OUT", endpoint->bmAttributes, typeattr[endpoint->bmAttributes & 3], syncattr[(endpoint->bmAttributes >> 2) & 3], usage[(endpoint->bmAttributes >> 4) & 3], wmax, hb[(wmax >> 11) & 3], wmax & 0x7ff, endpoint->bInterval); /* only for audio endpoints */ if (endpoint->bLength == 9) printk(" bRefresh %5u " " bSynchAddress %5u ", endpoint->bRefresh, endpoint->bSynchAddress); } //probe处理函数,有匹配usb设备时调用 static int sheldon_uvc_probe(struct usb_interface *intf, const struct usb_device_id *id) { static int cnt; static int i, j ,k ,l ,m; unsigned char *buffer; int buflen; int desc_len; //int desc_cnt; //根据interface结构体获得usb_device结构体,其中包含了设备描述符 struct usb_device *dev = interface_to_usbdev(intf); //此处需要定义一个描述符结构体 struct usb_device_descriptor *descriptor = &dev->descriptor; //从usb_device结构体中获得配置描述符相关信息 struct usb_host_config *host_config; struct usb_config_descriptor *config; //端点描述符 struct usb_endpoint_descriptor *endpoint; //定义接口联合体描述符结构体,获得 IAD 接口 struct usb_interface_assoc_descriptor *assoc_desc; //定义接口设置信息结构体 struct usb_interface_descriptor *idesc; printk("----sheldon_uvc_probe : cnt = %d---- ",cnt++); //打印第cnt个接口 //打印设备描述符 printk("Device Descriptor: " " bLength %5u " " bDescriptorType %5u " " bcdUSB %2x.%02x " " bDeviceClass %5u " " bDeviceSubClass %5u " " bDeviceProtocol %5u " " bMaxPacketSize0 %5u " " idVendor 0x%04x " " idProduct 0x%04x " " bcdDevice %2x.%02x " " iManufacturer %5u " " iProduct %5u " " iSerial %5u " " bNumConfigurations %5u ", descriptor->bLength, descriptor->bDescriptorType, descriptor->bcdUSB >> 8, descriptor->bcdUSB & 0xff, descriptor->bDeviceClass, descriptor->bDeviceSubClass, descriptor->bDeviceProtocol, descriptor->bMaxPacketSize0, descriptor->idVendor, descriptor->idProduct, descriptor->bcdDevice >> 8, descriptor->bcdDevice & 0xff, descriptor->iManufacturer, descriptor->iProduct, descriptor->iSerialNumber, descriptor->bNumConfigurations); //打印配置描述符 for(i = 0; i < descriptor->bNumConfigurations; i++) { host_config = &dev->config[i]; config = &host_config->desc; printk(" Configuration Descriptor Configuration %d : " " bLength %5u " " bDescriptorType %5u " " wTotalLength %5u " " bNumInterfaces %5u " " bConfigurationValue %5u " " iConfiguration %5u " " bmAttributes 0x%02x ", i, config->bLength, config->bDescriptorType, le16_to_cpu(config->wTotalLength), config->bNumInterfaces, config->bConfigurationValue, config->iConfiguration, config->bmAttributes); //打印接口联合体描述符 assoc_desc = host_config->intf_assoc[0]; printk(" Interface Association: %d " " bLength %5u " " bDescriptorType %5u " " bFirstInterface %5u " " bInterfaceCount %5u " " bFunctionClass %5u " " bFunctionSubClass %5u " " bFunctionProtocol %5u " " iFunction %5u ", i, assoc_desc->bLength, assoc_desc->bDescriptorType, assoc_desc->bFirstInterface, assoc_desc->bInterfaceCount, assoc_desc->bFunctionClass, assoc_desc->bFunctionSubClass, assoc_desc->bFunctionProtocol, assoc_desc->iFunction); //打印具体每个接口的描述符 for(j = 0; j < intf->num_altsetting; j++) { idesc = &intf->altsetting[j].desc; printk(" Interface Descriptor: %d " " bLength %5u " " bDescriptorType %5u " " bInterfaceNumber %5u " " bAlternateSetting %5u " " bNumEndpoints %5u " " bInterfaceClass %5u " " bInterfaceSubClass %5u " " bInterfaceProtocol %5u " " iInterface %5u ", j, idesc->bLength, idesc->bDescriptorType, idesc->bInterfaceNumber, idesc->bAlternateSetting, idesc->bNumEndpoints, idesc->bInterfaceClass, idesc->bInterfaceSubClass, idesc->bInterfaceProtocol, idesc->iInterface); //打印intf接口里的第i个设置的第m个端点的描述符 for (m = 0; m < idesc->bNumEndpoints; m++) { endpoint = &intf->altsetting[j].endpoint[m].desc; dump_endpoint(endpoint); } } //buffer存着设备自定义的描述符(第一个字节描述 长度) buffer = intf->cur_altsetting->extra; //自定义描述符长度 buflen = intf->cur_altsetting->extralen; printk("extra buffer of interface %d ",cnt-1); //desc_cnt = 0; //第几个额外的描述符 k = 0; while(k < buflen) //打印描述符 { desc_len = buffer[k]; //从下一个描述符的第一个字节获得其长度 printk("extra desc %d ",k); for(l = 0 ; l < desc_len; l++ ,k++) //保证k指向下一个描述符的第一个字节 { printk("%02x ", buffer[k]); } //desc_cnt++; printk(" "); } idesc = &intf->cur_altsetting->desc; //判断是CS还是VS, if((buffer[1] == USB_DT_CS_INTERFACE) && (idesc->bInterfaceSubClass == 1)) { parse_videocontrol_interface(intf, buffer, buflen); } if((buffer[1] == USB_DT_CS_INTERFACE) && (idesc->bInterfaceSubClass == 2)) { parse_videostreaming_interface(intf, buffer, buflen); } } return 0; } //disconnect函数,设备断开时调用 static void sheldon_uvc_disconnect(struct usb_interface *intf) { static int cnt; printk("sheldon_uvc_disconnect : cnt = %d ",cnt++); } //1.分配usb_driver结构体 //2.设置 static struct usb_driver sheldon_uvc_driver = { .name = "sheldon_uvc", .id_table = sheldon_uvc_ids, .probe = sheldon_uvc_probe, .disconnect = sheldon_uvc_disconnect, }; static int sheldon_uvc_init(void) { //3.注册 printk("sheldon_uvc_init ~ "); usb_register(&sheldon_uvc_driver); return 0; } static void sheldon_uvc_exit(void) { printk("sheldon_uvc_exit ~ "); usb_deregister(&sheldon_uvc_driver); } module_init(sheldon_uvc_init); module_exit(sheldon_uvc_exit); MODULE_LICENSE("GPL");

    /*-------------附Makefile-----------*/

    KERN_DIR = /usr/src/linux-headers-2.6.31-14-generic/
    
    all:
        make -C $(KERN_DIR) M=`pwd` modules 
    
    clean:
        make -C $(KERN_DIR) M=`pwd` modules clean
        rm -rf modules.order
    
    obj-m    += sheldon_uvc.o
  • 相关阅读:
    C语言归并排序
    三重for循环实现对二维数组的按列排序(JavaScript)
    MySQL创建子视图并查看的时候,字符集报错问题
    Windows下配置lua环境
    《机器学习》周志华西瓜书读书笔记
    《消费金融真经》读书笔记
    北海之行-小纪
    2017中国资产管理行业发展报告——思维导图
    工作小纪
    全球化3.0:世界是红的 (转载)
  • 原文地址:https://www.cnblogs.com/blogs-of-lxl/p/5021933.html
Copyright © 2020-2023  润新知