1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#undef DEBUG
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/idr.h>
24#include <linux/ioctl.h>
25#include <linux/uaccess.h>
26#include <linux/fs.h>
27#include <linux/device.h>
28#include <linux/cdev.h>
29#include <linux/err.h>
30#include <linux/kfifo.h>
31#include <linux/errno.h>
32#include <linux/mutex.h>
33#include <linux/of.h>
34#include <linux/of_device.h>
35#include <linux/interrupt.h>
36#include <linux/irq.h>
37#include <linux/gpio/consumer.h>
38#include <linux/kthread.h>
39#include <linux/wait.h>
40#include <linux/spi/spi.h>
41#ifdef CONFIG_COMPAT
42#include <linux/compat.h>
43#endif
44
45#include "pi433_if.h"
46#include "rf69.h"
47
48#define N_PI433_MINORS BIT(MINORBITS)
49#define MAX_MSG_SIZE 900
50#define MSG_FIFO_SIZE 65536
51#define NUM_DIO 2
52
53static dev_t pi433_dev;
54static DEFINE_IDR(pi433_idr);
55static DEFINE_MUTEX(minor_lock);
56
57static struct class *pi433_class;
58
59
60
61
62
63
64
65
66
67struct pi433_device {
68
69 dev_t devt;
70 int minor;
71 struct device *dev;
72 struct cdev *cdev;
73 struct spi_device *spi;
74
75
76 struct gpio_desc *gpiod[NUM_DIO];
77 int irq_num[NUM_DIO];
78 u8 irq_state[NUM_DIO];
79
80
81 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
82 struct mutex tx_fifo_lock;
83 struct task_struct *tx_task_struct;
84 wait_queue_head_t tx_wait_queue;
85 u8 free_in_fifo;
86 char buffer[MAX_MSG_SIZE];
87
88
89 struct pi433_rx_cfg rx_cfg;
90 u8 *rx_buffer;
91 unsigned int rx_buffer_size;
92 u32 rx_bytes_to_drop;
93 u32 rx_bytes_dropped;
94 unsigned int rx_position;
95 struct mutex rx_lock;
96 wait_queue_head_t rx_wait_queue;
97
98
99 struct task_struct *fifo_task_struct;
100 wait_queue_head_t fifo_wait_queue;
101
102
103 bool rx_active;
104 bool tx_active;
105 bool interrupt_rx_allowed;
106};
107
108struct pi433_instance {
109 struct pi433_device *device;
110 struct pi433_tx_cfg tx_cfg;
111};
112
113
114
115
116static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
117{
118 struct pi433_device *device = dev_id;
119
120 if (device->irq_state[DIO0] == DIO_PACKET_SENT) {
121 device->free_in_fifo = FIFO_SIZE;
122 dev_dbg(device->dev, "DIO0 irq: Packet sent\n");
123 wake_up_interruptible(&device->fifo_wait_queue);
124 } else if (device->irq_state[DIO0] == DIO_RSSI_DIO0) {
125 dev_dbg(device->dev, "DIO0 irq: RSSI level over threshold\n");
126 wake_up_interruptible(&device->rx_wait_queue);
127 } else if (device->irq_state[DIO0] == DIO_PAYLOAD_READY) {
128 dev_dbg(device->dev, "DIO0 irq: Payload ready\n");
129 device->free_in_fifo = 0;
130 wake_up_interruptible(&device->fifo_wait_queue);
131 }
132
133 return IRQ_HANDLED;
134}
135
136static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
137{
138 struct pi433_device *device = dev_id;
139
140 if (device->irq_state[DIO1] == DIO_FIFO_NOT_EMPTY_DIO1) {
141 device->free_in_fifo = FIFO_SIZE;
142 } else if (device->irq_state[DIO1] == DIO_FIFO_LEVEL) {
143 if (device->rx_active)
144 device->free_in_fifo = FIFO_THRESHOLD - 1;
145 else
146 device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
147 }
148 dev_dbg(device->dev,
149 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
150 wake_up_interruptible(&device->fifo_wait_queue);
151
152 return IRQ_HANDLED;
153}
154
155
156
157static int
158rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
159{
160 int ret;
161 int payload_length;
162
163
164 ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
165 if (ret < 0)
166 return ret;
167 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
168 if (ret < 0)
169 return ret;
170 ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
171 if (ret < 0)
172 return ret;
173 ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
174 if (ret < 0)
175 return ret;
176 ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
177 if (ret < 0)
178 return ret;
179 ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
180 if (ret < 0)
181 return ret;
182 ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
183 rx_cfg->bw_exponent);
184 if (ret < 0)
185 return ret;
186 ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
187 rx_cfg->bw_exponent);
188 if (ret < 0)
189 return ret;
190 ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
191 if (ret < 0)
192 return ret;
193
194 dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
195
196
197
198 if (rx_cfg->enable_sync == OPTION_ON) {
199 ret = rf69_enable_sync(dev->spi);
200 if (ret < 0)
201 return ret;
202
203 ret = rf69_set_fifo_fill_condition(dev->spi,
204 after_sync_interrupt);
205 if (ret < 0)
206 return ret;
207 } else {
208 ret = rf69_disable_sync(dev->spi);
209 if (ret < 0)
210 return ret;
211
212 ret = rf69_set_fifo_fill_condition(dev->spi, always);
213 if (ret < 0)
214 return ret;
215 }
216 if (rx_cfg->enable_length_byte == OPTION_ON) {
217 ret = rf69_set_packet_format(dev->spi, packet_length_var);
218 if (ret < 0)
219 return ret;
220 } else {
221 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
222 if (ret < 0)
223 return ret;
224 }
225 ret = rf69_set_address_filtering(dev->spi,
226 rx_cfg->enable_address_filtering);
227 if (ret < 0)
228 return ret;
229
230 if (rx_cfg->enable_crc == OPTION_ON) {
231 ret = rf69_enable_crc(dev->spi);
232 if (ret < 0)
233 return ret;
234 } else {
235 ret = rf69_disable_crc(dev->spi);
236 if (ret < 0)
237 return ret;
238 }
239
240
241 ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
242 if (ret < 0)
243 return ret;
244 if (rx_cfg->enable_length_byte == OPTION_ON) {
245 ret = rf69_set_payload_length(dev->spi, 0xff);
246 if (ret < 0)
247 return ret;
248 } else if (rx_cfg->fixed_message_length != 0) {
249 payload_length = rx_cfg->fixed_message_length;
250 if (rx_cfg->enable_length_byte == OPTION_ON)
251 payload_length++;
252 if (rx_cfg->enable_address_filtering != filtering_off)
253 payload_length++;
254 ret = rf69_set_payload_length(dev->spi, payload_length);
255 if (ret < 0)
256 return ret;
257 } else {
258 ret = rf69_set_payload_length(dev->spi, 0);
259 if (ret < 0)
260 return ret;
261 }
262
263
264 if (rx_cfg->enable_sync == OPTION_ON) {
265 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
266 if (ret < 0)
267 return ret;
268 }
269 if (rx_cfg->enable_address_filtering != filtering_off) {
270 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
271 if (ret < 0)
272 return ret;
273 ret = rf69_set_broadcast_address(dev->spi,
274 rx_cfg->broadcast_address);
275 if (ret < 0)
276 return ret;
277 }
278
279 return 0;
280}
281
282static int
283rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
284{
285 int ret;
286
287 ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
288 if (ret < 0)
289 return ret;
290 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
291 if (ret < 0)
292 return ret;
293 ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
294 if (ret < 0)
295 return ret;
296 ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
297 if (ret < 0)
298 return ret;
299 ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
300 if (ret < 0)
301 return ret;
302 ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
303 if (ret < 0)
304 return ret;
305 ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
306 if (ret < 0)
307 return ret;
308
309
310 if (tx_cfg->enable_preamble == OPTION_ON) {
311 ret = rf69_set_preamble_length(dev->spi,
312 tx_cfg->preamble_length);
313 if (ret < 0)
314 return ret;
315 } else {
316 ret = rf69_set_preamble_length(dev->spi, 0);
317 if (ret < 0)
318 return ret;
319 }
320
321 if (tx_cfg->enable_sync == OPTION_ON) {
322 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
323 if (ret < 0)
324 return ret;
325 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
326 if (ret < 0)
327 return ret;
328 ret = rf69_enable_sync(dev->spi);
329 if (ret < 0)
330 return ret;
331 } else {
332 ret = rf69_disable_sync(dev->spi);
333 if (ret < 0)
334 return ret;
335 }
336
337 if (tx_cfg->enable_length_byte == OPTION_ON) {
338 ret = rf69_set_packet_format(dev->spi, packet_length_var);
339 if (ret < 0)
340 return ret;
341 } else {
342 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
343 if (ret < 0)
344 return ret;
345 }
346
347 if (tx_cfg->enable_crc == OPTION_ON) {
348 ret = rf69_enable_crc(dev->spi);
349 if (ret < 0)
350 return ret;
351 } else {
352 ret = rf69_disable_crc(dev->spi);
353 if (ret < 0)
354 return ret;
355 }
356
357 return 0;
358}
359
360
361
362static int
363pi433_start_rx(struct pi433_device *dev)
364{
365 int retval;
366
367
368 if (!dev->rx_active)
369 return 0;
370
371
372 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
373 if (retval)
374 return retval;
375
376
377 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
378 if (retval < 0)
379 return retval;
380 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
381 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
382
383
384 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
385 if (retval < 0)
386 return retval;
387 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
388 if (retval < 0)
389 return retval;
390 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
391 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
392
393
394 retval = rf69_set_mode(dev->spi, receive);
395 if (retval < 0)
396 return retval;
397
398 return 0;
399}
400
401
402
403static int
404pi433_receive(void *data)
405{
406 struct pi433_device *dev = data;
407 struct spi_device *spi = dev->spi;
408 int bytes_to_read, bytes_total;
409 int retval;
410
411 dev->interrupt_rx_allowed = false;
412
413
414 dev_dbg(dev->dev, "rx: going to wait for any tx to finish");
415 retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
416 if (retval) {
417
418 dev->interrupt_rx_allowed = true;
419 wake_up_interruptible(&dev->tx_wait_queue);
420 return retval;
421 }
422
423
424 dev->free_in_fifo = FIFO_SIZE;
425 dev->rx_position = 0;
426 dev->rx_bytes_dropped = 0;
427
428
429 retval = pi433_start_rx(dev);
430 if (retval)
431 return retval;
432
433
434 while (!rf69_get_flag(dev->spi, rssi_exceeded_threshold)) {
435
436 dev->interrupt_rx_allowed = true;
437 wake_up_interruptible(&dev->tx_wait_queue);
438
439
440 dev_dbg(dev->dev, "rx: going to wait for high RSSI level");
441 retval = wait_event_interruptible(dev->rx_wait_queue,
442 rf69_get_flag(dev->spi,
443 rssi_exceeded_threshold));
444 if (retval)
445 goto abort;
446 dev->interrupt_rx_allowed = false;
447
448
449 if (!dev->tx_active)
450 break;
451 }
452
453
454 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
455 if (retval < 0)
456 goto abort;
457 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
458 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
459
460
461 if (dev->rx_cfg.fixed_message_length != 0) {
462 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
463 retval = -1;
464 goto abort;
465 }
466 bytes_total = dev->rx_cfg.fixed_message_length;
467 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length",
468 bytes_total);
469 } else {
470 bytes_total = dev->rx_buffer_size;
471 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read",
472 bytes_total);
473 }
474
475
476 if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
477 retval = wait_event_interruptible(dev->fifo_wait_queue,
478 dev->free_in_fifo < FIFO_SIZE);
479 if (retval)
480 goto abort;
481
482 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
483 if (bytes_total > dev->rx_buffer_size) {
484 retval = -1;
485 goto abort;
486 }
487 dev->free_in_fifo++;
488 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte",
489 bytes_total);
490 }
491
492
493 if (dev->rx_cfg.enable_address_filtering != filtering_off) {
494 u8 dummy;
495
496 bytes_total--;
497
498 retval = wait_event_interruptible(dev->fifo_wait_queue,
499 dev->free_in_fifo < FIFO_SIZE);
500 if (retval)
501 goto abort;
502
503 rf69_read_fifo(spi, &dummy, 1);
504 dev->free_in_fifo++;
505 dev_dbg(dev->dev, "rx: address byte stripped off");
506 }
507
508
509 while (dev->rx_position < bytes_total) {
510 if (!rf69_get_flag(dev->spi, payload_ready)) {
511 retval = wait_event_interruptible(dev->fifo_wait_queue,
512 dev->free_in_fifo < FIFO_SIZE);
513 if (retval)
514 goto abort;
515 }
516
517
518 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
519 bytes_to_read = dev->rx_bytes_to_drop -
520 dev->rx_bytes_dropped;
521 else
522 bytes_to_read = bytes_total - dev->rx_position;
523
524
525 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
526 bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
527 retval = rf69_read_fifo(spi,
528 &dev->rx_buffer[dev->rx_position],
529 bytes_to_read);
530 if (retval)
531 goto abort;
532
533 dev->free_in_fifo += bytes_to_read;
534
535
536 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
537 dev->rx_bytes_dropped += bytes_to_read;
538 else
539 dev->rx_position += bytes_to_read;
540 }
541
542
543abort:
544 dev->interrupt_rx_allowed = true;
545 if (rf69_set_mode(dev->spi, standby))
546 pr_err("rf69_set_mode(): radio module failed to go standby\n");
547 wake_up_interruptible(&dev->tx_wait_queue);
548
549 if (retval)
550 return retval;
551 else
552 return bytes_total;
553}
554
555static int
556pi433_tx_thread(void *data)
557{
558 struct pi433_device *device = data;
559 struct spi_device *spi = device->spi;
560 struct pi433_tx_cfg tx_cfg;
561 size_t size;
562 bool rx_interrupted = false;
563 int position, repetitions;
564 int retval;
565
566 while (1) {
567
568 dev_dbg(device->dev, "thread: going to wait for new messages");
569 wait_event_interruptible(device->tx_wait_queue,
570 (!kfifo_is_empty(&device->tx_fifo) ||
571 kthread_should_stop()));
572 if (kthread_should_stop())
573 return 0;
574
575
576
577
578
579
580
581 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
582 if (retval != sizeof(tx_cfg)) {
583 dev_dbg(device->dev,
584 "reading tx_cfg from fifo failed: got %d byte(s), expected %d",
585 retval, (unsigned int)sizeof(tx_cfg));
586 continue;
587 }
588
589 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
590 if (retval != sizeof(size_t)) {
591 dev_dbg(device->dev,
592 "reading msg size from fifo failed: got %d, expected %d",
593 retval, (unsigned int)sizeof(size_t));
594 continue;
595 }
596
597
598 if (tx_cfg.fixed_message_length != 0)
599 size = tx_cfg.fixed_message_length;
600
601
602 if (tx_cfg.enable_length_byte == OPTION_ON)
603 size++;
604
605
606 if (tx_cfg.enable_address_byte == OPTION_ON)
607 size++;
608
609
610 memset(device->buffer, 0, size);
611 position = 0;
612
613
614 if (tx_cfg.enable_length_byte == OPTION_ON)
615
616
617
618
619 device->buffer[position++] = size - 1;
620
621
622 if (tx_cfg.enable_address_byte == OPTION_ON)
623 device->buffer[position++] = tx_cfg.address_byte;
624
625
626 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
627 sizeof(device->buffer) - position);
628 dev_dbg(device->dev,
629 "read %d message byte(s) from fifo queue.", retval);
630
631
632
633
634
635
636
637
638 wait_event_interruptible(device->tx_wait_queue,
639 !device->rx_active ||
640 device->interrupt_rx_allowed);
641
642
643
644
645
646 disable_irq(device->irq_num[DIO0]);
647 device->tx_active = true;
648
649
650 retval = rf69_set_mode(spi, standby);
651 if (retval < 0)
652 return retval;
653
654 if (device->rx_active && !rx_interrupted) {
655
656
657
658
659 rx_interrupted = true;
660 }
661
662 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
663 if (retval < 0)
664 return retval;
665 if (tx_cfg.enable_length_byte == OPTION_ON) {
666 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
667 if (retval < 0)
668 return retval;
669 } else {
670 retval = rf69_set_payload_length(spi, 0);
671 if (retval < 0)
672 return retval;
673 }
674
675
676 retval = rf69_set_tx_cfg(device, &tx_cfg);
677 if (retval < 0)
678 return retval;
679
680
681 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
682 if (retval < 0)
683 return retval;
684 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
685 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
686
687
688 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
689 if (retval < 0)
690 return retval;
691 device->irq_state[DIO0] = DIO_PACKET_SENT;
692 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
693 enable_irq(device->irq_num[DIO0]);
694
695
696 retval = rf69_set_mode(spi, transmit);
697 if (retval < 0)
698 return retval;
699
700
701 device->free_in_fifo = FIFO_SIZE;
702 position = 0;
703 repetitions = tx_cfg.repetitions;
704 while ((repetitions > 0) && (size > position)) {
705 if ((size - position) > device->free_in_fifo) {
706
707 int write_size = device->free_in_fifo;
708
709 device->free_in_fifo = 0;
710 rf69_write_fifo(spi,
711 &device->buffer[position],
712 write_size);
713 position += write_size;
714 } else {
715
716 device->free_in_fifo -= size;
717 repetitions--;
718 rf69_write_fifo(spi,
719 &device->buffer[position],
720 (size - position));
721 position = 0;
722 }
723
724 retval = wait_event_interruptible(device->fifo_wait_queue,
725 device->free_in_fifo > 0);
726 if (retval) {
727 dev_dbg(device->dev, "ABORT\n");
728 goto abort;
729 }
730 }
731
732
733 dev_dbg(device->dev,
734 "thread: wait for packet to get sent/fifo to be empty");
735 wait_event_interruptible(device->fifo_wait_queue,
736 device->free_in_fifo == FIFO_SIZE ||
737 kthread_should_stop());
738 if (kthread_should_stop())
739 return 0;
740
741
742 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.");
743 retval = rf69_set_mode(spi, standby);
744 if (retval < 0)
745 return retval;
746
747
748 if (kfifo_is_empty(&device->tx_fifo)) {
749abort:
750 if (rx_interrupted) {
751 rx_interrupted = false;
752 pi433_start_rx(device);
753 }
754 device->tx_active = false;
755 wake_up_interruptible(&device->rx_wait_queue);
756 }
757 }
758}
759
760
761
762static ssize_t
763pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
764{
765 struct pi433_instance *instance;
766 struct pi433_device *device;
767 int bytes_received;
768 ssize_t retval;
769
770
771 if (size > MAX_MSG_SIZE)
772 return -EMSGSIZE;
773
774 instance = filp->private_data;
775 device = instance->device;
776
777
778 mutex_lock(&device->rx_lock);
779 if (device->rx_active) {
780 mutex_unlock(&device->rx_lock);
781 return -EAGAIN;
782 }
783
784 device->rx_active = true;
785 mutex_unlock(&device->rx_lock);
786
787
788
789 device->rx_buffer_size = size;
790 bytes_received = pi433_receive(device);
791
792
793 mutex_lock(&device->rx_lock);
794 device->rx_active = false;
795 mutex_unlock(&device->rx_lock);
796
797
798 if (bytes_received > 0) {
799 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
800 if (retval)
801 return -EFAULT;
802 }
803
804 return bytes_received;
805}
806
807static ssize_t
808pi433_write(struct file *filp, const char __user *buf,
809 size_t count, loff_t *f_pos)
810{
811 struct pi433_instance *instance;
812 struct pi433_device *device;
813 int retval;
814 unsigned int required, available, copied;
815
816 instance = filp->private_data;
817 device = instance->device;
818
819
820
821
822
823 if (count > MAX_MSG_SIZE)
824 return -EMSGSIZE;
825
826
827
828
829
830
831
832 mutex_lock(&device->tx_fifo_lock);
833
834 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
835 available = kfifo_avail(&device->tx_fifo);
836 if (required > available) {
837 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available",
838 required, available);
839 mutex_unlock(&device->tx_fifo_lock);
840 return -EAGAIN;
841 }
842
843 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
844 sizeof(instance->tx_cfg));
845 if (retval != sizeof(instance->tx_cfg))
846 goto abort;
847
848 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
849 if (retval != sizeof(size_t))
850 goto abort;
851
852 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
853 if (retval || copied != count)
854 goto abort;
855
856 mutex_unlock(&device->tx_fifo_lock);
857
858
859 wake_up_interruptible(&device->tx_wait_queue);
860 dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied);
861
862 return copied;
863
864abort:
865 dev_warn(device->dev,
866 "write to fifo failed, non recoverable: 0x%x", retval);
867 mutex_unlock(&device->tx_fifo_lock);
868 return -EAGAIN;
869}
870
871static long
872pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
873{
874 struct pi433_instance *instance;
875 struct pi433_device *device;
876 struct pi433_tx_cfg tx_cfg;
877 void __user *argp = (void __user *)arg;
878
879
880 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
881 return -ENOTTY;
882
883 instance = filp->private_data;
884 device = instance->device;
885
886 if (!device)
887 return -ESHUTDOWN;
888
889 switch (cmd) {
890 case PI433_IOC_RD_TX_CFG:
891 if (copy_to_user(argp, &instance->tx_cfg,
892 sizeof(struct pi433_tx_cfg)))
893 return -EFAULT;
894 break;
895 case PI433_IOC_WR_TX_CFG:
896 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
897 return -EFAULT;
898 mutex_lock(&device->tx_fifo_lock);
899 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
900 mutex_unlock(&device->tx_fifo_lock);
901 break;
902 case PI433_IOC_RD_RX_CFG:
903 if (copy_to_user(argp, &device->rx_cfg,
904 sizeof(struct pi433_rx_cfg)))
905 return -EFAULT;
906 break;
907 case PI433_IOC_WR_RX_CFG:
908 mutex_lock(&device->rx_lock);
909
910
911 if (device->rx_active) {
912 mutex_unlock(&device->rx_lock);
913 return -EAGAIN;
914 }
915
916 if (copy_from_user(&device->rx_cfg, argp,
917 sizeof(struct pi433_rx_cfg))) {
918 mutex_unlock(&device->rx_lock);
919 return -EFAULT;
920 }
921
922 mutex_unlock(&device->rx_lock);
923 break;
924 default:
925 return -EINVAL;
926 }
927
928 return 0;
929}
930
931
932
933static int pi433_open(struct inode *inode, struct file *filp)
934{
935 struct pi433_device *device;
936 struct pi433_instance *instance;
937
938 mutex_lock(&minor_lock);
939 device = idr_find(&pi433_idr, iminor(inode));
940 mutex_unlock(&minor_lock);
941 if (!device) {
942 pr_debug("device: minor %d unknown.\n", iminor(inode));
943 return -ENODEV;
944 }
945
946 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
947 if (!instance)
948 return -ENOMEM;
949
950
951 instance->device = device;
952 instance->tx_cfg.bit_rate = 4711;
953
954
955
956 filp->private_data = instance;
957 stream_open(inode, filp);
958
959 return 0;
960}
961
962static int pi433_release(struct inode *inode, struct file *filp)
963{
964 struct pi433_instance *instance;
965
966 instance = filp->private_data;
967 kfree(instance);
968 filp->private_data = NULL;
969
970 return 0;
971}
972
973
974
975static int setup_gpio(struct pi433_device *device)
976{
977 char name[5];
978 int retval;
979 int i;
980 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
981 DIO0_irq_handler,
982 DIO1_irq_handler
983 };
984
985 for (i = 0; i < NUM_DIO; i++) {
986
987 snprintf(name, sizeof(name), "DIO%d", i);
988 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
989 0 );
990
991 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
992 dev_dbg(&device->spi->dev,
993 "Could not find entry for %s. Ignoring.", name);
994 continue;
995 }
996
997 if (device->gpiod[i] == ERR_PTR(-EBUSY))
998 dev_dbg(&device->spi->dev, "%s is busy.", name);
999
1000 if (IS_ERR(device->gpiod[i])) {
1001 retval = PTR_ERR(device->gpiod[i]);
1002
1003 for (i--; i >= 0; i--) {
1004 free_irq(device->irq_num[i], device);
1005 gpiod_put(device->gpiod[i]);
1006 }
1007 return retval;
1008 }
1009
1010
1011 gpiod_unexport(device->gpiod[i]);
1012 retval = gpiod_direction_input(device->gpiod[i]);
1013 if (retval)
1014 return retval;
1015
1016
1017 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1018 if (device->irq_num[i] < 0) {
1019 device->gpiod[i] = ERR_PTR(-EINVAL);
1020 return device->irq_num[i];
1021 }
1022 retval = request_irq(device->irq_num[i],
1023 DIO_irq_handler[i],
1024 0,
1025 name,
1026 device);
1027
1028 if (retval)
1029 return retval;
1030
1031 dev_dbg(&device->spi->dev, "%s successfully configured", name);
1032 }
1033
1034 return 0;
1035}
1036
1037static void free_gpio(struct pi433_device *device)
1038{
1039 int i;
1040
1041 for (i = 0; i < NUM_DIO; i++) {
1042
1043 if (IS_ERR(device->gpiod[i]))
1044 continue;
1045
1046 free_irq(device->irq_num[i], device);
1047 gpiod_put(device->gpiod[i]);
1048 }
1049}
1050
1051static int pi433_get_minor(struct pi433_device *device)
1052{
1053 int retval = -ENOMEM;
1054
1055 mutex_lock(&minor_lock);
1056 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1057 if (retval >= 0) {
1058 device->minor = retval;
1059 retval = 0;
1060 } else if (retval == -ENOSPC) {
1061 dev_err(&device->spi->dev, "too many pi433 devices\n");
1062 retval = -EINVAL;
1063 }
1064 mutex_unlock(&minor_lock);
1065 return retval;
1066}
1067
1068static void pi433_free_minor(struct pi433_device *dev)
1069{
1070 mutex_lock(&minor_lock);
1071 idr_remove(&pi433_idr, dev->minor);
1072 mutex_unlock(&minor_lock);
1073}
1074
1075
1076
1077static const struct file_operations pi433_fops = {
1078 .owner = THIS_MODULE,
1079
1080
1081
1082
1083
1084 .write = pi433_write,
1085 .read = pi433_read,
1086 .unlocked_ioctl = pi433_ioctl,
1087 .compat_ioctl = compat_ptr_ioctl,
1088 .open = pi433_open,
1089 .release = pi433_release,
1090 .llseek = no_llseek,
1091};
1092
1093
1094
1095static int pi433_probe(struct spi_device *spi)
1096{
1097 struct pi433_device *device;
1098 int retval;
1099
1100
1101 spi->mode = 0x00;
1102 spi->bits_per_word = 8;
1103
1104
1105
1106
1107
1108 retval = spi_setup(spi);
1109 if (retval) {
1110 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1111 return retval;
1112 }
1113
1114 dev_dbg(&spi->dev,
1115 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed",
1116 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1117
1118
1119 retval = spi_w8r8(spi, 0x10);
1120 if (retval < 0)
1121 return retval;
1122
1123 switch (retval) {
1124 case 0x24:
1125 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval);
1126 break;
1127 default:
1128 dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval);
1129 return -ENODEV;
1130 }
1131
1132
1133 device = kzalloc(sizeof(*device), GFP_KERNEL);
1134 if (!device)
1135 return -ENOMEM;
1136
1137
1138 device->spi = spi;
1139 device->rx_active = false;
1140 device->tx_active = false;
1141 device->interrupt_rx_allowed = false;
1142
1143
1144 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1145 if (!device->rx_buffer) {
1146 retval = -ENOMEM;
1147 goto RX_failed;
1148 }
1149
1150
1151 init_waitqueue_head(&device->tx_wait_queue);
1152 init_waitqueue_head(&device->rx_wait_queue);
1153 init_waitqueue_head(&device->fifo_wait_queue);
1154
1155
1156 INIT_KFIFO(device->tx_fifo);
1157
1158
1159 mutex_init(&device->tx_fifo_lock);
1160 mutex_init(&device->rx_lock);
1161
1162
1163 retval = setup_gpio(device);
1164 if (retval) {
1165 dev_dbg(&spi->dev, "setup of GPIOs failed");
1166 goto GPIO_failed;
1167 }
1168
1169
1170 retval = rf69_set_mode(spi, standby);
1171 if (retval < 0)
1172 goto minor_failed;
1173 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1174 if (retval < 0)
1175 goto minor_failed;
1176 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1177 if (retval < 0)
1178 goto minor_failed;
1179 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1180 if (retval < 0)
1181 goto minor_failed;
1182 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1183 if (retval < 0)
1184 goto minor_failed;
1185 retval = rf69_set_output_power_level(spi, 13);
1186 if (retval < 0)
1187 goto minor_failed;
1188 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1189 if (retval < 0)
1190 goto minor_failed;
1191
1192
1193 retval = pi433_get_minor(device);
1194 if (retval) {
1195 dev_dbg(&spi->dev, "get of minor number failed");
1196 goto minor_failed;
1197 }
1198
1199
1200 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1201 device->dev = device_create(pi433_class,
1202 &spi->dev,
1203 device->devt,
1204 device,
1205 "pi433.%d",
1206 device->minor);
1207 if (IS_ERR(device->dev)) {
1208 pr_err("pi433: device register failed\n");
1209 retval = PTR_ERR(device->dev);
1210 goto device_create_failed;
1211 } else {
1212 dev_dbg(device->dev,
1213 "created device for major %d, minor %d\n",
1214 MAJOR(pi433_dev),
1215 device->minor);
1216 }
1217
1218
1219 device->tx_task_struct = kthread_run(pi433_tx_thread,
1220 device,
1221 "pi433.%d_tx_task",
1222 device->minor);
1223 if (IS_ERR(device->tx_task_struct)) {
1224 dev_dbg(device->dev, "start of send thread failed");
1225 retval = PTR_ERR(device->tx_task_struct);
1226 goto send_thread_failed;
1227 }
1228
1229
1230 device->cdev = cdev_alloc();
1231 if (!device->cdev) {
1232 dev_dbg(device->dev, "allocation of cdev failed");
1233 goto cdev_failed;
1234 }
1235 device->cdev->owner = THIS_MODULE;
1236 cdev_init(device->cdev, &pi433_fops);
1237 retval = cdev_add(device->cdev, device->devt, 1);
1238 if (retval) {
1239 dev_dbg(device->dev, "register of cdev failed");
1240 goto del_cdev;
1241 }
1242
1243
1244 spi_set_drvdata(spi, device);
1245
1246 return 0;
1247
1248del_cdev:
1249 cdev_del(device->cdev);
1250cdev_failed:
1251 kthread_stop(device->tx_task_struct);
1252send_thread_failed:
1253 device_destroy(pi433_class, device->devt);
1254device_create_failed:
1255 pi433_free_minor(device);
1256minor_failed:
1257 free_gpio(device);
1258GPIO_failed:
1259 kfree(device->rx_buffer);
1260RX_failed:
1261 kfree(device);
1262
1263 return retval;
1264}
1265
1266static int pi433_remove(struct spi_device *spi)
1267{
1268 struct pi433_device *device = spi_get_drvdata(spi);
1269
1270
1271 free_gpio(device);
1272
1273
1274 device->spi = NULL;
1275
1276 kthread_stop(device->tx_task_struct);
1277
1278 device_destroy(pi433_class, device->devt);
1279
1280 cdev_del(device->cdev);
1281
1282 pi433_free_minor(device);
1283
1284 kfree(device->rx_buffer);
1285 kfree(device);
1286
1287 return 0;
1288}
1289
1290static const struct of_device_id pi433_dt_ids[] = {
1291 { .compatible = "Smarthome-Wolf,pi433" },
1292 {},
1293};
1294
1295MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1296
1297static struct spi_driver pi433_spi_driver = {
1298 .driver = {
1299 .name = "pi433",
1300 .owner = THIS_MODULE,
1301 .of_match_table = of_match_ptr(pi433_dt_ids),
1302 },
1303 .probe = pi433_probe,
1304 .remove = pi433_remove,
1305
1306
1307
1308
1309
1310
1311
1312};
1313
1314
1315
1316static int __init pi433_init(void)
1317{
1318 int status;
1319
1320
1321
1322
1323
1324 if (MAX_MSG_SIZE < FIFO_SIZE)
1325 return -EINVAL;
1326
1327
1328
1329
1330
1331
1332 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1333 if (status < 0)
1334 return status;
1335
1336 pi433_class = class_create(THIS_MODULE, "pi433");
1337 if (IS_ERR(pi433_class)) {
1338 unregister_chrdev(MAJOR(pi433_dev),
1339 pi433_spi_driver.driver.name);
1340 return PTR_ERR(pi433_class);
1341 }
1342
1343 status = spi_register_driver(&pi433_spi_driver);
1344 if (status < 0) {
1345 class_destroy(pi433_class);
1346 unregister_chrdev(MAJOR(pi433_dev),
1347 pi433_spi_driver.driver.name);
1348 }
1349
1350 return status;
1351}
1352
1353module_init(pi433_init);
1354
1355static void __exit pi433_exit(void)
1356{
1357 spi_unregister_driver(&pi433_spi_driver);
1358 class_destroy(pi433_class);
1359 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1360}
1361module_exit(pi433_exit);
1362
1363MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1364MODULE_DESCRIPTION("Driver for Pi433");
1365MODULE_LICENSE("GPL");
1366MODULE_ALIAS("spi:pi433");
1367