Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/pci.h>
- #include <linux/ioctl.h>
- #include <linux/version.h>
- #include <asm/uaccess.h>
- #include <linux/string.h>
- #include <linux/fs.h>
- #include <asm/segment.h>
- #include <linux/buffer_head.h>
- #include "caller.h"
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Sorokin AA");
- MODULE_DESCRIPTION("ioctl module");
- MODULE_VERSION("0.01");
- static char logBuffer[1024] = "\0";
- // dev_open function
- static int dev_open(struct inode* n, struct file* f)
- {
- return 0;
- }
- // dev_release function
- static int dev_release(struct inode* n, struct file* f)
- {
- return 0;
- }
- static ssize_t dev_read( struct file * file, char * buf, size_t count, loff_t *ppos )
- {
- int len = strlen(logBuffer);
- if( count < len ) return -EINVAL;
- if( *ppos != 0 ) {
- printk( KERN_INFO "=== read return : 0\n" ); // EOF
- return 0;
- }
- if(copy_to_user(buf, logBuffer, len))
- {
- return -EINVAL;
- }
- *ppos = len;
- return len;
- }
- // find pci
- static void findPCI(int operType, argVenDev venDev, int devFN)
- {
- if (operType == FIND_FUNCTION_NUMBER)
- {
- struct pci_dev *dev = NULL;
- dev = pci_get_device(venDev.vendor, venDev.device, dev);
- if(dev == NULL)
- {
- strcat(logBuffer, "Can't find a device with such vendor and device\n");
- strcat(logBuffer, "\n");
- }
- else {
- strcat(logBuffer, "Found a device with such vendor and device\n");
- sprintf(logBuffer + strlen(logBuffer), "Device function number: %d", dev->devfn);
- strcat(logBuffer, "\n");
- }
- }
- else if (operType == FIND_VENDOR_DEVICE)
- {
- struct pci_dev *dev = NULL;
- bool found = false;
- do {
- dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
- if(dev == NULL)
- break;
- if(dev->devfn == devFN)
- {
- strcat(logBuffer, "Found a device with function number\n");
- sprintf(logBuffer + strlen(logBuffer), "Vendor: %d\n", dev->vendor);
- sprintf(logBuffer + strlen(logBuffer), "Device: %d", dev->device);
- strcat(logBuffer, "\n");
- found = true;
- }
- } while (dev != NULL);
- if(!found)
- {
- strcat(logBuffer, "Can't find a device with such function number\n");
- }
- }
- }
- static void writeLog(void)
- {
- struct file *log = NULL;
- static char filePath[] = "/home/sorokinaa/Documents/bos3/ioctl_log";
- log = filp_open(filePath, O_WRONLY | O_APPEND, 0);
- if (log == NULL)
- {
- pr_info("filp open error\n");
- return;
- }
- kernel_write(log, logBuffer, strlen(logBuffer), &log->f_pos);
- filp_close(log, NULL);
- memset(logBuffer, 0, 1024);
- }
- // dev_ioctl
- #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35))
- static int dev_ioctl(struct inode *i, struct file *f, unsigned int cmd, unsigned long arg)
- #else
- static long dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
- #endif
- {
- if(_IOC_TYPE(cmd) != IOCTL_MAGIC) return -ENOTTY;
- void __user *arg_user = (void __user*)arg;
- argVenDev venDev;
- int devFN;
- strcat(logBuffer, "\n");
- strcat(logBuffer, "IOCTL is called\n");
- switch( cmd )
- {
- case IOCTL_GET_PSI_DEVFN:
- {
- strcat(logBuffer, "DEVFN is called\n");
- copy_from_user(&venDev, arg_user, sizeof(venDev));
- // pr_info("Vendor sent: %u", venDev.vendor);
- // pr_info("Device sent: %u\n", venDev.device);
- // pr_info("\n");
- findPCI(FIND_FUNCTION_NUMBER, venDev, devFN);
- strcat(logBuffer, "\n");
- break;
- }
- case IOCTL_GET_PSI_VENDEV:
- {
- strcat(logBuffer, "VENDEV is called\n");
- copy_from_user(&devFN, arg_user, sizeof(devFN));
- // pr_info("Function sent: %d\n", devFN);
- // pr_info("\n");
- findPCI(FIND_VENDOR_DEVICE, venDev, devFN);
- strcat(logBuffer, "\n");
- break;
- }
- }
- writeLog();
- return 0;
- }
- struct file_operations ioctlOps = {
- .read = dev_read,
- #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35))
- .ioctl = dev_ioctl,
- #else
- .unlocked_ioctl = dev_ioctl,
- #endif
- .open = dev_open,
- .release = dev_release,
- };
- static void printDevices(void)
- {
- struct pci_dev *dev = NULL;
- do {
- dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
- if(dev == NULL)
- break;
- pr_info("Device function number: %lu", dev->devfn);
- pr_info("Vendor id: %u", dev->vendor);
- pr_info("Device id: %u", dev->device);
- pr_info("\n");
- }while (dev != NULL);
- }
- // Init function
- static int __init initModule(void)
- {
- // pr_info = define printk with KERN_INFO priority
- pr_info("Called init function\n");
- // List all PSI device numbers to see numbers with dmesg
- printDevices();
- // Registering device to access it by /dev/ioctl_module
- int ret;
- if ( (ret = register_chrdev(200, "ioctl_module", &ioctlOps) < 0) )
- {
- pr_info("Can't register current device\n" );
- return 1;
- }
- else
- {
- pr_info("Successfully registered device\n");
- pr_info("\n");
- }
- return 0;
- }
- // Exit function
- static void __exit exitModule(void)
- {
- unregister_chrdev(200, "ioctl_module");
- pr_info( "Called exit function\n" );
- }
- // Registering module functions
- module_init(initModule);
- module_exit(exitModule);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement