blob: 97e1feffc6e4d23ba1a8b83a8c4afa0f1e345682 [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;
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400113 u8 *data;
114 int ret;
Chris Bagwell41343612011-10-26 22:32:52 -0700115
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700116 switch (usage->hid) {
117 case HID_DG_CONTACTMAX:
118 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400119 if (!features->touch_max) {
120 /* read manually */
121 data = kzalloc(2, GFP_KERNEL);
122 if (!data)
123 break;
124 data[0] = field->report->id;
125 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
126 data, 2, 0);
127 if (ret == 2)
128 features->touch_max = data[1];
129 kfree(data);
130 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700131 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700132 }
133}
134
Chris Bagwell428f8582011-10-26 22:26:59 -0700135/*
136 * Interface Descriptor of wacom devices can be incomplete and
137 * inconsistent so wacom_features table is used to store stylus
138 * device's packet lengths, various maximum values, and tablet
139 * resolution based on product ID's.
140 *
141 * For devices that contain 2 interfaces, wacom_features table is
142 * inaccurate for the touch interface. Since the Interface Descriptor
143 * for touch interfaces has pretty complete data, this function exists
144 * to query tablet for this missing information instead of hard coding in
145 * an additional table.
146 *
147 * A typical Interface Descriptor for a stylus will contain a
148 * boot mouse application collection that is not of interest and this
149 * function will ignore it.
150 *
151 * It also contains a digitizer application collection that also is not
152 * of interest since any information it contains would be duplicate
153 * of what is in wacom_features. Usually it defines a report of an array
154 * of bytes that could be used as max length of the stylus packet returned.
155 * If it happens to define a Digitizer-Stylus Physical Collection then
156 * the X and Y logical values contain valid data but it is ignored.
157 *
158 * A typical Interface Descriptor for a touch interface will contain a
159 * Digitizer-Finger Physical Collection which will define both logical
160 * X/Y maximum as well as the physical size of tablet. Since touch
161 * interfaces haven't supported pressure or distance, this is enough
162 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700163 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700164 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
165 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700166 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700167static void wacom_usage_mapping(struct hid_device *hdev,
168 struct hid_field *field, struct hid_usage *usage)
169{
170 struct wacom *wacom = hid_get_drvdata(hdev);
171 struct wacom_features *features = &wacom->wacom_wac.features;
172 bool finger = (field->logical == HID_DG_FINGER) ||
173 (field->physical == HID_DG_FINGER);
174 bool pen = (field->logical == HID_DG_STYLUS) ||
175 (field->physical == HID_DG_STYLUS);
176
177 /*
178 * Requiring Stylus Usage will ignore boot mouse
179 * X/Y values and some cases of invalid Digitizer X/Y
180 * values commonly reported.
181 */
182 if (!pen && !finger)
183 return;
184
185 if (finger && !features->touch_max)
186 /* touch device at least supports one touch point */
187 features->touch_max = 1;
188
189 switch (usage->hid) {
190 case HID_GD_X:
191 features->x_max = field->logical_maximum;
192 if (finger) {
193 features->device_type = BTN_TOOL_FINGER;
194 features->x_phy = field->physical_maximum;
195 if (features->type != BAMBOO_PT) {
196 features->unit = field->unit;
197 features->unitExpo = field->unit_exponent;
198 }
199 } else {
200 features->device_type = BTN_TOOL_PEN;
201 }
202 break;
203 case HID_GD_Y:
204 features->y_max = field->logical_maximum;
205 if (finger) {
206 features->y_phy = field->physical_maximum;
207 if (features->type != BAMBOO_PT) {
208 features->unit = field->unit;
209 features->unitExpo = field->unit_exponent;
210 }
211 }
212 break;
213 case HID_DG_TIPPRESSURE:
214 if (pen)
215 features->pressure_max = field->logical_maximum;
216 break;
217 }
218}
219
220static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800221 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500222{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700223 struct hid_report_enum *rep_enum;
224 struct hid_report *hreport;
225 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500226
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700227 /* check features first */
228 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
229 list_for_each_entry(hreport, &rep_enum->report_list, list) {
230 for (i = 0; i < hreport->maxfield; i++) {
231 /* Ignore if report count is out of bounds. */
232 if (hreport->field[i]->report_count < 1)
233 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500234
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700235 for (j = 0; j < hreport->field[i]->maxusage; j++) {
236 wacom_feature_mapping(hdev, hreport->field[i],
237 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500238 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500239 }
240 }
241
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700242 /* now check the input usages */
243 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
244 list_for_each_entry(hreport, &rep_enum->report_list, list) {
245
246 if (!hreport->maxfield)
247 continue;
248
249 for (i = 0; i < hreport->maxfield; i++)
250 for (j = 0; j < hreport->field[i]->maxusage; j++)
251 wacom_usage_mapping(hdev, hreport->field[i],
252 hreport->field[i]->usage + j);
253 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500254}
255
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700256static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
257 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700258{
259 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700260 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700261
Jason Gereckefe494bc2012-10-03 17:25:35 -0700262 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700263 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800264 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700265
Jason Gereckefe494bc2012-10-03 17:25:35 -0700266 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800267 rep_data[0] = report_id;
268 rep_data[1] = mode;
269
Przemo Firszt296b7372014-08-06 14:00:38 -0700270 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
271 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700272 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700273 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700274 rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700275 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700276
277 kfree(rep_data);
278
279 return error < 0 ? error : 0;
280}
281
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700282static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
283 struct wacom_features *features)
284{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700285 struct wacom *wacom = hid_get_drvdata(hdev);
286 int ret;
287 u8 rep_data[2];
288
289 switch (features->type) {
290 case GRAPHIRE_BT:
291 rep_data[0] = 0x03;
292 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700293 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
294 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700295
296 if (ret >= 0) {
297 rep_data[0] = speed == 0 ? 0x05 : 0x06;
298 rep_data[1] = 0x00;
299
300 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700301 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700302
303 if (ret >= 0) {
304 wacom->wacom_wac.bt_high_speed = speed;
305 return 0;
306 }
307 }
308
309 /*
310 * Note that if the raw queries fail, it's not a hard failure
311 * and it is safe to continue
312 */
313 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
314 rep_data[0], ret);
315 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700316 case INTUOS4WL:
317 if (speed == 1)
318 wacom->wacom_wac.bt_features &= ~0x20;
319 else
320 wacom->wacom_wac.bt_features |= 0x20;
321
322 rep_data[0] = 0x03;
323 rep_data[1] = wacom->wacom_wac.bt_features;
324
Przemo Firszt296b7372014-08-06 14:00:38 -0700325 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
326 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700327 if (ret >= 0)
328 wacom->wacom_wac.bt_high_speed = speed;
329 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700330 }
331
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700332 return 0;
333}
334
Jason Gereckefe494bc2012-10-03 17:25:35 -0700335/*
336 * Switch the tablet into its most-capable mode. Wacom tablets are
337 * typically configured to power-up in a mode which sends mouse-like
338 * reports to the OS. To get absolute position, pressure data, etc.
339 * from the tablet, it is necessary to switch the tablet out of this
340 * mode and into one which sends the full range of tablet data.
341 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700342static int wacom_query_tablet_data(struct hid_device *hdev,
343 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700344{
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700345 if (hdev->bus == BUS_BLUETOOTH)
346 return wacom_bt_query_tablet_data(hdev, 1, features);
347
Jason Gereckefe494bc2012-10-03 17:25:35 -0700348 if (features->device_type == BTN_TOOL_FINGER) {
349 if (features->type > TABLETPC) {
350 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700351 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700352 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700353 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700354 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700355 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700356 } else if (features->device_type == BTN_TOOL_PEN) {
357 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700358 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700359 }
360 }
361
362 return 0;
363}
364
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700365static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700366 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800367{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700368 struct wacom *wacom = hid_get_drvdata(hdev);
369 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800370
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700371 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800372 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700373 features->x_fuzz = 4;
374 features->y_fuzz = 4;
375 features->pressure_fuzz = 0;
376 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800377
Chris Bagwelld3825d52012-03-25 23:26:11 -0700378 /*
379 * The wireless device HID is basic and layout conflicts with
380 * other tablets (monitor and touch interface can look like pen).
381 * Skip the query for this type and modify defaults based on
382 * interface number.
383 */
384 if (features->type == WIRELESS) {
385 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
386 features->device_type = 0;
387 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700388 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700389 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
390 }
391 }
392
Ping Cheng19635182012-04-29 21:09:18 -0700393 /* only devices that support touch need to retrieve the info */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700394 if (features->type < BAMBOO_PT)
395 return;
Ping Chengec67bbe2009-12-15 00:35:24 -0800396
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700397 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800398}
399
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700400struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700401 struct list_head list;
402 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700403 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700404 struct wacom_shared shared;
405};
406
407static LIST_HEAD(wacom_udev_list);
408static DEFINE_MUTEX(wacom_udev_list_lock);
409
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700410static bool wacom_are_sibling(struct hid_device *hdev,
411 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700412{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700413 struct wacom *wacom = hid_get_drvdata(hdev);
414 struct wacom_features *features = &wacom->wacom_wac.features;
415 int vid = features->oVid;
416 int pid = features->oPid;
417 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700418
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700419 if (vid == 0 && pid == 0) {
420 vid = hdev->vendor;
421 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700422 }
423
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700424 if (vid != sibling->vendor || pid != sibling->product)
425 return false;
426
427 /* Compare the physical path. */
428 n1 = strrchr(hdev->phys, '.') - hdev->phys;
429 n2 = strrchr(sibling->phys, '.') - sibling->phys;
430 if (n1 != n2 || n1 <= 0 || n2 <= 0)
431 return false;
432
433 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700434}
435
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700436static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700437{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700438 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700439
440 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700441 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700442 kref_get(&data->kref);
443 return data;
444 }
445 }
446
447 return NULL;
448}
449
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700450static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700451{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700452 struct wacom *wacom = hid_get_drvdata(hdev);
453 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
454 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700455 int retval = 0;
456
457 mutex_lock(&wacom_udev_list_lock);
458
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700459 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700460 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700461 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700462 if (!data) {
463 retval = -ENOMEM;
464 goto out;
465 }
466
467 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700468 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700469 list_add_tail(&data->list, &wacom_udev_list);
470 }
471
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700472 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700473
474out:
475 mutex_unlock(&wacom_udev_list_lock);
476 return retval;
477}
478
479static void wacom_release_shared_data(struct kref *kref)
480{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700481 struct wacom_hdev_data *data =
482 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700483
484 mutex_lock(&wacom_udev_list_lock);
485 list_del(&data->list);
486 mutex_unlock(&wacom_udev_list_lock);
487
488 kfree(data);
489}
490
491static void wacom_remove_shared_data(struct wacom_wac *wacom)
492{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700493 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700494
495 if (wacom->shared) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700496 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
Ping Cheng4492eff2010-03-19 22:18:15 -0700497 kref_put(&data->kref, wacom_release_shared_data);
498 wacom->shared = NULL;
499 }
500}
501
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700502static int wacom_led_control(struct wacom *wacom)
503{
504 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700505 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700506 unsigned char report_id = WAC_CMD_LED_CONTROL;
507 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700508
Ping Cheng912ca212014-09-10 12:41:31 -0700509 if (wacom->wacom_wac.pid) { /* wireless connected */
510 report_id = WAC_CMD_WL_LED_CONTROL;
511 buf_size = 13;
512 }
513 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700514 if (!buf)
515 return -ENOMEM;
516
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700517 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700518 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700519 /*
520 * Touch Ring and crop mark LED luminance may take on
521 * one of four values:
522 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
523 */
524 int ring_led = wacom->led.select[0] & 0x03;
525 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
526 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700527 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700528
Ping Cheng912ca212014-09-10 12:41:31 -0700529 buf[0] = report_id;
530 if (wacom->wacom_wac.pid) {
531 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
532 buf, buf_size, WAC_CMD_RETRIES);
533 buf[0] = report_id;
534 buf[4] = led_bits;
535 } else
536 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700537 }
538 else {
539 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700540
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700541 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
542 wacom->wacom_wac.features.type == WACOM_24HD)
543 led |= (wacom->led.select[1] << 4) | 0x40;
544
Ping Cheng912ca212014-09-10 12:41:31 -0700545 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700546 buf[1] = led;
547 buf[2] = wacom->led.llv;
548 buf[3] = wacom->led.hlv;
549 buf[4] = wacom->led.img_lum;
550 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700551
Ping Cheng912ca212014-09-10 12:41:31 -0700552 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700553 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700554 kfree(buf);
555
556 return retval;
557}
558
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700559static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
560 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700561{
562 unsigned char *buf;
563 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700564 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700565
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700566 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700567 if (!buf)
568 return -ENOMEM;
569
570 /* Send 'start' command */
571 buf[0] = WAC_CMD_ICON_START;
572 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700573 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
574 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700575 if (retval < 0)
576 goto out;
577
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700578 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700579 buf[1] = button_id & 0x07;
580 for (i = 0; i < 4; i++) {
581 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700582 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700583
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700584 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700585 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700586 if (retval < 0)
587 break;
588 }
589
590 /* Send 'stop' */
591 buf[0] = WAC_CMD_ICON_START;
592 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700593 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
594 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700595
596out:
597 kfree(buf);
598 return retval;
599}
600
Ping Cheng09e7d942011-10-04 23:51:14 -0700601static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700602 const char *buf, size_t count)
603{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700604 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700605 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700606 unsigned int id;
607 int err;
608
609 err = kstrtouint(buf, 10, &id);
610 if (err)
611 return err;
612
613 mutex_lock(&wacom->lock);
614
Ping Cheng09e7d942011-10-04 23:51:14 -0700615 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700616 err = wacom_led_control(wacom);
617
618 mutex_unlock(&wacom->lock);
619
620 return err < 0 ? err : count;
621}
622
Ping Cheng09e7d942011-10-04 23:51:14 -0700623#define DEVICE_LED_SELECT_ATTR(SET_ID) \
624static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
625 struct device_attribute *attr, const char *buf, size_t count) \
626{ \
627 return wacom_led_select_store(dev, SET_ID, buf, count); \
628} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700629static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
630 struct device_attribute *attr, char *buf) \
631{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700632 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700633 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700634 return scnprintf(buf, PAGE_SIZE, "%d\n", \
635 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700636} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700637static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700638 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700639 wacom_led##SET_ID##_select_store)
640
641DEVICE_LED_SELECT_ATTR(0);
642DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700643
644static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
645 const char *buf, size_t count)
646{
647 unsigned int value;
648 int err;
649
650 err = kstrtouint(buf, 10, &value);
651 if (err)
652 return err;
653
654 mutex_lock(&wacom->lock);
655
656 *dest = value & 0x7f;
657 err = wacom_led_control(wacom);
658
659 mutex_unlock(&wacom->lock);
660
661 return err < 0 ? err : count;
662}
663
664#define DEVICE_LUMINANCE_ATTR(name, field) \
665static ssize_t wacom_##name##_luminance_store(struct device *dev, \
666 struct device_attribute *attr, const char *buf, size_t count) \
667{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700668 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700669 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700670 \
671 return wacom_luminance_store(wacom, &wacom->led.field, \
672 buf, count); \
673} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700674static ssize_t wacom_##name##_luminance_show(struct device *dev, \
675 struct device_attribute *attr, char *buf) \
676{ \
677 struct wacom *wacom = dev_get_drvdata(dev); \
678 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
679} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700680static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700681 wacom_##name##_luminance_show, \
682 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700683
684DEVICE_LUMINANCE_ATTR(status0, llv);
685DEVICE_LUMINANCE_ATTR(status1, hlv);
686DEVICE_LUMINANCE_ATTR(buttons, img_lum);
687
688static ssize_t wacom_button_image_store(struct device *dev, int button_id,
689 const char *buf, size_t count)
690{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700691 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700692 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700693 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700694 unsigned len;
695 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700696
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700697 if (hdev->bus == BUS_BLUETOOTH) {
698 len = 256;
699 xfer_id = WAC_CMD_ICON_BT_XFER;
700 } else {
701 len = 1024;
702 xfer_id = WAC_CMD_ICON_XFER;
703 }
704
705 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700706 return -EINVAL;
707
708 mutex_lock(&wacom->lock);
709
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700710 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700711
712 mutex_unlock(&wacom->lock);
713
714 return err < 0 ? err : count;
715}
716
717#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
718static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
719 struct device_attribute *attr, const char *buf, size_t count) \
720{ \
721 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
722} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700723static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700724 NULL, wacom_btnimg##BUTTON_ID##_store)
725
726DEVICE_BTNIMG_ATTR(0);
727DEVICE_BTNIMG_ATTR(1);
728DEVICE_BTNIMG_ATTR(2);
729DEVICE_BTNIMG_ATTR(3);
730DEVICE_BTNIMG_ATTR(4);
731DEVICE_BTNIMG_ATTR(5);
732DEVICE_BTNIMG_ATTR(6);
733DEVICE_BTNIMG_ATTR(7);
734
Ping Cheng09e7d942011-10-04 23:51:14 -0700735static struct attribute *cintiq_led_attrs[] = {
736 &dev_attr_status_led0_select.attr,
737 &dev_attr_status_led1_select.attr,
738 NULL
739};
740
741static struct attribute_group cintiq_led_attr_group = {
742 .name = "wacom_led",
743 .attrs = cintiq_led_attrs,
744};
745
746static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700747 &dev_attr_status0_luminance.attr,
748 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700749 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700750 &dev_attr_buttons_luminance.attr,
751 &dev_attr_button0_rawimg.attr,
752 &dev_attr_button1_rawimg.attr,
753 &dev_attr_button2_rawimg.attr,
754 &dev_attr_button3_rawimg.attr,
755 &dev_attr_button4_rawimg.attr,
756 &dev_attr_button5_rawimg.attr,
757 &dev_attr_button6_rawimg.attr,
758 &dev_attr_button7_rawimg.attr,
759 NULL
760};
761
Ping Cheng09e7d942011-10-04 23:51:14 -0700762static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700763 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700764 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700765};
766
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700767static struct attribute *intuos5_led_attrs[] = {
768 &dev_attr_status0_luminance.attr,
769 &dev_attr_status_led0_select.attr,
770 NULL
771};
772
773static struct attribute_group intuos5_led_attr_group = {
774 .name = "wacom_led",
775 .attrs = intuos5_led_attrs,
776};
777
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700778static int wacom_initialize_leds(struct wacom *wacom)
779{
780 int error;
781
Ping Cheng09e7d942011-10-04 23:51:14 -0700782 /* Initialize default values */
783 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700784 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700785 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700786 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700787 case INTUOS4L:
788 wacom->led.select[0] = 0;
789 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700790 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700791 wacom->led.hlv = 20;
792 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700793 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700794 &intuos4_led_attr_group);
795 break;
796
Jason Gerecke246835f2011-12-12 00:12:04 -0800797 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700798 case WACOM_21UX2:
799 wacom->led.select[0] = 0;
800 wacom->led.select[1] = 0;
801 wacom->led.llv = 0;
802 wacom->led.hlv = 0;
803 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700804
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700805 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700806 &cintiq_led_attr_group);
807 break;
808
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700809 case INTUOS5S:
810 case INTUOS5:
811 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700812 case INTUOSPS:
813 case INTUOSPM:
814 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700815 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
816 wacom->led.select[0] = 0;
817 wacom->led.select[1] = 0;
818 wacom->led.llv = 32;
819 wacom->led.hlv = 0;
820 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700821
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700822 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700823 &intuos5_led_attr_group);
824 } else
825 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700826 break;
827
Ping Cheng09e7d942011-10-04 23:51:14 -0700828 default:
829 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700830 }
831
Ping Cheng09e7d942011-10-04 23:51:14 -0700832 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700833 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700834 "cannot create sysfs group err: %d\n", error);
835 return error;
836 }
837 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700838 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700839
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700840 return 0;
841}
842
843static void wacom_destroy_leds(struct wacom *wacom)
844{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700845 if (!wacom->led_initialized)
846 return;
847
848 wacom->led_initialized = false;
849
Ping Cheng09e7d942011-10-04 23:51:14 -0700850 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700851 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700852 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700853 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700854 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700855 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700856 &intuos4_led_attr_group);
857 break;
858
Jason Gerecke246835f2011-12-12 00:12:04 -0800859 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700860 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700861 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700862 &cintiq_led_attr_group);
863 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700864
865 case INTUOS5S:
866 case INTUOS5:
867 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700868 case INTUOSPS:
869 case INTUOSPM:
870 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700871 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700872 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700873 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700874 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700875 }
876}
877
Chris Bagwella1d552c2012-03-25 23:26:30 -0700878static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700879 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700880 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700881 POWER_SUPPLY_PROP_CAPACITY
882};
883
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700884static enum power_supply_property wacom_ac_props[] = {
885 POWER_SUPPLY_PROP_PRESENT,
886 POWER_SUPPLY_PROP_ONLINE,
887 POWER_SUPPLY_PROP_SCOPE,
888};
889
Chris Bagwella1d552c2012-03-25 23:26:30 -0700890static int wacom_battery_get_property(struct power_supply *psy,
891 enum power_supply_property psp,
892 union power_supply_propval *val)
893{
894 struct wacom *wacom = container_of(psy, struct wacom, battery);
895 int ret = 0;
896
897 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700898 case POWER_SUPPLY_PROP_SCOPE:
899 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
900 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700901 case POWER_SUPPLY_PROP_CAPACITY:
902 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700903 wacom->wacom_wac.battery_capacity;
904 break;
905 case POWER_SUPPLY_PROP_STATUS:
906 if (wacom->wacom_wac.bat_charging)
907 val->intval = POWER_SUPPLY_STATUS_CHARGING;
908 else if (wacom->wacom_wac.battery_capacity == 100 &&
909 wacom->wacom_wac.ps_connected)
910 val->intval = POWER_SUPPLY_STATUS_FULL;
911 else
912 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700913 break;
914 default:
915 ret = -EINVAL;
916 break;
917 }
918
919 return ret;
920}
921
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700922static int wacom_ac_get_property(struct power_supply *psy,
923 enum power_supply_property psp,
924 union power_supply_propval *val)
925{
926 struct wacom *wacom = container_of(psy, struct wacom, ac);
927 int ret = 0;
928
929 switch (psp) {
930 case POWER_SUPPLY_PROP_PRESENT:
931 /* fall through */
932 case POWER_SUPPLY_PROP_ONLINE:
933 val->intval = wacom->wacom_wac.ps_connected;
934 break;
935 case POWER_SUPPLY_PROP_SCOPE:
936 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
937 break;
938 default:
939 ret = -EINVAL;
940 break;
941 }
942 return ret;
943}
944
Chris Bagwella1d552c2012-03-25 23:26:30 -0700945static int wacom_initialize_battery(struct wacom *wacom)
946{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -0700947 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700948 int error;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -0700949 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700950
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700951 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -0700952 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700953
Chris Bagwella1d552c2012-03-25 23:26:30 -0700954 wacom->battery.properties = wacom_battery_props;
955 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
956 wacom->battery.get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -0700957 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
958 wacom->battery.name = wacom->wacom_wac.bat_name;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700959 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
960 wacom->battery.use_for_apm = 0;
961
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700962 wacom->ac.properties = wacom_ac_props;
963 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
964 wacom->ac.get_property = wacom_ac_get_property;
965 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
966 wacom->ac.name = wacom->wacom_wac.ac_name;
967 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
968 wacom->ac.use_for_apm = 0;
969
Benjamin Tissoiresdd3181a2014-07-24 13:01:40 -0700970 error = power_supply_register(&wacom->hdev->dev,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700971 &wacom->battery);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700972 if (error)
973 return error;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700974
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700975 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
976
977 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
978 if (error) {
979 power_supply_unregister(&wacom->battery);
980 return error;
981 }
982
983 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -0700984 }
985
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700986 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700987}
988
989static void wacom_destroy_battery(struct wacom *wacom)
990{
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700991 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
992 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -0700993 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700994 wacom->battery.dev = NULL;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700995 power_supply_unregister(&wacom->ac);
996 wacom->ac.dev = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700997 }
Chris Bagwella1d552c2012-03-25 23:26:30 -0700998}
999
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001000static ssize_t wacom_show_speed(struct device *dev,
1001 struct device_attribute
1002 *attr, char *buf)
1003{
1004 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1005 struct wacom *wacom = hid_get_drvdata(hdev);
1006
1007 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1008}
1009
1010static ssize_t wacom_store_speed(struct device *dev,
1011 struct device_attribute *attr,
1012 const char *buf, size_t count)
1013{
1014 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1015 struct wacom *wacom = hid_get_drvdata(hdev);
1016 u8 new_speed;
1017
1018 if (kstrtou8(buf, 0, &new_speed))
1019 return -EINVAL;
1020
1021 if (new_speed != 0 && new_speed != 1)
1022 return -EINVAL;
1023
1024 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1025
1026 return count;
1027}
1028
Ping Chenge0984bc2014-09-10 12:40:05 -07001029static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001030 wacom_show_speed, wacom_store_speed);
1031
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001032static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001033{
1034 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001035 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001036 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001037
1038 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001039 if (!input_dev)
1040 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001041
1042 input_dev->name = wacom_wac->name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001043 input_dev->phys = hdev->phys;
1044 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001045 input_dev->open = wacom_open;
1046 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001047 input_dev->uniq = hdev->uniq;
1048 input_dev->id.bustype = hdev->bus;
1049 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001050 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001051 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001052 input_set_drvdata(input_dev, wacom);
1053
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001054 return input_dev;
1055}
1056
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001057static void wacom_unregister_inputs(struct wacom *wacom)
1058{
1059 if (wacom->wacom_wac.input)
1060 input_unregister_device(wacom->wacom_wac.input);
1061 if (wacom->wacom_wac.pad_input)
1062 input_unregister_device(wacom->wacom_wac.pad_input);
1063 wacom->wacom_wac.input = NULL;
1064 wacom->wacom_wac.pad_input = NULL;
Ping Cheng912ca212014-09-10 12:41:31 -07001065 wacom_destroy_leds(wacom);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001066}
1067
1068static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001069{
1070 struct input_dev *input_dev, *pad_input_dev;
1071 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1072 int error;
1073
1074 input_dev = wacom_allocate_input(wacom);
1075 pad_input_dev = wacom_allocate_input(wacom);
1076 if (!input_dev || !pad_input_dev) {
1077 error = -ENOMEM;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001078 goto fail_allocate_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001079 }
1080
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001081 wacom_wac->input = input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001082 wacom_wac->pad_input = pad_input_dev;
1083 wacom_wac->pad_input->name = wacom_wac->pad_name;
1084
Ping Cheng19635182012-04-29 21:09:18 -07001085 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1086 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001087 goto fail_input_cap;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001088
1089 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001090 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001091 goto fail_register_input;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001092
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001093 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1094 if (error) {
1095 /* no pad in use on this interface */
1096 input_free_device(pad_input_dev);
1097 wacom_wac->pad_input = NULL;
1098 pad_input_dev = NULL;
1099 } else {
1100 error = input_register_device(pad_input_dev);
1101 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001102 goto fail_register_pad_input;
Ping Cheng912ca212014-09-10 12:41:31 -07001103
1104 error = wacom_initialize_leds(wacom);
1105 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001106 goto fail_leds;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001107 }
1108
Ping Cheng19635182012-04-29 21:09:18 -07001109 return 0;
1110
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001111fail_leds:
Ping Cheng912ca212014-09-10 12:41:31 -07001112 input_unregister_device(pad_input_dev);
1113 pad_input_dev = NULL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001114fail_register_pad_input:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001115 input_unregister_device(input_dev);
1116 input_dev = NULL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001117fail_register_input:
1118fail_input_cap:
Ping Cheng19635182012-04-29 21:09:18 -07001119 wacom_wac->input = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001120 wacom_wac->pad_input = NULL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001121fail_allocate_input:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001122 if (input_dev)
1123 input_free_device(input_dev);
1124 if (pad_input_dev)
1125 input_free_device(pad_input_dev);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001126 return error;
1127}
1128
Chris Bagwell16bf2882012-03-25 23:26:20 -07001129static void wacom_wireless_work(struct work_struct *work)
1130{
1131 struct wacom *wacom = container_of(work, struct wacom, work);
1132 struct usb_device *usbdev = wacom->usbdev;
1133 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001134 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001135 struct wacom *wacom1, *wacom2;
1136 struct wacom_wac *wacom_wac1, *wacom_wac2;
1137 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001138
1139 /*
1140 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001141 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001142 */
1143
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001144 wacom_destroy_battery(wacom);
1145
Chris Bagwell16bf2882012-03-25 23:26:20 -07001146 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001147 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1148 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001149 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001150 wacom_unregister_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001151
1152 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001153 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1154 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001155 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001156 wacom_unregister_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001157
1158 if (wacom_wac->pid == 0) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001159 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001160 wacom_wac1->shared->type = 0;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001161 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001162 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001163
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001164 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001165 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001166
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001167 while (id->bus) {
1168 if (id->vendor == USB_VENDOR_ID_WACOM &&
1169 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001170 break;
1171 id++;
1172 }
1173
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001174 if (!id->bus) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001175 hid_info(wacom->hdev, "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001176 return;
1177 }
1178
1179 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001180 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001181 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001182 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001183 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1184 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001185 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1186 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001187 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1188 wacom_wac1->shared->type = wacom_wac1->features.type;
Ping Cheng912ca212014-09-10 12:41:31 -07001189 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001190 error = wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001191 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001192 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001193
1194 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001195 if (wacom_wac1->features.touch_max ||
1196 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001197 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001198 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001199 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1200 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1201 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1202 if (wacom_wac2->features.touch_max)
1203 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1204 "%s (WL) Finger",wacom_wac2->features.name);
1205 else
1206 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1207 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001208 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1209 "%s (WL) Pad", wacom_wac2->features.name);
Ping Cheng912ca212014-09-10 12:41:31 -07001210 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001211 error = wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001212 if (error)
1213 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001214
1215 if (wacom_wac1->features.type == INTUOSHT &&
1216 wacom_wac1->features.touch_max)
1217 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001218 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001219
1220 error = wacom_initialize_battery(wacom);
1221 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001222 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001223 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001224
1225 return;
1226
Ping Cheng57bcfce2013-10-15 23:44:00 -07001227fail:
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001228 wacom_unregister_inputs(wacom1);
1229 wacom_unregister_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001230 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001231}
1232
Ping Cheng401d7d12013-07-28 00:38:54 -07001233/*
1234 * Not all devices report physical dimensions from HID.
1235 * Compute the default from hardcoded logical dimension
1236 * and resolution before driver overwrites them.
1237 */
1238static void wacom_set_default_phy(struct wacom_features *features)
1239{
1240 if (features->x_resolution) {
1241 features->x_phy = (features->x_max * 100) /
1242 features->x_resolution;
1243 features->y_phy = (features->y_max * 100) /
1244 features->y_resolution;
1245 }
1246}
1247
1248static void wacom_calculate_res(struct wacom_features *features)
1249{
1250 features->x_resolution = wacom_calc_hid_res(features->x_max,
1251 features->x_phy,
1252 features->unit,
1253 features->unitExpo);
1254 features->y_resolution = wacom_calc_hid_res(features->y_max,
1255 features->y_phy,
1256 features->unit,
1257 features->unitExpo);
1258}
1259
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001260static int wacom_hid_report_len(struct hid_report *report)
1261{
1262 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1263 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1264}
1265
1266static size_t wacom_compute_pktlen(struct hid_device *hdev)
1267{
1268 struct hid_report_enum *report_enum;
1269 struct hid_report *report;
1270 size_t size = 0;
1271
1272 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1273
1274 list_for_each_entry(report, &report_enum->report_list, list) {
1275 size_t report_size = wacom_hid_report_len(report);
1276 if (report_size > size)
1277 size = report_size;
1278 }
1279
1280 return size;
1281}
1282
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001283static int wacom_probe(struct hid_device *hdev,
1284 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001285{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001286 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001287 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001288 struct wacom *wacom;
1289 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001290 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001291 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001292
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001293 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001294 return -EINVAL;
1295
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -04001296 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1297
Ping Cheng3bea7332006-07-13 18:01:36 -07001298 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001299 if (!wacom)
1300 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001301
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001302 hid_set_drvdata(hdev, wacom);
1303 wacom->hdev = hdev;
1304
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001305 /* ask for the report descriptor to be loaded by HID */
1306 error = hid_parse(hdev);
1307 if (error) {
1308 hid_err(hdev, "parse failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001309 goto fail_parse;
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001310 }
1311
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001312 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001313 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001314 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001315 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001316 if (features->pktlen > WACOM_PKGLEN_MAX) {
1317 error = -EINVAL;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001318 goto fail_pktlen;
Jason Childse33da8a2010-02-17 22:38:31 -08001319 }
1320
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001321 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1322 error = -ENODEV;
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001323 goto fail_type;
Jason Childse33da8a2010-02-17 22:38:31 -08001324 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001325
Ping Cheng3bea7332006-07-13 18:01:36 -07001326 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001327 wacom->intf = intf;
1328 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001329 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001330
Ping Cheng401d7d12013-07-28 00:38:54 -07001331 /* set the default size in case we do not get them from hid */
1332 wacom_set_default_phy(features);
1333
Ping Chengf393ee22012-04-29 21:09:17 -07001334 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001335 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -05001336
Jason Gereckeae584ca2012-04-03 15:50:40 -07001337 /*
1338 * Intuos5 has no useful data about its touch interface in its
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001339 * HID descriptor. If this is the touch interface (PacketSize
Jason Gereckeae584ca2012-04-03 15:50:40 -07001340 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1341 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001342 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001343 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001344 features->device_type = BTN_TOOL_FINGER;
Jason Gereckeae584ca2012-04-03 15:50:40 -07001345
Jason Gereckeae584ca2012-04-03 15:50:40 -07001346 features->x_max = 4096;
1347 features->y_max = 4096;
1348 } else {
1349 features->device_type = BTN_TOOL_PEN;
1350 }
1351 }
1352
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001353 /*
1354 * Same thing for Bamboo 3rd gen.
1355 */
1356 if ((features->type == BAMBOO_PT) &&
1357 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1358 (features->device_type == BTN_TOOL_PEN)) {
1359 features->device_type = BTN_TOOL_FINGER;
1360
1361 features->x_max = 4096;
1362 features->y_max = 4096;
1363 }
1364
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001365 if (hdev->bus == BUS_BLUETOOTH)
1366 features->quirks |= WACOM_QUIRK_BATTERY;
1367
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001368 wacom_setup_device_quirks(features);
1369
Ping Cheng401d7d12013-07-28 00:38:54 -07001370 /* set unit to "100th of a mm" for devices not reported by HID */
1371 if (!features->unit) {
1372 features->unit = 0x11;
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001373 features->unitExpo = -3;
Ping Cheng401d7d12013-07-28 00:38:54 -07001374 }
1375 wacom_calculate_res(features);
1376
Ping Cheng49b764a2010-02-20 00:53:49 -08001377 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001378 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1379 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001380
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001381 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001382 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001383 if (features->device_type != BTN_TOOL_FINGER)
1384 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1385 else if (features->touch_max)
1386 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1387 else
1388 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001389
Benjamin Tissoires4451e082014-07-24 13:01:05 -07001390 error = wacom_add_shared_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001391 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001392 goto fail_shared_data;
Ping Cheng49b764a2010-02-20 00:53:49 -08001393 }
1394
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001395 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1396 (features->quirks & WACOM_QUIRK_BATTERY)) {
1397 error = wacom_initialize_battery(wacom);
1398 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001399 goto fail_battery;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001400 }
1401
Chris Bagwelld3825d52012-03-25 23:26:11 -07001402 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001403 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001404 if (error)
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001405 goto fail_register_inputs;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001406 }
1407
1408 if (hdev->bus == BUS_BLUETOOTH) {
1409 error = device_create_file(&hdev->dev, &dev_attr_speed);
1410 if (error)
1411 hid_warn(hdev,
1412 "can't create sysfs speed attribute err: %d\n",
1413 error);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001414 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001415
Ping Chengec67bbe2009-12-15 00:35:24 -08001416 /* Note that if query fails it is not a hard failure */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001417 wacom_query_tablet_data(hdev, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001418
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001419 /* Regular HID work starts now */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001420 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1421 if (error) {
1422 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001423 goto fail_hw_start;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001424 }
1425
1426 if (features->quirks & WACOM_QUIRK_MONITOR)
1427 error = hid_hw_open(hdev);
1428
Ping Cheng961794a2013-12-05 12:54:53 -08001429 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1430 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1431 wacom_wac->shared->touch_input = wacom_wac->input;
1432 }
1433
Ping Cheng3bea7332006-07-13 18:01:36 -07001434 return 0;
1435
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001436fail_hw_start:
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001437 wacom_unregister_inputs(wacom);
Benjamin Tissoires7fefeec2014-09-23 12:08:05 -04001438 if (hdev->bus == BUS_BLUETOOTH)
1439 device_remove_file(&hdev->dev, &dev_attr_speed);
1440fail_register_inputs:
1441 wacom_unregister_inputs(wacom);
1442 wacom_destroy_battery(wacom);
1443fail_battery:
1444 wacom_remove_shared_data(wacom_wac);
1445fail_shared_data:
1446fail_type:
1447fail_pktlen:
1448fail_parse:
1449 kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001450 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001451 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001452}
1453
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001454static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001455{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001456 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001457
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001458 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001459
Chris Bagwell16bf2882012-03-25 23:26:20 -07001460 cancel_work_sync(&wacom->work);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001461 wacom_unregister_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001462 if (hdev->bus == BUS_BLUETOOTH)
1463 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001464 wacom_destroy_battery(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001465 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001466
1467 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001468 kfree(wacom);
1469}
1470
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001471#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001472static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001473{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001474 struct wacom *wacom = hid_get_drvdata(hdev);
1475 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001476
1477 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001478
1479 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001480 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001481 wacom_led_control(wacom);
1482
Oliver Neukume7224092008-04-15 01:31:57 -04001483 mutex_unlock(&wacom->lock);
1484
1485 return 0;
1486}
1487
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001488static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001489{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001490 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001491}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001492#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001493
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001494static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001495 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001496 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001497 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001498 .remove = wacom_remove,
1499#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001500 .resume = wacom_resume,
1501 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001502#endif
1503 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001504};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001505module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001506
1507MODULE_VERSION(DRIVER_VERSION);
1508MODULE_AUTHOR(DRIVER_AUTHOR);
1509MODULE_DESCRIPTION(DRIVER_DESC);
1510MODULE_LICENSE(DRIVER_LICENSE);