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