xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 8ffffd5212846b72f116f7a9572e83d580e25802)
1471d1714SBenjamin Tissoires /*
2471d1714SBenjamin Tissoires  * drivers/input/tablet/wacom_sys.c
3471d1714SBenjamin Tissoires  *
4471d1714SBenjamin Tissoires  *  USB Wacom tablet support - system specific code
5471d1714SBenjamin Tissoires  */
6471d1714SBenjamin Tissoires 
7471d1714SBenjamin Tissoires /*
8471d1714SBenjamin Tissoires  * This program is free software; you can redistribute it and/or modify
9471d1714SBenjamin Tissoires  * it under the terms of the GNU General Public License as published by
10471d1714SBenjamin Tissoires  * the Free Software Foundation; either version 2 of the License, or
11471d1714SBenjamin Tissoires  * (at your option) any later version.
12471d1714SBenjamin Tissoires  */
13471d1714SBenjamin Tissoires 
14471d1714SBenjamin Tissoires #include "wacom_wac.h"
15471d1714SBenjamin Tissoires #include "wacom.h"
16471d1714SBenjamin Tissoires #include <linux/hid.h>
17471d1714SBenjamin Tissoires 
18471d1714SBenjamin Tissoires #define WAC_MSG_RETRIES		5
19471d1714SBenjamin Tissoires 
20912ca216SPing Cheng #define WAC_CMD_WL_LED_CONTROL	0x03
21471d1714SBenjamin Tissoires #define WAC_CMD_LED_CONTROL	0x20
22471d1714SBenjamin Tissoires #define WAC_CMD_ICON_START	0x21
23471d1714SBenjamin Tissoires #define WAC_CMD_ICON_XFER	0x23
24849e2f06SBenjamin Tissoires #define WAC_CMD_ICON_BT_XFER	0x26
25471d1714SBenjamin Tissoires #define WAC_CMD_RETRIES		10
26471d1714SBenjamin Tissoires 
27e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
28e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
29e0984bc3SPing Cheng 
30c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
31c64d8834SPing Cheng 			    size_t size, unsigned int retries)
32471d1714SBenjamin Tissoires {
33471d1714SBenjamin Tissoires 	int retval;
34471d1714SBenjamin Tissoires 
35471d1714SBenjamin Tissoires 	do {
36c64d8834SPing Cheng 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
37471d1714SBenjamin Tissoires 				HID_REQ_GET_REPORT);
38471d1714SBenjamin Tissoires 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
39471d1714SBenjamin Tissoires 
40471d1714SBenjamin Tissoires 	return retval;
41471d1714SBenjamin Tissoires }
42471d1714SBenjamin Tissoires 
43296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
44296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
45471d1714SBenjamin Tissoires {
46471d1714SBenjamin Tissoires 	int retval;
47471d1714SBenjamin Tissoires 
48471d1714SBenjamin Tissoires 	do {
49296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
50471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
51471d1714SBenjamin Tissoires 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
52471d1714SBenjamin Tissoires 
53471d1714SBenjamin Tissoires 	return retval;
54471d1714SBenjamin Tissoires }
55471d1714SBenjamin Tissoires 
56471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
57471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
58471d1714SBenjamin Tissoires {
59471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
60471d1714SBenjamin Tissoires 
61471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
62471d1714SBenjamin Tissoires 		return 1;
63471d1714SBenjamin Tissoires 
64471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
65471d1714SBenjamin Tissoires 
66471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
67471d1714SBenjamin Tissoires 
68471d1714SBenjamin Tissoires 	return 0;
69471d1714SBenjamin Tissoires }
70471d1714SBenjamin Tissoires 
71471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
72471d1714SBenjamin Tissoires {
73471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
74471d1714SBenjamin Tissoires 	int retval;
75471d1714SBenjamin Tissoires 
76471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
77471d1714SBenjamin Tissoires 	retval = hid_hw_open(wacom->hdev);
78471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
79471d1714SBenjamin Tissoires 
80471d1714SBenjamin Tissoires 	return retval;
81471d1714SBenjamin Tissoires }
82471d1714SBenjamin Tissoires 
83471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
84471d1714SBenjamin Tissoires {
85471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
86471d1714SBenjamin Tissoires 
87471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
88471d1714SBenjamin Tissoires 	hid_hw_close(wacom->hdev);
89471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
90471d1714SBenjamin Tissoires }
91471d1714SBenjamin Tissoires 
92471d1714SBenjamin Tissoires /*
93471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
94471d1714SBenjamin Tissoires  */
95471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
96471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
97471d1714SBenjamin Tissoires {
98471d1714SBenjamin Tissoires 	struct hid_field field = {
99471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
100471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
101471d1714SBenjamin Tissoires 		.unit = unit,
102471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
103471d1714SBenjamin Tissoires 	};
104471d1714SBenjamin Tissoires 
105471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
106471d1714SBenjamin Tissoires }
107471d1714SBenjamin Tissoires 
108471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
109471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
110471d1714SBenjamin Tissoires {
111471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
112471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
113*8ffffd52SBenjamin Tissoires 	u8 *data;
114*8ffffd52SBenjamin Tissoires 	int ret;
115471d1714SBenjamin Tissoires 
116471d1714SBenjamin Tissoires 	switch (usage->hid) {
117471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
118471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
119*8ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
120*8ffffd52SBenjamin Tissoires 			/* read manually */
121*8ffffd52SBenjamin Tissoires 			data = kzalloc(2, GFP_KERNEL);
122*8ffffd52SBenjamin Tissoires 			if (!data)
123*8ffffd52SBenjamin Tissoires 				break;
124*8ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
125*8ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
126*8ffffd52SBenjamin Tissoires 						data, 2, 0);
127*8ffffd52SBenjamin Tissoires 			if (ret == 2)
128*8ffffd52SBenjamin Tissoires 				features->touch_max = data[1];
129*8ffffd52SBenjamin Tissoires 			kfree(data);
130*8ffffd52SBenjamin Tissoires 		}
131471d1714SBenjamin Tissoires 		break;
132471d1714SBenjamin Tissoires 	}
133471d1714SBenjamin Tissoires }
134471d1714SBenjamin Tissoires 
135471d1714SBenjamin Tissoires /*
136471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
137471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
138471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
139471d1714SBenjamin Tissoires  * resolution based on product ID's.
140471d1714SBenjamin Tissoires  *
141471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
142471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
143471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
144471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
145471d1714SBenjamin Tissoires  * an additional table.
146471d1714SBenjamin Tissoires  *
147471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
148471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
149471d1714SBenjamin Tissoires  * function will ignore it.
150471d1714SBenjamin Tissoires  *
151471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
152471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
153471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
154471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
155471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
156471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
157471d1714SBenjamin Tissoires  *
158471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
159471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
160471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
161471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
162471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
163471d1714SBenjamin Tissoires  *
164471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
165471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
166471d1714SBenjamin Tissoires  */
167471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
168471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
169471d1714SBenjamin Tissoires {
170471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
171471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
172471d1714SBenjamin Tissoires 	bool finger = (field->logical == HID_DG_FINGER) ||
173471d1714SBenjamin Tissoires 		      (field->physical == HID_DG_FINGER);
174471d1714SBenjamin Tissoires 	bool pen = (field->logical == HID_DG_STYLUS) ||
175471d1714SBenjamin Tissoires 		   (field->physical == HID_DG_STYLUS);
176471d1714SBenjamin Tissoires 
177471d1714SBenjamin Tissoires 	/*
178471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
179471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
180471d1714SBenjamin Tissoires 	* values commonly reported.
181471d1714SBenjamin Tissoires 	*/
182471d1714SBenjamin Tissoires 	if (!pen && !finger)
183471d1714SBenjamin Tissoires 		return;
184471d1714SBenjamin Tissoires 
185471d1714SBenjamin Tissoires 	if (finger && !features->touch_max)
186471d1714SBenjamin Tissoires 		/* touch device at least supports one touch point */
187471d1714SBenjamin Tissoires 		features->touch_max = 1;
188471d1714SBenjamin Tissoires 
189471d1714SBenjamin Tissoires 	switch (usage->hid) {
190471d1714SBenjamin Tissoires 	case HID_GD_X:
191471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
192471d1714SBenjamin Tissoires 		if (finger) {
193471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
194471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
195471d1714SBenjamin Tissoires 			if (features->type != BAMBOO_PT) {
196471d1714SBenjamin Tissoires 				features->unit = field->unit;
197471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
198471d1714SBenjamin Tissoires 			}
199471d1714SBenjamin Tissoires 		} else {
200471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_PEN;
201471d1714SBenjamin Tissoires 		}
202471d1714SBenjamin Tissoires 		break;
203471d1714SBenjamin Tissoires 	case HID_GD_Y:
204471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
205471d1714SBenjamin Tissoires 		if (finger) {
206471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
207471d1714SBenjamin Tissoires 			if (features->type != BAMBOO_PT) {
208471d1714SBenjamin Tissoires 				features->unit = field->unit;
209471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
210471d1714SBenjamin Tissoires 			}
211471d1714SBenjamin Tissoires 		}
212471d1714SBenjamin Tissoires 		break;
213471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
214471d1714SBenjamin Tissoires 		if (pen)
215471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
216471d1714SBenjamin Tissoires 		break;
217471d1714SBenjamin Tissoires 	}
218471d1714SBenjamin Tissoires }
219471d1714SBenjamin Tissoires 
220471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
221471d1714SBenjamin Tissoires 			   struct wacom_features *features)
222471d1714SBenjamin Tissoires {
223471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
224471d1714SBenjamin Tissoires 	struct hid_report *hreport;
225471d1714SBenjamin Tissoires 	int i, j;
226471d1714SBenjamin Tissoires 
227471d1714SBenjamin Tissoires 	/* check features first */
228471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
229471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
230471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
231471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
232471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
233471d1714SBenjamin Tissoires 				continue;
234471d1714SBenjamin Tissoires 
235471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
236471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
237471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
238471d1714SBenjamin Tissoires 			}
239471d1714SBenjamin Tissoires 		}
240471d1714SBenjamin Tissoires 	}
241471d1714SBenjamin Tissoires 
242471d1714SBenjamin Tissoires 	/* now check the input usages */
243471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
244471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
245471d1714SBenjamin Tissoires 
246471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
247471d1714SBenjamin Tissoires 			continue;
248471d1714SBenjamin Tissoires 
249471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
250471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
251471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
252471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
253471d1714SBenjamin Tissoires 	}
254471d1714SBenjamin Tissoires }
255471d1714SBenjamin Tissoires 
256471d1714SBenjamin Tissoires static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
257471d1714SBenjamin Tissoires 		int length, int mode)
258471d1714SBenjamin Tissoires {
259471d1714SBenjamin Tissoires 	unsigned char *rep_data;
260471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
261471d1714SBenjamin Tissoires 
262471d1714SBenjamin Tissoires 	rep_data = kzalloc(length, GFP_KERNEL);
263471d1714SBenjamin Tissoires 	if (!rep_data)
264471d1714SBenjamin Tissoires 		return error;
265471d1714SBenjamin Tissoires 
266471d1714SBenjamin Tissoires 	do {
267471d1714SBenjamin Tissoires 		rep_data[0] = report_id;
268471d1714SBenjamin Tissoires 		rep_data[1] = mode;
269471d1714SBenjamin Tissoires 
270296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
271296b7378SPrzemo Firszt 					 length, 1);
272471d1714SBenjamin Tissoires 		if (error >= 0)
273471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
274c64d8834SPing Cheng 			                         rep_data, length, 1);
275471d1714SBenjamin Tissoires 	} while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
276471d1714SBenjamin Tissoires 
277471d1714SBenjamin Tissoires 	kfree(rep_data);
278471d1714SBenjamin Tissoires 
279471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
280471d1714SBenjamin Tissoires }
281471d1714SBenjamin Tissoires 
282f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
283f81a1295SBenjamin Tissoires 		struct wacom_features *features)
284f81a1295SBenjamin Tissoires {
285387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
286387142bbSBenjamin Tissoires 	int ret;
287387142bbSBenjamin Tissoires 	u8 rep_data[2];
288387142bbSBenjamin Tissoires 
289387142bbSBenjamin Tissoires 	switch (features->type) {
290387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
291387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
292387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
293296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
294296b7378SPrzemo Firszt 					3);
295387142bbSBenjamin Tissoires 
296387142bbSBenjamin Tissoires 		if (ret >= 0) {
297387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
298387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
299387142bbSBenjamin Tissoires 
300387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
301296b7378SPrzemo Firszt 						rep_data, 2, 3);
302387142bbSBenjamin Tissoires 
303387142bbSBenjamin Tissoires 			if (ret >= 0) {
304387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
305387142bbSBenjamin Tissoires 				return 0;
306387142bbSBenjamin Tissoires 			}
307387142bbSBenjamin Tissoires 		}
308387142bbSBenjamin Tissoires 
309387142bbSBenjamin Tissoires 		/*
310387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
311387142bbSBenjamin Tissoires 		 * and it is safe to continue
312387142bbSBenjamin Tissoires 		 */
313387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
314387142bbSBenjamin Tissoires 			 rep_data[0], ret);
315387142bbSBenjamin Tissoires 		break;
31681af7e61SBenjamin Tissoires 	case INTUOS4WL:
31781af7e61SBenjamin Tissoires 		if (speed == 1)
31881af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
31981af7e61SBenjamin Tissoires 		else
32081af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
32181af7e61SBenjamin Tissoires 
32281af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
32381af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
32481af7e61SBenjamin Tissoires 
325296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
326296b7378SPrzemo Firszt 					1);
32781af7e61SBenjamin Tissoires 		if (ret >= 0)
32881af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
32981af7e61SBenjamin Tissoires 		break;
330387142bbSBenjamin Tissoires 	}
331387142bbSBenjamin Tissoires 
332f81a1295SBenjamin Tissoires 	return 0;
333f81a1295SBenjamin Tissoires }
334f81a1295SBenjamin Tissoires 
335471d1714SBenjamin Tissoires /*
336471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
337471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
338471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
339471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
340471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
341471d1714SBenjamin Tissoires  */
342471d1714SBenjamin Tissoires static int wacom_query_tablet_data(struct hid_device *hdev,
343471d1714SBenjamin Tissoires 		struct wacom_features *features)
344471d1714SBenjamin Tissoires {
345f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
346f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
347f81a1295SBenjamin Tissoires 
348471d1714SBenjamin Tissoires 	if (features->device_type == BTN_TOOL_FINGER) {
349471d1714SBenjamin Tissoires 		if (features->type > TABLETPC) {
350471d1714SBenjamin Tissoires 			/* MT Tablet PC touch */
351471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 3, 4, 4);
352471d1714SBenjamin Tissoires 		}
353471d1714SBenjamin Tissoires 		else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
354471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 18, 3, 2);
355471d1714SBenjamin Tissoires 		}
356471d1714SBenjamin Tissoires 	} else if (features->device_type == BTN_TOOL_PEN) {
357471d1714SBenjamin Tissoires 		if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
358471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 2, 2, 2);
359471d1714SBenjamin Tissoires 		}
360471d1714SBenjamin Tissoires 	}
361471d1714SBenjamin Tissoires 
362471d1714SBenjamin Tissoires 	return 0;
363471d1714SBenjamin Tissoires }
364471d1714SBenjamin Tissoires 
365471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
366471d1714SBenjamin Tissoires 					 struct wacom_features *features)
367471d1714SBenjamin Tissoires {
368471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
369471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
370471d1714SBenjamin Tissoires 
371471d1714SBenjamin Tissoires 	/* default features */
372471d1714SBenjamin Tissoires 	features->device_type = BTN_TOOL_PEN;
373471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
374471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
375471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
376471d1714SBenjamin Tissoires 	features->distance_fuzz = 0;
377471d1714SBenjamin Tissoires 
378471d1714SBenjamin Tissoires 	/*
379471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
380471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
381471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
382471d1714SBenjamin Tissoires 	 * interface number.
383471d1714SBenjamin Tissoires 	 */
384471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
385471d1714SBenjamin Tissoires 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
386471d1714SBenjamin Tissoires 			features->device_type = 0;
387471d1714SBenjamin Tissoires 		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
388471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
389471d1714SBenjamin Tissoires 			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
390471d1714SBenjamin Tissoires 		}
391471d1714SBenjamin Tissoires 	}
392471d1714SBenjamin Tissoires 
393471d1714SBenjamin Tissoires 	/* only devices that support touch need to retrieve the info */
394471d1714SBenjamin Tissoires 	if (features->type < BAMBOO_PT)
395471d1714SBenjamin Tissoires 		return;
396471d1714SBenjamin Tissoires 
397471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
398471d1714SBenjamin Tissoires }
399471d1714SBenjamin Tissoires 
400471d1714SBenjamin Tissoires struct wacom_hdev_data {
401471d1714SBenjamin Tissoires 	struct list_head list;
402471d1714SBenjamin Tissoires 	struct kref kref;
403471d1714SBenjamin Tissoires 	struct hid_device *dev;
404471d1714SBenjamin Tissoires 	struct wacom_shared shared;
405471d1714SBenjamin Tissoires };
406471d1714SBenjamin Tissoires 
407471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
408471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
409471d1714SBenjamin Tissoires 
410471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
411471d1714SBenjamin Tissoires 		struct hid_device *sibling)
412471d1714SBenjamin Tissoires {
413471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
414471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
415471d1714SBenjamin Tissoires 	int vid = features->oVid;
416471d1714SBenjamin Tissoires 	int pid = features->oPid;
417471d1714SBenjamin Tissoires 	int n1,n2;
418471d1714SBenjamin Tissoires 
419471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
420471d1714SBenjamin Tissoires 		vid = hdev->vendor;
421471d1714SBenjamin Tissoires 		pid = hdev->product;
422471d1714SBenjamin Tissoires 	}
423471d1714SBenjamin Tissoires 
424471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
425471d1714SBenjamin Tissoires 		return false;
426471d1714SBenjamin Tissoires 
427471d1714SBenjamin Tissoires 	/* Compare the physical path. */
428471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
429471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
430471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
431471d1714SBenjamin Tissoires 		return false;
432471d1714SBenjamin Tissoires 
433471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
434471d1714SBenjamin Tissoires }
435471d1714SBenjamin Tissoires 
436471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
437471d1714SBenjamin Tissoires {
438471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
439471d1714SBenjamin Tissoires 
440471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
441471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
442471d1714SBenjamin Tissoires 			kref_get(&data->kref);
443471d1714SBenjamin Tissoires 			return data;
444471d1714SBenjamin Tissoires 		}
445471d1714SBenjamin Tissoires 	}
446471d1714SBenjamin Tissoires 
447471d1714SBenjamin Tissoires 	return NULL;
448471d1714SBenjamin Tissoires }
449471d1714SBenjamin Tissoires 
450471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
451471d1714SBenjamin Tissoires {
452471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
453471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
454471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
455471d1714SBenjamin Tissoires 	int retval = 0;
456471d1714SBenjamin Tissoires 
457471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
458471d1714SBenjamin Tissoires 
459471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
460471d1714SBenjamin Tissoires 	if (!data) {
461471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
462471d1714SBenjamin Tissoires 		if (!data) {
463471d1714SBenjamin Tissoires 			retval = -ENOMEM;
464471d1714SBenjamin Tissoires 			goto out;
465471d1714SBenjamin Tissoires 		}
466471d1714SBenjamin Tissoires 
467471d1714SBenjamin Tissoires 		kref_init(&data->kref);
468471d1714SBenjamin Tissoires 		data->dev = hdev;
469471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
470471d1714SBenjamin Tissoires 	}
471471d1714SBenjamin Tissoires 
472471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
473471d1714SBenjamin Tissoires 
474471d1714SBenjamin Tissoires out:
475471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
476471d1714SBenjamin Tissoires 	return retval;
477471d1714SBenjamin Tissoires }
478471d1714SBenjamin Tissoires 
479471d1714SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
480471d1714SBenjamin Tissoires {
481471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data =
482471d1714SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
483471d1714SBenjamin Tissoires 
484471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
485471d1714SBenjamin Tissoires 	list_del(&data->list);
486471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
487471d1714SBenjamin Tissoires 
488471d1714SBenjamin Tissoires 	kfree(data);
489471d1714SBenjamin Tissoires }
490471d1714SBenjamin Tissoires 
491471d1714SBenjamin Tissoires static void wacom_remove_shared_data(struct wacom_wac *wacom)
492471d1714SBenjamin Tissoires {
493471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
494471d1714SBenjamin Tissoires 
495471d1714SBenjamin Tissoires 	if (wacom->shared) {
496471d1714SBenjamin Tissoires 		data = container_of(wacom->shared, struct wacom_hdev_data, shared);
497471d1714SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
498471d1714SBenjamin Tissoires 		wacom->shared = NULL;
499471d1714SBenjamin Tissoires 	}
500471d1714SBenjamin Tissoires }
501471d1714SBenjamin Tissoires 
502471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
503471d1714SBenjamin Tissoires {
504471d1714SBenjamin Tissoires 	unsigned char *buf;
505471d1714SBenjamin Tissoires 	int retval;
506912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
507912ca216SPing Cheng 	int buf_size = 9;
508471d1714SBenjamin Tissoires 
509912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
510912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
511912ca216SPing Cheng 		buf_size = 13;
512912ca216SPing Cheng 	}
513912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
514471d1714SBenjamin Tissoires 	if (!buf)
515471d1714SBenjamin Tissoires 		return -ENOMEM;
516471d1714SBenjamin Tissoires 
517471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
518471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
519471d1714SBenjamin Tissoires 		/*
520471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
521471d1714SBenjamin Tissoires 		 * one of four values:
522471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
523471d1714SBenjamin Tissoires 		 */
524471d1714SBenjamin Tissoires 		int ring_led = wacom->led.select[0] & 0x03;
525471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
526471d1714SBenjamin Tissoires 		int crop_lum = 0;
527912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
528471d1714SBenjamin Tissoires 
529912ca216SPing Cheng 		buf[0] = report_id;
530912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
531912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
532912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
533912ca216SPing Cheng 			buf[0] = report_id;
534912ca216SPing Cheng 			buf[4] = led_bits;
535912ca216SPing Cheng 		} else
536912ca216SPing Cheng 			buf[1] = led_bits;
537471d1714SBenjamin Tissoires 	}
538471d1714SBenjamin Tissoires 	else {
539471d1714SBenjamin Tissoires 		int led = wacom->led.select[0] | 0x4;
540471d1714SBenjamin Tissoires 
541471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
542471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
543471d1714SBenjamin Tissoires 			led |= (wacom->led.select[1] << 4) | 0x40;
544471d1714SBenjamin Tissoires 
545912ca216SPing Cheng 		buf[0] = report_id;
546471d1714SBenjamin Tissoires 		buf[1] = led;
547471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
548471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
549471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
550471d1714SBenjamin Tissoires 	}
551471d1714SBenjamin Tissoires 
552912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
553296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
554471d1714SBenjamin Tissoires 	kfree(buf);
555471d1714SBenjamin Tissoires 
556471d1714SBenjamin Tissoires 	return retval;
557471d1714SBenjamin Tissoires }
558471d1714SBenjamin Tissoires 
559849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
560849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
561471d1714SBenjamin Tissoires {
562471d1714SBenjamin Tissoires 	unsigned char *buf;
563471d1714SBenjamin Tissoires 	int i, retval;
564849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
565471d1714SBenjamin Tissoires 
566849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
567471d1714SBenjamin Tissoires 	if (!buf)
568471d1714SBenjamin Tissoires 		return -ENOMEM;
569471d1714SBenjamin Tissoires 
570471d1714SBenjamin Tissoires 	/* Send 'start' command */
571471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
572471d1714SBenjamin Tissoires 	buf[1] = 1;
573296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
574296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
575471d1714SBenjamin Tissoires 	if (retval < 0)
576471d1714SBenjamin Tissoires 		goto out;
577471d1714SBenjamin Tissoires 
578849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
579471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
580471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
581471d1714SBenjamin Tissoires 		buf[2] = i;
582849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
583471d1714SBenjamin Tissoires 
584471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
585296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
586471d1714SBenjamin Tissoires 		if (retval < 0)
587471d1714SBenjamin Tissoires 			break;
588471d1714SBenjamin Tissoires 	}
589471d1714SBenjamin Tissoires 
590471d1714SBenjamin Tissoires 	/* Send 'stop' */
591471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
592471d1714SBenjamin Tissoires 	buf[1] = 0;
593296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
594296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
595471d1714SBenjamin Tissoires 
596471d1714SBenjamin Tissoires out:
597471d1714SBenjamin Tissoires 	kfree(buf);
598471d1714SBenjamin Tissoires 	return retval;
599471d1714SBenjamin Tissoires }
600471d1714SBenjamin Tissoires 
601471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
602471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
603471d1714SBenjamin Tissoires {
604471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
605471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
606471d1714SBenjamin Tissoires 	unsigned int id;
607471d1714SBenjamin Tissoires 	int err;
608471d1714SBenjamin Tissoires 
609471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
610471d1714SBenjamin Tissoires 	if (err)
611471d1714SBenjamin Tissoires 		return err;
612471d1714SBenjamin Tissoires 
613471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
614471d1714SBenjamin Tissoires 
615471d1714SBenjamin Tissoires 	wacom->led.select[set_id] = id & 0x3;
616471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
617471d1714SBenjamin Tissoires 
618471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
619471d1714SBenjamin Tissoires 
620471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
621471d1714SBenjamin Tissoires }
622471d1714SBenjamin Tissoires 
623471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
624471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
625471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
626471d1714SBenjamin Tissoires {									\
627471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
628471d1714SBenjamin Tissoires }									\
629471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
630471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
631471d1714SBenjamin Tissoires {									\
632471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
633471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
63437449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
63537449adcSPing Cheng 			 wacom->led.select[SET_ID]);			\
636471d1714SBenjamin Tissoires }									\
637e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
638471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
639471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
640471d1714SBenjamin Tissoires 
641471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
642471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
643471d1714SBenjamin Tissoires 
644471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
645471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
646471d1714SBenjamin Tissoires {
647471d1714SBenjamin Tissoires 	unsigned int value;
648471d1714SBenjamin Tissoires 	int err;
649471d1714SBenjamin Tissoires 
650471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
651471d1714SBenjamin Tissoires 	if (err)
652471d1714SBenjamin Tissoires 		return err;
653471d1714SBenjamin Tissoires 
654471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
655471d1714SBenjamin Tissoires 
656471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
657471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
658471d1714SBenjamin Tissoires 
659471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
660471d1714SBenjamin Tissoires 
661471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
662471d1714SBenjamin Tissoires }
663471d1714SBenjamin Tissoires 
664471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
665471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
666471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
667471d1714SBenjamin Tissoires {									\
668471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
669471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
670471d1714SBenjamin Tissoires 									\
671471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
672471d1714SBenjamin Tissoires 				     buf, count);			\
673471d1714SBenjamin Tissoires }									\
67437449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
67537449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
67637449adcSPing Cheng {									\
67737449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
67837449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
67937449adcSPing Cheng }									\
680e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
68137449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
68237449adcSPing Cheng 		   wacom_##name##_luminance_store)
683471d1714SBenjamin Tissoires 
684471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
685471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
686471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
687471d1714SBenjamin Tissoires 
688471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
689471d1714SBenjamin Tissoires 					const char *buf, size_t count)
690471d1714SBenjamin Tissoires {
691471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
692471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
693471d1714SBenjamin Tissoires 	int err;
694849e2f06SBenjamin Tissoires 	unsigned len;
695849e2f06SBenjamin Tissoires 	u8 xfer_id;
696471d1714SBenjamin Tissoires 
697849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
698849e2f06SBenjamin Tissoires 		len = 256;
699849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
700849e2f06SBenjamin Tissoires 	} else {
701849e2f06SBenjamin Tissoires 		len = 1024;
702849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
703849e2f06SBenjamin Tissoires 	}
704849e2f06SBenjamin Tissoires 
705849e2f06SBenjamin Tissoires 	if (count != len)
706471d1714SBenjamin Tissoires 		return -EINVAL;
707471d1714SBenjamin Tissoires 
708471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
709471d1714SBenjamin Tissoires 
710849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
711471d1714SBenjamin Tissoires 
712471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
713471d1714SBenjamin Tissoires 
714471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
715471d1714SBenjamin Tissoires }
716471d1714SBenjamin Tissoires 
717471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
718471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
719471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
720471d1714SBenjamin Tissoires {									\
721471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
722471d1714SBenjamin Tissoires }									\
723e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
724471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
725471d1714SBenjamin Tissoires 
726471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
727471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
728471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
729471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
730471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
731471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
732471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
733471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
734471d1714SBenjamin Tissoires 
735471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
736471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
737471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
738471d1714SBenjamin Tissoires 	NULL
739471d1714SBenjamin Tissoires };
740471d1714SBenjamin Tissoires 
741471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
742471d1714SBenjamin Tissoires 	.name = "wacom_led",
743471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
744471d1714SBenjamin Tissoires };
745471d1714SBenjamin Tissoires 
746471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
747471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
748471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
749471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
750471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
751471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
752471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
753471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
754471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
755471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
756471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
757471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
758471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
759471d1714SBenjamin Tissoires 	NULL
760471d1714SBenjamin Tissoires };
761471d1714SBenjamin Tissoires 
762471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
763471d1714SBenjamin Tissoires 	.name = "wacom_led",
764471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
765471d1714SBenjamin Tissoires };
766471d1714SBenjamin Tissoires 
767471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
768471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
769471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
770471d1714SBenjamin Tissoires 	NULL
771471d1714SBenjamin Tissoires };
772471d1714SBenjamin Tissoires 
773471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
774471d1714SBenjamin Tissoires 	.name = "wacom_led",
775471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
776471d1714SBenjamin Tissoires };
777471d1714SBenjamin Tissoires 
778471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
779471d1714SBenjamin Tissoires {
780471d1714SBenjamin Tissoires 	int error;
781471d1714SBenjamin Tissoires 
782471d1714SBenjamin Tissoires 	/* Initialize default values */
783471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
784471d1714SBenjamin Tissoires 	case INTUOS4S:
785471d1714SBenjamin Tissoires 	case INTUOS4:
78681af7e61SBenjamin Tissoires 	case INTUOS4WL:
787471d1714SBenjamin Tissoires 	case INTUOS4L:
788471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
789471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
790471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
791471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
792471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
793471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
794471d1714SBenjamin Tissoires 					   &intuos4_led_attr_group);
795471d1714SBenjamin Tissoires 		break;
796471d1714SBenjamin Tissoires 
797471d1714SBenjamin Tissoires 	case WACOM_24HD:
798471d1714SBenjamin Tissoires 	case WACOM_21UX2:
799471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
800471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
801471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
802471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
803471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
804471d1714SBenjamin Tissoires 
805471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
806471d1714SBenjamin Tissoires 					   &cintiq_led_attr_group);
807471d1714SBenjamin Tissoires 		break;
808471d1714SBenjamin Tissoires 
809471d1714SBenjamin Tissoires 	case INTUOS5S:
810471d1714SBenjamin Tissoires 	case INTUOS5:
811471d1714SBenjamin Tissoires 	case INTUOS5L:
812471d1714SBenjamin Tissoires 	case INTUOSPS:
813471d1714SBenjamin Tissoires 	case INTUOSPM:
814471d1714SBenjamin Tissoires 	case INTUOSPL:
815471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
816471d1714SBenjamin Tissoires 			wacom->led.select[0] = 0;
817471d1714SBenjamin Tissoires 			wacom->led.select[1] = 0;
818471d1714SBenjamin Tissoires 			wacom->led.llv = 32;
819471d1714SBenjamin Tissoires 			wacom->led.hlv = 0;
820471d1714SBenjamin Tissoires 			wacom->led.img_lum = 0;
821471d1714SBenjamin Tissoires 
822471d1714SBenjamin Tissoires 			error = sysfs_create_group(&wacom->hdev->dev.kobj,
823471d1714SBenjamin Tissoires 						  &intuos5_led_attr_group);
824471d1714SBenjamin Tissoires 		} else
825471d1714SBenjamin Tissoires 			return 0;
826471d1714SBenjamin Tissoires 		break;
827471d1714SBenjamin Tissoires 
828471d1714SBenjamin Tissoires 	default:
829471d1714SBenjamin Tissoires 		return 0;
830471d1714SBenjamin Tissoires 	}
831471d1714SBenjamin Tissoires 
832471d1714SBenjamin Tissoires 	if (error) {
833471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
834471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
835471d1714SBenjamin Tissoires 		return error;
836471d1714SBenjamin Tissoires 	}
837471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
838c757cbafSBenjamin Tissoires 	wacom->led_initialized = true;
839471d1714SBenjamin Tissoires 
840471d1714SBenjamin Tissoires 	return 0;
841471d1714SBenjamin Tissoires }
842471d1714SBenjamin Tissoires 
843471d1714SBenjamin Tissoires static void wacom_destroy_leds(struct wacom *wacom)
844471d1714SBenjamin Tissoires {
845c757cbafSBenjamin Tissoires 	if (!wacom->led_initialized)
846c757cbafSBenjamin Tissoires 		return;
847c757cbafSBenjamin Tissoires 
848c757cbafSBenjamin Tissoires 	wacom->led_initialized = false;
849c757cbafSBenjamin Tissoires 
850471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
851471d1714SBenjamin Tissoires 	case INTUOS4S:
852471d1714SBenjamin Tissoires 	case INTUOS4:
85381af7e61SBenjamin Tissoires 	case INTUOS4WL:
854471d1714SBenjamin Tissoires 	case INTUOS4L:
855471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
856471d1714SBenjamin Tissoires 				   &intuos4_led_attr_group);
857471d1714SBenjamin Tissoires 		break;
858471d1714SBenjamin Tissoires 
859471d1714SBenjamin Tissoires 	case WACOM_24HD:
860471d1714SBenjamin Tissoires 	case WACOM_21UX2:
861471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
862471d1714SBenjamin Tissoires 				   &cintiq_led_attr_group);
863471d1714SBenjamin Tissoires 		break;
864471d1714SBenjamin Tissoires 
865471d1714SBenjamin Tissoires 	case INTUOS5S:
866471d1714SBenjamin Tissoires 	case INTUOS5:
867471d1714SBenjamin Tissoires 	case INTUOS5L:
868471d1714SBenjamin Tissoires 	case INTUOSPS:
869471d1714SBenjamin Tissoires 	case INTUOSPM:
870471d1714SBenjamin Tissoires 	case INTUOSPL:
871471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
872471d1714SBenjamin Tissoires 			sysfs_remove_group(&wacom->hdev->dev.kobj,
873471d1714SBenjamin Tissoires 					   &intuos5_led_attr_group);
874471d1714SBenjamin Tissoires 		break;
875471d1714SBenjamin Tissoires 	}
876471d1714SBenjamin Tissoires }
877471d1714SBenjamin Tissoires 
878471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
879ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
880471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
881471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
882471d1714SBenjamin Tissoires };
883471d1714SBenjamin Tissoires 
8847dbd229eSBenjamin Tissoires static enum power_supply_property wacom_ac_props[] = {
8857dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_PRESENT,
8867dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_ONLINE,
8877dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
8887dbd229eSBenjamin Tissoires };
8897dbd229eSBenjamin Tissoires 
890471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
891471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
892471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
893471d1714SBenjamin Tissoires {
894471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(psy, struct wacom, battery);
895471d1714SBenjamin Tissoires 	int ret = 0;
896471d1714SBenjamin Tissoires 
897471d1714SBenjamin Tissoires 	switch (psp) {
898471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
899471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
900471d1714SBenjamin Tissoires 			break;
901471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
902471d1714SBenjamin Tissoires 			val->intval =
903ac8d1010SBenjamin Tissoires 				wacom->wacom_wac.battery_capacity;
904ac8d1010SBenjamin Tissoires 			break;
905ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
906ac8d1010SBenjamin Tissoires 			if (wacom->wacom_wac.bat_charging)
907ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
908ac8d1010SBenjamin Tissoires 			else if (wacom->wacom_wac.battery_capacity == 100 &&
909ac8d1010SBenjamin Tissoires 				    wacom->wacom_wac.ps_connected)
910ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
911ac8d1010SBenjamin Tissoires 			else
912ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
913471d1714SBenjamin Tissoires 			break;
914471d1714SBenjamin Tissoires 		default:
915471d1714SBenjamin Tissoires 			ret = -EINVAL;
916471d1714SBenjamin Tissoires 			break;
917471d1714SBenjamin Tissoires 	}
918471d1714SBenjamin Tissoires 
919471d1714SBenjamin Tissoires 	return ret;
920471d1714SBenjamin Tissoires }
921471d1714SBenjamin Tissoires 
9227dbd229eSBenjamin Tissoires static int wacom_ac_get_property(struct power_supply *psy,
9237dbd229eSBenjamin Tissoires 				enum power_supply_property psp,
9247dbd229eSBenjamin Tissoires 				union power_supply_propval *val)
9257dbd229eSBenjamin Tissoires {
9267dbd229eSBenjamin Tissoires 	struct wacom *wacom = container_of(psy, struct wacom, ac);
9277dbd229eSBenjamin Tissoires 	int ret = 0;
9287dbd229eSBenjamin Tissoires 
9297dbd229eSBenjamin Tissoires 	switch (psp) {
9307dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_PRESENT:
9317dbd229eSBenjamin Tissoires 		/* fall through */
9327dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_ONLINE:
9337dbd229eSBenjamin Tissoires 		val->intval = wacom->wacom_wac.ps_connected;
9347dbd229eSBenjamin Tissoires 		break;
9357dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_SCOPE:
9367dbd229eSBenjamin Tissoires 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
9377dbd229eSBenjamin Tissoires 		break;
9387dbd229eSBenjamin Tissoires 	default:
9397dbd229eSBenjamin Tissoires 		ret = -EINVAL;
9407dbd229eSBenjamin Tissoires 		break;
9417dbd229eSBenjamin Tissoires 	}
9427dbd229eSBenjamin Tissoires 	return ret;
9437dbd229eSBenjamin Tissoires }
9447dbd229eSBenjamin Tissoires 
945471d1714SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
946471d1714SBenjamin Tissoires {
947d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
9487dbd229eSBenjamin Tissoires 	int error;
949d70420b9SBenjamin Tissoires 	unsigned long n;
950471d1714SBenjamin Tissoires 
951ac8d1010SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
952d70420b9SBenjamin Tissoires 		n = atomic_inc_return(&battery_no) - 1;
9537dbd229eSBenjamin Tissoires 
954471d1714SBenjamin Tissoires 		wacom->battery.properties = wacom_battery_props;
955471d1714SBenjamin Tissoires 		wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
956471d1714SBenjamin Tissoires 		wacom->battery.get_property = wacom_battery_get_property;
957d70420b9SBenjamin Tissoires 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
958d70420b9SBenjamin Tissoires 		wacom->battery.name = wacom->wacom_wac.bat_name;
959471d1714SBenjamin Tissoires 		wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
960471d1714SBenjamin Tissoires 		wacom->battery.use_for_apm = 0;
961471d1714SBenjamin Tissoires 
9627dbd229eSBenjamin Tissoires 		wacom->ac.properties = wacom_ac_props;
9637dbd229eSBenjamin Tissoires 		wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
9647dbd229eSBenjamin Tissoires 		wacom->ac.get_property = wacom_ac_get_property;
9657dbd229eSBenjamin Tissoires 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
9667dbd229eSBenjamin Tissoires 		wacom->ac.name = wacom->wacom_wac.ac_name;
9677dbd229eSBenjamin Tissoires 		wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
9687dbd229eSBenjamin Tissoires 		wacom->ac.use_for_apm = 0;
9697dbd229eSBenjamin Tissoires 
970471d1714SBenjamin Tissoires 		error = power_supply_register(&wacom->hdev->dev,
971471d1714SBenjamin Tissoires 					      &wacom->battery);
9727dbd229eSBenjamin Tissoires 		if (error)
9737dbd229eSBenjamin Tissoires 			return error;
974471d1714SBenjamin Tissoires 
9757dbd229eSBenjamin Tissoires 		power_supply_powers(&wacom->battery, &wacom->hdev->dev);
9767dbd229eSBenjamin Tissoires 
9777dbd229eSBenjamin Tissoires 		error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
9787dbd229eSBenjamin Tissoires 		if (error) {
9797dbd229eSBenjamin Tissoires 			power_supply_unregister(&wacom->battery);
9807dbd229eSBenjamin Tissoires 			return error;
981471d1714SBenjamin Tissoires 		}
982471d1714SBenjamin Tissoires 
9837dbd229eSBenjamin Tissoires 		power_supply_powers(&wacom->ac, &wacom->hdev->dev);
9847dbd229eSBenjamin Tissoires 	}
9857dbd229eSBenjamin Tissoires 
9867dbd229eSBenjamin Tissoires 	return 0;
987471d1714SBenjamin Tissoires }
988471d1714SBenjamin Tissoires 
989471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
990471d1714SBenjamin Tissoires {
991ac8d1010SBenjamin Tissoires 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
992471d1714SBenjamin Tissoires 	     wacom->battery.dev) {
993471d1714SBenjamin Tissoires 		power_supply_unregister(&wacom->battery);
994471d1714SBenjamin Tissoires 		wacom->battery.dev = NULL;
9957dbd229eSBenjamin Tissoires 		power_supply_unregister(&wacom->ac);
9967dbd229eSBenjamin Tissoires 		wacom->ac.dev = NULL;
997471d1714SBenjamin Tissoires 	}
998471d1714SBenjamin Tissoires }
999471d1714SBenjamin Tissoires 
1000f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1001f81a1295SBenjamin Tissoires 				struct device_attribute
1002f81a1295SBenjamin Tissoires 				*attr, char *buf)
1003f81a1295SBenjamin Tissoires {
1004f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1005f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1006f81a1295SBenjamin Tissoires 
1007f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1008f81a1295SBenjamin Tissoires }
1009f81a1295SBenjamin Tissoires 
1010f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1011f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1012f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1013f81a1295SBenjamin Tissoires {
1014f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1015f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1016f81a1295SBenjamin Tissoires 	u8 new_speed;
1017f81a1295SBenjamin Tissoires 
1018f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1019f81a1295SBenjamin Tissoires 		return -EINVAL;
1020f81a1295SBenjamin Tissoires 
1021f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1022f81a1295SBenjamin Tissoires 		return -EINVAL;
1023f81a1295SBenjamin Tissoires 
1024f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1025f81a1295SBenjamin Tissoires 
1026f81a1295SBenjamin Tissoires 	return count;
1027f81a1295SBenjamin Tissoires }
1028f81a1295SBenjamin Tissoires 
1029e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1030f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1031f81a1295SBenjamin Tissoires 
1032471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1033471d1714SBenjamin Tissoires {
1034471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1035471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1036471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1037471d1714SBenjamin Tissoires 
1038471d1714SBenjamin Tissoires 	input_dev = input_allocate_device();
1039471d1714SBenjamin Tissoires 	if (!input_dev)
1040471d1714SBenjamin Tissoires 		return NULL;
1041471d1714SBenjamin Tissoires 
1042471d1714SBenjamin Tissoires 	input_dev->name = wacom_wac->name;
1043471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1044471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1045471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1046471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1047471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1048471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1049471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
105012969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1051471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1052471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1053471d1714SBenjamin Tissoires 
1054471d1714SBenjamin Tissoires 	return input_dev;
1055471d1714SBenjamin Tissoires }
1056471d1714SBenjamin Tissoires 
1057471d1714SBenjamin Tissoires static void wacom_unregister_inputs(struct wacom *wacom)
1058471d1714SBenjamin Tissoires {
1059471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.input)
1060471d1714SBenjamin Tissoires 		input_unregister_device(wacom->wacom_wac.input);
1061471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.pad_input)
1062471d1714SBenjamin Tissoires 		input_unregister_device(wacom->wacom_wac.pad_input);
1063471d1714SBenjamin Tissoires 	wacom->wacom_wac.input = NULL;
1064471d1714SBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
1065912ca216SPing Cheng 	wacom_destroy_leds(wacom);
1066471d1714SBenjamin Tissoires }
1067471d1714SBenjamin Tissoires 
1068471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1069471d1714SBenjamin Tissoires {
1070471d1714SBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
1071471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1072471d1714SBenjamin Tissoires 	int error;
1073471d1714SBenjamin Tissoires 
1074471d1714SBenjamin Tissoires 	input_dev = wacom_allocate_input(wacom);
1075471d1714SBenjamin Tissoires 	pad_input_dev = wacom_allocate_input(wacom);
1076471d1714SBenjamin Tissoires 	if (!input_dev || !pad_input_dev) {
1077471d1714SBenjamin Tissoires 		error = -ENOMEM;
1078471d1714SBenjamin Tissoires 		goto fail1;
1079471d1714SBenjamin Tissoires 	}
1080471d1714SBenjamin Tissoires 
1081471d1714SBenjamin Tissoires 	wacom_wac->input = input_dev;
1082471d1714SBenjamin Tissoires 	wacom_wac->pad_input = pad_input_dev;
1083471d1714SBenjamin Tissoires 	wacom_wac->pad_input->name = wacom_wac->pad_name;
1084471d1714SBenjamin Tissoires 
1085471d1714SBenjamin Tissoires 	error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1086471d1714SBenjamin Tissoires 	if (error)
1087471d1714SBenjamin Tissoires 		goto fail2;
1088471d1714SBenjamin Tissoires 
1089471d1714SBenjamin Tissoires 	error = input_register_device(input_dev);
1090471d1714SBenjamin Tissoires 	if (error)
1091471d1714SBenjamin Tissoires 		goto fail2;
1092471d1714SBenjamin Tissoires 
1093471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1094471d1714SBenjamin Tissoires 	if (error) {
1095471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1096471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1097471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1098471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1099471d1714SBenjamin Tissoires 	} else {
1100471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1101471d1714SBenjamin Tissoires 		if (error)
1102471d1714SBenjamin Tissoires 			goto fail3;
1103912ca216SPing Cheng 
1104912ca216SPing Cheng 		error = wacom_initialize_leds(wacom);
1105912ca216SPing Cheng 		if (error)
1106912ca216SPing Cheng 			goto fail4;
1107471d1714SBenjamin Tissoires 	}
1108471d1714SBenjamin Tissoires 
1109471d1714SBenjamin Tissoires 	return 0;
1110471d1714SBenjamin Tissoires 
1111912ca216SPing Cheng fail4:
1112912ca216SPing Cheng 	input_unregister_device(pad_input_dev);
1113912ca216SPing Cheng 	pad_input_dev = NULL;
1114471d1714SBenjamin Tissoires fail3:
1115471d1714SBenjamin Tissoires 	input_unregister_device(input_dev);
1116471d1714SBenjamin Tissoires 	input_dev = NULL;
1117471d1714SBenjamin Tissoires fail2:
1118471d1714SBenjamin Tissoires 	wacom_wac->input = NULL;
1119471d1714SBenjamin Tissoires 	wacom_wac->pad_input = NULL;
1120471d1714SBenjamin Tissoires fail1:
1121471d1714SBenjamin Tissoires 	if (input_dev)
1122471d1714SBenjamin Tissoires 		input_free_device(input_dev);
1123471d1714SBenjamin Tissoires 	if (pad_input_dev)
1124471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1125471d1714SBenjamin Tissoires 	return error;
1126471d1714SBenjamin Tissoires }
1127471d1714SBenjamin Tissoires 
1128471d1714SBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
1129471d1714SBenjamin Tissoires {
1130471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, work);
1131471d1714SBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
1132471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1133471d1714SBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
1134471d1714SBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
1135471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1136471d1714SBenjamin Tissoires 	int error;
1137471d1714SBenjamin Tissoires 
1138471d1714SBenjamin Tissoires 	/*
1139471d1714SBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
1140471d1714SBenjamin Tissoires 	 * remove any existing input and battery devices.
1141471d1714SBenjamin Tissoires 	 */
1142471d1714SBenjamin Tissoires 
1143471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1144471d1714SBenjamin Tissoires 
1145471d1714SBenjamin Tissoires 	/* Stylus interface */
1146471d1714SBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1147471d1714SBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
1148471d1714SBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
1149471d1714SBenjamin Tissoires 	wacom_unregister_inputs(wacom1);
1150471d1714SBenjamin Tissoires 
1151471d1714SBenjamin Tissoires 	/* Touch interface */
1152471d1714SBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1153471d1714SBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
1154471d1714SBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
1155471d1714SBenjamin Tissoires 	wacom_unregister_inputs(wacom2);
1156471d1714SBenjamin Tissoires 
1157471d1714SBenjamin Tissoires 	if (wacom_wac->pid == 0) {
1158471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1159ac8d1010SBenjamin Tissoires 		wacom_wac1->shared->type = 0;
1160471d1714SBenjamin Tissoires 	} else {
1161471d1714SBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
1162471d1714SBenjamin Tissoires 
1163471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1164471d1714SBenjamin Tissoires 			 wacom_wac->pid);
1165471d1714SBenjamin Tissoires 
1166471d1714SBenjamin Tissoires 		while (id->bus) {
1167471d1714SBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1168471d1714SBenjamin Tissoires 			    id->product == wacom_wac->pid)
1169471d1714SBenjamin Tissoires 				break;
1170471d1714SBenjamin Tissoires 			id++;
1171471d1714SBenjamin Tissoires 		}
1172471d1714SBenjamin Tissoires 
1173471d1714SBenjamin Tissoires 		if (!id->bus) {
1174471d1714SBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1175471d1714SBenjamin Tissoires 			return;
1176471d1714SBenjamin Tissoires 		}
1177471d1714SBenjamin Tissoires 
1178471d1714SBenjamin Tissoires 		/* Stylus interface */
1179471d1714SBenjamin Tissoires 		wacom_wac1->features =
1180471d1714SBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
1181471d1714SBenjamin Tissoires 		wacom_wac1->features.device_type = BTN_TOOL_PEN;
1182471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1183471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1184471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1185471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1186471d1714SBenjamin Tissoires 		wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1187471d1714SBenjamin Tissoires 		wacom_wac1->shared->type = wacom_wac1->features.type;
1188912ca216SPing Cheng 		wacom_wac1->pid = wacom_wac->pid;
1189471d1714SBenjamin Tissoires 		error = wacom_register_inputs(wacom1);
1190471d1714SBenjamin Tissoires 		if (error)
1191471d1714SBenjamin Tissoires 			goto fail;
1192471d1714SBenjamin Tissoires 
1193471d1714SBenjamin Tissoires 		/* Touch interface */
1194471d1714SBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
1195471d1714SBenjamin Tissoires 		    wacom_wac1->features.type == INTUOSHT) {
1196471d1714SBenjamin Tissoires 			wacom_wac2->features =
1197471d1714SBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
1198471d1714SBenjamin Tissoires 			wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1199471d1714SBenjamin Tissoires 			wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1200471d1714SBenjamin Tissoires 			wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1201471d1714SBenjamin Tissoires 			if (wacom_wac2->features.touch_max)
1202471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1203471d1714SBenjamin Tissoires 					 "%s (WL) Finger",wacom_wac2->features.name);
1204471d1714SBenjamin Tissoires 			else
1205471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1206471d1714SBenjamin Tissoires 					 "%s (WL) Pad",wacom_wac2->features.name);
1207471d1714SBenjamin Tissoires 			snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1208471d1714SBenjamin Tissoires 				 "%s (WL) Pad", wacom_wac2->features.name);
1209912ca216SPing Cheng 			wacom_wac2->pid = wacom_wac->pid;
1210471d1714SBenjamin Tissoires 			error = wacom_register_inputs(wacom2);
1211471d1714SBenjamin Tissoires 			if (error)
1212471d1714SBenjamin Tissoires 				goto fail;
1213471d1714SBenjamin Tissoires 
1214471d1714SBenjamin Tissoires 			if (wacom_wac1->features.type == INTUOSHT &&
1215471d1714SBenjamin Tissoires 			    wacom_wac1->features.touch_max)
1216471d1714SBenjamin Tissoires 				wacom_wac->shared->touch_input = wacom_wac2->input;
1217471d1714SBenjamin Tissoires 		}
1218471d1714SBenjamin Tissoires 
1219471d1714SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1220471d1714SBenjamin Tissoires 		if (error)
1221471d1714SBenjamin Tissoires 			goto fail;
1222471d1714SBenjamin Tissoires 	}
1223471d1714SBenjamin Tissoires 
1224471d1714SBenjamin Tissoires 	return;
1225471d1714SBenjamin Tissoires 
1226471d1714SBenjamin Tissoires fail:
1227471d1714SBenjamin Tissoires 	wacom_unregister_inputs(wacom1);
1228471d1714SBenjamin Tissoires 	wacom_unregister_inputs(wacom2);
1229471d1714SBenjamin Tissoires 	return;
1230471d1714SBenjamin Tissoires }
1231471d1714SBenjamin Tissoires 
1232471d1714SBenjamin Tissoires /*
1233471d1714SBenjamin Tissoires  * Not all devices report physical dimensions from HID.
1234471d1714SBenjamin Tissoires  * Compute the default from hardcoded logical dimension
1235471d1714SBenjamin Tissoires  * and resolution before driver overwrites them.
1236471d1714SBenjamin Tissoires  */
1237471d1714SBenjamin Tissoires static void wacom_set_default_phy(struct wacom_features *features)
1238471d1714SBenjamin Tissoires {
1239471d1714SBenjamin Tissoires 	if (features->x_resolution) {
1240471d1714SBenjamin Tissoires 		features->x_phy = (features->x_max * 100) /
1241471d1714SBenjamin Tissoires 					features->x_resolution;
1242471d1714SBenjamin Tissoires 		features->y_phy = (features->y_max * 100) /
1243471d1714SBenjamin Tissoires 					features->y_resolution;
1244471d1714SBenjamin Tissoires 	}
1245471d1714SBenjamin Tissoires }
1246471d1714SBenjamin Tissoires 
1247471d1714SBenjamin Tissoires static void wacom_calculate_res(struct wacom_features *features)
1248471d1714SBenjamin Tissoires {
1249471d1714SBenjamin Tissoires 	features->x_resolution = wacom_calc_hid_res(features->x_max,
1250471d1714SBenjamin Tissoires 						    features->x_phy,
1251471d1714SBenjamin Tissoires 						    features->unit,
1252471d1714SBenjamin Tissoires 						    features->unitExpo);
1253471d1714SBenjamin Tissoires 	features->y_resolution = wacom_calc_hid_res(features->y_max,
1254471d1714SBenjamin Tissoires 						    features->y_phy,
1255471d1714SBenjamin Tissoires 						    features->unit,
1256471d1714SBenjamin Tissoires 						    features->unitExpo);
1257471d1714SBenjamin Tissoires }
1258471d1714SBenjamin Tissoires 
1259471d1714SBenjamin Tissoires static int wacom_hid_report_len(struct hid_report *report)
1260471d1714SBenjamin Tissoires {
1261471d1714SBenjamin Tissoires 	/* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1262471d1714SBenjamin Tissoires 	return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1263471d1714SBenjamin Tissoires }
1264471d1714SBenjamin Tissoires 
1265471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1266471d1714SBenjamin Tissoires {
1267471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1268471d1714SBenjamin Tissoires 	struct hid_report *report;
1269471d1714SBenjamin Tissoires 	size_t size = 0;
1270471d1714SBenjamin Tissoires 
1271471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1272471d1714SBenjamin Tissoires 
1273471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1274471d1714SBenjamin Tissoires 		size_t report_size = wacom_hid_report_len(report);
1275471d1714SBenjamin Tissoires 		if (report_size > size)
1276471d1714SBenjamin Tissoires 			size = report_size;
1277471d1714SBenjamin Tissoires 	}
1278471d1714SBenjamin Tissoires 
1279471d1714SBenjamin Tissoires 	return size;
1280471d1714SBenjamin Tissoires }
1281471d1714SBenjamin Tissoires 
1282471d1714SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
1283471d1714SBenjamin Tissoires 		const struct hid_device_id *id)
1284471d1714SBenjamin Tissoires {
1285471d1714SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1286471d1714SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
1287471d1714SBenjamin Tissoires 	struct wacom *wacom;
1288471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
1289471d1714SBenjamin Tissoires 	struct wacom_features *features;
1290471d1714SBenjamin Tissoires 	int error;
1291471d1714SBenjamin Tissoires 
1292471d1714SBenjamin Tissoires 	if (!id->driver_data)
1293471d1714SBenjamin Tissoires 		return -EINVAL;
1294471d1714SBenjamin Tissoires 
1295*8ffffd52SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1296*8ffffd52SBenjamin Tissoires 
1297471d1714SBenjamin Tissoires 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1298471d1714SBenjamin Tissoires 	if (!wacom)
1299471d1714SBenjamin Tissoires 		return -ENOMEM;
1300471d1714SBenjamin Tissoires 
1301471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
1302471d1714SBenjamin Tissoires 	wacom->hdev = hdev;
1303471d1714SBenjamin Tissoires 
1304471d1714SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
1305471d1714SBenjamin Tissoires 	error = hid_parse(hdev);
1306471d1714SBenjamin Tissoires 	if (error) {
1307471d1714SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
1308471d1714SBenjamin Tissoires 		goto fail1;
1309471d1714SBenjamin Tissoires 	}
1310471d1714SBenjamin Tissoires 
1311471d1714SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
1312471d1714SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
1313471d1714SBenjamin Tissoires 	features = &wacom_wac->features;
1314471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1315471d1714SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX) {
1316471d1714SBenjamin Tissoires 		error = -EINVAL;
1317471d1714SBenjamin Tissoires 		goto fail1;
1318471d1714SBenjamin Tissoires 	}
1319471d1714SBenjamin Tissoires 
1320471d1714SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
1321471d1714SBenjamin Tissoires 		error = -ENODEV;
1322471d1714SBenjamin Tissoires 		goto fail1;
1323471d1714SBenjamin Tissoires 	}
1324471d1714SBenjamin Tissoires 
1325471d1714SBenjamin Tissoires 	wacom->usbdev = dev;
1326471d1714SBenjamin Tissoires 	wacom->intf = intf;
1327471d1714SBenjamin Tissoires 	mutex_init(&wacom->lock);
1328471d1714SBenjamin Tissoires 	INIT_WORK(&wacom->work, wacom_wireless_work);
1329471d1714SBenjamin Tissoires 
1330471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1331471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1332471d1714SBenjamin Tissoires 
1333471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1334471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
1335471d1714SBenjamin Tissoires 
1336471d1714SBenjamin Tissoires 	/*
1337471d1714SBenjamin Tissoires 	 * Intuos5 has no useful data about its touch interface in its
1338471d1714SBenjamin Tissoires 	 * HID descriptor. If this is the touch interface (PacketSize
1339471d1714SBenjamin Tissoires 	 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1340471d1714SBenjamin Tissoires 	 */
1341471d1714SBenjamin Tissoires 	if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1342471d1714SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1343471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
1344471d1714SBenjamin Tissoires 
1345471d1714SBenjamin Tissoires 			features->x_max = 4096;
1346471d1714SBenjamin Tissoires 			features->y_max = 4096;
1347471d1714SBenjamin Tissoires 		} else {
1348471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_PEN;
1349471d1714SBenjamin Tissoires 		}
1350471d1714SBenjamin Tissoires 	}
1351471d1714SBenjamin Tissoires 
1352471d1714SBenjamin Tissoires 	/*
1353471d1714SBenjamin Tissoires 	 * Same thing for Bamboo 3rd gen.
1354471d1714SBenjamin Tissoires 	 */
1355471d1714SBenjamin Tissoires 	if ((features->type == BAMBOO_PT) &&
1356471d1714SBenjamin Tissoires 	    (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1357471d1714SBenjamin Tissoires 	    (features->device_type == BTN_TOOL_PEN)) {
1358471d1714SBenjamin Tissoires 		features->device_type = BTN_TOOL_FINGER;
1359471d1714SBenjamin Tissoires 
1360471d1714SBenjamin Tissoires 		features->x_max = 4096;
1361471d1714SBenjamin Tissoires 		features->y_max = 4096;
1362471d1714SBenjamin Tissoires 	}
1363471d1714SBenjamin Tissoires 
1364f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1365f81a1295SBenjamin Tissoires 		features->quirks |= WACOM_QUIRK_BATTERY;
1366f81a1295SBenjamin Tissoires 
1367471d1714SBenjamin Tissoires 	wacom_setup_device_quirks(features);
1368471d1714SBenjamin Tissoires 
1369471d1714SBenjamin Tissoires 	/* set unit to "100th of a mm" for devices not reported by HID */
1370471d1714SBenjamin Tissoires 	if (!features->unit) {
1371471d1714SBenjamin Tissoires 		features->unit = 0x11;
1372471d1714SBenjamin Tissoires 		features->unitExpo = -3;
1373471d1714SBenjamin Tissoires 	}
1374471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1375471d1714SBenjamin Tissoires 
1376471d1714SBenjamin Tissoires 	strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1377471d1714SBenjamin Tissoires 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1378471d1714SBenjamin Tissoires 		"%s Pad", features->name);
1379471d1714SBenjamin Tissoires 
1380471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1381471d1714SBenjamin Tissoires 		/* Append the device type to the name */
1382471d1714SBenjamin Tissoires 		if (features->device_type != BTN_TOOL_FINGER)
1383471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1384471d1714SBenjamin Tissoires 		else if (features->touch_max)
1385471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1386471d1714SBenjamin Tissoires 		else
1387471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1388471d1714SBenjamin Tissoires 
1389471d1714SBenjamin Tissoires 		error = wacom_add_shared_data(hdev);
1390471d1714SBenjamin Tissoires 		if (error)
1391471d1714SBenjamin Tissoires 			goto fail1;
1392471d1714SBenjamin Tissoires 	}
1393471d1714SBenjamin Tissoires 
1394f81a1295SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1395f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1396f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1397f81a1295SBenjamin Tissoires 		if (error)
1398912ca216SPing Cheng 			goto fail2;
1399f81a1295SBenjamin Tissoires 	}
1400f81a1295SBenjamin Tissoires 
1401471d1714SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1402471d1714SBenjamin Tissoires 		error = wacom_register_inputs(wacom);
1403471d1714SBenjamin Tissoires 		if (error)
1404912ca216SPing Cheng 			goto fail3;
1405f81a1295SBenjamin Tissoires 	}
1406f81a1295SBenjamin Tissoires 
1407f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1408f81a1295SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
1409f81a1295SBenjamin Tissoires 		if (error)
1410f81a1295SBenjamin Tissoires 			hid_warn(hdev,
1411f81a1295SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
1412f81a1295SBenjamin Tissoires 				 error);
1413471d1714SBenjamin Tissoires 	}
1414471d1714SBenjamin Tissoires 
1415471d1714SBenjamin Tissoires 	/* Note that if query fails it is not a hard failure */
1416471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
1417471d1714SBenjamin Tissoires 
1418471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
1419471d1714SBenjamin Tissoires 	error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1420471d1714SBenjamin Tissoires 	if (error) {
1421471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
1422912ca216SPing Cheng 		goto fail4;
1423471d1714SBenjamin Tissoires 	}
1424471d1714SBenjamin Tissoires 
1425471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MONITOR)
1426471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
1427471d1714SBenjamin Tissoires 
1428471d1714SBenjamin Tissoires 	if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1429471d1714SBenjamin Tissoires 		if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1430471d1714SBenjamin Tissoires 			wacom_wac->shared->touch_input = wacom_wac->input;
1431471d1714SBenjamin Tissoires 	}
1432471d1714SBenjamin Tissoires 
1433471d1714SBenjamin Tissoires 	return 0;
1434471d1714SBenjamin Tissoires 
1435912ca216SPing Cheng  fail4:	if (hdev->bus == BUS_BLUETOOTH)
1436f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
1437f81a1295SBenjamin Tissoires 	wacom_unregister_inputs(wacom);
1438912ca216SPing Cheng  fail3:	wacom_destroy_battery(wacom);
1439471d1714SBenjamin Tissoires  fail2:	wacom_remove_shared_data(wacom_wac);
1440471d1714SBenjamin Tissoires  fail1:	kfree(wacom);
1441471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1442471d1714SBenjamin Tissoires 	return error;
1443471d1714SBenjamin Tissoires }
1444471d1714SBenjamin Tissoires 
1445471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
1446471d1714SBenjamin Tissoires {
1447471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1448471d1714SBenjamin Tissoires 
1449471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
1450471d1714SBenjamin Tissoires 
1451471d1714SBenjamin Tissoires 	cancel_work_sync(&wacom->work);
1452471d1714SBenjamin Tissoires 	wacom_unregister_inputs(wacom);
1453f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1454f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
1455471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1456471d1714SBenjamin Tissoires 	wacom_remove_shared_data(&wacom->wacom_wac);
1457471d1714SBenjamin Tissoires 
1458471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1459471d1714SBenjamin Tissoires 	kfree(wacom);
1460471d1714SBenjamin Tissoires }
1461471d1714SBenjamin Tissoires 
146241a74581SGeert Uytterhoeven #ifdef CONFIG_PM
1463471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
1464471d1714SBenjamin Tissoires {
1465471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1466471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1467471d1714SBenjamin Tissoires 
1468471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1469471d1714SBenjamin Tissoires 
1470471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
1471471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
1472471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1473471d1714SBenjamin Tissoires 
1474471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1475471d1714SBenjamin Tissoires 
1476471d1714SBenjamin Tissoires 	return 0;
1477471d1714SBenjamin Tissoires }
1478471d1714SBenjamin Tissoires 
1479471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
1480471d1714SBenjamin Tissoires {
1481471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
1482471d1714SBenjamin Tissoires }
148341a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
1484471d1714SBenjamin Tissoires 
1485471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
1486471d1714SBenjamin Tissoires 	.name =		"wacom",
1487471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
1488471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
1489471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
1490471d1714SBenjamin Tissoires #ifdef CONFIG_PM
1491471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
1492471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
1493471d1714SBenjamin Tissoires #endif
1494471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
1495471d1714SBenjamin Tissoires };
1496471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
1497f2e0a7d4SBenjamin Tissoires 
1498f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
1499f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
1500f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
1501f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
1502