/* * * handshake_client.c * Description: * handshake mrpc client driver * * * GPL LICENSE SUMMARY * * Copyright(c) 2016 Intel Corporation. * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. * The full GNU General Public License is included in this distribution * in the file called LICENSE.GPL. * * Contact Information: * Intel Corporation * 2200 Mission College Blvd. * Santa Clara, CA 97052 */ #define pr_fmt(fmt) KBUILD_MODNAME ":%s:%d " fmt "\n", __func__, __LINE__ /************************************************/ /** Includes */ /************************************************/ #include #include #include #include #include #include #include #include #include #include #include #ifdef DEBUG #define DBG(fmt, ...) pr_err(fmt, ##__VA_ARGS__) #else #define DBG(fmt, ...) pr_debug(fmt, ##__VA_ARGS__) #endif #define HANDSHAKE_CLIENT_NAME "handshake_client" /* driver private database */ struct handshake_private { struct platform_device *pdev; /* platform device */ struct mrpc_client *mrpc; /* mrpc client handle */ }; static struct handshake_private *this; static inline int handshake_mrpc_poll(void) { struct handshake_private *priv = this; int status, ret, errcode; if (!priv) { pr_err("ERROR: mrpc handshake not initialized"); return -1; } ret = mrpc_call(priv->mrpc, 0, NULL, 0, &status, sizeof(status), 0, &errcode); if (ret || errcode) { pr_err("ERROR: ret=%d, errcode=%d", ret, errcode); return -2; } return ntohl(status); } static ssize_t status_show(struct device *dev, struct device_attribute *attr, char *buf) { struct handshake_private *priv = dev_get_drvdata(dev); int ret; if (!priv) return -EINVAL; ret = handshake_mrpc_poll(); return scnprintf(buf, PAGE_SIZE, "%d\n", ret); } static DEVICE_ATTR(status, S_IRUGO, status_show, NULL); static struct attribute *handshake_attrs[] = { &dev_attr_status.attr, NULL }; static struct attribute_group handshake_attrs_group = { .attrs = handshake_attrs, }; /** * handshake_mrpc_probe * * @param pdev platform device * * @return 0 for success, error code otherwise */ static int handshake_mrpc_probe(struct platform_device *pdev) { struct handshake_private *priv; int ret; priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); if (!priv) { pr_err("memory allocation failed"); return -ENOMEM; } dev_set_drvdata(&pdev->dev, priv); priv->pdev = pdev; ret = sysfs_create_group(&priv->pdev->dev.kobj, &handshake_attrs_group); if (ret) { pr_err("sysfs_create_group failed (ret=%d)", ret); return ret; } priv->mrpc = mrpc_client_register(MRPC_RESERVED_ID_HANDSHAKE, HANDSHAKE_CLIENT_NAME); if (!priv->mrpc) { pr_err("failed to register handshake"); ret = -ENODEV; goto out_remove_group; } this = priv; return 0; out_remove_group: sysfs_remove_group(&priv->pdev->dev.kobj, &handshake_attrs_group); return 0; } /** * handshake_mrpc_remove * * This function is called when the handshake mrpc driver is * removed. * * @param pdev platform device * * @return 0 for success, error code otherwise */ static int handshake_mrpc_remove(struct platform_device *pdev) { struct handshake_private *priv = platform_get_drvdata(pdev); mrpc_client_unregister(priv->mrpc); sysfs_remove_group(&priv->pdev->dev.kobj, &handshake_attrs_group); dev_set_drvdata(&pdev->dev, NULL); this = NULL; return 0; } static struct platform_driver handshake_driver = { .driver = { .name = HANDSHAKE_CLIENT_NAME, }, .probe = handshake_mrpc_probe, .remove = handshake_mrpc_remove, }; static struct platform_device *handshake_device; static int handshake_mrpc_init(void) { int ret; ret = platform_driver_register(&handshake_driver); if (ret < 0) { pr_err("Failed to register handshake platform driver: %d\n", ret); return ret; } pr_debug("handshake client platform driver is registered"); handshake_device = platform_device_register_simple(HANDSHAKE_CLIENT_NAME, -1, NULL, 0); if (IS_ERR(handshake_device)) { ret = PTR_ERR(handshake_device); pr_err("Failed to register handshake platform device: %d\n", ret); platform_driver_unregister(&handshake_driver); return ret; } dev_info(&handshake_device->dev, "platform device is registered\n"); return 0; } static void __exit handshake_mrpc_exit(void) { platform_device_unregister(handshake_device); platform_driver_unregister(&handshake_driver); } /******************************************************/ /** Module Declarations **/ /******************************************************/ static_notifier_module_init(mrpc_ready, handshake_mrpc_init); module_exit(handshake_mrpc_exit); MODULE_AUTHOR("Intel Corporation"); MODULE_AUTHOR("Tzviel Lemberger tzviel.lemberger@intel.com"); MODULE_DESCRIPTION("handshake mrpc client"); MODULE_LICENSE("GPL"); MODULE_VERSION("1.0");