]>
git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-module.c
2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/sysmacros.h>
28 #include <sys/sunddi.h>
30 #ifdef DEBUG_SUBSYSTEM
31 #undef DEBUG_SUBSYSTEM
34 #define DEBUG_SUBSYSTEM S_MODULE
36 static spinlock_t dev_info_lock
= SPIN_LOCK_UNLOCKED
;
37 static LIST_HEAD(dev_info_list
);
39 static struct dev_info
*
40 get_dev_info(dev_t dev
)
44 spin_lock(&dev_info_lock
);
46 list_for_each_entry(di
, &dev_info_list
, di_list
)
47 if (di
->di_dev
== dev
)
52 spin_unlock(&dev_info_lock
);
57 mod_generic_ioctl(struct inode
*ino
, struct file
*filp
,
58 unsigned int cmd
, unsigned long arg
)
61 int rc
, flags
= 0, rvalp
= 0;
64 di
= get_dev_info(MKDEV(imajor(ino
), iminor(ino
)));
68 rc
= di
->di_ops
->devo_cb_ops
->cb_ioctl(di
->di_dev
,
69 (int)cmd
,(intptr_t)arg
,
75 __ddi_create_minor_node(dev_info_t
*di
, char *name
, int spec_type
,
76 minor_t minor_num
, char *node_type
,
77 int flags
, struct module
*mod
)
80 struct dev_ops
*dev_ops
;
81 struct cb_ops
*cb_ops
;
82 struct file_operations
*fops
;
86 ASSERT(spec_type
== S_IFCHR
);
87 ASSERT(minor_num
< di
->di_minors
);
88 ASSERT(!strcmp(node_type
, DDI_PSEUDO
));
90 fops
= kzalloc(sizeof(struct file_operations
), GFP_KERNEL
);
102 mutex_enter(&di
->di_lock
);
103 dev_ops
= di
->di_ops
;
105 cb_ops
= di
->di_ops
->devo_cb_ops
;
108 /* Setup the fops to cb_ops mapping */
110 if (cb_ops
->cb_ioctl
)
111 fops
->ioctl
= mod_generic_ioctl
;
115 fops
->open
= mod_generic_open
;
117 if (cb_ops
->cb_close
)
118 fops
->release
= mod_generic_close
;
121 fops
->read
= mod_generic_read
;
123 if (cb_ops
->cb_write
)
124 fops
->write
= mod_generic_write
;
126 /* XXX: Currently unsupported operations */
127 ASSERT(cb_ops
->cb_open
== NULL
);
128 ASSERT(cb_ops
->cb_close
== NULL
);
129 ASSERT(cb_ops
->cb_read
== NULL
);
130 ASSERT(cb_ops
->cb_write
== NULL
);
131 ASSERT(cb_ops
->cb_strategy
== NULL
);
132 ASSERT(cb_ops
->cb_print
== NULL
);
133 ASSERT(cb_ops
->cb_dump
== NULL
);
134 ASSERT(cb_ops
->cb_devmap
== NULL
);
135 ASSERT(cb_ops
->cb_mmap
== NULL
);
136 ASSERT(cb_ops
->cb_segmap
== NULL
);
137 ASSERT(cb_ops
->cb_chpoll
== NULL
);
138 ASSERT(cb_ops
->cb_prop_op
== NULL
);
139 ASSERT(cb_ops
->cb_str
== NULL
);
140 ASSERT(cb_ops
->cb_aread
== NULL
);
141 ASSERT(cb_ops
->cb_awrite
== NULL
);
144 di
->di_flags
= flags
;
145 di
->di_minor
= minor_num
;
146 di
->di_dev
= MKDEV(di
->di_major
, di
->di_minor
);
148 rc
= cdev_add(cdev
, di
->di_dev
, 1);
150 CERROR("Error adding cdev, %d\n", rc
);
153 mutex_exit(&di
->di_lock
);
157 spin_lock(&dev_info_lock
);
158 list_add(&di
->di_list
, &dev_info_list
);
159 spin_unlock(&dev_info_lock
);
161 mutex_exit(&di
->di_lock
);
165 EXPORT_SYMBOL(__ddi_create_minor_node
);
168 __ddi_remove_minor_node_locked(dev_info_t
*di
, char *name
)
171 cdev_del(di
->di_cdev
);
175 spin_lock(&dev_info_lock
);
176 list_del_init(&di
->di_list
);
177 spin_unlock(&dev_info_lock
);
181 __ddi_remove_minor_node(dev_info_t
*di
, char *name
)
184 mutex_enter(&di
->di_lock
);
185 __ddi_remove_minor_node_locked(di
, name
);
186 mutex_exit(&di
->di_lock
);
189 EXPORT_SYMBOL(__ddi_remove_minor_node
);
192 ddi_quiesce_not_needed(dev_info_t
*dip
)
196 EXPORT_SYMBOL(ddi_quiesce_not_needed
);
200 mod_generic_open(struct inode
*, struct file
*)
202 open(dev_t
*devp
, int flags
, int otyp
, cred_t
*credp
);
206 mod_generic_close(struct inode
*, struct file
*)
208 close(dev_t dev
, int flags
, int otyp
, cred_t
*credp
);
212 mod_generic_read(struct file
*, char __user
*, size_t, loff_t
*)
214 read(dev_t dev
, struct uio
*uiop
, cred_t
*credp
);
218 mod_generic_write(struct file
*, const char __user
*, size_t, loff_t
*)
220 write(dev_t dev
, struct uio
*uiop
, cred_t
*credp
);
224 static struct dev_info
*
225 dev_info_alloc(major_t major
, minor_t minors
, struct dev_ops
*ops
) {
228 di
= kmalloc(sizeof(struct dev_info
), GFP_KERNEL
);
232 mutex_init(&di
->di_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
233 INIT_LIST_HEAD(&di
->di_list
);
237 di
->di_major
= major
;
239 di
->di_minors
= minors
;
246 dev_info_free(struct dev_info
*di
)
248 mutex_enter(&di
->di_lock
);
249 __ddi_remove_minor_node_locked(di
, NULL
);
250 mutex_exit(&di
->di_lock
);
251 mutex_destroy(&di
->di_lock
);
256 __mod_install(struct modlinkage
*modlp
)
258 struct modldrv
*drv
= modlp
->ml_modldrv
;
263 di
= dev_info_alloc(modlp
->ml_major
, modlp
->ml_minors
,
268 /* XXX: Really we need to be calling devo_probe if it's available
269 * and then calling devo_attach for each device discovered. However
270 * for now we just call it once and let the app sort it out.
272 rc
= drv
->drv_dev_ops
->devo_attach(di
, DDI_ATTACH
);
273 if (rc
!= DDI_SUCCESS
) {
278 drv
->drv_dev_info
= di
;
282 EXPORT_SYMBOL(__mod_install
);
285 __mod_remove(struct modlinkage
*modlp
)
287 struct modldrv
*drv
= modlp
->ml_modldrv
;
288 struct dev_info
*di
= drv
->drv_dev_info
;
292 rc
= drv
->drv_dev_ops
->devo_detach(di
, DDI_DETACH
);
293 if (rc
!= DDI_SUCCESS
)
297 drv
->drv_dev_info
= NULL
;
301 EXPORT_SYMBOL(__mod_remove
);
304 ldi_ident_from_mod(struct modlinkage
*modlp
, ldi_ident_t
*lip
)
312 li
= kmalloc(sizeof(struct ldi_ident
), GFP_KERNEL
);
316 li
->li_dev
= MKDEV(modlp
->ml_major
, 0);
321 EXPORT_SYMBOL(ldi_ident_from_mod
);
324 ldi_ident_release(ldi_ident_t lip
)
331 EXPORT_SYMBOL(ldi_ident_release
);