blob: 8593047bb726cb1046ebbdba7a48a5802d1dc764 [file] [log] [blame]
Ping Cheng3bea7332006-07-13 18:01:36 -07001/*
Dmitry Torokhov4104d132007-05-07 16:16:29 -04002 * drivers/input/tablet/wacom_sys.c
Ping Cheng3bea7332006-07-13 18:01:36 -07003 *
Ping Cheng232f5692009-12-15 00:35:24 -08004 * USB Wacom tablet support - system specific code
Ping Cheng3bea7332006-07-13 18:01:36 -07005 */
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 Cheng3bea7332006-07-13 18:01:36 -070014#include "wacom_wac.h"
Dmitry Torokhov51269fe2010-03-19 22:18:15 -070015#include "wacom.h"
Ping Cheng3bea7332006-07-13 18:01:36 -070016
Ping Chenga417ea42011-08-16 00:17:56 -070017#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070018
Ping Cheng912ca212014-09-10 12:41:31 -070019#define WAC_CMD_WL_LED_CONTROL 0x03
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070020#define WAC_CMD_LED_CONTROL 0x20
21#define WAC_CMD_ICON_START 0x21
22#define WAC_CMD_ICON_XFER 0x23
Benjamin Tissoires849e2f02014-08-06 13:58:25 -070023#define WAC_CMD_ICON_BT_XFER 0x26
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070024#define WAC_CMD_RETRIES 10
25
Ping Chenge0984bc2014-09-10 12:40:05 -070026#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
27#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
28
Ping Chengc64d8832014-09-10 12:41:04 -070029static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
30 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070031{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070032 int retval;
33
34 do {
Ping Chengc64d8832014-09-10 12:41:04 -070035 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070036 HID_REQ_GET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070037 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
38
39 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070040}
41
Przemo Firszt296b7372014-08-06 14:00:38 -070042static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070044{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070045 int retval;
46
47 do {
Przemo Firszt296b7372014-08-06 14:00:38 -070048 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070049 HID_REQ_SET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070050 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
51
52 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070053}
54
Benjamin Tissoires29b47392014-07-24 12:52:23 -070055static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
56 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070057{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070058 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070059
Benjamin Tissoires29b47392014-07-24 12:52:23 -070060 if (size > WACOM_PKGLEN_MAX)
61 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070062
Benjamin Tissoires29b47392014-07-24 12:52:23 -070063 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070064
Benjamin Tissoires29b47392014-07-24 12:52:23 -070065 wacom_wac_irq(&wacom->wacom_wac, size);
66
67 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070068}
69
Ping Cheng3bea7332006-07-13 18:01:36 -070070static int wacom_open(struct input_dev *dev)
71{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040072 struct wacom *wacom = input_get_drvdata(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070073 int retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070074
Oliver Neukume7224092008-04-15 01:31:57 -040075 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070076 retval = hid_hw_open(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040077 mutex_unlock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070078
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -070079 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070080}
81
82static void wacom_close(struct input_dev *dev)
83{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040084 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070085
Oliver Neukume7224092008-04-15 01:31:57 -040086 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070087 hid_hw_close(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040088 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -070089}
90
Chris Bagwell16bf2882012-03-25 23:26:20 -070091/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070092 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -070093 */
94static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -070095 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -070096{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070097 struct hid_field field = {
98 .logical_maximum = logical_extents,
99 .physical_maximum = physical_extents,
100 .unit = unit,
101 .unit_exponent = exponent,
102 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700103
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700104 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700105}
106
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700107static void wacom_feature_mapping(struct hid_device *hdev,
108 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700109{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700110 struct wacom *wacom = hid_get_drvdata(hdev);
111 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400112 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400113 u8 *data;
114 int ret;
Chris Bagwell41343612011-10-26 22:32:52 -0700115
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700116 switch (usage->hid) {
117 case HID_DG_CONTACTMAX:
118 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400119 if (!features->touch_max) {
120 /* read manually */
121 data = kzalloc(2, GFP_KERNEL);
122 if (!data)
123 break;
124 data[0] = field->report->id;
125 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
126 data, 2, 0);
127 if (ret == 2)
128 features->touch_max = data[1];
129 kfree(data);
130 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700131 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400132 case HID_DG_INPUTMODE:
133 /* Ignore if value index is out of bounds. */
134 if (usage->usage_index >= field->report_count) {
135 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
136 break;
137 }
138
139 hid_data->inputmode = field->report->id;
140 hid_data->inputmode_index = usage->usage_index;
141 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700142 }
143}
144
Chris Bagwell428f8582011-10-26 22:26:59 -0700145/*
146 * Interface Descriptor of wacom devices can be incomplete and
147 * inconsistent so wacom_features table is used to store stylus
148 * device's packet lengths, various maximum values, and tablet
149 * resolution based on product ID's.
150 *
151 * For devices that contain 2 interfaces, wacom_features table is
152 * inaccurate for the touch interface. Since the Interface Descriptor
153 * for touch interfaces has pretty complete data, this function exists
154 * to query tablet for this missing information instead of hard coding in
155 * an additional table.
156 *
157 * A typical Interface Descriptor for a stylus will contain a
158 * boot mouse application collection that is not of interest and this
159 * function will ignore it.
160 *
161 * It also contains a digitizer application collection that also is not
162 * of interest since any information it contains would be duplicate
163 * of what is in wacom_features. Usually it defines a report of an array
164 * of bytes that could be used as max length of the stylus packet returned.
165 * If it happens to define a Digitizer-Stylus Physical Collection then
166 * the X and Y logical values contain valid data but it is ignored.
167 *
168 * A typical Interface Descriptor for a touch interface will contain a
169 * Digitizer-Finger Physical Collection which will define both logical
170 * X/Y maximum as well as the physical size of tablet. Since touch
171 * interfaces haven't supported pressure or distance, this is enough
172 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700173 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700174 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
175 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700176 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700177static void wacom_usage_mapping(struct hid_device *hdev,
178 struct hid_field *field, struct hid_usage *usage)
179{
180 struct wacom *wacom = hid_get_drvdata(hdev);
181 struct wacom_features *features = &wacom->wacom_wac.features;
182 bool finger = (field->logical == HID_DG_FINGER) ||
183 (field->physical == HID_DG_FINGER);
184 bool pen = (field->logical == HID_DG_STYLUS) ||
185 (field->physical == HID_DG_STYLUS);
186
187 /*
188 * Requiring Stylus Usage will ignore boot mouse
189 * X/Y values and some cases of invalid Digitizer X/Y
190 * values commonly reported.
191 */
192 if (!pen && !finger)
193 return;
194
195 if (finger && !features->touch_max)
196 /* touch device at least supports one touch point */
197 features->touch_max = 1;
198
199 switch (usage->hid) {
200 case HID_GD_X:
201 features->x_max = field->logical_maximum;
202 if (finger) {
203 features->device_type = BTN_TOOL_FINGER;
204 features->x_phy = field->physical_maximum;
205 if (features->type != BAMBOO_PT) {
206 features->unit = field->unit;
207 features->unitExpo = field->unit_exponent;
208 }
209 } else {
210 features->device_type = BTN_TOOL_PEN;
211 }
212 break;
213 case HID_GD_Y:
214 features->y_max = field->logical_maximum;
215 if (finger) {
216 features->y_phy = field->physical_maximum;
217 if (features->type != BAMBOO_PT) {
218 features->unit = field->unit;
219 features->unitExpo = field->unit_exponent;
220 }
221 }
222 break;
223 case HID_DG_TIPPRESSURE:
224 if (pen)
225 features->pressure_max = field->logical_maximum;
226 break;
227 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400228
229 if (features->type == HID_GENERIC)
230 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700231}
232
233static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800234 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500235{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700236 struct hid_report_enum *rep_enum;
237 struct hid_report *hreport;
238 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500239
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700240 /* check features first */
241 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
242 list_for_each_entry(hreport, &rep_enum->report_list, list) {
243 for (i = 0; i < hreport->maxfield; i++) {
244 /* Ignore if report count is out of bounds. */
245 if (hreport->field[i]->report_count < 1)
246 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500247
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700248 for (j = 0; j < hreport->field[i]->maxusage; j++) {
249 wacom_feature_mapping(hdev, hreport->field[i],
250 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500251 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500252 }
253 }
254
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700255 /* now check the input usages */
256 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
257 list_for_each_entry(hreport, &rep_enum->report_list, list) {
258
259 if (!hreport->maxfield)
260 continue;
261
262 for (i = 0; i < hreport->maxfield; i++)
263 for (j = 0; j < hreport->field[i]->maxusage; j++)
264 wacom_usage_mapping(hdev, hreport->field[i],
265 hreport->field[i]->usage + j);
266 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500267}
268
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400269static int wacom_hid_set_device_mode(struct hid_device *hdev)
270{
271 struct wacom *wacom = hid_get_drvdata(hdev);
272 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
273 struct hid_report *r;
274 struct hid_report_enum *re;
275
276 if (hid_data->inputmode < 0)
277 return 0;
278
279 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
280 r = re->report_id_hash[hid_data->inputmode];
281 if (r) {
282 r->field[0]->value[hid_data->inputmode_index] = 2;
283 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
284 }
285 return 0;
286}
287
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700288static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
289 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700290{
291 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700292 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700293
Jason Gereckefe494bc2012-10-03 17:25:35 -0700294 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700295 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800296 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700297
Jason Gereckefe494bc2012-10-03 17:25:35 -0700298 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800299 rep_data[0] = report_id;
300 rep_data[1] = mode;
301
Przemo Firszt296b7372014-08-06 14:00:38 -0700302 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
303 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700304 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700305 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700306 rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700307 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700308
309 kfree(rep_data);
310
311 return error < 0 ? error : 0;
312}
313
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700314static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
315 struct wacom_features *features)
316{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700317 struct wacom *wacom = hid_get_drvdata(hdev);
318 int ret;
319 u8 rep_data[2];
320
321 switch (features->type) {
322 case GRAPHIRE_BT:
323 rep_data[0] = 0x03;
324 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700325 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
326 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700327
328 if (ret >= 0) {
329 rep_data[0] = speed == 0 ? 0x05 : 0x06;
330 rep_data[1] = 0x00;
331
332 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700333 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700334
335 if (ret >= 0) {
336 wacom->wacom_wac.bt_high_speed = speed;
337 return 0;
338 }
339 }
340
341 /*
342 * Note that if the raw queries fail, it's not a hard failure
343 * and it is safe to continue
344 */
345 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
346 rep_data[0], ret);
347 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700348 case INTUOS4WL:
349 if (speed == 1)
350 wacom->wacom_wac.bt_features &= ~0x20;
351 else
352 wacom->wacom_wac.bt_features |= 0x20;
353
354 rep_data[0] = 0x03;
355 rep_data[1] = wacom->wacom_wac.bt_features;
356
Przemo Firszt296b7372014-08-06 14:00:38 -0700357 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
358 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700359 if (ret >= 0)
360 wacom->wacom_wac.bt_high_speed = speed;
361 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700362 }
363
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700364 return 0;
365}
366
Jason Gereckefe494bc2012-10-03 17:25:35 -0700367/*
368 * Switch the tablet into its most-capable mode. Wacom tablets are
369 * typically configured to power-up in a mode which sends mouse-like
370 * reports to the OS. To get absolute position, pressure data, etc.
371 * from the tablet, it is necessary to switch the tablet out of this
372 * mode and into one which sends the full range of tablet data.
373 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700374static int wacom_query_tablet_data(struct hid_device *hdev,
375 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700376{
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700377 if (hdev->bus == BUS_BLUETOOTH)
378 return wacom_bt_query_tablet_data(hdev, 1, features);
379
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400380 if (features->type == HID_GENERIC)
381 return wacom_hid_set_device_mode(hdev);
382
Jason Gereckefe494bc2012-10-03 17:25:35 -0700383 if (features->device_type == BTN_TOOL_FINGER) {
384 if (features->type > TABLETPC) {
385 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700386 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700387 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700388 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700389 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700390 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700391 } else if (features->device_type == BTN_TOOL_PEN) {
392 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700393 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700394 }
395 }
396
397 return 0;
398}
399
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700400static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700401 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800402{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700403 struct wacom *wacom = hid_get_drvdata(hdev);
404 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800405
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700406 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800407 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700408 features->x_fuzz = 4;
409 features->y_fuzz = 4;
410 features->pressure_fuzz = 0;
411 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800412
Chris Bagwelld3825d52012-03-25 23:26:11 -0700413 /*
414 * The wireless device HID is basic and layout conflicts with
415 * other tablets (monitor and touch interface can look like pen).
416 * Skip the query for this type and modify defaults based on
417 * interface number.
418 */
419 if (features->type == WIRELESS) {
420 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
421 features->device_type = 0;
422 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700423 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700424 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
425 }
426 }
427
Ping Cheng19635182012-04-29 21:09:18 -0700428 /* only devices that support touch need to retrieve the info */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700429 if (features->type < BAMBOO_PT)
430 return;
Ping Chengec67bbe2009-12-15 00:35:24 -0800431
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700432 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800433}
434
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700435struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700436 struct list_head list;
437 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700438 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700439 struct wacom_shared shared;
440};
441
442static LIST_HEAD(wacom_udev_list);
443static DEFINE_MUTEX(wacom_udev_list_lock);
444
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700445static bool wacom_are_sibling(struct hid_device *hdev,
446 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700447{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700448 struct wacom *wacom = hid_get_drvdata(hdev);
449 struct wacom_features *features = &wacom->wacom_wac.features;
450 int vid = features->oVid;
451 int pid = features->oPid;
452 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700453
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700454 if (vid == 0 && pid == 0) {
455 vid = hdev->vendor;
456 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700457 }
458
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700459 if (vid != sibling->vendor || pid != sibling->product)
460 return false;
461
462 /* Compare the physical path. */
463 n1 = strrchr(hdev->phys, '.') - hdev->phys;
464 n2 = strrchr(sibling->phys, '.') - sibling->phys;
465 if (n1 != n2 || n1 <= 0 || n2 <= 0)
466 return false;
467
468 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700469}
470
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700471static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700472{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700473 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700474
475 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700476 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700477 kref_get(&data->kref);
478 return data;
479 }
480 }
481
482 return NULL;
483}
484
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700485static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700486{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700487 struct wacom *wacom = hid_get_drvdata(hdev);
488 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
489 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700490 int retval = 0;
491
492 mutex_lock(&wacom_udev_list_lock);
493
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700494 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700495 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700496 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700497 if (!data) {
498 retval = -ENOMEM;
499 goto out;
500 }
501
502 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700503 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700504 list_add_tail(&data->list, &wacom_udev_list);
505 }
506
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700507 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700508
509out:
510 mutex_unlock(&wacom_udev_list_lock);
511 return retval;
512}
513
514static void wacom_release_shared_data(struct kref *kref)
515{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700516 struct wacom_hdev_data *data =
517 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700518
519 mutex_lock(&wacom_udev_list_lock);
520 list_del(&data->list);
521 mutex_unlock(&wacom_udev_list_lock);
522
523 kfree(data);
524}
525
526static void wacom_remove_shared_data(struct wacom_wac *wacom)
527{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700528 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700529
530 if (wacom->shared) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700531 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
Ping Cheng4492eff2010-03-19 22:18:15 -0700532 kref_put(&data->kref, wacom_release_shared_data);
533 wacom->shared = NULL;
534 }
535}
536
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700537static int wacom_led_control(struct wacom *wacom)
538{
539 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700540 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700541 unsigned char report_id = WAC_CMD_LED_CONTROL;
542 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700543
Ping Cheng912ca212014-09-10 12:41:31 -0700544 if (wacom->wacom_wac.pid) { /* wireless connected */
545 report_id = WAC_CMD_WL_LED_CONTROL;
546 buf_size = 13;
547 }
548 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700549 if (!buf)
550 return -ENOMEM;
551
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700552 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700553 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700554 /*
555 * Touch Ring and crop mark LED luminance may take on
556 * one of four values:
557 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
558 */
559 int ring_led = wacom->led.select[0] & 0x03;
560 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
561 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700562 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700563
Ping Cheng912ca212014-09-10 12:41:31 -0700564 buf[0] = report_id;
565 if (wacom->wacom_wac.pid) {
566 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
567 buf, buf_size, WAC_CMD_RETRIES);
568 buf[0] = report_id;
569 buf[4] = led_bits;
570 } else
571 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700572 }
573 else {
574 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700575
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700576 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
577 wacom->wacom_wac.features.type == WACOM_24HD)
578 led |= (wacom->led.select[1] << 4) | 0x40;
579
Ping Cheng912ca212014-09-10 12:41:31 -0700580 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700581 buf[1] = led;
582 buf[2] = wacom->led.llv;
583 buf[3] = wacom->led.hlv;
584 buf[4] = wacom->led.img_lum;
585 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700586
Ping Cheng912ca212014-09-10 12:41:31 -0700587 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700588 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700589 kfree(buf);
590
591 return retval;
592}
593
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700594static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
595 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700596{
597 unsigned char *buf;
598 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700599 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700600
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700601 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700602 if (!buf)
603 return -ENOMEM;
604
605 /* Send 'start' command */
606 buf[0] = WAC_CMD_ICON_START;
607 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700608 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
609 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700610 if (retval < 0)
611 goto out;
612
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700613 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700614 buf[1] = button_id & 0x07;
615 for (i = 0; i < 4; i++) {
616 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700617 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700618
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700619 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700620 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700621 if (retval < 0)
622 break;
623 }
624
625 /* Send 'stop' */
626 buf[0] = WAC_CMD_ICON_START;
627 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700628 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
629 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700630
631out:
632 kfree(buf);
633 return retval;
634}
635
Ping Cheng09e7d942011-10-04 23:51:14 -0700636static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700637 const char *buf, size_t count)
638{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700639 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700640 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700641 unsigned int id;
642 int err;
643
644 err = kstrtouint(buf, 10, &id);
645 if (err)
646 return err;
647
648 mutex_lock(&wacom->lock);
649
Ping Cheng09e7d942011-10-04 23:51:14 -0700650 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700651 err = wacom_led_control(wacom);
652
653 mutex_unlock(&wacom->lock);
654
655 return err < 0 ? err : count;
656}
657
Ping Cheng09e7d942011-10-04 23:51:14 -0700658#define DEVICE_LED_SELECT_ATTR(SET_ID) \
659static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
660 struct device_attribute *attr, const char *buf, size_t count) \
661{ \
662 return wacom_led_select_store(dev, SET_ID, buf, count); \
663} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700664static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
665 struct device_attribute *attr, char *buf) \
666{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700667 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700668 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700669 return scnprintf(buf, PAGE_SIZE, "%d\n", \
670 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700671} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700672static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700673 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700674 wacom_led##SET_ID##_select_store)
675
676DEVICE_LED_SELECT_ATTR(0);
677DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700678
679static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
680 const char *buf, size_t count)
681{
682 unsigned int value;
683 int err;
684
685 err = kstrtouint(buf, 10, &value);
686 if (err)
687 return err;
688
689 mutex_lock(&wacom->lock);
690
691 *dest = value & 0x7f;
692 err = wacom_led_control(wacom);
693
694 mutex_unlock(&wacom->lock);
695
696 return err < 0 ? err : count;
697}
698
699#define DEVICE_LUMINANCE_ATTR(name, field) \
700static ssize_t wacom_##name##_luminance_store(struct device *dev, \
701 struct device_attribute *attr, const char *buf, size_t count) \
702{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700703 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700704 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700705 \
706 return wacom_luminance_store(wacom, &wacom->led.field, \
707 buf, count); \
708} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700709static ssize_t wacom_##name##_luminance_show(struct device *dev, \
710 struct device_attribute *attr, char *buf) \
711{ \
712 struct wacom *wacom = dev_get_drvdata(dev); \
713 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
714} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700715static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700716 wacom_##name##_luminance_show, \
717 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700718
719DEVICE_LUMINANCE_ATTR(status0, llv);
720DEVICE_LUMINANCE_ATTR(status1, hlv);
721DEVICE_LUMINANCE_ATTR(buttons, img_lum);
722
723static ssize_t wacom_button_image_store(struct device *dev, int button_id,
724 const char *buf, size_t count)
725{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700726 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700727 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700728 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700729 unsigned len;
730 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700731
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700732 if (hdev->bus == BUS_BLUETOOTH) {
733 len = 256;
734 xfer_id = WAC_CMD_ICON_BT_XFER;
735 } else {
736 len = 1024;
737 xfer_id = WAC_CMD_ICON_XFER;
738 }
739
740 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700741 return -EINVAL;
742
743 mutex_lock(&wacom->lock);
744
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700745 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700746
747 mutex_unlock(&wacom->lock);
748
749 return err < 0 ? err : count;
750}
751
752#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
753static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
754 struct device_attribute *attr, const char *buf, size_t count) \
755{ \
756 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
757} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700758static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700759 NULL, wacom_btnimg##BUTTON_ID##_store)
760
761DEVICE_BTNIMG_ATTR(0);
762DEVICE_BTNIMG_ATTR(1);
763DEVICE_BTNIMG_ATTR(2);
764DEVICE_BTNIMG_ATTR(3);
765DEVICE_BTNIMG_ATTR(4);
766DEVICE_BTNIMG_ATTR(5);
767DEVICE_BTNIMG_ATTR(6);
768DEVICE_BTNIMG_ATTR(7);
769
Ping Cheng09e7d942011-10-04 23:51:14 -0700770static struct attribute *cintiq_led_attrs[] = {
771 &dev_attr_status_led0_select.attr,
772 &dev_attr_status_led1_select.attr,
773 NULL
774};
775
776static struct attribute_group cintiq_led_attr_group = {
777 .name = "wacom_led",
778 .attrs = cintiq_led_attrs,
779};
780
781static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700782 &dev_attr_status0_luminance.attr,
783 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700784 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700785 &dev_attr_buttons_luminance.attr,
786 &dev_attr_button0_rawimg.attr,
787 &dev_attr_button1_rawimg.attr,
788 &dev_attr_button2_rawimg.attr,
789 &dev_attr_button3_rawimg.attr,
790 &dev_attr_button4_rawimg.attr,
791 &dev_attr_button5_rawimg.attr,
792 &dev_attr_button6_rawimg.attr,
793 &dev_attr_button7_rawimg.attr,
794 NULL
795};
796
Ping Cheng09e7d942011-10-04 23:51:14 -0700797static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700798 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700799 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700800};
801
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700802static struct attribute *intuos5_led_attrs[] = {
803 &dev_attr_status0_luminance.attr,
804 &dev_attr_status_led0_select.attr,
805 NULL
806};
807
808static struct attribute_group intuos5_led_attr_group = {
809 .name = "wacom_led",
810 .attrs = intuos5_led_attrs,
811};
812
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700813static int wacom_initialize_leds(struct wacom *wacom)
814{
815 int error;
816
Ping Cheng09e7d942011-10-04 23:51:14 -0700817 /* Initialize default values */
818 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700819 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700820 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700821 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700822 case INTUOS4L:
823 wacom->led.select[0] = 0;
824 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700825 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700826 wacom->led.hlv = 20;
827 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700828 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700829 &intuos4_led_attr_group);
830 break;
831
Jason Gerecke246835f2011-12-12 00:12:04 -0800832 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700833 case WACOM_21UX2:
834 wacom->led.select[0] = 0;
835 wacom->led.select[1] = 0;
836 wacom->led.llv = 0;
837 wacom->led.hlv = 0;
838 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700839
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700840 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700841 &cintiq_led_attr_group);
842 break;
843
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700844 case INTUOS5S:
845 case INTUOS5:
846 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700847 case INTUOSPS:
848 case INTUOSPM:
849 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700850 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
851 wacom->led.select[0] = 0;
852 wacom->led.select[1] = 0;
853 wacom->led.llv = 32;
854 wacom->led.hlv = 0;
855 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700856
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700857 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700858 &intuos5_led_attr_group);
859 } else
860 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700861 break;
862
Ping Cheng09e7d942011-10-04 23:51:14 -0700863 default:
864 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700865 }
866
Ping Cheng09e7d942011-10-04 23:51:14 -0700867 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700868 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700869 "cannot create sysfs group err: %d\n", error);
870 return error;
871 }
872 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700873 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700874
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700875 return 0;
876}
877
878static void wacom_destroy_leds(struct wacom *wacom)
879{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700880 if (!wacom->led_initialized)
881 return;
882
883 wacom->led_initialized = false;
884
Ping Cheng09e7d942011-10-04 23:51:14 -0700885 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700886 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700887 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700888 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700889 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700890 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700891 &intuos4_led_attr_group);
892 break;
893
Jason Gerecke246835f2011-12-12 00:12:04 -0800894 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700895 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700896 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700897 &cintiq_led_attr_group);
898 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700899
900 case INTUOS5S:
901 case INTUOS5:
902 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700903 case INTUOSPS:
904 case INTUOSPM:
905 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700906 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700907 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700908 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700909 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700910 }
911}
912
Chris Bagwella1d552c2012-03-25 23:26:30 -0700913static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700914 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700915 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700916 POWER_SUPPLY_PROP_CAPACITY
917};
918
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700919static enum power_supply_property wacom_ac_props[] = {
920 POWER_SUPPLY_PROP_PRESENT,
921 POWER_SUPPLY_PROP_ONLINE,
922 POWER_SUPPLY_PROP_SCOPE,
923};
924
Chris Bagwella1d552c2012-03-25 23:26:30 -0700925static int wacom_battery_get_property(struct power_supply *psy,
926 enum power_supply_property psp,
927 union power_supply_propval *val)
928{
929 struct wacom *wacom = container_of(psy, struct wacom, battery);
930 int ret = 0;
931
932 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700933 case POWER_SUPPLY_PROP_SCOPE:
934 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
935 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700936 case POWER_SUPPLY_PROP_CAPACITY:
937 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700938 wacom->wacom_wac.battery_capacity;
939 break;
940 case POWER_SUPPLY_PROP_STATUS:
941 if (wacom->wacom_wac.bat_charging)
942 val->intval = POWER_SUPPLY_STATUS_CHARGING;
943 else if (wacom->wacom_wac.battery_capacity == 100 &&
944 wacom->wacom_wac.ps_connected)
945 val->intval = POWER_SUPPLY_STATUS_FULL;
946 else
947 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700948 break;
949 default:
950 ret = -EINVAL;
951 break;
952 }
953
954 return ret;
955}
956
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700957static int wacom_ac_get_property(struct power_supply *psy,
958 enum power_supply_property psp,
959 union power_supply_propval *val)
960{
961 struct wacom *wacom = container_of(psy, struct wacom, ac);
962 int ret = 0;
963
964 switch (psp) {
965 case POWER_SUPPLY_PROP_PRESENT:
966 /* fall through */
967 case POWER_SUPPLY_PROP_ONLINE:
968 val->intval = wacom->wacom_wac.ps_connected;
969 break;
970 case POWER_SUPPLY_PROP_SCOPE:
971 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
972 break;
973 default:
974 ret = -EINVAL;
975 break;
976 }
977 return ret;
978}
979
Chris Bagwella1d552c2012-03-25 23:26:30 -0700980static int wacom_initialize_battery(struct wacom *wacom)
981{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700982 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700983 int error;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700984 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700985
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700986 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700987 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700988
Chris Bagwella1d552c2012-03-25 23:26:30 -0700989 wacom->battery.properties = wacom_battery_props;
990 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
991 wacom->battery.get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700992 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
993 wacom->battery.name = wacom->wacom_wac.bat_name;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700994 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
995 wacom->battery.use_for_apm = 0;
996
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700997 wacom->ac.properties = wacom_ac_props;
998 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
999 wacom->ac.get_property = wacom_ac_get_property;
1000 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1001 wacom->ac.name = wacom->wacom_wac.ac_name;
1002 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
1003 wacom->ac.use_for_apm = 0;
1004
Benjamin Tissoiresdd3181a2014-07-24 13:01:40 -07001005 error = power_supply_register(&wacom->hdev->dev,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001006 &wacom->battery);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001007 if (error)
1008 return error;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001009
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001010 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
1011
1012 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
1013 if (error) {
1014 power_supply_unregister(&wacom->battery);
1015 return error;
1016 }
1017
1018 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001019 }
1020
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001021 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001022}
1023
1024static void wacom_destroy_battery(struct wacom *wacom)
1025{
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001026 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1027 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -07001028 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001029 wacom->battery.dev = NULL;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001030 power_supply_unregister(&wacom->ac);
1031 wacom->ac.dev = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001032 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001033}
1034
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001035static ssize_t wacom_show_speed(struct device *dev,
1036 struct device_attribute
1037 *attr, char *buf)
1038{
1039 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1040 struct wacom *wacom = hid_get_drvdata(hdev);
1041
1042 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1043}
1044
1045static ssize_t wacom_store_speed(struct device *dev,
1046 struct device_attribute *attr,
1047 const char *buf, size_t count)
1048{
1049 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1050 struct wacom *wacom = hid_get_drvdata(hdev);
1051 u8 new_speed;
1052
1053 if (kstrtou8(buf, 0, &new_speed))
1054 return -EINVAL;
1055
1056 if (new_speed != 0 && new_speed != 1)
1057 return -EINVAL;
1058
1059 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1060
1061 return count;
1062}
1063
Ping Chenge0984bc2014-09-10 12:40:05 -07001064static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001065 wacom_show_speed, wacom_store_speed);
1066
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001067static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001068{
1069 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001070 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001071 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001072
1073 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001074 if (!input_dev)
1075 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001076
1077 input_dev->name = wacom_wac->name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001078 input_dev->phys = hdev->phys;
1079 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001080 input_dev->open = wacom_open;
1081 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001082 input_dev->uniq = hdev->uniq;
1083 input_dev->id.bustype = hdev->bus;
1084 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001085 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001086 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001087 input_set_drvdata(input_dev, wacom);
1088
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001089 return input_dev;
1090}
1091
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001092static void wacom_free_inputs(struct wacom *wacom)
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001093{
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001094 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1095
1096 if (wacom_wac->input)
1097 input_free_device(wacom_wac->input);
1098 if (wacom_wac->pad_input)
1099 input_free_device(wacom_wac->pad_input);
1100 wacom_wac->input = NULL;
1101 wacom_wac->pad_input = NULL;
1102}
1103
1104static int wacom_allocate_inputs(struct wacom *wacom)
1105{
1106 struct input_dev *input_dev, *pad_input_dev;
1107 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1108
1109 input_dev = wacom_allocate_input(wacom);
1110 pad_input_dev = wacom_allocate_input(wacom);
1111 if (!input_dev || !pad_input_dev) {
1112 wacom_free_inputs(wacom);
1113 return -ENOMEM;
1114 }
1115
1116 wacom_wac->input = input_dev;
1117 wacom_wac->pad_input = pad_input_dev;
1118 wacom_wac->pad_input->name = wacom_wac->pad_name;
1119
1120 return 0;
1121}
1122
1123static void wacom_clean_inputs(struct wacom *wacom)
1124{
1125 if (wacom->wacom_wac.input) {
1126 if (wacom->wacom_wac.input_registered)
1127 input_unregister_device(wacom->wacom_wac.input);
1128 else
1129 input_free_device(wacom->wacom_wac.input);
1130 }
1131 if (wacom->wacom_wac.pad_input) {
1132 if (wacom->wacom_wac.input_registered)
1133 input_unregister_device(wacom->wacom_wac.pad_input);
1134 else
1135 input_free_device(wacom->wacom_wac.pad_input);
1136 }
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001137 wacom->wacom_wac.input = NULL;
1138 wacom->wacom_wac.pad_input = NULL;
Ping Cheng912ca212014-09-10 12:41:31 -07001139 wacom_destroy_leds(wacom);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001140}
1141
1142static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001143{
1144 struct input_dev *input_dev, *pad_input_dev;
1145 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1146 int error;
1147
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001148 input_dev = wacom_wac->input;
1149 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001150
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001151 if (!input_dev || !pad_input_dev)
1152 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001153
Ping Cheng19635182012-04-29 21:09:18 -07001154 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1155 if (error)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001156 return error;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001157
1158 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001159 if (error)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001160 return error;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001161
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001162 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1163 if (error) {
1164 /* no pad in use on this interface */
1165 input_free_device(pad_input_dev);
1166 wacom_wac->pad_input = NULL;
1167 pad_input_dev = NULL;
1168 } else {
1169 error = input_register_device(pad_input_dev);
1170 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001171 goto fail_register_pad_input;
Ping Cheng912ca212014-09-10 12:41:31 -07001172
1173 error = wacom_initialize_leds(wacom);
1174 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001175 goto fail_leds;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001176 }
1177
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001178 wacom_wac->input_registered = true;
1179
Ping Cheng19635182012-04-29 21:09:18 -07001180 return 0;
1181
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001182fail_leds:
Ping Cheng912ca212014-09-10 12:41:31 -07001183 input_unregister_device(pad_input_dev);
1184 pad_input_dev = NULL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001185fail_register_pad_input:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001186 input_unregister_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001187 wacom_wac->input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001188 return error;
1189}
1190
Chris Bagwell16bf2882012-03-25 23:26:20 -07001191static void wacom_wireless_work(struct work_struct *work)
1192{
1193 struct wacom *wacom = container_of(work, struct wacom, work);
1194 struct usb_device *usbdev = wacom->usbdev;
1195 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001196 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001197 struct wacom *wacom1, *wacom2;
1198 struct wacom_wac *wacom_wac1, *wacom_wac2;
1199 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001200
1201 /*
1202 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001203 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001204 */
1205
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001206 wacom_destroy_battery(wacom);
1207
Chris Bagwell16bf2882012-03-25 23:26:20 -07001208 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001209 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1210 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001211 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001212 wacom_clean_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001213
1214 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001215 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1216 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001217 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001218 wacom_clean_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001219
1220 if (wacom_wac->pid == 0) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001221 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001222 wacom_wac1->shared->type = 0;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001223 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001224 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001225
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001226 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001227 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001228
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001229 while (id->bus) {
1230 if (id->vendor == USB_VENDOR_ID_WACOM &&
1231 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001232 break;
1233 id++;
1234 }
1235
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001236 if (!id->bus) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001237 hid_info(wacom->hdev, "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001238 return;
1239 }
1240
1241 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001242 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001243 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001244 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001245 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1246 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001247 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1248 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001249 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1250 wacom_wac1->shared->type = wacom_wac1->features.type;
Ping Cheng912ca212014-09-10 12:41:31 -07001251 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001252 error = wacom_allocate_inputs(wacom1) ||
1253 wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001254 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001255 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001256
1257 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001258 if (wacom_wac1->features.touch_max ||
1259 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001260 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001261 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001262 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1263 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1264 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1265 if (wacom_wac2->features.touch_max)
1266 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1267 "%s (WL) Finger",wacom_wac2->features.name);
1268 else
1269 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1270 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001271 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1272 "%s (WL) Pad", wacom_wac2->features.name);
Ping Cheng912ca212014-09-10 12:41:31 -07001273 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001274 error = wacom_allocate_inputs(wacom2) ||
1275 wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001276 if (error)
1277 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001278
1279 if (wacom_wac1->features.type == INTUOSHT &&
1280 wacom_wac1->features.touch_max)
1281 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001282 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001283
1284 error = wacom_initialize_battery(wacom);
1285 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001286 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001287 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001288
1289 return;
1290
Ping Cheng57bcfce2013-10-15 23:44:00 -07001291fail:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001292 wacom_clean_inputs(wacom1);
1293 wacom_clean_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001294 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001295}
1296
Ping Cheng401d7d12013-07-28 00:38:54 -07001297/*
1298 * Not all devices report physical dimensions from HID.
1299 * Compute the default from hardcoded logical dimension
1300 * and resolution before driver overwrites them.
1301 */
1302static void wacom_set_default_phy(struct wacom_features *features)
1303{
1304 if (features->x_resolution) {
1305 features->x_phy = (features->x_max * 100) /
1306 features->x_resolution;
1307 features->y_phy = (features->y_max * 100) /
1308 features->y_resolution;
1309 }
1310}
1311
1312static void wacom_calculate_res(struct wacom_features *features)
1313{
1314 features->x_resolution = wacom_calc_hid_res(features->x_max,
1315 features->x_phy,
1316 features->unit,
1317 features->unitExpo);
1318 features->y_resolution = wacom_calc_hid_res(features->y_max,
1319 features->y_phy,
1320 features->unit,
1321 features->unitExpo);
1322}
1323
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001324static int wacom_hid_report_len(struct hid_report *report)
1325{
1326 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1327 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1328}
1329
1330static size_t wacom_compute_pktlen(struct hid_device *hdev)
1331{
1332 struct hid_report_enum *report_enum;
1333 struct hid_report *report;
1334 size_t size = 0;
1335
1336 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1337
1338 list_for_each_entry(report, &report_enum->report_list, list) {
1339 size_t report_size = wacom_hid_report_len(report);
1340 if (report_size > size)
1341 size = report_size;
1342 }
1343
1344 return size;
1345}
1346
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001347static int wacom_probe(struct hid_device *hdev,
1348 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001349{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001350 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001351 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001352 struct wacom *wacom;
1353 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001354 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001355 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001356 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001357
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001358 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001359 return -EINVAL;
1360
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -04001361 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1362
Ping Cheng3bea7332006-07-13 18:01:36 -07001363 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001364 if (!wacom)
1365 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001366
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001367 hid_set_drvdata(hdev, wacom);
1368 wacom->hdev = hdev;
1369
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001370 /* ask for the report descriptor to be loaded by HID */
1371 error = hid_parse(hdev);
1372 if (error) {
1373 hid_err(hdev, "parse failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001374 goto fail_parse;
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001375 }
1376
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001377 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001378 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001379 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001380 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001381 if (features->pktlen > WACOM_PKGLEN_MAX) {
1382 error = -EINVAL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001383 goto fail_pktlen;
Jason Childse33da8a2010-02-17 22:38:31 -08001384 }
1385
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001386 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1387 error = -ENODEV;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001388 goto fail_type;
Jason Childse33da8a2010-02-17 22:38:31 -08001389 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001390
Ping Cheng3bea7332006-07-13 18:01:36 -07001391 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001392 wacom->intf = intf;
1393 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001394 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001395
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001396 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1397 error = wacom_allocate_inputs(wacom);
1398 if (error)
1399 goto fail_allocate_inputs;
1400 }
1401
Ping Cheng401d7d12013-07-28 00:38:54 -07001402 /* set the default size in case we do not get them from hid */
1403 wacom_set_default_phy(features);
1404
Ping Chengf393ee22012-04-29 21:09:17 -07001405 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001406 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -05001407
Jason Gereckeae584ca2012-04-03 15:50:40 -07001408 /*
1409 * Intuos5 has no useful data about its touch interface in its
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001410 * HID descriptor. If this is the touch interface (PacketSize
Jason Gereckeae584ca2012-04-03 15:50:40 -07001411 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1412 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001413 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001414 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001415 features->device_type = BTN_TOOL_FINGER;
Jason Gereckeae584ca2012-04-03 15:50:40 -07001416
Jason Gereckeae584ca2012-04-03 15:50:40 -07001417 features->x_max = 4096;
1418 features->y_max = 4096;
1419 } else {
1420 features->device_type = BTN_TOOL_PEN;
1421 }
1422 }
1423
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001424 /*
1425 * Same thing for Bamboo 3rd gen.
1426 */
1427 if ((features->type == BAMBOO_PT) &&
1428 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1429 (features->device_type == BTN_TOOL_PEN)) {
1430 features->device_type = BTN_TOOL_FINGER;
1431
1432 features->x_max = 4096;
1433 features->y_max = 4096;
1434 }
1435
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001436 if (hdev->bus == BUS_BLUETOOTH)
1437 features->quirks |= WACOM_QUIRK_BATTERY;
1438
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001439 wacom_setup_device_quirks(features);
1440
Ping Cheng401d7d12013-07-28 00:38:54 -07001441 /* set unit to "100th of a mm" for devices not reported by HID */
1442 if (!features->unit) {
1443 features->unit = 0x11;
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001444 features->unitExpo = -3;
Ping Cheng401d7d12013-07-28 00:38:54 -07001445 }
1446 wacom_calculate_res(features);
1447
Ping Cheng49b764a2010-02-20 00:53:49 -08001448 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001449 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1450 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001451
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001452 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001453 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001454 if (features->device_type != BTN_TOOL_FINGER)
1455 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1456 else if (features->touch_max)
1457 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1458 else
1459 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001460
Benjamin Tissoires4451e082014-07-24 13:01:05 -07001461 error = wacom_add_shared_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001462 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001463 goto fail_shared_data;
Ping Cheng49b764a2010-02-20 00:53:49 -08001464 }
1465
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001466 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1467 (features->quirks & WACOM_QUIRK_BATTERY)) {
1468 error = wacom_initialize_battery(wacom);
1469 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001470 goto fail_battery;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001471 }
1472
Chris Bagwelld3825d52012-03-25 23:26:11 -07001473 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001474 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001475 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001476 goto fail_register_inputs;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001477 }
1478
1479 if (hdev->bus == BUS_BLUETOOTH) {
1480 error = device_create_file(&hdev->dev, &dev_attr_speed);
1481 if (error)
1482 hid_warn(hdev,
1483 "can't create sysfs speed attribute err: %d\n",
1484 error);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001485 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001486
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001487 if (features->type == HID_GENERIC)
1488 connect_mask |= HID_CONNECT_DRIVER;
1489
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001490 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001491 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001492 if (error) {
1493 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001494 goto fail_hw_start;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001495 }
1496
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -04001497 /* Note that if query fails it is not a hard failure */
1498 wacom_query_tablet_data(hdev, features);
1499
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001500 if (features->quirks & WACOM_QUIRK_MONITOR)
1501 error = hid_hw_open(hdev);
1502
Ping Cheng961794a2013-12-05 12:54:53 -08001503 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1504 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1505 wacom_wac->shared->touch_input = wacom_wac->input;
1506 }
1507
Ping Cheng3bea7332006-07-13 18:01:36 -07001508 return 0;
1509
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001510fail_hw_start:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001511 if (hdev->bus == BUS_BLUETOOTH)
1512 device_remove_file(&hdev->dev, &dev_attr_speed);
1513fail_register_inputs:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001514 wacom_clean_inputs(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001515 wacom_destroy_battery(wacom);
1516fail_battery:
1517 wacom_remove_shared_data(wacom_wac);
1518fail_shared_data:
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001519 wacom_clean_inputs(wacom);
1520fail_allocate_inputs:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001521fail_type:
1522fail_pktlen:
1523fail_parse:
1524 kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001525 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001526 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001527}
1528
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001529static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001530{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001531 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001532
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001533 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001534
Chris Bagwell16bf2882012-03-25 23:26:20 -07001535 cancel_work_sync(&wacom->work);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001536 wacom_clean_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001537 if (hdev->bus == BUS_BLUETOOTH)
1538 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001539 wacom_destroy_battery(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001540 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001541
1542 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001543 kfree(wacom);
1544}
1545
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001546#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001547static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001548{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001549 struct wacom *wacom = hid_get_drvdata(hdev);
1550 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001551
1552 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001553
1554 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001555 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001556 wacom_led_control(wacom);
1557
Oliver Neukume7224092008-04-15 01:31:57 -04001558 mutex_unlock(&wacom->lock);
1559
1560 return 0;
1561}
1562
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001563static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001564{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001565 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001566}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001567#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001568
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001569static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001570 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001571 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001572 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001573 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001574 .event = wacom_wac_event,
1575 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001576#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001577 .resume = wacom_resume,
1578 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001579#endif
1580 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001581};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001582module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001583
1584MODULE_VERSION(DRIVER_VERSION);
1585MODULE_AUTHOR(DRIVER_AUTHOR);
1586MODULE_DESCRIPTION(DRIVER_DESC);
1587MODULE_LICENSE(DRIVER_LICENSE);