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