C Linux USB驱动程序|打印URB缓冲区内容的最佳方法

时间:2016-11-05 15:16:59

标签: c linux usb driver

我是Linux USB驱动程序开发的新手,到目前为止我非常喜欢它!我目前正在为Xbox One控制器创建一个驱动程序,我有一个问题给你们。在下面的代码中,您将看到我在open函数中填写INB中断,并且我想在xb1_int_in_callback()函数中打印URB缓冲区的内容。最好的方法是什么?目前,我正在使用printk(KERN_INFO" int_in_buffer:%s",dev-> int_in_buffer),但是我没有看到打印的整个URB缓冲区内容,并获得了奇怪的字符串打印到dmesg。

很抱歉,如果这是一个简单的问题,我是编程和C的新手,所以我还在学习,但到目前为止我非常喜欢它!

代码:

static void xb1_int_in_callback(struct urb *int_in_urb) {
    struct xb1_controller *dev = int_in_urb->context;

    printk(KERN_INFO "xb1_int_in_callback successfully called");
    printk(KERN_INFO "int_in_buffer: %s", dev->int_in_buffer);
}

static int xb1_open(struct inode *inode, struct file *file) {
    printk(KERN_INFO "open function called..");

    struct xb1_controller *dev;
    struct usb_interface *interface;
    int subminor;
    int retval = 0;

    subminor = iminor(inode);

    interface = usb_find_interface(&xb1_driver, subminor);
    if(!interface) {
        printk(KERN_INFO "Unable to locate interface in open   
        function");
        retval = -ENODEV;
        goto exit;
    }

    dev = usb_get_intfdata(interface);
        if(!dev) {
            printk(KERN_INFO "Unable to locate dev structure in open   
            function");
            retval = -ENODEV;
            goto exit;
        }

    usb_fill_int_urb(dev->int_in_urb, dev->udev, usb_rcvintpipe(dev- 
                 >udev, dev->int_in_endpoint->bEndpointAddress),
                 dev->int_in_buffer, dev->int_in_endpoint- 
                 >wMaxPacketSize, xb1_int_in_callback,
                 dev, dev->int_in_endpoint->bInterval);

    dev->int_in_running = 1;

    retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL);
    if(retval) {
        printk(KERN_INFO "Unable to submit int_in_urb in open 
        function");
        dev->int_in_running = 0;
        goto exit;
    }

    file->private_data = dev;

exit:
    return retval;
}


static int xb1_probe(struct usb_interface *interface, const struct     
usb_device_id *id) {
    struct usb_device *udev = interface_to_usbdev(interface);
    struct xb1_controller *dev = NULL;
    struct usb_host_interface *iface_desc;
    struct usb_endpoint_descriptor *endpoint;
    int i;
    int retval = -ENODEV;

    if(!udev) {
        printk(KERN_INFO "udev is NULL in probe function");
        xb1_abort(dev);
        return retval;
    }

    dev = kzalloc(sizeof(struct xb1_controller), GFP_KERNEL);
    if(!dev) {
        printk(KERN_INFO "Unable to allocate memory for dev in probe   
        function");
        xb1_abort(dev);
        return retval;
    }

    dev->udev = udev;
    dev->interface = interface;

    iface_desc = interface->cur_altsetting;

    for(i=0; i<iface_desc->desc.bNumEndpoints; i++) {
        endpoint = &iface_desc->endpoint[i].desc;

        if(((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==   
        USB_DIR_IN)
           && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == 
              USB_ENDPOINT_XFER_INT)) {

            dev->int_in_endpoint = endpoint;
        }
    }

    if(!dev->int_in_endpoint) {
        printk(KERN_INFO "Unable to locate interrupt in endpoint for 
        interface in probe function");
        xb1_abort(dev);
        return retval;
    }
    else {
        printk(KERN_INFO "Interrupt in endpoint found!");
    }

1 个答案:

答案 0 :(得分:1)

要打印一个小缓冲区(最长64个字节),请使用printk格式:

Raw buffer as hex string:

%*ph    00 01 02  ...  3f
%*phC   00:01:02: ... :3f
%*phD   00-01-02- ... -3f
%*phN   000102 ... 3f

对于较大的缓冲区,请使用print_hex_dump()。参考here