xref: /src/sys/compat/linuxkpi/common/include/linux/miscdevice.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1aa0a1e58SJeff Roberson /*-
2aa0a1e58SJeff Roberson  * Copyright (c) 2010 Isilon Systems, Inc.
3aa0a1e58SJeff Roberson  * Copyright (c) 2010 iX Systems, Inc.
4aa0a1e58SJeff Roberson  * Copyright (c) 2010 Panasas, Inc.
5c7818b48SHans Petter Selasky  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6aa0a1e58SJeff Roberson  * All rights reserved.
7aa0a1e58SJeff Roberson  *
8aa0a1e58SJeff Roberson  * Redistribution and use in source and binary forms, with or without
9aa0a1e58SJeff Roberson  * modification, are permitted provided that the following conditions
10aa0a1e58SJeff Roberson  * are met:
11aa0a1e58SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
12aa0a1e58SJeff Roberson  *    notice unmodified, this list of conditions, and the following
13aa0a1e58SJeff Roberson  *    disclaimer.
14aa0a1e58SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
15aa0a1e58SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
16aa0a1e58SJeff Roberson  *    documentation and/or other materials provided with the distribution.
17aa0a1e58SJeff Roberson  *
18aa0a1e58SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19aa0a1e58SJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20aa0a1e58SJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21aa0a1e58SJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22aa0a1e58SJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23aa0a1e58SJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24aa0a1e58SJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25aa0a1e58SJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26aa0a1e58SJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27aa0a1e58SJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28aa0a1e58SJeff Roberson  */
29307f78f3SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_MISCDEVICE_H_
30307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_MISCDEVICE_H_
31aa0a1e58SJeff Roberson 
32aa0a1e58SJeff Roberson #define	MISC_DYNAMIC_MINOR	-1
33aa0a1e58SJeff Roberson 
34aa0a1e58SJeff Roberson #include <linux/device.h>
35aa0a1e58SJeff Roberson #include <linux/cdev.h>
36aa0a1e58SJeff Roberson 
37aa0a1e58SJeff Roberson struct miscdevice  {
38aa0a1e58SJeff Roberson 	const char	*name;
39aa0a1e58SJeff Roberson 	struct device	*this_device;
40aa0a1e58SJeff Roberson 	const struct file_operations *fops;
41aa0a1e58SJeff Roberson 	struct cdev	*cdev;
42aa0a1e58SJeff Roberson 	int		minor;
43c7818b48SHans Petter Selasky 	const char *nodename;
44c7818b48SHans Petter Selasky 	umode_t mode;
45aa0a1e58SJeff Roberson };
46aa0a1e58SJeff Roberson 
4706204f8eSHans Petter Selasky extern struct class linux_class_misc;
48aa0a1e58SJeff Roberson 
49aa0a1e58SJeff Roberson static inline int
misc_register(struct miscdevice * misc)50aa0a1e58SJeff Roberson misc_register(struct miscdevice *misc)
51aa0a1e58SJeff Roberson {
5206204f8eSHans Petter Selasky 	misc->this_device = device_create(&linux_class_misc,
5306204f8eSHans Petter Selasky 	    &linux_root_device, 0, misc, misc->name);
54aa0a1e58SJeff Roberson 	misc->cdev = cdev_alloc();
55aa0a1e58SJeff Roberson 	if (misc->cdev == NULL)
56aa0a1e58SJeff Roberson 		return -ENOMEM;
57aa0a1e58SJeff Roberson 	misc->cdev->owner = THIS_MODULE;
58aa0a1e58SJeff Roberson 	misc->cdev->ops = misc->fops;
59aa0a1e58SJeff Roberson 	kobject_set_name(&misc->cdev->kobj, misc->name);
60aa0a1e58SJeff Roberson 	if (cdev_add(misc->cdev, misc->this_device->devt, 1))
61aa0a1e58SJeff Roberson 		return -EINVAL;
62aa0a1e58SJeff Roberson 	return (0);
63aa0a1e58SJeff Roberson }
64aa0a1e58SJeff Roberson 
65aa0a1e58SJeff Roberson static inline int
misc_deregister(struct miscdevice * misc)66aa0a1e58SJeff Roberson misc_deregister(struct miscdevice *misc)
67aa0a1e58SJeff Roberson {
6806204f8eSHans Petter Selasky 	device_destroy(&linux_class_misc, misc->this_device->devt);
69aa0a1e58SJeff Roberson 	cdev_del(misc->cdev);
70aa0a1e58SJeff Roberson 
71aa0a1e58SJeff Roberson 	return (0);
72aa0a1e58SJeff Roberson }
73aa0a1e58SJeff Roberson 
74307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_MISCDEVICE_H_ */
75