root/drivers/staging/pi433/pi433_if.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. DIO0_irq_handler
  2. DIO1_irq_handler
  3. rf69_set_rx_cfg
  4. rf69_set_tx_cfg
  5. pi433_start_rx
  6. pi433_receive
  7. pi433_tx_thread
  8. pi433_read
  9. pi433_write
  10. pi433_ioctl
  11. pi433_compat_ioctl
  12. pi433_open
  13. pi433_release
  14. setup_gpio
  15. free_gpio
  16. pi433_get_minor
  17. pi433_free_minor
  18. pi433_probe
  19. pi433_remove
  20. pi433_init
  21. pi433_exit

   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_set_sync_size(dev->spi, tx_cfg->sync_length);
 323                 if (ret < 0)
 324                         return ret;
 325                 ret = rf69_set_sync_values(dev->spi, tx_cfg->sync_pattern);
 326                 if (ret < 0)
 327                         return ret;
 328                 ret = rf69_enable_sync(dev->spi);
 329                 if (ret < 0)
 330                         return ret;
 331         } else {
 332                 ret = rf69_disable_sync(dev->spi);
 333                 if (ret < 0)
 334                         return ret;
 335         }
 336 
 337         if (tx_cfg->enable_length_byte == OPTION_ON) {
 338                 ret = rf69_set_packet_format(dev->spi, packet_length_var);
 339                 if (ret < 0)
 340                         return ret;
 341         } else {
 342                 ret = rf69_set_packet_format(dev->spi, packet_length_fix);
 343                 if (ret < 0)
 344                         return ret;
 345         }
 346 
 347         if (tx_cfg->enable_crc == OPTION_ON) {
 348                 ret = rf69_enable_crc(dev->spi);
 349                 if (ret < 0)
 350                         return ret;
 351         } else {
 352                 ret = rf69_disable_crc(dev->spi);
 353                 if (ret < 0)
 354                         return ret;
 355         }
 356 
 357         return 0;
 358 }
 359 
 360 /*-------------------------------------------------------------------------*/
 361 
 362 static int
 363 pi433_start_rx(struct pi433_device *dev)
 364 {
 365         int retval;
 366 
 367         /* return without action, if no pending read request */
 368         if (!dev->rx_active)
 369                 return 0;
 370 
 371         /* setup for receiving */
 372         retval = rf69_set_rx_cfg(dev, &dev->rx_cfg);
 373         if (retval)
 374                 return retval;
 375 
 376         /* setup rssi irq */
 377         retval = rf69_set_dio_mapping(dev->spi, DIO0, DIO_RSSI_DIO0);
 378         if (retval < 0)
 379                 return retval;
 380         dev->irq_state[DIO0] = DIO_RSSI_DIO0;
 381         irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
 382 
 383         /* setup fifo level interrupt */
 384         retval = rf69_set_fifo_threshold(dev->spi, FIFO_SIZE - FIFO_THRESHOLD);
 385         if (retval < 0)
 386                 return retval;
 387         retval = rf69_set_dio_mapping(dev->spi, DIO1, DIO_FIFO_LEVEL);
 388         if (retval < 0)
 389                 return retval;
 390         dev->irq_state[DIO1] = DIO_FIFO_LEVEL;
 391         irq_set_irq_type(dev->irq_num[DIO1], IRQ_TYPE_EDGE_RISING);
 392 
 393         /* set module to receiving mode */
 394         retval = rf69_set_mode(dev->spi, receive);
 395         if (retval < 0)
 396                 return retval;
 397 
 398         return 0;
 399 }
 400 
 401 /*-------------------------------------------------------------------------*/
 402 
 403 static int
 404 pi433_receive(void *data)
 405 {
 406         struct pi433_device *dev = data;
 407         struct spi_device *spi = dev->spi;
 408         int bytes_to_read, bytes_total;
 409         int retval;
 410 
 411         dev->interrupt_rx_allowed = false;
 412 
 413         /* wait for any tx to finish */
 414         dev_dbg(dev->dev, "rx: going to wait for any tx to finish");
 415         retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active);
 416         if (retval) {
 417                 /* wait was interrupted */
 418                 dev->interrupt_rx_allowed = true;
 419                 wake_up_interruptible(&dev->tx_wait_queue);
 420                 return retval;
 421         }
 422 
 423         /* prepare status vars */
 424         dev->free_in_fifo = FIFO_SIZE;
 425         dev->rx_position = 0;
 426         dev->rx_bytes_dropped = 0;
 427 
 428         /* setup radio module to listen for something "in the air" */
 429         retval = pi433_start_rx(dev);
 430         if (retval)
 431                 return retval;
 432 
 433         /* now check RSSI, if low wait for getting high (RSSI interrupt) */
 434         while (!rf69_get_flag(dev->spi, rssi_exceeded_threshold)) {
 435                 /* allow tx to interrupt us while waiting for high RSSI */
 436                 dev->interrupt_rx_allowed = true;
 437                 wake_up_interruptible(&dev->tx_wait_queue);
 438 
 439                 /* wait for RSSI level to become high */
 440                 dev_dbg(dev->dev, "rx: going to wait for high RSSI level");
 441                 retval = wait_event_interruptible(dev->rx_wait_queue,
 442                                                   rf69_get_flag(dev->spi,
 443                                                                 rssi_exceeded_threshold));
 444                 if (retval) /* wait was interrupted */
 445                         goto abort;
 446                 dev->interrupt_rx_allowed = false;
 447 
 448                 /* cross check for ongoing tx */
 449                 if (!dev->tx_active)
 450                         break;
 451         }
 452 
 453         /* configure payload ready irq */
 454         retval = rf69_set_dio_mapping(spi, DIO0, DIO_PAYLOAD_READY);
 455         if (retval < 0)
 456                 goto abort;
 457         dev->irq_state[DIO0] = DIO_PAYLOAD_READY;
 458         irq_set_irq_type(dev->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
 459 
 460         /* fixed or unlimited length? */
 461         if (dev->rx_cfg.fixed_message_length != 0) {
 462                 if (dev->rx_cfg.fixed_message_length > dev->rx_buffer_size) {
 463                         retval = -1;
 464                         goto abort;
 465                 }
 466                 bytes_total = dev->rx_cfg.fixed_message_length;
 467                 dev_dbg(dev->dev, "rx: msg len set to %d by fixed length",
 468                         bytes_total);
 469         } else {
 470                 bytes_total = dev->rx_buffer_size;
 471                 dev_dbg(dev->dev, "rx: msg len set to %d as requested by read",
 472                         bytes_total);
 473         }
 474 
 475         /* length byte enabled? */
 476         if (dev->rx_cfg.enable_length_byte == OPTION_ON) {
 477                 retval = wait_event_interruptible(dev->fifo_wait_queue,
 478                                                   dev->free_in_fifo < FIFO_SIZE);
 479                 if (retval) /* wait was interrupted */
 480                         goto abort;
 481 
 482                 rf69_read_fifo(spi, (u8 *)&bytes_total, 1);
 483                 if (bytes_total > dev->rx_buffer_size) {
 484                         retval = -1;
 485                         goto abort;
 486                 }
 487                 dev->free_in_fifo++;
 488                 dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte",
 489                         bytes_total);
 490         }
 491 
 492         /* address byte enabled? */
 493         if (dev->rx_cfg.enable_address_filtering != filtering_off) {
 494                 u8 dummy;
 495 
 496                 bytes_total--;
 497 
 498                 retval = wait_event_interruptible(dev->fifo_wait_queue,
 499                                                   dev->free_in_fifo < FIFO_SIZE);
 500                 if (retval) /* wait was interrupted */
 501                         goto abort;
 502 
 503                 rf69_read_fifo(spi, &dummy, 1);
 504                 dev->free_in_fifo++;
 505                 dev_dbg(dev->dev, "rx: address byte stripped off");
 506         }
 507 
 508         /* get payload */
 509         while (dev->rx_position < bytes_total) {
 510                 if (!rf69_get_flag(dev->spi, payload_ready)) {
 511                         retval = wait_event_interruptible(dev->fifo_wait_queue,
 512                                                           dev->free_in_fifo < FIFO_SIZE);
 513                         if (retval) /* wait was interrupted */
 514                                 goto abort;
 515                 }
 516 
 517                 /* need to drop bytes or acquire? */
 518                 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
 519                         bytes_to_read = dev->rx_bytes_to_drop -
 520                                         dev->rx_bytes_dropped;
 521                 else
 522                         bytes_to_read = bytes_total - dev->rx_position;
 523 
 524                 /* access the fifo */
 525                 if (bytes_to_read > FIFO_SIZE - dev->free_in_fifo)
 526                         bytes_to_read = FIFO_SIZE - dev->free_in_fifo;
 527                 retval = rf69_read_fifo(spi,
 528                                         &dev->rx_buffer[dev->rx_position],
 529                                         bytes_to_read);
 530                 if (retval) /* read failed */
 531                         goto abort;
 532 
 533                 dev->free_in_fifo += bytes_to_read;
 534 
 535                 /* adjust status vars */
 536                 if (dev->rx_bytes_to_drop > dev->rx_bytes_dropped)
 537                         dev->rx_bytes_dropped += bytes_to_read;
 538                 else
 539                         dev->rx_position += bytes_to_read;
 540         }
 541 
 542         /* rx done, wait was interrupted or error occurred */
 543 abort:
 544         dev->interrupt_rx_allowed = true;
 545         if (rf69_set_mode(dev->spi, standby))
 546                 pr_err("rf69_set_mode(): radio module failed to go standby\n");
 547         wake_up_interruptible(&dev->tx_wait_queue);
 548 
 549         if (retval)
 550                 return retval;
 551         else
 552                 return bytes_total;
 553 }
 554 
 555 static int
 556 pi433_tx_thread(void *data)
 557 {
 558         struct pi433_device *device = data;
 559         struct spi_device *spi = device->spi;
 560         struct pi433_tx_cfg tx_cfg;
 561         size_t size;
 562         bool   rx_interrupted = false;
 563         int    position, repetitions;
 564         int    retval;
 565 
 566         while (1) {
 567                 /* wait for fifo to be populated or for request to terminate*/
 568                 dev_dbg(device->dev, "thread: going to wait for new messages");
 569                 wait_event_interruptible(device->tx_wait_queue,
 570                                          (!kfifo_is_empty(&device->tx_fifo) ||
 571                                           kthread_should_stop()));
 572                 if (kthread_should_stop())
 573                         return 0;
 574 
 575                 /*
 576                  * get data from fifo in the following order:
 577                  * - tx_cfg
 578                  * - size of message
 579                  * - message
 580                  */
 581                 retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg));
 582                 if (retval != sizeof(tx_cfg)) {
 583                         dev_dbg(device->dev,
 584                                 "reading tx_cfg from fifo failed: got %d byte(s), expected %d",
 585                                 retval, (unsigned int)sizeof(tx_cfg));
 586                         continue;
 587                 }
 588 
 589                 retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t));
 590                 if (retval != sizeof(size_t)) {
 591                         dev_dbg(device->dev,
 592                                 "reading msg size from fifo failed: got %d, expected %d",
 593                                 retval, (unsigned int)sizeof(size_t));
 594                         continue;
 595                 }
 596 
 597                 /* use fixed message length, if requested */
 598                 if (tx_cfg.fixed_message_length != 0)
 599                         size = tx_cfg.fixed_message_length;
 600 
 601                 /* increase size, if len byte is requested */
 602                 if (tx_cfg.enable_length_byte == OPTION_ON)
 603                         size++;
 604 
 605                 /* increase size, if adr byte is requested */
 606                 if (tx_cfg.enable_address_byte == OPTION_ON)
 607                         size++;
 608 
 609                 /* prime buffer */
 610                 memset(device->buffer, 0, size);
 611                 position = 0;
 612 
 613                 /* add length byte, if requested */
 614                 if (tx_cfg.enable_length_byte  == OPTION_ON)
 615                         /*
 616                          * according to spec, length byte itself must be
 617                          * excluded from the length calculation
 618                          */
 619                         device->buffer[position++] = size - 1;
 620 
 621                 /* add adr byte, if requested */
 622                 if (tx_cfg.enable_address_byte == OPTION_ON)
 623                         device->buffer[position++] = tx_cfg.address_byte;
 624 
 625                 /* finally get message data from fifo */
 626                 retval = kfifo_out(&device->tx_fifo, &device->buffer[position],
 627                                    sizeof(device->buffer) - position);
 628                 dev_dbg(device->dev,
 629                         "read %d message byte(s) from fifo queue.", retval);
 630 
 631                 /*
 632                  * if rx is active, we need to interrupt the waiting for
 633                  * incoming telegrams, to be able to send something.
 634                  * We are only allowed, if currently no reception takes
 635                  * place otherwise we need to  wait for the incoming telegram
 636                  * to finish
 637                  */
 638                 wait_event_interruptible(device->tx_wait_queue,
 639                                          !device->rx_active ||
 640                                           device->interrupt_rx_allowed);
 641 
 642                 /*
 643                  * prevent race conditions
 644                  * irq will be reenabled after tx config is set
 645                  */
 646                 disable_irq(device->irq_num[DIO0]);
 647                 device->tx_active = true;
 648 
 649                 /* clear fifo, set fifo threshold, set payload length */
 650                 retval = rf69_set_mode(spi, standby); /* this clears the fifo */
 651                 if (retval < 0)
 652                         return retval;
 653 
 654                 if (device->rx_active && !rx_interrupted) {
 655                         /*
 656                          * rx is currently waiting for a telegram;
 657                          * we need to set the radio module to standby
 658                          */
 659                         rx_interrupted = true;
 660                 }
 661 
 662                 retval = rf69_set_fifo_threshold(spi, FIFO_THRESHOLD);
 663                 if (retval < 0)
 664                         return retval;
 665                 if (tx_cfg.enable_length_byte == OPTION_ON) {
 666                         retval = rf69_set_payload_length(spi, size * tx_cfg.repetitions);
 667                         if (retval < 0)
 668                                 return retval;
 669                 } else {
 670                         retval = rf69_set_payload_length(spi, 0);
 671                         if (retval < 0)
 672                                 return retval;
 673                 }
 674 
 675                 /* configure the rf chip */
 676                 retval = rf69_set_tx_cfg(device, &tx_cfg);
 677                 if (retval < 0)
 678                         return retval;
 679 
 680                 /* enable fifo level interrupt */
 681                 retval = rf69_set_dio_mapping(spi, DIO1, DIO_FIFO_LEVEL);
 682                 if (retval < 0)
 683                         return retval;
 684                 device->irq_state[DIO1] = DIO_FIFO_LEVEL;
 685                 irq_set_irq_type(device->irq_num[DIO1], IRQ_TYPE_EDGE_FALLING);
 686 
 687                 /* enable packet sent interrupt */
 688                 retval = rf69_set_dio_mapping(spi, DIO0, DIO_PACKET_SENT);
 689                 if (retval < 0)
 690                         return retval;
 691                 device->irq_state[DIO0] = DIO_PACKET_SENT;
 692                 irq_set_irq_type(device->irq_num[DIO0], IRQ_TYPE_EDGE_RISING);
 693                 enable_irq(device->irq_num[DIO0]); /* was disabled by rx active check */
 694 
 695                 /* enable transmission */
 696                 retval = rf69_set_mode(spi, transmit);
 697                 if (retval < 0)
 698                         return retval;
 699 
 700                 /* transfer this msg (and repetitions) to chip fifo */
 701                 device->free_in_fifo = FIFO_SIZE;
 702                 position = 0;
 703                 repetitions = tx_cfg.repetitions;
 704                 while ((repetitions > 0) && (size > position)) {
 705                         if ((size - position) > device->free_in_fifo) {
 706                                 /* msg to big for fifo - take a part */
 707                                 int write_size = device->free_in_fifo;
 708 
 709                                 device->free_in_fifo = 0;
 710                                 rf69_write_fifo(spi,
 711                                                 &device->buffer[position],
 712                                                 write_size);
 713                                 position += write_size;
 714                         } else {
 715                                 /* msg fits into fifo - take all */
 716                                 device->free_in_fifo -= size;
 717                                 repetitions--;
 718                                 rf69_write_fifo(spi,
 719                                                 &device->buffer[position],
 720                                                 (size - position));
 721                                 position = 0; /* reset for next repetition */
 722                         }
 723 
 724                         retval = wait_event_interruptible(device->fifo_wait_queue,
 725                                                           device->free_in_fifo > 0);
 726                         if (retval) {
 727                                 dev_dbg(device->dev, "ABORT\n");
 728                                 goto abort;
 729                         }
 730                 }
 731 
 732                 /* we are done. Wait for packet to get sent */
 733                 dev_dbg(device->dev,
 734                         "thread: wait for packet to get sent/fifo to be empty");
 735                 wait_event_interruptible(device->fifo_wait_queue,
 736                                          device->free_in_fifo == FIFO_SIZE ||
 737                                          kthread_should_stop());
 738                 if (kthread_should_stop())
 739                         return 0;
 740 
 741                 /* STOP_TRANSMISSION */
 742                 dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.");
 743                 retval = rf69_set_mode(spi, standby);
 744                 if (retval < 0)
 745                         return retval;
 746 
 747                 /* everything sent? */
 748                 if (kfifo_is_empty(&device->tx_fifo)) {
 749 abort:
 750                         if (rx_interrupted) {
 751                                 rx_interrupted = false;
 752                                 pi433_start_rx(device);
 753                         }
 754                         device->tx_active = false;
 755                         wake_up_interruptible(&device->rx_wait_queue);
 756                 }
 757         }
 758 }
 759 
 760 /*-------------------------------------------------------------------------*/
 761 
 762 static ssize_t
 763 pi433_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)
 764 {
 765         struct pi433_instance   *instance;
 766         struct pi433_device     *device;
 767         int                     bytes_received;
 768         ssize_t                 retval;
 769 
 770         /* check, whether internal buffer is big enough for requested size */
 771         if (size > MAX_MSG_SIZE)
 772                 return -EMSGSIZE;
 773 
 774         instance = filp->private_data;
 775         device = instance->device;
 776 
 777         /* just one read request at a time */
 778         mutex_lock(&device->rx_lock);
 779         if (device->rx_active) {
 780                 mutex_unlock(&device->rx_lock);
 781                 return -EAGAIN;
 782         }
 783 
 784         device->rx_active = true;
 785         mutex_unlock(&device->rx_lock);
 786 
 787         /* start receiving */
 788         /* will block until something was received*/
 789         device->rx_buffer_size = size;
 790         bytes_received = pi433_receive(device);
 791 
 792         /* release rx */
 793         mutex_lock(&device->rx_lock);
 794         device->rx_active = false;
 795         mutex_unlock(&device->rx_lock);
 796 
 797         /* if read was successful copy to user space*/
 798         if (bytes_received > 0) {
 799                 retval = copy_to_user(buf, device->rx_buffer, bytes_received);
 800                 if (retval)
 801                         return -EFAULT;
 802         }
 803 
 804         return bytes_received;
 805 }
 806 
 807 static ssize_t
 808 pi433_write(struct file *filp, const char __user *buf,
 809             size_t count, loff_t *f_pos)
 810 {
 811         struct pi433_instance   *instance;
 812         struct pi433_device     *device;
 813         int                     retval;
 814         unsigned int            required, available, copied;
 815 
 816         instance = filp->private_data;
 817         device = instance->device;
 818 
 819         /*
 820          * check, whether internal buffer (tx thread) is big enough
 821          * for requested size
 822          */
 823         if (count > MAX_MSG_SIZE)
 824                 return -EMSGSIZE;
 825 
 826         /*
 827          * write the following sequence into fifo:
 828          * - tx_cfg
 829          * - size of message
 830          * - message
 831          */
 832         mutex_lock(&device->tx_fifo_lock);
 833 
 834         required = sizeof(instance->tx_cfg) + sizeof(size_t) + count;
 835         available = kfifo_avail(&device->tx_fifo);
 836         if (required > available) {
 837                 dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available",
 838                         required, available);
 839                 mutex_unlock(&device->tx_fifo_lock);
 840                 return -EAGAIN;
 841         }
 842 
 843         retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg,
 844                           sizeof(instance->tx_cfg));
 845         if (retval != sizeof(instance->tx_cfg))
 846                 goto abort;
 847 
 848         retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t));
 849         if (retval != sizeof(size_t))
 850                 goto abort;
 851 
 852         retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied);
 853         if (retval || copied != count)
 854                 goto abort;
 855 
 856         mutex_unlock(&device->tx_fifo_lock);
 857 
 858         /* start transfer */
 859         wake_up_interruptible(&device->tx_wait_queue);
 860         dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied);
 861 
 862         return copied;
 863 
 864 abort:
 865         dev_warn(device->dev,
 866                  "write to fifo failed, non recoverable: 0x%x", retval);
 867         mutex_unlock(&device->tx_fifo_lock);
 868         return -EAGAIN;
 869 }
 870 
 871 static long
 872 pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 873 {
 874         struct pi433_instance   *instance;
 875         struct pi433_device     *device;
 876         struct pi433_tx_cfg     tx_cfg;
 877         void __user *argp = (void __user *)arg;
 878 
 879         /* Check type and command number */
 880         if (_IOC_TYPE(cmd) != PI433_IOC_MAGIC)
 881                 return -ENOTTY;
 882 
 883         instance = filp->private_data;
 884         device = instance->device;
 885 
 886         if (!device)
 887                 return -ESHUTDOWN;
 888 
 889         switch (cmd) {
 890         case PI433_IOC_RD_TX_CFG:
 891                 if (copy_to_user(argp, &instance->tx_cfg,
 892                                  sizeof(struct pi433_tx_cfg)))
 893                         return -EFAULT;
 894                 break;
 895         case PI433_IOC_WR_TX_CFG:
 896                 if (copy_from_user(&tx_cfg, argp, sizeof(struct pi433_tx_cfg)))
 897                         return -EFAULT;
 898                 mutex_lock(&device->tx_fifo_lock);
 899                 memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg));
 900                 mutex_unlock(&device->tx_fifo_lock);
 901                 break;
 902         case PI433_IOC_RD_RX_CFG:
 903                 if (copy_to_user(argp, &device->rx_cfg,
 904                                  sizeof(struct pi433_rx_cfg)))
 905                         return -EFAULT;
 906                 break;
 907         case PI433_IOC_WR_RX_CFG:
 908                 mutex_lock(&device->rx_lock);
 909 
 910                 /* during pendig read request, change of config not allowed */
 911                 if (device->rx_active) {
 912                         mutex_unlock(&device->rx_lock);
 913                         return -EAGAIN;
 914                 }
 915 
 916                 if (copy_from_user(&device->rx_cfg, argp,
 917                                    sizeof(struct pi433_rx_cfg))) {
 918                         mutex_unlock(&device->rx_lock);
 919                         return -EFAULT;
 920                 }
 921 
 922                 mutex_unlock(&device->rx_lock);
 923                 break;
 924         default:
 925                 return -EINVAL;
 926         }
 927 
 928         return 0;
 929 }
 930 
 931 #ifdef CONFIG_COMPAT
 932 static long
 933 pi433_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 934 {
 935         return pi433_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
 936 }
 937 #else
 938 #define pi433_compat_ioctl NULL
 939 #endif /* CONFIG_COMPAT */
 940 
 941 /*-------------------------------------------------------------------------*/
 942 
 943 static int pi433_open(struct inode *inode, struct file *filp)
 944 {
 945         struct pi433_device     *device;
 946         struct pi433_instance   *instance;
 947 
 948         mutex_lock(&minor_lock);
 949         device = idr_find(&pi433_idr, iminor(inode));
 950         mutex_unlock(&minor_lock);
 951         if (!device) {
 952                 pr_debug("device: minor %d unknown.\n", iminor(inode));
 953                 return -ENODEV;
 954         }
 955 
 956         instance = kzalloc(sizeof(*instance), GFP_KERNEL);
 957         if (!instance)
 958                 return -ENOMEM;
 959 
 960         /* setup instance data*/
 961         instance->device = device;
 962         instance->tx_cfg.bit_rate = 4711;
 963         // TODO: fill instance->tx_cfg;
 964 
 965         /* instance data as context */
 966         filp->private_data = instance;
 967         stream_open(inode, filp);
 968 
 969         return 0;
 970 }
 971 
 972 static int pi433_release(struct inode *inode, struct file *filp)
 973 {
 974         struct pi433_instance   *instance;
 975 
 976         instance = filp->private_data;
 977         kfree(instance);
 978         filp->private_data = NULL;
 979 
 980         return 0;
 981 }
 982 
 983 /*-------------------------------------------------------------------------*/
 984 
 985 static int setup_gpio(struct pi433_device *device)
 986 {
 987         char    name[5];
 988         int     retval;
 989         int     i;
 990         const irq_handler_t DIO_irq_handler[NUM_DIO] = {
 991                 DIO0_irq_handler,
 992                 DIO1_irq_handler
 993         };
 994 
 995         for (i = 0; i < NUM_DIO; i++) {
 996                 /* "construct" name and get the gpio descriptor */
 997                 snprintf(name, sizeof(name), "DIO%d", i);
 998                 device->gpiod[i] = gpiod_get(&device->spi->dev, name,
 999                                              0 /*GPIOD_IN*/);
1000 
1001                 if (device->gpiod[i] == ERR_PTR(-ENOENT)) {
1002                         dev_dbg(&device->spi->dev,
1003                                 "Could not find entry for %s. Ignoring.", name);
1004                         continue;
1005                 }
1006 
1007                 if (device->gpiod[i] == ERR_PTR(-EBUSY))
1008                         dev_dbg(&device->spi->dev, "%s is busy.", name);
1009 
1010                 if (IS_ERR(device->gpiod[i])) {
1011                         retval = PTR_ERR(device->gpiod[i]);
1012                         /* release already allocated gpios */
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                 /* configure the pin */
1021                 gpiod_unexport(device->gpiod[i]);
1022                 retval = gpiod_direction_input(device->gpiod[i]);
1023                 if (retval)
1024                         return retval;
1025 
1026                 /* configure irq */
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, /* flags */
1035                                      name,
1036                                      device);
1037 
1038                 if (retval)
1039                         return retval;
1040 
1041                 dev_dbg(&device->spi->dev, "%s successfully configured", 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                 /* check if gpiod is valid */
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          * REVISIT switch to aio primitives, so that userspace
1091          * gets more complete API coverage.  It'll simplify things
1092          * too, except for the locking.
1093          */
1094         .write =        pi433_write,
1095         .read =         pi433_read,
1096         .unlocked_ioctl = pi433_ioctl,
1097         .compat_ioctl = pi433_compat_ioctl,
1098         .open =         pi433_open,
1099         .release =      pi433_release,
1100         .llseek =       no_llseek,
1101 };
1102 
1103 /*-------------------------------------------------------------------------*/
1104 
1105 static int pi433_probe(struct spi_device *spi)
1106 {
1107         struct pi433_device     *device;
1108         int                     retval;
1109 
1110         /* setup spi parameters */
1111         spi->mode = 0x00;
1112         spi->bits_per_word = 8;
1113         /*
1114          * spi->max_speed_hz = 10000000;
1115          * 1MHz already set by device tree overlay
1116          */
1117 
1118         retval = spi_setup(spi);
1119         if (retval) {
1120                 dev_dbg(&spi->dev, "configuration of SPI interface failed!\n");
1121                 return retval;
1122         }
1123 
1124         dev_dbg(&spi->dev,
1125                 "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed",
1126                 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1127 
1128         /* Ping the chip by reading the version register */
1129         retval = spi_w8r8(spi, 0x10);
1130         if (retval < 0)
1131                 return retval;
1132 
1133         switch (retval) {
1134         case 0x24:
1135                 dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval);
1136                 break;
1137         default:
1138                 dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval);
1139                 return -ENODEV;
1140         }
1141 
1142         /* Allocate driver data */
1143         device = kzalloc(sizeof(*device), GFP_KERNEL);
1144         if (!device)
1145                 return -ENOMEM;
1146 
1147         /* Initialize the driver data */
1148         device->spi = spi;
1149         device->rx_active = false;
1150         device->tx_active = false;
1151         device->interrupt_rx_allowed = false;
1152 
1153         /* init rx buffer */
1154         device->rx_buffer = kmalloc(MAX_MSG_SIZE, GFP_KERNEL);
1155         if (!device->rx_buffer) {
1156                 retval = -ENOMEM;
1157                 goto RX_failed;
1158         }
1159 
1160         /* init wait queues */
1161         init_waitqueue_head(&device->tx_wait_queue);
1162         init_waitqueue_head(&device->rx_wait_queue);
1163         init_waitqueue_head(&device->fifo_wait_queue);
1164 
1165         /* init fifo */
1166         INIT_KFIFO(device->tx_fifo);
1167 
1168         /* init mutexes and locks */
1169         mutex_init(&device->tx_fifo_lock);
1170         mutex_init(&device->rx_lock);
1171 
1172         /* setup GPIO (including irq_handler) for the different DIOs */
1173         retval = setup_gpio(device);
1174         if (retval) {
1175                 dev_dbg(&spi->dev, "setup of GPIOs failed");
1176                 goto GPIO_failed;
1177         }
1178 
1179         /* setup the radio module */
1180         retval = rf69_set_mode(spi, standby);
1181         if (retval < 0)
1182                 goto minor_failed;
1183         retval = rf69_set_data_mode(spi, DATAMODUL_MODE_PACKET);
1184         if (retval < 0)
1185                 goto minor_failed;
1186         retval = rf69_enable_amplifier(spi, MASK_PALEVEL_PA0);
1187         if (retval < 0)
1188                 goto minor_failed;
1189         retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA1);
1190         if (retval < 0)
1191                 goto minor_failed;
1192         retval = rf69_disable_amplifier(spi, MASK_PALEVEL_PA2);
1193         if (retval < 0)
1194                 goto minor_failed;
1195         retval = rf69_set_output_power_level(spi, 13);
1196         if (retval < 0)
1197                 goto minor_failed;
1198         retval = rf69_set_antenna_impedance(spi, fifty_ohm);
1199         if (retval < 0)
1200                 goto minor_failed;
1201 
1202         /* determ minor number */
1203         retval = pi433_get_minor(device);
1204         if (retval) {
1205                 dev_dbg(&spi->dev, "get of minor number failed");
1206                 goto minor_failed;
1207         }
1208 
1209         /* create device */
1210         device->devt = MKDEV(MAJOR(pi433_dev), device->minor);
1211         device->dev = device_create(pi433_class,
1212                                     &spi->dev,
1213                                     device->devt,
1214                                     device,
1215                                     "pi433.%d",
1216                                     device->minor);
1217         if (IS_ERR(device->dev)) {
1218                 pr_err("pi433: device register failed\n");
1219                 retval = PTR_ERR(device->dev);
1220                 goto device_create_failed;
1221         } else {
1222                 dev_dbg(device->dev,
1223                         "created device for major %d, minor %d\n",
1224                         MAJOR(pi433_dev),
1225                         device->minor);
1226         }
1227 
1228         /* start tx thread */
1229         device->tx_task_struct = kthread_run(pi433_tx_thread,
1230                                              device,
1231                                              "pi433.%d_tx_task",
1232                                              device->minor);
1233         if (IS_ERR(device->tx_task_struct)) {
1234                 dev_dbg(device->dev, "start of send thread failed");
1235                 retval = PTR_ERR(device->tx_task_struct);
1236                 goto send_thread_failed;
1237         }
1238 
1239         /* create cdev */
1240         device->cdev = cdev_alloc();
1241         if (!device->cdev) {
1242                 dev_dbg(device->dev, "allocation of cdev failed");
1243                 goto cdev_failed;
1244         }
1245         device->cdev->owner = THIS_MODULE;
1246         cdev_init(device->cdev, &pi433_fops);
1247         retval = cdev_add(device->cdev, device->devt, 1);
1248         if (retval) {
1249                 dev_dbg(device->dev, "register of cdev failed");
1250                 goto del_cdev;
1251         }
1252 
1253         /* spi setup */
1254         spi_set_drvdata(spi, device);
1255 
1256         return 0;
1257 
1258 del_cdev:
1259         cdev_del(device->cdev);
1260 cdev_failed:
1261         kthread_stop(device->tx_task_struct);
1262 send_thread_failed:
1263         device_destroy(pi433_class, device->devt);
1264 device_create_failed:
1265         pi433_free_minor(device);
1266 minor_failed:
1267         free_gpio(device);
1268 GPIO_failed:
1269         kfree(device->rx_buffer);
1270 RX_failed:
1271         kfree(device);
1272 
1273         return retval;
1274 }
1275 
1276 static int pi433_remove(struct spi_device *spi)
1277 {
1278         struct pi433_device     *device = spi_get_drvdata(spi);
1279 
1280         /* free GPIOs */
1281         free_gpio(device);
1282 
1283         /* make sure ops on existing fds can abort cleanly */
1284         device->spi = NULL;
1285 
1286         kthread_stop(device->tx_task_struct);
1287 
1288         device_destroy(pi433_class, device->devt);
1289 
1290         cdev_del(device->cdev);
1291 
1292         pi433_free_minor(device);
1293 
1294         kfree(device->rx_buffer);
1295         kfree(device);
1296 
1297         return 0;
1298 }
1299 
1300 static const struct of_device_id pi433_dt_ids[] = {
1301         { .compatible = "Smarthome-Wolf,pi433" },
1302         {},
1303 };
1304 
1305 MODULE_DEVICE_TABLE(of, pi433_dt_ids);
1306 
1307 static struct spi_driver pi433_spi_driver = {
1308         .driver = {
1309                 .name =         "pi433",
1310                 .owner =        THIS_MODULE,
1311                 .of_match_table = of_match_ptr(pi433_dt_ids),
1312         },
1313         .probe =        pi433_probe,
1314         .remove =       pi433_remove,
1315 
1316         /*
1317          * NOTE:  suspend/resume methods are not necessary here.
1318          * We don't do anything except pass the requests to/from
1319          * the underlying controller.  The refrigerator handles
1320          * most issues; the controller driver handles the rest.
1321          */
1322 };
1323 
1324 /*-------------------------------------------------------------------------*/
1325 
1326 static int __init pi433_init(void)
1327 {
1328         int status;
1329 
1330         /*
1331          * If MAX_MSG_SIZE is smaller then FIFO_SIZE, the driver won't
1332          * work stable - risk of buffer overflow
1333          */
1334         if (MAX_MSG_SIZE < FIFO_SIZE)
1335                 return -EINVAL;
1336 
1337         /*
1338          * Claim device numbers.  Then register a class
1339          * that will key udev/mdev to add/remove /dev nodes.  Last, register
1340          * Last, register the driver which manages those device numbers.
1341          */
1342         status = alloc_chrdev_region(&pi433_dev, 0, N_PI433_MINORS, "pi433");
1343         if (status < 0)
1344                 return status;
1345 
1346         pi433_class = class_create(THIS_MODULE, "pi433");
1347         if (IS_ERR(pi433_class)) {
1348                 unregister_chrdev(MAJOR(pi433_dev),
1349                                   pi433_spi_driver.driver.name);
1350                 return PTR_ERR(pi433_class);
1351         }
1352 
1353         status = spi_register_driver(&pi433_spi_driver);
1354         if (status < 0) {
1355                 class_destroy(pi433_class);
1356                 unregister_chrdev(MAJOR(pi433_dev),
1357                                   pi433_spi_driver.driver.name);
1358         }
1359 
1360         return status;
1361 }
1362 
1363 module_init(pi433_init);
1364 
1365 static void __exit pi433_exit(void)
1366 {
1367         spi_unregister_driver(&pi433_spi_driver);
1368         class_destroy(pi433_class);
1369         unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name);
1370 }
1371 module_exit(pi433_exit);
1372 
1373 MODULE_AUTHOR("Marcus Wolf, <linux@wolf-entwicklungen.de>");
1374 MODULE_DESCRIPTION("Driver for Pi433");
1375 MODULE_LICENSE("GPL");
1376 MODULE_ALIAS("spi:pi433");

/* [<][>][^][v][top][bottom][index][help] */