xref: /freebsd-src/lib/libdevctl/devctl.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
164de8019SJohn Baldwin /*-
264de8019SJohn Baldwin  * Copyright (c) 2014 John Baldwin <jhb@FreeBSD.org>
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/types.h>
2764de8019SJohn Baldwin #include <sys/bus.h>
2864de8019SJohn Baldwin #include <errno.h>
2964de8019SJohn Baldwin #include <fcntl.h>
30*b01f409fSWarner Losh #include <stdlib.h>
3164de8019SJohn Baldwin #include <string.h>
3264de8019SJohn Baldwin #include "devctl.h"
3364de8019SJohn Baldwin 
3464de8019SJohn Baldwin static int
devctl_request(u_long cmd,struct devreq * req)3564de8019SJohn Baldwin devctl_request(u_long cmd, struct devreq *req)
3664de8019SJohn Baldwin {
3764de8019SJohn Baldwin 	static int devctl2_fd = -1;
3864de8019SJohn Baldwin 
3964de8019SJohn Baldwin 	if (devctl2_fd == -1) {
4064de8019SJohn Baldwin 		devctl2_fd = open("/dev/devctl2", O_RDONLY);
4164de8019SJohn Baldwin 		if (devctl2_fd == -1)
4264de8019SJohn Baldwin 			return (-1);
4364de8019SJohn Baldwin 	}
4464de8019SJohn Baldwin 	return (ioctl(devctl2_fd, cmd, req));
4564de8019SJohn Baldwin }
4664de8019SJohn Baldwin 
4764de8019SJohn Baldwin static int
devctl_simple_request(u_long cmd,const char * name,int flags)4864de8019SJohn Baldwin devctl_simple_request(u_long cmd, const char *name, int flags)
4964de8019SJohn Baldwin {
5064de8019SJohn Baldwin 	struct devreq req;
5164de8019SJohn Baldwin 
5264de8019SJohn Baldwin 	memset(&req, 0, sizeof(req));
5364de8019SJohn Baldwin 	if (strlcpy(req.dr_name, name, sizeof(req.dr_name)) >=
5464de8019SJohn Baldwin 	    sizeof(req.dr_name)) {
5564de8019SJohn Baldwin 		errno = EINVAL;
5664de8019SJohn Baldwin 		return (-1);
5764de8019SJohn Baldwin 	}
5864de8019SJohn Baldwin 	req.dr_flags = flags;
5964de8019SJohn Baldwin 	return (devctl_request(cmd, &req));
6064de8019SJohn Baldwin }
6164de8019SJohn Baldwin 
6264de8019SJohn Baldwin int
devctl_attach(const char * device)6364de8019SJohn Baldwin devctl_attach(const char *device)
6464de8019SJohn Baldwin {
6564de8019SJohn Baldwin 
6664de8019SJohn Baldwin 	return (devctl_simple_request(DEV_ATTACH, device, 0));
6764de8019SJohn Baldwin }
6864de8019SJohn Baldwin 
6964de8019SJohn Baldwin int
devctl_detach(const char * device,bool force)7064de8019SJohn Baldwin devctl_detach(const char *device, bool force)
7164de8019SJohn Baldwin {
7264de8019SJohn Baldwin 
7364de8019SJohn Baldwin 	return (devctl_simple_request(DEV_DETACH, device, force ?
7464de8019SJohn Baldwin 	    DEVF_FORCE_DETACH : 0));
7564de8019SJohn Baldwin }
7664de8019SJohn Baldwin 
7764de8019SJohn Baldwin int
devctl_enable(const char * device)7864de8019SJohn Baldwin devctl_enable(const char *device)
7964de8019SJohn Baldwin {
8064de8019SJohn Baldwin 
8164de8019SJohn Baldwin 	return (devctl_simple_request(DEV_ENABLE, device, 0));
8264de8019SJohn Baldwin }
8364de8019SJohn Baldwin 
8464de8019SJohn Baldwin int
devctl_disable(const char * device,bool force_detach)8564de8019SJohn Baldwin devctl_disable(const char *device, bool force_detach)
8664de8019SJohn Baldwin {
8764de8019SJohn Baldwin 
8864de8019SJohn Baldwin 	return (devctl_simple_request(DEV_DISABLE, device, force_detach ?
8964de8019SJohn Baldwin 	    DEVF_FORCE_DETACH : 0));
9064de8019SJohn Baldwin }
9164de8019SJohn Baldwin 
9264de8019SJohn Baldwin int
devctl_suspend(const char * device)9364de8019SJohn Baldwin devctl_suspend(const char *device)
9464de8019SJohn Baldwin {
9564de8019SJohn Baldwin 
9664de8019SJohn Baldwin 	return (devctl_simple_request(DEV_SUSPEND, device, 0));
9764de8019SJohn Baldwin }
9864de8019SJohn Baldwin 
9964de8019SJohn Baldwin int
devctl_resume(const char * device)10064de8019SJohn Baldwin devctl_resume(const char *device)
10164de8019SJohn Baldwin {
10264de8019SJohn Baldwin 
10364de8019SJohn Baldwin 	return (devctl_simple_request(DEV_RESUME, device, 0));
10464de8019SJohn Baldwin }
10564de8019SJohn Baldwin 
10664de8019SJohn Baldwin int
devctl_set_driver(const char * device,const char * driver,bool force)10764de8019SJohn Baldwin devctl_set_driver(const char *device, const char *driver, bool force)
10864de8019SJohn Baldwin {
10964de8019SJohn Baldwin 	struct devreq req;
11064de8019SJohn Baldwin 
11164de8019SJohn Baldwin 	memset(&req, 0, sizeof(req));
11264de8019SJohn Baldwin 	if (strlcpy(req.dr_name, device, sizeof(req.dr_name)) >=
11364de8019SJohn Baldwin 	    sizeof(req.dr_name)) {
11464de8019SJohn Baldwin 		errno = EINVAL;
11564de8019SJohn Baldwin 		return (-1);
11664de8019SJohn Baldwin 	}
11764de8019SJohn Baldwin 	req.dr_data = __DECONST(char *, driver);
11864de8019SJohn Baldwin 	if (force)
11964de8019SJohn Baldwin 		req.dr_flags |= DEVF_SET_DRIVER_DETACH;
12064de8019SJohn Baldwin 	return (devctl_request(DEV_SET_DRIVER, &req));
12164de8019SJohn Baldwin }
122a907c691SJohn Baldwin 
123a907c691SJohn Baldwin int
devctl_clear_driver(const char * device,bool force)124e05ec081SJohn Baldwin devctl_clear_driver(const char *device, bool force)
125e05ec081SJohn Baldwin {
126e05ec081SJohn Baldwin 
127e05ec081SJohn Baldwin 	return (devctl_simple_request(DEV_CLEAR_DRIVER, device, force ?
128e05ec081SJohn Baldwin 	    DEVF_CLEAR_DRIVER_DETACH : 0));
129e05ec081SJohn Baldwin }
130e05ec081SJohn Baldwin 
131e05ec081SJohn Baldwin int
devctl_rescan(const char * device)132a907c691SJohn Baldwin devctl_rescan(const char *device)
133a907c691SJohn Baldwin {
134a907c691SJohn Baldwin 
135a907c691SJohn Baldwin 	return (devctl_simple_request(DEV_RESCAN, device, 0));
136a907c691SJohn Baldwin }
13788eb5c50SJohn Baldwin 
13888eb5c50SJohn Baldwin int
devctl_delete(const char * device,bool force)13988eb5c50SJohn Baldwin devctl_delete(const char *device, bool force)
14088eb5c50SJohn Baldwin {
14188eb5c50SJohn Baldwin 
14288eb5c50SJohn Baldwin 	return (devctl_simple_request(DEV_DELETE, device, force ?
14388eb5c50SJohn Baldwin 	    DEVF_FORCE_DELETE : 0));
14488eb5c50SJohn Baldwin }
1455fa29797SWarner Losh 
1465fa29797SWarner Losh int
devctl_freeze(void)1475fa29797SWarner Losh devctl_freeze(void)
1485fa29797SWarner Losh {
1495fa29797SWarner Losh 
1505fa29797SWarner Losh 	return (devctl_simple_request(DEV_FREEZE, "", 0));
1515fa29797SWarner Losh }
1525fa29797SWarner Losh 
1535fa29797SWarner Losh int
devctl_thaw(void)1545fa29797SWarner Losh devctl_thaw(void)
1555fa29797SWarner Losh {
1565fa29797SWarner Losh 
1575fa29797SWarner Losh 	return (devctl_simple_request(DEV_THAW, "", 0));
1585fa29797SWarner Losh }
1594fbf8e1cSKonstantin Belousov 
1604fbf8e1cSKonstantin Belousov int
devctl_reset(const char * device,bool detach)1614fbf8e1cSKonstantin Belousov devctl_reset(const char *device, bool detach)
1624fbf8e1cSKonstantin Belousov {
1634fbf8e1cSKonstantin Belousov 
1644fbf8e1cSKonstantin Belousov 	return (devctl_simple_request(DEV_RESET, device, detach ?
1654fbf8e1cSKonstantin Belousov 	    DEVF_RESET_DETACH : 0));
1664fbf8e1cSKonstantin Belousov }
167*b01f409fSWarner Losh 
168*b01f409fSWarner Losh #define BUFLEN 1024
169*b01f409fSWarner Losh 
170*b01f409fSWarner Losh int
devctl_getpath(const char * device,const char * locator,char ** buffer)171*b01f409fSWarner Losh devctl_getpath(const char *device, const char *locator, char **buffer)
172*b01f409fSWarner Losh {
173*b01f409fSWarner Losh 	struct devreq req;
174*b01f409fSWarner Losh 	int serrno;
175*b01f409fSWarner Losh 
176*b01f409fSWarner Losh 	memset(&req, 0, sizeof(req));
177*b01f409fSWarner Losh 	if (strlcpy(req.dr_name, device, sizeof(req.dr_name)) >=
178*b01f409fSWarner Losh 	    sizeof(req.dr_name)) {
179*b01f409fSWarner Losh 		errno = EINVAL;
180*b01f409fSWarner Losh 		*buffer = NULL;
181*b01f409fSWarner Losh 		return (-1);
182*b01f409fSWarner Losh 	}
183*b01f409fSWarner Losh 
184*b01f409fSWarner Losh 	/*
185*b01f409fSWarner Losh 	 * Maybe do the request twice. Once to get the length, and then again to
186*b01f409fSWarner Losh 	 * get the string if BUFLEN bytes is insufficient.
187*b01f409fSWarner Losh 	 */
188*b01f409fSWarner Losh 	req.dr_flags = 0;
189*b01f409fSWarner Losh 	req.dr_buffer.length = BUFLEN;
190*b01f409fSWarner Losh again:
191*b01f409fSWarner Losh 	req.dr_buffer.buffer = malloc(req.dr_buffer.length);
192*b01f409fSWarner Losh 	strlcpy(req.dr_buffer.buffer, locator, req.dr_buffer.length);
193*b01f409fSWarner Losh 	if (devctl_request(DEV_GET_PATH, &req) == 0) {
194*b01f409fSWarner Losh 		*buffer = req.dr_buffer.buffer;
195*b01f409fSWarner Losh 		return (0);
196*b01f409fSWarner Losh 	}
197*b01f409fSWarner Losh 	if (errno == ENAMETOOLONG && req.dr_buffer.length != BUFLEN) {
198*b01f409fSWarner Losh 		free(req.dr_buffer.buffer);
199*b01f409fSWarner Losh 		goto again;
200*b01f409fSWarner Losh 	}
201*b01f409fSWarner Losh 	serrno = errno;
202*b01f409fSWarner Losh 	free(req.dr_buffer.buffer);
203*b01f409fSWarner Losh 	errno = serrno;
204*b01f409fSWarner Losh 	*buffer = NULL;
205*b01f409fSWarner Losh 	return (-1);
206*b01f409fSWarner Losh }
207