This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

USB connect/disconnect event notification in am335x

Hello all,

If any usb mass storage device connected to my custom board(beagle bone black reff ,kernel 3.14 and SDK 8.0) will get asynchronous notifications to application

how to notify an application when a usb flash drive gets connected and which way follow in linux to get usb device added notifications to application, please could you help me on this.

I got some information about syfs and libudev but which is related pci bus (http://www.signal11.us/oss/udev/).

Could you help on this.

Thanks

N Shankar

  • Hi,

    I will ask the USB experts to help on this.

  • It depends on the exact requirement in your application.

    When kernel detects a USB device is inserted or removed, it will emit a udev event. you can use a udev rules to listen to the events and call a corresponding script/application to process.

    Or you can refer to the description of libudev in the link you mentioned to process the udev events. I never used libudev, but based on what I read from this link, libudev is not limited to PCI, it should provide what you need.

    Additionally, the sysfs information on AM335x kernel corresponding to the sysfs PCI information mentioned in the link you mentioned is located at /sys/bus/usb/devices/.

  • Hi Bin,

    Thanks for you reply,

    From udev rules I am able do mount and umount and my application knows that a usb device has been inserted/removed along with the device node (like /dev/sda1, /dev/sdb1 etc). Then I need to mount this device node to a known location in the application.
    hereby my application want know the usb status(if its idle or busy with transferring files) while transferring some files from pen drive to EMMC memory.

    Thanks & regards

    N Shankar

  • shankar naragoni said:
    hereby my application want know the usb status(if its idle or busy with transferring files) while transferring some files from pen drive to EMMC memory.

    I am not sure if this is a question for me. If so, then the kernel does not have an interface to tell user space about the usb bus is busy or not, however, any usb transfer is initiated by the host, so your application should already know if usb is idle or busy.

  • Thank you for reply Bin I learn lot of new things from you, Now I solved usb otg related drivers in kernel,dts and as well as rootfs. after that I am moving to usb application side .  

    any usb transfer is initiated by the host, so your application should already know if usb is idle or busy.


    Could you please give me any suggestion on application side, how can I know if usb pen drive files copy happening or not.

    Thanks in advance

    N Shankar

  • shankar naragoni said:
    how can I know if usb pen drive files copy happening or not.

    The pen drive sends or receives files only when the host side application initiate the transfer, either by a dedicated application or, for example, the 'cp' command, so you know the USB data are transferring when the application/command is running and, the transfer is done when the application/command terminates.

  • Thanks for your inputs Bin,

        we have one more application based requirement i.e switch based turn on and turn off usb ports, By default our usb port switch off in application mode (not in kernel) whenever user want usb connection then only it will work based on input switch pressing.

         for that I am using syfs bellow commands.

       1) echo usb1 > /sys/bus/usb/drivers/usb/unbind   ----> using for driver unload(by default off state)

       2)  echo usb1 > /sys/bus/usb/drivers/usb/bind     ---->using for driver load(whenever switch press then port will work )

    here  unbind working properly(for this usb not detected) but bind command was not working( usb detection failure).

    and bellow commands also I tried  but which was not working.

    usb turn off power cmds:

    echo "0" > /sys/bus/usb/devices/usb1/power/autosuspend
    echo "auto" > /sys/bus/usb/devices/usb1/power/control

    echo "0" > /sys/bus/usb/devices/usb2/power/autosuspend
    echo "auto" > /sys/bus/usb/devices/usb2/power/control

    usb turn on cmds:

    echo "on" > "/sys/bus/usb/devices/usb1/power/level"

    Could please help on this.

    Thanks in advance

    N Shankar.

  • I am not sure if I understand what exactly you want to achieve, but please check if my patch referred in http://e2e.ti.com/support/arm/sitara_arm/f/791/p/436542/1563464#1563464 can do what you want.

  • Thanks Bin,

    I am using kernel 3.14 and sdk8, In our board USB0 port using for otg(not for usb1). I added above patch then compile it no error after that I run bellow commands and test with the usb pen drive its enumerated both cases please find it bellow cmds.


    case 1) root@am335x-evm:~# echo 0 > /sys/kernel/debug/musb-hdrc.0.auto/softconnect

    case 2) root@am335x-evm:~# echo 1 > /sys/kernel/debug/musb-hdrc.0.auto/softconnect

    1) root@am335x-evm:~# cat /sys/kernel/debug/musb-hdrc.1.auto/softconnect
    -1

    2) root@am335x-evm:~# cat /sys/bus/platform/devices/musb-hdrc.1.auto/mode
    b_idle

    3) root@am335x-evm:~# grep -i 'power\|devctl' /sys/kernel/debug/musb-hdrc.1.auto/regdump
    Power       : 00
    DevCtl      : 80

    4) DTS FILE

    &usb {
        status = "okay";

        control@44e10620 {
            status = "okay";
        };

        usb-phy@47401300 {
            status = "okay";
        };

        usb-phy@47401b00 {
            status = "okay";
        };

        usb@47401000 {
            status = "okay";
            dr_mode = "otg";
        };

        usb@47401800 {
            status = "okay";
            /*dr_mode = "host";*/  //COMMENTED  for USB1 to UART3
        };

    root@am335x-evm:~# cat /sys/kernel/debug/musb-hdrc.0.auto/softconnect   // this command also not working


    Is it patch support both ports usb1 and usb0?  please could you tell me how to turn on and turn off usb0.

    Thanks Regards

    N Shankar.

  • Hi Bin LIu,

     Could you please give me reply for above problem, I am waiting for your reply.....

    Thanks in advance

    N Shankar

  • Shankar,

    I am traveling internationally, please expect my response will be delayed.

    shankar naragoni said:

    case 1) root@am335x-evm:~# echo 0 > /sys/kernel/debug/musb-hdrc.0.auto/softconnect

    case 2) root@am335x-evm:~# echo 1 > /sys/kernel/debug/musb-hdrc.0.auto/softconnect

    Just hope you did not run both command one after another immediately. 'echo 0' is for disconnect the enumerated device when a device is already connected/enumerated, and 'echo 1' is for connect/enumerate the inserted device.

    shankar naragoni said:

    1) root@am335x-evm:~# cat /sys/kernel/debug/musb-hdrc.1.auto/softconnect
    -1

    2) root@am335x-evm:~# cat /sys/bus/platform/devices/musb-hdrc.1.auto/mode
    b_idle

    3) root@am335x-evm:~# grep -i 'power\|devctl' /sys/kernel/debug/musb-hdrc.1.auto/regdump
    Power       : 00
    DevCtl      : 80

    Why you did these commands on usb1? we are interested on usb0 here.

    shankar naragoni said:
    root@am335x-evm:~# cat /sys/kernel/debug/musb-hdrc.0.auto/softconnect   // this command also not working

    What do you mean it is not working? what is the output of this command?

    shankar naragoni said:
    Is it patch support both ports usb1 and usb0?  please could you tell me how to turn on and turn off usb0.

    Yes, this patch support both ports.

    Please ensure

    1. for the port in dr_mode=otg, a gadget driver is loaded;

    2. run the 'echo 0', 'echo 1', and 'cat ...' commands above only when a usb device is plugged in the usb port.

  • Thanks for your valuable support ,

    'echo 0' is for disconnect the enumerated device when a device is already connected/enumerated, and 'echo 1' is for connect/enumerate the inserted device.

    The above commands working for me fine but  enumerated devices only. but I want not enumerated devices.


    But my requirement is by default the usb state disconnected and the enumeration device is depend on user input switch means whenever user want usb connection just he press the button. 

    Thanks in advance,

    Thanks & Regards

    N Shankar.

  • Hi Bin,


     Can you please give suggest above query, I am waiting for your response.

    Thanks in advance

    Thanks & regards

    N Shankar.

  • Hi Bin,

       Could please give me response for above query, Is it possible usb turn on/off not enumerated usb devices. I am waiting for your reply.

    Thanks in advance.

    N Shankar.

  • Sorry for my late response, I just came back from the business trip.

    From the original patch, please try to remove the lines shown below, it basically removes the otg state checking when 'echo 0 ...', and directly clears MUSB_DEVCTL_SESSION bit. Then you can 'echo 0 ...' to turn off USB without plug in a device. I did not test this change, but I think it should work for you.

    	if (!strncmp(buf, "0", 1)) {
    -		switch (musb->xceiv->otg->state) {
    -		case OTG_STATE_A_HOST:
     			musb_root_disconnect(musb);
     			reg = musb_readb(musb->mregs, MUSB_DEVCTL);
     			reg &= ~MUSB_DEVCTL_SESSION;
     			musb_writeb(musb->mregs, MUSB_DEVCTL, reg);
    -			break;
    -		default:
    -			break;
    -		}
    
  • Thanks for update Bin,

    I am removed above lines of code from original patch.

    root@am335x-evm:~# echo 0 > /sys/kernel/debug/musb-hdrc.0.auto/softconnect  

    The above command working for turn off the USB without plug in device, Turn off usb device working but resume back not working using "echo 1 >.."

    Thanks in advance.

    Thanks & Regards

    N Shankar.




          

  • Sorry bin your patch working perfectly, Its my mistake only I am removed above lines in only one switch case later I removed both switch cases its working properly.

    Thank you very much your supported.......

    Could you please suggest me about usb power management, I follow below commands usb power off and turn on.

    My Application Powers down using below USB commands:

    1. USB can be powered down by the following commands
           A) echo "0" > "/sys/bus/usb/devices/usb1/power/autosuspend"
           B) echo "auto" > "/sys/bus/usb/devices/usb1/power/control"

    2. USB can be powered up by the following commands
              echo "on" > "/sys/bus/usb/devices/usb1/power/control"

    Is it proper way turn on/off usb power or if any other way could please tell me.


    Thanks in advance,

    N shankar

  • I assume you want to save USB power on AM335x? USB autosuspend does not save power on the AM335x USB host side. so 'echo auto > ...' does not help.

    If you really want to same USB power, you can try to turn off the USB PHY when USB is not in use. Please try the following patch. I did not test it though.

    diff --git a/drivers/usb/musb/musb_debugfs.c b/drivers/usb/musb/musb_debugfs.c
    index a154796..58dd94f 100644
    --- a/drivers/usb/musb/musb_debugfs.c
    +++ b/drivers/usb/musb/musb_debugfs.c
    @@ -286,6 +286,7 @@ static ssize_t musb_softconnect_write(struct file *file,
                            reg = musb_readb(musb->mregs, MUSB_DEVCTL);
                            reg &= ~MUSB_DEVCTL_SESSION;
                            musb_writeb(musb->mregs, MUSB_DEVCTL, reg);
    +                       usb_phy_shutdown(musb->xceiv);
                            break;
                    default:
                            break;
    @@ -294,6 +295,7 @@ static ssize_t musb_softconnect_write(struct file *file,
            } else if (!strncmp(buf, "1", 1)) {
                    switch(musb->xceiv->otg->state) {
                    case OTG_STATE_A_WAIT_BCON:
    +                       usb_phy_init(musb->xceiv);
                            /*
                             * musb_save_context() called in musb_runtime_suspend()
                             * might cache devctl with SESSION bit cleared during
    
    
  • Thank you very much bin,

    I will test and then i will update status.......

    Thanks & regards
    N Shankar
  • Hi Bin,

        I have another request, In our kernel we have to remove debugfs completely from the built kernel. So is any another way to turn of and turn on usb device like sysfs entry.


    Thanks in advance,


    Thanks & Regards

    N Shankar.

  • Yes, it is feasible. I don't have the patch ready, but you can port this softconnect patch to procfs.
    You can use AM335x AMSDK6.0 as an reference: http://arago-project.org/git/projects/?p=linux-am33x.git;a=blob;f=drivers/usb/musb/musb_procfs.c;h=2db7eaca77dc298c6dd22e94c474178733588d70;hb=d5720d33bc7c434f9a023dbb62c795538f976b7a

  • Thanks Bin,

    I will check with above procfs patch and will update soon.

      But we most of the drivers are interfacing  to application through sysfs only, so could please provide sysfs patch.

    Thanks in advance,

    Thanks & Regards

    N Shankar.

  • I am sorry Shankar, but we don't do kernel customization. I don't have the patch ready for sysfs, you either have to implement it by yourself or get an third party.
  • Thanks for your valuable support, Could please guide me on sysfs interface I will start the sysfs programming, can you give any links or books for writing sysfs interfacing.

    Thanks in advance

    Thanks & regards

    N Shankar   

  • Shankar,


    I don't know your background, but Linux Device Drivers (https://lwn.net/Kernel/LDD3/) could be a good start. The book is old, but still useful.

  • Thank you Bin,

        I am facing one more problem in usb mass storage device, when I was removing usb pendrive from board then its getting following error

    root@lifeline:~# [   75.237783] CAUTION: musb: Babble Interrupt Occurred
    [   75.243118] musb_stage0_irq 787: unhandled DISCONNECT transition (a_wait_bcon)
    [   75.252259] usb 1-1: USB disconnect, device number 2
    [   75.331090] musb-hdrc musb-hdrc.0.auto: Restarting MUSB to recover from Babble  

    After that usb enumeration not happening, could please help on this .

    Thanks & Regards

    N Shankar

  • Is this happened after you applied the softconnect patch? If so what is the value of the softconnect sysfs entry and the value of register Power and DevCtl, before you remove the usb device?

  • If this error happens with the softconnect patch, is it my version of the patch for debugfs and your patch for sysfs?
  • Is this happened after you applied the softconnect patch? No

    This happened without adding debugfs patch also I am getting above log message.

    Thanks& Regards
    N Shankar
  • Have you tried multiple thumb drives and other usb devices? do they all cause the Babble interrupt during removal or not?

    shankar naragoni said:

    [   75.331090] musb-hdrc musb-hdrc.0.auto: Restarting MUSB to recover from Babble  

    After that usb enumeration not happening, could please help on this .

    Please run the following command at this point to get the register values.

    # grep -i 'power\|devctl' /sys/kernel/debug/musb-hdrc.0.auto/regdump

  • Have you tried multiple thumb drives and other usb devices? do they all cause the Babble interrupt during removal or not? yes

    Yes I tried multiple pendrive with different companies and memory sizes,

    Please find the following log messages

    root@lifeline:~# [ 1640.121140] usb 1-1: new high-speed USB device number 4 using musb-hdrc

    [ 1640.268348] usb 1-1: New USB device found, idVendor=8564, idProduct=1000

    [ 1640.275471] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3

    [ 1640.282996] usb 1-1: Product: Mass Storage Device

    [ 1640.287942] usb 1-1: Manufacturer: JetFlash

    [ 1640.292369] usb 1-1: SerialNumber: 4SSIX9UR

    [ 1640.308912] usb-storage 1-1:1.0: USB Mass Storage device detected

    [ 1640.331733] scsi2 : usb-storage 1-1:1.0

    [ 1641.333524] scsi 2:0:0:0: Direct-Access     JetFlash Transcend 4GB    8.07 PQ: 0 ANSI: 4

    [ 1641.355288] sd 2:0:0:0: [sda] 7626752 512-byte logical blocks: (3.90 GB/3.63 GiB)

    [ 1641.385797] sd 2:0:0:0: [sda] Write Protect is off

    [ 1641.392683] sd 2:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA

    [ 1641.415593]  sda: sda1

    [ 1641.433899] sd 2:0:0:0: [sda] Attached SCSI removable disk

    [ 1641.733721] FAT-fs (sda1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

    root@lifeline:~#

    root@lifeline:~#

    root@lifeline:~# [ 1648.847149] CAUTION: musb: Babble Interrupt Occurred

    [ 1648.852494] musb_stage0_irq 787: unhandled DISCONNECT transition (a_wait_bcon)

    [ 1648.860352] usb 1-1: USB disconnect, device number 4

    [ 1648.941177] musb-hdrc musb-hdrc.0.auto: Restarting MUSB to recover from Babble

    root@lifeline:~#

    root@lifeline:~#

    umpt@lifeline:~# grep -i 'power\|devctl' /sys/kernel/debug/musb-hdrc.0.auto/regd

    Power       : e0

    DevCtl      : 80

    root@lifeline:~#

    Before  usb pendrive plugin

    umpt@lifeline:~# grep -i 'power\|devctl' /sys/kernel/debug/musb-hdrc.0.auto/regd
    Power       : e0
    DevCtl      : 80

    Thanks & Regards

    N Shankar

  • After babble happened, and plugged in a thumb drive, does the following commnad trigger the enumeration?

    # devmem2 0x47401c60 b 1

    # devmem2 0x47401460 b 1


    If so, what is the dr_mode setting for usb0 in your dts file?

  • I corrected the register address in above post - always messing up with usb0 and usb1...
    In case you only read the email notification, but not come here to read the thread...

    # devmem2 0x47401460 b 1
  • Thanks Bin,

    I run above  the usb0 register command but still usb device not enumerate,Is it any hardware issue. 

    In case you only read the email notification, but not come here to read the thread...

    Can I open new thread for this error.

    Thanks & Regards

    N Shankar.

  • Yes, please open a new thread for this issue.

  • Hi Bin,

    Could you please reply on below thread I am waiting for your reply.

    musb: Babble Interrupt Occurred when I removed the USB cable from USB0

    https://e2e.ti.com/support/arm/sitara_arm/f/791/t/451079


    Thanks in advance,

    Thanks & regards

    N Shankar

  • I replied on that thread.
  • Hi Bin,

     I am updated usb related  reg dump, usbreg script and usb switch schematic and could you please reply on below thread.

    https://e2e.ti.com/support/arm/sitara_arm/f/791/t/451079

    Thanks & regards

    N Shankar