OrangePi Zero 3学习笔记(Android篇)6 - hid-ft260
目录
1. 将hid-ft260.c拷贝到Android目录内
2. 修改hid-ids.h
3. 修改hid-quirks.c
4. 修改Kconfig
5. 修改Makefile
6. 配置内核
7. 编译内核
8. 增加权限
9. 验证
在Android中添加驱动模块ko文件,以hid-ft260为例。
1. 将hid-ft260.c拷贝到Android目录内
将下载hid-ft260文件夹中hid-ft260.c拷贝到目录/longan/kernel/linux-5.4/drivers/hid下。
2. 修改hid-ids.h
longan/kernel/linux-5.4/drivers/hid/hid-ids.h
查找
#define USB_VENDOR_ID_FUTURE_TECHNOLOGY 0x0403
在其后添加FT260的PID。
#define USB_DEVICE_ID_FT260 0x6030
3. 修改hid-quirks.c
longan/kernel/linux-5.4/drivers/hid/hid-quirks.c
参考CP2112添加
#if IS_ENABLED(CONFIG_HID_FT260){ HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY, USB_DEVICE_ID_FT260) },
#endif
4. 修改Kconfig
longan/kernel/linux-5.4/drivers/hid/Kconfig
添加下面的信息
config HID_FT260tristate "FTDI FT260 HID USB-to-UART/I2C/GPIO Bridge support"depends on USB_HID && HIDRAW && I2C && GPIOLIBselect GPIOLIB_IRQCHIP---help---Support for FTDI FT260 HID USB-to-UART/I2C/GPIO Bridge support.This is a HID device driver which registers as an i2c adapter, an uart adapter and gpiochip to expose these functions of the FT260. The customizable USB descriptor fields are exposed as sysfs attributes.
5. 修改Makefile
longan/kernel/linux-5.4/drivers/hid/Makefile
增加
obj-$(CONFIG_HID_FT260) += hid-ft260.o
6. 配置内核
和编译内核类似,在编译前,先执行
./build.sh menuconfig
进入菜单device drivers -> HID support -> Special HID drivers
在这一级列表中可以找到刚刚添加的FT260选项。
7. 编译内核
第一次编译会提示错误,找不到头文件 minmax.h,可以在目录内查找一下这个文件是否存在,可以看到AOSP里面的文件名改为了win_minmax.h,修改hid-ft260.c
#include <linux/win_minmax.h>
第二次编译错误:
error: implicit declaration of function 'timer_delete_sync' [-Werror,-Wimplicit-function-declaration]
以关键字wakeup_timer搜一下hid-ft260.c里面的代码,找到timer_setup,再找一下对应的函数原型, 在timer.h里面可以找到类似的函数
#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)extern int del_timer_sync(struct timer_list *timer);
#else
# define del_timer_sync(t) del_timer(t)
#endif
直接替换函数。
//timer_delete_sync(&port->wakeup_timer);
del_timer_sync(&port->wakeup_timer);
第三个错误
error: incompatible function pointer types initializing 'int (*)(struct tty_struct *)' with an expression of type 'unsigned int (struct tty_struct *)' [-Werror,-Wincompatible-function-pointer-types].write_room = ft260_uart_write_room,^~~~~~~~~~~~~~~~~~~~~error: incompatible function pointer types initializing 'int (*)(struct tty_struct *)' with an expression of type 'unsigned int (struct tty_struct *)' [-Werror,-Wincompatible-function-pointer-types].chars_in_buffer = ft260_uart_chars_in_buffer,^~~~~~~~~~~~~~~~~~~~~~~~~~error: incompatible function pointer types initializing 'void (*)(struct tty_struct *, struct ktermios *)' with an expression of type 'void (struct tty_struct *, const struct ktermios *)' [-Werror,-Wincompatible-function-pointer-types].set_termios = ft260_uart_set_termios,
这3个错误是因为函数原型不同(Linux和Android不一样),需要把这3个函数改为
#if defined(ANDROID) || defined(__ANDROID__)
static int ft260_uart_write_room(struct tty_struct *tty)
#else
static unsigned int ft260_uart_write_room(struct tty_struct *tty)
#endif
{struct ft260_device *port = tty->driver_data;return kfifo_avail(&port->xmit_fifo);
}#if defined(ANDROID) || defined(__ANDROID__)
static int ft260_uart_chars_in_buffer(struct tty_struct *tty)
#else
static unsigned int ft260_uart_chars_in_buffer(struct tty_struct *tty)
#endif
{struct ft260_device *port = tty->driver_data;return kfifo_len(&port->xmit_fifo);
}#if defined(ANDROID) || defined(__ANDROID__)
static void ft260_uart_set_termios(struct tty_struct *tty,struct ktermios *old_termios)
#else
static void ft260_uart_set_termios(struct tty_struct *tty,const struct ktermios *old_termios)
#endif
{struct ft260_device *port = tty->driver_data;ft260_uart_change_speed(port, &tty->termios, NULL);
}
第四个错误:
error: implicit declaration of function 'hid_is_usb' [-Werror,-Wimplicit-function-declaration]
没有hid_is_usb这个函数,所以直接去掉这个判断即可。
//if (!hid_is_usb(hdev))// return -EINVAL;
8. 增加权限
在device/softwinner/apollo/common/system/init.sun50iw9p1.rc中添加FT260串口ttyFT*的权限
on post-fs-data# create file for audio dump datamkdir /data/vendor/hardware/audio_d 0777 audio audiomkdir /data/audio_d 0777 media mediachown system system /dev/nsichmod 0660 /dev/nsichmod 0666 /dev/ttyAS5chmod 0666 /dev/ttyFT*chmod 0666 /dev/ttyi2c-*
9. 验证
进Android Shell
apollo-p2:/ $ cd /sys/bus/i2c/devices/
apollo-p2:/sys/bus/i2c/devices $ ls
5-0036 i2c-0 i2c-3 i2c-5
apollo-p2:/sys/bus/i2c/devices $ cd i2c-0
apollo-p2:/sys/bus/i2c/devices/i2c-0 $ ls
delete_device i2c-dev name new_device power subsystem uevent waiting_for_supplier
apollo-p2:/sys/bus/i2c/devices/i2c-0 $ cat name
FT260 usb-i2c bridge