├── THANKS ├── chardev.h ├── lir941r.h ├── info.h ├── lir941r_user.h ├── README.md ├── Makefile ├── lir941r_hw.h ├── log.h ├── lir941r_main.c ├── chardev.c ├── lir941r_hw.c └── COPYING /THANKS: -------------------------------------------------------------------------------- 1 | Great thanks to: 2 | D. Kashin, SKB IS - hardware specifications, support and consulting 3 | A. Kozlov, Crimean astrophysical observatory - Hardware testing support 4 | -------------------------------------------------------------------------------- /chardev.h: -------------------------------------------------------------------------------- 1 | /* 2 | chardev.c 3 | - kernel module character devices implementation 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef CHARDEV_H 21 | #define CHARDEV_H 22 | 23 | #include "lir941r.h" 24 | 25 | int create_char_devs(struct lir941r_driver* drv); 26 | int destroy_char_devs(void); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /lir941r.h: -------------------------------------------------------------------------------- 1 | /* 2 | lir941r.h 3 | - defines for lir device 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef LIR_941_R_H 21 | #define LIR_941_R_H 22 | 23 | #include 24 | 25 | #define LIR_941_VENDOR_ID_1 0x0F0F 26 | #define LIR_941_PRODUCT_ID_1 0x0F0F 27 | 28 | struct lir941r_driver { 29 | u8 __iomem *hwmem; 30 | }; 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /info.h: -------------------------------------------------------------------------------- 1 | /* 2 | info.c 3 | - general module info 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef LIR_INFO_H 21 | #define LIR_INFO_H 22 | 23 | #define DRIVER_NAME "lir941r" 24 | #define DRIVER_VERSION "0.1" 25 | #define DRIVER_DESCRIPTION "LIR941R encoders interface driver" 26 | #define DRIVER_COPYRIGHT "Copyright (c) 2017 Oleg Kutkov. Crimean astrophysical observatory" 27 | 28 | 29 | #endif 30 | 31 | -------------------------------------------------------------------------------- /lir941r_user.h: -------------------------------------------------------------------------------- 1 | /* 2 | lir941r_user.h 3 | - defines of the user ioctl's 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef LIR_941_USER_H 21 | #define LIR_941_USER_H 22 | 23 | #define LIR941_START_CHANNEL_POLLING 0x42 24 | #define LIR941_STOP_CHANNEL_POLLING 0x43 25 | 26 | #define LIR941_STOP_CHANNEL_ONE_REQUEST 0x44 27 | 28 | #define LIR941_CHANNEL_DATAWIDTH 0x45 29 | 30 | #define LIR941_CHANNEL_SPEED 0x50 31 | #define LIR941_CHANNEL_PAUSE 0x51 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | LIR-941R driver for Linux 2 | -- 3 | 4 | This device driver supports LIR941R pci encoders interface. 5 | 6 | Hardware vendor: http://www.skbis.ru/ 7 | 8 | Author: Oleg Kutkov , Crimean astrophysical observatory. 9 | 10 | Thanks skbis for device specification and support! 11 | 12 | Building and installation: 13 | 14 | * Install linux kernel headers 15 | * For example Ubuntu/Debian/Mint: sudo apt-get install linux-headers-$(uname -r) 16 | * Build module: 17 | * make 18 | * Install 19 | * sudo make install 20 | 21 | For now module is able to load with command: sudo modprobe lir941r 22 | 23 | If you wish to load this module automatically at boot time - add "lir941r" (without quotes) to the end of the /etc/modules (Ubuntu based systems) or /etc/modules.conf (Red hat based systems). 24 | 25 | After sucessfully loading you can found new char devices in you /dev directory, each device for one encoder channel: 26 | * /dev/lir941r-0 - for channel 1 27 | * /dev/lir941r-1 - for channel 2 28 | * /dev/lir941r-2 - for channel 3 29 | * /dev/lir941r-3 - for channel 4 30 | 31 | Please checkout this simple testing program: https://github.com/olegkutkov/lir941_test_util 32 | 33 | Tested on Linux kernel versions: 3.13, 3.16 x86 and x86_64 34 | 35 | If you encountered some problems - feel free to contact me. 36 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | # Copyright 2017 Oleg Kutkov 5 | # 6 | # This program is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, either version 3 of the License, or 9 | # (at your option) any later version. 10 | # 11 | # This program is distributed in the hope that it will be useful, 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | # GNU General Public License for more details. 15 | # 16 | # You should have received a copy of the GNU General Public License 17 | # along with this program. If not, see . 18 | # 19 | 20 | BINARY := lir941r 21 | KERNEL := /lib/modules/$(shell uname -r)/build 22 | ARCH := x86 23 | C_FLAGS := -Wall 24 | KMOD_DIR := $(shell pwd) 25 | TARGET_PATH := /lib/modules/$(shell uname -r)/kernel/drivers/char 26 | 27 | OBJECTS := \ 28 | lir941r_main.o \ 29 | chardev.o \ 30 | lir941r_hw.o \ 31 | 32 | ccflags-y += $(C_FLAGS) 33 | 34 | obj-m += $(BINARY).o 35 | 36 | $(BINARY)-y := $(OBJECTS) 37 | 38 | $(BINARY).ko: 39 | make -C $(KERNEL) M=$(KMOD_DIR) modules 40 | 41 | install: 42 | cp $(BINARY).ko $(TARGET_PATH) 43 | depmod -a 44 | 45 | uninstall: 46 | rm $(TARGET_PATH)/$(BINARY).ko 47 | depmod -a 48 | 49 | clean: 50 | make -C $(KERNEL) M=$(KMOD_DIR) clean 51 | 52 | -------------------------------------------------------------------------------- /lir941r_hw.h: -------------------------------------------------------------------------------- 1 | /* 2 | lir941r_hw.h 3 | - hw communication module 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef LIR_941_HW_H 21 | #define LIR_941_HW_H 22 | 23 | #include "lir941r.h" 24 | 25 | void set_channel_bit(struct lir941r_driver* drv, uint8_t chnum, uint8_t bit); 26 | void set_channel_clk(struct lir941r_driver* drv, uint8_t chnum, uint16_t clk); 27 | void set_channel_pause_rate(struct lir941r_driver* drv, uint8_t chnum, uint16_t prate); 28 | 29 | void start_channel_polling(struct lir941r_driver* drv, uint8_t chnum); 30 | void stop_channel_polling(struct lir941r_driver* drv, uint8_t chnum); 31 | 32 | void channel_generate_one_req(struct lir941r_driver* drv, uint8_t chnum); 33 | 34 | uint8_t is_channel_polling(struct lir941r_driver* drv, uint8_t chnum); 35 | 36 | uint32_t get_channel_data(struct lir941r_driver* drv, uint8_t chnum); 37 | 38 | #endif 39 | 40 | -------------------------------------------------------------------------------- /log.h: -------------------------------------------------------------------------------- 1 | /* 2 | log.h 3 | - simple logger 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | 21 | #ifndef LIR_LOG_H 22 | #define LIR_LOG_H 23 | 24 | #include 25 | #include "info.h" 26 | 27 | #ifndef LOG_LEVEL_INFO 28 | #define LOG_LEVEL_INFO "INFO" 29 | #endif 30 | 31 | #ifndef LOG_LEVEL_ERROR 32 | #define LOG_LEVEL_ERROR "ERROR" 33 | #endif 34 | 35 | #ifndef LOG_LEVEL_WARNING 36 | #define LOG_LEVEL_WARNING "WARNING" 37 | #endif 38 | 39 | /* 40 | */ 41 | static void __log_func(const char* lvl, const char* fmt, ...) 42 | { 43 | va_list args; 44 | va_start(args, fmt); 45 | printk("%s [%s]: ", DRIVER_NAME, lvl); 46 | vprintk(fmt, args); 47 | va_end(args); 48 | } 49 | 50 | #ifndef LOG_INFO 51 | #define LOG_INFO(fmt, args...) \ 52 | __log_func(LOG_LEVEL_INFO, fmt, ## args) 53 | #endif 54 | 55 | #ifndef LOG_ERROR 56 | #define LOG_ERROR(fmt, args...) \ 57 | __log_func(LOG_LEVEL_ERROR, fmt, ## args) 58 | #endif 59 | 60 | #ifndef LOG_WARNING 61 | #define LOG_WARNING(fmt, args...) \ 62 | __log_func(LOG_LEVEL_WARNING, fmt, ## args) 63 | #endif 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /lir941r_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | lir941r_main.c 3 | - kernel module entry point 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include "chardev.h" 24 | #include "lir941r.h" 25 | #include "log.h" 26 | 27 | static struct pci_device_id lir941r_id_table[] = { 28 | { PCI_DEVICE(LIR_941_VENDOR_ID_1, LIR_941_PRODUCT_ID_1) }, 29 | {0,} 30 | }; 31 | 32 | MODULE_DEVICE_TABLE(pci, lir941r_id_table); 33 | 34 | static int lir941r_probe(struct pci_dev *pdev, const struct pci_device_id *ent); 35 | static void lir941r_remove(struct pci_dev *pdev); 36 | 37 | static struct pci_driver lir941r_driver = { 38 | .name = DRIVER_NAME, 39 | .id_table = lir941r_id_table, 40 | .probe = lir941r_probe, 41 | .remove = lir941r_remove 42 | }; 43 | 44 | static int __init lir941r_init(void) 45 | { 46 | LOG_INFO("Loading %s - version %s\n", DRIVER_DESCRIPTION, DRIVER_VERSION); 47 | LOG_INFO("%s\n", DRIVER_COPYRIGHT); 48 | 49 | return pci_register_driver(&lir941r_driver); 50 | } 51 | 52 | static void __exit lir941r_exit(void) 53 | { 54 | pci_unregister_driver(&lir941r_driver); 55 | } 56 | 57 | int read_device_config(struct pci_dev *pdev) 58 | { 59 | u16 vendor, device, status_reg, command_reg; 60 | 61 | pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor); 62 | pci_read_config_word(pdev, PCI_DEVICE_ID, &device); 63 | 64 | LOG_INFO("Device vid: 0x%X pid: 0x%X\n", vendor, device); 65 | 66 | pci_read_config_word(pdev, PCI_STATUS, &status_reg); 67 | 68 | LOG_INFO("Device status reg: 0x%X\n", status_reg); 69 | 70 | 71 | pci_read_config_word(pdev, PCI_COMMAND, &command_reg); 72 | 73 | if (command_reg | PCI_COMMAND_MEMORY) { 74 | LOG_INFO("Device supports memory access\n"); 75 | 76 | return 0; 77 | } 78 | 79 | LOG_ERROR("Device doesn't supports memory access!"); 80 | 81 | return -EIO; 82 | } 83 | 84 | void release_device(struct pci_dev *pdev) 85 | { 86 | pci_release_region(pdev, pci_select_bars(pdev, IORESOURCE_MEM)); 87 | pci_disable_device(pdev); 88 | } 89 | 90 | static int lir941r_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 91 | { 92 | int bar, err; 93 | unsigned long mmio_start,mmio_len; 94 | struct lir941r_driver *drv_priv; 95 | 96 | if (read_device_config(pdev) < 0) { 97 | return -EIO; 98 | } 99 | 100 | bar = pci_select_bars(pdev, IORESOURCE_MEM); 101 | 102 | LOG_INFO("Device availale MEM BAR are 0x%x\n", bar); 103 | 104 | err = pci_enable_device_mem(pdev); 105 | 106 | if (err) { 107 | LOG_ERROR("Failed to enable LIR device memory, err: %i\n", err); 108 | return err; 109 | } 110 | 111 | err = pci_request_region(pdev, bar, DRIVER_NAME); 112 | 113 | if (err) { 114 | pci_disable_device(pdev); 115 | return err; 116 | } 117 | 118 | mmio_start = pci_resource_start(pdev, 0); 119 | mmio_len = pci_resource_len(pdev, 0); 120 | 121 | LOG_INFO("LIR device resource 0: start at 0x%lx with lenght %lu\n", mmio_start, mmio_len); 122 | 123 | drv_priv = kzalloc(sizeof(struct lir941r_driver), GFP_KERNEL); 124 | 125 | if (!drv_priv) { 126 | release_device(pdev); 127 | return -ENOMEM; 128 | } 129 | 130 | drv_priv->hwmem = ioremap(mmio_start, mmio_len); 131 | 132 | if (!drv_priv->hwmem) { 133 | release_device(pdev); 134 | return -EIO; 135 | } 136 | 137 | LOG_INFO("LIR device mapped resource 0x%lx to 0x%p\n", mmio_start, drv_priv->hwmem); 138 | 139 | create_char_devs(drv_priv); 140 | 141 | pci_set_drvdata(pdev, drv_priv); 142 | 143 | return 0; 144 | } 145 | 146 | static void lir941r_remove(struct pci_dev *pdev) 147 | { 148 | struct lir941r_driver *drv_priv = pci_get_drvdata(pdev); 149 | 150 | destroy_char_devs(); 151 | 152 | if (drv_priv) { 153 | if (drv_priv->hwmem) { 154 | iounmap(drv_priv->hwmem); 155 | } 156 | 157 | kfree(drv_priv); 158 | } 159 | 160 | release_device(pdev); 161 | 162 | LOG_INFO("Unloaded %s\n", DRIVER_DESCRIPTION); 163 | } 164 | 165 | MODULE_LICENSE("GPL"); 166 | MODULE_AUTHOR("Oleg Kutkov "); 167 | MODULE_DESCRIPTION(DRIVER_DESCRIPTION); 168 | MODULE_VERSION(DRIVER_VERSION); 169 | 170 | module_init(lir941r_init); 171 | module_exit(lir941r_exit); 172 | 173 | -------------------------------------------------------------------------------- /chardev.c: -------------------------------------------------------------------------------- 1 | /* 2 | chardev.c 3 | - kernel module character devices implementation 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include "chardev.h" 27 | #include "lir941r_user.h" 28 | #include "lir941r_hw.h" 29 | 30 | #define MAX_DEV 4 31 | #define DEV_MAJOR 89 32 | 33 | static int lirdev_open(struct inode *inode, struct file *file); 34 | static int lirdev_release(struct inode *inode, struct file *file); 35 | static long lirdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 36 | static ssize_t lirdev_read(struct file *file, char __user *buf, size_t count, loff_t *offset); 37 | static ssize_t lirdev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset); 38 | 39 | static const struct file_operations lirdev_fops = { 40 | .owner = THIS_MODULE, 41 | .open = lirdev_open, 42 | .release = lirdev_release, 43 | .unlocked_ioctl = lirdev_ioctl, 44 | .read = lirdev_read, 45 | .write = lirdev_write 46 | }; 47 | 48 | struct lir_device_data { 49 | struct device* lirdev; 50 | struct cdev cdev; 51 | }; 52 | 53 | struct lir_device_private { 54 | uint8_t chnum; 55 | struct lir941r_driver* drv; 56 | }; 57 | 58 | static int dev_major = 0; 59 | static struct class *lirclass = NULL; 60 | static struct lir_device_data lirdev_data[MAX_DEV]; 61 | static struct lir941r_driver* drv_access = NULL; 62 | 63 | static int lir941_uevent(struct device *dev, struct kobj_uevent_env *env) 64 | { 65 | add_uevent_var(env, "DEVMODE=%#o", 0666); 66 | 67 | return 0; 68 | } 69 | 70 | int create_char_devs(struct lir941r_driver* drv) 71 | { 72 | int err, i; 73 | dev_t dev; 74 | 75 | err = alloc_chrdev_region(&dev, 0, MAX_DEV, "lir941r"); 76 | 77 | dev_major = MAJOR(dev); 78 | 79 | lirclass = class_create(THIS_MODULE, "lir941r-dev"); 80 | 81 | lirclass->dev_uevent = lir941_uevent; 82 | 83 | for (i = 0; i < MAX_DEV; i++) { 84 | cdev_init(&lirdev_data[i].cdev, &lirdev_fops); 85 | lirdev_data[i].cdev.owner = THIS_MODULE; 86 | cdev_add(&lirdev_data[i].cdev, MKDEV(dev_major, i), 1); 87 | 88 | lirdev_data[i].lirdev = device_create(lirclass, NULL, MKDEV(dev_major, i), NULL, "lir941r-%d", i); 89 | } 90 | 91 | drv_access = drv; 92 | 93 | return 0; 94 | } 95 | 96 | int destroy_char_devs(void) 97 | { 98 | int i; 99 | 100 | for (i = 0; i < MAX_DEV; i++) { 101 | device_destroy(lirclass, MKDEV(dev_major, i)); 102 | } 103 | 104 | class_unregister(lirclass); 105 | class_destroy(lirclass); 106 | unregister_chrdev_region(MKDEV(dev_major, 0), MINORMASK); 107 | 108 | return 0; 109 | } 110 | 111 | static int lirdev_open(struct inode *inode, struct file *file) 112 | { 113 | struct lir_device_private* lir_priv; 114 | unsigned int minor = iminor(inode); 115 | 116 | lir_priv = kzalloc(sizeof(struct lir_device_private), GFP_KERNEL); 117 | lir_priv->drv = drv_access; 118 | lir_priv->chnum = minor; 119 | 120 | file->private_data = lir_priv; 121 | 122 | return 0; 123 | } 124 | 125 | static int lirdev_release(struct inode *inode, struct file *file) 126 | { 127 | struct lir_device_private* priv = file->private_data; 128 | 129 | kfree(priv); 130 | 131 | priv = NULL; 132 | 133 | return 0; 134 | } 135 | 136 | static long lirdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 137 | { 138 | struct lir_device_private* drv = file->private_data; 139 | 140 | switch (cmd) { 141 | case LIR941_START_CHANNEL_POLLING: 142 | start_channel_polling(drv->drv, drv->chnum); 143 | break; 144 | 145 | case LIR941_STOP_CHANNEL_POLLING: 146 | stop_channel_polling(drv->drv, drv->chnum); 147 | break; 148 | 149 | case LIR941_STOP_CHANNEL_ONE_REQUEST: 150 | channel_generate_one_req(drv->drv, drv->chnum); 151 | break; 152 | 153 | case LIR941_CHANNEL_DATAWIDTH: 154 | set_channel_bit(drv->drv, drv->chnum, (uint8_t)arg); 155 | break; 156 | 157 | case LIR941_CHANNEL_SPEED: 158 | set_channel_clk(drv->drv, drv->chnum, (uint16_t)arg); 159 | break; 160 | 161 | case LIR941_CHANNEL_PAUSE: 162 | set_channel_pause_rate(drv->drv, drv->chnum, (uint16_t)arg); 163 | break; 164 | 165 | default: 166 | return -EINVAL; 167 | }; 168 | 169 | return 0; 170 | } 171 | 172 | static ssize_t lirdev_read(struct file *file, char __user *buf, size_t count, loff_t *offset) 173 | { 174 | struct lir_device_private* drv = file->private_data; 175 | uint32_t data; 176 | 177 | data = get_channel_data(drv->drv, drv->chnum); 178 | 179 | if (copy_to_user(buf, &data, count)) { 180 | return -EFAULT; 181 | } 182 | 183 | return count; 184 | } 185 | 186 | static ssize_t lirdev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) 187 | { 188 | return count; 189 | } 190 | 191 | -------------------------------------------------------------------------------- /lir941r_hw.c: -------------------------------------------------------------------------------- 1 | /* 2 | lir941r_hw.c 3 | - hw communication module 4 | 5 | Copyright 2017 Oleg Kutkov 6 | Crimean astrophysical observatory 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include "lir941r_hw.h" 22 | 23 | /* 24 | * define bit nums in channel control/status register 25 | */ 26 | #define CHREG_ON_REQ_BIT 0 27 | #define CHREG_POLLING_BIT 1 28 | 29 | #define CHREG_EXT1_BIT 2 30 | #define CHREG_EXT2_BIT 3 31 | #define CHREG_EXT3_BIT 4 32 | #define CHREG_EXT4_BIT 5 33 | 34 | #define CHREG_STATUS_TR_ACTIVE_BIT 6 35 | #define CHREG_STATUS_TR_PAUSE_BIT 7 36 | 37 | #define CHREG_CONTROL_MODE 7 38 | 39 | #define MODE_NORMAL_SSI 0 40 | #define MODE_LEGACY_SSI 1 41 | 42 | /* */ 43 | 44 | /* 45 | * data access macros 46 | */ 47 | #define CHANNEL_DATA_OFFSET(chnum) (sizeof(uint32_t) * chnum) 48 | #define CHANNEL_DATA_BIT_OFFSET(chnum) (sizeof(uint8_t) * chnum) 49 | #define CHANNEL_CLK_RATE_OFFSET(chnum) (sizeof(uint32_t) + (sizeof(uint16_t) * chnum)) 50 | #define CHANNEL_PAUSE_RATE_OFFSET(chnum) ((sizeof(uint32_t) * 3) + (sizeof(uint16_t) * chnum)) 51 | #define CHANNEL_RG_ST_OFFSET(chnum) ((sizeof(uint32_t) * 4) + (sizeof(uint8_t) * chnum)) 52 | #define CHANNEL_RG_CTRL_OFFSET(chnum) ((sizeof(uint32_t) * 5) + (sizeof(uint8_t) * chnum)) 53 | 54 | #define CHECK_BIT(var,pos) ((var) & (1 << (pos))) 55 | #define SET_BIT(var, pos) var |= (1 << pos) 56 | #define CLEAR_BIT(var, pos) var &= ~(1 << pos) 57 | 58 | /* */ 59 | 60 | static uint8_t ADAPTER_CHANNEL_MODE = MODE_NORMAL_SSI; 61 | static uint8_t ADAPTER_ALLOW_EXT1 = 1; 62 | static uint8_t ADAPTER_ALLOW_EXT2 = 1; 63 | static uint8_t ADAPTER_ALLOW_EXT3 = 1; 64 | static uint8_t ADAPTER_ALLOW_EXT4 = 1; 65 | 66 | 67 | void set_channel_bit(struct lir941r_driver* drv, uint8_t chnum, uint8_t bit) 68 | { 69 | void* wr_addr = drv->hwmem + CHANNEL_DATA_BIT_OFFSET(chnum); 70 | 71 | printk("lirdevice channel %i set data width = %i\n", chnum, bit); 72 | 73 | iowrite8(bit, wr_addr); 74 | } 75 | 76 | void set_channel_clk(struct lir941r_driver* drv, uint8_t chnum, uint16_t clk) 77 | { 78 | void* wr_addr = drv->hwmem + CHANNEL_CLK_RATE_OFFSET(chnum); 79 | 80 | printk("lirdevice channel %i set clock div = %i\n", chnum, clk); 81 | 82 | iowrite16(clk, wr_addr); 83 | } 84 | 85 | void set_channel_pause_rate(struct lir941r_driver* drv, uint8_t chnum, uint16_t prate) 86 | { 87 | void* wr_addr = drv->hwmem + CHANNEL_PAUSE_RATE_OFFSET(chnum); 88 | 89 | printk("lirdevice channel %i set pause rate = %i\n", chnum, prate); 90 | 91 | iowrite16(prate, wr_addr); 92 | } 93 | 94 | static void wait_for_transaction_finish(struct lir941r_driver* drv, uint8_t chnum) 95 | { 96 | void* r_addr; 97 | uint8_t reg; 98 | 99 | r_addr = drv->hwmem + CHANNEL_RG_ST_OFFSET(chnum); 100 | 101 | while (1) { 102 | reg = ioread8(r_addr); 103 | 104 | if (!CHECK_BIT(reg, CHREG_STATUS_TR_ACTIVE_BIT)) { 105 | break; 106 | } else { 107 | printk("Waiting for transaction finish...\n"); 108 | } 109 | } 110 | } 111 | 112 | static void wait_for_pause_in_tx(struct lir941r_driver* drv, uint8_t chnum) 113 | { 114 | void* r_addr; 115 | uint8_t reg; 116 | 117 | r_addr = drv->hwmem + CHANNEL_RG_ST_OFFSET(chnum); 118 | 119 | while(1) { 120 | reg = ioread8(r_addr); 121 | 122 | if (!CHECK_BIT(reg, CHREG_STATUS_TR_PAUSE_BIT)) { 123 | break; 124 | } else { 125 | printk("Waiting for transaction pause...\n"); 126 | } 127 | } 128 | 129 | } 130 | 131 | static uint8_t create_basic_config_reg(void) 132 | { 133 | uint8_t value = 0x0; 134 | 135 | if (ADAPTER_CHANNEL_MODE != MODE_NORMAL_SSI) { 136 | SET_BIT(value, CHREG_CONTROL_MODE); 137 | } 138 | 139 | if (ADAPTER_ALLOW_EXT1) { 140 | SET_BIT(value, CHREG_EXT1_BIT); 141 | } 142 | 143 | if (ADAPTER_ALLOW_EXT2) { 144 | SET_BIT(value, CHREG_EXT2_BIT); 145 | } 146 | 147 | if (ADAPTER_ALLOW_EXT3) { 148 | SET_BIT(value, CHREG_EXT3_BIT); 149 | } 150 | 151 | if (ADAPTER_ALLOW_EXT4) { 152 | SET_BIT(value, CHREG_EXT4_BIT); 153 | } 154 | 155 | // polling and single request is unset by default 156 | 157 | return value; 158 | } 159 | 160 | static void change_channel_polling_mode(struct lir941r_driver* drv, uint8_t chnum, uint8_t start) 161 | { 162 | void* w_addr; 163 | uint8_t reg; 164 | 165 | w_addr = drv->hwmem + CHANNEL_RG_CTRL_OFFSET(chnum); 166 | 167 | reg = create_basic_config_reg(); 168 | 169 | if (start) { 170 | SET_BIT(reg, CHREG_POLLING_BIT); 171 | } 172 | 173 | printk("config reg value: 0x%X\n", reg); 174 | 175 | iowrite8(reg, w_addr); 176 | } 177 | 178 | void start_channel_polling(struct lir941r_driver* drv, uint8_t chnum) 179 | { 180 | change_channel_polling_mode(drv, chnum, 1); 181 | } 182 | 183 | void stop_channel_polling(struct lir941r_driver* drv, uint8_t chnum) 184 | { 185 | change_channel_polling_mode(drv, chnum, 0); 186 | } 187 | 188 | void channel_generate_one_req(struct lir941r_driver* drv, uint8_t chnum) 189 | { 190 | void* w_addr; 191 | uint8_t reg; 192 | 193 | w_addr = drv->hwmem + CHANNEL_RG_CTRL_OFFSET(chnum); 194 | 195 | reg = create_basic_config_reg(); 196 | 197 | SET_BIT(reg, CHREG_ON_REQ_BIT); 198 | 199 | wait_for_transaction_finish(drv, chnum); 200 | 201 | iowrite8(reg, w_addr); 202 | } 203 | 204 | uint8_t is_channel_polling(struct lir941r_driver* drv, uint8_t chnum) 205 | { 206 | void* read_addr; 207 | uint8_t reg; 208 | 209 | read_addr = drv->hwmem + CHANNEL_RG_ST_OFFSET(chnum); 210 | 211 | reg = ioread8(read_addr); 212 | 213 | return CHECK_BIT(reg, CHREG_POLLING_BIT); 214 | } 215 | 216 | uint32_t get_channel_data(struct lir941r_driver* drv, uint8_t chnum) 217 | { 218 | uint32_t res; 219 | void* read_addr = drv->hwmem + CHANNEL_DATA_OFFSET(chnum); 220 | 221 | wait_for_pause_in_tx(drv, chnum); 222 | 223 | res = ioread32(read_addr); 224 | 225 | printk("raw data from reg: 0x%X\n", res); 226 | 227 | return res; 228 | } 229 | 230 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 675 Mass Ave, Cambridge, MA 02139, USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | 283 | 284 | --------------------------------------------------------------------------------