From owner-svn-src-stable@freebsd.org Fri Sep 30 22:05:49 2016 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 9B0B0C03C9E; Fri, 30 Sep 2016 22:05:49 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 7631EB0B; Fri, 30 Sep 2016 22:05:49 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u8UM5mtW061300; Fri, 30 Sep 2016 22:05:48 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u8UM5m84061293; Fri, 30 Sep 2016 22:05:48 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <201609302205.u8UM5m84061293@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Fri, 30 Sep 2016 22:05:48 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r306533 - in stable: 10/lib/libdevctl 10/sys/kern 10/sys/sys 10/usr.sbin/devctl 11/lib/libdevctl 11/sys/kern 11/sys/sys 11/usr.sbin/devctl X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Sep 2016 22:05:49 -0000 Author: jhb Date: Fri Sep 30 22:05:47 2016 New Revision: 306533 URL: https://svnweb.freebsd.org/changeset/base/306533 Log: MFC 305034: Implement 'devctl clear driver' to undo a previous 'set driver'. Add a new 'clear driver' command for devctl along with the accompanying ioctl and devctl_clear_driver() library routine to reset a device to use a wildcard devclass instead of a fixed devclass. This can be used to undo a previous 'set driver' command. After the device's name has been reset to permit wildcard names, it is reprobed so that it can attach to newly-available (to it) device drivers. Sponsored by: Chelsio Communications Modified: stable/10/lib/libdevctl/devctl.3 stable/10/lib/libdevctl/devctl.c stable/10/lib/libdevctl/devctl.h stable/10/sys/kern/subr_bus.c stable/10/sys/sys/bus.h stable/10/usr.sbin/devctl/devctl.8 stable/10/usr.sbin/devctl/devctl.c Directory Properties: stable/10/ (props changed) Changes in other areas also in this revision: Modified: stable/11/lib/libdevctl/devctl.3 stable/11/lib/libdevctl/devctl.c stable/11/lib/libdevctl/devctl.h stable/11/sys/kern/subr_bus.c stable/11/sys/sys/bus.h stable/11/usr.sbin/devctl/devctl.8 stable/11/usr.sbin/devctl/devctl.c Directory Properties: stable/11/ (props changed) Modified: stable/10/lib/libdevctl/devctl.3 ============================================================================== --- stable/10/lib/libdevctl/devctl.3 Fri Sep 30 21:14:42 2016 (r306532) +++ stable/10/lib/libdevctl/devctl.3 Fri Sep 30 22:05:47 2016 (r306533) @@ -25,12 +25,13 @@ .\" .\" $FreeBSD$ .\" -.Dd February 2, 2016 +.Dd August 29, 2016 .Dt DEVCTL 3 .Os .Sh NAME .Nm devctl , .Nm devctl_attach , +.Nm devctl_clear_driver , .Nm devctl_detach , .Nm devctl_disable , .Nm devctl_enable , @@ -43,6 +44,8 @@ .Ft int .Fn devctl_attach "const char *device" .Ft int +.Fn devctl_clear_driver "const char *device" "bool force" +.Ft int .Fn devctl_detach "const char *device" "bool force" .Ft int .Fn devctl_disable "const char *device" "bool force_detach" @@ -133,9 +136,26 @@ If the device is already attached and is true, the device will be detached from its current device driver before it is attached to the new device driver. +.Pp +The +.Fn devctl_clear_driver +function resets a device so that it can be attached to any valid device +driver rather than only drivers with a previously specified name. +This function is used to undo a previous call to +.Fn devctl_set_driver . +If the device is already attached and +.Fa force +is false, +the request will fail. +If the device is already attached and +.Fa force +is true, +the device will be detached from its current device driver. +After the device's name is reset, +it is reprobed and attached to a suitable device driver if one is found. .Sh RETURN VALUES -.Rv -std devctl_attach devctl_detach devctl_disable devctl_enable \ -devctl_set_driver +.Rv -std devctl_attach devctl_clear_driver devctl_detach \ +devctl_disable devctl_enable devctl_set_driver .Sh ERRORS In addition to specific errors noted below, all of the @@ -244,6 +264,24 @@ The device is disabled. .It Bq Er ENXIO The new device driver failed to attach. .El +.Pp +The +.Fn devctl_clear_driver +function may fail if: +.Bl -tag -width Er +.It Bq Er EBUSY +The device is currently attached to a device driver and +.Fa force +is false. +.It Bq Er EBUSY +The current device driver for +.Fa device +is busy and cannot detach at this time. +.It Bq Er EINVAL +The device is not configured for a specific device driver name. +.It Bq Er ENXIO +The device driver chosen after reprobing failed to attach. +.El .Sh SEE ALSO .Xr devinfo 3 , .Xr devstat 3 , Modified: stable/10/lib/libdevctl/devctl.c ============================================================================== --- stable/10/lib/libdevctl/devctl.c Fri Sep 30 21:14:42 2016 (r306532) +++ stable/10/lib/libdevctl/devctl.c Fri Sep 30 22:05:47 2016 (r306533) @@ -108,3 +108,11 @@ devctl_set_driver(const char *device, co req.dr_flags |= DEVF_SET_DRIVER_DETACH; return (devctl_request(DEV_SET_DRIVER, &req)); } + +int +devctl_clear_driver(const char *device, bool force) +{ + + return (devctl_simple_request(DEV_CLEAR_DRIVER, device, force ? + DEVF_CLEAR_DRIVER_DETACH : 0)); +} Modified: stable/10/lib/libdevctl/devctl.h ============================================================================== --- stable/10/lib/libdevctl/devctl.h Fri Sep 30 21:14:42 2016 (r306532) +++ stable/10/lib/libdevctl/devctl.h Fri Sep 30 22:05:47 2016 (r306533) @@ -36,5 +36,6 @@ int devctl_detach(const char *device, bo int devctl_enable(const char *device); int devctl_disable(const char *device, bool force_detach); int devctl_set_driver(const char *device, const char *driver, bool force); +int devctl_clear_driver(const char *device, bool force); #endif /* !__DEVCTL_H__ */ Modified: stable/10/sys/kern/subr_bus.c ============================================================================== --- stable/10/sys/kern/subr_bus.c Fri Sep 30 21:14:42 2016 (r306532) +++ stable/10/sys/kern/subr_bus.c Fri Sep 30 22:05:47 2016 (r306533) @@ -5069,6 +5069,7 @@ devctl2_ioctl(struct cdev *cdev, u_long case DEV_ENABLE: case DEV_DISABLE: case DEV_SET_DRIVER: + case DEV_CLEAR_DRIVER: error = priv_check(td, PRIV_DRIVER); if (error == 0) error = find_device(req, &dev); @@ -5210,6 +5211,25 @@ devctl2_ioctl(struct cdev *cdev, u_long error = device_probe_and_attach(dev); break; } + case DEV_CLEAR_DRIVER: + if (!(dev->flags & DF_FIXEDCLASS)) { + error = 0; + break; + } + if (device_is_attached(dev)) { + if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH) + error = device_detach(dev); + else + error = EBUSY; + if (error) + break; + } + + dev->flags &= ~DF_FIXEDCLASS; + dev->flags |= DF_WILDCARD; + devclass_delete_device(dev->devclass, dev); + error = device_probe_and_attach(dev); + break; } mtx_unlock(&Giant); return (error); Modified: stable/10/sys/sys/bus.h ============================================================================== --- stable/10/sys/sys/bus.h Fri Sep 30 21:14:42 2016 (r306532) +++ stable/10/sys/sys/bus.h Fri Sep 30 22:05:47 2016 (r306533) @@ -103,6 +103,7 @@ struct devreq { #define DEV_ENABLE _IOW('D', 3, struct devreq) #define DEV_DISABLE _IOW('D', 4, struct devreq) #define DEV_SET_DRIVER _IOW('D', 7, struct devreq) +#define DEV_CLEAR_DRIVER _IOW('D', 8, struct devreq) /* Flags for DEV_DETACH and DEV_DISABLE. */ #define DEVF_FORCE_DETACH 0x0000001 @@ -110,6 +111,9 @@ struct devreq { /* Flags for DEV_SET_DRIVER. */ #define DEVF_SET_DRIVER_DETACH 0x0000001 /* Detach existing driver. */ +/* Flags for DEV_CLEAR_DRIVER. */ +#define DEVF_CLEAR_DRIVER_DETACH 0x0000001 /* Detach existing driver. */ + #ifdef _KERNEL #include Modified: stable/10/usr.sbin/devctl/devctl.8 ============================================================================== --- stable/10/usr.sbin/devctl/devctl.8 Fri Sep 30 21:14:42 2016 (r306532) +++ stable/10/usr.sbin/devctl/devctl.8 Fri Sep 30 22:05:47 2016 (r306533) @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd February 2, 2016 +.Dd August 29, 2016 .Dt DEVCTL 8 .Os .Sh NAME @@ -36,6 +36,10 @@ .Cm attach .Ar device .Nm +.Cm clear driver +.Op Fl f +.Ar device +.Nm .Cm detach .Op Fl f .Ar device @@ -115,6 +119,21 @@ If the device is already attached to a d .Fl f flag is not specified, the device will not be changed. +.It Xo Cm clear driver +.Op Fl f +.Ar device +.Xc +Clear a previously-forced driver name so that the device is able to use any +valid device driver. +After the previous name has been cleared, +the device is reprobed so that other device drivers may attach to it. +This can be used to undo an earlier +.Cm set driver +command. +If the device is currently attached to a device driver and the +.Fl f +flag is not specified, +the device will not be changed. .El .Sh SEE ALSO .Xr devctl 3 , Modified: stable/10/usr.sbin/devctl/devctl.c ============================================================================== --- stable/10/usr.sbin/devctl/devctl.c Fri Sep 30 21:14:42 2016 (r306532) +++ stable/10/usr.sbin/devctl/devctl.c Fri Sep 30 22:05:47 2016 (r306533) @@ -65,17 +65,19 @@ static int devctl_table_handler(struct d SET_DECLARE(DEVCTL_DATASET(top), struct devctl_command); +DEVCTL_TABLE(top, clear); DEVCTL_TABLE(top, set); static void usage(void) { - fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", + fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n", "usage: devctl attach device", " devctl detach [-f] device", " devctl disable [-f] device", " devctl enable device", - " devctl set driver [-f] device driver"); + " devctl set driver [-f] device driver", + " devctl clear driver [-f] device"); exit(1); } @@ -233,6 +235,40 @@ set_driver(int ac, char **av) } DEVCTL_COMMAND(set, driver, set_driver); +static void +clear_driver_usage(void) +{ + + fprintf(stderr, "usage: devctl clear driver [-f] device\n"); + exit(1); +} + +static int +clear_driver(int ac, char **av) +{ + bool force; + int ch; + + force = false; + while ((ch = getopt(ac, av, "f")) != -1) + switch (ch) { + case 'f': + force = true; + break; + default: + clear_driver_usage(); + } + ac -= optind; + av += optind; + + if (ac != 1) + clear_driver_usage(); + if (devctl_clear_driver(av[0], force) < 0) + err(1, "Failed to clear %s driver", av[0]); + return (0); +} +DEVCTL_COMMAND(clear, driver, clear_driver); + int main(int ac, char *av[]) {