blob: c6970e6283b449d3e6484a6a2cb60beab0f5fd19 [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
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;
Chris Bagwell41343612011-10-26 22:32:52 -0700112
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700113 switch (usage->hid) {
114 case HID_DG_CONTACTMAX:
115 /* leave touch_max as is if predefined */
116 if (!features->touch_max)
117 features->touch_max = field->value[0];
118 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700119 }
120}
121
Chris Bagwell428f8582011-10-26 22:26:59 -0700122/*
123 * Interface Descriptor of wacom devices can be incomplete and
124 * inconsistent so wacom_features table is used to store stylus
125 * device's packet lengths, various maximum values, and tablet
126 * resolution based on product ID's.
127 *
128 * For devices that contain 2 interfaces, wacom_features table is
129 * inaccurate for the touch interface. Since the Interface Descriptor
130 * for touch interfaces has pretty complete data, this function exists
131 * to query tablet for this missing information instead of hard coding in
132 * an additional table.
133 *
134 * A typical Interface Descriptor for a stylus will contain a
135 * boot mouse application collection that is not of interest and this
136 * function will ignore it.
137 *
138 * It also contains a digitizer application collection that also is not
139 * of interest since any information it contains would be duplicate
140 * of what is in wacom_features. Usually it defines a report of an array
141 * of bytes that could be used as max length of the stylus packet returned.
142 * If it happens to define a Digitizer-Stylus Physical Collection then
143 * the X and Y logical values contain valid data but it is ignored.
144 *
145 * A typical Interface Descriptor for a touch interface will contain a
146 * Digitizer-Finger Physical Collection which will define both logical
147 * X/Y maximum as well as the physical size of tablet. Since touch
148 * interfaces haven't supported pressure or distance, this is enough
149 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700150 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700151 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
152 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700153 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700154static void wacom_usage_mapping(struct hid_device *hdev,
155 struct hid_field *field, struct hid_usage *usage)
156{
157 struct wacom *wacom = hid_get_drvdata(hdev);
158 struct wacom_features *features = &wacom->wacom_wac.features;
159 bool finger = (field->logical == HID_DG_FINGER) ||
160 (field->physical == HID_DG_FINGER);
161 bool pen = (field->logical == HID_DG_STYLUS) ||
162 (field->physical == HID_DG_STYLUS);
163
164 /*
165 * Requiring Stylus Usage will ignore boot mouse
166 * X/Y values and some cases of invalid Digitizer X/Y
167 * values commonly reported.
168 */
169 if (!pen && !finger)
170 return;
171
172 if (finger && !features->touch_max)
173 /* touch device at least supports one touch point */
174 features->touch_max = 1;
175
176 switch (usage->hid) {
177 case HID_GD_X:
178 features->x_max = field->logical_maximum;
179 if (finger) {
180 features->device_type = BTN_TOOL_FINGER;
181 features->x_phy = field->physical_maximum;
182 if (features->type != BAMBOO_PT) {
183 features->unit = field->unit;
184 features->unitExpo = field->unit_exponent;
185 }
186 } else {
187 features->device_type = BTN_TOOL_PEN;
188 }
189 break;
190 case HID_GD_Y:
191 features->y_max = field->logical_maximum;
192 if (finger) {
193 features->y_phy = field->physical_maximum;
194 if (features->type != BAMBOO_PT) {
195 features->unit = field->unit;
196 features->unitExpo = field->unit_exponent;
197 }
198 }
199 break;
200 case HID_DG_TIPPRESSURE:
201 if (pen)
202 features->pressure_max = field->logical_maximum;
203 break;
204 }
205}
206
207static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800208 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500209{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700210 struct hid_report_enum *rep_enum;
211 struct hid_report *hreport;
212 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500213
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700214 /* check features first */
215 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
216 list_for_each_entry(hreport, &rep_enum->report_list, list) {
217 for (i = 0; i < hreport->maxfield; i++) {
218 /* Ignore if report count is out of bounds. */
219 if (hreport->field[i]->report_count < 1)
220 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500221
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700222 for (j = 0; j < hreport->field[i]->maxusage; j++) {
223 wacom_feature_mapping(hdev, hreport->field[i],
224 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500225 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500226 }
227 }
228
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700229 /* now check the input usages */
230 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
231 list_for_each_entry(hreport, &rep_enum->report_list, list) {
232
233 if (!hreport->maxfield)
234 continue;
235
236 for (i = 0; i < hreport->maxfield; i++)
237 for (j = 0; j < hreport->field[i]->maxusage; j++)
238 wacom_usage_mapping(hdev, hreport->field[i],
239 hreport->field[i]->usage + j);
240 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500241}
242
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700243static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
244 int length, int mode)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700245{
246 unsigned char *rep_data;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700247 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700248
Jason Gereckefe494bc2012-10-03 17:25:35 -0700249 rep_data = kzalloc(length, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700250 if (!rep_data)
Ping Chengec67bbe2009-12-15 00:35:24 -0800251 return error;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700252
Jason Gereckefe494bc2012-10-03 17:25:35 -0700253 do {
Chris Bagwell9937c022013-01-23 19:37:34 -0800254 rep_data[0] = report_id;
255 rep_data[1] = mode;
256
Przemo Firszt296b7372014-08-06 14:00:38 -0700257 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
258 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700259 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700260 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700261 rep_data, length, 1);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700262 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700263
264 kfree(rep_data);
265
266 return error < 0 ? error : 0;
267}
268
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700269static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
270 struct wacom_features *features)
271{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700272 struct wacom *wacom = hid_get_drvdata(hdev);
273 int ret;
274 u8 rep_data[2];
275
276 switch (features->type) {
277 case GRAPHIRE_BT:
278 rep_data[0] = 0x03;
279 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700280 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
281 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700282
283 if (ret >= 0) {
284 rep_data[0] = speed == 0 ? 0x05 : 0x06;
285 rep_data[1] = 0x00;
286
287 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700288 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700289
290 if (ret >= 0) {
291 wacom->wacom_wac.bt_high_speed = speed;
292 return 0;
293 }
294 }
295
296 /*
297 * Note that if the raw queries fail, it's not a hard failure
298 * and it is safe to continue
299 */
300 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
301 rep_data[0], ret);
302 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700303 case INTUOS4WL:
304 if (speed == 1)
305 wacom->wacom_wac.bt_features &= ~0x20;
306 else
307 wacom->wacom_wac.bt_features |= 0x20;
308
309 rep_data[0] = 0x03;
310 rep_data[1] = wacom->wacom_wac.bt_features;
311
Przemo Firszt296b7372014-08-06 14:00:38 -0700312 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
313 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700314 if (ret >= 0)
315 wacom->wacom_wac.bt_high_speed = speed;
316 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700317 }
318
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700319 return 0;
320}
321
Jason Gereckefe494bc2012-10-03 17:25:35 -0700322/*
323 * Switch the tablet into its most-capable mode. Wacom tablets are
324 * typically configured to power-up in a mode which sends mouse-like
325 * reports to the OS. To get absolute position, pressure data, etc.
326 * from the tablet, it is necessary to switch the tablet out of this
327 * mode and into one which sends the full range of tablet data.
328 */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700329static int wacom_query_tablet_data(struct hid_device *hdev,
330 struct wacom_features *features)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700331{
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700332 if (hdev->bus == BUS_BLUETOOTH)
333 return wacom_bt_query_tablet_data(hdev, 1, features);
334
Jason Gereckefe494bc2012-10-03 17:25:35 -0700335 if (features->device_type == BTN_TOOL_FINGER) {
336 if (features->type > TABLETPC) {
337 /* MT Tablet PC touch */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700338 return wacom_set_device_mode(hdev, 3, 4, 4);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700339 }
Jason Gerecke36d3c512013-09-20 09:47:35 -0700340 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700341 return wacom_set_device_mode(hdev, 18, 3, 2);
Jason Gereckeb1e42792012-10-21 00:38:04 -0700342 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700343 } else if (features->device_type == BTN_TOOL_PEN) {
344 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700345 return wacom_set_device_mode(hdev, 2, 2, 2);
Jason Gereckefe494bc2012-10-03 17:25:35 -0700346 }
347 }
348
349 return 0;
350}
351
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700352static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700353 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800354{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700355 struct wacom *wacom = hid_get_drvdata(hdev);
356 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800357
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700358 /* default features */
Ping Chengec67bbe2009-12-15 00:35:24 -0800359 features->device_type = BTN_TOOL_PEN;
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700360 features->x_fuzz = 4;
361 features->y_fuzz = 4;
362 features->pressure_fuzz = 0;
363 features->distance_fuzz = 0;
Ping Chengec67bbe2009-12-15 00:35:24 -0800364
Chris Bagwelld3825d52012-03-25 23:26:11 -0700365 /*
366 * The wireless device HID is basic and layout conflicts with
367 * other tablets (monitor and touch interface can look like pen).
368 * Skip the query for this type and modify defaults based on
369 * interface number.
370 */
371 if (features->type == WIRELESS) {
372 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
373 features->device_type = 0;
374 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
Ping Chengadad0042012-06-28 16:48:17 -0700375 features->device_type = BTN_TOOL_FINGER;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700376 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
377 }
378 }
379
Ping Cheng19635182012-04-29 21:09:18 -0700380 /* only devices that support touch need to retrieve the info */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700381 if (features->type < BAMBOO_PT)
382 return;
Ping Chengec67bbe2009-12-15 00:35:24 -0800383
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700384 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800385}
386
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700387struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700388 struct list_head list;
389 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700390 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700391 struct wacom_shared shared;
392};
393
394static LIST_HEAD(wacom_udev_list);
395static DEFINE_MUTEX(wacom_udev_list_lock);
396
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700397static bool wacom_are_sibling(struct hid_device *hdev,
398 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700399{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700400 struct wacom *wacom = hid_get_drvdata(hdev);
401 struct wacom_features *features = &wacom->wacom_wac.features;
402 int vid = features->oVid;
403 int pid = features->oPid;
404 int n1,n2;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700405
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700406 if (vid == 0 && pid == 0) {
407 vid = hdev->vendor;
408 pid = hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700409 }
410
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700411 if (vid != sibling->vendor || pid != sibling->product)
412 return false;
413
414 /* Compare the physical path. */
415 n1 = strrchr(hdev->phys, '.') - hdev->phys;
416 n2 = strrchr(sibling->phys, '.') - sibling->phys;
417 if (n1 != n2 || n1 <= 0 || n2 <= 0)
418 return false;
419
420 return !strncmp(hdev->phys, sibling->phys, n1);
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700421}
422
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700423static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700424{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700425 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700426
427 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700428 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700429 kref_get(&data->kref);
430 return data;
431 }
432 }
433
434 return NULL;
435}
436
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700437static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700438{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700439 struct wacom *wacom = hid_get_drvdata(hdev);
440 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
441 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700442 int retval = 0;
443
444 mutex_lock(&wacom_udev_list_lock);
445
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700446 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700447 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700448 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700449 if (!data) {
450 retval = -ENOMEM;
451 goto out;
452 }
453
454 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700455 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700456 list_add_tail(&data->list, &wacom_udev_list);
457 }
458
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700459 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700460
461out:
462 mutex_unlock(&wacom_udev_list_lock);
463 return retval;
464}
465
466static void wacom_release_shared_data(struct kref *kref)
467{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700468 struct wacom_hdev_data *data =
469 container_of(kref, struct wacom_hdev_data, kref);
Ping Cheng4492eff2010-03-19 22:18:15 -0700470
471 mutex_lock(&wacom_udev_list_lock);
472 list_del(&data->list);
473 mutex_unlock(&wacom_udev_list_lock);
474
475 kfree(data);
476}
477
478static void wacom_remove_shared_data(struct wacom_wac *wacom)
479{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700480 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700481
482 if (wacom->shared) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700483 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
Ping Cheng4492eff2010-03-19 22:18:15 -0700484 kref_put(&data->kref, wacom_release_shared_data);
485 wacom->shared = NULL;
486 }
487}
488
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700489static int wacom_led_control(struct wacom *wacom)
490{
491 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700492 int retval;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700493
494 buf = kzalloc(9, GFP_KERNEL);
495 if (!buf)
496 return -ENOMEM;
497
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700498 if (wacom->wacom_wac.features.type >= INTUOS5S &&
Ping Cheng9a35c412013-09-20 09:51:56 -0700499 wacom->wacom_wac.features.type <= INTUOSPL) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700500 /*
501 * Touch Ring and crop mark LED luminance may take on
502 * one of four values:
503 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
504 */
505 int ring_led = wacom->led.select[0] & 0x03;
506 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
507 int crop_lum = 0;
Ping Cheng09e7d942011-10-04 23:51:14 -0700508
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700509 buf[0] = WAC_CMD_LED_CONTROL;
510 buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
511 }
512 else {
513 int led = wacom->led.select[0] | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700514
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700515 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
516 wacom->wacom_wac.features.type == WACOM_24HD)
517 led |= (wacom->led.select[1] << 4) | 0x40;
518
519 buf[0] = WAC_CMD_LED_CONTROL;
520 buf[1] = led;
521 buf[2] = wacom->led.llv;
522 buf[3] = wacom->led.hlv;
523 buf[4] = wacom->led.img_lum;
524 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700525
Przemo Firszt296b7372014-08-06 14:00:38 -0700526 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 9,
527 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700528 kfree(buf);
529
530 return retval;
531}
532
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700533static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
534 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700535{
536 unsigned char *buf;
537 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700538 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700539
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700540 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700541 if (!buf)
542 return -ENOMEM;
543
544 /* Send 'start' command */
545 buf[0] = WAC_CMD_ICON_START;
546 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700547 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
548 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700549 if (retval < 0)
550 goto out;
551
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700552 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700553 buf[1] = button_id & 0x07;
554 for (i = 0; i < 4; i++) {
555 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700556 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700557
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700558 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700559 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700560 if (retval < 0)
561 break;
562 }
563
564 /* Send 'stop' */
565 buf[0] = WAC_CMD_ICON_START;
566 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700567 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
568 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700569
570out:
571 kfree(buf);
572 return retval;
573}
574
Ping Cheng09e7d942011-10-04 23:51:14 -0700575static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700576 const char *buf, size_t count)
577{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700578 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700579 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700580 unsigned int id;
581 int err;
582
583 err = kstrtouint(buf, 10, &id);
584 if (err)
585 return err;
586
587 mutex_lock(&wacom->lock);
588
Ping Cheng09e7d942011-10-04 23:51:14 -0700589 wacom->led.select[set_id] = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700590 err = wacom_led_control(wacom);
591
592 mutex_unlock(&wacom->lock);
593
594 return err < 0 ? err : count;
595}
596
Ping Cheng09e7d942011-10-04 23:51:14 -0700597#define DEVICE_LED_SELECT_ATTR(SET_ID) \
598static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
599 struct device_attribute *attr, const char *buf, size_t count) \
600{ \
601 return wacom_led_select_store(dev, SET_ID, buf, count); \
602} \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700603static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
604 struct device_attribute *attr, char *buf) \
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); \
Ping Cheng37449ad2014-09-10 12:40:30 -0700608 return scnprintf(buf, PAGE_SIZE, "%d\n", \
609 wacom->led.select[SET_ID]); \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700610} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700611static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -0700612 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -0700613 wacom_led##SET_ID##_select_store)
614
615DEVICE_LED_SELECT_ATTR(0);
616DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700617
618static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
619 const char *buf, size_t count)
620{
621 unsigned int value;
622 int err;
623
624 err = kstrtouint(buf, 10, &value);
625 if (err)
626 return err;
627
628 mutex_lock(&wacom->lock);
629
630 *dest = value & 0x7f;
631 err = wacom_led_control(wacom);
632
633 mutex_unlock(&wacom->lock);
634
635 return err < 0 ? err : count;
636}
637
638#define DEVICE_LUMINANCE_ATTR(name, field) \
639static ssize_t wacom_##name##_luminance_store(struct device *dev, \
640 struct device_attribute *attr, const char *buf, size_t count) \
641{ \
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700642 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700643 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700644 \
645 return wacom_luminance_store(wacom, &wacom->led.field, \
646 buf, count); \
647} \
Ping Cheng37449ad2014-09-10 12:40:30 -0700648static ssize_t wacom_##name##_luminance_show(struct device *dev, \
649 struct device_attribute *attr, char *buf) \
650{ \
651 struct wacom *wacom = dev_get_drvdata(dev); \
652 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
653} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700654static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -0700655 wacom_##name##_luminance_show, \
656 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700657
658DEVICE_LUMINANCE_ATTR(status0, llv);
659DEVICE_LUMINANCE_ATTR(status1, hlv);
660DEVICE_LUMINANCE_ATTR(buttons, img_lum);
661
662static ssize_t wacom_button_image_store(struct device *dev, int button_id,
663 const char *buf, size_t count)
664{
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700665 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700666 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700667 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700668 unsigned len;
669 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700670
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700671 if (hdev->bus == BUS_BLUETOOTH) {
672 len = 256;
673 xfer_id = WAC_CMD_ICON_BT_XFER;
674 } else {
675 len = 1024;
676 xfer_id = WAC_CMD_ICON_XFER;
677 }
678
679 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700680 return -EINVAL;
681
682 mutex_lock(&wacom->lock);
683
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700684 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700685
686 mutex_unlock(&wacom->lock);
687
688 return err < 0 ? err : count;
689}
690
691#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
692static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
693 struct device_attribute *attr, const char *buf, size_t count) \
694{ \
695 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
696} \
Ping Chenge0984bc2014-09-10 12:40:05 -0700697static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700698 NULL, wacom_btnimg##BUTTON_ID##_store)
699
700DEVICE_BTNIMG_ATTR(0);
701DEVICE_BTNIMG_ATTR(1);
702DEVICE_BTNIMG_ATTR(2);
703DEVICE_BTNIMG_ATTR(3);
704DEVICE_BTNIMG_ATTR(4);
705DEVICE_BTNIMG_ATTR(5);
706DEVICE_BTNIMG_ATTR(6);
707DEVICE_BTNIMG_ATTR(7);
708
Ping Cheng09e7d942011-10-04 23:51:14 -0700709static struct attribute *cintiq_led_attrs[] = {
710 &dev_attr_status_led0_select.attr,
711 &dev_attr_status_led1_select.attr,
712 NULL
713};
714
715static struct attribute_group cintiq_led_attr_group = {
716 .name = "wacom_led",
717 .attrs = cintiq_led_attrs,
718};
719
720static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700721 &dev_attr_status0_luminance.attr,
722 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -0700723 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700724 &dev_attr_buttons_luminance.attr,
725 &dev_attr_button0_rawimg.attr,
726 &dev_attr_button1_rawimg.attr,
727 &dev_attr_button2_rawimg.attr,
728 &dev_attr_button3_rawimg.attr,
729 &dev_attr_button4_rawimg.attr,
730 &dev_attr_button5_rawimg.attr,
731 &dev_attr_button6_rawimg.attr,
732 &dev_attr_button7_rawimg.attr,
733 NULL
734};
735
Ping Cheng09e7d942011-10-04 23:51:14 -0700736static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700737 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -0700738 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700739};
740
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700741static struct attribute *intuos5_led_attrs[] = {
742 &dev_attr_status0_luminance.attr,
743 &dev_attr_status_led0_select.attr,
744 NULL
745};
746
747static struct attribute_group intuos5_led_attr_group = {
748 .name = "wacom_led",
749 .attrs = intuos5_led_attrs,
750};
751
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700752static int wacom_initialize_leds(struct wacom *wacom)
753{
754 int error;
755
Ping Cheng09e7d942011-10-04 23:51:14 -0700756 /* Initialize default values */
757 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700758 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700759 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700760 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700761 case INTUOS4L:
762 wacom->led.select[0] = 0;
763 wacom->led.select[1] = 0;
Ping Chengf4fa9a62011-10-04 23:49:42 -0700764 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700765 wacom->led.hlv = 20;
766 wacom->led.img_lum = 10;
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700767 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700768 &intuos4_led_attr_group);
769 break;
770
Jason Gerecke246835f2011-12-12 00:12:04 -0800771 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700772 case WACOM_21UX2:
773 wacom->led.select[0] = 0;
774 wacom->led.select[1] = 0;
775 wacom->led.llv = 0;
776 wacom->led.hlv = 0;
777 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700778
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700779 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700780 &cintiq_led_attr_group);
781 break;
782
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700783 case INTUOS5S:
784 case INTUOS5:
785 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700786 case INTUOSPS:
787 case INTUOSPM:
788 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700789 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
790 wacom->led.select[0] = 0;
791 wacom->led.select[1] = 0;
792 wacom->led.llv = 32;
793 wacom->led.hlv = 0;
794 wacom->led.img_lum = 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700795
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700796 error = sysfs_create_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700797 &intuos5_led_attr_group);
798 } else
799 return 0;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700800 break;
801
Ping Cheng09e7d942011-10-04 23:51:14 -0700802 default:
803 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700804 }
805
Ping Cheng09e7d942011-10-04 23:51:14 -0700806 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700807 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -0700808 "cannot create sysfs group err: %d\n", error);
809 return error;
810 }
811 wacom_led_control(wacom);
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700812 wacom->led_initialized = true;
Ping Cheng09e7d942011-10-04 23:51:14 -0700813
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700814 return 0;
815}
816
817static void wacom_destroy_leds(struct wacom *wacom)
818{
Benjamin Tissoiresc757cba2014-07-24 13:16:17 -0700819 if (!wacom->led_initialized)
820 return;
821
822 wacom->led_initialized = false;
823
Ping Cheng09e7d942011-10-04 23:51:14 -0700824 switch (wacom->wacom_wac.features.type) {
Jason Gereckea19fc982012-06-12 00:27:53 -0700825 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -0700826 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700827 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -0700828 case INTUOS4L:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700829 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700830 &intuos4_led_attr_group);
831 break;
832
Jason Gerecke246835f2011-12-12 00:12:04 -0800833 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -0700834 case WACOM_21UX2:
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700835 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Cheng09e7d942011-10-04 23:51:14 -0700836 &cintiq_led_attr_group);
837 break;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700838
839 case INTUOS5S:
840 case INTUOS5:
841 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -0700842 case INTUOSPS:
843 case INTUOSPM:
844 case INTUOSPL:
Ping Chengc2b0c272013-09-20 09:50:14 -0700845 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -0700846 sysfs_remove_group(&wacom->hdev->dev.kobj,
Ping Chengc2b0c272013-09-20 09:50:14 -0700847 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700848 break;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700849 }
850}
851
Chris Bagwella1d552c2012-03-25 23:26:30 -0700852static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700853 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700854 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700855 POWER_SUPPLY_PROP_CAPACITY
856};
857
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700858static enum power_supply_property wacom_ac_props[] = {
859 POWER_SUPPLY_PROP_PRESENT,
860 POWER_SUPPLY_PROP_ONLINE,
861 POWER_SUPPLY_PROP_SCOPE,
862};
863
Chris Bagwella1d552c2012-03-25 23:26:30 -0700864static int wacom_battery_get_property(struct power_supply *psy,
865 enum power_supply_property psp,
866 union power_supply_propval *val)
867{
868 struct wacom *wacom = container_of(psy, struct wacom, battery);
869 int ret = 0;
870
871 switch (psp) {
Bastien Nocera6e2a6e82013-10-15 23:33:00 -0700872 case POWER_SUPPLY_PROP_SCOPE:
873 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
874 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700875 case POWER_SUPPLY_PROP_CAPACITY:
876 val->intval =
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700877 wacom->wacom_wac.battery_capacity;
878 break;
879 case POWER_SUPPLY_PROP_STATUS:
880 if (wacom->wacom_wac.bat_charging)
881 val->intval = POWER_SUPPLY_STATUS_CHARGING;
882 else if (wacom->wacom_wac.battery_capacity == 100 &&
883 wacom->wacom_wac.ps_connected)
884 val->intval = POWER_SUPPLY_STATUS_FULL;
885 else
886 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700887 break;
888 default:
889 ret = -EINVAL;
890 break;
891 }
892
893 return ret;
894}
895
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700896static int wacom_ac_get_property(struct power_supply *psy,
897 enum power_supply_property psp,
898 union power_supply_propval *val)
899{
900 struct wacom *wacom = container_of(psy, struct wacom, ac);
901 int ret = 0;
902
903 switch (psp) {
904 case POWER_SUPPLY_PROP_PRESENT:
905 /* fall through */
906 case POWER_SUPPLY_PROP_ONLINE:
907 val->intval = wacom->wacom_wac.ps_connected;
908 break;
909 case POWER_SUPPLY_PROP_SCOPE:
910 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
911 break;
912 default:
913 ret = -EINVAL;
914 break;
915 }
916 return ret;
917}
918
Chris Bagwella1d552c2012-03-25 23:26:30 -0700919static int wacom_initialize_battery(struct wacom *wacom)
920{
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700921 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700922 int error;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700923 unsigned long n;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700924
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700925 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700926 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700927
Chris Bagwella1d552c2012-03-25 23:26:30 -0700928 wacom->battery.properties = wacom_battery_props;
929 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
930 wacom->battery.get_property = wacom_battery_get_property;
Benjamin Tissoiresd70420b2014-07-25 17:31:51 -0700931 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
932 wacom->battery.name = wacom->wacom_wac.bat_name;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700933 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
934 wacom->battery.use_for_apm = 0;
935
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700936 wacom->ac.properties = wacom_ac_props;
937 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
938 wacom->ac.get_property = wacom_ac_get_property;
939 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
940 wacom->ac.name = wacom->wacom_wac.ac_name;
941 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
942 wacom->ac.use_for_apm = 0;
943
Benjamin Tissoiresdd3181a2014-07-24 13:01:40 -0700944 error = power_supply_register(&wacom->hdev->dev,
Chris Bagwella1d552c2012-03-25 23:26:30 -0700945 &wacom->battery);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700946 if (error)
947 return error;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700948
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700949 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
950
951 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
952 if (error) {
953 power_supply_unregister(&wacom->battery);
954 return error;
955 }
956
957 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
Chris Bagwella1d552c2012-03-25 23:26:30 -0700958 }
959
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700960 return 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -0700961}
962
963static void wacom_destroy_battery(struct wacom *wacom)
964{
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -0700965 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
966 wacom->battery.dev) {
Chris Bagwella1d552c2012-03-25 23:26:30 -0700967 power_supply_unregister(&wacom->battery);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700968 wacom->battery.dev = NULL;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -0700969 power_supply_unregister(&wacom->ac);
970 wacom->ac.dev = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -0700971 }
Chris Bagwella1d552c2012-03-25 23:26:30 -0700972}
973
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700974static ssize_t wacom_show_speed(struct device *dev,
975 struct device_attribute
976 *attr, char *buf)
977{
978 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
979 struct wacom *wacom = hid_get_drvdata(hdev);
980
981 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
982}
983
984static ssize_t wacom_store_speed(struct device *dev,
985 struct device_attribute *attr,
986 const char *buf, size_t count)
987{
988 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
989 struct wacom *wacom = hid_get_drvdata(hdev);
990 u8 new_speed;
991
992 if (kstrtou8(buf, 0, &new_speed))
993 return -EINVAL;
994
995 if (new_speed != 0 && new_speed != 1)
996 return -EINVAL;
997
998 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
999
1000 return count;
1001}
1002
Ping Chenge0984bc2014-09-10 12:40:05 -07001003static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001004 wacom_show_speed, wacom_store_speed);
1005
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001006static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001007{
1008 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001009 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001010 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001011
1012 input_dev = input_allocate_device();
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001013 if (!input_dev)
1014 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001015
1016 input_dev->name = wacom_wac->name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001017 input_dev->phys = hdev->phys;
1018 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001019 input_dev->open = wacom_open;
1020 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001021 input_dev->uniq = hdev->uniq;
1022 input_dev->id.bustype = hdev->bus;
1023 input_dev->id.vendor = hdev->vendor;
1024 input_dev->id.product = hdev->product;
1025 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001026 input_set_drvdata(input_dev, wacom);
1027
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001028 return input_dev;
1029}
1030
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001031static void wacom_unregister_inputs(struct wacom *wacom)
1032{
1033 if (wacom->wacom_wac.input)
1034 input_unregister_device(wacom->wacom_wac.input);
1035 if (wacom->wacom_wac.pad_input)
1036 input_unregister_device(wacom->wacom_wac.pad_input);
1037 wacom->wacom_wac.input = NULL;
1038 wacom->wacom_wac.pad_input = NULL;
1039}
1040
1041static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001042{
1043 struct input_dev *input_dev, *pad_input_dev;
1044 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1045 int error;
1046
1047 input_dev = wacom_allocate_input(wacom);
1048 pad_input_dev = wacom_allocate_input(wacom);
1049 if (!input_dev || !pad_input_dev) {
1050 error = -ENOMEM;
1051 goto fail1;
1052 }
1053
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001054 wacom_wac->input = input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001055 wacom_wac->pad_input = pad_input_dev;
1056 wacom_wac->pad_input->name = wacom_wac->pad_name;
1057
Ping Cheng19635182012-04-29 21:09:18 -07001058 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1059 if (error)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001060 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001061
1062 error = input_register_device(input_dev);
Ping Cheng19635182012-04-29 21:09:18 -07001063 if (error)
1064 goto fail2;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001065
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001066 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1067 if (error) {
1068 /* no pad in use on this interface */
1069 input_free_device(pad_input_dev);
1070 wacom_wac->pad_input = NULL;
1071 pad_input_dev = NULL;
1072 } else {
1073 error = input_register_device(pad_input_dev);
1074 if (error)
1075 goto fail3;
1076 }
1077
Ping Cheng19635182012-04-29 21:09:18 -07001078 return 0;
1079
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001080fail3:
1081 input_unregister_device(input_dev);
1082 input_dev = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001083fail2:
Ping Cheng19635182012-04-29 21:09:18 -07001084 wacom_wac->input = NULL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001085 wacom_wac->pad_input = NULL;
Ping Cheng19635182012-04-29 21:09:18 -07001086fail1:
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001087 if (input_dev)
1088 input_free_device(input_dev);
1089 if (pad_input_dev)
1090 input_free_device(pad_input_dev);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001091 return error;
1092}
1093
Chris Bagwell16bf2882012-03-25 23:26:20 -07001094static void wacom_wireless_work(struct work_struct *work)
1095{
1096 struct wacom *wacom = container_of(work, struct wacom, work);
1097 struct usb_device *usbdev = wacom->usbdev;
1098 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001099 struct hid_device *hdev1, *hdev2;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001100 struct wacom *wacom1, *wacom2;
1101 struct wacom_wac *wacom_wac1, *wacom_wac2;
1102 int error;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001103
1104 /*
1105 * Regardless if this is a disconnect or a new tablet,
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001106 * remove any existing input and battery devices.
Chris Bagwell16bf2882012-03-25 23:26:20 -07001107 */
1108
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001109 wacom_destroy_battery(wacom);
1110
Chris Bagwell16bf2882012-03-25 23:26:20 -07001111 /* Stylus interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001112 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1113 wacom1 = hid_get_drvdata(hdev1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001114 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001115 wacom_unregister_inputs(wacom1);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001116
1117 /* Touch interface */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001118 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1119 wacom2 = hid_get_drvdata(hdev2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001120 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001121 wacom_unregister_inputs(wacom2);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001122
1123 if (wacom_wac->pid == 0) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001124 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001125 wacom_wac1->shared->type = 0;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001126 } else {
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001127 const struct hid_device_id *id = wacom_ids;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001128
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001129 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
Dmitry Torokhoveb71d1b2012-05-02 00:13:38 -07001130 wacom_wac->pid);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001131
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001132 while (id->bus) {
1133 if (id->vendor == USB_VENDOR_ID_WACOM &&
1134 id->product == wacom_wac->pid)
Chris Bagwell16bf2882012-03-25 23:26:20 -07001135 break;
1136 id++;
1137 }
1138
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001139 if (!id->bus) {
Benjamin Tissoirese2114ce2014-07-24 13:01:56 -07001140 hid_info(wacom->hdev, "ignoring unknown PID.\n");
Chris Bagwell16bf2882012-03-25 23:26:20 -07001141 return;
1142 }
1143
1144 /* Stylus interface */
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001145 wacom_wac1->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001146 *((struct wacom_features *)id->driver_data);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001147 wacom_wac1->features.device_type = BTN_TOOL_PEN;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001148 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1149 wacom_wac1->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001150 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1151 wacom_wac1->features.name);
Ping Cheng961794a2013-12-05 12:54:53 -08001152 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1153 wacom_wac1->shared->type = wacom_wac1->features.type;
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001154 error = wacom_register_inputs(wacom1);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001155 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001156 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001157
1158 /* Touch interface */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001159 if (wacom_wac1->features.touch_max ||
1160 wacom_wac1->features.type == INTUOSHT) {
Ping Cheng57bcfce2013-10-15 23:44:00 -07001161 wacom_wac2->features =
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001162 *((struct wacom_features *)id->driver_data);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001163 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1164 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1165 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1166 if (wacom_wac2->features.touch_max)
1167 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1168 "%s (WL) Finger",wacom_wac2->features.name);
1169 else
1170 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1171 "%s (WL) Pad",wacom_wac2->features.name);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001172 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1173 "%s (WL) Pad", wacom_wac2->features.name);
1174 error = wacom_register_inputs(wacom2);
Ping Cheng57bcfce2013-10-15 23:44:00 -07001175 if (error)
1176 goto fail;
Ping Cheng961794a2013-12-05 12:54:53 -08001177
1178 if (wacom_wac1->features.type == INTUOSHT &&
1179 wacom_wac1->features.touch_max)
1180 wacom_wac->shared->touch_input = wacom_wac2->input;
Ping Cheng57bcfce2013-10-15 23:44:00 -07001181 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001182
1183 error = wacom_initialize_battery(wacom);
1184 if (error)
Ping Cheng57bcfce2013-10-15 23:44:00 -07001185 goto fail;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001186 }
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001187
1188 return;
1189
Ping Cheng57bcfce2013-10-15 23:44:00 -07001190fail:
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001191 wacom_unregister_inputs(wacom1);
1192 wacom_unregister_inputs(wacom2);
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001193 return;
Chris Bagwell16bf2882012-03-25 23:26:20 -07001194}
1195
Ping Cheng401d7d12013-07-28 00:38:54 -07001196/*
1197 * Not all devices report physical dimensions from HID.
1198 * Compute the default from hardcoded logical dimension
1199 * and resolution before driver overwrites them.
1200 */
1201static void wacom_set_default_phy(struct wacom_features *features)
1202{
1203 if (features->x_resolution) {
1204 features->x_phy = (features->x_max * 100) /
1205 features->x_resolution;
1206 features->y_phy = (features->y_max * 100) /
1207 features->y_resolution;
1208 }
1209}
1210
1211static void wacom_calculate_res(struct wacom_features *features)
1212{
1213 features->x_resolution = wacom_calc_hid_res(features->x_max,
1214 features->x_phy,
1215 features->unit,
1216 features->unitExpo);
1217 features->y_resolution = wacom_calc_hid_res(features->y_max,
1218 features->y_phy,
1219 features->unit,
1220 features->unitExpo);
1221}
1222
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001223static int wacom_hid_report_len(struct hid_report *report)
1224{
1225 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1226 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1227}
1228
1229static size_t wacom_compute_pktlen(struct hid_device *hdev)
1230{
1231 struct hid_report_enum *report_enum;
1232 struct hid_report *report;
1233 size_t size = 0;
1234
1235 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1236
1237 list_for_each_entry(report, &report_enum->report_list, list) {
1238 size_t report_size = wacom_hid_report_len(report);
1239 if (report_size > size)
1240 size = report_size;
1241 }
1242
1243 return size;
1244}
1245
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001246static int wacom_probe(struct hid_device *hdev,
1247 const struct hid_device_id *id)
Ping Cheng3bea7332006-07-13 18:01:36 -07001248{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001249 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
Ping Cheng3bea7332006-07-13 18:01:36 -07001250 struct usb_device *dev = interface_to_usbdev(intf);
Ping Cheng3bea7332006-07-13 18:01:36 -07001251 struct wacom *wacom;
1252 struct wacom_wac *wacom_wac;
Jason Childse33da8a2010-02-17 22:38:31 -08001253 struct wacom_features *features;
Jason Childse33da8a2010-02-17 22:38:31 -08001254 int error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001255
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001256 if (!id->driver_data)
Bastian Blankb036f6f2010-02-10 23:06:23 -08001257 return -EINVAL;
1258
Ping Cheng3bea7332006-07-13 18:01:36 -07001259 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
Dan Carpenterf1823942012-03-29 22:38:11 -07001260 if (!wacom)
1261 return -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -07001262
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001263 hid_set_drvdata(hdev, wacom);
1264 wacom->hdev = hdev;
1265
Benjamin Tissoiresba9a3542014-07-24 12:58:45 -07001266 /* ask for the report descriptor to be loaded by HID */
1267 error = hid_parse(hdev);
1268 if (error) {
1269 hid_err(hdev, "parse failed\n");
1270 goto fail1;
1271 }
1272
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001273 wacom_wac = &wacom->wacom_wac;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001274 wacom_wac->features = *((struct wacom_features *)id->driver_data);
Jason Childse33da8a2010-02-17 22:38:31 -08001275 features = &wacom_wac->features;
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001276 features->pktlen = wacom_compute_pktlen(hdev);
Jason Childse33da8a2010-02-17 22:38:31 -08001277 if (features->pktlen > WACOM_PKGLEN_MAX) {
1278 error = -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07001279 goto fail1;
Jason Childse33da8a2010-02-17 22:38:31 -08001280 }
1281
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001282 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1283 error = -ENODEV;
Jason Childse33da8a2010-02-17 22:38:31 -08001284 goto fail1;
1285 }
Ping Cheng3bea7332006-07-13 18:01:36 -07001286
Ping Cheng3bea7332006-07-13 18:01:36 -07001287 wacom->usbdev = dev;
Oliver Neukume7224092008-04-15 01:31:57 -04001288 wacom->intf = intf;
1289 mutex_init(&wacom->lock);
Chris Bagwell16bf2882012-03-25 23:26:20 -07001290 INIT_WORK(&wacom->work, wacom_wireless_work);
Ping Cheng3bea7332006-07-13 18:01:36 -07001291
Ping Cheng401d7d12013-07-28 00:38:54 -07001292 /* set the default size in case we do not get them from hid */
1293 wacom_set_default_phy(features);
1294
Ping Chengf393ee22012-04-29 21:09:17 -07001295 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001296 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -05001297
Jason Gereckeae584ca2012-04-03 15:50:40 -07001298 /*
1299 * Intuos5 has no useful data about its touch interface in its
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001300 * HID descriptor. If this is the touch interface (PacketSize
Jason Gereckeae584ca2012-04-03 15:50:40 -07001301 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1302 */
Ping Chengb5fd2a32013-11-25 18:44:55 -08001303 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07001304 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
Jason Gereckeae584ca2012-04-03 15:50:40 -07001305 features->device_type = BTN_TOOL_FINGER;
Jason Gereckeae584ca2012-04-03 15:50:40 -07001306
Jason Gereckeae584ca2012-04-03 15:50:40 -07001307 features->x_max = 4096;
1308 features->y_max = 4096;
1309 } else {
1310 features->device_type = BTN_TOOL_PEN;
1311 }
1312 }
1313
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001314 /*
1315 * Same thing for Bamboo 3rd gen.
1316 */
1317 if ((features->type == BAMBOO_PT) &&
1318 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1319 (features->device_type == BTN_TOOL_PEN)) {
1320 features->device_type = BTN_TOOL_FINGER;
1321
1322 features->x_max = 4096;
1323 features->y_max = 4096;
1324 }
1325
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001326 if (hdev->bus == BUS_BLUETOOTH)
1327 features->quirks |= WACOM_QUIRK_BATTERY;
1328
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001329 wacom_setup_device_quirks(features);
1330
Ping Cheng401d7d12013-07-28 00:38:54 -07001331 /* set unit to "100th of a mm" for devices not reported by HID */
1332 if (!features->unit) {
1333 features->unit = 0x11;
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07001334 features->unitExpo = -3;
Ping Cheng401d7d12013-07-28 00:38:54 -07001335 }
1336 wacom_calculate_res(features);
1337
Ping Cheng49b764a2010-02-20 00:53:49 -08001338 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001339 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1340 "%s Pad", features->name);
Ping Cheng49b764a2010-02-20 00:53:49 -08001341
Henrik Rydbergbc73dd32010-09-05 12:26:16 -07001342 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
Ping Cheng49b764a2010-02-20 00:53:49 -08001343 /* Append the device type to the name */
Ping Cheng57bcfce2013-10-15 23:44:00 -07001344 if (features->device_type != BTN_TOOL_FINGER)
1345 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1346 else if (features->touch_max)
1347 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1348 else
1349 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
Ping Cheng4492eff2010-03-19 22:18:15 -07001350
Benjamin Tissoires4451e082014-07-24 13:01:05 -07001351 error = wacom_add_shared_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -07001352 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001353 goto fail1;
Ping Cheng49b764a2010-02-20 00:53:49 -08001354 }
1355
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001356 error = wacom_initialize_leds(wacom);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001357 if (error)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001358 goto fail2;
Ping Cheng3bea7332006-07-13 18:01:36 -07001359
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001360 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1361 (features->quirks & WACOM_QUIRK_BATTERY)) {
1362 error = wacom_initialize_battery(wacom);
1363 if (error)
1364 goto fail3;
1365 }
1366
Chris Bagwelld3825d52012-03-25 23:26:11 -07001367 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001368 error = wacom_register_inputs(wacom);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001369 if (error)
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001370 goto fail4;
1371 }
1372
1373 if (hdev->bus == BUS_BLUETOOTH) {
1374 error = device_create_file(&hdev->dev, &dev_attr_speed);
1375 if (error)
1376 hid_warn(hdev,
1377 "can't create sysfs speed attribute err: %d\n",
1378 error);
Chris Bagwelld3825d52012-03-25 23:26:11 -07001379 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001380
Ping Chengec67bbe2009-12-15 00:35:24 -08001381 /* Note that if query fails it is not a hard failure */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001382 wacom_query_tablet_data(hdev, features);
Ping Cheng3bea7332006-07-13 18:01:36 -07001383
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001384 /* Regular HID work starts now */
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001385 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1386 if (error) {
1387 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001388 goto fail5;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001389 }
1390
1391 if (features->quirks & WACOM_QUIRK_MONITOR)
1392 error = hid_hw_open(hdev);
1393
Ping Cheng961794a2013-12-05 12:54:53 -08001394 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1395 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1396 wacom_wac->shared->touch_input = wacom_wac->input;
1397 }
1398
Ping Cheng3bea7332006-07-13 18:01:36 -07001399 return 0;
1400
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001401 fail5: if (hdev->bus == BUS_BLUETOOTH)
1402 device_remove_file(&hdev->dev, &dev_attr_speed);
1403 wacom_unregister_inputs(wacom);
1404 fail4: wacom_destroy_battery(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001405 fail3: wacom_destroy_leds(wacom);
1406 fail2: wacom_remove_shared_data(wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001407 fail1: kfree(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001408 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001409 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07001410}
1411
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001412static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07001413{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001414 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07001415
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001416 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001417
Chris Bagwell16bf2882012-03-25 23:26:20 -07001418 cancel_work_sync(&wacom->work);
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07001419 wacom_unregister_inputs(wacom);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001420 if (hdev->bus == BUS_BLUETOOTH)
1421 device_remove_file(&hdev->dev, &dev_attr_speed);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001422 wacom_destroy_battery(wacom);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001423 wacom_destroy_leds(wacom);
Dmitry Torokhov51269fe2010-03-19 22:18:15 -07001424 wacom_remove_shared_data(&wacom->wacom_wac);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001425
1426 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04001427 kfree(wacom);
1428}
1429
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001430#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001431static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001432{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001433 struct wacom *wacom = hid_get_drvdata(hdev);
1434 struct wacom_features *features = &wacom->wacom_wac.features;
Oliver Neukume7224092008-04-15 01:31:57 -04001435
1436 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001437
1438 /* switch to wacom mode first */
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001439 wacom_query_tablet_data(hdev, features);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001440 wacom_led_control(wacom);
1441
Oliver Neukume7224092008-04-15 01:31:57 -04001442 mutex_unlock(&wacom->lock);
1443
1444 return 0;
1445}
1446
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001447static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04001448{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001449 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04001450}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07001451#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04001452
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001453static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07001454 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08001455 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07001456 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001457 .remove = wacom_remove,
1458#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04001459 .resume = wacom_resume,
1460 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001461#endif
1462 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07001463};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001464module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07001465
1466MODULE_VERSION(DRIVER_VERSION);
1467MODULE_AUTHOR(DRIVER_AUTHOR);
1468MODULE_DESCRIPTION(DRIVER_DESC);
1469MODULE_LICENSE(DRIVER_LICENSE);