From 9863ba3871e029501bc591a290f44a174108e648 Mon Sep 17 00:00:00 2001 From: YusukeKato Date: Tue, 26 Nov 2024 11:48:02 +0900 Subject: [PATCH] =?UTF-8?q?=E5=8F=B3=E3=81=A8=E5=B7=A6=E3=81=A7=E3=81=9D?= =?UTF-8?q?=E3=82=8C=E3=81=9E=E3=82=8C=E7=94=A8=E6=84=8F=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=82=8B=E9=96=A2=E6=95=B0=E3=82=92=E4=B8=80?= =?UTF-8?q?=E3=81=A4=E3=81=AB=E3=81=BE=E3=81=A8=E3=82=81=E3=82=8B=EF=BC=88?= =?UTF-8?q?=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF=E3=83=AA=E3=83=B3?= =?UTF-8?q?=E3=82=B0=EF=BC=89=20(#93)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drivers/rtmouse.h | 3 + src/drivers/rtmouse_dev.c | 95 +++++++++++++----------------- src/drivers/rtmouse_i2c.c | 117 ++++++++++++------------------------- src/drivers/rtmouse_main.c | 4 +- 4 files changed, 81 insertions(+), 138 deletions(-) diff --git a/src/drivers/rtmouse.h b/src/drivers/rtmouse.h index a11c390..b2cef95 100644 --- a/src/drivers/rtmouse.h +++ b/src/drivers/rtmouse.h @@ -54,6 +54,9 @@ // Raspberry Pi 4 B : 4 #define RASPBERRYPI 2 +#define DEV_RIGHT 0 +#define DEV_LEFT 1 + /* --- Device ID --- */ #define ID_DEV_LED 0 #define ID_DEV_SWITCH 1 diff --git a/src/drivers/rtmouse_dev.c b/src/drivers/rtmouse_dev.c index d124d64..57ca4a6 100644 --- a/src/drivers/rtmouse_dev.c +++ b/src/drivers/rtmouse_dev.c @@ -229,10 +229,10 @@ static int getPWMCount(int freq) } /* - * left motor function + * motor function * called by parseMotorCmd() and rawmotor_l_write() */ -static void set_motor_l_freq(int freq) +static void set_motor_freq(int freq, const int dev_side) { int dat; @@ -244,65 +244,50 @@ static void set_motor_l_freq(int freq) } if (freq == 0) { - rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_OUTPUT); + if (dev_side == DEV_LEFT) { + rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_OUTPUT); + } else if (dev_side == DEV_RIGHT) { + rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_OUTPUT); + } return; } else { - rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_ALT0); + if (dev_side == DEV_LEFT) { + rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_ALT0); + } else if (dev_side == DEV_RIGHT) { + rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_ALT0); + } } if (freq > 0) { - motor_l_freq_is_positive = 1; - rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE); + if (dev_side == DEV_LEFT) { + motor_l_freq_is_positive = 1; + rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE); + } else if (dev_side == DEV_RIGHT) { + motor_r_freq_is_positive = 1; + rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE); + } } else { - motor_l_freq_is_positive = 0; - rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE); - freq = -freq; + if (dev_side == DEV_LEFT) { + motor_l_freq_is_positive = 0; + rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE); + freq = -freq; + } else if (dev_side == DEV_RIGHT) { + motor_r_freq_is_positive = 0; + rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE); + freq = -freq; + } } dat = getPWMCount(freq); - rpi_pwm_write32(RPI_PWM_RNG1, dat); - rpi_pwm_write32(RPI_PWM_DAT1, dat >> 1); - - return; -} - -/* - * right motor function - * called by parseMotorCmd() and rawmotor_r_write() - */ -static void set_motor_r_freq(int freq) -{ - int dat; - - rpi_gpio_function_set(BUZZER_BASE, RPI_GPF_OUTPUT); - - // Reset uncontrollable frequency to zero. - if (abs(freq) < MOTOR_UNCONTROLLABLE_FREQ) { - freq = 0; - } - - if (freq == 0) { - rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_OUTPUT); - return; - } else { - rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_ALT0); - } - - if (freq > 0) { - motor_r_freq_is_positive = 1; - rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE); - } else { - motor_r_freq_is_positive = 0; - rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE); - freq = -freq; + if (dev_side == DEV_LEFT) { + rpi_pwm_write32(RPI_PWM_RNG1, dat); + rpi_pwm_write32(RPI_PWM_DAT1, dat >> 1); + } else if (dev_side == DEV_RIGHT) { + rpi_pwm_write32(RPI_PWM_RNG2, dat); + rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1); } - dat = getPWMCount(freq); - - rpi_pwm_write32(RPI_PWM_RNG2, dat); - rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1); - return; } @@ -326,13 +311,13 @@ static int parseMotorCmd(const char __user *buf, size_t count, int *ret) mutex_lock(&lock); - set_motor_l_freq(l_motor_val); - set_motor_r_freq(r_motor_val); + set_motor_freq(l_motor_val, DEV_LEFT); + set_motor_freq(r_motor_val, DEV_RIGHT); msleep_interruptible(time_val); - set_motor_l_freq(0); - set_motor_r_freq(0); + set_motor_freq(0, DEV_LEFT); + set_motor_freq(0, DEV_RIGHT); mutex_unlock(&lock); @@ -751,7 +736,7 @@ static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf, DRIVER_NAME, __func__); return ret; } - set_motor_l_freq(freq); + set_motor_freq(freq, DEV_LEFT); return count; } @@ -772,7 +757,7 @@ static ssize_t rawmotor_r_write(struct file *filep, const char __user *buf, return ret; } - set_motor_r_freq(freq); + set_motor_freq(freq, DEV_RIGHT); return count; } diff --git a/src/drivers/rtmouse_i2c.c b/src/drivers/rtmouse_i2c.c index a63acee..e219212 100644 --- a/src/drivers/rtmouse_i2c.c +++ b/src/drivers/rtmouse_i2c.c @@ -68,77 +68,9 @@ static struct i2c_driver i2c_counter_driver = { /* -- Device Addition -- */ MODULE_DEVICE_TABLE(i2c, i2c_counter_id); -static int rtcntr_i2c_create_cdev(struct rtcnt_device_info *dev_info) -{ - int minor; - int alloc_ret = 0; - int cdev_err = 0; - dev_t dev; - - /* 空いているメジャー番号を確保する */ - alloc_ret = alloc_chrdev_region(&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT], - DEVNAME_CNTR); - if (alloc_ret != 0) { - printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret); - return -1; - } - - /* 取得したdev( = メジャー番号 + マイナー番号) - * からメジャー番号を取得して保持しておく */ - dev_info->device_major = MAJOR(dev); - dev = MKDEV(dev_info->device_major, DEV_MINOR); - - /* cdev構造体の初期化とシステムコールハンドラテーブルの登録 */ - cdev_init(&dev_info->cdev, &dev_fops[ID_DEV_CNT]); - dev_info->cdev.owner = THIS_MODULE; - - /* このデバイスドライバ(cdev)をカーネルに登録する */ - cdev_err = cdev_add(&dev_info->cdev, dev, NUM_DEV[ID_DEV_CNT]); - if (cdev_err != 0) { - printk(KERN_ERR "cdev_add = %d\n", alloc_ret); - unregister_chrdev_region(dev, NUM_DEV[ID_DEV_CNT]); - return -1; - } - - /* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */ -#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0) - dev_info->device_class = class_create(THIS_MODULE, DEVNAME_CNTR); -#else - dev_info->device_class = class_create(DEVNAME_CNTR); -#endif - - if (IS_ERR(dev_info->device_class)) { - printk(KERN_ERR "class_create\n"); - cdev_del(&dev_info->cdev); - unregister_chrdev_region(dev, NUM_DEV[ID_DEV_CNT]); - return -1; - } - - for (minor = DEV_MINOR; minor < DEV_MINOR + NUM_DEV[ID_DEV_CNT]; - minor++) { - - struct device *dev_ret; - dev_ret = device_create(dev_info->device_class, NULL, - MKDEV(dev_info->device_major, minor), - NULL, "rtcounter_r%d", minor); - - /* デバイスファイル作成の可否を判定 */ - if (IS_ERR(dev_ret)) { - /* デバイスファイルの作成に失敗した */ - printk(KERN_ERR "device_create failed minor = %d\n", - minor); - /* リソースリークを避けるために登録された状態cdevを削除する - */ - cdev_del(&(cdev_array[cdev_index])); - return PTR_ERR(dev_ret); - } - } - - return 0; -} - // called by rtcnt_i2c_probe() -static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info) +static int rtcnt_i2c_create_cdev(struct rtcnt_device_info *dev_info, + const int dev_side) { int minor; int alloc_ret = 0; @@ -146,8 +78,13 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info) dev_t dev; /* 空いているメジャー番号を確保する */ - alloc_ret = alloc_chrdev_region(&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT], - DEVNAME_CNTL); + if (dev_side == DEV_LEFT) { + alloc_ret = alloc_chrdev_region( + &dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT], DEVNAME_CNTL); + } else if (dev_side == DEV_RIGHT) { + alloc_ret = alloc_chrdev_region( + &dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT], DEVNAME_CNTR); + } if (alloc_ret != 0) { printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret); return -1; @@ -172,9 +109,19 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info) /* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */ #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0) - dev_info->device_class = class_create(THIS_MODULE, DEVNAME_CNTL); + if (dev_side == DEV_LEFT) { + dev_info->device_class = + class_create(THIS_MODULE, DEVNAME_CNTL); + } else if (dev_side == DEV_RIGHT) { + dev_info->device_class = + class_create(THIS_MODULE, DEVNAME_CNTR); + } #else - dev_info->device_class = class_create(DEVNAME_CNTL); + if (dev_side == DEV_LEFT) { + dev_info->device_class = class_create(DEVNAME_CNTL); + } else if (dev_side == DEV_RIGHT) { + dev_info->device_class = class_create(DEVNAME_CNTR); + } #endif if (IS_ERR(dev_info->device_class)) { @@ -189,9 +136,17 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info) minor++) { struct device *dev_ret; - dev_ret = device_create(dev_info->device_class, NULL, - MKDEV(dev_info->device_major, minor), - NULL, "rtcounter_l%d", minor); + if (dev_side == DEV_LEFT) { + dev_ret = + device_create(dev_info->device_class, NULL, + MKDEV(dev_info->device_major, minor), + NULL, "rtcounter_l%d", minor); + } else if (dev_side == DEV_RIGHT) { + dev_ret = + device_create(dev_info->device_class, NULL, + MKDEV(dev_info->device_major, minor), + NULL, "rtcounter_r%d", minor); + } /* デバイスファイル作成の可否を判定 */ if (IS_ERR(dev_ret)) { @@ -260,10 +215,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client, /* create character device */ if ((int)(id->driver_data) == 0) { - if (rtcntl_i2c_create_cdev(dev_info)) + if (rtcnt_i2c_create_cdev(dev_info, DEV_LEFT)) return -ENOMEM; } else if ((int)(id->driver_data) == 1) { - if (rtcntr_i2c_create_cdev(dev_info)) + if (rtcnt_i2c_create_cdev(dev_info, DEV_RIGHT)) return -ENOMEM; } @@ -301,10 +256,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client) /* create character device */ if ((int)(id->driver_data) == 0) { - if (rtcntl_i2c_create_cdev(dev_info)) + if (rtcnt_i2c_create_cdev(dev_info, DEV_LEFT)) return -ENOMEM; } else if ((int)(id->driver_data) == 1) { - if (rtcntr_i2c_create_cdev(dev_info)) + if (rtcnt_i2c_create_cdev(dev_info, DEV_RIGHT)) return -ENOMEM; } diff --git a/src/drivers/rtmouse_main.c b/src/drivers/rtmouse_main.c index df071f5..0f415a1 100644 --- a/src/drivers/rtmouse_main.c +++ b/src/drivers/rtmouse_main.c @@ -3,7 +3,7 @@ * rtmouse_main.c * Raspberry Pi Mouse device driver * - * Version: 3.3.3 + * Version: 3.3.4 * * Copyright (C) 2015-2024 RT Corporation * @@ -26,7 +26,7 @@ MODULE_AUTHOR("RT Corporation"); MODULE_LICENSE("GPL"); -MODULE_VERSION("3.3.3"); +MODULE_VERSION("3.3.4"); MODULE_DESCRIPTION("Raspberry Pi Mouse device driver"); /*