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
16 * Marcus Wolf <linux@wolf-entwicklungen.de>
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/idr.h>
34 #include <linux/ioctl.h>
35 #include <linux/uaccess.h>
37 #include <linux/device.h>
38 #include <linux/cdev.h>
39 #include <linux/err.h>
40 #include <linux/kfifo.h>
41 #include <linux/errno.h>
42 #include <linux/mutex.h>
44 #include <linux/of_device.h>
45 #include <linux/interrupt.h>
46 #include <linux/irq.h>
47 #include <linux/gpio/consumer.h>
48 #include <linux/kthread.h>
49 #include <linux/wait.h>
50 #include <linux/spi/spi.h>
52 #include <linux/compat.h>
58 #define N_PI433_MINORS BIT(MINORBITS) /*32*/ /* ... up to 256 */
59 #define MAX_MSG_SIZE 900 /* min: FIFO_SIZE! */
60 #define MSG_FIFO_SIZE 65536 /* 65536 = 2^16 */
63 static dev_t pi433_dev;
64 static DEFINE_IDR(pi433_idr);
65 static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
67 static struct class *pi433_class; /* mainly for udev to create /dev/pi433 */
70 * tx config is instance specific
71 * so with each open a new tx config struct is needed
74 * rx config is device specific
75 * so we have just one rx config, ebedded in device struct
78 /* device handling related values */
83 struct spi_device *spi;
85 /* irq related values */
86 struct gpio_desc *gpiod[NUM_DIO];
88 u8 irq_state[NUM_DIO];
90 /* tx related values */
91 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
92 struct mutex tx_fifo_lock; /* serialize userspace writers */
93 struct task_struct *tx_task_struct;
94 wait_queue_head_t tx_wait_queue;
96 char buffer[MAX_MSG_SIZE];
98 /* rx related values */
99 struct pi433_rx_cfg rx_cfg;
101 unsigned int rx_buffer_size;
102 u32 rx_bytes_to_drop;
103 u32 rx_bytes_dropped;
104 unsigned int rx_position;
105 struct mutex rx_lock;
106 wait_queue_head_t rx_wait_queue;
108 /* fifo wait queue */
109 struct task_struct *fifo_task_struct;
110 wait_queue_head_t fifo_wait_queue;
115 bool interrupt_rx_allowed;
118 struct pi433_instance {
119 struct pi433_device *device;
120 struct pi433_tx_cfg tx_cfg;
123 /*-------------------------------------------------------------------------*/
125 /* GPIO interrupt handlers */
126 static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
128 struct pi433_device *device = dev_id;
130 if (device->irq_state[DIO0] == DIO_PACKET_SENT) {
131 device->free_in_fifo = FIFO_SIZE;
132 dev_dbg(device->dev, "DIO0 irq: Packet sent\n");
133 wake_up_interruptible(&device->fifo_wait_queue);
134 } else if (device->irq_state[DIO0] == DIO_RSSI_DIO0) {
135 dev_dbg(device->dev, "DIO0 irq: RSSI level over threshold\n");
136 wake_up_interruptible(&device->rx_wait_queue);
137 } else if (device->irq_state[DIO0] == DIO_PAYLOAD_READY) {
138 dev_dbg(device->dev, "DIO0 irq: Payload ready\n");
139 device->free_in_fifo = 0;
140 wake_up_interruptible(&device->fifo_wait_queue);
146 static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
148 struct pi433_device *device = dev_id;
150 if (device->irq_state[DIO1] == DIO_FIFO_NOT_EMPTY_DIO1) {
151 device->free_in_fifo = FIFO_SIZE;
152 } else if (device->irq_state[DIO1] == DIO_FIFO_LEVEL) {
153 if (device->rx_active)
154 device->free_in_fifo = FIFO_THRESHOLD - 1;
156 device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
159 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
160 wake_up_interruptible(&device->fifo_wait_queue);
165 /*-------------------------------------------------------------------------*/
168 rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
173 /* receiver config */
174 ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
177 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
180 ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
183 ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
186 ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
189 ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
192 ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
193 rx_cfg->bw_exponent);
196 ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
197 rx_cfg->bw_exponent);
200 ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
204 dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
208 if (rx_cfg->enable_sync == OPTION_ON) {
209 ret = rf69_enable_sync(dev->spi);
213 ret = rf69_set_fifo_fill_condition(dev->spi,
214 after_sync_interrupt);
218 ret = rf69_disable_sync(dev->spi);
222 ret = rf69_set_fifo_fill_condition(dev->spi, always);
226 if (rx_cfg->enable_length_byte == OPTION_ON) {
227 ret = rf69_set_packet_format(dev->spi, packet_length_var);
231 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
235 ret = rf69_set_address_filtering(dev->spi,
236 rx_cfg->enable_address_filtering);
240 if (rx_cfg->enable_crc == OPTION_ON) {
241 ret = rf69_enable_crc(dev->spi);
245 ret = rf69_disable_crc(dev->spi);
251 ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
254 if (rx_cfg->enable_length_byte == OPTION_ON) {
255 ret = rf69_set_payload_length(dev->spi, 0xff);
258 } else if (rx_cfg->fixed_message_length != 0) {
259 payload_length = rx_cfg->fixed_message_length;
260 if (rx_cfg->enable_length_byte == OPTION_ON)
262 if (rx_cfg->enable_address_filtering != filtering_off)
264 ret = rf69_set_payload_length(dev->spi, payload_length);
268 ret = rf69_set_payload_length(dev->spi, 0);
274 if (rx_cfg->enable_sync == OPTION_ON) {
275 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
279 if (rx_cfg->enable_address_filtering != filtering_off) {
280 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
283 ret = rf69_set_broadcast_address(dev->spi,
284 rx_cfg->broadcast_address);
293 rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
297 ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
300 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
303 ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
306 ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
309 ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
312 ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
315 ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
319 /* packet format enable */
320 if (tx_cfg->enable_preamble == OPTION_ON) {
321 ret = rf69_set_preamble_length(dev->spi,
322 tx_cfg->preamble_length);
326 ret = rf69_set_preamble_length(dev->spi, 0);
331 if (tx_cfg->enable_sync == OPTION_ON) {
332 ret = rf69_enable_sync(dev->spi);
336 ret = rf69_disable_sync(dev->spi);
341 if (tx_cfg->enable_length_byte == OPTION_ON) {
342 ret = rf69_set_packet_format(dev->spi, packet_length_var);
346 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
351 if (tx_cfg->enable_crc == OPTION_ON) {
352 ret = rf69_enable_crc(dev->spi);
356 ret = rf69_disable_crc(dev->spi);
361 /* configure sync, if enabled */
362 if (tx_cfg->enable_sync == OPTION_ON) {
363 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
366 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
374 /*-------------------------------------------------------------------------*/
377 pi433_start_rx(struct pi433_device *dev)
381 /* return without action, if no pending read request */
385 /* setup for receiving */
386 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
391 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
394 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
395 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
397 /* setup fifo level interrupt */
398 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
401 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
404 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
405 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
407 /* set module to receiving mode */
408 retval = rf69_set_mode(dev->spi, receive);
415 /*-------------------------------------------------------------------------*/
418 pi433_receive(void *data)
420 struct pi433_device *dev = data;
421 struct spi_device *spi = dev->spi;
422 int bytes_to_read, bytes_total;
425 dev->interrupt_rx_allowed = false;
427 /* wait for any tx to finish */
428 dev_dbg(dev->dev, "rx: going to wait for any tx to finish");
429 retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
431 /* wait was interrupted */
432 dev->interrupt_rx_allowed = true;
433 wake_up_interruptible(&dev->tx_wait_queue);
437 /* prepare status vars */
438 dev->free_in_fifo = FIFO_SIZE;
439 dev->rx_position = 0;
440 dev->rx_bytes_dropped = 0;
442 /* setup radio module to listen for something "in the air" */
443 retval = pi433_start_rx(dev);
447 /* now check RSSI, if low wait for getting high (RSSI interrupt) */
448 while (!rf69_get_flag(dev->spi, rssi_exceeded_threshold)) {
449 /* allow tx to interrupt us while waiting for high RSSI */
450 dev->interrupt_rx_allowed = true;
451 wake_up_interruptible(&dev->tx_wait_queue);
453 /* wait for RSSI level to become high */
454 dev_dbg(dev->dev, "rx: going to wait for high RSSI level");
455 retval = wait_event_interruptible(dev->rx_wait_queue,
456 rf69_get_flag(dev->spi,
457 rssi_exceeded_threshold));
458 if (retval) /* wait was interrupted */
460 dev->interrupt_rx_allowed = false;
462 /* cross check for ongoing tx */
467 /* configure payload ready irq */
468 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
471 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
472 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
474 /* fixed or unlimited length? */
475 if (dev->rx_cfg.fixed_message_length != 0) {
476 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
480 bytes_total = dev->rx_cfg.fixed_message_length;
481 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length",
484 bytes_total = dev->rx_buffer_size;
485 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read",
489 /* length byte enabled? */
490 if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
491 retval = wait_event_interruptible(dev->fifo_wait_queue,
492 dev->free_in_fifo < FIFO_SIZE);
493 if (retval) /* wait was interrupted */
496 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
497 if (bytes_total > dev->rx_buffer_size) {
502 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte",
506 /* address byte enabled? */
507 if (dev->rx_cfg.enable_address_filtering != filtering_off) {
512 retval = wait_event_interruptible(dev->fifo_wait_queue,
513 dev->free_in_fifo < FIFO_SIZE);
514 if (retval) /* wait was interrupted */
517 rf69_read_fifo(spi, &dummy, 1);
519 dev_dbg(dev->dev, "rx: address byte stripped off");
523 while (dev->rx_position < bytes_total) {
524 if (!rf69_get_flag(dev->spi, payload_ready)) {
525 retval = wait_event_interruptible(dev->fifo_wait_queue,
526 dev->free_in_fifo < FIFO_SIZE);
527 if (retval) /* wait was interrupted */
531 /* need to drop bytes or acquire? */
532 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
533 bytes_to_read = dev->rx_bytes_to_drop -
534 dev->rx_bytes_dropped;
536 bytes_to_read = bytes_total - dev->rx_position;
538 /* access the fifo */
539 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
540 bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
541 retval = rf69_read_fifo(spi,
542 &dev->rx_buffer[dev->rx_position],
544 if (retval) /* read failed */
547 dev->free_in_fifo += bytes_to_read;
549 /* adjust status vars */
550 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
551 dev->rx_bytes_dropped += bytes_to_read;
553 dev->rx_position += bytes_to_read;
556 /* rx done, wait was interrupted or error occurred */
558 dev->interrupt_rx_allowed = true;
559 if (rf69_set_mode(dev->spi, standby))
560 pr_err("rf69_set_mode(): radio module failed to go standby\n");
561 wake_up_interruptible(&dev->tx_wait_queue);
570 pi433_tx_thread(void *data)
572 struct pi433_device *device = data;
573 struct spi_device *spi = device->spi;
574 struct pi433_tx_cfg tx_cfg;
576 bool rx_interrupted = false;
577 int position, repetitions;
581 /* wait for fifo to be populated or for request to terminate*/
582 dev_dbg(device->dev, "thread: going to wait for new messages");
583 wait_event_interruptible(device->tx_wait_queue,
584 (!kfifo_is_empty(&device->tx_fifo) ||
585 kthread_should_stop()));
586 if (kthread_should_stop())
590 * get data from fifo in the following order:
595 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
596 if (retval != sizeof(tx_cfg)) {
598 "reading tx_cfg from fifo failed: got %d byte(s), expected %d",
599 retval, (unsigned int)sizeof(tx_cfg));
603 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
604 if (retval != sizeof(size_t)) {
606 "reading msg size from fifo failed: got %d, expected %d",
607 retval, (unsigned int)sizeof(size_t));
611 /* use fixed message length, if requested */
612 if (tx_cfg.fixed_message_length != 0)
613 size = tx_cfg.fixed_message_length;
615 /* increase size, if len byte is requested */
616 if (tx_cfg.enable_length_byte == OPTION_ON)
619 /* increase size, if adr byte is requested */
620 if (tx_cfg.enable_address_byte == OPTION_ON)
624 memset(device->buffer, 0, size);
627 /* add length byte, if requested */
628 if (tx_cfg.enable_length_byte == OPTION_ON)
630 * according to spec, length byte itself must be
631 * excluded from the length calculation
633 device->buffer[position++] = size - 1;
635 /* add adr byte, if requested */
636 if (tx_cfg.enable_address_byte == OPTION_ON)
637 device->buffer[position++] = tx_cfg.address_byte;
639 /* finally get message data from fifo */
640 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
641 sizeof(device->buffer) - position);
643 "read %d message byte(s) from fifo queue.", retval);
646 * if rx is active, we need to interrupt the waiting for
647 * incoming telegrams, to be able to send something.
648 * We are only allowed, if currently no reception takes
649 * place otherwise we need to wait for the incoming telegram
652 wait_event_interruptible(device->tx_wait_queue,
653 !device->rx_active ||
654 device->interrupt_rx_allowed);
657 * prevent race conditions
658 * irq will be reenabled after tx config is set
660 disable_irq(device->irq_num[DIO0]);
661 device->tx_active = true;
663 if (device->rx_active && !rx_interrupted) {
665 * rx is currently waiting for a telegram;
666 * we need to set the radio module to standby
668 retval = rf69_set_mode(device->spi, standby);
671 rx_interrupted = true;
674 /* clear fifo, set fifo threshold, set payload length */
675 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
678 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
681 if (tx_cfg.enable_length_byte == OPTION_ON) {
682 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
686 retval = rf69_set_payload_length(spi, 0);
691 /* configure the rf chip */
692 retval = rf69_set_tx_cfg(device, &tx_cfg);
696 /* enable fifo level interrupt */
697 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
700 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
701 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
703 /* enable packet sent interrupt */
704 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
707 device->irq_state[DIO0] = DIO_PACKET_SENT;
708 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
709 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
711 /* enable transmission */
712 retval = rf69_set_mode(spi, transmit);
716 /* transfer this msg (and repetitions) to chip fifo */
717 device->free_in_fifo = FIFO_SIZE;
719 repetitions = tx_cfg.repetitions;
720 while ((repetitions > 0) && (size > position)) {
721 if ((size - position) > device->free_in_fifo) {
722 /* msg to big for fifo - take a part */
723 int write_size = device->free_in_fifo;
725 device->free_in_fifo = 0;
727 &device->buffer[position],
729 position += write_size;
731 /* msg fits into fifo - take all */
732 device->free_in_fifo -= size;
735 &device->buffer[position],
737 position = 0; /* reset for next repetition */
740 retval = wait_event_interruptible(device->fifo_wait_queue,
741 device->free_in_fifo > 0);
743 dev_dbg(device->dev, "ABORT\n");
748 /* we are done. Wait for packet to get sent */
750 "thread: wait for packet to get sent/fifo to be empty");
751 wait_event_interruptible(device->fifo_wait_queue,
752 device->free_in_fifo == FIFO_SIZE ||
753 kthread_should_stop());
754 if (kthread_should_stop())
755 dev_dbg(device->dev, "ABORT\n");
757 /* STOP_TRANSMISSION */
758 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.");
759 retval = rf69_set_mode(spi, standby);
763 /* everything sent? */
764 if (kfifo_is_empty(&device->tx_fifo)) {
766 if (rx_interrupted) {
767 rx_interrupted = false;
768 pi433_start_rx(device);
770 device->tx_active = false;
771 wake_up_interruptible(&device->rx_wait_queue);
776 /*-------------------------------------------------------------------------*/
779 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
781 struct pi433_instance *instance;
782 struct pi433_device *device;
786 /* check, whether internal buffer is big enough for requested size */
787 if (size > MAX_MSG_SIZE)
790 instance = filp->private_data;
791 device = instance->device;
793 /* just one read request at a time */
794 mutex_lock(&device->rx_lock);
795 if (device->rx_active) {
796 mutex_unlock(&device->rx_lock);
800 device->rx_active = true;
801 mutex_unlock(&device->rx_lock);
803 /* start receiving */
804 /* will block until something was received*/
805 device->rx_buffer_size = size;
806 bytes_received = pi433_receive(device);
809 mutex_lock(&device->rx_lock);
810 device->rx_active = false;
811 mutex_unlock(&device->rx_lock);
813 /* if read was successful copy to user space*/
814 if (bytes_received > 0) {
815 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
820 return bytes_received;
824 pi433_write(struct file *filp, const char __user *buf,
825 size_t count, loff_t *f_pos)
827 struct pi433_instance *instance;
828 struct pi433_device *device;
830 unsigned int required, available, copied;
832 instance = filp->private_data;
833 device = instance->device;
836 * check, whether internal buffer (tx thread) is big enough
839 if (count > MAX_MSG_SIZE)
843 * write the following sequence into fifo:
848 mutex_lock(&device->tx_fifo_lock);
850 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
851 available = kfifo_avail(&device->tx_fifo);
852 if (required > available) {
853 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available",
854 required, available);
855 mutex_unlock(&device->tx_fifo_lock);
859 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
860 sizeof(instance->tx_cfg));
861 if (retval != sizeof(instance->tx_cfg))
864 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
865 if (retval != sizeof(size_t))
868 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
869 if (retval || copied != count)
872 mutex_unlock(&device->tx_fifo_lock);
875 wake_up_interruptible(&device->tx_wait_queue);
876 dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied);
881 dev_warn(device->dev,
882 "write to fifo failed, non recoverable: 0x%x", retval);
883 mutex_unlock(&device->tx_fifo_lock);
888 pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
891 struct pi433_instance *instance;
892 struct pi433_device *device;
893 struct pi433_tx_cfg tx_cfg;
894 void __user *argp = (void __user *)arg;
896 /* Check type and command number */
897 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
900 instance = filp->private_data;
901 device = instance->device;
907 case PI433_IOC_RD_TX_CFG:
908 if (copy_to_user(argp, &instance->tx_cfg,
909 sizeof(struct pi433_tx_cfg)))
912 case PI433_IOC_WR_TX_CFG:
913 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
915 mutex_lock(&device->tx_fifo_lock);
916 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
917 mutex_unlock(&device->tx_fifo_lock);
919 case PI433_IOC_RD_RX_CFG:
920 if (copy_to_user(argp, &device->rx_cfg,
921 sizeof(struct pi433_rx_cfg)))
924 case PI433_IOC_WR_RX_CFG:
925 mutex_lock(&device->rx_lock);
927 /* during pendig read request, change of config not allowed */
928 if (device->rx_active) {
929 mutex_unlock(&device->rx_lock);
933 if (copy_from_user(&device->rx_cfg, argp,
934 sizeof(struct pi433_rx_cfg))) {
935 mutex_unlock(&device->rx_lock);
939 mutex_unlock(&device->rx_lock);
950 pi433_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
952 return pi433_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
955 #define pi433_compat_ioctl NULL
956 #endif /* CONFIG_COMPAT */
958 /*-------------------------------------------------------------------------*/
960 static int pi433_open(struct inode *inode, struct file *filp)
962 struct pi433_device *device;
963 struct pi433_instance *instance;
965 mutex_lock(&minor_lock);
966 device = idr_find(&pi433_idr, iminor(inode));
967 mutex_unlock(&minor_lock);
969 pr_debug("device: minor %d unknown.\n", iminor(inode));
973 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
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;
993 instance = filp->private_data;
995 filp->private_data = NULL;
1000 /*-------------------------------------------------------------------------*/
1002 static int setup_gpio(struct pi433_device *device)
1007 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
1012 for (i = 0; i < NUM_DIO; i++) {
1013 /* "construct" name and get the gpio descriptor */
1014 snprintf(name, sizeof(name), "DIO%d", i);
1015 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
1018 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1019 dev_dbg(&device->spi->dev,
1020 "Could not find entry for %s. Ignoring.", name);
1024 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1025 dev_dbg(&device->spi->dev, "%s is busy.", name);
1027 if (IS_ERR(device->gpiod[i])) {
1028 retval = PTR_ERR(device->gpiod[i]);
1029 /* release already allocated gpios */
1030 for (i--; i >= 0; i--) {
1031 free_irq(device->irq_num[i], device);
1032 gpiod_put(device->gpiod[i]);
1037 /* configure the pin */
1038 gpiod_unexport(device->gpiod[i]);
1039 retval = gpiod_direction_input(device->gpiod[i]);
1044 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1045 if (device->irq_num[i] < 0) {
1046 device->gpiod[i] = ERR_PTR(-EINVAL);
1047 return device->irq_num[i];
1049 retval = request_irq(device->irq_num[i],
1058 dev_dbg(&device->spi->dev, "%s successfully configured", name);
1064 static void free_gpio(struct pi433_device *device)
1068 for (i = 0; i < NUM_DIO; i++) {
1069 /* check if gpiod is valid */
1070 if (IS_ERR(device->gpiod[i]))
1073 free_irq(device->irq_num[i], device);
1074 gpiod_put(device->gpiod[i]);
1078 static int pi433_get_minor(struct pi433_device *device)
1080 int retval = -ENOMEM;
1082 mutex_lock(&minor_lock);
1083 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1085 device->minor = retval;
1087 } else if (retval == -ENOSPC) {
1088 dev_err(&device->spi->dev, "too many pi433 devices\n");
1091 mutex_unlock(&minor_lock);
1095 static void pi433_free_minor(struct pi433_device *dev)
1097 mutex_lock(&minor_lock);
1098 idr_remove(&pi433_idr, dev->minor);
1099 mutex_unlock(&minor_lock);
1102 /*-------------------------------------------------------------------------*/
1104 static const struct file_operations pi433_fops = {
1105 .owner = THIS_MODULE,
1107 * REVISIT switch to aio primitives, so that userspace
1108 * gets more complete API coverage. It'll simplify things
1109 * too, except for the locking.
1111 .write = pi433_write,
1113 .unlocked_ioctl = pi433_ioctl,
1114 .compat_ioctl = pi433_compat_ioctl,
1116 .release = pi433_release,
1117 .llseek = no_llseek,
1120 /*-------------------------------------------------------------------------*/
1122 static int pi433_probe(struct spi_device *spi)
1124 struct pi433_device *device;
1127 /* setup spi parameters */
1129 spi->bits_per_word = 8;
1131 * spi->max_speed_hz = 10000000;
1132 * 1MHz already set by device tree overlay
1135 retval = spi_setup(spi);
1137 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1142 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed",
1143 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1145 /* Ping the chip by reading the version register */
1146 retval = spi_w8r8(spi, 0x10);
1152 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval);
1155 dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval);
1159 /* Allocate driver data */
1160 device = kzalloc(sizeof(*device), GFP_KERNEL);
1164 /* Initialize the driver data */
1166 device->rx_active = false;
1167 device->tx_active = false;
1168 device->interrupt_rx_allowed = false;
1170 /* init rx buffer */
1171 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1172 if (!device->rx_buffer) {
1177 /* init wait queues */
1178 init_waitqueue_head(&device->tx_wait_queue);
1179 init_waitqueue_head(&device->rx_wait_queue);
1180 init_waitqueue_head(&device->fifo_wait_queue);
1183 INIT_KFIFO(device->tx_fifo);
1185 /* init mutexes and locks */
1186 mutex_init(&device->tx_fifo_lock);
1187 mutex_init(&device->rx_lock);
1189 /* setup GPIO (including irq_handler) for the different DIOs */
1190 retval = setup_gpio(device);
1192 dev_dbg(&spi->dev, "setup of GPIOs failed");
1196 /* setup the radio module */
1197 retval = rf69_set_mode(spi, standby);
1200 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1203 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1206 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1209 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1212 retval = rf69_set_output_power_level(spi, 13);
1215 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1219 /* determ minor number */
1220 retval = pi433_get_minor(device);
1222 dev_dbg(&spi->dev, "get of minor number failed");
1227 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1228 device->dev = device_create(pi433_class,
1234 if (IS_ERR(device->dev)) {
1235 pr_err("pi433: device register failed\n");
1236 retval = PTR_ERR(device->dev);
1237 goto device_create_failed;
1239 dev_dbg(device->dev,
1240 "created device for major %d, minor %d\n",
1245 /* start tx thread */
1246 device->tx_task_struct = kthread_run(pi433_tx_thread,
1250 if (IS_ERR(device->tx_task_struct)) {
1251 dev_dbg(device->dev, "start of send thread failed");
1252 retval = PTR_ERR(device->tx_task_struct);
1253 goto send_thread_failed;
1257 device->cdev = cdev_alloc();
1258 if (!device->cdev) {
1259 dev_dbg(device->dev, "allocation of cdev failed");
1262 device->cdev->owner = THIS_MODULE;
1263 cdev_init(device->cdev, &pi433_fops);
1264 retval = cdev_add(device->cdev, device->devt, 1);
1266 dev_dbg(device->dev, "register of cdev failed");
1271 spi_set_drvdata(spi, device);
1276 kthread_stop(device->tx_task_struct);
1278 device_destroy(pi433_class, device->devt);
1279 device_create_failed:
1280 pi433_free_minor(device);
1284 kfree(device->rx_buffer);
1291 static int pi433_remove(struct spi_device *spi)
1293 struct pi433_device *device = spi_get_drvdata(spi);
1298 /* make sure ops on existing fds can abort cleanly */
1301 kthread_stop(device->tx_task_struct);
1303 device_destroy(pi433_class, device->devt);
1305 cdev_del(device->cdev);
1307 pi433_free_minor(device);
1309 kfree(device->rx_buffer);
1315 static const struct of_device_id pi433_dt_ids[] = {
1316 { .compatible = "Smarthome-Wolf,pi433" },
1320 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1322 static struct spi_driver pi433_spi_driver = {
1325 .owner = THIS_MODULE,
1326 .of_match_table = of_match_ptr(pi433_dt_ids),
1328 .probe = pi433_probe,
1329 .remove = pi433_remove,
1332 * NOTE: suspend/resume methods are not necessary here.
1333 * We don't do anything except pass the requests to/from
1334 * the underlying controller. The refrigerator handles
1335 * most issues; the controller driver handles the rest.
1339 /*-------------------------------------------------------------------------*/
1341 static int __init pi433_init(void)
1346 * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1347 * work stable - risk of buffer overflow
1349 if (MAX_MSG_SIZE < FIFO_SIZE)
1353 * Claim device numbers. Then register a class
1354 * that will key udev/mdev to add/remove /dev nodes. Last, register
1355 * Last, register the driver which manages those device numbers.
1357 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1361 pi433_class = class_create(THIS_MODULE, "pi433");
1362 if (IS_ERR(pi433_class)) {
1363 unregister_chrdev(MAJOR(pi433_dev),
1364 pi433_spi_driver.driver.name);
1365 return PTR_ERR(pi433_class);
1368 status = spi_register_driver(&pi433_spi_driver);
1370 class_destroy(pi433_class);
1371 unregister_chrdev(MAJOR(pi433_dev),
1372 pi433_spi_driver.driver.name);
1378 module_init(pi433_init);
1380 static void __exit pi433_exit(void)
1382 spi_unregister_driver(&pi433_spi_driver);
1383 class_destroy(pi433_class);
1384 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1386 module_exit(pi433_exit);
1388 MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1389 MODULE_DESCRIPTION("Driver for Pi433");
1390 MODULE_LICENSE("GPL");
1391 MODULE_ALIAS("spi:pi433");