History

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid

Pull HID updates from Jiri Kosina: - support for U2F Zero device, from Andrej Shadura - logitech-dj has historically been treating devices behind non-unifying receivers as generic devices, using the HID emulation in the receiver. That had several shortcomings (special keys handling, battery level monitoring, etc). The driver has been reworked to enumarate (and directly communicate with) the devices behind the receiver, to avoid the (too) generic HID implementation in the receiver itself. All the work done by Benjamin Tissoires and Hans de Goede. - restructuring of intel-ish driver in order to allow for multiple clients of the ISH implementation, from Srinivas Pandruvada - several other smaller fixes and assorted device ID additions * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid: (68 commits) HID: logitech-dj: fix spelling in printk HID: input: fix assignment of .value HID: input: make sure the wheel high resolution multiplier is set HID: logitech-dj: add usbhid dependency in Kconfig HID: logitech-hidpp: add support for HID++ 1.0 consumer keys reports HID: logitech-hidpp: add support for HID++ 1.0 extra mouse buttons reports HID: logitech-hidpp: add support for HID++ 1.0 wheel reports HID: logitech-hidpp: make hidpp10_set_register_bit a bit more generic HID: logitech-hidpp: add input_device ptr to struct hidpp_device HID: logitech-hidpp: do not hardcode very long report length HID: logitech-hidpp: handle devices attached to 27MHz wireless receivers HID: logitech-hidpp: use RAP instead of FAP to get the protocol version HID: logitech-hidpp: remove unused origin_is_hid_core function parameter HID: logitech-hidpp: remove double assignment from __hidpp_send_report HID: logitech-hidpp: do not make failure to get the name fatal HID: logitech-hidpp: ignore very-short or empty names HID: logitech-hidpp: make .probe usbhid capable HID: logitech-hidpp: allow non HID++ devices to be handled by this module HID: logitech-dj: add support for Logitech Bluetooth Mini-Receiver HID: logitech-dj: make appending of the HID++ descriptors conditional ...

Linus Torvalds committed 7 years ago

Commit b4dd05d2 parents 80104bb 63b6f0b29 files · +3518 −690Browse files

Unified · Split

29 files
  1. drivers/hid/Kconfig +27 −0
  2. drivers/hid/Makefile +3 −0
  3. drivers/hid/hid-core.c +39 −14
  4. drivers/hid/hid-ids.h +6 −1
  5. drivers/hid/hid-input.c +50 −31
  6. drivers/hid/hid-lg.c +0 −2
  7. drivers/hid/hid-logitech-dj.c +920 −222
  8. drivers/hid/hid-logitech-hidpp.c +537 −199
  9. drivers/hid/hid-macally.c +45 −0
  10. drivers/hid/hid-picolcd_core.c +6 −12
  11. drivers/hid/hid-quirks.c +0 −6
  12. drivers/hid/hid-sensor-custom.c +4 −8
  13. drivers/hid/hid-u2fzero.c +374 −0
  14. drivers/hid/i2c-hid/i2c-hid-core.c +0 −2
  15. drivers/hid/intel-ish-hid/Kconfig +15 −0
  16. drivers/hid/intel-ish-hid/Makefile +3 −0
  17. drivers/hid/intel-ish-hid/ipc/hw-ish.h +1 −0
  18. drivers/hid/intel-ish-hid/ipc/pci-ish.c +1 −0
  19. drivers/hid/intel-ish-hid/ishtp-fw-loader.c +1085 −0
  20. drivers/hid/intel-ish-hid/ishtp-hid-client.c +92 −76
  21. drivers/hid/intel-ish-hid/ishtp-hid.c +43 −6
  22. drivers/hid/intel-ish-hid/ishtp-hid.h +11 −3
  23. drivers/hid/intel-ish-hid/ishtp/bus.c +92 −4
  24. drivers/hid/intel-ish-hid/ishtp/bus.h +1 −36
  25. drivers/hid/intel-ish-hid/ishtp/client.c +48 −12
  26. drivers/hid/intel-ish-hid/ishtp/client.h +0 −24
  27. drivers/hid/intel-ish-hid/ishtp/ishtp-dev.h +0 −31
  28. include/linux/hid.h +3 −1
  29. include/linux/intel-ish-client-if.h +112 −0
