18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Simple USB RGB LED driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2016 Heiner Kallweit <hkallweit1@gmail.com>
68c2ecf20Sopenharmony_ci * Based on drivers/hid/hid-thingm.c and
78c2ecf20Sopenharmony_ci * drivers/usb/misc/usbled.c
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/hid.h>
118c2ecf20Sopenharmony_ci#include <linux/hidraw.h>
128c2ecf20Sopenharmony_ci#include <linux/leds.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/mutex.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "hid-ids.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cienum hidled_report_type {
198c2ecf20Sopenharmony_ci	RAW_REQUEST,
208c2ecf20Sopenharmony_ci	OUTPUT_REPORT
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cienum hidled_type {
248c2ecf20Sopenharmony_ci	RISO_KAGAKU,
258c2ecf20Sopenharmony_ci	DREAM_CHEEKY,
268c2ecf20Sopenharmony_ci	THINGM,
278c2ecf20Sopenharmony_ci	DELCOM,
288c2ecf20Sopenharmony_ci	LUXAFOR,
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic unsigned const char riso_kagaku_tbl[] = {
328c2ecf20Sopenharmony_ci/* R+2G+4B -> riso kagaku color index */
338c2ecf20Sopenharmony_ci	[0] = 0, /* black   */
348c2ecf20Sopenharmony_ci	[1] = 2, /* red     */
358c2ecf20Sopenharmony_ci	[2] = 1, /* green   */
368c2ecf20Sopenharmony_ci	[3] = 5, /* yellow  */
378c2ecf20Sopenharmony_ci	[4] = 3, /* blue    */
388c2ecf20Sopenharmony_ci	[5] = 6, /* magenta */
398c2ecf20Sopenharmony_ci	[6] = 4, /* cyan    */
408c2ecf20Sopenharmony_ci	[7] = 7  /* white   */
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define RISO_KAGAKU_IX(r, g, b) riso_kagaku_tbl[((r)?1:0)+((g)?2:0)+((b)?4:0)]
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciunion delcom_packet {
468c2ecf20Sopenharmony_ci	__u8 data[8];
478c2ecf20Sopenharmony_ci	struct {
488c2ecf20Sopenharmony_ci		__u8 major_cmd;
498c2ecf20Sopenharmony_ci		__u8 minor_cmd;
508c2ecf20Sopenharmony_ci		__u8 data_lsb;
518c2ecf20Sopenharmony_ci		__u8 data_msb;
528c2ecf20Sopenharmony_ci	} tx;
538c2ecf20Sopenharmony_ci	struct {
548c2ecf20Sopenharmony_ci		__u8 cmd;
558c2ecf20Sopenharmony_ci	} rx;
568c2ecf20Sopenharmony_ci	struct {
578c2ecf20Sopenharmony_ci		__le16 family_code;
588c2ecf20Sopenharmony_ci		__le16 security_code;
598c2ecf20Sopenharmony_ci		__u8 fw_version;
608c2ecf20Sopenharmony_ci	} fw;
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define DELCOM_GREEN_LED	0
648c2ecf20Sopenharmony_ci#define DELCOM_RED_LED		1
658c2ecf20Sopenharmony_ci#define DELCOM_BLUE_LED		2
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistruct hidled_device;
688c2ecf20Sopenharmony_cistruct hidled_rgb;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct hidled_config {
718c2ecf20Sopenharmony_ci	enum hidled_type	type;
728c2ecf20Sopenharmony_ci	const char		*name;
738c2ecf20Sopenharmony_ci	const char		*short_name;
748c2ecf20Sopenharmony_ci	enum led_brightness	max_brightness;
758c2ecf20Sopenharmony_ci	int			num_leds;
768c2ecf20Sopenharmony_ci	size_t			report_size;
778c2ecf20Sopenharmony_ci	enum hidled_report_type	report_type;
788c2ecf20Sopenharmony_ci	int (*init)(struct hidled_device *ldev);
798c2ecf20Sopenharmony_ci	int (*write)(struct led_classdev *cdev, enum led_brightness br);
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistruct hidled_led {
838c2ecf20Sopenharmony_ci	struct led_classdev	cdev;
848c2ecf20Sopenharmony_ci	struct hidled_rgb	*rgb;
858c2ecf20Sopenharmony_ci	char			name[32];
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistruct hidled_rgb {
898c2ecf20Sopenharmony_ci	struct hidled_device	*ldev;
908c2ecf20Sopenharmony_ci	struct hidled_led	red;
918c2ecf20Sopenharmony_ci	struct hidled_led	green;
928c2ecf20Sopenharmony_ci	struct hidled_led	blue;
938c2ecf20Sopenharmony_ci	u8			num;
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct hidled_device {
978c2ecf20Sopenharmony_ci	const struct hidled_config *config;
988c2ecf20Sopenharmony_ci	struct hid_device       *hdev;
998c2ecf20Sopenharmony_ci	struct hidled_rgb	*rgb;
1008c2ecf20Sopenharmony_ci	u8			*buf;
1018c2ecf20Sopenharmony_ci	struct mutex		lock;
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define MAX_REPORT_SIZE		16
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define to_hidled_led(arg) container_of(arg, struct hidled_led, cdev)
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic bool riso_kagaku_switch_green_blue;
1098c2ecf20Sopenharmony_cimodule_param(riso_kagaku_switch_green_blue, bool, S_IRUGO | S_IWUSR);
1108c2ecf20Sopenharmony_ciMODULE_PARM_DESC(riso_kagaku_switch_green_blue,
1118c2ecf20Sopenharmony_ci	"switch green and blue RGB component for Riso Kagaku devices");
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic int hidled_send(struct hidled_device *ldev, __u8 *buf)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	int ret;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	mutex_lock(&ldev->lock);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/*
1208c2ecf20Sopenharmony_ci	 * buffer provided to hid_hw_raw_request must not be on the stack
1218c2ecf20Sopenharmony_ci	 * and must not be part of a data structure
1228c2ecf20Sopenharmony_ci	 */
1238c2ecf20Sopenharmony_ci	memcpy(ldev->buf, buf, ldev->config->report_size);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (ldev->config->report_type == RAW_REQUEST)
1268c2ecf20Sopenharmony_ci		ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf,
1278c2ecf20Sopenharmony_ci					 ldev->config->report_size,
1288c2ecf20Sopenharmony_ci					 HID_FEATURE_REPORT,
1298c2ecf20Sopenharmony_ci					 HID_REQ_SET_REPORT);
1308c2ecf20Sopenharmony_ci	else if (ldev->config->report_type == OUTPUT_REPORT)
1318c2ecf20Sopenharmony_ci		ret = hid_hw_output_report(ldev->hdev, ldev->buf,
1328c2ecf20Sopenharmony_ci					   ldev->config->report_size);
1338c2ecf20Sopenharmony_ci	else
1348c2ecf20Sopenharmony_ci		ret = -EINVAL;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	mutex_unlock(&ldev->lock);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (ret < 0)
1398c2ecf20Sopenharmony_ci		return ret;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	return ret == ldev->config->report_size ? 0 : -EMSGSIZE;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/* reading data is supported for report type RAW_REQUEST only */
1458c2ecf20Sopenharmony_cistatic int hidled_recv(struct hidled_device *ldev, __u8 *buf)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	int ret;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (ldev->config->report_type != RAW_REQUEST)
1508c2ecf20Sopenharmony_ci		return -EINVAL;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	mutex_lock(&ldev->lock);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	memcpy(ldev->buf, buf, ldev->config->report_size);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf,
1578c2ecf20Sopenharmony_ci				 ldev->config->report_size,
1588c2ecf20Sopenharmony_ci				 HID_FEATURE_REPORT,
1598c2ecf20Sopenharmony_ci				 HID_REQ_SET_REPORT);
1608c2ecf20Sopenharmony_ci	if (ret < 0)
1618c2ecf20Sopenharmony_ci		goto err;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	ret = hid_hw_raw_request(ldev->hdev, buf[0], ldev->buf,
1648c2ecf20Sopenharmony_ci				 ldev->config->report_size,
1658c2ecf20Sopenharmony_ci				 HID_FEATURE_REPORT,
1668c2ecf20Sopenharmony_ci				 HID_REQ_GET_REPORT);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	memcpy(buf, ldev->buf, ldev->config->report_size);
1698c2ecf20Sopenharmony_cierr:
1708c2ecf20Sopenharmony_ci	mutex_unlock(&ldev->lock);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return ret < 0 ? ret : 0;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic u8 riso_kagaku_index(struct hidled_rgb *rgb)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	enum led_brightness r, g, b;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	r = rgb->red.cdev.brightness;
1808c2ecf20Sopenharmony_ci	g = rgb->green.cdev.brightness;
1818c2ecf20Sopenharmony_ci	b = rgb->blue.cdev.brightness;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (riso_kagaku_switch_green_blue)
1848c2ecf20Sopenharmony_ci		return RISO_KAGAKU_IX(r, b, g);
1858c2ecf20Sopenharmony_ci	else
1868c2ecf20Sopenharmony_ci		return RISO_KAGAKU_IX(r, g, b);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int riso_kagaku_write(struct led_classdev *cdev, enum led_brightness br)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	struct hidled_led *led = to_hidled_led(cdev);
1928c2ecf20Sopenharmony_ci	struct hidled_rgb *rgb = led->rgb;
1938c2ecf20Sopenharmony_ci	__u8 buf[MAX_REPORT_SIZE] = {};
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	buf[1] = riso_kagaku_index(rgb);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return hidled_send(rgb->ldev, buf);
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int dream_cheeky_write(struct led_classdev *cdev, enum led_brightness br)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct hidled_led *led = to_hidled_led(cdev);
2038c2ecf20Sopenharmony_ci	struct hidled_rgb *rgb = led->rgb;
2048c2ecf20Sopenharmony_ci	__u8 buf[MAX_REPORT_SIZE] = {};
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	buf[1] = rgb->red.cdev.brightness;
2078c2ecf20Sopenharmony_ci	buf[2] = rgb->green.cdev.brightness;
2088c2ecf20Sopenharmony_ci	buf[3] = rgb->blue.cdev.brightness;
2098c2ecf20Sopenharmony_ci	buf[7] = 0x1a;
2108c2ecf20Sopenharmony_ci	buf[8] = 0x05;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return hidled_send(rgb->ldev, buf);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic int dream_cheeky_init(struct hidled_device *ldev)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	__u8 buf[MAX_REPORT_SIZE] = {};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* Dream Cheeky magic */
2208c2ecf20Sopenharmony_ci	buf[1] = 0x1f;
2218c2ecf20Sopenharmony_ci	buf[2] = 0x02;
2228c2ecf20Sopenharmony_ci	buf[4] = 0x5f;
2238c2ecf20Sopenharmony_ci	buf[7] = 0x1a;
2248c2ecf20Sopenharmony_ci	buf[8] = 0x03;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	return hidled_send(ldev, buf);
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic int _thingm_write(struct led_classdev *cdev, enum led_brightness br,
2308c2ecf20Sopenharmony_ci			 u8 offset)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	struct hidled_led *led = to_hidled_led(cdev);
2338c2ecf20Sopenharmony_ci	__u8 buf[MAX_REPORT_SIZE] = { 1, 'c' };
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	buf[2] = led->rgb->red.cdev.brightness;
2368c2ecf20Sopenharmony_ci	buf[3] = led->rgb->green.cdev.brightness;
2378c2ecf20Sopenharmony_ci	buf[4] = led->rgb->blue.cdev.brightness;
2388c2ecf20Sopenharmony_ci	buf[7] = led->rgb->num + offset;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	return hidled_send(led->rgb->ldev, buf);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic int thingm_write_v1(struct led_classdev *cdev, enum led_brightness br)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	return _thingm_write(cdev, br, 0);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic int thingm_write(struct led_classdev *cdev, enum led_brightness br)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	return _thingm_write(cdev, br, 1);
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic const struct hidled_config hidled_config_thingm_v1 = {
2548c2ecf20Sopenharmony_ci	.name = "ThingM blink(1) v1",
2558c2ecf20Sopenharmony_ci	.short_name = "thingm",
2568c2ecf20Sopenharmony_ci	.max_brightness = 255,
2578c2ecf20Sopenharmony_ci	.num_leds = 1,
2588c2ecf20Sopenharmony_ci	.report_size = 9,
2598c2ecf20Sopenharmony_ci	.report_type = RAW_REQUEST,
2608c2ecf20Sopenharmony_ci	.write = thingm_write_v1,
2618c2ecf20Sopenharmony_ci};
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic int thingm_init(struct hidled_device *ldev)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	__u8 buf[MAX_REPORT_SIZE] = { 1, 'v' };
2668c2ecf20Sopenharmony_ci	int ret;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	ret = hidled_recv(ldev, buf);
2698c2ecf20Sopenharmony_ci	if (ret)
2708c2ecf20Sopenharmony_ci		return ret;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* Check for firmware major version 1 */
2738c2ecf20Sopenharmony_ci	if (buf[3] == '1')
2748c2ecf20Sopenharmony_ci		ldev->config = &hidled_config_thingm_v1;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	return 0;
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic inline int delcom_get_lednum(const struct hidled_led *led)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	if (led == &led->rgb->red)
2828c2ecf20Sopenharmony_ci		return DELCOM_RED_LED;
2838c2ecf20Sopenharmony_ci	else if (led == &led->rgb->green)
2848c2ecf20Sopenharmony_ci		return DELCOM_GREEN_LED;
2858c2ecf20Sopenharmony_ci	else
2868c2ecf20Sopenharmony_ci		return DELCOM_BLUE_LED;
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic int delcom_enable_led(struct hidled_led *led)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	union delcom_packet dp = { .tx.major_cmd = 101, .tx.minor_cmd = 12 };
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	dp.tx.data_lsb = 1 << delcom_get_lednum(led);
2948c2ecf20Sopenharmony_ci	dp.tx.data_msb = 0;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	return hidled_send(led->rgb->ldev, dp.data);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int delcom_set_pwm(struct hidled_led *led)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	union delcom_packet dp = { .tx.major_cmd = 101, .tx.minor_cmd = 34 };
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	dp.tx.data_lsb = delcom_get_lednum(led);
3048c2ecf20Sopenharmony_ci	dp.tx.data_msb = led->cdev.brightness;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	return hidled_send(led->rgb->ldev, dp.data);
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic int delcom_write(struct led_classdev *cdev, enum led_brightness br)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	struct hidled_led *led = to_hidled_led(cdev);
3128c2ecf20Sopenharmony_ci	int ret;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/*
3158c2ecf20Sopenharmony_ci	 * enable LED
3168c2ecf20Sopenharmony_ci	 * We can't do this in the init function already because the device
3178c2ecf20Sopenharmony_ci	 * is internally reset later.
3188c2ecf20Sopenharmony_ci	 */
3198c2ecf20Sopenharmony_ci	ret = delcom_enable_led(led);
3208c2ecf20Sopenharmony_ci	if (ret)
3218c2ecf20Sopenharmony_ci		return ret;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return delcom_set_pwm(led);
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic int delcom_init(struct hidled_device *ldev)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	union delcom_packet dp = { .rx.cmd = 104 };
3298c2ecf20Sopenharmony_ci	int ret;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	ret = hidled_recv(ldev, dp.data);
3328c2ecf20Sopenharmony_ci	if (ret)
3338c2ecf20Sopenharmony_ci		return ret;
3348c2ecf20Sopenharmony_ci	/*
3358c2ecf20Sopenharmony_ci	 * Several Delcom devices share the same USB VID/PID
3368c2ecf20Sopenharmony_ci	 * Check for family id 2 for Visual Signal Indicator
3378c2ecf20Sopenharmony_ci	 */
3388c2ecf20Sopenharmony_ci	return le16_to_cpu(dp.fw.family_code) == 2 ? 0 : -ENODEV;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic int luxafor_write(struct led_classdev *cdev, enum led_brightness br)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	struct hidled_led *led = to_hidled_led(cdev);
3448c2ecf20Sopenharmony_ci	__u8 buf[MAX_REPORT_SIZE] = { [1] = 1 };
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	buf[2] = led->rgb->num + 1;
3478c2ecf20Sopenharmony_ci	buf[3] = led->rgb->red.cdev.brightness;
3488c2ecf20Sopenharmony_ci	buf[4] = led->rgb->green.cdev.brightness;
3498c2ecf20Sopenharmony_ci	buf[5] = led->rgb->blue.cdev.brightness;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	return hidled_send(led->rgb->ldev, buf);
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic const struct hidled_config hidled_configs[] = {
3558c2ecf20Sopenharmony_ci	{
3568c2ecf20Sopenharmony_ci		.type = RISO_KAGAKU,
3578c2ecf20Sopenharmony_ci		.name = "Riso Kagaku Webmail Notifier",
3588c2ecf20Sopenharmony_ci		.short_name = "riso_kagaku",
3598c2ecf20Sopenharmony_ci		.max_brightness = 1,
3608c2ecf20Sopenharmony_ci		.num_leds = 1,
3618c2ecf20Sopenharmony_ci		.report_size = 6,
3628c2ecf20Sopenharmony_ci		.report_type = OUTPUT_REPORT,
3638c2ecf20Sopenharmony_ci		.write = riso_kagaku_write,
3648c2ecf20Sopenharmony_ci	},
3658c2ecf20Sopenharmony_ci	{
3668c2ecf20Sopenharmony_ci		.type = DREAM_CHEEKY,
3678c2ecf20Sopenharmony_ci		.name = "Dream Cheeky Webmail Notifier",
3688c2ecf20Sopenharmony_ci		.short_name = "dream_cheeky",
3698c2ecf20Sopenharmony_ci		.max_brightness = 63,
3708c2ecf20Sopenharmony_ci		.num_leds = 1,
3718c2ecf20Sopenharmony_ci		.report_size = 9,
3728c2ecf20Sopenharmony_ci		.report_type = RAW_REQUEST,
3738c2ecf20Sopenharmony_ci		.init = dream_cheeky_init,
3748c2ecf20Sopenharmony_ci		.write = dream_cheeky_write,
3758c2ecf20Sopenharmony_ci	},
3768c2ecf20Sopenharmony_ci	{
3778c2ecf20Sopenharmony_ci		.type = THINGM,
3788c2ecf20Sopenharmony_ci		.name = "ThingM blink(1)",
3798c2ecf20Sopenharmony_ci		.short_name = "thingm",
3808c2ecf20Sopenharmony_ci		.max_brightness = 255,
3818c2ecf20Sopenharmony_ci		.num_leds = 2,
3828c2ecf20Sopenharmony_ci		.report_size = 9,
3838c2ecf20Sopenharmony_ci		.report_type = RAW_REQUEST,
3848c2ecf20Sopenharmony_ci		.init = thingm_init,
3858c2ecf20Sopenharmony_ci		.write = thingm_write,
3868c2ecf20Sopenharmony_ci	},
3878c2ecf20Sopenharmony_ci	{
3888c2ecf20Sopenharmony_ci		.type = DELCOM,
3898c2ecf20Sopenharmony_ci		.name = "Delcom Visual Signal Indicator G2",
3908c2ecf20Sopenharmony_ci		.short_name = "delcom",
3918c2ecf20Sopenharmony_ci		.max_brightness = 100,
3928c2ecf20Sopenharmony_ci		.num_leds = 1,
3938c2ecf20Sopenharmony_ci		.report_size = 8,
3948c2ecf20Sopenharmony_ci		.report_type = RAW_REQUEST,
3958c2ecf20Sopenharmony_ci		.init = delcom_init,
3968c2ecf20Sopenharmony_ci		.write = delcom_write,
3978c2ecf20Sopenharmony_ci	},
3988c2ecf20Sopenharmony_ci	{
3998c2ecf20Sopenharmony_ci		.type = LUXAFOR,
4008c2ecf20Sopenharmony_ci		.name = "Greynut Luxafor",
4018c2ecf20Sopenharmony_ci		.short_name = "luxafor",
4028c2ecf20Sopenharmony_ci		.max_brightness = 255,
4038c2ecf20Sopenharmony_ci		.num_leds = 6,
4048c2ecf20Sopenharmony_ci		.report_size = 9,
4058c2ecf20Sopenharmony_ci		.report_type = OUTPUT_REPORT,
4068c2ecf20Sopenharmony_ci		.write = luxafor_write,
4078c2ecf20Sopenharmony_ci	},
4088c2ecf20Sopenharmony_ci};
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic int hidled_init_led(struct hidled_led *led, const char *color_name,
4118c2ecf20Sopenharmony_ci			   struct hidled_rgb *rgb, unsigned int minor)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	const struct hidled_config *config = rgb->ldev->config;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	if (config->num_leds > 1)
4168c2ecf20Sopenharmony_ci		snprintf(led->name, sizeof(led->name), "%s%u:%s:led%u",
4178c2ecf20Sopenharmony_ci			 config->short_name, minor, color_name, rgb->num);
4188c2ecf20Sopenharmony_ci	else
4198c2ecf20Sopenharmony_ci		snprintf(led->name, sizeof(led->name), "%s%u:%s",
4208c2ecf20Sopenharmony_ci			 config->short_name, minor, color_name);
4218c2ecf20Sopenharmony_ci	led->cdev.name = led->name;
4228c2ecf20Sopenharmony_ci	led->cdev.max_brightness = config->max_brightness;
4238c2ecf20Sopenharmony_ci	led->cdev.brightness_set_blocking = config->write;
4248c2ecf20Sopenharmony_ci	led->cdev.flags = LED_HW_PLUGGABLE;
4258c2ecf20Sopenharmony_ci	led->rgb = rgb;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	return devm_led_classdev_register(&rgb->ldev->hdev->dev, &led->cdev);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic int hidled_init_rgb(struct hidled_rgb *rgb, unsigned int minor)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	int ret;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	/* Register the red diode */
4358c2ecf20Sopenharmony_ci	ret = hidled_init_led(&rgb->red, "red", rgb, minor);
4368c2ecf20Sopenharmony_ci	if (ret)
4378c2ecf20Sopenharmony_ci		return ret;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	/* Register the green diode */
4408c2ecf20Sopenharmony_ci	ret = hidled_init_led(&rgb->green, "green", rgb, minor);
4418c2ecf20Sopenharmony_ci	if (ret)
4428c2ecf20Sopenharmony_ci		return ret;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/* Register the blue diode */
4458c2ecf20Sopenharmony_ci	return hidled_init_led(&rgb->blue, "blue", rgb, minor);
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	struct hidled_device *ldev;
4518c2ecf20Sopenharmony_ci	unsigned int minor;
4528c2ecf20Sopenharmony_ci	int ret, i;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL);
4558c2ecf20Sopenharmony_ci	if (!ldev)
4568c2ecf20Sopenharmony_ci		return -ENOMEM;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	ldev->buf = devm_kmalloc(&hdev->dev, MAX_REPORT_SIZE, GFP_KERNEL);
4598c2ecf20Sopenharmony_ci	if (!ldev->buf)
4608c2ecf20Sopenharmony_ci		return -ENOMEM;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	ret = hid_parse(hdev);
4638c2ecf20Sopenharmony_ci	if (ret)
4648c2ecf20Sopenharmony_ci		return ret;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	ldev->hdev = hdev;
4678c2ecf20Sopenharmony_ci	mutex_init(&ldev->lock);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	for (i = 0; !ldev->config && i < ARRAY_SIZE(hidled_configs); i++)
4708c2ecf20Sopenharmony_ci		if (hidled_configs[i].type == id->driver_data)
4718c2ecf20Sopenharmony_ci			ldev->config = &hidled_configs[i];
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	if (!ldev->config)
4748c2ecf20Sopenharmony_ci		return -EINVAL;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	if (ldev->config->init) {
4778c2ecf20Sopenharmony_ci		ret = ldev->config->init(ldev);
4788c2ecf20Sopenharmony_ci		if (ret)
4798c2ecf20Sopenharmony_ci			return ret;
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	ldev->rgb = devm_kcalloc(&hdev->dev, ldev->config->num_leds,
4838c2ecf20Sopenharmony_ci				 sizeof(struct hidled_rgb), GFP_KERNEL);
4848c2ecf20Sopenharmony_ci	if (!ldev->rgb)
4858c2ecf20Sopenharmony_ci		return -ENOMEM;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
4888c2ecf20Sopenharmony_ci	if (ret)
4898c2ecf20Sopenharmony_ci		return ret;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	minor = ((struct hidraw *) hdev->hidraw)->minor;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	for (i = 0; i < ldev->config->num_leds; i++) {
4948c2ecf20Sopenharmony_ci		ldev->rgb[i].ldev = ldev;
4958c2ecf20Sopenharmony_ci		ldev->rgb[i].num = i;
4968c2ecf20Sopenharmony_ci		ret = hidled_init_rgb(&ldev->rgb[i], minor);
4978c2ecf20Sopenharmony_ci		if (ret) {
4988c2ecf20Sopenharmony_ci			hid_hw_stop(hdev);
4998c2ecf20Sopenharmony_ci			return ret;
5008c2ecf20Sopenharmony_ci		}
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	hid_info(hdev, "%s initialized\n", ldev->config->name);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	return 0;
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic const struct hid_device_id hidled_table[] = {
5098c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_RISO_KAGAKU,
5108c2ecf20Sopenharmony_ci	  USB_DEVICE_ID_RI_KA_WEBMAIL), .driver_data = RISO_KAGAKU },
5118c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
5128c2ecf20Sopenharmony_ci	  USB_DEVICE_ID_DREAM_CHEEKY_WN), .driver_data = DREAM_CHEEKY },
5138c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DREAM_CHEEKY,
5148c2ecf20Sopenharmony_ci	  USB_DEVICE_ID_DREAM_CHEEKY_FA), .driver_data = DREAM_CHEEKY },
5158c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_THINGM,
5168c2ecf20Sopenharmony_ci	  USB_DEVICE_ID_BLINK1), .driver_data = THINGM },
5178c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DELCOM,
5188c2ecf20Sopenharmony_ci	  USB_DEVICE_ID_DELCOM_VISUAL_IND), .driver_data = DELCOM },
5198c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP,
5208c2ecf20Sopenharmony_ci	  USB_DEVICE_ID_LUXAFOR), .driver_data = LUXAFOR },
5218c2ecf20Sopenharmony_ci	{ }
5228c2ecf20Sopenharmony_ci};
5238c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, hidled_table);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cistatic struct hid_driver hidled_driver = {
5268c2ecf20Sopenharmony_ci	.name = "hid-led",
5278c2ecf20Sopenharmony_ci	.probe = hidled_probe,
5288c2ecf20Sopenharmony_ci	.id_table = hidled_table,
5298c2ecf20Sopenharmony_ci};
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_cimodule_hid_driver(hidled_driver);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
5348c2ecf20Sopenharmony_ciMODULE_AUTHOR("Heiner Kallweit <hkallweit1@gmail.com>");
5358c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Simple USB RGB LED driver");
536