|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Driver for FPGA Accelerated Function Unit (AFU) |
| 4 | + * |
| 5 | + * Copyright (C) 2017-2018 Intel Corporation, Inc. |
| 6 | + * |
| 7 | + * Authors: |
| 8 | + |
| 9 | + * Xiao Guangrong <[email protected]> |
| 10 | + * Joseph Grecco <[email protected]> |
| 11 | + * Enno Luebbers <[email protected]> |
| 12 | + * Tim Whisonant <[email protected]> |
| 13 | + * Ananda Ravuri <[email protected]> |
| 14 | + * Henry Mitchel <[email protected]> |
| 15 | + */ |
| 16 | + |
| 17 | +#include <linux/kernel.h> |
| 18 | +#include <linux/module.h> |
| 19 | + |
| 20 | +#include "dfl.h" |
| 21 | + |
| 22 | +static int port_hdr_init(struct platform_device *pdev, |
| 23 | + struct dfl_feature *feature) |
| 24 | +{ |
| 25 | + dev_dbg(&pdev->dev, "PORT HDR Init.\n"); |
| 26 | + |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +static void port_hdr_uinit(struct platform_device *pdev, |
| 31 | + struct dfl_feature *feature) |
| 32 | +{ |
| 33 | + dev_dbg(&pdev->dev, "PORT HDR UInit.\n"); |
| 34 | +} |
| 35 | + |
| 36 | +static const struct dfl_feature_ops port_hdr_ops = { |
| 37 | + .init = port_hdr_init, |
| 38 | + .uinit = port_hdr_uinit, |
| 39 | +}; |
| 40 | + |
| 41 | +static struct dfl_feature_driver port_feature_drvs[] = { |
| 42 | + { |
| 43 | + .id = PORT_FEATURE_ID_HEADER, |
| 44 | + .ops = &port_hdr_ops, |
| 45 | + }, |
| 46 | + { |
| 47 | + .ops = NULL, |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +static int afu_open(struct inode *inode, struct file *filp) |
| 52 | +{ |
| 53 | + struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode); |
| 54 | + struct dfl_feature_platform_data *pdata; |
| 55 | + int ret; |
| 56 | + |
| 57 | + pdata = dev_get_platdata(&fdev->dev); |
| 58 | + if (WARN_ON(!pdata)) |
| 59 | + return -ENODEV; |
| 60 | + |
| 61 | + ret = dfl_feature_dev_use_begin(pdata); |
| 62 | + if (ret) |
| 63 | + return ret; |
| 64 | + |
| 65 | + dev_dbg(&fdev->dev, "Device File Open\n"); |
| 66 | + filp->private_data = fdev; |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +static int afu_release(struct inode *inode, struct file *filp) |
| 72 | +{ |
| 73 | + struct platform_device *pdev = filp->private_data; |
| 74 | + struct dfl_feature_platform_data *pdata; |
| 75 | + |
| 76 | + dev_dbg(&pdev->dev, "Device File Release\n"); |
| 77 | + |
| 78 | + pdata = dev_get_platdata(&pdev->dev); |
| 79 | + |
| 80 | + dfl_feature_dev_use_end(pdata); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 86 | +{ |
| 87 | + struct platform_device *pdev = filp->private_data; |
| 88 | + struct dfl_feature_platform_data *pdata; |
| 89 | + struct dfl_feature *f; |
| 90 | + long ret; |
| 91 | + |
| 92 | + dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd); |
| 93 | + |
| 94 | + pdata = dev_get_platdata(&pdev->dev); |
| 95 | + |
| 96 | + switch (cmd) { |
| 97 | + default: |
| 98 | + /* |
| 99 | + * Let sub-feature's ioctl function to handle the cmd |
| 100 | + * Sub-feature's ioctl returns -ENODEV when cmd is not |
| 101 | + * handled in this sub feature, and returns 0 and other |
| 102 | + * error code if cmd is handled. |
| 103 | + */ |
| 104 | + dfl_fpga_dev_for_each_feature(pdata, f) |
| 105 | + if (f->ops && f->ops->ioctl) { |
| 106 | + ret = f->ops->ioctl(pdev, f, cmd, arg); |
| 107 | + if (ret != -ENODEV) |
| 108 | + return ret; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return -EINVAL; |
| 113 | +} |
| 114 | + |
| 115 | +static const struct file_operations afu_fops = { |
| 116 | + .owner = THIS_MODULE, |
| 117 | + .open = afu_open, |
| 118 | + .release = afu_release, |
| 119 | + .unlocked_ioctl = afu_ioctl, |
| 120 | +}; |
| 121 | + |
| 122 | +static int afu_probe(struct platform_device *pdev) |
| 123 | +{ |
| 124 | + int ret; |
| 125 | + |
| 126 | + dev_dbg(&pdev->dev, "%s\n", __func__); |
| 127 | + |
| 128 | + ret = dfl_fpga_dev_feature_init(pdev, port_feature_drvs); |
| 129 | + if (ret) |
| 130 | + return ret; |
| 131 | + |
| 132 | + ret = dfl_fpga_dev_ops_register(pdev, &afu_fops, THIS_MODULE); |
| 133 | + if (ret) |
| 134 | + dfl_fpga_dev_feature_uinit(pdev); |
| 135 | + |
| 136 | + return ret; |
| 137 | +} |
| 138 | + |
| 139 | +static int afu_remove(struct platform_device *pdev) |
| 140 | +{ |
| 141 | + dev_dbg(&pdev->dev, "%s\n", __func__); |
| 142 | + |
| 143 | + dfl_fpga_dev_ops_unregister(pdev); |
| 144 | + dfl_fpga_dev_feature_uinit(pdev); |
| 145 | + |
| 146 | + return 0; |
| 147 | +} |
| 148 | + |
| 149 | +static struct platform_driver afu_driver = { |
| 150 | + .driver = { |
| 151 | + .name = DFL_FPGA_FEATURE_DEV_PORT, |
| 152 | + }, |
| 153 | + .probe = afu_probe, |
| 154 | + .remove = afu_remove, |
| 155 | +}; |
| 156 | + |
| 157 | +module_platform_driver(afu_driver); |
| 158 | + |
| 159 | +MODULE_DESCRIPTION("FPGA Accelerated Function Unit driver"); |
| 160 | +MODULE_AUTHOR("Intel Corporation"); |
| 161 | +MODULE_LICENSE("GPL v2"); |
| 162 | +MODULE_ALIAS("platform:dfl-port"); |
0 commit comments