Skip to content

Commit 1bbb18b

Browse files
committed
enable done
1 parent 034c611 commit 1bbb18b

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

assignment02/part2/hc_sr04.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static ssize_t echo_show(struct device *dev,
9292
printk(KERN_ALERT "echo_show@ Done: %s", hcsr->pplat_dev->plf_dev.name);
9393
return ret;
9494
}
95+
9596
static ssize_t echo_store(struct device *dev,
9697
struct device_attribute *attr,
9798
const char *buf,
@@ -124,6 +125,14 @@ static ssize_t echo_store(struct device *dev,
124125
goto FAILED_SET;
125126
}
126127

128+
spin_lock(&(hcsr->irq_done_lock));
129+
hcsr->irq_done = IRQ_DONE;
130+
spin_unlock(&(hcsr->irq_done_lock));
131+
132+
spin_lock(&(hcsr->irq_done_lock));
133+
hcsr->irq_done = IRQ_DONE;
134+
spin_unlock(&(hcsr->irq_done_lock));
135+
127136
goto SUCCESS_SET;
128137

129138

@@ -166,6 +175,8 @@ static ssize_t mode_store(struct device *dev,
166175
printk(KERN_ALERT "mode_store@ %s\n", hcsr->pplat_dev->plf_dev.name);
167176

168177
ret = sscanf(buf,"%d", &mode);
178+
if(ONE_SHOT != mode && PERIODIC != mode) return -EINVAL;
179+
169180
if(0 >= ret) {
170181
printk(KERN_ALERT "sscanf error: %d, mode:%d", ret, mode);
171182
goto FAILED_SETMODE;
@@ -214,6 +225,8 @@ static ssize_t frequency_store(struct device *dev,
214225
printk(KERN_ALERT "sscanf error: %d, freq:%d", ret, freq);
215226
goto FAILED_SETMODE;
216227
}
228+
if(0 == freq) return -EINVAL;
229+
217230
goto SUCCESS_SETMODE;
218231

219232
FAILED_SETMODE:
@@ -256,11 +269,50 @@ static ssize_t enable_store(struct device *dev,
256269

257270
printk(KERN_ALERT "enable_store@ %s\n", hcsr->pplat_dev->plf_dev.name);
258271

272+
259273
ret = sscanf(buf,"%d", &enable);
260274
if(0 >= ret) {
261275
printk(KERN_ALERT "sscanf error: %d, mode:%d", ret, enable);
262276
goto FAILED_SETMODE;
263277
}
278+
if(0 == enable) {
279+
//stop kthread
280+
spin_lock(&(hcsr->kthread_lock));
281+
if(NULL != hcsr->kthread) {
282+
kthread_stop(hcsr->kthread);
283+
hcsr->kthread = NULL;
284+
}
285+
spin_unlock(&(hcsr->kthread_lock));
286+
287+
spin_lock(&(hcsr->ongoing_lock));
288+
hcsr->ongoing = STOPPING;
289+
spin_unlock(&(hcsr->ongoing_lock));
290+
} else if (1 == enable) {
291+
if(NULL != hcsr->kthread) return count;
292+
293+
spin_lock(&(hcsr->kconfig.kconfig_lock));
294+
if(ONE_SHOT == hcsr->kconfig.set.working_mode.mode) {
295+
spin_unlock(&(hcsr->kconfig.kconfig_lock));
296+
do {
297+
spin_lock(&(hcsr->ongoing_lock));
298+
if(STOPPING == hcsr->ongoing) {
299+
spin_unlock(&(hcsr->ongoing_lock));
300+
send(hcsr, 5);
301+
break;
302+
}
303+
spin_unlock(&(hcsr->ongoing_lock));
304+
}
305+
while(0);
306+
} else{
307+
spin_lock(&(hcsr->kthread_lock));
308+
hcsr->kthread = kthread_run(thread_function, hcsr, hcsr->hc_sr04->name);
309+
spin_unlock(&(hcsr->kthread_lock));
310+
}
311+
//kthread , freq some issues
312+
//if ONE_SHOT
313+
} else {
314+
return -EINVAL;
315+
}
264316
goto SUCCESS_SETMODE;
265317

266318
FAILED_SETMODE:
@@ -422,9 +474,14 @@ void hc_sr04_exit(struct HCSR_device* pplat_dev) {
422474
hcsr_struct *pdev = (hcsr_struct *)(pplat_dev->pdev);
423475
printk(KERN_ALERT "hc_sr04: GoodBye Kernel World!!!: %s\n", pdev->hc_sr04->name);
424476

477+
if(NULL != pdev->kthread)
478+
kthread_stop(pdev->kthread);
479+
425480
spin_lock(&(pdev->irq_done_lock));
426481
if(IRQ_DONE == pdev->irq_done) {
427482
free_irq(pdev->echo_isr_number, pdev);
483+
printk(KERN_ALERT "hc_sr04: Free IRQ");
484+
428485
}
429486
free_gpio(pdev);
430487
spin_unlock(&(pdev->irq_done_lock));

assignment02/part2/hc_sr04_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define __DEUBG__
44
#include<linux/ioctl.h>
55
#define ONE_SHOT (0)
6+
#define PERIODIC (1)
67
#define NUM_DATA (5)
78
#define HCSR_W_MAGIC ('H')
89
#define SETPINS _IOW (HCSR_W_MAGIC, 1, struct pin_set*)

assignment02/part2/hc_sr04_kernel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ static long ioctl_SETMODE(struct file* file, unsigned long addr) {
417417
ret = -EAGAIN;
418418
goto ERR_SETMODE_RETURN;
419419
}
420+
if(ONE_SHOT != working_mode.mode && PERIODIC != working_mode.mode) return -EINVAL;
420421

421422

422423
hcsr = (struct hcsr_struct*) file->private_data;
@@ -426,6 +427,7 @@ static long ioctl_SETMODE(struct file* file, unsigned long addr) {
426427
/*For Periodic Task*/
427428
hcsr->kconfig.set.working_mode = working_mode;
428429
printk(KERN_ALERT "%s: mode:%d, freq:%d\n", hcsr->hc_sr04->name, hcsr->kconfig.set.working_mode.mode, hcsr->kconfig.set.working_mode.freq);
430+
spin_lock(&(hcsr->kthread_lock));
429431
if(ONE_SHOT != hcsr->kconfig.set.working_mode.mode && NULL != hcsr->kthread) {
430432
printk(KERN_ALERT "Keep Thread Going with period change: %s\n", hcsr->hc_sr04->name);
431433
kthread_stop(hcsr->kthread);
@@ -435,6 +437,7 @@ static long ioctl_SETMODE(struct file* file, unsigned long addr) {
435437
kthread_stop(hcsr->kthread);
436438
hcsr->kthread = NULL;
437439
}
440+
spin_unlock(&(hcsr->kthread_lock));
438441
spin_unlock(&(hcsr->kconfig.kconfig_lock));
439442
goto SUCCESS_SETMODE_RETURN;
440443

@@ -572,6 +575,10 @@ static ssize_t hc_sr04_write(struct file *file, const char __user *buf, size_t c
572575
printk(KERN_ALERT "CALL ThreadStop");
573576
kthread_stop(hcsr->kthread);
574577
hcsr->kthread = NULL;
578+
579+
spin_lock(&(hcsr->ongoing_lock));
580+
hcsr->ongoing = STOPPING;
581+
spin_unlock(&(hcsr->ongoing_lock));
575582
} while(0);
576583
} else {
577584
do {

assignment02/part2/hw_setting.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ char* A_pin_str[2][5] = {
3131
char B_pins[2][5][2] = {
3232
//Linux Level Pull Mux0 Mux1
3333

34-
//old
3534
/*IO10*/ {{HCSR_B_TRIG_PIN, 0}, {26, DIR_OUT}, {-1,-1}, {74, 0}, {-1,-1}},
3635
/*IO 6*/ {{HCSR_B_ECHO_PIN, 0}, {20, DIR_IN}, { 21, PULL_DOWN}, {68, 0}, {-1, -1}},
3736
};

0 commit comments

Comments
 (0)