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/of_device.h>
35 #include <linux/interrupt.h>
36 #include <linux/irq.h>
37 #include <linux/gpio/consumer.h>
38 #include <linux/kthread.h>
39 #include <linux/wait.h>
40 #include <linux/spi/spi.h>
42 #include <linux/compat.h>
48 #define N_PI433_MINORS BIT(MINORBITS) /*32*/ /* ... up to 256 */
49 #define MAX_MSG_SIZE 900 /* min: FIFO_SIZE! */
50 #define MSG_FIFO_SIZE 65536 /* 65536 = 2^16 */
53 static dev_t pi433_dev;
54 static DEFINE_IDR(pi433_idr);
55 static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
57 static struct class *pi433_class; /* mainly for udev to create /dev/pi433 */
60 * tx config is instance specific
61 * so with each open a new tx config struct is needed
64 * rx config is device specific
65 * so we have just one rx config, ebedded in device struct
68 /* device handling related values */
73 struct spi_device *spi;
75 /* irq related values */
76 struct gpio_desc *gpiod[NUM_DIO];
78 u8 irq_state[NUM_DIO];
80 /* tx related values */
81 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
82 struct mutex tx_fifo_lock; /* serialize userspace writers */
83 struct task_struct *tx_task_struct;
84 wait_queue_head_t tx_wait_queue;
86 char buffer[MAX_MSG_SIZE];
88 /* rx related values */
89 struct pi433_rx_cfg rx_cfg;
91 unsigned int rx_buffer_size;
94 unsigned int rx_position;
95 struct mutex rx_lock; /* protects rx_* variable accesses */
96 wait_queue_head_t rx_wait_queue;
99 struct task_struct *fifo_task_struct;
100 wait_queue_head_t fifo_wait_queue;
105 bool interrupt_rx_allowed;
108 struct pi433_instance {
109 struct pi433_device *device;
110 struct pi433_tx_cfg tx_cfg;
113 /*-------------------------------------------------------------------------*/
115 /* GPIO interrupt handlers */
116 static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
118 struct pi433_device *device = dev_id;
120 if (device->irq_state[DIO0] == DIO_PACKET_SENT) {
121 device->free_in_fifo = FIFO_SIZE;
122 dev_dbg(device->dev, "DIO0 irq: Packet sent\n");
123 wake_up_interruptible(&device->fifo_wait_queue);
124 } else if (device->irq_state[DIO0] == DIO_RSSI_DIO0) {
125 dev_dbg(device->dev, "DIO0 irq: RSSI level over threshold\n");
126 wake_up_interruptible(&device->rx_wait_queue);
127 } else if (device->irq_state[DIO0] == DIO_PAYLOAD_READY) {
128 dev_dbg(device->dev, "DIO0 irq: Payload ready\n");
129 device->free_in_fifo = 0;
130 wake_up_interruptible(&device->fifo_wait_queue);
136 static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
138 struct pi433_device *device = dev_id;
140 if (device->irq_state[DIO1] == DIO_FIFO_NOT_EMPTY_DIO1) {
141 device->free_in_fifo = FIFO_SIZE;
142 } else if (device->irq_state[DIO1] == DIO_FIFO_LEVEL) {
143 if (device->rx_active)
144 device->free_in_fifo = FIFO_THRESHOLD - 1;
146 device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
149 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
150 wake_up_interruptible(&device->fifo_wait_queue);
155 /*-------------------------------------------------------------------------*/
158 rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
163 /* receiver config */
164 ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
167 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
170 ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
173 ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
176 ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
179 ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
182 ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
183 rx_cfg->bw_exponent);
186 ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
187 rx_cfg->bw_exponent);
190 ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
194 dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
198 if (rx_cfg->enable_sync == OPTION_ON) {
199 ret = rf69_enable_sync(dev->spi);
203 ret = rf69_set_fifo_fill_condition(dev->spi,
204 after_sync_interrupt);
208 ret = rf69_disable_sync(dev->spi);
212 ret = rf69_set_fifo_fill_condition(dev->spi, always);
216 if (rx_cfg->enable_length_byte == OPTION_ON) {
217 ret = rf69_set_packet_format(dev->spi, packet_length_var);
221 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
225 ret = rf69_set_address_filtering(dev->spi,
226 rx_cfg->enable_address_filtering);
230 if (rx_cfg->enable_crc == OPTION_ON) {
231 ret = rf69_enable_crc(dev->spi);
235 ret = rf69_disable_crc(dev->spi);
241 ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
244 if (rx_cfg->enable_length_byte == OPTION_ON) {
245 ret = rf69_set_payload_length(dev->spi, 0xff);
248 } else if (rx_cfg->fixed_message_length != 0) {
249 payload_length = rx_cfg->fixed_message_length;
250 if (rx_cfg->enable_length_byte == OPTION_ON)
252 if (rx_cfg->enable_address_filtering != filtering_off)
254 ret = rf69_set_payload_length(dev->spi, payload_length);
258 ret = rf69_set_payload_length(dev->spi, 0);
264 if (rx_cfg->enable_sync == OPTION_ON) {
265 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
269 if (rx_cfg->enable_address_filtering != filtering_off) {
270 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
273 ret = rf69_set_broadcast_address(dev->spi,
274 rx_cfg->broadcast_address);
283 rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
287 ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
290 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
293 ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
296 ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
299 ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
302 ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
305 ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
309 /* packet format enable */
310 if (tx_cfg->enable_preamble == OPTION_ON) {
311 ret = rf69_set_preamble_length(dev->spi,
312 tx_cfg->preamble_length);
316 ret = rf69_set_preamble_length(dev->spi, 0);
321 if (tx_cfg->enable_sync == OPTION_ON) {
322 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
325 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
328 ret = rf69_enable_sync(dev->spi);
332 ret = rf69_disable_sync(dev->spi);
337 if (tx_cfg->enable_length_byte == OPTION_ON) {
338 ret = rf69_set_packet_format(dev->spi, packet_length_var);
342 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
347 if (tx_cfg->enable_crc == OPTION_ON) {
348 ret = rf69_enable_crc(dev->spi);
352 ret = rf69_disable_crc(dev->spi);
360 /*-------------------------------------------------------------------------*/
363 pi433_start_rx(struct pi433_device *dev)
367 /* return without action, if no pending read request */
371 /* setup for receiving */
372 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
377 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
380 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
381 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
383 /* setup fifo level interrupt */
384 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
387 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
390 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
391 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
393 /* set module to receiving mode */
394 retval = rf69_set_mode(dev->spi, receive);
401 /*-------------------------------------------------------------------------*/
404 pi433_receive(void *data)
406 struct pi433_device *dev = data;
407 struct spi_device *spi = dev->spi;
408 int bytes_to_read, bytes_total;
411 dev->interrupt_rx_allowed = false;
413 /* wait for any tx to finish */
414 dev_dbg(dev->dev, "rx: going to wait for any tx to finish");
415 retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
417 /* wait was interrupted */
418 dev->interrupt_rx_allowed = true;
419 wake_up_interruptible(&dev->tx_wait_queue);
423 /* prepare status vars */
424 dev->free_in_fifo = FIFO_SIZE;
425 dev->rx_position = 0;
426 dev->rx_bytes_dropped = 0;
428 /* setup radio module to listen for something "in the air" */
429 retval = pi433_start_rx(dev);
433 /* now check RSSI, if low wait for getting high (RSSI interrupt) */
434 while (!rf69_get_flag(dev->spi, rssi_exceeded_threshold)) {
435 /* allow tx to interrupt us while waiting for high RSSI */
436 dev->interrupt_rx_allowed = true;
437 wake_up_interruptible(&dev->tx_wait_queue);
439 /* wait for RSSI level to become high */
440 dev_dbg(dev->dev, "rx: going to wait for high RSSI level");
441 retval = wait_event_interruptible(dev->rx_wait_queue,
442 rf69_get_flag(dev->spi,
443 rssi_exceeded_threshold));
444 if (retval) /* wait was interrupted */
446 dev->interrupt_rx_allowed = false;
448 /* cross check for ongoing tx */
453 /* configure payload ready irq */
454 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
457 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
458 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
460 /* fixed or unlimited length? */
461 if (dev->rx_cfg.fixed_message_length != 0) {
462 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
466 bytes_total = dev->rx_cfg.fixed_message_length;
467 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length",
470 bytes_total = dev->rx_buffer_size;
471 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read",
475 /* length byte enabled? */
476 if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
477 retval = wait_event_interruptible(dev->fifo_wait_queue,
478 dev->free_in_fifo < FIFO_SIZE);
479 if (retval) /* wait was interrupted */
482 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
483 if (bytes_total > dev->rx_buffer_size) {
488 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte",
492 /* address byte enabled? */
493 if (dev->rx_cfg.enable_address_filtering != filtering_off) {
498 retval = wait_event_interruptible(dev->fifo_wait_queue,
499 dev->free_in_fifo < FIFO_SIZE);
500 if (retval) /* wait was interrupted */
503 rf69_read_fifo(spi, &dummy, 1);
505 dev_dbg(dev->dev, "rx: address byte stripped off");
509 while (dev->rx_position < bytes_total) {
510 if (!rf69_get_flag(dev->spi, payload_ready)) {
511 retval = wait_event_interruptible(dev->fifo_wait_queue,
512 dev->free_in_fifo < FIFO_SIZE);
513 if (retval) /* wait was interrupted */
517 /* need to drop bytes or acquire? */
518 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
519 bytes_to_read = dev->rx_bytes_to_drop -
520 dev->rx_bytes_dropped;
522 bytes_to_read = bytes_total - dev->rx_position;
524 /* access the fifo */
525 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
526 bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
527 retval = rf69_read_fifo(spi,
528 &dev->rx_buffer[dev->rx_position],
530 if (retval) /* read failed */
533 dev->free_in_fifo += bytes_to_read;
535 /* adjust status vars */
536 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
537 dev->rx_bytes_dropped += bytes_to_read;
539 dev->rx_position += bytes_to_read;
542 /* rx done, wait was interrupted or error occurred */
544 dev->interrupt_rx_allowed = true;
545 if (rf69_set_mode(dev->spi, standby))
546 pr_err("rf69_set_mode(): radio module failed to go standby\n");
547 wake_up_interruptible(&dev->tx_wait_queue);
556 pi433_tx_thread(void *data)
558 struct pi433_device *device = data;
559 struct spi_device *spi = device->spi;
560 struct pi433_tx_cfg tx_cfg;
562 bool rx_interrupted = false;
563 int position, repetitions;
567 /* wait for fifo to be populated or for request to terminate*/
568 dev_dbg(device->dev, "thread: going to wait for new messages");
569 wait_event_interruptible(device->tx_wait_queue,
570 (!kfifo_is_empty(&device->tx_fifo) ||
571 kthread_should_stop()));
572 if (kthread_should_stop())
576 * get data from fifo in the following order:
581 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
582 if (retval != sizeof(tx_cfg)) {
584 "reading tx_cfg from fifo failed: got %d byte(s), expected %d",
585 retval, (unsigned int)sizeof(tx_cfg));
589 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
590 if (retval != sizeof(size_t)) {
592 "reading msg size from fifo failed: got %d, expected %d",
593 retval, (unsigned int)sizeof(size_t));
597 /* use fixed message length, if requested */
598 if (tx_cfg.fixed_message_length != 0)
599 size = tx_cfg.fixed_message_length;
601 /* increase size, if len byte is requested */
602 if (tx_cfg.enable_length_byte == OPTION_ON)
605 /* increase size, if adr byte is requested */
606 if (tx_cfg.enable_address_byte == OPTION_ON)
610 memset(device->buffer, 0, size);
613 /* add length byte, if requested */
614 if (tx_cfg.enable_length_byte == OPTION_ON)
616 * according to spec, length byte itself must be
617 * excluded from the length calculation
619 device->buffer[position++] = size - 1;
621 /* add adr byte, if requested */
622 if (tx_cfg.enable_address_byte == OPTION_ON)
623 device->buffer[position++] = tx_cfg.address_byte;
625 /* finally get message data from fifo */
626 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
627 sizeof(device->buffer) - position);
629 "read %d message byte(s) from fifo queue.", retval);
632 * if rx is active, we need to interrupt the waiting for
633 * incoming telegrams, to be able to send something.
634 * We are only allowed, if currently no reception takes
635 * place otherwise we need to wait for the incoming telegram
638 wait_event_interruptible(device->tx_wait_queue,
639 !device->rx_active ||
640 device->interrupt_rx_allowed);
643 * prevent race conditions
644 * irq will be reenabled after tx config is set
646 disable_irq(device->irq_num[DIO0]);
647 device->tx_active = true;
649 /* clear fifo, set fifo threshold, set payload length */
650 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
654 if (device->rx_active && !rx_interrupted) {
656 * rx is currently waiting for a telegram;
657 * we need to set the radio module to standby
659 rx_interrupted = true;
662 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
665 if (tx_cfg.enable_length_byte == OPTION_ON) {
666 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
670 retval = rf69_set_payload_length(spi, 0);
675 /* configure the rf chip */
676 retval = rf69_set_tx_cfg(device, &tx_cfg);
680 /* enable fifo level interrupt */
681 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
684 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
685 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
687 /* enable packet sent interrupt */
688 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
691 device->irq_state[DIO0] = DIO_PACKET_SENT;
692 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
693 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
695 /* enable transmission */
696 retval = rf69_set_mode(spi, transmit);
700 /* transfer this msg (and repetitions) to chip fifo */
701 device->free_in_fifo = FIFO_SIZE;
703 repetitions = tx_cfg.repetitions;
704 while ((repetitions > 0) && (size > position)) {
705 if ((size - position) > device->free_in_fifo) {
706 /* msg to big for fifo - take a part */
707 int write_size = device->free_in_fifo;
709 device->free_in_fifo = 0;
711 &device->buffer[position],
713 position += write_size;
715 /* msg fits into fifo - take all */
716 device->free_in_fifo -= size;
719 &device->buffer[position],
721 position = 0; /* reset for next repetition */
724 retval = wait_event_interruptible(device->fifo_wait_queue,
725 device->free_in_fifo > 0);
727 dev_dbg(device->dev, "ABORT\n");
732 /* we are done. Wait for packet to get sent */
734 "thread: wait for packet to get sent/fifo to be empty");
735 wait_event_interruptible(device->fifo_wait_queue,
736 device->free_in_fifo == FIFO_SIZE ||
737 kthread_should_stop());
738 if (kthread_should_stop())
741 /* STOP_TRANSMISSION */
742 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.");
743 retval = rf69_set_mode(spi, standby);
747 /* everything sent? */
748 if (kfifo_is_empty(&device->tx_fifo)) {
750 if (rx_interrupted) {
751 rx_interrupted = false;
752 pi433_start_rx(device);
754 device->tx_active = false;
755 wake_up_interruptible(&device->rx_wait_queue);
760 /*-------------------------------------------------------------------------*/
763 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
765 struct pi433_instance *instance;
766 struct pi433_device *device;
770 /* check, whether internal buffer is big enough for requested size */
771 if (size > MAX_MSG_SIZE)
774 instance = filp->private_data;
775 device = instance->device;
777 /* just one read request at a time */
778 mutex_lock(&device->rx_lock);
779 if (device->rx_active) {
780 mutex_unlock(&device->rx_lock);
784 device->rx_active = true;
785 mutex_unlock(&device->rx_lock);
787 /* start receiving */
788 /* will block until something was received*/
789 device->rx_buffer_size = size;
790 bytes_received = pi433_receive(device);
793 mutex_lock(&device->rx_lock);
794 device->rx_active = false;
795 mutex_unlock(&device->rx_lock);
797 /* if read was successful copy to user space*/
798 if (bytes_received > 0) {
799 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
804 return bytes_received;
808 pi433_write(struct file *filp, const char __user *buf,
809 size_t count, loff_t *f_pos)
811 struct pi433_instance *instance;
812 struct pi433_device *device;
814 unsigned int required, available, copied;
816 instance = filp->private_data;
817 device = instance->device;
820 * check, whether internal buffer (tx thread) is big enough
823 if (count > MAX_MSG_SIZE)
827 * write the following sequence into fifo:
832 mutex_lock(&device->tx_fifo_lock);
834 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
835 available = kfifo_avail(&device->tx_fifo);
836 if (required > available) {
837 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available",
838 required, available);
839 mutex_unlock(&device->tx_fifo_lock);
843 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
844 sizeof(instance->tx_cfg));
845 if (retval != sizeof(instance->tx_cfg))
848 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
849 if (retval != sizeof(size_t))
852 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
853 if (retval || copied != count)
856 mutex_unlock(&device->tx_fifo_lock);
859 wake_up_interruptible(&device->tx_wait_queue);
860 dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied);
865 dev_warn(device->dev,
866 "write to fifo failed, non recoverable: 0x%x", retval);
867 mutex_unlock(&device->tx_fifo_lock);
872 pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
874 struct pi433_instance *instance;
875 struct pi433_device *device;
876 struct pi433_tx_cfg tx_cfg;
877 void __user *argp = (void __user *)arg;
879 /* Check type and command number */
880 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
883 instance = filp->private_data;
884 device = instance->device;
890 case PI433_IOC_RD_TX_CFG:
891 if (copy_to_user(argp, &instance->tx_cfg,
892 sizeof(struct pi433_tx_cfg)))
895 case PI433_IOC_WR_TX_CFG:
896 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
898 mutex_lock(&device->tx_fifo_lock);
899 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
900 mutex_unlock(&device->tx_fifo_lock);
902 case PI433_IOC_RD_RX_CFG:
903 if (copy_to_user(argp, &device->rx_cfg,
904 sizeof(struct pi433_rx_cfg)))
907 case PI433_IOC_WR_RX_CFG:
908 mutex_lock(&device->rx_lock);
910 /* during pendig read request, change of config not allowed */
911 if (device->rx_active) {
912 mutex_unlock(&device->rx_lock);
916 if (copy_from_user(&device->rx_cfg, argp,
917 sizeof(struct pi433_rx_cfg))) {
918 mutex_unlock(&device->rx_lock);
922 mutex_unlock(&device->rx_lock);
931 /*-------------------------------------------------------------------------*/
933 static int pi433_open(struct inode *inode, struct file *filp)
935 struct pi433_device *device;
936 struct pi433_instance *instance;
938 mutex_lock(&minor_lock);
939 device = idr_find(&pi433_idr, iminor(inode));
940 mutex_unlock(&minor_lock);
942 pr_debug("device: minor %d unknown.\n", iminor(inode));
946 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
950 /* setup instance data*/
951 instance->device = device;
952 instance->tx_cfg.bit_rate = 4711;
953 // TODO: fill instance->tx_cfg;
955 /* instance data as context */
956 filp->private_data = instance;
957 stream_open(inode, filp);
962 static int pi433_release(struct inode *inode, struct file *filp)
964 struct pi433_instance *instance;
966 instance = filp->private_data;
968 filp->private_data = NULL;
973 /*-------------------------------------------------------------------------*/
975 static int setup_gpio(struct pi433_device *device)
980 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
985 for (i = 0; i < NUM_DIO; i++) {
986 /* "construct" name and get the gpio descriptor */
987 snprintf(name, sizeof(name), "DIO%d", i);
988 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
991 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
992 dev_dbg(&device->spi->dev,
993 "Could not find entry for %s. Ignoring.", name);
997 if (device->gpiod[i] == ERR_PTR(-EBUSY))
998 dev_dbg(&device->spi->dev, "%s is busy.", name);
1000 if (IS_ERR(device->gpiod[i])) {
1001 retval = PTR_ERR(device->gpiod[i]);
1002 /* release already allocated gpios */
1003 for (i--; i >= 0; i--) {
1004 free_irq(device->irq_num[i], device);
1005 gpiod_put(device->gpiod[i]);
1010 /* configure the pin */
1011 gpiod_unexport(device->gpiod[i]);
1012 retval = gpiod_direction_input(device->gpiod[i]);
1017 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1018 if (device->irq_num[i] < 0) {
1019 device->gpiod[i] = ERR_PTR(-EINVAL);
1020 return device->irq_num[i];
1022 retval = request_irq(device->irq_num[i],
1031 dev_dbg(&device->spi->dev, "%s successfully configured", name);
1037 static void free_gpio(struct pi433_device *device)
1041 for (i = 0; i < NUM_DIO; i++) {
1042 /* check if gpiod is valid */
1043 if (IS_ERR(device->gpiod[i]))
1046 free_irq(device->irq_num[i], device);
1047 gpiod_put(device->gpiod[i]);
1051 static int pi433_get_minor(struct pi433_device *device)
1053 int retval = -ENOMEM;
1055 mutex_lock(&minor_lock);
1056 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1058 device->minor = retval;
1060 } else if (retval == -ENOSPC) {
1061 dev_err(&device->spi->dev, "too many pi433 devices\n");
1064 mutex_unlock(&minor_lock);
1068 static void pi433_free_minor(struct pi433_device *dev)
1070 mutex_lock(&minor_lock);
1071 idr_remove(&pi433_idr, dev->minor);
1072 mutex_unlock(&minor_lock);
1075 /*-------------------------------------------------------------------------*/
1077 static const struct file_operations pi433_fops = {
1078 .owner = THIS_MODULE,
1080 * REVISIT switch to aio primitives, so that userspace
1081 * gets more complete API coverage. It'll simplify things
1082 * too, except for the locking.
1084 .write = pi433_write,
1086 .unlocked_ioctl = pi433_ioctl,
1087 .compat_ioctl = compat_ptr_ioctl,
1089 .release = pi433_release,
1090 .llseek = no_llseek,
1093 /*-------------------------------------------------------------------------*/
1095 static int pi433_probe(struct spi_device *spi)
1097 struct pi433_device *device;
1100 /* setup spi parameters */
1102 spi->bits_per_word = 8;
1104 * spi->max_speed_hz = 10000000;
1105 * 1MHz already set by device tree overlay
1108 retval = spi_setup(spi);
1110 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1115 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed",
1116 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1118 /* Ping the chip by reading the version register */
1119 retval = spi_w8r8(spi, 0x10);
1125 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval);
1128 dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval);
1132 /* Allocate driver data */
1133 device = kzalloc(sizeof(*device), GFP_KERNEL);
1137 /* Initialize the driver data */
1139 device->rx_active = false;
1140 device->tx_active = false;
1141 device->interrupt_rx_allowed = false;
1143 /* init rx buffer */
1144 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1145 if (!device->rx_buffer) {
1150 /* init wait queues */
1151 init_waitqueue_head(&device->tx_wait_queue);
1152 init_waitqueue_head(&device->rx_wait_queue);
1153 init_waitqueue_head(&device->fifo_wait_queue);
1156 INIT_KFIFO(device->tx_fifo);
1158 /* init mutexes and locks */
1159 mutex_init(&device->tx_fifo_lock);
1160 mutex_init(&device->rx_lock);
1162 /* setup GPIO (including irq_handler) for the different DIOs */
1163 retval = setup_gpio(device);
1165 dev_dbg(&spi->dev, "setup of GPIOs failed");
1169 /* setup the radio module */
1170 retval = rf69_set_mode(spi, standby);
1173 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1176 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1179 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1182 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1185 retval = rf69_set_output_power_level(spi, 13);
1188 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1192 /* determ minor number */
1193 retval = pi433_get_minor(device);
1195 dev_dbg(&spi->dev, "get of minor number failed");
1200 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1201 device->dev = device_create(pi433_class,
1207 if (IS_ERR(device->dev)) {
1208 pr_err("pi433: device register failed\n");
1209 retval = PTR_ERR(device->dev);
1210 goto device_create_failed;
1212 dev_dbg(device->dev,
1213 "created device for major %d, minor %d\n",
1218 /* start tx thread */
1219 device->tx_task_struct = kthread_run(pi433_tx_thread,
1223 if (IS_ERR(device->tx_task_struct)) {
1224 dev_dbg(device->dev, "start of send thread failed");
1225 retval = PTR_ERR(device->tx_task_struct);
1226 goto send_thread_failed;
1230 device->cdev = cdev_alloc();
1231 if (!device->cdev) {
1232 dev_dbg(device->dev, "allocation of cdev failed");
1236 device->cdev->owner = THIS_MODULE;
1237 cdev_init(device->cdev, &pi433_fops);
1238 retval = cdev_add(device->cdev, device->devt, 1);
1240 dev_dbg(device->dev, "register of cdev failed");
1245 spi_set_drvdata(spi, device);
1250 cdev_del(device->cdev);
1252 kthread_stop(device->tx_task_struct);
1254 device_destroy(pi433_class, device->devt);
1255 device_create_failed:
1256 pi433_free_minor(device);
1260 kfree(device->rx_buffer);
1267 static int pi433_remove(struct spi_device *spi)
1269 struct pi433_device *device = spi_get_drvdata(spi);
1274 /* make sure ops on existing fds can abort cleanly */
1277 kthread_stop(device->tx_task_struct);
1279 device_destroy(pi433_class, device->devt);
1281 cdev_del(device->cdev);
1283 pi433_free_minor(device);
1285 kfree(device->rx_buffer);
1291 static const struct of_device_id pi433_dt_ids[] = {
1292 { .compatible = "Smarthome-Wolf,pi433" },
1296 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1298 static struct spi_driver pi433_spi_driver = {
1301 .owner = THIS_MODULE,
1302 .of_match_table = of_match_ptr(pi433_dt_ids),
1304 .probe = pi433_probe,
1305 .remove = pi433_remove,
1308 * NOTE: suspend/resume methods are not necessary here.
1309 * We don't do anything except pass the requests to/from
1310 * the underlying controller. The refrigerator handles
1311 * most issues; the controller driver handles the rest.
1315 /*-------------------------------------------------------------------------*/
1317 static int __init pi433_init(void)
1322 * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1323 * work stable - risk of buffer overflow
1325 if (MAX_MSG_SIZE < FIFO_SIZE)
1329 * Claim device numbers. Then register a class
1330 * that will key udev/mdev to add/remove /dev nodes. Last, register
1331 * Last, register the driver which manages those device numbers.
1333 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1337 pi433_class = class_create(THIS_MODULE, "pi433");
1338 if (IS_ERR(pi433_class)) {
1339 unregister_chrdev(MAJOR(pi433_dev),
1340 pi433_spi_driver.driver.name);
1341 return PTR_ERR(pi433_class);
1344 status = spi_register_driver(&pi433_spi_driver);
1346 class_destroy(pi433_class);
1347 unregister_chrdev(MAJOR(pi433_dev),
1348 pi433_spi_driver.driver.name);
1354 module_init(pi433_init);
1356 static void __exit pi433_exit(void)
1358 spi_unregister_driver(&pi433_spi_driver);
1359 class_destroy(pi433_class);
1360 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1362 module_exit(pi433_exit);
1365 MODULE_DESCRIPTION("Driver for Pi433");
1366 MODULE_LICENSE("GPL");
1367 MODULE_ALIAS("spi:pi433");