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