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/interrupt.h>
35#include <linux/irq.h>
36#include <linux/gpio/consumer.h>
37#include <linux/kthread.h>
38#include <linux/wait.h>
39#include <linux/spi/spi.h>
40#ifdef CONFIG_COMPAT
41#include <linux/compat.h>
42#endif
43#include <linux/debugfs.h>
44#include <linux/seq_file.h>
45
46#include "pi433_if.h"
47#include "rf69.h"
48
49#define N_PI433_MINORS		BIT(MINORBITS) /*32*/	/* ... up to 256 */
50#define MAX_MSG_SIZE		900	/* min: FIFO_SIZE! */
51#define MSG_FIFO_SIZE		65536   /* 65536 = 2^16  */
52#define FIFO_THRESHOLD	15		/* bytes */
53#define NUM_DIO			2
54
55static dev_t pi433_dev;
56static DEFINE_IDR(pi433_idr);
57static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
58static struct dentry *root_dir;	/* debugfs root directory for the driver */
59
60/* mainly for udev to create /dev/pi433 */
61static const struct class pi433_class = {
62	.name = "pi433",
63};
64
65/*
66 * tx config is instance specific
67 * so with each open a new tx config struct is needed
68 */
69/*
70 * rx config is device specific
71 * so we have just one rx config, ebedded in device struct
72 */
73struct pi433_device {
74	/* device handling related values */
75	dev_t			devt;
76	int			minor;
77	struct device		*dev;
78	struct cdev		*cdev;
79	struct spi_device	*spi;
80
81	/* irq related values */
82	struct gpio_desc	*gpiod[NUM_DIO];
83	int			irq_num[NUM_DIO];
84	u8			irq_state[NUM_DIO];
85
86	/* tx related values */
87	STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
88	struct mutex		tx_fifo_lock; /* serialize userspace writers */
89	struct task_struct	*tx_task_struct;
90	wait_queue_head_t	tx_wait_queue;
91	u8			free_in_fifo;
92	char			buffer[MAX_MSG_SIZE];
93
94	/* rx related values */
95	struct pi433_rx_cfg	rx_cfg;
96	u8			*rx_buffer;
97	unsigned int		rx_buffer_size;
98	u32			rx_bytes_to_drop;
99	u32			rx_bytes_dropped;
100	unsigned int		rx_position;
101	struct mutex		rx_lock; /* protects rx_* variable accesses */
102	wait_queue_head_t	rx_wait_queue;
103
104	/* fifo wait queue */
105	struct task_struct	*fifo_task_struct;
106	wait_queue_head_t	fifo_wait_queue;
107
108	/* flags */
109	bool			rx_active;
110	bool			tx_active;
111	bool			interrupt_rx_allowed;
112};
113
114struct pi433_instance {
115	struct pi433_device	*device;
116	struct pi433_tx_cfg	tx_cfg;
117
118	/* control flags */
119	bool			tx_cfg_initialized;
120};
121
122/*-------------------------------------------------------------------------*/
123
124/* GPIO interrupt handlers */
125static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
126{
127	struct pi433_device *device = dev_id;
128
129	if (device->irq_state[DIO0] == DIO_PACKET_SENT) {
130		device->free_in_fifo = FIFO_SIZE;
131		dev_dbg(device->dev, "DIO0 irq: Packet sent\n");
132		wake_up_interruptible(&device->fifo_wait_queue);
133	} else if (device->irq_state[DIO0] == DIO_RSSI_DIO0) {
134		dev_dbg(device->dev, "DIO0 irq: RSSI level over threshold\n");
135		wake_up_interruptible(&device->rx_wait_queue);
136	} else if (device->irq_state[DIO0] == DIO_PAYLOAD_READY) {
137		dev_dbg(device->dev, "DIO0 irq: Payload ready\n");
138		device->free_in_fifo = 0;
139		wake_up_interruptible(&device->fifo_wait_queue);
140	}
141
142	return IRQ_HANDLED;
143}
144
145static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
146{
147	struct pi433_device *device = dev_id;
148
149	if (device->irq_state[DIO1] == DIO_FIFO_NOT_EMPTY_DIO1) {
150		device->free_in_fifo = FIFO_SIZE;
151	} else if (device->irq_state[DIO1] == DIO_FIFO_LEVEL) {
152		if (device->rx_active)
153			device->free_in_fifo = FIFO_THRESHOLD - 1;
154		else
155			device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
156	}
157	dev_dbg(device->dev,
158		"DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
159	wake_up_interruptible(&device->fifo_wait_queue);
160
161	return IRQ_HANDLED;
162}
163
164/*-------------------------------------------------------------------------*/
165
166static int
167rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
168{
169	int ret;
170	int payload_length;
171
172	/* receiver config */
173	ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
174	if (ret < 0)
175		return ret;
176	ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
177	if (ret < 0)
178		return ret;
179	ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
180	if (ret < 0)
181		return ret;
182	ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
183	if (ret < 0)
184		return ret;
185	ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
186	if (ret < 0)
187		return ret;
188	ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
189	if (ret < 0)
190		return ret;
191	ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
192				 rx_cfg->bw_exponent);
193	if (ret < 0)
194		return ret;
195	ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
196					    rx_cfg->bw_exponent);
197	if (ret < 0)
198		return ret;
199	ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
200	if (ret < 0)
201		return ret;
202
203	dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
204
205	/* packet config */
206	/* enable */
207	if (rx_cfg->enable_sync == OPTION_ON) {
208		ret = rf69_enable_sync(dev->spi);
209		if (ret < 0)
210			return ret;
211
212		ret = rf69_set_fifo_fill_condition(dev->spi,
213						   after_sync_interrupt);
214		if (ret < 0)
215			return ret;
216	} else {
217		ret = rf69_disable_sync(dev->spi);
218		if (ret < 0)
219			return ret;
220
221		ret = rf69_set_fifo_fill_condition(dev->spi, always);
222		if (ret < 0)
223			return ret;
224	}
225	if (rx_cfg->enable_length_byte == OPTION_ON) {
226		ret = rf69_set_packet_format(dev->spi, packet_length_var);
227		if (ret < 0)
228			return ret;
229	} else {
230		ret = rf69_set_packet_format(dev->spi, packet_length_fix);
231		if (ret < 0)
232			return ret;
233	}
234	ret = rf69_set_address_filtering(dev->spi,
235					 rx_cfg->enable_address_filtering);
236	if (ret < 0)
237		return ret;
238
239	if (rx_cfg->enable_crc == OPTION_ON) {
240		ret = rf69_enable_crc(dev->spi);
241		if (ret < 0)
242			return ret;
243	} else {
244		ret = rf69_disable_crc(dev->spi);
245		if (ret < 0)
246			return ret;
247	}
248
249	/* lengths */
250	ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
251	if (ret < 0)
252		return ret;
253	if (rx_cfg->enable_length_byte == OPTION_ON) {
254		ret = rf69_set_payload_length(dev->spi, 0xff);
255		if (ret < 0)
256			return ret;
257	} else if (rx_cfg->fixed_message_length != 0) {
258		payload_length = rx_cfg->fixed_message_length;
259		if (rx_cfg->enable_length_byte  == OPTION_ON)
260			payload_length++;
261		if (rx_cfg->enable_address_filtering != filtering_off)
262			payload_length++;
263		ret = rf69_set_payload_length(dev->spi, payload_length);
264		if (ret < 0)
265			return ret;
266	} else {
267		ret = rf69_set_payload_length(dev->spi, 0);
268		if (ret < 0)
269			return ret;
270	}
271
272	/* values */
273	if (rx_cfg->enable_sync == OPTION_ON) {
274		ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
275		if (ret < 0)
276			return ret;
277	}
278	if (rx_cfg->enable_address_filtering != filtering_off) {
279		ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
280		if (ret < 0)
281			return ret;
282		ret = rf69_set_broadcast_address(dev->spi,
283						 rx_cfg->broadcast_address);
284		if (ret < 0)
285			return ret;
286	}
287
288	return 0;
289}
290
291static int
292rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
293{
294	int ret;
295
296	ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
297	if (ret < 0)
298		return ret;
299	ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
300	if (ret < 0)
301		return ret;
302	ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
303	if (ret < 0)
304		return ret;
305	ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
306	if (ret < 0)
307		return ret;
308	ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
309	if (ret < 0)
310		return ret;
311	ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
312	if (ret < 0)
313		return ret;
314	ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
315	if (ret < 0)
316		return ret;
317
318	/* packet format enable */
319	if (tx_cfg->enable_preamble == OPTION_ON) {
320		ret = rf69_set_preamble_length(dev->spi,
321					       tx_cfg->preamble_length);
322		if (ret < 0)
323			return ret;
324	} else {
325		ret = rf69_set_preamble_length(dev->spi, 0);
326		if (ret < 0)
327			return ret;
328	}
329
330	if (tx_cfg->enable_sync == OPTION_ON) {
331		ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
332		if (ret < 0)
333			return ret;
334		ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
335		if (ret < 0)
336			return ret;
337		ret = rf69_enable_sync(dev->spi);
338		if (ret < 0)
339			return ret;
340	} else {
341		ret = rf69_disable_sync(dev->spi);
342		if (ret < 0)
343			return ret;
344	}
345
346	if (tx_cfg->enable_length_byte == OPTION_ON) {
347		ret = rf69_set_packet_format(dev->spi, packet_length_var);
348		if (ret < 0)
349			return ret;
350	} else {
351		ret = rf69_set_packet_format(dev->spi, packet_length_fix);
352		if (ret < 0)
353			return ret;
354	}
355
356	if (tx_cfg->enable_crc == OPTION_ON) {
357		ret = rf69_enable_crc(dev->spi);
358		if (ret < 0)
359			return ret;
360	} else {
361		ret = rf69_disable_crc(dev->spi);
362		if (ret < 0)
363			return ret;
364	}
365
366	return 0;
367}
368
369/*-------------------------------------------------------------------------*/
370
371static int pi433_start_rx(struct pi433_device *dev)
372{
373	int retval;
374
375	/* return without action, if no pending read request */
376	if (!dev->rx_active)
377		return 0;
378
379	/* setup for receiving */
380	retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
381	if (retval)
382		return retval;
383
384	/* setup rssi irq */
385	retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
386	if (retval < 0)
387		return retval;
388	dev->irq_state[DIO0] = DIO_RSSI_DIO0;
389	irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
390
391	/* setup fifo level interrupt */
392	retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
393	if (retval < 0)
394		return retval;
395	retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
396	if (retval < 0)
397		return retval;
398	dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
399	irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
400
401	/* set module to receiving mode */
402	retval = rf69_set_mode(dev->spi, receive);
403	if (retval < 0)
404		return retval;
405
406	return 0;
407}
408
409/*-------------------------------------------------------------------------*/
410
411static int pi433_receive(void *data)
412{
413	struct pi433_device *dev = data;
414	struct spi_device *spi = dev->spi;
415	int bytes_to_read, bytes_total;
416	int retval;
417
418	dev->interrupt_rx_allowed = false;
419
420	/* wait for any tx to finish */
421	dev_dbg(dev->dev, "rx: going to wait for any tx to finish\n");
422	retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
423	if (retval) {
424		/* wait was interrupted */
425		dev->interrupt_rx_allowed = true;
426		wake_up_interruptible(&dev->tx_wait_queue);
427		return retval;
428	}
429
430	/* prepare status vars */
431	dev->free_in_fifo = FIFO_SIZE;
432	dev->rx_position = 0;
433	dev->rx_bytes_dropped = 0;
434
435	/* setup radio module to listen for something "in the air" */
436	retval = pi433_start_rx(dev);
437	if (retval)
438		return retval;
439
440	/* now check RSSI, if low wait for getting high (RSSI interrupt) */
441	while (!(rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RSSI)) {
442		/* allow tx to interrupt us while waiting for high RSSI */
443		dev->interrupt_rx_allowed = true;
444		wake_up_interruptible(&dev->tx_wait_queue);
445
446		/* wait for RSSI level to become high */
447		dev_dbg(dev->dev, "rx: going to wait for high RSSI level\n");
448		retval = wait_event_interruptible(dev->rx_wait_queue,
449						  rf69_read_reg(spi, REG_IRQFLAGS1) &
450						  MASK_IRQFLAGS1_RSSI);
451		if (retval) /* wait was interrupted */
452			goto abort;
453		dev->interrupt_rx_allowed = false;
454
455		/* cross check for ongoing tx */
456		if (!dev->tx_active)
457			break;
458	}
459
460	/* configure payload ready irq */
461	retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
462	if (retval < 0)
463		goto abort;
464	dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
465	irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
466
467	/* fixed or unlimited length? */
468	if (dev->rx_cfg.fixed_message_length != 0) {
469		if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
470			retval = -1;
471			goto abort;
472		}
473		bytes_total = dev->rx_cfg.fixed_message_length;
474		dev_dbg(dev->dev, "rx: msg len set to %d by fixed length\n",
475			bytes_total);
476	} else {
477		bytes_total = dev->rx_buffer_size;
478		dev_dbg(dev->dev, "rx: msg len set to %d as requested by read\n",
479			bytes_total);
480	}
481
482	/* length byte enabled? */
483	if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
484		retval = wait_event_interruptible(dev->fifo_wait_queue,
485						  dev->free_in_fifo < FIFO_SIZE);
486		if (retval) /* wait was interrupted */
487			goto abort;
488
489		rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
490		if (bytes_total > dev->rx_buffer_size) {
491			retval = -1;
492			goto abort;
493		}
494		dev->free_in_fifo++;
495		dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte\n",
496			bytes_total);
497	}
498
499	/* address byte enabled? */
500	if (dev->rx_cfg.enable_address_filtering != filtering_off) {
501		u8 dummy;
502
503		bytes_total--;
504
505		retval = wait_event_interruptible(dev->fifo_wait_queue,
506						  dev->free_in_fifo < FIFO_SIZE);
507		if (retval) /* wait was interrupted */
508			goto abort;
509
510		rf69_read_fifo(spi, &dummy, 1);
511		dev->free_in_fifo++;
512		dev_dbg(dev->dev, "rx: address byte stripped off\n");
513	}
514
515	/* get payload */
516	while (dev->rx_position < bytes_total) {
517		if (!(rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PAYLOAD_READY)) {
518			retval = wait_event_interruptible(dev->fifo_wait_queue,
519							  dev->free_in_fifo < FIFO_SIZE);
520			if (retval) /* wait was interrupted */
521				goto abort;
522		}
523
524		/* need to drop bytes or acquire? */
525		if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
526			bytes_to_read = dev->rx_bytes_to_drop -
527					dev->rx_bytes_dropped;
528		else
529			bytes_to_read = bytes_total - dev->rx_position;
530
531		/* access the fifo */
532		if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
533			bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
534		retval = rf69_read_fifo(spi,
535					&dev->rx_buffer[dev->rx_position],
536					bytes_to_read);
537		if (retval) /* read failed */
538			goto abort;
539
540		dev->free_in_fifo += bytes_to_read;
541
542		/* adjust status vars */
543		if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
544			dev->rx_bytes_dropped += bytes_to_read;
545		else
546			dev->rx_position += bytes_to_read;
547	}
548
549	/* rx done, wait was interrupted or error occurred */
550abort:
551	dev->interrupt_rx_allowed = true;
552	if (rf69_set_mode(dev->spi, standby))
553		pr_err("rf69_set_mode(): radio module failed to go standby\n");
554	wake_up_interruptible(&dev->tx_wait_queue);
555
556	if (retval)
557		return retval;
558	else
559		return bytes_total;
560}
561
562static int pi433_tx_thread(void *data)
563{
564	struct pi433_device *device = data;
565	struct spi_device *spi = device->spi;
566	struct pi433_tx_cfg tx_cfg;
567	size_t size;
568	bool   rx_interrupted = false;
569	int    position, repetitions;
570	int    retval;
571
572	while (1) {
573		/* wait for fifo to be populated or for request to terminate*/
574		dev_dbg(device->dev, "thread: going to wait for new messages\n");
575		wait_event_interruptible(device->tx_wait_queue,
576					 (!kfifo_is_empty(&device->tx_fifo) ||
577					  kthread_should_stop()));
578		if (kthread_should_stop())
579			return 0;
580
581		/*
582		 * get data from fifo in the following order:
583		 * - tx_cfg
584		 * - size of message
585		 * - message
586		 */
587		retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
588		if (retval != sizeof(tx_cfg)) {
589			dev_dbg(device->dev,
590				"reading tx_cfg from fifo failed: got %d byte(s), expected %d\n",
591				retval, (unsigned int)sizeof(tx_cfg));
592			continue;
593		}
594
595		retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
596		if (retval != sizeof(size_t)) {
597			dev_dbg(device->dev,
598				"reading msg size from fifo failed: got %d, expected %d\n",
599				retval, (unsigned int)sizeof(size_t));
600			continue;
601		}
602
603		/* use fixed message length, if requested */
604		if (tx_cfg.fixed_message_length != 0)
605			size = tx_cfg.fixed_message_length;
606
607		/* increase size, if len byte is requested */
608		if (tx_cfg.enable_length_byte == OPTION_ON)
609			size++;
610
611		/* increase size, if adr byte is requested */
612		if (tx_cfg.enable_address_byte == OPTION_ON)
613			size++;
614
615		/* prime buffer */
616		memset(device->buffer, 0, size);
617		position = 0;
618
619		/* add length byte, if requested */
620		if (tx_cfg.enable_length_byte  == OPTION_ON)
621			/*
622			 * according to spec, length byte itself must be
623			 * excluded from the length calculation
624			 */
625			device->buffer[position++] = size - 1;
626
627		/* add adr byte, if requested */
628		if (tx_cfg.enable_address_byte == OPTION_ON)
629			device->buffer[position++] = tx_cfg.address_byte;
630
631		/* finally get message data from fifo */
632		retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
633				   sizeof(device->buffer) - position);
634		dev_dbg(device->dev,
635			"read %d message byte(s) from fifo queue.\n", retval);
636
637		/*
638		 * if rx is active, we need to interrupt the waiting for
639		 * incoming telegrams, to be able to send something.
640		 * We are only allowed, if currently no reception takes
641		 * place otherwise we need to  wait for the incoming telegram
642		 * to finish
643		 */
644		wait_event_interruptible(device->tx_wait_queue,
645					 !device->rx_active ||
646					  device->interrupt_rx_allowed);
647
648		/*
649		 * prevent race conditions
650		 * irq will be reenabled after tx config is set
651		 */
652		disable_irq(device->irq_num[DIO0]);
653		device->tx_active = true;
654
655		/* clear fifo, set fifo threshold, set payload length */
656		retval = rf69_set_mode(spi, standby); /* this clears the fifo */
657		if (retval < 0)
658			goto abort;
659
660		if (device->rx_active && !rx_interrupted) {
661			/*
662			 * rx is currently waiting for a telegram;
663			 * we need to set the radio module to standby
664			 */
665			rx_interrupted = true;
666		}
667
668		retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
669		if (retval < 0)
670			goto abort;
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				goto abort;
675		} else {
676			retval = rf69_set_payload_length(spi, 0);
677			if (retval < 0)
678				goto abort;
679		}
680
681		/* configure the rf chip */
682		retval = rf69_set_tx_cfg(device, &tx_cfg);
683		if (retval < 0)
684			goto abort;
685
686		/* enable fifo level interrupt */
687		retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
688		if (retval < 0)
689			goto abort;
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			goto abort;
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			goto abort;
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\n");
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			return 0;
746
747		/* STOP_TRANSMISSION */
748		dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.\n");
749		retval = rf69_set_mode(spi, standby);
750		if (retval < 0)
751			goto abort;
752
753		/* everything sent? */
754		if (kfifo_is_empty(&device->tx_fifo)) {
755abort:
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
768static ssize_t
769pi433_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
813static ssize_t
814pi433_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	 * check if tx_cfg has been initialized otherwise we won't be able to
834	 * config the RF trasmitter correctly due to invalid settings
835	 */
836	if (!instance->tx_cfg_initialized) {
837		dev_notice_once(device->dev,
838				"write: failed due to unconfigured tx_cfg (see PI433_IOC_WR_TX_CFG)\n");
839		return -EINVAL;
840	}
841
842	/*
843	 * write the following sequence into fifo:
844	 * - tx_cfg
845	 * - size of message
846	 * - message
847	 */
848	mutex_lock(&device->tx_fifo_lock);
849
850	required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
851	available = kfifo_avail(&device->tx_fifo);
852	if (required > available) {
853		dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available\n",
854			required, available);
855		mutex_unlock(&device->tx_fifo_lock);
856		return -EAGAIN;
857	}
858
859	retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
860			  sizeof(instance->tx_cfg));
861	if (retval != sizeof(instance->tx_cfg))
862		goto abort;
863
864	retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
865	if (retval != sizeof(size_t))
866		goto abort;
867
868	retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
869	if (retval || copied != count)
870		goto abort;
871
872	mutex_unlock(&device->tx_fifo_lock);
873
874	/* start transfer */
875	wake_up_interruptible(&device->tx_wait_queue);
876	dev_dbg(device->dev, "write: generated new msg with %d bytes.\n", copied);
877
878	return copied;
879
880abort:
881	dev_warn(device->dev,
882		 "write to fifo failed, non recoverable: 0x%x\n", retval);
883	mutex_unlock(&device->tx_fifo_lock);
884	return -EAGAIN;
885}
886
887static long pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
888{
889	struct pi433_instance	*instance;
890	struct pi433_device	*device;
891	struct pi433_tx_cfg	tx_cfg;
892	void __user *argp = (void __user *)arg;
893
894	/* Check type and command number */
895	if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
896		return -ENOTTY;
897
898	instance = filp->private_data;
899	device = instance->device;
900
901	if (!device)
902		return -ESHUTDOWN;
903
904	switch (cmd) {
905	case PI433_IOC_RD_TX_CFG:
906		if (copy_to_user(argp, &instance->tx_cfg,
907				 sizeof(struct pi433_tx_cfg)))
908			return -EFAULT;
909		break;
910	case PI433_IOC_WR_TX_CFG:
911		if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
912			return -EFAULT;
913		mutex_lock(&device->tx_fifo_lock);
914		memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
915		instance->tx_cfg_initialized = true;
916		mutex_unlock(&device->tx_fifo_lock);
917		break;
918	case PI433_IOC_RD_RX_CFG:
919		if (copy_to_user(argp, &device->rx_cfg,
920				 sizeof(struct pi433_rx_cfg)))
921			return -EFAULT;
922		break;
923	case PI433_IOC_WR_RX_CFG:
924		mutex_lock(&device->rx_lock);
925
926		/* during pendig read request, change of config not allowed */
927		if (device->rx_active) {
928			mutex_unlock(&device->rx_lock);
929			return -EAGAIN;
930		}
931
932		if (copy_from_user(&device->rx_cfg, argp,
933				   sizeof(struct pi433_rx_cfg))) {
934			mutex_unlock(&device->rx_lock);
935			return -EFAULT;
936		}
937
938		mutex_unlock(&device->rx_lock);
939		break;
940	default:
941		return -EINVAL;
942	}
943
944	return 0;
945}
946
947/*-------------------------------------------------------------------------*/
948
949static int pi433_open(struct inode *inode, struct file *filp)
950{
951	struct pi433_device	*device;
952	struct pi433_instance	*instance;
953
954	mutex_lock(&minor_lock);
955	device = idr_find(&pi433_idr, iminor(inode));
956	mutex_unlock(&minor_lock);
957	if (!device) {
958		pr_debug("device: minor %d unknown.\n", iminor(inode));
959		return -ENODEV;
960	}
961
962	instance = kzalloc(sizeof(*instance), GFP_KERNEL);
963	if (!instance)
964		return -ENOMEM;
965
966	/* setup instance data*/
967	instance->device = device;
968
969	/* instance data as context */
970	filp->private_data = instance;
971	stream_open(inode, filp);
972
973	return 0;
974}
975
976static int pi433_release(struct inode *inode, struct file *filp)
977{
978	struct pi433_instance	*instance;
979
980	instance = filp->private_data;
981	kfree(instance);
982	filp->private_data = NULL;
983
984	return 0;
985}
986
987/*-------------------------------------------------------------------------*/
988
989static int setup_gpio(struct pi433_device *device)
990{
991	char	name[5];
992	int	retval;
993	int	i;
994	const irq_handler_t DIO_irq_handler[NUM_DIO] = {
995		DIO0_irq_handler,
996		DIO1_irq_handler
997	};
998
999	for (i = 0; i < NUM_DIO; i++) {
1000		/* "construct" name and get the gpio descriptor */
1001		snprintf(name, sizeof(name), "DIO%d", i);
1002		device->gpiod[i] = gpiod_get(&device->spi->dev, name,
1003					     0 /*GPIOD_IN*/);
1004
1005		if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1006			dev_dbg(&device->spi->dev,
1007				"Could not find entry for %s. Ignoring.\n", name);
1008			continue;
1009		}
1010
1011		if (device->gpiod[i] == ERR_PTR(-EBUSY))
1012			dev_dbg(&device->spi->dev, "%s is busy.\n", name);
1013
1014		if (IS_ERR(device->gpiod[i])) {
1015			retval = PTR_ERR(device->gpiod[i]);
1016			/* release already allocated gpios */
1017			for (i--; i >= 0; i--) {
1018				free_irq(device->irq_num[i], device);
1019				gpiod_put(device->gpiod[i]);
1020			}
1021			return retval;
1022		}
1023
1024		/* configure the pin */
1025		retval = gpiod_direction_input(device->gpiod[i]);
1026		if (retval)
1027			return retval;
1028
1029		/* configure irq */
1030		device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1031		if (device->irq_num[i] < 0) {
1032			device->gpiod[i] = ERR_PTR(-EINVAL);
1033			return device->irq_num[i];
1034		}
1035		retval = request_irq(device->irq_num[i],
1036				     DIO_irq_handler[i],
1037				     0, /* flags */
1038				     name,
1039				     device);
1040
1041		if (retval)
1042			return retval;
1043
1044		dev_dbg(&device->spi->dev, "%s successfully configured\n", name);
1045	}
1046
1047	return 0;
1048}
1049
1050static void free_gpio(struct pi433_device *device)
1051{
1052	int i;
1053
1054	for (i = 0; i < NUM_DIO; i++) {
1055		/* check if gpiod is valid */
1056		if (IS_ERR(device->gpiod[i]))
1057			continue;
1058
1059		free_irq(device->irq_num[i], device);
1060		gpiod_put(device->gpiod[i]);
1061	}
1062}
1063
1064static int pi433_get_minor(struct pi433_device *device)
1065{
1066	int retval = -ENOMEM;
1067
1068	mutex_lock(&minor_lock);
1069	retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1070	if (retval >= 0) {
1071		device->minor = retval;
1072		retval = 0;
1073	} else if (retval == -ENOSPC) {
1074		dev_err(&device->spi->dev, "too many pi433 devices\n");
1075		retval = -EINVAL;
1076	}
1077	mutex_unlock(&minor_lock);
1078	return retval;
1079}
1080
1081static void pi433_free_minor(struct pi433_device *dev)
1082{
1083	mutex_lock(&minor_lock);
1084	idr_remove(&pi433_idr, dev->minor);
1085	mutex_unlock(&minor_lock);
1086}
1087
1088/*-------------------------------------------------------------------------*/
1089
1090static const struct file_operations pi433_fops = {
1091	.owner =	THIS_MODULE,
1092	/*
1093	 * REVISIT switch to aio primitives, so that userspace
1094	 * gets more complete API coverage.  It'll simplify things
1095	 * too, except for the locking.
1096	 */
1097	.write =	pi433_write,
1098	.read =		pi433_read,
1099	.unlocked_ioctl = pi433_ioctl,
1100	.compat_ioctl = compat_ptr_ioctl,
1101	.open =		pi433_open,
1102	.release =	pi433_release,
1103	.llseek =	no_llseek,
1104};
1105
1106static int pi433_debugfs_regs_show(struct seq_file *m, void *p)
1107{
1108	struct pi433_device *dev;
1109	u8 reg_data[114];
1110	int i;
1111	char *fmt = "0x%02x, 0x%02x\n";
1112	int ret;
1113
1114	dev = m->private;
1115
1116	mutex_lock(&dev->tx_fifo_lock);
1117	mutex_lock(&dev->rx_lock);
1118
1119	// wait for on-going operations to finish
1120	ret = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
1121	if (ret)
1122		goto out_unlock;
1123
1124	ret = wait_event_interruptible(dev->tx_wait_queue, !dev->rx_active);
1125	if (ret)
1126		goto out_unlock;
1127
1128	// skip FIFO register (0x0) otherwise this can affect some of uC ops
1129	for (i = 1; i < 0x50; i++)
1130		reg_data[i] = rf69_read_reg(dev->spi, i);
1131
1132	reg_data[REG_TESTLNA] = rf69_read_reg(dev->spi, REG_TESTLNA);
1133	reg_data[REG_TESTPA1] = rf69_read_reg(dev->spi, REG_TESTPA1);
1134	reg_data[REG_TESTPA2] = rf69_read_reg(dev->spi, REG_TESTPA2);
1135	reg_data[REG_TESTDAGC] = rf69_read_reg(dev->spi, REG_TESTDAGC);
1136	reg_data[REG_TESTAFC] = rf69_read_reg(dev->spi, REG_TESTAFC);
1137
1138	seq_puts(m, "# reg, val\n");
1139
1140	for (i = 1; i < 0x50; i++)
1141		seq_printf(m, fmt, i, reg_data[i]);
1142
1143	seq_printf(m, fmt, REG_TESTLNA, reg_data[REG_TESTLNA]);
1144	seq_printf(m, fmt, REG_TESTPA1, reg_data[REG_TESTPA1]);
1145	seq_printf(m, fmt, REG_TESTPA2, reg_data[REG_TESTPA2]);
1146	seq_printf(m, fmt, REG_TESTDAGC, reg_data[REG_TESTDAGC]);
1147	seq_printf(m, fmt, REG_TESTAFC, reg_data[REG_TESTAFC]);
1148
1149out_unlock:
1150	mutex_unlock(&dev->rx_lock);
1151	mutex_unlock(&dev->tx_fifo_lock);
1152
1153	return ret;
1154}
1155DEFINE_SHOW_ATTRIBUTE(pi433_debugfs_regs);
1156
1157/*-------------------------------------------------------------------------*/
1158
1159static int pi433_probe(struct spi_device *spi)
1160{
1161	struct pi433_device	*device;
1162	int			retval;
1163	struct dentry		*entry;
1164
1165	/* setup spi parameters */
1166	spi->mode = 0x00;
1167	spi->bits_per_word = 8;
1168	/*
1169	 * spi->max_speed_hz = 10000000;
1170	 * 1MHz already set by device tree overlay
1171	 */
1172
1173	retval = spi_setup(spi);
1174	if (retval) {
1175		dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1176		return retval;
1177	}
1178
1179	dev_dbg(&spi->dev,
1180		"spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed\n",
1181		spi->mode, spi->bits_per_word, spi->max_speed_hz);
1182
1183	/* read chip version */
1184	retval = rf69_get_version(spi);
1185	if (retval < 0)
1186		return retval;
1187
1188	switch (retval) {
1189	case 0x24:
1190		dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)\n", retval);
1191		break;
1192	default:
1193		dev_dbg(&spi->dev, "unknown chip version: 0x%x\n", retval);
1194		return -ENODEV;
1195	}
1196
1197	/* Allocate driver data */
1198	device = kzalloc(sizeof(*device), GFP_KERNEL);
1199	if (!device)
1200		return -ENOMEM;
1201
1202	/* Initialize the driver data */
1203	device->spi = spi;
1204	device->rx_active = false;
1205	device->tx_active = false;
1206	device->interrupt_rx_allowed = false;
1207
1208	/* init rx buffer */
1209	device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1210	if (!device->rx_buffer) {
1211		retval = -ENOMEM;
1212		goto RX_failed;
1213	}
1214
1215	/* init wait queues */
1216	init_waitqueue_head(&device->tx_wait_queue);
1217	init_waitqueue_head(&device->rx_wait_queue);
1218	init_waitqueue_head(&device->fifo_wait_queue);
1219
1220	/* init fifo */
1221	INIT_KFIFO(device->tx_fifo);
1222
1223	/* init mutexes and locks */
1224	mutex_init(&device->tx_fifo_lock);
1225	mutex_init(&device->rx_lock);
1226
1227	/* setup GPIO (including irq_handler) for the different DIOs */
1228	retval = setup_gpio(device);
1229	if (retval) {
1230		dev_dbg(&spi->dev, "setup of GPIOs failed\n");
1231		goto GPIO_failed;
1232	}
1233
1234	/* setup the radio module */
1235	retval = rf69_set_mode(spi, standby);
1236	if (retval < 0)
1237		goto minor_failed;
1238	retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1239	if (retval < 0)
1240		goto minor_failed;
1241	retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1242	if (retval < 0)
1243		goto minor_failed;
1244	retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1245	if (retval < 0)
1246		goto minor_failed;
1247	retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1248	if (retval < 0)
1249		goto minor_failed;
1250	retval = rf69_set_output_power_level(spi, 13);
1251	if (retval < 0)
1252		goto minor_failed;
1253	retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1254	if (retval < 0)
1255		goto minor_failed;
1256
1257	/* determ minor number */
1258	retval = pi433_get_minor(device);
1259	if (retval) {
1260		dev_dbg(&spi->dev, "get of minor number failed\n");
1261		goto minor_failed;
1262	}
1263
1264	/* create device */
1265	device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1266	device->dev = device_create(&pi433_class,
1267				    &spi->dev,
1268				    device->devt,
1269				    device,
1270				    "pi433.%d",
1271				    device->minor);
1272	if (IS_ERR(device->dev)) {
1273		pr_err("pi433: device register failed\n");
1274		retval = PTR_ERR(device->dev);
1275		goto device_create_failed;
1276	} else {
1277		dev_dbg(device->dev,
1278			"created device for major %d, minor %d\n",
1279			MAJOR(pi433_dev),
1280			device->minor);
1281	}
1282
1283	/* start tx thread */
1284	device->tx_task_struct = kthread_run(pi433_tx_thread,
1285					     device,
1286					     "pi433.%d_tx_task",
1287					     device->minor);
1288	if (IS_ERR(device->tx_task_struct)) {
1289		dev_dbg(device->dev, "start of send thread failed\n");
1290		retval = PTR_ERR(device->tx_task_struct);
1291		goto send_thread_failed;
1292	}
1293
1294	/* create cdev */
1295	device->cdev = cdev_alloc();
1296	if (!device->cdev) {
1297		dev_dbg(device->dev, "allocation of cdev failed\n");
1298		retval = -ENOMEM;
1299		goto cdev_failed;
1300	}
1301	device->cdev->owner = THIS_MODULE;
1302	cdev_init(device->cdev, &pi433_fops);
1303	retval = cdev_add(device->cdev, device->devt, 1);
1304	if (retval) {
1305		dev_dbg(device->dev, "register of cdev failed\n");
1306		goto del_cdev;
1307	}
1308
1309	/* spi setup */
1310	spi_set_drvdata(spi, device);
1311
1312	entry = debugfs_create_dir(dev_name(device->dev), root_dir);
1313	debugfs_create_file("regs", 0400, entry, device, &pi433_debugfs_regs_fops);
1314
1315	return 0;
1316
1317del_cdev:
1318	cdev_del(device->cdev);
1319cdev_failed:
1320	kthread_stop(device->tx_task_struct);
1321send_thread_failed:
1322	device_destroy(&pi433_class, device->devt);
1323device_create_failed:
1324	pi433_free_minor(device);
1325minor_failed:
1326	free_gpio(device);
1327GPIO_failed:
1328	kfree(device->rx_buffer);
1329RX_failed:
1330	kfree(device);
1331
1332	return retval;
1333}
1334
1335static void pi433_remove(struct spi_device *spi)
1336{
1337	struct pi433_device	*device = spi_get_drvdata(spi);
1338
1339	debugfs_lookup_and_remove(dev_name(device->dev), root_dir);
1340
1341	/* free GPIOs */
1342	free_gpio(device);
1343
1344	/* make sure ops on existing fds can abort cleanly */
1345	device->spi = NULL;
1346
1347	kthread_stop(device->tx_task_struct);
1348
1349	device_destroy(&pi433_class, device->devt);
1350
1351	cdev_del(device->cdev);
1352
1353	pi433_free_minor(device);
1354
1355	kfree(device->rx_buffer);
1356	kfree(device);
1357}
1358
1359static const struct of_device_id pi433_dt_ids[] = {
1360	{ .compatible = "Smarthome-Wolf,pi433" },
1361	{},
1362};
1363
1364MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1365
1366static struct spi_driver pi433_spi_driver = {
1367	.driver = {
1368		.name =		"pi433",
1369		.owner =	THIS_MODULE,
1370		.of_match_table = of_match_ptr(pi433_dt_ids),
1371	},
1372	.probe =	pi433_probe,
1373	.remove =	pi433_remove,
1374
1375	/*
1376	 * NOTE:  suspend/resume methods are not necessary here.
1377	 * We don't do anything except pass the requests to/from
1378	 * the underlying controller.  The refrigerator handles
1379	 * most issues; the controller driver handles the rest.
1380	 */
1381};
1382
1383/*-------------------------------------------------------------------------*/
1384
1385static int __init pi433_init(void)
1386{
1387	int status;
1388
1389	/*
1390	 * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1391	 * work stable - risk of buffer overflow
1392	 */
1393	if (MAX_MSG_SIZE < FIFO_SIZE)
1394		return -EINVAL;
1395
1396	/*
1397	 * Claim device numbers.  Then register a class
1398	 * that will key udev/mdev to add/remove /dev nodes.
1399	 * Last, register the driver which manages those device numbers.
1400	 */
1401	status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1402	if (status < 0)
1403		return status;
1404
1405	status = class_register(&pi433_class);
1406	if (status) {
1407		unregister_chrdev(MAJOR(pi433_dev),
1408				  pi433_spi_driver.driver.name);
1409		return status;
1410	}
1411
1412	root_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1413
1414	status = spi_register_driver(&pi433_spi_driver);
1415	if (status < 0) {
1416		class_unregister(&pi433_class);
1417		unregister_chrdev(MAJOR(pi433_dev),
1418				  pi433_spi_driver.driver.name);
1419	}
1420
1421	return status;
1422}
1423
1424module_init(pi433_init);
1425
1426static void __exit pi433_exit(void)
1427{
1428	spi_unregister_driver(&pi433_spi_driver);
1429	class_unregister(&pi433_class);
1430	unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1431	debugfs_remove(root_dir);
1432}
1433module_exit(pi433_exit);
1434
1435MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1436MODULE_DESCRIPTION("Driver for Pi433");
1437MODULE_LICENSE("GPL");
1438MODULE_ALIAS("spi:pi433");
1439