pax_global_header00006660000000000000000000000064130264627410014517gustar00rootroot0000000000000052 comment=2532dc008c43c9a08f7d08c8350aa60b49eb1f42 ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/000077500000000000000000000000001302646274100227405ustar00rootroot00000000000000ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/Makefile000066400000000000000000000033121302646274100243770ustar00rootroot00000000000000#!/usr/bin/make -f # (c) 2015 Christoph Grenz # This file is part of ddcci-driver-linux. # # ddcci-driver-linux is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # ddcci-driver-linux is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with ddcci-driver-linux. If not, see . MODULES := ddcci ddcci-backlight ddcci-backlight: ddcci reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(firstword $(1)) REVMODS := $(call reverse,$(MODULES)) all: $(MODULES) $(MODULES): $(MAKE) -C "$@" CLEANMODS := $(addprefix clean-,$(MODULES)) $(CLEANMODS): $(MAKE) -C "$(subst clean-,,$@)" clean clean: $(CLEANMODS) LOADMODS := $(addprefix load-,$(MODULES)) $(LOADMODS): $(MAKE) -C "$(subst load-,,$@)" load load: $(LOADMODS) UNLOADMODS := $(addprefix unload-,$(REVMODS)) $(UNLOADMODS): $(MAKE) -C "$(subst unload-,,$@)" unload unload: $(UNLOADMODS) INSTALLMODS := $(addprefix install-,$(MODULES)) $(INSTALLMODS): $(MAKE) -C "$(subst install-,,$@)" install install: $(INSTALLMODS) UNINSTALLMODS := $(addprefix uninstall-,$(REVMODS)) $(UNINSTALLMODS): $(MAKE) -C "$(subst uninstall-,,$@)" uninstall uninstall: $(UNINSTALLMODS) .PHONY: all clean $(MODULES) $(CLEANMODS) $(LOADMODS) $(UNLOADMODS) $(INSTALLMODS) $(UNINSTALLMODS)ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/Makefile.dkms000066400000000000000000000024151302646274100253370ustar00rootroot00000000000000#!/usr/bin/make -f # (c) 2015 Christoph Grenz # This file is part of ddcci-driver-linux. # # ddcci-driver-linux is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # ddcci-driver-linux is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with ddcci-driver-linux. If not, see . PACKAGE_NAME := ddcci PACKAGE_VERSION := 0.3.1 install: dkms add . dkms install $(PACKAGE_NAME)/$(PACKAGE_VERSION) uninstall: dkms uninstall $(PACKAGE_NAME)/$(PACKAGE_VERSION) || true dkms remove $(PACKAGE_NAME)/$(PACKAGE_VERSION) --all || true rm -r /usr/src/$(PACKAGE_NAME)-$(PACKAGE_VERSION) || true load: @test -n "$$(dkms status ddcci/0.3.1)" || { echo 'Please run `make -f Makefile.dkms install` first.'; false; } modprobe ddcci unload: rmmod ddcci-backlight ddcci .PHONY: install uninstall load unloadddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/README.md000066400000000000000000000121051302646274100242160ustar00rootroot00000000000000# ddcci-driver-linux # A pair of Linux kernel drivers for DDC/CI monitors. DDC/CI is a control protocol for monitor settings supported by most monitors since about 2005. It is based on ACCESS.bus (an early USB predecessor). ## ddcci (bus driver) ## This driver detects DDC/CI devices on DDC I²C busses, identifies them and creates corresponding devices. As this is a I²C driver it won't be autoloaded and must be manually loaded, for example by putting a line with `ddcci` in `/etc/modules`. ### sysfs interface ### Each detected DDC/CI device gets a directory in `/sys/bus/ddcci/devices`. The main device on a bus is named `ddcci[I²C bus number]`. Internal dependent devices are named `ddcci[I²C bus number]i[hex address]` External dependent devices are named `ddcci[I²C bus number]e[hex address]` There the following files export information about the device: #### capabilities #### The full ACCESS.bus capabilities string. It contains the protocol, type and model of the device, a list of all supported command codes, etc. See the ACCESS.bus spec for more information. #### idProt #### ACCESS.bus protocol supported by the device. Usually "monitor". #### idType #### ACCESS.bus device subtype. Usually "LCD" or "CRT". #### idModel #### ACCESS.bus device model identifier. Usually a shortened form of the device model name. #### idVendor #### ACCESS.bus device vendor identifier. Empty if the Identification command is not supported. #### idModule #### ACCESS.bus device module identifier. Empty if the Identification command is not supported. #### idSerial #### 32 bit device number. A fixed serial number if it's positive, a temporary serial number if negative and zero if the Identification command is not supported. ### Character device interface ### For each DDC/CI device a character device in `/dev/bus/ddcci/[I²C bus number]/` is created. The main device on the bus is named `display`. Internal dependent devices are named `i[hex address]` External dependent devices are named `e[hex address]` These character devices can be used to issue commands to a DDC/CI device more easily than over i2c-dev devices. They should be opened unbuffered and may be opened with O_EXCL if you want exclusive access. To send a command just write the command byte and the arguments with a single `write()` operation. The length byte and checksum are automatically calculated. To read a response use `read()` with a buffer big enough for the expected answer. NOTE: The maximum length of a DDC/CI message is 127 bytes. An Example (in Python): with open('/dev/bus/ddcci/3/display', 'r+b', buffering=0) as f: # Read contrast f.write(bytes([0x01, 0x12])) response = f.read(8) print("Contrast:", response[6] * 256 + response[7], "/", response[4] * 256 + response[5]) The following error codes are used: * EAGAIN: there was no response yet or (with O_NONBLOCK) the device was in use by another thread * EBADMSG: there was a response but the checksum didn't match * EBUSY: the device is opened exclusively by another thread (on open()) * EINVAL: message too big (on write()) * EIO: generic I/O failure * EMSGSIZE: the buffer was too small (on read()) * ENOMEM: not enough free memory to allocate buffers (on open()) Lower layers may pass error codes not in this list like ENXIO, so be prepared for that. ## ddcci-backlight (monitor backlight driver) ## For each monitor that supports accessing the Backlight Level White or the Luminance property, a backlight device of type "raw" named like the corresponding ddcci device is created. You can find them in `/sys/class/backlight/`. ## Limitations ## Dependent device (sub devices using DDC/CI directly wired to the monitor, like Calibration devices, IR remotes, etc.) aren't automatically detected. You can force detection of internal dependent devices by setting the `autoprobe_addrs` module parameter of ddcci. You can force detection of external dependent devices by writing "ddcci-dependent [address]" into /sys/bus/i2c/i2c-?/new_device. There is no direct synchronization if you manually change the luminance with the buttons on your monitor, as this can only be realized through polling and some monitors close their OSD every time a DDC/CI command is received. Monitor hotplugging is not detected. You need to detach/reattach the I²C driver or reload the module. ## Installation ## Generally, you only need to clone the repository and run make to build both kernel modules, then run make load. To permanently install the drivers, use `make install` (or `make -f Makefile.dkms install` if you prefer DKMS). ## Debugging ## Both drivers use the [dynamic debugging feature](https://www.kernel.org/doc/Documentation/dynamic-debug-howto.txt) of the Linux kernel. To get detailed debugging messages, set the `dyndbg` module parameter. For details on the syntax and for dynamic activation/deactivation of the debugging messages, see the documentation linked above. If you want to enable debugging permanently across reboots, create a file `/etc/modprobe.d/ddcci.conf` containing lines like the following before loading the modules: options ddcci dyndbg options ddcci-backlight dyndbg ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/ddcci-backlight/000077500000000000000000000000001302646274100257345ustar00rootroot00000000000000ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/ddcci-backlight/Makefile000077500000000000000000000037071302646274100274060ustar00rootroot00000000000000#!/usr/bin/make -f # (c) 2015 Christoph Grenz # This file is part of ddcci-driver-linux. # # ddcci-driver-linux is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # ddcci-driver-linux is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with ddcci-driver-linux. If not, see . MODULE_NAME := ddcci-backlight MODULE_VERSION := 0.3.1 KVER := $(shell uname -r) KERNEL_MODLIB := /lib/modules/$(KVER) MODDIR := $(KERNEL_MODLIB)/extra KDIR := /lib/modules/$(KVER)/build PWD := $(shell pwd) obj-m := ddcci-backlight.o src ?= $(PWD) obj ?= . ccflags-y := -I$(src)/../include KBUILD_EXTRA_SYMBOLS := $(src)/../ddcci/Module.symvers module: ddcci-backlight.ko ddcci-backlight.ko: ddcci-backlight.c $(MAKE) -C "$(KDIR)" SUBDIRS="$(src)" modules ddcci-backlight.mod.c: ddcci-backlight.ko ddcci-backlight.mod.o: ddcci-backlight.mod.c clean: rm $(obj-m) ddcci-backlight.mod.c ddcci-backlight.mod.o ddcci-backlight.ko modules.order Module.symvers .ddcci-backlight*.cmd .tmp_versions/ddcci-backlight.mod || true rmdir .tmp_versions 2>/dev/null || true install: module ddcci-backlight.mod.o install -d "$(MODDIR)" install -m 644 ddcci-backlight.ko "$(MODDIR)/" depmod "$(KVER)" uninstall: rm "$(MODDIR)"/ddcci-backlight.ko || true rmdir --ignore-fail-on-non-empty "$(MODDIR)" depmod "$(KVER)" load: module insmod ddcci-backlight.ko unload: ! grep -q "^ddcci[_-]backlight " /proc/modules || rmmod ddcci-backlight .PHONY: module clean install uninstall load unload install-dkms uninstall-dkms ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/ddcci-backlight/ddcci-backlight.c000066400000000000000000000202571302646274100311020ustar00rootroot00000000000000/* * DDC/CI monitor backlight driver * * Copyright (c) 2015 Christoph Grenz */ /* * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include #include #include #include #define DDCCI_COMMAND_READ 0x01 /* read ctrl value */ #define DDCCI_REPLY_READ 0x02 /* read ctrl value reply */ #define DDCCI_COMMAND_WRITE 0x03 /* write ctrl value */ #define DDCCI_COMMAND_SAVE 0x0c /* save current settings */ #define DDCCI_MONITOR_LUMINANCE 0x10 #define DDCCI_MONITOR_BACKLIGHT 0x13 #define DDCCI_MONITOR_BL_WHITE 0x6B struct ddcci_monitor_drv_data { struct ddcci_device *device; struct backlight_device *bl_dev; struct device *fb_dev; unsigned char used_vcp; }; static int ddcci_monitor_writectrl(struct ddcci_device *device, unsigned char ctrl, unsigned short value) { unsigned char buf[4]; int ret; buf[0] = DDCCI_COMMAND_WRITE; buf[1] = ctrl; buf[2] = (value >> 8); buf[3] = (value & 255); ret = ddcci_device_write(device, true, buf, sizeof(buf)); return ret; } static int ddcci_monitor_readctrl(struct ddcci_device *device, unsigned char ctrl, unsigned short *value, unsigned short *maximum) { int ret; unsigned char buf[10]; buf[0] = DDCCI_COMMAND_READ; buf[1] = ctrl; ret = ddcci_device_writeread(device, true, buf, 2, sizeof(buf)); if (ret < 0) return ret; if (ret == 0) return -ENOTSUPP; if (ret == 8 && buf[0] == DDCCI_REPLY_READ && buf[2] == ctrl) { if (value) *value = buf[6] * 256 + buf[7]; if (maximum) *maximum = buf[4] * 256 + buf[5]; if (buf[1] == 1) return -ENOTSUPP; if (buf[1] != 0) return -EIO; return 0; } return -EIO; } static int ddcci_backlight_check_fb(struct backlight_device *bl, struct fb_info *info) { struct ddcci_monitor_drv_data *drv_data = bl_get_data(bl); return drv_data->fb_dev == NULL || drv_data->fb_dev == info->dev; } static int ddcci_backlight_update_status(struct backlight_device *bl) { struct ddcci_monitor_drv_data *drv_data = bl_get_data(bl); int brightness = bl->props.brightness; int ret; if (bl->props.power != FB_BLANK_UNBLANK || bl->props.state & BL_CORE_FBBLANK) brightness = 0; ret = ddcci_monitor_writectrl(drv_data->device, drv_data->used_vcp, brightness); if (ret > 0) ret = 0; return ret; } static int ddcci_backlight_get_brightness(struct backlight_device *bl) { unsigned short value = 0, maxval = 0; int ret; struct ddcci_monitor_drv_data *drv_data = bl_get_data(bl); ret = ddcci_monitor_readctrl(drv_data->device, drv_data->used_vcp, &value, &maxval); if (ret < 0) return ret; bl->props.brightness = value; bl->props.max_brightness = maxval; ret = value; return ret; } static const struct backlight_ops ddcci_backlight_ops = { .options = 0, .update_status = ddcci_backlight_update_status, .get_brightness = ddcci_backlight_get_brightness, .check_fb = ddcci_backlight_check_fb, }; static const char *ddcci_monitor_vcp_name(unsigned char vcp) { switch (vcp) { case DDCCI_MONITOR_BL_WHITE: return "backlight"; case DDCCI_MONITOR_LUMINANCE: return "luminance"; default: return "???"; } } static const char *ddcci_monitor_next_vcp_item(const char *ptr) { int depth = 0; /* Sanity check */ if (unlikely(ptr == NULL || ptr[0] == '\0')) return NULL; /* Find next white space outside of parentheses */ while ((ptr = strpbrk(ptr, " ()"))) { if (!ptr || depth == INT_MAX) { return NULL; } else if (*ptr == '(') { depth++; } else if (depth > 0) { if (*ptr == ')') depth--; } else { break; } ++ptr; } /* Skip over whitespace */ ptr = skip_spaces(ptr); /* Check if we're now at the end of the list */ if (unlikely(*ptr == '\0' || *ptr == ')')) return NULL; return ptr; } static bool ddcci_monitor_find_vcp(unsigned char vcp, const char *s) { const char *ptr = s; char vcp_hex[3]; /* Sanity check */ if (unlikely(s == NULL || s[0] == '\0')) return false; /* Create hex representation of VCP */ if (unlikely(snprintf(vcp_hex, 3, "%02hhX", vcp) != 2)) { pr_err("snprintf failed to convert to hex. This should not happen.\n"); return false; } /* Search for it */ do { if (strncasecmp(vcp_hex, ptr, 2) == 0) { if (ptr[2] == ' ' || ptr[2] == '(' || ptr[2] == ')') { return true; } } } while ((ptr = ddcci_monitor_next_vcp_item(ptr))); return false; } static int ddcci_monitor_probe(struct ddcci_device *dev, const struct ddcci_device_id *id) { struct ddcci_monitor_drv_data *drv_data; struct backlight_properties props; struct backlight_device *bl = NULL; int ret = 0; bool support_luminance, support_bl_white; unsigned short brightness = 0, max_brightness = 0; const char *vcps; dev_dbg(&dev->dev, "probing monitor backlight device\n"); /* Get VCP list */ vcps = ddcci_find_capstr_item(dev->capabilities, "vcp", NULL); if (IS_ERR(vcps)) { dev_info(&dev->dev, "monitor doesn't provide a list of supported controls.\n"); support_bl_white = support_luminance = true; } else { /* Check VCP list for supported VCPs */ support_bl_white = ddcci_monitor_find_vcp(DDCCI_MONITOR_BL_WHITE, vcps); support_luminance = ddcci_monitor_find_vcp(DDCCI_MONITOR_LUMINANCE, vcps); /* Fallback to trying if no support is found */ if (!support_bl_white && !support_luminance) { dev_info(&dev->dev, "monitor doesn't announce support for backlight or luminance controls.\n"); support_bl_white = support_luminance = true; } } /* Initialize driver data structure */ drv_data = devm_kzalloc(&dev->dev, sizeof(struct ddcci_monitor_drv_data), GFP_KERNEL); if (!drv_data) return -ENOMEM; drv_data->device = dev; if (support_bl_white) { /* Try getting backlight level */ dev_dbg(&dev->dev, "trying to access \"backlight level white\" control\n"); ret = ddcci_monitor_readctrl(drv_data->device, DDCCI_MONITOR_BL_WHITE, &brightness, &max_brightness); if (ret < 0) { if (ret == -ENOTSUPP) dev_info(&dev->dev, "monitor does not support reading backlight level\n"); else goto err_free; } else { drv_data->used_vcp = DDCCI_MONITOR_BL_WHITE; } } if (support_luminance && !drv_data->used_vcp) { /* Try getting luminance */ dev_dbg(&dev->dev, "trying to access \"luminance\" control\n"); ret = ddcci_monitor_readctrl(drv_data->device, DDCCI_MONITOR_LUMINANCE, &brightness, &max_brightness); if (ret < 0) { if (ret == -ENOTSUPP) dev_info(&dev->dev, "monitor does not support reading luminance\n"); else goto err_free; } else { drv_data->used_vcp = DDCCI_MONITOR_LUMINANCE; } drv_data->used_vcp = DDCCI_MONITOR_LUMINANCE; } if (!drv_data->used_vcp) goto err_free; /* Create brightness device */ memset(&props, 0, sizeof(props)); props.type = BACKLIGHT_RAW; props.max_brightness = max_brightness; props.brightness = brightness; bl = devm_backlight_device_register(&dev->dev, dev_name(&dev->dev), &dev->dev, drv_data, &ddcci_backlight_ops, &props); drv_data->bl_dev = bl; if (IS_ERR(bl)) { dev_err(&dev->dev, "failed to register backlight\n"); return PTR_ERR(bl); } dev_info(&dev->dev, "registered %s as backlight device %s\n", ddcci_monitor_vcp_name(drv_data->used_vcp), dev_name(&dev->dev)); goto end; err_free: devm_kfree(&dev->dev, drv_data); end: return ret; } static int ddcci_monitor_remove(struct ddcci_device *dev) { dev_dbg(&dev->dev, "removing device\n"); return 0; } static struct ddcci_device_id ddcci_monitor_idtable[] = { { "monitor", DDCCI_ANY_ID, DDCCI_ANY_ID, DDCCI_ANY_ID, DDCCI_ANY_ID, 0 }, {} }; static struct ddcci_driver ddcci_backlight_driver = { .driver = { .name = "ddcci-backlight", .owner = THIS_MODULE, }, .id_table = ddcci_monitor_idtable, .probe = ddcci_monitor_probe, .remove = ddcci_monitor_remove, }; module_ddcci_driver(ddcci_backlight_driver); MODULE_AUTHOR("Christoph Grenz"); MODULE_DESCRIPTION("DDC/CI generic monitor backlight driver"); MODULE_VERSION("0.3.1"); MODULE_LICENSE("GPL"); MODULE_ALIAS("ddcci:monitor-*-*-*-*"); ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/ddcci/000077500000000000000000000000001302646274100240065ustar00rootroot00000000000000ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/ddcci/Makefile000077500000000000000000000036521302646274100254570ustar00rootroot00000000000000#!/usr/bin/make -f # (c) 2015 Christoph Grenz # This file is part of ddcci-driver-linux. # # ddcci-driver-linux is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # ddcci-driver-linux is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with ddcci-driver-linux. If not, see . MODULE_NAME := ddcci MODULE_VERSION := 0.3.1 KVER := $(shell uname -r) KERNEL_MODLIB := /lib/modules/$(KVER) MODDIR := $(KERNEL_MODLIB)/extra INCLUDEDIR := /usr/local/include KDIR := /lib/modules/$(KVER)/build PWD := $(shell pwd) obj-m := ddcci.o src ?= $(PWD) obj ?= . ccflags-y := -I$(src)/../include module: ddcci.ko ddcci.ko: ddcci.c $(MAKE) -C "$(KDIR)" SUBDIRS="$(src)" modules ddcci.mod.c: ddcci.ko ddcci.mod.o: ddcci.mod.c clean: rm $(obj-m) ddcci.mod.c ddcci.mod.o ddcci.ko modules.order Module.symvers .ddcci*.cmd .tmp_versions/ddcci.mod || true rmdir .tmp_versions 2>/dev/null || true install: module ddcci.mod.o install -d "$(MODDIR)" install -m 644 ddcci.ko "$(MODDIR)/" install -d "$(INCLUDEDIR)/linux/" install -m 644 ../include/linux/ddcci.h "$(INCLUDEDIR)/linux/" depmod "$(KVER)" uninstall: rm "$(MODDIR)"/ddcci.ko || true rmdir --ignore-fail-on-non-empty "$(MODDIR)" rm "$(INCLUDEDIR)/linux/ddcci.h" || true rmdir --ignore-fail-on-non-empty "$(INCLUDEDIR)/linux" depmod "$(KVER)" load: module insmod ddcci.ko unload: ! grep -q "^ddcci " /proc/modules || rmmod ddcci .PHONY: module clean install uninstall load unload install-dkms uninstall-dkms ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/ddcci/ddcci.c000066400000000000000000001242161302646274100252260ustar00rootroot00000000000000/* * DDC/CI sub-bus driver * * Copyright (c) 2015 Christoph Grenz */ /* * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include #include #include #include #include #include #include #include #include #include #include #include #include #define DDCCI_RECV_BUFFER_SIZE 130 #define DEVICE_NAME "ddcci" static unsigned int delay = 60; static unsigned short autoprobe_addrs[127] = {0xF0, 0xF2, 0xF4, 0xF6, 0xF8}; static int autoprobe_addr_count = 5; static dev_t ddcci_cdev_first; static dev_t ddcci_cdev_next; static dev_t ddcci_cdev_end; static DEFINE_SEMAPHORE(core_lock); struct bus_type ddcci_bus_type; EXPORT_SYMBOL_GPL(ddcci_bus_type); /* Internal per-i2c-client driver data */ struct ddcci_bus_drv_data { unsigned long quirks; struct i2c_client *i2c_dev; struct semaphore sem; unsigned char recv_buffer[DDCCI_RECV_BUFFER_SIZE]; }; /* Write a message to the DDC/CI bus using i2c_smbus_write_byte() */ static int __ddcci_write_bytewise(struct i2c_client *client, unsigned char addr, bool p_flag, const unsigned char *buf, unsigned char len) { int ret = 0; unsigned char outer_addr = (unsigned char)(client->addr << 1); unsigned xor = outer_addr; /* initial xor value */ /* Consistency checks */ if (len > 127) return -EINVAL; /* Special case: sender to 0x6E is always 0x51 */ if (addr == DDCCI_DEFAULT_DEVICE_ADDR) { addr = DDCCI_HOST_ADDR_ODD; } else { /* When sending the odd address is used */ addr = addr | 1; } /* first byte: sender address */ xor ^= addr; ret = i2c_smbus_write_byte(client, addr); if (ret < 0) return ret; /* second byte: protocol flag and message size */ xor ^= ((p_flag << 7) | len); ret = i2c_smbus_write_byte(client, (p_flag << 7)|len); if (ret < 0) return ret; /* send payload */ while (len--) { xor ^= (*buf); ret = i2c_smbus_write_byte(client, (*buf)); if (ret < 0) return ret; buf++; } /* send checksum */ ret = i2c_smbus_write_byte(client, xor); return ret; } /* Write a message to the DDC/CI bus using i2c_master_send() */ static int __ddcci_write_block(struct i2c_client *client, unsigned char addr, unsigned char *sendbuf, bool p_flag, const unsigned char *data, unsigned char len) { unsigned char outer_addr = (unsigned char)(client->addr << 1); unsigned xor = outer_addr; /* initial xor value */ unsigned char *ptr = sendbuf; /* Consistency checks */ if (len > 127) return -EINVAL; /* Special case: sender to 0x6E is always 0x51 */ if (addr == DDCCI_DEFAULT_DEVICE_ADDR) { addr = DDCCI_HOST_ADDR_ODD; } else { /* When sending the odd address is used */ addr = addr | 1; } /* first byte: sender address */ xor ^= addr; *(ptr++) = addr; /* second byte: protocol flag and message size */ xor ^= ((p_flag << 7) | len); *(ptr++) = (p_flag << 7)|len; /* payload */ while (len--) { xor ^= (*data); *(ptr++) = (*data); data++; } /* checksum */ (*ptr) = xor; /* Send it */ return i2c_master_send(client, sendbuf, len+3); } /* * Write a message to the DDC/CI bus. * * You must hold the bus semaphore when calling this function. */ static int ddcci_write(struct i2c_client *client, unsigned char addr, bool p_flag, const unsigned char *data, unsigned char len) { struct ddcci_bus_drv_data *drv_data; unsigned char *sendbuf; int ret; drv_data = i2c_get_clientdata(client); pr_debug("sending to %d:%02x:%02x: %*ph\n", client->adapter->nr, client->addr << 1, addr, len, data); if (drv_data->quirks & DDCCI_QUIRK_WRITE_BYTEWISE) { ret = __ddcci_write_bytewise(client, addr, p_flag, data, len); } else { sendbuf = drv_data->recv_buffer; ret = __ddcci_write_block(client, addr, sendbuf, p_flag, data, len); } return ret; } /* * Read a response from the DDC/CI bus with headers directly into a buffer. * Always check for DDCCI_QUIRK_SKIP_FIRST_BYTE when using this function. * The returned length contains the whole unmodified response. * If -EMSGSIZE is returned, the buffer contains the response up to `len`. * If any other negative error code is returned, the buffer content is * unspecified. */ static int __ddcci_read(struct i2c_client *client, unsigned char addr, bool p_flag, unsigned long quirks, unsigned char *buf, unsigned char len) { int i, payload_len, packet_length, ret; unsigned char xor = DDCCI_HOST_ADDR_EVEN; /* Consistency checks */ if (len < 3) return -EINVAL; /* Read frame */ ret = i2c_master_recv(client, buf, len); if (ret < 0) goto out_err; packet_length = ret; /* Skip first byte if quirk active */ if ((quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE) && ret > 0 && len > 0) { ret--; len--; buf++; } /* If answer too short (= incomplete) break out */ if (ret < 3) { ret = -EIO; goto out_err; } /* validate first byte */ if (unlikely(buf[0] != addr)) { ret = (buf[0] == '\0') ? -EAGAIN : -EIO; goto out_err; } /* validate second byte (protocol flag) */ if (unlikely((buf[1] & 0x80) != (p_flag << 7))) { if (!p_flag || !(quirks & DDCCI_QUIRK_NO_PFLAG)) { ret = -EIO; goto out_err; } } /* get and check payload length */ payload_len = buf[1] & 0x7F; if (3+payload_len > packet_length) return -EBADMSG; if (3+payload_len > len) return -EMSGSIZE; /* calculate checksum */ for (i = 0; i < 3+payload_len; i++) xor ^= buf[i]; /* verify checksum */ if (xor != 0) { dev_err(&client->dev, "invalid DDC/CI response, corrupted data - xor is 0x%02x, length 0x%02x\n", xor, payload_len); ret = -EBADMSG; goto out_err; } /* return result */ ret = payload_len+3+((quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE)?1:0); out_err: return ret; } /* * Read a response from the DDC/CI bus * * You must hold the bus semaphore when calling this function. */ static int ddcci_read(struct i2c_client *client, unsigned char addr, bool p_flag, unsigned char *buf, unsigned char len) { struct ddcci_bus_drv_data *drv_data; unsigned char *recvbuf; int ret; drv_data = i2c_get_clientdata(client); recvbuf = drv_data->recv_buffer; /* Read frame */ ret = __ddcci_read(client, addr, p_flag, drv_data->quirks, recvbuf, DDCCI_RECV_BUFFER_SIZE); if (ret < 0) return ret; if (drv_data->quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE) recvbuf++; /* return result */ if (buf) { if (ret > 3) { ret = ret-3; /* copy to caller buffer */ memcpy(buf, &recvbuf[2], (ret < len) ? ret : len); if (ret > len) { /* if message was truncated, return -EMSGSIZE */ pr_debug("received from %d:%02x:%02x: [%u/%u] %*ph ...\n", client->adapter->nr, client->addr << 1, addr, ret, len, len, buf); ret = -EMSGSIZE; } else { pr_debug("received from %d:%02x:%02x: [%u/%u] %*ph\n", client->adapter->nr, client->addr << 1, addr, ret, len, ret, buf); } } } if (!(drv_data->quirks & DDCCI_QUIRK_WRITE_BYTEWISE)) { /* second read to clear buffers, needed on some devices */ __ddcci_read(client, addr, true, drv_data->quirks, recvbuf, 1); } return ret; } /* Request the capability string for a device and put it into buf */ static int ddcci_get_caps(struct i2c_client *client, unsigned char addr, unsigned char *buf, unsigned int len) { int result = 0, counter = 0, offset = 0; unsigned char cmd[3] = { DDCCI_COMMAND_CAPS, 0x00, 0x00}; unsigned char *chunkbuf = kzalloc(35, GFP_KERNEL); if (!chunkbuf) return -ENOMEM; do { /* Send command */ result = ddcci_write(client, addr, true, cmd, sizeof(cmd)); if (result < 0) goto err_free; msleep(delay); /* read result chunk */ result = ddcci_read(client, addr, true, chunkbuf, (len > 32) ? 35 : len+3); if (result < 0) goto err_free; if (result > 0) { /* check chunk header */ if (chunkbuf[0] != DDCCI_REPLY_CAPS) { result = -EIO; goto err_free; } if (chunkbuf[1] != cmd[1] || chunkbuf[2] != cmd[2]) { result = -EIO; goto err_free; } memcpy(buf, chunkbuf+3, result-3); counter++; /* adjust offset, etc. */ offset += result-3; len -= result-3; buf += result-3; cmd[1] = offset >> 8; cmd[2] = offset & 0xFF; /* Another superfluous read to make some devices happy... */ ddcci_read(client, addr, true, NULL, 2); } } while (result > 3 && counter < 200); kfree(chunkbuf); return offset+result-3; err_free: kfree(chunkbuf); return result; } /* * Request the device identification and put it into buf. * * Also detects all communication quirks and sets the corresponding flags * in the ddcci_bus_drv_data structure associated with client. * * The identification command will fail on most DDC devices, as it is optional * to support, but even the "failed" response suffices to detect quirks. */ static int ddcci_identify_device(struct i2c_client *client, unsigned char addr, unsigned char *buf, unsigned char len) { int i, payload_len, ret = -ENODEV; unsigned long quirks; unsigned char cmd[2] = { DDCCI_COMMAND_ID, 0x00 }; unsigned char *buffer; unsigned char xor = DDCCI_HOST_ADDR_EVEN; struct ddcci_bus_drv_data *bus_drv_data; bus_drv_data = i2c_get_clientdata(client); quirks = bus_drv_data->quirks; buffer = bus_drv_data->recv_buffer; /* Send Identification command */ if (!(quirks & DDCCI_QUIRK_WRITE_BYTEWISE)) { ret = __ddcci_write_block(client, addr, buffer, true, cmd, 1); if ((ret == -ENXIO) && i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) { quirks |= DDCCI_QUIRK_WRITE_BYTEWISE; dev_dbg(&client->dev, "DDC/CI bus quirk detected: writes must be done bytewise\n"); /* Some devices need writing twice after a failed blockwise write */ __ddcci_write_bytewise(client, addr, true, cmd, 2); msleep(delay); } } if (ret < 0 && (quirks & DDCCI_QUIRK_WRITE_BYTEWISE)) ret = __ddcci_write_bytewise(client, addr, true, cmd, 2); if (ret < 0) return -ENODEV; /* Wait */ msleep(delay); /* Receive response */ ret = i2c_master_recv(client, buffer, DDCCI_RECV_BUFFER_SIZE); if (ret < 3) return -ENODEV; /* Skip first byte if quirk already active */ if (quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE) { ret--; buffer++; } /* If answer too short (= incomplete) break out */ if (ret < 3) return -EIO; /* validate first byte */ if (buffer[0] != addr) return -ENODEV; /* Check if first byte is doubled (QUIRK_SKIP_FIRST_BYTE) */ if (!(quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE)) { if (buffer[0] == buffer[1]) { quirks |= DDCCI_QUIRK_SKIP_FIRST_BYTE; dev_dbg(&client->dev, "DDC/CI bus quirk detected: doubled first byte on read\n"); ret--; buffer++; if (ret < 3) return -EIO; } } /* validate second byte (protocol flag) */ if ((buffer[1] & 0x80) != 0x80 && !(quirks & DDCCI_QUIRK_NO_PFLAG)) { dev_dbg(&client->dev, "DDC/CI bus quirk detected: device omits protocol flag on responses\n"); quirks |= DDCCI_QUIRK_NO_PFLAG; } /* get and check payload length */ payload_len = buffer[1] & 0x7F; if (3+payload_len > ret) return -EBADMSG; /* calculate checksum */ for (i = 0; i < 3+payload_len; i++) xor ^= buffer[i]; /* verify checksum */ if (xor != 0) { dev_err(&client->dev, "invalid DDC/CI response, corrupted data - xor is 0x%02x, length 0x%02x\n", xor, payload_len); return -EBADMSG; } /* save quirks */ bus_drv_data->quirks = quirks; /* return result */ if (payload_len <= len) { ret = payload_len; memcpy(buf, &buffer[2], payload_len); } else { ret = -EMSGSIZE; memcpy(buf, &buffer[2], len); } return ret; } /* Character device */ /* Data structure for an open file handle */ struct ddcci_fp_data { struct ddcci_device *dev; bool exclusive; unsigned char buffer[129]; }; /* Called when the character device is opened */ static int ddcci_cdev_open(struct inode *inode, struct file *filp) { struct ddcci_device *dev = container_of(inode->i_cdev, struct ddcci_device, cdev); struct ddcci_fp_data *fp_data = NULL; fp_data = kzalloc(sizeof(struct ddcci_fp_data), GFP_KERNEL); if (!fp_data) return -ENOMEM; fp_data->exclusive = filp->f_flags & O_EXCL; if (fp_data->exclusive) { if (down_write_trylock(&dev->cdev_sem) == 0) { kfree(fp_data); return -EBUSY; } } else { if (down_read_trylock(&dev->cdev_sem) == 0) { kfree(fp_data); return -EBUSY; } } fp_data->dev = dev; filp->private_data = fp_data; return 0; } /* Called when the character device is closed */ static int ddcci_cdev_close(struct inode *inode, struct file *filp) { struct ddcci_fp_data *fp_data = filp->private_data; struct ddcci_device *dev = fp_data->dev; if (fp_data->exclusive) up_write(&dev->cdev_sem); else up_read(&dev->cdev_sem); filp->private_data = NULL; kfree(fp_data); return 0; } /* Called when reading from the character device */ static ssize_t ddcci_cdev_read(struct file *filp, char __user *buffer, size_t length, loff_t *offset) { struct ddcci_fp_data *fp_data = filp->private_data; struct ddcci_device *dev = fp_data->dev; unsigned char *buf = fp_data->buffer; const bool nonblocking = (filp->f_flags & O_NONBLOCK) != 0; int ret = 0; if ((filp->f_mode & FMODE_READ) == 0) return -EBADF; /* Lock mutex */ if (nonblocking) { if (down_trylock(&dev->bus_drv_data->sem)) return -EAGAIN; } else { if (down_interruptible(&dev->bus_drv_data->sem)) return -ERESTARTSYS; } /* Execute read */ ret = ddcci_read(dev->bus_drv_data->i2c_dev, dev->inner_addr, true, buf, length); if (ret > 0) { /* Copy data from user space */ if (copy_to_user(buffer, buf, ret)) { ret = -EFAULT; goto out; } } out: up(&dev->bus_drv_data->sem); return ret; } /* Called when writing to the character device */ static ssize_t ddcci_cdev_write(struct file *filp, const char __user *buffer, size_t count, loff_t *offset) { struct ddcci_fp_data *fp_data = filp->private_data; struct ddcci_device *dev = fp_data->dev; unsigned char *buf = fp_data->buffer; const bool nonblocking = (filp->f_flags & O_NONBLOCK) != 0; int ret = 0; if ((filp->f_mode & FMODE_WRITE) == 0) return -EBADF; if (count > 127) return -EINVAL; /* Lock mutex */ if (nonblocking) { if (down_trylock(&dev->bus_drv_data->sem)) return -EAGAIN; } else { if (down_interruptible(&dev->bus_drv_data->sem)) return -ERESTARTSYS; } if (count > 0) { /* Copy data from user space */ if (copy_from_user(buf, buffer, count)) { ret = -EFAULT; goto err_out; } /* Execute write */ ret = ddcci_write(dev->bus_drv_data->i2c_dev, dev->inner_addr, true, buf, count); } if (ret >= 0) { msleep(delay); up(&dev->bus_drv_data->sem); return count; } err_out: up(&dev->bus_drv_data->sem); return ret; } /* Called when seeking the character device */ static loff_t ddcci_cdev_seek(struct file *filp, loff_t offset, int anchor) { return -EINVAL; } static const struct file_operations ddcci_fops = { .owner = THIS_MODULE, .read = ddcci_cdev_read, .write = ddcci_cdev_write, .open = ddcci_cdev_open, .release = ddcci_cdev_close, .llseek = ddcci_cdev_seek }; /* Set up the character device for a DDC/CI device */ static int ddcci_setup_char_device(struct ddcci_device *device) { int ret = -EINVAL; /* Check if free minor exists */ if (ddcci_cdev_next == ddcci_cdev_end) { dev_err(&device->dev, "no free major/minor\n"); ret = -ENFILE; goto out; } /* Initialize rwsem */ init_rwsem(&device->cdev_sem); /* Initialize character device node */ cdev_init(&device->cdev, &ddcci_fops); device->cdev.owner = THIS_MODULE; /* Publish char device */ device->dev.devt = ddcci_cdev_next; ret = cdev_add(&device->cdev, ddcci_cdev_next, 1); if (ret) { device->dev.devt = 0; goto out; } ddcci_cdev_next++; out: return ret; } /* sysfs attributes */ static ssize_t ddcci_attr_capabilities_show(struct device *dev, struct device_attribute *attr, char *buf) { struct ddcci_device *device = ddcci_verify_device(dev); ssize_t ret = -ENOENT; size_t len; if (likely(device != NULL)) { len = device->capabilities_len; if (unlikely(len > PAGE_SIZE)) len = PAGE_SIZE; if (len == 0) { ret = len; } else { memcpy(buf, device->capabilities, len); if (likely(len < PAGE_SIZE)) { buf[len] = '\n'; ret = len+1; } } } return ret; } static ssize_t ddcci_attr_prot_show(struct device *dev, struct device_attribute *attr, char *buf) { struct ddcci_device *device = ddcci_verify_device(dev); ssize_t ret = -ENOENT; size_t len; if (likely(device != NULL)) { len = strlen(device->prot); strncpy(buf, device->prot, PAGE_SIZE); if (len == 0) { ret = len; } else if (likely(len < PAGE_SIZE)) { buf[len] = '\n'; ret = len+1; } else { ret = PAGE_SIZE; } } return ret; } static ssize_t ddcci_attr_type_show(struct device *dev, struct device_attribute *attr, char *buf) { struct ddcci_device *device = ddcci_verify_device(dev); ssize_t ret = -ENOENT; size_t len; if (likely(device != NULL)) { len = strlen(device->type); strncpy(buf, device->type, PAGE_SIZE); if (len == 0) { ret = len; } else if (likely(len < PAGE_SIZE)) { buf[len] = '\n'; ret = len+1; } else { ret = PAGE_SIZE; } } return ret; } static ssize_t ddcci_attr_model_show(struct device *dev, struct device_attribute *attr, char *buf) { struct ddcci_device *device = ddcci_verify_device(dev); ssize_t ret = -ENOENT; size_t len; if (likely(device != NULL)) { len = strlen(device->model); strncpy(buf, device->model, PAGE_SIZE); if (len == 0) { ret = len; } else if (likely(len < PAGE_SIZE)) { buf[len] = '\n'; ret = len+1; } else { ret = PAGE_SIZE; } } return ret; } static ssize_t ddcci_attr_vendor_show(struct device *dev, struct device_attribute *attr, char *buf) { struct ddcci_device *device = ddcci_verify_device(dev); ssize_t ret = -ENOENT; size_t len; if (likely(device != NULL)) { len = strlen(device->vendor); strncpy(buf, device->vendor, PAGE_SIZE); if (len == 0) { ret = len; } else if (likely(len < PAGE_SIZE)) { buf[len] = '\n'; ret = len+1; } else { ret = PAGE_SIZE; } } return ret; } static ssize_t ddcci_attr_module_show(struct device *dev, struct device_attribute *attr, char *buf) { struct ddcci_device *device = ddcci_verify_device(dev); ssize_t ret = -ENOENT; size_t len; if (likely(device != NULL)) { len = strlen(device->module); strncpy(buf, device->module, PAGE_SIZE); if (len == 0) { ret = len; } else if (likely(len < PAGE_SIZE)) { buf[len] = '\n'; ret = len+1; } else { ret = PAGE_SIZE; } } return ret; } static ssize_t ddcci_attr_serial_show(struct device *dev, struct device_attribute *attr, char *buf) { struct ddcci_device *device = ddcci_verify_device(dev); ssize_t ret = -ENOENT; if (likely(device != NULL)) ret = scnprintf(buf, PAGE_SIZE, "%d\n", device->device_number); return ret; } static DEVICE_ATTR(capabilities, S_IRUGO, ddcci_attr_capabilities_show, NULL); static DEVICE_ATTR(idProt, S_IRUGO, ddcci_attr_prot_show, NULL); static DEVICE_ATTR(idType, S_IRUGO, ddcci_attr_type_show, NULL); static DEVICE_ATTR(idModel, S_IRUGO, ddcci_attr_model_show, NULL); static DEVICE_ATTR(idVendor, S_IRUGO, ddcci_attr_vendor_show, NULL); static DEVICE_ATTR(idModule, S_IRUGO, ddcci_attr_module_show, NULL); static DEVICE_ATTR(idSerial, S_IRUGO, ddcci_attr_serial_show, NULL); static struct attribute *ddcci_char_device_attrs[] = { &dev_attr_capabilities.attr, &dev_attr_idProt.attr, &dev_attr_idType.attr, &dev_attr_idModel.attr, &dev_attr_idVendor.attr, &dev_attr_idModule.attr, &dev_attr_idSerial.attr, NULL, }; ATTRIBUTE_GROUPS(ddcci_char_device); /* DDC/CI bus */ static int ddcci_device_uevent(struct device *dev, struct kobj_uevent_env *env) { struct ddcci_device *device = to_ddcci_device(dev); if (add_uevent_var(env, "MODALIAS=%s%s-%s-%s-%s-%s", DDCCI_MODULE_PREFIX, device->prot, device->type, device->model, device->vendor, device->module )) return -ENOMEM; if (device->prot[0]) if (add_uevent_var(env, "DDCCI_PROT=%s", device->prot)) return -ENOMEM; if (device->type[0]) if (add_uevent_var(env, "DDCCI_TYPE=%s", device->type)) return -ENOMEM; if (device->model[0]) if (add_uevent_var(env, "DDCCI_MODEL=%s", device->model)) return -ENOMEM; if (device->vendor[0]) { if (add_uevent_var(env, "DDCCI_VENDOR=%s", device->vendor)) return -ENOMEM; if (add_uevent_var(env, "DDCCI_MODULE=%s", device->module)) return -ENOMEM; if (add_uevent_var(env, "DDCCI_UNIQ=%d", device->device_number)) return -ENOMEM; } return 0; } static void ddcci_device_release(struct device *dev) { struct ddcci_device *device = to_ddcci_device(dev); struct ddcci_driver *driver; /* Notify driver */ if (dev->driver) { driver = to_ddcci_driver(dev->driver); if (driver->remove) driver->remove(device); } /* Teardown chardev */ if (dev->devt) { down(&core_lock); if (device->cdev.dev == ddcci_cdev_next-1) ddcci_cdev_next--; cdev_del(&device->cdev); up(&core_lock); } /* Free capability string */ if (device->capabilities) { device->capabilities_len = 0; kfree(device->capabilities); } /* Free device */ kfree(device); } static char *ddcci_devnode(struct device *dev, umode_t *mode, kuid_t *uid, kgid_t *gid) { struct ddcci_device *device; device = to_ddcci_device(dev); return kasprintf(GFP_KERNEL, "bus/ddcci/%d/display", device->i2c_client->adapter->nr); } static char *ddcci_dependent_devnode(struct device *dev, umode_t *mode, kuid_t *uid, kgid_t *gid) { struct ddcci_device *device; device = to_ddcci_device(dev); if (device->flags & DDCCI_FLAG_EXTERNAL) { if (device->outer_addr == device->inner_addr) return kasprintf(GFP_KERNEL, "bus/ddcci/%d/e%02x", device->i2c_client->adapter->nr, device->outer_addr); else return kasprintf(GFP_KERNEL, "bus/ddcci/%d/e%02x%02x", device->i2c_client->adapter->nr, device->outer_addr, device->inner_addr); } else { return kasprintf(GFP_KERNEL, "bus/ddcci/%d/i%02x", device->i2c_client->adapter->nr, device->inner_addr); } } /* Device type for main DDC/CI devices*/ static struct device_type ddcci_device_type = { .name = "ddcci-device", .uevent = ddcci_device_uevent, .groups = ddcci_char_device_groups, .release = ddcci_device_release, .devnode = ddcci_devnode }; /* Device type for dependent DDC/CI devices*/ static struct device_type ddcci_dependent_type = { .name = "ddcci-dependent-device", .uevent = ddcci_device_uevent, .groups = ddcci_char_device_groups, .release = ddcci_device_release, .devnode = ddcci_dependent_devnode }; /** * ddcci_verify_device - return parameter as ddcci_device, or NULL * @dev: device, probably from some driver model iterator */ struct ddcci_device *ddcci_verify_device(struct device *dev) { if (unlikely(!dev)) return NULL; return (dev->type == &ddcci_device_type || dev->type == &ddcci_dependent_type) ? to_ddcci_device(dev) : NULL; } EXPORT_SYMBOL(ddcci_verify_device); /** * ddcci_quirks - Get quirks for DDC/CI device * @dev: Target DDC/CI device */ unsigned long ddcci_quirks(struct ddcci_device *dev) { if (unlikely(WARN_ON(!dev))) return ~0L; if (unlikely(WARN_ON(!dev->bus_drv_data))) return ~0L; return dev->bus_drv_data->quirks; } EXPORT_SYMBOL(ddcci_quirks); /** * ddcci_register_driver - register DDC/CI driver * @owner: the owning module * @driver: the driver to register */ int ddcci_register_driver(struct module *owner, struct ddcci_driver *driver) { int ret; /* Can't register until after driver model init */ if (unlikely(WARN_ON(!ddcci_bus_type.p))) return -EAGAIN; pr_debug("registering driver [%s]\n", driver->driver.name); /* add the driver to the list of ddcci drivers in the driver core */ driver->driver.owner = owner; driver->driver.bus = &ddcci_bus_type; /* When registration returns, the driver core * will have called probe() for all matching-but-unbound devices. */ ret = driver_register(&driver->driver); if (ret) return ret; pr_debug("driver [%s] registered\n", driver->driver.name); return 0; } EXPORT_SYMBOL(ddcci_register_driver); /** * ddcci_del_driver - unregister DDC/CI driver * @driver: the driver being unregistered */ void ddcci_del_driver(struct ddcci_driver *driver) { driver_unregister(&driver->driver); pr_debug("driver [%s] unregistered\n", driver->driver.name); } EXPORT_SYMBOL(ddcci_del_driver); /** * ddcci_device_write - Write a message to a DDC/CI device * @dev: Target DDC/CI device * @p_flag: Protocol flag, true for standard control messages * @data: Data that will be written to the device * @length: How many bytes to write * * Writes the message to the device and sleeps (see module parameter 'delay') */ int ddcci_device_write(struct ddcci_device *dev, bool p_flag, unsigned char *data, unsigned char length) { int ret; if (down_interruptible(&dev->bus_drv_data->sem)) return -EAGAIN; ret = ddcci_write(dev->bus_drv_data->i2c_dev, dev->inner_addr, p_flag, data, length); msleep(delay); up(&dev->bus_drv_data->sem); return ret; } EXPORT_SYMBOL(ddcci_device_write); /** * ddcci_device_read - Read a response from a DDC/CI device * @dev: Target DDC/CI device * @p_flag: Protocol flag, must match the corresponding write * @buffer: Where to store data read from the device * @length: Buffer size */ int ddcci_device_read(struct ddcci_device *dev, bool p_flag, unsigned char *buffer, unsigned char length) { int ret; if (down_interruptible(&dev->bus_drv_data->sem)) return -EAGAIN; ret = ddcci_read(dev->bus_drv_data->i2c_dev, dev->inner_addr, p_flag, buffer, length); up(&dev->bus_drv_data->sem); return ret; } EXPORT_SYMBOL(ddcci_device_read); /** * ddcci_device_writeread - Write a message to a device and read the response * @dev: Target DDC/CI device * @p_flag: Protocol flag, true for standard control messages * @buffer: Buffer used for write and read * @length: How many bytes to write * @maxlength: Buffer size on read * * Writing, sleeping and reading are done without releasing the DDC/CI bus. * This provides atomicity in respect to other DDC/CI accesses on the same bus. */ int ddcci_device_writeread(struct ddcci_device *dev, bool p_flag, unsigned char *buffer, unsigned char length, unsigned char maxlength) { int ret; if (down_interruptible(&dev->bus_drv_data->sem)) return -EAGAIN; ret = ddcci_write(dev->bus_drv_data->i2c_dev, dev->inner_addr, p_flag, buffer, length); if (ret < 0) goto err; msleep(delay); ret = ddcci_read(dev->bus_drv_data->i2c_dev, dev->inner_addr, p_flag, buffer, maxlength); err: up(&dev->bus_drv_data->sem); return ret; } EXPORT_SYMBOL(ddcci_device_writeread); #define IS_ANY_ID(x) (((x)[0] == -1) && ((x)[7] == -1)) /* Check if any device id in the array matches the device and return the matching id */ static const struct ddcci_device_id *ddcci_match_id(const struct ddcci_device_id *id, const struct ddcci_device *device) { while (id->prot[0] || id->type[0] || id->model[0] || id->vendor[0] || id->module[0]) { if ((IS_ANY_ID(id->prot) || (strcmp(device->prot, id->prot) == 0)) && (IS_ANY_ID(id->type) || (strcmp(device->type, id->type) == 0)) && (IS_ANY_ID(id->model) || (strcmp(device->model, id->model) == 0)) && (IS_ANY_ID(id->vendor) || (strcmp(device->vendor, id->vendor) == 0)) && (IS_ANY_ID(id->module) || (strcmp(device->module, id->module) == 0))) { return id; } id++; } return NULL; } static int ddcci_device_match(struct device *dev, struct device_driver *drv) { struct ddcci_device *device = ddcci_verify_device(dev); struct ddcci_driver *driver; if (!device) return 0; driver = to_ddcci_driver(drv); /* match on an id table if there is one */ if (driver->id_table) return ddcci_match_id(driver->id_table, device) != NULL; return 0; } static int ddcci_device_probe(struct device *dev) { struct ddcci_device *device = ddcci_verify_device(dev); struct ddcci_driver *driver; const struct ddcci_device_id *id; int ret = 0; if (!device) return -EINVAL; driver = to_ddcci_driver(dev->driver); id = ddcci_match_id(driver->id_table, device); if (!id) return -ENODEV; if (driver->probe) ret = driver->probe(device, id); return ret; } static int ddcci_device_remove(struct device *dev) { struct ddcci_device *device = ddcci_verify_device(dev); struct ddcci_driver *driver; int ret = 0; if (!device) return -EINVAL; driver = to_ddcci_driver(dev->driver); if (driver->remove) ret = driver->remove(device); return ret; } /** * DDCCI bus type structure */ struct bus_type ddcci_bus_type = { .name = "ddcci", .match = ddcci_device_match, .probe = ddcci_device_probe, .remove = ddcci_device_remove }; /* Main I2C driver */ /* Get a pointer to the closing parenthesis */ static char *ddcci_capstr_tok(const char *s, int depth) { const char *ptr = s; char *end; if (s == NULL || s[0] == '\0') return NULL; while ((end = strpbrk(ptr, "()"))) { if (!end || depth == INT_MAX) return NULL; if (*end == '(') depth++; else if (depth > 0) depth--; else break; ptr = end+1; } return end; } /** * ddcci_find_capstr_item - Search capability string for a tag * @capabilities: Capability string to search * @tag: Tag to find * @length: Buffer for the length of the found tag value (optional) * * Return a pointer to the start of the tag value (directly after the '(') on * success and write the length of the value (excluding the ')') into `length`. * * If the tag is not found or another error occurs, an ERR_PTR is returned * and `length` stays untouched. */ const char *ddcci_find_capstr_item(const char *capabilities, const char *tag, ptrdiff_t *length) { const char *src = capabilities, *ptr; ptrdiff_t len; int taglen = strlen(tag); /* Check length of requested tag */ if (unlikely(taglen <= 0 || taglen > 65535)) return ERR_PTR(-EINVAL); /* Find tag */ while (src && (strncmp(src+1, tag, taglen) != 0 || src[1+taglen] != '(')) src = ddcci_capstr_tok(src+1, -1); if (!src || src[0] == '\0') return ERR_PTR(-ENOENT); /* Locate end of value */ src += taglen+2; ptr = ddcci_capstr_tok(src, 0); if (unlikely(!ptr)) return ERR_PTR(-EOVERFLOW); /* Check length of tag data */ len = ptr-src; if (unlikely(len < 0 || len > 65535)) return ERR_PTR(-EMSGSIZE); /* Return pointer and length */ if (likely(length != NULL)) *length = len; return src; } EXPORT_SYMBOL(ddcci_find_capstr_item); /* Search the capability string for a tag and copy the value to dest */ static int ddcci_cpy_capstr_item(char *dest, const char *src, const char *tag, size_t maxlen) { const char *ptr; ptrdiff_t len; /* Find tag */ ptr = ddcci_find_capstr_item(src, tag, &len); if (IS_ERR(ptr)) { return PTR_ERR(ptr); } /* Copy value */ memcpy(dest, ptr, (len < maxlen) ? len : maxlen); return 0; } /* Fill fields in device by parsing the capability string */ static int ddcci_parse_capstring(struct ddcci_device *device) { const char *capstr = device->capabilities; int ret = 0; if (!capstr) return -EINVAL; /* capability string start with a paren */ if (capstr[0] != '(') return -EINVAL; /* get prot(...) */ ret = ddcci_cpy_capstr_item(device->prot, capstr, "prot", 8); if (ret) return ret; /* get type(...) */ ret = ddcci_cpy_capstr_item(device->type, capstr, "type", 8); if (ret) { if (ret == -ENOENT) { dev_warn(&device->dev, "malformed capability string: no type tag"); memset(device->type, 0, 8); } else { return ret; } } /* and then model(...) */ ret = ddcci_cpy_capstr_item(device->model, capstr, "model", 8); if (ret) { if (ret == -ENOENT) { dev_warn(&device->dev, "malformed capability string: no model tag"); memset(device->model, 0, 8); } else { return ret; } } /* skip the rest for now */ return 0; } /* Probe for a device on an inner address and create a ddcci_device for it */ static int ddcci_detect_device(struct i2c_client *client, unsigned char addr, int dependent) { int ret; unsigned char outer_addr = client->addr << 1; unsigned char *buffer = NULL; struct ddcci_bus_drv_data *drv_data = i2c_get_clientdata(client); struct ddcci_device *device = NULL; down(&drv_data->sem); /* Allocate buffer big enough for any capability string */ buffer = kmalloc(16384, GFP_KERNEL); if (!buffer) { ret = -ENOMEM; goto end; } /* Allocate device struct */ device = kzalloc(sizeof(struct ddcci_device), GFP_KERNEL); if (!buffer) { ret = -ENOMEM; goto end; } /* Initialize device */ device_initialize(&device->dev); device->dev.parent = &client->dev; device->dev.bus = &ddcci_bus_type; device->outer_addr = outer_addr; device->inner_addr = addr; device->bus_drv_data = drv_data; device->i2c_client = client; if (!dependent) { device->dev.type = &ddcci_device_type; ret = dev_set_name(&device->dev, "ddcci%d", client->adapter->nr); } else if (outer_addr == dependent) { /* Internal dependent device */ device->dev.type = &ddcci_dependent_type; device->flags = DDCCI_FLAG_DEPENDENT; ret = dev_set_name(&device->dev, "ddcci%di%02x", client->adapter->nr, addr); } else if (outer_addr == addr) { /* External dependent device */ device->dev.type = &ddcci_dependent_type; device->flags = DDCCI_FLAG_DEPENDENT | DDCCI_FLAG_EXTERNAL; ret = dev_set_name(&device->dev, "ddcci%de%02x", client->adapter->nr, addr); } else { /* Dependent device of external dependent device Just in case something like this exists */ device->dev.type = &ddcci_dependent_type; device->flags = DDCCI_FLAG_DEPENDENT | DDCCI_FLAG_EXTERNAL; ret = dev_set_name(&device->dev, "ddcci%de%02x%02x", client->adapter->nr, outer_addr, addr); } if (ret) goto err_free; /* Read identification and check for quirks */ ret = ddcci_identify_device(client, addr, buffer, 29); if (ret < 0) goto err_free; if (ret == 29 && buffer[0] == DDCCI_REPLY_ID) { memcpy(device->vendor, &buffer[7], 8); memcpy(device->module, &buffer[17], 8); device->device_number = be32_to_cpu(*(__force __be32 *)&buffer[18]); } /* Read capabilities */ ret = ddcci_get_caps(client, addr, buffer, 16384); if (ret > 0) { device->capabilities = kzalloc(ret+1, GFP_KERNEL); if (!device->capabilities) { ret = -ENOMEM; goto err_free; } device->capabilities_len = ret; memcpy(device->capabilities, buffer, ret); ret = ddcci_parse_capstring(device); if (ret) { dev_err(&device->dev, "malformed capability string: %d", ret); ret = -EINVAL; goto err_free; } } /* Found a device if either identification or capabilities succeeded */ if (!device->capabilities && device->vendor[0] == '\0') { ret = -ENODEV; goto err_free; } /* Setup chardev */ down(&core_lock); ret = ddcci_setup_char_device(device); if (ret) goto err_free; /* Add device */ pr_debug("found device at %d:%02x:%02x\n", client->adapter->nr, outer_addr, addr); ret = device_add(&device->dev); if (ret) goto err_free; goto end; err_free: put_device(&device->dev); end: kfree(buffer); up(&drv_data->sem); up(&core_lock); return ret; } /* I2C detect function: check if a main or external dependent device exists */ static int ddcci_detect(struct i2c_client *client, struct i2c_board_info *info) { int ret; unsigned char outer_addr; unsigned char inner_addr; unsigned char buf[32]; unsigned char cmd[2] = { DDCCI_COMMAND_ID, 0x00 }; /* Check for i2c_master_* functionality */ if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { pr_debug("i2c adapter %d unsuitable: no i2c_master functionality\n", client->adapter->nr); return -ENODEV; } /* send Identification Request command */ outer_addr = client->addr << 1; inner_addr = (outer_addr == DDCCI_DEFAULT_DEVICE_ADDR) ? DDCCI_HOST_ADDR_ODD : outer_addr | 1; pr_debug("detecting %d:%02x\n", client->adapter->nr, outer_addr); ret = __ddcci_write_block(client, inner_addr, buf, true, cmd, 2); if (ret == -ENXIO) { if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) return -ENODEV; pr_debug("i2c write failed with ENXIO, trying bytewise writing\n"); ret = __ddcci_write_bytewise(client, inner_addr, true, cmd, 2); if (ret == 0) { msleep(delay); ret = __ddcci_write_bytewise(client, inner_addr, true, cmd, 2); } } if (ret < 0) return -ENODEV; /* wait for device */ msleep(delay); /* receive answer */ ret = i2c_master_recv(client, buf, 32); if (ret < 3) { pr_debug("detection failed: no answer\n"); return -ENODEV; } /* check response starts with outer addr */ if (buf[0] != outer_addr) { pr_debug("detection failed: invalid answer\n"); return -ENODEV; } pr_debug("detected %d:%02x\n", client->adapter->nr, outer_addr); /* set device type */ strlcpy(info->type, (outer_addr == DDCCI_DEFAULT_DEVICE_ADDR) ? "ddcci" : "ddcci-dependent", I2C_NAME_SIZE); return 0; } /* I2C probe function */ static int ddcci_probe(struct i2c_client *client, const struct i2c_device_id *id) { int i, ret = -ENODEV, tmp; unsigned char main_addr, addr; struct ddcci_bus_drv_data *drv_data; /* Initialize driver data structure */ drv_data = devm_kzalloc(&client->dev, sizeof(struct ddcci_bus_drv_data), GFP_KERNEL); if (!drv_data) return -ENOMEM; drv_data->i2c_dev = client; sema_init(&drv_data->sem, 1); /* Set i2c client data */ i2c_set_clientdata(client, drv_data); if (id->driver_data == 0) { /* Core device, probe at 0x6E */ main_addr = DDCCI_DEFAULT_DEVICE_ADDR; dev_dbg(&client->dev, "probing core device [%02x]\n", client->addr << 1); ret = ddcci_detect_device(client, main_addr, 0); if (ret) { dev_info(&client->dev, "core device [%02x] probe failed: %d\n", client->addr << 1, ret); if (ret == -EIO) ret = -ENODEV; goto err_free; } /* Detect internal dependent devices */ dev_dbg(&client->dev, "probing internal dependent devices\n"); for (i = 0; i < autoprobe_addr_count; ++i) { addr = (unsigned short)autoprobe_addrs[i]; if ((addr & 1) == 0 && addr != main_addr) { tmp = ddcci_detect_device(client, addr, main_addr); if (tmp < 0 && tmp != -ENODEV) { dev_info(&client->dev, "internal dependent device [%02x:%02x] probe failed: %d\n", client->addr << 1, addr, ret); } } } } else if (id->driver_data == 1) { /* External dependent device */ main_addr = client->addr << 1; dev_dbg(&client->dev, "probing external dependent device [%02x]\n", main_addr); ret = ddcci_detect_device(client, main_addr, -1); if (ret) { dev_info(&client->dev, "external dependent device [%02x] probe failed: %d\n", main_addr, ret); if (ret == -EIO) ret = -ENODEV; goto err_free; } } else { dev_warn(&client->dev, "probe() called with invalid i2c device id\n"); ret = -EINVAL; } goto end; err_free: devm_kfree(&client->dev, drv_data); end: return ret; } /* * Callback for bus_find_device() used in ddcci_remove() * * Find next device with matching outer address not flagged with * DDCCI_FLAG_REMOVED and flag it. */ static int ddcci_remove_helper(struct device *dev, void *p) { unsigned char outer_addr; struct ddcci_device *device; if (p) outer_addr = *(unsigned char *)p; else outer_addr = 0; device = ddcci_verify_device(dev); if (!device || device->flags & DDCCI_FLAG_REMOVED) return 0; if (!outer_addr || (device->outer_addr == outer_addr)) { device->flags |= DDCCI_FLAG_REMOVED; wmb(); return 1; } return 0; } /* I2C driver remove callback: unregister all subdevices */ static int ddcci_remove(struct i2c_client *client) { struct ddcci_bus_drv_data *drv_data = i2c_get_clientdata(client); struct device *dev; unsigned char outer_addr = client->addr << 1; down(&drv_data->sem); while (1) { dev = bus_find_device(&ddcci_bus_type, NULL, &outer_addr, ddcci_remove_helper); if (!dev) break; device_unregister(dev); put_device(dev); } up(&drv_data->sem); return 0; } /* * I2C driver device identification table. */ static const struct i2c_device_id ddcci_idtable[] = { { "ddcci", 0 }, { "ddcci-dependent", 1 }, {} }; MODULE_DEVICE_TABLE(i2c, ddcci_idtable); /* * I2C driver description structure */ static struct i2c_driver ddcci_driver = { .driver = { .name = "ddcci", .owner = THIS_MODULE, }, .id_table = ddcci_idtable, .probe = ddcci_probe, .remove = ddcci_remove, .class = I2C_CLASS_DDC, .detect = ddcci_detect, .address_list = I2C_ADDRS( DDCCI_DEFAULT_DEVICE_ADDR>>1 ), }; /* * Module initialization function. Called when the module is inserted or * (if builtin) at boot time. */ static int __init ddcci_module_init(void) { int ret; pr_debug("initializing ddcci driver\n"); /* Allocate a device number region for the character devices */ ret = alloc_chrdev_region(&ddcci_cdev_first, 0, 128, DEVICE_NAME); if (ret < 0) { pr_err("failed to register device region: error %d\n", ret); goto err_chrdevreg; } ddcci_cdev_next = ddcci_cdev_first; ddcci_cdev_end = MKDEV(MAJOR(ddcci_cdev_first), MINOR(ddcci_cdev_first)+128); /* Register bus */ ret = bus_register(&ddcci_bus_type); if (ret) { pr_err("failed to register bus 'ddcci'\n"); goto err_busreg; } /* Register I2C driver */ ret = i2c_add_driver(&ddcci_driver); if (ret) { pr_err("failed to register i2c driver\n"); goto err_drvreg; } pr_debug("ddcci driver initialized\n"); return 0; err_drvreg: bus_unregister(&ddcci_bus_type); err_busreg: unregister_chrdev_region(ddcci_cdev_first, 128); err_chrdevreg: return ret; } /* * Module clean-up function. Called when the module is removed. */ static void __exit ddcci_module_exit(void) { struct device *dev; while (1) { dev = bus_find_device(&ddcci_bus_type, NULL, NULL, ddcci_remove_helper); if (!dev) break; device_unregister(dev); put_device(dev); } i2c_del_driver(&ddcci_driver); bus_unregister(&ddcci_bus_type); unregister_chrdev_region(ddcci_cdev_first, 128); } /* Let the kernel know the calls for module init and exit */ module_init(ddcci_module_init); module_exit(ddcci_module_exit); /* Module parameter description */ module_param(delay, uint, S_IRUGO|S_IWUSR); MODULE_PARM_DESC(delay, "default delay after bus writes (in ms, default 60)"); module_param_array(autoprobe_addrs, ushort, &autoprobe_addr_count, S_IRUGO|S_IWUSR); MODULE_PARM_DESC(autoprobe_addrs, "internal dependent device addresses to autoprobe"); /* Module description */ MODULE_AUTHOR("Christoph Grenz"); MODULE_DESCRIPTION("DDC/CI bus driver"); MODULE_VERSION("0.3.1"); MODULE_LICENSE("GPL"); ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/dkms.conf000066400000000000000000000005761302646274100245550ustar00rootroot00000000000000PACKAGE_VERSION="0.3.1" PACKAGE_NAME="ddcci" CLEAN="make clean" BUILT_MODULE_NAME[0]="ddcci" BUILT_MODULE_NAME[1]="ddcci-backlight" BUILT_MODULE_LOCATION[0]="ddcci/" BUILT_MODULE_LOCATION[1]="ddcci-backlight/" DEST_MODULE_LOCATION[0]="/extra" DEST_MODULE_LOCATION[1]="/extra" MAKE[0]="make KVER=$kernelver -C ${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build" AUTOINSTALL="yes" ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/include/000077500000000000000000000000001302646274100243635ustar00rootroot00000000000000ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/include/linux/000077500000000000000000000000001302646274100255225ustar00rootroot00000000000000ddcci-driver-linux-v0.3.1-2532dc008c43c9a08f7d08c8350aa60b49eb1f42/include/linux/ddcci.h000066400000000000000000000115651302646274100267510ustar00rootroot00000000000000/* * DDC/CI bus driver * * Copyright (c) 2015 Christoph Grenz */ /* * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. */ #ifndef _DDCCI_H #define _DDCCI_H #include #include #include #define DDCCI_MODULE_PREFIX "ddcci:" /* Special addresses */ /* default device address (even) */ #define DDCCI_DEFAULT_DEVICE_ADDR 0x6E /* receiving host address for communication with default device address */ #define DDCCI_HOST_ADDR_EVEN 0x50 /* sending host address for communication with default device address */ #define DDCCI_HOST_ADDR_ODD 0x51 /* Command codes */ /* Identification Request */ #define DDCCI_COMMAND_ID 0xf1 /* Identification Reply */ #define DDCCI_REPLY_ID 0xe1 /* Capabilities Request */ #define DDCCI_COMMAND_CAPS 0xf3 /* Capabilities Reply */ #define DDCCI_REPLY_CAPS 0xe3 /* Quirks */ /* Device always responds with unset protocol flag */ #define DDCCI_QUIRK_NO_PFLAG BIT(1) /* Device needs writing one byte at a time */ #define DDCCI_QUIRK_WRITE_BYTEWISE BIT(2) /* Device repeats first byte on read */ #define DDCCI_QUIRK_SKIP_FIRST_BYTE BIT(3) /* Flags */ #define DDCCI_FLAG_REMOVED BIT(1) #define DDCCI_FLAG_DEPENDENT BIT(2) #define DDCCI_FLAG_EXTERNAL BIT(3) extern struct bus_type ddcci_bus_type; struct ddcci_bus_drv_data; /* struct ddcci_device_id - identifies DDC/CI devices for probing */ struct ddcci_device_id { char prot[9]; char type[9]; char model[9]; char vendor[9]; char module[9]; kernel_ulong_t driver_data; /* Data private to the driver */ }; #define DDCCI_ANY_ID "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" /** * struct ddcci_device - represent an DDC/CI device * @outer_addr: Outer device address (I2C address << 1). * @inner_addr: Inner device address. * @flags: Device flags. * @capabilities: Device capability string. * @capabilities_len: Length of capability string. * @i2c_client: Parent I2C device. * @bus_drv_data: Driver internal data structure. * @dev: Driver model device node for the slave. * @cdev: Character device structure * @cdev_sem: RW semaphore for exclusive access on character device. * @prot: Device class ("protocol", from capability string) * @type: Device subclass ("type", from capability string) * @model: Device model (from capability string) * @vendor: Device vendor (from identification command response) * @module: Device module (from identification command response) * @device_number: Device serial (from identification command response) */ struct ddcci_device { unsigned short outer_addr; unsigned short inner_addr; int flags; char *capabilities; size_t capabilities_len; struct i2c_client *i2c_client; struct ddcci_bus_drv_data *bus_drv_data; struct device dev; struct cdev cdev; struct rw_semaphore cdev_sem; char prot[9]; char type[9]; char model[9]; char vendor[9]; char module[9]; int device_number; }; #define to_ddcci_device(d) container_of(d, struct ddcci_device, dev) /** * struct ddcci_driver - represent an DDC/CI device driver * @probe: Callback for device binding * @remove: Callback for device unbinding * @driver: Device driver model driver * @id_table: List of DDC/CI devices supported by this driver * * The driver.owner field should be set to the module owner of this driver. * The driver.name field should be set to the name of this driver. */ struct ddcci_driver { int (*probe)(struct ddcci_device *, const struct ddcci_device_id *); int (*remove)(struct ddcci_device *); struct device_driver driver; struct ddcci_device_id *id_table; }; #define to_ddcci_driver(d) container_of(d, struct ddcci_driver, driver) int ddcci_register_driver(struct module *owner, struct ddcci_driver *driver); #define ddcci_add_driver(driver) \ ddcci_register_driver(THIS_MODULE, driver) void ddcci_del_driver(struct ddcci_driver *driver); struct ddcci_device *ddcci_verify_device(struct device *dev); #define module_ddcci_driver(__ddcci_driver) \ module_driver(__ddcci_driver, ddcci_add_driver, \ ddcci_del_driver) int ddcci_device_write(struct ddcci_device *, bool p_flag, unsigned char *data, unsigned char length); int ddcci_device_read(struct ddcci_device *, bool p_flag, unsigned char *buffer, unsigned char length); int ddcci_device_writeread(struct ddcci_device *, bool p_flag, unsigned char *buffer, unsigned char length, unsigned char maxlength); static inline void *ddcci_get_drvdata(const struct ddcci_device *dev) { return dev_get_drvdata(&dev->dev); } static inline void ddcci_set_drvdata(struct ddcci_device *dev, void *data) { dev_set_drvdata(&dev->dev, data); } unsigned long ddcci_quirks(struct ddcci_device *dev); const char *ddcci_find_capstr_item(const char *capabilities, const char *tag, ptrdiff_t *length); #endif