cdev.h revision 271127
1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#ifndef _LINUX_CDEV_H_ 31#define _LINUX_CDEV_H_ 32 33#include <linux/kobject.h> 34#include <linux/kdev_t.h> 35#include <linux/list.h> 36 37struct file_operations; 38struct inode; 39struct module; 40 41extern struct cdevsw linuxcdevsw; 42 43struct linux_cdev { 44 struct kobject kobj; 45 struct module *owner; 46 struct cdev *cdev; 47 dev_t dev; 48 const struct file_operations *ops; 49}; 50 51static inline void 52cdev_release(struct kobject *kobj) 53{ 54 struct linux_cdev *cdev; 55 56 cdev = container_of(kobj, struct linux_cdev, kobj); 57 if (cdev->cdev) 58 destroy_dev(cdev->cdev); 59 kfree(cdev); 60} 61 62static inline void 63cdev_static_release(struct kobject *kobj) 64{ 65 struct linux_cdev *cdev; 66 67 cdev = container_of(kobj, struct linux_cdev, kobj); 68 if (cdev->cdev) 69 destroy_dev(cdev->cdev); 70} 71 72static struct kobj_type cdev_ktype = { 73 .release = cdev_release, 74}; 75 76static struct kobj_type cdev_static_ktype = { 77 .release = cdev_static_release, 78}; 79 80static inline void 81cdev_init(struct linux_cdev *cdev, const struct file_operations *ops) 82{ 83 84 kobject_init(&cdev->kobj, &cdev_static_ktype); 85 cdev->ops = ops; 86} 87 88static inline struct linux_cdev * 89cdev_alloc(void) 90{ 91 struct linux_cdev *cdev; 92 93 cdev = kzalloc(sizeof(struct linux_cdev), M_WAITOK); 94 if (cdev) 95 kobject_init(&cdev->kobj, &cdev_ktype); 96 return (cdev); 97} 98 99static inline void 100cdev_put(struct linux_cdev *p) 101{ 102 kobject_put(&p->kobj); 103} 104 105static inline int 106cdev_add(struct linux_cdev *cdev, dev_t dev, unsigned count) 107{ 108 if (count != 1) 109 panic("cdev_add: Unsupported count: %d", count); 110 cdev->cdev = make_dev(&linuxcdevsw, MINOR(dev), 0, 0, 0700, 111 "%s", kobject_name(&cdev->kobj)); 112 cdev->dev = dev; 113 cdev->cdev->si_drv1 = cdev; 114 115 return (0); 116} 117 118static inline void 119cdev_del(struct linux_cdev *cdev) 120{ 121 if (cdev->cdev) { 122 destroy_dev(cdev->cdev); 123 cdev->cdev = NULL; 124 } 125 kobject_put(&cdev->kobj); 126} 127 128#define cdev linux_cdev 129 130#endif /* _LINUX_CDEV_H_ */ 131