diff options
Diffstat (limited to 'drivers/staging/pi433')
-rw-r--r-- | drivers/staging/pi433/TODO | 4 | ||||
-rw-r--r-- | drivers/staging/pi433/pi433_if.c | 176 | ||||
-rw-r--r-- | drivers/staging/pi433/pi433_if.h | 2 | ||||
-rw-r--r-- | drivers/staging/pi433/rf69.c | 169 | ||||
-rw-r--r-- | drivers/staging/pi433/rf69.h | 3 | ||||
-rw-r--r-- | drivers/staging/pi433/rf69_enum.h | 20 | ||||
-rw-r--r-- | drivers/staging/pi433/rf69_registers.h | 2 |
7 files changed, 183 insertions, 193 deletions
diff --git a/drivers/staging/pi433/TODO b/drivers/staging/pi433/TODO index 63a40bfcc67e..5cf3fd99d521 100644 --- a/drivers/staging/pi433/TODO +++ b/drivers/staging/pi433/TODO @@ -1,5 +1,5 @@ -* coding style does not fully comply with the kernel style guide. -* still TODOs, annotated in the code * currently the code introduces new IOCTLs. I'm afraid this is a bad idea. -> Replace this with another interface, hints are welcome! * Some missing data (marked with ###) needs to be added in the documentation +* Change (struct pi433_tx_cfg)->bit_rate to be a u32 so that we can support + bit rates up to 300kbps per the spec. diff --git a/drivers/staging/pi433/pi433_if.c b/drivers/staging/pi433/pi433_if.c index 1d31c35875e3..941aaa7eab2e 100644 --- a/drivers/staging/pi433/pi433_if.c +++ b/drivers/staging/pi433/pi433_if.c @@ -41,6 +41,8 @@ #ifdef CONFIG_COMPAT #include <linux/compat.h> #endif +#include <linux/debugfs.h> +#include <linux/seq_file.h> #include "pi433_if.h" #include "rf69.h" @@ -108,6 +110,9 @@ struct pi433_device { struct pi433_instance { struct pi433_device *device; struct pi433_tx_cfg tx_cfg; + + /* control flags */ + bool tx_cfg_initialized; }; /*-------------------------------------------------------------------------*/ @@ -164,10 +169,10 @@ rf69_set_rx_cfg(struct pi433_device *dev, struct pi433_rx_cfg *rx_cfg) ret = rf69_set_frequency(dev->spi, rx_cfg->frequency); if (ret < 0) return ret; - ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate); + ret = rf69_set_modulation(dev->spi, rx_cfg->modulation); if (ret < 0) return ret; - ret = rf69_set_modulation(dev->spi, rx_cfg->modulation); + ret = rf69_set_bit_rate(dev->spi, rx_cfg->bit_rate); if (ret < 0) return ret; ret = rf69_set_antenna_impedance(dev->spi, rx_cfg->antenna_impedance); @@ -287,10 +292,10 @@ rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg) ret = rf69_set_frequency(dev->spi, tx_cfg->frequency); if (ret < 0) return ret; - ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate); + ret = rf69_set_modulation(dev->spi, tx_cfg->modulation); if (ret < 0) return ret; - ret = rf69_set_modulation(dev->spi, tx_cfg->modulation); + ret = rf69_set_bit_rate(dev->spi, tx_cfg->bit_rate); if (ret < 0) return ret; ret = rf69_set_deviation(dev->spi, tx_cfg->dev_frequency); @@ -359,8 +364,7 @@ rf69_set_tx_cfg(struct pi433_device *dev, struct pi433_tx_cfg *tx_cfg) /*-------------------------------------------------------------------------*/ -static int -pi433_start_rx(struct pi433_device *dev) +static int pi433_start_rx(struct pi433_device *dev) { int retval; @@ -400,8 +404,7 @@ pi433_start_rx(struct pi433_device *dev) /*-------------------------------------------------------------------------*/ -static int -pi433_receive(void *data) +static int pi433_receive(void *data) { struct pi433_device *dev = data; struct spi_device *spi = dev->spi; @@ -411,7 +414,7 @@ pi433_receive(void *data) dev->interrupt_rx_allowed = false; /* wait for any tx to finish */ - dev_dbg(dev->dev, "rx: going to wait for any tx to finish"); + dev_dbg(dev->dev, "rx: going to wait for any tx to finish\n"); retval = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active); if (retval) { /* wait was interrupted */ @@ -431,16 +434,16 @@ pi433_receive(void *data) return retval; /* now check RSSI, if low wait for getting high (RSSI interrupt) */ - while (!rf69_get_flag(dev->spi, rssi_exceeded_threshold)) { + while (!(rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RSSI)) { /* allow tx to interrupt us while waiting for high RSSI */ dev->interrupt_rx_allowed = true; wake_up_interruptible(&dev->tx_wait_queue); /* wait for RSSI level to become high */ - dev_dbg(dev->dev, "rx: going to wait for high RSSI level"); + dev_dbg(dev->dev, "rx: going to wait for high RSSI level\n"); retval = wait_event_interruptible(dev->rx_wait_queue, - rf69_get_flag(dev->spi, - rssi_exceeded_threshold)); + rf69_read_reg(spi, REG_IRQFLAGS1) & + MASK_IRQFLAGS1_RSSI); if (retval) /* wait was interrupted */ goto abort; dev->interrupt_rx_allowed = false; @@ -464,11 +467,11 @@ pi433_receive(void *data) goto abort; } bytes_total = dev->rx_cfg.fixed_message_length; - dev_dbg(dev->dev, "rx: msg len set to %d by fixed length", + dev_dbg(dev->dev, "rx: msg len set to %d by fixed length\n", bytes_total); } else { bytes_total = dev->rx_buffer_size; - dev_dbg(dev->dev, "rx: msg len set to %d as requested by read", + dev_dbg(dev->dev, "rx: msg len set to %d as requested by read\n", bytes_total); } @@ -485,7 +488,7 @@ pi433_receive(void *data) goto abort; } dev->free_in_fifo++; - dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte", + dev_dbg(dev->dev, "rx: msg len reset to %d due to length byte\n", bytes_total); } @@ -502,12 +505,12 @@ pi433_receive(void *data) rf69_read_fifo(spi, &dummy, 1); dev->free_in_fifo++; - dev_dbg(dev->dev, "rx: address byte stripped off"); + dev_dbg(dev->dev, "rx: address byte stripped off\n"); } /* get payload */ while (dev->rx_position < bytes_total) { - if (!rf69_get_flag(dev->spi, payload_ready)) { + if (!(rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PAYLOAD_READY)) { retval = wait_event_interruptible(dev->fifo_wait_queue, dev->free_in_fifo < FIFO_SIZE); if (retval) /* wait was interrupted */ @@ -552,8 +555,7 @@ abort: return bytes_total; } -static int -pi433_tx_thread(void *data) +static int pi433_tx_thread(void *data) { struct pi433_device *device = data; struct spi_device *spi = device->spi; @@ -565,7 +567,7 @@ pi433_tx_thread(void *data) while (1) { /* wait for fifo to be populated or for request to terminate*/ - dev_dbg(device->dev, "thread: going to wait for new messages"); + dev_dbg(device->dev, "thread: going to wait for new messages\n"); wait_event_interruptible(device->tx_wait_queue, (!kfifo_is_empty(&device->tx_fifo) || kthread_should_stop())); @@ -581,7 +583,7 @@ pi433_tx_thread(void *data) retval = kfifo_out(&device->tx_fifo, &tx_cfg, sizeof(tx_cfg)); if (retval != sizeof(tx_cfg)) { dev_dbg(device->dev, - "reading tx_cfg from fifo failed: got %d byte(s), expected %d", + "reading tx_cfg from fifo failed: got %d byte(s), expected %d\n", retval, (unsigned int)sizeof(tx_cfg)); continue; } @@ -589,7 +591,7 @@ pi433_tx_thread(void *data) retval = kfifo_out(&device->tx_fifo, &size, sizeof(size_t)); if (retval != sizeof(size_t)) { dev_dbg(device->dev, - "reading msg size from fifo failed: got %d, expected %d", + "reading msg size from fifo failed: got %d, expected %d\n", retval, (unsigned int)sizeof(size_t)); continue; } @@ -626,7 +628,7 @@ pi433_tx_thread(void *data) retval = kfifo_out(&device->tx_fifo, &device->buffer[position], sizeof(device->buffer) - position); dev_dbg(device->dev, - "read %d message byte(s) from fifo queue.", retval); + "read %d message byte(s) from fifo queue.\n", retval); /* * if rx is active, we need to interrupt the waiting for @@ -731,7 +733,7 @@ pi433_tx_thread(void *data) /* we are done. Wait for packet to get sent */ dev_dbg(device->dev, - "thread: wait for packet to get sent/fifo to be empty"); + "thread: wait for packet to get sent/fifo to be empty\n"); wait_event_interruptible(device->fifo_wait_queue, device->free_in_fifo == FIFO_SIZE || kthread_should_stop()); @@ -739,7 +741,7 @@ pi433_tx_thread(void *data) return 0; /* STOP_TRANSMISSION */ - dev_dbg(device->dev, "thread: Packet sent. Set mode to stby."); + dev_dbg(device->dev, "thread: Packet sent. Set mode to stby.\n"); retval = rf69_set_mode(spi, standby); if (retval < 0) goto abort; @@ -824,6 +826,16 @@ pi433_write(struct file *filp, const char __user *buf, return -EMSGSIZE; /* + * check if tx_cfg has been initialized otherwise we won't be able to + * config the RF trasmitter correctly due to invalid settings + */ + if (!instance->tx_cfg_initialized) { + dev_notice_once(device->dev, + "write: failed due to unconfigured tx_cfg (see PI433_IOC_WR_TX_CFG)\n"); + return -EINVAL; + } + + /* * write the following sequence into fifo: * - tx_cfg * - size of message @@ -834,7 +846,7 @@ pi433_write(struct file *filp, const char __user *buf, required = sizeof(instance->tx_cfg) + sizeof(size_t) + count; available = kfifo_avail(&device->tx_fifo); if (required > available) { - dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available", + dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available\n", required, available); mutex_unlock(&device->tx_fifo_lock); return -EAGAIN; @@ -857,19 +869,18 @@ pi433_write(struct file *filp, const char __user *buf, /* start transfer */ wake_up_interruptible(&device->tx_wait_queue); - dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied); + dev_dbg(device->dev, "write: generated new msg with %d bytes.\n", copied); return copied; abort: dev_warn(device->dev, - "write to fifo failed, non recoverable: 0x%x", retval); + "write to fifo failed, non recoverable: 0x%x\n", retval); mutex_unlock(&device->tx_fifo_lock); return -EAGAIN; } -static long -pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) +static long pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct pi433_instance *instance; struct pi433_device *device; @@ -897,6 +908,7 @@ pi433_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) return -EFAULT; mutex_lock(&device->tx_fifo_lock); memcpy(&instance->tx_cfg, &tx_cfg, sizeof(struct pi433_tx_cfg)); + instance->tx_cfg_initialized = true; mutex_unlock(&device->tx_fifo_lock); break; case PI433_IOC_RD_RX_CFG: @@ -949,8 +961,6 @@ static int pi433_open(struct inode *inode, struct file *filp) /* setup instance data*/ instance->device = device; - instance->tx_cfg.bit_rate = 4711; - // TODO: fill instance->tx_cfg; /* instance data as context */ filp->private_data = instance; @@ -990,12 +1000,12 @@ static int setup_gpio(struct pi433_device *device) if (device->gpiod[i] == ERR_PTR(-ENOENT)) { dev_dbg(&device->spi->dev, - "Could not find entry for %s. Ignoring.", name); + "Could not find entry for %s. Ignoring.\n", name); continue; } if (device->gpiod[i] == ERR_PTR(-EBUSY)) - dev_dbg(&device->spi->dev, "%s is busy.", name); + dev_dbg(&device->spi->dev, "%s is busy.\n", name); if (IS_ERR(device->gpiod[i])) { retval = PTR_ERR(device->gpiod[i]); @@ -1028,7 +1038,7 @@ static int setup_gpio(struct pi433_device *device) if (retval) return retval; - dev_dbg(&device->spi->dev, "%s successfully configured", name); + dev_dbg(&device->spi->dev, "%s successfully configured\n", name); } return 0; @@ -1090,12 +1100,76 @@ static const struct file_operations pi433_fops = { .llseek = no_llseek, }; +static int pi433_debugfs_regs_show(struct seq_file *m, void *p) +{ + struct pi433_device *dev; + u8 reg_data[114]; + int i; + char *fmt = "0x%02x, 0x%02x\n"; + int ret; + + dev = m->private; + + mutex_lock(&dev->tx_fifo_lock); + mutex_lock(&dev->rx_lock); + + // wait for on-going operations to finish + ret = wait_event_interruptible(dev->rx_wait_queue, !dev->tx_active); + if (ret) + goto out_unlock; + + ret = wait_event_interruptible(dev->tx_wait_queue, !dev->rx_active); + if (ret) + goto out_unlock; + + // skip FIFO register (0x0) otherwise this can affect some of uC ops + for (i = 1; i < 0x50; i++) + reg_data[i] = rf69_read_reg(dev->spi, i); + + reg_data[REG_TESTLNA] = rf69_read_reg(dev->spi, REG_TESTLNA); + reg_data[REG_TESTPA1] = rf69_read_reg(dev->spi, REG_TESTPA1); + reg_data[REG_TESTPA2] = rf69_read_reg(dev->spi, REG_TESTPA2); + reg_data[REG_TESTDAGC] = rf69_read_reg(dev->spi, REG_TESTDAGC); + reg_data[REG_TESTAFC] = rf69_read_reg(dev->spi, REG_TESTAFC); + + seq_puts(m, "# reg, val\n"); + + for (i = 1; i < 0x50; i++) + seq_printf(m, fmt, i, reg_data[i]); + + seq_printf(m, fmt, REG_TESTLNA, reg_data[REG_TESTLNA]); + seq_printf(m, fmt, REG_TESTPA1, reg_data[REG_TESTPA1]); + seq_printf(m, fmt, REG_TESTPA2, reg_data[REG_TESTPA2]); + seq_printf(m, fmt, REG_TESTDAGC, reg_data[REG_TESTDAGC]); + seq_printf(m, fmt, REG_TESTAFC, reg_data[REG_TESTAFC]); + +out_unlock: + mutex_unlock(&dev->rx_lock); + mutex_unlock(&dev->tx_fifo_lock); + + return ret; +} + +static int pi433_debugfs_regs_open(struct inode *inode, struct file *filp) +{ + return single_open(filp, pi433_debugfs_regs_show, inode->i_private); +} + +static const struct file_operations debugfs_fops = { + .llseek = seq_lseek, + .open = pi433_debugfs_regs_open, + .owner = THIS_MODULE, + .read = seq_read, + .release = single_release +}; + /*-------------------------------------------------------------------------*/ static int pi433_probe(struct spi_device *spi) { struct pi433_device *device; int retval; + struct dentry *entry; /* setup spi parameters */ spi->mode = 0x00; @@ -1112,20 +1186,20 @@ static int pi433_probe(struct spi_device *spi) } dev_dbg(&spi->dev, - "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed", + "spi interface setup: mode 0x%2x, %d bits per word, %dhz max speed\n", spi->mode, spi->bits_per_word, spi->max_speed_hz); - /* Ping the chip by reading the version register */ - retval = spi_w8r8(spi, 0x10); + /* read chip version */ + retval = rf69_get_version(spi); if (retval < 0) return retval; switch (retval) { case 0x24: - dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)", retval); + dev_dbg(&spi->dev, "found pi433 (ver. 0x%x)\n", retval); break; default: - dev_dbg(&spi->dev, "unknown chip version: 0x%x", retval); + dev_dbg(&spi->dev, "unknown chip version: 0x%x\n", retval); return -ENODEV; } @@ -1162,7 +1236,7 @@ static int pi433_probe(struct spi_device *spi) /* setup GPIO (including irq_handler) for the different DIOs */ retval = setup_gpio(device); if (retval) { - dev_dbg(&spi->dev, "setup of GPIOs failed"); + dev_dbg(&spi->dev, "setup of GPIOs failed\n"); goto GPIO_failed; } @@ -1192,7 +1266,7 @@ static int pi433_probe(struct spi_device *spi) /* determ minor number */ retval = pi433_get_minor(device); if (retval) { - dev_dbg(&spi->dev, "get of minor number failed"); + dev_dbg(&spi->dev, "get of minor number failed\n"); goto minor_failed; } @@ -1221,7 +1295,7 @@ static int pi433_probe(struct spi_device *spi) "pi433.%d_tx_task", device->minor); if (IS_ERR(device->tx_task_struct)) { - dev_dbg(device->dev, "start of send thread failed"); + dev_dbg(device->dev, "start of send thread failed\n"); retval = PTR_ERR(device->tx_task_struct); goto send_thread_failed; } @@ -1229,7 +1303,7 @@ static int pi433_probe(struct spi_device *spi) /* create cdev */ device->cdev = cdev_alloc(); if (!device->cdev) { - dev_dbg(device->dev, "allocation of cdev failed"); + dev_dbg(device->dev, "allocation of cdev failed\n"); retval = -ENOMEM; goto cdev_failed; } @@ -1237,13 +1311,17 @@ static int pi433_probe(struct spi_device *spi) cdev_init(device->cdev, &pi433_fops); retval = cdev_add(device->cdev, device->devt, 1); if (retval) { - dev_dbg(device->dev, "register of cdev failed"); + dev_dbg(device->dev, "register of cdev failed\n"); goto del_cdev; } /* spi setup */ spi_set_drvdata(spi, device); + entry = debugfs_create_dir(dev_name(device->dev), + debugfs_lookup(KBUILD_MODNAME, NULL)); + debugfs_create_file("regs", 0400, entry, device, &debugfs_fops); + return 0; del_cdev: @@ -1267,6 +1345,9 @@ RX_failed: static void pi433_remove(struct spi_device *spi) { struct pi433_device *device = spi_get_drvdata(spi); + struct dentry *mod_entry = debugfs_lookup(KBUILD_MODNAME, NULL); + + debugfs_remove(debugfs_lookup(dev_name(device->dev), mod_entry)); /* free GPIOs */ free_gpio(device); @@ -1339,6 +1420,8 @@ static int __init pi433_init(void) return PTR_ERR(pi433_class); } + debugfs_create_dir(KBUILD_MODNAME, NULL); + status = spi_register_driver(&pi433_spi_driver); if (status < 0) { class_destroy(pi433_class); @@ -1356,6 +1439,7 @@ static void __exit pi433_exit(void) spi_unregister_driver(&pi433_spi_driver); class_destroy(pi433_class); unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name); + debugfs_remove_recursive(debugfs_lookup(KBUILD_MODNAME, NULL)); } module_exit(pi433_exit); diff --git a/drivers/staging/pi433/pi433_if.h b/drivers/staging/pi433/pi433_if.h index 855f0bebdc1c..25ee0b77a32c 100644 --- a/drivers/staging/pi433/pi433_if.h +++ b/drivers/staging/pi433/pi433_if.h @@ -1,7 +1,5 @@ /* SPDX-License-Identifier: GPL-2.0+ */ /* - * include/linux/TODO - * * userspace interface for pi433 radio module * * Pi433 is a 433MHz radio module for the Raspberry Pi. diff --git a/drivers/staging/pi433/rf69.c b/drivers/staging/pi433/rf69.c index d64df072d8e8..659c8c1b38fd 100644 --- a/drivers/staging/pi433/rf69.c +++ b/drivers/staging/pi433/rf69.c @@ -6,13 +6,6 @@ * Marcus Wolf <linux@wolf-entwicklungen.de> */ -/* enable prosa debug info */ -#undef DEBUG -/* enable print of values on reg access */ -#undef DEBUG_VALUES -/* enable print of values on fifo access */ -#undef DEBUG_FIFO_ACCESS - #include <linux/types.h> #include <linux/spi/spi.h> @@ -24,50 +17,19 @@ /*-------------------------------------------------------------------------*/ -static u8 rf69_read_reg(struct spi_device *spi, u8 addr) +u8 rf69_read_reg(struct spi_device *spi, u8 addr) { - int retval; - - retval = spi_w8r8(spi, addr); - -#ifdef DEBUG_VALUES - if (retval < 0) - /* - * should never happen, since we already checked, - * that module is connected. Therefore no error - * handling, just an optional error message... - */ - dev_dbg(&spi->dev, "read 0x%x FAILED\n", addr); - else - dev_dbg(&spi->dev, "read 0x%x from reg 0x%x\n", retval, addr); -#endif - - return retval; + return spi_w8r8(spi, addr); } static int rf69_write_reg(struct spi_device *spi, u8 addr, u8 value) { - int retval; char buffer[2]; buffer[0] = addr | WRITE_BIT; buffer[1] = value; - retval = spi_write(spi, &buffer, 2); - -#ifdef DEBUG_VALUES - if (retval < 0) - /* - * should never happen, since we already checked, - * that module is connected. Therefore no error - * handling, just an optional error message... - */ - dev_dbg(&spi->dev, "write 0x%x to 0x%x FAILED\n", value, addr); - else - dev_dbg(&spi->dev, "wrote 0x%x to reg 0x%x\n", value, addr); -#endif - - return retval; + return spi_write(spi, &buffer, ARRAY_SIZE(buffer)); } /*-------------------------------------------------------------------------*/ @@ -102,6 +64,11 @@ static inline int rf69_read_mod_write(struct spi_device *spi, u8 reg, /*-------------------------------------------------------------------------*/ +int rf69_get_version(struct spi_device *spi) +{ + return rf69_read_reg(spi, REG_VERSION); +} + int rf69_set_mode(struct spi_device *spi, enum mode mode) { static const u8 mode_map[] = { @@ -113,7 +80,7 @@ int rf69_set_mode(struct spi_device *spi, enum mode mode) }; if (unlikely(mode >= ARRAY_SIZE(mode_map))) { - dev_dbg(&spi->dev, "set: illegal mode %u", mode); + dev_dbg(&spi->dev, "set: illegal mode %u\n", mode); return -EINVAL; } @@ -143,7 +110,7 @@ int rf69_set_modulation(struct spi_device *spi, enum modulation modulation) }; if (unlikely(modulation >= ARRAY_SIZE(modulation_map))) { - dev_dbg(&spi->dev, "set: illegal modulation %u", modulation); + dev_dbg(&spi->dev, "set: illegal modulation %u\n", modulation); return -EINVAL; } @@ -191,7 +158,7 @@ int rf69_set_modulation_shaping(struct spi_device *spi, MASK_DATAMODUL_MODULATION_SHAPE, DATAMODUL_MODULATION_SHAPE_0_3); default: - dev_dbg(&spi->dev, "set: illegal mod shaping for FSK %u", mod_shaping); + dev_dbg(&spi->dev, "set: illegal mod shaping for FSK %u\n", mod_shaping); return -EINVAL; } case OOK: @@ -209,11 +176,11 @@ int rf69_set_modulation_shaping(struct spi_device *spi, MASK_DATAMODUL_MODULATION_SHAPE, DATAMODUL_MODULATION_SHAPE_2BR); default: - dev_dbg(&spi->dev, "set: illegal mod shaping for OOK %u", mod_shaping); + dev_dbg(&spi->dev, "set: illegal mod shaping for OOK %u\n", mod_shaping); return -EINVAL; } default: - dev_dbg(&spi->dev, "set: modulation undefined"); + dev_dbg(&spi->dev, "set: modulation undefined\n"); return -EINVAL; } } @@ -221,15 +188,21 @@ int rf69_set_modulation_shaping(struct spi_device *spi, int rf69_set_bit_rate(struct spi_device *spi, u16 bit_rate) { int retval; - u32 bit_rate_min; u32 bit_rate_reg; u8 msb; u8 lsb; + enum modulation mod; + + // check if modulation is configured + mod = rf69_get_modulation(spi); + if (mod == UNDEF) { + dev_dbg(&spi->dev, "setBitRate: modulation is undefined\n"); + return -EINVAL; + } // check input value - bit_rate_min = F_OSC / 8388608; // 8388608 = 2^23; - if (bit_rate < bit_rate_min) { - dev_dbg(&spi->dev, "setBitRate: illegal input param"); + if (bit_rate < 1200 || (mod == OOK && bit_rate > 32768)) { + dev_dbg(&spi->dev, "setBitRate: illegal input param\n"); return -EINVAL; } @@ -273,7 +246,7 @@ int rf69_set_deviation(struct spi_device *spi, u32 deviation) */ if (deviation < 600 || (deviation + (bit_rate / 2)) > 500000) { dev_dbg(&spi->dev, - "set_deviation: illegal input param: %u", deviation); + "set_deviation: illegal input param: %u\n", deviation); return -EINVAL; } @@ -290,7 +263,7 @@ int rf69_set_deviation(struct spi_device *spi, u32 deviation) // check msb if (msb & ~FDEVMASB_MASK) { - dev_dbg(&spi->dev, "set_deviation: err in calc of msb"); + dev_dbg(&spi->dev, "set_deviation: err in calc of msb\n"); return -EINVAL; } @@ -323,7 +296,7 @@ int rf69_set_frequency(struct spi_device *spi, u32 frequency) // check input value f_max = div_u64(f_step * 8388608, factor); if (frequency > f_max) { - dev_dbg(&spi->dev, "setFrequency: illegal input param"); + dev_dbg(&spi->dev, "setFrequency: illegal input param\n"); return -EINVAL; } @@ -404,7 +377,7 @@ int rf69_set_output_power_level(struct spi_device *spi, u8 power_level) return rf69_read_mod_write(spi, REG_PALEVEL, MASK_PALEVEL_OUTPUT_POWER, power_level); failed: - dev_dbg(&spi->dev, "set: illegal power level %u", power_level); + dev_dbg(&spi->dev, "set: illegal power level %u\n", power_level); return -EINVAL; } @@ -429,7 +402,7 @@ int rf69_set_pa_ramp(struct spi_device *spi, enum pa_ramp pa_ramp) }; if (unlikely(pa_ramp >= ARRAY_SIZE(pa_ramp_map))) { - dev_dbg(&spi->dev, "set: illegal pa_ramp %u", pa_ramp); + dev_dbg(&spi->dev, "set: illegal pa_ramp %u\n", pa_ramp); return -EINVAL; } @@ -445,7 +418,7 @@ int rf69_set_antenna_impedance(struct spi_device *spi, case two_hundred_ohm: return rf69_set_bit(spi, REG_LNA, MASK_LNA_ZIN); default: - dev_dbg(&spi->dev, "set: illegal antenna impedance %u", antenna_impedance); + dev_dbg(&spi->dev, "set: illegal antenna impedance %u\n", antenna_impedance); return -EINVAL; } } @@ -463,7 +436,7 @@ int rf69_set_lna_gain(struct spi_device *spi, enum lna_gain lna_gain) }; if (unlikely(lna_gain >= ARRAY_SIZE(lna_gain_map))) { - dev_dbg(&spi->dev, "set: illegal lna gain %u", lna_gain); + dev_dbg(&spi->dev, "set: illegal lna gain %u\n", lna_gain); return -EINVAL; } @@ -478,14 +451,14 @@ static int rf69_set_bandwidth_intern(struct spi_device *spi, u8 reg, // check value for mantisse and exponent if (exponent > 7) { - dev_dbg(&spi->dev, "set: illegal bandwidth exponent %u", exponent); + dev_dbg(&spi->dev, "set: illegal bandwidth exponent %u\n", exponent); return -EINVAL; } if (mantisse != mantisse16 && mantisse != mantisse20 && mantisse != mantisse24) { - dev_dbg(&spi->dev, "set: illegal bandwidth mantisse %u", mantisse); + dev_dbg(&spi->dev, "set: illegal bandwidth mantisse %u\n", mantisse); return -EINVAL; } @@ -543,7 +516,8 @@ int rf69_set_ook_threshold_dec(struct spi_device *spi, }; if (unlikely(threshold_decrement >= ARRAY_SIZE(td_map))) { - dev_dbg(&spi->dev, "set: illegal OOK threshold decrement %u", threshold_decrement); + dev_dbg(&spi->dev, "set: illegal OOK threshold decrement %u\n", + threshold_decrement); return -EINVAL; } @@ -590,7 +564,7 @@ int rf69_set_dio_mapping(struct spi_device *spi, u8 dio_number, u8 value) dio_addr = REG_DIOMAPPING2; break; default: - dev_dbg(&spi->dev, "set: illegal dio number %u", dio_number); + dev_dbg(&spi->dev, "set: illegal dio number %u\n", dio_number); return -EINVAL; } @@ -604,49 +578,6 @@ int rf69_set_dio_mapping(struct spi_device *spi, u8 dio_number, u8 value) return rf69_write_reg(spi, dio_addr, dio_value); } -bool rf69_get_flag(struct spi_device *spi, enum flag flag) -{ - switch (flag) { - case mode_switch_completed: - return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_MODE_READY); - case ready_to_receive: - return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RX_READY); - case ready_to_send: - return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_TX_READY); - case pll_locked: - return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_PLL_LOCK); - case rssi_exceeded_threshold: - return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RSSI); - case timeout: - return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_TIMEOUT); - case automode: - return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_AUTOMODE); - case sync_address_match: - return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_SYNC_ADDRESS_MATCH); - case fifo_full: - return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_FULL); -/* - * case fifo_not_empty: - * return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_NOT_EMPTY); - */ - case fifo_empty: - return !(rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_NOT_EMPTY); - case fifo_level_below_threshold: - return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_LEVEL); - case fifo_overrun: - return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_OVERRUN); - case packet_sent: - return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PACKET_SENT); - case payload_ready: - return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PAYLOAD_READY); - case crc_ok: - return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_CRC_OK); - case battery_low: - return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_LOW_BAT); - default: return false; - } -} - int rf69_set_rssi_threshold(struct spi_device *spi, u8 threshold) { /* no value check needed - u8 exactly matches register size */ @@ -693,7 +624,7 @@ int rf69_set_fifo_fill_condition(struct spi_device *spi, return rf69_clear_bit(spi, REG_SYNC_CONFIG, MASK_SYNC_CONFIG_FIFO_FILL_CONDITION); default: - dev_dbg(&spi->dev, "set: illegal fifo fill condition %u", fifo_fill_condition); + dev_dbg(&spi->dev, "set: illegal fifo fill condition %u\n", fifo_fill_condition); return -EINVAL; } } @@ -702,7 +633,7 @@ int rf69_set_sync_size(struct spi_device *spi, u8 sync_size) { // check input value if (sync_size > 0x07) { - dev_dbg(&spi->dev, "set: illegal sync size %u", sync_size); + dev_dbg(&spi->dev, "set: illegal sync size %u\n", sync_size); return -EINVAL; } @@ -739,7 +670,7 @@ int rf69_set_packet_format(struct spi_device *spi, return rf69_clear_bit(spi, REG_PACKETCONFIG1, MASK_PACKETCONFIG1_PACKET_FORMAT_VARIABLE); default: - dev_dbg(&spi->dev, "set: illegal packet format %u", packet_format); + dev_dbg(&spi->dev, "set: illegal packet format %u\n", packet_format); return -EINVAL; } } @@ -765,7 +696,7 @@ int rf69_set_address_filtering(struct spi_device *spi, }; if (unlikely(address_filtering >= ARRAY_SIZE(af_map))) { - dev_dbg(&spi->dev, "set: illegal address filtering %u", address_filtering); + dev_dbg(&spi->dev, "set: illegal address filtering %u\n", address_filtering); return -EINVAL; } @@ -800,7 +731,7 @@ int rf69_set_tx_start_condition(struct spi_device *spi, return rf69_set_bit(spi, REG_FIFO_THRESH, MASK_FIFO_THRESH_TXSTART); default: - dev_dbg(&spi->dev, "set: illegal tx start condition %u", tx_start_condition); + dev_dbg(&spi->dev, "set: illegal tx start condition %u\n", tx_start_condition); return -EINVAL; } } @@ -810,8 +741,8 @@ int rf69_set_fifo_threshold(struct spi_device *spi, u8 threshold) int retval; /* check input value */ - if (threshold & 0x80) { - dev_dbg(&spi->dev, "set: illegal fifo threshold %u", threshold); + if (threshold & ~MASK_FIFO_THRESH_VALUE) { + dev_dbg(&spi->dev, "set: illegal fifo threshold %u\n", threshold); return -EINVAL; } @@ -838,7 +769,7 @@ int rf69_set_dagc(struct spi_device *spi, enum dagc dagc) }; if (unlikely(dagc >= ARRAY_SIZE(dagc_map))) { - dev_dbg(&spi->dev, "set: illegal dagc %u", dagc); + dev_dbg(&spi->dev, "set: illegal dagc %u\n", dagc); return -EINVAL; } @@ -849,11 +780,9 @@ int rf69_set_dagc(struct spi_device *spi, enum dagc dagc) int rf69_read_fifo(struct spi_device *spi, u8 *buffer, unsigned int size) { -#ifdef DEBUG_FIFO_ACCESS int i; -#endif struct spi_transfer transfer; - u8 local_buffer[FIFO_SIZE + 1]; + u8 local_buffer[FIFO_SIZE + 1] = {}; int retval; if (size > FIFO_SIZE) { @@ -871,10 +800,9 @@ int rf69_read_fifo(struct spi_device *spi, u8 *buffer, unsigned int size) retval = spi_sync_transfer(spi, &transfer, 1); -#ifdef DEBUG_FIFO_ACCESS + /* print content read from fifo for debugging purposes */ for (i = 0; i < size; i++) dev_dbg(&spi->dev, "%d - 0x%x\n", i, local_buffer[i + 1]); -#endif memcpy(buffer, &local_buffer[1], size); @@ -883,9 +811,7 @@ int rf69_read_fifo(struct spi_device *spi, u8 *buffer, unsigned int size) int rf69_write_fifo(struct spi_device *spi, u8 *buffer, unsigned int size) { -#ifdef DEBUG_FIFO_ACCESS int i; -#endif u8 local_buffer[FIFO_SIZE + 1]; if (size > FIFO_SIZE) { @@ -897,10 +823,9 @@ int rf69_write_fifo(struct spi_device *spi, u8 *buffer, unsigned int size) local_buffer[0] = REG_FIFO | WRITE_BIT; memcpy(&local_buffer[1], buffer, size); -#ifdef DEBUG_FIFO_ACCESS + /* print content written from fifo for debugging purposes */ for (i = 0; i < size; i++) - dev_dbg(&spi->dev, "0x%x\n", buffer[i]); -#endif + dev_dbg(&spi->dev, "%d - 0x%x\n", i, buffer[i]); return spi_write(spi, local_buffer, size + 1); } diff --git a/drivers/staging/pi433/rf69.h b/drivers/staging/pi433/rf69.h index b648ba5fff89..78fa0b8bab8b 100644 --- a/drivers/staging/pi433/rf69.h +++ b/drivers/staging/pi433/rf69.h @@ -17,6 +17,8 @@ #define FIFO_SIZE 66 /* bytes */ #define FIFO_THRESHOLD 15 /* bytes */ +u8 rf69_read_reg(struct spi_device *spi, u8 addr); +int rf69_get_version(struct spi_device *spi); int rf69_set_mode(struct spi_device *spi, enum mode mode); int rf69_set_data_mode(struct spi_device *spi, u8 data_mode); int rf69_set_modulation(struct spi_device *spi, enum modulation modulation); @@ -40,7 +42,6 @@ int rf69_set_bandwidth_during_afc(struct spi_device *spi, int rf69_set_ook_threshold_dec(struct spi_device *spi, enum threshold_decrement threshold_decrement); int rf69_set_dio_mapping(struct spi_device *spi, u8 dio_number, u8 value); -bool rf69_get_flag(struct spi_device *spi, enum flag flag); int rf69_set_rssi_threshold(struct spi_device *spi, u8 threshold); int rf69_set_preamble_length(struct spi_device *spi, u16 preamble_length); int rf69_enable_sync(struct spi_device *spi); diff --git a/drivers/staging/pi433/rf69_enum.h b/drivers/staging/pi433/rf69_enum.h index b33a33a85d3b..9dc906124e98 100644 --- a/drivers/staging/pi433/rf69_enum.h +++ b/drivers/staging/pi433/rf69_enum.h @@ -84,26 +84,6 @@ enum threshold_decrement { dec_16times }; -enum flag { - mode_switch_completed, - ready_to_receive, - ready_to_send, - pll_locked, - rssi_exceeded_threshold, - timeout, - automode, - sync_address_match, - fifo_full, -// fifo_not_empty, collision with next enum; replaced by following enum... - fifo_empty, - fifo_level_below_threshold, - fifo_overrun, - packet_sent, - payload_ready, - crc_ok, - battery_low -}; - enum fifo_fill_condition { after_sync_interrupt, always diff --git a/drivers/staging/pi433/rf69_registers.h b/drivers/staging/pi433/rf69_registers.h index a170c66c3d5b..0d6737738841 100644 --- a/drivers/staging/pi433/rf69_registers.h +++ b/drivers/staging/pi433/rf69_registers.h @@ -89,9 +89,11 @@ #define REG_AESKEY16 0x4D #define REG_TEMP1 0x4E #define REG_TEMP2 0x4F +#define REG_TESTLNA 0x58 #define REG_TESTPA1 0x5A /* only present on RFM69HW */ #define REG_TESTPA2 0x5C /* only present on RFM69HW */ #define REG_TESTDAGC 0x6F +#define REG_TESTAFC 0x71 /******************************************************/ /* RF69/SX1231 bit definition */ |