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>
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;
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_enable_sync(dev->spi);
326 ret = rf69_disable_sync(dev->spi);
331 if (tx_cfg->enable_length_byte == OPTION_ON) {
332 ret = rf69_set_packet_format(dev->spi, packet_length_var);
336 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
341 if (tx_cfg->enable_crc == OPTION_ON) {
342 ret = rf69_enable_crc(dev->spi);
346 ret = rf69_disable_crc(dev->spi);
351 /* configure sync, if enabled */
352 if (tx_cfg->enable_sync == OPTION_ON) {
353 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
356 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
364 /*-------------------------------------------------------------------------*/
367 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 /*-------------------------------------------------------------------------*/
408 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");
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_get_flag(dev->spi, rssi_exceeded_threshold)) {
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");
445 retval = wait_event_interruptible(dev->rx_wait_queue,
446 rf69_get_flag(dev->spi,
447 rssi_exceeded_threshold));
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",
474 bytes_total = dev->rx_buffer_size;
475 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read",
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",
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");
513 while (dev->rx_position < bytes_total) {
514 if (!rf69_get_flag(dev->spi, 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);
560 pi433_tx_thread(void *data)
562 struct pi433_device *device = data;
563 struct spi_device *spi = device->spi;
564 struct pi433_tx_cfg tx_cfg;
566 bool rx_interrupted = false;
567 int position, repetitions;
571 /* wait for fifo to be populated or for request to terminate*/
572 dev_dbg(device->dev, "thread: going to wait for new messages");
573 wait_event_interruptible(device->tx_wait_queue,
574 (!kfifo_is_empty(&device->tx_fifo) ||
575 kthread_should_stop()));
576 if (kthread_should_stop())
580 * get data from fifo in the following order:
585 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
586 if (retval != sizeof(tx_cfg)) {
588 "reading tx_cfg from fifo failed: got %d byte(s), expected %d",
589 retval, (unsigned int)sizeof(tx_cfg));
593 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
594 if (retval != sizeof(size_t)) {
596 "reading msg size from fifo failed: got %d, expected %d",
597 retval, (unsigned int)sizeof(size_t));
601 /* use fixed message length, if requested */
602 if (tx_cfg.fixed_message_length != 0)
603 size = tx_cfg.fixed_message_length;
605 /* increase size, if len byte is requested */
606 if (tx_cfg.enable_length_byte == OPTION_ON)
609 /* increase size, if adr byte is requested */
610 if (tx_cfg.enable_address_byte == OPTION_ON)
614 memset(device->buffer, 0, size);
617 /* add length byte, if requested */
618 if (tx_cfg.enable_length_byte == OPTION_ON)
620 * according to spec, length byte itself must be
621 * excluded from the length calculation
623 device->buffer[position++] = size - 1;
625 /* add adr byte, if requested */
626 if (tx_cfg.enable_address_byte == OPTION_ON)
627 device->buffer[position++] = tx_cfg.address_byte;
629 /* finally get message data from fifo */
630 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
631 sizeof(device->buffer) - position);
633 "read %d message byte(s) from fifo queue.", retval);
636 * if rx is active, we need to interrupt the waiting for
637 * incoming telegrams, to be able to send something.
638 * We are only allowed, if currently no reception takes
639 * place otherwise we need to wait for the incoming telegram
642 wait_event_interruptible(device->tx_wait_queue,
643 !device->rx_active ||
644 device->interrupt_rx_allowed);
647 * prevent race conditions
648 * irq will be reenabled after tx config is set
650 disable_irq(device->irq_num[DIO0]);
651 device->tx_active = true;
653 if (device->rx_active && !rx_interrupted) {
655 * rx is currently waiting for a telegram;
656 * we need to set the radio module to standby
658 retval = rf69_set_mode(device->spi, standby);
661 rx_interrupted = true;
664 /* clear fifo, set fifo threshold, set payload length */
665 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
668 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
671 if (tx_cfg.enable_length_byte == OPTION_ON) {
672 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
676 retval = rf69_set_payload_length(spi, 0);
681 /* configure the rf chip */
682 retval = rf69_set_tx_cfg(device, &tx_cfg);
686 /* enable fifo level interrupt */
687 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
690 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
691 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
693 /* enable packet sent interrupt */
694 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
697 device->irq_state[DIO0] = DIO_PACKET_SENT;
698 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
699 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
701 /* enable transmission */
702 retval = rf69_set_mode(spi, transmit);
706 /* transfer this msg (and repetitions) to chip fifo */
707 device->free_in_fifo = FIFO_SIZE;
709 repetitions = tx_cfg.repetitions;
710 while ((repetitions > 0) && (size > position)) {
711 if ((size - position) > device->free_in_fifo) {
712 /* msg to big for fifo - take a part */
713 int write_size = device->free_in_fifo;
715 device->free_in_fifo = 0;
717 &device->buffer[position],
719 position += write_size;
721 /* msg fits into fifo - take all */
722 device->free_in_fifo -= size;
725 &device->buffer[position],
727 position = 0; /* reset for next repetition */
730 retval = wait_event_interruptible(device->fifo_wait_queue,
731 device->free_in_fifo > 0);
733 dev_dbg(device->dev, "ABORT\n");
738 /* we are done. Wait for packet to get sent */
740 "thread: wait for packet to get sent/fifo to be empty");
741 wait_event_interruptible(device->fifo_wait_queue,
742 device->free_in_fifo == FIFO_SIZE ||
743 kthread_should_stop());
744 if (kthread_should_stop())
745 dev_dbg(device->dev, "ABORT\n");
747 /* STOP_TRANSMISSION */
748 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.");
749 retval = rf69_set_mode(spi, standby);
753 /* everything sent? */
754 if (kfifo_is_empty(&device->tx_fifo)) {
756 if (rx_interrupted) {
757 rx_interrupted = false;
758 pi433_start_rx(device);
760 device->tx_active = false;
761 wake_up_interruptible(&device->rx_wait_queue);
766 /*-------------------------------------------------------------------------*/
769 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
771 struct pi433_instance *instance;
772 struct pi433_device *device;
776 /* check, whether internal buffer is big enough for requested size */
777 if (size > MAX_MSG_SIZE)
780 instance = filp->private_data;
781 device = instance->device;
783 /* just one read request at a time */
784 mutex_lock(&device->rx_lock);
785 if (device->rx_active) {
786 mutex_unlock(&device->rx_lock);
790 device->rx_active = true;
791 mutex_unlock(&device->rx_lock);
793 /* start receiving */
794 /* will block until something was received*/
795 device->rx_buffer_size = size;
796 bytes_received = pi433_receive(device);
799 mutex_lock(&device->rx_lock);
800 device->rx_active = false;
801 mutex_unlock(&device->rx_lock);
803 /* if read was successful copy to user space*/
804 if (bytes_received > 0) {
805 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
810 return bytes_received;
814 pi433_write(struct file *filp, const char __user *buf,
815 size_t count, loff_t *f_pos)
817 struct pi433_instance *instance;
818 struct pi433_device *device;
820 unsigned int required, available, copied;
822 instance = filp->private_data;
823 device = instance->device;
826 * check, whether internal buffer (tx thread) is big enough
829 if (count > MAX_MSG_SIZE)
833 * write the following sequence into fifo:
838 mutex_lock(&device->tx_fifo_lock);
840 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
841 available = kfifo_avail(&device->tx_fifo);
842 if (required > available) {
843 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available",
844 required, available);
845 mutex_unlock(&device->tx_fifo_lock);
849 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
850 sizeof(instance->tx_cfg));
851 if (retval != sizeof(instance->tx_cfg))
854 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
855 if (retval != sizeof(size_t))
858 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
859 if (retval || copied != count)
862 mutex_unlock(&device->tx_fifo_lock);
865 wake_up_interruptible(&device->tx_wait_queue);
866 dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied);
871 dev_warn(device->dev,
872 "write to fifo failed, non recoverable: 0x%x", retval);
873 mutex_unlock(&device->tx_fifo_lock);
878 pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
881 struct pi433_instance *instance;
882 struct pi433_device *device;
883 struct pi433_tx_cfg tx_cfg;
884 void __user *argp = (void __user *)arg;
886 /* Check type and command number */
887 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
890 instance = filp->private_data;
891 device = instance->device;
897 case PI433_IOC_RD_TX_CFG:
898 if (copy_to_user(argp, &instance->tx_cfg,
899 sizeof(struct pi433_tx_cfg)))
902 case PI433_IOC_WR_TX_CFG:
903 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
905 mutex_lock(&device->tx_fifo_lock);
906 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
907 mutex_unlock(&device->tx_fifo_lock);
909 case PI433_IOC_RD_RX_CFG:
910 if (copy_to_user(argp, &device->rx_cfg,
911 sizeof(struct pi433_rx_cfg)))
914 case PI433_IOC_WR_RX_CFG:
915 mutex_lock(&device->rx_lock);
917 /* during pendig read request, change of config not allowed */
918 if (device->rx_active) {
919 mutex_unlock(&device->rx_lock);
923 if (copy_from_user(&device->rx_cfg, argp,
924 sizeof(struct pi433_rx_cfg))) {
925 mutex_unlock(&device->rx_lock);
929 mutex_unlock(&device->rx_lock);
940 pi433_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
942 return pi433_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
945 #define pi433_compat_ioctl NULL
946 #endif /* CONFIG_COMPAT */
948 /*-------------------------------------------------------------------------*/
950 static int pi433_open(struct inode *inode, struct file *filp)
952 struct pi433_device *device;
953 struct pi433_instance *instance;
955 mutex_lock(&minor_lock);
956 device = idr_find(&pi433_idr, iminor(inode));
957 mutex_unlock(&minor_lock);
959 pr_debug("device: minor %d unknown.\n", iminor(inode));
963 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
967 /* setup instance data*/
968 instance->device = device;
969 instance->tx_cfg.bit_rate = 4711;
970 // TODO: fill instance->tx_cfg;
972 /* instance data as context */
973 filp->private_data = instance;
974 stream_open(inode, filp);
979 static int pi433_release(struct inode *inode, struct file *filp)
981 struct pi433_instance *instance;
983 instance = filp->private_data;
985 filp->private_data = NULL;
990 /*-------------------------------------------------------------------------*/
992 static int setup_gpio(struct pi433_device *device)
997 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
1002 for (i = 0; i < NUM_DIO; i++) {
1003 /* "construct" name and get the gpio descriptor */
1004 snprintf(name, sizeof(name), "DIO%d", i);
1005 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
1008 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1009 dev_dbg(&device->spi->dev,
1010 "Could not find entry for %s. Ignoring.", name);
1014 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1015 dev_dbg(&device->spi->dev, "%s is busy.", name);
1017 if (IS_ERR(device->gpiod[i])) {
1018 retval = PTR_ERR(device->gpiod[i]);
1019 /* release already allocated gpios */
1020 for (i--; i >= 0; i--) {
1021 free_irq(device->irq_num[i], device);
1022 gpiod_put(device->gpiod[i]);
1027 /* configure the pin */
1028 gpiod_unexport(device->gpiod[i]);
1029 retval = gpiod_direction_input(device->gpiod[i]);
1034 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1035 if (device->irq_num[i] < 0) {
1036 device->gpiod[i] = ERR_PTR(-EINVAL);
1037 return device->irq_num[i];
1039 retval = request_irq(device->irq_num[i],
1048 dev_dbg(&device->spi->dev, "%s successfully configured", name);
1054 static void free_gpio(struct pi433_device *device)
1058 for (i = 0; i < NUM_DIO; i++) {
1059 /* check if gpiod is valid */
1060 if (IS_ERR(device->gpiod[i]))
1063 free_irq(device->irq_num[i], device);
1064 gpiod_put(device->gpiod[i]);
1068 static int pi433_get_minor(struct pi433_device *device)
1070 int retval = -ENOMEM;
1072 mutex_lock(&minor_lock);
1073 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1075 device->minor = retval;
1077 } else if (retval == -ENOSPC) {
1078 dev_err(&device->spi->dev, "too many pi433 devices\n");
1081 mutex_unlock(&minor_lock);
1085 static void pi433_free_minor(struct pi433_device *dev)
1087 mutex_lock(&minor_lock);
1088 idr_remove(&pi433_idr, dev->minor);
1089 mutex_unlock(&minor_lock);
1092 /*-------------------------------------------------------------------------*/
1094 static const struct file_operations pi433_fops = {
1095 .owner = THIS_MODULE,
1097 * REVISIT switch to aio primitives, so that userspace
1098 * gets more complete API coverage. It'll simplify things
1099 * too, except for the locking.
1101 .write = pi433_write,
1103 .unlocked_ioctl = pi433_ioctl,
1104 .compat_ioctl = pi433_compat_ioctl,
1106 .release = pi433_release,
1107 .llseek = no_llseek,
1110 /*-------------------------------------------------------------------------*/
1112 static int pi433_probe(struct spi_device *spi)
1114 struct pi433_device *device;
1117 /* setup spi parameters */
1119 spi->bits_per_word = 8;
1121 * spi->max_speed_hz = 10000000;
1122 * 1MHz already set by device tree overlay
1125 retval = spi_setup(spi);
1127 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1132 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed",
1133 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1135 /* Ping the chip by reading the version register */
1136 retval = spi_w8r8(spi, 0x10);
1142 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval);
1145 dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval);
1149 /* Allocate driver data */
1150 device = kzalloc(sizeof(*device), GFP_KERNEL);
1154 /* Initialize the driver data */
1156 device->rx_active = false;
1157 device->tx_active = false;
1158 device->interrupt_rx_allowed = false;
1160 /* init rx buffer */
1161 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1162 if (!device->rx_buffer) {
1167 /* init wait queues */
1168 init_waitqueue_head(&device->tx_wait_queue);
1169 init_waitqueue_head(&device->rx_wait_queue);
1170 init_waitqueue_head(&device->fifo_wait_queue);
1173 INIT_KFIFO(device->tx_fifo);
1175 /* init mutexes and locks */
1176 mutex_init(&device->tx_fifo_lock);
1177 mutex_init(&device->rx_lock);
1179 /* setup GPIO (including irq_handler) for the different DIOs */
1180 retval = setup_gpio(device);
1182 dev_dbg(&spi->dev, "setup of GPIOs failed");
1186 /* setup the radio module */
1187 retval = rf69_set_mode(spi, standby);
1190 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1193 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1196 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1199 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1202 retval = rf69_set_output_power_level(spi, 13);
1205 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1209 /* determ minor number */
1210 retval = pi433_get_minor(device);
1212 dev_dbg(&spi->dev, "get of minor number failed");
1217 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1218 device->dev = device_create(pi433_class,
1224 if (IS_ERR(device->dev)) {
1225 pr_err("pi433: device register failed\n");
1226 retval = PTR_ERR(device->dev);
1227 goto device_create_failed;
1229 dev_dbg(device->dev,
1230 "created device for major %d, minor %d\n",
1235 /* start tx thread */
1236 device->tx_task_struct = kthread_run(pi433_tx_thread,
1240 if (IS_ERR(device->tx_task_struct)) {
1241 dev_dbg(device->dev, "start of send thread failed");
1242 retval = PTR_ERR(device->tx_task_struct);
1243 goto send_thread_failed;
1247 device->cdev = cdev_alloc();
1248 if (!device->cdev) {
1249 dev_dbg(device->dev, "allocation of cdev failed");
1252 device->cdev->owner = THIS_MODULE;
1253 cdev_init(device->cdev, &pi433_fops);
1254 retval = cdev_add(device->cdev, device->devt, 1);
1256 dev_dbg(device->dev, "register of cdev failed");
1261 spi_set_drvdata(spi, device);
1266 cdev_del(device->cdev);
1268 kthread_stop(device->tx_task_struct);
1270 device_destroy(pi433_class, device->devt);
1271 device_create_failed:
1272 pi433_free_minor(device);
1276 kfree(device->rx_buffer);
1283 static int pi433_remove(struct spi_device *spi)
1285 struct pi433_device *device = spi_get_drvdata(spi);
1290 /* make sure ops on existing fds can abort cleanly */
1293 kthread_stop(device->tx_task_struct);
1295 device_destroy(pi433_class, device->devt);
1297 cdev_del(device->cdev);
1299 pi433_free_minor(device);
1301 kfree(device->rx_buffer);
1307 static const struct of_device_id pi433_dt_ids[] = {
1308 { .compatible = "Smarthome-Wolf,pi433" },
1312 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1314 static struct spi_driver pi433_spi_driver = {
1317 .owner = THIS_MODULE,
1318 .of_match_table = of_match_ptr(pi433_dt_ids),
1320 .probe = pi433_probe,
1321 .remove = pi433_remove,
1324 * NOTE: suspend/resume methods are not necessary here.
1325 * We don't do anything except pass the requests to/from
1326 * the underlying controller. The refrigerator handles
1327 * most issues; the controller driver handles the rest.
1331 /*-------------------------------------------------------------------------*/
1333 static int __init pi433_init(void)
1338 * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1339 * work stable - risk of buffer overflow
1341 if (MAX_MSG_SIZE < FIFO_SIZE)
1345 * Claim device numbers. Then register a class
1346 * that will key udev/mdev to add/remove /dev nodes. Last, register
1347 * Last, register the driver which manages those device numbers.
1349 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1353 pi433_class = class_create(THIS_MODULE, "pi433");
1354 if (IS_ERR(pi433_class)) {
1355 unregister_chrdev(MAJOR(pi433_dev),
1356 pi433_spi_driver.driver.name);
1357 return PTR_ERR(pi433_class);
1360 status = spi_register_driver(&pi433_spi_driver);
1362 class_destroy(pi433_class);
1363 unregister_chrdev(MAJOR(pi433_dev),
1364 pi433_spi_driver.driver.name);
1370 module_init(pi433_init);
1372 static void __exit pi433_exit(void)
1374 spi_unregister_driver(&pi433_spi_driver);
1375 class_destroy(pi433_class);
1376 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1378 module_exit(pi433_exit);
1380 MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1381 MODULE_DESCRIPTION("Driver for Pi433");
1382 MODULE_LICENSE("GPL");
1383 MODULE_ALIAS("spi:pi433");