From owner-svn-src-all@freebsd.org Fri Oct 2 18:26:42 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id CD21A4338A2; Fri, 2 Oct 2020 18:26:42 +0000 (UTC) (envelope-from manu@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4C2z3Q4mGXz4VDB; Fri, 2 Oct 2020 18:26:42 +0000 (UTC) (envelope-from manu@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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7EBEB26521; Fri, 2 Oct 2020 18:26:42 +0000 (UTC) (envelope-from manu@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 092IQg4l023813; Fri, 2 Oct 2020 18:26:42 GMT (envelope-from manu@FreeBSD.org) Received: (from manu@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 092IQfdj023808; Fri, 2 Oct 2020 18:26:41 GMT (envelope-from manu@FreeBSD.org) Message-Id: <202010021826.092IQfdj023808@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: manu set sender to manu@FreeBSD.org using -f From: Emmanuel Vadot Date: Fri, 2 Oct 2020 18:26:41 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r366372 - in head/sys: compat/linuxkpi/common/include/linux compat/linuxkpi/common/src conf X-SVN-Group: head X-SVN-Commit-Author: manu X-SVN-Commit-Paths: in head/sys: compat/linuxkpi/common/include/linux compat/linuxkpi/common/src conf X-SVN-Commit-Revision: 366372 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 Oct 2020 18:26:42 -0000 Author: manu Date: Fri Oct 2 18:26:41 2020 New Revision: 366372 URL: https://svnweb.freebsd.org/changeset/base/366372 Log: linuxkpi: Add backlight support Add backlight function to linuxkpi. Graphics drivers expose the backlight of the panel directly so allow them to use the backlight subsystem so user can use backlight(8) to configure them. Reviewed by: hselasky Relnotes: yes Differential Revision: The FreeBSD Foundation Added: head/sys/compat/linuxkpi/common/include/linux/backlight.h (contents, props changed) Modified: head/sys/compat/linuxkpi/common/include/linux/device.h head/sys/compat/linuxkpi/common/src/linux_kmod.c head/sys/compat/linuxkpi/common/src/linux_pci.c head/sys/conf/kmod.mk Added: head/sys/compat/linuxkpi/common/include/linux/backlight.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/compat/linuxkpi/common/include/linux/backlight.h Fri Oct 2 18:26:41 2020 (r366372) @@ -0,0 +1,94 @@ +/*- + * Copyright (c) 2020 The FreeBSD Foundation + * + * This software was developed by Emmanuel Vadot under sponsorship + * from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _LINUX_BACKLIGHT_H_ +#define _LINUX_BACKLIGHT_H_ + +#include + +struct backlight_device; + +enum backlight_type { + BACKLIGHT_RAW = 0, +}; + +struct backlight_properties { + int type; + int max_brightness; + int brightness; + int power; +}; + +enum backlight_notification { + BACKLIGHT_REGISTERED, + BACKLIGHT_UNREGISTERED, +}; + +enum backlight_update_reason { + BACKLIGHT_UPDATE_HOTKEY = 0 +}; + +struct backlight_ops { + int options; +#define BL_CORE_SUSPENDRESUME 1 + int (*update_status)(struct backlight_device *); + int (*get_brightness)(struct backlight_device *); +}; + +struct backlight_device { + const struct backlight_ops *ops; + struct backlight_properties props; + void *data; + struct device *dev; + char *name; +}; + +#define bl_get_data(bd) (bd)->data + +struct backlight_device *linux_backlight_device_register(const char *name, + struct device *dev, void *data, const struct backlight_ops *ops, struct backlight_properties *props); +void linux_backlight_device_unregister(struct backlight_device *bd); +#define backlight_device_register(name, dev, data, ops, props) \ + linux_backlight_device_register(name, dev, data, ops, props) +#define backlight_device_unregister(bd) linux_backlight_device_unregister(bd) + +static inline void +backlight_update_status(struct backlight_device *bd) +{ + bd->ops->update_status(bd); +} + +static inline void +backlight_force_update(struct backlight_device *bd, int reason) +{ + bd->props.brightness = bd->ops->get_brightness(bd); +} + +#endif /* _LINUX_BACKLIGHT_H_ */ Modified: head/sys/compat/linuxkpi/common/include/linux/device.h ============================================================================== --- head/sys/compat/linuxkpi/common/include/linux/device.h Fri Oct 2 18:23:27 2020 (r366371) +++ head/sys/compat/linuxkpi/common/include/linux/device.h Fri Oct 2 18:26:41 2020 (r366372) @@ -41,9 +41,11 @@ #include #include #include +#include #include #include +#include struct device; struct fwnode_handle; @@ -114,6 +116,8 @@ struct device { unsigned int irq_end; const struct attribute_group **groups; struct fwnode_handle *fwnode; + struct cdev *backlight_dev; + struct backlight_device *bd; spinlock_t devres_lock; struct list_head devres_head; Modified: head/sys/compat/linuxkpi/common/src/linux_kmod.c ============================================================================== --- head/sys/compat/linuxkpi/common/src/linux_kmod.c Fri Oct 2 18:23:27 2020 (r366371) +++ head/sys/compat/linuxkpi/common/src/linux_kmod.c Fri Oct 2 18:26:41 2020 (r366372) @@ -31,4 +31,5 @@ __FBSDID("$FreeBSD$"); #include MODULE_VERSION(linuxkpi, 1); +MODULE_DEPEND(linuxkpi, backlight, 1, 1, 1); MODULE_DEPEND(linuxkpi, pci, 1, 1, 1); Modified: head/sys/compat/linuxkpi/common/src/linux_pci.c ============================================================================== --- head/sys/compat/linuxkpi/common/src/linux_pci.c Fri Oct 2 18:23:27 2020 (r366371) +++ head/sys/compat/linuxkpi/common/src/linux_pci.c Fri Oct 2 18:26:41 2020 (r366372) @@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include @@ -64,6 +65,10 @@ __FBSDID("$FreeBSD$"); #include #include +#include + +#include "backlight_if.h" + static device_probe_t linux_pci_probe; static device_attach_t linux_pci_attach; static device_detach_t linux_pci_detach; @@ -73,6 +78,9 @@ static device_shutdown_t linux_pci_shutdown; static pci_iov_init_t linux_pci_iov_init; static pci_iov_uninit_t linux_pci_iov_uninit; static pci_iov_add_vf_t linux_pci_iov_add_vf; +static int linux_backlight_get_status(device_t dev, struct backlight_props *props); +static int linux_backlight_update_status(device_t dev, struct backlight_props *props); +static int linux_backlight_get_info(device_t dev, struct backlight_info *info); static device_method_t pci_methods[] = { DEVMETHOD(device_probe, linux_pci_probe), @@ -84,6 +92,11 @@ static device_method_t pci_methods[] = { DEVMETHOD(pci_iov_init, linux_pci_iov_init), DEVMETHOD(pci_iov_uninit, linux_pci_iov_uninit), DEVMETHOD(pci_iov_add_vf, linux_pci_iov_add_vf), + + /* backlight interface */ + DEVMETHOD(backlight_update_status, linux_backlight_update_status), + DEVMETHOD(backlight_get_status, linux_backlight_get_status), + DEVMETHOD(backlight_get_info, linux_backlight_get_info), DEVMETHOD_END }; @@ -950,4 +963,74 @@ linux_dma_pool_free(struct dma_pool *pool, void *vaddr DMA_POOL_UNLOCK(pool); uma_zfree_arg(pool->pool_zone, obj, pool); +} + +static int +linux_backlight_get_status(device_t dev, struct backlight_props *props) +{ + struct pci_dev *pdev; + + linux_set_current(curthread); + pdev = device_get_softc(dev); + + props->brightness = pdev->dev.bd->props.brightness; + props->brightness = props->brightness * 100 / pdev->dev.bd->props.max_brightness; + props->nlevels = 0; + + return (0); +} + +static int +linux_backlight_get_info(device_t dev, struct backlight_info *info) +{ + struct pci_dev *pdev; + + linux_set_current(curthread); + pdev = device_get_softc(dev); + + info->type = BACKLIGHT_TYPE_PANEL; + strlcpy(info->name, pdev->dev.bd->name, BACKLIGHTMAXNAMELENGTH); + return (0); +} + +static int +linux_backlight_update_status(device_t dev, struct backlight_props *props) +{ + struct pci_dev *pdev; + + linux_set_current(curthread); + pdev = device_get_softc(dev); + + pdev->dev.bd->props.brightness = pdev->dev.bd->props.max_brightness * + props->brightness / 100; + return (pdev->dev.bd->ops->update_status(pdev->dev.bd)); +} + +struct backlight_device * +linux_backlight_device_register(const char *name, struct device *dev, + void *data, const struct backlight_ops *ops, struct backlight_properties *props) +{ + + dev->bd = malloc(sizeof(*dev->bd), M_DEVBUF, M_WAITOK | M_ZERO); + dev->bd->ops = ops; + dev->bd->props.type = props->type; + dev->bd->props.max_brightness = props->max_brightness; + dev->bd->props.brightness = props->brightness; + dev->bd->props.power = props->power; + dev->bd->data = data; + dev->bd->dev = dev; + dev->bd->name = strdup(name, M_DEVBUF); + + dev->backlight_dev = backlight_register(name, dev->bsddev); + + return (dev->bd); +} + +void +linux_backlight_device_unregister(struct backlight_device *bd) +{ + + backlight_destroy(bd->dev->backlight_dev); + free(bd->name, M_DEVBUF); + free(bd, M_DEVBUF); } Modified: head/sys/conf/kmod.mk ============================================================================== --- head/sys/conf/kmod.mk Fri Oct 2 18:23:27 2020 (r366371) +++ head/sys/conf/kmod.mk Fri Oct 2 18:26:41 2020 (r366372) @@ -91,6 +91,7 @@ CFLAGS+= -fno-strict-aliasing WERROR?= -Werror LINUXKPI_GENSRCS+= \ + backlight_if.h \ bus_if.h \ device_if.h \ pci_if.h \