urb status

int status
    When the urb is finished, or being processed by the USB core, this variable is set
    to the current status of the urb. The only time a USB driver can safely access this
    variable is in the urb completion handler function (described in the section
    “Completing Urbs: The Completion Callback Handler”). This restriction is to
    prevent race conditions that occur while the urb is being processed by the USB
    core. For isochronous urbs, a successful value (0) in this variable merely indi-
    cates whether the urb has been unlinked. To obtain a detailed status on isochro-
    nous urbs, the iso_frame_desc variables should be checked.


    Valid values for this variable include:
0
    The urb transfer was successful.
-ENOENT
    The urb was stopped by a call to usb_kill_urb.
-ECONNRESET
    The urb was unlinked by a call to usb_unlink_urb, and the transfer_flags
    variable of the urb was set to URB_ASYNC_UNLINK.
-EINPROGRESS
    The urb is still being processed by the USB host controllers. If your driver
    ever sees this value, it is a bug in your driver.
-EPROTO
    One of the following errors occurred with this urb:
        ? A bitstuff error happened during the transfer.
        ? No response packet was received in time by the hardware.
-EILSEQ
    There was a CRC mismatch in the urb transfer.
-EPIPE
    The endpoint is now stalled. If the endpoint involved is not a control end-
    point, this error can be cleared through a call to the function usb_clear_halt.
-ECOMM
    Data was received faster during the transfer than it could be written to sys-
    tem memory. This error value happens only for an IN urb.
-ENOSR
    Data could not be retrieved from the system memory during the transfer fast
    enough to keep up with the requested USB data rate. This error value hap-
    pens only for an OUT urb.
-EOVERFLOW
    A “babble” error happened to the urb. A “babble” error occurs when the
    endpoint receives more data than the endpoint’s specified maximum packet
    size.
-EREMOTEIO
    Occurs only if the URB_SHORT_NOT_OK flag is set in the urb’s transfer_flags
    variable and means that the full amount of data requested by the urb was
    not received.
-ENODEV
    The USB device is now gone from the system.
-EXDEV
    Occurs only for a isochronous urb and means that the transfer was only par-
    tially completed. In order to determine what was transferred, the driver
    must look at the individual frame status.
-EINVAL
    Something very bad happened with the urb. The USB kernel documentation
    describes what this value means:
         ISO madness, if this happens: Log off and go home
    It also can happen if a parameter is incorrectly set in the urb stucture or if an
    incorrect function parameter in the usb_submit_urb call submitted the urb to
    the USB core.
-ESHUTDOWN
    There was a severe error with the USB host controller driver; it has now
    been disabled, or the device was disconnected from the system, and the urb
    was submitted after the device was removed. It can also occur if the configu-
    ration was changed for the device, while the urb was submitted to the
    device.


    Generally, the error values -EPROTO, -EILSEQ, and -EOVERFLOW indicate hardware
problems with the device, the device firmware, or the cable connecting the
device to the computer.

 源地址:http://hi.baidu.com/zengzhaonong/item/9fda4f191e4a79623f87ce57

07-15
### URB 提交失败的处理方法 当调用 `usb_submit_urb()` 函数提交一个 URB(USB Request Block)时,如果返回值不为 0,则表示提交失败。此时需要分析可能的原因并进行相应的处理。常见的失败原因包括内存分配失败、设备未连接或端点状态异常等。 在提交 URB 到 USB 核心后,直到完成函数被调用之前,不应访问 URB 中的任何成员,否则可能导致不可预知的行为[^2]。因此,在处理提交失败的情况时,应首先检查返回值,并根据具体的错误码采取适当措施,例如重新提交 URB 或释放相关资源。 ```c int status = usb_submit_urb(urb, GFP_KERNEL); if (status) { // 处理提交失败的情况 printk(KERN_ERR "Failed to submit URB: %d\n", status); usb_free_urb(urb); // 释放 URB 资源 } ``` ### 设备通信问题的排查与解决 在 USB 子系统中,URB 完成后由 USB 主机控制器驱动通知 USB 设备驱动。如果设备通信出现问题,可以检查是否正确初始化了 URB,并确保数据缓冲区是使用 `kmalloc()` 动态分配的,而不是静态缓冲区。此外,对于中断类型的 URB,使用 `usb_fill_int_urb()` 函数来初始化;而对于批量传输类型的 URB,则应使用 `usb_fill_bulk_urb()` 函数来初始化。 以下是一个初始化批量传输 URB 的示例代码: ```c struct urb *urb = usb_alloc_urb(0, GFP_KERNEL); if (!urb) { // 分配 URB 失败 return -ENOMEM; } // 初始化批量传输 URB usb_fill_bulk_urb(urb, dev->udev, pipe, buffer, buffer_size, completion_handler, dev); // 提交 URB int status = usb_submit_urb(urb, GFP_KERNEL); if (status) { // 处理提交失败的情况 usb_free_urb(urb); } ``` 在初始化过程中,确保管道参数是通过 `usb_sndbulkpipe()` 或 `usb_rcvbulkpipe()` 创建的,并且完成处理函数和上下文参数设置正确[^4]。 ### URB 完成后的处理 一旦 URB 完成,USB 主机控制器驱动会调用完成处理函数。在此函数中,可以检查 URB 的状态以确定传输是否成功,并根据需要执行后续操作。例如,如果传输失败,可以选择重新提交相同的 URB 或者采取其他恢复措施。 ```c void completion_handler(struct urb *urb) { if (urb->status) { // 检查 URB 状态,处理传输错误 printk(KERN_ERR "URB transfer failed: %d\n", urb->status); // 可以选择重新提交 URB 或者释放资源 } else { // 传输成功,处理接收到的数据或准备下一次传输 } // 释放 URB 资源 usb_free_urb(urb); } ``` 通过上述方法,可以有效地处理 URB 提交失败以及设备通信问题,从而保证 USB 设备能够稳定地工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值