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>
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>
44 #include <linux/debugfs.h>
45 #include <linux/seq_file.h>
50 #define N_PI433_MINORS BIT(MINORBITS) /*32*/ /* ... up to 256 */
51 #define MAX_MSG_SIZE 900 /* min: FIFO_SIZE! */
52 #define MSG_FIFO_SIZE 65536 /* 65536 = 2^16 */
55 static dev_t pi433_dev;
56 static DEFINE_IDR(pi433_idr);
57 static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
59 static struct class *pi433_class; /* mainly for udev to create /dev/pi433 */
62 * tx config is instance specific
63 * so with each open a new tx config struct is needed
66 * rx config is device specific
67 * so we have just one rx config, ebedded in device struct
70 /* device handling related values */
75 struct spi_device *spi;
77 /* irq related values */
78 struct gpio_desc *gpiod[NUM_DIO];
80 u8 irq_state[NUM_DIO];
82 /* tx related values */
83 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
84 struct mutex tx_fifo_lock; /* serialize userspace writers */
85 struct task_struct *tx_task_struct;
86 wait_queue_head_t tx_wait_queue;
88 char buffer[MAX_MSG_SIZE];
90 /* rx related values */
91 struct pi433_rx_cfg rx_cfg;
93 unsigned int rx_buffer_size;
96 unsigned int rx_position;
97 struct mutex rx_lock; /* protects rx_* variable accesses */
98 wait_queue_head_t rx_wait_queue;
100 /* fifo wait queue */
101 struct task_struct *fifo_task_struct;
102 wait_queue_head_t fifo_wait_queue;
107 bool interrupt_rx_allowed;
110 struct pi433_instance {
111 struct pi433_device *device;
112 struct pi433_tx_cfg tx_cfg;
115 bool tx_cfg_initialized;
118 /*-------------------------------------------------------------------------*/
120 /* GPIO interrupt handlers */
121 static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
123 struct pi433_device *device = dev_id;
125 if (device->irq_state[DIO0] == DIO_PACKET_SENT) {
126 device->free_in_fifo = FIFO_SIZE;
127 dev_dbg(device->dev, "DIO0 irq: Packet sent\n");
128 wake_up_interruptible(&device->fifo_wait_queue);
129 } else if (device->irq_state[DIO0] == DIO_RSSI_DIO0) {
130 dev_dbg(device->dev, "DIO0 irq: RSSI level over threshold\n");
131 wake_up_interruptible(&device->rx_wait_queue);
132 } else if (device->irq_state[DIO0] == DIO_PAYLOAD_READY) {
133 dev_dbg(device->dev, "DIO0 irq: Payload ready\n");
134 device->free_in_fifo = 0;
135 wake_up_interruptible(&device->fifo_wait_queue);
141 static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
143 struct pi433_device *device = dev_id;
145 if (device->irq_state[DIO1] == DIO_FIFO_NOT_EMPTY_DIO1) {
146 device->free_in_fifo = FIFO_SIZE;
147 } else if (device->irq_state[DIO1] == DIO_FIFO_LEVEL) {
148 if (device->rx_active)
149 device->free_in_fifo = FIFO_THRESHOLD - 1;
151 device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
154 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
155 wake_up_interruptible(&device->fifo_wait_queue);
160 /*-------------------------------------------------------------------------*/
163 rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
168 /* receiver config */
169 ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
172 ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
175 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
178 ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
181 ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
184 ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
187 ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
188 rx_cfg->bw_exponent);
191 ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
192 rx_cfg->bw_exponent);
195 ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
199 dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
203 if (rx_cfg->enable_sync == OPTION_ON) {
204 ret = rf69_enable_sync(dev->spi);
208 ret = rf69_set_fifo_fill_condition(dev->spi,
209 after_sync_interrupt);
213 ret = rf69_disable_sync(dev->spi);
217 ret = rf69_set_fifo_fill_condition(dev->spi, always);
221 if (rx_cfg->enable_length_byte == OPTION_ON) {
222 ret = rf69_set_packet_format(dev->spi, packet_length_var);
226 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
230 ret = rf69_set_address_filtering(dev->spi,
231 rx_cfg->enable_address_filtering);
235 if (rx_cfg->enable_crc == OPTION_ON) {
236 ret = rf69_enable_crc(dev->spi);
240 ret = rf69_disable_crc(dev->spi);
246 ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
249 if (rx_cfg->enable_length_byte == OPTION_ON) {
250 ret = rf69_set_payload_length(dev->spi, 0xff);
253 } else if (rx_cfg->fixed_message_length != 0) {
254 payload_length = rx_cfg->fixed_message_length;
255 if (rx_cfg->enable_length_byte == OPTION_ON)
257 if (rx_cfg->enable_address_filtering != filtering_off)
259 ret = rf69_set_payload_length(dev->spi, payload_length);
263 ret = rf69_set_payload_length(dev->spi, 0);
269 if (rx_cfg->enable_sync == OPTION_ON) {
270 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
274 if (rx_cfg->enable_address_filtering != filtering_off) {
275 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
278 ret = rf69_set_broadcast_address(dev->spi,
279 rx_cfg->broadcast_address);
288 rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
292 ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
295 ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
298 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
301 ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
304 ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
307 ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
310 ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
314 /* packet format enable */
315 if (tx_cfg->enable_preamble == OPTION_ON) {
316 ret = rf69_set_preamble_length(dev->spi,
317 tx_cfg->preamble_length);
321 ret = rf69_set_preamble_length(dev->spi, 0);
326 if (tx_cfg->enable_sync == OPTION_ON) {
327 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
330 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
333 ret = rf69_enable_sync(dev->spi);
337 ret = rf69_disable_sync(dev->spi);
342 if (tx_cfg->enable_length_byte == OPTION_ON) {
343 ret = rf69_set_packet_format(dev->spi, packet_length_var);
347 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
352 if (tx_cfg->enable_crc == OPTION_ON) {
353 ret = rf69_enable_crc(dev->spi);
357 ret = rf69_disable_crc(dev->spi);
365 /*-------------------------------------------------------------------------*/
367 static int pi433_start_rx(struct pi433_device *dev)
371 /* return without action, if no pending read request */
375 /* setup for receiving */
376 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
381 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
384 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
385 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
387 /* setup fifo level interrupt */
388 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
391 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
394 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
395 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
397 /* set module to receiving mode */
398 retval = rf69_set_mode(dev->spi, receive);
405 /*-------------------------------------------------------------------------*/
407 static int pi433_receive(void *data)
409 struct pi433_device *dev = data;
410 struct spi_device *spi = dev->spi;
411 int bytes_to_read, bytes_total;
414 dev->interrupt_rx_allowed = false;
416 /* wait for any tx to finish */
417 dev_dbg(dev->dev, "rx: going to wait for any tx to finish\n");
418 retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
420 /* wait was interrupted */
421 dev->interrupt_rx_allowed = true;
422 wake_up_interruptible(&dev->tx_wait_queue);
426 /* prepare status vars */
427 dev->free_in_fifo = FIFO_SIZE;
428 dev->rx_position = 0;
429 dev->rx_bytes_dropped = 0;
431 /* setup radio module to listen for something "in the air" */
432 retval = pi433_start_rx(dev);
436 /* now check RSSI, if low wait for getting high (RSSI interrupt) */
437 while (!(rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RSSI)) {
438 /* allow tx to interrupt us while waiting for high RSSI */
439 dev->interrupt_rx_allowed = true;
440 wake_up_interruptible(&dev->tx_wait_queue);
442 /* wait for RSSI level to become high */
443 dev_dbg(dev->dev, "rx: going to wait for high RSSI level\n");
444 retval = wait_event_interruptible(dev->rx_wait_queue,
445 rf69_read_reg(spi, REG_IRQFLAGS1) &
446 MASK_IRQFLAGS1_RSSI);
447 if (retval) /* wait was interrupted */
449 dev->interrupt_rx_allowed = false;
451 /* cross check for ongoing tx */
456 /* configure payload ready irq */
457 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
460 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
461 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
463 /* fixed or unlimited length? */
464 if (dev->rx_cfg.fixed_message_length != 0) {
465 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
469 bytes_total = dev->rx_cfg.fixed_message_length;
470 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length\n",
473 bytes_total = dev->rx_buffer_size;
474 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read\n",
478 /* length byte enabled? */
479 if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
480 retval = wait_event_interruptible(dev->fifo_wait_queue,
481 dev->free_in_fifo < FIFO_SIZE);
482 if (retval) /* wait was interrupted */
485 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
486 if (bytes_total > dev->rx_buffer_size) {
491 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte\n",
495 /* address byte enabled? */
496 if (dev->rx_cfg.enable_address_filtering != filtering_off) {
501 retval = wait_event_interruptible(dev->fifo_wait_queue,
502 dev->free_in_fifo < FIFO_SIZE);
503 if (retval) /* wait was interrupted */
506 rf69_read_fifo(spi, &dummy, 1);
508 dev_dbg(dev->dev, "rx: address byte stripped off\n");
512 while (dev->rx_position < bytes_total) {
513 if (!(rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PAYLOAD_READY)) {
514 retval = wait_event_interruptible(dev->fifo_wait_queue,
515 dev->free_in_fifo < FIFO_SIZE);
516 if (retval) /* wait was interrupted */
520 /* need to drop bytes or acquire? */
521 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
522 bytes_to_read = dev->rx_bytes_to_drop -
523 dev->rx_bytes_dropped;
525 bytes_to_read = bytes_total - dev->rx_position;
527 /* access the fifo */
528 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
529 bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
530 retval = rf69_read_fifo(spi,
531 &dev->rx_buffer[dev->rx_position],
533 if (retval) /* read failed */
536 dev->free_in_fifo += bytes_to_read;
538 /* adjust status vars */
539 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
540 dev->rx_bytes_dropped += bytes_to_read;
542 dev->rx_position += bytes_to_read;
545 /* rx done, wait was interrupted or error occurred */
547 dev->interrupt_rx_allowed = true;
548 if (rf69_set_mode(dev->spi, standby))
549 pr_err("rf69_set_mode(): radio module failed to go standby\n");
550 wake_up_interruptible(&dev->tx_wait_queue);
558 static int pi433_tx_thread(void *data)
560 struct pi433_device *device = data;
561 struct spi_device *spi = device->spi;
562 struct pi433_tx_cfg tx_cfg;
564 bool rx_interrupted = false;
565 int position, repetitions;
569 /* wait for fifo to be populated or for request to terminate*/
570 dev_dbg(device->dev, "thread: going to wait for new messages\n");
571 wait_event_interruptible(device->tx_wait_queue,
572 (!kfifo_is_empty(&device->tx_fifo) ||
573 kthread_should_stop()));
574 if (kthread_should_stop())
578 * get data from fifo in the following order:
583 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
584 if (retval != sizeof(tx_cfg)) {
586 "reading tx_cfg from fifo failed: got %d byte(s), expected %d\n",
587 retval, (unsigned int)sizeof(tx_cfg));
591 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
592 if (retval != sizeof(size_t)) {
594 "reading msg size from fifo failed: got %d, expected %d\n",
595 retval, (unsigned int)sizeof(size_t));
599 /* use fixed message length, if requested */
600 if (tx_cfg.fixed_message_length != 0)
601 size = tx_cfg.fixed_message_length;
603 /* increase size, if len byte is requested */
604 if (tx_cfg.enable_length_byte == OPTION_ON)
607 /* increase size, if adr byte is requested */
608 if (tx_cfg.enable_address_byte == OPTION_ON)
612 memset(device->buffer, 0, size);
615 /* add length byte, if requested */
616 if (tx_cfg.enable_length_byte == OPTION_ON)
618 * according to spec, length byte itself must be
619 * excluded from the length calculation
621 device->buffer[position++] = size - 1;
623 /* add adr byte, if requested */
624 if (tx_cfg.enable_address_byte == OPTION_ON)
625 device->buffer[position++] = tx_cfg.address_byte;
627 /* finally get message data from fifo */
628 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
629 sizeof(device->buffer) - position);
631 "read %d message byte(s) from fifo queue.\n", retval);
634 * if rx is active, we need to interrupt the waiting for
635 * incoming telegrams, to be able to send something.
636 * We are only allowed, if currently no reception takes
637 * place otherwise we need to wait for the incoming telegram
640 wait_event_interruptible(device->tx_wait_queue,
641 !device->rx_active ||
642 device->interrupt_rx_allowed);
645 * prevent race conditions
646 * irq will be reenabled after tx config is set
648 disable_irq(device->irq_num[DIO0]);
649 device->tx_active = true;
651 /* clear fifo, set fifo threshold, set payload length */
652 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
656 if (device->rx_active && !rx_interrupted) {
658 * rx is currently waiting for a telegram;
659 * we need to set the radio module to standby
661 rx_interrupted = true;
664 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
667 if (tx_cfg.enable_length_byte == OPTION_ON) {
668 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
672 retval = rf69_set_payload_length(spi, 0);
677 /* configure the rf chip */
678 retval = rf69_set_tx_cfg(device, &tx_cfg);
682 /* enable fifo level interrupt */
683 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
686 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
687 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
689 /* enable packet sent interrupt */
690 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
693 device->irq_state[DIO0] = DIO_PACKET_SENT;
694 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
695 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
697 /* enable transmission */
698 retval = rf69_set_mode(spi, transmit);
702 /* transfer this msg (and repetitions) to chip fifo */
703 device->free_in_fifo = FIFO_SIZE;
705 repetitions = tx_cfg.repetitions;
706 while ((repetitions > 0) && (size > position)) {
707 if ((size - position) > device->free_in_fifo) {
708 /* msg to big for fifo - take a part */
709 int write_size = device->free_in_fifo;
711 device->free_in_fifo = 0;
713 &device->buffer[position],
715 position += write_size;
717 /* msg fits into fifo - take all */
718 device->free_in_fifo -= size;
721 &device->buffer[position],
723 position = 0; /* reset for next repetition */
726 retval = wait_event_interruptible(device->fifo_wait_queue,
727 device->free_in_fifo > 0);
729 dev_dbg(device->dev, "ABORT\n");
734 /* we are done. Wait for packet to get sent */
736 "thread: wait for packet to get sent/fifo to be empty\n");
737 wait_event_interruptible(device->fifo_wait_queue,
738 device->free_in_fifo == FIFO_SIZE ||
739 kthread_should_stop());
740 if (kthread_should_stop())
743 /* STOP_TRANSMISSION */
744 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.\n");
745 retval = rf69_set_mode(spi, standby);
749 /* everything sent? */
750 if (kfifo_is_empty(&device->tx_fifo)) {
752 if (rx_interrupted) {
753 rx_interrupted = false;
754 pi433_start_rx(device);
756 device->tx_active = false;
757 wake_up_interruptible(&device->rx_wait_queue);
762 /*-------------------------------------------------------------------------*/
765 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
767 struct pi433_instance *instance;
768 struct pi433_device *device;
772 /* check, whether internal buffer is big enough for requested size */
773 if (size > MAX_MSG_SIZE)
776 instance = filp->private_data;
777 device = instance->device;
779 /* just one read request at a time */
780 mutex_lock(&device->rx_lock);
781 if (device->rx_active) {
782 mutex_unlock(&device->rx_lock);
786 device->rx_active = true;
787 mutex_unlock(&device->rx_lock);
789 /* start receiving */
790 /* will block until something was received*/
791 device->rx_buffer_size = size;
792 bytes_received = pi433_receive(device);
795 mutex_lock(&device->rx_lock);
796 device->rx_active = false;
797 mutex_unlock(&device->rx_lock);
799 /* if read was successful copy to user space*/
800 if (bytes_received > 0) {
801 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
806 return bytes_received;
810 pi433_write(struct file *filp, const char __user *buf,
811 size_t count, loff_t *f_pos)
813 struct pi433_instance *instance;
814 struct pi433_device *device;
816 unsigned int required, available, copied;
818 instance = filp->private_data;
819 device = instance->device;
822 * check, whether internal buffer (tx thread) is big enough
825 if (count > MAX_MSG_SIZE)
829 * check if tx_cfg has been initialized otherwise we won't be able to
830 * config the RF trasmitter correctly due to invalid settings
832 if (!instance->tx_cfg_initialized) {
833 dev_notice_once(device->dev,
834 "write: failed due to unconfigured tx_cfg (see PI433_IOC_WR_TX_CFG)\n");
839 * write the following sequence into fifo:
844 mutex_lock(&device->tx_fifo_lock);
846 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
847 available = kfifo_avail(&device->tx_fifo);
848 if (required > available) {
849 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available\n",
850 required, available);
851 mutex_unlock(&device->tx_fifo_lock);
855 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
856 sizeof(instance->tx_cfg));
857 if (retval != sizeof(instance->tx_cfg))
860 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
861 if (retval != sizeof(size_t))
864 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
865 if (retval || copied != count)
868 mutex_unlock(&device->tx_fifo_lock);
871 wake_up_interruptible(&device->tx_wait_queue);
872 dev_dbg(device->dev, "write: generated new msg with %d bytes.\n", copied);
877 dev_warn(device->dev,
878 "write to fifo failed, non recoverable: 0x%x\n", retval);
879 mutex_unlock(&device->tx_fifo_lock);
883 static long pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
885 struct pi433_instance *instance;
886 struct pi433_device *device;
887 struct pi433_tx_cfg tx_cfg;
888 void __user *argp = (void __user *)arg;
890 /* Check type and command number */
891 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
894 instance = filp->private_data;
895 device = instance->device;
901 case PI433_IOC_RD_TX_CFG:
902 if (copy_to_user(argp, &instance->tx_cfg,
903 sizeof(struct pi433_tx_cfg)))
906 case PI433_IOC_WR_TX_CFG:
907 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
909 mutex_lock(&device->tx_fifo_lock);
910 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
911 instance->tx_cfg_initialized = true;
912 mutex_unlock(&device->tx_fifo_lock);
914 case PI433_IOC_RD_RX_CFG:
915 if (copy_to_user(argp, &device->rx_cfg,
916 sizeof(struct pi433_rx_cfg)))
919 case PI433_IOC_WR_RX_CFG:
920 mutex_lock(&device->rx_lock);
922 /* during pendig read request, change of config not allowed */
923 if (device->rx_active) {
924 mutex_unlock(&device->rx_lock);
928 if (copy_from_user(&device->rx_cfg, argp,
929 sizeof(struct pi433_rx_cfg))) {
930 mutex_unlock(&device->rx_lock);
934 mutex_unlock(&device->rx_lock);
943 /*-------------------------------------------------------------------------*/
945 static int pi433_open(struct inode *inode, struct file *filp)
947 struct pi433_device *device;
948 struct pi433_instance *instance;
950 mutex_lock(&minor_lock);
951 device = idr_find(&pi433_idr, iminor(inode));
952 mutex_unlock(&minor_lock);
954 pr_debug("device: minor %d unknown.\n", iminor(inode));
958 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
962 /* setup instance data*/
963 instance->device = device;
965 /* instance data as context */
966 filp->private_data = instance;
967 stream_open(inode, filp);
972 static int pi433_release(struct inode *inode, struct file *filp)
974 struct pi433_instance *instance;
976 instance = filp->private_data;
978 filp->private_data = NULL;
983 /*-------------------------------------------------------------------------*/
985 static int setup_gpio(struct pi433_device *device)
990 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
995 for (i = 0; i < NUM_DIO; i++) {
996 /* "construct" name and get the gpio descriptor */
997 snprintf(name, sizeof(name), "DIO%d", i);
998 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
1001 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1002 dev_dbg(&device->spi->dev,
1003 "Could not find entry for %s. Ignoring.\n", name);
1007 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1008 dev_dbg(&device->spi->dev, "%s is busy.\n", name);
1010 if (IS_ERR(device->gpiod[i])) {
1011 retval = PTR_ERR(device->gpiod[i]);
1012 /* release already allocated gpios */
1013 for (i--; i >= 0; i--) {
1014 free_irq(device->irq_num[i], device);
1015 gpiod_put(device->gpiod[i]);
1020 /* configure the pin */
1021 gpiod_unexport(device->gpiod[i]);
1022 retval = gpiod_direction_input(device->gpiod[i]);
1027 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1028 if (device->irq_num[i] < 0) {
1029 device->gpiod[i] = ERR_PTR(-EINVAL);
1030 return device->irq_num[i];
1032 retval = request_irq(device->irq_num[i],
1041 dev_dbg(&device->spi->dev, "%s successfully configured\n", name);
1047 static void free_gpio(struct pi433_device *device)
1051 for (i = 0; i < NUM_DIO; i++) {
1052 /* check if gpiod is valid */
1053 if (IS_ERR(device->gpiod[i]))
1056 free_irq(device->irq_num[i], device);
1057 gpiod_put(device->gpiod[i]);
1061 static int pi433_get_minor(struct pi433_device *device)
1063 int retval = -ENOMEM;
1065 mutex_lock(&minor_lock);
1066 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1068 device->minor = retval;
1070 } else if (retval == -ENOSPC) {
1071 dev_err(&device->spi->dev, "too many pi433 devices\n");
1074 mutex_unlock(&minor_lock);
1078 static void pi433_free_minor(struct pi433_device *dev)
1080 mutex_lock(&minor_lock);
1081 idr_remove(&pi433_idr, dev->minor);
1082 mutex_unlock(&minor_lock);
1085 /*-------------------------------------------------------------------------*/
1087 static const struct file_operations pi433_fops = {
1088 .owner = THIS_MODULE,
1090 * REVISIT switch to aio primitives, so that userspace
1091 * gets more complete API coverage. It'll simplify things
1092 * too, except for the locking.
1094 .write = pi433_write,
1096 .unlocked_ioctl = pi433_ioctl,
1097 .compat_ioctl = compat_ptr_ioctl,
1099 .release = pi433_release,
1100 .llseek = no_llseek,
1103 static int pi433_debugfs_regs_show(struct seq_file *m, void *p)
1105 struct pi433_device *dev;
1108 char *fmt = "0x%02x, 0x%02x\n";
1113 mutex_lock(&dev->tx_fifo_lock);
1114 mutex_lock(&dev->rx_lock);
1116 // wait for on-going operations to finish
1117 ret = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
1121 ret = wait_event_interruptible(dev->tx_wait_queue, !dev->rx_active);
1125 // skip FIFO register (0x0) otherwise this can affect some of uC ops
1126 for (i = 1; i < 0x50; i++)
1127 reg_data[i] = rf69_read_reg(dev->spi, i);
1129 reg_data[REG_TESTLNA] = rf69_read_reg(dev->spi, REG_TESTLNA);
1130 reg_data[REG_TESTPA1] = rf69_read_reg(dev->spi, REG_TESTPA1);
1131 reg_data[REG_TESTPA2] = rf69_read_reg(dev->spi, REG_TESTPA2);
1132 reg_data[REG_TESTDAGC] = rf69_read_reg(dev->spi, REG_TESTDAGC);
1133 reg_data[REG_TESTAFC] = rf69_read_reg(dev->spi, REG_TESTAFC);
1135 seq_puts(m, "# reg, val\n");
1137 for (i = 1; i < 0x50; i++)
1138 seq_printf(m, fmt, i, reg_data[i]);
1140 seq_printf(m, fmt, REG_TESTLNA, reg_data[REG_TESTLNA]);
1141 seq_printf(m, fmt, REG_TESTPA1, reg_data[REG_TESTPA1]);
1142 seq_printf(m, fmt, REG_TESTPA2, reg_data[REG_TESTPA2]);
1143 seq_printf(m, fmt, REG_TESTDAGC, reg_data[REG_TESTDAGC]);
1144 seq_printf(m, fmt, REG_TESTAFC, reg_data[REG_TESTAFC]);
1147 mutex_unlock(&dev->rx_lock);
1148 mutex_unlock(&dev->tx_fifo_lock);
1153 static int pi433_debugfs_regs_open(struct inode *inode, struct file *filp)
1155 return single_open(filp, pi433_debugfs_regs_show, inode->i_private);
1158 static const struct file_operations debugfs_fops = {
1159 .llseek = seq_lseek,
1160 .open = pi433_debugfs_regs_open,
1161 .owner = THIS_MODULE,
1163 .release = single_release
1166 /*-------------------------------------------------------------------------*/
1168 static int pi433_probe(struct spi_device *spi)
1170 struct pi433_device *device;
1172 struct dentry *entry;
1174 /* setup spi parameters */
1176 spi->bits_per_word = 8;
1178 * spi->max_speed_hz = 10000000;
1179 * 1MHz already set by device tree overlay
1182 retval = spi_setup(spi);
1184 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1189 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed\n",
1190 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1192 /* read chip version */
1193 retval = rf69_get_version(spi);
1199 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)\n", retval);
1202 dev_dbg(&spi->dev, "unknown chip version: 0x%x\n", retval);
1206 /* Allocate driver data */
1207 device = kzalloc(sizeof(*device), GFP_KERNEL);
1211 /* Initialize the driver data */
1213 device->rx_active = false;
1214 device->tx_active = false;
1215 device->interrupt_rx_allowed = false;
1217 /* init rx buffer */
1218 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1219 if (!device->rx_buffer) {
1224 /* init wait queues */
1225 init_waitqueue_head(&device->tx_wait_queue);
1226 init_waitqueue_head(&device->rx_wait_queue);
1227 init_waitqueue_head(&device->fifo_wait_queue);
1230 INIT_KFIFO(device->tx_fifo);
1232 /* init mutexes and locks */
1233 mutex_init(&device->tx_fifo_lock);
1234 mutex_init(&device->rx_lock);
1236 /* setup GPIO (including irq_handler) for the different DIOs */
1237 retval = setup_gpio(device);
1239 dev_dbg(&spi->dev, "setup of GPIOs failed\n");
1243 /* setup the radio module */
1244 retval = rf69_set_mode(spi, standby);
1247 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1250 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1253 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1256 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1259 retval = rf69_set_output_power_level(spi, 13);
1262 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1266 /* determ minor number */
1267 retval = pi433_get_minor(device);
1269 dev_dbg(&spi->dev, "get of minor number failed\n");
1274 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1275 device->dev = device_create(pi433_class,
1281 if (IS_ERR(device->dev)) {
1282 pr_err("pi433: device register failed\n");
1283 retval = PTR_ERR(device->dev);
1284 goto device_create_failed;
1286 dev_dbg(device->dev,
1287 "created device for major %d, minor %d\n",
1292 /* start tx thread */
1293 device->tx_task_struct = kthread_run(pi433_tx_thread,
1297 if (IS_ERR(device->tx_task_struct)) {
1298 dev_dbg(device->dev, "start of send thread failed\n");
1299 retval = PTR_ERR(device->tx_task_struct);
1300 goto send_thread_failed;
1304 device->cdev = cdev_alloc();
1305 if (!device->cdev) {
1306 dev_dbg(device->dev, "allocation of cdev failed\n");
1310 device->cdev->owner = THIS_MODULE;
1311 cdev_init(device->cdev, &pi433_fops);
1312 retval = cdev_add(device->cdev, device->devt, 1);
1314 dev_dbg(device->dev, "register of cdev failed\n");
1319 spi_set_drvdata(spi, device);
1321 entry = debugfs_create_dir(dev_name(device->dev),
1322 debugfs_lookup(KBUILD_MODNAME, NULL));
1323 debugfs_create_file("regs", 0400, entry, device, &debugfs_fops);
1328 cdev_del(device->cdev);
1330 kthread_stop(device->tx_task_struct);
1332 device_destroy(pi433_class, device->devt);
1333 device_create_failed:
1334 pi433_free_minor(device);
1338 kfree(device->rx_buffer);
1345 static void pi433_remove(struct spi_device *spi)
1347 struct pi433_device *device = spi_get_drvdata(spi);
1348 struct dentry *mod_entry = debugfs_lookup(KBUILD_MODNAME, NULL);
1350 debugfs_remove(debugfs_lookup(dev_name(device->dev), mod_entry));
1355 /* make sure ops on existing fds can abort cleanly */
1358 kthread_stop(device->tx_task_struct);
1360 device_destroy(pi433_class, device->devt);
1362 cdev_del(device->cdev);
1364 pi433_free_minor(device);
1366 kfree(device->rx_buffer);
1370 static const struct of_device_id pi433_dt_ids[] = {
1371 { .compatible = "Smarthome-Wolf,pi433" },
1375 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1377 static struct spi_driver pi433_spi_driver = {
1380 .owner = THIS_MODULE,
1381 .of_match_table = of_match_ptr(pi433_dt_ids),
1383 .probe = pi433_probe,
1384 .remove = pi433_remove,
1387 * NOTE: suspend/resume methods are not necessary here.
1388 * We don't do anything except pass the requests to/from
1389 * the underlying controller. The refrigerator handles
1390 * most issues; the controller driver handles the rest.
1394 /*-------------------------------------------------------------------------*/
1396 static int __init pi433_init(void)
1401 * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1402 * work stable - risk of buffer overflow
1404 if (MAX_MSG_SIZE < FIFO_SIZE)
1408 * Claim device numbers. Then register a class
1409 * that will key udev/mdev to add/remove /dev nodes. Last, register
1410 * Last, register the driver which manages those device numbers.
1412 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1416 pi433_class = class_create(THIS_MODULE, "pi433");
1417 if (IS_ERR(pi433_class)) {
1418 unregister_chrdev(MAJOR(pi433_dev),
1419 pi433_spi_driver.driver.name);
1420 return PTR_ERR(pi433_class);
1423 debugfs_create_dir(KBUILD_MODNAME, NULL);
1425 status = spi_register_driver(&pi433_spi_driver);
1427 class_destroy(pi433_class);
1428 unregister_chrdev(MAJOR(pi433_dev),
1429 pi433_spi_driver.driver.name);
1435 module_init(pi433_init);
1437 static void __exit pi433_exit(void)
1439 spi_unregister_driver(&pi433_spi_driver);
1440 class_destroy(pi433_class);
1441 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1442 debugfs_remove_recursive(debugfs_lookup(KBUILD_MODNAME, NULL));
1444 module_exit(pi433_exit);
1446 MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1447 MODULE_DESCRIPTION("Driver for Pi433");
1448 MODULE_LICENSE("GPL");
1449 MODULE_ALIAS("spi:pi433");