blob: dd288b2fbfe86b81bade1ab41320b260d46d11db [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 Tissoires8ffffd52014-09-16 16:56:39 -0400112 u8 *data;
113 int ret;
Chris Bagwell41343612011-10-26 22:32:52 -0700114
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700115 switch (usage->hid) {
116 case HID_DG_CONTACTMAX:
117 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400118 if (!features->touch_max) {
119 /* read manually */
120 data = kzalloc(2, GFP_KERNEL);
121 if (!data)
122 break;
123 data[0] = field->report->id;
124 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
125 data, 2, 0);
126 if (ret == 2)
127 features->touch_max = data[1];
128 kfree(data);
129 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700130 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700131 }
132}
133
Chris Bagwell428f8582011-10-26 22:26:59 -0700134/*
135 * Interface Descriptor of wacom devices can be incomplete and
136 * inconsistent so wacom_features table is used to store stylus
137 * device's packet lengths, various maximum values, and tablet
138 * resolution based on product ID's.
139 *
140 * For devices that contain 2 interfaces, wacom_features table is
141 * inaccurate for the touch interface. Since the Interface Descriptor
142 * for touch interfaces has pretty complete data, this function exists
143 * to query tablet for this missing information instead of hard coding in
144 * an additional table.
145 *
146 * A typical Interface Descriptor for a stylus will contain a
147 * boot mouse application collection that is not of interest and this
148 * function will ignore it.
149 *
150 * It also contains a digitizer application collection that also is not
151 * of interest since any information it contains would be duplicate
152 * of what is in wacom_features. Usually it defines a report of an array
153 * of bytes that could be used as max length of the stylus packet returned.
154 * If it happens to define a Digitizer-Stylus Physical Collection then
155 * the X and Y logical values contain valid data but it is ignored.
156 *
157 * A typical Interface Descriptor for a touch interface will contain a
158 * Digitizer-Finger Physical Collection which will define both logical
159 * X/Y maximum as well as the physical size of tablet. Since touch
160 * interfaces haven't supported pressure or distance, this is enough
161 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700162 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700163 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
164 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700165 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700166static void wacom_usage_mapping(struct hid_device *hdev,
167 struct hid_field *field, struct hid_usage *usage)
168{
169 struct wacom *wacom = hid_get_drvdata(hdev);
170 struct wacom_features *features = &wacom->wacom_wac.features;
171 bool finger = (field->logical == HID_DG_FINGER) ||
172 (field->physical == HID_DG_FINGER);
173 bool pen = (field->logical == HID_DG_STYLUS) ||
174 (field->physical == HID_DG_STYLUS);
175
176 /*
177 * Requiring Stylus Usage will ignore boot mouse
178 * X/Y values and some cases of invalid Digitizer X/Y
179 * values commonly reported.
180 */
181 if (!pen && !finger)
182 return;
183
184 if (finger && !features->touch_max)
185 /* touch device at least supports one touch point */
186 features->touch_max = 1;
187
188 switch (usage->hid) {
189 case HID_GD_X:
190 features->x_max = field->logical_maximum;
191 if (finger) {
192 features->device_type = BTN_TOOL_FINGER;
193 features->x_phy = field->physical_maximum;
194 if (features->type != BAMBOO_PT) {
195 features->unit = field->unit;
196 features->unitExpo = field->unit_exponent;
197 }
198 } else {
199 features->device_type = BTN_TOOL_PEN;
200 }
201 break;
202 case HID_GD_Y:
203 features->y_max = field->logical_maximum;
204 if (finger) {
205 features->y_phy = field->physical_maximum;
206 if (features->type != BAMBOO_PT) {
207 features->unit = field->unit;
208 features->unitExpo = field->unit_exponent;
209 }
210 }
211 break;
212 case HID_DG_TIPPRESSURE:
213 if (pen)
214 features->pressure_max = field->logical_maximum;
215 break;
216 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400217
218 if (features->type == HID_GENERIC)
219 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700220}
221
222static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800223 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500224{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700225 struct hid_report_enum *rep_enum;
226 struct hid_report *hreport;
227 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500228
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700229 /* check features first */
230 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
231 list_for_each_entry(hreport, &rep_enum->report_list, list) {
232 for (i = 0; i < hreport->maxfield; i++) {
233 /* Ignore if report count is out of bounds. */
234 if (hreport->field[i]->report_count < 1)
235 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500236
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700237 for (j = 0; j < hreport->field[i]->maxusage; j++) {
238 wacom_feature_mapping(hdev, hreport->field[i],
239 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500240 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500241 }
242 }
243
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700244 /* now check the input usages */
245 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
246 list_for_each_entry(hreport, &rep_enum->report_list, list) {
247
248 if (!hreport->maxfield)
249 continue;
250
251 for (i = 0; i < hreport->maxfield; i++)
252 for (j = 0; j < hreport->field[i]->maxusage; j++)
253 wacom_usage_mapping(hdev, hreport->field[i],
254 hreport->field[i]->usage + j);
255 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500256}
257
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700258static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
259 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700260{
261 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700262 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700263
Jason Gereckefe494bc2012-10-03 17:25:35 -0700264 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700265 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800266 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700267
Jason Gereckefe494bc2012-10-03 17:25:35 -0700268 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800269 rep_data[0] = report_id;
270 rep_data[1] = mode;
271
Przemo Firszt296b7372014-08-06 14:00:38 -0700272 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
273 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700274 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700275 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700276 rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700277 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700278
279 kfree(rep_data);
280
281 return error < 0 ? error : 0;
282}
283
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700284static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
285 struct wacom_features *features)
286{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700287 struct wacom *wacom = hid_get_drvdata(hdev);
288 int ret;
289 u8 rep_data[2];
290
291 switch (features->type) {
292 case GRAPHIRE_BT:
293 rep_data[0] = 0x03;
294 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700295 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
296 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700297
298 if (ret >= 0) {
299 rep_data[0] = speed == 0 ? 0x05 : 0x06;
300 rep_data[1] = 0x00;
301
302 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700303 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700304
305 if (ret >= 0) {
306 wacom->wacom_wac.bt_high_speed = speed;
307 return 0;
308 }
309 }
310
311 /*
312 * Note that if the raw queries fail, it's not a hard failure
313 * and it is safe to continue
314 */
315 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
316 rep_data[0], ret);
317 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700318 case INTUOS4WL:
319 if (speed == 1)
320 wacom->wacom_wac.bt_features &= ~0x20;
321 else
322 wacom->wacom_wac.bt_features |= 0x20;
323
324 rep_data[0] = 0x03;
325 rep_data[1] = wacom->wacom_wac.bt_features;
326
Przemo Firszt296b7372014-08-06 14:00:38 -0700327 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
328 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700329 if (ret >= 0)
330 wacom->wacom_wac.bt_high_speed = speed;
331 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700332 }
333
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700334 return 0;
335}
336
Jason Gereckefe494bc2012-10-03 17:25:35 -0700337/*
338 * Switch the tablet into its most-capable mode. Wacom tablets are
339 * typically configured to power-up in a mode which sends mouse-like
340 * reports to the OS. To get absolute position, pressure data, etc.
341 * from the tablet, it is necessary to switch the tablet out of this
342 * mode and into one which sends the full range of tablet data.
343 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700344static int wacom_query_tablet_data(struct hid_device *hdev,
345 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700346{
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700347 if (hdev->bus == BUS_BLUETOOTH)
348 return wacom_bt_query_tablet_data(hdev, 1, features);
349
Jason Gereckefe494bc2012-10-03 17:25:35 -0700350 if (features->device_type == BTN_TOOL_FINGER) {
351 if (features->type > TABLETPC) {
352 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700353 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700354 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700355 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700356 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700357 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700358 } else if (features->device_type == BTN_TOOL_PEN) {
359 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700360 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700361 }
362 }
363
364 return 0;
365}
366
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700367static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700368 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800369{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700370 struct wacom *wacom = hid_get_drvdata(hdev);
371 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800372
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700373 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800374 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700375 features->x_fuzz = 4;
376 features->y_fuzz = 4;
377 features->pressure_fuzz = 0;
378 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800379
Chris Bagwelld3825d52012-03-25 23:26:11 -0700380 /*
381 * The wireless device HID is basic and layout conflicts with
382 * other tablets (monitor and touch interface can look like pen).
383 * Skip the query for this type and modify defaults based on
384 * interface number.
385 */
386 if (features->type == WIRELESS) {
387 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
388 features->device_type = 0;
389 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700390 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700391 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
392 }
393 }
394
Ping Cheng19635182012-04-29 21:09:18 -0700395 /* only devices that support touch need to retrieve the info */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700396 if (features->type < BAMBOO_PT)
397 return;
Ping Chengec67bbe2009-12-15 00:35:24 -0800398
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700399 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800400}
401
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700402struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700403 struct list_head list;
404 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700405 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700406 struct wacom_shared shared;
407};
408
409static LIST_HEAD(wacom_udev_list);
410static DEFINE_MUTEX(wacom_udev_list_lock);
411
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700412static bool wacom_are_sibling(struct hid_device *hdev,
413 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700414{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700415 struct wacom *wacom = hid_get_drvdata(hdev);
416 struct wacom_features *features = &wacom->wacom_wac.features;
417 int vid = features->oVid;
418 int pid = features->oPid;
419 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700420
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700421 if (vid == 0 && pid == 0) {
422 vid = hdev->vendor;
423 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700424 }
425
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700426 if (vid != sibling->vendor || pid != sibling->product)
427 return false;
428
429 /* Compare the physical path. */
430 n1 = strrchr(hdev->phys, '.') - hdev->phys;
431 n2 = strrchr(sibling->phys, '.') - sibling->phys;
432 if (n1 != n2 || n1 <= 0 || n2 <= 0)
433 return false;
434
435 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700436}
437
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700438static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700439{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700440 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700441
442 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700443 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700444 kref_get(&data->kref);
445 return data;
446 }
447 }
448
449 return NULL;
450}
451
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700452static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700453{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700454 struct wacom *wacom = hid_get_drvdata(hdev);
455 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
456 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700457 int retval = 0;
458
459 mutex_lock(&wacom_udev_list_lock);
460
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700461 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700462 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700463 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700464 if (!data) {
465 retval = -ENOMEM;
466 goto out;
467 }
468
469 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700470 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700471 list_add_tail(&data->list, &wacom_udev_list);
472 }
473
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700474 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700475
476out:
477 mutex_unlock(&wacom_udev_list_lock);
478 return retval;
479}
480
481static void wacom_release_shared_data(struct kref *kref)
482{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700483 struct wacom_hdev_data *data =
484 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700485
486 mutex_lock(&wacom_udev_list_lock);
487 list_del(&data->list);
488 mutex_unlock(&wacom_udev_list_lock);
489
490 kfree(data);
491}
492
493static void wacom_remove_shared_data(struct wacom_wac *wacom)
494{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700495 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700496
497 if (wacom->shared) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700498 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
Ping Cheng4492eff2010-03-19 22:18:15 -0700499 kref_put(&data->kref, wacom_release_shared_data);
500 wacom->shared = NULL;
501 }
502}
503
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700504static int wacom_led_control(struct wacom *wacom)
505{
506 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700507 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700508 unsigned char report_id = WAC_CMD_LED_CONTROL;
509 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700510
Ping Cheng912ca212014-09-10 12:41:31 -0700511 if (wacom->wacom_wac.pid) { /* wireless connected */
512 report_id = WAC_CMD_WL_LED_CONTROL;
513 buf_size = 13;
514 }
515 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700516 if (!buf)
517 return -ENOMEM;
518
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700519 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700520 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700521 /*
522 * Touch Ring and crop mark LED luminance may take on
523 * one of four values:
524 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
525 */
526 int ring_led = wacom->led.select[0] & 0x03;
527 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
528 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700529 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700530
Ping Cheng912ca212014-09-10 12:41:31 -0700531 buf[0] = report_id;
532 if (wacom->wacom_wac.pid) {
533 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
534 buf, buf_size, WAC_CMD_RETRIES);
535 buf[0] = report_id;
536 buf[4] = led_bits;
537 } else
538 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700539 }
540 else {
541 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700542
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700543 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
544 wacom->wacom_wac.features.type == WACOM_24HD)
545 led |= (wacom->led.select[1] << 4) | 0x40;
546
Ping Cheng912ca212014-09-10 12:41:31 -0700547 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700548 buf[1] = led;
549 buf[2] = wacom->led.llv;
550 buf[3] = wacom->led.hlv;
551 buf[4] = wacom->led.img_lum;
552 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700553
Ping Cheng912ca212014-09-10 12:41:31 -0700554 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700555 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700556 kfree(buf);
557
558 return retval;
559}
560
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700561static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
562 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700563{
564 unsigned char *buf;
565 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700566 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700567
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700568 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700569 if (!buf)
570 return -ENOMEM;
571
572 /* Send 'start' command */
573 buf[0] = WAC_CMD_ICON_START;
574 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700575 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
576 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700577 if (retval < 0)
578 goto out;
579
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700580 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700581 buf[1] = button_id & 0x07;
582 for (i = 0; i < 4; i++) {
583 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700584 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700585
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700586 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700587 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700588 if (retval < 0)
589 break;
590 }
591
592 /* Send 'stop' */
593 buf[0] = WAC_CMD_ICON_START;
594 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700595 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
596 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700597
598out:
599 kfree(buf);
600 return retval;
601}
602
Ping Cheng09e7d942011-10-04 23:51:14 -0700603static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700604 const char *buf, size_t count)
605{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700606 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700607 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700608 unsigned int id;
609 int err;
610
611 err = kstrtouint(buf, 10, &id);
612 if (err)
613 return err;
614
615 mutex_lock(&wacom->lock);
616
Ping Cheng09e7d942011-10-04 23:51:14 -0700617 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700618 err = wacom_led_control(wacom);
619
620 mutex_unlock(&wacom->lock);
621
622 return err < 0 ? err : count;
623}
624
Ping Cheng09e7d942011-10-04 23:51:14 -0700625#define DEVICE_LED_SELECT_ATTR(SET_ID) \
626static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
627 struct device_attribute *attr, const char *buf, size_t count) \
628{ \
629 return wacom_led_select_store(dev, SET_ID, buf, count); \
630} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700631static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
632 struct device_attribute *attr, char *buf) \
633{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700634 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700635 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700636 return scnprintf(buf, PAGE_SIZE, "%d\n", \
637 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700638} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700639static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700640 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700641 wacom_led##SET_ID##_select_store)
642
643DEVICE_LED_SELECT_ATTR(0);
644DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700645
646static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
647 const char *buf, size_t count)
648{
649 unsigned int value;
650 int err;
651
652 err = kstrtouint(buf, 10, &value);
653 if (err)
654 return err;
655
656 mutex_lock(&wacom->lock);
657
658 *dest = value & 0x7f;
659 err = wacom_led_control(wacom);
660
661 mutex_unlock(&wacom->lock);
662
663 return err < 0 ? err : count;
664}
665
666#define DEVICE_LUMINANCE_ATTR(name, field) \
667static ssize_t wacom_##name##_luminance_store(struct device *dev, \
668 struct device_attribute *attr, const char *buf, size_t count) \
669{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700670 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700671 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700672 \
673 return wacom_luminance_store(wacom, &wacom->led.field, \
674 buf, count); \
675} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700676static ssize_t wacom_##name##_luminance_show(struct device *dev, \
677 struct device_attribute *attr, char *buf) \
678{ \
679 struct wacom *wacom = dev_get_drvdata(dev); \
680 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
681} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700682static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700683 wacom_##name##_luminance_show, \
684 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700685
686DEVICE_LUMINANCE_ATTR(status0, llv);
687DEVICE_LUMINANCE_ATTR(status1, hlv);
688DEVICE_LUMINANCE_ATTR(buttons, img_lum);
689
690static ssize_t wacom_button_image_store(struct device *dev, int button_id,
691 const char *buf, size_t count)
692{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700693 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700694 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700695 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700696 unsigned len;
697 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700698
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700699 if (hdev->bus == BUS_BLUETOOTH) {
700 len = 256;
701 xfer_id = WAC_CMD_ICON_BT_XFER;
702 } else {
703 len = 1024;
704 xfer_id = WAC_CMD_ICON_XFER;
705 }
706
707 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700708 return -EINVAL;
709
710 mutex_lock(&wacom->lock);
711
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700712 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700713
714 mutex_unlock(&wacom->lock);
715
716 return err < 0 ? err : count;
717}
718
719#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
720static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
721 struct device_attribute *attr, const char *buf, size_t count) \
722{ \
723 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
724} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700725static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700726 NULL, wacom_btnimg##BUTTON_ID##_store)
727
728DEVICE_BTNIMG_ATTR(0);
729DEVICE_BTNIMG_ATTR(1);
730DEVICE_BTNIMG_ATTR(2);
731DEVICE_BTNIMG_ATTR(3);
732DEVICE_BTNIMG_ATTR(4);
733DEVICE_BTNIMG_ATTR(5);
734DEVICE_BTNIMG_ATTR(6);
735DEVICE_BTNIMG_ATTR(7);
736
Ping Cheng09e7d942011-10-04 23:51:14 -0700737static struct attribute *cintiq_led_attrs[] = {
738 &dev_attr_status_led0_select.attr,
739 &dev_attr_status_led1_select.attr,
740 NULL
741};
742
743static struct attribute_group cintiq_led_attr_group = {
744 .name = "wacom_led",
745 .attrs = cintiq_led_attrs,
746};
747
748static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700749 &dev_attr_status0_luminance.attr,
750 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700751 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700752 &dev_attr_buttons_luminance.attr,
753 &dev_attr_button0_rawimg.attr,
754 &dev_attr_button1_rawimg.attr,
755 &dev_attr_button2_rawimg.attr,
756 &dev_attr_button3_rawimg.attr,
757 &dev_attr_button4_rawimg.attr,
758 &dev_attr_button5_rawimg.attr,
759 &dev_attr_button6_rawimg.attr,
760 &dev_attr_button7_rawimg.attr,
761 NULL
762};
763
Ping Cheng09e7d942011-10-04 23:51:14 -0700764static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700765 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700766 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700767};
768
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700769static struct attribute *intuos5_led_attrs[] = {
770 &dev_attr_status0_luminance.attr,
771 &dev_attr_status_led0_select.attr,
772 NULL
773};
774
775static struct attribute_group intuos5_led_attr_group = {
776 .name = "wacom_led",
777 .attrs = intuos5_led_attrs,
778};
779
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700780static int wacom_initialize_leds(struct wacom *wacom)
781{
782 int error;
783
Ping Cheng09e7d942011-10-04 23:51:14 -0700784 /* Initialize default values */
785 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700786 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700787 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700788 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700789 case INTUOS4L:
790 wacom->led.select[0] = 0;
791 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700792 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700793 wacom->led.hlv = 20;
794 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700795 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700796 &intuos4_led_attr_group);
797 break;
798
Jason Gerecke246835f2011-12-12 00:12:04 -0800799 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700800 case WACOM_21UX2:
801 wacom->led.select[0] = 0;
802 wacom->led.select[1] = 0;
803 wacom->led.llv = 0;
804 wacom->led.hlv = 0;
805 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700806
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700807 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700808 &cintiq_led_attr_group);
809 break;
810
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700811 case INTUOS5S:
812 case INTUOS5:
813 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700814 case INTUOSPS:
815 case INTUOSPM:
816 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700817 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
818 wacom->led.select[0] = 0;
819 wacom->led.select[1] = 0;
820 wacom->led.llv = 32;
821 wacom->led.hlv = 0;
822 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700823
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700824 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700825 &intuos5_led_attr_group);
826 } else
827 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700828 break;
829
Ping Cheng09e7d942011-10-04 23:51:14 -0700830 default:
831 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700832 }
833
Ping Cheng09e7d942011-10-04 23:51:14 -0700834 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700835 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700836 "cannot create sysfs group err: %d\n", error);
837 return error;
838 }
839 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700840 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700841
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700842 return 0;
843}
844
845static void wacom_destroy_leds(struct wacom *wacom)
846{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700847 if (!wacom->led_initialized)
848 return;
849
850 wacom->led_initialized = false;
851
Ping Cheng09e7d942011-10-04 23:51:14 -0700852 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700853 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700854 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700855 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700856 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700857 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700858 &intuos4_led_attr_group);
859 break;
860
Jason Gerecke246835f2011-12-12 00:12:04 -0800861 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700862 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700863 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700864 &cintiq_led_attr_group);
865 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700866
867 case INTUOS5S:
868 case INTUOS5:
869 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700870 case INTUOSPS:
871 case INTUOSPM:
872 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700873 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700874 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700875 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700876 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700877 }
878}
879
Chris Bagwella1d552c2012-03-25 23:26:30 -0700880static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700881 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700882 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700883 POWER_SUPPLY_PROP_CAPACITY
884};
885
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700886static enum power_supply_property wacom_ac_props[] = {
887 POWER_SUPPLY_PROP_PRESENT,
888 POWER_SUPPLY_PROP_ONLINE,
889 POWER_SUPPLY_PROP_SCOPE,
890};
891
Chris Bagwella1d552c2012-03-25 23:26:30 -0700892static int wacom_battery_get_property(struct power_supply *psy,
893 enum power_supply_property psp,
894 union power_supply_propval *val)
895{
896 struct wacom *wacom = container_of(psy, struct wacom, battery);
897 int ret = 0;
898
899 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700900 case POWER_SUPPLY_PROP_SCOPE:
901 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
902 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700903 case POWER_SUPPLY_PROP_CAPACITY:
904 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700905 wacom->wacom_wac.battery_capacity;
906 break;
907 case POWER_SUPPLY_PROP_STATUS:
908 if (wacom->wacom_wac.bat_charging)
909 val->intval = POWER_SUPPLY_STATUS_CHARGING;
910 else if (wacom->wacom_wac.battery_capacity == 100 &&
911 wacom->wacom_wac.ps_connected)
912 val->intval = POWER_SUPPLY_STATUS_FULL;
913 else
914 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700915 break;
916 default:
917 ret = -EINVAL;
918 break;
919 }
920
921 return ret;
922}
923
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700924static int wacom_ac_get_property(struct power_supply *psy,
925 enum power_supply_property psp,
926 union power_supply_propval *val)
927{
928 struct wacom *wacom = container_of(psy, struct wacom, ac);
929 int ret = 0;
930
931 switch (psp) {
932 case POWER_SUPPLY_PROP_PRESENT:
933 /* fall through */
934 case POWER_SUPPLY_PROP_ONLINE:
935 val->intval = wacom->wacom_wac.ps_connected;
936 break;
937 case POWER_SUPPLY_PROP_SCOPE:
938 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
939 break;
940 default:
941 ret = -EINVAL;
942 break;
943 }
944 return ret;
945}
946
Chris Bagwella1d552c2012-03-25 23:26:30 -0700947static int wacom_initialize_battery(struct wacom *wacom)
948{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700949 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700950 int error;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700951 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700952
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700953 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700954 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700955
Chris Bagwella1d552c2012-03-25 23:26:30 -0700956 wacom->battery.properties = wacom_battery_props;
957 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
958 wacom->battery.get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700959 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
960 wacom->battery.name = wacom->wacom_wac.bat_name;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700961 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
962 wacom->battery.use_for_apm = 0;
963
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700964 wacom->ac.properties = wacom_ac_props;
965 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
966 wacom->ac.get_property = wacom_ac_get_property;
967 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
968 wacom->ac.name = wacom->wacom_wac.ac_name;
969 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
970 wacom->ac.use_for_apm = 0;
971
Benjamin Tissoiresdd3181a2014-07-24 13:01:40 -0700972 error = power_supply_register(&wacom->hdev->dev,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700973 &wacom->battery);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700974 if (error)
975 return error;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700976
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700977 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
978
979 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
980 if (error) {
981 power_supply_unregister(&wacom->battery);
982 return error;
983 }
984
985 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -0700986 }
987
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700988 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700989}
990
991static void wacom_destroy_battery(struct wacom *wacom)
992{
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700993 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
994 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -0700995 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700996 wacom->battery.dev = NULL;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700997 power_supply_unregister(&wacom->ac);
998 wacom->ac.dev = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700999 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001000}
1001
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001002static ssize_t wacom_show_speed(struct device *dev,
1003 struct device_attribute
1004 *attr, char *buf)
1005{
1006 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1007 struct wacom *wacom = hid_get_drvdata(hdev);
1008
1009 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1010}
1011
1012static ssize_t wacom_store_speed(struct device *dev,
1013 struct device_attribute *attr,
1014 const char *buf, size_t count)
1015{
1016 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1017 struct wacom *wacom = hid_get_drvdata(hdev);
1018 u8 new_speed;
1019
1020 if (kstrtou8(buf, 0, &new_speed))
1021 return -EINVAL;
1022
1023 if (new_speed != 0 && new_speed != 1)
1024 return -EINVAL;
1025
1026 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1027
1028 return count;
1029}
1030
Ping Chenge0984bc2014-09-10 12:40:05 -07001031static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001032 wacom_show_speed, wacom_store_speed);
1033
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001034static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001035{
1036 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001037 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001038 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001039
1040 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001041 if (!input_dev)
1042 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001043
1044 input_dev->name = wacom_wac->name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001045 input_dev->phys = hdev->phys;
1046 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001047 input_dev->open = wacom_open;
1048 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001049 input_dev->uniq = hdev->uniq;
1050 input_dev->id.bustype = hdev->bus;
1051 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001052 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001053 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001054 input_set_drvdata(input_dev, wacom);
1055
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001056 return input_dev;
1057}
1058
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001059static void wacom_free_inputs(struct wacom *wacom)
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001060{
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001061 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1062
1063 if (wacom_wac->input)
1064 input_free_device(wacom_wac->input);
1065 if (wacom_wac->pad_input)
1066 input_free_device(wacom_wac->pad_input);
1067 wacom_wac->input = NULL;
1068 wacom_wac->pad_input = NULL;
1069}
1070
1071static int wacom_allocate_inputs(struct wacom *wacom)
1072{
1073 struct input_dev *input_dev, *pad_input_dev;
1074 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1075
1076 input_dev = wacom_allocate_input(wacom);
1077 pad_input_dev = wacom_allocate_input(wacom);
1078 if (!input_dev || !pad_input_dev) {
1079 wacom_free_inputs(wacom);
1080 return -ENOMEM;
1081 }
1082
1083 wacom_wac->input = input_dev;
1084 wacom_wac->pad_input = pad_input_dev;
1085 wacom_wac->pad_input->name = wacom_wac->pad_name;
1086
1087 return 0;
1088}
1089
1090static void wacom_clean_inputs(struct wacom *wacom)
1091{
1092 if (wacom->wacom_wac.input) {
1093 if (wacom->wacom_wac.input_registered)
1094 input_unregister_device(wacom->wacom_wac.input);
1095 else
1096 input_free_device(wacom->wacom_wac.input);
1097 }
1098 if (wacom->wacom_wac.pad_input) {
1099 if (wacom->wacom_wac.input_registered)
1100 input_unregister_device(wacom->wacom_wac.pad_input);
1101 else
1102 input_free_device(wacom->wacom_wac.pad_input);
1103 }
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001104 wacom->wacom_wac.input = NULL;
1105 wacom->wacom_wac.pad_input = NULL;
Ping Cheng912ca212014-09-10 12:41:31 -07001106 wacom_destroy_leds(wacom);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001107}
1108
1109static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001110{
1111 struct input_dev *input_dev, *pad_input_dev;
1112 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1113 int error;
1114
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001115 input_dev = wacom_wac->input;
1116 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001117
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001118 if (!input_dev || !pad_input_dev)
1119 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001120
Ping Cheng19635182012-04-29 21:09:18 -07001121 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1122 if (error)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001123 return error;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001124
1125 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001126 if (error)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001127 return error;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001128
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001129 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1130 if (error) {
1131 /* no pad in use on this interface */
1132 input_free_device(pad_input_dev);
1133 wacom_wac->pad_input = NULL;
1134 pad_input_dev = NULL;
1135 } else {
1136 error = input_register_device(pad_input_dev);
1137 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001138 goto fail_register_pad_input;
Ping Cheng912ca212014-09-10 12:41:31 -07001139
1140 error = wacom_initialize_leds(wacom);
1141 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001142 goto fail_leds;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001143 }
1144
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001145 wacom_wac->input_registered = true;
1146
Ping Cheng19635182012-04-29 21:09:18 -07001147 return 0;
1148
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001149fail_leds:
Ping Cheng912ca212014-09-10 12:41:31 -07001150 input_unregister_device(pad_input_dev);
1151 pad_input_dev = NULL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001152fail_register_pad_input:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001153 input_unregister_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001154 wacom_wac->input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001155 return error;
1156}
1157
Chris Bagwell16bf2882012-03-25 23:26:20 -07001158static void wacom_wireless_work(struct work_struct *work)
1159{
1160 struct wacom *wacom = container_of(work, struct wacom, work);
1161 struct usb_device *usbdev = wacom->usbdev;
1162 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001163 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001164 struct wacom *wacom1, *wacom2;
1165 struct wacom_wac *wacom_wac1, *wacom_wac2;
1166 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001167
1168 /*
1169 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001170 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001171 */
1172
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001173 wacom_destroy_battery(wacom);
1174
Chris Bagwell16bf2882012-03-25 23:26:20 -07001175 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001176 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1177 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001178 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001179 wacom_clean_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001180
1181 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001182 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1183 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001184 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001185 wacom_clean_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001186
1187 if (wacom_wac->pid == 0) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001188 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001189 wacom_wac1->shared->type = 0;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001190 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001191 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001192
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001193 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001194 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001195
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001196 while (id->bus) {
1197 if (id->vendor == USB_VENDOR_ID_WACOM &&
1198 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001199 break;
1200 id++;
1201 }
1202
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001203 if (!id->bus) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001204 hid_info(wacom->hdev, "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001205 return;
1206 }
1207
1208 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001209 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001210 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001211 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001212 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1213 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001214 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1215 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001216 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1217 wacom_wac1->shared->type = wacom_wac1->features.type;
Ping Cheng912ca212014-09-10 12:41:31 -07001218 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001219 error = wacom_allocate_inputs(wacom1) ||
1220 wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001221 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001222 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001223
1224 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001225 if (wacom_wac1->features.touch_max ||
1226 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001227 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001228 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001229 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1230 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1231 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1232 if (wacom_wac2->features.touch_max)
1233 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1234 "%s (WL) Finger",wacom_wac2->features.name);
1235 else
1236 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1237 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001238 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1239 "%s (WL) Pad", wacom_wac2->features.name);
Ping Cheng912ca212014-09-10 12:41:31 -07001240 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001241 error = wacom_allocate_inputs(wacom2) ||
1242 wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001243 if (error)
1244 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001245
1246 if (wacom_wac1->features.type == INTUOSHT &&
1247 wacom_wac1->features.touch_max)
1248 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001249 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001250
1251 error = wacom_initialize_battery(wacom);
1252 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001253 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001254 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001255
1256 return;
1257
Ping Cheng57bcfce2013-10-15 23:44:00 -07001258fail:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001259 wacom_clean_inputs(wacom1);
1260 wacom_clean_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001261 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001262}
1263
Ping Cheng401d7d12013-07-28 00:38:54 -07001264/*
1265 * Not all devices report physical dimensions from HID.
1266 * Compute the default from hardcoded logical dimension
1267 * and resolution before driver overwrites them.
1268 */
1269static void wacom_set_default_phy(struct wacom_features *features)
1270{
1271 if (features->x_resolution) {
1272 features->x_phy = (features->x_max * 100) /
1273 features->x_resolution;
1274 features->y_phy = (features->y_max * 100) /
1275 features->y_resolution;
1276 }
1277}
1278
1279static void wacom_calculate_res(struct wacom_features *features)
1280{
1281 features->x_resolution = wacom_calc_hid_res(features->x_max,
1282 features->x_phy,
1283 features->unit,
1284 features->unitExpo);
1285 features->y_resolution = wacom_calc_hid_res(features->y_max,
1286 features->y_phy,
1287 features->unit,
1288 features->unitExpo);
1289}
1290
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001291static int wacom_hid_report_len(struct hid_report *report)
1292{
1293 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1294 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1295}
1296
1297static size_t wacom_compute_pktlen(struct hid_device *hdev)
1298{
1299 struct hid_report_enum *report_enum;
1300 struct hid_report *report;
1301 size_t size = 0;
1302
1303 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1304
1305 list_for_each_entry(report, &report_enum->report_list, list) {
1306 size_t report_size = wacom_hid_report_len(report);
1307 if (report_size > size)
1308 size = report_size;
1309 }
1310
1311 return size;
1312}
1313
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001314static int wacom_probe(struct hid_device *hdev,
1315 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001316{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001317 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001318 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001319 struct wacom *wacom;
1320 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001321 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001322 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001323 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07001324
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001325 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001326 return -EINVAL;
1327
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -04001328 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1329
Ping Cheng3bea7332006-07-13 18:01:36 -07001330 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001331 if (!wacom)
1332 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001333
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001334 hid_set_drvdata(hdev, wacom);
1335 wacom->hdev = hdev;
1336
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001337 /* ask for the report descriptor to be loaded by HID */
1338 error = hid_parse(hdev);
1339 if (error) {
1340 hid_err(hdev, "parse failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001341 goto fail_parse;
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001342 }
1343
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001344 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001345 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001346 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001347 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001348 if (features->pktlen > WACOM_PKGLEN_MAX) {
1349 error = -EINVAL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001350 goto fail_pktlen;
Jason Childse33da8a2010-02-17 22:38:31 -08001351 }
1352
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001353 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1354 error = -ENODEV;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001355 goto fail_type;
Jason Childse33da8a2010-02-17 22:38:31 -08001356 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001357
Ping Cheng3bea7332006-07-13 18:01:36 -07001358 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001359 wacom->intf = intf;
1360 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001361 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001362
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001363 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1364 error = wacom_allocate_inputs(wacom);
1365 if (error)
1366 goto fail_allocate_inputs;
1367 }
1368
Ping Cheng401d7d12013-07-28 00:38:54 -07001369 /* set the default size in case we do not get them from hid */
1370 wacom_set_default_phy(features);
1371
Ping Chengf393ee22012-04-29 21:09:17 -07001372 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001373 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -05001374
Jason Gereckeae584ca2012-04-03 15:50:40 -07001375 /*
1376 * Intuos5 has no useful data about its touch interface in its
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001377 * HID descriptor. If this is the touch interface (PacketSize
Jason Gereckeae584ca2012-04-03 15:50:40 -07001378 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1379 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001380 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001381 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001382 features->device_type = BTN_TOOL_FINGER;
Jason Gereckeae584ca2012-04-03 15:50:40 -07001383
Jason Gereckeae584ca2012-04-03 15:50:40 -07001384 features->x_max = 4096;
1385 features->y_max = 4096;
1386 } else {
1387 features->device_type = BTN_TOOL_PEN;
1388 }
1389 }
1390
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001391 /*
1392 * Same thing for Bamboo 3rd gen.
1393 */
1394 if ((features->type == BAMBOO_PT) &&
1395 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1396 (features->device_type == BTN_TOOL_PEN)) {
1397 features->device_type = BTN_TOOL_FINGER;
1398
1399 features->x_max = 4096;
1400 features->y_max = 4096;
1401 }
1402
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001403 if (hdev->bus == BUS_BLUETOOTH)
1404 features->quirks |= WACOM_QUIRK_BATTERY;
1405
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001406 wacom_setup_device_quirks(features);
1407
Ping Cheng401d7d12013-07-28 00:38:54 -07001408 /* set unit to "100th of a mm" for devices not reported by HID */
1409 if (!features->unit) {
1410 features->unit = 0x11;
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001411 features->unitExpo = -3;
Ping Cheng401d7d12013-07-28 00:38:54 -07001412 }
1413 wacom_calculate_res(features);
1414
Ping Cheng49b764a2010-02-20 00:53:49 -08001415 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001416 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1417 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001418
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001419 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001420 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001421 if (features->device_type != BTN_TOOL_FINGER)
1422 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1423 else if (features->touch_max)
1424 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1425 else
1426 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001427
Benjamin Tissoires4451e082014-07-24 13:01:05 -07001428 error = wacom_add_shared_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001429 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001430 goto fail_shared_data;
Ping Cheng49b764a2010-02-20 00:53:49 -08001431 }
1432
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001433 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1434 (features->quirks & WACOM_QUIRK_BATTERY)) {
1435 error = wacom_initialize_battery(wacom);
1436 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001437 goto fail_battery;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001438 }
1439
Chris Bagwelld3825d52012-03-25 23:26:11 -07001440 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001441 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001442 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001443 goto fail_register_inputs;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001444 }
1445
1446 if (hdev->bus == BUS_BLUETOOTH) {
1447 error = device_create_file(&hdev->dev, &dev_attr_speed);
1448 if (error)
1449 hid_warn(hdev,
1450 "can't create sysfs speed attribute err: %d\n",
1451 error);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001452 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001453
Ping Chengec67bbe2009-12-15 00:35:24 -08001454 /* Note that if query fails it is not a hard failure */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001455 wacom_query_tablet_data(hdev, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001456
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001457 if (features->type == HID_GENERIC)
1458 connect_mask |= HID_CONNECT_DRIVER;
1459
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001460 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001461 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001462 if (error) {
1463 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001464 goto fail_hw_start;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001465 }
1466
1467 if (features->quirks & WACOM_QUIRK_MONITOR)
1468 error = hid_hw_open(hdev);
1469
Ping Cheng961794a2013-12-05 12:54:53 -08001470 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1471 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1472 wacom_wac->shared->touch_input = wacom_wac->input;
1473 }
1474
Ping Cheng3bea7332006-07-13 18:01:36 -07001475 return 0;
1476
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001477fail_hw_start:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001478 if (hdev->bus == BUS_BLUETOOTH)
1479 device_remove_file(&hdev->dev, &dev_attr_speed);
1480fail_register_inputs:
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001481 wacom_clean_inputs(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001482 wacom_destroy_battery(wacom);
1483fail_battery:
1484 wacom_remove_shared_data(wacom_wac);
1485fail_shared_data:
Benjamin Tissoires494078b2014-09-23 12:08:07 -04001486 wacom_clean_inputs(wacom);
1487fail_allocate_inputs:
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001488fail_type:
1489fail_pktlen:
1490fail_parse:
1491 kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001492 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001493 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001494}
1495
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001496static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001497{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001498 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001499
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001500 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001501
Chris Bagwell16bf2882012-03-25 23:26:20 -07001502 cancel_work_sync(&wacom->work);
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04001503 wacom_clean_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001504 if (hdev->bus == BUS_BLUETOOTH)
1505 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001506 wacom_destroy_battery(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001507 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001508
1509 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001510 kfree(wacom);
1511}
1512
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001513#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001514static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001515{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001516 struct wacom *wacom = hid_get_drvdata(hdev);
1517 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001518
1519 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001520
1521 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001522 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001523 wacom_led_control(wacom);
1524
Oliver Neukume7224092008-04-15 01:31:57 -04001525 mutex_unlock(&wacom->lock);
1526
1527 return 0;
1528}
1529
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001530static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001531{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001532 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001533}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001534#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001535
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001536static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001537 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001538 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001539 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001540 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04001541 .event = wacom_wac_event,
1542 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001543#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001544 .resume = wacom_resume,
1545 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001546#endif
1547 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001548};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001549module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001550
1551MODULE_VERSION(DRIVER_VERSION);
1552MODULE_AUTHOR(DRIVER_AUTHOR);
1553MODULE_DESCRIPTION(DRIVER_DESC);
1554MODULE_LICENSE(DRIVER_LICENSE);