Android 如何实现usb驱动自动安装?

前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN:
http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文

 

如何实现usb驱动自动安装?
 
具体的需求可能是:连上usb后,默认选项为内置光盘(cdrom)模式,自动运行内置于光盘中的驱动程序,安装完成后,再切回到usb mass stoage(ums)模式
[KEYWORD]
usb默认连接,Built-in cd-rom,BICR,CD-ROM,cdrom,内置光盘,自动安装驱动,自动运行,内置驱动程序
[SOLUTION]
以下提供一个实现的思路:
1,将您的驱动程序做成一个iso文件,制作工具可在网上找。
2,修改USB连接后的默认功能为bicr。如何将usb默认连接方式设置为bicr,请参考FAQ04464
3,将iso文件添加进BICR,具体请参考FAQ05690 How to add ISO files into BICR
4,需要修改您的驱动程序。发对应的cmd REZERO_UNIT,根据cmnd[1] 参数不同,来决定是切换到UMS还是BICR。
       1). 我们的code中cmnd[1] 参数默认为0xD和0xB,您可以定制自己的cmd。
       2).我们的code中的实现是切换到UMS,您可以定制切换到其他usb模式,如MTP。
            可以通过修改usbdevicemanager.java实现
                    public void updateState(String state) {
......
} else if ("REZEROCMD".equals(state)) {
/*When recieve REZEROCMD, it means that PC has installed a proper driver, so can switch to other USB function(UMS)*/
Slog.w(TAG, "PC knows me");
mIsPcKnowMe = true;
setCurrentFunctions(UsbManager.USB_FUNCTION_MASS_STORAGE, false); //修改此处的USB_FUNCTION_MASS_STORAGE为USB_FUNCTION_MTP
return;
......
附上手机侧相关code:
此功能相关的code在以下文件,可以参考了解手机侧具体实现:
1. f_mass_storage.c
 static int do_scsi_command(struct fsg_common *common)
{
  case REZERO_UNIT:
  printk("Get REZERO_UNIT command = %x\r\n", common->cmnd[1]);
  if (common->cmnd[1] == 0xB)
   common->android_callback(1);
  else if (common->cmnd[1] == 0xD)
   common->android_callback(2);
......
}
2. android.c
static void android_work(struct work_struct *data)
{
 ......
 char *rezero_event[2] = { "USB_STATE=REZEROCMD", NULL };
 char *showcdrom_event[2] = { "USB_STATE=SHOWCDROMCMD", NULL };
 ......
 if (dev->rezero_cmd == 1) {
  uevent_envp_cdrom = rezero_event;
  dev->rezero_cmd = 0;
 } else if (dev->rezero_cmd == 2) {
  uevent_envp_cdrom = showcdrom_event;
  dev->rezero_cmd = 0;
 }
......
 if (uevent_envp_cdrom) {
  kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, uevent_envp_cdrom);
  xlog_printk(ANDROID_LOG_INFO, USB_LOG, "%s: sent uevent %s\n", __func__, uevent_envp_cdrom[0]);
 } else {
  xlog_printk(ANDROID_LOG_INFO, USB_LOG, "%s: did not send zero uevent\n", __func__);
 }

}
void mass_storage_callback(unsigned char cmd_type)
{
 struct android_dev *dev = _android_dev;
 if (cmd_type != 0)
  dev->rezero_cmd = cmd_type;
 schedule_work(&dev->work);
}
3. usbdevicemanager.java
        public void updateState(String state) {
            ......
            int connected, configured;
            if (DEBUG) SXlog.d(TAG, "updateState - " + state);
            Message msg;
            ...
            } else if ("REZEROCMD".equals(state)) {
                /*When recieve REZEROCMD, it means that PC has installed a proper driver, so can switch to other USB function(UMS)*/
                Slog.w(TAG, "PC knows me");
                mIsPcKnowMe = true;
                setCurrentFunctions(UsbManager.USB_FUNCTION_MASS_STORAGE, false);
                return;
            } else if ("SHOWCDROMCMD".equals(state)) {
                /*When recieve REZEROCMD, it means that PC has installed a proper driver, so can switch to other USB function(UMS)*/
                Slog.w(TAG, "SHOW CD-ROM CMD");
                setCurrentFunctions(UsbManager.USB_FUNCTION_BICR, false);
                return;
                ......
}

Android 如何实现usb驱动自动安装?,布布扣,bubuko.com

Android 如何实现usb驱动自动安装?

上一篇:Windows下编译valgrind for Android


下一篇:Solon 开发,五、切面与环绕拦截