How to detect USB plug and unplug with webcam gadget

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to detect USB plug and unplug with webcam gadget

Jump to solution
1,923 Views
edpearson
Contributor II

Hello,

This is on the i.MX6.  We've have had some success implementing the webcam gadget in our application, but we don't know how to detect when the device is unplugged from the host. 

If the host-side application stops streaming, we can unplug from USB, re-plug in to USB and start streaming again, all without disturbing the main application running in Linux.

The problem occurs if the user disconnects the camera from the host without stopping video  and then reconnects and tries to start video streaming again. Our application freezes, and sometimes the kernel crashes.  If we had a way to detect that the device is unplugged from the host, we could probably take appropriate action to shut down streaming in an orderly manner.   Is there a way to do this?

Thanks very much,

Ed Pearson

0 Kudos
1 Solution
1,453 Views
igorpadykov
NXP Employee
NXP Employee

Hi Ed

one can try to test USBPHYx_STATUS regsiter, please

check sect.66.2.4.4 HS Disconnect Detector, USB Plugged-In Detector,

i.MX6DQ Reference Manual

http://cache.freescale.com/files/32bit/doc/ref_manual/IMX6DQRM.pdf

Best regards

igor

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. Thank you!

-----------------------------------------------------------------------------------------------------------------------

View solution in original post

6 Replies
1,453 Views
edpearson
Contributor II

Hi Ivan,

I have one more question.

We tried detecting that the device is disconnected by detecting when it has stopped receiving SOFs.  We the steps to do this were:

  • add a SOF counter in arcotg_udc.c
  • enable SOF interrupts
  • increment the counter each time a SOF interrupt is received
  • add the necessary logic, including a new IOCTL, to the webcam driver that allows me to test the value of the SOF counter from the application level
  • check the SOF counter at regular intervals

The idea being, if the SOF count stops incrementing we have a disconnect event and can respond appropriately.

What we've found is that, for about 30 seconds after disconnecting from the host, we continue to receive SOF interrupts.  After about 30 seconds, something calls fsl_pullup with is_on == 0, and we stop receiving interrupts, and uvc_function_disable() is called.  (I am not certain of the exact order of events, I'm relying on printk() statements to trace through all this.)

This happens even if the application is not loaded.

I'm told (but can't verify) that the embedded hub will stop sending SOFs immediately on disconnection from the host.  If this is true can you think of anything that might explain the behavior that I'm seeing?

Thanks very much for all the help.

Regards,

Ed Pearson

0 Kudos
1,453 Views
edpearson
Contributor II

I was afraid of that.  But thanks very much for trying to help.

Regards,

Ed

0 Kudos
1,453 Views
igorpadykov
NXP Employee
NXP Employee

Hi Ed

I am afraid with hub this may not work.

Unfortunately I am not aware of methods detecting

disconnection using high level OS APIs of usb driver, sorry.

Best regards

igor

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. Thank you!

-----------------------------------------------------------------------------------------------------------------------

1,453 Views
edpearson
Contributor II

Hi Igor,

Can you please check this code and make sure that I'm doing this correctly?  It should print a message to the console when the state of the plug changes.  I see nothing when I plug or unplug; either I am not addressing the register correctly, or there is something else wrong.  Thanks very much.

Regards,

Ed Pearson

u32 get_plugin_status(void)

{

    static u32 plug_status = 0;

   

    void __iomem *phy_reg = MX6_IO_ADDRESS(USB_PHY0_BASE_ADDR);

    u32 status = fsl_readl(phy_reg+HW_USBPHY_STATUS);

    status &= BM_USBPHY_STATUS_DEVPLUGIN_STATUS;

    if (status != plug_status)

    {

         plug_status = status; // report change in status to console?

         printk("WEBCAM: plug_status changed to %d\n",plug_status);

    }

    if (status != 0)

     return 1;

    else

     return 0;

}

0 Kudos
1,454 Views
igorpadykov
NXP Employee
NXP Employee

Hi Ed

one can try to test USBPHYx_STATUS regsiter, please

check sect.66.2.4.4 HS Disconnect Detector, USB Plugged-In Detector,

i.MX6DQ Reference Manual

http://cache.freescale.com/files/32bit/doc/ref_manual/IMX6DQRM.pdf

Best regards

igor

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. Thank you!

-----------------------------------------------------------------------------------------------------------------------

1,453 Views
edpearson
Contributor II

Hi Igor,

Thanks very much for the prompt reply. Our customer's design includes an onboard hub, connecting both the USB PHY and another USB peripheral to the host, so that an unplug would not be seen directly by the PHY.  I'm a USB newbie, and don't know whether or not an unplug event would be reflected to the PHY.  Do you happen to know if this is the case?  If it is, then I'm all set.  If not, I need to keep digging.

Thanks,

Ed

0 Kudos