xref: /freebsd/sys/compat/linuxkpi/common/include/linux/miscdevice.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
18d59ecb2SHans Petter Selasky /*-
28d59ecb2SHans Petter Selasky  * Copyright (c) 2010 Isilon Systems, Inc.
38d59ecb2SHans Petter Selasky  * Copyright (c) 2010 iX Systems, Inc.
48d59ecb2SHans Petter Selasky  * Copyright (c) 2010 Panasas, Inc.
58d59ecb2SHans Petter Selasky  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky  * All rights reserved.
78d59ecb2SHans Petter Selasky  *
88d59ecb2SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
98d59ecb2SHans Petter Selasky  * modification, are permitted provided that the following conditions
108d59ecb2SHans Petter Selasky  * are met:
118d59ecb2SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
128d59ecb2SHans Petter Selasky  *    notice unmodified, this list of conditions, and the following
138d59ecb2SHans Petter Selasky  *    disclaimer.
148d59ecb2SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
158d59ecb2SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
168d59ecb2SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
178d59ecb2SHans Petter Selasky  *
188d59ecb2SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198d59ecb2SHans Petter Selasky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208d59ecb2SHans Petter Selasky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218d59ecb2SHans Petter Selasky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228d59ecb2SHans Petter Selasky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238d59ecb2SHans Petter Selasky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248d59ecb2SHans Petter Selasky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258d59ecb2SHans Petter Selasky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268d59ecb2SHans Petter Selasky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278d59ecb2SHans Petter Selasky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288d59ecb2SHans Petter Selasky  */
29*307f78f3SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_MISCDEVICE_H_
30*307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_MISCDEVICE_H_
318d59ecb2SHans Petter Selasky 
328d59ecb2SHans Petter Selasky #define	MISC_DYNAMIC_MINOR	-1
338d59ecb2SHans Petter Selasky 
348d59ecb2SHans Petter Selasky #include <linux/device.h>
358d59ecb2SHans Petter Selasky #include <linux/cdev.h>
368d59ecb2SHans Petter Selasky 
378d59ecb2SHans Petter Selasky struct miscdevice  {
388d59ecb2SHans Petter Selasky 	const char	*name;
398d59ecb2SHans Petter Selasky 	struct device	*this_device;
408d59ecb2SHans Petter Selasky 	const struct file_operations *fops;
418d59ecb2SHans Petter Selasky 	struct cdev	*cdev;
428d59ecb2SHans Petter Selasky 	int		minor;
438d59ecb2SHans Petter Selasky 	const char *nodename;
448d59ecb2SHans Petter Selasky 	umode_t mode;
458d59ecb2SHans Petter Selasky };
468d59ecb2SHans Petter Selasky 
4706204f8eSHans Petter Selasky extern struct class linux_class_misc;
488d59ecb2SHans Petter Selasky 
498d59ecb2SHans Petter Selasky static inline int
misc_register(struct miscdevice * misc)508d59ecb2SHans Petter Selasky misc_register(struct miscdevice *misc)
518d59ecb2SHans Petter Selasky {
5206204f8eSHans Petter Selasky 	misc->this_device = device_create(&linux_class_misc,
5306204f8eSHans Petter Selasky 	    &linux_root_device, 0, misc, misc->name);
548d59ecb2SHans Petter Selasky 	misc->cdev = cdev_alloc();
558d59ecb2SHans Petter Selasky 	if (misc->cdev == NULL)
568d59ecb2SHans Petter Selasky 		return -ENOMEM;
578d59ecb2SHans Petter Selasky 	misc->cdev->owner = THIS_MODULE;
588d59ecb2SHans Petter Selasky 	misc->cdev->ops = misc->fops;
598d59ecb2SHans Petter Selasky 	kobject_set_name(&misc->cdev->kobj, misc->name);
608d59ecb2SHans Petter Selasky 	if (cdev_add(misc->cdev, misc->this_device->devt, 1))
618d59ecb2SHans Petter Selasky 		return -EINVAL;
628d59ecb2SHans Petter Selasky 	return (0);
638d59ecb2SHans Petter Selasky }
648d59ecb2SHans Petter Selasky 
658d59ecb2SHans Petter Selasky static inline int
misc_deregister(struct miscdevice * misc)668d59ecb2SHans Petter Selasky misc_deregister(struct miscdevice *misc)
678d59ecb2SHans Petter Selasky {
6806204f8eSHans Petter Selasky 	device_destroy(&linux_class_misc, misc->this_device->devt);
698d59ecb2SHans Petter Selasky 	cdev_del(misc->cdev);
708d59ecb2SHans Petter Selasky 
718d59ecb2SHans Petter Selasky 	return (0);
728d59ecb2SHans Petter Selasky }
738d59ecb2SHans Petter Selasky 
74*307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_MISCDEVICE_H_ */
75