android--4.2 Vold Mount Management _kernel_usb_uevent (vii)

Source: Internet
Author: User

In the previous android--4.2 Vold Mount management _netlinkmanager (iv) has resolved to Vold is to obtain a kernel event from uevent, to obtain device information, which is through a netlink socket, At present, the whole vold mechanism is analyzed,

Previous Article android--4.2 Vold Mount Management _mountservice (vi) Analysis of the mechanism of the top , here to analyze the lowest kernel uevent event sent to USB device for example!



Writing is not easy, reproduced please specify the source: http://blog.csdn.net/jscese/article/details/39006999


A. Usb_hub_event:

USB driver in the/drivers/usb/directory, this directory has a number of folders, the specific compilation using which is to see kernel. config, you can see the makefile in the directory config* macros are defined.

My side of the kernel version 3.1.10, according to config my kernel with the driver in the/drivers/usb/core directory, USB.C for the entrance.

static int __init usb_init (void) {    int retval;    if (NOUSB) {        pr_info ("%s:usb support disabled\n", usbcore_name);        return 0;    }    retval = Usb_debugfs_init ();//debug the initialization of the file    if (retval)        goto out;    retval = Bus_register (&usb_bus_type);    if (retval)        goto bus_register_failed;    retval = Bus_register_notifier (&usb_bus_type, &USB_BUS_NB);    if (retval)        goto bus_notifier_failed;    retval = Usb_major_init ();    if (retval)        goto major_init_failed;    retval = Usb_register (&usbfs_driver);    if (retval)        goto driver_register_failed;    retval = Usb_devio_init ();    if (retval)        goto usb_devio_init_failed;    retval = Usbfs_init ();    if (retval)        goto fs_init_failed;    retval = Usb_hub_init (); Initialization of cyclic monitoring ...  } Subsys_initcall (Usb_init);

You can see that the initial entrance is Usb_init, in which the USB-related parameters are initialized, which focuses on the analysis and the usb_hub_init ()of the Plug and unplug USB device.

Call to Usb_hub_initin/DRIVERS/USB/CORE/HUB.C:

int Usb_hub_init (void) {    if (Usb_register (&hub_driver) < 0) {        printk (kern_err "%s:can ' t register hub Driver\n ",            usbcore_name);        return-1;    }    Khubd_task = Kthread_run (Hub_thread, NULL, "khubd");//Create a thread, execute Hub_thread function, thread named KHUBD    if (!is_err (Khubd_task))        return 0;    /* Fall through if Kernel_thread failed *    /Usb_deregister (&hub_driver);    PRINTK (kern_err "%s:can ' t start khubd\n", usbcore_name);    return-1;}

Look at the thread function hub_thread:

static int hub_thread (void *__unused) {/    * KHUBD needs to is freezable to avoid intefering with usb-persist     * Port Handover.  Otherwise it might see that a full-speed device     * is gone before the EHCI Controller had handed its port     * The companion Full-speed controller.     *    /Set_freezable ();    do {        hub_events ();//Enter a conditional cycle, continuously monitor        wait_event_freezable (khubd_wait,                !list_empty (&hub_event_list ) ||                Kthread_should_stop ());    } while (!kthread_should_stop () | |!list_empty (&hub_event_list));    Pr_debug ("%s:khubd exiting\n", usbcore_name);    return 0;}

See hub_eventsagain:

static void Hub_events (void) {struct List_head *tmp;    struct Usb_device *hdev;    struct Usb_interface *intf;    struct Usb_hub *hub;  struct device *hub_dev; ... while (1) {/* Grab the first entry at the beginning of the list */SPIN_LOCK_IRQ (&hub_event_lock        );            if (List_empty (&hub_event_list)) {SPIN_UNLOCK_IRQ (&hub_event_lock);        Break  } .../* deal with Port status changes */for (i = 1; I <= hub->descriptor->bnbrports; i++) {            Traverse the USB port to detect port status if (Test_bit (i, hub->busy_bits)) continue;            Connect_change = Test_bit (i, hub->change_bits);            if (!test_and_clear_bit (i, hub->event_bits) &&!connect_change) continue; ret = Hub_port_status (hub, I, &portstatus, &portchange);            Determine if the port status is changed in turn if (Ret < 0) continue; if (porTchange & Usb_port_stat_c_connection) {//Determine port status change clear_port_feature (Hdev, I, USB                _port_feat_c_connection);                Connect_change = 1;                    #if defined (enable_battery_charge) && (mp_usb_mstar==1) if (hdev->parent = = NULL) {                                Usb_bc_enable (HCD, true); } #endif/* Patch for DM all keep high issue */#if (_usb_hs_cur_drive_dm_ Allways_high_patch) && (mp_usb_mstar==1)/* Turn on Overwrite mode */if (hdev->p arent = = NULL) {writeb (READB (void*) (hcd->utmi_base+0x0*2)) | BIT1, (void*) (hcd->utmi_base+0x0*2));                Tern_ov = 1} #endif} ... if (Connect_change) Hub_port_connect_change (Hub, I, Portstatus, portchange);          Handle the changed ports ...     } ...  }}

Through the USB port status cycle monitoring, to handle the USB device Plug and unplug!

Port processing begins with the detection of changes:

static void Hub_port_connect_change (struct usb_hub *hub, int port1, U16 Portstatus, U16 Portchange) {struct usb_    Device *hdev = hub->hdev;    struct Device *hub_dev = hub->intfdev;    struct USB_HCD *HCD = BUS_TO_HCD (Hdev->bus);    unsigned whubcharacteristics = le16_to_cpu (hub->descriptor->whubcharacteristics);   struct Usb_device *udev; ... for (i = 0; i < set_config_tries; i++) {/* reallocate for each attempt, since references * to T   He previous one can escape in various ways */Udev = Usb_alloc_dev (Hdev, Hdev->bus, Port1); ...//Some initial assignment current speed what's/* Reset (NON-USB 3.0 devices) and get descriptor */status = Hub_port_init (Hub, Udev, p Ort1, i); Initial operation on port if (status < 0) goto loop;  ... status = Usb_new_device (Udev); Define a new USB device  } ...} 

The detection part of USB driver is basically like this, mainly through the monitoring of port!


Two. Kobject_uevent_env:

In the above hub_event, the kernel detection and identification of the insertion device is analyzed, to usb_new_device:

int Usb_new_device (struct usb_device *udev) {    int err; ....    /* Tell the world! */    Announce_device (UDEV)///Print out information about the identified USB device, including name Idvendor idproduct    ... Err = Device_add (&udev->dev); Add this device that is recognized ...}

See Device_add again:

int Device_add (struct device *dev) {    struct device *parent = NULL;    /* First, register with generic layer. */    * We require the name to be set before, and pass NULL *    /error = Kobject_add (&dev->kobj, DEV->KOBJ.P Arent, NULL);    .. Kobject_uevent (&dev->kobj, Kobj_add); Send this device event, event type to add ...}

The last call to actually send the Uevent event is under/LIB/KOBJECT_UEVENT.C.kobject_uevent_env
int kobject_uevent_env (struct kobject *kobj, enum kobject_action action, Char *envp_ext[]) {struct KOBJ_U    Event_env *env; const char *action_string = kobject_actions[action];    According to the definition array const char *devpath = NULL; const char *subsystem, ..../* Originating Subsystem */if (uevent_ops && uevent_ops->name) subsyste    m = Uevent_ops->name (Kset, kobj); else subsystem = Kobject_name (&kset->kobj), .../* Environment buffer */env = kzalloc (sizeof (struct KO    bj_uevent_env), Gfp_kernel);    retval = Add_uevent_var (env, "action=%s", action_string);//Add to env by format if (retval) goto exit;    retval = Add_uevent_var (env, "devpath=%s", DEVPATH);    if (retval) goto exit;    retval = Add_uevent_var (env, "subsystem=%s", SUBSYSTEM); if (retval) goto exit;. */Send NetLink message *///////////////////////////////start with NetLink socket Mutex_lock (&uevent_    Sock_mutex); List_for_each_entry (Ue_sk, &uevent_sock_list, LisT) {struct sock *uevent_sock = ue_sk->sk;        struct Sk_buff *skb;        size_t Len;        /* Allocate message with the maximum possible size */len = strlen (action_string) + strlen (Devpath) + 2;        SKB = ALLOC_SKB (len + Env->buflen, Gfp_kernel);            if (SKB) {char *scratch;            /* Add header */scratch = Skb_put (SKB, Len);            sprintf (Scratch, "%[email protected]%s", action_string, Devpath);                /* Copy keys to our continuous event payload buffer */for (i = 0; i < env->envp_idx; i++) {                Len = strlen (Env->envp[i]) + 1;                Scratch = Skb_put (SKB, Len);            strcpy (Scratch, env->envp[i]);            }//Extract the data prepared above NETLINK_CB (SKB). Dst_group = 1;                                retval = netlink_broadcast_filtered (Uevent_sock, SKB,//Send function 0, 1, Gfp_kernel,                          Kobj_bcast_filter,      Kobj); /* ENOBUFS should be handled in userspace */if (retval =-enobufs | | retval = =-esrch) retval        = 0;    } else retval =-enomem; } mutex_unlock (&uevent_sock_mutex); ...}

Finally through the netlink_broadcast_filtered send device uevent information, for kernel in the USB driver I touch not much, here is just a brief analysis of the vold to send uevent the process,

Many of these complex processes I did not write, and later have the opportunity to study USB driver!




android--4.2 Vold Mount Management _kernel_usb_uevent (vii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.