-
Notifications
You must be signed in to change notification settings - Fork 455
/
rtmouse.c
1800 lines (1534 loc) · 46 KB
/
rtmouse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* rtmouse.c
* Raspberry Pi Mouse device driver
*
* Version: 3.3.2
*
* Copyright (C) 2015-2024 RT Corporation <shop@rt-net.jp>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "rtmouse.h"
MODULE_AUTHOR("RT Corporation");
MODULE_LICENSE("GPL");
MODULE_VERSION("3.3.2");
MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");
static int _major_dev[ID_DEV_SIZE] = {
[ID_DEV_LED] = DEV_MAJOR, [ID_DEV_SWITCH] = DEV_MAJOR,
[ID_DEV_SENSOR] = DEV_MAJOR, [ID_DEV_BUZZER] = DEV_MAJOR,
[ID_DEV_MOTORRAWR] = DEV_MAJOR, [ID_DEV_MOTORRAWL] = DEV_MAJOR,
[ID_DEV_MOTOREN] = DEV_MAJOR, [ID_DEV_MOTOR] = DEV_MAJOR};
static int _minor_dev[ID_DEV_SIZE] = {
[ID_DEV_LED] = DEV_MINOR, [ID_DEV_SWITCH] = DEV_MINOR,
[ID_DEV_SENSOR] = DEV_MINOR, [ID_DEV_BUZZER] = DEV_MINOR,
[ID_DEV_MOTORRAWR] = DEV_MINOR, [ID_DEV_MOTORRAWL] = DEV_MINOR,
[ID_DEV_MOTOREN] = DEV_MINOR, [ID_DEV_MOTOR] = DEV_MINOR};
/* --- General Options --- */
static struct cdev *cdev_array = NULL;
static struct class *class_dev[ID_DEV_SIZE] = {
[ID_DEV_LED] = NULL, [ID_DEV_SWITCH] = NULL,
[ID_DEV_SENSOR] = NULL, [ID_DEV_BUZZER] = NULL,
[ID_DEV_MOTORRAWR] = NULL, [ID_DEV_MOTORRAWL] = NULL,
[ID_DEV_MOTOREN] = NULL, [ID_DEV_MOTOR] = NULL};
static volatile void __iomem *pwm_base;
static volatile void __iomem *clk_base;
static volatile uint32_t *gpio_base;
static volatile int cdev_index = 0;
static struct mutex lock;
/* --- Function Declarations --- */
static void set_motor_r_freq(int freq);
static void set_motor_l_freq(int freq);
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
static int mcp3204_remove(struct spi_device *spi);
#else
static void mcp3204_remove(struct spi_device *spi);
#endif
static int mcp3204_probe(struct spi_device *spi);
static unsigned int mcp3204_get_value(int channel);
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
static int rtcnt_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id);
#else
static int rtcnt_i2c_probe(struct i2c_client *client);
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 1, 0)
static int rtcnt_i2c_remove(struct i2c_client *client);
#else
static void rtcnt_i2c_remove(struct i2c_client *client);
#endif
/* --- Variable Type definitions --- */
/* SPI */
struct mcp3204_drvdata {
struct spi_device *spi;
struct mutex lock;
unsigned char tx[MCP320X_PACKET_SIZE] ____cacheline_aligned;
unsigned char rx[MCP320X_PACKET_SIZE] ____cacheline_aligned;
struct spi_transfer xfer ____cacheline_aligned;
struct spi_message msg ____cacheline_aligned;
};
/* --- Static variables --- */
/* SPI device ID */
static struct spi_device_id mcp3204_id[] = {
{"mcp3204", 0},
{},
};
/* SPI Info */
static struct spi_board_info mcp3204_info = {
.modalias = "mcp3204",
.max_speed_hz = 100000,
.bus_num = 0,
.chip_select = 0,
.mode = SPI_MODE_3,
};
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
static struct device *mcp320x_dev;
#endif
/* SPI Dirver Info */
static struct spi_driver mcp3204_driver = {
.driver =
{
.name = DEVNAME_SENSOR,
.owner = THIS_MODULE,
},
.id_table = mcp3204_id,
.probe = mcp3204_probe,
.remove = mcp3204_remove,
};
/* I2C */
struct rtcnt_device_info {
struct cdev cdev;
unsigned int device_major;
unsigned int device_minor;
struct class *device_class;
struct i2c_client *client;
struct mutex lock;
int signed_pulse_count;
int raw_pulse_count;
};
static struct i2c_client *i2c_client_r = NULL;
static struct i2c_client *i2c_client_l = NULL;
static unsigned int motor_l_freq_is_positive = 1;
static unsigned int motor_r_freq_is_positive = 1;
/* I2C Device ID */
static struct i2c_device_id i2c_counter_id[] = {
{DEVNAME_CNTL, 0},
{DEVNAME_CNTR, 1},
{},
};
/* I2C Dirver Info */
static struct i2c_driver i2c_counter_driver = {
.driver =
{
.name = "rtcounter",
.owner = THIS_MODULE,
},
.id_table = i2c_counter_id,
.probe = rtcnt_i2c_probe,
.remove = rtcnt_i2c_remove,
};
/* -- Device Addition -- */
MODULE_DEVICE_TABLE(spi, mcp3204_id);
MODULE_DEVICE_TABLE(i2c, i2c_counter_id);
/* --- GPIO Operation --- */
/* getPWMCount function for GPIO Operation */
static int getPWMCount(int freq)
{
if (freq < 1)
return PWM_BASECLK;
if (freq > 10000)
return PWM_BASECLK / 10000;
return PWM_BASECLK / freq;
}
/* set function */
static int rpi_gpio_function_set(int pin, uint32_t func)
{
int index = RPI_GPFSEL0_INDEX + pin / 10;
uint32_t mask = ~(0x7 << ((pin % 10) * 3));
gpio_base[index] =
(gpio_base[index] & mask) | ((func & 0x7) << ((pin % 10) * 3));
return 1;
}
/*set mask and value */
static void rpi_gpio_set32(uint32_t mask, uint32_t val)
{
gpio_base[RPI_GPSET0_INDEX] = val & mask;
}
/* clear mask and value */
static void rpi_gpio_clear32(uint32_t mask, uint32_t val)
{
gpio_base[RPI_GPCLR0_INDEX] = val & mask;
}
/* pwm set function */
static void rpi_pwm_write32(uint32_t offset, uint32_t val)
{
iowrite32(val, pwm_base + offset);
}
/* left motor function */
static void set_motor_l_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_L_BASE, RPI_GPF_OUTPUT);
return;
} else {
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_ALT0);
}
if (freq > 0) {
motor_l_freq_is_positive = 1;
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
} else {
motor_l_freq_is_positive = 0;
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_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 */
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;
}
dat = getPWMCount(freq);
rpi_pwm_write32(RPI_PWM_RNG2, dat);
rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1);
return;
}
/* --- Function for device file operations --- */
/*
* Read Push Switches
* return 0 : device close
*/
static ssize_t sw_read(struct file *filep, char __user *buf, size_t count,
loff_t *f_pos)
{
int buflen = 0;
unsigned char rw_buf[MAX_BUFLEN];
unsigned int ret = 0;
int len;
int index;
unsigned int pin = SW1_PIN;
uint32_t mask;
int minor = *((int *)filep->private_data);
#if RASPBERRYPI == 4
int pullreg = GPPUPPDN1 + (pin >> 4); // SW1, 2, 3 is between GPIO16-31
int pullshift = (pin & 0xf) << 1;
unsigned int pullbits;
unsigned int pull;
#endif
switch (minor) {
case 0:
pin = SW1_PIN;
break;
case 1:
pin = SW2_PIN;
break;
case 2:
pin = SW3_PIN;
break;
default:
return 0;
break;
}
if (*f_pos > 0)
return 0; /* End of file */
#if RASPBERRYPI == 4
pull = GPIO_PULLUP;
pullbits = *(gpio_base + pullreg);
pullbits &= ~(3 << pullshift);
pullbits |= (pull << pullshift);
*(gpio_base + pullreg) = pullbits;
#else
// プルモード (2bit)を書き込む NONE/DOWN/UP
gpio_base[37] = GPIO_PULLUP & 0x3; // GPPUD
// ピンにクロックを供給(前後にウェイト)
msleep(1);
gpio_base[38] = 0x1 << pin; // GPPUDCLK0
msleep(1);
// プルモード・クロック状態をクリアして終了
gpio_base[37] = 0;
gpio_base[38] = 0;
#endif
index = RPI_GPFSEL0_INDEX + pin / 10;
mask = ~(0x7 << ((pin % 10) * 3));
ret = ((gpio_base[13] & (0x01 << pin)) != 0);
sprintf(rw_buf, "%d\n", ret);
buflen = strlen(rw_buf);
count = buflen;
len = buflen;
if (copy_to_user((void *)buf, &rw_buf, count)) {
printk(KERN_INFO "err read buffer from ret %d\n", ret);
printk(KERN_INFO "err read buffer from %s\n", rw_buf);
printk(KERN_INFO "err sample_char_read size(%zu)\n", count);
printk(KERN_INFO "sample_char_read size err(%d)\n", -EFAULT);
return 0;
}
*f_pos += count;
return count;
}
/*
* Read Sensor information
* return 0 : device close
*/
static ssize_t sensor_read(struct file *filep, char __user *buf, size_t count,
loff_t *f_pos)
{
int buflen = 0;
unsigned char rw_buf[MAX_BUFLEN];
unsigned int ret = 0;
int len;
// printk(KERN_INFO "new\n");
int usecs = 30;
int rf = 0, lf = 0, r = 0, l = 0;
int orf = 0, olf = 0, or = 0, ol = 0;
if (*f_pos > 0)
return 0; /* End of file */
/* get values through MCP3204 */
/* Right side */
or = mcp3204_get_value(R_AD_CH);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << R_LED_BASE);
udelay(usecs);
r = mcp3204_get_value(R_AD_CH);
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << R_LED_BASE);
udelay(usecs);
/* Left side */
ol = mcp3204_get_value(L_AD_CH);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << L_LED_BASE);
udelay(usecs);
l = mcp3204_get_value(L_AD_CH);
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << L_LED_BASE);
udelay(usecs);
/* Right front side */
orf = mcp3204_get_value(RF_AD_CH);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << RF_LED_BASE);
udelay(usecs);
rf = mcp3204_get_value(RF_AD_CH);
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << RF_LED_BASE);
udelay(usecs);
/* Left front side */
olf = mcp3204_get_value(LF_AD_CH);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << LF_LED_BASE);
udelay(usecs);
lf = mcp3204_get_value(LF_AD_CH);
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << LF_LED_BASE);
udelay(usecs);
/* set sensor data to rw_buf(static buffer) */
snprintf(rw_buf, sizeof(rw_buf), "%d %d %d %d\n", rf - orf, r - or,
l - ol, lf - olf);
buflen = strlen(rw_buf);
count = buflen;
len = buflen;
/* copy data to user area */
if (copy_to_user((void *)buf, &rw_buf, count)) {
printk(KERN_INFO "err read buffer from ret %d\n", ret);
printk(KERN_INFO "err read buffer from %s\n", rw_buf);
printk(KERN_INFO "err sample_char_read size(%zu)\n", count);
printk(KERN_INFO "sample_char_read size err(%d)\n", -EFAULT);
return 0;
}
*f_pos += count;
return count;
}
/*
* Turn On LEDs
* return 0 : device close
*/
static int led_put(int ledno)
{
switch (ledno) {
case 0:
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << LED0_BASE);
break;
case 1:
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << LED1_BASE);
break;
case 2:
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << LED2_BASE);
break;
case 3:
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << LED3_BASE);
break;
}
return 0;
}
/*
* Turn Off LEDs
* return 0 : device close
*/
static int led_del(int ledno)
{
switch (ledno) {
case 0:
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << LED0_BASE);
break;
case 1:
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << LED1_BASE);
break;
case 2:
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << LED2_BASE);
break;
case 3:
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << LED3_BASE);
break;
}
return 0;
}
/*
* Initialize buzzer
* return 0 : device close
*/
static int buzzer_init(void)
{
rpi_gpio_function_set(BUZZER_BASE, RPI_GPF_OUTPUT); // io is pwm out
rpi_pwm_write32(RPI_PWM_CTRL, 0x00000000);
udelay(1000);
rpi_pwm_write32(RPI_PWM_CTRL, 0x00008181); // PWM1,2 enable
// printk(KERN_DEBUG "%s: rpi_pwm_ctrl:%08X\n", DRIVER_NAME,
// ioread32(pwm_base + RPI_PWM_CTRL));
return 0;
}
/* --- GPIO mapping for Device Open/Close --- */
/*
* Get gpio addresses and set them to global variables.
* - gpio_base
* - pwm_base
* - clk_base
* - clk_status
*/
static int gpio_map(void)
{
static int clk_status = 1;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(5, 0, 0)
if (gpio_base == NULL) {
gpio_base = ioremap_nocache(RPI_GPIO_BASE, RPI_GPIO_SIZE);
}
if (pwm_base == NULL) {
pwm_base = ioremap_nocache(RPI_PWM_BASE, RPI_PWM_SIZE);
}
if (clk_base == NULL) {
clk_base = ioremap_nocache(RPI_CLK_BASE, RPI_CLK_SIZE);
}
#else
if (gpio_base == NULL) {
gpio_base = ioremap(RPI_GPIO_BASE, RPI_GPIO_SIZE);
}
if (pwm_base == NULL) {
pwm_base = ioremap(RPI_PWM_BASE, RPI_PWM_SIZE);
}
if (clk_base == NULL) {
clk_base = ioremap(RPI_CLK_BASE, RPI_CLK_SIZE);
}
#endif
/* kill */
if (clk_status == 1) {
iowrite32(0x5a000000 | (1 << 5), clk_base + CLK_PWM_INDEX);
udelay(1000);
/* clk set */
iowrite32(0x5a000000 | (2 << 12), clk_base + CLK_PWMDIV_INDEX);
iowrite32(0x5a000011, clk_base + CLK_PWM_INDEX);
udelay(1000); /* wait for 1msec */
clk_status = 0;
}
return 0;
}
/* Unmap GPIO addresses */
static int gpio_unmap(void)
{
iounmap(gpio_base);
iounmap(pwm_base);
iounmap(clk_base);
gpio_base = NULL;
pwm_base = NULL;
clk_base = NULL;
return 0;
}
/* --- Device File Operation --- */
/* Open Device */
static int dev_open(struct inode *inode, struct file *filep)
{
int *minor = (int *)kmalloc(sizeof(int), GFP_KERNEL);
int major = MAJOR(inode->i_rdev);
*minor = MINOR(inode->i_rdev);
filep->private_data = (void *)minor;
const char *dev_name = filep->f_path.dentry->d_name.name;
printk(KERN_INFO "Device opened: %s, Major: %d\n", dev_name, major);
return 0;
}
/* Close device */
static int dev_release(struct inode *inode, struct file *filep)
{
kfree(filep->private_data);
return 0;
}
static int i2c_dev_open(struct inode *inode, struct file *filep)
{
struct rtcnt_device_info *dev_info;
dev_info = container_of(inode->i_cdev, struct rtcnt_device_info, cdev);
if (dev_info == NULL || dev_info->client == NULL) {
printk(KERN_ERR "%s: i2c dev_open failed.\n", DRIVER_NAME);
}
dev_info->device_minor = MINOR(inode->i_rdev);
filep->private_data = dev_info;
return 0;
}
static int i2c_dev_release(struct inode *inode, struct file *filep)
{
return 0;
}
/* Parse motor command */
static int parseMotorCmd(const char __user *buf, size_t count, int *ret)
{
int r_motor_val, l_motor_val, time_val;
char *newbuf = kmalloc(sizeof(char) * count, GFP_KERNEL);
if (copy_from_user(newbuf, buf, sizeof(char) * count)) {
kfree(newbuf);
return -EFAULT;
}
sscanf(newbuf, "%d%d%d\n", &l_motor_val, &r_motor_val, &time_val);
kfree(newbuf);
mutex_lock(&lock);
set_motor_l_freq(l_motor_val);
set_motor_r_freq(r_motor_val);
msleep_interruptible(time_val);
set_motor_l_freq(0);
set_motor_r_freq(0);
mutex_unlock(&lock);
return count;
}
/*
* led_write - Trun ON/OFF LEDs
* Write function of /dev/rtled
*/
static ssize_t led_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
char cval;
int ret;
int minor = *((int *)filep->private_data);
if (count > 0) {
if (copy_from_user(&cval, buf, sizeof(char))) {
return -EFAULT;
}
switch (cval) {
case '1':
ret = led_put(minor);
break;
case '0':
ret = led_del(minor);
break;
}
return sizeof(char);
}
return 0;
}
/*
* buzzer_write - Write buzzer frequency
* Write function of /dev/rtbuzzer
*/
static ssize_t buzzer_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
int ret;
int freq, dat;
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) {
freq = 1;
}
if (freq > 20000) {
freq = 20000;
}
rpi_gpio_function_set(BUZZER_BASE,
RPI_GPF_ALT5); // io is pwm out
dat = PWM_BASECLK / freq;
rpi_pwm_write32(RPI_PWM_RNG2, dat);
rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1);
} else {
rpi_gpio_function_set(BUZZER_BASE,
RPI_GPF_OUTPUT); // io is pwm out
}
return count;
}
/*
* rawmotor_l_write - Output frequency to the left motor
* Write function of /dev/rtmotor_raw_l
*/
static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
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 count;
}
/*
* rawmotor_r_write - Output frequency to the right motor
* Write function of /dev/rtmotor_raw_r
*/
static ssize_t rawmotor_r_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
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 count;
}
/*
* motoren_write - Turn ON/OFF SteppingMotor Power
* Write function of /dev/rtmotoren
*/
static ssize_t motoren_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
char cval;
if (count > 0) {
if (copy_from_user(&cval, buf, sizeof(char))) {
return -EFAULT;
}
switch (cval) {
case '1':
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTEN_BASE);
break;
case '0':
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTEN_BASE);
break;
}
return sizeof(char);
}
return 0;
}
/*
* motor_write - Output frequency to right and left both motors
* Write function of /dev/rtmotor
*/
static ssize_t motor_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
int tmp;
int bufcnt;
bufcnt = parseMotorCmd(buf, count, &tmp);
return bufcnt;
}
/*
* i2c_counter_set - set value to I2C pulse counter
* called by cntr_write() and cntl_write()
*/
static int i2c_counter_set(struct rtcnt_device_info *dev_info, int setval)
{
int ret = 0;
int lsb = 0, msb = 0;
struct i2c_client *client = dev_info->client;
// printk(KERN_INFO "set 0x%x = 0x%x\n", client->addr, setval);
msb = (setval >> 8) & 0xFF;
lsb = setval & 0xFF;
mutex_lock(&dev_info->lock);
// printk(KERN_INFO "set 0x%x msb = 0x%x\n", client->addr, msb);
ret = i2c_smbus_write_byte_data(client, 0x10, msb);
if (ret < 0) {
printk(KERN_ERR
"%s: Failed writing to i2c counter device, addr=0x%x\n",
__func__, client->addr);
return -ENODEV;
}
// printk(KERN_INFO "set 0x%x lsb = 0x%x\n", client->addr, lsb);
ret = i2c_smbus_write_byte_data(client, 0x11, lsb);
if (ret < 0) {
printk(KERN_ERR
"%s: Failed writing to i2c counter device, addr=0x%x\n",
__func__, client->addr);
return -ENODEV;
}
mutex_unlock(&dev_info->lock);
return ret;
}
/*
* i2c_counter_read - get value from I2C pulse counter
* called by rtcnt_read()
*/
static int i2c_counter_read(struct rtcnt_device_info *dev_info, int *ret)
{
int lsb = 0, msb = 0;
// printk(KERN_INFO "read 0x%x\n", client->addr);
struct i2c_client *client = dev_info->client;
mutex_lock(&dev_info->lock);
lsb = i2c_smbus_read_byte_data(client, CNT_ADDR_LSB);
if (lsb < 0) {
printk(
KERN_ERR
"%s: Failed reading from i2c counter device, addr=0x%x\n",
__func__, client->addr);
return -ENODEV;
}
msb = i2c_smbus_read_byte_data(client, CNT_ADDR_MSB);
if (msb < 0) {
printk(
KERN_ERR
"%s: Failed reading from i2c counter device, addr=0x%x\n",
__func__, client->addr);
return -ENODEV;
}
mutex_unlock(&dev_info->lock);
*ret = ((msb << 8) & 0xFF00) + (lsb & 0xFF);
// printk(KERN_INFO "0x%x == 0x%x\n", client->addr, *ret);
return 0;
}
/*
* update_signed_count - update signed pulse count of dev_info
* called by rtcnt_read()
*/
void update_signed_count(struct rtcnt_device_info *dev_info, int rtcnt_count)
{
int diff_count = rtcnt_count - dev_info->raw_pulse_count;
// カウントがMAX_PULSE_COUNTから0に変わる場合の処理
// ただし、それ以外でもdiffが負の値になることがある
// そのため、diffが十分に大きな負の値の場合に処理する
// if(diff_count < 0) では正常に動作しない
if (diff_count < -SIGNED_COUNT_SIZE) {
diff_count += MAX_PULSE_COUNT;
}
if (dev_info->client->addr == DEV_ADDR_CNTL) {
if (motor_l_freq_is_positive) {
dev_info->signed_pulse_count += diff_count;
} else {
dev_info->signed_pulse_count -= diff_count;
}
} else {
if (motor_r_freq_is_positive) {
dev_info->signed_pulse_count += diff_count;
} else {
dev_info->signed_pulse_count -= diff_count;
}
}
if (dev_info->signed_pulse_count > SIGNED_COUNT_SIZE ||
dev_info->signed_pulse_count < -SIGNED_COUNT_SIZE) {
dev_info->signed_pulse_count = 0;
}
}
/*
* reset_signed_count - reset signed pulse count of dev_info
* called by rtcnt_write()
*/
void reset_signed_count(struct rtcnt_device_info *dev_info, int rtcnt_count)
{
int raw_count;
if (rtcnt_count > SIGNED_COUNT_SIZE) {
rtcnt_count = SIGNED_COUNT_SIZE;
} else if (rtcnt_count < -SIGNED_COUNT_SIZE) {
rtcnt_count = -SIGNED_COUNT_SIZE;
}
dev_info->signed_pulse_count = rtcnt_count;
i2c_counter_read(dev_info, &raw_count);
dev_info->raw_pulse_count = raw_count;
}
/*
* rtcnt_read - Read value from right/left pulse counter
* Read function of /dev/rtcounter_*
*/
static ssize_t rtcnt_read(struct file *filep, char __user *buf, size_t count,
loff_t *f_pos)
{
struct rtcnt_device_info *dev_info = filep->private_data;
unsigned char rw_buf[64];
int buflen;
int rtcnt_count = 0;
if (*f_pos > 0)
return 0; /* close device */
i2c_counter_read(dev_info, &rtcnt_count);
if (dev_info->device_minor == 1) {
update_signed_count(dev_info, rtcnt_count);
dev_info->raw_pulse_count = rtcnt_count;
rtcnt_count = dev_info->signed_pulse_count;
} else {
dev_info->raw_pulse_count = rtcnt_count;
}
/* set sensor data to rw_buf(static buffer) */
sprintf(rw_buf, "%d\n", rtcnt_count);
buflen = strlen(rw_buf);
count = buflen;
/* copy data to user area */
if (copy_to_user((void *)buf, &rw_buf, count)) {
printk(KERN_INFO "err read buffer from %s\n", rw_buf);
printk(KERN_INFO "err sample_char_read size(%zu)\n", count);
printk(KERN_INFO "sample_char_read size err(%d)\n", -EFAULT);
return -EFAULT;
}
*f_pos += count;
return count;
}
/*
* cnt_write - Set value to right/left pulse counter
* Write function of /dev/rtcounter
*/
static ssize_t rtcnt_write(struct file *filep, const char __user *buf,
size_t count, loff_t *pos)
{
struct rtcnt_device_info *dev_info = filep->private_data;
int rtcnt_count = 0;
int ret;
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);
if (dev_info->device_minor == 1) {
reset_signed_count(dev_info, rtcnt_count);
}
printk(KERN_INFO "%s: set pulse counter value %d\n", DRIVER_NAME,
rtcnt_count);
return count;
}
/* --- Device File Operations --- */
static struct file_operations dev_fops[ID_DEV_SIZE] = {
[ID_DEV_LED].open = dev_open,
[ID_DEV_LED].release = dev_release,
[ID_DEV_LED].write = led_write,
[ID_DEV_SWITCH].open = dev_open,
[ID_DEV_SWITCH].read = sw_read,
[ID_DEV_SWITCH].release = dev_release,
[ID_DEV_SENSOR].open = dev_open,
[ID_DEV_SENSOR].read = sensor_read,
[ID_DEV_SENSOR].release = dev_release,
[ID_DEV_BUZZER].open = dev_open,
[ID_DEV_BUZZER].release = dev_release,
[ID_DEV_BUZZER].write = buzzer_write,
[ID_DEV_MOTORRAWR].open = dev_open,
[ID_DEV_MOTORRAWR].release = dev_release,
[ID_DEV_MOTORRAWR].write = rawmotor_r_write,
[ID_DEV_MOTORRAWL].open = dev_open,
[ID_DEV_MOTORRAWL].release = dev_release,