18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2015, 2016 Intel Corporation.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
58c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
118c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
148c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
168c2ecf20Sopenharmony_ci * General Public License for more details.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * BSD LICENSE
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
218c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
228c2ecf20Sopenharmony_ci * are met:
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *  - Redistributions of source code must retain the above copyright
258c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
268c2ecf20Sopenharmony_ci *  - Redistributions in binary form must reproduce the above copyright
278c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
288c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
298c2ecf20Sopenharmony_ci *    distribution.
308c2ecf20Sopenharmony_ci *  - Neither the name of Intel Corporation nor the names of its
318c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
328c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
358c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
368c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
378c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
388c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
398c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
408c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
418c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
428c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
438c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
448c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#include <linux/cdev.h>
498c2ecf20Sopenharmony_ci#include <linux/module.h>
508c2ecf20Sopenharmony_ci#include <linux/device.h>
518c2ecf20Sopenharmony_ci#include <linux/fs.h>
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#include "hfi.h"
548c2ecf20Sopenharmony_ci#include "device.h"
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic struct class *class;
578c2ecf20Sopenharmony_cistatic struct class *user_class;
588c2ecf20Sopenharmony_cistatic dev_t hfi1_dev;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciint hfi1_cdev_init(int minor, const char *name,
618c2ecf20Sopenharmony_ci		   const struct file_operations *fops,
628c2ecf20Sopenharmony_ci		   struct cdev *cdev, struct device **devp,
638c2ecf20Sopenharmony_ci		   bool user_accessible,
648c2ecf20Sopenharmony_ci		   struct kobject *parent)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	const dev_t dev = MKDEV(MAJOR(hfi1_dev), minor);
678c2ecf20Sopenharmony_ci	struct device *device = NULL;
688c2ecf20Sopenharmony_ci	int ret;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	cdev_init(cdev, fops);
718c2ecf20Sopenharmony_ci	cdev->owner = THIS_MODULE;
728c2ecf20Sopenharmony_ci	cdev_set_parent(cdev, parent);
738c2ecf20Sopenharmony_ci	kobject_set_name(&cdev->kobj, name);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	ret = cdev_add(cdev, dev, 1);
768c2ecf20Sopenharmony_ci	if (ret < 0) {
778c2ecf20Sopenharmony_ci		pr_err("Could not add cdev for minor %d, %s (err %d)\n",
788c2ecf20Sopenharmony_ci		       minor, name, -ret);
798c2ecf20Sopenharmony_ci		goto done;
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (user_accessible)
838c2ecf20Sopenharmony_ci		device = device_create(user_class, NULL, dev, NULL, "%s", name);
848c2ecf20Sopenharmony_ci	else
858c2ecf20Sopenharmony_ci		device = device_create(class, NULL, dev, NULL, "%s", name);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	if (IS_ERR(device)) {
888c2ecf20Sopenharmony_ci		ret = PTR_ERR(device);
898c2ecf20Sopenharmony_ci		device = NULL;
908c2ecf20Sopenharmony_ci		pr_err("Could not create device for minor %d, %s (err %d)\n",
918c2ecf20Sopenharmony_ci			minor, name, -ret);
928c2ecf20Sopenharmony_ci		cdev_del(cdev);
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_cidone:
958c2ecf20Sopenharmony_ci	*devp = device;
968c2ecf20Sopenharmony_ci	return ret;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_civoid hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	struct device *device = *devp;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (device) {
1048c2ecf20Sopenharmony_ci		device_unregister(device);
1058c2ecf20Sopenharmony_ci		*devp = NULL;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		cdev_del(cdev);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic const char *hfi1_class_name = "hfi1";
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciconst char *class_name(void)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	return hfi1_class_name;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic char *hfi1_devnode(struct device *dev, umode_t *mode)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	if (mode)
1218c2ecf20Sopenharmony_ci		*mode = 0600;
1228c2ecf20Sopenharmony_ci	return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic const char *hfi1_class_name_user = "hfi1_user";
1268c2ecf20Sopenharmony_cistatic const char *class_name_user(void)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	return hfi1_class_name_user;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic char *hfi1_user_devnode(struct device *dev, umode_t *mode)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	if (mode)
1348c2ecf20Sopenharmony_ci		*mode = 0666;
1358c2ecf20Sopenharmony_ci	return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciint __init dev_init(void)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	int ret;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	ret = alloc_chrdev_region(&hfi1_dev, 0, HFI1_NMINORS, DRIVER_NAME);
1438c2ecf20Sopenharmony_ci	if (ret < 0) {
1448c2ecf20Sopenharmony_ci		pr_err("Could not allocate chrdev region (err %d)\n", -ret);
1458c2ecf20Sopenharmony_ci		goto done;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	class = class_create(THIS_MODULE, class_name());
1498c2ecf20Sopenharmony_ci	if (IS_ERR(class)) {
1508c2ecf20Sopenharmony_ci		ret = PTR_ERR(class);
1518c2ecf20Sopenharmony_ci		pr_err("Could not create device class (err %d)\n", -ret);
1528c2ecf20Sopenharmony_ci		unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
1538c2ecf20Sopenharmony_ci		goto done;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci	class->devnode = hfi1_devnode;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	user_class = class_create(THIS_MODULE, class_name_user());
1588c2ecf20Sopenharmony_ci	if (IS_ERR(user_class)) {
1598c2ecf20Sopenharmony_ci		ret = PTR_ERR(user_class);
1608c2ecf20Sopenharmony_ci		pr_err("Could not create device class for user accessible files (err %d)\n",
1618c2ecf20Sopenharmony_ci		       -ret);
1628c2ecf20Sopenharmony_ci		class_destroy(class);
1638c2ecf20Sopenharmony_ci		class = NULL;
1648c2ecf20Sopenharmony_ci		user_class = NULL;
1658c2ecf20Sopenharmony_ci		unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
1668c2ecf20Sopenharmony_ci		goto done;
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci	user_class->devnode = hfi1_user_devnode;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cidone:
1718c2ecf20Sopenharmony_ci	return ret;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_civoid dev_cleanup(void)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	class_destroy(class);
1778c2ecf20Sopenharmony_ci	class = NULL;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	class_destroy(user_class);
1808c2ecf20Sopenharmony_ci	user_class = NULL;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	unregister_chrdev_region(hfi1_dev, HFI1_NMINORS);
1838c2ecf20Sopenharmony_ci}
184