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