164de8019SJohn Baldwin /*-
264de8019SJohn Baldwin * Copyright (c) 2014 John Baldwin <[email protected]>
364de8019SJohn Baldwin *
464de8019SJohn Baldwin * Redistribution and use in source and binary forms, with or without
564de8019SJohn Baldwin * modification, are permitted provided that the following conditions
664de8019SJohn Baldwin * are met:
764de8019SJohn Baldwin * 1. Redistributions of source code must retain the above copyright
864de8019SJohn Baldwin * notice, this list of conditions and the following disclaimer.
964de8019SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright
1064de8019SJohn Baldwin * notice, this list of conditions and the following disclaimer in the
1164de8019SJohn Baldwin * documentation and/or other materials provided with the distribution.
1264de8019SJohn Baldwin *
1364de8019SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1464de8019SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1564de8019SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1664de8019SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1764de8019SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1864de8019SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1964de8019SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2064de8019SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2164de8019SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2264de8019SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2364de8019SJohn Baldwin * SUCH DAMAGE.
2464de8019SJohn Baldwin */
2564de8019SJohn Baldwin
2664de8019SJohn Baldwin #include <sys/cdefs.h>
2764de8019SJohn Baldwin __FBSDID("$FreeBSD$");
2864de8019SJohn Baldwin
2964de8019SJohn Baldwin #include <sys/types.h>
3064de8019SJohn Baldwin #include <sys/bus.h>
3164de8019SJohn Baldwin #include <errno.h>
3264de8019SJohn Baldwin #include <fcntl.h>
3364de8019SJohn Baldwin #include <string.h>
3464de8019SJohn Baldwin #include "devctl.h"
3564de8019SJohn Baldwin
3664de8019SJohn Baldwin static int
devctl_request(u_long cmd,struct devreq * req)3764de8019SJohn Baldwin devctl_request(u_long cmd, struct devreq *req)
3864de8019SJohn Baldwin {
3964de8019SJohn Baldwin static int devctl2_fd = -1;
4064de8019SJohn Baldwin
4164de8019SJohn Baldwin if (devctl2_fd == -1) {
4264de8019SJohn Baldwin devctl2_fd = open("/dev/devctl2", O_RDONLY);
4364de8019SJohn Baldwin if (devctl2_fd == -1)
4464de8019SJohn Baldwin return (-1);
4564de8019SJohn Baldwin }
4664de8019SJohn Baldwin return (ioctl(devctl2_fd, cmd, req));
4764de8019SJohn Baldwin }
4864de8019SJohn Baldwin
4964de8019SJohn Baldwin static int
devctl_simple_request(u_long cmd,const char * name,int flags)5064de8019SJohn Baldwin devctl_simple_request(u_long cmd, const char *name, int flags)
5164de8019SJohn Baldwin {
5264de8019SJohn Baldwin struct devreq req;
5364de8019SJohn Baldwin
5464de8019SJohn Baldwin memset(&req, 0, sizeof(req));
5564de8019SJohn Baldwin if (strlcpy(req.dr_name, name, sizeof(req.dr_name)) >=
5664de8019SJohn Baldwin sizeof(req.dr_name)) {
5764de8019SJohn Baldwin errno = EINVAL;
5864de8019SJohn Baldwin return (-1);
5964de8019SJohn Baldwin }
6064de8019SJohn Baldwin req.dr_flags = flags;
6164de8019SJohn Baldwin return (devctl_request(cmd, &req));
6264de8019SJohn Baldwin }
6364de8019SJohn Baldwin
6464de8019SJohn Baldwin int
devctl_attach(const char * device)6564de8019SJohn Baldwin devctl_attach(const char *device)
6664de8019SJohn Baldwin {
6764de8019SJohn Baldwin
6864de8019SJohn Baldwin return (devctl_simple_request(DEV_ATTACH, device, 0));
6964de8019SJohn Baldwin }
7064de8019SJohn Baldwin
7164de8019SJohn Baldwin int
devctl_detach(const char * device,bool force)7264de8019SJohn Baldwin devctl_detach(const char *device, bool force)
7364de8019SJohn Baldwin {
7464de8019SJohn Baldwin
7564de8019SJohn Baldwin return (devctl_simple_request(DEV_DETACH, device, force ?
7664de8019SJohn Baldwin DEVF_FORCE_DETACH : 0));
7764de8019SJohn Baldwin }
7864de8019SJohn Baldwin
7964de8019SJohn Baldwin int
devctl_enable(const char * device)8064de8019SJohn Baldwin devctl_enable(const char *device)
8164de8019SJohn Baldwin {
8264de8019SJohn Baldwin
8364de8019SJohn Baldwin return (devctl_simple_request(DEV_ENABLE, device, 0));
8464de8019SJohn Baldwin }
8564de8019SJohn Baldwin
8664de8019SJohn Baldwin int
devctl_disable(const char * device,bool force_detach)8764de8019SJohn Baldwin devctl_disable(const char *device, bool force_detach)
8864de8019SJohn Baldwin {
8964de8019SJohn Baldwin
9064de8019SJohn Baldwin return (devctl_simple_request(DEV_DISABLE, device, force_detach ?
9164de8019SJohn Baldwin DEVF_FORCE_DETACH : 0));
9264de8019SJohn Baldwin }
9364de8019SJohn Baldwin
9464de8019SJohn Baldwin int
devctl_suspend(const char * device)9564de8019SJohn Baldwin devctl_suspend(const char *device)
9664de8019SJohn Baldwin {
9764de8019SJohn Baldwin
9864de8019SJohn Baldwin return (devctl_simple_request(DEV_SUSPEND, device, 0));
9964de8019SJohn Baldwin }
10064de8019SJohn Baldwin
10164de8019SJohn Baldwin int
devctl_resume(const char * device)10264de8019SJohn Baldwin devctl_resume(const char *device)
10364de8019SJohn Baldwin {
10464de8019SJohn Baldwin
10564de8019SJohn Baldwin return (devctl_simple_request(DEV_RESUME, device, 0));
10664de8019SJohn Baldwin }
10764de8019SJohn Baldwin
10864de8019SJohn Baldwin int
devctl_set_driver(const char * device,const char * driver,bool force)10964de8019SJohn Baldwin devctl_set_driver(const char *device, const char *driver, bool force)
11064de8019SJohn Baldwin {
11164de8019SJohn Baldwin struct devreq req;
11264de8019SJohn Baldwin
11364de8019SJohn Baldwin memset(&req, 0, sizeof(req));
11464de8019SJohn Baldwin if (strlcpy(req.dr_name, device, sizeof(req.dr_name)) >=
11564de8019SJohn Baldwin sizeof(req.dr_name)) {
11664de8019SJohn Baldwin errno = EINVAL;
11764de8019SJohn Baldwin return (-1);
11864de8019SJohn Baldwin }
11964de8019SJohn Baldwin req.dr_data = __DECONST(char *, driver);
12064de8019SJohn Baldwin if (force)
12164de8019SJohn Baldwin req.dr_flags |= DEVF_SET_DRIVER_DETACH;
12264de8019SJohn Baldwin return (devctl_request(DEV_SET_DRIVER, &req));
12364de8019SJohn Baldwin }
124a907c691SJohn Baldwin
125a907c691SJohn Baldwin int
devctl_clear_driver(const char * device,bool force)126e05ec081SJohn Baldwin devctl_clear_driver(const char *device, bool force)
127e05ec081SJohn Baldwin {
128e05ec081SJohn Baldwin
129e05ec081SJohn Baldwin return (devctl_simple_request(DEV_CLEAR_DRIVER, device, force ?
130e05ec081SJohn Baldwin DEVF_CLEAR_DRIVER_DETACH : 0));
131e05ec081SJohn Baldwin }
132e05ec081SJohn Baldwin
133e05ec081SJohn Baldwin int
devctl_rescan(const char * device)134a907c691SJohn Baldwin devctl_rescan(const char *device)
135a907c691SJohn Baldwin {
136a907c691SJohn Baldwin
137a907c691SJohn Baldwin return (devctl_simple_request(DEV_RESCAN, device, 0));
138a907c691SJohn Baldwin }
13988eb5c50SJohn Baldwin
14088eb5c50SJohn Baldwin int
devctl_delete(const char * device,bool force)14188eb5c50SJohn Baldwin devctl_delete(const char *device, bool force)
14288eb5c50SJohn Baldwin {
14388eb5c50SJohn Baldwin
14488eb5c50SJohn Baldwin return (devctl_simple_request(DEV_DELETE, device, force ?
14588eb5c50SJohn Baldwin DEVF_FORCE_DELETE : 0));
14688eb5c50SJohn Baldwin }
1475fa29797SWarner Losh
1485fa29797SWarner Losh int
devctl_freeze(void)1495fa29797SWarner Losh devctl_freeze(void)
1505fa29797SWarner Losh {
1515fa29797SWarner Losh
1525fa29797SWarner Losh return (devctl_simple_request(DEV_FREEZE, "", 0));
1535fa29797SWarner Losh }
1545fa29797SWarner Losh
1555fa29797SWarner Losh int
devctl_thaw(void)1565fa29797SWarner Losh devctl_thaw(void)
1575fa29797SWarner Losh {
1585fa29797SWarner Losh
1595fa29797SWarner Losh return (devctl_simple_request(DEV_THAW, "", 0));
1605fa29797SWarner Losh }
161*4fbf8e1cSKonstantin Belousov
162*4fbf8e1cSKonstantin Belousov int
devctl_reset(const char * device,bool detach)163*4fbf8e1cSKonstantin Belousov devctl_reset(const char *device, bool detach)
164*4fbf8e1cSKonstantin Belousov {
165*4fbf8e1cSKonstantin Belousov
166*4fbf8e1cSKonstantin Belousov return (devctl_simple_request(DEV_RESET, device, detach ?
167*4fbf8e1cSKonstantin Belousov DEVF_RESET_DETACH : 0));
168*4fbf8e1cSKonstantin Belousov }
169