Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 1 | /* |
Dmitry Torokhov | 4104d13 | 2007-05-07 16:16:29 -0400 | [diff] [blame] | 2 | * drivers/input/tablet/wacom_sys.c |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 3 | * |
Ping Cheng | 232f569 | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 4 | * USB Wacom tablet support - system specific code |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 14 | #include "wacom_wac.h" |
Dmitry Torokhov | 51269fe | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 15 | #include "wacom.h" |
Jason Gerecke | b58ba1b | 2014-12-05 13:37:32 -0800 | [diff] [blame] | 16 | #include <linux/input/mt.h> |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 17 | |
Ping Cheng | a417ea4 | 2011-08-16 00:17:56 -0700 | [diff] [blame] | 18 | #define WAC_MSG_RETRIES 5 |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 19 | #define WAC_CMD_RETRIES 10 |
| 20 | |
Ping Cheng | e0984bc | 2014-09-10 12:40:05 -0700 | [diff] [blame] | 21 | #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP) |
| 22 | #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP) |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 23 | #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP) |
Ping Cheng | e0984bc | 2014-09-10 12:40:05 -0700 | [diff] [blame] | 24 | |
Ping Cheng | c64d883 | 2014-09-10 12:41:04 -0700 | [diff] [blame] | 25 | static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf, |
| 26 | size_t size, unsigned int retries) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 27 | { |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 28 | int retval; |
| 29 | |
| 30 | do { |
Ping Cheng | c64d883 | 2014-09-10 12:41:04 -0700 | [diff] [blame] | 31 | retval = hid_hw_raw_request(hdev, buf[0], buf, size, type, |
Benjamin Tissoires | 27b20a9 | 2014-07-24 12:56:22 -0700 | [diff] [blame] | 32 | HID_REQ_GET_REPORT); |
Jason Gerecke | aef3156 | 2015-05-21 10:44:31 -0700 | [diff] [blame] | 33 | } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries); |
| 34 | |
| 35 | if (retval < 0) |
| 36 | hid_err(hdev, "wacom_get_report: ran out of retries " |
| 37 | "(last error = %d)\n", retval); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 38 | |
| 39 | return retval; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 42 | static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf, |
| 43 | size_t size, unsigned int retries) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 44 | { |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 45 | int retval; |
| 46 | |
| 47 | do { |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 48 | retval = hid_hw_raw_request(hdev, buf[0], buf, size, type, |
Benjamin Tissoires | 27b20a9 | 2014-07-24 12:56:22 -0700 | [diff] [blame] | 49 | HID_REQ_SET_REPORT); |
Jason Gerecke | aef3156 | 2015-05-21 10:44:31 -0700 | [diff] [blame] | 50 | } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries); |
| 51 | |
| 52 | if (retval < 0) |
| 53 | hid_err(hdev, "wacom_set_report: ran out of retries " |
| 54 | "(last error = %d)\n", retval); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 55 | |
| 56 | return retval; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jason Gerecke | 8341720 | 2017-11-10 11:50:01 -0800 | [diff] [blame] | 59 | static void wacom_wac_queue_insert(struct hid_device *hdev, |
| 60 | struct kfifo_rec_ptr_2 *fifo, |
| 61 | u8 *raw_data, int size) |
| 62 | { |
| 63 | bool warned = false; |
| 64 | |
| 65 | while (kfifo_avail(fifo) < size) { |
| 66 | if (!warned) |
| 67 | hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__); |
| 68 | warned = true; |
| 69 | |
| 70 | kfifo_skip(fifo); |
| 71 | } |
| 72 | |
| 73 | kfifo_in(fifo, raw_data, size); |
| 74 | } |
| 75 | |
| 76 | static void wacom_wac_queue_flush(struct hid_device *hdev, |
| 77 | struct kfifo_rec_ptr_2 *fifo) |
| 78 | { |
| 79 | while (!kfifo_is_empty(fifo)) { |
| 80 | u8 buf[WACOM_PKGLEN_MAX]; |
| 81 | int size; |
| 82 | int err; |
| 83 | |
| 84 | size = kfifo_out(fifo, buf, sizeof(buf)); |
| 85 | err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false); |
| 86 | if (err) { |
| 87 | hid_warn(hdev, "%s: unable to flush event due to error %d\n", |
| 88 | __func__, err); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static int wacom_wac_pen_serial_enforce(struct hid_device *hdev, |
| 94 | struct hid_report *report, u8 *raw_data, int size) |
| 95 | { |
| 96 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 97 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
| 98 | struct wacom_features *features = &wacom_wac->features; |
| 99 | bool flush = false; |
| 100 | bool insert = false; |
| 101 | int i, j; |
| 102 | |
| 103 | if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL)) |
| 104 | return 0; |
| 105 | |
| 106 | /* Queue events which have invalid tool type or serial number */ |
| 107 | for (i = 0; i < report->maxfield; i++) { |
| 108 | for (j = 0; j < report->field[i]->maxusage; j++) { |
| 109 | struct hid_field *field = report->field[i]; |
| 110 | struct hid_usage *usage = &field->usage[j]; |
| 111 | unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid); |
| 112 | unsigned int offset; |
| 113 | unsigned int size; |
| 114 | unsigned int value; |
| 115 | |
| 116 | if (equivalent_usage != HID_DG_INRANGE && |
| 117 | equivalent_usage != HID_DG_TOOLSERIALNUMBER && |
| 118 | equivalent_usage != WACOM_HID_WD_SERIALHI && |
| 119 | equivalent_usage != WACOM_HID_WD_TOOLTYPE) |
| 120 | continue; |
| 121 | |
| 122 | offset = field->report_offset; |
| 123 | size = field->report_size; |
| 124 | value = hid_field_extract(hdev, raw_data+1, offset + j * size, size); |
| 125 | |
| 126 | /* If we go out of range, we need to flush the queue ASAP */ |
| 127 | if (equivalent_usage == HID_DG_INRANGE) |
| 128 | value = !value; |
| 129 | |
| 130 | if (value) { |
| 131 | flush = true; |
| 132 | switch (equivalent_usage) { |
| 133 | case HID_DG_TOOLSERIALNUMBER: |
| 134 | wacom_wac->serial[0] = value; |
| 135 | break; |
| 136 | |
| 137 | case WACOM_HID_WD_SERIALHI: |
| 138 | wacom_wac->serial[0] |= ((__u64)value) << 32; |
| 139 | break; |
| 140 | |
| 141 | case WACOM_HID_WD_TOOLTYPE: |
| 142 | wacom_wac->id[0] = value; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | else { |
| 147 | insert = true; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (flush) |
| 153 | wacom_wac_queue_flush(hdev, &wacom_wac->pen_fifo); |
| 154 | else if (insert) |
| 155 | wacom_wac_queue_insert(hdev, &wacom_wac->pen_fifo, raw_data, size); |
| 156 | |
| 157 | return insert && !flush; |
| 158 | } |
| 159 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 160 | static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 161 | u8 *raw_data, int size) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 162 | { |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 163 | struct wacom *wacom = hid_get_drvdata(hdev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 164 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 165 | if (size > WACOM_PKGLEN_MAX) |
| 166 | return 1; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 167 | |
Jason Gerecke | 8341720 | 2017-11-10 11:50:01 -0800 | [diff] [blame] | 168 | if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size)) |
| 169 | return -1; |
| 170 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 171 | memcpy(wacom->wacom_wac.data, raw_data, size); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 172 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 173 | wacom_wac_irq(&wacom->wacom_wac, size); |
| 174 | |
| 175 | return 0; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 178 | static int wacom_open(struct input_dev *dev) |
| 179 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 180 | struct wacom *wacom = input_get_drvdata(dev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 181 | |
Benjamin Tissoires | dff6741 | 2014-12-01 11:52:40 -0500 | [diff] [blame] | 182 | return hid_hw_open(wacom->hdev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void wacom_close(struct input_dev *dev) |
| 186 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 187 | struct wacom *wacom = input_get_drvdata(dev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 188 | |
Benjamin Tissoires | 3dad188 | 2016-07-13 18:05:54 +0200 | [diff] [blame] | 189 | /* |
| 190 | * wacom->hdev should never be null, but surprisingly, I had the case |
| 191 | * once while unplugging the Wacom Wireless Receiver. |
| 192 | */ |
| 193 | if (wacom->hdev) |
| 194 | hid_hw_close(wacom->hdev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Chris Bagwell | 16bf288 | 2012-03-25 23:26:20 -0700 | [diff] [blame] | 197 | /* |
Benjamin Tissoires | 198fdee | 2014-07-24 13:03:05 -0700 | [diff] [blame] | 198 | * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res. |
Jason Gerecke | 115d5e1 | 2012-10-03 17:24:32 -0700 | [diff] [blame] | 199 | */ |
| 200 | static int wacom_calc_hid_res(int logical_extents, int physical_extents, |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 201 | unsigned unit, int exponent) |
Jason Gerecke | 115d5e1 | 2012-10-03 17:24:32 -0700 | [diff] [blame] | 202 | { |
Benjamin Tissoires | 198fdee | 2014-07-24 13:03:05 -0700 | [diff] [blame] | 203 | struct hid_field field = { |
| 204 | .logical_maximum = logical_extents, |
| 205 | .physical_maximum = physical_extents, |
| 206 | .unit = unit, |
| 207 | .unit_exponent = exponent, |
| 208 | }; |
Jason Gerecke | 115d5e1 | 2012-10-03 17:24:32 -0700 | [diff] [blame] | 209 | |
Benjamin Tissoires | 198fdee | 2014-07-24 13:03:05 -0700 | [diff] [blame] | 210 | return hidinput_calc_abs_res(&field, ABS_X); |
Jason Gerecke | 115d5e1 | 2012-10-03 17:24:32 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jason Gerecke | 5783251 | 2018-06-25 13:24:35 -0700 | [diff] [blame] | 213 | static void wacom_hid_usage_quirk(struct hid_device *hdev, |
| 214 | struct hid_field *field, struct hid_usage *usage) |
| 215 | { |
| 216 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 217 | struct wacom_features *features = &wacom->wacom_wac.features; |
| 218 | unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid); |
| 219 | |
| 220 | /* |
| 221 | * The Dell Canvas 27 needs to be switched to its vendor-defined |
| 222 | * report to provide the best resolution. |
| 223 | */ |
| 224 | if (hdev->vendor == USB_VENDOR_ID_WACOM && |
| 225 | hdev->product == 0x4200 && |
| 226 | field->application == HID_UP_MSVENDOR) { |
| 227 | wacom->wacom_wac.mode_report = field->report->id; |
| 228 | wacom->wacom_wac.mode_value = 2; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * ISDv4 devices which predate HID's adoption of the |
| 233 | * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its |
| 234 | * position instead. We can accurately detect if a |
| 235 | * usage with that value should be HID_DG_BARRELSWITCH2 |
| 236 | * based on the surrounding usages, which have remained |
| 237 | * constant across generations. |
| 238 | */ |
| 239 | if (features->type == HID_GENERIC && |
| 240 | usage->hid == 0x000D0000 && |
| 241 | field->application == HID_DG_PEN && |
| 242 | field->physical == HID_DG_STYLUS) { |
| 243 | int i = usage->usage_index; |
| 244 | |
| 245 | if (i-4 >= 0 && i+1 < field->maxusage && |
| 246 | field->usage[i-4].hid == HID_DG_TIPSWITCH && |
| 247 | field->usage[i-3].hid == HID_DG_BARRELSWITCH && |
| 248 | field->usage[i-2].hid == HID_DG_ERASER && |
| 249 | field->usage[i-1].hid == HID_DG_INVERT && |
| 250 | field->usage[i+1].hid == HID_DG_INRANGE) { |
| 251 | usage->hid = HID_DG_BARRELSWITCH2; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* 2nd-generation Intuos Pro Large has incorrect Y maximum */ |
| 256 | if (hdev->vendor == USB_VENDOR_ID_WACOM && |
| 257 | hdev->product == 0x0358 && |
| 258 | WACOM_PEN_FIELD(field) && |
| 259 | equivalent_usage == HID_GD_Y) { |
| 260 | field->logical_maximum = 43200; |
| 261 | } |
| 262 | } |
| 263 | |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 264 | static void wacom_feature_mapping(struct hid_device *hdev, |
| 265 | struct hid_field *field, struct hid_usage *usage) |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 266 | { |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 267 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 268 | struct wacom_features *features = &wacom->wacom_wac.features; |
Benjamin Tissoires | 5ae6e89 | 2014-09-23 12:08:09 -0400 | [diff] [blame] | 269 | struct hid_data *hid_data = &wacom->wacom_wac.hid_data; |
Aaron Armstrong Skomra | ac2423c | 2017-01-25 12:08:40 -0800 | [diff] [blame] | 270 | unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid); |
Benjamin Tissoires | 8ffffd5 | 2014-09-16 16:56:39 -0400 | [diff] [blame] | 271 | u8 *data; |
| 272 | int ret; |
Aaron Ma | 3064a03 | 2018-02-03 23:57:15 +0800 | [diff] [blame] | 273 | u32 n; |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 274 | |
Jason Gerecke | 5783251 | 2018-06-25 13:24:35 -0700 | [diff] [blame] | 275 | wacom_hid_usage_quirk(hdev, field, usage); |
| 276 | |
Aaron Armstrong Skomra | ac2423c | 2017-01-25 12:08:40 -0800 | [diff] [blame] | 277 | switch (equivalent_usage) { |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 278 | case HID_DG_CONTACTMAX: |
| 279 | /* leave touch_max as is if predefined */ |
Benjamin Tissoires | 8ffffd5 | 2014-09-16 16:56:39 -0400 | [diff] [blame] | 280 | if (!features->touch_max) { |
| 281 | /* read manually */ |
| 282 | data = kzalloc(2, GFP_KERNEL); |
| 283 | if (!data) |
| 284 | break; |
| 285 | data[0] = field->report->id; |
| 286 | ret = wacom_get_report(hdev, HID_FEATURE_REPORT, |
Jason Gerecke | 05e8fd9 | 2015-05-21 10:44:32 -0700 | [diff] [blame] | 287 | data, 2, WAC_CMD_RETRIES); |
| 288 | if (ret == 2) { |
Benjamin Tissoires | 8ffffd5 | 2014-09-16 16:56:39 -0400 | [diff] [blame] | 289 | features->touch_max = data[1]; |
Jason Gerecke | 05e8fd9 | 2015-05-21 10:44:32 -0700 | [diff] [blame] | 290 | } else { |
| 291 | features->touch_max = 16; |
| 292 | hid_warn(hdev, "wacom_feature_mapping: " |
| 293 | "could not get HID_DG_CONTACTMAX, " |
| 294 | "defaulting to %d\n", |
| 295 | features->touch_max); |
| 296 | } |
Benjamin Tissoires | 8ffffd5 | 2014-09-16 16:56:39 -0400 | [diff] [blame] | 297 | kfree(data); |
| 298 | } |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 299 | break; |
Benjamin Tissoires | 5ae6e89 | 2014-09-23 12:08:09 -0400 | [diff] [blame] | 300 | case HID_DG_INPUTMODE: |
| 301 | /* Ignore if value index is out of bounds. */ |
| 302 | if (usage->usage_index >= field->report_count) { |
| 303 | dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n"); |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | hid_data->inputmode = field->report->id; |
| 308 | hid_data->inputmode_index = usage->usage_index; |
| 309 | break; |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 310 | |
| 311 | case HID_UP_DIGITIZER: |
| 312 | if (field->report->id == 0x0B && |
Jason Gerecke | 8de8228 | 2016-10-19 18:03:37 -0700 | [diff] [blame] | 313 | (field->application == WACOM_HID_G9_PEN || |
| 314 | field->application == WACOM_HID_G11_PEN)) { |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 315 | wacom->wacom_wac.mode_report = field->report->id; |
| 316 | wacom->wacom_wac.mode_value = 0; |
| 317 | } |
| 318 | break; |
| 319 | |
Jason Gerecke | c9c0958 | 2016-10-19 18:03:43 -0700 | [diff] [blame] | 320 | case WACOM_HID_WD_DATAMODE: |
| 321 | wacom->wacom_wac.mode_report = field->report->id; |
| 322 | wacom->wacom_wac.mode_value = 2; |
| 323 | break; |
| 324 | |
Jason Gerecke | 8de8228 | 2016-10-19 18:03:37 -0700 | [diff] [blame] | 325 | case WACOM_HID_UP_G9: |
| 326 | case WACOM_HID_UP_G11: |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 327 | if (field->report->id == 0x03 && |
Jason Gerecke | 8de8228 | 2016-10-19 18:03:37 -0700 | [diff] [blame] | 328 | (field->application == WACOM_HID_G9_TOUCHSCREEN || |
| 329 | field->application == WACOM_HID_G11_TOUCHSCREEN)) { |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 330 | wacom->wacom_wac.mode_report = field->report->id; |
| 331 | wacom->wacom_wac.mode_value = 0; |
| 332 | } |
| 333 | break; |
Jason Gerecke | 345857b | 2016-10-19 18:03:50 -0700 | [diff] [blame] | 334 | case WACOM_HID_WD_OFFSETLEFT: |
| 335 | case WACOM_HID_WD_OFFSETTOP: |
| 336 | case WACOM_HID_WD_OFFSETRIGHT: |
| 337 | case WACOM_HID_WD_OFFSETBOTTOM: |
| 338 | /* read manually */ |
| 339 | n = hid_report_len(field->report); |
| 340 | data = hid_alloc_report_buf(field->report, GFP_KERNEL); |
| 341 | if (!data) |
| 342 | break; |
| 343 | data[0] = field->report->id; |
| 344 | ret = wacom_get_report(hdev, HID_FEATURE_REPORT, |
| 345 | data, n, WAC_CMD_RETRIES); |
| 346 | if (ret == n) { |
| 347 | ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, |
| 348 | data, n, 0); |
| 349 | } else { |
| 350 | hid_warn(hdev, "%s: could not retrieve sensor offsets\n", |
| 351 | __func__); |
| 352 | } |
| 353 | kfree(data); |
| 354 | break; |
Ping Cheng | f393ee2 | 2012-04-29 21:09:17 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Chris Bagwell | 428f858 | 2011-10-26 22:26:59 -0700 | [diff] [blame] | 358 | /* |
| 359 | * Interface Descriptor of wacom devices can be incomplete and |
| 360 | * inconsistent so wacom_features table is used to store stylus |
| 361 | * device's packet lengths, various maximum values, and tablet |
| 362 | * resolution based on product ID's. |
| 363 | * |
| 364 | * For devices that contain 2 interfaces, wacom_features table is |
| 365 | * inaccurate for the touch interface. Since the Interface Descriptor |
| 366 | * for touch interfaces has pretty complete data, this function exists |
| 367 | * to query tablet for this missing information instead of hard coding in |
| 368 | * an additional table. |
| 369 | * |
| 370 | * A typical Interface Descriptor for a stylus will contain a |
| 371 | * boot mouse application collection that is not of interest and this |
| 372 | * function will ignore it. |
| 373 | * |
| 374 | * It also contains a digitizer application collection that also is not |
| 375 | * of interest since any information it contains would be duplicate |
| 376 | * of what is in wacom_features. Usually it defines a report of an array |
| 377 | * of bytes that could be used as max length of the stylus packet returned. |
| 378 | * If it happens to define a Digitizer-Stylus Physical Collection then |
| 379 | * the X and Y logical values contain valid data but it is ignored. |
| 380 | * |
| 381 | * A typical Interface Descriptor for a touch interface will contain a |
| 382 | * Digitizer-Finger Physical Collection which will define both logical |
| 383 | * X/Y maximum as well as the physical size of tablet. Since touch |
| 384 | * interfaces haven't supported pressure or distance, this is enough |
| 385 | * information to override invalid values in the wacom_features table. |
Chris Bagwell | 4134361 | 2011-10-26 22:32:52 -0700 | [diff] [blame] | 386 | * |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 387 | * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful |
| 388 | * data. We deal with them after returning from this function. |
Chris Bagwell | 428f858 | 2011-10-26 22:26:59 -0700 | [diff] [blame] | 389 | */ |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 390 | static void wacom_usage_mapping(struct hid_device *hdev, |
| 391 | struct hid_field *field, struct hid_usage *usage) |
| 392 | { |
| 393 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 394 | struct wacom_features *features = &wacom->wacom_wac.features; |
Benjamin Tissoires | d97a552 | 2015-01-05 16:32:12 -0500 | [diff] [blame] | 395 | bool finger = WACOM_FINGER_FIELD(field); |
| 396 | bool pen = WACOM_PEN_FIELD(field); |
Ping Cheng | 418b573 | 2018-06-25 13:24:36 -0700 | [diff] [blame^] | 397 | unsigned equivalent_usage = wacom_equivalent_usage(usage->hid); |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 398 | |
| 399 | /* |
| 400 | * Requiring Stylus Usage will ignore boot mouse |
| 401 | * X/Y values and some cases of invalid Digitizer X/Y |
| 402 | * values commonly reported. |
| 403 | */ |
Jason Gerecke | 042628a | 2015-04-30 17:51:54 -0700 | [diff] [blame] | 404 | if (pen) |
Jason Gerecke | aa86b18 | 2015-06-15 18:01:42 -0700 | [diff] [blame] | 405 | features->device_type |= WACOM_DEVICETYPE_PEN; |
Jason Gerecke | 042628a | 2015-04-30 17:51:54 -0700 | [diff] [blame] | 406 | else if (finger) |
Jason Gerecke | aa86b18 | 2015-06-15 18:01:42 -0700 | [diff] [blame] | 407 | features->device_type |= WACOM_DEVICETYPE_TOUCH; |
Jason Gerecke | 042628a | 2015-04-30 17:51:54 -0700 | [diff] [blame] | 408 | else |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 409 | return; |
| 410 | |
Jason Gerecke | 5783251 | 2018-06-25 13:24:35 -0700 | [diff] [blame] | 411 | wacom_hid_usage_quirk(hdev, field, usage); |
Jason Gerecke | d471b6b | 2018-06-12 13:42:46 -0700 | [diff] [blame] | 412 | |
Ping Cheng | 418b573 | 2018-06-25 13:24:36 -0700 | [diff] [blame^] | 413 | switch (equivalent_usage) { |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 414 | case HID_GD_X: |
| 415 | features->x_max = field->logical_maximum; |
| 416 | if (finger) { |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 417 | features->x_phy = field->physical_maximum; |
Ping Cheng | 3b164a0 | 2015-09-23 09:59:10 -0700 | [diff] [blame] | 418 | if ((features->type != BAMBOO_PT) && |
| 419 | (features->type != BAMBOO_TOUCH)) { |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 420 | features->unit = field->unit; |
| 421 | features->unitExpo = field->unit_exponent; |
| 422 | } |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 423 | } |
| 424 | break; |
| 425 | case HID_GD_Y: |
| 426 | features->y_max = field->logical_maximum; |
| 427 | if (finger) { |
| 428 | features->y_phy = field->physical_maximum; |
Ping Cheng | 3b164a0 | 2015-09-23 09:59:10 -0700 | [diff] [blame] | 429 | if ((features->type != BAMBOO_PT) && |
| 430 | (features->type != BAMBOO_TOUCH)) { |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 431 | features->unit = field->unit; |
| 432 | features->unitExpo = field->unit_exponent; |
| 433 | } |
| 434 | } |
| 435 | break; |
| 436 | case HID_DG_TIPPRESSURE: |
| 437 | if (pen) |
| 438 | features->pressure_max = field->logical_maximum; |
| 439 | break; |
| 440 | } |
Benjamin Tissoires | 7704ac9 | 2014-09-23 12:08:08 -0400 | [diff] [blame] | 441 | |
| 442 | if (features->type == HID_GENERIC) |
| 443 | wacom_wac_usage_mapping(hdev, field, usage); |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Jason Gerecke | b58ba1b | 2014-12-05 13:37:32 -0800 | [diff] [blame] | 446 | static void wacom_post_parse_hid(struct hid_device *hdev, |
| 447 | struct wacom_features *features) |
| 448 | { |
| 449 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 450 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
| 451 | |
| 452 | if (features->type == HID_GENERIC) { |
| 453 | /* Any last-minute generic device setup */ |
Benjamin Tissoires | 4082da8 | 2017-02-14 21:27:18 -0800 | [diff] [blame] | 454 | if (wacom_wac->has_mode_change) { |
| 455 | if (wacom_wac->is_direct_mode) |
| 456 | features->device_type |= WACOM_DEVICETYPE_DIRECT; |
| 457 | else |
| 458 | features->device_type &= ~WACOM_DEVICETYPE_DIRECT; |
| 459 | } |
| 460 | |
Jason Gerecke | b58ba1b | 2014-12-05 13:37:32 -0800 | [diff] [blame] | 461 | if (features->touch_max > 1) { |
Aaron Armstrong Skomra | ac2423c | 2017-01-25 12:08:40 -0800 | [diff] [blame] | 462 | if (features->device_type & WACOM_DEVICETYPE_DIRECT) |
| 463 | input_mt_init_slots(wacom_wac->touch_input, |
| 464 | wacom_wac->features.touch_max, |
| 465 | INPUT_MT_DIRECT); |
| 466 | else |
| 467 | input_mt_init_slots(wacom_wac->touch_input, |
| 468 | wacom_wac->features.touch_max, |
| 469 | INPUT_MT_POINTER); |
Jason Gerecke | b58ba1b | 2014-12-05 13:37:32 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 474 | static void wacom_parse_hid(struct hid_device *hdev, |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 475 | struct wacom_features *features) |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 476 | { |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 477 | struct hid_report_enum *rep_enum; |
| 478 | struct hid_report *hreport; |
| 479 | int i, j; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 480 | |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 481 | /* check features first */ |
| 482 | rep_enum = &hdev->report_enum[HID_FEATURE_REPORT]; |
| 483 | list_for_each_entry(hreport, &rep_enum->report_list, list) { |
| 484 | for (i = 0; i < hreport->maxfield; i++) { |
| 485 | /* Ignore if report count is out of bounds. */ |
| 486 | if (hreport->field[i]->report_count < 1) |
| 487 | continue; |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 488 | |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 489 | for (j = 0; j < hreport->field[i]->maxusage; j++) { |
| 490 | wacom_feature_mapping(hdev, hreport->field[i], |
| 491 | hreport->field[i]->usage + j); |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 492 | } |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 496 | /* now check the input usages */ |
| 497 | rep_enum = &hdev->report_enum[HID_INPUT_REPORT]; |
| 498 | list_for_each_entry(hreport, &rep_enum->report_list, list) { |
| 499 | |
| 500 | if (!hreport->maxfield) |
| 501 | continue; |
| 502 | |
| 503 | for (i = 0; i < hreport->maxfield; i++) |
| 504 | for (j = 0; j < hreport->field[i]->maxusage; j++) |
| 505 | wacom_usage_mapping(hdev, hreport->field[i], |
| 506 | hreport->field[i]->usage + j); |
| 507 | } |
Jason Gerecke | b58ba1b | 2014-12-05 13:37:32 -0800 | [diff] [blame] | 508 | |
| 509 | wacom_post_parse_hid(hdev, features); |
Ping Cheng | 545f4e9 | 2008-11-24 11:44:27 -0500 | [diff] [blame] | 510 | } |
| 511 | |
Benjamin Tissoires | 5ae6e89 | 2014-09-23 12:08:09 -0400 | [diff] [blame] | 512 | static int wacom_hid_set_device_mode(struct hid_device *hdev) |
| 513 | { |
| 514 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 515 | struct hid_data *hid_data = &wacom->wacom_wac.hid_data; |
| 516 | struct hid_report *r; |
| 517 | struct hid_report_enum *re; |
| 518 | |
| 519 | if (hid_data->inputmode < 0) |
| 520 | return 0; |
| 521 | |
| 522 | re = &(hdev->report_enum[HID_FEATURE_REPORT]); |
| 523 | r = re->report_id_hash[hid_data->inputmode]; |
| 524 | if (r) { |
| 525 | r->field[0]->value[hid_data->inputmode_index] = 2; |
| 526 | hid_hw_request(hdev, r, HID_REQ_SET_REPORT); |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 531 | static int wacom_set_device_mode(struct hid_device *hdev, |
| 532 | struct wacom_wac *wacom_wac) |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 533 | { |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 534 | u8 *rep_data; |
| 535 | struct hid_report *r; |
| 536 | struct hid_report_enum *re; |
Aaron Ma | 3064a03 | 2018-02-03 23:57:15 +0800 | [diff] [blame] | 537 | u32 length; |
Jason Gerecke | fe494bc | 2012-10-03 17:25:35 -0700 | [diff] [blame] | 538 | int error = -ENOMEM, limit = 0; |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 539 | |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 540 | if (wacom_wac->mode_report < 0) |
| 541 | return 0; |
| 542 | |
| 543 | re = &(hdev->report_enum[HID_FEATURE_REPORT]); |
| 544 | r = re->report_id_hash[wacom_wac->mode_report]; |
| 545 | if (!r) |
| 546 | return -EINVAL; |
| 547 | |
| 548 | rep_data = hid_alloc_report_buf(r, GFP_KERNEL); |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 549 | if (!rep_data) |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 550 | return -ENOMEM; |
| 551 | |
| 552 | length = hid_report_len(r); |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 553 | |
Jason Gerecke | fe494bc | 2012-10-03 17:25:35 -0700 | [diff] [blame] | 554 | do { |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 555 | rep_data[0] = wacom_wac->mode_report; |
| 556 | rep_data[1] = wacom_wac->mode_value; |
Chris Bagwell | 9937c02 | 2013-01-23 19:37:34 -0800 | [diff] [blame] | 557 | |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 558 | error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, |
| 559 | length, 1); |
Benjamin Tissoires | 3cb8315 | 2014-07-24 12:47:47 -0700 | [diff] [blame] | 560 | if (error >= 0) |
Benjamin Tissoires | 27b20a9 | 2014-07-24 12:56:22 -0700 | [diff] [blame] | 561 | error = wacom_get_report(hdev, HID_FEATURE_REPORT, |
Ping Cheng | c64d883 | 2014-09-10 12:41:04 -0700 | [diff] [blame] | 562 | rep_data, length, 1); |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 563 | } while (error >= 0 && |
| 564 | rep_data[1] != wacom_wac->mode_report && |
| 565 | limit++ < WAC_MSG_RETRIES); |
Dmitry Torokhov | 3b7307c | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 566 | |
| 567 | kfree(rep_data); |
| 568 | |
| 569 | return error < 0 ? error : 0; |
| 570 | } |
| 571 | |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 572 | static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed, |
| 573 | struct wacom_features *features) |
| 574 | { |
Benjamin Tissoires | 387142b | 2014-08-06 13:52:56 -0700 | [diff] [blame] | 575 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 576 | int ret; |
| 577 | u8 rep_data[2]; |
| 578 | |
| 579 | switch (features->type) { |
| 580 | case GRAPHIRE_BT: |
| 581 | rep_data[0] = 0x03; |
| 582 | rep_data[1] = 0x00; |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 583 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2, |
| 584 | 3); |
Benjamin Tissoires | 387142b | 2014-08-06 13:52:56 -0700 | [diff] [blame] | 585 | |
| 586 | if (ret >= 0) { |
| 587 | rep_data[0] = speed == 0 ? 0x05 : 0x06; |
| 588 | rep_data[1] = 0x00; |
| 589 | |
| 590 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 591 | rep_data, 2, 3); |
Benjamin Tissoires | 387142b | 2014-08-06 13:52:56 -0700 | [diff] [blame] | 592 | |
| 593 | if (ret >= 0) { |
| 594 | wacom->wacom_wac.bt_high_speed = speed; |
| 595 | return 0; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Note that if the raw queries fail, it's not a hard failure |
| 601 | * and it is safe to continue |
| 602 | */ |
| 603 | hid_warn(hdev, "failed to poke device, command %d, err %d\n", |
| 604 | rep_data[0], ret); |
| 605 | break; |
Benjamin Tissoires | 81af7e6 | 2014-08-06 13:55:56 -0700 | [diff] [blame] | 606 | case INTUOS4WL: |
| 607 | if (speed == 1) |
| 608 | wacom->wacom_wac.bt_features &= ~0x20; |
| 609 | else |
| 610 | wacom->wacom_wac.bt_features |= 0x20; |
| 611 | |
| 612 | rep_data[0] = 0x03; |
| 613 | rep_data[1] = wacom->wacom_wac.bt_features; |
| 614 | |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 615 | ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2, |
| 616 | 1); |
Benjamin Tissoires | 81af7e6 | 2014-08-06 13:55:56 -0700 | [diff] [blame] | 617 | if (ret >= 0) |
| 618 | wacom->wacom_wac.bt_high_speed = speed; |
| 619 | break; |
Benjamin Tissoires | 387142b | 2014-08-06 13:52:56 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
Jason Gerecke | fe494bc | 2012-10-03 17:25:35 -0700 | [diff] [blame] | 625 | /* |
| 626 | * Switch the tablet into its most-capable mode. Wacom tablets are |
| 627 | * typically configured to power-up in a mode which sends mouse-like |
| 628 | * reports to the OS. To get absolute position, pressure data, etc. |
| 629 | * from the tablet, it is necessary to switch the tablet out of this |
| 630 | * mode and into one which sends the full range of tablet data. |
| 631 | */ |
Benjamin Tissoires | a544c61 | 2017-01-20 16:20:13 +0100 | [diff] [blame] | 632 | static int _wacom_query_tablet_data(struct wacom *wacom) |
Jason Gerecke | fe494bc | 2012-10-03 17:25:35 -0700 | [diff] [blame] | 633 | { |
Benjamin Tissoires | a544c61 | 2017-01-20 16:20:13 +0100 | [diff] [blame] | 634 | struct hid_device *hdev = wacom->hdev; |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 635 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
Benjamin Tissoires | a544c61 | 2017-01-20 16:20:13 +0100 | [diff] [blame] | 636 | struct wacom_features *features = &wacom_wac->features; |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 637 | |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 638 | if (hdev->bus == BUS_BLUETOOTH) |
| 639 | return wacom_bt_query_tablet_data(hdev, 1, features); |
| 640 | |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 641 | if (features->type != HID_GENERIC) { |
| 642 | if (features->device_type & WACOM_DEVICETYPE_TOUCH) { |
| 643 | if (features->type > TABLETPC) { |
| 644 | /* MT Tablet PC touch */ |
| 645 | wacom_wac->mode_report = 3; |
| 646 | wacom_wac->mode_value = 4; |
| 647 | } else if (features->type == WACOM_24HDT) { |
| 648 | wacom_wac->mode_report = 18; |
| 649 | wacom_wac->mode_value = 2; |
| 650 | } else if (features->type == WACOM_27QHDT) { |
| 651 | wacom_wac->mode_report = 131; |
| 652 | wacom_wac->mode_value = 2; |
| 653 | } else if (features->type == BAMBOO_PAD) { |
| 654 | wacom_wac->mode_report = 2; |
| 655 | wacom_wac->mode_value = 2; |
| 656 | } |
| 657 | } else if (features->device_type & WACOM_DEVICETYPE_PEN) { |
| 658 | if (features->type <= BAMBOO_PT) { |
| 659 | wacom_wac->mode_report = 2; |
| 660 | wacom_wac->mode_value = 2; |
| 661 | } |
Jason Gerecke | fe494bc | 2012-10-03 17:25:35 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 665 | wacom_set_device_mode(hdev, wacom_wac); |
| 666 | |
| 667 | if (features->type == HID_GENERIC) |
| 668 | return wacom_hid_set_device_mode(hdev); |
| 669 | |
Jason Gerecke | fe494bc | 2012-10-03 17:25:35 -0700 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 673 | static void wacom_retrieve_hid_descriptor(struct hid_device *hdev, |
Ping Cheng | 1963518 | 2012-04-29 21:09:18 -0700 | [diff] [blame] | 674 | struct wacom_features *features) |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 675 | { |
Benjamin Tissoires | 27b20a9 | 2014-07-24 12:56:22 -0700 | [diff] [blame] | 676 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 677 | struct usb_interface *intf = wacom->intf; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 678 | |
Henrik Rydberg | fed87e6 | 2010-09-05 12:25:11 -0700 | [diff] [blame] | 679 | /* default features */ |
Henrik Rydberg | fed87e6 | 2010-09-05 12:25:11 -0700 | [diff] [blame] | 680 | features->x_fuzz = 4; |
| 681 | features->y_fuzz = 4; |
| 682 | features->pressure_fuzz = 0; |
Jason Gerecke | bef7e20 | 2016-04-22 14:30:53 -0700 | [diff] [blame] | 683 | features->distance_fuzz = 1; |
| 684 | features->tilt_fuzz = 1; |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 685 | |
Chris Bagwell | d3825d5 | 2012-03-25 23:26:11 -0700 | [diff] [blame] | 686 | /* |
| 687 | * The wireless device HID is basic and layout conflicts with |
| 688 | * other tablets (monitor and touch interface can look like pen). |
| 689 | * Skip the query for this type and modify defaults based on |
| 690 | * interface number. |
| 691 | */ |
| 692 | if (features->type == WIRELESS) { |
Jason Gerecke | 3f14a63 | 2015-08-03 10:17:05 -0700 | [diff] [blame] | 693 | if (intf->cur_altsetting->desc.bInterfaceNumber == 0) |
Jason Gerecke | ccad85c | 2015-08-03 10:17:04 -0700 | [diff] [blame] | 694 | features->device_type = WACOM_DEVICETYPE_WL_MONITOR; |
Jason Gerecke | 3f14a63 | 2015-08-03 10:17:05 -0700 | [diff] [blame] | 695 | else |
Jason Gerecke | aa86b18 | 2015-06-15 18:01:42 -0700 | [diff] [blame] | 696 | features->device_type = WACOM_DEVICETYPE_NONE; |
Jason Gerecke | 3f14a63 | 2015-08-03 10:17:05 -0700 | [diff] [blame] | 697 | return; |
Chris Bagwell | d3825d5 | 2012-03-25 23:26:11 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 700 | wacom_parse_hid(hdev, features); |
Ping Cheng | ec67bbe | 2009-12-15 00:35:24 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 703 | struct wacom_hdev_data { |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 704 | struct list_head list; |
| 705 | struct kref kref; |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 706 | struct hid_device *dev; |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 707 | struct wacom_shared shared; |
| 708 | }; |
| 709 | |
| 710 | static LIST_HEAD(wacom_udev_list); |
| 711 | static DEFINE_MUTEX(wacom_udev_list_lock); |
| 712 | |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 713 | static bool compare_device_paths(struct hid_device *hdev_a, |
| 714 | struct hid_device *hdev_b, char separator) |
| 715 | { |
| 716 | int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys; |
| 717 | int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys; |
| 718 | |
| 719 | if (n1 != n2 || n1 <= 0 || n2 <= 0) |
| 720 | return false; |
| 721 | |
| 722 | return !strncmp(hdev_a->phys, hdev_b->phys, n1); |
| 723 | } |
| 724 | |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 725 | static bool wacom_are_sibling(struct hid_device *hdev, |
| 726 | struct hid_device *sibling) |
Jason Gerecke | aea2bf6 | 2012-10-21 00:38:03 -0700 | [diff] [blame] | 727 | { |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 728 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 729 | struct wacom_features *features = &wacom->wacom_wac.features; |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 730 | struct wacom *sibling_wacom = hid_get_drvdata(sibling); |
| 731 | struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features; |
| 732 | __u32 oVid = features->oVid ? features->oVid : hdev->vendor; |
| 733 | __u32 oPid = features->oPid ? features->oPid : hdev->product; |
Jason Gerecke | aea2bf6 | 2012-10-21 00:38:03 -0700 | [diff] [blame] | 734 | |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 735 | /* The defined oVid/oPid must match that of the sibling */ |
| 736 | if (features->oVid != HID_ANY_ID && sibling->vendor != oVid) |
| 737 | return false; |
| 738 | if (features->oPid != HID_ANY_ID && sibling->product != oPid) |
| 739 | return false; |
| 740 | |
| 741 | /* |
| 742 | * Devices with the same VID/PID must share the same physical |
| 743 | * device path, while those with different VID/PID must share |
| 744 | * the same physical parent device path. |
| 745 | */ |
| 746 | if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) { |
| 747 | if (!compare_device_paths(hdev, sibling, '/')) |
| 748 | return false; |
| 749 | } else { |
| 750 | if (!compare_device_paths(hdev, sibling, '.')) |
| 751 | return false; |
Jason Gerecke | aea2bf6 | 2012-10-21 00:38:03 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 754 | /* Skip the remaining heuristics unless you are a HID_GENERIC device */ |
| 755 | if (features->type != HID_GENERIC) |
| 756 | return true; |
| 757 | |
| 758 | /* |
| 759 | * Direct-input devices may not be siblings of indirect-input |
| 760 | * devices. |
| 761 | */ |
| 762 | if ((features->device_type & WACOM_DEVICETYPE_DIRECT) && |
| 763 | !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT)) |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 764 | return false; |
| 765 | |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 766 | /* |
| 767 | * Indirect-input devices may not be siblings of direct-input |
| 768 | * devices. |
| 769 | */ |
| 770 | if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) && |
| 771 | (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT)) |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 772 | return false; |
| 773 | |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 774 | /* Pen devices may only be siblings of touch devices */ |
| 775 | if ((features->device_type & WACOM_DEVICETYPE_PEN) && |
| 776 | !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH)) |
| 777 | return false; |
| 778 | |
| 779 | /* Touch devices may only be siblings of pen devices */ |
| 780 | if ((features->device_type & WACOM_DEVICETYPE_TOUCH) && |
| 781 | !(sibling_features->device_type & WACOM_DEVICETYPE_PEN)) |
| 782 | return false; |
| 783 | |
| 784 | /* |
| 785 | * No reason could be found for these two devices to NOT be |
| 786 | * siblings, so there's a good chance they ARE siblings |
| 787 | */ |
| 788 | return true; |
Jason Gerecke | aea2bf6 | 2012-10-21 00:38:03 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 791 | static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev) |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 792 | { |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 793 | struct wacom_hdev_data *data; |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 794 | |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 795 | /* Try to find an already-probed interface from the same device */ |
| 796 | list_for_each_entry(data, &wacom_udev_list, list) { |
Jason Gerecke | 2a5e597 | 2017-09-18 09:27:42 -0700 | [diff] [blame] | 797 | if (compare_device_paths(hdev, data->dev, '/')) { |
| 798 | kref_get(&data->kref); |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 799 | return data; |
Jason Gerecke | 2a5e597 | 2017-09-18 09:27:42 -0700 | [diff] [blame] | 800 | } |
Jason Gerecke | 41372d5 | 2016-08-08 12:06:30 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* Fallback to finding devices that appear to be "siblings" */ |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 804 | list_for_each_entry(data, &wacom_udev_list, list) { |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 805 | if (wacom_are_sibling(hdev, data->dev)) { |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 806 | kref_get(&data->kref); |
| 807 | return data; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return NULL; |
| 812 | } |
| 813 | |
Benjamin Tissoires | 1c817c8 | 2016-07-13 18:05:59 +0200 | [diff] [blame] | 814 | static void wacom_release_shared_data(struct kref *kref) |
| 815 | { |
| 816 | struct wacom_hdev_data *data = |
| 817 | container_of(kref, struct wacom_hdev_data, kref); |
| 818 | |
| 819 | mutex_lock(&wacom_udev_list_lock); |
| 820 | list_del(&data->list); |
| 821 | mutex_unlock(&wacom_udev_list_lock); |
| 822 | |
| 823 | kfree(data); |
| 824 | } |
| 825 | |
| 826 | static void wacom_remove_shared_data(void *res) |
| 827 | { |
| 828 | struct wacom *wacom = res; |
| 829 | struct wacom_hdev_data *data; |
| 830 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
| 831 | |
| 832 | if (wacom_wac->shared) { |
| 833 | data = container_of(wacom_wac->shared, struct wacom_hdev_data, |
| 834 | shared); |
| 835 | |
| 836 | if (wacom_wac->shared->touch == wacom->hdev) |
| 837 | wacom_wac->shared->touch = NULL; |
| 838 | else if (wacom_wac->shared->pen == wacom->hdev) |
| 839 | wacom_wac->shared->pen = NULL; |
| 840 | |
| 841 | kref_put(&data->kref, wacom_release_shared_data); |
| 842 | wacom_wac->shared = NULL; |
| 843 | } |
| 844 | } |
| 845 | |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 846 | static int wacom_add_shared_data(struct hid_device *hdev) |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 847 | { |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 848 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 849 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
| 850 | struct wacom_hdev_data *data; |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 851 | int retval = 0; |
| 852 | |
| 853 | mutex_lock(&wacom_udev_list_lock); |
| 854 | |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 855 | data = wacom_get_hdev_data(hdev); |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 856 | if (!data) { |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 857 | data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL); |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 858 | if (!data) { |
| 859 | retval = -ENOMEM; |
| 860 | goto out; |
| 861 | } |
| 862 | |
| 863 | kref_init(&data->kref); |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 864 | data->dev = hdev; |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 865 | list_add_tail(&data->list, &wacom_udev_list); |
| 866 | } |
| 867 | |
Benjamin Tissoires | 4451e08 | 2014-07-24 13:01:05 -0700 | [diff] [blame] | 868 | wacom_wac->shared = &data->shared; |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 869 | |
Benjamin Tissoires | 1c817c8 | 2016-07-13 18:05:59 +0200 | [diff] [blame] | 870 | retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom); |
| 871 | if (retval) { |
| 872 | mutex_unlock(&wacom_udev_list_lock); |
| 873 | wacom_remove_shared_data(wacom); |
| 874 | return retval; |
| 875 | } |
| 876 | |
Jason Gerecke | a9ce785 | 2017-01-17 15:38:58 -0800 | [diff] [blame] | 877 | if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) |
| 878 | wacom_wac->shared->touch = hdev; |
| 879 | else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN) |
| 880 | wacom_wac->shared->pen = hdev; |
| 881 | |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 882 | out: |
| 883 | mutex_unlock(&wacom_udev_list_lock); |
| 884 | return retval; |
| 885 | } |
| 886 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 887 | static int wacom_led_control(struct wacom *wacom) |
| 888 | { |
| 889 | unsigned char *buf; |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 890 | int retval; |
Ping Cheng | 912ca21 | 2014-09-10 12:41:31 -0700 | [diff] [blame] | 891 | unsigned char report_id = WAC_CMD_LED_CONTROL; |
| 892 | int buf_size = 9; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 893 | |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 894 | if (!wacom->led.groups) |
| 895 | return -ENOTSUPP; |
| 896 | |
Aaron Armstrong Skomra | 74aebed6 | 2017-08-28 14:15:39 -0700 | [diff] [blame] | 897 | if (wacom->wacom_wac.features.type == REMOTE) |
| 898 | return -ENOTSUPP; |
| 899 | |
Ping Cheng | 912ca21 | 2014-09-10 12:41:31 -0700 | [diff] [blame] | 900 | if (wacom->wacom_wac.pid) { /* wireless connected */ |
| 901 | report_id = WAC_CMD_WL_LED_CONTROL; |
| 902 | buf_size = 13; |
| 903 | } |
Jason Gerecke | 4922cd2 | 2017-01-25 12:08:37 -0800 | [diff] [blame] | 904 | else if (wacom->wacom_wac.features.type == INTUOSP2_BT) { |
| 905 | report_id = WAC_CMD_WL_INTUOSP2; |
| 906 | buf_size = 51; |
| 907 | } |
Ping Cheng | 912ca21 | 2014-09-10 12:41:31 -0700 | [diff] [blame] | 908 | buf = kzalloc(buf_size, GFP_KERNEL); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 909 | if (!buf) |
| 910 | return -ENOMEM; |
| 911 | |
Aaron Armstrong Skomra | 10c55ca | 2017-01-25 12:08:42 -0800 | [diff] [blame] | 912 | if (wacom->wacom_wac.features.type == HID_GENERIC) { |
| 913 | buf[0] = WAC_CMD_LED_CONTROL_GENERIC; |
| 914 | buf[1] = wacom->led.llv; |
| 915 | buf[2] = wacom->led.groups[0].select & 0x03; |
| 916 | |
| 917 | } else if ((wacom->wacom_wac.features.type >= INTUOS5S && |
| 918 | wacom->wacom_wac.features.type <= INTUOSPL)) { |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 919 | /* |
| 920 | * Touch Ring and crop mark LED luminance may take on |
| 921 | * one of four values: |
| 922 | * 0 = Low; 1 = Medium; 2 = High; 3 = Off |
| 923 | */ |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 924 | int ring_led = wacom->led.groups[0].select & 0x03; |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 925 | int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03; |
| 926 | int crop_lum = 0; |
Ping Cheng | 912ca21 | 2014-09-10 12:41:31 -0700 | [diff] [blame] | 927 | unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led); |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 928 | |
Ping Cheng | 912ca21 | 2014-09-10 12:41:31 -0700 | [diff] [blame] | 929 | buf[0] = report_id; |
| 930 | if (wacom->wacom_wac.pid) { |
| 931 | wacom_get_report(wacom->hdev, HID_FEATURE_REPORT, |
| 932 | buf, buf_size, WAC_CMD_RETRIES); |
| 933 | buf[0] = report_id; |
| 934 | buf[4] = led_bits; |
| 935 | } else |
| 936 | buf[1] = led_bits; |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 937 | } |
Jason Gerecke | 4922cd2 | 2017-01-25 12:08:37 -0800 | [diff] [blame] | 938 | else if (wacom->wacom_wac.features.type == INTUOSP2_BT) { |
| 939 | buf[0] = report_id; |
| 940 | buf[4] = 100; // Power Connection LED (ORANGE) |
| 941 | buf[5] = 100; // BT Connection LED (BLUE) |
| 942 | buf[6] = 100; // Paper Mode (RED?) |
| 943 | buf[7] = 100; // Paper Mode (GREEN?) |
| 944 | buf[8] = 100; // Paper Mode (BLUE?) |
| 945 | buf[9] = wacom->led.llv; |
| 946 | buf[10] = wacom->led.groups[0].select & 0x03; |
| 947 | } |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 948 | else { |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 949 | int led = wacom->led.groups[0].select | 0x4; |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 950 | |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 951 | if (wacom->wacom_wac.features.type == WACOM_21UX2 || |
| 952 | wacom->wacom_wac.features.type == WACOM_24HD) |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 953 | led |= (wacom->led.groups[1].select << 4) | 0x40; |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 954 | |
Ping Cheng | 912ca21 | 2014-09-10 12:41:31 -0700 | [diff] [blame] | 955 | buf[0] = report_id; |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 956 | buf[1] = led; |
| 957 | buf[2] = wacom->led.llv; |
| 958 | buf[3] = wacom->led.hlv; |
| 959 | buf[4] = wacom->led.img_lum; |
| 960 | } |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 961 | |
Ping Cheng | 912ca21 | 2014-09-10 12:41:31 -0700 | [diff] [blame] | 962 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size, |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 963 | WAC_CMD_RETRIES); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 964 | kfree(buf); |
| 965 | |
| 966 | return retval; |
| 967 | } |
| 968 | |
Benjamin Tissoires | 849e2f0 | 2014-08-06 13:58:25 -0700 | [diff] [blame] | 969 | static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id, |
| 970 | const unsigned len, const void *img) |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 971 | { |
| 972 | unsigned char *buf; |
| 973 | int i, retval; |
Benjamin Tissoires | 849e2f0 | 2014-08-06 13:58:25 -0700 | [diff] [blame] | 974 | const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */ |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 975 | |
Benjamin Tissoires | 849e2f0 | 2014-08-06 13:58:25 -0700 | [diff] [blame] | 976 | buf = kzalloc(chunk_len + 3 , GFP_KERNEL); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 977 | if (!buf) |
| 978 | return -ENOMEM; |
| 979 | |
| 980 | /* Send 'start' command */ |
| 981 | buf[0] = WAC_CMD_ICON_START; |
| 982 | buf[1] = 1; |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 983 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2, |
| 984 | WAC_CMD_RETRIES); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 985 | if (retval < 0) |
| 986 | goto out; |
| 987 | |
Benjamin Tissoires | 849e2f0 | 2014-08-06 13:58:25 -0700 | [diff] [blame] | 988 | buf[0] = xfer_id; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 989 | buf[1] = button_id & 0x07; |
| 990 | for (i = 0; i < 4; i++) { |
| 991 | buf[2] = i; |
Benjamin Tissoires | 849e2f0 | 2014-08-06 13:58:25 -0700 | [diff] [blame] | 992 | memcpy(buf + 3, img + i * chunk_len, chunk_len); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 993 | |
Benjamin Tissoires | 27b20a9 | 2014-07-24 12:56:22 -0700 | [diff] [blame] | 994 | retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 995 | buf, chunk_len + 3, WAC_CMD_RETRIES); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 996 | if (retval < 0) |
| 997 | break; |
| 998 | } |
| 999 | |
| 1000 | /* Send 'stop' */ |
| 1001 | buf[0] = WAC_CMD_ICON_START; |
| 1002 | buf[1] = 0; |
Przemo Firszt | 296b737 | 2014-08-06 14:00:38 -0700 | [diff] [blame] | 1003 | wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2, |
| 1004 | WAC_CMD_RETRIES); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1005 | |
| 1006 | out: |
| 1007 | kfree(buf); |
| 1008 | return retval; |
| 1009 | } |
| 1010 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1011 | static ssize_t wacom_led_select_store(struct device *dev, int set_id, |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1012 | const char *buf, size_t count) |
| 1013 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 1014 | struct hid_device *hdev = to_hid_device(dev); |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 1015 | struct wacom *wacom = hid_get_drvdata(hdev); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1016 | unsigned int id; |
| 1017 | int err; |
| 1018 | |
| 1019 | err = kstrtouint(buf, 10, &id); |
| 1020 | if (err) |
| 1021 | return err; |
| 1022 | |
| 1023 | mutex_lock(&wacom->lock); |
| 1024 | |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1025 | wacom->led.groups[set_id].select = id & 0x3; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1026 | err = wacom_led_control(wacom); |
| 1027 | |
| 1028 | mutex_unlock(&wacom->lock); |
| 1029 | |
| 1030 | return err < 0 ? err : count; |
| 1031 | } |
| 1032 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1033 | #define DEVICE_LED_SELECT_ATTR(SET_ID) \ |
| 1034 | static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \ |
| 1035 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 1036 | { \ |
| 1037 | return wacom_led_select_store(dev, SET_ID, buf, count); \ |
| 1038 | } \ |
Ping Cheng | 04c59ab | 2011-10-04 23:51:49 -0700 | [diff] [blame] | 1039 | static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \ |
| 1040 | struct device_attribute *attr, char *buf) \ |
| 1041 | { \ |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 1042 | struct hid_device *hdev = to_hid_device(dev);\ |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 1043 | struct wacom *wacom = hid_get_drvdata(hdev); \ |
Ping Cheng | 37449ad | 2014-09-10 12:40:30 -0700 | [diff] [blame] | 1044 | return scnprintf(buf, PAGE_SIZE, "%d\n", \ |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1045 | wacom->led.groups[SET_ID].select); \ |
Ping Cheng | 04c59ab | 2011-10-04 23:51:49 -0700 | [diff] [blame] | 1046 | } \ |
Ping Cheng | e0984bc | 2014-09-10 12:40:05 -0700 | [diff] [blame] | 1047 | static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \ |
Ping Cheng | 04c59ab | 2011-10-04 23:51:49 -0700 | [diff] [blame] | 1048 | wacom_led##SET_ID##_select_show, \ |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1049 | wacom_led##SET_ID##_select_store) |
| 1050 | |
| 1051 | DEVICE_LED_SELECT_ATTR(0); |
| 1052 | DEVICE_LED_SELECT_ATTR(1); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1053 | |
| 1054 | static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest, |
| 1055 | const char *buf, size_t count) |
| 1056 | { |
| 1057 | unsigned int value; |
| 1058 | int err; |
| 1059 | |
| 1060 | err = kstrtouint(buf, 10, &value); |
| 1061 | if (err) |
| 1062 | return err; |
| 1063 | |
| 1064 | mutex_lock(&wacom->lock); |
| 1065 | |
| 1066 | *dest = value & 0x7f; |
| 1067 | err = wacom_led_control(wacom); |
| 1068 | |
| 1069 | mutex_unlock(&wacom->lock); |
| 1070 | |
| 1071 | return err < 0 ? err : count; |
| 1072 | } |
| 1073 | |
| 1074 | #define DEVICE_LUMINANCE_ATTR(name, field) \ |
| 1075 | static ssize_t wacom_##name##_luminance_store(struct device *dev, \ |
| 1076 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 1077 | { \ |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 1078 | struct hid_device *hdev = to_hid_device(dev);\ |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 1079 | struct wacom *wacom = hid_get_drvdata(hdev); \ |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1080 | \ |
| 1081 | return wacom_luminance_store(wacom, &wacom->led.field, \ |
| 1082 | buf, count); \ |
| 1083 | } \ |
Ping Cheng | 37449ad | 2014-09-10 12:40:30 -0700 | [diff] [blame] | 1084 | static ssize_t wacom_##name##_luminance_show(struct device *dev, \ |
| 1085 | struct device_attribute *attr, char *buf) \ |
| 1086 | { \ |
| 1087 | struct wacom *wacom = dev_get_drvdata(dev); \ |
| 1088 | return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \ |
| 1089 | } \ |
Ping Cheng | e0984bc | 2014-09-10 12:40:05 -0700 | [diff] [blame] | 1090 | static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \ |
Ping Cheng | 37449ad | 2014-09-10 12:40:30 -0700 | [diff] [blame] | 1091 | wacom_##name##_luminance_show, \ |
| 1092 | wacom_##name##_luminance_store) |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1093 | |
| 1094 | DEVICE_LUMINANCE_ATTR(status0, llv); |
| 1095 | DEVICE_LUMINANCE_ATTR(status1, hlv); |
| 1096 | DEVICE_LUMINANCE_ATTR(buttons, img_lum); |
| 1097 | |
| 1098 | static ssize_t wacom_button_image_store(struct device *dev, int button_id, |
| 1099 | const char *buf, size_t count) |
| 1100 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 1101 | struct hid_device *hdev = to_hid_device(dev); |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 1102 | struct wacom *wacom = hid_get_drvdata(hdev); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1103 | int err; |
Benjamin Tissoires | 849e2f0 | 2014-08-06 13:58:25 -0700 | [diff] [blame] | 1104 | unsigned len; |
| 1105 | u8 xfer_id; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1106 | |
Benjamin Tissoires | 849e2f0 | 2014-08-06 13:58:25 -0700 | [diff] [blame] | 1107 | if (hdev->bus == BUS_BLUETOOTH) { |
| 1108 | len = 256; |
| 1109 | xfer_id = WAC_CMD_ICON_BT_XFER; |
| 1110 | } else { |
| 1111 | len = 1024; |
| 1112 | xfer_id = WAC_CMD_ICON_XFER; |
| 1113 | } |
| 1114 | |
| 1115 | if (count != len) |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1116 | return -EINVAL; |
| 1117 | |
| 1118 | mutex_lock(&wacom->lock); |
| 1119 | |
Benjamin Tissoires | 849e2f0 | 2014-08-06 13:58:25 -0700 | [diff] [blame] | 1120 | err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf); |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1121 | |
| 1122 | mutex_unlock(&wacom->lock); |
| 1123 | |
| 1124 | return err < 0 ? err : count; |
| 1125 | } |
| 1126 | |
| 1127 | #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \ |
| 1128 | static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \ |
| 1129 | struct device_attribute *attr, const char *buf, size_t count) \ |
| 1130 | { \ |
| 1131 | return wacom_button_image_store(dev, BUTTON_ID, buf, count); \ |
| 1132 | } \ |
Ping Cheng | e0984bc | 2014-09-10 12:40:05 -0700 | [diff] [blame] | 1133 | static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \ |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1134 | NULL, wacom_btnimg##BUTTON_ID##_store) |
| 1135 | |
| 1136 | DEVICE_BTNIMG_ATTR(0); |
| 1137 | DEVICE_BTNIMG_ATTR(1); |
| 1138 | DEVICE_BTNIMG_ATTR(2); |
| 1139 | DEVICE_BTNIMG_ATTR(3); |
| 1140 | DEVICE_BTNIMG_ATTR(4); |
| 1141 | DEVICE_BTNIMG_ATTR(5); |
| 1142 | DEVICE_BTNIMG_ATTR(6); |
| 1143 | DEVICE_BTNIMG_ATTR(7); |
| 1144 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1145 | static struct attribute *cintiq_led_attrs[] = { |
| 1146 | &dev_attr_status_led0_select.attr, |
| 1147 | &dev_attr_status_led1_select.attr, |
| 1148 | NULL |
| 1149 | }; |
| 1150 | |
| 1151 | static struct attribute_group cintiq_led_attr_group = { |
| 1152 | .name = "wacom_led", |
| 1153 | .attrs = cintiq_led_attrs, |
| 1154 | }; |
| 1155 | |
| 1156 | static struct attribute *intuos4_led_attrs[] = { |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1157 | &dev_attr_status0_luminance.attr, |
| 1158 | &dev_attr_status1_luminance.attr, |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1159 | &dev_attr_status_led0_select.attr, |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1160 | &dev_attr_buttons_luminance.attr, |
| 1161 | &dev_attr_button0_rawimg.attr, |
| 1162 | &dev_attr_button1_rawimg.attr, |
| 1163 | &dev_attr_button2_rawimg.attr, |
| 1164 | &dev_attr_button3_rawimg.attr, |
| 1165 | &dev_attr_button4_rawimg.attr, |
| 1166 | &dev_attr_button5_rawimg.attr, |
| 1167 | &dev_attr_button6_rawimg.attr, |
| 1168 | &dev_attr_button7_rawimg.attr, |
| 1169 | NULL |
| 1170 | }; |
| 1171 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1172 | static struct attribute_group intuos4_led_attr_group = { |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1173 | .name = "wacom_led", |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1174 | .attrs = intuos4_led_attrs, |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1175 | }; |
| 1176 | |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 1177 | static struct attribute *intuos5_led_attrs[] = { |
| 1178 | &dev_attr_status0_luminance.attr, |
| 1179 | &dev_attr_status_led0_select.attr, |
| 1180 | NULL |
| 1181 | }; |
| 1182 | |
| 1183 | static struct attribute_group intuos5_led_attr_group = { |
| 1184 | .name = "wacom_led", |
| 1185 | .attrs = intuos5_led_attrs, |
| 1186 | }; |
| 1187 | |
Aaron Armstrong Skomra | 10c55ca | 2017-01-25 12:08:42 -0800 | [diff] [blame] | 1188 | static struct attribute *generic_led_attrs[] = { |
| 1189 | &dev_attr_status0_luminance.attr, |
| 1190 | &dev_attr_status_led0_select.attr, |
| 1191 | NULL |
| 1192 | }; |
| 1193 | |
| 1194 | static struct attribute_group generic_led_attr_group = { |
| 1195 | .name = "wacom_led", |
| 1196 | .attrs = generic_led_attrs, |
| 1197 | }; |
| 1198 | |
Benjamin Tissoires | 2df68a8 | 2016-07-13 18:05:56 +0200 | [diff] [blame] | 1199 | struct wacom_sysfs_group_devres { |
| 1200 | struct attribute_group *group; |
| 1201 | struct kobject *root; |
| 1202 | }; |
| 1203 | |
| 1204 | static void wacom_devm_sysfs_group_release(struct device *dev, void *res) |
| 1205 | { |
| 1206 | struct wacom_sysfs_group_devres *devres = res; |
| 1207 | struct kobject *kobj = devres->root; |
| 1208 | |
| 1209 | dev_dbg(dev, "%s: dropping reference to %s\n", |
| 1210 | __func__, devres->group->name); |
| 1211 | sysfs_remove_group(kobj, devres->group); |
| 1212 | } |
| 1213 | |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 1214 | static int __wacom_devm_sysfs_create_group(struct wacom *wacom, |
| 1215 | struct kobject *root, |
| 1216 | struct attribute_group *group) |
Benjamin Tissoires | 2df68a8 | 2016-07-13 18:05:56 +0200 | [diff] [blame] | 1217 | { |
| 1218 | struct wacom_sysfs_group_devres *devres; |
| 1219 | int error; |
| 1220 | |
| 1221 | devres = devres_alloc(wacom_devm_sysfs_group_release, |
| 1222 | sizeof(struct wacom_sysfs_group_devres), |
| 1223 | GFP_KERNEL); |
| 1224 | if (!devres) |
| 1225 | return -ENOMEM; |
| 1226 | |
| 1227 | devres->group = group; |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 1228 | devres->root = root; |
Benjamin Tissoires | 2df68a8 | 2016-07-13 18:05:56 +0200 | [diff] [blame] | 1229 | |
| 1230 | error = sysfs_create_group(devres->root, group); |
Arvind Yadav | 097b8f6 | 2018-04-24 13:33:03 +0530 | [diff] [blame] | 1231 | if (error) { |
| 1232 | devres_free(devres); |
Benjamin Tissoires | 2df68a8 | 2016-07-13 18:05:56 +0200 | [diff] [blame] | 1233 | return error; |
Arvind Yadav | 097b8f6 | 2018-04-24 13:33:03 +0530 | [diff] [blame] | 1234 | } |
Benjamin Tissoires | 2df68a8 | 2016-07-13 18:05:56 +0200 | [diff] [blame] | 1235 | |
| 1236 | devres_add(&wacom->hdev->dev, devres); |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 1241 | static int wacom_devm_sysfs_create_group(struct wacom *wacom, |
| 1242 | struct attribute_group *group) |
| 1243 | { |
| 1244 | return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj, |
| 1245 | group); |
| 1246 | } |
| 1247 | |
Benjamin Tissoires | 34736aa | 2016-07-13 18:06:12 +0200 | [diff] [blame] | 1248 | enum led_brightness wacom_leds_brightness_get(struct wacom_led *led) |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1249 | { |
| 1250 | struct wacom *wacom = led->wacom; |
| 1251 | |
| 1252 | if (wacom->led.max_hlv) |
| 1253 | return led->hlv * LED_FULL / wacom->led.max_hlv; |
| 1254 | |
| 1255 | if (wacom->led.max_llv) |
| 1256 | return led->llv * LED_FULL / wacom->led.max_llv; |
| 1257 | |
| 1258 | /* device doesn't support brightness tuning */ |
| 1259 | return LED_FULL; |
| 1260 | } |
| 1261 | |
| 1262 | static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev) |
| 1263 | { |
| 1264 | struct wacom_led *led = container_of(cdev, struct wacom_led, cdev); |
| 1265 | struct wacom *wacom = led->wacom; |
| 1266 | |
| 1267 | if (wacom->led.groups[led->group].select != led->id) |
| 1268 | return LED_OFF; |
| 1269 | |
| 1270 | return wacom_leds_brightness_get(led); |
| 1271 | } |
| 1272 | |
| 1273 | static int wacom_led_brightness_set(struct led_classdev *cdev, |
| 1274 | enum led_brightness brightness) |
| 1275 | { |
| 1276 | struct wacom_led *led = container_of(cdev, struct wacom_led, cdev); |
| 1277 | struct wacom *wacom = led->wacom; |
| 1278 | int error; |
| 1279 | |
| 1280 | mutex_lock(&wacom->lock); |
| 1281 | |
| 1282 | if (!wacom->led.groups || (brightness == LED_OFF && |
| 1283 | wacom->led.groups[led->group].select != led->id)) { |
| 1284 | error = 0; |
| 1285 | goto out; |
| 1286 | } |
| 1287 | |
| 1288 | led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL; |
| 1289 | led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL; |
| 1290 | |
| 1291 | wacom->led.groups[led->group].select = led->id; |
| 1292 | |
| 1293 | error = wacom_led_control(wacom); |
| 1294 | |
| 1295 | out: |
| 1296 | mutex_unlock(&wacom->lock); |
| 1297 | |
| 1298 | return error; |
| 1299 | } |
| 1300 | |
| 1301 | static void wacom_led_readonly_brightness_set(struct led_classdev *cdev, |
| 1302 | enum led_brightness brightness) |
| 1303 | { |
| 1304 | } |
| 1305 | |
| 1306 | static int wacom_led_register_one(struct device *dev, struct wacom *wacom, |
| 1307 | struct wacom_led *led, unsigned int group, |
| 1308 | unsigned int id, bool read_only) |
| 1309 | { |
| 1310 | int error; |
| 1311 | char *name; |
| 1312 | |
| 1313 | name = devm_kasprintf(dev, GFP_KERNEL, |
| 1314 | "%s::wacom-%d.%d", |
| 1315 | dev_name(dev), |
| 1316 | group, |
| 1317 | id); |
| 1318 | if (!name) |
| 1319 | return -ENOMEM; |
| 1320 | |
Benjamin Tissoires | 34736aa | 2016-07-13 18:06:12 +0200 | [diff] [blame] | 1321 | if (!read_only) { |
| 1322 | led->trigger.name = name; |
| 1323 | error = devm_led_trigger_register(dev, &led->trigger); |
| 1324 | if (error) { |
| 1325 | hid_err(wacom->hdev, |
| 1326 | "failed to register LED trigger %s: %d\n", |
| 1327 | led->cdev.name, error); |
| 1328 | return error; |
| 1329 | } |
| 1330 | } |
| 1331 | |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1332 | led->group = group; |
| 1333 | led->id = id; |
| 1334 | led->wacom = wacom; |
| 1335 | led->llv = wacom->led.llv; |
| 1336 | led->hlv = wacom->led.hlv; |
| 1337 | led->cdev.name = name; |
| 1338 | led->cdev.max_brightness = LED_FULL; |
| 1339 | led->cdev.flags = LED_HW_PLUGGABLE; |
| 1340 | led->cdev.brightness_get = __wacom_led_brightness_get; |
Benjamin Tissoires | 34736aa | 2016-07-13 18:06:12 +0200 | [diff] [blame] | 1341 | if (!read_only) { |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1342 | led->cdev.brightness_set_blocking = wacom_led_brightness_set; |
Benjamin Tissoires | 34736aa | 2016-07-13 18:06:12 +0200 | [diff] [blame] | 1343 | led->cdev.default_trigger = led->cdev.name; |
| 1344 | } else { |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1345 | led->cdev.brightness_set = wacom_led_readonly_brightness_set; |
Benjamin Tissoires | 34736aa | 2016-07-13 18:06:12 +0200 | [diff] [blame] | 1346 | } |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1347 | |
| 1348 | error = devm_led_classdev_register(dev, &led->cdev); |
| 1349 | if (error) { |
| 1350 | hid_err(wacom->hdev, |
| 1351 | "failed to register LED %s: %d\n", |
| 1352 | led->cdev.name, error); |
| 1353 | led->cdev.name = NULL; |
| 1354 | return error; |
| 1355 | } |
| 1356 | |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
Benjamin Tissoires | 589e506 | 2016-07-13 18:06:11 +0200 | [diff] [blame] | 1360 | static void wacom_led_groups_release_one(void *data) |
| 1361 | { |
| 1362 | struct wacom_group_leds *group = data; |
| 1363 | |
| 1364 | devres_release_group(group->dev, group); |
| 1365 | } |
| 1366 | |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1367 | static int wacom_led_groups_alloc_and_register_one(struct device *dev, |
| 1368 | struct wacom *wacom, |
| 1369 | int group_id, int count, |
| 1370 | bool read_only) |
| 1371 | { |
| 1372 | struct wacom_led *leds; |
| 1373 | int i, error; |
| 1374 | |
| 1375 | if (group_id >= wacom->led.count || count <= 0) |
| 1376 | return -EINVAL; |
| 1377 | |
| 1378 | if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL)) |
| 1379 | return -ENOMEM; |
| 1380 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 1381 | leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL); |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1382 | if (!leds) { |
| 1383 | error = -ENOMEM; |
| 1384 | goto err; |
| 1385 | } |
| 1386 | |
| 1387 | wacom->led.groups[group_id].leds = leds; |
| 1388 | wacom->led.groups[group_id].count = count; |
| 1389 | |
| 1390 | for (i = 0; i < count; i++) { |
| 1391 | error = wacom_led_register_one(dev, wacom, &leds[i], |
| 1392 | group_id, i, read_only); |
| 1393 | if (error) |
| 1394 | goto err; |
| 1395 | } |
| 1396 | |
Benjamin Tissoires | 589e506 | 2016-07-13 18:06:11 +0200 | [diff] [blame] | 1397 | wacom->led.groups[group_id].dev = dev; |
| 1398 | |
| 1399 | devres_close_group(dev, &wacom->led.groups[group_id]); |
| 1400 | |
| 1401 | /* |
| 1402 | * There is a bug (?) in devm_led_classdev_register() in which its |
| 1403 | * increments the refcount of the parent. If the parent is an input |
| 1404 | * device, that means the ref count never reaches 0 when |
| 1405 | * devm_input_device_release() gets called. |
| 1406 | * This means that the LEDs are still there after disconnect. |
| 1407 | * Manually force the release of the group so that the leds are released |
| 1408 | * once we are done using them. |
| 1409 | */ |
| 1410 | error = devm_add_action_or_reset(&wacom->hdev->dev, |
| 1411 | wacom_led_groups_release_one, |
| 1412 | &wacom->led.groups[group_id]); |
| 1413 | if (error) |
| 1414 | return error; |
| 1415 | |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1416 | return 0; |
| 1417 | |
| 1418 | err: |
| 1419 | devres_release_group(dev, &wacom->led.groups[group_id]); |
| 1420 | return error; |
| 1421 | } |
| 1422 | |
Benjamin Tissoires | 34736aa | 2016-07-13 18:06:12 +0200 | [diff] [blame] | 1423 | struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id, |
| 1424 | unsigned int id) |
| 1425 | { |
| 1426 | struct wacom_group_leds *group; |
| 1427 | |
| 1428 | if (group_id >= wacom->led.count) |
| 1429 | return NULL; |
| 1430 | |
| 1431 | group = &wacom->led.groups[group_id]; |
| 1432 | |
| 1433 | if (!group->leds) |
| 1434 | return NULL; |
| 1435 | |
| 1436 | id %= group->count; |
| 1437 | |
| 1438 | return &group->leds[id]; |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * wacom_led_next: gives the next available led with a wacom trigger. |
| 1443 | * |
| 1444 | * returns the next available struct wacom_led which has its default trigger |
| 1445 | * or the current one if none is available. |
| 1446 | */ |
| 1447 | struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur) |
| 1448 | { |
| 1449 | struct wacom_led *next_led; |
| 1450 | int group, next; |
| 1451 | |
| 1452 | if (!wacom || !cur) |
| 1453 | return NULL; |
| 1454 | |
| 1455 | group = cur->group; |
| 1456 | next = cur->id; |
| 1457 | |
| 1458 | do { |
| 1459 | next_led = wacom_led_find(wacom, group, ++next); |
| 1460 | if (!next_led || next_led == cur) |
| 1461 | return next_led; |
| 1462 | } while (next_led->cdev.trigger != &next_led->trigger); |
| 1463 | |
| 1464 | return next_led; |
| 1465 | } |
| 1466 | |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1467 | static void wacom_led_groups_release(void *data) |
| 1468 | { |
| 1469 | struct wacom *wacom = data; |
| 1470 | |
| 1471 | wacom->led.groups = NULL; |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1472 | wacom->led.count = 0; |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | static int wacom_led_groups_allocate(struct wacom *wacom, int count) |
| 1476 | { |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1477 | struct device *dev = &wacom->hdev->dev; |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1478 | struct wacom_group_leds *groups; |
| 1479 | int error; |
| 1480 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 1481 | groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds), |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1482 | GFP_KERNEL); |
| 1483 | if (!groups) |
| 1484 | return -ENOMEM; |
| 1485 | |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1486 | error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom); |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1487 | if (error) |
| 1488 | return error; |
| 1489 | |
| 1490 | wacom->led.groups = groups; |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1491 | wacom->led.count = count; |
| 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
| 1496 | static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count, |
| 1497 | int led_per_group, bool read_only) |
| 1498 | { |
| 1499 | struct device *dev; |
| 1500 | int i, error; |
| 1501 | |
| 1502 | if (!wacom->wacom_wac.pad_input) |
| 1503 | return -EINVAL; |
| 1504 | |
| 1505 | dev = &wacom->wacom_wac.pad_input->dev; |
| 1506 | |
| 1507 | error = wacom_led_groups_allocate(wacom, group_count); |
| 1508 | if (error) |
| 1509 | return error; |
| 1510 | |
| 1511 | for (i = 0; i < group_count; i++) { |
| 1512 | error = wacom_led_groups_alloc_and_register_one(dev, wacom, i, |
| 1513 | led_per_group, |
| 1514 | read_only); |
| 1515 | if (error) |
| 1516 | return error; |
| 1517 | } |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1518 | |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
Aaron Armstrong Skomra | 10c55ca | 2017-01-25 12:08:42 -0800 | [diff] [blame] | 1522 | int wacom_initialize_leds(struct wacom *wacom) |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1523 | { |
| 1524 | int error; |
| 1525 | |
Jason Gerecke | 862cf55 | 2015-06-15 18:01:43 -0700 | [diff] [blame] | 1526 | if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD)) |
| 1527 | return 0; |
| 1528 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1529 | /* Initialize default values */ |
| 1530 | switch (wacom->wacom_wac.features.type) { |
Aaron Armstrong Skomra | 10c55ca | 2017-01-25 12:08:42 -0800 | [diff] [blame] | 1531 | case HID_GENERIC: |
| 1532 | if (!wacom->generic_has_leds) |
| 1533 | return 0; |
| 1534 | wacom->led.llv = 100; |
| 1535 | wacom->led.max_llv = 100; |
| 1536 | |
| 1537 | error = wacom_leds_alloc_and_register(wacom, 1, 4, false); |
| 1538 | if (error) { |
| 1539 | hid_err(wacom->hdev, |
| 1540 | "cannot create leds err: %d\n", error); |
| 1541 | return error; |
| 1542 | } |
| 1543 | |
| 1544 | error = wacom_devm_sysfs_create_group(wacom, |
| 1545 | &generic_led_attr_group); |
| 1546 | break; |
| 1547 | |
Jason Gerecke | a19fc98 | 2012-06-12 00:27:53 -0700 | [diff] [blame] | 1548 | case INTUOS4S: |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1549 | case INTUOS4: |
Benjamin Tissoires | 81af7e6 | 2014-08-06 13:55:56 -0700 | [diff] [blame] | 1550 | case INTUOS4WL: |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1551 | case INTUOS4L: |
Ping Cheng | f4fa9a6 | 2011-10-04 23:49:42 -0700 | [diff] [blame] | 1552 | wacom->led.llv = 10; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1553 | wacom->led.hlv = 20; |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1554 | wacom->led.max_llv = 127; |
| 1555 | wacom->led.max_hlv = 127; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1556 | wacom->led.img_lum = 10; |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1557 | |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1558 | error = wacom_leds_alloc_and_register(wacom, 1, 4, false); |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1559 | if (error) { |
| 1560 | hid_err(wacom->hdev, |
| 1561 | "cannot create leds err: %d\n", error); |
| 1562 | return error; |
| 1563 | } |
| 1564 | |
Benjamin Tissoires | 2df68a8 | 2016-07-13 18:05:56 +0200 | [diff] [blame] | 1565 | error = wacom_devm_sysfs_create_group(wacom, |
| 1566 | &intuos4_led_attr_group); |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1567 | break; |
| 1568 | |
Jason Gerecke | 246835f | 2011-12-12 00:12:04 -0800 | [diff] [blame] | 1569 | case WACOM_24HD: |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1570 | case WACOM_21UX2: |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1571 | wacom->led.llv = 0; |
| 1572 | wacom->led.hlv = 0; |
| 1573 | wacom->led.img_lum = 0; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1574 | |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1575 | error = wacom_leds_alloc_and_register(wacom, 2, 4, false); |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1576 | if (error) { |
| 1577 | hid_err(wacom->hdev, |
| 1578 | "cannot create leds err: %d\n", error); |
| 1579 | return error; |
| 1580 | } |
| 1581 | |
Benjamin Tissoires | 2df68a8 | 2016-07-13 18:05:56 +0200 | [diff] [blame] | 1582 | error = wacom_devm_sysfs_create_group(wacom, |
| 1583 | &cintiq_led_attr_group); |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1584 | break; |
| 1585 | |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 1586 | case INTUOS5S: |
| 1587 | case INTUOS5: |
| 1588 | case INTUOS5L: |
Ping Cheng | 9a35c41 | 2013-09-20 09:51:56 -0700 | [diff] [blame] | 1589 | case INTUOSPS: |
| 1590 | case INTUOSPM: |
| 1591 | case INTUOSPL: |
Jason Gerecke | 862cf55 | 2015-06-15 18:01:43 -0700 | [diff] [blame] | 1592 | wacom->led.llv = 32; |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1593 | wacom->led.max_llv = 96; |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 1594 | |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1595 | error = wacom_leds_alloc_and_register(wacom, 1, 4, false); |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1596 | if (error) { |
| 1597 | hid_err(wacom->hdev, |
| 1598 | "cannot create leds err: %d\n", error); |
| 1599 | return error; |
| 1600 | } |
| 1601 | |
Benjamin Tissoires | 2df68a8 | 2016-07-13 18:05:56 +0200 | [diff] [blame] | 1602 | error = wacom_devm_sysfs_create_group(wacom, |
| 1603 | &intuos5_led_attr_group); |
Jason Gerecke | 9b5b95d | 2012-04-03 15:50:37 -0700 | [diff] [blame] | 1604 | break; |
| 1605 | |
Jason Gerecke | 4922cd2 | 2017-01-25 12:08:37 -0800 | [diff] [blame] | 1606 | case INTUOSP2_BT: |
| 1607 | wacom->led.llv = 50; |
| 1608 | wacom->led.max_llv = 100; |
| 1609 | error = wacom_leds_alloc_and_register(wacom, 1, 4, false); |
| 1610 | if (error) { |
| 1611 | hid_err(wacom->hdev, |
| 1612 | "cannot create leds err: %d\n", error); |
| 1613 | return error; |
| 1614 | } |
| 1615 | return 0; |
| 1616 | |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1617 | case REMOTE: |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 1618 | wacom->led.llv = 255; |
| 1619 | wacom->led.max_llv = 255; |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1620 | error = wacom_led_groups_allocate(wacom, 5); |
| 1621 | if (error) { |
| 1622 | hid_err(wacom->hdev, |
| 1623 | "cannot create leds err: %d\n", error); |
| 1624 | return error; |
| 1625 | } |
| 1626 | return 0; |
| 1627 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1628 | default: |
| 1629 | return 0; |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1632 | if (error) { |
Benjamin Tissoires | c31a408 | 2014-07-24 12:59:45 -0700 | [diff] [blame] | 1633 | hid_err(wacom->hdev, |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1634 | "cannot create sysfs group err: %d\n", error); |
| 1635 | return error; |
| 1636 | } |
Ping Cheng | 09e7d94 | 2011-10-04 23:51:14 -0700 | [diff] [blame] | 1637 | |
Eduard Hasenleithner | 5d7e7d4 | 2011-09-07 14:08:54 -0700 | [diff] [blame] | 1638 | return 0; |
| 1639 | } |
| 1640 | |
Benjamin Tissoires | a544c61 | 2017-01-20 16:20:13 +0100 | [diff] [blame] | 1641 | static void wacom_init_work(struct work_struct *work) |
| 1642 | { |
| 1643 | struct wacom *wacom = container_of(work, struct wacom, init_work.work); |
| 1644 | |
| 1645 | _wacom_query_tablet_data(wacom); |
| 1646 | wacom_led_control(wacom); |
| 1647 | } |
| 1648 | |
| 1649 | static void wacom_query_tablet_data(struct wacom *wacom) |
| 1650 | { |
| 1651 | schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000)); |
| 1652 | } |
| 1653 | |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1654 | static enum power_supply_property wacom_battery_props[] = { |
Benjamin Tissoires | 9956953 | 2016-07-13 18:06:17 +0200 | [diff] [blame] | 1655 | POWER_SUPPLY_PROP_MODEL_NAME, |
Jason Gerecke | 71fa641 | 2015-03-11 10:25:41 -0700 | [diff] [blame] | 1656 | POWER_SUPPLY_PROP_PRESENT, |
Benjamin Tissoires | ac8d101 | 2014-07-25 17:29:48 -0700 | [diff] [blame] | 1657 | POWER_SUPPLY_PROP_STATUS, |
Bastien Nocera | 6e2a6e8 | 2013-10-15 23:33:00 -0700 | [diff] [blame] | 1658 | POWER_SUPPLY_PROP_SCOPE, |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1659 | POWER_SUPPLY_PROP_CAPACITY |
| 1660 | }; |
| 1661 | |
| 1662 | static int wacom_battery_get_property(struct power_supply *psy, |
| 1663 | enum power_supply_property psp, |
| 1664 | union power_supply_propval *val) |
| 1665 | { |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1666 | struct wacom_battery *battery = power_supply_get_drvdata(psy); |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1667 | int ret = 0; |
| 1668 | |
| 1669 | switch (psp) { |
Benjamin Tissoires | 9956953 | 2016-07-13 18:06:17 +0200 | [diff] [blame] | 1670 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 1671 | val->strval = battery->wacom->wacom_wac.name; |
| 1672 | break; |
Jason Gerecke | 71fa641 | 2015-03-11 10:25:41 -0700 | [diff] [blame] | 1673 | case POWER_SUPPLY_PROP_PRESENT: |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1674 | val->intval = battery->bat_connected; |
Jason Gerecke | 71fa641 | 2015-03-11 10:25:41 -0700 | [diff] [blame] | 1675 | break; |
Bastien Nocera | 6e2a6e8 | 2013-10-15 23:33:00 -0700 | [diff] [blame] | 1676 | case POWER_SUPPLY_PROP_SCOPE: |
| 1677 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; |
| 1678 | break; |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1679 | case POWER_SUPPLY_PROP_CAPACITY: |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1680 | val->intval = battery->battery_capacity; |
Benjamin Tissoires | ac8d101 | 2014-07-25 17:29:48 -0700 | [diff] [blame] | 1681 | break; |
| 1682 | case POWER_SUPPLY_PROP_STATUS: |
Jason Gerecke | 16e4598 | 2017-04-28 09:25:33 -0700 | [diff] [blame] | 1683 | if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO) |
| 1684 | val->intval = battery->bat_status; |
| 1685 | else if (battery->bat_charging) |
Benjamin Tissoires | ac8d101 | 2014-07-25 17:29:48 -0700 | [diff] [blame] | 1686 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1687 | else if (battery->battery_capacity == 100 && |
| 1688 | battery->ps_connected) |
Benjamin Tissoires | ac8d101 | 2014-07-25 17:29:48 -0700 | [diff] [blame] | 1689 | val->intval = POWER_SUPPLY_STATUS_FULL; |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1690 | else if (battery->ps_connected) |
Jason Gerecke | b0882cb | 2015-03-06 11:47:43 -0800 | [diff] [blame] | 1691 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Benjamin Tissoires | ac8d101 | 2014-07-25 17:29:48 -0700 | [diff] [blame] | 1692 | else |
| 1693 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1694 | break; |
| 1695 | default: |
| 1696 | ret = -EINVAL; |
| 1697 | break; |
| 1698 | } |
| 1699 | |
| 1700 | return ret; |
| 1701 | } |
| 1702 | |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1703 | static int __wacom_initialize_battery(struct wacom *wacom, |
| 1704 | struct wacom_battery *battery) |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1705 | { |
Benjamin Tissoires | d70420b9 | 2014-07-25 17:31:51 -0700 | [diff] [blame] | 1706 | static atomic_t battery_no = ATOMIC_INIT(0); |
Benjamin Tissoires | b189da9 | 2016-07-13 18:05:53 +0200 | [diff] [blame] | 1707 | struct device *dev = &wacom->hdev->dev; |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1708 | struct power_supply_config psy_cfg = { .drv_data = battery, }; |
Benjamin Tissoires | 136ae5e | 2016-07-13 18:06:16 +0200 | [diff] [blame] | 1709 | struct power_supply *ps_bat; |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1710 | struct power_supply_desc *bat_desc = &battery->bat_desc; |
Benjamin Tissoires | d70420b9 | 2014-07-25 17:31:51 -0700 | [diff] [blame] | 1711 | unsigned long n; |
Benjamin Tissoires | b189da9 | 2016-07-13 18:05:53 +0200 | [diff] [blame] | 1712 | int error; |
| 1713 | |
| 1714 | if (!devres_open_group(dev, bat_desc, GFP_KERNEL)) |
| 1715 | return -ENOMEM; |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1716 | |
Benjamin Tissoires | 9956953 | 2016-07-13 18:06:17 +0200 | [diff] [blame] | 1717 | battery->wacom = wacom; |
| 1718 | |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1719 | n = atomic_inc_return(&battery_no) - 1; |
Benjamin Tissoires | 7dbd229 | 2014-07-25 17:32:41 -0700 | [diff] [blame] | 1720 | |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1721 | bat_desc->properties = wacom_battery_props; |
| 1722 | bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props); |
| 1723 | bat_desc->get_property = wacom_battery_get_property; |
| 1724 | sprintf(battery->bat_name, "wacom_battery_%ld", n); |
| 1725 | bat_desc->name = battery->bat_name; |
Benjamin Tissoires | 9698329 | 2016-07-13 18:06:15 +0200 | [diff] [blame] | 1726 | bat_desc->type = POWER_SUPPLY_TYPE_USB; |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1727 | bat_desc->use_for_apm = 0; |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1728 | |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1729 | ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg); |
| 1730 | if (IS_ERR(ps_bat)) { |
| 1731 | error = PTR_ERR(ps_bat); |
| 1732 | goto err; |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1735 | power_supply_powers(ps_bat, &wacom->hdev->dev); |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1736 | |
| 1737 | battery->battery = ps_bat; |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1738 | |
Benjamin Tissoires | b189da9 | 2016-07-13 18:05:53 +0200 | [diff] [blame] | 1739 | devres_close_group(dev, bat_desc); |
Benjamin Tissoires | 7dbd229 | 2014-07-25 17:32:41 -0700 | [diff] [blame] | 1740 | return 0; |
Benjamin Tissoires | b189da9 | 2016-07-13 18:05:53 +0200 | [diff] [blame] | 1741 | |
| 1742 | err: |
| 1743 | devres_release_group(dev, bat_desc); |
| 1744 | return error; |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1747 | static int wacom_initialize_battery(struct wacom *wacom) |
| 1748 | { |
| 1749 | if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) |
| 1750 | return __wacom_initialize_battery(wacom, &wacom->battery); |
| 1751 | |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1755 | static void wacom_destroy_battery(struct wacom *wacom) |
| 1756 | { |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 1757 | if (wacom->battery.battery) { |
| 1758 | devres_release_group(&wacom->hdev->dev, |
| 1759 | &wacom->battery.bat_desc); |
| 1760 | wacom->battery.battery = NULL; |
Chris Bagwell | b7af2bb | 2012-06-12 00:25:23 -0700 | [diff] [blame] | 1761 | } |
Chris Bagwell | a1d552c | 2012-03-25 23:26:30 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 1764 | static ssize_t wacom_show_speed(struct device *dev, |
| 1765 | struct device_attribute |
| 1766 | *attr, char *buf) |
| 1767 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 1768 | struct hid_device *hdev = to_hid_device(dev); |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 1769 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 1770 | |
| 1771 | return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed); |
| 1772 | } |
| 1773 | |
| 1774 | static ssize_t wacom_store_speed(struct device *dev, |
| 1775 | struct device_attribute *attr, |
| 1776 | const char *buf, size_t count) |
| 1777 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 1778 | struct hid_device *hdev = to_hid_device(dev); |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 1779 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 1780 | u8 new_speed; |
| 1781 | |
| 1782 | if (kstrtou8(buf, 0, &new_speed)) |
| 1783 | return -EINVAL; |
| 1784 | |
| 1785 | if (new_speed != 0 && new_speed != 1) |
| 1786 | return -EINVAL; |
| 1787 | |
| 1788 | wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features); |
| 1789 | |
| 1790 | return count; |
| 1791 | } |
| 1792 | |
Ping Cheng | e0984bc | 2014-09-10 12:40:05 -0700 | [diff] [blame] | 1793 | static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM, |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 1794 | wacom_show_speed, wacom_store_speed); |
| 1795 | |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1796 | |
| 1797 | static ssize_t wacom_show_remote_mode(struct kobject *kobj, |
| 1798 | struct kobj_attribute *kattr, |
| 1799 | char *buf, int index) |
| 1800 | { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 1801 | struct device *dev = kobj_to_dev(kobj->parent); |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 1802 | struct hid_device *hdev = to_hid_device(dev); |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1803 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 1804 | u8 mode; |
| 1805 | |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1806 | mode = wacom->led.groups[index].select; |
Christos Gkekas | bc35f73 | 2017-07-08 20:25:48 +0100 | [diff] [blame] | 1807 | return sprintf(buf, "%d\n", mode < 3 ? mode : -1); |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | #define DEVICE_EKR_ATTR_GROUP(SET_ID) \ |
| 1811 | static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \ |
| 1812 | struct kobj_attribute *kattr, char *buf) \ |
| 1813 | { \ |
| 1814 | return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \ |
| 1815 | } \ |
| 1816 | static struct kobj_attribute remote##SET_ID##_mode_attr = { \ |
| 1817 | .attr = {.name = "remote_mode", \ |
| 1818 | .mode = DEV_ATTR_RO_PERM}, \ |
| 1819 | .show = wacom_show_remote##SET_ID##_mode, \ |
| 1820 | }; \ |
| 1821 | static struct attribute *remote##SET_ID##_serial_attrs[] = { \ |
| 1822 | &remote##SET_ID##_mode_attr.attr, \ |
| 1823 | NULL \ |
| 1824 | }; \ |
| 1825 | static struct attribute_group remote##SET_ID##_serial_group = { \ |
| 1826 | .name = NULL, \ |
| 1827 | .attrs = remote##SET_ID##_serial_attrs, \ |
| 1828 | } |
| 1829 | |
| 1830 | DEVICE_EKR_ATTR_GROUP(0); |
| 1831 | DEVICE_EKR_ATTR_GROUP(1); |
| 1832 | DEVICE_EKR_ATTR_GROUP(2); |
| 1833 | DEVICE_EKR_ATTR_GROUP(3); |
| 1834 | DEVICE_EKR_ATTR_GROUP(4); |
| 1835 | |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 1836 | static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, |
| 1837 | int index) |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1838 | { |
| 1839 | int error = 0; |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 1840 | struct wacom_remote *remote = wacom->remote; |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1841 | |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 1842 | remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev, |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 1843 | GFP_KERNEL, |
| 1844 | "%d", serial); |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 1845 | if (!remote->remotes[index].group.name) |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1846 | return -ENOMEM; |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1847 | |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 1848 | error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir, |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 1849 | &remote->remotes[index].group); |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1850 | if (error) { |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 1851 | remote->remotes[index].group.name = NULL; |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1852 | hid_err(wacom->hdev, |
| 1853 | "cannot create sysfs group err: %d\n", error); |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1854 | return error; |
| 1855 | } |
| 1856 | |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1860 | static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector) |
| 1861 | { |
| 1862 | const size_t buf_size = 2; |
| 1863 | unsigned char *buf; |
| 1864 | int retval; |
| 1865 | |
| 1866 | buf = kzalloc(buf_size, GFP_KERNEL); |
| 1867 | if (!buf) |
| 1868 | return -ENOMEM; |
| 1869 | |
| 1870 | buf[0] = WAC_CMD_DELETE_PAIRING; |
| 1871 | buf[1] = selector; |
| 1872 | |
| 1873 | retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf, |
| 1874 | buf_size, WAC_CMD_RETRIES); |
| 1875 | kfree(buf); |
| 1876 | |
| 1877 | return retval; |
| 1878 | } |
| 1879 | |
| 1880 | static ssize_t wacom_store_unpair_remote(struct kobject *kobj, |
| 1881 | struct kobj_attribute *attr, |
| 1882 | const char *buf, size_t count) |
| 1883 | { |
| 1884 | unsigned char selector = 0; |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 1885 | struct device *dev = kobj_to_dev(kobj->parent); |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 1886 | struct hid_device *hdev = to_hid_device(dev); |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1887 | struct wacom *wacom = hid_get_drvdata(hdev); |
| 1888 | int err; |
| 1889 | |
| 1890 | if (!strncmp(buf, "*\n", 2)) { |
| 1891 | selector = WAC_CMD_UNPAIR_ALL; |
| 1892 | } else { |
| 1893 | hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n", |
| 1894 | buf); |
| 1895 | return -1; |
| 1896 | } |
| 1897 | |
| 1898 | mutex_lock(&wacom->lock); |
| 1899 | |
| 1900 | err = wacom_cmd_unpair_remote(wacom, selector); |
| 1901 | mutex_unlock(&wacom->lock); |
| 1902 | |
| 1903 | return err < 0 ? err : count; |
| 1904 | } |
| 1905 | |
| 1906 | static struct kobj_attribute unpair_remote_attr = { |
| 1907 | .attr = {.name = "unpair_remote", .mode = 0200}, |
| 1908 | .store = wacom_store_unpair_remote, |
| 1909 | }; |
| 1910 | |
| 1911 | static const struct attribute *remote_unpair_attrs[] = { |
| 1912 | &unpair_remote_attr.attr, |
| 1913 | NULL |
| 1914 | }; |
| 1915 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 1916 | static void wacom_remotes_destroy(void *data) |
| 1917 | { |
| 1918 | struct wacom *wacom = data; |
| 1919 | struct wacom_remote *remote = wacom->remote; |
| 1920 | |
| 1921 | if (!remote) |
| 1922 | return; |
| 1923 | |
| 1924 | kobject_put(remote->remote_dir); |
| 1925 | kfifo_free(&remote->remote_fifo); |
| 1926 | wacom->remote = NULL; |
| 1927 | } |
| 1928 | |
| 1929 | static int wacom_initialize_remotes(struct wacom *wacom) |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1930 | { |
| 1931 | int error = 0; |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 1932 | struct wacom_remote *remote; |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1933 | int i; |
| 1934 | |
| 1935 | if (wacom->wacom_wac.features.type != REMOTE) |
| 1936 | return 0; |
| 1937 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 1938 | remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote), |
| 1939 | GFP_KERNEL); |
| 1940 | if (!remote) |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1941 | return -ENOMEM; |
| 1942 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 1943 | wacom->remote = remote; |
| 1944 | |
| 1945 | spin_lock_init(&remote->remote_lock); |
| 1946 | |
| 1947 | error = kfifo_alloc(&remote->remote_fifo, |
| 1948 | 5 * sizeof(struct wacom_remote_data), |
| 1949 | GFP_KERNEL); |
| 1950 | if (error) { |
| 1951 | hid_err(wacom->hdev, "failed allocating remote_fifo\n"); |
| 1952 | return -ENOMEM; |
| 1953 | } |
| 1954 | |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 1955 | remote->remotes[0].group = remote0_serial_group; |
| 1956 | remote->remotes[1].group = remote1_serial_group; |
| 1957 | remote->remotes[2].group = remote2_serial_group; |
| 1958 | remote->remotes[3].group = remote3_serial_group; |
| 1959 | remote->remotes[4].group = remote4_serial_group; |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 1960 | |
| 1961 | remote->remote_dir = kobject_create_and_add("wacom_remote", |
| 1962 | &wacom->hdev->dev.kobj); |
| 1963 | if (!remote->remote_dir) |
| 1964 | return -ENOMEM; |
| 1965 | |
| 1966 | error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs); |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1967 | |
| 1968 | if (error) { |
| 1969 | hid_err(wacom->hdev, |
| 1970 | "cannot create sysfs group err: %d\n", error); |
| 1971 | return error; |
| 1972 | } |
| 1973 | |
| 1974 | for (i = 0; i < WACOM_MAX_REMOTES; i++) { |
Benjamin Tissoires | a50aac7 | 2016-07-13 18:06:00 +0200 | [diff] [blame] | 1975 | wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN; |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 1976 | remote->remotes[i].serial = 0; |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 1979 | error = devm_add_action_or_reset(&wacom->hdev->dev, |
| 1980 | wacom_remotes_destroy, wacom); |
| 1981 | if (error) |
| 1982 | return error; |
| 1983 | |
Aaron Skomra | 72b236d | 2015-08-20 16:05:17 -0700 | [diff] [blame] | 1984 | return 0; |
| 1985 | } |
| 1986 | |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 1987 | static struct input_dev *wacom_allocate_input(struct wacom *wacom) |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame] | 1988 | { |
| 1989 | struct input_dev *input_dev; |
Benjamin Tissoires | b6c79f2 | 2014-07-24 13:00:03 -0700 | [diff] [blame] | 1990 | struct hid_device *hdev = wacom->hdev; |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame] | 1991 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame] | 1992 | |
Benjamin Tissoires | 3dad188 | 2016-07-13 18:05:54 +0200 | [diff] [blame] | 1993 | input_dev = devm_input_allocate_device(&hdev->dev); |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 1994 | if (!input_dev) |
| 1995 | return NULL; |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame] | 1996 | |
Jason Gerecke | 2bdd163 | 2015-07-13 18:03:45 -0700 | [diff] [blame] | 1997 | input_dev->name = wacom_wac->features.name; |
Benjamin Tissoires | b6c79f2 | 2014-07-24 13:00:03 -0700 | [diff] [blame] | 1998 | input_dev->phys = hdev->phys; |
| 1999 | input_dev->dev.parent = &hdev->dev; |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame] | 2000 | input_dev->open = wacom_open; |
| 2001 | input_dev->close = wacom_close; |
Benjamin Tissoires | b6c79f2 | 2014-07-24 13:00:03 -0700 | [diff] [blame] | 2002 | input_dev->uniq = hdev->uniq; |
| 2003 | input_dev->id.bustype = hdev->bus; |
| 2004 | input_dev->id.vendor = hdev->vendor; |
Benjamin Tissoires | 12969e3 | 2014-09-11 13:14:04 -0400 | [diff] [blame] | 2005 | input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product; |
Benjamin Tissoires | b6c79f2 | 2014-07-24 13:00:03 -0700 | [diff] [blame] | 2006 | input_dev->id.version = hdev->version; |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame] | 2007 | input_set_drvdata(input_dev, wacom); |
| 2008 | |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 2009 | return input_dev; |
| 2010 | } |
| 2011 | |
Jason Gerecke | d9f2d20 | 2015-07-13 18:03:44 -0700 | [diff] [blame] | 2012 | static int wacom_allocate_inputs(struct wacom *wacom) |
| 2013 | { |
| 2014 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
| 2015 | |
| 2016 | wacom_wac->pen_input = wacom_allocate_input(wacom); |
| 2017 | wacom_wac->touch_input = wacom_allocate_input(wacom); |
| 2018 | wacom_wac->pad_input = wacom_allocate_input(wacom); |
Benjamin Tissoires | 3dad188 | 2016-07-13 18:05:54 +0200 | [diff] [blame] | 2019 | if (!wacom_wac->pen_input || |
| 2020 | !wacom_wac->touch_input || |
| 2021 | !wacom_wac->pad_input) |
Jason Gerecke | d9f2d20 | 2015-07-13 18:03:44 -0700 | [diff] [blame] | 2022 | return -ENOMEM; |
Jason Gerecke | d9f2d20 | 2015-07-13 18:03:44 -0700 | [diff] [blame] | 2023 | |
Jason Gerecke | 2bdd163 | 2015-07-13 18:03:45 -0700 | [diff] [blame] | 2024 | wacom_wac->pen_input->name = wacom_wac->pen_name; |
Jason Gerecke | d9f2d20 | 2015-07-13 18:03:44 -0700 | [diff] [blame] | 2025 | wacom_wac->touch_input->name = wacom_wac->touch_name; |
| 2026 | wacom_wac->pad_input->name = wacom_wac->pad_name; |
| 2027 | |
| 2028 | return 0; |
| 2029 | } |
| 2030 | |
Benjamin Tissoires | 008f4d9 | 2014-07-24 12:51:26 -0700 | [diff] [blame] | 2031 | static int wacom_register_inputs(struct wacom *wacom) |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 2032 | { |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2033 | struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev; |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 2034 | struct wacom_wac *wacom_wac = &(wacom->wacom_wac); |
Jason Gerecke | 2636a3f | 2015-06-15 18:01:44 -0700 | [diff] [blame] | 2035 | int error = 0; |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 2036 | |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2037 | pen_input_dev = wacom_wac->pen_input; |
| 2038 | touch_input_dev = wacom_wac->touch_input; |
Benjamin Tissoires | 2546dac | 2014-09-23 12:08:06 -0400 | [diff] [blame] | 2039 | pad_input_dev = wacom_wac->pad_input; |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 2040 | |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2041 | if (!pen_input_dev || !touch_input_dev || !pad_input_dev) |
Benjamin Tissoires | 2546dac | 2014-09-23 12:08:06 -0400 | [diff] [blame] | 2042 | return -EINVAL; |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 2043 | |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2044 | error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac); |
| 2045 | if (error) { |
| 2046 | /* no pen in use on this interface */ |
| 2047 | input_free_device(pen_input_dev); |
| 2048 | wacom_wac->pen_input = NULL; |
| 2049 | pen_input_dev = NULL; |
| 2050 | } else { |
| 2051 | error = input_register_device(pen_input_dev); |
Ping Cheng | 30ebc1a | 2014-11-18 13:29:16 -0800 | [diff] [blame] | 2052 | if (error) |
Benjamin Tissoires | 3dad188 | 2016-07-13 18:05:54 +0200 | [diff] [blame] | 2053 | goto fail; |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac); |
| 2057 | if (error) { |
| 2058 | /* no touch in use on this interface */ |
| 2059 | input_free_device(touch_input_dev); |
| 2060 | wacom_wac->touch_input = NULL; |
| 2061 | touch_input_dev = NULL; |
| 2062 | } else { |
| 2063 | error = input_register_device(touch_input_dev); |
| 2064 | if (error) |
Benjamin Tissoires | 3dad188 | 2016-07-13 18:05:54 +0200 | [diff] [blame] | 2065 | goto fail; |
Ping Cheng | 30ebc1a | 2014-11-18 13:29:16 -0800 | [diff] [blame] | 2066 | } |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame] | 2067 | |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 2068 | error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac); |
| 2069 | if (error) { |
| 2070 | /* no pad in use on this interface */ |
| 2071 | input_free_device(pad_input_dev); |
| 2072 | wacom_wac->pad_input = NULL; |
| 2073 | pad_input_dev = NULL; |
| 2074 | } else { |
| 2075 | error = input_register_device(pad_input_dev); |
| 2076 | if (error) |
Benjamin Tissoires | 3dad188 | 2016-07-13 18:05:54 +0200 | [diff] [blame] | 2077 | goto fail; |
Benjamin Tissoires | d2d13f1 | 2014-07-24 12:48:28 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
Ping Cheng | 1963518 | 2012-04-29 21:09:18 -0700 | [diff] [blame] | 2080 | return 0; |
| 2081 | |
Benjamin Tissoires | 3dad188 | 2016-07-13 18:05:54 +0200 | [diff] [blame] | 2082 | fail: |
| 2083 | wacom_wac->pad_input = NULL; |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2084 | wacom_wac->touch_input = NULL; |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2085 | wacom_wac->pen_input = NULL; |
Chris Bagwell | 3aac0ef | 2012-03-25 23:25:45 -0700 | [diff] [blame] | 2086 | return error; |
| 2087 | } |
| 2088 | |
Jason Gerecke | 0be0171 | 2015-08-05 15:44:53 -0700 | [diff] [blame] | 2089 | /* |
| 2090 | * Not all devices report physical dimensions from HID. |
| 2091 | * Compute the default from hardcoded logical dimension |
| 2092 | * and resolution before driver overwrites them. |
| 2093 | */ |
| 2094 | static void wacom_set_default_phy(struct wacom_features *features) |
| 2095 | { |
| 2096 | if (features->x_resolution) { |
| 2097 | features->x_phy = (features->x_max * 100) / |
| 2098 | features->x_resolution; |
| 2099 | features->y_phy = (features->y_max * 100) / |
| 2100 | features->y_resolution; |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | static void wacom_calculate_res(struct wacom_features *features) |
| 2105 | { |
| 2106 | /* set unit to "100th of a mm" for devices not reported by HID */ |
| 2107 | if (!features->unit) { |
| 2108 | features->unit = 0x11; |
| 2109 | features->unitExpo = -3; |
| 2110 | } |
| 2111 | |
| 2112 | features->x_resolution = wacom_calc_hid_res(features->x_max, |
| 2113 | features->x_phy, |
| 2114 | features->unit, |
| 2115 | features->unitExpo); |
| 2116 | features->y_resolution = wacom_calc_hid_res(features->y_max, |
| 2117 | features->y_phy, |
| 2118 | features->unit, |
| 2119 | features->unitExpo); |
| 2120 | } |
| 2121 | |
Jason Gerecke | fce9957 | 2015-03-06 11:47:40 -0800 | [diff] [blame] | 2122 | void wacom_battery_work(struct work_struct *work) |
| 2123 | { |
Benjamin Tissoires | d17d1f1 | 2016-07-13 18:05:52 +0200 | [diff] [blame] | 2124 | struct wacom *wacom = container_of(work, struct wacom, battery_work); |
Jason Gerecke | fce9957 | 2015-03-06 11:47:40 -0800 | [diff] [blame] | 2125 | |
| 2126 | if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) && |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 2127 | !wacom->battery.battery) { |
Jason Gerecke | fce9957 | 2015-03-06 11:47:40 -0800 | [diff] [blame] | 2128 | wacom_initialize_battery(wacom); |
| 2129 | } |
| 2130 | else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) && |
Benjamin Tissoires | 59d69bc | 2016-07-13 18:06:08 +0200 | [diff] [blame] | 2131 | wacom->battery.battery) { |
Jason Gerecke | fce9957 | 2015-03-06 11:47:40 -0800 | [diff] [blame] | 2132 | wacom_destroy_battery(wacom); |
| 2133 | } |
| 2134 | } |
| 2135 | |
Benjamin Tissoires | 01c846f | 2014-07-24 12:59:11 -0700 | [diff] [blame] | 2136 | static size_t wacom_compute_pktlen(struct hid_device *hdev) |
| 2137 | { |
| 2138 | struct hid_report_enum *report_enum; |
| 2139 | struct hid_report *report; |
| 2140 | size_t size = 0; |
| 2141 | |
| 2142 | report_enum = hdev->report_enum + HID_INPUT_REPORT; |
| 2143 | |
| 2144 | list_for_each_entry(report, &report_enum->report_list, list) { |
Mathieu Magnaudet | dabb05c6 | 2014-11-27 16:02:36 +0100 | [diff] [blame] | 2145 | size_t report_size = hid_report_len(report); |
Benjamin Tissoires | 01c846f | 2014-07-24 12:59:11 -0700 | [diff] [blame] | 2146 | if (report_size > size) |
| 2147 | size = report_size; |
| 2148 | } |
| 2149 | |
| 2150 | return size; |
| 2151 | } |
| 2152 | |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2153 | static void wacom_update_name(struct wacom *wacom, const char *suffix) |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2154 | { |
| 2155 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
| 2156 | struct wacom_features *features = &wacom_wac->features; |
Jason Gerecke | 44b5250 | 2015-06-15 18:01:41 -0700 | [diff] [blame] | 2157 | char name[WACOM_NAME_MAX]; |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2158 | |
| 2159 | /* Generic devices name unspecified */ |
| 2160 | if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) { |
Jason Gerecke | 09dc28a | 2017-07-24 09:46:19 -0700 | [diff] [blame] | 2161 | char *product_name = wacom->hdev->name; |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2162 | |
Jason Gerecke | 09dc28a | 2017-07-24 09:46:19 -0700 | [diff] [blame] | 2163 | if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) { |
| 2164 | struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent); |
| 2165 | struct usb_device *dev = interface_to_usbdev(intf); |
| 2166 | product_name = dev->product; |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2167 | } |
Jason Gerecke | 09dc28a | 2017-07-24 09:46:19 -0700 | [diff] [blame] | 2168 | |
| 2169 | if (wacom->hdev->bus == BUS_I2C) { |
| 2170 | snprintf(name, sizeof(name), "%s %X", |
| 2171 | features->name, wacom->hdev->product); |
| 2172 | } else if (strstr(product_name, "Wacom") || |
| 2173 | strstr(product_name, "wacom") || |
| 2174 | strstr(product_name, "WACOM")) { |
| 2175 | strlcpy(name, product_name, sizeof(name)); |
| 2176 | } else { |
| 2177 | snprintf(name, sizeof(name), "Wacom %s", product_name); |
| 2178 | } |
| 2179 | |
| 2180 | /* strip out excess whitespaces */ |
| 2181 | while (1) { |
| 2182 | char *gap = strstr(name, " "); |
| 2183 | if (gap == NULL) |
| 2184 | break; |
| 2185 | /* shift everything including the terminator */ |
| 2186 | memmove(gap, gap+1, strlen(gap)); |
| 2187 | } |
| 2188 | |
| 2189 | /* get rid of trailing whitespace */ |
| 2190 | if (name[strlen(name)-1] == ' ') |
| 2191 | name[strlen(name)-1] = '\0'; |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2192 | } else { |
Jason Gerecke | 44b5250 | 2015-06-15 18:01:41 -0700 | [diff] [blame] | 2193 | strlcpy(name, features->name, sizeof(name)); |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
Benjamin Tissoires | 9956953 | 2016-07-13 18:06:17 +0200 | [diff] [blame] | 2196 | snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s", |
| 2197 | name, suffix); |
| 2198 | |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2199 | /* Append the device type to the name */ |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2200 | snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name), |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2201 | "%s%s Pen", name, suffix); |
Jason Gerecke | 2a6cdbd | 2015-06-15 18:01:45 -0700 | [diff] [blame] | 2202 | snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name), |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2203 | "%s%s Finger", name, suffix); |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2204 | snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name), |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2205 | "%s%s Pad", name, suffix); |
Ping Cheng | c24eab4 | 2015-04-24 15:32:51 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Benjamin Tissoires | 84dfbd7 | 2016-07-13 18:05:55 +0200 | [diff] [blame] | 2208 | static void wacom_release_resources(struct wacom *wacom) |
| 2209 | { |
| 2210 | struct hid_device *hdev = wacom->hdev; |
| 2211 | |
| 2212 | if (!wacom->resources) |
| 2213 | return; |
| 2214 | |
| 2215 | devres_release_group(&hdev->dev, wacom); |
| 2216 | |
| 2217 | wacom->resources = false; |
| 2218 | |
| 2219 | wacom->wacom_wac.pen_input = NULL; |
| 2220 | wacom->wacom_wac.touch_input = NULL; |
| 2221 | wacom->wacom_wac.pad_input = NULL; |
| 2222 | } |
| 2223 | |
Aaron Armstrong Skomra | d2ec58a | 2017-01-25 12:08:41 -0800 | [diff] [blame] | 2224 | static void wacom_set_shared_values(struct wacom_wac *wacom_wac) |
| 2225 | { |
| 2226 | if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) { |
| 2227 | wacom_wac->shared->type = wacom_wac->features.type; |
| 2228 | wacom_wac->shared->touch_input = wacom_wac->touch_input; |
| 2229 | } |
| 2230 | |
Ping Cheng | d793ff8 | 2017-02-14 21:27:45 -0800 | [diff] [blame] | 2231 | if (wacom_wac->has_mute_touch_switch) { |
Aaron Armstrong Skomra | d2ec58a | 2017-01-25 12:08:41 -0800 | [diff] [blame] | 2232 | wacom_wac->shared->has_mute_touch_switch = true; |
Ping Cheng | d793ff8 | 2017-02-14 21:27:45 -0800 | [diff] [blame] | 2233 | wacom_wac->shared->is_touch_on = true; |
| 2234 | } |
Aaron Armstrong Skomra | d2ec58a | 2017-01-25 12:08:41 -0800 | [diff] [blame] | 2235 | |
| 2236 | if (wacom_wac->shared->has_mute_touch_switch && |
| 2237 | wacom_wac->shared->touch_input) { |
| 2238 | set_bit(EV_SW, wacom_wac->shared->touch_input->evbit); |
| 2239 | input_set_capability(wacom_wac->shared->touch_input, EV_SW, |
| 2240 | SW_MUTE_DEVICE); |
| 2241 | } |
| 2242 | } |
| 2243 | |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2244 | static int wacom_parse_and_register(struct wacom *wacom, bool wireless) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2245 | { |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2246 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
| 2247 | struct wacom_features *features = &wacom_wac->features; |
| 2248 | struct hid_device *hdev = wacom->hdev; |
Jason Childs | e33da8a | 2010-02-17 22:38:31 -0800 | [diff] [blame] | 2249 | int error; |
Benjamin Tissoires | 7704ac9 | 2014-09-23 12:08:08 -0400 | [diff] [blame] | 2250 | unsigned int connect_mask = HID_CONNECT_HIDRAW; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2251 | |
Benjamin Tissoires | 01c846f | 2014-07-24 12:59:11 -0700 | [diff] [blame] | 2252 | features->pktlen = wacom_compute_pktlen(hdev); |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2253 | if (features->pktlen > WACOM_PKGLEN_MAX) |
| 2254 | return -EINVAL; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2255 | |
Benjamin Tissoires | 84dfbd7 | 2016-07-13 18:05:55 +0200 | [diff] [blame] | 2256 | if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL)) |
| 2257 | return -ENOMEM; |
| 2258 | |
| 2259 | wacom->resources = true; |
| 2260 | |
Jason Gerecke | 3f14a63 | 2015-08-03 10:17:05 -0700 | [diff] [blame] | 2261 | error = wacom_allocate_inputs(wacom); |
| 2262 | if (error) |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2263 | goto fail; |
Benjamin Tissoires | 494078b | 2014-09-23 12:08:07 -0400 | [diff] [blame] | 2264 | |
Benjamin Tissoires | 8c97a76 | 2015-02-26 11:28:50 -0500 | [diff] [blame] | 2265 | /* |
| 2266 | * Bamboo Pad has a generic hid handling for the Pen, and we switch it |
| 2267 | * into debug mode for the touch part. |
| 2268 | * We ignore the other interfaces. |
| 2269 | */ |
| 2270 | if (features->type == BAMBOO_PAD) { |
| 2271 | if (features->pktlen == WACOM_PKGLEN_PENABLED) { |
| 2272 | features->type = HID_GENERIC; |
| 2273 | } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) && |
| 2274 | (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) { |
| 2275 | error = -ENODEV; |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2276 | goto fail; |
Benjamin Tissoires | 8c97a76 | 2015-02-26 11:28:50 -0500 | [diff] [blame] | 2277 | } |
| 2278 | } |
| 2279 | |
Ping Cheng | 401d7d1 | 2013-07-28 00:38:54 -0700 | [diff] [blame] | 2280 | /* set the default size in case we do not get them from hid */ |
| 2281 | wacom_set_default_phy(features); |
| 2282 | |
Ping Cheng | f393ee2 | 2012-04-29 21:09:17 -0700 | [diff] [blame] | 2283 | /* Retrieve the physical and logical size for touch devices */ |
Benjamin Tissoires | c669fb2 | 2014-07-24 13:02:14 -0700 | [diff] [blame] | 2284 | wacom_retrieve_hid_descriptor(hdev, features); |
Ping Cheng | 42f4f27 | 2015-04-15 16:53:54 -0700 | [diff] [blame] | 2285 | wacom_setup_device_quirks(wacom); |
Jason Gerecke | 042628a | 2015-04-30 17:51:54 -0700 | [diff] [blame] | 2286 | |
Jason Gerecke | aa86b18 | 2015-06-15 18:01:42 -0700 | [diff] [blame] | 2287 | if (features->device_type == WACOM_DEVICETYPE_NONE && |
| 2288 | features->type != WIRELESS) { |
Jason Gerecke | 8e116d3 | 2015-04-30 17:51:55 -0700 | [diff] [blame] | 2289 | error = features->type == HID_GENERIC ? -ENODEV : 0; |
| 2290 | |
Jason Gerecke | 042628a | 2015-04-30 17:51:54 -0700 | [diff] [blame] | 2291 | dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.", |
Jason Gerecke | 8e116d3 | 2015-04-30 17:51:55 -0700 | [diff] [blame] | 2292 | hdev->name, |
| 2293 | error ? "Ignoring" : "Assuming pen"); |
| 2294 | |
| 2295 | if (error) |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2296 | goto fail; |
Jason Gerecke | 042628a | 2015-04-30 17:51:54 -0700 | [diff] [blame] | 2297 | |
Jason Gerecke | aa86b18 | 2015-06-15 18:01:42 -0700 | [diff] [blame] | 2298 | features->device_type |= WACOM_DEVICETYPE_PEN; |
Jason Gerecke | 042628a | 2015-04-30 17:51:54 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
Ping Cheng | 401d7d1 | 2013-07-28 00:38:54 -0700 | [diff] [blame] | 2301 | wacom_calculate_res(features); |
| 2302 | |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2303 | wacom_update_name(wacom, wireless ? " (WL)" : ""); |
Ping Cheng | 4492eff | 2010-03-19 22:18:15 -0700 | [diff] [blame] | 2304 | |
Aaron Armstrong Skomra | 8b40735 | 2017-03-29 10:35:39 -0700 | [diff] [blame] | 2305 | /* pen only Bamboo neither support touch nor pad */ |
| 2306 | if ((features->type == BAMBOO_PEN) && |
| 2307 | ((features->device_type & WACOM_DEVICETYPE_TOUCH) || |
| 2308 | (features->device_type & WACOM_DEVICETYPE_PAD))) { |
| 2309 | error = -ENODEV; |
| 2310 | goto fail; |
| 2311 | } |
| 2312 | |
Jason Gerecke | a9ce785 | 2017-01-17 15:38:58 -0800 | [diff] [blame] | 2313 | error = wacom_add_shared_data(hdev); |
| 2314 | if (error) |
| 2315 | goto fail; |
Ping Cheng | 49b764a | 2010-02-20 00:53:49 -0800 | [diff] [blame] | 2316 | |
Jason Gerecke | ccad85c | 2015-08-03 10:17:04 -0700 | [diff] [blame] | 2317 | if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) && |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 2318 | (features->quirks & WACOM_QUIRK_BATTERY)) { |
| 2319 | error = wacom_initialize_battery(wacom); |
| 2320 | if (error) |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2321 | goto fail; |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Jason Gerecke | 3f14a63 | 2015-08-03 10:17:05 -0700 | [diff] [blame] | 2324 | error = wacom_register_inputs(wacom); |
| 2325 | if (error) |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2326 | goto fail; |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 2327 | |
Benjamin Tissoires | b62f646 | 2016-07-13 18:05:50 +0200 | [diff] [blame] | 2328 | if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) { |
Benjamin Tissoires | 85d2c77 | 2016-07-13 18:05:51 +0200 | [diff] [blame] | 2329 | error = wacom_initialize_leds(wacom); |
| 2330 | if (error) |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2331 | goto fail; |
Benjamin Tissoires | 85d2c77 | 2016-07-13 18:05:51 +0200 | [diff] [blame] | 2332 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 2333 | error = wacom_initialize_remotes(wacom); |
Benjamin Tissoires | b62f646 | 2016-07-13 18:05:50 +0200 | [diff] [blame] | 2334 | if (error) |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2335 | goto fail; |
Benjamin Tissoires | b62f646 | 2016-07-13 18:05:50 +0200 | [diff] [blame] | 2336 | } |
| 2337 | |
Benjamin Tissoires | 7704ac9 | 2014-09-23 12:08:08 -0400 | [diff] [blame] | 2338 | if (features->type == HID_GENERIC) |
| 2339 | connect_mask |= HID_CONNECT_DRIVER; |
| 2340 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2341 | /* Regular HID work starts now */ |
Benjamin Tissoires | 7704ac9 | 2014-09-23 12:08:08 -0400 | [diff] [blame] | 2342 | error = hid_hw_start(hdev, connect_mask); |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2343 | if (error) { |
| 2344 | hid_err(hdev, "hw start failed\n"); |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2345 | goto fail; |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2348 | if (!wireless) { |
| 2349 | /* Note that if query fails it is not a hard failure */ |
Benjamin Tissoires | a544c61 | 2017-01-20 16:20:13 +0100 | [diff] [blame] | 2350 | wacom_query_tablet_data(wacom); |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2351 | } |
Jason Gerecke | 86e88f0 | 2015-11-03 16:57:58 -0800 | [diff] [blame] | 2352 | |
| 2353 | /* touch only Bamboo doesn't support pen */ |
| 2354 | if ((features->type == BAMBOO_TOUCH) && |
| 2355 | (features->device_type & WACOM_DEVICETYPE_PEN)) { |
Aaron Armstrong Skomra | 4d20c33 | 2017-03-29 11:41:28 -0700 | [diff] [blame] | 2356 | cancel_delayed_work_sync(&wacom->init_work); |
| 2357 | _wacom_query_tablet_data(wacom); |
Jason Gerecke | 86e88f0 | 2015-11-03 16:57:58 -0800 | [diff] [blame] | 2358 | error = -ENODEV; |
Benjamin Tissoires | b62f646 | 2016-07-13 18:05:50 +0200 | [diff] [blame] | 2359 | goto fail_quirks; |
Jason Gerecke | 86e88f0 | 2015-11-03 16:57:58 -0800 | [diff] [blame] | 2360 | } |
| 2361 | |
Jason Gerecke | ccad85c | 2015-08-03 10:17:04 -0700 | [diff] [blame] | 2362 | if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2363 | error = hid_hw_open(hdev); |
| 2364 | |
Aaron Armstrong Skomra | d2ec58a | 2017-01-25 12:08:41 -0800 | [diff] [blame] | 2365 | wacom_set_shared_values(wacom_wac); |
Benjamin Tissoires | 84dfbd7 | 2016-07-13 18:05:55 +0200 | [diff] [blame] | 2366 | devres_close_group(&hdev->dev, wacom); |
| 2367 | |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2368 | return 0; |
| 2369 | |
Benjamin Tissoires | b62f646 | 2016-07-13 18:05:50 +0200 | [diff] [blame] | 2370 | fail_quirks: |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2371 | hid_hw_stop(hdev); |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2372 | fail: |
Benjamin Tissoires | 84dfbd7 | 2016-07-13 18:05:55 +0200 | [diff] [blame] | 2373 | wacom_release_resources(wacom); |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2374 | return error; |
| 2375 | } |
| 2376 | |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2377 | static void wacom_wireless_work(struct work_struct *work) |
| 2378 | { |
Benjamin Tissoires | d17d1f1 | 2016-07-13 18:05:52 +0200 | [diff] [blame] | 2379 | struct wacom *wacom = container_of(work, struct wacom, wireless_work); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2380 | struct usb_device *usbdev = wacom->usbdev; |
| 2381 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
| 2382 | struct hid_device *hdev1, *hdev2; |
| 2383 | struct wacom *wacom1, *wacom2; |
| 2384 | struct wacom_wac *wacom_wac1, *wacom_wac2; |
| 2385 | int error; |
| 2386 | |
| 2387 | /* |
| 2388 | * Regardless if this is a disconnect or a new tablet, |
| 2389 | * remove any existing input and battery devices. |
| 2390 | */ |
| 2391 | |
| 2392 | wacom_destroy_battery(wacom); |
| 2393 | |
| 2394 | /* Stylus interface */ |
| 2395 | hdev1 = usb_get_intfdata(usbdev->config->interface[1]); |
| 2396 | wacom1 = hid_get_drvdata(hdev1); |
| 2397 | wacom_wac1 = &(wacom1->wacom_wac); |
Benjamin Tissoires | 84dfbd7 | 2016-07-13 18:05:55 +0200 | [diff] [blame] | 2398 | wacom_release_resources(wacom1); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2399 | |
| 2400 | /* Touch interface */ |
| 2401 | hdev2 = usb_get_intfdata(usbdev->config->interface[2]); |
| 2402 | wacom2 = hid_get_drvdata(hdev2); |
| 2403 | wacom_wac2 = &(wacom2->wacom_wac); |
Benjamin Tissoires | 84dfbd7 | 2016-07-13 18:05:55 +0200 | [diff] [blame] | 2404 | wacom_release_resources(wacom2); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2405 | |
| 2406 | if (wacom_wac->pid == 0) { |
| 2407 | hid_info(wacom->hdev, "wireless tablet disconnected\n"); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2408 | } else { |
| 2409 | const struct hid_device_id *id = wacom_ids; |
| 2410 | |
| 2411 | hid_info(wacom->hdev, "wireless tablet connected with PID %x\n", |
| 2412 | wacom_wac->pid); |
| 2413 | |
| 2414 | while (id->bus) { |
| 2415 | if (id->vendor == USB_VENDOR_ID_WACOM && |
| 2416 | id->product == wacom_wac->pid) |
| 2417 | break; |
| 2418 | id++; |
| 2419 | } |
| 2420 | |
| 2421 | if (!id->bus) { |
| 2422 | hid_info(wacom->hdev, "ignoring unknown PID.\n"); |
| 2423 | return; |
| 2424 | } |
| 2425 | |
| 2426 | /* Stylus interface */ |
| 2427 | wacom_wac1->features = |
| 2428 | *((struct wacom_features *)id->driver_data); |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2429 | |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2430 | wacom_wac1->pid = wacom_wac->pid; |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2431 | hid_hw_stop(hdev1); |
| 2432 | error = wacom_parse_and_register(wacom1, true); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2433 | if (error) |
| 2434 | goto fail; |
| 2435 | |
| 2436 | /* Touch interface */ |
| 2437 | if (wacom_wac1->features.touch_max || |
| 2438 | (wacom_wac1->features.type >= INTUOSHT && |
| 2439 | wacom_wac1->features.type <= BAMBOO_PT)) { |
| 2440 | wacom_wac2->features = |
| 2441 | *((struct wacom_features *)id->driver_data); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2442 | wacom_wac2->pid = wacom_wac->pid; |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2443 | hid_hw_stop(hdev2); |
| 2444 | error = wacom_parse_and_register(wacom2, true); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2445 | if (error) |
| 2446 | goto fail; |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2447 | } |
| 2448 | |
Benjamin Tissoires | 9956953 | 2016-07-13 18:06:17 +0200 | [diff] [blame] | 2449 | strlcpy(wacom_wac->name, wacom_wac1->name, |
| 2450 | sizeof(wacom_wac->name)); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2451 | error = wacom_initialize_battery(wacom); |
| 2452 | if (error) |
| 2453 | goto fail; |
| 2454 | } |
| 2455 | |
| 2456 | return; |
| 2457 | |
| 2458 | fail: |
Benjamin Tissoires | 84dfbd7 | 2016-07-13 18:05:55 +0200 | [diff] [blame] | 2459 | wacom_release_resources(wacom1); |
Benjamin Tissoires | 84dfbd7 | 2016-07-13 18:05:55 +0200 | [diff] [blame] | 2460 | wacom_release_resources(wacom2); |
Benjamin Tissoires | a2f091a | 2016-02-12 17:27:42 +0100 | [diff] [blame] | 2461 | return; |
| 2462 | } |
| 2463 | |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2464 | static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index) |
| 2465 | { |
| 2466 | struct wacom_remote *remote = wacom->remote; |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2467 | u32 serial = remote->remotes[index].serial; |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2468 | int i; |
Benjamin Tissoires | 7c35dc3 | 2016-07-13 18:06:07 +0200 | [diff] [blame] | 2469 | unsigned long flags; |
| 2470 | |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2471 | for (i = 0; i < WACOM_MAX_REMOTES; i++) { |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2472 | if (remote->remotes[i].serial == serial) { |
Aaron Armstrong Skomra | 791ae27 | 2017-12-07 12:31:56 -0800 | [diff] [blame] | 2473 | |
| 2474 | spin_lock_irqsave(&remote->remote_lock, flags); |
| 2475 | remote->remotes[i].registered = false; |
| 2476 | spin_unlock_irqrestore(&remote->remote_lock, flags); |
| 2477 | |
| 2478 | if (remote->remotes[i].battery.battery) |
| 2479 | devres_release_group(&wacom->hdev->dev, |
| 2480 | &remote->remotes[i].battery.bat_desc); |
| 2481 | |
| 2482 | if (remote->remotes[i].group.name) |
| 2483 | devres_release_group(&wacom->hdev->dev, |
| 2484 | &remote->remotes[i]); |
| 2485 | |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2486 | remote->remotes[i].serial = 0; |
| 2487 | remote->remotes[i].group.name = NULL; |
Benjamin Tissoires | 9f1015d4 | 2016-07-13 18:06:09 +0200 | [diff] [blame] | 2488 | remote->remotes[i].battery.battery = NULL; |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2489 | wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN; |
| 2490 | } |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | static int wacom_remote_create_one(struct wacom *wacom, u32 serial, |
| 2495 | unsigned int index) |
| 2496 | { |
| 2497 | struct wacom_remote *remote = wacom->remote; |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 2498 | struct device *dev = &wacom->hdev->dev; |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2499 | int error, k; |
| 2500 | |
| 2501 | /* A remote can pair more than once with an EKR, |
| 2502 | * check to make sure this serial isn't already paired. |
| 2503 | */ |
| 2504 | for (k = 0; k < WACOM_MAX_REMOTES; k++) { |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2505 | if (remote->remotes[k].serial == serial) |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | if (k < WACOM_MAX_REMOTES) { |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2510 | remote->remotes[index].serial = serial; |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2511 | return 0; |
| 2512 | } |
| 2513 | |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2514 | if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL)) |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 2515 | return -ENOMEM; |
| 2516 | |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2517 | error = wacom_remote_create_attr_group(wacom, serial, index); |
| 2518 | if (error) |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 2519 | goto fail; |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2520 | |
Benjamin Tissoires | 7c35dc3 | 2016-07-13 18:06:07 +0200 | [diff] [blame] | 2521 | remote->remotes[index].input = wacom_allocate_input(wacom); |
| 2522 | if (!remote->remotes[index].input) { |
| 2523 | error = -ENOMEM; |
| 2524 | goto fail; |
| 2525 | } |
| 2526 | remote->remotes[index].input->uniq = remote->remotes[index].group.name; |
| 2527 | remote->remotes[index].input->name = wacom->wacom_wac.pad_name; |
| 2528 | |
| 2529 | if (!remote->remotes[index].input->name) { |
| 2530 | error = -EINVAL; |
| 2531 | goto fail; |
| 2532 | } |
| 2533 | |
| 2534 | error = wacom_setup_pad_input_capabilities(remote->remotes[index].input, |
| 2535 | &wacom->wacom_wac); |
| 2536 | if (error) |
| 2537 | goto fail; |
| 2538 | |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2539 | remote->remotes[index].serial = serial; |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2540 | |
Benjamin Tissoires | 7c35dc3 | 2016-07-13 18:06:07 +0200 | [diff] [blame] | 2541 | error = input_register_device(remote->remotes[index].input); |
| 2542 | if (error) |
| 2543 | goto fail; |
| 2544 | |
Benjamin Tissoires | 97f5541 | 2016-07-13 18:06:10 +0200 | [diff] [blame] | 2545 | error = wacom_led_groups_alloc_and_register_one( |
| 2546 | &remote->remotes[index].input->dev, |
| 2547 | wacom, index, 3, true); |
| 2548 | if (error) |
| 2549 | goto fail; |
| 2550 | |
Benjamin Tissoires | 7c35dc3 | 2016-07-13 18:06:07 +0200 | [diff] [blame] | 2551 | remote->remotes[index].registered = true; |
| 2552 | |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2553 | devres_close_group(dev, &remote->remotes[index]); |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2554 | return 0; |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 2555 | |
| 2556 | fail: |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2557 | devres_release_group(dev, &remote->remotes[index]); |
| 2558 | remote->remotes[index].serial = 0; |
Benjamin Tissoires | f9036bd | 2016-07-13 18:06:05 +0200 | [diff] [blame] | 2559 | return error; |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
Benjamin Tissoires | 9f1015d4 | 2016-07-13 18:06:09 +0200 | [diff] [blame] | 2562 | static int wacom_remote_attach_battery(struct wacom *wacom, int index) |
| 2563 | { |
| 2564 | struct wacom_remote *remote = wacom->remote; |
| 2565 | int error; |
| 2566 | |
| 2567 | if (!remote->remotes[index].registered) |
| 2568 | return 0; |
| 2569 | |
| 2570 | if (remote->remotes[index].battery.battery) |
| 2571 | return 0; |
| 2572 | |
| 2573 | if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN) |
| 2574 | return 0; |
| 2575 | |
| 2576 | error = __wacom_initialize_battery(wacom, |
| 2577 | &wacom->remote->remotes[index].battery); |
| 2578 | if (error) |
| 2579 | return error; |
| 2580 | |
| 2581 | return 0; |
| 2582 | } |
| 2583 | |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2584 | static void wacom_remote_work(struct work_struct *work) |
| 2585 | { |
| 2586 | struct wacom *wacom = container_of(work, struct wacom, remote_work); |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 2587 | struct wacom_remote *remote = wacom->remote; |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2588 | struct wacom_remote_data data; |
| 2589 | unsigned long flags; |
| 2590 | unsigned int count; |
| 2591 | u32 serial; |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2592 | int i; |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2593 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 2594 | spin_lock_irqsave(&remote->remote_lock, flags); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2595 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 2596 | count = kfifo_out(&remote->remote_fifo, &data, sizeof(data)); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2597 | |
| 2598 | if (count != sizeof(data)) { |
| 2599 | hid_err(wacom->hdev, |
| 2600 | "workitem triggered without status available\n"); |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 2601 | spin_unlock_irqrestore(&remote->remote_lock, flags); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2602 | return; |
| 2603 | } |
| 2604 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 2605 | if (!kfifo_is_empty(&remote->remote_fifo)) |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2606 | wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE); |
| 2607 | |
Benjamin Tissoires | 83e6b40 | 2016-07-13 18:06:02 +0200 | [diff] [blame] | 2608 | spin_unlock_irqrestore(&remote->remote_lock, flags); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2609 | |
| 2610 | for (i = 0; i < WACOM_MAX_REMOTES; i++) { |
| 2611 | serial = data.remote[i].serial; |
| 2612 | if (data.remote[i].connected) { |
| 2613 | |
Benjamin Tissoires | 9f1015d4 | 2016-07-13 18:06:09 +0200 | [diff] [blame] | 2614 | if (remote->remotes[i].serial == serial) { |
| 2615 | wacom_remote_attach_battery(wacom, i); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2616 | continue; |
Benjamin Tissoires | 9f1015d4 | 2016-07-13 18:06:09 +0200 | [diff] [blame] | 2617 | } |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2618 | |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2619 | if (remote->remotes[i].serial) |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2620 | wacom_remote_destroy_one(wacom, i); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2621 | |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2622 | wacom_remote_create_one(wacom, serial, i); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2623 | |
Benjamin Tissoires | e7749f6 | 2016-07-13 18:06:06 +0200 | [diff] [blame] | 2624 | } else if (remote->remotes[i].serial) { |
Benjamin Tissoires | 04bfa27 | 2016-07-13 18:06:04 +0200 | [diff] [blame] | 2625 | wacom_remote_destroy_one(wacom, i); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | |
Benjamin Tissoires | 4082da8 | 2017-02-14 21:27:18 -0800 | [diff] [blame] | 2630 | static void wacom_mode_change_work(struct work_struct *work) |
| 2631 | { |
| 2632 | struct wacom *wacom = container_of(work, struct wacom, mode_change_work); |
| 2633 | struct wacom_shared *shared = wacom->wacom_wac.shared; |
| 2634 | struct wacom *wacom1 = NULL; |
| 2635 | struct wacom *wacom2 = NULL; |
| 2636 | bool is_direct = wacom->wacom_wac.is_direct_mode; |
| 2637 | int error = 0; |
| 2638 | |
| 2639 | if (shared->pen) { |
| 2640 | wacom1 = hid_get_drvdata(shared->pen); |
| 2641 | wacom_release_resources(wacom1); |
| 2642 | hid_hw_stop(wacom1->hdev); |
| 2643 | wacom1->wacom_wac.has_mode_change = true; |
| 2644 | wacom1->wacom_wac.is_direct_mode = is_direct; |
| 2645 | } |
| 2646 | |
| 2647 | if (shared->touch) { |
| 2648 | wacom2 = hid_get_drvdata(shared->touch); |
| 2649 | wacom_release_resources(wacom2); |
| 2650 | hid_hw_stop(wacom2->hdev); |
| 2651 | wacom2->wacom_wac.has_mode_change = true; |
| 2652 | wacom2->wacom_wac.is_direct_mode = is_direct; |
| 2653 | } |
| 2654 | |
| 2655 | if (wacom1) { |
| 2656 | error = wacom_parse_and_register(wacom1, false); |
| 2657 | if (error) |
| 2658 | return; |
| 2659 | } |
| 2660 | |
| 2661 | if (wacom2) { |
| 2662 | error = wacom_parse_and_register(wacom2, false); |
| 2663 | if (error) |
| 2664 | return; |
| 2665 | } |
| 2666 | |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2667 | return; |
| 2668 | } |
| 2669 | |
| 2670 | static int wacom_probe(struct hid_device *hdev, |
| 2671 | const struct hid_device_id *id) |
| 2672 | { |
| 2673 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 2674 | struct usb_device *dev = interface_to_usbdev(intf); |
| 2675 | struct wacom *wacom; |
| 2676 | struct wacom_wac *wacom_wac; |
| 2677 | struct wacom_features *features; |
| 2678 | int error; |
| 2679 | |
| 2680 | if (!id->driver_data) |
| 2681 | return -EINVAL; |
| 2682 | |
| 2683 | hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; |
| 2684 | |
| 2685 | /* hid-core sets this quirk for the boot interface */ |
| 2686 | hdev->quirks &= ~HID_QUIRK_NOGET; |
| 2687 | |
Benjamin Tissoires | 19b6433 | 2016-07-13 18:05:58 +0200 | [diff] [blame] | 2688 | wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL); |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2689 | if (!wacom) |
| 2690 | return -ENOMEM; |
| 2691 | |
| 2692 | hid_set_drvdata(hdev, wacom); |
| 2693 | wacom->hdev = hdev; |
| 2694 | |
| 2695 | wacom_wac = &wacom->wacom_wac; |
| 2696 | wacom_wac->features = *((struct wacom_features *)id->driver_data); |
| 2697 | features = &wacom_wac->features; |
| 2698 | |
| 2699 | if (features->check_for_hid_type && features->hid_type != hdev->type) { |
| 2700 | error = -ENODEV; |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2701 | goto fail; |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2702 | } |
| 2703 | |
Jason Gerecke | 8341720 | 2017-11-10 11:50:01 -0800 | [diff] [blame] | 2704 | error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL); |
| 2705 | if (error) |
| 2706 | goto fail; |
| 2707 | |
Jason Gerecke | c6fa1ae | 2016-04-04 11:26:51 -0700 | [diff] [blame] | 2708 | wacom_wac->hid_data.inputmode = -1; |
Jason Gerecke | 326ea2a | 2016-04-04 11:26:52 -0700 | [diff] [blame] | 2709 | wacom_wac->mode_report = -1; |
Jason Gerecke | c6fa1ae | 2016-04-04 11:26:51 -0700 | [diff] [blame] | 2710 | |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2711 | wacom->usbdev = dev; |
| 2712 | wacom->intf = intf; |
| 2713 | mutex_init(&wacom->lock); |
Benjamin Tissoires | a544c61 | 2017-01-20 16:20:13 +0100 | [diff] [blame] | 2714 | INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work); |
Benjamin Tissoires | d17d1f1 | 2016-07-13 18:05:52 +0200 | [diff] [blame] | 2715 | INIT_WORK(&wacom->wireless_work, wacom_wireless_work); |
| 2716 | INIT_WORK(&wacom->battery_work, wacom_battery_work); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2717 | INIT_WORK(&wacom->remote_work, wacom_remote_work); |
Benjamin Tissoires | 4082da8 | 2017-02-14 21:27:18 -0800 | [diff] [blame] | 2718 | INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work); |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2719 | |
| 2720 | /* ask for the report descriptor to be loaded by HID */ |
| 2721 | error = hid_parse(hdev); |
| 2722 | if (error) { |
| 2723 | hid_err(hdev, "parse failed\n"); |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2724 | goto fail; |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2725 | } |
| 2726 | |
Benjamin Tissoires | fd5f92b | 2016-02-12 17:27:43 +0100 | [diff] [blame] | 2727 | error = wacom_parse_and_register(wacom, false); |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2728 | if (error) |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2729 | goto fail; |
Benjamin Tissoires | c58ac3a | 2016-02-12 17:27:41 +0100 | [diff] [blame] | 2730 | |
| 2731 | if (hdev->bus == BUS_BLUETOOTH) { |
| 2732 | error = device_create_file(&hdev->dev, &dev_attr_speed); |
| 2733 | if (error) |
| 2734 | hid_warn(hdev, |
| 2735 | "can't create sysfs speed attribute err: %d\n", |
| 2736 | error); |
| 2737 | } |
| 2738 | |
| 2739 | return 0; |
| 2740 | |
Benjamin Tissoires | 3888b0d | 2016-07-13 18:06:03 +0200 | [diff] [blame] | 2741 | fail: |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2742 | hid_set_drvdata(hdev, NULL); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 2743 | return error; |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2746 | static void wacom_remove(struct hid_device *hdev) |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2747 | { |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2748 | struct wacom *wacom = hid_get_drvdata(hdev); |
Benjamin Tissoires | f620516 | 2016-02-12 17:27:45 +0100 | [diff] [blame] | 2749 | struct wacom_wac *wacom_wac = &wacom->wacom_wac; |
| 2750 | struct wacom_features *features = &wacom_wac->features; |
| 2751 | |
| 2752 | if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) |
| 2753 | hid_hw_close(hdev); |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2754 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2755 | hid_hw_stop(hdev); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2756 | |
Benjamin Tissoires | a544c61 | 2017-01-20 16:20:13 +0100 | [diff] [blame] | 2757 | cancel_delayed_work_sync(&wacom->init_work); |
Benjamin Tissoires | d17d1f1 | 2016-07-13 18:05:52 +0200 | [diff] [blame] | 2758 | cancel_work_sync(&wacom->wireless_work); |
| 2759 | cancel_work_sync(&wacom->battery_work); |
Benjamin Tissoires | e6f2813 | 2016-07-13 18:06:01 +0200 | [diff] [blame] | 2760 | cancel_work_sync(&wacom->remote_work); |
Benjamin Tissoires | 4082da8 | 2017-02-14 21:27:18 -0800 | [diff] [blame] | 2761 | cancel_work_sync(&wacom->mode_change_work); |
Benjamin Tissoires | f81a129 | 2014-08-06 13:48:01 -0700 | [diff] [blame] | 2762 | if (hdev->bus == BUS_BLUETOOTH) |
| 2763 | device_remove_file(&hdev->dev, &dev_attr_speed); |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2764 | |
Benjamin Tissoires | c0265a94 | 2017-01-20 16:20:12 +0100 | [diff] [blame] | 2765 | /* make sure we don't trigger the LEDs */ |
| 2766 | wacom_led_groups_release(wacom); |
Aaron Armstrong Skomra | b6b1f19 | 2017-03-06 10:54:58 -0800 | [diff] [blame] | 2767 | |
| 2768 | if (wacom->wacom_wac.features.type != REMOTE) |
| 2769 | wacom_release_resources(wacom); |
Benjamin Tissoires | 5b779fc | 2017-01-20 16:20:11 +0100 | [diff] [blame] | 2770 | |
Jason Gerecke | 8341720 | 2017-11-10 11:50:01 -0800 | [diff] [blame] | 2771 | kfifo_free(&wacom_wac->pen_fifo); |
| 2772 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2773 | hid_set_drvdata(hdev, NULL); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2774 | } |
| 2775 | |
Geert Uytterhoeven | 41a7458 | 2014-08-11 11:03:19 -0700 | [diff] [blame] | 2776 | #ifdef CONFIG_PM |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2777 | static int wacom_resume(struct hid_device *hdev) |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2778 | { |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2779 | struct wacom *wacom = hid_get_drvdata(hdev); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2780 | |
| 2781 | mutex_lock(&wacom->lock); |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2782 | |
| 2783 | /* switch to wacom mode first */ |
Benjamin Tissoires | a544c61 | 2017-01-20 16:20:13 +0100 | [diff] [blame] | 2784 | _wacom_query_tablet_data(wacom); |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2785 | wacom_led_control(wacom); |
| 2786 | |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2787 | mutex_unlock(&wacom->lock); |
| 2788 | |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2792 | static int wacom_reset_resume(struct hid_device *hdev) |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2793 | { |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2794 | return wacom_resume(hdev); |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2795 | } |
Geert Uytterhoeven | 41a7458 | 2014-08-11 11:03:19 -0700 | [diff] [blame] | 2796 | #endif /* CONFIG_PM */ |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2797 | |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2798 | static struct hid_driver wacom_driver = { |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2799 | .name = "wacom", |
Bastian Blank | b036f6f | 2010-02-10 23:06:23 -0800 | [diff] [blame] | 2800 | .id_table = wacom_ids, |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2801 | .probe = wacom_probe, |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2802 | .remove = wacom_remove, |
Benjamin Tissoires | 7704ac9 | 2014-09-23 12:08:08 -0400 | [diff] [blame] | 2803 | .report = wacom_wac_report, |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2804 | #ifdef CONFIG_PM |
Oliver Neukum | e722409 | 2008-04-15 01:31:57 -0400 | [diff] [blame] | 2805 | .resume = wacom_resume, |
| 2806 | .reset_resume = wacom_reset_resume, |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2807 | #endif |
| 2808 | .raw_event = wacom_raw_event, |
Ping Cheng | 3bea733 | 2006-07-13 18:01:36 -0700 | [diff] [blame] | 2809 | }; |
Benjamin Tissoires | 29b4739 | 2014-07-24 12:52:23 -0700 | [diff] [blame] | 2810 | module_hid_driver(wacom_driver); |
Benjamin Tissoires | f2e0a7d | 2014-08-06 14:07:49 -0700 | [diff] [blame] | 2811 | |
| 2812 | MODULE_VERSION(DRIVER_VERSION); |
| 2813 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 2814 | MODULE_DESCRIPTION(DRIVER_DESC); |
Grant Grundler | 7021b60 | 2017-01-05 11:07:04 -0800 | [diff] [blame] | 2815 | MODULE_LICENSE("GPL"); |