|
| 1 | +/* |
| 2 | + * Copyright (C) 2010 Broadcom |
| 3 | + * Copyright (C) 2015 Noralf Trønnes |
| 4 | + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd. |
| 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 version 2 as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#include <linux/cdev.h> |
| 13 | +#include <linux/device.h> |
| 14 | +#include <linux/fs.h> |
| 15 | +#include <linux/init.h> |
| 16 | +#include <linux/ioctl.h> |
| 17 | +#include <linux/module.h> |
| 18 | +#include <linux/slab.h> |
| 19 | +#include <linux/uaccess.h> |
| 20 | +#include <linux/compat.h> |
| 21 | +#include <linux/miscdevice.h> |
| 22 | +#include <linux/of.h> |
| 23 | +#include <linux/platform_device.h> |
| 24 | +#include <soc/bcm2835/raspberrypi-firmware.h> |
| 25 | + |
| 26 | +#define MODULE_NAME "vcio" |
| 27 | +#define VCIO_IOC_MAGIC 100 |
| 28 | +#define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *) |
| 29 | +#ifdef CONFIG_COMPAT |
| 30 | +#define IOCTL_MBOX_PROPERTY32 _IOWR(VCIO_IOC_MAGIC, 0, compat_uptr_t) |
| 31 | +#endif |
| 32 | + |
| 33 | +struct vcio_data { |
| 34 | + struct rpi_firmware *fw; |
| 35 | + struct miscdevice misc_dev; |
| 36 | +}; |
| 37 | + |
| 38 | +static int vcio_user_property_list(struct vcio_data *vcio, void *user) |
| 39 | +{ |
| 40 | + u32 *buf, size; |
| 41 | + int ret; |
| 42 | + |
| 43 | + /* The first 32-bit is the size of the buffer */ |
| 44 | + if (copy_from_user(&size, user, sizeof(size))) |
| 45 | + return -EFAULT; |
| 46 | + |
| 47 | + buf = kmalloc(size, GFP_KERNEL); |
| 48 | + if (!buf) |
| 49 | + return -ENOMEM; |
| 50 | + |
| 51 | + if (copy_from_user(buf, user, size)) { |
| 52 | + kfree(buf); |
| 53 | + return -EFAULT; |
| 54 | + } |
| 55 | + |
| 56 | + /* Strip off protocol encapsulation */ |
| 57 | + ret = rpi_firmware_property_list(vcio->fw, &buf[2], size - 12); |
| 58 | + if (ret) { |
| 59 | + kfree(buf); |
| 60 | + return ret; |
| 61 | + } |
| 62 | + |
| 63 | + buf[1] = RPI_FIRMWARE_STATUS_SUCCESS; |
| 64 | + if (copy_to_user(user, buf, size)) |
| 65 | + ret = -EFAULT; |
| 66 | + |
| 67 | + kfree(buf); |
| 68 | + |
| 69 | + return ret; |
| 70 | +} |
| 71 | + |
| 72 | +static int vcio_device_open(struct inode *inode, struct file *file) |
| 73 | +{ |
| 74 | + try_module_get(THIS_MODULE); |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +static int vcio_device_release(struct inode *inode, struct file *file) |
| 80 | +{ |
| 81 | + module_put(THIS_MODULE); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +static long vcio_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 87 | + unsigned long ioctl_param) |
| 88 | +{ |
| 89 | + struct vcio_data *vcio = container_of(file->private_data, |
| 90 | + struct vcio_data, misc_dev); |
| 91 | + |
| 92 | + switch (ioctl_num) { |
| 93 | + case IOCTL_MBOX_PROPERTY: |
| 94 | + return vcio_user_property_list(vcio, (void *)ioctl_param); |
| 95 | + default: |
| 96 | + pr_err("unknown ioctl: %x\n", ioctl_num); |
| 97 | + return -EINVAL; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#ifdef CONFIG_COMPAT |
| 102 | +static long vcio_device_compat_ioctl(struct file *file, unsigned int ioctl_num, |
| 103 | + unsigned long ioctl_param) |
| 104 | +{ |
| 105 | + struct vcio_data *vcio = container_of(file->private_data, |
| 106 | + struct vcio_data, misc_dev); |
| 107 | + |
| 108 | + switch (ioctl_num) { |
| 109 | + case IOCTL_MBOX_PROPERTY32: |
| 110 | + return vcio_user_property_list(vcio, compat_ptr(ioctl_param)); |
| 111 | + default: |
| 112 | + pr_err("unknown ioctl: %x\n", ioctl_num); |
| 113 | + return -EINVAL; |
| 114 | + } |
| 115 | +} |
| 116 | +#endif |
| 117 | + |
| 118 | +const struct file_operations vcio_fops = { |
| 119 | + .unlocked_ioctl = vcio_device_ioctl, |
| 120 | +#ifdef CONFIG_COMPAT |
| 121 | + .compat_ioctl = vcio_device_compat_ioctl, |
| 122 | +#endif |
| 123 | + .open = vcio_device_open, |
| 124 | + .release = vcio_device_release, |
| 125 | +}; |
| 126 | + |
| 127 | +static int vcio_probe(struct platform_device *pdev) |
| 128 | +{ |
| 129 | + struct device *dev = &pdev->dev; |
| 130 | + struct device_node *np = dev->of_node; |
| 131 | + struct device_node *fw_node; |
| 132 | + struct rpi_firmware *fw; |
| 133 | + struct vcio_data *vcio; |
| 134 | + |
| 135 | + fw_node = of_get_parent(np); |
| 136 | + if (!fw_node) { |
| 137 | + dev_err(dev, "Missing firmware node\n"); |
| 138 | + return -ENOENT; |
| 139 | + } |
| 140 | + |
| 141 | + fw = rpi_firmware_get(fw_node); |
| 142 | + of_node_put(fw_node); |
| 143 | + if (!fw) |
| 144 | + return -EPROBE_DEFER; |
| 145 | + |
| 146 | + vcio = devm_kzalloc(dev, sizeof(struct vcio_data), GFP_KERNEL); |
| 147 | + if (!vcio) |
| 148 | + return -ENOMEM; |
| 149 | + |
| 150 | + vcio->fw = fw; |
| 151 | + vcio->misc_dev.fops = &vcio_fops; |
| 152 | + vcio->misc_dev.minor = MISC_DYNAMIC_MINOR; |
| 153 | + vcio->misc_dev.name = "vcio"; |
| 154 | + vcio->misc_dev.parent = dev; |
| 155 | + |
| 156 | + return misc_register(&vcio->misc_dev); |
| 157 | +} |
| 158 | + |
| 159 | +static void vcio_remove(struct platform_device *pdev) |
| 160 | +{ |
| 161 | + struct device *dev = &pdev->dev; |
| 162 | + |
| 163 | + misc_deregister(dev_get_drvdata(dev)); |
| 164 | +} |
| 165 | + |
| 166 | +static const struct of_device_id vcio_ids[] = { |
| 167 | + { .compatible = "raspberrypi,vcio" }, |
| 168 | + { } |
| 169 | +}; |
| 170 | +MODULE_DEVICE_TABLE(of, vcio_ids); |
| 171 | + |
| 172 | +static struct platform_driver vcio_driver = { |
| 173 | + .driver = { |
| 174 | + .name = MODULE_NAME, |
| 175 | + .of_match_table = of_match_ptr(vcio_ids), |
| 176 | + }, |
| 177 | + .probe = vcio_probe, |
| 178 | + .remove = vcio_remove, |
| 179 | +}; |
| 180 | + |
| 181 | +module_platform_driver(vcio_driver); |
| 182 | + |
| 183 | +MODULE_AUTHOR("Gray Girling"); |
| 184 | +MODULE_AUTHOR("Noralf Trønnes"); |
| 185 | +MODULE_DESCRIPTION("Mailbox userspace access"); |
| 186 | +MODULE_LICENSE("GPL"); |
| 187 | +MODULE_ALIAS("platform:rpi-vcio"); |
0 commit comments