How a SD card is mounted over a USB bus as SCSI device

1k Views Asked by At

I have a sd card which is connected on a microchip usb224x controller on im6qp processor based board.

SD signals are going to be converted in a USB dp and dm signal. Now there are two use cases,

use case1: SD card is already inserted before power on,

sd 0:0:0:0: [sda] 249737216 512-byte logical blocks: (128 GB/119 GiB)

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

sd 0:0:0:0: [sda] No Caching mode page found

sd 0:0:0:0: [sda] Assuming drive cache: write through

sda: sda1

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

Now if I remove SD card I don't get any kernel print which says that card is removed.

usecase2: SD card is inserted at running kernel. No print comes that says that SD card is detected as sda.

In case 1 I can mount this SD card and access its contents.In case2 I cannot.

I have this question/confusion

  1. Is user space responsible such as udev to tell if a device is present or not? I tried putting prints in many usb core files and none prints anything. However at the same time I am able to get interrupts on touch device that is using same usb bus but another channel.

I tried getting prints in usb functions in drivers/usb,storage and scsi subsysystem, but no observable prints came.

I tried enabling debugfs prints but I am getting no log even then and thats another issue which I am unable to resolve.

Main problem is I am getting no idea how and who initiates this change of removal and insertion, is it a low level kernel driver which looks for an interrupt and initiate the whole thing or udev such as /sbin/hotplug?

My kernel version is 4.9 and I am using build root for normal usecases, and also android O with same kernel. Same observation I am getting.

1

There are 1 best solutions below

0
Griff0417 On

I am actually working on a device with the same chip and same kernel version. This would be better suited as a comment on your original post, however I don't have 50 points to do so yet (I am aware moderators).

Issues I am having: I can detect add and remove uevents via udevadm monitor when the sd is not mounted. When the sd is mounted I can only detect change events. Major/minor numbers I see are 8,0 and 8,1. Add and remove uevents come from minor number 1 (only while unmounted) while with minor 0 I only see change events (always, no difference if mounted) and which seem to be in a polling manner ("polling" behavior only seen with minor 0) (just going off looks. still looking at source to confirm it is actually polling). Quick insert and removal while mounted will only send one change uevent.

To answer your question '1': from what I understand so far, these uevents are send from the kernel driver, specifically from the scsi mid-layer (drivers/scsi/scsi_lib.c). I have tracked that down to void scsi_evt_thread(struct work_struct *work) where it calls scsi_evt_emit() and eventually calls kobject_uevent_env() which from my understanding is responsible for blasting the uevent out to userspace via netlink.

If you are still debugging this, other information I figured I note along here is that scsi is a three-layered system. You have the top-most layer which for me is drivers/scsi/sd.c, mid-layer which I see as drivers/scsi/scsi.c and the bottom layer of drivers/usb/storage/usb.c (These are the main files for each layer from what I can tell. Learning as I go).

Still looking to figure out how a particular event is set on a specific scsi device. Is a scan of the bus triggered by interrupt or is there a thread which just continuously polls for changes (tdb)?