drivers/hid/Kconfig+27−0
@@ -231,6 +231,16 @@ config HID_COUGAR · 230 unmodified lines
231231 Supported devices:
232232 - Cougar 500k Gaming Keyboard
233233
234+config HID_MACALLY
235+ tristate "Macally devices"
236+ depends on HID
237+ help
238+ Support for Macally devices that are not fully compliant with the
239+ HID standard.
240+
241+ supported devices:
242+ - Macally ikey keyboard
243+
234244 config HID_PRODIKEYS
235245 tristate "Prodikeys PC-MIDI Keyboard support"
236246 depends on HID && SND
@@ -511,6 +521,7 @@ config HID_LOGITECH · 274 unmodified lines
511521
512522 config HID_LOGITECH_DJ
513523 tristate "Logitech Unifying receivers full support"
524+ depends on USB_HID
514525 depends on HIDRAW
515526 depends on HID_LOGITECH
516527 select HID_LOGITECH_HIDPP
@@ -1003,6 +1014,22 @@ config HID_UDRAW_PS3 · 486 unmodified lines
10031014 Say Y here if you want to use the THQ uDraw gaming tablet for
10041015 the PS3.
10051016
1017+config HID_U2FZERO
1018+ tristate "U2F Zero LED and RNG support"
1019+ depends on USB_HID
1020+ depends on LEDS_CLASS
1021+ depends on HW_RANDOM
1022+ help
1023+ Support for the LED of the U2F Zero device.
1024+
1025+ U2F Zero supports custom commands for blinking the LED
1026+ and getting data from the internal hardware RNG.
1027+ The internal hardware can be used to feed the enthropy pool.
1028+
1029+ U2F Zero only supports blinking its LED, so this driver doesn't
1030+ allow setting the brightness to anything but 1, which will
1031+ trigger a single blink and immediately reset to back 0.
1032+
10061033 config HID_WACOM
10071034 tristate "Wacom Intuos/Graphire tablet support (USB)"
10081035 depends on USB_HID
drivers/hid/Makefile+3−0
@@ -65,6 +65,7 @@ obj-$(CONFIG_HID_LENOVO) += hid-lenovo.o · 64 unmodified lines
6565 obj-$(CONFIG_HID_LOGITECH) += hid-logitech.o
6666 obj-$(CONFIG_HID_LOGITECH_DJ) += hid-logitech-dj.o
6767 obj-$(CONFIG_HID_LOGITECH_HIDPP) += hid-logitech-hidpp.o
68+obj-$(CONFIG_HID_MACALLY) += hid-macally.o
6869 obj-$(CONFIG_HID_MAGICMOUSE) += hid-magicmouse.o
6970 obj-$(CONFIG_HID_MALTRON) += hid-maltron.o
7071 obj-$(CONFIG_HID_MAYFLASH) += hid-mf.o
@@ -109,6 +110,7 @@ obj-$(CONFIG_HID_THRUSTMASTER) += hid-tmff.o · 38 unmodified lines
109110 obj-$(CONFIG_HID_TIVO) += hid-tivo.o
110111 obj-$(CONFIG_HID_TOPSEED) += hid-topseed.o
111112 obj-$(CONFIG_HID_TWINHAN) += hid-twinhan.o
113+obj-$(CONFIG_HID_U2FZERO) += hid-u2fzero.o
112114 hid-uclogic-objs := hid-uclogic-core.o \
113115 hid-uclogic-rdesc.o \
114116 hid-uclogic-params.o
@@ -134,3 +136,4 @@ obj-$(CONFIG_USB_KBD) += usbhid/ · 19 unmodified lines
134136 obj-$(CONFIG_I2C_HID) += i2c-hid/
135137
136138 obj-$(CONFIG_INTEL_ISH_HID) += intel-ish-hid/
139+obj-$(INTEL_ISH_FIRMWARE_DOWNLOADER) += intel-ish-hid/
drivers/hid/hid-core.c+39−14
@@ -30,6 +30,7 @@ · 29 unmodified lines
3030 #include <linux/vmalloc.h>
3131 #include <linux/sched.h>
3232 #include <linux/semaphore.h>
33+#include <linux/async.h>
3334
3435 #include <linux/hid.h>
3536 #include <linux/hiddev.h>
@@ -218,13 +219,14 @@ static unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type) · 182 unmodified lines
218219 * Add a usage to the temporary parser table.
219220 */
220221
221−static int hid_add_usage(struct hid_parser *parser, unsigned usage)
222+static int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size)
222223 {
223224 if (parser->local.usage_index >= HID_MAX_USAGES) {
224225 hid_err(parser->device, "usage index exceeded\n");
225226 return -1;
226227 }
227228 parser->local.usage[parser->local.usage_index] = usage;
229+ parser->local.usage_size[parser->local.usage_index] = size;
228230 parser->local.collection_index[parser->local.usage_index] =
229231 parser->collection_stack_ptr ?
230232 parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
@@ -486,10 +488,7 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item) · 255 unmodified lines
486488 return 0;
487489 }
488490
489− if (item->size <= 2)
490− data = (parser->global.usage_page << 16) + data;
491−
492− return hid_add_usage(parser, data);
491+ return hid_add_usage(parser, data, item->size);
493492
494493 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
495494
@@ -498,9 +497,6 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item) · 2 unmodified lines
498497 return 0;
499498 }
500499
501− if (item->size <= 2)
502− data = (parser->global.usage_page << 16) + data;
503−
504500 parser->local.usage_minimum = data;
505501 return 0;
506502
@@ -511,9 +507,6 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item) · 4 unmodified lines
511507 return 0;
512508 }
513509
514− if (item->size <= 2)
515− data = (parser->global.usage_page << 16) + data;
516−
517510 count = data - parser->local.usage_minimum;
518511 if (count + parser->local.usage_index >= HID_MAX_USAGES) {
519512 /*
@@ -533,7 +526,7 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item) · 13 unmodified lines
533526 }
534527
535528 for (n = parser->local.usage_minimum; n <= data; n++)
536− if (hid_add_usage(parser, n)) {
529+ if (hid_add_usage(parser, n, item->size)) {
537530 dbg_hid("hid_add_usage failed\n");
538531 return -1;
539532 }
@@ -547,6 +540,22 @@ static int hid_parser_local(struct hid_parser *parser, struct hid_item *item) · 7 unmodified lines
547540 return 0;
548541 }
549542
543+/*
544+ * Concatenate Usage Pages into Usages where relevant:
545+ * As per specification, 6.2.2.8: "When the parser encounters a main item it
546+ * concatenates the last declared Usage Page with a Usage to form a complete
547+ * usage value."
548+ */
549+
550+static void hid_concatenate_usage_page(struct hid_parser *parser)
551+{
552+ int i;
553+
554+ for (i = 0; i < parser->local.usage_index; i++)
555+ if (parser->local.usage_size[i] <= 2)
556+ parser->local.usage[i] += parser->global.usage_page << 16;
557+}
558+
550559 /*
551560 * Process a main item.
552561 */
@@ -556,6 +565,8 @@ static int hid_parser_main(struct hid_parser *parser, struct hid_item *item) · 3 unmodified lines
556565 __u32 data;
557566 int ret;
558567
568+ hid_concatenate_usage_page(parser);
569+
559570 data = item_udata(item);
560571
561572 switch (item->tag) {
@@ -765,6 +776,8 @@ static int hid_scan_main(struct hid_parser *parser, struct hid_item *item) · 203 unmodified lines
765776 __u32 data;
766777 int i;
767778
779+ hid_concatenate_usage_page(parser);
780+
768781 data = item_udata(item);
769782
770783 switch (item->tag) {
@@ -1624,7 +1637,7 @@ static struct hid_report *hid_get_report(struct hid_report_enum *report_enum, · 853 unmodified lines
16241637 * Implement a generic .request() callback, using .raw_request()
16251638 * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
16261639 */
1627−void __hid_request(struct hid_device *hid, struct hid_report *report,
1640+int __hid_request(struct hid_device *hid, struct hid_report *report,
16281641 int reqtype)
16291642 {
16301643 char *buf;
@@ -1633,7 +1646,7 @@ void __hid_request(struct hid_device *hid, struct hid_report *report, · 2 unmodified lines
16331646
16341647 buf = hid_alloc_report_buf(report, GFP_KERNEL);
16351648 if (!buf)
1636− return;
1649+ return -ENOMEM;
16371650
16381651 len = hid_report_len(report);
16391652
@@ -1650,8 +1663,11 @@ void __hid_request(struct hid_device *hid, struct hid_report *report, · 10 unmodified lines
16501663 if (reqtype == HID_REQ_GET_REPORT)
16511664 hid_input_report(hid, report->type, buf, ret, 0);
16521665
1666+ ret = 0;
1667+
16531668 out:
16541669 kfree(buf);
1670+ return ret;
16551671 }
16561672 EXPORT_SYMBOL_GPL(__hid_request);
16571673
@@ -2349,6 +2365,15 @@ int hid_add_device(struct hid_device *hdev) · 691 unmodified lines
23492365 dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
23502366 hdev->vendor, hdev->product, atomic_inc_return(&id));
23512367
2368+ /*
2369+ * Try loading the module for the device before the add, so that we do
2370+ * not first have hid-generic binding only to have it replaced
2371+ * immediately afterwards with a specialized driver.
2372+ */
2373+ if (!current_is_async())
2374+ request_module("hid:b%04Xg%04Xv%08Xp%08X", hdev->bus,
2375+ hdev->group, hdev->vendor, hdev->product);
2376+
23522377 hid_debug_register(hdev, dev_name(&hdev->dev));
23532378 ret = device_add(&hdev->dev);
23542379 if (!ret)
drivers/hid/hid-ids.h+6−1
@@ -323,6 +323,7 @@ · 322 unmodified lines
323323 #define USB_DEVICE_ID_CYGNAL_RADIO_SI470X 0x818a
324324 #define USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH 0x81b9
325325 #define USB_DEVICE_ID_CYGNAL_CP2112 0xea90
326+#define USB_DEVICE_ID_U2F_ZERO 0x8acf
326327
327328 #define USB_DEVICE_ID_CYGNAL_RADIO_SI4713 0x8244
328329
@@ -762,8 +763,12 @@ · 433 unmodified lines
762763 #define USB_DEVICE_ID_S510_RECEIVER_2 0xc517
763764 #define USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500 0xc512
764765 #define USB_DEVICE_ID_MX3000_RECEIVER 0xc513
766+#define USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER 0xc51b
765767 #define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER 0xc52b
768+#define USB_DEVICE_ID_LOGITECH_NANO_RECEIVER 0xc52f
766769 #define USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2 0xc532
770+#define USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2 0xc534
771+#define USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_GAMING 0xc539
767772 #define USB_DEVICE_ID_SPACETRAVELLER 0xc623
768773 #define USB_DEVICE_ID_SPACENAVIGATOR 0xc626
769774 #define USB_DEVICE_ID_DINOVO_DESKTOP 0xc704
@@ -1034,6 +1039,7 @@ · 264 unmodified lines
10341039 #define USB_DEVICE_ID_SINO_LITE_CONTROLLER 0x3008
10351040
10361041 #define USB_VENDOR_ID_SOLID_YEAR 0x060b
1042+#define USB_DEVICE_ID_MACALLY_IKEY_KEYBOARD 0x0001
10371043 #define USB_DEVICE_ID_COUGAR_500K_GAMING_KEYBOARD 0x500a
10381044 #define USB_DEVICE_ID_COUGAR_700K_GAMING_KEYBOARD 0x700a
10391045
@@ -1083,7 +1089,6 @@ · 43 unmodified lines
10831089 #define USB_DEVICE_ID_SYNAPTICS_HD 0x0ac3
10841090 #define USB_DEVICE_ID_SYNAPTICS_QUAD_HD 0x1ac3
10851091 #define USB_DEVICE_ID_SYNAPTICS_TP_V103 0x5710
1086−#define I2C_DEVICE_ID_SYNAPTICS_7E7E 0x7e7e
10871092
10881093 #define USB_VENDOR_ID_TEXAS_INSTRUMENTS 0x2047
10891094 #define USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA 0x0855
drivers/hid/hid-input.c+50−31
@@ -1557,52 +1557,71 @@ static void hidinput_close(struct input_dev *dev) · 1556 unmodified lines
15571557 hid_hw_close(hid);
15581558 }
15591559
1560−static void hidinput_change_resolution_multipliers(struct hid_device *hid)
1560+static bool __hidinput_change_resolution_multipliers(struct hid_device *hid,
1561+ struct hid_report *report, bool use_logical_max)
15611562 {
1562− struct hid_report_enum *rep_enum;
1563− struct hid_report *rep;
15641563 struct hid_usage *usage;
1564+ bool update_needed = false;
15651565 int i, j;
15661566
1567− rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1568− list_for_each_entry(rep, &rep_enum->report_list, list) {
1569− bool update_needed = false;
1567+ if (report->maxfield == 0)
1568+ return false;
15701569
1571− if (rep->maxfield == 0)
1572− continue;
1570+ /*
1571+ * If we have more than one feature within this report we
1572+ * need to fill in the bits from the others before we can
1573+ * overwrite the ones for the Resolution Multiplier.
1574+ */
1575+ if (report->maxfield > 1) {
1576+ hid_hw_request(hid, report, HID_REQ_GET_REPORT);
1577+ hid_hw_wait(hid);
1578+ }
15731579
1574− /*
1575− * If we have more than one feature within this report we
1576− * need to fill in the bits from the others before we can
1577− * overwrite the ones for the Resolution Multiplier.
1580+ for (i = 0; i < report->maxfield; i++) {
1581+ __s32 value = use_logical_max ?
1582+ report->field[i]->logical_maximum :
1583+ report->field[i]->logical_minimum;
1584+
1585+ /* There is no good reason for a Resolution
1586+ * Multiplier to have a count other than 1.
1587+ * Ignore that case.
15781588 */
1579− if (rep->maxfield > 1) {
1580− hid_hw_request(hid, rep, HID_REQ_GET_REPORT);
1581− hid_hw_wait(hid);
1582− }
1589+ if (report->field[i]->report_count != 1)
1590+ continue;
15831591
1584− for (i = 0; i < rep->maxfield; i++) {
1585− __s32 logical_max = rep->field[i]->logical_maximum;
1592+ for (j = 0; j < report->field[i]->maxusage; j++) {
1593+ usage = &report->field[i]->usage[j];
15861594
1587− /* There is no good reason for a Resolution
1588− * Multiplier to have a count other than 1.
1589− * Ignore that case.
1590− */
1591− if (rep->field[i]->report_count != 1)
1595+ if (usage->hid != HID_GD_RESOLUTION_MULTIPLIER)
15921596 continue;
15931597
1594− for (j = 0; j < rep->field[i]->maxusage; j++) {
1595− usage = &rep->field[i]->usage[j];
1598+ report->field[i]->value[j] = value;
1599+ update_needed = true;
1600+ }
1601+ }
1602+
1603+ return update_needed;
1604+}
15961605
1597− if (usage->hid != HID_GD_RESOLUTION_MULTIPLIER)
1598− continue;
1606+static void hidinput_change_resolution_multipliers(struct hid_device *hid)
1607+{
1608+ struct hid_report_enum *rep_enum;
1609+ struct hid_report *rep;
1610+ int ret;
15991611
1600− *rep->field[i]->value = logical_max;
1601− update_needed = true;
1612+ rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1613+ list_for_each_entry(rep, &rep_enum->report_list, list) {
1614+ bool update_needed = __hidinput_change_resolution_multipliers(hid,
1615+ rep, true);
1616+
1617+ if (update_needed) {
1618+ ret = __hid_request(hid, rep, HID_REQ_SET_REPORT);
1619+ if (ret) {
1620+ __hidinput_change_resolution_multipliers(hid,
1621+ rep, false);
1622+ return;
16021623 }
16031624 }
1604− if (update_needed)
1605− hid_hw_request(hid, rep, HID_REQ_SET_REPORT);
16061625 }
16071626
16081627 /* refresh our structs */
drivers/hid/hid-lg.c+0−2
@@ -876,8 +876,6 @@ static const struct hid_device_id lg_devices[] = { · 875 unmodified lines
876876 .driver_data = LG_RDESC | LG_WIRELESS },
877877 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER),
878878 .driver_data = LG_RDESC | LG_WIRELESS },
879− { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2),
880− .driver_data = LG_RDESC | LG_WIRELESS },
881879
882880 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER),
883881 .driver_data = LG_BAD_RELATIVE_KEYS },
drivers/hid/hid-logitech-dj.c+920−222
@@ -25,13 +25,14 @@ · 24 unmodified lines
2525 #include <linux/device.h>
2626 #include <linux/hid.h>
2727 #include <linux/module.h>
28−#include <linux/usb.h>
2928 #include <linux/kfifo.h>
29+#include <linux/delay.h>
30+#include <linux/usb.h> /* For to_usb_interface for kvm extra intf check */
3031 #include <asm/unaligned.h>
3132 #include "hid-ids.h"
3233
3334 #define DJ_MAX_PAIRED_DEVICES 6
34−#define DJ_MAX_NUMBER_NOTIFICATIONS 8
35+#define DJ_MAX_NUMBER_NOTIFS 8
3536 #define DJ_RECEIVER_INDEX 0
3637 #define DJ_DEVICE_INDEX_MIN 1
3738 #define DJ_DEVICE_INDEX_MAX 6
@@ -74,7 +75,6 @@ · 36 unmodified lines
7475 /* Device Un-Paired Notification */
7576 #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
7677
77−
7878 /* Connection Status Notification */
7979 #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
8080 #define CONNECTION_STATUS_PARAM_STATUS 0x00
@@ -94,12 +94,43 @@ · 13 unmodified lines
9494 #define REPORT_TYPE_LEDS 0x0E
9595
9696 /* RF Report types bitfield */
97−#define STD_KEYBOARD 0x00000002
98−#define STD_MOUSE 0x00000004
99−#define MULTIMEDIA 0x00000008
100−#define POWER_KEYS 0x00000010
101−#define MEDIA_CENTER 0x00000100
102−#define KBD_LEDS 0x00004000
97+#define STD_KEYBOARD BIT(1)
98+#define STD_MOUSE BIT(2)
99+#define MULTIMEDIA BIT(3)
100+#define POWER_KEYS BIT(4)
101+#define MEDIA_CENTER BIT(8)
102+#define KBD_LEDS BIT(14)
103+/* Fake (bitnr > NUMBER_OF_HID_REPORTS) bit to track HID++ capability */
104+#define HIDPP BIT_ULL(63)
105+
106+/* HID++ Device Connected Notification */
107+#define REPORT_TYPE_NOTIF_DEVICE_CONNECTED 0x41
108+#define HIDPP_PARAM_PROTO_TYPE 0x00
109+#define HIDPP_PARAM_DEVICE_INFO 0x01
110+#define HIDPP_PARAM_EQUAD_LSB 0x02
111+#define HIDPP_PARAM_EQUAD_MSB 0x03
112+#define HIDPP_PARAM_27MHZ_DEVID 0x03
113+#define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0)
114+#define HIDPP_LINK_STATUS_MASK BIT(6)
115+#define HIDPP_MANUFACTURER_MASK BIT(7)
116+
117+#define HIDPP_DEVICE_TYPE_KEYBOARD 1
118+#define HIDPP_DEVICE_TYPE_MOUSE 2
119+
120+#define HIDPP_SET_REGISTER 0x80
121+#define HIDPP_GET_LONG_REGISTER 0x83
122+#define HIDPP_REG_CONNECTION_STATE 0x02
123+#define HIDPP_REG_PAIRING_INFORMATION 0xB5
124+#define HIDPP_PAIRING_INFORMATION 0x20
125+#define HIDPP_FAKE_DEVICE_ARRIVAL 0x02
126+
127+enum recvr_type {
128+ recvr_type_dj,
129+ recvr_type_hidpp,
130+ recvr_type_gaming_hidpp,
131+ recvr_type_27mhz,
132+ recvr_type_bluetooth,
133+};
103134
104135 struct dj_report {
105136 u8 report_id;
@@ -108,23 +139,51 @@ struct dj_report { · 2 unmodified lines
108139 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
109140 };
110141
142+struct hidpp_event {
143+ u8 report_id;
144+ u8 device_index;
145+ u8 sub_id;
146+ u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
147+} __packed;
148+
111149 struct dj_receiver_dev {
112− struct hid_device *hdev;
150+ struct hid_device *mouse;
151+ struct hid_device *keyboard;
152+ struct hid_device *hidpp;
113153 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
114154 DJ_DEVICE_INDEX_MIN];
155+ struct list_head list;
156+ struct kref kref;
115157 struct work_struct work;
116158 struct kfifo notif_fifo;
159+ unsigned long last_query; /* in jiffies */
160+ bool ready;
161+ enum recvr_type type;
162+ unsigned int unnumbered_application;
117163 spinlock_t lock;
118− bool querying_devices;
119164 };
120165
121166 struct dj_device {
122167 struct hid_device *hdev;
123168 struct dj_receiver_dev *dj_receiver_dev;
124− u32 reports_supported;
169+ u64 reports_supported;
125170 u8 device_index;
126171 };
127172
173+#define WORKITEM_TYPE_EMPTY 0
174+#define WORKITEM_TYPE_PAIRED 1
175+#define WORKITEM_TYPE_UNPAIRED 2
176+#define WORKITEM_TYPE_UNKNOWN 255
177+
178+struct dj_workitem {
179+ u8 type; /* WORKITEM_TYPE_* */
180+ u8 device_index;
181+ u8 device_type;
182+ u8 quad_id_msb;
183+ u8 quad_id_lsb;
184+ u64 reports_supported;
185+};
186+
128187 /* Keyboard descriptor (1) */
129188 static const char kbd_descriptor[] = {
130189 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
@@ -200,6 +259,131 @@ static const char mse_descriptor[] = { · 69 unmodified lines
200259 0xC0, /* END_COLLECTION */
201260 };
202261
262+/* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
263+static const char mse_27mhz_descriptor[] = {
264+ 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
265+ 0x09, 0x02, /* USAGE (Mouse) */
266+ 0xA1, 0x01, /* COLLECTION (Application) */
267+ 0x85, 0x02, /* REPORT_ID = 2 */
268+ 0x09, 0x01, /* USAGE (pointer) */
269+ 0xA1, 0x00, /* COLLECTION (physical) */
270+ 0x05, 0x09, /* USAGE_PAGE (buttons) */
271+ 0x19, 0x01, /* USAGE_MIN (1) */
272+ 0x29, 0x08, /* USAGE_MAX (8) */
273+ 0x15, 0x00, /* LOGICAL_MIN (0) */
274+ 0x25, 0x01, /* LOGICAL_MAX (1) */
275+ 0x95, 0x08, /* REPORT_COUNT (8) */
276+ 0x75, 0x01, /* REPORT_SIZE (1) */
277+ 0x81, 0x02, /* INPUT (data var abs) */
278+ 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
279+ 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
280+ 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
281+ 0x75, 0x0C, /* REPORT_SIZE (12) */
282+ 0x95, 0x02, /* REPORT_COUNT (2) */
283+ 0x09, 0x30, /* USAGE (X) */
284+ 0x09, 0x31, /* USAGE (Y) */
285+ 0x81, 0x06, /* INPUT */
286+ 0x15, 0x81, /* LOGICAL_MIN (-127) */
287+ 0x25, 0x7F, /* LOGICAL_MAX (127) */
288+ 0x75, 0x08, /* REPORT_SIZE (8) */
289+ 0x95, 0x01, /* REPORT_COUNT (1) */
290+ 0x09, 0x38, /* USAGE (wheel) */
291+ 0x81, 0x06, /* INPUT */
292+ 0x05, 0x0C, /* USAGE_PAGE(consumer) */
293+ 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
294+ 0x95, 0x01, /* REPORT_COUNT (1) */
295+ 0x81, 0x06, /* INPUT */
296+ 0xC0, /* END_COLLECTION */
297+ 0xC0, /* END_COLLECTION */
298+};
299+
300+/* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
301+static const char mse_bluetooth_descriptor[] = {
302+ 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
303+ 0x09, 0x02, /* USAGE (Mouse) */
304+ 0xA1, 0x01, /* COLLECTION (Application) */
305+ 0x85, 0x02, /* REPORT_ID = 2 */
306+ 0x09, 0x01, /* USAGE (pointer) */
307+ 0xA1, 0x00, /* COLLECTION (physical) */
308+ 0x05, 0x09, /* USAGE_PAGE (buttons) */
309+ 0x19, 0x01, /* USAGE_MIN (1) */
310+ 0x29, 0x08, /* USAGE_MAX (8) */
311+ 0x15, 0x00, /* LOGICAL_MIN (0) */
312+ 0x25, 0x01, /* LOGICAL_MAX (1) */
313+ 0x95, 0x08, /* REPORT_COUNT (8) */
314+ 0x75, 0x01, /* REPORT_SIZE (1) */
315+ 0x81, 0x02, /* INPUT (data var abs) */
316+ 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
317+ 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
318+ 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
319+ 0x75, 0x0C, /* REPORT_SIZE (12) */
320+ 0x95, 0x02, /* REPORT_COUNT (2) */
321+ 0x09, 0x30, /* USAGE (X) */
322+ 0x09, 0x31, /* USAGE (Y) */
323+ 0x81, 0x06, /* INPUT */
324+ 0x15, 0x81, /* LOGICAL_MIN (-127) */
325+ 0x25, 0x7F, /* LOGICAL_MAX (127) */
326+ 0x75, 0x08, /* REPORT_SIZE (8) */
327+ 0x95, 0x01, /* REPORT_COUNT (1) */
328+ 0x09, 0x38, /* USAGE (wheel) */
329+ 0x81, 0x06, /* INPUT */
330+ 0x05, 0x0C, /* USAGE_PAGE(consumer) */
331+ 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
332+ 0x15, 0xF9, /* LOGICAL_MIN (-7) */
333+ 0x25, 0x07, /* LOGICAL_MAX (7) */
334+ 0x75, 0x04, /* REPORT_SIZE (4) */
335+ 0x95, 0x01, /* REPORT_COUNT (1) */
336+ 0x81, 0x06, /* INPUT */
337+ 0x05, 0x09, /* USAGE_PAGE (buttons) */
338+ 0x19, 0x09, /* USAGE_MIN (9) */
339+ 0x29, 0x0C, /* USAGE_MAX (12) */
340+ 0x15, 0x00, /* LOGICAL_MIN (0) */
341+ 0x25, 0x01, /* LOGICAL_MAX (1) */
342+ 0x75, 0x01, /* REPORT_SIZE (1) */
343+ 0x95, 0x04, /* REPORT_COUNT (4) */
344+ 0x81, 0x06, /* INPUT */
345+ 0xC0, /* END_COLLECTION */
346+ 0xC0, /* END_COLLECTION */
347+};
348+
349+/* Gaming Mouse descriptor (2) */
350+static const char mse_high_res_descriptor[] = {
351+ 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
352+ 0x09, 0x02, /* USAGE (Mouse) */
353+ 0xA1, 0x01, /* COLLECTION (Application) */
354+ 0x85, 0x02, /* REPORT_ID = 2 */
355+ 0x09, 0x01, /* USAGE (pointer) */
356+ 0xA1, 0x00, /* COLLECTION (physical) */
357+ 0x05, 0x09, /* USAGE_PAGE (buttons) */
358+ 0x19, 0x01, /* USAGE_MIN (1) */
359+ 0x29, 0x10, /* USAGE_MAX (16) */
360+ 0x15, 0x00, /* LOGICAL_MIN (0) */
361+ 0x25, 0x01, /* LOGICAL_MAX (1) */
362+ 0x95, 0x10, /* REPORT_COUNT (16) */
363+ 0x75, 0x01, /* REPORT_SIZE (1) */
364+ 0x81, 0x02, /* INPUT (data var abs) */
365+ 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
366+ 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
367+ 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
368+ 0x75, 0x10, /* REPORT_SIZE (16) */
369+ 0x95, 0x02, /* REPORT_COUNT (2) */
370+ 0x09, 0x30, /* USAGE (X) */
371+ 0x09, 0x31, /* USAGE (Y) */
372+ 0x81, 0x06, /* INPUT */
373+ 0x15, 0x81, /* LOGICAL_MIN (-127) */
374+ 0x25, 0x7F, /* LOGICAL_MAX (127) */
375+ 0x75, 0x08, /* REPORT_SIZE (8) */
376+ 0x95, 0x01, /* REPORT_COUNT (1) */
377+ 0x09, 0x38, /* USAGE (wheel) */
378+ 0x81, 0x06, /* INPUT */
379+ 0x05, 0x0C, /* USAGE_PAGE(consumer) */
380+ 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
381+ 0x95, 0x01, /* REPORT_COUNT (1) */
382+ 0x81, 0x06, /* INPUT */
383+ 0xC0, /* END_COLLECTION */
384+ 0xC0, /* END_COLLECTION */
385+};
386+
203387 /* Consumer Control descriptor (3) */
204388 static const char consumer_descriptor[] = {
205389 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
@@ -308,7 +492,7 @@ static const char hidpp_descriptor[] = { · 102 unmodified lines
308492 /* Make sure all descriptors are present here */
309493 #define MAX_RDESC_SIZE \
310494 (sizeof(kbd_descriptor) + \
311− sizeof(mse_descriptor) + \
495+ sizeof(mse_bluetooth_descriptor) + \
312496 sizeof(consumer_descriptor) + \
313497 sizeof(syscontrol_descriptor) + \
314498 sizeof(media_descriptor) + \
@@ -341,51 +525,160 @@ static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = { · 26 unmodified lines
341525 static struct hid_ll_driver logi_dj_ll_driver;
342526
343527 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
528+static void delayedwork_callback(struct work_struct *work);
529+
530+static LIST_HEAD(dj_hdev_list);
531+static DEFINE_MUTEX(dj_hdev_list_lock);
532+
533+/*
534+ * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
535+ * compatibility they have multiple USB interfaces. On HID++ receivers we need
536+ * to listen for input reports on both interfaces. The functions below are used
537+ * to create a single struct dj_receiver_dev for all interfaces belonging to
538+ * a single USB-device / receiver.
539+ */
540+static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
541+ enum recvr_type type)
542+{
543+ struct dj_receiver_dev *djrcv_dev;
544+ char sep;
545+
546+ /*
547+ * The bluetooth receiver contains a built-in hub and has separate
548+ * USB-devices for the keyboard and mouse interfaces.
549+ */
550+ sep = (type == recvr_type_bluetooth) ? '.' : '/';
551+
552+ /* Try to find an already-probed interface from the same device */
553+ list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
554+ if (djrcv_dev->mouse &&
555+ hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
556+ kref_get(&djrcv_dev->kref);
557+ return djrcv_dev;
558+ }
559+ if (djrcv_dev->keyboard &&
560+ hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
561+ kref_get(&djrcv_dev->kref);
562+ return djrcv_dev;
563+ }
564+ if (djrcv_dev->hidpp &&
565+ hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
566+ kref_get(&djrcv_dev->kref);
567+ return djrcv_dev;
568+ }
569+ }
570+
571+ return NULL;
572+}
573+
574+static void dj_release_receiver_dev(struct kref *kref)
575+{
576+ struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
577+
578+ list_del(&djrcv_dev->list);
579+ kfifo_free(&djrcv_dev->notif_fifo);
580+ kfree(djrcv_dev);
581+}
582+
583+static void dj_put_receiver_dev(struct hid_device *hdev)
584+{
585+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
586+
587+ mutex_lock(&dj_hdev_list_lock);
588+
589+ if (djrcv_dev->mouse == hdev)
590+ djrcv_dev->mouse = NULL;
591+ if (djrcv_dev->keyboard == hdev)
592+ djrcv_dev->keyboard = NULL;
593+ if (djrcv_dev->hidpp == hdev)
594+ djrcv_dev->hidpp = NULL;
595+
596+ kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
597+
598+ mutex_unlock(&dj_hdev_list_lock);
599+}
600+
601+static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
602+ enum recvr_type type,
603+ unsigned int application,
604+ bool is_hidpp)
605+{
606+ struct dj_receiver_dev *djrcv_dev;
607+
608+ mutex_lock(&dj_hdev_list_lock);
609+
610+ djrcv_dev = dj_find_receiver_dev(hdev, type);
611+ if (!djrcv_dev) {
612+ djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
613+ if (!djrcv_dev)
614+ goto out;
615+
616+ INIT_WORK(&djrcv_dev->work, delayedwork_callback);
617+ spin_lock_init(&djrcv_dev->lock);
618+ if (kfifo_alloc(&djrcv_dev->notif_fifo,
619+ DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
620+ GFP_KERNEL)) {
621+ kfree(djrcv_dev);
622+ djrcv_dev = NULL;
623+ goto out;
624+ }
625+ kref_init(&djrcv_dev->kref);
626+ list_add_tail(&djrcv_dev->list, &dj_hdev_list);
627+ djrcv_dev->last_query = jiffies;
628+ djrcv_dev->type = type;
629+ }
630+
631+ if (application == HID_GD_KEYBOARD)
632+ djrcv_dev->keyboard = hdev;
633+ if (application == HID_GD_MOUSE)
634+ djrcv_dev->mouse = hdev;
635+ if (is_hidpp)
636+ djrcv_dev->hidpp = hdev;
637+
638+ hid_set_drvdata(hdev, djrcv_dev);
639+out:
640+ mutex_unlock(&dj_hdev_list_lock);
641+ return djrcv_dev;
642+}
344643
345644 static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
346− struct dj_report *dj_report)
645+ struct dj_workitem *workitem)
347646 {
348647 /* Called in delayed work context */
349648 struct dj_device *dj_dev;
350649 unsigned long flags;
351650
352651 spin_lock_irqsave(&djrcv_dev->lock, flags);
353− dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
354− djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
652+ dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
653+ djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
355654 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
356655
357656 if (dj_dev != NULL) {
358657 hid_destroy_device(dj_dev->hdev);
359658 kfree(dj_dev);
360659 } else {
361− dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
660+ hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
362661 __func__);
363662 }
364663 }
365664
366665 static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
367− struct dj_report *dj_report)
666+ struct dj_workitem *workitem)
368667 {
369668 /* Called in delayed work context */
370− struct hid_device *djrcv_hdev = djrcv_dev->hdev;
371− struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
372− struct usb_device *usbdev = interface_to_usbdev(intf);
669+ struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
373670 struct hid_device *dj_hiddev;
374671 struct dj_device *dj_dev;
672+ u8 device_index = workitem->device_index;
673+ unsigned long flags;
375674
376675 /* Device index goes from 1 to 6, we need 3 bytes to store the
377676 * semicolon, the index, and a null terminator
378677 */
379678 unsigned char tmpstr[3];
380679
381− if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
382− SPFUNCTION_DEVICE_LIST_EMPTY) {
383− dbg_hid("%s: device list is empty\n", __func__);
384− djrcv_dev->querying_devices = false;
385− return;
386− }
387−
388− if (djrcv_dev->paired_dj_devices[dj_report->device_index]) {
680+ /* We are the only one ever adding a device, no need to lock */
681+ if (djrcv_dev->paired_dj_devices[device_index]) {
389682 /* The device is already known. No need to reallocate it. */
390683 dbg_hid("%s: device is already known\n", __func__);
391684 return;
@@ -393,8 +686,7 @@ static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev, · 1 unmodified line
393686
394687 dj_hiddev = hid_allocate_device();
395688 if (IS_ERR(dj_hiddev)) {
396− dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
397− __func__);
689+ hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
398690 return;
399691 }
400692
@@ -402,48 +694,67 @@ static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev, · 1 unmodified line
402694
403695 dj_hiddev->dev.parent = &djrcv_hdev->dev;
404696 dj_hiddev->bus = BUS_USB;
405− dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
406− dj_hiddev->product =
407− (dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]
408− << 8) |
409− dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
410− snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
411− "Logitech Unifying Device. Wireless PID:%04x",
412− dj_hiddev->product);
413−
414− dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
415−
416− usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
417− snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
697+ dj_hiddev->vendor = djrcv_hdev->vendor;
698+ dj_hiddev->product = (workitem->quad_id_msb << 8) |
699+ workitem->quad_id_lsb;
700+ if (workitem->device_type) {
701+ const char *type_str = "Device";
702+
703+ switch (workitem->device_type) {
704+ case 0x01: type_str = "Keyboard"; break;
705+ case 0x02: type_str = "Mouse"; break;
706+ case 0x03: type_str = "Numpad"; break;
707+ case 0x04: type_str = "Presenter"; break;
708+ case 0x07: type_str = "Remote Control"; break;
709+ case 0x08: type_str = "Trackball"; break;
710+ case 0x09: type_str = "Touchpad"; break;
711+ }
712+ snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
713+ "Logitech Wireless %s PID:%04x",
714+ type_str, dj_hiddev->product);
715+ } else {
716+ snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
717+ "Logitech Unifying Device. Wireless PID:%04x",
718+ dj_hiddev->product);
719+ }
720+
721+ if (djrcv_dev->type == recvr_type_27mhz)
722+ dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
723+ else
724+ dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
725+
726+ memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
727+ snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
418728 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
419729
420730 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
421731
422732 if (!dj_dev) {
423− dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
424− __func__);
733+ hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
425734 goto dj_device_allocate_fail;
426735 }
427736
428− dj_dev->reports_supported = get_unaligned_le32(
429− dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE);
737+ dj_dev->reports_supported = workitem->reports_supported;
430738 dj_dev->hdev = dj_hiddev;
431739 dj_dev->dj_receiver_dev = djrcv_dev;
432− dj_dev->device_index = dj_report->device_index;
740+ dj_dev->device_index = device_index;
433741 dj_hiddev->driver_data = dj_dev;
434742
435− djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
743+ spin_lock_irqsave(&djrcv_dev->lock, flags);
744+ djrcv_dev->paired_dj_devices[device_index] = dj_dev;
745+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
436746
437747 if (hid_add_device(dj_hiddev)) {
438− dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
439− __func__);
748+ hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
440749 goto hid_add_device_fail;
441750 }
442751
443752 return;
444753
445754 hid_add_device_fail:
446− djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
755+ spin_lock_irqsave(&djrcv_dev->lock, flags);
756+ djrcv_dev->paired_dj_devices[device_index] = NULL;
757+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
447758 kfree(dj_dev);
448759 dj_device_allocate_fail:
449760 hid_destroy_device(dj_hiddev);
@@ -454,7 +765,7 @@ static void delayedwork_callback(struct work_struct *work) · 4 unmodified lines
454765 struct dj_receiver_dev *djrcv_dev =
455766 container_of(work, struct dj_receiver_dev, work);
456767
457− struct dj_report dj_report;
768+ struct dj_workitem workitem;
458769 unsigned long flags;
459770 int count;
460771 int retval;
@@ -463,69 +774,231 @@ static void delayedwork_callback(struct work_struct *work) · 2 unmodified lines
463774
464775 spin_lock_irqsave(&djrcv_dev->lock, flags);
465776
466− count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
467− sizeof(struct dj_report));
468−
469− if (count != sizeof(struct dj_report)) {
470− dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
471− "notifications available\n", __func__);
777+ /*
778+ * Since we attach to multiple interfaces, we may get scheduled before
779+ * we are bound to the HID++ interface, catch this.
780+ */
781+ if (!djrcv_dev->ready) {
782+ pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
783+ __func__);
472784 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
473785 return;
474786 }
475787
476− if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
477− if (schedule_work(&djrcv_dev->work) == 0) {
478− dbg_hid("%s: did not schedule the work item, was "
479− "already queued\n", __func__);
480− }
788+ count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
789+
790+ if (count != sizeof(workitem)) {
791+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
792+ return;
481793 }
482794
795+ if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
796+ schedule_work(&djrcv_dev->work);
797+
483798 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
484799
485− switch (dj_report.report_type) {
486− case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
487− logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
800+ switch (workitem.type) {
801+ case WORKITEM_TYPE_PAIRED:
802+ logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
488803 break;
489− case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
490− logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
804+ case WORKITEM_TYPE_UNPAIRED:
805+ logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
491806 break;
492− default:
493− /* A normal report (i. e. not belonging to a pair/unpair notification)
494− * arriving here, means that the report arrived but we did not have a
495− * paired dj_device associated to the report's device_index, this
496− * means that the original "device paired" notification corresponding
497− * to this dj_device never arrived to this driver. The reason is that
498− * hid-core discards all packets coming from a device while probe() is
499− * executing. */
500− if (!djrcv_dev->paired_dj_devices[dj_report.device_index]) {
501− /* ok, we don't know the device, just re-ask the
502− * receiver for the list of connected devices. */
807+ case WORKITEM_TYPE_UNKNOWN:
503808 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
504− if (!retval) {
505− /* everything went fine, so just leave */
506− break;
507− }
508− dev_err(&djrcv_dev->hdev->dev,
509− "%s:logi_dj_recv_query_paired_devices "
510− "error:%d\n", __func__, retval);
809+ if (retval) {
810+ hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
811+ __func__, retval);
511812 }
512− dbg_hid("%s: unexpected report type\n", __func__);
813+ break;
814+ case WORKITEM_TYPE_EMPTY:
815+ dbg_hid("%s: device list is empty\n", __func__);
816+ break;
513817 }
514818 }
515819
820+/*
821+ * Sometimes we receive reports for which we do not have a paired dj_device
822+ * associated with the device_index or report-type to forward the report to.
823+ * This means that the original "device paired" notification corresponding
824+ * to the dj_device never arrived to this driver. Possible reasons for this are:
825+ * 1) hid-core discards all packets coming from a device during probe().
826+ * 2) if the receiver is plugged into a KVM switch then the pairing reports
827+ * are only forwarded to it if the focus is on this PC.
828+ * This function deals with this by re-asking the receiver for the list of
829+ * connected devices in the delayed work callback.
830+ * This function MUST be called with djrcv->lock held.
831+ */
832+static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
833+{
834+ struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
835+
836+ /* Rate limit queries done because of unhandeled reports to 2/sec */
837+ if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
838+ return;
839+
840+ kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
841+ schedule_work(&djrcv_dev->work);
842+}
843+
516844 static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
517845 struct dj_report *dj_report)
518846 {
519847 /* We are called from atomic context (tasklet && djrcv->lock held) */
848+ struct dj_workitem workitem = {
849+ .device_index = dj_report->device_index,
850+ };
851+
852+ switch (dj_report->report_type) {
853+ case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
854+ workitem.type = WORKITEM_TYPE_PAIRED;
855+ if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
856+ SPFUNCTION_DEVICE_LIST_EMPTY) {
857+ workitem.type = WORKITEM_TYPE_EMPTY;
858+ break;
859+ }
860+ /* fall-through */
861+ case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
862+ workitem.quad_id_msb =
863+ dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
864+ workitem.quad_id_lsb =
865+ dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
866+ workitem.reports_supported = get_unaligned_le32(
867+ dj_report->report_params +
868+ DEVICE_PAIRED_RF_REPORT_TYPE);
869+ workitem.reports_supported |= HIDPP;
870+ if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
871+ workitem.type = WORKITEM_TYPE_UNPAIRED;
872+ break;
873+ default:
874+ logi_dj_recv_queue_unknown_work(djrcv_dev);
875+ return;
876+ }
520877
521− kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
878+ kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
879+ schedule_work(&djrcv_dev->work);
880+}
522881
523− if (schedule_work(&djrcv_dev->work) == 0) {
524− dbg_hid("%s: did not schedule the work item, was already "
525− "queued\n", __func__);
882+static void logi_hidpp_dev_conn_notif_equad(struct hidpp_event *hidpp_report,
883+ struct dj_workitem *workitem)
884+{
885+ workitem->type = WORKITEM_TYPE_PAIRED;
886+ workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
887+ HIDPP_DEVICE_TYPE_MASK;
888+ workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
889+ workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
890+ switch (workitem->device_type) {
891+ case REPORT_TYPE_KEYBOARD:
892+ workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
893+ POWER_KEYS | MEDIA_CENTER |
894+ HIDPP;
895+ break;
896+ case REPORT_TYPE_MOUSE:
897+ workitem->reports_supported |= STD_MOUSE | HIDPP;
898+ break;
899+ }
900+}
901+
902+static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
903+ struct hidpp_event *hidpp_report,
904+ struct dj_workitem *workitem)
905+{
906+ workitem->type = WORKITEM_TYPE_PAIRED;
907+ workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
908+ switch (hidpp_report->device_index) {
909+ case 1: /* Index 1 is always a mouse */
910+ case 2: /* Index 2 is always a mouse */
911+ workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
912+ workitem->reports_supported |= STD_MOUSE | HIDPP;
913+ break;
914+ case 3: /* Index 3 is always the keyboard */
915+ case 4: /* Index 4 is used for an optional separate numpad */
916+ workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
917+ workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
918+ POWER_KEYS | HIDPP;
919+ break;
920+ default:
921+ hid_warn(hdev, "%s: unexpected device-index %d", __func__,
922+ hidpp_report->device_index);
526923 }
527924 }
528925
926+static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
927+ struct hidpp_event *hidpp_report)
928+{
929+ /* We are called from atomic context (tasklet && djrcv->lock held) */
930+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
931+ const char *device_type = "UNKNOWN";
932+ struct dj_workitem workitem = {
933+ .type = WORKITEM_TYPE_EMPTY,
934+ .device_index = hidpp_report->device_index,
935+ };
936+
937+ switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
938+ case 0x01:
939+ device_type = "Bluetooth";
940+ /* Bluetooth connect packet contents is the same as (e)QUAD */
941+ logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
942+ if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
943+ HIDPP_MANUFACTURER_MASK)) {
944+ hid_info(hdev, "Non Logitech device connected on slot %d\n",
945+ hidpp_report->device_index);
946+ workitem.reports_supported &= ~HIDPP;
947+ }
948+ break;
949+ case 0x02:
950+ device_type = "27 Mhz";
951+ logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
952+ break;
953+ case 0x03:
954+ device_type = "QUAD or eQUAD";
955+ logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
956+ break;
957+ case 0x04:
958+ device_type = "eQUAD step 4 DJ";
959+ logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
960+ break;
961+ case 0x05:
962+ device_type = "DFU Lite";
963+ break;
964+ case 0x06:
965+ device_type = "eQUAD step 4 Lite";
966+ logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
967+ break;
968+ case 0x07:
969+ device_type = "eQUAD step 4 Gaming";
970+ break;
971+ case 0x08:
972+ device_type = "eQUAD step 4 for gamepads";
973+ break;
974+ case 0x0a:
975+ device_type = "eQUAD nano Lite";
976+ logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
977+ break;
978+ case 0x0c:
979+ device_type = "eQUAD Lightspeed";
980+ logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
981+ workitem.reports_supported |= STD_KEYBOARD;
982+ break;
983+ }
984+
985+ if (workitem.type == WORKITEM_TYPE_EMPTY) {
986+ hid_warn(hdev,
987+ "unusable device of type %s (0x%02x) connected on slot %d",
988+ device_type,
989+ hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
990+ hidpp_report->device_index);
991+ return;
992+ }
993+
994+ hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
995+ device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
996+ hidpp_report->device_index);
997+
998+ kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
999+ schedule_work(&djrcv_dev->work);
1000+}
1001+
5291002 static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
5301003 struct dj_report *dj_report)
5311004 {
@@ -552,8 +1025,8 @@ static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev, · 20 unmodified lines
5521025 }
5531026 }
5541027
555−static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
556− struct dj_report *dj_report)
1028+static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
1029+ struct dj_report *dj_report)
5571030 {
5581031 /* We are called from atomic context (tasklet && djrcv->lock held) */
5591032 struct dj_device *dj_device;
@@ -573,18 +1046,48 @@ static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev, · 13 unmodified lines
5731046 }
5741047 }
5751048
576−static void logi_dj_recv_forward_hidpp(struct dj_device *dj_dev, u8 *data,
577− int size)
1049+static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
1050+ int size)
5781051 {
5791052 /* We are called from atomic context (tasklet && djrcv->lock held) */
5801053 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
5811054 dbg_hid("hid_input_report error\n");
5821055 }
5831056
1057+static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
1058+ u8 *data, int size)
1059+{
1060+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1061+ struct dj_device *dj_dev;
1062+ unsigned long flags;
1063+ u8 report = data[0];
1064+ int i;
1065+
1066+ if (report > REPORT_TYPE_RFREPORT_LAST) {
1067+ hid_err(hdev, "Unexpected input report number %d\n", report);
1068+ return;
1069+ }
1070+
1071+ spin_lock_irqsave(&djrcv_dev->lock, flags);
1072+ for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1073+ dj_dev = djrcv_dev->paired_dj_devices[i];
1074+ if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
1075+ logi_dj_recv_forward_report(dj_dev, data, size);
1076+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1077+ return;
1078+ }
1079+ }
1080+
1081+ logi_dj_recv_queue_unknown_work(djrcv_dev);
1082+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1083+
1084+ dbg_hid("No dj-devs handling input report number %d\n", report);
1085+}
1086+
5841087 static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
5851088 struct dj_report *dj_report)
5861089 {
587− struct hid_device *hdev = djrcv_dev->hdev;
1090+ struct hid_device *hdev = djrcv_dev->hidpp;
5881091 struct hid_report *report;
5891092 struct hid_report_enum *output_report_enum;
5901093 u8 *data = (u8 *)(&dj_report->device_index);
@@ -594,7 +1097,7 @@ static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, · 3 unmodified lines
5941097 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
5951098
5961099 if (!report) {
597− dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
1100+ hid_err(hdev, "%s: unable to find dj report\n", __func__);
5981101 return -ENODEV;
5991102 }
6001103
@@ -606,14 +1109,40 @@ static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, · 5 unmodified lines
6061109 return 0;
6071110 }
6081111
1112+static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1113+{
1114+ const u8 template[] = {REPORT_ID_HIDPP_SHORT,
1115+ HIDPP_RECEIVER_INDEX,
1116+ HIDPP_SET_REGISTER,
1117+ HIDPP_REG_CONNECTION_STATE,
1118+ HIDPP_FAKE_DEVICE_ARRIVAL,
1119+ 0x00, 0x00};
1120+ u8 *hidpp_report;
1121+ int retval;
1122+
1123+ hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1124+ if (!hidpp_report)
1125+ return -ENOMEM;
1126+
1127+ retval = hid_hw_raw_request(djrcv_dev->hidpp,
1128+ REPORT_ID_HIDPP_SHORT,
1129+ hidpp_report, sizeof(template),
1130+ HID_OUTPUT_REPORT,
1131+ HID_REQ_SET_REPORT);
1132+
1133+ kfree(hidpp_report);
1134+ return 0;
1135+}
1136+
6091137 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
6101138 {
6111139 struct dj_report *dj_report;
6121140 int retval;
6131141
614− /* no need to protect djrcv_dev->querying_devices */
615− if (djrcv_dev->querying_devices)
616− return 0;
1142+ djrcv_dev->last_query = jiffies;
1143+
1144+ if (djrcv_dev->type != recvr_type_dj)
1145+ return logi_dj_recv_query_hidpp_devices(djrcv_dev);
6171146
6181147 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
6191148 if (!dj_report)
@@ -630,27 +1159,33 @@ static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev) · 10 unmodified lines
6301159 static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
6311160 unsigned timeout)
6321161 {
633− struct hid_device *hdev = djrcv_dev->hdev;
1162+ struct hid_device *hdev = djrcv_dev->hidpp;
6341163 struct dj_report *dj_report;
6351164 u8 *buf;
636− int retval;
1165+ int retval = 0;
6371166
6381167 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
6391168 if (!dj_report)
6401169 return -ENOMEM;
641− dj_report->report_id = REPORT_ID_DJ_SHORT;
642− dj_report->device_index = 0xFF;
643− dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
644− dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
645− dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
646− retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
6471170
648− /*
649− * Ugly sleep to work around a USB 3.0 bug when the receiver is still
650− * processing the "switch-to-dj" command while we send an other command.
651− * 50 msec should gives enough time to the receiver to be ready.
652− */
653− msleep(50);
1171+ if (djrcv_dev->type == recvr_type_dj) {
1172+ dj_report->report_id = REPORT_ID_DJ_SHORT;
1173+ dj_report->device_index = 0xFF;
1174+ dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1175+ dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1176+ dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1177+ (u8)timeout;
1178+
1179+ retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1180+
1181+ /*
1182+ * Ugly sleep to work around a USB 3.0 bug when the receiver is
1183+ * still processing the "switch-to-dj" command while we send an
1184+ * other command.
1185+ * 50 msec should gives enough time to the receiver to be ready.
1186+ */
1187+ msleep(50);
1188+ }
6541189
6551190 /*
6561191 * Magical bits to set up hidpp notifications when the dj devices
@@ -682,22 +1217,28 @@ static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev, · 25 unmodified lines
6821217
6831218 static int logi_dj_ll_open(struct hid_device *hid)
6841219 {
685− dbg_hid("%s:%s\n", __func__, hid->phys);
1220+ dbg_hid("%s: %s\n", __func__, hid->phys);
6861221 return 0;
6871222
6881223 }
6891224
6901225 static void logi_dj_ll_close(struct hid_device *hid)
6911226 {
692− dbg_hid("%s:%s\n", __func__, hid->phys);
1227+ dbg_hid("%s: %s\n", __func__, hid->phys);
6931228 }
6941229
6951230 /*
6961231 * Register 0xB5 is "pairing information". It is solely intended for the
6971232 * receiver, so do not overwrite the device index.
6981233 */
699−static u8 unifying_pairing_query[] = {0x10, 0xff, 0x83, 0xb5};
700−static u8 unifying_pairing_answer[] = {0x11, 0xff, 0x83, 0xb5};
1234+static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1235+ HIDPP_RECEIVER_INDEX,
1236+ HIDPP_GET_LONG_REGISTER,
1237+ HIDPP_REG_PAIRING_INFORMATION };
1238+static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1239+ HIDPP_RECEIVER_INDEX,
1240+ HIDPP_GET_LONG_REGISTER,
1241+ HIDPP_REG_PAIRING_INFORMATION };
7011242
7021243 static int logi_dj_ll_raw_request(struct hid_device *hid,
7031244 unsigned char reportnum, __u8 *buf,
@@ -721,13 +1262,23 @@ static int logi_dj_ll_raw_request(struct hid_device *hid, · 17 unmodified lines
7211262 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
7221263 else
7231264 buf[1] = djdev->device_index;
724− return hid_hw_raw_request(djrcv_dev->hdev, reportnum, buf,
1265+ return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
7251266 count, report_type, reqtype);
7261267 }
7271268
7281269 if (buf[0] != REPORT_TYPE_LEDS)
7291270 return -EINVAL;
7301271
1272+ if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1273+ if (!djrcv_dev->keyboard) {
1274+ hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1275+ return 0;
1276+ }
1277+ /* usbhid overrides the report ID and ignores the first byte */
1278+ return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1279+ report_type, reqtype);
1280+ }
1281+
7311282 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
7321283 if (!out_buf)
7331284 return -ENOMEM;
@@ -739,7 +1290,7 @@ static int logi_dj_ll_raw_request(struct hid_device *hid, · 5 unmodified lines
7391290 out_buf[1] = djdev->device_index;
7401291 memcpy(out_buf + 2, buf, count);
7411292
742− ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf,
1293+ ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
7431294 DJREPORT_SHORT_LENGTH, report_type, reqtype);
7441295
7451296 kfree(out_buf);
@@ -769,41 +1320,55 @@ static int logi_dj_ll_parse(struct hid_device *hid) · 23 unmodified lines
7691320 return -ENOMEM;
7701321
7711322 if (djdev->reports_supported & STD_KEYBOARD) {
772− dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
1323+ dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
7731324 __func__, djdev->reports_supported);
7741325 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
7751326 }
7761327
7771328 if (djdev->reports_supported & STD_MOUSE) {
778− dbg_hid("%s: sending a mouse descriptor, reports_supported: "
779− "%x\n", __func__, djdev->reports_supported);
780− rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
1329+ dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
1330+ __func__, djdev->reports_supported);
1331+ if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp)
1332+ rdcat(rdesc, &rsize, mse_high_res_descriptor,
1333+ sizeof(mse_high_res_descriptor));
1334+ else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1335+ rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1336+ sizeof(mse_27mhz_descriptor));
1337+ else if (djdev->dj_receiver_dev->type == recvr_type_bluetooth)
1338+ rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
1339+ sizeof(mse_bluetooth_descriptor));
1340+ else
1341+ rdcat(rdesc, &rsize, mse_descriptor,
1342+ sizeof(mse_descriptor));
7811343 }
7821344
7831345 if (djdev->reports_supported & MULTIMEDIA) {
784− dbg_hid("%s: sending a multimedia report descriptor: %x\n",
1346+ dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
7851347 __func__, djdev->reports_supported);
7861348 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
7871349 }
7881350
7891351 if (djdev->reports_supported & POWER_KEYS) {
790− dbg_hid("%s: sending a power keys report descriptor: %x\n",
1352+ dbg_hid("%s: sending a power keys report descriptor: %llx\n",
7911353 __func__, djdev->reports_supported);
7921354 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
7931355 }
7941356
7951357 if (djdev->reports_supported & MEDIA_CENTER) {
796− dbg_hid("%s: sending a media center report descriptor: %x\n",
1358+ dbg_hid("%s: sending a media center report descriptor: %llx\n",
7971359 __func__, djdev->reports_supported);
7981360 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
7991361 }
8001362
8011363 if (djdev->reports_supported & KBD_LEDS) {
802− dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
1364+ dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
8031365 __func__, djdev->reports_supported);
8041366 }
8051367
806− rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor));
1368+ if (djdev->reports_supported & HIDPP) {
1369+ rdcat(rdesc, &rsize, hidpp_descriptor,
1370+ sizeof(hidpp_descriptor));
1371+ }
8071372
8081373 retval = hid_parse_report(hid, rdesc, rsize);
8091374 kfree(rdesc);
@@ -866,7 +1431,7 @@ static int logi_dj_dj_event(struct hid_device *hdev, · 56 unmodified lines
8661431 * so ignore those reports too.
8671432 */
8681433 if (dj_report->device_index != DJ_RECEIVER_INDEX)
869− dev_err(&hdev->dev, "%s: invalid device index:%d\n",
1434+ hid_err(hdev, "%s: invalid device index:%d\n",
8701435 __func__, dj_report->device_index);
8711436 return false;
8721437 }
@@ -893,7 +1458,7 @@ static int logi_dj_dj_event(struct hid_device *hdev, · 20 unmodified lines
8931458 }
8941459 break;
8951460 default:
896− logi_dj_recv_forward_report(djrcv_dev, dj_report);
1461+ logi_dj_recv_forward_dj(djrcv_dev, dj_report);
8971462 }
8981463
8991464 out:
@@ -907,9 +1472,10 @@ static int logi_dj_hidpp_event(struct hid_device *hdev, · 7 unmodified lines
9071472 int size)
9081473 {
9091474 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
910− struct dj_report *dj_report = (struct dj_report *) data;
1475+ struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
1476+ struct dj_device *dj_dev;
9111477 unsigned long flags;
912− u8 device_index = dj_report->device_index;
1478+ u8 device_index = hidpp_report->device_index;
9131479
9141480 if (device_index == HIDPP_RECEIVER_INDEX) {
9151481 /* special case were the device wants to know its unifying
@@ -937,21 +1503,42 @@ static int logi_dj_hidpp_event(struct hid_device *hdev, · 21 unmodified lines
9371503 * This driver can ignore safely the receiver notifications,
9381504 * so ignore those reports too.
9391505 */
940− dev_err(&hdev->dev, "%s: invalid device index:%d\n",
941− __func__, dj_report->device_index);
1506+ hid_err(hdev, "%s: invalid device index:%d\n", __func__,
1507+ hidpp_report->device_index);
9421508 return false;
9431509 }
9441510
9451511 spin_lock_irqsave(&djrcv_dev->lock, flags);
9461512
947− if (!djrcv_dev->paired_dj_devices[device_index])
948− /* received an event for an unknown device, bail out */
949− goto out;
1513+ dj_dev = djrcv_dev->paired_dj_devices[device_index];
1514+
1515+ /*
1516+ * With 27 MHz receivers, we do not get an explicit unpair event,
1517+ * remove the old device if the user has paired a *different* device.
1518+ */
1519+ if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1520+ hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1521+ hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1522+ hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1523+ dj_dev->hdev->product) {
1524+ struct dj_workitem workitem = {
1525+ .device_index = hidpp_report->device_index,
1526+ .type = WORKITEM_TYPE_UNPAIRED,
1527+ };
1528+ kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1529+ /* logi_hidpp_recv_queue_notif will queue the work */
1530+ dj_dev = NULL;
1531+ }
9501532
951− logi_dj_recv_forward_hidpp(djrcv_dev->paired_dj_devices[device_index],
952− data, size);
1533+ if (dj_dev) {
1534+ logi_dj_recv_forward_report(dj_dev, data, size);
1535+ } else {
1536+ if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1537+ logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1538+ else
1539+ logi_dj_recv_queue_unknown_work(djrcv_dev);
1540+ }
9531541
954−out:
9551542 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
9561543
9571544 return false;
@@ -961,112 +1548,176 @@ static int logi_dj_raw_event(struct hid_device *hdev, · 3 unmodified lines
9611548 struct hid_report *report, u8 *data,
9621549 int size)
9631550 {
1551+ struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
9641552 dbg_hid("%s, size:%d\n", __func__, size);
9651553
1554+ if (!djrcv_dev)
1555+ return 0;
1556+
1557+ if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1558+
1559+ if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1560+ /*
1561+ * For the keyboard, we can reuse the same report by
1562+ * using the second byte which is constant in the USB
1563+ * HID report descriptor.
1564+ */
1565+ data[1] = data[0];
1566+ data[0] = REPORT_TYPE_KEYBOARD;
1567+
1568+ logi_dj_recv_forward_input_report(hdev, data, size);
1569+
1570+ /* restore previous state */
1571+ data[0] = data[1];
1572+ data[1] = 0;
1573+ }
1574+ /* The 27 MHz mouse-only receiver sends unnumbered mouse data */
1575+ if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
1576+ size == 6) {
1577+ u8 mouse_report[7];
1578+
1579+ /* Prepend report id */
1580+ mouse_report[0] = REPORT_TYPE_MOUSE;
1581+ memcpy(mouse_report + 1, data, 6);
1582+ logi_dj_recv_forward_input_report(hdev, mouse_report, 7);
1583+ }
1584+
1585+ return false;
1586+ }
1587+
9661588 switch (data[0]) {
9671589 case REPORT_ID_DJ_SHORT:
9681590 if (size != DJREPORT_SHORT_LENGTH) {
969− dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
1591+ hid_err(hdev, "Short DJ report bad size (%d)", size);
1592+ return false;
1593+ }
1594+ return logi_dj_dj_event(hdev, report, data, size);
1595+ case REPORT_ID_DJ_LONG:
1596+ if (size != DJREPORT_LONG_LENGTH) {
1597+ hid_err(hdev, "Long DJ report bad size (%d)", size);
9701598 return false;
9711599 }
9721600 return logi_dj_dj_event(hdev, report, data, size);
9731601 case REPORT_ID_HIDPP_SHORT:
9741602 if (size != HIDPP_REPORT_SHORT_LENGTH) {
975− dev_err(&hdev->dev,
976− "Short HID++ report of bad size (%d)", size);
1603+ hid_err(hdev, "Short HID++ report bad size (%d)", size);
9771604 return false;
9781605 }
9791606 return logi_dj_hidpp_event(hdev, report, data, size);
9801607 case REPORT_ID_HIDPP_LONG:
9811608 if (size != HIDPP_REPORT_LONG_LENGTH) {
982− dev_err(&hdev->dev,
983− "Long HID++ report of bad size (%d)", size);
1609+ hid_err(hdev, "Long HID++ report bad size (%d)", size);
9841610 return false;
9851611 }
9861612 return logi_dj_hidpp_event(hdev, report, data, size);
9871613 }
9881614
1615+ logi_dj_recv_forward_input_report(hdev, data, size);
1616+
9891617 return false;
9901618 }
9911619
9921620 static int logi_dj_probe(struct hid_device *hdev,
9931621 const struct hid_device_id *id)
9941622 {
995− struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1623+ struct hid_report_enum *rep_enum;
1624+ struct hid_report *rep;
9961625 struct dj_receiver_dev *djrcv_dev;
1626+ struct usb_interface *intf;
1627+ unsigned int no_dj_interfaces = 0;
1628+ bool has_hidpp = false;
1629+ unsigned long flags;
9971630 int retval;
9981631
999− dbg_hid("%s called for ifnum %d\n", __func__,
1000− intf->cur_altsetting->desc.bInterfaceNumber);
1632+ /*
1633+ * Call to usbhid to fetch the HID descriptors of the current
1634+ * interface subsequently call to the hid/hid-core to parse the
1635+ * fetched descriptors.
1636+ */
1637+ retval = hid_parse(hdev);
1638+ if (retval) {
1639+ hid_err(hdev, "%s: parse failed\n", __func__);
1640+ return retval;
1641+ }
10011642
1002− /* Ignore interfaces 0 and 1, they will not carry any data, dont create
1003− * any hid_device for them */
1004− if (intf->cur_altsetting->desc.bInterfaceNumber !=
1005− LOGITECH_DJ_INTERFACE_NUMBER) {
1006− dbg_hid("%s: ignoring ifnum %d\n", __func__,
1007− intf->cur_altsetting->desc.bInterfaceNumber);
1643+ /*
1644+ * Some KVMs add an extra interface for e.g. mouse emulation. If we
1645+ * treat these as logitech-dj interfaces then this causes input events
1646+ * reported through this extra interface to not be reported correctly.
1647+ * To avoid this, we treat these as generic-hid devices.
1648+ */
1649+ switch (id->driver_data) {
1650+ case recvr_type_dj: no_dj_interfaces = 3; break;
1651+ case recvr_type_hidpp: no_dj_interfaces = 2; break;
1652+ case recvr_type_gaming_hidpp: no_dj_interfaces = 3; break;
1653+ case recvr_type_27mhz: no_dj_interfaces = 2; break;
1654+ case recvr_type_bluetooth: no_dj_interfaces = 2; break;
1655+ }
1656+ if (hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1657+ intf = to_usb_interface(hdev->dev.parent);
1658+ if (intf && intf->altsetting->desc.bInterfaceNumber >=
1659+ no_dj_interfaces) {
1660+ hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1661+ return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1662+ }
1663+ }
1664+
1665+ rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1666+
1667+ /* no input reports, bail out */
1668+ if (list_empty(&rep_enum->report_list))
10081669 return -ENODEV;
1670+
1671+ /*
1672+ * Check for the HID++ application.
1673+ * Note: we should theoretically check for HID++ and DJ
1674+ * collections, but this will do.
1675+ */
1676+ list_for_each_entry(rep, &rep_enum->report_list, list) {
1677+ if (rep->application == 0xff000001)
1678+ has_hidpp = true;
10091679 }
10101680
1011− /* Treat interface 2 */
1681+ /*
1682+ * Ignore interfaces without DJ/HID++ collection, they will not carry
1683+ * any data, dont create any hid_device for them.
1684+ */
1685+ if (!has_hidpp && id->driver_data == recvr_type_dj)
1686+ return -ENODEV;
10121687
1013− djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
1688+ /* get the current application attached to the node */
1689+ rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
1690+ djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
1691+ rep->application, has_hidpp);
10141692 if (!djrcv_dev) {
1015− dev_err(&hdev->dev,
1016− "%s:failed allocating dj_receiver_dev\n", __func__);
1693+ hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
10171694 return -ENOMEM;
10181695 }
1019− djrcv_dev->hdev = hdev;
1020− INIT_WORK(&djrcv_dev->work, delayedwork_callback);
1021− spin_lock_init(&djrcv_dev->lock);
1022− if (kfifo_alloc(&djrcv_dev->notif_fifo,
1023− DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
1024− GFP_KERNEL)) {
1025− dev_err(&hdev->dev,
1026− "%s:failed allocating notif_fifo\n", __func__);
1027− kfree(djrcv_dev);
1028− return -ENOMEM;
1029− }
1030− hid_set_drvdata(hdev, djrcv_dev);
10311696
1032− /* Call to usbhid to fetch the HID descriptors of interface 2 and
1033− * subsequently call to the hid/hid-core to parse the fetched
1034− * descriptors, this will in turn create the hidraw and hiddev nodes
1035− * for interface 2 of the receiver */
1036− retval = hid_parse(hdev);
1037− if (retval) {
1038− dev_err(&hdev->dev,
1039− "%s:parse of interface 2 failed\n", __func__);
1040− goto hid_parse_fail;
1041− }
1042−
1043− if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT,
1044− 0, DJREPORT_SHORT_LENGTH - 1)) {
1045− retval = -ENODEV;
1046− goto hid_parse_fail;
1047− }
1697+ if (!rep_enum->numbered)
1698+ djrcv_dev->unnumbered_application = rep->application;
10481699
10491700 /* Starts the usb device and connects to upper interfaces hiddev and
10501701 * hidraw */
1051− retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1702+ retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
10521703 if (retval) {
1053− dev_err(&hdev->dev,
1054− "%s:hid_hw_start returned error\n", __func__);
1704+ hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
10551705 goto hid_hw_start_fail;
10561706 }
10571707
1058− retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1059− if (retval < 0) {
1060− dev_err(&hdev->dev,
1061− "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1062− __func__, retval);
1063− goto switch_to_dj_mode_fail;
1708+ if (has_hidpp) {
1709+ retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1710+ if (retval < 0) {
1711+ hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1712+ __func__, retval);
1713+ goto switch_to_dj_mode_fail;
1714+ }
10641715 }
10651716
10661717 /* This is enabling the polling urb on the IN endpoint */
10671718 retval = hid_hw_open(hdev);
10681719 if (retval < 0) {
1069− dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1720+ hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
10701721 __func__, retval);
10711722 goto llopen_failed;
10721723 }
@@ -1074,11 +1725,16 @@ static int logi_dj_probe(struct hid_device *hdev, · 1 unmodified line
10741725 /* Allow incoming packets to arrive: */
10751726 hid_device_io_start(hdev);
10761727
1077− retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1078− if (retval < 0) {
1079− dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
1080− "error:%d\n", __func__, retval);
1081− goto logi_dj_recv_query_paired_devices_failed;
1728+ if (has_hidpp) {
1729+ spin_lock_irqsave(&djrcv_dev->lock, flags);
1730+ djrcv_dev->ready = true;
1731+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1732+ retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1733+ if (retval < 0) {
1734+ hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1735+ __func__, retval);
1736+ goto logi_dj_recv_query_paired_devices_failed;
1737+ }
10821738 }
10831739
10841740 return retval;
@@ -1091,12 +1747,8 @@ switch_to_dj_mode_fail: · 6 unmodified lines
10911747 hid_hw_stop(hdev);
10921748
10931749 hid_hw_start_fail:
1094−hid_parse_fail:
1095− kfifo_free(&djrcv_dev->notif_fifo);
1096− kfree(djrcv_dev);
1097− hid_set_drvdata(hdev, NULL);
1750+ dj_put_receiver_dev(hdev);
10981751 return retval;
1099−
11001752 }
11011753
11021754 #ifdef CONFIG_PM
@@ -1105,10 +1757,12 @@ static int logi_dj_reset_resume(struct hid_device *hdev) · 2 unmodified lines
11051757 int retval;
11061758 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
11071759
1760+ if (!djrcv_dev || djrcv_dev->hidpp != hdev)
1761+ return 0;
1762+
11081763 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
11091764 if (retval < 0) {
1110− dev_err(&hdev->dev,
1111− "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1765+ hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
11121766 __func__, retval);
11131767 }
11141768
@@ -1120,39 +1774,83 @@ static void logi_dj_remove(struct hid_device *hdev) · 5 unmodified lines
11201774 {
11211775 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
11221776 struct dj_device *dj_dev;
1777+ unsigned long flags;
11231778 int i;
11241779
11251780 dbg_hid("%s\n", __func__);
11261781
1782+ if (!djrcv_dev)
1783+ return hid_hw_stop(hdev);
1784+
1785+ /*
1786+ * This ensures that if the work gets requeued from another
1787+ * interface of the same receiver it will be a no-op.
1788+ */
1789+ spin_lock_irqsave(&djrcv_dev->lock, flags);
1790+ djrcv_dev->ready = false;
1791+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1792+
11271793 cancel_work_sync(&djrcv_dev->work);
11281794
11291795 hid_hw_close(hdev);
11301796 hid_hw_stop(hdev);
11311797
1132− /* I suppose that at this point the only context that can access
1133− * the djrecv_data is this thread as the work item is guaranteed to
1134− * have finished and no more raw_event callbacks should arrive after
1135− * the remove callback was triggered so no locks are put around the
1136− * code below */
1798+ /*
1799+ * For proper operation we need access to all interfaces, so we destroy
1800+ * the paired devices when we're unbound from any interface.
1801+ *
1802+ * Note we may still be bound to other interfaces, sharing the same
1803+ * djrcv_dev, so we need locking here.
1804+ */
11371805 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1806+ spin_lock_irqsave(&djrcv_dev->lock, flags);
11381807 dj_dev = djrcv_dev->paired_dj_devices[i];
1808+ djrcv_dev->paired_dj_devices[i] = NULL;
1809+ spin_unlock_irqrestore(&djrcv_dev->lock, flags);
11391810 if (dj_dev != NULL) {
11401811 hid_destroy_device(dj_dev->hdev);
11411812 kfree(dj_dev);
1142− djrcv_dev->paired_dj_devices[i] = NULL;
11431813 }
11441814 }
11451815
1146− kfifo_free(&djrcv_dev->notif_fifo);
1147− kfree(djrcv_dev);
1148− hid_set_drvdata(hdev, NULL);
1816+ dj_put_receiver_dev(hdev);
11491817 }
11501818
11511819 static const struct hid_device_id logi_dj_receivers[] = {
11521820 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1153− USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
1821+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1822+ .driver_data = recvr_type_dj},
11541823 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1155− USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
1824+ USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1825+ .driver_data = recvr_type_dj},
1826+ { /* Logitech Nano (non DJ) receiver */
1827+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1828+ USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
1829+ .driver_data = recvr_type_hidpp},
1830+ { /* Logitech Nano (non DJ) receiver */
1831+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1832+ USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1833+ .driver_data = recvr_type_hidpp},
1834+ { /* Logitech gaming receiver (0xc539) */
1835+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1836+ USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_GAMING),
1837+ .driver_data = recvr_type_gaming_hidpp},
1838+ { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
1839+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1840+ USB_DEVICE_ID_S510_RECEIVER_2),
1841+ .driver_data = recvr_type_27mhz},
1842+ { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
1843+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1844+ USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
1845+ .driver_data = recvr_type_27mhz},
1846+ { /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. */
1847+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1848+ 0xc70e),
1849+ .driver_data = recvr_type_bluetooth},
1850+ { /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. */
1851+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1852+ 0xc70a),
1853+ .driver_data = recvr_type_bluetooth},
11561854 {}
11571855 };
11581856
drivers/hid/hid-logitech-hidpp.c+537−199
@@ -51,7 +51,11 @@ MODULE_PARM_DESC(disable_tap_to_click, · 50 unmodified lines
5151
5252 #define HIDPP_REPORT_SHORT_LENGTH 7
5353 #define HIDPP_REPORT_LONG_LENGTH 20
54−#define HIDPP_REPORT_VERY_LONG_LENGTH 64
54+#define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
55+
56+#define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
57+#define HIDPP_SUB_ID_ROLLER 0x05
58+#define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
5559
5660 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
5761 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
@@ -68,6 +72,13 @@ MODULE_PARM_DESC(disable_tap_to_click, · 10 unmodified lines
6872 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
6973 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
7074 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
75+#define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
76+#define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
77+#define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
78+
79+/* These are just aliases for now */
80+#define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
81+#define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
7182
7283 /* Convenience constant to check for any high-res support. */
7384 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
@@ -106,13 +117,13 @@ MODULE_PARM_DESC(disable_tap_to_click, · 32 unmodified lines
106117 struct fap {
107118 u8 feature_index;
108119 u8 funcindex_clientid;
109− u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
120+ u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
110121 };
111122
112123 struct rap {
113124 u8 sub_id;
114125 u8 reg_address;
115− u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
126+ u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
116127 };
117128
118129 struct hidpp_report {
@@ -149,7 +160,6 @@ struct hidpp_battery { · 30 unmodified lines
149160 * @last_time: last event time, used to reset remainder after inactivity
150161 */
151162 struct hidpp_scroll_counter {
152− struct input_dev *dev;
153163 int wheel_multiplier;
154164 int remainder;
155165 int direction;
@@ -158,10 +168,12 @@ struct hidpp_scroll_counter { · 2 unmodified lines
158168
159169 struct hidpp_device {
160170 struct hid_device *hid_dev;
171+ struct input_dev *input;
161172 struct mutex send_mutex;
162173 void *send_receive_buf;
163174 char *name; /* will never be NULL and should not be freed */
164175 wait_queue_head_t wait;
176+ int very_long_report_length;
165177 bool answer_available;
166178 u8 protocol_major;
167179 u8 protocol_minor;
@@ -206,8 +218,6 @@ static int __hidpp_send_report(struct hid_device *hdev, · 38 unmodified lines
206218 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
207219 int fields_count, ret;
208220
209− hidpp = hid_get_drvdata(hdev);
210−
211221 switch (hidpp_report->report_id) {
212222 case REPORT_ID_HIDPP_SHORT:
213223 fields_count = HIDPP_REPORT_SHORT_LENGTH;
@@ -216,7 +226,7 @@ static int __hidpp_send_report(struct hid_device *hdev, · 2 unmodified lines
216226 fields_count = HIDPP_REPORT_LONG_LENGTH;
217227 break;
218228 case REPORT_ID_HIDPP_VERY_LONG:
219− fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
229+ fields_count = hidpp->very_long_report_length;
220230 break;
221231 default:
222232 return -ENODEV;
@@ -342,7 +352,7 @@ static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev, · 119 unmodified lines
342352 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
343353 break;
344354 case REPORT_ID_HIDPP_VERY_LONG:
345− max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
355+ max_count = hidpp_dev->very_long_report_length - 4;
346356 break;
347357 default:
348358 return -EINVAL;
@@ -434,14 +444,15 @@ static void hidpp_prefix_name(char **name, int name_length) · 85 unmodified lines
434444 * emit low-resolution scroll events when appropriate for
435445 * backwards-compatibility with userspace input libraries.
436446 */
437−static void hidpp_scroll_counter_handle_scroll(struct hidpp_scroll_counter *counter,
447+static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
448+ struct hidpp_scroll_counter *counter,
438449 int hi_res_value)
439450 {
440451 int low_res_value, remainder, direction;
441452 unsigned long long now, previous;
442453
443454 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
444− input_report_rel(counter->dev, REL_WHEEL_HI_RES, hi_res_value);
455+ input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
445456
446457 remainder = counter->remainder;
447458 direction = hi_res_value > 0 ? 1 : -1;
@@ -475,7 +486,7 @@ static void hidpp_scroll_counter_handle_scroll(struct hidpp_scroll_counter *coun · 27 unmodified lines
475486 low_res_value = remainder / 120;
476487 if (low_res_value == 0)
477488 low_res_value = (hi_res_value > 0 ? 1 : -1);
478− input_report_rel(counter->dev, REL_WHEEL, low_res_value);
489+ input_report_rel(input_dev, REL_WHEEL, low_res_value);
479490 remainder -= low_res_value * 120;
480491 }
481492 counter->remainder = remainder;
@@ -491,14 +502,16 @@ static void hidpp_scroll_counter_handle_scroll(struct hidpp_scroll_counter *coun · 9 unmodified lines
491502 #define HIDPP_GET_LONG_REGISTER 0x83
492503
493504 /**
494− * hidpp10_set_register_bit() - Sets a single bit in a HID++ 1.0 register.
505+ * hidpp10_set_register - Modify a HID++ 1.0 register.
495506 * @hidpp_dev: the device to set the register on.
496507 * @register_address: the address of the register to modify.
497508 * @byte: the byte of the register to modify. Should be less than 3.
509+ * @mask: mask of the bits to modify
510+ * @value: new values for the bits in mask
498511 * Return: 0 if successful, otherwise a negative error code.
499512 */
500−static int hidpp10_set_register_bit(struct hidpp_device *hidpp_dev,
501− u8 register_address, u8 byte, u8 bit)
513+static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
514+ u8 register_address, u8 byte, u8 mask, u8 value)
502515 {
503516 struct hidpp_report response;
504517 int ret;
@@ -514,7 +527,8 @@ static int hidpp10_set_register_bit(struct hidpp_device *hidpp_dev, · 9 unmodified lines
514527
515528 memcpy(params, response.rap.params, 3);
516529
517− params[byte] |= BIT(bit);
530+ params[byte] &= ~mask;
531+ params[byte] |= value & mask;
518532
519533 return hidpp_send_rap_command_sync(hidpp_dev,
520534 REPORT_ID_HIDPP_SHORT,
@@ -523,20 +537,28 @@ static int hidpp10_set_register_bit(struct hidpp_device *hidpp_dev, · 2 unmodified lines
523537 params, 3, &response);
524538 }
525539
526−
527−#define HIDPP_REG_GENERAL 0x00
540+#define HIDPP_REG_ENABLE_REPORTS 0x00
541+#define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
542+#define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
543+#define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
544+#define HIDPP_ENABLE_BAT_REPORT BIT(4)
545+#define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
528546
529547 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
530548 {
531− return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_GENERAL, 0, 4);
549+ return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
550+ HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
532551 }
533552
534553 #define HIDPP_REG_FEATURES 0x01
554+#define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
555+#define HIDPP_ENABLE_FAST_SCROLL BIT(6)
535556
536557 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
537558 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
538559 {
539− return hidpp10_set_register_bit(hidpp_dev, HIDPP_REG_FEATURES, 0, 6);
560+ return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
561+ HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
540562 }
541563
542564 #define HIDPP_REG_BATTERY_STATUS 0x07
@@ -741,6 +763,9 @@ static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev) · 198 unmodified lines
741763 if (2 + len > sizeof(response.rap.params))
742764 return NULL;
743765
766+ if (len < 4) /* logitech devices are usually at least Xddd */
767+ return NULL;
768+
744769 name = kzalloc(len + 1, GFP_KERNEL);
745770 if (!name)
746771 return NULL;
@@ -836,18 +861,21 @@ static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature, · 89 unmodified lines
836861
837862 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
838863 {
864+ const u8 ping_byte = 0x5a;
865+ u8 ping_data[3] = { 0, 0, ping_byte };
839866 struct hidpp_report response;
840867 int ret;
841868
842− ret = hidpp_send_fap_command_sync(hidpp,
869+ ret = hidpp_send_rap_command_sync(hidpp,
870+ REPORT_ID_HIDPP_SHORT,
843871 HIDPP_PAGE_ROOT_IDX,
844872 CMD_ROOT_GET_PROTOCOL_VERSION,
845− NULL, 0, &response);
873+ ping_data, sizeof(ping_data), &response);
846874
847875 if (ret == HIDPP_ERROR_INVALID_SUBID) {
848876 hidpp->protocol_major = 1;
849877 hidpp->protocol_minor = 0;
850− return 0;
878+ goto print_version;
851879 }
852880
853881 /* the device might not be connected */
@@ -862,21 +890,19 @@ static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp) · 8 unmodified lines
862890 if (ret)
863891 return ret;
864892
865− hidpp->protocol_major = response.fap.params[0];
866− hidpp->protocol_minor = response.fap.params[1];
893+ if (response.rap.params[2] != ping_byte) {
894+ hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
895+ __func__, response.rap.params[2], ping_byte);
896+ return -EPROTO;
897+ }
867898
868− return ret;
869−}
899+ hidpp->protocol_major = response.rap.params[0];
900+ hidpp->protocol_minor = response.rap.params[1];
870901
871−static bool hidpp_is_connected(struct hidpp_device *hidpp)
872−{
873− int ret;
874−
875− ret = hidpp_root_get_protocol_version(hidpp);
876− if (!ret)
877− hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
878− hidpp->protocol_major, hidpp->protocol_minor);
879− return ret == 0;
902+print_version:
903+ hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
904+ hidpp->protocol_major, hidpp->protocol_minor);
905+ return 0;
880906 }
881907
882908 /* -------------------------------------------------------------------------- */
@@ -932,7 +958,7 @@ static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp, · 49 unmodified lines
932958
933959 switch (response.report_id) {
934960 case REPORT_ID_HIDPP_VERY_LONG:
935− count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
961+ count = hidpp->very_long_report_length - 4;
936962 break;
937963 case REPORT_ID_HIDPP_LONG:
938964 count = HIDPP_REPORT_LONG_LENGTH - 4;
@@ -1012,7 +1038,11 @@ static int hidpp_map_battery_level(int capacity) · 73 unmodified lines
10121038 {
10131039 if (capacity < 11)
10141040 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1015− else if (capacity < 31)
1041+ /*
1042+ * The spec says this should be < 31 but some devices report 30
1043+ * with brand new batteries and Windows reports 30 as "Good".
1044+ */
1045+ else if (capacity < 30)
10161046 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
10171047 else if (capacity < 81)
10181048 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
@@ -2211,7 +2241,6 @@ static int hidpp_ff_deinit(struct hid_device *hid) · 1192 unmodified lines
22112241 #define WTP_MANUAL_RESOLUTION 39
22122242
22132243 struct wtp_data {
2214− struct input_dev *input;
22152244 u16 x_size, y_size;
22162245 u8 finger_count;
22172246 u8 mt_feature_index;
@@ -2229,7 +2258,7 @@ static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi, · 11 unmodified lines
22292258 }
22302259
22312260 static void wtp_populate_input(struct hidpp_device *hidpp,
2232− struct input_dev *input_dev, bool origin_is_hid_core)
2261+ struct input_dev *input_dev)
22332262 {
22342263 struct wtp_data *wd = hidpp->private_data;
22352264
@@ -2255,31 +2284,30 @@ static void wtp_populate_input(struct hidpp_device *hidpp, · 19 unmodified lines
22552284
22562285 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
22572286 INPUT_MT_DROP_UNUSED);
2258−
2259− wd->input = input_dev;
22602287 }
22612288
2262−static void wtp_touch_event(struct wtp_data *wd,
2289+static void wtp_touch_event(struct hidpp_device *hidpp,
22632290 struct hidpp_touchpad_raw_xy_finger *touch_report)
22642291 {
2292+ struct wtp_data *wd = hidpp->private_data;
22652293 int slot;
22662294
22672295 if (!touch_report->finger_id || touch_report->contact_type)
22682296 /* no actual data */
22692297 return;
22702298
2271− slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
2299+ slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
22722300
2273− input_mt_slot(wd->input, slot);
2274− input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
2301+ input_mt_slot(hidpp->input, slot);
2302+ input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
22752303 touch_report->contact_status);
22762304 if (touch_report->contact_status) {
2277− input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
2305+ input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
22782306 touch_report->x);
2279− input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
2307+ input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
22802308 wd->flip_y ? wd->y_size - touch_report->y :
22812309 touch_report->y);
2282− input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
2310+ input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
22832311 touch_report->area);
22842312 }
22852313 }
@@ -2287,19 +2315,18 @@ static void wtp_touch_event(struct wtp_data *wd, · 1 unmodified line
22872315 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
22882316 struct hidpp_touchpad_raw_xy *raw)
22892317 {
2290− struct wtp_data *wd = hidpp->private_data;
22912318 int i;
22922319
22932320 for (i = 0; i < 2; i++)
2294− wtp_touch_event(wd, &(raw->fingers[i]));
2321+ wtp_touch_event(hidpp, &(raw->fingers[i]));
22952322
22962323 if (raw->end_of_frame &&
22972324 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2298− input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
2325+ input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
22992326
23002327 if (raw->end_of_frame || raw->finger_count <= 2) {
2301− input_mt_sync_frame(wd->input);
2302− input_sync(wd->input);
2328+ input_mt_sync_frame(hidpp->input);
2329+ input_sync(hidpp->input);
23032330 }
23042331 }
23052332
@@ -2349,7 +2376,7 @@ static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size) · 43 unmodified lines
23492376 struct hidpp_report *report = (struct hidpp_report *)data;
23502377 struct hidpp_touchpad_raw_xy raw;
23512378
2352− if (!wd || !wd->input)
2379+ if (!wd || !hidpp->input)
23532380 return 1;
23542381
23552382 switch (data[0]) {
@@ -2360,11 +2387,11 @@ static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size) · 4 unmodified lines
23602387 return 1;
23612388 }
23622389 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2363− input_event(wd->input, EV_KEY, BTN_LEFT,
2390+ input_event(hidpp->input, EV_KEY, BTN_LEFT,
23642391 !!(data[1] & 0x01));
2365− input_event(wd->input, EV_KEY, BTN_RIGHT,
2392+ input_event(hidpp->input, EV_KEY, BTN_RIGHT,
23662393 !!(data[1] & 0x02));
2367− input_sync(wd->input);
2394+ input_sync(hidpp->input);
23682395 return 0;
23692396 } else {
23702397 if (size < 21)
@@ -2482,10 +2509,6 @@ static int wtp_connect(struct hid_device *hdev, bool connected) · 111 unmodified lines
24822509
24832510 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
24842511
2485−struct m560_private_data {
2486− struct input_dev *input;
2487−};
2488−
24892512 /* how buttons are mapped in the report */
24902513 #define M560_MOUSE_BTN_LEFT 0x01
24912514 #define M560_MOUSE_BTN_RIGHT 0x02
@@ -2513,28 +2536,12 @@ static int m560_send_config_command(struct hid_device *hdev, bool connected) · 21 unmodified lines
25132536 );
25142537 }
25152538
2516−static int m560_allocate(struct hid_device *hdev)
2517−{
2518− struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2519− struct m560_private_data *d;
2520−
2521− d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
2522− GFP_KERNEL);
2523− if (!d)
2524− return -ENOMEM;
2525−
2526− hidpp->private_data = d;
2527−
2528− return 0;
2529−};
2530−
25312539 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
25322540 {
25332541 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2534− struct m560_private_data *mydata = hidpp->private_data;
25352542
25362543 /* sanity check */
2537− if (!mydata || !mydata->input) {
2544+ if (!hidpp->input) {
25382545 hid_err(hdev, "error in parameter\n");
25392546 return -EINVAL;
25402547 }
@@ -2561,24 +2568,24 @@ static int m560_raw_event(struct hid_device *hdev, u8 *data, int size) · 20 unmodified lines
25612568
25622569 switch (data[5]) {
25632570 case 0xaf:
2564− input_report_key(mydata->input, BTN_MIDDLE, 1);
2571+ input_report_key(hidpp->input, BTN_MIDDLE, 1);
25652572 break;
25662573 case 0xb0:
2567− input_report_key(mydata->input, BTN_FORWARD, 1);
2574+ input_report_key(hidpp->input, BTN_FORWARD, 1);
25682575 break;
25692576 case 0xae:
2570− input_report_key(mydata->input, BTN_BACK, 1);
2577+ input_report_key(hidpp->input, BTN_BACK, 1);
25712578 break;
25722579 case 0x00:
2573− input_report_key(mydata->input, BTN_BACK, 0);
2574− input_report_key(mydata->input, BTN_FORWARD, 0);
2575− input_report_key(mydata->input, BTN_MIDDLE, 0);
2580+ input_report_key(hidpp->input, BTN_BACK, 0);
2581+ input_report_key(hidpp->input, BTN_FORWARD, 0);
2582+ input_report_key(hidpp->input, BTN_MIDDLE, 0);
25762583 break;
25772584 default:
25782585 hid_err(hdev, "error in report\n");
25792586 return 0;
25802587 }
2581− input_sync(mydata->input);
2588+ input_sync(hidpp->input);
25822589
25832590 } else if (data[0] == 0x02) {
25842591 /*
@@ -2592,59 +2599,55 @@ static int m560_raw_event(struct hid_device *hdev, u8 *data, int size) · 7 unmodified lines
25922599
25932600 int v;
25942601
2595− input_report_key(mydata->input, BTN_LEFT,
2602+ input_report_key(hidpp->input, BTN_LEFT,
25962603 !!(data[1] & M560_MOUSE_BTN_LEFT));
2597− input_report_key(mydata->input, BTN_RIGHT,
2604+ input_report_key(hidpp->input, BTN_RIGHT,
25982605 !!(data[1] & M560_MOUSE_BTN_RIGHT));
25992606
26002607 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
2601− input_report_rel(mydata->input, REL_HWHEEL, -1);
2602− input_report_rel(mydata->input, REL_HWHEEL_HI_RES,
2608+ input_report_rel(hidpp->input, REL_HWHEEL, -1);
2609+ input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
26032610 -120);
26042611 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
2605− input_report_rel(mydata->input, REL_HWHEEL, 1);
2606− input_report_rel(mydata->input, REL_HWHEEL_HI_RES,
2612+ input_report_rel(hidpp->input, REL_HWHEEL, 1);
2613+ input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
26072614 120);
26082615 }
26092616
26102617 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2611− input_report_rel(mydata->input, REL_X, v);
2618+ input_report_rel(hidpp->input, REL_X, v);
26122619
26132620 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2614− input_report_rel(mydata->input, REL_Y, v);
2621+ input_report_rel(hidpp->input, REL_Y, v);
26152622
26162623 v = hid_snto32(data[6], 8);
26172624 if (v != 0)
2618− hidpp_scroll_counter_handle_scroll(
2625+ hidpp_scroll_counter_handle_scroll(hidpp->input,
26192626 &hidpp->vertical_wheel_counter, v);
26202627
2621− input_sync(mydata->input);
2628+ input_sync(hidpp->input);
26222629 }
26232630
26242631 return 1;
26252632 }
26262633
26272634 static void m560_populate_input(struct hidpp_device *hidpp,
2628− struct input_dev *input_dev, bool origin_is_hid_core)
2635+ struct input_dev *input_dev)
26292636 {
2630− struct m560_private_data *mydata = hidpp->private_data;
2631−
2632− mydata->input = input_dev;
2633−
2634− __set_bit(EV_KEY, mydata->input->evbit);
2635− __set_bit(BTN_MIDDLE, mydata->input->keybit);
2636− __set_bit(BTN_RIGHT, mydata->input->keybit);
2637− __set_bit(BTN_LEFT, mydata->input->keybit);
2638− __set_bit(BTN_BACK, mydata->input->keybit);
2639− __set_bit(BTN_FORWARD, mydata->input->keybit);
2637+ __set_bit(EV_KEY, input_dev->evbit);
2638+ __set_bit(BTN_MIDDLE, input_dev->keybit);
2639+ __set_bit(BTN_RIGHT, input_dev->keybit);
2640+ __set_bit(BTN_LEFT, input_dev->keybit);
2641+ __set_bit(BTN_BACK, input_dev->keybit);
2642+ __set_bit(BTN_FORWARD, input_dev->keybit);
26402643
2641− __set_bit(EV_REL, mydata->input->evbit);
2642− __set_bit(REL_X, mydata->input->relbit);
2643− __set_bit(REL_Y, mydata->input->relbit);
2644− __set_bit(REL_WHEEL, mydata->input->relbit);
2645− __set_bit(REL_HWHEEL, mydata->input->relbit);
2646− __set_bit(REL_WHEEL_HI_RES, mydata->input->relbit);
2647− __set_bit(REL_HWHEEL_HI_RES, mydata->input->relbit);
2644+ __set_bit(EV_REL, input_dev->evbit);
2645+ __set_bit(REL_X, input_dev->relbit);
2646+ __set_bit(REL_Y, input_dev->relbit);
2647+ __set_bit(REL_WHEEL, input_dev->relbit);
2648+ __set_bit(REL_HWHEEL, input_dev->relbit);
2649+ __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2650+ __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
26482651 }
26492652
26502653 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
@@ -2746,6 +2749,175 @@ static int g920_get_config(struct hidpp_device *hidpp) · 95 unmodified lines
27462749 return 0;
27472750 }
27482751
2752+/* -------------------------------------------------------------------------- */
2753+/* HID++1.0 devices which use HID++ reports for their wheels */
2754+/* -------------------------------------------------------------------------- */
2755+static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
2756+{
2757+ return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2758+ HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
2759+ HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
2760+}
2761+
2762+static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
2763+ u8 *data, int size)
2764+{
2765+ s8 value, hvalue;
2766+
2767+ if (!hidpp->input)
2768+ return -EINVAL;
2769+
2770+ if (size < 7)
2771+ return 0;
2772+
2773+ if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
2774+ return 0;
2775+
2776+ value = data[3];
2777+ hvalue = data[4];
2778+
2779+ input_report_rel(hidpp->input, REL_WHEEL, value);
2780+ input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
2781+ input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
2782+ input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
2783+ input_sync(hidpp->input);
2784+
2785+ return 1;
2786+}
2787+
2788+static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
2789+ struct input_dev *input_dev)
2790+{
2791+ __set_bit(EV_REL, input_dev->evbit);
2792+ __set_bit(REL_WHEEL, input_dev->relbit);
2793+ __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2794+ __set_bit(REL_HWHEEL, input_dev->relbit);
2795+ __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
2796+}
2797+
2798+/* -------------------------------------------------------------------------- */
2799+/* HID++1.0 mice which use HID++ reports for extra mouse buttons */
2800+/* -------------------------------------------------------------------------- */
2801+static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
2802+{
2803+ return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2804+ HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
2805+ HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
2806+}
2807+
2808+static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
2809+ u8 *data, int size)
2810+{
2811+ int i;
2812+
2813+ if (!hidpp->input)
2814+ return -EINVAL;
2815+
2816+ if (size < 7)
2817+ return 0;
2818+
2819+ if (data[0] != REPORT_ID_HIDPP_SHORT ||
2820+ data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
2821+ return 0;
2822+
2823+ /*
2824+ * Buttons are either delivered through the regular mouse report *or*
2825+ * through the extra buttons report. At least for button 6 how it is
2826+ * delivered differs per receiver firmware version. Even receivers with
2827+ * the same usb-id show different behavior, so we handle both cases.
2828+ */
2829+ for (i = 0; i < 8; i++)
2830+ input_report_key(hidpp->input, BTN_MOUSE + i,
2831+ (data[3] & (1 << i)));
2832+
2833+ /* Some mice report events on button 9+, use BTN_MISC */
2834+ for (i = 0; i < 8; i++)
2835+ input_report_key(hidpp->input, BTN_MISC + i,
2836+ (data[4] & (1 << i)));
2837+
2838+ input_sync(hidpp->input);
2839+ return 1;
2840+}
2841+
2842+static void hidpp10_extra_mouse_buttons_populate_input(
2843+ struct hidpp_device *hidpp, struct input_dev *input_dev)
2844+{
2845+ /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
2846+ __set_bit(BTN_0, input_dev->keybit);
2847+ __set_bit(BTN_1, input_dev->keybit);
2848+ __set_bit(BTN_2, input_dev->keybit);
2849+ __set_bit(BTN_3, input_dev->keybit);
2850+ __set_bit(BTN_4, input_dev->keybit);
2851+ __set_bit(BTN_5, input_dev->keybit);
2852+ __set_bit(BTN_6, input_dev->keybit);
2853+ __set_bit(BTN_7, input_dev->keybit);
2854+}
2855+
2856+/* -------------------------------------------------------------------------- */
2857+/* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
2858+/* -------------------------------------------------------------------------- */
2859+
2860+/* Find the consumer-page input report desc and change Maximums to 0x107f */
2861+static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
2862+ u8 *_rdesc, unsigned int *rsize)
2863+{
2864+ /* Note 0 terminated so we can use strnstr to search for this. */
2865+ const char consumer_rdesc_start[] = {
2866+ 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
2867+ 0x09, 0x01, /* USAGE (Consumer Control) */
2868+ 0xA1, 0x01, /* COLLECTION (Application) */
2869+ 0x85, 0x03, /* REPORT_ID = 3 */
2870+ 0x75, 0x10, /* REPORT_SIZE (16) */
2871+ 0x95, 0x02, /* REPORT_COUNT (2) */
2872+ 0x15, 0x01, /* LOGICAL_MIN (1) */
2873+ 0x26, 0x00 /* LOGICAL_MAX (... */
2874+ };
2875+ char *consumer_rdesc, *rdesc = (char *)_rdesc;
2876+ unsigned int size;
2877+
2878+ consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
2879+ size = *rsize - (consumer_rdesc - rdesc);
2880+ if (consumer_rdesc && size >= 25) {
2881+ consumer_rdesc[15] = 0x7f;
2882+ consumer_rdesc[16] = 0x10;
2883+ consumer_rdesc[20] = 0x7f;
2884+ consumer_rdesc[21] = 0x10;
2885+ }
2886+ return _rdesc;
2887+}
2888+
2889+static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
2890+{
2891+ return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2892+ HIDPP_ENABLE_CONSUMER_REPORT,
2893+ HIDPP_ENABLE_CONSUMER_REPORT);
2894+}
2895+
2896+static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
2897+ u8 *data, int size)
2898+{
2899+ u8 consumer_report[5];
2900+
2901+ if (size < 7)
2902+ return 0;
2903+
2904+ if (data[0] != REPORT_ID_HIDPP_SHORT ||
2905+ data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
2906+ return 0;
2907+
2908+ /*
2909+ * Build a normal consumer report (3) out of the data, this detour
2910+ * is necessary to get some keyboards to report their 0x10xx usages.
2911+ */
2912+ consumer_report[0] = 0x03;
2913+ memcpy(&consumer_report[1], &data[3], 4);
2914+ /* We are called from atomic context */
2915+ hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
2916+ consumer_report, 5, 1);
2917+
2918+ return 1;
2919+}
2920+
27492921 /* -------------------------------------------------------------------------- */
27502922 /* High-resolution scroll wheels */
27512923 /* -------------------------------------------------------------------------- */
@@ -2781,12 +2953,31 @@ static int hi_res_scroll_enable(struct hidpp_device *hidpp) · 29 unmodified lines
27812953 /* Generic HID++ devices */
27822954 /* -------------------------------------------------------------------------- */
27832955
2956+static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
2957+ unsigned int *rsize)
2958+{
2959+ struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2960+
2961+ if (!hidpp)
2962+ return rdesc;
2963+
2964+ /* For 27 MHz keyboards the quirk gets set after hid_parse. */
2965+ if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
2966+ (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
2967+ rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
2968+
2969+ return rdesc;
2970+}
2971+
27842972 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
27852973 struct hid_field *field, struct hid_usage *usage,
27862974 unsigned long **bit, int *max)
27872975 {
27882976 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
27892977
2978+ if (!hidpp)
2979+ return 0;
2980+
27902981 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
27912982 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
27922983 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
@@ -2802,6 +2993,9 @@ static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi, · 9 unmodified lines
28022993 {
28032994 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
28042995
2996+ if (!hidpp)
2997+ return 0;
2998+
28052999 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
28063000 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
28073001 if (usage->type == EV_ABS && (usage->code == ABS_X ||
@@ -2816,15 +3010,20 @@ static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi, · 8 unmodified lines
28163010
28173011
28183012 static void hidpp_populate_input(struct hidpp_device *hidpp,
2819− struct input_dev *input, bool origin_is_hid_core)
3013+ struct input_dev *input)
28203014 {
3015+ hidpp->input = input;
3016+
28213017 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2822− wtp_populate_input(hidpp, input, origin_is_hid_core);
3018+ wtp_populate_input(hidpp, input);
28233019 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2824− m560_populate_input(hidpp, input, origin_is_hid_core);
3020+ m560_populate_input(hidpp, input);
28253021
2826− if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
2827− hidpp->vertical_wheel_counter.dev = input;
3022+ if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3023+ hidpp10_wheel_populate_input(hidpp, input);
3024+
3025+ if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3026+ hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
28283027 }
28293028
28303029 static int hidpp_input_configured(struct hid_device *hdev,
@@ -2833,7 +3032,10 @@ static int hidpp_input_configured(struct hid_device *hdev, · 2 unmodified lines
28333032 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
28343033 struct input_dev *input = hidinput->input;
28353034
2836− hidpp_populate_input(hidpp, input, true);
3035+ if (!hidpp)
3036+ return 0;
3037+
3038+ hidpp_populate_input(hidpp, input);
28373039
28383040 return 0;
28393041 }
@@ -2893,6 +3095,24 @@ static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, · 53 unmodified lines
28933095 return ret;
28943096 }
28953097
3098+ if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3099+ ret = hidpp10_wheel_raw_event(hidpp, data, size);
3100+ if (ret != 0)
3101+ return ret;
3102+ }
3103+
3104+ if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3105+ ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3106+ if (ret != 0)
3107+ return ret;
3108+ }
3109+
3110+ if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3111+ ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3112+ if (ret != 0)
3113+ return ret;
3114+ }
3115+
28963116 return 0;
28973117 }
28983118
@@ -2902,10 +3122,13 @@ static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report, · 3 unmodified lines
29023122 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
29033123 int ret = 0;
29043124
3125+ if (!hidpp)
3126+ return 0;
3127+
29053128 /* Generic HID++ processing. */
29063129 switch (data[0]) {
29073130 case REPORT_ID_HIDPP_VERY_LONG:
2908− if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
3131+ if (size != hidpp->very_long_report_length) {
29093132 hid_err(hdev, "received hid++ report of bad size (%d)",
29103133 size);
29113134 return 1;
@@ -2950,17 +3173,22 @@ static int hidpp_event(struct hid_device *hdev, struct hid_field *field, · 38 unmodified lines
29503173 * restriction imposed in hidpp_usages.
29513174 */
29523175 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2953− struct hidpp_scroll_counter *counter = &hidpp->vertical_wheel_counter;
3176+ struct hidpp_scroll_counter *counter;
3177+
3178+ if (!hidpp)
3179+ return 0;
3180+
3181+ counter = &hidpp->vertical_wheel_counter;
29543182 /* A scroll event may occur before the multiplier has been retrieved or
29553183 * the input device set, or high-res scroll enabling may fail. In such
29563184 * cases we must return early (falling back to default behaviour) to
29573185 * avoid a crash in hidpp_scroll_counter_handle_scroll.
29583186 */
29593187 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
2960− || counter->dev == NULL || counter->wheel_multiplier == 0)
3188+ || hidpp->input == NULL || counter->wheel_multiplier == 0)
29613189 return 0;
29623190
2963− hidpp_scroll_counter_handle_scroll(counter, value);
3191+ hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
29643192 return 1;
29653193 }
29663194
@@ -3132,32 +3360,45 @@ static void hidpp_connect_event(struct hidpp_device *hidpp) · 165 unmodified lines
31323360 return;
31333361 }
31343362
3363+ if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3364+ ret = hidpp10_wheel_connect(hidpp);
3365+ if (ret)
3366+ return;
3367+ }
3368+
3369+ if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3370+ ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3371+ if (ret)
3372+ return;
3373+ }
3374+
3375+ if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3376+ ret = hidpp10_consumer_keys_connect(hidpp);
3377+ if (ret)
3378+ return;
3379+ }
3380+
31353381 /* the device is already connected, we can ask for its name and
31363382 * protocol */
31373383 if (!hidpp->protocol_major) {
3138− ret = !hidpp_is_connected(hidpp);
3384+ ret = hidpp_root_get_protocol_version(hidpp);
31393385 if (ret) {
31403386 hid_err(hdev, "Can not get the protocol version.\n");
31413387 return;
31423388 }
3143− hid_info(hdev, "HID++ %u.%u device connected.\n",
3144− hidpp->protocol_major, hidpp->protocol_minor);
31453389 }
31463390
31473391 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
31483392 name = hidpp_get_device_name(hidpp);
3149− if (!name) {
3150− hid_err(hdev,
3151− "unable to retrieve the name of the device");
3152− return;
3153− }
3154−
3155− devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
3156− kfree(name);
3157− if (!devm_name)
3158− return;
3393+ if (name) {
3394+ devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3395+ "%s", name);
3396+ kfree(name);
3397+ if (!devm_name)
3398+ return;
31593399
3160− hidpp->name = devm_name;
3400+ hidpp->name = devm_name;
3401+ }
31613402 }
31623403
31633404 hidpp_initialize_battery(hidpp);
@@ -3188,7 +3429,7 @@ static void hidpp_connect_event(struct hidpp_device *hidpp) · 24 unmodified lines
31883429 return;
31893430 }
31903431
3191− hidpp_populate_input(hidpp, input, false);
3432+ hidpp_populate_input(hidpp, input);
31923433
31933434 ret = input_register_device(input);
31943435 if (ret)
@@ -3208,6 +3449,60 @@ static const struct attribute_group ps_attribute_group = { · 13 unmodified lines
32083449 .attrs = sysfs_attrs
32093450 };
32103451
3452+static int hidpp_get_report_length(struct hid_device *hdev, int id)
3453+{
3454+ struct hid_report_enum *re;
3455+ struct hid_report *report;
3456+
3457+ re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3458+ report = re->report_id_hash[id];
3459+ if (!report)
3460+ return 0;
3461+
3462+ return report->field[0]->report_count + 1;
3463+}
3464+
3465+static bool hidpp_validate_report(struct hid_device *hdev, int id,
3466+ int expected_length, bool optional)
3467+{
3468+ int report_length;
3469+
3470+ if (id >= HID_MAX_IDS || id < 0) {
3471+ hid_err(hdev, "invalid HID report id %u\n", id);
3472+ return false;
3473+ }
3474+
3475+ report_length = hidpp_get_report_length(hdev, id);
3476+ if (!report_length)
3477+ return optional;
3478+
3479+ if (report_length < expected_length) {
3480+ hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3481+ return false;
3482+ }
3483+
3484+ return true;
3485+}
3486+
3487+static bool hidpp_validate_device(struct hid_device *hdev)
3488+{
3489+ return hidpp_validate_report(hdev, REPORT_ID_HIDPP_SHORT,
3490+ HIDPP_REPORT_SHORT_LENGTH, false) &&
3491+ hidpp_validate_report(hdev, REPORT_ID_HIDPP_LONG,
3492+ HIDPP_REPORT_LONG_LENGTH, true);
3493+}
3494+
3495+static bool hidpp_application_equals(struct hid_device *hdev,
3496+ unsigned int application)
3497+{
3498+ struct list_head *report_list;
3499+ struct hid_report *report;
3500+
3501+ report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
3502+ report = list_first_entry_or_null(report_list, struct hid_report, list);
3503+ return report && report->application == application;
3504+}
3505+
32113506 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
32123507 {
32133508 struct hidpp_device *hidpp;
@@ -3215,20 +3510,48 @@ static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) · 1 unmodified line
32153510 bool connected;
32163511 unsigned int connect_mask = HID_CONNECT_DEFAULT;
32173512
3218− hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
3219− GFP_KERNEL);
3513+ /* report_fixup needs drvdata to be set before we call hid_parse */
3514+ hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
32203515 if (!hidpp)
32213516 return -ENOMEM;
32223517
32233518 hidpp->hid_dev = hdev;
32243519 hidpp->name = hdev->name;
3520+ hidpp->quirks = id->driver_data;
32253521 hid_set_drvdata(hdev, hidpp);
32263522
3227− hidpp->quirks = id->driver_data;
3523+ ret = hid_parse(hdev);
3524+ if (ret) {
3525+ hid_err(hdev, "%s:parse failed\n", __func__);
3526+ return ret;
3527+ }
3528+
3529+ /*
3530+ * Make sure the device is HID++ capable, otherwise treat as generic HID
3531+ */
3532+ if (!hidpp_validate_device(hdev)) {
3533+ hid_set_drvdata(hdev, NULL);
3534+ devm_kfree(&hdev->dev, hidpp);
3535+ return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
3536+ }
3537+
3538+ hidpp->very_long_report_length =
3539+ hidpp_get_report_length(hdev, REPORT_ID_HIDPP_VERY_LONG);
3540+ if (hidpp->very_long_report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3541+ hidpp->very_long_report_length = HIDPP_REPORT_VERY_LONG_MAX_LENGTH;
32283542
32293543 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
32303544 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
32313545
3546+ if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3547+ hidpp_application_equals(hdev, HID_GD_MOUSE))
3548+ hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
3549+ HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
3550+
3551+ if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3552+ hidpp_application_equals(hdev, HID_GD_KEYBOARD))
3553+ hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
3554+
32323555 if (disable_raw_mode) {
32333556 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
32343557 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
@@ -3237,15 +3560,11 @@ static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) · 2 unmodified lines
32373560 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
32383561 ret = wtp_allocate(hdev, id);
32393562 if (ret)
3240− goto allocate_fail;
3241− } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3242− ret = m560_allocate(hdev);
3243− if (ret)
3244− goto allocate_fail;
3563+ return ret;
32453564 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
32463565 ret = k400_allocate(hdev);
32473566 if (ret)
3248− goto allocate_fail;
3567+ return ret;
32493568 }
32503569
32513570 INIT_WORK(&hidpp->work, delayed_work_cb);
@@ -3258,93 +3577,79 @@ static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) · 6 unmodified lines
32583577 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
32593578 hdev->name);
32603579
3261− ret = hid_parse(hdev);
3580+ /*
3581+ * Plain USB connections need to actually call start and open
3582+ * on the transport driver to allow incoming data.
3583+ */
3584+ ret = hid_hw_start(hdev, 0);
32623585 if (ret) {
3263− hid_err(hdev, "%s:parse failed\n", __func__);
3264− goto hid_parse_fail;
3586+ hid_err(hdev, "hw start failed\n");
3587+ goto hid_hw_start_fail;
32653588 }
32663589
3267− if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3268− connect_mask &= ~HID_CONNECT_HIDINPUT;
3269−
3270− if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3271− ret = hid_hw_start(hdev, connect_mask);
3272− if (ret) {
3273− hid_err(hdev, "hw start failed\n");
3274− goto hid_hw_start_fail;
3275− }
3276− ret = hid_hw_open(hdev);
3277− if (ret < 0) {
3278− dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3279− __func__, ret);
3280− hid_hw_stop(hdev);
3281− goto hid_hw_start_fail;
3282− }
3590+ ret = hid_hw_open(hdev);
3591+ if (ret < 0) {
3592+ dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3593+ __func__, ret);
3594+ hid_hw_stop(hdev);
3595+ goto hid_hw_open_fail;
32833596 }
32843597
3285−
32863598 /* Allow incoming packets */
32873599 hid_device_io_start(hdev);
32883600
32893601 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
32903602 hidpp_unifying_init(hidpp);
32913603
3292− connected = hidpp_is_connected(hidpp);
3604+ connected = hidpp_root_get_protocol_version(hidpp) == 0;
32933605 atomic_set(&hidpp->connected, connected);
32943606 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
32953607 if (!connected) {
32963608 ret = -ENODEV;
32973609 hid_err(hdev, "Device not connected");
3298− goto hid_hw_open_failed;
3610+ goto hid_hw_init_fail;
32993611 }
33003612
3301− hid_info(hdev, "HID++ %u.%u device connected.\n",
3302− hidpp->protocol_major, hidpp->protocol_minor);
3303−
33043613 hidpp_overwrite_name(hdev);
33053614 }
33063615
33073616 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
33083617 ret = wtp_get_config(hidpp);
33093618 if (ret)
3310− goto hid_hw_open_failed;
3619+ goto hid_hw_init_fail;
33113620 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
33123621 ret = g920_get_config(hidpp);
33133622 if (ret)
3314− goto hid_hw_open_failed;
3623+ goto hid_hw_init_fail;
33153624 }
33163625
3317− /* Block incoming packets */
3318− hid_device_io_stop(hdev);
3626+ hidpp_connect_event(hidpp);
33193627
3320− if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3321− ret = hid_hw_start(hdev, connect_mask);
3322− if (ret) {
3323− hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3324− goto hid_hw_start_fail;
3325− }
3326− }
3628+ /* Reset the HID node state */
3629+ hid_device_io_stop(hdev);
3630+ hid_hw_close(hdev);
3631+ hid_hw_stop(hdev);
33273632
3328− /* Allow incoming packets */
3329− hid_device_io_start(hdev);
3633+ if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3634+ connect_mask &= ~HID_CONNECT_HIDINPUT;
33303635
3331− hidpp_connect_event(hidpp);
3636+ /* Now export the actual inputs and hidraw nodes to the world */
3637+ ret = hid_hw_start(hdev, connect_mask);
3638+ if (ret) {
3639+ hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3640+ goto hid_hw_start_fail;
3641+ }
33323642
33333643 return ret;
33343644
3335−hid_hw_open_failed:
3336− hid_device_io_stop(hdev);
3337− if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3338− hid_hw_close(hdev);
3339− hid_hw_stop(hdev);
3340− }
3645+hid_hw_init_fail:
3646+ hid_hw_close(hdev);
3647+hid_hw_open_fail:
3648+ hid_hw_stop(hdev);
33413649 hid_hw_start_fail:
3342−hid_parse_fail:
33433650 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
33443651 cancel_work_sync(&hidpp->work);
33453652 mutex_destroy(&hidpp->send_mutex);
3346−allocate_fail:
3347− hid_set_drvdata(hdev, NULL);
33483653 return ret;
33493654 }
33503655
@@ -3352,12 +3657,14 @@ static void hidpp_remove(struct hid_device *hdev) · 1 unmodified line
33523657 {
33533658 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
33543659
3660+ if (!hidpp)
3661+ return hid_hw_stop(hdev);
3662+
33553663 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
33563664
3357− if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3665+ if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)
33583666 hidpp_ff_deinit(hdev);
3359− hid_hw_close(hdev);
3360− }
3667+
33613668 hid_hw_stop(hdev);
33623669 cancel_work_sync(&hidpp->work);
33633670 mutex_destroy(&hidpp->send_mutex);
@@ -3367,6 +3674,10 @@ static void hidpp_remove(struct hid_device *hdev) · 3 unmodified lines
33673674 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
33683675 USB_VENDOR_ID_LOGITECH, (product))
33693676
3677+#define L27MHZ_DEVICE(product) \
3678+ HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
3679+ USB_VENDOR_ID_LOGITECH, (product))
3680+
33703681 static const struct hid_device_id hidpp_devices[] = {
33713682 { /* wireless touchpad */
33723683 LDJ_DEVICE(0x4011),
@@ -3418,11 +3729,37 @@ static const struct hid_device_id hidpp_devices[] = { · 45 unmodified lines
34183729 { /* Solar Keyboard Logitech K750 */
34193730 LDJ_DEVICE(0x4002),
34203731 .driver_data = HIDPP_QUIRK_CLASS_K750 },
3732+ { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
3733+ LDJ_DEVICE(0xb305),
3734+ .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
34213735
34223736 { LDJ_DEVICE(HID_ANY_ID) },
34233737
3424− { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
3738+ { /* Keyboard LX501 (Y-RR53) */
3739+ L27MHZ_DEVICE(0x0049),
3740+ .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
3741+ { /* Keyboard MX3000 (Y-RAM74) */
3742+ L27MHZ_DEVICE(0x0057),
3743+ .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
3744+ { /* Keyboard MX3200 (Y-RAV80) */
3745+ L27MHZ_DEVICE(0x005c),
3746+ .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
3747+
3748+ { L27MHZ_DEVICE(HID_ANY_ID) },
3749+
3750+ { /* Logitech G403 Gaming Mouse over USB */
3751+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
3752+ { /* Logitech G700 Gaming Mouse over USB */
3753+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC06B) },
3754+ { /* Logitech G900 Gaming Mouse over USB */
3755+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
3756+ { /* Logitech G920 Wheel over USB */
3757+ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
34253758 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
3759+
3760+ { /* MX5000 keyboard over Bluetooth */
3761+ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
3762+ .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
34263763 {}
34273764 };
34283765
@@ -3436,6 +3773,7 @@ static const struct hid_usage_id hidpp_usages[] = { · 7 unmodified lines
34363773 static struct hid_driver hidpp_driver = {
34373774 .name = "logitech-hidpp-device",
34383775 .id_table = hidpp_devices,
3776+ .report_fixup = hidpp_report_fixup,
34393777 .probe = hidpp_probe,
34403778 .remove = hidpp_remove,
34413779 .raw_event = hidpp_raw_event,
drivers/hid/hid-macally.c+45−0
@@ -0,0 +1,45 @@
1+// SPDX-License-Identifier: GPL-2.0+
2+/*
3+ * HID driver for quirky Macally devices
4+ *
5+ * Copyright (c) 2019 Alex Henrie <alexhenrie24@gmail.com>
6+ */
7+
8+#include <linux/hid.h>
9+#include <linux/module.h>
10+
11+#include "hid-ids.h"
12+
13+MODULE_AUTHOR("Alex Henrie <alexhenrie24@gmail.com>");
14+MODULE_DESCRIPTION("Macally devices");
15+MODULE_LICENSE("GPL");
16+
17+/*
18+ * The Macally ikey keyboard says that its logical and usage maximums are both
19+ * 101, but the power key is 102 and the equals key is 103
20+ */
21+static __u8 *macally_report_fixup(struct hid_device *hdev, __u8 *rdesc,
22+ unsigned int *rsize)
23+{
24+ if (*rsize >= 60 && rdesc[53] == 0x65 && rdesc[59] == 0x65) {
25+ hid_info(hdev,
26+ "fixing up Macally ikey keyboard report descriptor\n");
27+ rdesc[53] = rdesc[59] = 0x67;
28+ }
29+ return rdesc;
30+}
31+
32+static struct hid_device_id macally_id_table[] = {
33+ { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
34+ USB_DEVICE_ID_MACALLY_IKEY_KEYBOARD) },
35+ { }
36+};
37+MODULE_DEVICE_TABLE(hid, macally_id_table);
38+
39+static struct hid_driver macally_driver = {
40+ .name = "macally",
41+ .id_table = macally_id_table,
42+ .report_fixup = macally_report_fixup,
43+};
44+
45+module_hid_driver(macally_driver);
drivers/hid/hid-picolcd_core.c+6−12
@@ -28,6 +28,7 @@ · 27 unmodified lines
2828 #include <linux/completion.h>
2929 #include <linux/uaccess.h>
3030 #include <linux/module.h>
31+#include <linux/string.h>
3132
3233 #include "hid-picolcd.h"
3334
@@ -275,27 +276,20 @@ static ssize_t picolcd_operation_mode_store(struct device *dev, · 241 unmodified lines
275276 {
276277 struct picolcd_data *data = dev_get_drvdata(dev);
277278 struct hid_report *report = NULL;
278− size_t cnt = count;
279279 int timeout = data->opmode_delay;
280280 unsigned long flags;
281281
282− if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
282+ if (sysfs_streq(buf, "lcd")) {
283283 if (data->status & PICOLCD_BOOTLOADER)
284284 report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev);
285− buf += 3;
286− cnt -= 3;
287− } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) {
285+ } else if (sysfs_streq(buf, "bootloader")) {
288286 if (!(data->status & PICOLCD_BOOTLOADER))
289287 report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev);
290− buf += 10;
291− cnt -= 10;
292− }
293− if (!report || report->maxfield != 1)
288+ } else {
294289 return -EINVAL;
290+ }
295291
296− while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
297− cnt--;
298− if (cnt != 0)
292+ if (!report || report->maxfield != 1)
299293 return -EINVAL;
300294
301295 spin_lock_irqsave(&data->lock, flags);
drivers/hid/hid-quirks.c+0−6
@@ -432,7 +432,6 @@ static const struct hid_device_id hid_have_special_driver[] = { · 431 unmodified lines
432432 #if IS_ENABLED(CONFIG_HID_LOGITECH)
433433 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER) },
434434 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER) },
435− { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2) },
436435 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER) },
437436 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP) },
438437 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE) },
@@ -464,13 +463,8 @@ static const struct hid_device_id hid_have_special_driver[] = { · 25 unmodified lines
464463 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR) },
465464 #endif
466465 #if IS_ENABLED(CONFIG_HID_LOGITECH_HIDPP)
467− { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_T651) },
468466 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL) },
469467 #endif
470−#if IS_ENABLED(CONFIG_HID_LOGITECH_DJ)
471− { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER) },
472− { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2) },
473−#endif
474468 #if IS_ENABLED(CONFIG_HID_MAGICMOUSE)
475469 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE) },
476470 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICTRACKPAD) },
drivers/hid/hid-sensor-custom.c+4−8
@@ -157,8 +157,7 @@ static int usage_id_cmp(const void *p1, const void *p2) · 156 unmodified lines
157157 static ssize_t enable_sensor_show(struct device *dev,
158158 struct device_attribute *attr, char *buf)
159159 {
160− struct platform_device *pdev = to_platform_device(dev);
161− struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
160+ struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
162161
163162 return sprintf(buf, "%d\n", sensor_inst->enable);
164163 }
@@ -237,8 +236,7 @@ static ssize_t enable_sensor_store(struct device *dev, · 72 unmodified lines
237236 struct device_attribute *attr,
238237 const char *buf, size_t count)
239238 {
240− struct platform_device *pdev = to_platform_device(dev);
241− struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
239+ struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
242240 int value;
243241 int ret = -EINVAL;
244242
@@ -283,8 +281,7 @@ static const struct attribute_group enable_sensor_attr_group = { · 38 unmodified lines
283281 static ssize_t show_value(struct device *dev, struct device_attribute *attr,
284282 char *buf)
285283 {
286− struct platform_device *pdev = to_platform_device(dev);
287− struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
284+ struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
288285 struct hid_sensor_hub_attribute_info *attribute;
289286 int index, usage, field_index;
290287 char name[HID_CUSTOM_NAME_LENGTH];
@@ -392,8 +389,7 @@ static ssize_t show_value(struct device *dev, struct device_attribute *attr, · 101 unmodified lines
392389 static ssize_t store_value(struct device *dev, struct device_attribute *attr,
393390 const char *buf, size_t count)
394391 {
395− struct platform_device *pdev = to_platform_device(dev);
396− struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
392+ struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
397393 int index, field_index, usage;
398394 char name[HID_CUSTOM_NAME_LENGTH];
399395 int value;
drivers/hid/hid-u2fzero.c+374−0
@@ -0,0 +1,374 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * U2F Zero LED and RNG driver
4+ *
5+ * Copyright 2018 Andrej Shadura <andrew@shadura.me>
6+ * Loosely based on drivers/hid/hid-led.c
7+ * and drivers/usb/misc/chaoskey.c
8+ *
9+ * This program is free software; you can redistribute it and/or
10+ * modify it under the terms of the GNU General Public License as
11+ * published by the Free Software Foundation, version 2.
12+ */
13+
14+#include <linux/hid.h>
15+#include <linux/hidraw.h>
16+#include <linux/hw_random.h>
17+#include <linux/leds.h>
18+#include <linux/module.h>
19+#include <linux/mutex.h>
20+#include <linux/usb.h>
21+
22+#include "usbhid/usbhid.h"
23+#include "hid-ids.h"
24+
25+#define DRIVER_SHORT "u2fzero"
26+
27+#define HID_REPORT_SIZE 64
28+
29+/* We only use broadcast (CID-less) messages */
30+#define CID_BROADCAST 0xffffffff
31+
32+struct u2f_hid_msg {
33+ u32 cid;
34+ union {
35+ struct {
36+ u8 cmd;
37+ u8 bcnth;
38+ u8 bcntl;
39+ u8 data[HID_REPORT_SIZE - 7];
40+ } init;
41+ struct {
42+ u8 seq;
43+ u8 data[HID_REPORT_SIZE - 5];
44+ } cont;
45+ };
46+} __packed;
47+
48+struct u2f_hid_report {
49+ u8 report_type;
50+ struct u2f_hid_msg msg;
51+} __packed;
52+
53+#define U2F_HID_MSG_LEN(f) (size_t)(((f).init.bcnth << 8) + (f).init.bcntl)
54+
55+/* Custom extensions to the U2FHID protocol */
56+#define U2F_CUSTOM_GET_RNG 0x21
57+#define U2F_CUSTOM_WINK 0x24
58+
59+struct u2fzero_device {
60+ struct hid_device *hdev;
61+ struct urb *urb; /* URB for the RNG data */
62+ struct led_classdev ldev; /* Embedded struct for led */
63+ struct hwrng hwrng; /* Embedded struct for hwrng */
64+ char *led_name;
65+ char *rng_name;
66+ u8 *buf_out;
67+ u8 *buf_in;
68+ struct mutex lock;
69+ bool present;
70+};
71+
72+static int u2fzero_send(struct u2fzero_device *dev, struct u2f_hid_report *req)
73+{
74+ int ret;
75+
76+ mutex_lock(&dev->lock);
77+
78+ memcpy(dev->buf_out, req, sizeof(struct u2f_hid_report));
79+
80+ ret = hid_hw_output_report(dev->hdev, dev->buf_out,
81+ sizeof(struct u2f_hid_msg));
82+
83+ mutex_unlock(&dev->lock);
84+
85+ if (ret < 0)
86+ return ret;
87+
88+ return ret == sizeof(struct u2f_hid_msg) ? 0 : -EMSGSIZE;
89+}
90+
91+struct u2fzero_transfer_context {
92+ struct completion done;
93+ int status;
94+};
95+
96+static void u2fzero_read_callback(struct urb *urb)
97+{
98+ struct u2fzero_transfer_context *ctx = urb->context;
99+
100+ ctx->status = urb->status;
101+ complete(&ctx->done);
102+}
103+
104+static int u2fzero_recv(struct u2fzero_device *dev,
105+ struct u2f_hid_report *req,
106+ struct u2f_hid_msg *resp)
107+{
108+ int ret;
109+ struct hid_device *hdev = dev->hdev;
110+ struct u2fzero_transfer_context ctx;
111+
112+ mutex_lock(&dev->lock);
113+
114+ memcpy(dev->buf_out, req, sizeof(struct u2f_hid_report));
115+
116+ dev->urb->context = &ctx;
117+ init_completion(&ctx.done);
118+
119+ ret = usb_submit_urb(dev->urb, GFP_NOIO);
120+ if (unlikely(ret)) {
121+ hid_err(hdev, "usb_submit_urb failed: %d", ret);
122+ goto err;
123+ }
124+
125+ ret = hid_hw_output_report(dev->hdev, dev->buf_out,
126+ sizeof(struct u2f_hid_msg));
127+
128+ if (ret < 0) {
129+ hid_err(hdev, "hid_hw_output_report failed: %d", ret);
130+ goto err;
131+ }
132+
133+ ret = (wait_for_completion_timeout(
134+ &ctx.done, msecs_to_jiffies(USB_CTRL_SET_TIMEOUT)));
135+ if (ret < 0) {
136+ usb_kill_urb(dev->urb);
137+ hid_err(hdev, "urb submission timed out");
138+ } else {
139+ ret = dev->urb->actual_length;
140+ memcpy(resp, dev->buf_in, ret);
141+ }
142+
143+err:
144+ mutex_unlock(&dev->lock);
145+
146+ return ret;
147+}
148+
149+static int u2fzero_blink(struct led_classdev *ldev)
150+{
151+ struct u2fzero_device *dev = container_of(ldev,
152+ struct u2fzero_device, ldev);
153+ struct u2f_hid_report req = {
154+ .report_type = 0,
155+ .msg.cid = CID_BROADCAST,
156+ .msg.init = {
157+ .cmd = U2F_CUSTOM_WINK,
158+ .bcnth = 0,
159+ .bcntl = 0,
160+ .data = {0},
161+ }
162+ };
163+ return u2fzero_send(dev, &req);
164+}
165+
166+static int u2fzero_brightness_set(struct led_classdev *ldev,
167+ enum led_brightness brightness)
168+{
169+ ldev->brightness = LED_OFF;
170+ if (brightness)
171+ return u2fzero_blink(ldev);
172+ else
173+ return 0;
174+}
175+
176+static int u2fzero_rng_read(struct hwrng *rng, void *data,
177+ size_t max, bool wait)
178+{
179+ struct u2fzero_device *dev = container_of(rng,
180+ struct u2fzero_device, hwrng);
181+ struct u2f_hid_report req = {
182+ .report_type = 0,
183+ .msg.cid = CID_BROADCAST,
184+ .msg.init = {
185+ .cmd = U2F_CUSTOM_GET_RNG,
186+ .bcnth = 0,
187+ .bcntl = 0,
188+ .data = {0},
189+ }
190+ };
191+ struct u2f_hid_msg resp;
192+ int ret;
193+ size_t actual_length;
194+
195+ if (!dev->present) {
196+ hid_dbg(dev->hdev, "device not present");
197+ return 0;
198+ }
199+
200+ ret = u2fzero_recv(dev, &req, &resp);
201+ if (ret < 0)
202+ return 0;
203+
204+ /* only take the minimum amount of data it is safe to take */
205+ actual_length = min3((size_t)ret - offsetof(struct u2f_hid_msg,
206+ init.data), U2F_HID_MSG_LEN(resp), max);
207+
208+ memcpy(data, resp.init.data, actual_length);
209+
210+ return actual_length;
211+}
212+
213+static int u2fzero_init_led(struct u2fzero_device *dev,
214+ unsigned int minor)
215+{
216+ dev->led_name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
217+ "%s%u", DRIVER_SHORT, minor);
218+ if (dev->led_name == NULL)
219+ return -ENOMEM;
220+
221+ dev->ldev.name = dev->led_name;
222+ dev->ldev.max_brightness = LED_ON;
223+ dev->ldev.flags = LED_HW_PLUGGABLE;
224+ dev->ldev.brightness_set_blocking = u2fzero_brightness_set;
225+
226+ return devm_led_classdev_register(&dev->hdev->dev, &dev->ldev);
227+}
228+
229+static int u2fzero_init_hwrng(struct u2fzero_device *dev,
230+ unsigned int minor)
231+{
232+ dev->rng_name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
233+ "%s-rng%u", DRIVER_SHORT, minor);
234+ if (dev->rng_name == NULL)
235+ return -ENOMEM;
236+
237+ dev->hwrng.name = dev->rng_name;
238+ dev->hwrng.read = u2fzero_rng_read;
239+ dev->hwrng.quality = 1;
240+
241+ return devm_hwrng_register(&dev->hdev->dev, &dev->hwrng);
242+}
243+
244+static int u2fzero_fill_in_urb(struct u2fzero_device *dev)
245+{
246+ struct hid_device *hdev = dev->hdev;
247+ struct usb_device *udev;
248+ struct usbhid_device *usbhid = hdev->driver_data;
249+ unsigned int pipe_in;
250+ struct usb_host_endpoint *ep;
251+
252+ if (dev->hdev->bus != BUS_USB)
253+ return -EINVAL;
254+
255+ udev = hid_to_usb_dev(hdev);
256+
257+ if (!usbhid->urbout || !usbhid->urbin)
258+ return -ENODEV;
259+
260+ ep = usb_pipe_endpoint(udev, usbhid->urbin->pipe);
261+ if (!ep)
262+ return -ENODEV;
263+
264+ dev->urb = usb_alloc_urb(0, GFP_KERNEL);
265+ if (!dev->urb)
266+ return -ENOMEM;
267+
268+ pipe_in = (usbhid->urbin->pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
269+
270+ usb_fill_int_urb(dev->urb,
271+ udev,
272+ pipe_in,
273+ dev->buf_in,
274+ HID_REPORT_SIZE,
275+ u2fzero_read_callback,
276+ NULL,
277+ ep->desc.bInterval);
278+
279+ return 0;
280+}
281+
282+static int u2fzero_probe(struct hid_device *hdev,
283+ const struct hid_device_id *id)
284+{
285+ struct u2fzero_device *dev;
286+ unsigned int minor;
287+ int ret;
288+
289+ if (!hid_is_using_ll_driver(hdev, &usb_hid_driver))
290+ return -EINVAL;
291+
292+ dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
293+ if (dev == NULL)
294+ return -ENOMEM;
295+
296+ dev->buf_out = devm_kmalloc(&hdev->dev,
297+ sizeof(struct u2f_hid_report), GFP_KERNEL);
298+ if (dev->buf_out == NULL)
299+ return -ENOMEM;
300+
301+ dev->buf_in = devm_kmalloc(&hdev->dev,
302+ sizeof(struct u2f_hid_msg), GFP_KERNEL);
303+ if (dev->buf_in == NULL)
304+ return -ENOMEM;
305+
306+ ret = hid_parse(hdev);
307+ if (ret)
308+ return ret;
309+
310+ dev->hdev = hdev;
311+ hid_set_drvdata(hdev, dev);
312+ mutex_init(&dev->lock);
313+
314+ ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
315+ if (ret)
316+ return ret;
317+
318+ u2fzero_fill_in_urb(dev);
319+
320+ dev->present = true;
321+
322+ minor = ((struct hidraw *) hdev->hidraw)->minor;
323+
324+ ret = u2fzero_init_led(dev, minor);
325+ if (ret) {
326+ hid_hw_stop(hdev);
327+ return ret;
328+ }
329+
330+ hid_info(hdev, "U2F Zero LED initialised\n");
331+
332+ ret = u2fzero_init_hwrng(dev, minor);
333+ if (ret) {
334+ hid_hw_stop(hdev);
335+ return ret;
336+ }
337+
338+ hid_info(hdev, "U2F Zero RNG initialised\n");
339+
340+ return 0;
341+}
342+
343+static void u2fzero_remove(struct hid_device *hdev)
344+{
345+ struct u2fzero_device *dev = hid_get_drvdata(hdev);
346+
347+ mutex_lock(&dev->lock);
348+ dev->present = false;
349+ mutex_unlock(&dev->lock);
350+
351+ hid_hw_stop(hdev);
352+ usb_poison_urb(dev->urb);
353+ usb_free_urb(dev->urb);
354+}
355+
356+static const struct hid_device_id u2fzero_table[] = {
357+ { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
358+ USB_DEVICE_ID_U2F_ZERO) },
359+ { }
360+};
361+MODULE_DEVICE_TABLE(hid, u2fzero_table);
362+
363+static struct hid_driver u2fzero_driver = {
364+ .name = "hid-" DRIVER_SHORT,
365+ .probe = u2fzero_probe,
366+ .remove = u2fzero_remove,
367+ .id_table = u2fzero_table,
368+};
369+
370+module_hid_driver(u2fzero_driver);
371+
372+MODULE_LICENSE("GPL");
373+MODULE_AUTHOR("Andrej Shadura <andrew@shadura.me>");
374+MODULE_DESCRIPTION("U2F Zero LED and RNG driver");
drivers/hid/i2c-hid/i2c-hid-core.c+0−2
@@ -184,8 +184,6 @@ static const struct i2c_hid_quirks { · 183 unmodified lines
184184 I2C_HID_QUIRK_NO_RUNTIME_PM },
185185 { USB_VENDOR_ID_ELAN, HID_ANY_ID,
186186 I2C_HID_QUIRK_BOGUS_IRQ },
187− { USB_VENDOR_ID_SYNAPTICS, I2C_DEVICE_ID_SYNAPTICS_7E7E,
188− I2C_HID_QUIRK_NO_RUNTIME_PM },
189187 { 0, 0 }
190188 };
191189
drivers/hid/intel-ish-hid/Kconfig+15−0
@@ -14,4 +14,19 @@ config INTEL_ISH_HID · 13 unmodified lines
1414 Broxton and Kaby Lake.
1515
1616 Say Y here if you want to support Intel ISH. If unsure, say N.
17+
18+config INTEL_ISH_FIRMWARE_DOWNLOADER
19+ tristate "Host Firmware Load feature for Intel ISH"
20+ depends on INTEL_ISH_HID
21+ depends on X86
22+ help
23+ The Integrated Sensor Hub (ISH) enables the kernel to offload
24+ sensor polling and algorithm processing to a dedicated low power
25+ processor in the chipset.
26+
27+ The Host Firmware Load feature adds support to load the ISH
28+ firmware from host file system at boot.
29+
30+ Say M here if you want to support Host Firmware Loading feature
31+ for Intel ISH. If unsure, say N.
1732 endmenu
drivers/hid/intel-ish-hid/Makefile+3−0
@@ -20,4 +20,7 @@ obj-$(CONFIG_INTEL_ISH_HID) += intel-ishtp-hid.o · 19 unmodified lines
2020 intel-ishtp-hid-objs := ishtp-hid.o
2121 intel-ishtp-hid-objs += ishtp-hid-client.o
2222
23+obj-$(CONFIG_INTEL_ISH_FIRMWARE_DOWNLOADER) += intel-ishtp-loader.o
24+intel-ishtp-loader-objs += ishtp-fw-loader.o
25+
2326 ccflags-y += -Idrivers/hid/intel-ish-hid/ishtp
drivers/hid/intel-ish-hid/ipc/hw-ish.h+1−0
@@ -31,6 +31,7 @@ · 30 unmodified lines
3131 #define CNL_H_DEVICE_ID 0xA37C
3232 #define ICL_MOBILE_DEVICE_ID 0x34FC
3333 #define SPT_H_DEVICE_ID 0xA135
34+#define CML_LP_DEVICE_ID 0x02FC
3435
3536 #define REVISION_ID_CHT_A0 0x6
3637 #define REVISION_ID_CHT_Ax_SI 0x0
drivers/hid/intel-ish-hid/ipc/pci-ish.c+1−0
@@ -40,6 +40,7 @@ static const struct pci_device_id ish_pci_tbl[] = { · 39 unmodified lines
4040 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)},
4141 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ICL_MOBILE_DEVICE_ID)},
4242 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)},
43+ {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CML_LP_DEVICE_ID)},
4344 {0, }
4445 };
4546 MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
drivers/hid/intel-ish-hid/ishtp-fw-loader.c+1085−0
@@ -0,0 +1,1085 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * ISH-TP client driver for ISH firmware loading
4+ *
5+ * Copyright (c) 2019, Intel Corporation.
6+ */
7+
8+#include <linux/firmware.h>
9+#include <linux/module.h>
10+#include <linux/pci.h>
11+#include <linux/intel-ish-client-if.h>
12+#include <linux/property.h>
13+#include <asm/cacheflush.h>
14+
15+/* Number of times we attempt to load the firmware before giving up */
16+#define MAX_LOAD_ATTEMPTS 3
17+
18+/* ISH TX/RX ring buffer pool size */
19+#define LOADER_CL_RX_RING_SIZE 1
20+#define LOADER_CL_TX_RING_SIZE 1
21+
22+/*
23+ * ISH Shim firmware loader reserves 4 Kb buffer in SRAM. The buffer is
24+ * used to temporarily hold the data transferred from host to Shim
25+ * firmware loader. Reason for the odd size of 3968 bytes? Each IPC
26+ * transfer is 128 bytes (= 4 bytes header + 124 bytes payload). So the
27+ * 4 Kb buffer can hold maximum of 32 IPC transfers, which means we can
28+ * have a max payload of 3968 bytes (= 32 x 124 payload).
29+ */
30+#define LOADER_SHIM_IPC_BUF_SIZE 3968
31+
32+/**
33+ * enum ish_loader_commands - ISH loader host commands.
34+ * LOADER_CMD_XFER_QUERY Query the Shim firmware loader for
35+ * capabilities
36+ * LOADER_CMD_XFER_FRAGMENT Transfer one firmware image fragment at a
37+ * time. The command may be executed
38+ * multiple times until the entire firmware
39+ * image is downloaded to SRAM.
40+ * LOADER_CMD_START Start executing the main firmware.
41+ */
42+enum ish_loader_commands {
43+ LOADER_CMD_XFER_QUERY = 0,
44+ LOADER_CMD_XFER_FRAGMENT,
45+ LOADER_CMD_START,
46+};
47+
48+/* Command bit mask */
49+#define CMD_MASK GENMASK(6, 0)
50+#define IS_RESPONSE BIT(7)
51+
52+/*
53+ * ISH firmware max delay for one transmit failure is 1 Hz,
54+ * and firmware will retry 2 times, so 3 Hz is used for timeout.
55+ */
56+#define ISHTP_SEND_TIMEOUT (3 * HZ)
57+
58+/*
59+ * Loader transfer modes:
60+ *
61+ * LOADER_XFER_MODE_ISHTP mode uses the existing ISH-TP mechanism to
62+ * transfer data. This may use IPC or DMA if supported in firmware.
63+ * The buffer size is limited to 4 Kb by the IPC/ISH-TP protocol for
64+ * both IPC & DMA (legacy).
65+ *
66+ * LOADER_XFER_MODE_DIRECT_DMA - firmware loading is a bit different
67+ * from the sensor data streaming. Here we download a large (300+ Kb)
68+ * image directly to ISH SRAM memory. There is limited benefit of
69+ * DMA'ing 300 Kb image in 4 Kb chucks limit. Hence, we introduce
70+ * this "direct dma" mode, where we do not use ISH-TP for DMA, but
71+ * instead manage the DMA directly in kernel driver and Shim firmware
72+ * loader (allocate buffer, break in chucks and transfer). This allows
73+ * to overcome 4 Kb limit, and optimize the data flow path in firmware.
74+ */
75+#define LOADER_XFER_MODE_DIRECT_DMA BIT(0)
76+#define LOADER_XFER_MODE_ISHTP BIT(1)
77+
78+/* ISH Transport Loader client unique GUID */
79+static const guid_t loader_ishtp_guid =
80+ GUID_INIT(0xc804d06a, 0x55bd, 0x4ea7,
81+ 0xad, 0xed, 0x1e, 0x31, 0x22, 0x8c, 0x76, 0xdc);
82+
83+#define FILENAME_SIZE 256
84+
85+/*
86+ * The firmware loading latency will be minimum if we can DMA the
87+ * entire ISH firmware image in one go. This requires that we allocate
88+ * a large DMA buffer in kernel, which could be problematic on some
89+ * platforms. So here we limit the DMA buffer size via a module_param.
90+ * We default to 4 pages, but a customer can set it to higher limit if
91+ * deemed appropriate for his platform.
92+ */
93+static int dma_buf_size_limit = 4 * PAGE_SIZE;
94+
95+/**
96+ * struct loader_msg_hdr - Header for ISH Loader commands.
97+ * @command: LOADER_CMD* commands. Bit 7 is the response.
98+ * @status: Command response status. Non 0, is error
99+ * condition.
100+ *
101+ * This structure is used as header for every command/data sent/received
102+ * between Host driver and ISH Shim firmware loader.
103+ */
104+struct loader_msg_hdr {
105+ u8 command;
106+ u8 reserved[2];
107+ u8 status;
108+} __packed;
109+
110+struct loader_xfer_query {
111+ struct loader_msg_hdr hdr;
112+ u32 image_size;
113+} __packed;
114+
115+struct ish_fw_version {
116+ u16 major;
117+ u16 minor;
118+ u16 hotfix;
119+ u16 build;
120+} __packed;
121+
122+union loader_version {
123+ u32 value;
124+ struct {
125+ u8 major;
126+ u8 minor;
127+ u8 hotfix;
128+ u8 build;
129+ };
130+} __packed;
131+
132+struct loader_capability {
133+ u32 max_fw_image_size;
134+ u32 xfer_mode;
135+ u32 max_dma_buf_size; /* only for dma mode, multiples of cacheline */
136+} __packed;
137+
138+struct shim_fw_info {
139+ struct ish_fw_version ish_fw_version;
140+ u32 protocol_version;
141+ union loader_version ldr_version;
142+ struct loader_capability ldr_capability;
143+} __packed;
144+
145+struct loader_xfer_query_response {
146+ struct loader_msg_hdr hdr;
147+ struct shim_fw_info fw_info;
148+} __packed;
149+
150+struct loader_xfer_fragment {
151+ struct loader_msg_hdr hdr;
152+ u32 xfer_mode;
153+ u32 offset;
154+ u32 size;
155+ u32 is_last;
156+} __packed;
157+
158+struct loader_xfer_ipc_fragment {
159+ struct loader_xfer_fragment fragment;
160+ u8 data[] ____cacheline_aligned; /* variable length payload here */
161+} __packed;
162+
163+struct loader_xfer_dma_fragment {
164+ struct loader_xfer_fragment fragment;
165+ u64 ddr_phys_addr;
166+} __packed;
167+
168+struct loader_start {
169+ struct loader_msg_hdr hdr;
170+} __packed;
171+
172+/**
173+ * struct response_info - Encapsulate firmware response related
174+ * information for passing between function
175+ * loader_cl_send() and process_recv() callback.
176+ * @data Copy the data received from firmware here.
177+ * @max_size Max size allocated for the @data buffer. If the
178+ * received data exceeds this value, we log an
179+ * error.
180+ * @size Actual size of data received from firmware.
181+ * @error Returns 0 for success, negative error code for a
182+ * failure in function process_recv().
183+ * @received Set to true on receiving a valid firmware
184+ * response to host command
185+ * @wait_queue Wait queue for Host firmware loading where the
186+ * client sends message to ISH firmware and waits
187+ * for response
188+ */
189+struct response_info {
190+ void *data;
191+ size_t max_size;
192+ size_t size;
193+ int error;
194+ bool received;
195+ wait_queue_head_t wait_queue;
196+};
197+
198+/**
199+ * struct ishtp_cl_data - Encapsulate per ISH-TP Client Data.
200+ * @work_ishtp_reset: Work queue for reset handling.
201+ * @work_fw_load: Work queue for host firmware loading.
202+ * @flag_retry Flag for indicating host firmware loading should
203+ * be retried.
204+ * @retry_count Count the number of retries.
205+ *
206+ * This structure is used to store data per client.
207+ */
208+struct ishtp_cl_data {
209+ struct ishtp_cl *loader_ishtp_cl;
210+ struct ishtp_cl_device *cl_device;
211+
212+ /*
213+ * Used for passing firmware response information between
214+ * loader_cl_send() and process_recv() callback.
215+ */
216+ struct response_info response;
217+
218+ struct work_struct work_ishtp_reset;
219+ struct work_struct work_fw_load;
220+
221+ /*
222+ * In certain failure scenrios, it makes sense to reset the ISH
223+ * subsystem and retry Host firmware loading (e.g. bad message
224+ * packet, ENOMEM, etc.). On the other hand, failures due to
225+ * protocol mismatch, etc., are not recoverable. We do not
226+ * retry them.
227+ *
228+ * If set, the flag indicates that we should re-try the
229+ * particular failure.
230+ */
231+ bool flag_retry;
232+ int retry_count;
233+};
234+
235+#define IPC_FRAGMENT_DATA_PREAMBLE \
236+ offsetof(struct loader_xfer_ipc_fragment, data)
237+
238+#define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
239+
240+/**
241+ * get_firmware_variant() - Gets the filename of firmware image to be
242+ * loaded based on platform variant.
243+ * @client_data Client data instance.
244+ * @filename Returns firmware filename.
245+ *
246+ * Queries the firmware-name device property string.
247+ *
248+ * Return: 0 for success, negative error code for failure.
249+ */
250+static int get_firmware_variant(struct ishtp_cl_data *client_data,
251+ char *filename)
252+{
253+ int rv;
254+ const char *val;
255+ struct device *devc = ishtp_get_pci_device(client_data->cl_device);
256+
257+ rv = device_property_read_string(devc, "firmware-name", &val);
258+ if (rv < 0) {
259+ dev_err(devc,
260+ "Error: ISH firmware-name device property required\n");
261+ return rv;
262+ }
263+ return snprintf(filename, FILENAME_SIZE, "intel/%s", val);
264+}
265+
266+/**
267+ * loader_cl_send() Send message from host to firmware
268+ * @client_data: Client data instance
269+ * @out_msg Message buffer to be sent to firmware
270+ * @out_size Size of out going message
271+ * @in_msg Message buffer where the incoming data copied.
272+ * This buffer is allocated by calling
273+ * @in_size Max size of incoming message
274+ *
275+ * Return: Number of bytes copied in the in_msg on success, negative
276+ * error code on failure.
277+ */
278+static int loader_cl_send(struct ishtp_cl_data *client_data,
279+ u8 *out_msg, size_t out_size,
280+ u8 *in_msg, size_t in_size)
281+{
282+ int rv;
283+ struct loader_msg_hdr *out_hdr = (struct loader_msg_hdr *)out_msg;
284+ struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
285+
286+ dev_dbg(cl_data_to_dev(client_data),
287+ "%s: command=%02lx is_response=%u status=%02x\n",
288+ __func__,
289+ out_hdr->command & CMD_MASK,
290+ out_hdr->command & IS_RESPONSE ? 1 : 0,
291+ out_hdr->status);
292+
293+ /* Setup in coming buffer & size */
294+ client_data->response.data = in_msg;
295+ client_data->response.max_size = in_size;
296+ client_data->response.error = 0;
297+ client_data->response.received = false;
298+
299+ rv = ishtp_cl_send(loader_ishtp_cl, out_msg, out_size);
300+ if (rv < 0) {
301+ dev_err(cl_data_to_dev(client_data),
302+ "ishtp_cl_send error %d\n", rv);
303+ return rv;
304+ }
305+
306+ wait_event_interruptible_timeout(client_data->response.wait_queue,
307+ client_data->response.received,
308+ ISHTP_SEND_TIMEOUT);
309+ if (!client_data->response.received) {
310+ dev_err(cl_data_to_dev(client_data),
311+ "Timed out for response to command=%02lx",
312+ out_hdr->command & CMD_MASK);
313+ return -ETIMEDOUT;
314+ }
315+
316+ if (client_data->response.error < 0)
317+ return client_data->response.error;
318+
319+ return client_data->response.size;
320+}
321+
322+/**
323+ * process_recv() - Receive and parse incoming packet
324+ * @loader_ishtp_cl: Client instance to get stats
325+ * @rb_in_proc: ISH received message buffer
326+ *
327+ * Parse the incoming packet. If it is a response packet then it will
328+ * update received and wake up the caller waiting to for the response.
329+ */
330+static void process_recv(struct ishtp_cl *loader_ishtp_cl,
331+ struct ishtp_cl_rb *rb_in_proc)
332+{
333+ struct loader_msg_hdr *hdr;
334+ size_t data_len = rb_in_proc->buf_idx;
335+ struct ishtp_cl_data *client_data =
336+ ishtp_get_client_data(loader_ishtp_cl);
337+
338+ /* Sanity check */
339+ if (!client_data->response.data) {
340+ dev_err(cl_data_to_dev(client_data),
341+ "Receiving buffer is null. Should be allocated by calling function\n");
342+ client_data->response.error = -EINVAL;
343+ goto end;
344+ }
345+
346+ if (client_data->response.received) {
347+ dev_err(cl_data_to_dev(client_data),
348+ "Previous firmware message not yet processed\n");
349+ client_data->response.error = -EINVAL;
350+ goto end;
351+ }
352+ /*
353+ * All firmware messages have a header. Check buffer size
354+ * before accessing elements inside.
355+ */
356+ if (!rb_in_proc->buffer.data) {
357+ dev_warn(cl_data_to_dev(client_data),
358+ "rb_in_proc->buffer.data returned null");
359+ client_data->response.error = -EBADMSG;
360+ goto end;
361+ }
362+
363+ if (data_len < sizeof(struct loader_msg_hdr)) {
364+ dev_err(cl_data_to_dev(client_data),
365+ "data size %zu is less than header %zu\n",
366+ data_len, sizeof(struct loader_msg_hdr));
367+ client_data->response.error = -EMSGSIZE;
368+ goto end;
369+ }
370+
371+ hdr = (struct loader_msg_hdr *)rb_in_proc->buffer.data;
372+
373+ dev_dbg(cl_data_to_dev(client_data),
374+ "%s: command=%02lx is_response=%u status=%02x\n",
375+ __func__,
376+ hdr->command & CMD_MASK,
377+ hdr->command & IS_RESPONSE ? 1 : 0,
378+ hdr->status);
379+
380+ if (((hdr->command & CMD_MASK) != LOADER_CMD_XFER_QUERY) &&
381+ ((hdr->command & CMD_MASK) != LOADER_CMD_XFER_FRAGMENT) &&
382+ ((hdr->command & CMD_MASK) != LOADER_CMD_START)) {
383+ dev_err(cl_data_to_dev(client_data),
384+ "Invalid command=%02lx\n",
385+ hdr->command & CMD_MASK);
386+ client_data->response.error = -EPROTO;
387+ goto end;
388+ }
389+
390+ if (data_len > client_data->response.max_size) {
391+ dev_err(cl_data_to_dev(client_data),
392+ "Received buffer size %zu is larger than allocated buffer %zu\n",
393+ data_len, client_data->response.max_size);
394+ client_data->response.error = -EMSGSIZE;
395+ goto end;
396+ }
397+
398+ /* We expect only "response" messages from firmware */
399+ if (!(hdr->command & IS_RESPONSE)) {
400+ dev_err(cl_data_to_dev(client_data),
401+ "Invalid response to command\n");
402+ client_data->response.error = -EIO;
403+ goto end;
404+ }
405+
406+ if (hdr->status) {
407+ dev_err(cl_data_to_dev(client_data),
408+ "Loader returned status %d\n",
409+ hdr->status);
410+ client_data->response.error = -EIO;
411+ goto end;
412+ }
413+
414+ /* Update the actual received buffer size */
415+ client_data->response.size = data_len;
416+
417+ /*
418+ * Copy the buffer received in firmware response for the
419+ * calling thread.
420+ */
421+ memcpy(client_data->response.data,
422+ rb_in_proc->buffer.data, data_len);
423+
424+ /* Set flag before waking up the caller */
425+ client_data->response.received = true;
426+
427+end:
428+ /* Free the buffer */
429+ ishtp_cl_io_rb_recycle(rb_in_proc);
430+ rb_in_proc = NULL;
431+
432+ /* Wake the calling thread */
433+ wake_up_interruptible(&client_data->response.wait_queue);
434+}
435+
436+/**
437+ * loader_cl_event_cb() - bus driver callback for incoming message
438+ * @device: Pointer to the ishtp client device for which this
439+ * message is targeted
440+ *
441+ * Remove the packet from the list and process the message by calling
442+ * process_recv
443+ */
444+static void loader_cl_event_cb(struct ishtp_cl_device *cl_device)
445+{
446+ struct ishtp_cl_rb *rb_in_proc;
447+ struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
448+
449+ while ((rb_in_proc = ishtp_cl_rx_get_rb(loader_ishtp_cl)) != NULL) {
450+ /* Process the data packet from firmware */
451+ process_recv(loader_ishtp_cl, rb_in_proc);
452+ }
453+}
454+
455+/**
456+ * ish_query_loader_prop() - Query ISH Shim firmware loader
457+ * @client_data: Client data instance
458+ * @fw: Poiner to firmware data struct in host memory
459+ * @fw_info: Loader firmware properties
460+ *
461+ * This function queries the ISH Shim firmware loader for capabilities.
462+ *
463+ * Return: 0 for success, negative error code for failure.
464+ */
465+static int ish_query_loader_prop(struct ishtp_cl_data *client_data,
466+ const struct firmware *fw,
467+ struct shim_fw_info *fw_info)
468+{
469+ int rv;
470+ struct loader_xfer_query ldr_xfer_query;
471+ struct loader_xfer_query_response ldr_xfer_query_resp;
472+
473+ memset(&ldr_xfer_query, 0, sizeof(ldr_xfer_query));
474+ ldr_xfer_query.hdr.command = LOADER_CMD_XFER_QUERY;
475+ ldr_xfer_query.image_size = fw->size;
476+ rv = loader_cl_send(client_data,
477+ (u8 *)&ldr_xfer_query,
478+ sizeof(ldr_xfer_query),
479+ (u8 *)&ldr_xfer_query_resp,
480+ sizeof(ldr_xfer_query_resp));
481+ if (rv < 0) {
482+ client_data->flag_retry = true;
483+ return rv;
484+ }
485+
486+ /* On success, the return value is the received buffer size */
487+ if (rv != sizeof(struct loader_xfer_query_response)) {
488+ dev_err(cl_data_to_dev(client_data),
489+ "data size %d is not equal to size of loader_xfer_query_response %zu\n",
490+ rv, sizeof(struct loader_xfer_query_response));
491+ client_data->flag_retry = true;
492+ return -EMSGSIZE;
493+ }
494+
495+ /* Save fw_info for use outside this function */
496+ *fw_info = ldr_xfer_query_resp.fw_info;
497+
498+ /* Loader firmware properties */
499+ dev_dbg(cl_data_to_dev(client_data),
500+ "ish_fw_version: major=%d minor=%d hotfix=%d build=%d protocol_version=0x%x loader_version=%d\n",
501+ fw_info->ish_fw_version.major,
502+ fw_info->ish_fw_version.minor,
503+ fw_info->ish_fw_version.hotfix,
504+ fw_info->ish_fw_version.build,
505+ fw_info->protocol_version,
506+ fw_info->ldr_version.value);
507+
508+ dev_dbg(cl_data_to_dev(client_data),
509+ "loader_capability: max_fw_image_size=0x%x xfer_mode=%d max_dma_buf_size=0x%x dma_buf_size_limit=0x%x\n",
510+ fw_info->ldr_capability.max_fw_image_size,
511+ fw_info->ldr_capability.xfer_mode,
512+ fw_info->ldr_capability.max_dma_buf_size,
513+ dma_buf_size_limit);
514+
515+ /* Sanity checks */
516+ if (fw_info->ldr_capability.max_fw_image_size < fw->size) {
517+ dev_err(cl_data_to_dev(client_data),
518+ "ISH firmware size %zu is greater than Shim firmware loader max supported %d\n",
519+ fw->size,
520+ fw_info->ldr_capability.max_fw_image_size);
521+ return -ENOSPC;
522+ }
523+
524+ /* For DMA the buffer size should be multiple of cacheline size */
525+ if ((fw_info->ldr_capability.xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) &&
526+ (fw_info->ldr_capability.max_dma_buf_size % L1_CACHE_BYTES)) {
527+ dev_err(cl_data_to_dev(client_data),
528+ "Shim firmware loader buffer size %d should be multiple of cacheline\n",
529+ fw_info->ldr_capability.max_dma_buf_size);
530+ return -EINVAL;
531+ }
532+
533+ return 0;
534+}
535+
536+/**
537+ * ish_fw_xfer_ishtp() Loads ISH firmware using ishtp interface
538+ * @client_data: Client data instance
539+ * @fw: Pointer to firmware data struct in host memory
540+ *
541+ * This function uses ISH-TP to transfer ISH firmware from host to
542+ * ISH SRAM. Lower layers may use IPC or DMA depending on firmware
543+ * support.
544+ *
545+ * Return: 0 for success, negative error code for failure.
546+ */
547+static int ish_fw_xfer_ishtp(struct ishtp_cl_data *client_data,
548+ const struct firmware *fw)
549+{
550+ int rv;
551+ u32 fragment_offset, fragment_size, payload_max_size;
552+ struct loader_xfer_ipc_fragment *ldr_xfer_ipc_frag;
553+ struct loader_msg_hdr ldr_xfer_ipc_ack;
554+
555+ payload_max_size =
556+ LOADER_SHIM_IPC_BUF_SIZE - IPC_FRAGMENT_DATA_PREAMBLE;
557+
558+ ldr_xfer_ipc_frag = kzalloc(LOADER_SHIM_IPC_BUF_SIZE, GFP_KERNEL);
559+ if (!ldr_xfer_ipc_frag) {
560+ client_data->flag_retry = true;
561+ return -ENOMEM;
562+ }
563+
564+ ldr_xfer_ipc_frag->fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
565+ ldr_xfer_ipc_frag->fragment.xfer_mode = LOADER_XFER_MODE_ISHTP;
566+
567+ /* Break the firmware image into fragments and send as ISH-TP payload */
568+ fragment_offset = 0;
569+ while (fragment_offset < fw->size) {
570+ if (fragment_offset + payload_max_size < fw->size) {
571+ fragment_size = payload_max_size;
572+ ldr_xfer_ipc_frag->fragment.is_last = 0;
573+ } else {
574+ fragment_size = fw->size - fragment_offset;
575+ ldr_xfer_ipc_frag->fragment.is_last = 1;
576+ }
577+
578+ ldr_xfer_ipc_frag->fragment.offset = fragment_offset;
579+ ldr_xfer_ipc_frag->fragment.size = fragment_size;
580+ memcpy(ldr_xfer_ipc_frag->data,
581+ &fw->data[fragment_offset],
582+ fragment_size);
583+
584+ dev_dbg(cl_data_to_dev(client_data),
585+ "xfer_mode=ipc offset=0x%08x size=0x%08x is_last=%d\n",
586+ ldr_xfer_ipc_frag->fragment.offset,
587+ ldr_xfer_ipc_frag->fragment.size,
588+ ldr_xfer_ipc_frag->fragment.is_last);
589+
590+ rv = loader_cl_send(client_data,
591+ (u8 *)ldr_xfer_ipc_frag,
592+ IPC_FRAGMENT_DATA_PREAMBLE + fragment_size,
593+ (u8 *)&ldr_xfer_ipc_ack,
594+ sizeof(ldr_xfer_ipc_ack));
595+ if (rv < 0) {
596+ client_data->flag_retry = true;
597+ goto end_err_resp_buf_release;
598+ }
599+
600+ fragment_offset += fragment_size;
601+ }
602+
603+ kfree(ldr_xfer_ipc_frag);
604+ return 0;
605+
606+end_err_resp_buf_release:
607+ /* Free ISH buffer if not done already, in error case */
608+ kfree(ldr_xfer_ipc_frag);
609+ return rv;
610+}
611+
612+/**
613+ * ish_fw_xfer_direct_dma() - Loads ISH firmware using direct dma
614+ * @client_data: Client data instance
615+ * @fw: Pointer to firmware data struct in host memory
616+ * @fw_info: Loader firmware properties
617+ *
618+ * Host firmware load is a unique case where we need to download
619+ * a large firmware image (200+ Kb). This function implements
620+ * direct DMA transfer in kernel and ISH firmware. This allows
621+ * us to overcome the ISH-TP 4 Kb limit, and allows us to DMA
622+ * directly to ISH UMA at location of choice.
623+ * Function depends on corresponding support in ISH firmware.
624+ *
625+ * Return: 0 for success, negative error code for failure.
626+ */
627+static int ish_fw_xfer_direct_dma(struct ishtp_cl_data *client_data,
628+ const struct firmware *fw,
629+ const struct shim_fw_info fw_info)
630+{
631+ int rv;
632+ void *dma_buf;
633+ dma_addr_t dma_buf_phy;
634+ u32 fragment_offset, fragment_size, payload_max_size;
635+ struct loader_msg_hdr ldr_xfer_dma_frag_ack;
636+ struct loader_xfer_dma_fragment ldr_xfer_dma_frag;
637+ struct device *devc = ishtp_get_pci_device(client_data->cl_device);
638+ u32 shim_fw_buf_size =
639+ fw_info.ldr_capability.max_dma_buf_size;
640+
641+ /*
642+ * payload_max_size should be set to minimum of
643+ * (1) Size of firmware to be loaded,
644+ * (2) Max DMA buffer size supported by Shim firmware,
645+ * (3) DMA buffer size limit set by boot_param dma_buf_size_limit.
646+ */
647+ payload_max_size = min3(fw->size,
648+ (size_t)shim_fw_buf_size,
649+ (size_t)dma_buf_size_limit);
650+
651+ /*
652+ * Buffer size should be multiple of cacheline size
653+ * if it's not, select the previous cacheline boundary.
654+ */
655+ payload_max_size &= ~(L1_CACHE_BYTES - 1);
656+
657+ dma_buf = kmalloc(payload_max_size, GFP_KERNEL | GFP_DMA32);
658+ if (!dma_buf) {
659+ client_data->flag_retry = true;
660+ return -ENOMEM;
661+ }
662+
663+ dma_buf_phy = dma_map_single(devc, dma_buf, payload_max_size,
664+ DMA_TO_DEVICE);
665+ if (dma_mapping_error(devc, dma_buf_phy)) {
666+ dev_err(cl_data_to_dev(client_data), "DMA map failed\n");
667+ client_data->flag_retry = true;
668+ rv = -ENOMEM;
669+ goto end_err_dma_buf_release;
670+ }
671+
672+ ldr_xfer_dma_frag.fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
673+ ldr_xfer_dma_frag.fragment.xfer_mode = LOADER_XFER_MODE_DIRECT_DMA;
674+ ldr_xfer_dma_frag.ddr_phys_addr = (u64)dma_buf_phy;
675+
676+ /* Send the firmware image in chucks of payload_max_size */
677+ fragment_offset = 0;
678+ while (fragment_offset < fw->size) {
679+ if (fragment_offset + payload_max_size < fw->size) {
680+ fragment_size = payload_max_size;
681+ ldr_xfer_dma_frag.fragment.is_last = 0;
682+ } else {
683+ fragment_size = fw->size - fragment_offset;
684+ ldr_xfer_dma_frag.fragment.is_last = 1;
685+ }
686+
687+ ldr_xfer_dma_frag.fragment.offset = fragment_offset;
688+ ldr_xfer_dma_frag.fragment.size = fragment_size;
689+ memcpy(dma_buf, &fw->data[fragment_offset], fragment_size);
690+
691+ dma_sync_single_for_device(devc, dma_buf_phy,
692+ payload_max_size,
693+ DMA_TO_DEVICE);
694+
695+ /*
696+ * Flush cache here because the dma_sync_single_for_device()
697+ * does not do for x86.
698+ */
699+ clflush_cache_range(dma_buf, payload_max_size);
700+
701+ dev_dbg(cl_data_to_dev(client_data),
702+ "xfer_mode=dma offset=0x%08x size=0x%x is_last=%d ddr_phys_addr=0x%016llx\n",
703+ ldr_xfer_dma_frag.fragment.offset,
704+ ldr_xfer_dma_frag.fragment.size,
705+ ldr_xfer_dma_frag.fragment.is_last,
706+ ldr_xfer_dma_frag.ddr_phys_addr);
707+
708+ rv = loader_cl_send(client_data,
709+ (u8 *)&ldr_xfer_dma_frag,
710+ sizeof(ldr_xfer_dma_frag),
711+ (u8 *)&ldr_xfer_dma_frag_ack,
712+ sizeof(ldr_xfer_dma_frag_ack));
713+ if (rv < 0) {
714+ client_data->flag_retry = true;
715+ goto end_err_resp_buf_release;
716+ }
717+
718+ fragment_offset += fragment_size;
719+ }
720+
721+ dma_unmap_single(devc, dma_buf_phy, payload_max_size, DMA_TO_DEVICE);
722+ kfree(dma_buf);
723+ return 0;
724+
725+end_err_resp_buf_release:
726+ /* Free ISH buffer if not done already, in error case */
727+ dma_unmap_single(devc, dma_buf_phy, payload_max_size, DMA_TO_DEVICE);
728+end_err_dma_buf_release:
729+ kfree(dma_buf);
730+ return rv;
731+}
732+
733+/**
734+ * ish_fw_start() Start executing ISH main firmware
735+ * @client_data: client data instance
736+ *
737+ * This function sends message to Shim firmware loader to start
738+ * the execution of ISH main firmware.
739+ *
740+ * Return: 0 for success, negative error code for failure.
741+ */
742+static int ish_fw_start(struct ishtp_cl_data *client_data)
743+{
744+ struct loader_start ldr_start;
745+ struct loader_msg_hdr ldr_start_ack;
746+
747+ memset(&ldr_start, 0, sizeof(ldr_start));
748+ ldr_start.hdr.command = LOADER_CMD_START;
749+ return loader_cl_send(client_data,
750+ (u8 *)&ldr_start,
751+ sizeof(ldr_start),
752+ (u8 *)&ldr_start_ack,
753+ sizeof(ldr_start_ack));
754+}
755+
756+/**
757+ * load_fw_from_host() Loads ISH firmware from host
758+ * @client_data: Client data instance
759+ *
760+ * This function loads the ISH firmware to ISH SRAM and starts execution
761+ *
762+ * Return: 0 for success, negative error code for failure.
763+ */
764+static int load_fw_from_host(struct ishtp_cl_data *client_data)
765+{
766+ int rv;
767+ u32 xfer_mode;
768+ char *filename;
769+ const struct firmware *fw;
770+ struct shim_fw_info fw_info;
771+ struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
772+
773+ client_data->flag_retry = false;
774+
775+ filename = kzalloc(FILENAME_SIZE, GFP_KERNEL);
776+ if (!filename) {
777+ client_data->flag_retry = true;
778+ rv = -ENOMEM;
779+ goto end_error;
780+ }
781+
782+ /* Get filename of the ISH firmware to be loaded */
783+ rv = get_firmware_variant(client_data, filename);
784+ if (rv < 0)
785+ goto end_err_filename_buf_release;
786+
787+ rv = request_firmware(&fw, filename, cl_data_to_dev(client_data));
788+ if (rv < 0)
789+ goto end_err_filename_buf_release;
790+
791+ /* Step 1: Query Shim firmware loader properties */
792+
793+ rv = ish_query_loader_prop(client_data, fw, &fw_info);
794+ if (rv < 0)
795+ goto end_err_fw_release;
796+
797+ /* Step 2: Send the main firmware image to be loaded, to ISH SRAM */
798+
799+ xfer_mode = fw_info.ldr_capability.xfer_mode;
800+ if (xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) {
801+ rv = ish_fw_xfer_direct_dma(client_data, fw, fw_info);
802+ } else if (xfer_mode & LOADER_XFER_MODE_ISHTP) {
803+ rv = ish_fw_xfer_ishtp(client_data, fw);
804+ } else {
805+ dev_err(cl_data_to_dev(client_data),
806+ "No transfer mode selected in firmware\n");
807+ rv = -EINVAL;
808+ }
809+ if (rv < 0)
810+ goto end_err_fw_release;
811+
812+ /* Step 3: Start ISH main firmware exeuction */
813+
814+ rv = ish_fw_start(client_data);
815+ if (rv < 0)
816+ goto end_err_fw_release;
817+
818+ release_firmware(fw);
819+ kfree(filename);
820+ dev_info(cl_data_to_dev(client_data), "ISH firmware %s loaded\n",
821+ filename);
822+ return 0;
823+
824+end_err_fw_release:
825+ release_firmware(fw);
826+end_err_filename_buf_release:
827+ kfree(filename);
828+end_error:
829+ /* Keep a count of retries, and give up after 3 attempts */
830+ if (client_data->flag_retry &&
831+ client_data->retry_count++ < MAX_LOAD_ATTEMPTS) {
832+ dev_warn(cl_data_to_dev(client_data),
833+ "ISH host firmware load failed %d. Resetting ISH, and trying again..\n",
834+ rv);
835+ ish_hw_reset(ishtp_get_ishtp_device(loader_ishtp_cl));
836+ } else {
837+ dev_err(cl_data_to_dev(client_data),
838+ "ISH host firmware load failed %d\n", rv);
839+ }
840+ return rv;
841+}
842+
843+static void load_fw_from_host_handler(struct work_struct *work)
844+{
845+ struct ishtp_cl_data *client_data;
846+
847+ client_data = container_of(work, struct ishtp_cl_data,
848+ work_fw_load);
849+ load_fw_from_host(client_data);
850+}
851+
852+/**
853+ * loader_init() - Init function for ISH-TP client
854+ * @loader_ishtp_cl: ISH-TP client instance
855+ * @reset: true if called for init after reset
856+ *
857+ * Return: 0 for success, negative error code for failure
858+ */
859+static int loader_init(struct ishtp_cl *loader_ishtp_cl, int reset)
860+{
861+ int rv;
862+ struct ishtp_fw_client *fw_client;
863+ struct ishtp_cl_data *client_data =
864+ ishtp_get_client_data(loader_ishtp_cl);
865+
866+ dev_dbg(cl_data_to_dev(client_data), "reset flag: %d\n", reset);
867+
868+ rv = ishtp_cl_link(loader_ishtp_cl);
869+ if (rv < 0) {
870+ dev_err(cl_data_to_dev(client_data), "ishtp_cl_link failed\n");
871+ return rv;
872+ }
873+
874+ /* Connect to firmware client */
875+ ishtp_set_tx_ring_size(loader_ishtp_cl, LOADER_CL_TX_RING_SIZE);
876+ ishtp_set_rx_ring_size(loader_ishtp_cl, LOADER_CL_RX_RING_SIZE);
877+
878+ fw_client =
879+ ishtp_fw_cl_get_client(ishtp_get_ishtp_device(loader_ishtp_cl),
880+ &loader_ishtp_guid);
881+ if (!fw_client) {
882+ dev_err(cl_data_to_dev(client_data),
883+ "ISH client uuid not found\n");
884+ rv = -ENOENT;
885+ goto err_cl_unlink;
886+ }
887+
888+ ishtp_cl_set_fw_client_id(loader_ishtp_cl,
889+ ishtp_get_fw_client_id(fw_client));
890+ ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_CONNECTING);
891+
892+ rv = ishtp_cl_connect(loader_ishtp_cl);
893+ if (rv < 0) {
894+ dev_err(cl_data_to_dev(client_data), "Client connect fail\n");
895+ goto err_cl_unlink;
896+ }
897+
898+ dev_dbg(cl_data_to_dev(client_data), "Client connected\n");
899+
900+ ishtp_register_event_cb(client_data->cl_device, loader_cl_event_cb);
901+
902+ return 0;
903+
904+err_cl_unlink:
905+ ishtp_cl_unlink(loader_ishtp_cl);
906+ return rv;
907+}
908+
909+static void loader_deinit(struct ishtp_cl *loader_ishtp_cl)
910+{
911+ ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_DISCONNECTING);
912+ ishtp_cl_disconnect(loader_ishtp_cl);
913+ ishtp_cl_unlink(loader_ishtp_cl);
914+ ishtp_cl_flush_queues(loader_ishtp_cl);
915+
916+ /* Disband and free all Tx and Rx client-level rings */
917+ ishtp_cl_free(loader_ishtp_cl);
918+}
919+
920+static void reset_handler(struct work_struct *work)
921+{
922+ int rv;
923+ struct ishtp_cl_data *client_data;
924+ struct ishtp_cl *loader_ishtp_cl;
925+ struct ishtp_cl_device *cl_device;
926+
927+ client_data = container_of(work, struct ishtp_cl_data,
928+ work_ishtp_reset);
929+
930+ loader_ishtp_cl = client_data->loader_ishtp_cl;
931+ cl_device = client_data->cl_device;
932+
933+ /* Unlink, flush queues & start again */
934+ ishtp_cl_unlink(loader_ishtp_cl);
935+ ishtp_cl_flush_queues(loader_ishtp_cl);
936+ ishtp_cl_free(loader_ishtp_cl);
937+
938+ loader_ishtp_cl = ishtp_cl_allocate(cl_device);
939+ if (!loader_ishtp_cl)
940+ return;
941+
942+ ishtp_set_drvdata(cl_device, loader_ishtp_cl);
943+ ishtp_set_client_data(loader_ishtp_cl, client_data);
944+ client_data->loader_ishtp_cl = loader_ishtp_cl;
945+ client_data->cl_device = cl_device;
946+
947+ rv = loader_init(loader_ishtp_cl, 1);
948+ if (rv < 0) {
949+ dev_err(ishtp_device(cl_device), "Reset Failed\n");
950+ return;
951+ }
952+
953+ /* ISH firmware loading from host */
954+ load_fw_from_host(client_data);
955+}
956+
957+/**
958+ * loader_ishtp_cl_probe() - ISH-TP client driver probe
959+ * @cl_device: ISH-TP client device instance
960+ *
961+ * This function gets called on device create on ISH-TP bus
962+ *
963+ * Return: 0 for success, negative error code for failure
964+ */
965+static int loader_ishtp_cl_probe(struct ishtp_cl_device *cl_device)
966+{
967+ struct ishtp_cl *loader_ishtp_cl;
968+ struct ishtp_cl_data *client_data;
969+ int rv;
970+
971+ client_data = devm_kzalloc(ishtp_device(cl_device),
972+ sizeof(*client_data),
973+ GFP_KERNEL);
974+ if (!client_data)
975+ return -ENOMEM;
976+
977+ loader_ishtp_cl = ishtp_cl_allocate(cl_device);
978+ if (!loader_ishtp_cl)
979+ return -ENOMEM;
980+
981+ ishtp_set_drvdata(cl_device, loader_ishtp_cl);
982+ ishtp_set_client_data(loader_ishtp_cl, client_data);
983+ client_data->loader_ishtp_cl = loader_ishtp_cl;
984+ client_data->cl_device = cl_device;
985+
986+ init_waitqueue_head(&client_data->response.wait_queue);
987+
988+ INIT_WORK(&client_data->work_ishtp_reset,
989+ reset_handler);
990+ INIT_WORK(&client_data->work_fw_load,
991+ load_fw_from_host_handler);
992+
993+ rv = loader_init(loader_ishtp_cl, 0);
994+ if (rv < 0) {
995+ ishtp_cl_free(loader_ishtp_cl);
996+ return rv;
997+ }
998+ ishtp_get_device(cl_device);
999+
1000+ client_data->retry_count = 0;
1001+
1002+ /* ISH firmware loading from host */
1003+ schedule_work(&client_data->work_fw_load);
1004+
1005+ return 0;
1006+}
1007+
1008+/**
1009+ * loader_ishtp_cl_remove() - ISH-TP client driver remove
1010+ * @cl_device: ISH-TP client device instance
1011+ *
1012+ * This function gets called on device remove on ISH-TP bus
1013+ *
1014+ * Return: 0
1015+ */
1016+static int loader_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
1017+{
1018+ struct ishtp_cl_data *client_data;
1019+ struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
1020+
1021+ client_data = ishtp_get_client_data(loader_ishtp_cl);
1022+
1023+ /*
1024+ * The sequence of the following two cancel_work_sync() is
1025+ * important. The work_fw_load can in turn schedue
1026+ * work_ishtp_reset, so first cancel work_fw_load then
1027+ * cancel work_ishtp_reset.
1028+ */
1029+ cancel_work_sync(&client_data->work_fw_load);
1030+ cancel_work_sync(&client_data->work_ishtp_reset);
1031+ loader_deinit(loader_ishtp_cl);
1032+ ishtp_put_device(cl_device);
1033+
1034+ return 0;
1035+}
1036+
1037+/**
1038+ * loader_ishtp_cl_reset() - ISH-TP client driver reset
1039+ * @cl_device: ISH-TP client device instance
1040+ *
1041+ * This function gets called on device reset on ISH-TP bus
1042+ *
1043+ * Return: 0
1044+ */
1045+static int loader_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
1046+{
1047+ struct ishtp_cl_data *client_data;
1048+ struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
1049+
1050+ client_data = ishtp_get_client_data(loader_ishtp_cl);
1051+
1052+ schedule_work(&client_data->work_ishtp_reset);
1053+
1054+ return 0;
1055+}
1056+
1057+static struct ishtp_cl_driver loader_ishtp_cl_driver = {
1058+ .name = "ish-loader",
1059+ .guid = &loader_ishtp_guid,
1060+ .probe = loader_ishtp_cl_probe,
1061+ .remove = loader_ishtp_cl_remove,
1062+ .reset = loader_ishtp_cl_reset,
1063+};
1064+
1065+static int __init ish_loader_init(void)
1066+{
1067+ return ishtp_cl_driver_register(&loader_ishtp_cl_driver, THIS_MODULE);
1068+}
1069+
1070+static void __exit ish_loader_exit(void)
1071+{
1072+ ishtp_cl_driver_unregister(&loader_ishtp_cl_driver);
1073+}
1074+
1075+late_initcall(ish_loader_init);
1076+module_exit(ish_loader_exit);
1077+
1078+module_param(dma_buf_size_limit, int, 0644);
1079+MODULE_PARM_DESC(dma_buf_size_limit, "Limit the DMA buf size to this value in bytes");
1080+
1081+MODULE_DESCRIPTION("ISH ISH-TP Host firmware Loader Client Driver");
1082+MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
1083+
1084+MODULE_LICENSE("GPL v2");
1085+MODULE_ALIAS("ishtp:*");
drivers/hid/intel-ish-hid/ishtp-hid-client.c+92−76
@@ -15,15 +15,16 @@ · 14 unmodified lines
1515
1616 #include <linux/module.h>
1717 #include <linux/hid.h>
18+#include <linux/intel-ish-client-if.h>
1819 #include <linux/sched.h>
19−#include "ishtp/ishtp-dev.h"
20−#include "ishtp/client.h"
2120 #include "ishtp-hid.h"
2221
2322 /* Rx ring buffer pool size */
2423 #define HID_CL_RX_RING_SIZE 32
2524 #define HID_CL_TX_RING_SIZE 16
2625
26+#define cl_data_to_dev(client_data) ishtp_device(client_data->cl_device)
27+
2728 /**
2829 * report_bad_packets() - Report bad packets
2930 * @hid_ishtp_cl: Client instance to get stats
@@ -37,9 +38,9 @@ static void report_bad_packet(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · 7 unmodified lines
3738 size_t cur_pos, size_t payload_len)
3839 {
3940 struct hostif_msg *recv_msg = recv_buf;
40− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
41+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
4142
42− dev_err(&client_data->cl_device->dev, "[hid-ish]: BAD packet %02X\n"
43+ dev_err(cl_data_to_dev(client_data), "[hid-ish]: BAD packet %02X\n"
4344 "total_bad=%u cur_pos=%u\n"
4445 "[%02X %02X %02X %02X]\n"
4546 "payload_len=%u\n"
@@ -69,13 +70,15 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · 23 unmodified lines
6970 unsigned char *payload;
7071 struct device_info *dev_info;
7172 int i, j;
72− size_t payload_len, total_len, cur_pos;
73+ size_t payload_len, total_len, cur_pos, raw_len;
7374 int report_type;
7475 struct report_list *reports_list;
7576 char *reports;
7677 size_t report_len;
77− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
78+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
7879 int curr_hid_dev = client_data->cur_hid_dev;
80+ struct ishtp_hid_data *hid_data = NULL;
81+ struct hid_device *hid = NULL;
7982
8083 payload = recv_buf + sizeof(struct hostif_msg_hdr);
8184 total_len = data_len;
@@ -83,12 +86,12 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · 1 unmodified line
8386
8487 do {
8588 if (cur_pos + sizeof(struct hostif_msg) > total_len) {
86− dev_err(&client_data->cl_device->dev,
89+ dev_err(cl_data_to_dev(client_data),
8790 "[hid-ish]: error, received %u which is less than data header %u\n",
8891 (unsigned int)data_len,
8992 (unsigned int)sizeof(struct hostif_msg_hdr));
9093 ++client_data->bad_recv_cnt;
91− ish_hw_reset(hid_ishtp_cl->dev);
94+ ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
9295 break;
9396 }
9497
@@ -101,7 +104,7 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · 6 unmodified lines
101104 ++client_data->bad_recv_cnt;
102105 report_bad_packet(hid_ishtp_cl, recv_msg, cur_pos,
103106 payload_len);
104− ish_hw_reset(hid_ishtp_cl->dev);
107+ ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
105108 break;
106109 }
107110
@@ -116,18 +119,18 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · 8 unmodified lines
116119 report_bad_packet(hid_ishtp_cl, recv_msg,
117120 cur_pos,
118121 payload_len);
119− ish_hw_reset(hid_ishtp_cl->dev);
122+ ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
120123 break;
121124 }
122125 client_data->hid_dev_count = (unsigned int)*payload;
123126 if (!client_data->hid_devices)
124127 client_data->hid_devices = devm_kcalloc(
125− &client_data->cl_device->dev,
128+ cl_data_to_dev(client_data),
126129 client_data->hid_dev_count,
127130 sizeof(struct device_info),
128131 GFP_KERNEL);
129132 if (!client_data->hid_devices) {
130− dev_err(&client_data->cl_device->dev,
133+ dev_err(cl_data_to_dev(client_data),
131134 "Mem alloc failed for hid device info\n");
132135 wake_up_interruptible(&client_data->init_wait);
133136 break;
@@ -135,7 +138,7 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · 1 unmodified line
135138 for (i = 0; i < client_data->hid_dev_count; ++i) {
136139 if (1 + sizeof(struct device_info) * i >=
137140 payload_len) {
138− dev_err(&client_data->cl_device->dev,
141+ dev_err(cl_data_to_dev(client_data),
139142 "[hid-ish]: [ENUM_DEVICES]: content size %zu is bigger than payload_len %zu\n",
140143 1 + sizeof(struct device_info)
141144 * i, payload_len);
@@ -165,12 +168,12 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · 23 unmodified lines
165168 report_bad_packet(hid_ishtp_cl, recv_msg,
166169 cur_pos,
167170 payload_len);
168− ish_hw_reset(hid_ishtp_cl->dev);
171+ ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
169172 break;
170173 }
171174 if (!client_data->hid_descr[curr_hid_dev])
172175 client_data->hid_descr[curr_hid_dev] =
173− devm_kmalloc(&client_data->cl_device->dev,
176+ devm_kmalloc(cl_data_to_dev(client_data),
174177 payload_len, GFP_KERNEL);
175178 if (client_data->hid_descr[curr_hid_dev]) {
176179 memcpy(client_data->hid_descr[curr_hid_dev],
@@ -190,12 +193,12 @@ static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf, · 13 unmodified lines
190193 report_bad_packet(hid_ishtp_cl, recv_msg,
191194 cur_pos,
192195 payload_len);
193− ish_hw_reset(hid_ishtp_cl->dev);
196+ ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
194197 break;
195198 }
196199 if (!client_data->report_descr[curr_hid_dev])
197200 client_data->report_descr[curr_hid_dev] =
198− devm_kmalloc(&client_data->cl_device->dev,
201+ devm_kmalloc(cl_data_to_dev(client_data),
199202 payload_len, GFP_KERNEL);
200203 if (client_data->report_descr[curr_hid_dev]) {
201204 memcpy(client_data->report_descr[curr_hid_dev],
@@ -219,18 +222,31 @@ do_get_report: · 17 unmodified lines
219222 /* Get index of device that matches this id */
220223 for (i = 0; i < client_data->num_hid_devices; ++i) {
221224 if (recv_msg->hdr.device_id ==
222− client_data->hid_devices[i].dev_id)
223− if (client_data->hid_sensor_hubs[i]) {
224− hid_input_report(
225− client_data->hid_sensor_hubs[
226− i],
227− report_type, payload,
228− payload_len, 0);
229− ishtp_hid_wakeup(
230− client_data->hid_sensor_hubs[
231− i]);
225+ client_data->hid_devices[i].dev_id) {
226+ hid = client_data->hid_sensor_hubs[i];
227+ if (!hid)
232228 break;
229+
230+ hid_data = hid->driver_data;
231+ if (hid_data->raw_get_req) {
232+ raw_len =
233+ (hid_data->raw_buf_size <
234+ payload_len) ?
235+ hid_data->raw_buf_size :
236+ payload_len;
237+
238+ memcpy(hid_data->raw_buf,
239+ payload, raw_len);
240+ } else {
241+ hid_input_report
242+ (hid, report_type,
243+ payload, payload_len,
244+ 0);
233245 }
246+
247+ ishtp_hid_wakeup(hid);
248+ break;
249+ }
234250 }
235251 break;
236252
@@ -295,7 +311,7 @@ do_get_report: · 58 unmodified lines
295311 ++client_data->bad_recv_cnt;
296312 report_bad_packet(hid_ishtp_cl, recv_msg, cur_pos,
297313 payload_len);
298− ish_hw_reset(hid_ishtp_cl->dev);
314+ ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
299315 break;
300316
301317 }
@@ -475,7 +491,7 @@ int ishtp_hid_link_ready_wait(struct ishtp_cl_data *client_data) · 173 unmodified lines
475491 static int ishtp_enum_enum_devices(struct ishtp_cl *hid_ishtp_cl)
476492 {
477493 struct hostif_msg msg;
478− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
494+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
479495 int retry_count;
480496 int rv;
481497
@@ -501,18 +517,18 @@ static int ishtp_enum_enum_devices(struct ishtp_cl *hid_ishtp_cl) · 19 unmodified lines
501517 sizeof(struct hostif_msg));
502518 }
503519 if (!client_data->enum_devices_done) {
504− dev_err(&client_data->cl_device->dev,
520+ dev_err(cl_data_to_dev(client_data),
505521 "[hid-ish]: timed out waiting for enum_devices\n");
506522 return -ETIMEDOUT;
507523 }
508524 if (!client_data->hid_devices) {
509− dev_err(&client_data->cl_device->dev,
525+ dev_err(cl_data_to_dev(client_data),
510526 "[hid-ish]: failed to allocate HID dev structures\n");
511527 return -ENOMEM;
512528 }
513529
514530 client_data->num_hid_devices = client_data->hid_dev_count;
515− dev_info(&hid_ishtp_cl->device->dev,
531+ dev_info(ishtp_device(client_data->cl_device),
516532 "[hid-ish]: enum_devices_done OK, num_hid_devices=%d\n",
517533 client_data->num_hid_devices);
518534
@@ -531,7 +547,7 @@ static int ishtp_enum_enum_devices(struct ishtp_cl *hid_ishtp_cl) · 12 unmodified lines
531547 static int ishtp_get_hid_descriptor(struct ishtp_cl *hid_ishtp_cl, int index)
532548 {
533549 struct hostif_msg msg;
534− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
550+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
535551 int rv;
536552
537553 /* Get HID descriptor */
@@ -549,13 +565,13 @@ static int ishtp_get_hid_descriptor(struct ishtp_cl *hid_ishtp_cl, int index) · 11 unmodified lines
549565 client_data->hid_descr_done,
550566 3 * HZ);
551567 if (!client_data->hid_descr_done) {
552− dev_err(&client_data->cl_device->dev,
568+ dev_err(cl_data_to_dev(client_data),
553569 "[hid-ish]: timed out for hid_descr_done\n");
554570 return -EIO;
555571 }
556572
557573 if (!client_data->hid_descr[index]) {
558− dev_err(&client_data->cl_device->dev,
574+ dev_err(cl_data_to_dev(client_data),
559575 "[hid-ish]: allocation HID desc fail\n");
560576 return -ENOMEM;
561577 }
@@ -578,7 +594,7 @@ static int ishtp_get_report_descriptor(struct ishtp_cl *hid_ishtp_cl, · 16 unmodified lines
578594 int index)
579595 {
580596 struct hostif_msg msg;
581− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
597+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
582598 int rv;
583599
584600 /* Get report descriptor */
@@ -596,12 +612,12 @@ static int ishtp_get_report_descriptor(struct ishtp_cl *hid_ishtp_cl, · 11 unmodified lines
596612 client_data->report_descr_done,
597613 3 * HZ);
598614 if (!client_data->report_descr_done) {
599− dev_err(&client_data->cl_device->dev,
615+ dev_err(cl_data_to_dev(client_data),
600616 "[hid-ish]: timed out for report descr\n");
601617 return -EIO;
602618 }
603619 if (!client_data->report_descr[index]) {
604− dev_err(&client_data->cl_device->dev,
620+ dev_err(cl_data_to_dev(client_data),
605621 "[hid-ish]: failed to alloc report descr\n");
606622 return -ENOMEM;
607623 }
@@ -626,42 +642,42 @@ static int ishtp_get_report_descriptor(struct ishtp_cl *hid_ishtp_cl, · 18 unmodified lines
626642 static int hid_ishtp_cl_init(struct ishtp_cl *hid_ishtp_cl, int reset)
627643 {
628644 struct ishtp_device *dev;
629− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
645+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
630646 struct ishtp_fw_client *fw_client;
631647 int i;
632648 int rv;
633649
634− dev_dbg(&client_data->cl_device->dev, "%s\n", __func__);
650+ dev_dbg(cl_data_to_dev(client_data), "%s\n", __func__);
635651 hid_ishtp_trace(client_data, "%s reset flag: %d\n", __func__, reset);
636652
637− rv = ishtp_cl_link(hid_ishtp_cl, ISHTP_HOST_CLIENT_ID_ANY);
653+ rv = ishtp_cl_link(hid_ishtp_cl);
638654 if (rv) {
639− dev_err(&client_data->cl_device->dev,
655+ dev_err(cl_data_to_dev(client_data),
640656 "ishtp_cl_link failed\n");
641657 return -ENOMEM;
642658 }
643659
644660 client_data->init_done = 0;
645661
646− dev = hid_ishtp_cl->dev;
662+ dev = ishtp_get_ishtp_device(hid_ishtp_cl);
647663
648664 /* Connect to FW client */
649− hid_ishtp_cl->rx_ring_size = HID_CL_RX_RING_SIZE;
650− hid_ishtp_cl->tx_ring_size = HID_CL_TX_RING_SIZE;
665+ ishtp_set_tx_ring_size(hid_ishtp_cl, HID_CL_TX_RING_SIZE);
666+ ishtp_set_rx_ring_size(hid_ishtp_cl, HID_CL_RX_RING_SIZE);
651667
652668 fw_client = ishtp_fw_cl_get_client(dev, &hid_ishtp_guid);
653669 if (!fw_client) {
654− dev_err(&client_data->cl_device->dev,
670+ dev_err(cl_data_to_dev(client_data),
655671 "ish client uuid not found\n");
656672 return -ENOENT;
657673 }
658−
659− hid_ishtp_cl->fw_client_id = fw_client->client_id;
660− hid_ishtp_cl->state = ISHTP_CL_CONNECTING;
674+ ishtp_cl_set_fw_client_id(hid_ishtp_cl,
675+ ishtp_get_fw_client_id(fw_client));
676+ ishtp_set_connection_state(hid_ishtp_cl, ISHTP_CL_CONNECTING);
661677
662678 rv = ishtp_cl_connect(hid_ishtp_cl);
663679 if (rv) {
664− dev_err(&client_data->cl_device->dev,
680+ dev_err(cl_data_to_dev(client_data),
665681 "client connect fail\n");
666682 goto err_cl_unlink;
667683 }
@@ -669,7 +685,7 @@ static int hid_ishtp_cl_init(struct ishtp_cl *hid_ishtp_cl, int reset) · 1 unmodified line
669685 hid_ishtp_trace(client_data, "%s client connected\n", __func__);
670686
671687 /* Register read callback */
672− ishtp_register_event_cb(hid_ishtp_cl->device, ish_cl_event_cb);
688+ ishtp_register_event_cb(client_data->cl_device, ish_cl_event_cb);
673689
674690 rv = ishtp_enum_enum_devices(hid_ishtp_cl);
675691 if (rv)
@@ -692,7 +708,7 @@ static int hid_ishtp_cl_init(struct ishtp_cl *hid_ishtp_cl, int reset) · 16 unmodified lines
692708 if (!reset) {
693709 rv = ishtp_hid_probe(i, client_data);
694710 if (rv) {
695− dev_err(&client_data->cl_device->dev,
711+ dev_err(cl_data_to_dev(client_data),
696712 "[hid-ish]: HID probe for #%u failed: %d\n",
697713 i, rv);
698714 goto err_cl_disconnect;
@@ -707,7 +723,7 @@ static int hid_ishtp_cl_init(struct ishtp_cl *hid_ishtp_cl, int reset) · 8 unmodified lines
707723 return 0;
708724
709725 err_cl_disconnect:
710− hid_ishtp_cl->state = ISHTP_CL_DISCONNECTING;
726+ ishtp_set_connection_state(hid_ishtp_cl, ISHTP_CL_DISCONNECTING);
711727 ishtp_cl_disconnect(hid_ishtp_cl);
712728 err_cl_unlink:
713729 ishtp_cl_unlink(hid_ishtp_cl);
@@ -744,16 +760,16 @@ static void hid_ishtp_cl_reset_handler(struct work_struct *work) · 30 unmodified lines
744760
745761 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
746762 hid_ishtp_cl);
747− dev_dbg(&cl_device->dev, "%s\n", __func__);
763+ dev_dbg(ishtp_device(client_data->cl_device), "%s\n", __func__);
748764
749765 hid_ishtp_cl_deinit(hid_ishtp_cl);
750766
751− hid_ishtp_cl = ishtp_cl_allocate(cl_device->ishtp_dev);
767+ hid_ishtp_cl = ishtp_cl_allocate(cl_device);
752768 if (!hid_ishtp_cl)
753769 return;
754770
755771 ishtp_set_drvdata(cl_device, hid_ishtp_cl);
756− hid_ishtp_cl->client_data = client_data;
772+ ishtp_set_client_data(hid_ishtp_cl, client_data);
757773 client_data->hid_ishtp_cl = hid_ishtp_cl;
758774
759775 client_data->num_hid_devices = 0;
@@ -762,15 +778,17 @@ static void hid_ishtp_cl_reset_handler(struct work_struct *work) · 2 unmodified lines
762778 rv = hid_ishtp_cl_init(hid_ishtp_cl, 1);
763779 if (!rv)
764780 break;
765− dev_err(&client_data->cl_device->dev, "Retry reset init\n");
781+ dev_err(cl_data_to_dev(client_data), "Retry reset init\n");
766782 }
767783 if (rv) {
768− dev_err(&client_data->cl_device->dev, "Reset Failed\n");
784+ dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
769785 hid_ishtp_trace(client_data, "%s Failed hid_ishtp_cl %p\n",
770786 __func__, hid_ishtp_cl);
771787 }
772788 }
773789
790+void (*hid_print_trace)(void *unused, const char *format, ...);
791+
774792 /**
775793 * hid_ishtp_cl_probe() - ISHTP client driver probe
776794 * @cl_device: ISHTP client device instance
@@ -788,21 +806,18 @@ static int hid_ishtp_cl_probe(struct ishtp_cl_device *cl_device) · 11 unmodified lines
788806 if (!cl_device)
789807 return -ENODEV;
790808
791− if (!guid_equal(&hid_ishtp_guid,
792− &cl_device->fw_client->props.protocol_name))
793− return -ENODEV;
794−
795− client_data = devm_kzalloc(&cl_device->dev, sizeof(*client_data),
809+ client_data = devm_kzalloc(ishtp_device(cl_device),
810+ sizeof(*client_data),
796811 GFP_KERNEL);
797812 if (!client_data)
798813 return -ENOMEM;
799814
800− hid_ishtp_cl = ishtp_cl_allocate(cl_device->ishtp_dev);
815+ hid_ishtp_cl = ishtp_cl_allocate(cl_device);
801816 if (!hid_ishtp_cl)
802817 return -ENOMEM;
803818
804819 ishtp_set_drvdata(cl_device, hid_ishtp_cl);
805− hid_ishtp_cl->client_data = client_data;
820+ ishtp_set_client_data(hid_ishtp_cl, client_data);
806821 client_data->hid_ishtp_cl = hid_ishtp_cl;
807822 client_data->cl_device = cl_device;
808823
@@ -811,6 +826,8 @@ static int hid_ishtp_cl_probe(struct ishtp_cl_device *cl_device) · 2 unmodified lines
811826
812827 INIT_WORK(&client_data->work, hid_ishtp_cl_reset_handler);
813828
829+ hid_print_trace = ishtp_trace_callback(cl_device);
830+
814831 rv = hid_ishtp_cl_init(hid_ishtp_cl, 0);
815832 if (rv) {
816833 ishtp_cl_free(hid_ishtp_cl);
@@ -832,13 +849,13 @@ static int hid_ishtp_cl_probe(struct ishtp_cl_device *cl_device) · 15 unmodified lines
832849 static int hid_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
833850 {
834851 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
835− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
852+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
836853
837854 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
838855 hid_ishtp_cl);
839856
840− dev_dbg(&cl_device->dev, "%s\n", __func__);
841− hid_ishtp_cl->state = ISHTP_CL_DISCONNECTING;
857+ dev_dbg(ishtp_device(cl_device), "%s\n", __func__);
858+ ishtp_set_connection_state(hid_ishtp_cl, ISHTP_CL_DISCONNECTING);
842859 ishtp_cl_disconnect(hid_ishtp_cl);
843860 ishtp_put_device(cl_device);
844861 ishtp_hid_remove(client_data);
@@ -862,7 +879,7 @@ static int hid_ishtp_cl_remove(struct ishtp_cl_device *cl_device) · 17 unmodified lines
862879 static int hid_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
863880 {
864881 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
865− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
882+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
866883
867884 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
868885 hid_ishtp_cl);
@@ -872,8 +889,6 @@ static int hid_ishtp_cl_reset(struct ishtp_cl_device *cl_device) · 3 unmodified lines
872889 return 0;
873890 }
874891
875−#define to_ishtp_cl_device(d) container_of(d, struct ishtp_cl_device, dev)
876−
877892 /**
878893 * hid_ishtp_cl_suspend() - ISHTP client driver suspend
879894 * @device: device instance
@@ -884,9 +899,9 @@ static int hid_ishtp_cl_reset(struct ishtp_cl_device *cl_device) · 4 unmodified lines
884899 */
885900 static int hid_ishtp_cl_suspend(struct device *device)
886901 {
887− struct ishtp_cl_device *cl_device = to_ishtp_cl_device(device);
902+ struct ishtp_cl_device *cl_device = dev_get_drvdata(device);
888903 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
889− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
904+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
890905
891906 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
892907 hid_ishtp_cl);
@@ -905,9 +920,9 @@ static int hid_ishtp_cl_suspend(struct device *device) · 12 unmodified lines
905920 */
906921 static int hid_ishtp_cl_resume(struct device *device)
907922 {
908− struct ishtp_cl_device *cl_device = to_ishtp_cl_device(device);
923+ struct ishtp_cl_device *cl_device = dev_get_drvdata(device);
909924 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
910− struct ishtp_cl_data *client_data = hid_ishtp_cl->client_data;
925+ struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
911926
912927 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
913928 hid_ishtp_cl);
@@ -922,6 +937,7 @@ static const struct dev_pm_ops hid_ishtp_pm_ops = { · 8 unmodified lines
922937
923938 static struct ishtp_cl_driver hid_ishtp_cl_driver = {
924939 .name = "ish-hid",
940+ .guid = &hid_ishtp_guid,
925941 .probe = hid_ishtp_cl_probe,
926942 .remove = hid_ishtp_cl_remove,
927943 .reset = hid_ishtp_cl_reset,
@@ -933,7 +949,7 @@ static int __init ish_hid_init(void) · 5 unmodified lines
933949 int rv;
934950
935951 /* Register ISHTP client device driver with ISHTP Bus */
936− rv = ishtp_cl_driver_register(&hid_ishtp_cl_driver);
952+ rv = ishtp_cl_driver_register(&hid_ishtp_cl_driver, THIS_MODULE);
937953
938954 return rv;
939955
drivers/hid/intel-ish-hid/ishtp-hid.c+43−6
@@ -14,8 +14,8 @@ · 13 unmodified lines
1414 */
1515
1616 #include <linux/hid.h>
17+#include <linux/intel-ish-client-if.h>
1718 #include <uapi/linux/input.h>
18−#include "ishtp/client.h"
1919 #include "ishtp-hid.h"
2020
2121 /**
@@ -59,10 +59,46 @@ static void ishtp_hid_close(struct hid_device *hid) · 37 unmodified lines
5959 {
6060 }
6161
62−static int ishtp_raw_request(struct hid_device *hdev, unsigned char reportnum,
63− __u8 *buf, size_t len, unsigned char rtype, int reqtype)
62+static int ishtp_raw_request(struct hid_device *hid, unsigned char reportnum,
63+ __u8 *buf, size_t len, unsigned char rtype,
64+ int reqtype)
6465 {
65− return 0;
66+ struct ishtp_hid_data *hid_data = hid->driver_data;
67+ char *ishtp_buf = NULL;
68+ size_t ishtp_buf_len;
69+ unsigned int header_size = sizeof(struct hostif_msg);
70+
71+ if (rtype == HID_OUTPUT_REPORT)
72+ return -EINVAL;
73+
74+ hid_data->request_done = false;
75+ switch (reqtype) {
76+ case HID_REQ_GET_REPORT:
77+ hid_data->raw_buf = buf;
78+ hid_data->raw_buf_size = len;
79+ hid_data->raw_get_req = true;
80+
81+ hid_ishtp_get_report(hid, reportnum, rtype);
82+ break;
83+ case HID_REQ_SET_REPORT:
84+ /*
85+ * Spare 7 bytes for 64b accesses through
86+ * get/put_unaligned_le64()
87+ */
88+ ishtp_buf_len = len + header_size;
89+ ishtp_buf = kzalloc(ishtp_buf_len + 7, GFP_KERNEL);
90+ if (!ishtp_buf)
91+ return -ENOMEM;
92+
93+ memcpy(ishtp_buf + header_size, buf, len);
94+ hid_ishtp_set_feature(hid, ishtp_buf, ishtp_buf_len, reportnum);
95+ kfree(ishtp_buf);
96+ break;
97+ }
98+
99+ hid_hw_wait(hid);
100+
101+ return len;
66102 }
67103
68104 /**
@@ -87,6 +123,7 @@ static void ishtp_hid_request(struct hid_device *hid, struct hid_report *rep, · 18 unmodified lines
87123 hid_data->request_done = false;
88124 switch (reqtype) {
89125 case HID_REQ_GET_REPORT:
126+ hid_data->raw_get_req = false;
90127 hid_ishtp_get_report(hid, rep->id, rep->type);
91128 break;
92129 case HID_REQ_SET_REPORT:
@@ -116,7 +153,6 @@ static void ishtp_hid_request(struct hid_device *hid, struct hid_report *rep, · 23 unmodified lines
116153 static int ishtp_wait_for_response(struct hid_device *hid)
117154 {
118155 struct ishtp_hid_data *hid_data = hid->driver_data;
119− struct ishtp_cl_data *client_data = hid_data->client_data;
120156 int rv;
121157
122158 hid_ishtp_trace(client_data, "%s hid %p\n", __func__, hid);
@@ -204,7 +240,8 @@ int ishtp_hid_probe(unsigned int cur_hid_dev, · 81 unmodified lines
204240
205241 hid->ll_driver = &ishtp_hid_ll_driver;
206242 hid->bus = BUS_INTEL_ISHTP;
207− hid->dev.parent = &client_data->cl_device->dev;
243+ hid->dev.parent = ishtp_device(client_data->cl_device);
244+
208245 hid->version = le16_to_cpu(ISH_HID_VERSION);
209246 hid->vendor = le16_to_cpu(client_data->hid_devices[cur_hid_dev].vid);
210247 hid->product = le16_to_cpu(client_data->hid_devices[cur_hid_dev].pid);
drivers/hid/intel-ish-hid/ishtp-hid.h+11−3
@@ -24,9 +24,9 @@ · 23 unmodified lines
2424 #define IS_RESPONSE 0x80
2525
2626 /* Used to dump to Linux trace buffer, if enabled */
27−#define hid_ishtp_trace(client, ...) \
28− client->cl_device->ishtp_dev->print_log(\
29− client->cl_device->ishtp_dev, __VA_ARGS__)
27+extern void (*hid_print_trace)(void *unused, const char *format, ...);
28+#define hid_ishtp_trace(client, ...) \
29+ (hid_print_trace)(NULL, __VA_ARGS__)
3030
3131 /* ISH Transport protocol (ISHTP in short) GUID */
3232 static const guid_t hid_ishtp_guid =
@@ -159,6 +159,9 @@ struct ishtp_cl_data { · 126 unmodified lines
159159 * @client_data: Link to the client instance
160160 * @hid_wait: Completion waitq
161161 *
162+ * @raw_get_req: Flag indicating raw get request ongoing
163+ * @raw_buf: raw request buffer filled on receiving get report
164+ * @raw_buf_size: raw request buffer size
162165 * Used to tie hid hid->driver data to driver client instance
163166 */
164167 struct ishtp_hid_data {
@@ -166,6 +169,11 @@ struct ishtp_hid_data { · 1 unmodified line
166169 bool request_done;
167170 struct ishtp_cl_data *client_data;
168171 wait_queue_head_t hid_wait;
172+
173+ /* raw request */
174+ bool raw_get_req;
175+ u8 *raw_buf;
176+ size_t raw_buf_size;
169177 };
170178
171179 /* Interface functions between HID LL driver and ISH TP client */
drivers/hid/intel-ish-hid/ishtp/bus.c+92−4
@@ -170,6 +170,19 @@ struct ishtp_fw_client *ishtp_fw_cl_get_client(struct ishtp_device *dev, · 169 unmodified lines
170170 }
171171 EXPORT_SYMBOL(ishtp_fw_cl_get_client);
172172
173+/**
174+ * ishtp_get_fw_client_id() - Get fw client id
175+ *
176+ * This interface is used to reset HW get FW client id.
177+ *
178+ * Return: firmware client id.
179+ */
180+int ishtp_get_fw_client_id(struct ishtp_fw_client *fw_client)
181+{
182+ return fw_client->client_id;
183+}
184+EXPORT_SYMBOL(ishtp_get_fw_client_id);
185+
173186 /**
174187 * ishtp_fw_cl_by_id() - return index to fw_clients for client_id
175188 * @dev: the ishtp device structure
@@ -219,6 +232,26 @@ static int ishtp_cl_device_probe(struct device *dev) · 43 unmodified lines
219232 return driver->probe(device);
220233 }
221234
235+/**
236+ * ishtp_cl_bus_match() - Bus match() callback
237+ * @dev: the device structure
238+ * @drv: the driver structure
239+ *
240+ * This is a bus match callback, called when a new ishtp_cl_device is
241+ * registered during ishtp bus client enumeration. Use the guid_t in
242+ * drv and dev to decide whether they match or not.
243+ *
244+ * Return: 1 if dev & drv matches, 0 otherwise.
245+ */
246+static int ishtp_cl_bus_match(struct device *dev, struct device_driver *drv)
247+{
248+ struct ishtp_cl_device *device = to_ishtp_cl_device(dev);
249+ struct ishtp_cl_driver *driver = to_ishtp_cl_driver(drv);
250+
251+ return guid_equal(driver->guid,
252+ &device->fw_client->props.protocol_name);
253+}
254+
222255 /**
223256 * ishtp_cl_device_remove() - Bus remove() callback
224257 * @dev: the device structure
@@ -372,6 +405,7 @@ static struct bus_type ishtp_cl_bus_type = { · 147 unmodified lines
372405 .name = "ishtp",
373406 .dev_groups = ishtp_cl_dev_groups,
374407 .probe = ishtp_cl_device_probe,
408+ .match = ishtp_cl_bus_match,
375409 .remove = ishtp_cl_device_remove,
376410 .pm = &ishtp_cl_bus_dev_pm_ops,
377411 .uevent = ishtp_cl_uevent,
@@ -445,6 +479,7 @@ static struct ishtp_cl_device *ishtp_bus_add_device(struct ishtp_device *dev, · 67 unmodified lines
445479 }
446480
447481 ishtp_device_ready = true;
482+ dev_set_drvdata(&device->dev, device);
448483
449484 return device;
450485 }
@@ -464,7 +499,7 @@ static void ishtp_bus_remove_device(struct ishtp_cl_device *device) · 13 unmodified lines
464499 }
465500
466501 /**
467− * __ishtp_cl_driver_register() - Client driver register
502+ * ishtp_cl_driver_register() - Client driver register
468503 * @driver: the client driver instance
469504 * @owner: Owner of this driver module
470505 *
@@ -473,8 +508,8 @@ static void ishtp_bus_remove_device(struct ishtp_cl_device *device) · 2 unmodified lines
473508 *
474509 * Return: Return value of driver_register or -ENODEV if not ready
475510 */
476−int __ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
477− struct module *owner)
511+int ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
512+ struct module *owner)
478513 {
479514 int err;
480515
@@ -491,7 +526,7 @@ int __ishtp_cl_driver_register(struct ishtp_cl_driver *driver, · 10 unmodified lines
491526
492527 return 0;
493528 }
494−EXPORT_SYMBOL(__ishtp_cl_driver_register);
529+EXPORT_SYMBOL(ishtp_cl_driver_register);
495530
496531 /**
497532 * ishtp_cl_driver_unregister() - Client driver unregister
@@ -806,6 +841,59 @@ int ishtp_use_dma_transfer(void) · 308 unmodified lines
806841 return ishtp_use_dma;
807842 }
808843
844+/**
845+ * ishtp_device() - Return device pointer
846+ *
847+ * This interface is used to return device pointer from ishtp_cl_device
848+ * instance.
849+ *
850+ * Return: device *.
851+ */
852+struct device *ishtp_device(struct ishtp_cl_device *device)
853+{
854+ return &device->dev;
855+}
856+EXPORT_SYMBOL(ishtp_device);
857+
858+/**
859+ * ishtp_get_pci_device() - Return PCI device dev pointer
860+ * This interface is used to return PCI device pointer
861+ * from ishtp_cl_device instance.
862+ *
863+ * Return: device *.
864+ */
865+struct device *ishtp_get_pci_device(struct ishtp_cl_device *device)
866+{
867+ return device->ishtp_dev->devc;
868+}
869+EXPORT_SYMBOL(ishtp_get_pci_device);
870+
871+/**
872+ * ishtp_trace_callback() - Return trace callback
873+ *
874+ * This interface is used to return trace callback function pointer.
875+ *
876+ * Return: void *.
877+ */
878+void *ishtp_trace_callback(struct ishtp_cl_device *cl_device)
879+{
880+ return cl_device->ishtp_dev->print_log;
881+}
882+EXPORT_SYMBOL(ishtp_trace_callback);
883+
884+/**
885+ * ish_hw_reset() - Call HW reset IPC callback
886+ *
887+ * This interface is used to reset HW in case of error.
888+ *
889+ * Return: value from IPC hw_reset callback
890+ */
891+int ish_hw_reset(struct ishtp_device *dev)
892+{
893+ return dev->ops->hw_reset(dev);
894+}
895+EXPORT_SYMBOL(ish_hw_reset);
896+
809897 /**
810898 * ishtp_bus_register() - Function to register bus
811899 *
drivers/hid/intel-ish-hid/ishtp/bus.h+1−36
@@ -17,6 +17,7 @@ · 16 unmodified lines
1717
1818 #include <linux/device.h>
1919 #include <linux/mod_devicetable.h>
20+#include <linux/intel-ish-client-if.h>
2021
2122 struct ishtp_cl;
2223 struct ishtp_cl_device;
@@ -52,25 +53,6 @@ struct ishtp_cl_device { · 29 unmodified lines
5253 void (*event_cb)(struct ishtp_cl_device *device);
5354 };
5455
55−/**
56− * struct ishtp_cl_device - ISHTP device handle
57− * @driver: driver instance on a bus
58− * @name: Name of the device for probe
59− * @probe: driver callback for device probe
60− * @remove: driver callback on device removal
61− *
62− * Client drivers defines to get probed/removed for ISHTP client device.
63− */
64−struct ishtp_cl_driver {
65− struct device_driver driver;
66− const char *name;
67− int (*probe)(struct ishtp_cl_device *dev);
68− int (*remove)(struct ishtp_cl_device *dev);
69− int (*reset)(struct ishtp_cl_device *dev);
70− const struct dev_pm_ops *pm;
71−};
72−
73−
7456 int ishtp_bus_new_client(struct ishtp_device *dev);
7557 void ishtp_remove_all_clients(struct ishtp_device *dev);
7658 int ishtp_cl_device_bind(struct ishtp_cl *cl);
@@ -98,22 +80,5 @@ void ishtp_recv(struct ishtp_device *dev); · 21 unmodified lines
9880 void ishtp_reset_handler(struct ishtp_device *dev);
9981 void ishtp_reset_compl_handler(struct ishtp_device *dev);
10082
101−void ishtp_put_device(struct ishtp_cl_device *);
102−void ishtp_get_device(struct ishtp_cl_device *);
103−
104−void ishtp_set_drvdata(struct ishtp_cl_device *cl_device, void *data);
105−void *ishtp_get_drvdata(struct ishtp_cl_device *cl_device);
106−
107−int __ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
108− struct module *owner);
109−#define ishtp_cl_driver_register(driver) \
110− __ishtp_cl_driver_register(driver, THIS_MODULE)
111−void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver);
112−
113−int ishtp_register_event_cb(struct ishtp_cl_device *device,
114− void (*read_cb)(struct ishtp_cl_device *));
11583 int ishtp_fw_cl_by_uuid(struct ishtp_device *dev, const guid_t *cuuid);
116−struct ishtp_fw_client *ishtp_fw_cl_get_client(struct ishtp_device *dev,
117− const guid_t *uuid);
118−
11984 #endif /* _LINUX_ISHTP_CL_BUS_H */
drivers/hid/intel-ish-hid/ishtp/client.c+48−12
@@ -126,7 +126,7 @@ static void ishtp_cl_init(struct ishtp_cl *cl, struct ishtp_device *dev) · 125 unmodified lines
126126 *
127127 * Return: The allocated client instance or NULL on failure
128128 */
129−struct ishtp_cl *ishtp_cl_allocate(struct ishtp_device *dev)
129+struct ishtp_cl *ishtp_cl_allocate(struct ishtp_cl_device *cl_device)
130130 {
131131 struct ishtp_cl *cl;
132132
@@ -134,7 +134,7 @@ struct ishtp_cl *ishtp_cl_allocate(struct ishtp_device *dev) · 1 unmodified line
134134 if (!cl)
135135 return NULL;
136136
137− ishtp_cl_init(cl, dev);
137+ ishtp_cl_init(cl, cl_device->ishtp_dev);
138138 return cl;
139139 }
140140 EXPORT_SYMBOL(ishtp_cl_allocate);
@@ -168,9 +168,6 @@ EXPORT_SYMBOL(ishtp_cl_free); · 27 unmodified lines
168168 /**
169169 * ishtp_cl_link() - Reserve a host id and link the client instance
170170 * @cl: client device instance
171− * @id: host client id to use. It can be ISHTP_HOST_CLIENT_ID_ANY if any
172− * id from the available can be used
173− *
174171 *
175172 * This allocates a single bit in the hostmap. This function will make sure
176173 * that not many client sessions are opened at the same time. Once allocated
@@ -179,11 +176,11 @@ EXPORT_SYMBOL(ishtp_cl_free); · 2 unmodified lines
179176 *
180177 * Return: 0 or error code on failure
181178 */
182−int ishtp_cl_link(struct ishtp_cl *cl, int id)
179+int ishtp_cl_link(struct ishtp_cl *cl)
183180 {
184181 struct ishtp_device *dev;
185− unsigned long flags, flags_cl;
186− int ret = 0;
182+ unsigned long flags, flags_cl;
183+ int id, ret = 0;
187184
188185 if (WARN_ON(!cl || !cl->dev))
189186 return -EINVAL;
@@ -197,10 +194,7 @@ int ishtp_cl_link(struct ishtp_cl *cl, int id) · 7 unmodified lines
197194 goto unlock_dev;
198195 }
199196
200− /* If Id is not assigned get one*/
201− if (id == ISHTP_HOST_CLIENT_ID_ANY)
202− id = find_first_zero_bit(dev->host_clients_map,
203− ISHTP_CLIENTS_MAX);
197+ id = find_first_zero_bit(dev->host_clients_map, ISHTP_CLIENTS_MAX);
204198
205199 if (id >= ISHTP_CLIENTS_MAX) {
206200 spin_unlock_irqrestore(&dev->device_lock, flags);
@@ -1069,3 +1063,45 @@ void recv_ishtp_cl_msg_dma(struct ishtp_device *dev, void *msg, · 862 unmodified lines
10691063 eoi:
10701064 return;
10711065 }
1066+
1067+void *ishtp_get_client_data(struct ishtp_cl *cl)
1068+{
1069+ return cl->client_data;
1070+}
1071+EXPORT_SYMBOL(ishtp_get_client_data);
1072+
1073+void ishtp_set_client_data(struct ishtp_cl *cl, void *data)
1074+{
1075+ cl->client_data = data;
1076+}
1077+EXPORT_SYMBOL(ishtp_set_client_data);
1078+
1079+struct ishtp_device *ishtp_get_ishtp_device(struct ishtp_cl *cl)
1080+{
1081+ return cl->dev;
1082+}
1083+EXPORT_SYMBOL(ishtp_get_ishtp_device);
1084+
1085+void ishtp_set_tx_ring_size(struct ishtp_cl *cl, int size)
1086+{
1087+ cl->tx_ring_size = size;
1088+}
1089+EXPORT_SYMBOL(ishtp_set_tx_ring_size);
1090+
1091+void ishtp_set_rx_ring_size(struct ishtp_cl *cl, int size)
1092+{
1093+ cl->rx_ring_size = size;
1094+}
1095+EXPORT_SYMBOL(ishtp_set_rx_ring_size);
1096+
1097+void ishtp_set_connection_state(struct ishtp_cl *cl, int state)
1098+{
1099+ cl->state = state;
1100+}
1101+EXPORT_SYMBOL(ishtp_set_connection_state);
1102+
1103+void ishtp_cl_set_fw_client_id(struct ishtp_cl *cl, int fw_client_id)
1104+{
1105+ cl->fw_client_id = fw_client_id;
1106+}
1107+EXPORT_SYMBOL(ishtp_cl_set_fw_client_id);
drivers/hid/intel-ish-hid/ishtp/client.h+0−24
@@ -19,15 +19,6 @@ · 18 unmodified lines
1919 #include <linux/types.h>
2020 #include "ishtp-dev.h"
2121
22−/* Client state */
23−enum cl_state {
24− ISHTP_CL_INITIALIZING = 0,
25− ISHTP_CL_CONNECTING,
26− ISHTP_CL_CONNECTED,
27− ISHTP_CL_DISCONNECTING,
28− ISHTP_CL_DISCONNECTED
29−};
30−
3122 /* Tx and Rx ring size */
3223 #define CL_DEF_RX_RING_SIZE 2
3324 #define CL_DEF_TX_RING_SIZE 2
@@ -169,19 +160,4 @@ static inline bool ishtp_cl_cmp_id(const struct ishtp_cl *cl1, · 135 unmodified lines
169160 (cl1->fw_client_id == cl2->fw_client_id);
170161 }
171162
172−/* exported functions from ISHTP under client management scope */
173−struct ishtp_cl *ishtp_cl_allocate(struct ishtp_device *dev);
174−void ishtp_cl_free(struct ishtp_cl *cl);
175−int ishtp_cl_link(struct ishtp_cl *cl, int id);
176−void ishtp_cl_unlink(struct ishtp_cl *cl);
177−int ishtp_cl_disconnect(struct ishtp_cl *cl);
178−int ishtp_cl_connect(struct ishtp_cl *cl);
179−int ishtp_cl_send(struct ishtp_cl *cl, uint8_t *buf, size_t length);
180−int ishtp_cl_flush_queues(struct ishtp_cl *cl);
181−
182−/* exported functions from ISHTP client buffer management scope */
183−int ishtp_cl_io_rb_recycle(struct ishtp_cl_rb *rb);
184−bool ishtp_cl_tx_empty(struct ishtp_cl *cl);
185−struct ishtp_cl_rb *ishtp_cl_rx_get_rb(struct ishtp_cl *cl);
186−
187163 #endif /* _ISHTP_CLIENT_H_ */
drivers/hid/intel-ish-hid/ishtp/ishtp-dev.h+0−31
@@ -79,32 +79,6 @@ struct ishtp_fw_client { · 78 unmodified lines
7979 uint8_t client_id;
8080 };
8181
82−/**
83− * struct ishtp_msg_data - ISHTP message data struct
84− * @size: Size of data in the *data
85− * @data: Pointer to data
86− */
87−struct ishtp_msg_data {
88− uint32_t size;
89− unsigned char *data;
90−};
91−
92−/*
93− * struct ishtp_cl_rb - request block structure
94− * @list: Link to list members
95− * @cl: ISHTP client instance
96− * @buffer: message header
97− * @buf_idx: Index into buffer
98− * @read_time: unused at this time
99− */
100−struct ishtp_cl_rb {
101− struct list_head list;
102− struct ishtp_cl *cl;
103− struct ishtp_msg_data buffer;
104− unsigned long buf_idx;
105− unsigned long read_time;
106−};
107−
10882 /*
10983 * Control info for IPC messages ISHTP/IPC sending FIFO -
11084 * list with inline data buffer
@@ -264,11 +238,6 @@ static inline int ish_ipc_reset(struct ishtp_device *dev) · 153 unmodified lines
264238 return dev->ops->ipc_reset(dev);
265239 }
266240
267−static inline int ish_hw_reset(struct ishtp_device *dev)
268−{
269− return dev->ops->hw_reset(dev);
270−}
271−
272241 /* Exported function */
273242 void ishtp_device_init(struct ishtp_device *dev);
274243 int ishtp_start(struct ishtp_device *dev);
include/linux/hid.h+3−1
@@ -382,6 +382,7 @@ struct hid_item { · 381 unmodified lines
382382 #define HID_GROUP_WACOM 0x0101
383383 #define HID_GROUP_LOGITECH_DJ_DEVICE 0x0102
384384 #define HID_GROUP_STEAM 0x0103
385+#define HID_GROUP_LOGITECH_27MHZ_DEVICE 0x0104
385386
386387 /*
387388 * HID protocol status
@@ -417,6 +418,7 @@ struct hid_global { · 29 unmodified lines
417418
418419 struct hid_local {
419420 unsigned usage[HID_MAX_USAGES]; /* usage array */
421+ u8 usage_size[HID_MAX_USAGES]; /* usage size array */
420422 unsigned collection_index[HID_MAX_USAGES]; /* collection index array */
421423 unsigned usage_index;
422424 unsigned usage_minimum;
@@ -893,7 +895,7 @@ struct hid_field *hidinput_get_led_field(struct hid_device *hid); · 470 unmodified lines
893895 unsigned int hidinput_count_leds(struct hid_device *hid);
894896 __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code);
895897 void hid_output_report(struct hid_report *report, __u8 *data);
896−void __hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype);
898+int __hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype);
897899 u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags);
898900 struct hid_device *hid_allocate_device(void);
899901 struct hid_report *hid_register_report(struct hid_device *device,
include/linux/intel-ish-client-if.h+112−0
@@ -0,0 +1,112 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Intel ISH client Interface definitions
4+ *
5+ * Copyright (c) 2019, Intel Corporation.
6+ */
7+
8+#ifndef _INTEL_ISH_CLIENT_IF_H_
9+#define _INTEL_ISH_CLIENT_IF_H_
10+
11+struct ishtp_cl_device;
12+struct ishtp_device;
13+struct ishtp_cl;
14+struct ishtp_fw_client;
15+
16+/* Client state */
17+enum cl_state {
18+ ISHTP_CL_INITIALIZING = 0,
19+ ISHTP_CL_CONNECTING,
20+ ISHTP_CL_CONNECTED,
21+ ISHTP_CL_DISCONNECTING,
22+ ISHTP_CL_DISCONNECTED
23+};
24+
25+/**
26+ * struct ishtp_cl_device - ISHTP device handle
27+ * @driver: driver instance on a bus
28+ * @name: Name of the device for probe
29+ * @probe: driver callback for device probe
30+ * @remove: driver callback on device removal
31+ *
32+ * Client drivers defines to get probed/removed for ISHTP client device.
33+ */
34+struct ishtp_cl_driver {
35+ struct device_driver driver;
36+ const char *name;
37+ const guid_t *guid;
38+ int (*probe)(struct ishtp_cl_device *dev);
39+ int (*remove)(struct ishtp_cl_device *dev);
40+ int (*reset)(struct ishtp_cl_device *dev);
41+ const struct dev_pm_ops *pm;
42+};
43+
44+/**
45+ * struct ishtp_msg_data - ISHTP message data struct
46+ * @size: Size of data in the *data
47+ * @data: Pointer to data
48+ */
49+struct ishtp_msg_data {
50+ uint32_t size;
51+ unsigned char *data;
52+};
53+
54+/*
55+ * struct ishtp_cl_rb - request block structure
56+ * @list: Link to list members
57+ * @cl: ISHTP client instance
58+ * @buffer: message header
59+ * @buf_idx: Index into buffer
60+ * @read_time: unused at this time
61+ */
62+struct ishtp_cl_rb {
63+ struct list_head list;
64+ struct ishtp_cl *cl;
65+ struct ishtp_msg_data buffer;
66+ unsigned long buf_idx;
67+ unsigned long read_time;
68+};
69+
70+int ishtp_cl_driver_register(struct ishtp_cl_driver *driver,
71+ struct module *owner);
72+void ishtp_cl_driver_unregister(struct ishtp_cl_driver *driver);
73+int ishtp_register_event_cb(struct ishtp_cl_device *device,
74+ void (*read_cb)(struct ishtp_cl_device *));
75+
76+/* Get the device * from ishtp device instance */
77+struct device *ishtp_device(struct ishtp_cl_device *cl_device);
78+/* Trace interface for clients */
79+void *ishtp_trace_callback(struct ishtp_cl_device *cl_device);
80+/* Get device pointer of PCI device for DMA acces */
81+struct device *ishtp_get_pci_device(struct ishtp_cl_device *cl_device);
82+
83+struct ishtp_cl *ishtp_cl_allocate(struct ishtp_cl_device *cl_device);
84+void ishtp_cl_free(struct ishtp_cl *cl);
85+int ishtp_cl_link(struct ishtp_cl *cl);
86+void ishtp_cl_unlink(struct ishtp_cl *cl);
87+int ishtp_cl_disconnect(struct ishtp_cl *cl);
88+int ishtp_cl_connect(struct ishtp_cl *cl);
89+int ishtp_cl_send(struct ishtp_cl *cl, uint8_t *buf, size_t length);
90+int ishtp_cl_flush_queues(struct ishtp_cl *cl);
91+int ishtp_cl_io_rb_recycle(struct ishtp_cl_rb *rb);
92+bool ishtp_cl_tx_empty(struct ishtp_cl *cl);
93+struct ishtp_cl_rb *ishtp_cl_rx_get_rb(struct ishtp_cl *cl);
94+void *ishtp_get_client_data(struct ishtp_cl *cl);
95+void ishtp_set_client_data(struct ishtp_cl *cl, void *data);
96+struct ishtp_device *ishtp_get_ishtp_device(struct ishtp_cl *cl);
97+void ishtp_set_tx_ring_size(struct ishtp_cl *cl, int size);
98+void ishtp_set_rx_ring_size(struct ishtp_cl *cl, int size);
99+void ishtp_set_connection_state(struct ishtp_cl *cl, int state);
100+void ishtp_cl_set_fw_client_id(struct ishtp_cl *cl, int fw_client_id);
101+
102+void ishtp_put_device(struct ishtp_cl_device *cl_dev);
103+void ishtp_get_device(struct ishtp_cl_device *cl_dev);
104+void ishtp_set_drvdata(struct ishtp_cl_device *cl_device, void *data);
105+void *ishtp_get_drvdata(struct ishtp_cl_device *cl_device);
106+int ishtp_register_event_cb(struct ishtp_cl_device *device,
107+ void (*read_cb)(struct ishtp_cl_device *));
108+struct ishtp_fw_client *ishtp_fw_cl_get_client(struct ishtp_device *dev,
109+ const guid_t *uuid);
110+int ishtp_get_fw_client_id(struct ishtp_fw_client *fw_client);
111+int ish_hw_reset(struct ishtp_device *dev);
112+#endif /* _INTEL_ISH_CLIENT_IF_H_ */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t