*: convert stream-like files from nonseekable_open -> stream_open
[sfrench/cifs-2.6.git] / drivers / staging / pi433 / pi433_if.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * userspace interface for pi433 radio module
4  *
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.
8  *
9  * If needed, this driver could be extended, to also support other
10  * devices, basing on HopeRfs rf69.
11  *
12  * The driver can also be extended, to support other modules of
13  * HopeRf with a similar interace - e. g. RFM69HCW, RFM12, RFM95, ...
14  *
15  * Copyright (C) 2016 Wolf-Entwicklungen
16  *      Marcus Wolf <linux@wolf-entwicklungen.de>
17  */
18
19 #undef DEBUG
20
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>
26 #include <linux/fs.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>
33 #include <linux/of.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>
41 #ifdef CONFIG_COMPAT
42 #include <linux/compat.h>
43 #endif
44
45 #include "pi433_if.h"
46 #include "rf69.h"
47
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  */
51 #define NUM_DIO                         2
52
53 static dev_t pi433_dev;
54 static DEFINE_IDR(pi433_idr);
55 static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
56
57 static struct class *pi433_class; /* mainly for udev to create /dev/pi433 */
58
59 /*
60  * tx config is instance specific
61  * so with each open a new tx config struct is needed
62  */
63 /*
64  * rx config is device specific
65  * so we have just one rx config, ebedded in device struct
66  */
67 struct pi433_device {
68         /* device handling related values */
69         dev_t                   devt;
70         int                     minor;
71         struct device           *dev;
72         struct cdev             *cdev;
73         struct spi_device       *spi;
74
75         /* irq related values */
76         struct gpio_desc        *gpiod[NUM_DIO];
77         int                     irq_num[NUM_DIO];
78         u8                      irq_state[NUM_DIO];
79
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;
85         u8                      free_in_fifo;
86         char                    buffer[MAX_MSG_SIZE];
87
88         /* rx related values */
89         struct pi433_rx_cfg     rx_cfg;
90         u8                      *rx_buffer;
91         unsigned int            rx_buffer_size;
92         u32                     rx_bytes_to_drop;
93         u32                     rx_bytes_dropped;
94         unsigned int            rx_position;
95         struct mutex            rx_lock;
96         wait_queue_head_t       rx_wait_queue;
97
98         /* fifo wait queue */
99         struct task_struct      *fifo_task_struct;
100         wait_queue_head_t       fifo_wait_queue;
101
102         /* flags */
103         bool                    rx_active;
104         bool                    tx_active;
105         bool                    interrupt_rx_allowed;
106 };
107
108 struct pi433_instance {
109         struct pi433_device     *device;
110         struct pi433_tx_cfg     tx_cfg;
111 };
112
113 /*-------------------------------------------------------------------------*/
114
115 /* GPIO interrupt handlers */
116 static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
117 {
118         struct pi433_device *device = dev_id;
119
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);
131         }
132
133         return IRQ_HANDLED;
134 }
135
136 static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
137 {
138         struct pi433_device *device = dev_id;
139
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;
145                 else
146                         device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
147         }
148         dev_dbg(device->dev,
149                 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
150         wake_up_interruptible(&device->fifo_wait_queue);
151
152         return IRQ_HANDLED;
153 }
154
155 /*-------------------------------------------------------------------------*/
156
157 static int
158 rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
159 {
160         int ret;
161         int payload_length;
162
163         /* receiver config */
164         ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
165         if (ret < 0)
166                 return ret;
167         ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
168         if (ret < 0)
169                 return ret;
170         ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
171         if (ret < 0)
172                 return ret;
173         ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
174         if (ret < 0)
175                 return ret;
176         ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
177         if (ret < 0)
178                 return ret;
179         ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
180         if (ret < 0)
181                 return ret;
182         ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
183                                  rx_cfg->bw_exponent);
184         if (ret < 0)
185                 return ret;
186         ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
187                                             rx_cfg->bw_exponent);
188         if (ret < 0)
189                 return ret;
190         ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
191         if (ret < 0)
192                 return ret;
193
194         dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
195
196         /* packet config */
197         /* enable */
198         if (rx_cfg->enable_sync == OPTION_ON) {
199                 ret = rf69_enable_sync(dev->spi);
200                 if (ret < 0)
201                         return ret;
202
203                 ret = rf69_set_fifo_fill_condition(dev->spi,
204                                                    after_sync_interrupt);
205                 if (ret < 0)
206                         return ret;
207         } else {
208                 ret = rf69_disable_sync(dev->spi);
209                 if (ret < 0)
210                         return ret;
211
212                 ret = rf69_set_fifo_fill_condition(dev->spi, always);
213                 if (ret < 0)
214                         return ret;
215         }
216         if (rx_cfg->enable_length_byte == OPTION_ON) {
217                 ret = rf69_set_packet_format(dev->spi, packet_length_var);
218                 if (ret < 0)
219                         return ret;
220         } else {
221                 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
222                 if (ret < 0)
223                         return ret;
224         }
225         ret = rf69_set_address_filtering(dev->spi,
226                                          rx_cfg->enable_address_filtering);
227         if (ret < 0)
228                 return ret;
229
230         if (rx_cfg->enable_crc == OPTION_ON) {
231                 ret = rf69_enable_crc(dev->spi);
232                 if (ret < 0)
233                         return ret;
234         } else {
235                 ret = rf69_disable_crc(dev->spi);
236                 if (ret < 0)
237                         return ret;
238         }
239
240         /* lengths */
241         ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
242         if (ret < 0)
243                 return ret;
244         if (rx_cfg->enable_length_byte == OPTION_ON) {
245                 ret = rf69_set_payload_length(dev->spi, 0xff);
246                 if (ret < 0)
247                         return ret;
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)
251                         payload_length++;
252                 if (rx_cfg->enable_address_filtering != filtering_off)
253                         payload_length++;
254                 ret = rf69_set_payload_length(dev->spi, payload_length);
255                 if (ret < 0)
256                         return ret;
257         } else {
258                 ret = rf69_set_payload_length(dev->spi, 0);
259                 if (ret < 0)
260                         return ret;
261         }
262
263         /* values */
264         if (rx_cfg->enable_sync == OPTION_ON) {
265                 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
266                 if (ret < 0)
267                         return ret;
268         }
269         if (rx_cfg->enable_address_filtering != filtering_off) {
270                 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
271                 if (ret < 0)
272                         return ret;
273                 ret = rf69_set_broadcast_address(dev->spi,
274                                                  rx_cfg->broadcast_address);
275                 if (ret < 0)
276                         return ret;
277         }
278
279         return 0;
280 }
281
282 static int
283 rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
284 {
285         int ret;
286
287         ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
288         if (ret < 0)
289                 return ret;
290         ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
291         if (ret < 0)
292                 return ret;
293         ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
294         if (ret < 0)
295                 return ret;
296         ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
297         if (ret < 0)
298                 return ret;
299         ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
300         if (ret < 0)
301                 return ret;
302         ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
303         if (ret < 0)
304                 return ret;
305         ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
306         if (ret < 0)
307                 return ret;
308
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);
313                 if (ret < 0)
314                         return ret;
315         } else {
316                 ret = rf69_set_preamble_length(dev->spi, 0);
317                 if (ret < 0)
318                         return ret;
319         }
320
321         if (tx_cfg->enable_sync == OPTION_ON) {
322                 ret = rf69_enable_sync(dev->spi);
323                 if (ret < 0)
324                         return ret;
325         } else {
326                 ret = rf69_disable_sync(dev->spi);
327                 if (ret < 0)
328                         return ret;
329         }
330
331         if (tx_cfg->enable_length_byte == OPTION_ON) {
332                 ret = rf69_set_packet_format(dev->spi, packet_length_var);
333                 if (ret < 0)
334                         return ret;
335         } else {
336                 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
337                 if (ret < 0)
338                         return ret;
339         }
340
341         if (tx_cfg->enable_crc == OPTION_ON) {
342                 ret = rf69_enable_crc(dev->spi);
343                 if (ret < 0)
344                         return ret;
345         } else {
346                 ret = rf69_disable_crc(dev->spi);
347                 if (ret < 0)
348                         return ret;
349         }
350
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);
354                 if (ret < 0)
355                         return ret;
356                 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
357                 if (ret < 0)
358                         return ret;
359         }
360
361         return 0;
362 }
363
364 /*-------------------------------------------------------------------------*/
365
366 static int
367 pi433_start_rx(struct pi433_device *dev)
368 {
369         int retval;
370
371         /* return without action, if no pending read request */
372         if (!dev->rx_active)
373                 return 0;
374
375         /* setup for receiving */
376         retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
377         if (retval)
378                 return retval;
379
380         /* setup rssi irq */
381         retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
382         if (retval < 0)
383                 return retval;
384         dev->irq_state[DIO0] = DIO_RSSI_DIO0;
385         irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
386
387         /* setup fifo level interrupt */
388         retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
389         if (retval < 0)
390                 return retval;
391         retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
392         if (retval < 0)
393                 return retval;
394         dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
395         irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
396
397         /* set module to receiving mode */
398         retval = rf69_set_mode(dev->spi, receive);
399         if (retval < 0)
400                 return retval;
401
402         return 0;
403 }
404
405 /*-------------------------------------------------------------------------*/
406
407 static int
408 pi433_receive(void *data)
409 {
410         struct pi433_device *dev = data;
411         struct spi_device *spi = dev->spi;
412         int bytes_to_read, bytes_total;
413         int retval;
414
415         dev->interrupt_rx_allowed = false;
416
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);
420         if (retval) {
421                 /* wait was interrupted */
422                 dev->interrupt_rx_allowed = true;
423                 wake_up_interruptible(&dev->tx_wait_queue);
424                 return retval;
425         }
426
427         /* prepare status vars */
428         dev->free_in_fifo = FIFO_SIZE;
429         dev->rx_position = 0;
430         dev->rx_bytes_dropped = 0;
431
432         /* setup radio module to listen for something "in the air" */
433         retval = pi433_start_rx(dev);
434         if (retval)
435                 return retval;
436
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);
442
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 */
449                         goto abort;
450                 dev->interrupt_rx_allowed = false;
451
452                 /* cross check for ongoing tx */
453                 if (!dev->tx_active)
454                         break;
455         }
456
457         /* configure payload ready irq */
458         retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
459         if (retval < 0)
460                 goto abort;
461         dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
462         irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
463
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) {
467                         retval = -1;
468                         goto abort;
469                 }
470                 bytes_total = dev->rx_cfg.fixed_message_length;
471                 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length",
472                         bytes_total);
473         } else {
474                 bytes_total = dev->rx_buffer_size;
475                 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read",
476                         bytes_total);
477         }
478
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 */
484                         goto abort;
485
486                 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
487                 if (bytes_total > dev->rx_buffer_size) {
488                         retval = -1;
489                         goto abort;
490                 }
491                 dev->free_in_fifo++;
492                 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte",
493                         bytes_total);
494         }
495
496         /* address byte enabled? */
497         if (dev->rx_cfg.enable_address_filtering != filtering_off) {
498                 u8 dummy;
499
500                 bytes_total--;
501
502                 retval = wait_event_interruptible(dev->fifo_wait_queue,
503                                                   dev->free_in_fifo < FIFO_SIZE);
504                 if (retval) /* wait was interrupted */
505                         goto abort;
506
507                 rf69_read_fifo(spi, &dummy, 1);
508                 dev->free_in_fifo++;
509                 dev_dbg(dev->dev, "rx: address byte stripped off");
510         }
511
512         /* get payload */
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 */
518                                 goto abort;
519                 }
520
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;
525                 else
526                         bytes_to_read = bytes_total - dev->rx_position;
527
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],
533                                         bytes_to_read);
534                 if (retval) /* read failed */
535                         goto abort;
536
537                 dev->free_in_fifo += bytes_to_read;
538
539                 /* adjust status vars */
540                 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
541                         dev->rx_bytes_dropped += bytes_to_read;
542                 else
543                         dev->rx_position += bytes_to_read;
544         }
545
546         /* rx done, wait was interrupted or error occurred */
547 abort:
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);
552
553         if (retval)
554                 return retval;
555         else
556                 return bytes_total;
557 }
558
559 static int
560 pi433_tx_thread(void *data)
561 {
562         struct pi433_device *device = data;
563         struct spi_device *spi = device->spi;
564         struct pi433_tx_cfg tx_cfg;
565         size_t size;
566         bool   rx_interrupted = false;
567         int    position, repetitions;
568         int    retval;
569
570         while (1) {
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())
577                         return 0;
578
579                 /*
580                  * get data from fifo in the following order:
581                  * - tx_cfg
582                  * - size of message
583                  * - message
584                  */
585                 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
586                 if (retval != sizeof(tx_cfg)) {
587                         dev_dbg(device->dev,
588                                 "reading tx_cfg from fifo failed: got %d byte(s), expected %d",
589                                 retval, (unsigned int)sizeof(tx_cfg));
590                         continue;
591                 }
592
593                 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
594                 if (retval != sizeof(size_t)) {
595                         dev_dbg(device->dev,
596                                 "reading msg size from fifo failed: got %d, expected %d",
597                                 retval, (unsigned int)sizeof(size_t));
598                         continue;
599                 }
600
601                 /* use fixed message length, if requested */
602                 if (tx_cfg.fixed_message_length != 0)
603                         size = tx_cfg.fixed_message_length;
604
605                 /* increase size, if len byte is requested */
606                 if (tx_cfg.enable_length_byte == OPTION_ON)
607                         size++;
608
609                 /* increase size, if adr byte is requested */
610                 if (tx_cfg.enable_address_byte == OPTION_ON)
611                         size++;
612
613                 /* prime buffer */
614                 memset(device->buffer, 0, size);
615                 position = 0;
616
617                 /* add length byte, if requested */
618                 if (tx_cfg.enable_length_byte  == OPTION_ON)
619                         /*
620                          * according to spec, length byte itself must be
621                          * excluded from the length calculation
622                          */
623                         device->buffer[position++] = size - 1;
624
625                 /* add adr byte, if requested */
626                 if (tx_cfg.enable_address_byte == OPTION_ON)
627                         device->buffer[position++] = tx_cfg.address_byte;
628
629                 /* finally get message data from fifo */
630                 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
631                                    sizeof(device->buffer) - position);
632                 dev_dbg(device->dev,
633                         "read %d message byte(s) from fifo queue.", retval);
634
635                 /*
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
640                  * to finish
641                  */
642                 wait_event_interruptible(device->tx_wait_queue,
643                                          !device->rx_active ||
644                                           device->interrupt_rx_allowed);
645
646                 /*
647                  * prevent race conditions
648                  * irq will be reenabled after tx config is set
649                  */
650                 disable_irq(device->irq_num[DIO0]);
651                 device->tx_active = true;
652
653                 if (device->rx_active && !rx_interrupted) {
654                         /*
655                          * rx is currently waiting for a telegram;
656                          * we need to set the radio module to standby
657                          */
658                         retval = rf69_set_mode(device->spi, standby);
659                         if (retval < 0)
660                                 return retval;
661                         rx_interrupted = true;
662                 }
663
664                 /* clear fifo, set fifo threshold, set payload length */
665                 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
666                 if (retval < 0)
667                         return retval;
668                 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
669                 if (retval < 0)
670                         return retval;
671                 if (tx_cfg.enable_length_byte == OPTION_ON) {
672                         retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
673                         if (retval < 0)
674                                 return retval;
675                 } else {
676                         retval = rf69_set_payload_length(spi, 0);
677                         if (retval < 0)
678                                 return retval;
679                 }
680
681                 /* configure the rf chip */
682                 retval = rf69_set_tx_cfg(device, &tx_cfg);
683                 if (retval < 0)
684                         return retval;
685
686                 /* enable fifo level interrupt */
687                 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
688                 if (retval < 0)
689                         return retval;
690                 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
691                 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
692
693                 /* enable packet sent interrupt */
694                 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
695                 if (retval < 0)
696                         return retval;
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 */
700
701                 /* enable transmission */
702                 retval = rf69_set_mode(spi, transmit);
703                 if (retval < 0)
704                         return retval;
705
706                 /* transfer this msg (and repetitions) to chip fifo */
707                 device->free_in_fifo = FIFO_SIZE;
708                 position = 0;
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;
714
715                                 device->free_in_fifo = 0;
716                                 rf69_write_fifo(spi,
717                                                 &device->buffer[position],
718                                                 write_size);
719                                 position += write_size;
720                         } else {
721                                 /* msg fits into fifo - take all */
722                                 device->free_in_fifo -= size;
723                                 repetitions--;
724                                 rf69_write_fifo(spi,
725                                                 &device->buffer[position],
726                                                 (size - position));
727                                 position = 0; /* reset for next repetition */
728                         }
729
730                         retval = wait_event_interruptible(device->fifo_wait_queue,
731                                                           device->free_in_fifo > 0);
732                         if (retval) {
733                                 dev_dbg(device->dev, "ABORT\n");
734                                 goto abort;
735                         }
736                 }
737
738                 /* we are done. Wait for packet to get sent */
739                 dev_dbg(device->dev,
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");
746
747                 /* STOP_TRANSMISSION */
748                 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.");
749                 retval = rf69_set_mode(spi, standby);
750                 if (retval < 0)
751                         return retval;
752
753                 /* everything sent? */
754                 if (kfifo_is_empty(&device->tx_fifo)) {
755 abort:
756                         if (rx_interrupted) {
757                                 rx_interrupted = false;
758                                 pi433_start_rx(device);
759                         }
760                         device->tx_active = false;
761                         wake_up_interruptible(&device->rx_wait_queue);
762                 }
763         }
764 }
765
766 /*-------------------------------------------------------------------------*/
767
768 static ssize_t
769 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
770 {
771         struct pi433_instance   *instance;
772         struct pi433_device     *device;
773         int                     bytes_received;
774         ssize_t                 retval;
775
776         /* check, whether internal buffer is big enough for requested size */
777         if (size > MAX_MSG_SIZE)
778                 return -EMSGSIZE;
779
780         instance = filp->private_data;
781         device = instance->device;
782
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);
787                 return -EAGAIN;
788         }
789
790         device->rx_active = true;
791         mutex_unlock(&device->rx_lock);
792
793         /* start receiving */
794         /* will block until something was received*/
795         device->rx_buffer_size = size;
796         bytes_received = pi433_receive(device);
797
798         /* release rx */
799         mutex_lock(&device->rx_lock);
800         device->rx_active = false;
801         mutex_unlock(&device->rx_lock);
802
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);
806                 if (retval)
807                         return -EFAULT;
808         }
809
810         return bytes_received;
811 }
812
813 static ssize_t
814 pi433_write(struct file *filp, const char __user *buf,
815             size_t count, loff_t *f_pos)
816 {
817         struct pi433_instance   *instance;
818         struct pi433_device     *device;
819         int                     retval;
820         unsigned int            required, available, copied;
821
822         instance = filp->private_data;
823         device = instance->device;
824
825         /*
826          * check, whether internal buffer (tx thread) is big enough
827          * for requested size
828          */
829         if (count > MAX_MSG_SIZE)
830                 return -EMSGSIZE;
831
832         /*
833          * write the following sequence into fifo:
834          * - tx_cfg
835          * - size of message
836          * - message
837          */
838         mutex_lock(&device->tx_fifo_lock);
839
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);
846                 return -EAGAIN;
847         }
848
849         retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
850                           sizeof(instance->tx_cfg));
851         if (retval != sizeof(instance->tx_cfg))
852                 goto abort;
853
854         retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
855         if (retval != sizeof(size_t))
856                 goto abort;
857
858         retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
859         if (retval || copied != count)
860                 goto abort;
861
862         mutex_unlock(&device->tx_fifo_lock);
863
864         /* start transfer */
865         wake_up_interruptible(&device->tx_wait_queue);
866         dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied);
867
868         return copied;
869
870 abort:
871         dev_warn(device->dev,
872                  "write to fifo failed, non recoverable: 0x%x", retval);
873         mutex_unlock(&device->tx_fifo_lock);
874         return -EAGAIN;
875 }
876
877 static long
878 pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
879 {
880         int                     retval = 0;
881         struct pi433_instance   *instance;
882         struct pi433_device     *device;
883         struct pi433_tx_cfg     tx_cfg;
884         void __user *argp = (void __user *)arg;
885
886         /* Check type and command number */
887         if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
888                 return -ENOTTY;
889
890         instance = filp->private_data;
891         device = instance->device;
892
893         if (!device)
894                 return -ESHUTDOWN;
895
896         switch (cmd) {
897         case PI433_IOC_RD_TX_CFG:
898                 if (copy_to_user(argp, &instance->tx_cfg,
899                                  sizeof(struct pi433_tx_cfg)))
900                         return -EFAULT;
901                 break;
902         case PI433_IOC_WR_TX_CFG:
903                 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
904                         return -EFAULT;
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);
908                 break;
909         case PI433_IOC_RD_RX_CFG:
910                 if (copy_to_user(argp, &device->rx_cfg,
911                                  sizeof(struct pi433_rx_cfg)))
912                         return -EFAULT;
913                 break;
914         case PI433_IOC_WR_RX_CFG:
915                 mutex_lock(&device->rx_lock);
916
917                 /* during pendig read request, change of config not allowed */
918                 if (device->rx_active) {
919                         mutex_unlock(&device->rx_lock);
920                         return -EAGAIN;
921                 }
922
923                 if (copy_from_user(&device->rx_cfg, argp,
924                                    sizeof(struct pi433_rx_cfg))) {
925                         mutex_unlock(&device->rx_lock);
926                         return -EFAULT;
927                 }
928
929                 mutex_unlock(&device->rx_lock);
930                 break;
931         default:
932                 retval = -EINVAL;
933         }
934
935         return retval;
936 }
937
938 #ifdef CONFIG_COMPAT
939 static long
940 pi433_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
941 {
942         return pi433_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
943 }
944 #else
945 #define pi433_compat_ioctl NULL
946 #endif /* CONFIG_COMPAT */
947
948 /*-------------------------------------------------------------------------*/
949
950 static int pi433_open(struct inode *inode, struct file *filp)
951 {
952         struct pi433_device     *device;
953         struct pi433_instance   *instance;
954
955         mutex_lock(&minor_lock);
956         device = idr_find(&pi433_idr, iminor(inode));
957         mutex_unlock(&minor_lock);
958         if (!device) {
959                 pr_debug("device: minor %d unknown.\n", iminor(inode));
960                 return -ENODEV;
961         }
962
963         instance = kzalloc(sizeof(*instance), GFP_KERNEL);
964         if (!instance)
965                 return -ENOMEM;
966
967         /* setup instance data*/
968         instance->device = device;
969         instance->tx_cfg.bit_rate = 4711;
970         // TODO: fill instance->tx_cfg;
971
972         /* instance data as context */
973         filp->private_data = instance;
974         stream_open(inode, filp);
975
976         return 0;
977 }
978
979 static int pi433_release(struct inode *inode, struct file *filp)
980 {
981         struct pi433_instance   *instance;
982
983         instance = filp->private_data;
984         kfree(instance);
985         filp->private_data = NULL;
986
987         return 0;
988 }
989
990 /*-------------------------------------------------------------------------*/
991
992 static int setup_gpio(struct pi433_device *device)
993 {
994         char    name[5];
995         int     retval;
996         int     i;
997         const irq_handler_t DIO_irq_handler[NUM_DIO] = {
998                 DIO0_irq_handler,
999                 DIO1_irq_handler
1000         };
1001
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,
1006                                              0 /*GPIOD_IN*/);
1007
1008                 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1009                         dev_dbg(&device->spi->dev,
1010                                 "Could not find entry for %s. Ignoring.", name);
1011                         continue;
1012                 }
1013
1014                 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1015                         dev_dbg(&device->spi->dev, "%s is busy.", name);
1016
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]);
1023                         }
1024                         return retval;
1025                 }
1026
1027                 /* configure the pin */
1028                 gpiod_unexport(device->gpiod[i]);
1029                 retval = gpiod_direction_input(device->gpiod[i]);
1030                 if (retval)
1031                         return retval;
1032
1033                 /* configure irq */
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];
1038                 }
1039                 retval = request_irq(device->irq_num[i],
1040                                      DIO_irq_handler[i],
1041                                      0, /* flags */
1042                                      name,
1043                                      device);
1044
1045                 if (retval)
1046                         return retval;
1047
1048                 dev_dbg(&device->spi->dev, "%s successfully configured", name);
1049         }
1050
1051         return 0;
1052 }
1053
1054 static void free_gpio(struct pi433_device *device)
1055 {
1056         int i;
1057
1058         for (i = 0; i < NUM_DIO; i++) {
1059                 /* check if gpiod is valid */
1060                 if (IS_ERR(device->gpiod[i]))
1061                         continue;
1062
1063                 free_irq(device->irq_num[i], device);
1064                 gpiod_put(device->gpiod[i]);
1065         }
1066 }
1067
1068 static int pi433_get_minor(struct pi433_device *device)
1069 {
1070         int retval = -ENOMEM;
1071
1072         mutex_lock(&minor_lock);
1073         retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1074         if (retval >= 0) {
1075                 device->minor = retval;
1076                 retval = 0;
1077         } else if (retval == -ENOSPC) {
1078                 dev_err(&device->spi->dev, "too many pi433 devices\n");
1079                 retval = -EINVAL;
1080         }
1081         mutex_unlock(&minor_lock);
1082         return retval;
1083 }
1084
1085 static void pi433_free_minor(struct pi433_device *dev)
1086 {
1087         mutex_lock(&minor_lock);
1088         idr_remove(&pi433_idr, dev->minor);
1089         mutex_unlock(&minor_lock);
1090 }
1091
1092 /*-------------------------------------------------------------------------*/
1093
1094 static const struct file_operations pi433_fops = {
1095         .owner =        THIS_MODULE,
1096         /*
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.
1100          */
1101         .write =        pi433_write,
1102         .read =         pi433_read,
1103         .unlocked_ioctl = pi433_ioctl,
1104         .compat_ioctl = pi433_compat_ioctl,
1105         .open =         pi433_open,
1106         .release =      pi433_release,
1107         .llseek =       no_llseek,
1108 };
1109
1110 /*-------------------------------------------------------------------------*/
1111
1112 static int pi433_probe(struct spi_device *spi)
1113 {
1114         struct pi433_device     *device;
1115         int                     retval;
1116
1117         /* setup spi parameters */
1118         spi->mode = 0x00;
1119         spi->bits_per_word = 8;
1120         /*
1121          * spi->max_speed_hz = 10000000;
1122          * 1MHz already set by device tree overlay
1123          */
1124
1125         retval = spi_setup(spi);
1126         if (retval) {
1127                 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1128                 return retval;
1129         }
1130
1131         dev_dbg(&spi->dev,
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);
1134
1135         /* Ping the chip by reading the version register */
1136         retval = spi_w8r8(spi, 0x10);
1137         if (retval < 0)
1138                 return retval;
1139
1140         switch (retval) {
1141         case 0x24:
1142                 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval);
1143                 break;
1144         default:
1145                 dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval);
1146                 return -ENODEV;
1147         }
1148
1149         /* Allocate driver data */
1150         device = kzalloc(sizeof(*device), GFP_KERNEL);
1151         if (!device)
1152                 return -ENOMEM;
1153
1154         /* Initialize the driver data */
1155         device->spi = spi;
1156         device->rx_active = false;
1157         device->tx_active = false;
1158         device->interrupt_rx_allowed = false;
1159
1160         /* init rx buffer */
1161         device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1162         if (!device->rx_buffer) {
1163                 retval = -ENOMEM;
1164                 goto RX_failed;
1165         }
1166
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);
1171
1172         /* init fifo */
1173         INIT_KFIFO(device->tx_fifo);
1174
1175         /* init mutexes and locks */
1176         mutex_init(&device->tx_fifo_lock);
1177         mutex_init(&device->rx_lock);
1178
1179         /* setup GPIO (including irq_handler) for the different DIOs */
1180         retval = setup_gpio(device);
1181         if (retval) {
1182                 dev_dbg(&spi->dev, "setup of GPIOs failed");
1183                 goto GPIO_failed;
1184         }
1185
1186         /* setup the radio module */
1187         retval = rf69_set_mode(spi, standby);
1188         if (retval < 0)
1189                 goto minor_failed;
1190         retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1191         if (retval < 0)
1192                 goto minor_failed;
1193         retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1194         if (retval < 0)
1195                 goto minor_failed;
1196         retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1197         if (retval < 0)
1198                 goto minor_failed;
1199         retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1200         if (retval < 0)
1201                 goto minor_failed;
1202         retval = rf69_set_output_power_level(spi, 13);
1203         if (retval < 0)
1204                 goto minor_failed;
1205         retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1206         if (retval < 0)
1207                 goto minor_failed;
1208
1209         /* determ minor number */
1210         retval = pi433_get_minor(device);
1211         if (retval) {
1212                 dev_dbg(&spi->dev, "get of minor number failed");
1213                 goto minor_failed;
1214         }
1215
1216         /* create device */
1217         device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1218         device->dev = device_create(pi433_class,
1219                                     &spi->dev,
1220                                     device->devt,
1221                                     device,
1222                                     "pi433.%d",
1223                                     device->minor);
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;
1228         } else {
1229                 dev_dbg(device->dev,
1230                         "created device for major %d, minor %d\n",
1231                         MAJOR(pi433_dev),
1232                         device->minor);
1233         }
1234
1235         /* start tx thread */
1236         device->tx_task_struct = kthread_run(pi433_tx_thread,
1237                                              device,
1238                                              "pi433.%d_tx_task",
1239                                              device->minor);
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;
1244         }
1245
1246         /* create cdev */
1247         device->cdev = cdev_alloc();
1248         if (!device->cdev) {
1249                 dev_dbg(device->dev, "allocation of cdev failed");
1250                 goto cdev_failed;
1251         }
1252         device->cdev->owner = THIS_MODULE;
1253         cdev_init(device->cdev, &pi433_fops);
1254         retval = cdev_add(device->cdev, device->devt, 1);
1255         if (retval) {
1256                 dev_dbg(device->dev, "register of cdev failed");
1257                 goto del_cdev;
1258         }
1259
1260         /* spi setup */
1261         spi_set_drvdata(spi, device);
1262
1263         return 0;
1264
1265 del_cdev:
1266         cdev_del(device->cdev);
1267 cdev_failed:
1268         kthread_stop(device->tx_task_struct);
1269 send_thread_failed:
1270         device_destroy(pi433_class, device->devt);
1271 device_create_failed:
1272         pi433_free_minor(device);
1273 minor_failed:
1274         free_gpio(device);
1275 GPIO_failed:
1276         kfree(device->rx_buffer);
1277 RX_failed:
1278         kfree(device);
1279
1280         return retval;
1281 }
1282
1283 static int pi433_remove(struct spi_device *spi)
1284 {
1285         struct pi433_device     *device = spi_get_drvdata(spi);
1286
1287         /* free GPIOs */
1288         free_gpio(device);
1289
1290         /* make sure ops on existing fds can abort cleanly */
1291         device->spi = NULL;
1292
1293         kthread_stop(device->tx_task_struct);
1294
1295         device_destroy(pi433_class, device->devt);
1296
1297         cdev_del(device->cdev);
1298
1299         pi433_free_minor(device);
1300
1301         kfree(device->rx_buffer);
1302         kfree(device);
1303
1304         return 0;
1305 }
1306
1307 static const struct of_device_id pi433_dt_ids[] = {
1308         { .compatible = "Smarthome-Wolf,pi433" },
1309         {},
1310 };
1311
1312 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1313
1314 static struct spi_driver pi433_spi_driver = {
1315         .driver = {
1316                 .name =         "pi433",
1317                 .owner =        THIS_MODULE,
1318                 .of_match_table = of_match_ptr(pi433_dt_ids),
1319         },
1320         .probe =        pi433_probe,
1321         .remove =       pi433_remove,
1322
1323         /*
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.
1328          */
1329 };
1330
1331 /*-------------------------------------------------------------------------*/
1332
1333 static int __init pi433_init(void)
1334 {
1335         int status;
1336
1337         /*
1338          * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1339          * work stable - risk of buffer overflow
1340          */
1341         if (MAX_MSG_SIZE < FIFO_SIZE)
1342                 return -EINVAL;
1343
1344         /*
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.
1348          */
1349         status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1350         if (status < 0)
1351                 return status;
1352
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);
1358         }
1359
1360         status = spi_register_driver(&pi433_spi_driver);
1361         if (status < 0) {
1362                 class_destroy(pi433_class);
1363                 unregister_chrdev(MAJOR(pi433_dev),
1364                                   pi433_spi_driver.driver.name);
1365         }
1366
1367         return status;
1368 }
1369
1370 module_init(pi433_init);
1371
1372 static void __exit pi433_exit(void)
1373 {
1374         spi_unregister_driver(&pi433_spi_driver);
1375         class_destroy(pi433_class);
1376         unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1377 }
1378 module_exit(pi433_exit);
1379
1380 MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1381 MODULE_DESCRIPTION("Driver for Pi433");
1382 MODULE_LICENSE("GPL");
1383 MODULE_ALIAS("spi:pi433");