blob: a8b7f16f76fae8f96273d4315e134495e10f5521 [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"
Benjamin Tissoiresf54bc612014-07-24 12:52:00 -070016#include <linux/hid.h>
Ping Cheng3bea7332006-07-13 18:01:36 -070017
Ping Chenga417ea42011-08-16 00:17:56 -070018#define WAC_MSG_RETRIES 5
Ping Cheng3bea7332006-07-13 18:01:36 -070019
Ping Cheng912ca212014-09-10 12:41:31 -070020#define WAC_CMD_WL_LED_CONTROL 0x03
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070021#define WAC_CMD_LED_CONTROL 0x20
22#define WAC_CMD_ICON_START 0x21
23#define WAC_CMD_ICON_XFER 0x23
Benjamin Tissoires849e2f02014-08-06 13:58:25 -070024#define WAC_CMD_ICON_BT_XFER 0x26
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070025#define WAC_CMD_RETRIES 10
26
Ping Chenge0984bc2014-09-10 12:40:05 -070027#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
28#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
29
Ping Chengc64d8832014-09-10 12:41:04 -070030static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
31 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070032{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070033 int retval;
34
35 do {
Ping Chengc64d8832014-09-10 12:41:04 -070036 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070037 HID_REQ_GET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070038 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
39
40 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070041}
42
Przemo Firszt296b7372014-08-06 14:00:38 -070043static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
44 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070045{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070046 int retval;
47
48 do {
Przemo Firszt296b7372014-08-06 14:00:38 -070049 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070050 HID_REQ_SET_REPORT);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070051 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
52
53 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070054}
55
Benjamin Tissoires29b47392014-07-24 12:52:23 -070056static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
57 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -070058{
Benjamin Tissoires29b47392014-07-24 12:52:23 -070059 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -070060
Benjamin Tissoires29b47392014-07-24 12:52:23 -070061 if (size > WACOM_PKGLEN_MAX)
62 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -070063
Benjamin Tissoires29b47392014-07-24 12:52:23 -070064 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -070065
Benjamin Tissoires29b47392014-07-24 12:52:23 -070066 wacom_wac_irq(&wacom->wacom_wac, size);
67
68 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -070069}
70
Ping Cheng3bea7332006-07-13 18:01:36 -070071static int wacom_open(struct input_dev *dev)
72{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040073 struct wacom *wacom = input_get_drvdata(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070074 int retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070075
Oliver Neukume7224092008-04-15 01:31:57 -040076 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070077 retval = hid_hw_open(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040078 mutex_unlock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070079
Dmitry Torokhovf6cd3782010-10-04 21:46:11 -070080 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070081}
82
83static void wacom_close(struct input_dev *dev)
84{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -040085 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -070086
Oliver Neukume7224092008-04-15 01:31:57 -040087 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -070088 hid_hw_close(wacom->hdev);
Oliver Neukume7224092008-04-15 01:31:57 -040089 mutex_unlock(&wacom->lock);
Ping Cheng3bea7332006-07-13 18:01:36 -070090}
91
Chris Bagwell16bf2882012-03-25 23:26:20 -070092/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070093 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -070094 */
95static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -070096 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -070097{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -070098 struct hid_field field = {
99 .logical_maximum = logical_extents,
100 .physical_maximum = physical_extents,
101 .unit = unit,
102 .unit_exponent = exponent,
103 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700104
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700105 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700106}
107
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700108static void wacom_feature_mapping(struct hid_device *hdev,
109 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700110{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700111 struct wacom *wacom = hid_get_drvdata(hdev);
112 struct wacom_features *features = &wacom->wacom_wac.features;
Chris Bagwell41343612011-10-26 22:32:52 -0700113
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700114 switch (usage->hid) {
115 case HID_DG_CONTACTMAX:
116 /* leave touch_max as is if predefined */
117 if (!features->touch_max)
118 features->touch_max = field->value[0];
119 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700120 }
121}
122
Chris Bagwell428f8582011-10-26 22:26:59 -0700123/*
124 * Interface Descriptor of wacom devices can be incomplete and
125 * inconsistent so wacom_features table is used to store stylus
126 * device's packet lengths, various maximum values, and tablet
127 * resolution based on product ID's.
128 *
129 * For devices that contain 2 interfaces, wacom_features table is
130 * inaccurate for the touch interface. Since the Interface Descriptor
131 * for touch interfaces has pretty complete data, this function exists
132 * to query tablet for this missing information instead of hard coding in
133 * an additional table.
134 *
135 * A typical Interface Descriptor for a stylus will contain a
136 * boot mouse application collection that is not of interest and this
137 * function will ignore it.
138 *
139 * It also contains a digitizer application collection that also is not
140 * of interest since any information it contains would be duplicate
141 * of what is in wacom_features. Usually it defines a report of an array
142 * of bytes that could be used as max length of the stylus packet returned.
143 * If it happens to define a Digitizer-Stylus Physical Collection then
144 * the X and Y logical values contain valid data but it is ignored.
145 *
146 * A typical Interface Descriptor for a touch interface will contain a
147 * Digitizer-Finger Physical Collection which will define both logical
148 * X/Y maximum as well as the physical size of tablet. Since touch
149 * interfaces haven't supported pressure or distance, this is enough
150 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700151 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700152 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
153 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700154 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700155static void wacom_usage_mapping(struct hid_device *hdev,
156 struct hid_field *field, struct hid_usage *usage)
157{
158 struct wacom *wacom = hid_get_drvdata(hdev);
159 struct wacom_features *features = &wacom->wacom_wac.features;
160 bool finger = (field->logical == HID_DG_FINGER) ||
161 (field->physical == HID_DG_FINGER);
162 bool pen = (field->logical == HID_DG_STYLUS) ||
163 (field->physical == HID_DG_STYLUS);
164
165 /*
166 * Requiring Stylus Usage will ignore boot mouse
167 * X/Y values and some cases of invalid Digitizer X/Y
168 * values commonly reported.
169 */
170 if (!pen && !finger)
171 return;
172
173 if (finger && !features->touch_max)
174 /* touch device at least supports one touch point */
175 features->touch_max = 1;
176
177 switch (usage->hid) {
178 case HID_GD_X:
179 features->x_max = field->logical_maximum;
180 if (finger) {
181 features->device_type = BTN_TOOL_FINGER;
182 features->x_phy = field->physical_maximum;
183 if (features->type != BAMBOO_PT) {
184 features->unit = field->unit;
185 features->unitExpo = field->unit_exponent;
186 }
187 } else {
188 features->device_type = BTN_TOOL_PEN;
189 }
190 break;
191 case HID_GD_Y:
192 features->y_max = field->logical_maximum;
193 if (finger) {
194 features->y_phy = field->physical_maximum;
195 if (features->type != BAMBOO_PT) {
196 features->unit = field->unit;
197 features->unitExpo = field->unit_exponent;
198 }
199 }
200 break;
201 case HID_DG_TIPPRESSURE:
202 if (pen)
203 features->pressure_max = field->logical_maximum;
204 break;
205 }
206}
207
208static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800209 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500210{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700211 struct hid_report_enum *rep_enum;
212 struct hid_report *hreport;
213 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500214
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700215 /* check features first */
216 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
217 list_for_each_entry(hreport, &rep_enum->report_list, list) {
218 for (i = 0; i < hreport->maxfield; i++) {
219 /* Ignore if report count is out of bounds. */
220 if (hreport->field[i]->report_count < 1)
221 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500222
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700223 for (j = 0; j < hreport->field[i]->maxusage; j++) {
224 wacom_feature_mapping(hdev, hreport->field[i],
225 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500226 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500227 }
228 }
229
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700230 /* now check the input usages */
231 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
232 list_for_each_entry(hreport, &rep_enum->report_list, list) {
233
234 if (!hreport->maxfield)
235 continue;
236
237 for (i = 0; i < hreport->maxfield; i++)
238 for (j = 0; j < hreport->field[i]->maxusage; j++)
239 wacom_usage_mapping(hdev, hreport->field[i],
240 hreport->field[i]->usage + j);
241 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500242}
243
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700244static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
245 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700246{
247 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700248 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700249
Jason Gereckefe494bc2012-10-03 17:25:35 -0700250 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700251 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800252 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700253
Jason Gereckefe494bc2012-10-03 17:25:35 -0700254 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800255 rep_data[0] = report_id;
256 rep_data[1] = mode;
257
Przemo Firszt296b7372014-08-06 14:00:38 -0700258 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
259 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700260 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700261 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700262 rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700263 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700264
265 kfree(rep_data);
266
267 return error < 0 ? error : 0;
268}
269
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700270static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
271 struct wacom_features *features)
272{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700273 struct wacom *wacom = hid_get_drvdata(hdev);
274 int ret;
275 u8 rep_data[2];
276
277 switch (features->type) {
278 case GRAPHIRE_BT:
279 rep_data[0] = 0x03;
280 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700281 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
282 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700283
284 if (ret >= 0) {
285 rep_data[0] = speed == 0 ? 0x05 : 0x06;
286 rep_data[1] = 0x00;
287
288 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700289 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700290
291 if (ret >= 0) {
292 wacom->wacom_wac.bt_high_speed = speed;
293 return 0;
294 }
295 }
296
297 /*
298 * Note that if the raw queries fail, it's not a hard failure
299 * and it is safe to continue
300 */
301 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
302 rep_data[0], ret);
303 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700304 case INTUOS4WL:
305 if (speed == 1)
306 wacom->wacom_wac.bt_features &= ~0x20;
307 else
308 wacom->wacom_wac.bt_features |= 0x20;
309
310 rep_data[0] = 0x03;
311 rep_data[1] = wacom->wacom_wac.bt_features;
312
Przemo Firszt296b7372014-08-06 14:00:38 -0700313 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
314 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700315 if (ret >= 0)
316 wacom->wacom_wac.bt_high_speed = speed;
317 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700318 }
319
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700320 return 0;
321}
322
Jason Gereckefe494bc2012-10-03 17:25:35 -0700323/*
324 * Switch the tablet into its most-capable mode. Wacom tablets are
325 * typically configured to power-up in a mode which sends mouse-like
326 * reports to the OS. To get absolute position, pressure data, etc.
327 * from the tablet, it is necessary to switch the tablet out of this
328 * mode and into one which sends the full range of tablet data.
329 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700330static int wacom_query_tablet_data(struct hid_device *hdev,
331 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700332{
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700333 if (hdev->bus == BUS_BLUETOOTH)
334 return wacom_bt_query_tablet_data(hdev, 1, features);
335
Jason Gereckefe494bc2012-10-03 17:25:35 -0700336 if (features->device_type == BTN_TOOL_FINGER) {
337 if (features->type > TABLETPC) {
338 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700339 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700340 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700341 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700342 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700343 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700344 } else if (features->device_type == BTN_TOOL_PEN) {
345 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700346 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700347 }
348 }
349
350 return 0;
351}
352
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700353static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700354 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800355{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700356 struct wacom *wacom = hid_get_drvdata(hdev);
357 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800358
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700359 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800360 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700361 features->x_fuzz = 4;
362 features->y_fuzz = 4;
363 features->pressure_fuzz = 0;
364 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800365
Chris Bagwelld3825d52012-03-25 23:26:11 -0700366 /*
367 * The wireless device HID is basic and layout conflicts with
368 * other tablets (monitor and touch interface can look like pen).
369 * Skip the query for this type and modify defaults based on
370 * interface number.
371 */
372 if (features->type == WIRELESS) {
373 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
374 features->device_type = 0;
375 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700376 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700377 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
378 }
379 }
380
Ping Cheng19635182012-04-29 21:09:18 -0700381 /* only devices that support touch need to retrieve the info */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700382 if (features->type < BAMBOO_PT)
383 return;
Ping Chengec67bbe2009-12-15 00:35:24 -0800384
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700385 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800386}
387
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700388struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700389 struct list_head list;
390 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700391 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700392 struct wacom_shared shared;
393};
394
395static LIST_HEAD(wacom_udev_list);
396static DEFINE_MUTEX(wacom_udev_list_lock);
397
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700398static bool wacom_are_sibling(struct hid_device *hdev,
399 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700400{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700401 struct wacom *wacom = hid_get_drvdata(hdev);
402 struct wacom_features *features = &wacom->wacom_wac.features;
403 int vid = features->oVid;
404 int pid = features->oPid;
405 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700406
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700407 if (vid == 0 && pid == 0) {
408 vid = hdev->vendor;
409 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700410 }
411
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700412 if (vid != sibling->vendor || pid != sibling->product)
413 return false;
414
415 /* Compare the physical path. */
416 n1 = strrchr(hdev->phys, '.') - hdev->phys;
417 n2 = strrchr(sibling->phys, '.') - sibling->phys;
418 if (n1 != n2 || n1 <= 0 || n2 <= 0)
419 return false;
420
421 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700422}
423
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700424static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700425{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700426 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700427
428 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700429 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700430 kref_get(&data->kref);
431 return data;
432 }
433 }
434
435 return NULL;
436}
437
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700438static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700439{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700440 struct wacom *wacom = hid_get_drvdata(hdev);
441 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
442 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700443 int retval = 0;
444
445 mutex_lock(&wacom_udev_list_lock);
446
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700447 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700448 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700449 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700450 if (!data) {
451 retval = -ENOMEM;
452 goto out;
453 }
454
455 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700456 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700457 list_add_tail(&data->list, &wacom_udev_list);
458 }
459
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700460 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700461
462out:
463 mutex_unlock(&wacom_udev_list_lock);
464 return retval;
465}
466
467static void wacom_release_shared_data(struct kref *kref)
468{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700469 struct wacom_hdev_data *data =
470 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700471
472 mutex_lock(&wacom_udev_list_lock);
473 list_del(&data->list);
474 mutex_unlock(&wacom_udev_list_lock);
475
476 kfree(data);
477}
478
479static void wacom_remove_shared_data(struct wacom_wac *wacom)
480{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700481 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700482
483 if (wacom->shared) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700484 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
Ping Cheng4492eff2010-03-19 22:18:15 -0700485 kref_put(&data->kref, wacom_release_shared_data);
486 wacom->shared = NULL;
487 }
488}
489
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700490static int wacom_led_control(struct wacom *wacom)
491{
492 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700493 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700494 unsigned char report_id = WAC_CMD_LED_CONTROL;
495 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700496
Ping Cheng912ca212014-09-10 12:41:31 -0700497 if (wacom->wacom_wac.pid) { /* wireless connected */
498 report_id = WAC_CMD_WL_LED_CONTROL;
499 buf_size = 13;
500 }
501 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700502 if (!buf)
503 return -ENOMEM;
504
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700505 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700506 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700507 /*
508 * Touch Ring and crop mark LED luminance may take on
509 * one of four values:
510 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
511 */
512 int ring_led = wacom->led.select[0] & 0x03;
513 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
514 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700515 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700516
Ping Cheng912ca212014-09-10 12:41:31 -0700517 buf[0] = report_id;
518 if (wacom->wacom_wac.pid) {
519 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
520 buf, buf_size, WAC_CMD_RETRIES);
521 buf[0] = report_id;
522 buf[4] = led_bits;
523 } else
524 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700525 }
526 else {
527 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700528
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700529 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
530 wacom->wacom_wac.features.type == WACOM_24HD)
531 led |= (wacom->led.select[1] << 4) | 0x40;
532
Ping Cheng912ca212014-09-10 12:41:31 -0700533 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700534 buf[1] = led;
535 buf[2] = wacom->led.llv;
536 buf[3] = wacom->led.hlv;
537 buf[4] = wacom->led.img_lum;
538 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700539
Ping Cheng912ca212014-09-10 12:41:31 -0700540 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700541 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700542 kfree(buf);
543
544 return retval;
545}
546
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700547static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
548 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700549{
550 unsigned char *buf;
551 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700552 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700553
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700554 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700555 if (!buf)
556 return -ENOMEM;
557
558 /* Send 'start' command */
559 buf[0] = WAC_CMD_ICON_START;
560 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700561 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
562 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700563 if (retval < 0)
564 goto out;
565
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700566 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700567 buf[1] = button_id & 0x07;
568 for (i = 0; i < 4; i++) {
569 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700570 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700571
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700572 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700573 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700574 if (retval < 0)
575 break;
576 }
577
578 /* Send 'stop' */
579 buf[0] = WAC_CMD_ICON_START;
580 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700581 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
582 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700583
584out:
585 kfree(buf);
586 return retval;
587}
588
Ping Cheng09e7d942011-10-04 23:51:14 -0700589static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700590 const char *buf, size_t count)
591{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700592 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700593 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700594 unsigned int id;
595 int err;
596
597 err = kstrtouint(buf, 10, &id);
598 if (err)
599 return err;
600
601 mutex_lock(&wacom->lock);
602
Ping Cheng09e7d942011-10-04 23:51:14 -0700603 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700604 err = wacom_led_control(wacom);
605
606 mutex_unlock(&wacom->lock);
607
608 return err < 0 ? err : count;
609}
610
Ping Cheng09e7d942011-10-04 23:51:14 -0700611#define DEVICE_LED_SELECT_ATTR(SET_ID) \
612static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
613 struct device_attribute *attr, const char *buf, size_t count) \
614{ \
615 return wacom_led_select_store(dev, SET_ID, buf, count); \
616} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700617static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
618 struct device_attribute *attr, char *buf) \
619{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700620 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700621 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700622 return scnprintf(buf, PAGE_SIZE, "%d\n", \
623 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700624} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700625static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700626 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700627 wacom_led##SET_ID##_select_store)
628
629DEVICE_LED_SELECT_ATTR(0);
630DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700631
632static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
633 const char *buf, size_t count)
634{
635 unsigned int value;
636 int err;
637
638 err = kstrtouint(buf, 10, &value);
639 if (err)
640 return err;
641
642 mutex_lock(&wacom->lock);
643
644 *dest = value & 0x7f;
645 err = wacom_led_control(wacom);
646
647 mutex_unlock(&wacom->lock);
648
649 return err < 0 ? err : count;
650}
651
652#define DEVICE_LUMINANCE_ATTR(name, field) \
653static ssize_t wacom_##name##_luminance_store(struct device *dev, \
654 struct device_attribute *attr, const char *buf, size_t count) \
655{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700656 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700657 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700658 \
659 return wacom_luminance_store(wacom, &wacom->led.field, \
660 buf, count); \
661} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700662static ssize_t wacom_##name##_luminance_show(struct device *dev, \
663 struct device_attribute *attr, char *buf) \
664{ \
665 struct wacom *wacom = dev_get_drvdata(dev); \
666 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
667} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700668static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700669 wacom_##name##_luminance_show, \
670 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700671
672DEVICE_LUMINANCE_ATTR(status0, llv);
673DEVICE_LUMINANCE_ATTR(status1, hlv);
674DEVICE_LUMINANCE_ATTR(buttons, img_lum);
675
676static ssize_t wacom_button_image_store(struct device *dev, int button_id,
677 const char *buf, size_t count)
678{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700679 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700680 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700681 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700682 unsigned len;
683 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700684
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700685 if (hdev->bus == BUS_BLUETOOTH) {
686 len = 256;
687 xfer_id = WAC_CMD_ICON_BT_XFER;
688 } else {
689 len = 1024;
690 xfer_id = WAC_CMD_ICON_XFER;
691 }
692
693 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700694 return -EINVAL;
695
696 mutex_lock(&wacom->lock);
697
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700698 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700699
700 mutex_unlock(&wacom->lock);
701
702 return err < 0 ? err : count;
703}
704
705#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
706static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
707 struct device_attribute *attr, const char *buf, size_t count) \
708{ \
709 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
710} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700711static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700712 NULL, wacom_btnimg##BUTTON_ID##_store)
713
714DEVICE_BTNIMG_ATTR(0);
715DEVICE_BTNIMG_ATTR(1);
716DEVICE_BTNIMG_ATTR(2);
717DEVICE_BTNIMG_ATTR(3);
718DEVICE_BTNIMG_ATTR(4);
719DEVICE_BTNIMG_ATTR(5);
720DEVICE_BTNIMG_ATTR(6);
721DEVICE_BTNIMG_ATTR(7);
722
Ping Cheng09e7d942011-10-04 23:51:14 -0700723static struct attribute *cintiq_led_attrs[] = {
724 &dev_attr_status_led0_select.attr,
725 &dev_attr_status_led1_select.attr,
726 NULL
727};
728
729static struct attribute_group cintiq_led_attr_group = {
730 .name = "wacom_led",
731 .attrs = cintiq_led_attrs,
732};
733
734static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700735 &dev_attr_status0_luminance.attr,
736 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700737 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700738 &dev_attr_buttons_luminance.attr,
739 &dev_attr_button0_rawimg.attr,
740 &dev_attr_button1_rawimg.attr,
741 &dev_attr_button2_rawimg.attr,
742 &dev_attr_button3_rawimg.attr,
743 &dev_attr_button4_rawimg.attr,
744 &dev_attr_button5_rawimg.attr,
745 &dev_attr_button6_rawimg.attr,
746 &dev_attr_button7_rawimg.attr,
747 NULL
748};
749
Ping Cheng09e7d942011-10-04 23:51:14 -0700750static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700751 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700752 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700753};
754
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700755static struct attribute *intuos5_led_attrs[] = {
756 &dev_attr_status0_luminance.attr,
757 &dev_attr_status_led0_select.attr,
758 NULL
759};
760
761static struct attribute_group intuos5_led_attr_group = {
762 .name = "wacom_led",
763 .attrs = intuos5_led_attrs,
764};
765
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700766static int wacom_initialize_leds(struct wacom *wacom)
767{
768 int error;
769
Ping Cheng09e7d942011-10-04 23:51:14 -0700770 /* Initialize default values */
771 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700772 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700773 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700774 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700775 case INTUOS4L:
776 wacom->led.select[0] = 0;
777 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700778 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700779 wacom->led.hlv = 20;
780 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700781 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700782 &intuos4_led_attr_group);
783 break;
784
Jason Gerecke246835f2011-12-12 00:12:04 -0800785 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700786 case WACOM_21UX2:
787 wacom->led.select[0] = 0;
788 wacom->led.select[1] = 0;
789 wacom->led.llv = 0;
790 wacom->led.hlv = 0;
791 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700792
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700793 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700794 &cintiq_led_attr_group);
795 break;
796
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700797 case INTUOS5S:
798 case INTUOS5:
799 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700800 case INTUOSPS:
801 case INTUOSPM:
802 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700803 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
804 wacom->led.select[0] = 0;
805 wacom->led.select[1] = 0;
806 wacom->led.llv = 32;
807 wacom->led.hlv = 0;
808 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700809
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700810 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700811 &intuos5_led_attr_group);
812 } else
813 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700814 break;
815
Ping Cheng09e7d942011-10-04 23:51:14 -0700816 default:
817 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700818 }
819
Ping Cheng09e7d942011-10-04 23:51:14 -0700820 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700821 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700822 "cannot create sysfs group err: %d\n", error);
823 return error;
824 }
825 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700826 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700827
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700828 return 0;
829}
830
831static void wacom_destroy_leds(struct wacom *wacom)
832{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700833 if (!wacom->led_initialized)
834 return;
835
836 wacom->led_initialized = false;
837
Ping Cheng09e7d942011-10-04 23:51:14 -0700838 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700839 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700840 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700841 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700842 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700843 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700844 &intuos4_led_attr_group);
845 break;
846
Jason Gerecke246835f2011-12-12 00:12:04 -0800847 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700848 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700849 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700850 &cintiq_led_attr_group);
851 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700852
853 case INTUOS5S:
854 case INTUOS5:
855 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700856 case INTUOSPS:
857 case INTUOSPM:
858 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700859 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700860 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700861 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700862 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700863 }
864}
865
Chris Bagwella1d552c2012-03-25 23:26:30 -0700866static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700867 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700868 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700869 POWER_SUPPLY_PROP_CAPACITY
870};
871
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700872static enum power_supply_property wacom_ac_props[] = {
873 POWER_SUPPLY_PROP_PRESENT,
874 POWER_SUPPLY_PROP_ONLINE,
875 POWER_SUPPLY_PROP_SCOPE,
876};
877
Chris Bagwella1d552c2012-03-25 23:26:30 -0700878static int wacom_battery_get_property(struct power_supply *psy,
879 enum power_supply_property psp,
880 union power_supply_propval *val)
881{
882 struct wacom *wacom = container_of(psy, struct wacom, battery);
883 int ret = 0;
884
885 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700886 case POWER_SUPPLY_PROP_SCOPE:
887 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
888 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700889 case POWER_SUPPLY_PROP_CAPACITY:
890 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700891 wacom->wacom_wac.battery_capacity;
892 break;
893 case POWER_SUPPLY_PROP_STATUS:
894 if (wacom->wacom_wac.bat_charging)
895 val->intval = POWER_SUPPLY_STATUS_CHARGING;
896 else if (wacom->wacom_wac.battery_capacity == 100 &&
897 wacom->wacom_wac.ps_connected)
898 val->intval = POWER_SUPPLY_STATUS_FULL;
899 else
900 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700901 break;
902 default:
903 ret = -EINVAL;
904 break;
905 }
906
907 return ret;
908}
909
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700910static int wacom_ac_get_property(struct power_supply *psy,
911 enum power_supply_property psp,
912 union power_supply_propval *val)
913{
914 struct wacom *wacom = container_of(psy, struct wacom, ac);
915 int ret = 0;
916
917 switch (psp) {
918 case POWER_SUPPLY_PROP_PRESENT:
919 /* fall through */
920 case POWER_SUPPLY_PROP_ONLINE:
921 val->intval = wacom->wacom_wac.ps_connected;
922 break;
923 case POWER_SUPPLY_PROP_SCOPE:
924 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
925 break;
926 default:
927 ret = -EINVAL;
928 break;
929 }
930 return ret;
931}
932
Chris Bagwella1d552c2012-03-25 23:26:30 -0700933static int wacom_initialize_battery(struct wacom *wacom)
934{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700935 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700936 int error;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700937 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700938
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700939 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700940 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700941
Chris Bagwella1d552c2012-03-25 23:26:30 -0700942 wacom->battery.properties = wacom_battery_props;
943 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
944 wacom->battery.get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700945 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
946 wacom->battery.name = wacom->wacom_wac.bat_name;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700947 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
948 wacom->battery.use_for_apm = 0;
949
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700950 wacom->ac.properties = wacom_ac_props;
951 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
952 wacom->ac.get_property = wacom_ac_get_property;
953 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
954 wacom->ac.name = wacom->wacom_wac.ac_name;
955 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
956 wacom->ac.use_for_apm = 0;
957
Benjamin Tissoiresdd3181a2014-07-24 13:01:40 -0700958 error = power_supply_register(&wacom->hdev->dev,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700959 &wacom->battery);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700960 if (error)
961 return error;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700962
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700963 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
964
965 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
966 if (error) {
967 power_supply_unregister(&wacom->battery);
968 return error;
969 }
970
971 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -0700972 }
973
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700974 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700975}
976
977static void wacom_destroy_battery(struct wacom *wacom)
978{
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700979 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
980 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -0700981 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700982 wacom->battery.dev = NULL;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700983 power_supply_unregister(&wacom->ac);
984 wacom->ac.dev = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700985 }
Chris Bagwella1d552c2012-03-25 23:26:30 -0700986}
987
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700988static ssize_t wacom_show_speed(struct device *dev,
989 struct device_attribute
990 *attr, char *buf)
991{
992 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
993 struct wacom *wacom = hid_get_drvdata(hdev);
994
995 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
996}
997
998static ssize_t wacom_store_speed(struct device *dev,
999 struct device_attribute *attr,
1000 const char *buf, size_t count)
1001{
1002 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1003 struct wacom *wacom = hid_get_drvdata(hdev);
1004 u8 new_speed;
1005
1006 if (kstrtou8(buf, 0, &new_speed))
1007 return -EINVAL;
1008
1009 if (new_speed != 0 && new_speed != 1)
1010 return -EINVAL;
1011
1012 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1013
1014 return count;
1015}
1016
Ping Chenge0984bc2014-09-10 12:40:05 -07001017static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001018 wacom_show_speed, wacom_store_speed);
1019
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001020static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001021{
1022 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001023 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001024 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001025
1026 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001027 if (!input_dev)
1028 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001029
1030 input_dev->name = wacom_wac->name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001031 input_dev->phys = hdev->phys;
1032 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001033 input_dev->open = wacom_open;
1034 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001035 input_dev->uniq = hdev->uniq;
1036 input_dev->id.bustype = hdev->bus;
1037 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001038 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001039 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001040 input_set_drvdata(input_dev, wacom);
1041
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001042 return input_dev;
1043}
1044
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001045static void wacom_unregister_inputs(struct wacom *wacom)
1046{
1047 if (wacom->wacom_wac.input)
1048 input_unregister_device(wacom->wacom_wac.input);
1049 if (wacom->wacom_wac.pad_input)
1050 input_unregister_device(wacom->wacom_wac.pad_input);
1051 wacom->wacom_wac.input = NULL;
1052 wacom->wacom_wac.pad_input = NULL;
Ping Cheng912ca212014-09-10 12:41:31 -07001053 wacom_destroy_leds(wacom);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001054}
1055
1056static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001057{
1058 struct input_dev *input_dev, *pad_input_dev;
1059 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1060 int error;
1061
1062 input_dev = wacom_allocate_input(wacom);
1063 pad_input_dev = wacom_allocate_input(wacom);
1064 if (!input_dev || !pad_input_dev) {
1065 error = -ENOMEM;
1066 goto fail1;
1067 }
1068
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001069 wacom_wac->input = input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001070 wacom_wac->pad_input = pad_input_dev;
1071 wacom_wac->pad_input->name = wacom_wac->pad_name;
1072
Ping Cheng19635182012-04-29 21:09:18 -07001073 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1074 if (error)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001075 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001076
1077 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001078 if (error)
1079 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001080
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001081 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1082 if (error) {
1083 /* no pad in use on this interface */
1084 input_free_device(pad_input_dev);
1085 wacom_wac->pad_input = NULL;
1086 pad_input_dev = NULL;
1087 } else {
1088 error = input_register_device(pad_input_dev);
1089 if (error)
1090 goto fail3;
Ping Cheng912ca212014-09-10 12:41:31 -07001091
1092 error = wacom_initialize_leds(wacom);
1093 if (error)
1094 goto fail4;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001095 }
1096
Ping Cheng19635182012-04-29 21:09:18 -07001097 return 0;
1098
Ping Cheng912ca212014-09-10 12:41:31 -07001099fail4:
1100 input_unregister_device(pad_input_dev);
1101 pad_input_dev = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001102fail3:
1103 input_unregister_device(input_dev);
1104 input_dev = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001105fail2:
Ping Cheng19635182012-04-29 21:09:18 -07001106 wacom_wac->input = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001107 wacom_wac->pad_input = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001108fail1:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001109 if (input_dev)
1110 input_free_device(input_dev);
1111 if (pad_input_dev)
1112 input_free_device(pad_input_dev);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001113 return error;
1114}
1115
Chris Bagwell16bf2882012-03-25 23:26:20 -07001116static void wacom_wireless_work(struct work_struct *work)
1117{
1118 struct wacom *wacom = container_of(work, struct wacom, work);
1119 struct usb_device *usbdev = wacom->usbdev;
1120 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001121 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001122 struct wacom *wacom1, *wacom2;
1123 struct wacom_wac *wacom_wac1, *wacom_wac2;
1124 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001125
1126 /*
1127 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001128 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001129 */
1130
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001131 wacom_destroy_battery(wacom);
1132
Chris Bagwell16bf2882012-03-25 23:26:20 -07001133 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001134 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1135 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001136 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001137 wacom_unregister_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001138
1139 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001140 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1141 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001142 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001143 wacom_unregister_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001144
1145 if (wacom_wac->pid == 0) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001146 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001147 wacom_wac1->shared->type = 0;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001148 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001149 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001150
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001151 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001152 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001153
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001154 while (id->bus) {
1155 if (id->vendor == USB_VENDOR_ID_WACOM &&
1156 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001157 break;
1158 id++;
1159 }
1160
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001161 if (!id->bus) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001162 hid_info(wacom->hdev, "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001163 return;
1164 }
1165
1166 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001167 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001168 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001169 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001170 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1171 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001172 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1173 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001174 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1175 wacom_wac1->shared->type = wacom_wac1->features.type;
Ping Cheng912ca212014-09-10 12:41:31 -07001176 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001177 error = wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001178 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001179 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001180
1181 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001182 if (wacom_wac1->features.touch_max ||
1183 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001184 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001185 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001186 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1187 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1188 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1189 if (wacom_wac2->features.touch_max)
1190 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1191 "%s (WL) Finger",wacom_wac2->features.name);
1192 else
1193 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1194 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001195 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1196 "%s (WL) Pad", wacom_wac2->features.name);
Ping Cheng912ca212014-09-10 12:41:31 -07001197 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001198 error = wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001199 if (error)
1200 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001201
1202 if (wacom_wac1->features.type == INTUOSHT &&
1203 wacom_wac1->features.touch_max)
1204 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001205 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001206
1207 error = wacom_initialize_battery(wacom);
1208 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001209 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001210 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001211
1212 return;
1213
Ping Cheng57bcfce2013-10-15 23:44:00 -07001214fail:
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001215 wacom_unregister_inputs(wacom1);
1216 wacom_unregister_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001217 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001218}
1219
Ping Cheng401d7d12013-07-28 00:38:54 -07001220/*
1221 * Not all devices report physical dimensions from HID.
1222 * Compute the default from hardcoded logical dimension
1223 * and resolution before driver overwrites them.
1224 */
1225static void wacom_set_default_phy(struct wacom_features *features)
1226{
1227 if (features->x_resolution) {
1228 features->x_phy = (features->x_max * 100) /
1229 features->x_resolution;
1230 features->y_phy = (features->y_max * 100) /
1231 features->y_resolution;
1232 }
1233}
1234
1235static void wacom_calculate_res(struct wacom_features *features)
1236{
1237 features->x_resolution = wacom_calc_hid_res(features->x_max,
1238 features->x_phy,
1239 features->unit,
1240 features->unitExpo);
1241 features->y_resolution = wacom_calc_hid_res(features->y_max,
1242 features->y_phy,
1243 features->unit,
1244 features->unitExpo);
1245}
1246
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001247static int wacom_hid_report_len(struct hid_report *report)
1248{
1249 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1250 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1251}
1252
1253static size_t wacom_compute_pktlen(struct hid_device *hdev)
1254{
1255 struct hid_report_enum *report_enum;
1256 struct hid_report *report;
1257 size_t size = 0;
1258
1259 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1260
1261 list_for_each_entry(report, &report_enum->report_list, list) {
1262 size_t report_size = wacom_hid_report_len(report);
1263 if (report_size > size)
1264 size = report_size;
1265 }
1266
1267 return size;
1268}
1269
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001270static int wacom_probe(struct hid_device *hdev,
1271 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001272{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001273 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001274 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001275 struct wacom *wacom;
1276 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001277 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001278 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001279
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001280 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001281 return -EINVAL;
1282
Ping Cheng3bea7332006-07-13 18:01:36 -07001283 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001284 if (!wacom)
1285 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001286
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001287 hid_set_drvdata(hdev, wacom);
1288 wacom->hdev = hdev;
1289
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001290 /* ask for the report descriptor to be loaded by HID */
1291 error = hid_parse(hdev);
1292 if (error) {
1293 hid_err(hdev, "parse failed\n");
1294 goto fail1;
1295 }
1296
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001297 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001298 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001299 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001300 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001301 if (features->pktlen > WACOM_PKGLEN_MAX) {
1302 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001303 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001304 }
1305
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001306 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1307 error = -ENODEV;
Jason Childse33da8a2010-02-17 22:38:31 -08001308 goto fail1;
1309 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001310
Ping Cheng3bea7332006-07-13 18:01:36 -07001311 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001312 wacom->intf = intf;
1313 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001314 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001315
Ping Cheng401d7d12013-07-28 00:38:54 -07001316 /* set the default size in case we do not get them from hid */
1317 wacom_set_default_phy(features);
1318
Ping Chengf393ee22012-04-29 21:09:17 -07001319 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001320 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -05001321
Jason Gereckeae584ca2012-04-03 15:50:40 -07001322 /*
1323 * Intuos5 has no useful data about its touch interface in its
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001324 * HID descriptor. If this is the touch interface (PacketSize
Jason Gereckeae584ca2012-04-03 15:50:40 -07001325 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1326 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001327 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001328 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001329 features->device_type = BTN_TOOL_FINGER;
Jason Gereckeae584ca2012-04-03 15:50:40 -07001330
Jason Gereckeae584ca2012-04-03 15:50:40 -07001331 features->x_max = 4096;
1332 features->y_max = 4096;
1333 } else {
1334 features->device_type = BTN_TOOL_PEN;
1335 }
1336 }
1337
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001338 /*
1339 * Same thing for Bamboo 3rd gen.
1340 */
1341 if ((features->type == BAMBOO_PT) &&
1342 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1343 (features->device_type == BTN_TOOL_PEN)) {
1344 features->device_type = BTN_TOOL_FINGER;
1345
1346 features->x_max = 4096;
1347 features->y_max = 4096;
1348 }
1349
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001350 if (hdev->bus == BUS_BLUETOOTH)
1351 features->quirks |= WACOM_QUIRK_BATTERY;
1352
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001353 wacom_setup_device_quirks(features);
1354
Ping Cheng401d7d12013-07-28 00:38:54 -07001355 /* set unit to "100th of a mm" for devices not reported by HID */
1356 if (!features->unit) {
1357 features->unit = 0x11;
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001358 features->unitExpo = -3;
Ping Cheng401d7d12013-07-28 00:38:54 -07001359 }
1360 wacom_calculate_res(features);
1361
Ping Cheng49b764a2010-02-20 00:53:49 -08001362 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001363 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1364 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001365
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001366 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001367 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001368 if (features->device_type != BTN_TOOL_FINGER)
1369 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1370 else if (features->touch_max)
1371 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1372 else
1373 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001374
Benjamin Tissoires4451e082014-07-24 13:01:05 -07001375 error = wacom_add_shared_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001376 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001377 goto fail1;
Ping Cheng49b764a2010-02-20 00:53:49 -08001378 }
1379
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001380 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1381 (features->quirks & WACOM_QUIRK_BATTERY)) {
1382 error = wacom_initialize_battery(wacom);
1383 if (error)
Ping Cheng912ca212014-09-10 12:41:31 -07001384 goto fail2;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001385 }
1386
Chris Bagwelld3825d52012-03-25 23:26:11 -07001387 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001388 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001389 if (error)
Ping Cheng912ca212014-09-10 12:41:31 -07001390 goto fail3;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001391 }
1392
1393 if (hdev->bus == BUS_BLUETOOTH) {
1394 error = device_create_file(&hdev->dev, &dev_attr_speed);
1395 if (error)
1396 hid_warn(hdev,
1397 "can't create sysfs speed attribute err: %d\n",
1398 error);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001399 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001400
Ping Chengec67bbe2009-12-15 00:35:24 -08001401 /* Note that if query fails it is not a hard failure */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001402 wacom_query_tablet_data(hdev, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001403
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001404 /* Regular HID work starts now */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001405 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1406 if (error) {
1407 hid_err(hdev, "hw start failed\n");
Ping Cheng912ca212014-09-10 12:41:31 -07001408 goto fail4;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001409 }
1410
1411 if (features->quirks & WACOM_QUIRK_MONITOR)
1412 error = hid_hw_open(hdev);
1413
Ping Cheng961794a2013-12-05 12:54:53 -08001414 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1415 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1416 wacom_wac->shared->touch_input = wacom_wac->input;
1417 }
1418
Ping Cheng3bea7332006-07-13 18:01:36 -07001419 return 0;
1420
Ping Cheng912ca212014-09-10 12:41:31 -07001421 fail4: if (hdev->bus == BUS_BLUETOOTH)
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001422 device_remove_file(&hdev->dev, &dev_attr_speed);
1423 wacom_unregister_inputs(wacom);
Ping Cheng912ca212014-09-10 12:41:31 -07001424 fail3: wacom_destroy_battery(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001425 fail2: wacom_remove_shared_data(wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001426 fail1: kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001427 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001428 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001429}
1430
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001431static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001432{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001433 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001434
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001435 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001436
Chris Bagwell16bf2882012-03-25 23:26:20 -07001437 cancel_work_sync(&wacom->work);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001438 wacom_unregister_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001439 if (hdev->bus == BUS_BLUETOOTH)
1440 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001441 wacom_destroy_battery(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001442 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001443
1444 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001445 kfree(wacom);
1446}
1447
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001448#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001449static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001450{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001451 struct wacom *wacom = hid_get_drvdata(hdev);
1452 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001453
1454 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001455
1456 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001457 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001458 wacom_led_control(wacom);
1459
Oliver Neukume7224092008-04-15 01:31:57 -04001460 mutex_unlock(&wacom->lock);
1461
1462 return 0;
1463}
1464
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001465static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001466{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001467 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001468}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001469#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001470
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001471static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001472 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001473 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001474 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001475 .remove = wacom_remove,
1476#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001477 .resume = wacom_resume,
1478 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001479#endif
1480 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001481};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001482module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001483
1484MODULE_VERSION(DRIVER_VERSION);
1485MODULE_AUTHOR(DRIVER_AUTHOR);
1486MODULE_DESCRIPTION(DRIVER_DESC);
1487MODULE_LICENSE(DRIVER_LICENSE);