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

标签 c linux usb driver

我是 Linux USB 驱动程序开发的新手,到目前为止我非常喜欢它!我目前正在为 Xbox One Controller 创建驱动程序,我有一个问题要问你们。在下面的代码中,您会看到我在 open 函数中填充了一个 IN URB 中断,并且我想在 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!");
    }

最佳答案

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

作为十六进制字符串的原始缓冲区:

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

对于更大的缓冲区,使用 print_hex_dump()。引用 here .

关于C Linux USB 驱动程序 |打印 URB 缓冲区内容的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40439829/

相关文章:

c - linux .ko 中用于多个设备的全局变量

linux - 使用 SharePoint URL 和 cURL 进行 URL 编码

linux - 使用双 ssh 从服务器下载文件

android - 以编程方式获取连接的Powerbank设备信息和Powerbank设备的电池容量

USB 端点停止错误

c - 使函数指针成为c中结构的成员

c - 二维数组分配问题(段错误)

使用 dup2 创建管道

linux - 在 Linux 中使用 tar 的不同方法

c - 将数据发送到 Ubuntu 上的 USB GPIO 设备