2 * userspace interface for pi433 radio module
4 * Pi433 is a 433MHz radio module for the Raspberry Pi.
5 * It is based on the HopeRf Module RFM69CW. Therefore inside of this
6 * driver, you'll find an abstraction of the rf69 chip.
8 * If needed, this driver could be extended, to also support other
9 * devices, basing on HopeRfs rf69.
11 * The driver can also be extended, to support other modules of
12 * HopeRf with a similar interace - e. g. RFM69HCW, RFM12, RFM95, ...
14 * Copyright (C) 2016 Wolf-Entwicklungen
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
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>
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>
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>
51 #include <linux/compat.h>
57 #define N_PI433_MINORS BIT(MINORBITS) /*32*/ /* ... up to 256 */
58 #define MAX_MSG_SIZE 900 /* min: FIFO_SIZE! */
59 #define MSG_FIFO_SIZE 65536 /* 65536 = 2^16 */
62 static dev_t pi433_dev;
63 static DEFINE_IDR(pi433_idr);
64 static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
66 static struct class *pi433_class; /* mainly for udev to create /dev/pi433 */
68 /* tx config is instance specific
69 * so with each open a new tx config struct is needed
71 /* rx config is device specific
72 * so we have just one rx config, ebedded in device struct
75 /* device handling related values */
80 struct spi_device *spi;
83 /* irq related values */
84 struct gpio_desc *gpiod[NUM_DIO];
86 u8 irq_state[NUM_DIO];
88 /* tx related values */
89 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
90 struct mutex tx_fifo_lock; /* serialize userspace writers */
91 struct task_struct *tx_task_struct;
92 wait_queue_head_t tx_wait_queue;
94 char buffer[MAX_MSG_SIZE];
96 /* rx related values */
97 struct pi433_rx_cfg rx_cfg;
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;
106 /* fifo wait queue */
107 struct task_struct *fifo_task_struct;
108 wait_queue_head_t fifo_wait_queue;
113 bool interrupt_rx_allowed;
116 struct pi433_instance {
117 struct pi433_device *device;
118 struct pi433_tx_cfg tx_cfg;
121 /*-------------------------------------------------------------------------*/
123 /* GPIO interrupt handlers */
124 static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
126 struct pi433_device *device = dev_id;
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);
144 static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
146 struct pi433_device *device = dev_id;
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;
154 device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
157 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
158 wake_up_interruptible(&device->fifo_wait_queue);
163 /*-------------------------------------------------------------------------*/
166 rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
171 /* receiver config */
172 ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
175 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
178 ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
181 ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
184 ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
187 ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
190 ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
191 rx_cfg->bw_exponent);
194 ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
195 rx_cfg->bw_exponent);
198 ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
202 dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
206 if (rx_cfg->enable_sync == OPTION_ON) {
207 ret = rf69_enable_sync(dev->spi);
211 ret = rf69_set_fifo_fill_condition(dev->spi,
212 after_sync_interrupt);
216 ret = rf69_disable_sync(dev->spi);
220 ret = rf69_set_fifo_fill_condition(dev->spi, always);
224 if (rx_cfg->enable_length_byte == OPTION_ON) {
225 ret = rf69_set_packet_format(dev->spi, packet_length_var);
229 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
233 ret = rf69_set_address_filtering(dev->spi,
234 rx_cfg->enable_address_filtering);
238 if (rx_cfg->enable_crc == OPTION_ON) {
239 ret = rf69_enable_crc(dev->spi);
243 ret = rf69_disable_crc(dev->spi);
249 ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
252 if (rx_cfg->enable_length_byte == OPTION_ON) {
253 ret = rf69_set_payload_length(dev->spi, 0xff);
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)
260 if (rx_cfg->enable_address_filtering != filtering_off)
262 ret = rf69_set_payload_length(dev->spi, payload_length);
266 ret = rf69_set_payload_length(dev->spi, 0);
272 if (rx_cfg->enable_sync == OPTION_ON) {
273 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
277 if (rx_cfg->enable_address_filtering != filtering_off) {
278 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
281 ret = rf69_set_broadcast_address(dev->spi,
282 rx_cfg->broadcast_address);
291 rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
295 ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
298 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
301 ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
304 ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
307 ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
310 ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
313 ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
317 /* packet format enable */
318 if (tx_cfg->enable_preamble == OPTION_ON) {
319 ret = rf69_set_preamble_length(dev->spi,
320 tx_cfg->preamble_length);
324 ret = rf69_set_preamble_length(dev->spi, 0);
329 if (tx_cfg->enable_sync == OPTION_ON) {
330 ret = rf69_enable_sync(dev->spi);
334 ret = rf69_disable_sync(dev->spi);
339 if (tx_cfg->enable_length_byte == OPTION_ON) {
340 ret = rf69_set_packet_format(dev->spi, packet_length_var);
344 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
349 if (tx_cfg->enable_crc == OPTION_ON) {
350 ret = rf69_enable_crc(dev->spi);
354 ret = rf69_disable_crc(dev->spi);
359 /* configure sync, if enabled */
360 if (tx_cfg->enable_sync == OPTION_ON) {
361 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
364 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
372 /*-------------------------------------------------------------------------*/
375 pi433_start_rx(struct pi433_device *dev)
379 /* return without action, if no pending read request */
383 /* setup for receiving */
384 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
389 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
392 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
393 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
395 /* setup fifo level interrupt */
396 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
399 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
402 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
403 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
405 /* set module to receiving mode */
406 retval = rf69_set_mode(dev->spi, receive);
413 /*-------------------------------------------------------------------------*/
416 pi433_receive(void *data)
418 struct pi433_device *dev = data;
419 struct spi_device *spi = dev->spi;
420 int bytes_to_read, bytes_total;
423 dev->interrupt_rx_allowed = false;
425 /* wait for any tx to finish */
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);
429 /* wait was interrupted */
430 dev->interrupt_rx_allowed = true;
431 wake_up_interruptible(&dev->tx_wait_queue);
435 /* prepare status vars */
436 dev->free_in_fifo = FIFO_SIZE;
437 dev->rx_position = 0;
438 dev->rx_bytes_dropped = 0;
440 /* setup radio module to listen for something "in the air" */
441 retval = pi433_start_rx(dev);
445 /* now check RSSI, if low wait for getting high (RSSI interrupt) */
446 while (!rf69_get_flag(dev->spi, rssi_exceeded_threshold)) {
447 /* allow tx to interrupt us while waiting for high RSSI */
448 dev->interrupt_rx_allowed = true;
449 wake_up_interruptible(&dev->tx_wait_queue);
451 /* wait for RSSI level to become high */
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) /* wait was interrupted */
458 dev->interrupt_rx_allowed = false;
460 /* cross check for ongoing tx */
465 /* configure payload ready irq */
466 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
469 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
470 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
472 /* fixed or unlimited length? */
473 if (dev->rx_cfg.fixed_message_length != 0) {
474 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
478 bytes_total = dev->rx_cfg.fixed_message_length;
479 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length",
482 bytes_total = dev->rx_buffer_size;
483 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read",
487 /* length byte enabled? */
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) /* wait was interrupted */
494 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
495 if (bytes_total > dev->rx_buffer_size) {
500 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte",
504 /* address byte enabled? */
505 if (dev->rx_cfg.enable_address_filtering != filtering_off) {
510 retval = wait_event_interruptible(dev->fifo_wait_queue,
511 dev->free_in_fifo < FIFO_SIZE);
512 if (retval) /* wait was interrupted */
515 rf69_read_fifo(spi, &dummy, 1);
517 dev_dbg(dev->dev, "rx: address byte stripped off");
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) /* wait was interrupted */
529 /* need to drop bytes or acquire? */
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;
534 bytes_to_read = bytes_total - dev->rx_position;
536 /* access the fifo */
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],
542 if (retval) /* read failed */
545 dev->free_in_fifo += bytes_to_read;
547 /* adjust status vars */
548 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
549 dev->rx_bytes_dropped += bytes_to_read;
551 dev->rx_position += bytes_to_read;
554 /* rx done, wait was interrupted or error occurred */
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);
568 pi433_tx_thread(void *data)
570 struct pi433_device *device = data;
571 struct spi_device *spi = device->spi;
572 struct pi433_tx_cfg tx_cfg;
574 bool rx_interrupted = false;
575 int position, repetitions;
579 /* wait for fifo to be populated or for request to terminate*/
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())
587 /* get data from fifo in the following order:
592 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
593 if (retval != sizeof(tx_cfg)) {
595 "reading tx_cfg from fifo failed: got %d byte(s), expected %d",
596 retval, (unsigned int)sizeof(tx_cfg));
600 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
601 if (retval != sizeof(size_t)) {
603 "reading msg size from fifo failed: got %d, expected %d",
604 retval, (unsigned int)sizeof(size_t));
608 /* use fixed message length, if requested */
609 if (tx_cfg.fixed_message_length != 0)
610 size = tx_cfg.fixed_message_length;
612 /* increase size, if len byte is requested */
613 if (tx_cfg.enable_length_byte == OPTION_ON)
616 /* increase size, if adr byte is requested */
617 if (tx_cfg.enable_address_byte == OPTION_ON)
621 memset(device->buffer, 0, size);
624 /* add length byte, if requested */
625 if (tx_cfg.enable_length_byte == OPTION_ON)
627 * according to spec, length byte itself must be
628 * excluded from the length calculation
630 device->buffer[position++] = size - 1;
632 /* add adr byte, if requested */
633 if (tx_cfg.enable_address_byte == OPTION_ON)
634 device->buffer[position++] = tx_cfg.address_byte;
636 /* finally get message data from fifo */
637 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
638 sizeof(device->buffer) - position);
640 "read %d message byte(s) from fifo queue.", retval);
642 /* if rx is active, we need to interrupt the waiting for
643 * incoming telegrams, to be able to send something.
644 * We are only allowed, if currently no reception takes
645 * place otherwise we need to wait for the incoming telegram
648 wait_event_interruptible(device->tx_wait_queue,
649 !device->rx_active ||
650 device->interrupt_rx_allowed);
652 /* prevent race conditions
653 * irq will be reenabled after tx config is set
655 disable_irq(device->irq_num[DIO0]);
656 device->tx_active = true;
658 if (device->rx_active && !rx_interrupted) {
659 /* rx is currently waiting for a telegram;
660 * we need to set the radio module to standby
662 retval = rf69_set_mode(device->spi, standby);
665 rx_interrupted = true;
668 /* clear fifo, set fifo threshold, set payload length */
669 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
672 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
675 if (tx_cfg.enable_length_byte == OPTION_ON) {
676 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
680 retval = rf69_set_payload_length(spi, 0);
685 /* configure the rf chip */
686 retval = rf69_set_tx_cfg(device, &tx_cfg);
690 /* enable fifo level interrupt */
691 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
694 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
695 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
697 /* enable packet sent interrupt */
698 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
701 device->irq_state[DIO0] = DIO_PACKET_SENT;
702 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
703 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
705 /* enable transmission */
706 retval = rf69_set_mode(spi, transmit);
710 /* transfer this msg (and repetitions) to chip fifo */
711 device->free_in_fifo = FIFO_SIZE;
713 repetitions = tx_cfg.repetitions;
714 while ((repetitions > 0) && (size > position)) {
715 if ((size - position) > device->free_in_fifo) {
716 /* msg to big for fifo - take a part */
717 int write_size = device->free_in_fifo;
719 device->free_in_fifo = 0;
721 &device->buffer[position],
723 position += write_size;
725 /* msg fits into fifo - take all */
726 device->free_in_fifo -= size;
729 &device->buffer[position],
731 position = 0; /* reset for next repetition */
734 retval = wait_event_interruptible(device->fifo_wait_queue,
735 device->free_in_fifo > 0);
737 dev_dbg(device->dev, "ABORT\n");
742 /* we are done. Wait for packet to get sent */
744 "thread: wait for packet to get sent/fifo to be empty");
745 wait_event_interruptible(device->fifo_wait_queue,
746 device->free_in_fifo == FIFO_SIZE ||
747 kthread_should_stop());
748 if (kthread_should_stop())
749 dev_dbg(device->dev, "ABORT\n");
751 /* STOP_TRANSMISSION */
752 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.");
753 retval = rf69_set_mode(spi, standby);
757 /* everything sent? */
758 if (kfifo_is_empty(&device->tx_fifo)) {
760 if (rx_interrupted) {
761 rx_interrupted = false;
762 pi433_start_rx(device);
764 device->tx_active = false;
765 wake_up_interruptible(&device->rx_wait_queue);
770 /*-------------------------------------------------------------------------*/
773 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
775 struct pi433_instance *instance;
776 struct pi433_device *device;
780 /* check, whether internal buffer is big enough for requested size */
781 if (size > MAX_MSG_SIZE)
784 instance = filp->private_data;
785 device = instance->device;
787 /* just one read request at a time */
788 mutex_lock(&device->rx_lock);
789 if (device->rx_active) {
790 mutex_unlock(&device->rx_lock);
794 device->rx_active = true;
795 mutex_unlock(&device->rx_lock);
797 /* start receiving */
798 /* will block until something was received*/
799 device->rx_buffer_size = size;
800 bytes_received = pi433_receive(device);
803 mutex_lock(&device->rx_lock);
804 device->rx_active = false;
805 mutex_unlock(&device->rx_lock);
807 /* if read was successful copy to user space*/
808 if (bytes_received > 0) {
809 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
814 return bytes_received;
818 pi433_write(struct file *filp, const char __user *buf,
819 size_t count, loff_t *f_pos)
821 struct pi433_instance *instance;
822 struct pi433_device *device;
824 unsigned int required, available, copied;
826 instance = filp->private_data;
827 device = instance->device;
829 /* check, whether internal buffer (tx thread) is big enough for requested size */
830 if (count > MAX_MSG_SIZE)
833 /* write the following sequence into fifo:
838 mutex_lock(&device->tx_fifo_lock);
840 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
841 available = kfifo_avail(&device->tx_fifo);
842 if (required > available) {
843 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available",
844 required, available);
845 mutex_unlock(&device->tx_fifo_lock);
849 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
850 sizeof(instance->tx_cfg));
851 if (retval != sizeof(instance->tx_cfg))
854 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
855 if (retval != sizeof(size_t))
858 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
859 if (retval || copied != count)
862 mutex_unlock(&device->tx_fifo_lock);
865 wake_up_interruptible(&device->tx_wait_queue);
866 dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied);
871 dev_warn(device->dev,
872 "write to fifo failed, non recoverable: 0x%x", retval);
873 mutex_unlock(&device->tx_fifo_lock);
878 pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
881 struct pi433_instance *instance;
882 struct pi433_device *device;
883 void __user *argp = (void __user *)arg;
885 /* Check type and command number */
886 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
889 /* TODO? guard against device removal before, or while,
890 * we issue this ioctl. --> device_get()
892 instance = filp->private_data;
893 device = instance->device;
899 case PI433_IOC_RD_TX_CFG:
900 if (copy_to_user(argp, &instance->tx_cfg,
901 sizeof(struct pi433_tx_cfg)))
904 case PI433_IOC_WR_TX_CFG:
905 if (copy_from_user(&instance->tx_cfg, argp,
906 sizeof(struct pi433_tx_cfg)))
909 case PI433_IOC_RD_RX_CFG:
910 if (copy_to_user(argp, &device->rx_cfg,
911 sizeof(struct pi433_rx_cfg)))
914 case PI433_IOC_WR_RX_CFG:
915 mutex_lock(&device->rx_lock);
917 /* during pendig read request, change of config not allowed */
918 if (device->rx_active) {
919 mutex_unlock(&device->rx_lock);
923 if (copy_from_user(&device->rx_cfg, argp,
924 sizeof(struct pi433_rx_cfg))) {
925 mutex_unlock(&device->rx_lock);
929 mutex_unlock(&device->rx_lock);
940 pi433_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
942 return pi433_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
945 #define pi433_compat_ioctl NULL
946 #endif /* CONFIG_COMPAT */
948 /*-------------------------------------------------------------------------*/
950 static int pi433_open(struct inode *inode, struct file *filp)
952 struct pi433_device *device;
953 struct pi433_instance *instance;
955 mutex_lock(&minor_lock);
956 device = idr_find(&pi433_idr, iminor(inode));
957 mutex_unlock(&minor_lock);
959 pr_debug("device: minor %d unknown.\n", iminor(inode));
963 if (!device->rx_buffer) {
964 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
965 if (!device->rx_buffer)
970 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
972 kfree(device->rx_buffer);
973 device->rx_buffer = NULL;
977 /* setup instance data*/
978 instance->device = device;
979 instance->tx_cfg.bit_rate = 4711;
980 // TODO: fill instance->tx_cfg;
982 /* instance data as context */
983 filp->private_data = instance;
984 nonseekable_open(inode, filp);
989 static int pi433_release(struct inode *inode, struct file *filp)
991 struct pi433_instance *instance;
992 struct pi433_device *device;
994 instance = filp->private_data;
995 device = instance->device;
997 filp->private_data = NULL;
1002 if (!device->users) {
1003 kfree(device->rx_buffer);
1004 device->rx_buffer = NULL;
1012 /*-------------------------------------------------------------------------*/
1014 static int setup_gpio(struct pi433_device *device)
1019 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
1024 for (i = 0; i < NUM_DIO; i++) {
1025 /* "construct" name and get the gpio descriptor */
1026 snprintf(name, sizeof(name), "DIO%d", i);
1027 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
1030 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1031 dev_dbg(&device->spi->dev,
1032 "Could not find entry for %s. Ignoring.", name);
1036 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1037 dev_dbg(&device->spi->dev, "%s is busy.", name);
1039 if (IS_ERR(device->gpiod[i])) {
1040 retval = PTR_ERR(device->gpiod[i]);
1041 /* release already allocated gpios */
1042 for (i--; i >= 0; i--) {
1043 free_irq(device->irq_num[i], device);
1044 gpiod_put(device->gpiod[i]);
1049 /* configure the pin */
1050 gpiod_unexport(device->gpiod[i]);
1051 retval = gpiod_direction_input(device->gpiod[i]);
1056 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1057 if (device->irq_num[i] < 0) {
1058 device->gpiod[i] = ERR_PTR(-EINVAL);
1059 return device->irq_num[i];
1061 retval = request_irq(device->irq_num[i],
1070 dev_dbg(&device->spi->dev, "%s successfully configured", name);
1076 static void free_gpio(struct pi433_device *device)
1080 for (i = 0; i < NUM_DIO; i++) {
1081 /* check if gpiod is valid */
1082 if (IS_ERR(device->gpiod[i]))
1085 free_irq(device->irq_num[i], device);
1086 gpiod_put(device->gpiod[i]);
1090 static int pi433_get_minor(struct pi433_device *device)
1092 int retval = -ENOMEM;
1094 mutex_lock(&minor_lock);
1095 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1097 device->minor = retval;
1099 } else if (retval == -ENOSPC) {
1100 dev_err(&device->spi->dev, "too many pi433 devices\n");
1103 mutex_unlock(&minor_lock);
1107 static void pi433_free_minor(struct pi433_device *dev)
1109 mutex_lock(&minor_lock);
1110 idr_remove(&pi433_idr, dev->minor);
1111 mutex_unlock(&minor_lock);
1114 /*-------------------------------------------------------------------------*/
1116 static const struct file_operations pi433_fops = {
1117 .owner = THIS_MODULE,
1118 /* REVISIT switch to aio primitives, so that userspace
1119 * gets more complete API coverage. It'll simplify things
1120 * too, except for the locking.
1122 .write = pi433_write,
1124 .unlocked_ioctl = pi433_ioctl,
1125 .compat_ioctl = pi433_compat_ioctl,
1127 .release = pi433_release,
1128 .llseek = no_llseek,
1131 /*-------------------------------------------------------------------------*/
1133 static int pi433_probe(struct spi_device *spi)
1135 struct pi433_device *device;
1138 /* setup spi parameters */
1140 spi->bits_per_word = 8;
1141 /* spi->max_speed_hz = 10000000; 1MHz already set by device tree overlay */
1143 retval = spi_setup(spi);
1145 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1150 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed",
1151 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1153 /* Ping the chip by reading the version register */
1154 retval = spi_w8r8(spi, 0x10);
1160 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval);
1163 dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval);
1167 /* Allocate driver data */
1168 device = kzalloc(sizeof(*device), GFP_KERNEL);
1172 /* Initialize the driver data */
1174 device->rx_active = false;
1175 device->tx_active = false;
1176 device->interrupt_rx_allowed = false;
1178 /* init wait queues */
1179 init_waitqueue_head(&device->tx_wait_queue);
1180 init_waitqueue_head(&device->rx_wait_queue);
1181 init_waitqueue_head(&device->fifo_wait_queue);
1184 INIT_KFIFO(device->tx_fifo);
1186 /* init mutexes and locks */
1187 mutex_init(&device->tx_fifo_lock);
1188 mutex_init(&device->rx_lock);
1190 /* setup GPIO (including irq_handler) for the different DIOs */
1191 retval = setup_gpio(device);
1193 dev_dbg(&spi->dev, "setup of GPIOs failed");
1197 /* setup the radio module */
1198 retval = rf69_set_mode(spi, standby);
1201 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1204 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1207 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1210 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1213 retval = rf69_set_output_power_level(spi, 13);
1216 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1220 /* determ minor number */
1221 retval = pi433_get_minor(device);
1223 dev_dbg(&spi->dev, "get of minor number failed");
1228 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1229 device->dev = device_create(pi433_class,
1235 if (IS_ERR(device->dev)) {
1236 pr_err("pi433: device register failed\n");
1237 retval = PTR_ERR(device->dev);
1238 goto device_create_failed;
1240 dev_dbg(device->dev,
1241 "created device for major %d, minor %d\n",
1246 /* start tx thread */
1247 device->tx_task_struct = kthread_run(pi433_tx_thread,
1251 if (IS_ERR(device->tx_task_struct)) {
1252 dev_dbg(device->dev, "start of send thread failed");
1253 goto send_thread_failed;
1257 device->cdev = cdev_alloc();
1258 device->cdev->owner = THIS_MODULE;
1259 cdev_init(device->cdev, &pi433_fops);
1260 retval = cdev_add(device->cdev, device->devt, 1);
1262 dev_dbg(device->dev, "register of cdev failed");
1267 spi_set_drvdata(spi, device);
1272 kthread_stop(device->tx_task_struct);
1274 device_destroy(pi433_class, device->devt);
1275 device_create_failed:
1276 pi433_free_minor(device);
1285 static int pi433_remove(struct spi_device *spi)
1287 struct pi433_device *device = spi_get_drvdata(spi);
1292 /* make sure ops on existing fds can abort cleanly */
1295 kthread_stop(device->tx_task_struct);
1297 device_destroy(pi433_class, device->devt);
1299 cdev_del(device->cdev);
1301 pi433_free_minor(device);
1303 if (device->users == 0)
1309 static const struct of_device_id pi433_dt_ids[] = {
1310 { .compatible = "Smarthome-Wolf,pi433" },
1314 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1316 static struct spi_driver pi433_spi_driver = {
1319 .owner = THIS_MODULE,
1320 .of_match_table = of_match_ptr(pi433_dt_ids),
1322 .probe = pi433_probe,
1323 .remove = pi433_remove,
1325 /* NOTE: suspend/resume methods are not necessary here.
1326 * We don't do anything except pass the requests to/from
1327 * the underlying controller. The refrigerator handles
1328 * most issues; the controller driver handles the rest.
1332 /*-------------------------------------------------------------------------*/
1334 static int __init pi433_init(void)
1338 /* If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1339 * work stable - risk of buffer overflow
1341 if (MAX_MSG_SIZE < FIFO_SIZE)
1344 /* Claim device numbers. Then register a class
1345 * that will key udev/mdev to add/remove /dev nodes. Last, register
1346 * Last, register the driver which manages those device numbers.
1348 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1352 pi433_class = class_create(THIS_MODULE, "pi433");
1353 if (IS_ERR(pi433_class)) {
1354 unregister_chrdev(MAJOR(pi433_dev),
1355 pi433_spi_driver.driver.name);
1356 return PTR_ERR(pi433_class);
1359 status = spi_register_driver(&pi433_spi_driver);
1361 class_destroy(pi433_class);
1362 unregister_chrdev(MAJOR(pi433_dev),
1363 pi433_spi_driver.driver.name);
1369 module_init(pi433_init);
1371 static void __exit pi433_exit(void)
1373 spi_unregister_driver(&pi433_spi_driver);
1374 class_destroy(pi433_class);
1375 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1377 module_exit(pi433_exit);
1380 MODULE_DESCRIPTION("Driver for Pi433");
1381 MODULE_LICENSE("GPL");
1382 MODULE_ALIAS("spi:pi433");