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>
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 */
58 static struct dentry *root_dir; /* debugfs root directory for the driver */
60 static struct class *pi433_class; /* mainly for udev to create /dev/pi433 */
63 * tx config is instance specific
64 * so with each open a new tx config struct is needed
67 * rx config is device specific
68 * so we have just one rx config, ebedded in device struct
71 /* device handling related values */
76 struct spi_device *spi;
78 /* irq related values */
79 struct gpio_desc *gpiod[NUM_DIO];
81 u8 irq_state[NUM_DIO];
83 /* tx related values */
84 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
85 struct mutex tx_fifo_lock; /* serialize userspace writers */
86 struct task_struct *tx_task_struct;
87 wait_queue_head_t tx_wait_queue;
89 char buffer[MAX_MSG_SIZE];
91 /* rx related values */
92 struct pi433_rx_cfg rx_cfg;
94 unsigned int rx_buffer_size;
97 unsigned int rx_position;
98 struct mutex rx_lock; /* protects rx_* variable accesses */
99 wait_queue_head_t rx_wait_queue;
101 /* fifo wait queue */
102 struct task_struct *fifo_task_struct;
103 wait_queue_head_t fifo_wait_queue;
108 bool interrupt_rx_allowed;
111 struct pi433_instance {
112 struct pi433_device *device;
113 struct pi433_tx_cfg tx_cfg;
116 bool tx_cfg_initialized;
119 /*-------------------------------------------------------------------------*/
121 /* GPIO interrupt handlers */
122 static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
124 struct pi433_device *device = dev_id;
126 if (device->irq_state[DIO0] == DIO_PACKET_SENT) {
127 device->free_in_fifo = FIFO_SIZE;
128 dev_dbg(device->dev, "DIO0 irq: Packet sent\n");
129 wake_up_interruptible(&device->fifo_wait_queue);
130 } else if (device->irq_state[DIO0] == DIO_RSSI_DIO0) {
131 dev_dbg(device->dev, "DIO0 irq: RSSI level over threshold\n");
132 wake_up_interruptible(&device->rx_wait_queue);
133 } else if (device->irq_state[DIO0] == DIO_PAYLOAD_READY) {
134 dev_dbg(device->dev, "DIO0 irq: Payload ready\n");
135 device->free_in_fifo = 0;
136 wake_up_interruptible(&device->fifo_wait_queue);
142 static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
144 struct pi433_device *device = dev_id;
146 if (device->irq_state[DIO1] == DIO_FIFO_NOT_EMPTY_DIO1) {
147 device->free_in_fifo = FIFO_SIZE;
148 } else if (device->irq_state[DIO1] == DIO_FIFO_LEVEL) {
149 if (device->rx_active)
150 device->free_in_fifo = FIFO_THRESHOLD - 1;
152 device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
155 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
156 wake_up_interruptible(&device->fifo_wait_queue);
161 /*-------------------------------------------------------------------------*/
164 rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
169 /* receiver config */
170 ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
173 ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
176 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
179 ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
182 ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
185 ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
188 ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
189 rx_cfg->bw_exponent);
192 ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
193 rx_cfg->bw_exponent);
196 ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
200 dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
204 if (rx_cfg->enable_sync == OPTION_ON) {
205 ret = rf69_enable_sync(dev->spi);
209 ret = rf69_set_fifo_fill_condition(dev->spi,
210 after_sync_interrupt);
214 ret = rf69_disable_sync(dev->spi);
218 ret = rf69_set_fifo_fill_condition(dev->spi, always);
222 if (rx_cfg->enable_length_byte == OPTION_ON) {
223 ret = rf69_set_packet_format(dev->spi, packet_length_var);
227 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
231 ret = rf69_set_address_filtering(dev->spi,
232 rx_cfg->enable_address_filtering);
236 if (rx_cfg->enable_crc == OPTION_ON) {
237 ret = rf69_enable_crc(dev->spi);
241 ret = rf69_disable_crc(dev->spi);
247 ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
250 if (rx_cfg->enable_length_byte == OPTION_ON) {
251 ret = rf69_set_payload_length(dev->spi, 0xff);
254 } else if (rx_cfg->fixed_message_length != 0) {
255 payload_length = rx_cfg->fixed_message_length;
256 if (rx_cfg->enable_length_byte == OPTION_ON)
258 if (rx_cfg->enable_address_filtering != filtering_off)
260 ret = rf69_set_payload_length(dev->spi, payload_length);
264 ret = rf69_set_payload_length(dev->spi, 0);
270 if (rx_cfg->enable_sync == OPTION_ON) {
271 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
275 if (rx_cfg->enable_address_filtering != filtering_off) {
276 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
279 ret = rf69_set_broadcast_address(dev->spi,
280 rx_cfg->broadcast_address);
289 rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
293 ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
296 ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
299 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
302 ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
305 ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
308 ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
311 ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
315 /* packet format enable */
316 if (tx_cfg->enable_preamble == OPTION_ON) {
317 ret = rf69_set_preamble_length(dev->spi,
318 tx_cfg->preamble_length);
322 ret = rf69_set_preamble_length(dev->spi, 0);
327 if (tx_cfg->enable_sync == OPTION_ON) {
328 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
331 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
334 ret = rf69_enable_sync(dev->spi);
338 ret = rf69_disable_sync(dev->spi);
343 if (tx_cfg->enable_length_byte == OPTION_ON) {
344 ret = rf69_set_packet_format(dev->spi, packet_length_var);
348 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
353 if (tx_cfg->enable_crc == OPTION_ON) {
354 ret = rf69_enable_crc(dev->spi);
358 ret = rf69_disable_crc(dev->spi);
366 /*-------------------------------------------------------------------------*/
368 static int pi433_start_rx(struct pi433_device *dev)
372 /* return without action, if no pending read request */
376 /* setup for receiving */
377 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
382 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
385 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
386 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
388 /* setup fifo level interrupt */
389 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
392 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
395 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
396 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
398 /* set module to receiving mode */
399 retval = rf69_set_mode(dev->spi, receive);
406 /*-------------------------------------------------------------------------*/
408 static int pi433_receive(void *data)
410 struct pi433_device *dev = data;
411 struct spi_device *spi = dev->spi;
412 int bytes_to_read, bytes_total;
415 dev->interrupt_rx_allowed = false;
417 /* wait for any tx to finish */
418 dev_dbg(dev->dev, "rx: going to wait for any tx to finish\n");
419 retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
421 /* wait was interrupted */
422 dev->interrupt_rx_allowed = true;
423 wake_up_interruptible(&dev->tx_wait_queue);
427 /* prepare status vars */
428 dev->free_in_fifo = FIFO_SIZE;
429 dev->rx_position = 0;
430 dev->rx_bytes_dropped = 0;
432 /* setup radio module to listen for something "in the air" */
433 retval = pi433_start_rx(dev);
437 /* now check RSSI, if low wait for getting high (RSSI interrupt) */
438 while (!(rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RSSI)) {
439 /* allow tx to interrupt us while waiting for high RSSI */
440 dev->interrupt_rx_allowed = true;
441 wake_up_interruptible(&dev->tx_wait_queue);
443 /* wait for RSSI level to become high */
444 dev_dbg(dev->dev, "rx: going to wait for high RSSI level\n");
445 retval = wait_event_interruptible(dev->rx_wait_queue,
446 rf69_read_reg(spi, REG_IRQFLAGS1) &
447 MASK_IRQFLAGS1_RSSI);
448 if (retval) /* wait was interrupted */
450 dev->interrupt_rx_allowed = false;
452 /* cross check for ongoing tx */
457 /* configure payload ready irq */
458 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
461 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
462 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
464 /* fixed or unlimited length? */
465 if (dev->rx_cfg.fixed_message_length != 0) {
466 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
470 bytes_total = dev->rx_cfg.fixed_message_length;
471 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length\n",
474 bytes_total = dev->rx_buffer_size;
475 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read\n",
479 /* length byte enabled? */
480 if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
481 retval = wait_event_interruptible(dev->fifo_wait_queue,
482 dev->free_in_fifo < FIFO_SIZE);
483 if (retval) /* wait was interrupted */
486 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
487 if (bytes_total > dev->rx_buffer_size) {
492 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte\n",
496 /* address byte enabled? */
497 if (dev->rx_cfg.enable_address_filtering != filtering_off) {
502 retval = wait_event_interruptible(dev->fifo_wait_queue,
503 dev->free_in_fifo < FIFO_SIZE);
504 if (retval) /* wait was interrupted */
507 rf69_read_fifo(spi, &dummy, 1);
509 dev_dbg(dev->dev, "rx: address byte stripped off\n");
513 while (dev->rx_position < bytes_total) {
514 if (!(rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PAYLOAD_READY)) {
515 retval = wait_event_interruptible(dev->fifo_wait_queue,
516 dev->free_in_fifo < FIFO_SIZE);
517 if (retval) /* wait was interrupted */
521 /* need to drop bytes or acquire? */
522 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
523 bytes_to_read = dev->rx_bytes_to_drop -
524 dev->rx_bytes_dropped;
526 bytes_to_read = bytes_total - dev->rx_position;
528 /* access the fifo */
529 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
530 bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
531 retval = rf69_read_fifo(spi,
532 &dev->rx_buffer[dev->rx_position],
534 if (retval) /* read failed */
537 dev->free_in_fifo += bytes_to_read;
539 /* adjust status vars */
540 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
541 dev->rx_bytes_dropped += bytes_to_read;
543 dev->rx_position += bytes_to_read;
546 /* rx done, wait was interrupted or error occurred */
548 dev->interrupt_rx_allowed = true;
549 if (rf69_set_mode(dev->spi, standby))
550 pr_err("rf69_set_mode(): radio module failed to go standby\n");
551 wake_up_interruptible(&dev->tx_wait_queue);
559 static int pi433_tx_thread(void *data)
561 struct pi433_device *device = data;
562 struct spi_device *spi = device->spi;
563 struct pi433_tx_cfg tx_cfg;
565 bool rx_interrupted = false;
566 int position, repetitions;
570 /* wait for fifo to be populated or for request to terminate*/
571 dev_dbg(device->dev, "thread: going to wait for new messages\n");
572 wait_event_interruptible(device->tx_wait_queue,
573 (!kfifo_is_empty(&device->tx_fifo) ||
574 kthread_should_stop()));
575 if (kthread_should_stop())
579 * get data from fifo in the following order:
584 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
585 if (retval != sizeof(tx_cfg)) {
587 "reading tx_cfg from fifo failed: got %d byte(s), expected %d\n",
588 retval, (unsigned int)sizeof(tx_cfg));
592 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
593 if (retval != sizeof(size_t)) {
595 "reading msg size from fifo failed: got %d, expected %d\n",
596 retval, (unsigned int)sizeof(size_t));
600 /* use fixed message length, if requested */
601 if (tx_cfg.fixed_message_length != 0)
602 size = tx_cfg.fixed_message_length;
604 /* increase size, if len byte is requested */
605 if (tx_cfg.enable_length_byte == OPTION_ON)
608 /* increase size, if adr byte is requested */
609 if (tx_cfg.enable_address_byte == OPTION_ON)
613 memset(device->buffer, 0, size);
616 /* add length byte, if requested */
617 if (tx_cfg.enable_length_byte == OPTION_ON)
619 * according to spec, length byte itself must be
620 * excluded from the length calculation
622 device->buffer[position++] = size - 1;
624 /* add adr byte, if requested */
625 if (tx_cfg.enable_address_byte == OPTION_ON)
626 device->buffer[position++] = tx_cfg.address_byte;
628 /* finally get message data from fifo */
629 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
630 sizeof(device->buffer) - position);
632 "read %d message byte(s) from fifo queue.\n", retval);
635 * if rx is active, we need to interrupt the waiting for
636 * incoming telegrams, to be able to send something.
637 * We are only allowed, if currently no reception takes
638 * place otherwise we need to wait for the incoming telegram
641 wait_event_interruptible(device->tx_wait_queue,
642 !device->rx_active ||
643 device->interrupt_rx_allowed);
646 * prevent race conditions
647 * irq will be reenabled after tx config is set
649 disable_irq(device->irq_num[DIO0]);
650 device->tx_active = true;
652 /* clear fifo, set fifo threshold, set payload length */
653 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
657 if (device->rx_active && !rx_interrupted) {
659 * rx is currently waiting for a telegram;
660 * we need to set the radio module to standby
662 rx_interrupted = true;
665 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
668 if (tx_cfg.enable_length_byte == OPTION_ON) {
669 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
673 retval = rf69_set_payload_length(spi, 0);
678 /* configure the rf chip */
679 retval = rf69_set_tx_cfg(device, &tx_cfg);
683 /* enable fifo level interrupt */
684 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
687 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
688 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
690 /* enable packet sent interrupt */
691 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
694 device->irq_state[DIO0] = DIO_PACKET_SENT;
695 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
696 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
698 /* enable transmission */
699 retval = rf69_set_mode(spi, transmit);
703 /* transfer this msg (and repetitions) to chip fifo */
704 device->free_in_fifo = FIFO_SIZE;
706 repetitions = tx_cfg.repetitions;
707 while ((repetitions > 0) && (size > position)) {
708 if ((size - position) > device->free_in_fifo) {
709 /* msg to big for fifo - take a part */
710 int write_size = device->free_in_fifo;
712 device->free_in_fifo = 0;
714 &device->buffer[position],
716 position += write_size;
718 /* msg fits into fifo - take all */
719 device->free_in_fifo -= size;
722 &device->buffer[position],
724 position = 0; /* reset for next repetition */
727 retval = wait_event_interruptible(device->fifo_wait_queue,
728 device->free_in_fifo > 0);
730 dev_dbg(device->dev, "ABORT\n");
735 /* we are done. Wait for packet to get sent */
737 "thread: wait for packet to get sent/fifo to be empty\n");
738 wait_event_interruptible(device->fifo_wait_queue,
739 device->free_in_fifo == FIFO_SIZE ||
740 kthread_should_stop());
741 if (kthread_should_stop())
744 /* STOP_TRANSMISSION */
745 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.\n");
746 retval = rf69_set_mode(spi, standby);
750 /* everything sent? */
751 if (kfifo_is_empty(&device->tx_fifo)) {
753 if (rx_interrupted) {
754 rx_interrupted = false;
755 pi433_start_rx(device);
757 device->tx_active = false;
758 wake_up_interruptible(&device->rx_wait_queue);
763 /*-------------------------------------------------------------------------*/
766 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
768 struct pi433_instance *instance;
769 struct pi433_device *device;
773 /* check, whether internal buffer is big enough for requested size */
774 if (size > MAX_MSG_SIZE)
777 instance = filp->private_data;
778 device = instance->device;
780 /* just one read request at a time */
781 mutex_lock(&device->rx_lock);
782 if (device->rx_active) {
783 mutex_unlock(&device->rx_lock);
787 device->rx_active = true;
788 mutex_unlock(&device->rx_lock);
790 /* start receiving */
791 /* will block until something was received*/
792 device->rx_buffer_size = size;
793 bytes_received = pi433_receive(device);
796 mutex_lock(&device->rx_lock);
797 device->rx_active = false;
798 mutex_unlock(&device->rx_lock);
800 /* if read was successful copy to user space*/
801 if (bytes_received > 0) {
802 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
807 return bytes_received;
811 pi433_write(struct file *filp, const char __user *buf,
812 size_t count, loff_t *f_pos)
814 struct pi433_instance *instance;
815 struct pi433_device *device;
817 unsigned int required, available, copied;
819 instance = filp->private_data;
820 device = instance->device;
823 * check, whether internal buffer (tx thread) is big enough
826 if (count > MAX_MSG_SIZE)
830 * check if tx_cfg has been initialized otherwise we won't be able to
831 * config the RF trasmitter correctly due to invalid settings
833 if (!instance->tx_cfg_initialized) {
834 dev_notice_once(device->dev,
835 "write: failed due to unconfigured tx_cfg (see PI433_IOC_WR_TX_CFG)\n");
840 * write the following sequence into fifo:
845 mutex_lock(&device->tx_fifo_lock);
847 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
848 available = kfifo_avail(&device->tx_fifo);
849 if (required > available) {
850 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available\n",
851 required, available);
852 mutex_unlock(&device->tx_fifo_lock);
856 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
857 sizeof(instance->tx_cfg));
858 if (retval != sizeof(instance->tx_cfg))
861 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
862 if (retval != sizeof(size_t))
865 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
866 if (retval || copied != count)
869 mutex_unlock(&device->tx_fifo_lock);
872 wake_up_interruptible(&device->tx_wait_queue);
873 dev_dbg(device->dev, "write: generated new msg with %d bytes.\n", copied);
878 dev_warn(device->dev,
879 "write to fifo failed, non recoverable: 0x%x\n", retval);
880 mutex_unlock(&device->tx_fifo_lock);
884 static long pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
886 struct pi433_instance *instance;
887 struct pi433_device *device;
888 struct pi433_tx_cfg tx_cfg;
889 void __user *argp = (void __user *)arg;
891 /* Check type and command number */
892 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
895 instance = filp->private_data;
896 device = instance->device;
902 case PI433_IOC_RD_TX_CFG:
903 if (copy_to_user(argp, &instance->tx_cfg,
904 sizeof(struct pi433_tx_cfg)))
907 case PI433_IOC_WR_TX_CFG:
908 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
910 mutex_lock(&device->tx_fifo_lock);
911 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
912 instance->tx_cfg_initialized = true;
913 mutex_unlock(&device->tx_fifo_lock);
915 case PI433_IOC_RD_RX_CFG:
916 if (copy_to_user(argp, &device->rx_cfg,
917 sizeof(struct pi433_rx_cfg)))
920 case PI433_IOC_WR_RX_CFG:
921 mutex_lock(&device->rx_lock);
923 /* during pendig read request, change of config not allowed */
924 if (device->rx_active) {
925 mutex_unlock(&device->rx_lock);
929 if (copy_from_user(&device->rx_cfg, argp,
930 sizeof(struct pi433_rx_cfg))) {
931 mutex_unlock(&device->rx_lock);
935 mutex_unlock(&device->rx_lock);
944 /*-------------------------------------------------------------------------*/
946 static int pi433_open(struct inode *inode, struct file *filp)
948 struct pi433_device *device;
949 struct pi433_instance *instance;
951 mutex_lock(&minor_lock);
952 device = idr_find(&pi433_idr, iminor(inode));
953 mutex_unlock(&minor_lock);
955 pr_debug("device: minor %d unknown.\n", iminor(inode));
959 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
963 /* setup instance data*/
964 instance->device = device;
966 /* instance data as context */
967 filp->private_data = instance;
968 stream_open(inode, filp);
973 static int pi433_release(struct inode *inode, struct file *filp)
975 struct pi433_instance *instance;
977 instance = filp->private_data;
979 filp->private_data = NULL;
984 /*-------------------------------------------------------------------------*/
986 static int setup_gpio(struct pi433_device *device)
991 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
996 for (i = 0; i < NUM_DIO; i++) {
997 /* "construct" name and get the gpio descriptor */
998 snprintf(name, sizeof(name), "DIO%d", i);
999 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
1002 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1003 dev_dbg(&device->spi->dev,
1004 "Could not find entry for %s. Ignoring.\n", name);
1008 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1009 dev_dbg(&device->spi->dev, "%s is busy.\n", name);
1011 if (IS_ERR(device->gpiod[i])) {
1012 retval = PTR_ERR(device->gpiod[i]);
1013 /* release already allocated gpios */
1014 for (i--; i >= 0; i--) {
1015 free_irq(device->irq_num[i], device);
1016 gpiod_put(device->gpiod[i]);
1021 /* configure the pin */
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);
1152 DEFINE_SHOW_ATTRIBUTE(pi433_debugfs_regs);
1154 /*-------------------------------------------------------------------------*/
1156 static int pi433_probe(struct spi_device *spi)
1158 struct pi433_device *device;
1160 struct dentry *entry;
1162 /* setup spi parameters */
1164 spi->bits_per_word = 8;
1166 * spi->max_speed_hz = 10000000;
1167 * 1MHz already set by device tree overlay
1170 retval = spi_setup(spi);
1172 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1177 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed\n",
1178 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1180 /* read chip version */
1181 retval = rf69_get_version(spi);
1187 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)\n", retval);
1190 dev_dbg(&spi->dev, "unknown chip version: 0x%x\n", retval);
1194 /* Allocate driver data */
1195 device = kzalloc(sizeof(*device), GFP_KERNEL);
1199 /* Initialize the driver data */
1201 device->rx_active = false;
1202 device->tx_active = false;
1203 device->interrupt_rx_allowed = false;
1205 /* init rx buffer */
1206 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1207 if (!device->rx_buffer) {
1212 /* init wait queues */
1213 init_waitqueue_head(&device->tx_wait_queue);
1214 init_waitqueue_head(&device->rx_wait_queue);
1215 init_waitqueue_head(&device->fifo_wait_queue);
1218 INIT_KFIFO(device->tx_fifo);
1220 /* init mutexes and locks */
1221 mutex_init(&device->tx_fifo_lock);
1222 mutex_init(&device->rx_lock);
1224 /* setup GPIO (including irq_handler) for the different DIOs */
1225 retval = setup_gpio(device);
1227 dev_dbg(&spi->dev, "setup of GPIOs failed\n");
1231 /* setup the radio module */
1232 retval = rf69_set_mode(spi, standby);
1235 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1238 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1241 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1244 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1247 retval = rf69_set_output_power_level(spi, 13);
1250 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1254 /* determ minor number */
1255 retval = pi433_get_minor(device);
1257 dev_dbg(&spi->dev, "get of minor number failed\n");
1262 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1263 device->dev = device_create(pi433_class,
1269 if (IS_ERR(device->dev)) {
1270 pr_err("pi433: device register failed\n");
1271 retval = PTR_ERR(device->dev);
1272 goto device_create_failed;
1274 dev_dbg(device->dev,
1275 "created device for major %d, minor %d\n",
1280 /* start tx thread */
1281 device->tx_task_struct = kthread_run(pi433_tx_thread,
1285 if (IS_ERR(device->tx_task_struct)) {
1286 dev_dbg(device->dev, "start of send thread failed\n");
1287 retval = PTR_ERR(device->tx_task_struct);
1288 goto send_thread_failed;
1292 device->cdev = cdev_alloc();
1293 if (!device->cdev) {
1294 dev_dbg(device->dev, "allocation of cdev failed\n");
1298 device->cdev->owner = THIS_MODULE;
1299 cdev_init(device->cdev, &pi433_fops);
1300 retval = cdev_add(device->cdev, device->devt, 1);
1302 dev_dbg(device->dev, "register of cdev failed\n");
1307 spi_set_drvdata(spi, device);
1309 entry = debugfs_create_dir(dev_name(device->dev), root_dir);
1310 debugfs_create_file("regs", 0400, entry, device, &pi433_debugfs_regs_fops);
1315 cdev_del(device->cdev);
1317 kthread_stop(device->tx_task_struct);
1319 device_destroy(pi433_class, device->devt);
1320 device_create_failed:
1321 pi433_free_minor(device);
1325 kfree(device->rx_buffer);
1332 static void pi433_remove(struct spi_device *spi)
1334 struct pi433_device *device = spi_get_drvdata(spi);
1336 debugfs_lookup_and_remove(dev_name(device->dev), root_dir);
1341 /* make sure ops on existing fds can abort cleanly */
1344 kthread_stop(device->tx_task_struct);
1346 device_destroy(pi433_class, device->devt);
1348 cdev_del(device->cdev);
1350 pi433_free_minor(device);
1352 kfree(device->rx_buffer);
1356 static const struct of_device_id pi433_dt_ids[] = {
1357 { .compatible = "Smarthome-Wolf,pi433" },
1361 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1363 static struct spi_driver pi433_spi_driver = {
1366 .owner = THIS_MODULE,
1367 .of_match_table = of_match_ptr(pi433_dt_ids),
1369 .probe = pi433_probe,
1370 .remove = pi433_remove,
1373 * NOTE: suspend/resume methods are not necessary here.
1374 * We don't do anything except pass the requests to/from
1375 * the underlying controller. The refrigerator handles
1376 * most issues; the controller driver handles the rest.
1380 /*-------------------------------------------------------------------------*/
1382 static int __init pi433_init(void)
1387 * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1388 * work stable - risk of buffer overflow
1390 if (MAX_MSG_SIZE < FIFO_SIZE)
1394 * Claim device numbers. Then register a class
1395 * that will key udev/mdev to add/remove /dev nodes.
1396 * Last, register the driver which manages those device numbers.
1398 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1402 pi433_class = class_create("pi433");
1403 if (IS_ERR(pi433_class)) {
1404 unregister_chrdev(MAJOR(pi433_dev),
1405 pi433_spi_driver.driver.name);
1406 return PTR_ERR(pi433_class);
1409 root_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1411 status = spi_register_driver(&pi433_spi_driver);
1413 class_destroy(pi433_class);
1414 unregister_chrdev(MAJOR(pi433_dev),
1415 pi433_spi_driver.driver.name);
1421 module_init(pi433_init);
1423 static void __exit pi433_exit(void)
1425 spi_unregister_driver(&pi433_spi_driver);
1426 class_destroy(pi433_class);
1427 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1428 debugfs_remove(root_dir);
1430 module_exit(pi433_exit);
1433 MODULE_DESCRIPTION("Driver for Pi433");
1434 MODULE_LICENSE("GPL");
1435 MODULE_ALIAS("spi:pi433");