Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 35 additions & 122 deletions src/drivers/rtmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* rtmouse.c
* Raspberry Pi Mouse device driver
*
* Version: 3.1.0
* Version: 3.1.1
*
* Copyright (C) 2015-2021 RT Corporation <shop@rt-net.jp>
*
Expand Down Expand Up @@ -55,7 +55,7 @@

MODULE_AUTHOR("RT Corporation");
MODULE_LICENSE("GPL");
MODULE_VERSION("3.0.0");
MODULE_VERSION("3.1.1");
MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");

/* --- Device Numbers --- */
Expand Down Expand Up @@ -848,59 +848,6 @@ static int i2c_dev_release(struct inode *inode, struct file *filep)
return 0;
}

/* Parse Frequency */
static int parseFreq(const char __user *buf, size_t count, int *ret)
{
char cval;
int error = 0, i = 0, tmp, bufcnt = 0, freq;
size_t readcount = count;
int sgn = 1;

char *newbuf = kmalloc(sizeof(char) * count, GFP_KERNEL);

while (readcount > 0) {
if (copy_from_user(&cval, buf + i, sizeof(char))) {
kfree(newbuf);
return -EFAULT;
}

if (cval == '-') {
if (bufcnt == 0) {
sgn = -1;
}
} else if (cval < '0' || cval > '9') {
newbuf[bufcnt] = 'e';
error = 1;
} else {
newbuf[bufcnt] = cval;
}

i++;
bufcnt++;
readcount--;

if (cval == '\n') {
break;
}
}

freq = 0;
for (i = 0, tmp = 1; i < bufcnt; i++) {
char c = newbuf[bufcnt - i - 1];

if (c >= '0' && c <= '9') {
freq += (newbuf[bufcnt - i - 1] - '0') * tmp;
tmp *= 10;
}
}

*ret = sgn * freq;

kfree(newbuf);

return bufcnt;
}

/* Parse motor command */
static int parseMotorCmd(const char __user *buf, size_t count, int *ret)
{
Expand Down Expand Up @@ -931,58 +878,6 @@ static int parseMotorCmd(const char __user *buf, size_t count, int *ret)
return count;
}

/* Parse I2C pulse counter value */
static int parse_count(const char __user *buf, size_t count, int *ret)
{
char cval;
int error = 0, i = 0, tmp, bufcnt = 0, freq;
size_t readcount = count;
int sgn = 1;

char *newbuf = kmalloc(sizeof(char) * count, GFP_KERNEL);

while (readcount > 0) {
if (copy_from_user(&cval, buf + i, sizeof(char))) {
kfree(newbuf);
return -EFAULT;
}

if (cval == '-') {
if (bufcnt == 0) {
sgn = -1;
}
} else if (cval < '0' || cval > '9') {
newbuf[bufcnt] = 'e';
error = 1;
} else {
newbuf[bufcnt] = cval;
}

i++;
bufcnt++;
readcount--;

if (cval == '\n') {
break;
}
}

freq = 0;
for (i = 0, tmp = 1; i < bufcnt; i++) {
char c = newbuf[bufcnt - i - 1];

if (c >= '0' && c <= '9') {
freq += (newbuf[bufcnt - i - 1] - '0') * tmp;
tmp *= 10;
}
}

*ret = sgn * freq;

kfree(newbuf);

return bufcnt;
}

/*
* led_write - Trun ON/OFF LEDs
Expand Down Expand Up @@ -1019,10 +914,15 @@ static ssize_t led_write(struct file *filep, const char __user *buf,
static ssize_t buzzer_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
int bufcnt;
int ret;
int freq, dat;

bufcnt = parseFreq(buf, count, &freq);
ret = kstrtoint_from_user(buf, count, 10, &freq);
if (ret) {
printk(KERN_ERR "%s: error parsing string to int in %s()\n",
DRIVER_NAME, __func__);
return ret;
}

if (freq != 0) {
if (freq < 1) {
Expand All @@ -1043,7 +943,7 @@ static ssize_t buzzer_write(struct file *filep, const char __user *buf,
RPI_GPF_OUTPUT); // io is pwm out
}

return bufcnt;
return count;
}

/*
Expand All @@ -1053,12 +953,17 @@ static ssize_t buzzer_write(struct file *filep, const char __user *buf,
static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
int freq, bufcnt;
bufcnt = parseFreq(buf, count, &freq);
int freq, ret;

ret = kstrtoint_from_user(buf, count, 10, &freq);
if (ret) {
printk(KERN_ERR "%s: error parsing string to int in %s()\n",
DRIVER_NAME, __func__);
return ret;
}
set_motor_l_freq(freq);

return bufcnt;
return count;
}

/*
Expand All @@ -1068,12 +973,18 @@ static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf,
static ssize_t rawmotor_r_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
int freq, bufcnt;
bufcnt = parseFreq(buf, count, &freq);
int freq, ret;

ret = kstrtoint_from_user(buf, count, 10, &freq);
if (ret) {
printk(KERN_ERR "%s: error parsing string to int in %s()\n",
DRIVER_NAME, __func__);
return ret;
}

set_motor_r_freq(freq);

return bufcnt;
return count;
}

/*
Expand Down Expand Up @@ -1292,13 +1203,15 @@ static ssize_t rtcnt_write(struct file *filep, const char __user *buf,
{
struct rtcnt_device_info *dev_info = filep->private_data;

int bufcnt = 0;
int rtcnt_count = 0;
int ret;

if (count < 0)
return 0;

bufcnt = parse_count(buf, count, &rtcnt_count);
ret = kstrtoint_from_user(buf, count, 10, &rtcnt_count);
if (ret) {
printk(KERN_ERR "%s: error parsing string to int in %s()\n",
DRIVER_NAME, __func__);
return ret;
}

i2c_counter_set(dev_info, rtcnt_count);

Expand All @@ -1308,7 +1221,7 @@ static ssize_t rtcnt_write(struct file *filep, const char __user *buf,

printk(KERN_INFO "%s: set pulse counter value %d\n", DRIVER_NAME,
rtcnt_count);
return bufcnt;
return count;
}

/* --- Device File Operations --- */
Expand Down