blob: a8802577fb55d2657d2fe055ded7804cc610b358 [file] [log] [blame]
Dan Williams45def222015-04-26 19:26:48 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/uaccess.h>
15#include <linux/fcntl.h>
Dan Williamse6dfb2d2015-04-25 03:56:17 -040016#include <linux/async.h>
Dan Williams45def222015-04-26 19:26:48 -040017#include <linux/slab.h>
18#include <linux/fs.h>
19#include <linux/io.h>
20#include "nd-core.h"
21
22static int nvdimm_bus_major;
23static struct class *nd_class;
24
Dan Williamse6dfb2d2015-04-25 03:56:17 -040025struct bus_type nvdimm_bus_type = {
26 .name = "nd",
27};
28
Dan Williams45def222015-04-26 19:26:48 -040029int nvdimm_bus_create_ndctl(struct nvdimm_bus *nvdimm_bus)
30{
31 dev_t devt = MKDEV(nvdimm_bus_major, nvdimm_bus->id);
32 struct device *dev;
33
34 dev = device_create(nd_class, &nvdimm_bus->dev, devt, nvdimm_bus,
35 "ndctl%d", nvdimm_bus->id);
36
37 if (IS_ERR(dev)) {
38 dev_dbg(&nvdimm_bus->dev, "failed to register ndctl%d: %ld\n",
39 nvdimm_bus->id, PTR_ERR(dev));
40 return PTR_ERR(dev);
41 }
42 return 0;
43}
44
45void nvdimm_bus_destroy_ndctl(struct nvdimm_bus *nvdimm_bus)
46{
47 device_destroy(nd_class, MKDEV(nvdimm_bus_major, nvdimm_bus->id));
48}
49
50static long nd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
51{
52 return -ENXIO;
53}
54
55static const struct file_operations nvdimm_bus_fops = {
56 .owner = THIS_MODULE,
57 .open = nonseekable_open,
58 .unlocked_ioctl = nd_ioctl,
59 .compat_ioctl = nd_ioctl,
60 .llseek = noop_llseek,
61};
62
63int __init nvdimm_bus_init(void)
64{
65 int rc;
66
Dan Williamse6dfb2d2015-04-25 03:56:17 -040067 rc = bus_register(&nvdimm_bus_type);
68 if (rc)
69 return rc;
70
Dan Williams45def222015-04-26 19:26:48 -040071 rc = register_chrdev(0, "ndctl", &nvdimm_bus_fops);
72 if (rc < 0)
Dan Williamse6dfb2d2015-04-25 03:56:17 -040073 goto err_chrdev;
Dan Williams45def222015-04-26 19:26:48 -040074 nvdimm_bus_major = rc;
75
76 nd_class = class_create(THIS_MODULE, "nd");
77 if (IS_ERR(nd_class))
78 goto err_class;
79
80 return 0;
81
82 err_class:
83 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -040084 err_chrdev:
85 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -040086
87 return rc;
88}
89
90void __exit nvdimm_bus_exit(void)
91{
92 class_destroy(nd_class);
93 unregister_chrdev(nvdimm_bus_major, "ndctl");
Dan Williamse6dfb2d2015-04-25 03:56:17 -040094 bus_unregister(&nvdimm_bus_type);
Dan Williams45def222015-04-26 19:26:48 -040095}