Android 4.0 USB挂载内核驱动层流程详细分析。
1.platform_device
在arch/arm/mach-msm/Board-xx.c中
static struct platform_device android_usb_device = {
.name = "android_usb",
.id = -1,
.dev = {
.platform_data = &android_usb_pdata, //@1
}
};
static struct android_usb_platform_data android_usb_pdata = {
.update_pid_and_serial_num = usb_diag_update_pid_and_serial_num,
};
在rpc_hsusb.c中:
int usb_diag_update_pid_and_serial_num(uint32_t pid, const char *snum)
{
int ret;
ret = msm_hsusb_send_productID(pid);
if (ret)
return ret;
if (!snum) {
ret = msm_hsusb_is_serial_num_null(1);
if (ret)
return ret;
return 0;
}
ret = msm_hsusb_is_serial_num_null(0);
if (ret)
return ret;
ret = msm_hsusb_send_serial_number(snum);
if (ret)
return ret;
return 0;
}
在内核初始化时,先注册了名为android_usb的设备。
2.platform_driver
在drivers/usb/gadget/Android.c中:
static struct platform_driver android_platform_driver = {
.driver = { .name = "android_usb"},
};
注册了名为android_usb的paltform_driver。但是并不像其他硬件驱动那样,有.probe函数用来匹配驱动。不要着急,慢慢看。
3.module_init
其实Android.c用的是module_init的方式:
static int __init init(void)
{
struct android_dev *dev;
int err;
android_class = class_create(THIS_MODULE, "android_usb"); //在sys/class下创建android_usb目录
if (IS_ERR(android_class)) //错误处理
return PTR_ERR(android_class);
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
dev->functions = supported_functions; //设备支持的一些功能,功能列表
/*
*static struct android_usb_function *supported_functions[] = {
* &rmnet_smd_function,
* &rmnet_sdio_function,
* &rmnet_smd_sdio_function,
* &rmnet_function,
* &diag_function,
* &serial_function,
* &adb_function,
* &ccid_function,
*// &acm_function,
* &mtp_function,
* &ptp_function,
* &rndis_function,
* &mass_storage_function,
* &accessory_function,
* NULL
*};
*/
INIT_LIST_HEAD(&dev->enabled_functions); //应该是加入到队列里的意思吧,使能这些功能
INIT_WORK(&dev->work, android_work);
err = android_create_device(dev); //创建dev,按内核目录来看:
/*
*localhost android_usb # ls
*android0 f_diag f_rmnet f_rndis
*f_accessory f_mass_storage f_rmnet_sdio f_serial
*f_adb f_mtp f_rmnet_smd
*f_ccid f_ptp f_rmnet_smd_sdio
*localhost android_usb #
*/
if (err) {
class_destroy(android_class);
kfree(dev);
return err;
}
_android_dev = dev;
/* Override composite driver functions */
composite_driver.setup = android_setup;
composite_driver.disconnect = android_disconnect;
platform_driver_probe(&android_platform_driver, android_probe); //驱动是通过platform_driver_probe方式进行匹配的,匹配函数位android_probe:
/*
*static int __devinit android_probe(struct platform_device *pdev)
*{
* struct android_usb_platform_data *pdata = pdev->dev.platform_data; //见@1
* struct android_dev *dev = _android_dev;
*
* dev->pdata = pdata;
*
* return 0;
*}
*/
return usb_composite_probe(&android_usb_driver, android_bind); //万能USB驱动,用android_bind进行绑定
}
module_init(init);
static void __exit cleanup(void)
{
usb_composite_unregister(&android_usb_driver);
class_destroy(android_class);
kfree(_android_dev);
_android_dev = NULL;
}
module_exit(cleanup);