2021-07-11 23:43:35 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
|
|
|
* Driver for the DapperMime-JTAG USB multitool: character device for userspace
|
|
|
|
* access while the module is loaded
|
|
|
|
*
|
|
|
|
* Copyright (c) sys64738 and haskal
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/cdev.h>
|
2021-07-14 21:26:04 +00:00
|
|
|
#include <linux/version.h>
|
2021-07-11 23:43:35 +00:00
|
|
|
#include <linux/device.h>
|
2021-07-14 21:26:04 +00:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,9,0) /* TODO: make this check more precise */
|
2021-07-11 23:43:35 +00:00
|
|
|
#include <linux/device/class.h>
|
2021-07-14 21:26:04 +00:00
|
|
|
#endif
|
2021-07-11 23:43:35 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
|
|
|
|
#if 0
|
2021-08-17 21:42:57 +00:00
|
|
|
#include <linux/mfd/dragonprobe.h>
|
2021-07-11 23:43:35 +00:00
|
|
|
#else
|
2021-08-17 21:42:57 +00:00
|
|
|
#include "dragonprobe.h"
|
2021-07-11 23:43:35 +00:00
|
|
|
#endif
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
#define HARDWARE_NAME "Dragon Probe"
|
|
|
|
#define DEVICE_NAME "dragonprobe-char"
|
|
|
|
#define CLASS_NAME "dragonprobe"
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
struct dp_char_dev {
|
2021-07-11 23:43:35 +00:00
|
|
|
struct cdev cdev;
|
|
|
|
struct device *dev;
|
|
|
|
struct platform_device *pdev;
|
|
|
|
int minor;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int n_cdevs = 0;
|
|
|
|
static spinlock_t ndevs_lock;
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
static int dp_char_major;
|
|
|
|
static struct class *dp_char_class;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
static ssize_t dp_char_read(struct file *file, char *buf, size_t len, loff_t *loff)
|
2021-07-11 23:43:35 +00:00
|
|
|
{
|
2021-07-14 02:45:35 +00:00
|
|
|
int res, ilen;
|
|
|
|
unsigned long ret;
|
2021-08-17 21:42:57 +00:00
|
|
|
struct dp_char_dev *dpch = file->private_data;
|
2021-07-14 02:45:35 +00:00
|
|
|
void *kbuf = NULL;
|
2021-08-17 21:42:57 +00:00
|
|
|
struct device *dev = dpch->dev;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-07-14 02:45:35 +00:00
|
|
|
if (len > INT_MAX) return -EINVAL;
|
|
|
|
ilen = (int)len;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-07-25 01:40:34 +00:00
|
|
|
/* no flags: act like libusb read */
|
2021-08-17 21:42:57 +00:00
|
|
|
res = dp_transfer(dpch->pdev, -1, 0/*DP_XFER_FLAGS_FILL_RECVBUF*/, NULL, 0, &kbuf, &ilen);
|
2021-07-14 02:45:35 +00:00
|
|
|
if (res < 0 || ilen < 0 || !kbuf) {
|
2021-07-25 17:47:22 +00:00
|
|
|
//dev_warn(dev, "err res=%d ilen=%d kbuf=%p\n", res, ilen, kbuf);
|
2021-07-14 02:45:35 +00:00
|
|
|
if (kbuf) kfree(kbuf);
|
|
|
|
return (res >= 0) ? -EIO : ret;
|
2021-07-11 23:43:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 02:45:35 +00:00
|
|
|
ret = copy_to_user(buf, kbuf, (size_t)ilen);
|
|
|
|
kfree(kbuf);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-07-14 02:45:35 +00:00
|
|
|
if (ret) return -EFAULT;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-07-14 02:45:35 +00:00
|
|
|
return (ssize_t)ilen;
|
2021-07-11 23:43:35 +00:00
|
|
|
}
|
2021-08-17 21:42:57 +00:00
|
|
|
static ssize_t dp_char_write(struct file *file, const char *buf, size_t len, loff_t *off)
|
2021-07-11 23:43:35 +00:00
|
|
|
{
|
|
|
|
unsigned long ret;
|
2021-07-14 02:45:35 +00:00
|
|
|
int res;
|
2021-08-17 21:42:57 +00:00
|
|
|
struct dp_char_dev *dpch = file->private_data;
|
2021-07-11 23:43:35 +00:00
|
|
|
void *kbuf;
|
|
|
|
|
|
|
|
kbuf = kmalloc(len, GFP_KERNEL);
|
|
|
|
if (!kbuf) return -ENOMEM;
|
|
|
|
|
|
|
|
ret = copy_from_user(kbuf, buf, len);
|
|
|
|
if (ret) {
|
|
|
|
kfree(kbuf);
|
2021-07-14 02:45:35 +00:00
|
|
|
return -EFAULT;
|
2021-07-11 23:43:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
res = dp_transfer(dpch->pdev, -1, 0, kbuf, len, NULL, NULL);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
kfree(kbuf);
|
2021-07-14 02:45:35 +00:00
|
|
|
return (res < 0) ? res : len;
|
2021-07-11 23:43:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
static int dp_char_open(struct inode *inode, struct file *file)
|
2021-07-11 23:43:35 +00:00
|
|
|
{
|
2021-08-17 21:42:57 +00:00
|
|
|
struct dp_char_dev *dpch;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
dpch = container_of(inode->i_cdev, struct dp_char_dev, cdev);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
file->private_data = dpch;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-17 21:42:57 +00:00
|
|
|
static int dp_char_release(struct inode *inode, struct file *file)
|
2021-07-11 23:43:35 +00:00
|
|
|
{
|
2021-08-17 21:42:57 +00:00
|
|
|
struct dp_char_dev *dpch;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
dpch = container_of(inode->i_cdev, struct dp_char_dev, cdev);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
static const struct file_operations dp_char_fops = {
|
2021-07-11 23:43:35 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
2021-08-17 21:42:57 +00:00
|
|
|
.read = dp_char_read,
|
|
|
|
.write = dp_char_write,
|
2021-07-11 23:43:35 +00:00
|
|
|
.unlocked_ioctl = NULL,
|
2021-08-17 21:42:57 +00:00
|
|
|
.open = dp_char_open,
|
|
|
|
.release = dp_char_release
|
2021-07-11 23:43:35 +00:00
|
|
|
};
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
static int dp_char_probe(struct platform_device *pdev)
|
2021-07-11 23:43:35 +00:00
|
|
|
{
|
|
|
|
int ret, minor;
|
|
|
|
struct device *device;
|
|
|
|
struct device *pd = &pdev->dev;
|
2021-08-17 21:42:57 +00:00
|
|
|
struct dp_char_dev *dpch;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
spin_lock(&ndevs_lock);
|
|
|
|
minor = n_cdevs;
|
|
|
|
++n_cdevs;
|
|
|
|
spin_unlock(&ndevs_lock);
|
|
|
|
|
|
|
|
dev_info(pd, HARDWARE_NAME " /dev entries driver, major=%d, minor=%d\n",
|
2021-08-17 21:42:57 +00:00
|
|
|
dp_char_major, minor);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
dpch = devm_kzalloc(pd, sizeof(*dpch), GFP_KERNEL);
|
|
|
|
if (!dpch) return -ENOMEM;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
platform_set_drvdata(pdev, dpch);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
cdev_init(&dpch->cdev, &dp_char_fops);
|
|
|
|
ret = cdev_add(&dpch->cdev, MKDEV(dp_char_major, minor), 1);
|
2021-07-11 23:43:35 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(pd, "failed to create cdev: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
device = device_create(dp_char_class, pd, MKDEV(dp_char_major, minor), dpch, "dragonprobe-%d", minor);
|
2021-07-11 23:43:35 +00:00
|
|
|
if (IS_ERR(device)) {
|
|
|
|
ret = PTR_ERR(device);
|
|
|
|
dev_err(pd, "failed to create device: %d\n", ret);
|
2021-08-17 21:42:57 +00:00
|
|
|
cdev_del(&dpch->cdev);
|
2021-07-11 23:43:35 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
dev_notice(device, "created device /dev/dragonprobe-%d\n", minor);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
dpch->dev = device;
|
|
|
|
dpch->minor = minor;
|
|
|
|
dpch->pdev = pdev;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-17 21:42:57 +00:00
|
|
|
static int dp_char_remove(struct platform_device *pdev)
|
2021-07-11 23:43:35 +00:00
|
|
|
{
|
2021-08-17 21:42:57 +00:00
|
|
|
struct dp_char_dev *dpch = platform_get_drvdata(pdev);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
device_destroy(dp_char_class, MKDEV(dp_char_major, dpch->minor));
|
|
|
|
cdev_del(&dpch->cdev);
|
|
|
|
unregister_chrdev(MKDEV(dp_char_major, dpch->minor), CLASS_NAME);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
static struct platform_driver dp_char_driver = {
|
2021-07-11 23:43:35 +00:00
|
|
|
.driver = {
|
2021-08-17 21:42:57 +00:00
|
|
|
.name = "dragonprobe-char"
|
2021-07-11 23:43:35 +00:00
|
|
|
},
|
2021-08-17 21:42:57 +00:00
|
|
|
.probe = dp_char_probe,
|
|
|
|
.remove = dp_char_remove
|
2021-07-11 23:43:35 +00:00
|
|
|
};
|
2021-08-17 21:42:57 +00:00
|
|
|
/*module_platform_driver(dp_char_driver);*/
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
static int __init dp_char_init(void)
|
2021-07-11 23:43:35 +00:00
|
|
|
{
|
|
|
|
int ret, major;
|
|
|
|
struct class *class;
|
|
|
|
dev_t devid;
|
|
|
|
|
|
|
|
spin_lock_init(&ndevs_lock);
|
|
|
|
|
|
|
|
n_cdevs = 0;
|
|
|
|
|
|
|
|
devid = MKDEV(0,0);
|
|
|
|
ret = alloc_chrdev_region(&devid, 0, 256, DEVICE_NAME);
|
|
|
|
if (ret < 0) {
|
|
|
|
printk(KERN_ERR " failed to register chrdev major number: %d\n", major);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
major = MAJOR(devid);
|
|
|
|
printk(KERN_NOTICE DEVICE_NAME " registered with major number %d\n", major);
|
|
|
|
|
|
|
|
class = class_create(THIS_MODULE, CLASS_NAME);
|
|
|
|
if (IS_ERR(class)) {
|
|
|
|
ret = PTR_ERR(class);
|
|
|
|
printk(KERN_ERR " failed to create class: %d\n", ret);
|
2021-07-13 02:11:46 +00:00
|
|
|
unregister_chrdev(major, DEVICE_NAME);
|
2021-07-11 23:43:35 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
printk(KERN_DEBUG DEVICE_NAME " created class\n");
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
dp_char_major = major;
|
|
|
|
dp_char_class = class;
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
platform_driver_register(&dp_char_driver);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-17 21:42:57 +00:00
|
|
|
static void __exit dp_char_exit(void)
|
2021-07-11 23:43:35 +00:00
|
|
|
{
|
2021-08-17 21:42:57 +00:00
|
|
|
platform_driver_unregister(&dp_char_driver);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
spin_lock(&ndevs_lock);
|
|
|
|
n_cdevs = 0;
|
|
|
|
spin_unlock(&ndevs_lock);
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
class_destroy(dp_char_class);
|
|
|
|
unregister_chrdev(dp_char_major, CLASS_NAME);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
dp_char_major = -1;
|
|
|
|
dp_char_class = NULL;
|
2021-07-11 23:43:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 21:42:57 +00:00
|
|
|
module_init(dp_char_init);
|
|
|
|
module_exit(dp_char_exit);
|
2021-07-11 23:43:35 +00:00
|
|
|
|
|
|
|
MODULE_AUTHOR("sys64738 <sys64738@disroot.org>");
|
|
|
|
MODULE_AUTHOR("haskal <haskal@awoo.systems>");
|
|
|
|
MODULE_DESCRIPTION("Character device for the " HARDWARE_NAME " USB multitool");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
2021-08-17 21:42:57 +00:00
|
|
|
MODULE_ALIAS("platform:dragonprobe-char");
|