1 // SPDX-License-Identifier: GPL-2.0+
3 * userspace interface for pi433 radio module
5 * Pi433 is a 433MHz radio module for the Raspberry Pi.
6 * It is based on the HopeRf Module RFM69CW. Therefore inside of this
7 * driver, you'll find an abstraction of the rf69 chip.
9 * If needed, this driver could be extended, to also support other
10 * devices, basing on HopeRfs rf69.
12 * The driver can also be extended, to support other modules of
13 * HopeRf with a similar interace - e. g. RFM69HCW, RFM12, RFM95, ...
15 * Copyright (C) 2016 Wolf-Entwicklungen
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>
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>
34 #include <linux/interrupt.h>
35 #include <linux/irq.h>
36 #include <linux/gpio/consumer.h>
37 #include <linux/kthread.h>
38 #include <linux/wait.h>
39 #include <linux/spi/spi.h>
41 #include <linux/compat.h>
43 #include <linux/debugfs.h>
44 #include <linux/seq_file.h>
49 #define N_PI433_MINORS BIT(MINORBITS) /*32*/ /* ... up to 256 */
50 #define MAX_MSG_SIZE 900 /* min: FIFO_SIZE! */
51 #define MSG_FIFO_SIZE 65536 /* 65536 = 2^16 */
54 static dev_t pi433_dev;
55 static DEFINE_IDR(pi433_idr);
56 static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
57 static struct dentry *root_dir; /* debugfs root directory for the driver */
59 /* mainly for udev to create /dev/pi433 */
60 static const struct class pi433_class = {
65 * tx config is instance specific
66 * so with each open a new tx config struct is needed
69 * rx config is device specific
70 * so we have just one rx config, ebedded in device struct
73 /* device handling related values */
78 struct spi_device *spi;
80 /* irq related values */
81 struct gpio_desc *gpiod[NUM_DIO];
83 u8 irq_state[NUM_DIO];
85 /* tx related values */
86 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
87 struct mutex tx_fifo_lock; /* serialize userspace writers */
88 struct task_struct *tx_task_struct;
89 wait_queue_head_t tx_wait_queue;
91 char buffer[MAX_MSG_SIZE];
93 /* rx related values */
94 struct pi433_rx_cfg rx_cfg;
96 unsigned int rx_buffer_size;
99 unsigned int rx_position;
100 struct mutex rx_lock; /* protects rx_* variable accesses */
101 wait_queue_head_t rx_wait_queue;
103 /* fifo wait queue */
104 struct task_struct *fifo_task_struct;
105 wait_queue_head_t fifo_wait_queue;
110 bool interrupt_rx_allowed;
113 struct pi433_instance {
114 struct pi433_device *device;
115 struct pi433_tx_cfg tx_cfg;
118 bool tx_cfg_initialized;
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_modulation(dev->spi, rx_cfg->modulation);
178 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
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_modulation(dev->spi, tx_cfg->modulation);
301 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
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_set_sync_size(dev->spi, tx_cfg->sync_length);
333 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
336 ret = rf69_enable_sync(dev->spi);
340 ret = rf69_disable_sync(dev->spi);
345 if (tx_cfg->enable_length_byte == OPTION_ON) {
346 ret = rf69_set_packet_format(dev->spi, packet_length_var);
350 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
355 if (tx_cfg->enable_crc == OPTION_ON) {
356 ret = rf69_enable_crc(dev->spi);
360 ret = rf69_disable_crc(dev->spi);
368 /*-------------------------------------------------------------------------*/
370 static int pi433_start_rx(struct pi433_device *dev)
374 /* return without action, if no pending read request */
378 /* setup for receiving */
379 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
384 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
387 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
388 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
390 /* setup fifo level interrupt */
391 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
394 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
397 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
398 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
400 /* set module to receiving mode */
401 retval = rf69_set_mode(dev->spi, receive);
408 /*-------------------------------------------------------------------------*/
410 static int pi433_receive(void *data)
412 struct pi433_device *dev = data;
413 struct spi_device *spi = dev->spi;
414 int bytes_to_read, bytes_total;
417 dev->interrupt_rx_allowed = false;
419 /* wait for any tx to finish */
420 dev_dbg(dev->dev, "rx: going to wait for any tx to finish\n");
421 retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
423 /* wait was interrupted */
424 dev->interrupt_rx_allowed = true;
425 wake_up_interruptible(&dev->tx_wait_queue);
429 /* prepare status vars */
430 dev->free_in_fifo = FIFO_SIZE;
431 dev->rx_position = 0;
432 dev->rx_bytes_dropped = 0;
434 /* setup radio module to listen for something "in the air" */
435 retval = pi433_start_rx(dev);
439 /* now check RSSI, if low wait for getting high (RSSI interrupt) */
440 while (!(rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RSSI)) {
441 /* allow tx to interrupt us while waiting for high RSSI */
442 dev->interrupt_rx_allowed = true;
443 wake_up_interruptible(&dev->tx_wait_queue);
445 /* wait for RSSI level to become high */
446 dev_dbg(dev->dev, "rx: going to wait for high RSSI level\n");
447 retval = wait_event_interruptible(dev->rx_wait_queue,
448 rf69_read_reg(spi, REG_IRQFLAGS1) &
449 MASK_IRQFLAGS1_RSSI);
450 if (retval) /* wait was interrupted */
452 dev->interrupt_rx_allowed = false;
454 /* cross check for ongoing tx */
459 /* configure payload ready irq */
460 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
463 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
464 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
466 /* fixed or unlimited length? */
467 if (dev->rx_cfg.fixed_message_length != 0) {
468 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
472 bytes_total = dev->rx_cfg.fixed_message_length;
473 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length\n",
476 bytes_total = dev->rx_buffer_size;
477 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read\n",
481 /* length byte enabled? */
482 if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
483 retval = wait_event_interruptible(dev->fifo_wait_queue,
484 dev->free_in_fifo < FIFO_SIZE);
485 if (retval) /* wait was interrupted */
488 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
489 if (bytes_total > dev->rx_buffer_size) {
494 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte\n",
498 /* address byte enabled? */
499 if (dev->rx_cfg.enable_address_filtering != filtering_off) {
504 retval = wait_event_interruptible(dev->fifo_wait_queue,
505 dev->free_in_fifo < FIFO_SIZE);
506 if (retval) /* wait was interrupted */
509 rf69_read_fifo(spi, &dummy, 1);
511 dev_dbg(dev->dev, "rx: address byte stripped off\n");
515 while (dev->rx_position < bytes_total) {
516 if (!(rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PAYLOAD_READY)) {
517 retval = wait_event_interruptible(dev->fifo_wait_queue,
518 dev->free_in_fifo < FIFO_SIZE);
519 if (retval) /* wait was interrupted */
523 /* need to drop bytes or acquire? */
524 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
525 bytes_to_read = dev->rx_bytes_to_drop -
526 dev->rx_bytes_dropped;
528 bytes_to_read = bytes_total - dev->rx_position;
530 /* access the fifo */
531 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
532 bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
533 retval = rf69_read_fifo(spi,
534 &dev->rx_buffer[dev->rx_position],
536 if (retval) /* read failed */
539 dev->free_in_fifo += bytes_to_read;
541 /* adjust status vars */
542 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
543 dev->rx_bytes_dropped += bytes_to_read;
545 dev->rx_position += bytes_to_read;
548 /* rx done, wait was interrupted or error occurred */
550 dev->interrupt_rx_allowed = true;
551 if (rf69_set_mode(dev->spi, standby))
552 pr_err("rf69_set_mode(): radio module failed to go standby\n");
553 wake_up_interruptible(&dev->tx_wait_queue);
561 static int pi433_tx_thread(void *data)
563 struct pi433_device *device = data;
564 struct spi_device *spi = device->spi;
565 struct pi433_tx_cfg tx_cfg;
567 bool rx_interrupted = false;
568 int position, repetitions;
572 /* wait for fifo to be populated or for request to terminate*/
573 dev_dbg(device->dev, "thread: going to wait for new messages\n");
574 wait_event_interruptible(device->tx_wait_queue,
575 (!kfifo_is_empty(&device->tx_fifo) ||
576 kthread_should_stop()));
577 if (kthread_should_stop())
581 * get data from fifo in the following order:
586 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
587 if (retval != sizeof(tx_cfg)) {
589 "reading tx_cfg from fifo failed: got %d byte(s), expected %d\n",
590 retval, (unsigned int)sizeof(tx_cfg));
594 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
595 if (retval != sizeof(size_t)) {
597 "reading msg size from fifo failed: got %d, expected %d\n",
598 retval, (unsigned int)sizeof(size_t));
602 /* use fixed message length, if requested */
603 if (tx_cfg.fixed_message_length != 0)
604 size = tx_cfg.fixed_message_length;
606 /* increase size, if len byte is requested */
607 if (tx_cfg.enable_length_byte == OPTION_ON)
610 /* increase size, if adr byte is requested */
611 if (tx_cfg.enable_address_byte == OPTION_ON)
615 memset(device->buffer, 0, size);
618 /* add length byte, if requested */
619 if (tx_cfg.enable_length_byte == OPTION_ON)
621 * according to spec, length byte itself must be
622 * excluded from the length calculation
624 device->buffer[position++] = size - 1;
626 /* add adr byte, if requested */
627 if (tx_cfg.enable_address_byte == OPTION_ON)
628 device->buffer[position++] = tx_cfg.address_byte;
630 /* finally get message data from fifo */
631 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
632 sizeof(device->buffer) - position);
634 "read %d message byte(s) from fifo queue.\n", retval);
637 * if rx is active, we need to interrupt the waiting for
638 * incoming telegrams, to be able to send something.
639 * We are only allowed, if currently no reception takes
640 * place otherwise we need to wait for the incoming telegram
643 wait_event_interruptible(device->tx_wait_queue,
644 !device->rx_active ||
645 device->interrupt_rx_allowed);
648 * prevent race conditions
649 * irq will be reenabled after tx config is set
651 disable_irq(device->irq_num[DIO0]);
652 device->tx_active = true;
654 /* clear fifo, set fifo threshold, set payload length */
655 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
659 if (device->rx_active && !rx_interrupted) {
661 * rx is currently waiting for a telegram;
662 * we need to set the radio module to standby
664 rx_interrupted = true;
667 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
670 if (tx_cfg.enable_length_byte == OPTION_ON) {
671 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
675 retval = rf69_set_payload_length(spi, 0);
680 /* configure the rf chip */
681 retval = rf69_set_tx_cfg(device, &tx_cfg);
685 /* enable fifo level interrupt */
686 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
689 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
690 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
692 /* enable packet sent interrupt */
693 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
696 device->irq_state[DIO0] = DIO_PACKET_SENT;
697 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
698 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
700 /* enable transmission */
701 retval = rf69_set_mode(spi, transmit);
705 /* transfer this msg (and repetitions) to chip fifo */
706 device->free_in_fifo = FIFO_SIZE;
708 repetitions = tx_cfg.repetitions;
709 while ((repetitions > 0) && (size > position)) {
710 if ((size - position) > device->free_in_fifo) {
711 /* msg to big for fifo - take a part */
712 int write_size = device->free_in_fifo;
714 device->free_in_fifo = 0;
716 &device->buffer[position],
718 position += write_size;
720 /* msg fits into fifo - take all */
721 device->free_in_fifo -= size;
724 &device->buffer[position],
726 position = 0; /* reset for next repetition */
729 retval = wait_event_interruptible(device->fifo_wait_queue,
730 device->free_in_fifo > 0);
732 dev_dbg(device->dev, "ABORT\n");
737 /* we are done. Wait for packet to get sent */
739 "thread: wait for packet to get sent/fifo to be empty\n");
740 wait_event_interruptible(device->fifo_wait_queue,
741 device->free_in_fifo == FIFO_SIZE ||
742 kthread_should_stop());
743 if (kthread_should_stop())
746 /* STOP_TRANSMISSION */
747 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.\n");
748 retval = rf69_set_mode(spi, standby);
752 /* everything sent? */
753 if (kfifo_is_empty(&device->tx_fifo)) {
755 if (rx_interrupted) {
756 rx_interrupted = false;
757 pi433_start_rx(device);
759 device->tx_active = false;
760 wake_up_interruptible(&device->rx_wait_queue);
765 /*-------------------------------------------------------------------------*/
768 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
770 struct pi433_instance *instance;
771 struct pi433_device *device;
775 /* check, whether internal buffer is big enough for requested size */
776 if (size > MAX_MSG_SIZE)
779 instance = filp->private_data;
780 device = instance->device;
782 /* just one read request at a time */
783 mutex_lock(&device->rx_lock);
784 if (device->rx_active) {
785 mutex_unlock(&device->rx_lock);
789 device->rx_active = true;
790 mutex_unlock(&device->rx_lock);
792 /* start receiving */
793 /* will block until something was received*/
794 device->rx_buffer_size = size;
795 bytes_received = pi433_receive(device);
798 mutex_lock(&device->rx_lock);
799 device->rx_active = false;
800 mutex_unlock(&device->rx_lock);
802 /* if read was successful copy to user space*/
803 if (bytes_received > 0) {
804 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
809 return bytes_received;
813 pi433_write(struct file *filp, const char __user *buf,
814 size_t count, loff_t *f_pos)
816 struct pi433_instance *instance;
817 struct pi433_device *device;
819 unsigned int required, available, copied;
821 instance = filp->private_data;
822 device = instance->device;
825 * check, whether internal buffer (tx thread) is big enough
828 if (count > MAX_MSG_SIZE)
832 * check if tx_cfg has been initialized otherwise we won't be able to
833 * config the RF trasmitter correctly due to invalid settings
835 if (!instance->tx_cfg_initialized) {
836 dev_notice_once(device->dev,
837 "write: failed due to unconfigured tx_cfg (see PI433_IOC_WR_TX_CFG)\n");
842 * write the following sequence into fifo:
847 mutex_lock(&device->tx_fifo_lock);
849 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
850 available = kfifo_avail(&device->tx_fifo);
851 if (required > available) {
852 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available\n",
853 required, available);
854 mutex_unlock(&device->tx_fifo_lock);
858 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
859 sizeof(instance->tx_cfg));
860 if (retval != sizeof(instance->tx_cfg))
863 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
864 if (retval != sizeof(size_t))
867 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
868 if (retval || copied != count)
871 mutex_unlock(&device->tx_fifo_lock);
874 wake_up_interruptible(&device->tx_wait_queue);
875 dev_dbg(device->dev, "write: generated new msg with %d bytes.\n", copied);
880 dev_warn(device->dev,
881 "write to fifo failed, non recoverable: 0x%x\n", retval);
882 mutex_unlock(&device->tx_fifo_lock);
886 static long pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
888 struct pi433_instance *instance;
889 struct pi433_device *device;
890 struct pi433_tx_cfg tx_cfg;
891 void __user *argp = (void __user *)arg;
893 /* Check type and command number */
894 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
897 instance = filp->private_data;
898 device = instance->device;
904 case PI433_IOC_RD_TX_CFG:
905 if (copy_to_user(argp, &instance->tx_cfg,
906 sizeof(struct pi433_tx_cfg)))
909 case PI433_IOC_WR_TX_CFG:
910 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
912 mutex_lock(&device->tx_fifo_lock);
913 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
914 instance->tx_cfg_initialized = true;
915 mutex_unlock(&device->tx_fifo_lock);
917 case PI433_IOC_RD_RX_CFG:
918 if (copy_to_user(argp, &device->rx_cfg,
919 sizeof(struct pi433_rx_cfg)))
922 case PI433_IOC_WR_RX_CFG:
923 mutex_lock(&device->rx_lock);
925 /* during pendig read request, change of config not allowed */
926 if (device->rx_active) {
927 mutex_unlock(&device->rx_lock);
931 if (copy_from_user(&device->rx_cfg, argp,
932 sizeof(struct pi433_rx_cfg))) {
933 mutex_unlock(&device->rx_lock);
937 mutex_unlock(&device->rx_lock);
946 /*-------------------------------------------------------------------------*/
948 static int pi433_open(struct inode *inode, struct file *filp)
950 struct pi433_device *device;
951 struct pi433_instance *instance;
953 mutex_lock(&minor_lock);
954 device = idr_find(&pi433_idr, iminor(inode));
955 mutex_unlock(&minor_lock);
957 pr_debug("device: minor %d unknown.\n", iminor(inode));
961 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
965 /* setup instance data*/
966 instance->device = device;
968 /* instance data as context */
969 filp->private_data = instance;
970 stream_open(inode, filp);
975 static int pi433_release(struct inode *inode, struct file *filp)
977 struct pi433_instance *instance;
979 instance = filp->private_data;
981 filp->private_data = NULL;
986 /*-------------------------------------------------------------------------*/
988 static int setup_gpio(struct pi433_device *device)
993 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
998 for (i = 0; i < NUM_DIO; i++) {
999 /* "construct" name and get the gpio descriptor */
1000 snprintf(name, sizeof(name), "DIO%d", i);
1001 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
1004 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1005 dev_dbg(&device->spi->dev,
1006 "Could not find entry for %s. Ignoring.\n", name);
1010 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1011 dev_dbg(&device->spi->dev, "%s is busy.\n", name);
1013 if (IS_ERR(device->gpiod[i])) {
1014 retval = PTR_ERR(device->gpiod[i]);
1015 /* release already allocated gpios */
1016 for (i--; i >= 0; i--) {
1017 free_irq(device->irq_num[i], device);
1018 gpiod_put(device->gpiod[i]);
1023 /* configure the pin */
1024 retval = gpiod_direction_input(device->gpiod[i]);
1029 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1030 if (device->irq_num[i] < 0) {
1031 device->gpiod[i] = ERR_PTR(-EINVAL);
1032 return device->irq_num[i];
1034 retval = request_irq(device->irq_num[i],
1043 dev_dbg(&device->spi->dev, "%s successfully configured\n", name);
1049 static void free_gpio(struct pi433_device *device)
1053 for (i = 0; i < NUM_DIO; i++) {
1054 /* check if gpiod is valid */
1055 if (IS_ERR(device->gpiod[i]))
1058 free_irq(device->irq_num[i], device);
1059 gpiod_put(device->gpiod[i]);
1063 static int pi433_get_minor(struct pi433_device *device)
1065 int retval = -ENOMEM;
1067 mutex_lock(&minor_lock);
1068 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1070 device->minor = retval;
1072 } else if (retval == -ENOSPC) {
1073 dev_err(&device->spi->dev, "too many pi433 devices\n");
1076 mutex_unlock(&minor_lock);
1080 static void pi433_free_minor(struct pi433_device *dev)
1082 mutex_lock(&minor_lock);
1083 idr_remove(&pi433_idr, dev->minor);
1084 mutex_unlock(&minor_lock);
1087 /*-------------------------------------------------------------------------*/
1089 static const struct file_operations pi433_fops = {
1090 .owner = THIS_MODULE,
1092 * REVISIT switch to aio primitives, so that userspace
1093 * gets more complete API coverage. It'll simplify things
1094 * too, except for the locking.
1096 .write = pi433_write,
1098 .unlocked_ioctl = pi433_ioctl,
1099 .compat_ioctl = compat_ptr_ioctl,
1101 .release = pi433_release,
1102 .llseek = no_llseek,
1105 static int pi433_debugfs_regs_show(struct seq_file *m, void *p)
1107 struct pi433_device *dev;
1110 char *fmt = "0x%02x, 0x%02x\n";
1115 mutex_lock(&dev->tx_fifo_lock);
1116 mutex_lock(&dev->rx_lock);
1118 // wait for on-going operations to finish
1119 ret = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
1123 ret = wait_event_interruptible(dev->tx_wait_queue, !dev->rx_active);
1127 // skip FIFO register (0x0) otherwise this can affect some of uC ops
1128 for (i = 1; i < 0x50; i++)
1129 reg_data[i] = rf69_read_reg(dev->spi, i);
1131 reg_data[REG_TESTLNA] = rf69_read_reg(dev->spi, REG_TESTLNA);
1132 reg_data[REG_TESTPA1] = rf69_read_reg(dev->spi, REG_TESTPA1);
1133 reg_data[REG_TESTPA2] = rf69_read_reg(dev->spi, REG_TESTPA2);
1134 reg_data[REG_TESTDAGC] = rf69_read_reg(dev->spi, REG_TESTDAGC);
1135 reg_data[REG_TESTAFC] = rf69_read_reg(dev->spi, REG_TESTAFC);
1137 seq_puts(m, "# reg, val\n");
1139 for (i = 1; i < 0x50; i++)
1140 seq_printf(m, fmt, i, reg_data[i]);
1142 seq_printf(m, fmt, REG_TESTLNA, reg_data[REG_TESTLNA]);
1143 seq_printf(m, fmt, REG_TESTPA1, reg_data[REG_TESTPA1]);
1144 seq_printf(m, fmt, REG_TESTPA2, reg_data[REG_TESTPA2]);
1145 seq_printf(m, fmt, REG_TESTDAGC, reg_data[REG_TESTDAGC]);
1146 seq_printf(m, fmt, REG_TESTAFC, reg_data[REG_TESTAFC]);
1149 mutex_unlock(&dev->rx_lock);
1150 mutex_unlock(&dev->tx_fifo_lock);
1154 DEFINE_SHOW_ATTRIBUTE(pi433_debugfs_regs);
1156 /*-------------------------------------------------------------------------*/
1158 static int pi433_probe(struct spi_device *spi)
1160 struct pi433_device *device;
1162 struct dentry *entry;
1164 /* setup spi parameters */
1166 spi->bits_per_word = 8;
1168 * spi->max_speed_hz = 10000000;
1169 * 1MHz already set by device tree overlay
1172 retval = spi_setup(spi);
1174 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1179 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed\n",
1180 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1182 /* read chip version */
1183 retval = rf69_get_version(spi);
1189 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)\n", retval);
1192 dev_dbg(&spi->dev, "unknown chip version: 0x%x\n", retval);
1196 /* Allocate driver data */
1197 device = kzalloc(sizeof(*device), GFP_KERNEL);
1201 /* Initialize the driver data */
1203 device->rx_active = false;
1204 device->tx_active = false;
1205 device->interrupt_rx_allowed = false;
1207 /* init rx buffer */
1208 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1209 if (!device->rx_buffer) {
1214 /* init wait queues */
1215 init_waitqueue_head(&device->tx_wait_queue);
1216 init_waitqueue_head(&device->rx_wait_queue);
1217 init_waitqueue_head(&device->fifo_wait_queue);
1220 INIT_KFIFO(device->tx_fifo);
1222 /* init mutexes and locks */
1223 mutex_init(&device->tx_fifo_lock);
1224 mutex_init(&device->rx_lock);
1226 /* setup GPIO (including irq_handler) for the different DIOs */
1227 retval = setup_gpio(device);
1229 dev_dbg(&spi->dev, "setup of GPIOs failed\n");
1233 /* setup the radio module */
1234 retval = rf69_set_mode(spi, standby);
1237 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1240 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1243 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1246 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1249 retval = rf69_set_output_power_level(spi, 13);
1252 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1256 /* determ minor number */
1257 retval = pi433_get_minor(device);
1259 dev_dbg(&spi->dev, "get of minor number failed\n");
1264 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1265 device->dev = device_create(&pi433_class,
1271 if (IS_ERR(device->dev)) {
1272 pr_err("pi433: device register failed\n");
1273 retval = PTR_ERR(device->dev);
1274 goto device_create_failed;
1276 dev_dbg(device->dev,
1277 "created device for major %d, minor %d\n",
1282 /* start tx thread */
1283 device->tx_task_struct = kthread_run(pi433_tx_thread,
1287 if (IS_ERR(device->tx_task_struct)) {
1288 dev_dbg(device->dev, "start of send thread failed\n");
1289 retval = PTR_ERR(device->tx_task_struct);
1290 goto send_thread_failed;
1294 device->cdev = cdev_alloc();
1295 if (!device->cdev) {
1296 dev_dbg(device->dev, "allocation of cdev failed\n");
1300 device->cdev->owner = THIS_MODULE;
1301 cdev_init(device->cdev, &pi433_fops);
1302 retval = cdev_add(device->cdev, device->devt, 1);
1304 dev_dbg(device->dev, "register of cdev failed\n");
1309 spi_set_drvdata(spi, device);
1311 entry = debugfs_create_dir(dev_name(device->dev), root_dir);
1312 debugfs_create_file("regs", 0400, entry, device, &pi433_debugfs_regs_fops);
1317 cdev_del(device->cdev);
1319 kthread_stop(device->tx_task_struct);
1321 device_destroy(&pi433_class, device->devt);
1322 device_create_failed:
1323 pi433_free_minor(device);
1327 kfree(device->rx_buffer);
1334 static void pi433_remove(struct spi_device *spi)
1336 struct pi433_device *device = spi_get_drvdata(spi);
1338 debugfs_lookup_and_remove(dev_name(device->dev), root_dir);
1343 /* make sure ops on existing fds can abort cleanly */
1346 kthread_stop(device->tx_task_struct);
1348 device_destroy(&pi433_class, device->devt);
1350 cdev_del(device->cdev);
1352 pi433_free_minor(device);
1354 kfree(device->rx_buffer);
1358 static const struct of_device_id pi433_dt_ids[] = {
1359 { .compatible = "Smarthome-Wolf,pi433" },
1363 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1365 static struct spi_driver pi433_spi_driver = {
1368 .owner = THIS_MODULE,
1369 .of_match_table = of_match_ptr(pi433_dt_ids),
1371 .probe = pi433_probe,
1372 .remove = pi433_remove,
1375 * NOTE: suspend/resume methods are not necessary here.
1376 * We don't do anything except pass the requests to/from
1377 * the underlying controller. The refrigerator handles
1378 * most issues; the controller driver handles the rest.
1382 /*-------------------------------------------------------------------------*/
1384 static int __init pi433_init(void)
1389 * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1390 * work stable - risk of buffer overflow
1392 if (MAX_MSG_SIZE < FIFO_SIZE)
1396 * Claim device numbers. Then register a class
1397 * that will key udev/mdev to add/remove /dev nodes.
1398 * Last, register the driver which manages those device numbers.
1400 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1404 status = class_register(&pi433_class);
1406 unregister_chrdev(MAJOR(pi433_dev),
1407 pi433_spi_driver.driver.name);
1411 root_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1413 status = spi_register_driver(&pi433_spi_driver);
1415 class_unregister(&pi433_class);
1416 unregister_chrdev(MAJOR(pi433_dev),
1417 pi433_spi_driver.driver.name);
1423 module_init(pi433_init);
1425 static void __exit pi433_exit(void)
1427 spi_unregister_driver(&pi433_spi_driver);
1428 class_unregister(&pi433_class);
1429 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1430 debugfs_remove(root_dir);
1432 module_exit(pi433_exit);
1435 MODULE_DESCRIPTION("Driver for Pi433");
1436 MODULE_LICENSE("GPL");
1437 MODULE_ALIAS("spi:pi433");