2 * userspace interface for pi433 radio module
4 * Pi433 is a 433MHz radio module for the Raspberry Pi.
5 * It is based on the HopeRf Module RFM69CW. Therefore inside of this
6 * driver, you'll find an abstraction of the rf69 chip.
8 * If needed, this driver could be extended, to also support other
9 * devices, basing on HopeRfs rf69.
11 * The driver can also be extended, to support other modules of
12 * HopeRf with a similar interace - e. g. RFM69HCW, RFM12, RFM95, ...
14 * Copyright (C) 2016 Wolf-Entwicklungen
15 * Marcus Wolf <linux@wolf-entwicklungen.de>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/idr.h>
33 #include <linux/ioctl.h>
34 #include <linux/uaccess.h>
36 #include <linux/device.h>
37 #include <linux/cdev.h>
38 #include <linux/err.h>
39 #include <linux/kfifo.h>
40 #include <linux/errno.h>
41 #include <linux/mutex.h>
43 #include <linux/of_device.h>
44 #include <linux/interrupt.h>
45 #include <linux/irq.h>
46 #include <linux/gpio/consumer.h>
47 #include <linux/kthread.h>
48 #include <linux/wait.h>
49 #include <linux/spi/spi.h>
51 #include <linux/compat.h>
57 #define N_PI433_MINORS BIT(MINORBITS) /*32*/ /* ... up to 256 */
58 #define MAX_MSG_SIZE 900 /* min: FIFO_SIZE! */
59 #define MSG_FIFO_SIZE 65536 /* 65536 = 2^16 */
62 static dev_t pi433_dev;
63 static DEFINE_IDR(pi433_idr);
64 static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
66 static struct class *pi433_class; /* mainly for udev to create /dev/pi433 */
68 /* tx config is instance specific
69 * so with each open a new tx config struct is needed
71 /* rx config is device specific
72 * so we have just one rx config, ebedded in device struct
75 /* device handling related values */
80 struct spi_device *spi;
83 /* irq related values */
84 struct gpio_desc *gpiod[NUM_DIO];
86 u8 irq_state[NUM_DIO];
88 /* tx related values */
89 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
90 struct mutex tx_fifo_lock; // TODO: check, whether necessary or obsolete
91 struct task_struct *tx_task_struct;
92 wait_queue_head_t tx_wait_queue;
94 char buffer[MAX_MSG_SIZE];
96 /* rx related values */
97 struct pi433_rx_cfg rx_cfg;
99 unsigned int rx_buffer_size;
100 u32 rx_bytes_to_drop;
101 u32 rx_bytes_dropped;
102 unsigned int rx_position;
103 struct mutex rx_lock;
104 wait_queue_head_t rx_wait_queue;
106 /* fifo wait queue */
107 struct task_struct *fifo_task_struct;
108 wait_queue_head_t fifo_wait_queue;
113 bool interrupt_rx_allowed;
116 struct pi433_instance {
117 struct pi433_device *device;
118 struct pi433_tx_cfg tx_cfg;
121 /*-------------------------------------------------------------------------*/
123 /* GPIO interrupt handlers */
124 static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
126 struct pi433_device *device = dev_id;
128 if (device->irq_state[DIO0] == DIO_PACKET_SENT) {
129 device->free_in_fifo = FIFO_SIZE;
130 dev_dbg(device->dev, "DIO0 irq: Packet sent\n");
131 wake_up_interruptible(&device->fifo_wait_queue);
132 } else if (device->irq_state[DIO0] == DIO_RSSI_DIO0) {
133 dev_dbg(device->dev, "DIO0 irq: RSSI level over threshold\n");
134 wake_up_interruptible(&device->rx_wait_queue);
135 } else if (device->irq_state[DIO0] == DIO_PAYLOAD_READY) {
136 dev_dbg(device->dev, "DIO0 irq: Payload ready\n");
137 device->free_in_fifo = 0;
138 wake_up_interruptible(&device->fifo_wait_queue);
144 static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
146 struct pi433_device *device = dev_id;
148 if (device->irq_state[DIO1] == DIO_FIFO_NOT_EMPTY_DIO1) {
149 device->free_in_fifo = FIFO_SIZE;
150 } else if (device->irq_state[DIO1] == DIO_FIFO_LEVEL) {
151 if (device->rx_active)
152 device->free_in_fifo = FIFO_THRESHOLD - 1;
154 device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
157 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
158 wake_up_interruptible(&device->fifo_wait_queue);
163 /*-------------------------------------------------------------------------*/
166 rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
171 /* receiver config */
172 ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
175 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
178 ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
181 ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
184 ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
187 ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
190 ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse, rx_cfg->bw_exponent);
193 ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse, 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, afterSyncInterrupt);
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, packetLengthVar);
226 ret = rf69_set_packet_format(dev->spi, packetLengthFix);
230 ret = rf69_set_adressFiltering(dev->spi, rx_cfg->enable_address_filtering);
234 if (rx_cfg->enable_crc == OPTION_ON) {
235 ret = rf69_enable_crc(dev->spi);
239 ret = rf69_disable_crc(dev->spi);
245 ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
248 if (rx_cfg->enable_length_byte == OPTION_ON) {
249 ret = rf69_set_payload_length(dev->spi, 0xff);
252 } else if (rx_cfg->fixed_message_length != 0) {
253 payload_length = rx_cfg->fixed_message_length;
254 if (rx_cfg->enable_length_byte == OPTION_ON)
256 if (rx_cfg->enable_address_filtering != filteringOff)
258 ret = rf69_set_payload_length(dev->spi, payload_length);
262 ret = rf69_set_payload_length(dev->spi, 0);
268 if (rx_cfg->enable_sync == OPTION_ON) {
269 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
273 if (rx_cfg->enable_address_filtering != filteringOff) {
274 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
277 ret = rf69_set_broadcast_address(dev->spi, rx_cfg->broadcast_address);
286 rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
290 ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
293 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
296 ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
299 ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
302 ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
305 ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
308 ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
312 /* packet format enable */
313 if (tx_cfg->enable_preamble == OPTION_ON) {
314 ret = rf69_set_preamble_length(dev->spi, tx_cfg->preamble_length);
318 ret = rf69_set_preamble_length(dev->spi, 0);
323 if (tx_cfg->enable_sync == OPTION_ON) {
324 ret = rf69_enable_sync(dev->spi);
328 ret = rf69_disable_sync(dev->spi);
333 if (tx_cfg->enable_length_byte == OPTION_ON) {
334 ret = rf69_set_packet_format(dev->spi, packetLengthVar);
338 ret = rf69_set_packet_format(dev->spi, packetLengthFix);
343 if (tx_cfg->enable_crc == OPTION_ON) {
344 ret = rf69_enable_crc(dev->spi);
348 ret = rf69_disable_crc(dev->spi);
353 /* configure sync, if enabled */
354 if (tx_cfg->enable_sync == OPTION_ON) {
355 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
358 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
366 /*-------------------------------------------------------------------------*/
369 pi433_start_rx(struct pi433_device *dev)
373 /* return without action, if no pending read request */
377 /* setup for receiving */
378 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
383 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
386 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
387 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
389 /* setup fifo level interrupt */
390 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
393 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
396 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
397 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
399 /* set module to receiving mode */
400 retval = rf69_set_mode(dev->spi, receive);
407 /*-------------------------------------------------------------------------*/
410 pi433_receive(void *data)
412 struct pi433_device *dev = data;
413 struct spi_device *spi = dev->spi;
414 int bytes_to_read, bytes_total;
417 dev->interrupt_rx_allowed = false;
419 /* wait for any tx to finish */
420 dev_dbg(dev->dev, "rx: going to wait for any tx to finish");
421 retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
423 /* wait was interrupted */
424 dev->interrupt_rx_allowed = true;
425 wake_up_interruptible(&dev->tx_wait_queue);
429 /* prepare status vars */
430 dev->free_in_fifo = FIFO_SIZE;
431 dev->rx_position = 0;
432 dev->rx_bytes_dropped = 0;
434 /* setup radio module to listen for something "in the air" */
435 retval = pi433_start_rx(dev);
439 /* now check RSSI, if low wait for getting high (RSSI interrupt) */
440 while (!rf69_get_flag(dev->spi, rssiExceededThreshold)) {
441 /* allow tx to interrupt us while waiting for high RSSI */
442 dev->interrupt_rx_allowed = true;
443 wake_up_interruptible(&dev->tx_wait_queue);
445 /* wait for RSSI level to become high */
446 dev_dbg(dev->dev, "rx: going to wait for high RSSI level");
447 retval = wait_event_interruptible(dev->rx_wait_queue,
448 rf69_get_flag(dev->spi,
449 rssiExceededThreshold));
450 if (retval) /* wait was interrupted */
452 dev->interrupt_rx_allowed = false;
454 /* cross check for ongoing tx */
459 /* configure payload ready irq */
460 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
463 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
464 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
466 /* fixed or unlimited length? */
467 if (dev->rx_cfg.fixed_message_length != 0) {
468 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
472 bytes_total = dev->rx_cfg.fixed_message_length;
473 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length", bytes_total);
475 bytes_total = dev->rx_buffer_size;
476 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read", bytes_total);
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", bytes_total);
495 /* address byte enabled? */
496 if (dev->rx_cfg.enable_address_filtering != filteringOff) {
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");
512 while (dev->rx_position < bytes_total) {
513 if (!rf69_get_flag(dev->spi, 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 - dev->rx_bytes_dropped;
524 bytes_to_read = bytes_total - dev->rx_position;
526 /* access the fifo */
527 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
528 bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
529 retval = rf69_read_fifo(spi,
530 &dev->rx_buffer[dev->rx_position],
532 if (retval) /* read failed */
535 dev->free_in_fifo += bytes_to_read;
537 /* adjust status vars */
538 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
539 dev->rx_bytes_dropped += bytes_to_read;
541 dev->rx_position += bytes_to_read;
544 /* rx done, wait was interrupted or error occurred */
546 dev->interrupt_rx_allowed = true;
547 if (rf69_set_mode(dev->spi, standby))
548 pr_err("rf69_set_mode(): radio module failed to go standby\n");
549 wake_up_interruptible(&dev->tx_wait_queue);
558 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");
571 wait_event_interruptible(device->tx_wait_queue,
572 (!kfifo_is_empty(&device->tx_fifo) ||
573 kthread_should_stop()));
574 if (kthread_should_stop())
577 /* get data from fifo in the following order:
582 mutex_lock(&device->tx_fifo_lock);
584 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
585 if (retval != sizeof(tx_cfg)) {
586 dev_dbg(device->dev, "reading tx_cfg from fifo failed: got %d byte(s), expected %d", retval, (unsigned int)sizeof(tx_cfg));
587 mutex_unlock(&device->tx_fifo_lock);
591 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
592 if (retval != sizeof(size_t)) {
593 dev_dbg(device->dev, "reading msg size from fifo failed: got %d, expected %d", retval, (unsigned int)sizeof(size_t));
594 mutex_unlock(&device->tx_fifo_lock);
598 /* use fixed message length, if requested */
599 if (tx_cfg.fixed_message_length != 0)
600 size = tx_cfg.fixed_message_length;
602 /* increase size, if len byte is requested */
603 if (tx_cfg.enable_length_byte == OPTION_ON)
606 /* increase size, if adr byte is requested */
607 if (tx_cfg.enable_address_byte == OPTION_ON)
611 memset(device->buffer, 0, size);
614 /* add length byte, if requested */
615 if (tx_cfg.enable_length_byte == OPTION_ON)
616 device->buffer[position++] = size - 1; /* according to spec length byte itself must be excluded from the length calculation */
618 /* add adr byte, if requested */
619 if (tx_cfg.enable_address_byte == OPTION_ON)
620 device->buffer[position++] = tx_cfg.address_byte;
622 /* finally get message data from fifo */
623 retval = kfifo_out(&device->tx_fifo, &device->buffer[position], sizeof(device->buffer) - position);
624 dev_dbg(device->dev, "read %d message byte(s) from fifo queue.", retval);
625 mutex_unlock(&device->tx_fifo_lock);
627 /* if rx is active, we need to interrupt the waiting for
628 * incoming telegrams, to be able to send something.
629 * We are only allowed, if currently no reception takes
630 * place otherwise we need to wait for the incoming telegram
633 wait_event_interruptible(device->tx_wait_queue,
634 !device->rx_active ||
635 device->interrupt_rx_allowed);
637 /* prevent race conditions
638 * irq will be reenabled after tx config is set
640 disable_irq(device->irq_num[DIO0]);
641 device->tx_active = true;
643 if (device->rx_active && !rx_interrupted) {
644 /* rx is currently waiting for a telegram;
645 * we need to set the radio module to standby
647 retval = rf69_set_mode(device->spi, standby);
650 rx_interrupted = true;
653 /* clear fifo, set fifo threshold, set payload length */
654 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
657 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
660 if (tx_cfg.enable_length_byte == OPTION_ON) {
661 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
665 retval = rf69_set_payload_length(spi, 0);
670 /* configure the rf chip */
671 retval = rf69_set_tx_cfg(device, &tx_cfg);
675 /* enable fifo level interrupt */
676 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
679 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
680 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
682 /* enable packet sent interrupt */
683 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
686 device->irq_state[DIO0] = DIO_PACKET_SENT;
687 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
688 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
690 /* enable transmission */
691 retval = rf69_set_mode(spi, transmit);
695 /* transfer this msg (and repetitions) to chip fifo */
696 device->free_in_fifo = FIFO_SIZE;
698 repetitions = tx_cfg.repetitions;
699 while ((repetitions > 0) && (size > position)) {
700 if ((size - position) > device->free_in_fifo) {
701 /* msg to big for fifo - take a part */
702 int temp = device->free_in_fifo;
703 device->free_in_fifo = 0;
705 &device->buffer[position],
709 /* msg fits into fifo - take all */
710 device->free_in_fifo -= size;
713 &device->buffer[position],
715 position = 0; /* reset for next repetition */
718 retval = wait_event_interruptible(device->fifo_wait_queue,
719 device->free_in_fifo > 0);
721 dev_dbg(device->dev, "ABORT\n");
726 /* we are done. Wait for packet to get sent */
727 dev_dbg(device->dev, "thread: wait for packet to get sent/fifo to be empty");
728 wait_event_interruptible(device->fifo_wait_queue,
729 device->free_in_fifo == FIFO_SIZE ||
730 kthread_should_stop());
731 if (kthread_should_stop())
732 dev_dbg(device->dev, "ABORT\n");
734 /* STOP_TRANSMISSION */
735 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.");
736 retval = rf69_set_mode(spi, standby);
740 /* everything sent? */
741 if (kfifo_is_empty(&device->tx_fifo)) {
743 if (rx_interrupted) {
744 rx_interrupted = false;
745 pi433_start_rx(device);
747 device->tx_active = false;
748 wake_up_interruptible(&device->rx_wait_queue);
753 /*-------------------------------------------------------------------------*/
756 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
758 struct pi433_instance *instance;
759 struct pi433_device *device;
763 /* check, whether internal buffer is big enough for requested size */
764 if (size > MAX_MSG_SIZE)
767 instance = filp->private_data;
768 device = instance->device;
770 /* just one read request at a time */
771 mutex_lock(&device->rx_lock);
772 if (device->rx_active) {
773 mutex_unlock(&device->rx_lock);
777 device->rx_active = true;
778 mutex_unlock(&device->rx_lock);
780 /* start receiving */
781 /* will block until something was received*/
782 device->rx_buffer_size = size;
783 bytes_received = pi433_receive(device);
786 mutex_lock(&device->rx_lock);
787 device->rx_active = false;
788 mutex_unlock(&device->rx_lock);
790 /* if read was successful copy to user space*/
791 if (bytes_received > 0) {
792 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
797 return bytes_received;
801 pi433_write(struct file *filp, const char __user *buf,
802 size_t count, loff_t *f_pos)
804 struct pi433_instance *instance;
805 struct pi433_device *device;
809 instance = filp->private_data;
810 device = instance->device;
812 /* check, whether internal buffer (tx thread) is big enough for requested size */
813 if (count > MAX_MSG_SIZE)
816 /* write the following sequence into fifo:
821 mutex_lock(&device->tx_fifo_lock);
822 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg, sizeof(instance->tx_cfg));
823 if (retval != sizeof(instance->tx_cfg))
826 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
827 if (retval != sizeof(size_t))
830 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
831 if (retval || copied != count)
834 mutex_unlock(&device->tx_fifo_lock);
837 wake_up_interruptible(&device->tx_wait_queue);
838 dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied);
843 dev_dbg(device->dev, "write to fifo failed: 0x%x", retval);
844 kfifo_reset(&device->tx_fifo); // TODO: maybe find a solution, not to discard already stored, valid entries
845 mutex_unlock(&device->tx_fifo_lock);
850 pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
853 struct pi433_instance *instance;
854 struct pi433_device *device;
855 void __user *argp = (void __user *)arg;
857 /* Check type and command number */
858 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
861 /* TODO? guard against device removal before, or while,
862 * we issue this ioctl. --> device_get()
864 instance = filp->private_data;
865 device = instance->device;
871 case PI433_IOC_RD_TX_CFG:
872 if (copy_to_user(argp, &instance->tx_cfg,
873 sizeof(struct pi433_tx_cfg)))
876 case PI433_IOC_WR_TX_CFG:
877 if (copy_from_user(&instance->tx_cfg, argp,
878 sizeof(struct pi433_tx_cfg)))
881 case PI433_IOC_RD_RX_CFG:
882 if (copy_to_user(argp, &device->rx_cfg,
883 sizeof(struct pi433_rx_cfg)))
886 case PI433_IOC_WR_RX_CFG:
887 mutex_lock(&device->rx_lock);
889 /* during pendig read request, change of config not allowed */
890 if (device->rx_active) {
891 mutex_unlock(&device->rx_lock);
895 if (copy_from_user(&device->rx_cfg, argp,
896 sizeof(struct pi433_rx_cfg))) {
897 mutex_unlock(&device->rx_lock);
901 mutex_unlock(&device->rx_lock);
912 pi433_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
914 return pi433_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
917 #define pi433_compat_ioctl NULL
918 #endif /* CONFIG_COMPAT */
920 /*-------------------------------------------------------------------------*/
922 static int pi433_open(struct inode *inode, struct file *filp)
924 struct pi433_device *device;
925 struct pi433_instance *instance;
927 mutex_lock(&minor_lock);
928 device = idr_find(&pi433_idr, iminor(inode));
929 mutex_unlock(&minor_lock);
931 pr_debug("device: minor %d unknown.\n", iminor(inode));
935 if (!device->rx_buffer) {
936 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
937 if (!device->rx_buffer)
942 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
944 kfree(device->rx_buffer);
945 device->rx_buffer = NULL;
949 /* setup instance data*/
950 instance->device = device;
951 instance->tx_cfg.bit_rate = 4711;
952 // TODO: fill instance->tx_cfg;
954 /* instance data as context */
955 filp->private_data = instance;
956 nonseekable_open(inode, filp);
961 static int pi433_release(struct inode *inode, struct file *filp)
963 struct pi433_instance *instance;
964 struct pi433_device *device;
966 instance = filp->private_data;
967 device = instance->device;
969 filp->private_data = NULL;
974 if (!device->users) {
975 kfree(device->rx_buffer);
976 device->rx_buffer = NULL;
984 /*-------------------------------------------------------------------------*/
986 static int setup_GPIOs(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, 0 /*GPIOD_IN*/);
1001 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1002 dev_dbg(&device->spi->dev, "Could not find entry for %s. Ignoring.", name);
1006 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1007 dev_dbg(&device->spi->dev, "%s is busy.", name);
1009 if (IS_ERR(device->gpiod[i])) {
1010 retval = PTR_ERR(device->gpiod[i]);
1011 /* release already allocated gpios */
1012 for (i--; i >= 0; i--) {
1013 free_irq(device->irq_num[i], device);
1014 gpiod_put(device->gpiod[i]);
1019 /* configure the pin */
1020 gpiod_unexport(device->gpiod[i]);
1021 retval = gpiod_direction_input(device->gpiod[i]);
1026 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1027 if (device->irq_num[i] < 0) {
1028 device->gpiod[i] = ERR_PTR(-EINVAL);//(struct gpio_desc *)device->irq_num[i];
1029 return device->irq_num[i];
1031 retval = request_irq(device->irq_num[i],
1040 dev_dbg(&device->spi->dev, "%s successfully configured", name);
1046 static void free_GPIOs(struct pi433_device *device)
1050 for (i = 0; i < NUM_DIO; i++) {
1051 /* check if gpiod is valid */
1052 if (IS_ERR(device->gpiod[i]))
1055 free_irq(device->irq_num[i], device);
1056 gpiod_put(device->gpiod[i]);
1060 static int pi433_get_minor(struct pi433_device *device)
1062 int retval = -ENOMEM;
1064 mutex_lock(&minor_lock);
1065 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1067 device->minor = retval;
1069 } else if (retval == -ENOSPC) {
1070 dev_err(&device->spi->dev, "too many pi433 devices\n");
1073 mutex_unlock(&minor_lock);
1077 static void pi433_free_minor(struct pi433_device *dev)
1079 mutex_lock(&minor_lock);
1080 idr_remove(&pi433_idr, dev->minor);
1081 mutex_unlock(&minor_lock);
1084 /*-------------------------------------------------------------------------*/
1086 static const struct file_operations pi433_fops = {
1087 .owner = THIS_MODULE,
1088 /* REVISIT switch to aio primitives, so that userspace
1089 * gets more complete API coverage. It'll simplify things
1090 * too, except for the locking.
1092 .write = pi433_write,
1094 .unlocked_ioctl = pi433_ioctl,
1095 .compat_ioctl = pi433_compat_ioctl,
1097 .release = pi433_release,
1098 .llseek = no_llseek,
1101 /*-------------------------------------------------------------------------*/
1103 static int pi433_probe(struct spi_device *spi)
1105 struct pi433_device *device;
1108 /* setup spi parameters */
1110 spi->bits_per_word = 8;
1111 /* spi->max_speed_hz = 10000000; 1MHz already set by device tree overlay */
1113 retval = spi_setup(spi);
1115 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1120 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed",
1121 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1123 /* Ping the chip by reading the version register */
1124 retval = spi_w8r8(spi, 0x10);
1130 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval);
1133 dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval);
1137 /* Allocate driver data */
1138 device = kzalloc(sizeof(*device), GFP_KERNEL);
1142 /* Initialize the driver data */
1144 device->rx_active = false;
1145 device->tx_active = false;
1146 device->interrupt_rx_allowed = false;
1148 /* init wait queues */
1149 init_waitqueue_head(&device->tx_wait_queue);
1150 init_waitqueue_head(&device->rx_wait_queue);
1151 init_waitqueue_head(&device->fifo_wait_queue);
1154 INIT_KFIFO(device->tx_fifo);
1156 /* init mutexes and locks */
1157 mutex_init(&device->tx_fifo_lock);
1158 mutex_init(&device->rx_lock);
1160 /* setup GPIO (including irq_handler) for the different DIOs */
1161 retval = setup_GPIOs(device);
1163 dev_dbg(&spi->dev, "setup of GPIOs failed");
1167 /* setup the radio module */
1168 retval = rf69_set_mode(spi, standby);
1171 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1174 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1177 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1180 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1183 retval = rf69_set_output_power_level(spi, 13);
1186 retval = rf69_set_antenna_impedance(spi, fiftyOhm);
1190 /* determ minor number */
1191 retval = pi433_get_minor(device);
1193 dev_dbg(&spi->dev, "get of minor number failed");
1198 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1199 device->dev = device_create(pi433_class,
1205 if (IS_ERR(device->dev)) {
1206 pr_err("pi433: device register failed\n");
1207 retval = PTR_ERR(device->dev);
1208 goto device_create_failed;
1210 dev_dbg(device->dev,
1211 "created device for major %d, minor %d\n",
1216 /* start tx thread */
1217 device->tx_task_struct = kthread_run(pi433_tx_thread,
1221 if (IS_ERR(device->tx_task_struct)) {
1222 dev_dbg(device->dev, "start of send thread failed");
1223 goto send_thread_failed;
1227 device->cdev = cdev_alloc();
1228 device->cdev->owner = THIS_MODULE;
1229 cdev_init(device->cdev, &pi433_fops);
1230 retval = cdev_add(device->cdev, device->devt, 1);
1232 dev_dbg(device->dev, "register of cdev failed");
1237 spi_set_drvdata(spi, device);
1242 kthread_stop(device->tx_task_struct);
1244 device_destroy(pi433_class, device->devt);
1245 device_create_failed:
1246 pi433_free_minor(device);
1255 static int pi433_remove(struct spi_device *spi)
1257 struct pi433_device *device = spi_get_drvdata(spi);
1262 /* make sure ops on existing fds can abort cleanly */
1265 kthread_stop(device->tx_task_struct);
1267 device_destroy(pi433_class, device->devt);
1269 cdev_del(device->cdev);
1271 pi433_free_minor(device);
1273 if (device->users == 0)
1279 static const struct of_device_id pi433_dt_ids[] = {
1280 { .compatible = "Smarthome-Wolf,pi433" },
1284 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1286 static struct spi_driver pi433_spi_driver = {
1289 .owner = THIS_MODULE,
1290 .of_match_table = of_match_ptr(pi433_dt_ids),
1292 .probe = pi433_probe,
1293 .remove = pi433_remove,
1295 /* NOTE: suspend/resume methods are not necessary here.
1296 * We don't do anything except pass the requests to/from
1297 * the underlying controller. The refrigerator handles
1298 * most issues; the controller driver handles the rest.
1302 /*-------------------------------------------------------------------------*/
1304 static int __init pi433_init(void)
1308 /* If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1309 * work stable - risk of buffer overflow
1311 if (MAX_MSG_SIZE < FIFO_SIZE)
1314 /* Claim device numbers. Then register a class
1315 * that will key udev/mdev to add/remove /dev nodes. Last, register
1316 * Last, register the driver which manages those device numbers.
1318 status = alloc_chrdev_region(&pi433_dev, 0 /*firstminor*/, N_PI433_MINORS /*count*/, "pi433" /*name*/);
1322 pi433_class = class_create(THIS_MODULE, "pi433");
1323 if (IS_ERR(pi433_class)) {
1324 unregister_chrdev(MAJOR(pi433_dev),
1325 pi433_spi_driver.driver.name);
1326 return PTR_ERR(pi433_class);
1329 status = spi_register_driver(&pi433_spi_driver);
1331 class_destroy(pi433_class);
1332 unregister_chrdev(MAJOR(pi433_dev),
1333 pi433_spi_driver.driver.name);
1339 module_init(pi433_init);
1341 static void __exit pi433_exit(void)
1343 spi_unregister_driver(&pi433_spi_driver);
1344 class_destroy(pi433_class);
1345 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1347 module_exit(pi433_exit);
1349 MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1350 MODULE_DESCRIPTION("Driver for Pi433");
1351 MODULE_LICENSE("GPL");
1352 MODULE_ALIAS("spi:pi433");