|MAKEDEV_REF||reference the created device|
|MAKEDEV_NOWAIT||do not sleep, the call may fail|
|MAKEDEV_WAITOK||allow the function to sleep to satisfy malloc|
|MAKEDEV_ETERNAL||created device will be never destroyed|
|MAKEDEV_CHECKNAME||return an error if the device name is invalid or already exists|
Only MAKEDEV_NOWAIT, MAKEDEV_WAITOK and MAKEDEV_CHECKNAME values are accepted for the make_dev_alias_p function.
The MAKEDEV_WAITOK flag is assumed if none of MAKEDEV_WAITOK, MAKEDEV_NOWAIT is specified.
The dev_clone(9) event handler shall specify MAKEDEV_REF flag when creating a device in response to lookup, to avoid race where the device created is destroyed immediately after devfs_lookup(9) drops his reference to cdev.
The MAKEDEV_ETERNAL flag allows the kernel to not acquire some locks when translating system calls into the cdevsw methods calls. It is responsibility of the driver author to make sure that destroy_dev is never called on the returned cdev. For the convenience, use the MAKEDEV_ETERNAL_KLD flag for the code that can be compiled into kernel or loaded (and unloaded) as loadable module.
A panic will occur if the MAKEDEV_CHECKNAME flag is not specified and the device name is invalid or already exists.
The make_dev_p use of the form
struct cdev *dev; int res; res = make_dev_p(flags, &dev, cdevsw, cred, uid, gid, perms, name);is equivalent to the code
struct cdev *dev; struct make_dev_args args; int res;
make_dev_args_init(&args); args.mda_flags = flags; args.mda_devsw = cdevsw; args.mda_cred = cred; args.mda_uid = uid; args.mda_gid = gid; args.mda_mode = perms; res = make_dev_s(&args, &dev, name);
Similarly, the make_dev_credf function call is equivalent to
(void) make_dev_s(&args, &dev, name);In other words, make_dev_credf does not allow the caller to obtain the return value, and in kernels compiled with the INVARIANTS options, the function asserts that the device creation succeeded.
The make_dev_cred function is equivalent to the call
make_dev_credf(0, cdevsw, unit, cr, uid, gid, perms, fmt, ...);
The make_dev function call is the same as
make_dev_credf(0, cdevsw, unit, NULL, uid, gid, perms, fmt, ...);
The make_dev_alias_p function takes the returned cdev from make_dev and makes another (aliased) name for this device. It is an error to call make_dev_alias_p prior to calling make_dev.
The make_dev_alias function is similar to make_dev_alias but it returns the resulting aliasing *cdev and may not return an error.
The cdev returned by make_dev_s and make_dev_alias_p has two fields, si_drv1 and si_drv2, that are available to store state. Both fields are of type void *, and can be initialized simultaneously with the cdev allocation by filling args.mda_si_drv1 and args.mda_si_drv2 members of the make_dev_s argument structure, or filled after the cdev is allocated, if using legacy interfaces. In the latter case, the driver should handle the race of accessing uninitialized si_drv1 and si_drv2 itself. These are designed to replace the unit argument to make_dev, which can be obtained with dev2unit.
The destroy_dev function takes the returned cdev from make_dev and destroys the registration for that device. The notification is sent to devctl(4) about the destruction event. Do not call destroy_dev on devices that were created with make_dev_alias.
The dev_depends function establishes a parent-child relationship between two devices. The net effect is that a destroy_dev of the parent device will also result in the destruction of the child device(s), if any exist. A device may simultaneously be a parent and a child, so it is possible to build a complete hierarchy.
The destroy_dev_sched_cb function schedules execution of the destroy_dev for the specified cdev in the safe context. After destroy_dev is finished, and if the supplied cb is not NULL, the callback cb is called, with argument arg. The destroy_dev_sched function is the same as
destroy_dev_sched_cb(cdev, NULL, NULL);
The d_close driver method cannot call destroy_dev directly. Doing so causes deadlock when destroy_dev waits for all threads to leave the driver methods. Also, because destroy_dev sleeps, no non-sleepable locks may be held over the call. The destroy_dev_sched family of functions overcome these issues.
The device driver may call the destroy_dev_drain function to wait until all devices that have supplied csw as cdevsw, are destroyed. This is useful when driver knows that destroy_dev_sched is called for all instantiated devices, but need to postpone module unload until destroy_dev is actually finished for all of them.
If successful, make_dev_s and make_dev_p will return 0, otherwise they will return an error. If successful, make_dev_credf will return a valid cdev pointer, otherwise it will return NULL.
The make_dev_s, make_dev_p and make_dev_alias_p calls will fail and the device will be not registered if:
[ENOMEM] The MAKEDEV_NOWAIT flag was specified and a memory allocation request could not be satisfied. [ENAMETOOLONG] The MAKEDEV_CHECKNAME flag was specified and the provided device name is longer than SPECNAMELEN. [EINVAL] The MAKEDEV_CHECKNAME flag was specified and the provided device name is empty, contains a "." or ".." path component or ends with /. [EINVAL] The MAKEDEV_CHECKNAME flag was specified and the provided device name contains invalid characters. [EEXIST] The MAKEDEV_CHECKNAME flag was specified and the provided device name already exists.
devctl(4), devfs(5), destroy_dev_drain(9), dev_clone(9)
The make_dev and destroy_dev functions first appeared in
.Fx 4.0 . The function make_dev_alias first appeared in
.Fx 4.1 . The function dev_depends first appeared in
.Fx 5.0 . The functions make_dev_credf, destroy_dev_sched, destroy_dev_sched_cb first appeared in
.Fx 7.0 . The function make_dev_p first appeared in
.Fx 8.2 . The function make_dev_s first appeared in
.Fx 11.0 .