android usb gadget分析

本文深入剖析Android USB驱动中的核心文件android/kernel/drivers/usb/gadget/android.c,讲解其如何实现USB上层应用协议。内容涉及宏定义、结构体、设备状态、配置信息等,并详细阐述了在模块初始化、设备绑定与解绑过程中的关键操作,以及如何在dts中配置Android USB设备。
摘要由CSDN通过智能技术生成

Android USB驱动中,上层应用协议里最重要的一个文件是android/kernel/drivers/usb/gadget/android.c。这个文件实现USB的上层应用协议。
首先包含了一些系统级别的头文件,如模块、电源管理、of API等


#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/utsname.h>
#include <linux/platform_device.h>
#include <linux/pm_qos.h>
#include <linux/of.h>

#include <linux/usb/ch9.h>
#include <linux/usb/composite.h>
#include <linux/usb/gadget.h>
#include <linux/usb/android.h>

#include <mach/diag_dload.h>

#include "gadget_chips.h"

/*
 * Kbuild is not very cooperative with respect to linking separately
 * compiled library objects into one module.  So for now we won't use
 * separate compilation ... ensuring init/exit sections work to shrink
 * the runtime footprint, and giving us at least some parts of what
 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
 */ 
//实现上层USB应用协议的c文件
#include "usbstring.c"
#include "config.c"
#include "epautoconf.c"
#include "composite.c"

#ifdef CONFIG_SND_RAWMIDI
#include "f_midi.c"
#endif
#include "f_diag.c"
#include "f_qdss.c"
#include "f_rmnet_smd.c"
#include "f_rmnet_sdio.c"
#include "f_rmnet_smd_sdio.c"
#include "f_rmnet.c"
#include "f_gps.c"
#ifdef CONFIG_SND_PCM   //宏 CONFIG_SND_PCM 有定义
#include "f_audio_source.c"
#endif
#include "f_fs.c"
#include "f_mass_storage.c"
#include "u_serial.c"
#include "u_sdio.c"
#include "u_smd.c"
#include "u_bam.c"
#include "u_rmnet_ctrl_smd.c"
#include "u_rmnet_ctrl_qti.c"
#include "u_ctrl_hsic.c"
#include "u_data_hsic.c"
#include "u_ctrl_hsuart.c"
#include "u_data_hsuart.c"
#include "f_serial.c"
#include "f_acm.c"
#include "f_adb.c"
#include "f_ccid.c"
#include "f_mtp.c"
#include "f_accessory.c"
#define USB_ETH_RNDIS y
#include "f_rndis.c"
#include "rndis.c"
#include "f_qc_ecm.c"
#include "f_mbim.c"
#include "u_bam_data.c"
#include "f_ecm.c"
#include "f_qc_rndis.c"
#include "u_ether.c"
#include "u_qc_ether.c"
#ifdef CONFIG_TARGET_CORE  //宏 CONFIG_TARGET_CORE 未定义
#include "f_tcm.c"
#endif
#ifdef CONFIG_SND_PCM
#include "u_uac1.c"
#include "f_uac1.c"
#endif
#include "f_ncm.c"

//Linux驱动中常见的宏声明,定义了作者、描述、LICENSE、版本号
MODULE_AUTHOR("Mike Lockwood");
MODULE_DESCRIPTION("Android Composite USB Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");

static const char longname[] = "Gadget Android";

/* Default vendor and product IDs, overridden by userspace */ 
// Google自己申请的VID与PID的宏,可以在用户空间的init.**.usb.rc文件里被覆写
#define VENDOR_ID       0x18D1
#define PRODUCT_ID      0x0001

#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
/* f_midi configuration */
#define MIDI_INPUT_PORTS    1
#define MIDI_OUTPUT_PORTS   1
#define MIDI_BUFFER_SIZE    1024
#define MIDI_QUEUE_LENGTH   32

#define ANDROID_DEVICE_NODE_NAME_LENGTH 11 定义 /sys/class/android_usb/ 下的文件节点android0之类的文件名最大长度
结构体android_usb_function抽象了Android自定义的用来hook Linux USB驱动框架中的相应功能的功能函数

struct android_usb_function {
    char *name;
    void *config;

    struct device *dev;
    char *dev_name;
    struct device_attribute **attributes;

    struct android_dev *android_dev;

    /* Optional: initialization during gadget bind */
    int (*init)(struct android_usb_function *, struct usb_composite_dev *);
    /* Optional: cleanup during gadget unbind */
    void (*cleanup)(struct android_usb_function *);
    /* Optional: called when the function is added the list of
     *      enabled functions */
    void (*enable)(struct android_usb_function *);
    /* Optional: called when it is removed */
    void (*disable)(struct android_usb_function *);

    int (*bind_config)(struct android_usb_function *,
               struct usb_configuration *);

    /* Optional: called when the configuration is removed */
    void (*unbind_config)(struct android_usb_function *,
                  struct usb_configuration *);
    /* Optional: handle ctrl requests before the device is configured */
    int (*ctrlrequest)(struct android_usb_function *,
                    struct usb_composite_dev *,
                    const struct usb_ctrlrequest *);
};

结构体 android_usb_function_holder 持有所有的当前支持的可以实现的USB设备

struct android_usb_function_holder {

    struct android_usb_function *f;

    /* for android_conf.enabled_functions */
    struct list_head enabled_list;
};

结构体android_dev抽象了android USB gadget device,即 /sys/class/android_usb下的 android0 等节点(目前就一个)


/**
* struct android_dev - represents android USB gadget device
* @name: device name.
* @functions: an array of all the supported USB function
*    drivers that this gadget support but not necessarily
*    added to one of the gadget configurations.
* @cdev: The internal composite device. Android gadget device
*    is a composite device, such that it can support configurations
*    with more than one function driver.
* @dev: The kernel device that represents this android device.
* @enabled: True if the android gadget is enabled, means all
*    the configurations were set and all function drivers were
*    bind and ready for USB enumeration.
* @disable_depth: Number of times the device was disabled, after
*    symmetrical number of enables the device willl be enabled.
*    Used for controlling ADB userspace disable/enable requests.
* @mutex: Internal mutex for protecting device member fields.
* @pdata: Platform data fetched from 
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值