0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #undef DEBUG
0020
0021 #include <linux/init.h>
0022 #include <linux/module.h>
0023 #include <linux/idr.h>
0024 #include <linux/ioctl.h>
0025 #include <linux/uaccess.h>
0026 #include <linux/fs.h>
0027 #include <linux/device.h>
0028 #include <linux/cdev.h>
0029 #include <linux/err.h>
0030 #include <linux/kfifo.h>
0031 #include <linux/errno.h>
0032 #include <linux/mutex.h>
0033 #include <linux/of.h>
0034 #include <linux/of_device.h>
0035 #include <linux/interrupt.h>
0036 #include <linux/irq.h>
0037 #include <linux/gpio/consumer.h>
0038 #include <linux/kthread.h>
0039 #include <linux/wait.h>
0040 #include <linux/spi/spi.h>
0041 #ifdef CONFIG_COMPAT
0042 #include <linux/compat.h>
0043 #endif
0044 #include <linux/debugfs.h>
0045 #include <linux/seq_file.h>
0046
0047 #include "pi433_if.h"
0048 #include "rf69.h"
0049
0050 #define N_PI433_MINORS BIT(MINORBITS)
0051 #define MAX_MSG_SIZE 900
0052 #define MSG_FIFO_SIZE 65536
0053 #define NUM_DIO 2
0054
0055 static dev_t pi433_dev;
0056 static DEFINE_IDR(pi433_idr);
0057 static DEFINE_MUTEX(minor_lock);
0058
0059 static struct class *pi433_class;
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 struct pi433_device {
0070
0071 dev_t devt;
0072 int minor;
0073 struct device *dev;
0074 struct cdev *cdev;
0075 struct spi_device *spi;
0076
0077
0078 struct gpio_desc *gpiod[NUM_DIO];
0079 int irq_num[NUM_DIO];
0080 u8 irq_state[NUM_DIO];
0081
0082
0083 STRUCT_KFIFO_REC_1(MSG_FIFO_SIZE) tx_fifo;
0084 struct mutex tx_fifo_lock;
0085 struct task_struct *tx_task_struct;
0086 wait_queue_head_t tx_wait_queue;
0087 u8 free_in_fifo;
0088 char buffer[MAX_MSG_SIZE];
0089
0090
0091 struct pi433_rx_cfg rx_cfg;
0092 u8 *rx_buffer;
0093 unsigned int rx_buffer_size;
0094 u32 rx_bytes_to_drop;
0095 u32 rx_bytes_dropped;
0096 unsigned int rx_position;
0097 struct mutex rx_lock;
0098 wait_queue_head_t rx_wait_queue;
0099
0100
0101 struct task_struct *fifo_task_struct;
0102 wait_queue_head_t fifo_wait_queue;
0103
0104
0105 bool rx_active;
0106 bool tx_active;
0107 bool interrupt_rx_allowed;
0108 };
0109
0110 struct pi433_instance {
0111 struct pi433_device *device;
0112 struct pi433_tx_cfg tx_cfg;
0113
0114
0115 bool tx_cfg_initialized;
0116 };
0117
0118
0119
0120
0121 static irqreturn_t DIO0_irq_handler(int irq, void *dev_id)
0122 {
0123 struct pi433_device *device = dev_id;
0124
0125 if (device->irq_state[DIO0] == DIO_PACKET_SENT) {
0126 device->free_in_fifo = FIFO_SIZE;
0127 dev_dbg(device->dev, "DIO0 irq: Packet sent\n");
0128 wake_up_interruptible(&device->fifo_wait_queue);
0129 } else if (device->irq_state[DIO0] == DIO_RSSI_DIO0) {
0130 dev_dbg(device->dev, "DIO0 irq: RSSI level over threshold\n");
0131 wake_up_interruptible(&device->rx_wait_queue);
0132 } else if (device->irq_state[DIO0] == DIO_PAYLOAD_READY) {
0133 dev_dbg(device->dev, "DIO0 irq: Payload ready\n");
0134 device->free_in_fifo = 0;
0135 wake_up_interruptible(&device->fifo_wait_queue);
0136 }
0137
0138 return IRQ_HANDLED;
0139 }
0140
0141 static irqreturn_t DIO1_irq_handler(int irq, void *dev_id)
0142 {
0143 struct pi433_device *device = dev_id;
0144
0145 if (device->irq_state[DIO1] == DIO_FIFO_NOT_EMPTY_DIO1) {
0146 device->free_in_fifo = FIFO_SIZE;
0147 } else if (device->irq_state[DIO1] == DIO_FIFO_LEVEL) {
0148 if (device->rx_active)
0149 device->free_in_fifo = FIFO_THRESHOLD - 1;
0150 else
0151 device->free_in_fifo = FIFO_SIZE - FIFO_THRESHOLD - 1;
0152 }
0153 dev_dbg(device->dev,
0154 "DIO1 irq: %d bytes free in fifo\n", device->free_in_fifo);
0155 wake_up_interruptible(&device->fifo_wait_queue);
0156
0157 return IRQ_HANDLED;
0158 }
0159
0160
0161
0162 static int
0163 rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg)
0164 {
0165 int ret;
0166 int payload_length;
0167
0168
0169 ret = rf69_set_frequency(dev->spi, rx_cfg->frequency);
0170 if (ret < 0)
0171 return ret;
0172 ret = rf69_set_modulation(dev->spi, rx_cfg->modulation);
0173 if (ret < 0)
0174 return ret;
0175 ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate);
0176 if (ret < 0)
0177 return ret;
0178 ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance);
0179 if (ret < 0)
0180 return ret;
0181 ret = rf69_set_rssi_threshold(dev->spi, rx_cfg->rssi_threshold);
0182 if (ret < 0)
0183 return ret;
0184 ret = rf69_set_ook_threshold_dec(dev->spi, rx_cfg->threshold_decrement);
0185 if (ret < 0)
0186 return ret;
0187 ret = rf69_set_bandwidth(dev->spi, rx_cfg->bw_mantisse,
0188 rx_cfg->bw_exponent);
0189 if (ret < 0)
0190 return ret;
0191 ret = rf69_set_bandwidth_during_afc(dev->spi, rx_cfg->bw_mantisse,
0192 rx_cfg->bw_exponent);
0193 if (ret < 0)
0194 return ret;
0195 ret = rf69_set_dagc(dev->spi, rx_cfg->dagc);
0196 if (ret < 0)
0197 return ret;
0198
0199 dev->rx_bytes_to_drop = rx_cfg->bytes_to_drop;
0200
0201
0202
0203 if (rx_cfg->enable_sync == OPTION_ON) {
0204 ret = rf69_enable_sync(dev->spi);
0205 if (ret < 0)
0206 return ret;
0207
0208 ret = rf69_set_fifo_fill_condition(dev->spi,
0209 after_sync_interrupt);
0210 if (ret < 0)
0211 return ret;
0212 } else {
0213 ret = rf69_disable_sync(dev->spi);
0214 if (ret < 0)
0215 return ret;
0216
0217 ret = rf69_set_fifo_fill_condition(dev->spi, always);
0218 if (ret < 0)
0219 return ret;
0220 }
0221 if (rx_cfg->enable_length_byte == OPTION_ON) {
0222 ret = rf69_set_packet_format(dev->spi, packet_length_var);
0223 if (ret < 0)
0224 return ret;
0225 } else {
0226 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
0227 if (ret < 0)
0228 return ret;
0229 }
0230 ret = rf69_set_address_filtering(dev->spi,
0231 rx_cfg->enable_address_filtering);
0232 if (ret < 0)
0233 return ret;
0234
0235 if (rx_cfg->enable_crc == OPTION_ON) {
0236 ret = rf69_enable_crc(dev->spi);
0237 if (ret < 0)
0238 return ret;
0239 } else {
0240 ret = rf69_disable_crc(dev->spi);
0241 if (ret < 0)
0242 return ret;
0243 }
0244
0245
0246 ret = rf69_set_sync_size(dev->spi, rx_cfg->sync_length);
0247 if (ret < 0)
0248 return ret;
0249 if (rx_cfg->enable_length_byte == OPTION_ON) {
0250 ret = rf69_set_payload_length(dev->spi, 0xff);
0251 if (ret < 0)
0252 return ret;
0253 } else if (rx_cfg->fixed_message_length != 0) {
0254 payload_length = rx_cfg->fixed_message_length;
0255 if (rx_cfg->enable_length_byte == OPTION_ON)
0256 payload_length++;
0257 if (rx_cfg->enable_address_filtering != filtering_off)
0258 payload_length++;
0259 ret = rf69_set_payload_length(dev->spi, payload_length);
0260 if (ret < 0)
0261 return ret;
0262 } else {
0263 ret = rf69_set_payload_length(dev->spi, 0);
0264 if (ret < 0)
0265 return ret;
0266 }
0267
0268
0269 if (rx_cfg->enable_sync == OPTION_ON) {
0270 ret = rf69_set_sync_values(dev->spi, rx_cfg->sync_pattern);
0271 if (ret < 0)
0272 return ret;
0273 }
0274 if (rx_cfg->enable_address_filtering != filtering_off) {
0275 ret = rf69_set_node_address(dev->spi, rx_cfg->node_address);
0276 if (ret < 0)
0277 return ret;
0278 ret = rf69_set_broadcast_address(dev->spi,
0279 rx_cfg->broadcast_address);
0280 if (ret < 0)
0281 return ret;
0282 }
0283
0284 return 0;
0285 }
0286
0287 static int
0288 rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg)
0289 {
0290 int ret;
0291
0292 ret = rf69_set_frequency(dev->spi, tx_cfg->frequency);
0293 if (ret < 0)
0294 return ret;
0295 ret = rf69_set_modulation(dev->spi, tx_cfg->modulation);
0296 if (ret < 0)
0297 return ret;
0298 ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate);
0299 if (ret < 0)
0300 return ret;
0301 ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency);
0302 if (ret < 0)
0303 return ret;
0304 ret = rf69_set_pa_ramp(dev->spi, tx_cfg->pa_ramp);
0305 if (ret < 0)
0306 return ret;
0307 ret = rf69_set_modulation_shaping(dev->spi, tx_cfg->mod_shaping);
0308 if (ret < 0)
0309 return ret;
0310 ret = rf69_set_tx_start_condition(dev->spi, tx_cfg->tx_start_condition);
0311 if (ret < 0)
0312 return ret;
0313
0314
0315 if (tx_cfg->enable_preamble == OPTION_ON) {
0316 ret = rf69_set_preamble_length(dev->spi,
0317 tx_cfg->preamble_length);
0318 if (ret < 0)
0319 return ret;
0320 } else {
0321 ret = rf69_set_preamble_length(dev->spi, 0);
0322 if (ret < 0)
0323 return ret;
0324 }
0325
0326 if (tx_cfg->enable_sync == OPTION_ON) {
0327 ret = rf69_set_sync_size(dev->spi, tx_cfg->sync_length);
0328 if (ret < 0)
0329 return ret;
0330 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
0331 if (ret < 0)
0332 return ret;
0333 ret = rf69_enable_sync(dev->spi);
0334 if (ret < 0)
0335 return ret;
0336 } else {
0337 ret = rf69_disable_sync(dev->spi);
0338 if (ret < 0)
0339 return ret;
0340 }
0341
0342 if (tx_cfg->enable_length_byte == OPTION_ON) {
0343 ret = rf69_set_packet_format(dev->spi, packet_length_var);
0344 if (ret < 0)
0345 return ret;
0346 } else {
0347 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
0348 if (ret < 0)
0349 return ret;
0350 }
0351
0352 if (tx_cfg->enable_crc == OPTION_ON) {
0353 ret = rf69_enable_crc(dev->spi);
0354 if (ret < 0)
0355 return ret;
0356 } else {
0357 ret = rf69_disable_crc(dev->spi);
0358 if (ret < 0)
0359 return ret;
0360 }
0361
0362 return 0;
0363 }
0364
0365
0366
0367 static int pi433_start_rx(struct pi433_device *dev)
0368 {
0369 int retval;
0370
0371
0372 if (!dev->rx_active)
0373 return 0;
0374
0375
0376 retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
0377 if (retval)
0378 return retval;
0379
0380
0381 retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
0382 if (retval < 0)
0383 return retval;
0384 dev->irq_state[DIO0] = DIO_RSSI_DIO0;
0385 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
0386
0387
0388 retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
0389 if (retval < 0)
0390 return retval;
0391 retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
0392 if (retval < 0)
0393 return retval;
0394 dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
0395 irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
0396
0397
0398 retval = rf69_set_mode(dev->spi, receive);
0399 if (retval < 0)
0400 return retval;
0401
0402 return 0;
0403 }
0404
0405
0406
0407 static int pi433_receive(void *data)
0408 {
0409 struct pi433_device *dev = data;
0410 struct spi_device *spi = dev->spi;
0411 int bytes_to_read, bytes_total;
0412 int retval;
0413
0414 dev->interrupt_rx_allowed = false;
0415
0416
0417 dev_dbg(dev->dev, "rx: going to wait for any tx to finish\n");
0418 retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
0419 if (retval) {
0420
0421 dev->interrupt_rx_allowed = true;
0422 wake_up_interruptible(&dev->tx_wait_queue);
0423 return retval;
0424 }
0425
0426
0427 dev->free_in_fifo = FIFO_SIZE;
0428 dev->rx_position = 0;
0429 dev->rx_bytes_dropped = 0;
0430
0431
0432 retval = pi433_start_rx(dev);
0433 if (retval)
0434 return retval;
0435
0436
0437 while (!(rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RSSI)) {
0438
0439 dev->interrupt_rx_allowed = true;
0440 wake_up_interruptible(&dev->tx_wait_queue);
0441
0442
0443 dev_dbg(dev->dev, "rx: going to wait for high RSSI level\n");
0444 retval = wait_event_interruptible(dev->rx_wait_queue,
0445 rf69_read_reg(spi, REG_IRQFLAGS1) &
0446 MASK_IRQFLAGS1_RSSI);
0447 if (retval)
0448 goto abort;
0449 dev->interrupt_rx_allowed = false;
0450
0451
0452 if (!dev->tx_active)
0453 break;
0454 }
0455
0456
0457 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
0458 if (retval < 0)
0459 goto abort;
0460 dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
0461 irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
0462
0463
0464 if (dev->rx_cfg.fixed_message_length != 0) {
0465 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
0466 retval = -1;
0467 goto abort;
0468 }
0469 bytes_total = dev->rx_cfg.fixed_message_length;
0470 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length\n",
0471 bytes_total);
0472 } else {
0473 bytes_total = dev->rx_buffer_size;
0474 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read\n",
0475 bytes_total);
0476 }
0477
0478
0479 if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
0480 retval = wait_event_interruptible(dev->fifo_wait_queue,
0481 dev->free_in_fifo < FIFO_SIZE);
0482 if (retval)
0483 goto abort;
0484
0485 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
0486 if (bytes_total > dev->rx_buffer_size) {
0487 retval = -1;
0488 goto abort;
0489 }
0490 dev->free_in_fifo++;
0491 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte\n",
0492 bytes_total);
0493 }
0494
0495
0496 if (dev->rx_cfg.enable_address_filtering != filtering_off) {
0497 u8 dummy;
0498
0499 bytes_total--;
0500
0501 retval = wait_event_interruptible(dev->fifo_wait_queue,
0502 dev->free_in_fifo < FIFO_SIZE);
0503 if (retval)
0504 goto abort;
0505
0506 rf69_read_fifo(spi, &dummy, 1);
0507 dev->free_in_fifo++;
0508 dev_dbg(dev->dev, "rx: address byte stripped off\n");
0509 }
0510
0511
0512 while (dev->rx_position < bytes_total) {
0513 if (!(rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PAYLOAD_READY)) {
0514 retval = wait_event_interruptible(dev->fifo_wait_queue,
0515 dev->free_in_fifo < FIFO_SIZE);
0516 if (retval)
0517 goto abort;
0518 }
0519
0520
0521 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
0522 bytes_to_read = dev->rx_bytes_to_drop -
0523 dev->rx_bytes_dropped;
0524 else
0525 bytes_to_read = bytes_total - dev->rx_position;
0526
0527
0528 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
0529 bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
0530 retval = rf69_read_fifo(spi,
0531 &dev->rx_buffer[dev->rx_position],
0532 bytes_to_read);
0533 if (retval)
0534 goto abort;
0535
0536 dev->free_in_fifo += bytes_to_read;
0537
0538
0539 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
0540 dev->rx_bytes_dropped += bytes_to_read;
0541 else
0542 dev->rx_position += bytes_to_read;
0543 }
0544
0545
0546 abort:
0547 dev->interrupt_rx_allowed = true;
0548 if (rf69_set_mode(dev->spi, standby))
0549 pr_err("rf69_set_mode(): radio module failed to go standby\n");
0550 wake_up_interruptible(&dev->tx_wait_queue);
0551
0552 if (retval)
0553 return retval;
0554 else
0555 return bytes_total;
0556 }
0557
0558 static int pi433_tx_thread(void *data)
0559 {
0560 struct pi433_device *device = data;
0561 struct spi_device *spi = device->spi;
0562 struct pi433_tx_cfg tx_cfg;
0563 size_t size;
0564 bool rx_interrupted = false;
0565 int position, repetitions;
0566 int retval;
0567
0568 while (1) {
0569
0570 dev_dbg(device->dev, "thread: going to wait for new messages\n");
0571 wait_event_interruptible(device->tx_wait_queue,
0572 (!kfifo_is_empty(&device->tx_fifo) ||
0573 kthread_should_stop()));
0574 if (kthread_should_stop())
0575 return 0;
0576
0577
0578
0579
0580
0581
0582
0583 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
0584 if (retval != sizeof(tx_cfg)) {
0585 dev_dbg(device->dev,
0586 "reading tx_cfg from fifo failed: got %d byte(s), expected %d\n",
0587 retval, (unsigned int)sizeof(tx_cfg));
0588 continue;
0589 }
0590
0591 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
0592 if (retval != sizeof(size_t)) {
0593 dev_dbg(device->dev,
0594 "reading msg size from fifo failed: got %d, expected %d\n",
0595 retval, (unsigned int)sizeof(size_t));
0596 continue;
0597 }
0598
0599
0600 if (tx_cfg.fixed_message_length != 0)
0601 size = tx_cfg.fixed_message_length;
0602
0603
0604 if (tx_cfg.enable_length_byte == OPTION_ON)
0605 size++;
0606
0607
0608 if (tx_cfg.enable_address_byte == OPTION_ON)
0609 size++;
0610
0611
0612 memset(device->buffer, 0, size);
0613 position = 0;
0614
0615
0616 if (tx_cfg.enable_length_byte == OPTION_ON)
0617
0618
0619
0620
0621 device->buffer[position++] = size - 1;
0622
0623
0624 if (tx_cfg.enable_address_byte == OPTION_ON)
0625 device->buffer[position++] = tx_cfg.address_byte;
0626
0627
0628 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
0629 sizeof(device->buffer) - position);
0630 dev_dbg(device->dev,
0631 "read %d message byte(s) from fifo queue.\n", retval);
0632
0633
0634
0635
0636
0637
0638
0639
0640 wait_event_interruptible(device->tx_wait_queue,
0641 !device->rx_active ||
0642 device->interrupt_rx_allowed);
0643
0644
0645
0646
0647
0648 disable_irq(device->irq_num[DIO0]);
0649 device->tx_active = true;
0650
0651
0652 retval = rf69_set_mode(spi, standby);
0653 if (retval < 0)
0654 goto abort;
0655
0656 if (device->rx_active && !rx_interrupted) {
0657
0658
0659
0660
0661 rx_interrupted = true;
0662 }
0663
0664 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
0665 if (retval < 0)
0666 goto abort;
0667 if (tx_cfg.enable_length_byte == OPTION_ON) {
0668 retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
0669 if (retval < 0)
0670 goto abort;
0671 } else {
0672 retval = rf69_set_payload_length(spi, 0);
0673 if (retval < 0)
0674 goto abort;
0675 }
0676
0677
0678 retval = rf69_set_tx_cfg(device, &tx_cfg);
0679 if (retval < 0)
0680 goto abort;
0681
0682
0683 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
0684 if (retval < 0)
0685 goto abort;
0686 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
0687 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
0688
0689
0690 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
0691 if (retval < 0)
0692 goto abort;
0693 device->irq_state[DIO0] = DIO_PACKET_SENT;
0694 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
0695 enable_irq(device->irq_num[DIO0]);
0696
0697
0698 retval = rf69_set_mode(spi, transmit);
0699 if (retval < 0)
0700 goto abort;
0701
0702
0703 device->free_in_fifo = FIFO_SIZE;
0704 position = 0;
0705 repetitions = tx_cfg.repetitions;
0706 while ((repetitions > 0) && (size > position)) {
0707 if ((size - position) > device->free_in_fifo) {
0708
0709 int write_size = device->free_in_fifo;
0710
0711 device->free_in_fifo = 0;
0712 rf69_write_fifo(spi,
0713 &device->buffer[position],
0714 write_size);
0715 position += write_size;
0716 } else {
0717
0718 device->free_in_fifo -= size;
0719 repetitions--;
0720 rf69_write_fifo(spi,
0721 &device->buffer[position],
0722 (size - position));
0723 position = 0;
0724 }
0725
0726 retval = wait_event_interruptible(device->fifo_wait_queue,
0727 device->free_in_fifo > 0);
0728 if (retval) {
0729 dev_dbg(device->dev, "ABORT\n");
0730 goto abort;
0731 }
0732 }
0733
0734
0735 dev_dbg(device->dev,
0736 "thread: wait for packet to get sent/fifo to be empty\n");
0737 wait_event_interruptible(device->fifo_wait_queue,
0738 device->free_in_fifo == FIFO_SIZE ||
0739 kthread_should_stop());
0740 if (kthread_should_stop())
0741 return 0;
0742
0743
0744 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.\n");
0745 retval = rf69_set_mode(spi, standby);
0746 if (retval < 0)
0747 goto abort;
0748
0749
0750 if (kfifo_is_empty(&device->tx_fifo)) {
0751 abort:
0752 if (rx_interrupted) {
0753 rx_interrupted = false;
0754 pi433_start_rx(device);
0755 }
0756 device->tx_active = false;
0757 wake_up_interruptible(&device->rx_wait_queue);
0758 }
0759 }
0760 }
0761
0762
0763
0764 static ssize_t
0765 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
0766 {
0767 struct pi433_instance *instance;
0768 struct pi433_device *device;
0769 int bytes_received;
0770 ssize_t retval;
0771
0772
0773 if (size > MAX_MSG_SIZE)
0774 return -EMSGSIZE;
0775
0776 instance = filp->private_data;
0777 device = instance->device;
0778
0779
0780 mutex_lock(&device->rx_lock);
0781 if (device->rx_active) {
0782 mutex_unlock(&device->rx_lock);
0783 return -EAGAIN;
0784 }
0785
0786 device->rx_active = true;
0787 mutex_unlock(&device->rx_lock);
0788
0789
0790
0791 device->rx_buffer_size = size;
0792 bytes_received = pi433_receive(device);
0793
0794
0795 mutex_lock(&device->rx_lock);
0796 device->rx_active = false;
0797 mutex_unlock(&device->rx_lock);
0798
0799
0800 if (bytes_received > 0) {
0801 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
0802 if (retval)
0803 return -EFAULT;
0804 }
0805
0806 return bytes_received;
0807 }
0808
0809 static ssize_t
0810 pi433_write(struct file *filp, const char __user *buf,
0811 size_t count, loff_t *f_pos)
0812 {
0813 struct pi433_instance *instance;
0814 struct pi433_device *device;
0815 int retval;
0816 unsigned int required, available, copied;
0817
0818 instance = filp->private_data;
0819 device = instance->device;
0820
0821
0822
0823
0824
0825 if (count > MAX_MSG_SIZE)
0826 return -EMSGSIZE;
0827
0828
0829
0830
0831
0832 if (!instance->tx_cfg_initialized) {
0833 dev_notice_once(device->dev,
0834 "write: failed due to unconfigured tx_cfg (see PI433_IOC_WR_TX_CFG)\n");
0835 return -EINVAL;
0836 }
0837
0838
0839
0840
0841
0842
0843
0844 mutex_lock(&device->tx_fifo_lock);
0845
0846 required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
0847 available = kfifo_avail(&device->tx_fifo);
0848 if (required > available) {
0849 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available\n",
0850 required, available);
0851 mutex_unlock(&device->tx_fifo_lock);
0852 return -EAGAIN;
0853 }
0854
0855 retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
0856 sizeof(instance->tx_cfg));
0857 if (retval != sizeof(instance->tx_cfg))
0858 goto abort;
0859
0860 retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
0861 if (retval != sizeof(size_t))
0862 goto abort;
0863
0864 retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
0865 if (retval || copied != count)
0866 goto abort;
0867
0868 mutex_unlock(&device->tx_fifo_lock);
0869
0870
0871 wake_up_interruptible(&device->tx_wait_queue);
0872 dev_dbg(device->dev, "write: generated new msg with %d bytes.\n", copied);
0873
0874 return copied;
0875
0876 abort:
0877 dev_warn(device->dev,
0878 "write to fifo failed, non recoverable: 0x%x\n", retval);
0879 mutex_unlock(&device->tx_fifo_lock);
0880 return -EAGAIN;
0881 }
0882
0883 static long pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0884 {
0885 struct pi433_instance *instance;
0886 struct pi433_device *device;
0887 struct pi433_tx_cfg tx_cfg;
0888 void __user *argp = (void __user *)arg;
0889
0890
0891 if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
0892 return -ENOTTY;
0893
0894 instance = filp->private_data;
0895 device = instance->device;
0896
0897 if (!device)
0898 return -ESHUTDOWN;
0899
0900 switch (cmd) {
0901 case PI433_IOC_RD_TX_CFG:
0902 if (copy_to_user(argp, &instance->tx_cfg,
0903 sizeof(struct pi433_tx_cfg)))
0904 return -EFAULT;
0905 break;
0906 case PI433_IOC_WR_TX_CFG:
0907 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
0908 return -EFAULT;
0909 mutex_lock(&device->tx_fifo_lock);
0910 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
0911 instance->tx_cfg_initialized = true;
0912 mutex_unlock(&device->tx_fifo_lock);
0913 break;
0914 case PI433_IOC_RD_RX_CFG:
0915 if (copy_to_user(argp, &device->rx_cfg,
0916 sizeof(struct pi433_rx_cfg)))
0917 return -EFAULT;
0918 break;
0919 case PI433_IOC_WR_RX_CFG:
0920 mutex_lock(&device->rx_lock);
0921
0922
0923 if (device->rx_active) {
0924 mutex_unlock(&device->rx_lock);
0925 return -EAGAIN;
0926 }
0927
0928 if (copy_from_user(&device->rx_cfg, argp,
0929 sizeof(struct pi433_rx_cfg))) {
0930 mutex_unlock(&device->rx_lock);
0931 return -EFAULT;
0932 }
0933
0934 mutex_unlock(&device->rx_lock);
0935 break;
0936 default:
0937 return -EINVAL;
0938 }
0939
0940 return 0;
0941 }
0942
0943
0944
0945 static int pi433_open(struct inode *inode, struct file *filp)
0946 {
0947 struct pi433_device *device;
0948 struct pi433_instance *instance;
0949
0950 mutex_lock(&minor_lock);
0951 device = idr_find(&pi433_idr, iminor(inode));
0952 mutex_unlock(&minor_lock);
0953 if (!device) {
0954 pr_debug("device: minor %d unknown.\n", iminor(inode));
0955 return -ENODEV;
0956 }
0957
0958 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
0959 if (!instance)
0960 return -ENOMEM;
0961
0962
0963 instance->device = device;
0964
0965
0966 filp->private_data = instance;
0967 stream_open(inode, filp);
0968
0969 return 0;
0970 }
0971
0972 static int pi433_release(struct inode *inode, struct file *filp)
0973 {
0974 struct pi433_instance *instance;
0975
0976 instance = filp->private_data;
0977 kfree(instance);
0978 filp->private_data = NULL;
0979
0980 return 0;
0981 }
0982
0983
0984
0985 static int setup_gpio(struct pi433_device *device)
0986 {
0987 char name[5];
0988 int retval;
0989 int i;
0990 const irq_handler_t DIO_irq_handler[NUM_DIO] = {
0991 DIO0_irq_handler,
0992 DIO1_irq_handler
0993 };
0994
0995 for (i = 0; i < NUM_DIO; i++) {
0996
0997 snprintf(name, sizeof(name), "DIO%d", i);
0998 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
0999 0 );
1000
1001 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1002 dev_dbg(&device->spi->dev,
1003 "Could not find entry for %s. Ignoring.\n", name);
1004 continue;
1005 }
1006
1007 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1008 dev_dbg(&device->spi->dev, "%s is busy.\n", name);
1009
1010 if (IS_ERR(device->gpiod[i])) {
1011 retval = PTR_ERR(device->gpiod[i]);
1012
1013 for (i--; i >= 0; i--) {
1014 free_irq(device->irq_num[i], device);
1015 gpiod_put(device->gpiod[i]);
1016 }
1017 return retval;
1018 }
1019
1020
1021 gpiod_unexport(device->gpiod[i]);
1022 retval = gpiod_direction_input(device->gpiod[i]);
1023 if (retval)
1024 return retval;
1025
1026
1027 device->irq_num[i] = gpiod_to_irq(device->gpiod[i]);
1028 if (device->irq_num[i] < 0) {
1029 device->gpiod[i] = ERR_PTR(-EINVAL);
1030 return device->irq_num[i];
1031 }
1032 retval = request_irq(device->irq_num[i],
1033 DIO_irq_handler[i],
1034 0,
1035 name,
1036 device);
1037
1038 if (retval)
1039 return retval;
1040
1041 dev_dbg(&device->spi->dev, "%s successfully configured\n", name);
1042 }
1043
1044 return 0;
1045 }
1046
1047 static void free_gpio(struct pi433_device *device)
1048 {
1049 int i;
1050
1051 for (i = 0; i < NUM_DIO; i++) {
1052
1053 if (IS_ERR(device->gpiod[i]))
1054 continue;
1055
1056 free_irq(device->irq_num[i], device);
1057 gpiod_put(device->gpiod[i]);
1058 }
1059 }
1060
1061 static int pi433_get_minor(struct pi433_device *device)
1062 {
1063 int retval = -ENOMEM;
1064
1065 mutex_lock(&minor_lock);
1066 retval = idr_alloc(&pi433_idr, device, 0, N_PI433_MINORS, GFP_KERNEL);
1067 if (retval >= 0) {
1068 device->minor = retval;
1069 retval = 0;
1070 } else if (retval == -ENOSPC) {
1071 dev_err(&device->spi->dev, "too many pi433 devices\n");
1072 retval = -EINVAL;
1073 }
1074 mutex_unlock(&minor_lock);
1075 return retval;
1076 }
1077
1078 static void pi433_free_minor(struct pi433_device *dev)
1079 {
1080 mutex_lock(&minor_lock);
1081 idr_remove(&pi433_idr, dev->minor);
1082 mutex_unlock(&minor_lock);
1083 }
1084
1085
1086
1087 static const struct file_operations pi433_fops = {
1088 .owner = THIS_MODULE,
1089
1090
1091
1092
1093
1094 .write = pi433_write,
1095 .read = pi433_read,
1096 .unlocked_ioctl = pi433_ioctl,
1097 .compat_ioctl = compat_ptr_ioctl,
1098 .open = pi433_open,
1099 .release = pi433_release,
1100 .llseek = no_llseek,
1101 };
1102
1103 static int pi433_debugfs_regs_show(struct seq_file *m, void *p)
1104 {
1105 struct pi433_device *dev;
1106 u8 reg_data[114];
1107 int i;
1108 char *fmt = "0x%02x, 0x%02x\n";
1109 int ret;
1110
1111 dev = m->private;
1112
1113 mutex_lock(&dev->tx_fifo_lock);
1114 mutex_lock(&dev->rx_lock);
1115
1116
1117 ret = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
1118 if (ret)
1119 goto out_unlock;
1120
1121 ret = wait_event_interruptible(dev->tx_wait_queue, !dev->rx_active);
1122 if (ret)
1123 goto out_unlock;
1124
1125
1126 for (i = 1; i < 0x50; i++)
1127 reg_data[i] = rf69_read_reg(dev->spi, i);
1128
1129 reg_data[REG_TESTLNA] = rf69_read_reg(dev->spi, REG_TESTLNA);
1130 reg_data[REG_TESTPA1] = rf69_read_reg(dev->spi, REG_TESTPA1);
1131 reg_data[REG_TESTPA2] = rf69_read_reg(dev->spi, REG_TESTPA2);
1132 reg_data[REG_TESTDAGC] = rf69_read_reg(dev->spi, REG_TESTDAGC);
1133 reg_data[REG_TESTAFC] = rf69_read_reg(dev->spi, REG_TESTAFC);
1134
1135 seq_puts(m, "# reg, val\n");
1136
1137 for (i = 1; i < 0x50; i++)
1138 seq_printf(m, fmt, i, reg_data[i]);
1139
1140 seq_printf(m, fmt, REG_TESTLNA, reg_data[REG_TESTLNA]);
1141 seq_printf(m, fmt, REG_TESTPA1, reg_data[REG_TESTPA1]);
1142 seq_printf(m, fmt, REG_TESTPA2, reg_data[REG_TESTPA2]);
1143 seq_printf(m, fmt, REG_TESTDAGC, reg_data[REG_TESTDAGC]);
1144 seq_printf(m, fmt, REG_TESTAFC, reg_data[REG_TESTAFC]);
1145
1146 out_unlock:
1147 mutex_unlock(&dev->rx_lock);
1148 mutex_unlock(&dev->tx_fifo_lock);
1149
1150 return ret;
1151 }
1152
1153 static int pi433_debugfs_regs_open(struct inode *inode, struct file *filp)
1154 {
1155 return single_open(filp, pi433_debugfs_regs_show, inode->i_private);
1156 }
1157
1158 static const struct file_operations debugfs_fops = {
1159 .llseek = seq_lseek,
1160 .open = pi433_debugfs_regs_open,
1161 .owner = THIS_MODULE,
1162 .read = seq_read,
1163 .release = single_release
1164 };
1165
1166
1167
1168 static int pi433_probe(struct spi_device *spi)
1169 {
1170 struct pi433_device *device;
1171 int retval;
1172 struct dentry *entry;
1173
1174
1175 spi->mode = 0x00;
1176 spi->bits_per_word = 8;
1177
1178
1179
1180
1181
1182 retval = spi_setup(spi);
1183 if (retval) {
1184 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1185 return retval;
1186 }
1187
1188 dev_dbg(&spi->dev,
1189 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed\n",
1190 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1191
1192
1193 retval = rf69_get_version(spi);
1194 if (retval < 0)
1195 return retval;
1196
1197 switch (retval) {
1198 case 0x24:
1199 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)\n", retval);
1200 break;
1201 default:
1202 dev_dbg(&spi->dev, "unknown chip version: 0x%x\n", retval);
1203 return -ENODEV;
1204 }
1205
1206
1207 device = kzalloc(sizeof(*device), GFP_KERNEL);
1208 if (!device)
1209 return -ENOMEM;
1210
1211
1212 device->spi = spi;
1213 device->rx_active = false;
1214 device->tx_active = false;
1215 device->interrupt_rx_allowed = false;
1216
1217
1218 device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1219 if (!device->rx_buffer) {
1220 retval = -ENOMEM;
1221 goto RX_failed;
1222 }
1223
1224
1225 init_waitqueue_head(&device->tx_wait_queue);
1226 init_waitqueue_head(&device->rx_wait_queue);
1227 init_waitqueue_head(&device->fifo_wait_queue);
1228
1229
1230 INIT_KFIFO(device->tx_fifo);
1231
1232
1233 mutex_init(&device->tx_fifo_lock);
1234 mutex_init(&device->rx_lock);
1235
1236
1237 retval = setup_gpio(device);
1238 if (retval) {
1239 dev_dbg(&spi->dev, "setup of GPIOs failed\n");
1240 goto GPIO_failed;
1241 }
1242
1243
1244 retval = rf69_set_mode(spi, standby);
1245 if (retval < 0)
1246 goto minor_failed;
1247 retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1248 if (retval < 0)
1249 goto minor_failed;
1250 retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1251 if (retval < 0)
1252 goto minor_failed;
1253 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1254 if (retval < 0)
1255 goto minor_failed;
1256 retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1257 if (retval < 0)
1258 goto minor_failed;
1259 retval = rf69_set_output_power_level(spi, 13);
1260 if (retval < 0)
1261 goto minor_failed;
1262 retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1263 if (retval < 0)
1264 goto minor_failed;
1265
1266
1267 retval = pi433_get_minor(device);
1268 if (retval) {
1269 dev_dbg(&spi->dev, "get of minor number failed\n");
1270 goto minor_failed;
1271 }
1272
1273
1274 device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1275 device->dev = device_create(pi433_class,
1276 &spi->dev,
1277 device->devt,
1278 device,
1279 "pi433.%d",
1280 device->minor);
1281 if (IS_ERR(device->dev)) {
1282 pr_err("pi433: device register failed\n");
1283 retval = PTR_ERR(device->dev);
1284 goto device_create_failed;
1285 } else {
1286 dev_dbg(device->dev,
1287 "created device for major %d, minor %d\n",
1288 MAJOR(pi433_dev),
1289 device->minor);
1290 }
1291
1292
1293 device->tx_task_struct = kthread_run(pi433_tx_thread,
1294 device,
1295 "pi433.%d_tx_task",
1296 device->minor);
1297 if (IS_ERR(device->tx_task_struct)) {
1298 dev_dbg(device->dev, "start of send thread failed\n");
1299 retval = PTR_ERR(device->tx_task_struct);
1300 goto send_thread_failed;
1301 }
1302
1303
1304 device->cdev = cdev_alloc();
1305 if (!device->cdev) {
1306 dev_dbg(device->dev, "allocation of cdev failed\n");
1307 retval = -ENOMEM;
1308 goto cdev_failed;
1309 }
1310 device->cdev->owner = THIS_MODULE;
1311 cdev_init(device->cdev, &pi433_fops);
1312 retval = cdev_add(device->cdev, device->devt, 1);
1313 if (retval) {
1314 dev_dbg(device->dev, "register of cdev failed\n");
1315 goto del_cdev;
1316 }
1317
1318
1319 spi_set_drvdata(spi, device);
1320
1321 entry = debugfs_create_dir(dev_name(device->dev),
1322 debugfs_lookup(KBUILD_MODNAME, NULL));
1323 debugfs_create_file("regs", 0400, entry, device, &debugfs_fops);
1324
1325 return 0;
1326
1327 del_cdev:
1328 cdev_del(device->cdev);
1329 cdev_failed:
1330 kthread_stop(device->tx_task_struct);
1331 send_thread_failed:
1332 device_destroy(pi433_class, device->devt);
1333 device_create_failed:
1334 pi433_free_minor(device);
1335 minor_failed:
1336 free_gpio(device);
1337 GPIO_failed:
1338 kfree(device->rx_buffer);
1339 RX_failed:
1340 kfree(device);
1341
1342 return retval;
1343 }
1344
1345 static void pi433_remove(struct spi_device *spi)
1346 {
1347 struct pi433_device *device = spi_get_drvdata(spi);
1348 struct dentry *mod_entry = debugfs_lookup(KBUILD_MODNAME, NULL);
1349
1350 debugfs_remove(debugfs_lookup(dev_name(device->dev), mod_entry));
1351
1352
1353 free_gpio(device);
1354
1355
1356 device->spi = NULL;
1357
1358 kthread_stop(device->tx_task_struct);
1359
1360 device_destroy(pi433_class, device->devt);
1361
1362 cdev_del(device->cdev);
1363
1364 pi433_free_minor(device);
1365
1366 kfree(device->rx_buffer);
1367 kfree(device);
1368 }
1369
1370 static const struct of_device_id pi433_dt_ids[] = {
1371 { .compatible = "Smarthome-Wolf,pi433" },
1372 {},
1373 };
1374
1375 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1376
1377 static struct spi_driver pi433_spi_driver = {
1378 .driver = {
1379 .name = "pi433",
1380 .owner = THIS_MODULE,
1381 .of_match_table = of_match_ptr(pi433_dt_ids),
1382 },
1383 .probe = pi433_probe,
1384 .remove = pi433_remove,
1385
1386
1387
1388
1389
1390
1391
1392 };
1393
1394
1395
1396 static int __init pi433_init(void)
1397 {
1398 int status;
1399
1400
1401
1402
1403
1404 if (MAX_MSG_SIZE < FIFO_SIZE)
1405 return -EINVAL;
1406
1407
1408
1409
1410
1411
1412 status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1413 if (status < 0)
1414 return status;
1415
1416 pi433_class = class_create(THIS_MODULE, "pi433");
1417 if (IS_ERR(pi433_class)) {
1418 unregister_chrdev(MAJOR(pi433_dev),
1419 pi433_spi_driver.driver.name);
1420 return PTR_ERR(pi433_class);
1421 }
1422
1423 debugfs_create_dir(KBUILD_MODNAME, NULL);
1424
1425 status = spi_register_driver(&pi433_spi_driver);
1426 if (status < 0) {
1427 class_destroy(pi433_class);
1428 unregister_chrdev(MAJOR(pi433_dev),
1429 pi433_spi_driver.driver.name);
1430 }
1431
1432 return status;
1433 }
1434
1435 module_init(pi433_init);
1436
1437 static void __exit pi433_exit(void)
1438 {
1439 spi_unregister_driver(&pi433_spi_driver);
1440 class_destroy(pi433_class);
1441 unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1442 debugfs_remove_recursive(debugfs_lookup(KBUILD_MODNAME, NULL));
1443 }
1444 module_exit(pi433_exit);
1445
1446 MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1447 MODULE_DESCRIPTION("Driver for Pi433");
1448 MODULE_LICENSE("GPL");
1449 MODULE_ALIAS("spi:pi433");