xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision dff674168878fe7b6d8b9ad60d62295ec517de79)
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 
17471d1714SBenjamin Tissoires #define WAC_MSG_RETRIES		5
18471d1714SBenjamin Tissoires 
19912ca216SPing Cheng #define WAC_CMD_WL_LED_CONTROL	0x03
20471d1714SBenjamin Tissoires #define WAC_CMD_LED_CONTROL	0x20
21471d1714SBenjamin Tissoires #define WAC_CMD_ICON_START	0x21
22471d1714SBenjamin Tissoires #define WAC_CMD_ICON_XFER	0x23
23849e2f06SBenjamin Tissoires #define WAC_CMD_ICON_BT_XFER	0x26
24471d1714SBenjamin Tissoires #define WAC_CMD_RETRIES		10
25471d1714SBenjamin Tissoires 
26e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
27e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
28e0984bc3SPing Cheng 
29c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
30c64d8834SPing Cheng 			    size_t size, unsigned int retries)
31471d1714SBenjamin Tissoires {
32471d1714SBenjamin Tissoires 	int retval;
33471d1714SBenjamin Tissoires 
34471d1714SBenjamin Tissoires 	do {
35c64d8834SPing Cheng 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
36471d1714SBenjamin Tissoires 				HID_REQ_GET_REPORT);
37471d1714SBenjamin Tissoires 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
38471d1714SBenjamin Tissoires 
39471d1714SBenjamin Tissoires 	return retval;
40471d1714SBenjamin Tissoires }
41471d1714SBenjamin Tissoires 
42296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
44471d1714SBenjamin Tissoires {
45471d1714SBenjamin Tissoires 	int retval;
46471d1714SBenjamin Tissoires 
47471d1714SBenjamin Tissoires 	do {
48296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
49471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
50471d1714SBenjamin Tissoires 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
51471d1714SBenjamin Tissoires 
52471d1714SBenjamin Tissoires 	return retval;
53471d1714SBenjamin Tissoires }
54471d1714SBenjamin Tissoires 
55471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
56471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
57471d1714SBenjamin Tissoires {
58471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
59471d1714SBenjamin Tissoires 
60471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
61471d1714SBenjamin Tissoires 		return 1;
62471d1714SBenjamin Tissoires 
63471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
64471d1714SBenjamin Tissoires 
65471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
66471d1714SBenjamin Tissoires 
67471d1714SBenjamin Tissoires 	return 0;
68471d1714SBenjamin Tissoires }
69471d1714SBenjamin Tissoires 
70471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
71471d1714SBenjamin Tissoires {
72471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
73471d1714SBenjamin Tissoires 
74*dff67416SBenjamin Tissoires 	return hid_hw_open(wacom->hdev);
75471d1714SBenjamin Tissoires }
76471d1714SBenjamin Tissoires 
77471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
78471d1714SBenjamin Tissoires {
79471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
80471d1714SBenjamin Tissoires 
81471d1714SBenjamin Tissoires 	hid_hw_close(wacom->hdev);
82471d1714SBenjamin Tissoires }
83471d1714SBenjamin Tissoires 
84471d1714SBenjamin Tissoires /*
85471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
86471d1714SBenjamin Tissoires  */
87471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
88471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
89471d1714SBenjamin Tissoires {
90471d1714SBenjamin Tissoires 	struct hid_field field = {
91471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
92471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
93471d1714SBenjamin Tissoires 		.unit = unit,
94471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
95471d1714SBenjamin Tissoires 	};
96471d1714SBenjamin Tissoires 
97471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
98471d1714SBenjamin Tissoires }
99471d1714SBenjamin Tissoires 
100471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
101471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
102471d1714SBenjamin Tissoires {
103471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
104471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1055ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
1068ffffd52SBenjamin Tissoires 	u8 *data;
1078ffffd52SBenjamin Tissoires 	int ret;
108471d1714SBenjamin Tissoires 
109471d1714SBenjamin Tissoires 	switch (usage->hid) {
110471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
111471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
1128ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
1138ffffd52SBenjamin Tissoires 			/* read manually */
1148ffffd52SBenjamin Tissoires 			data = kzalloc(2, GFP_KERNEL);
1158ffffd52SBenjamin Tissoires 			if (!data)
1168ffffd52SBenjamin Tissoires 				break;
1178ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
1188ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
1198ffffd52SBenjamin Tissoires 						data, 2, 0);
1208ffffd52SBenjamin Tissoires 			if (ret == 2)
1218ffffd52SBenjamin Tissoires 				features->touch_max = data[1];
1228ffffd52SBenjamin Tissoires 			kfree(data);
1238ffffd52SBenjamin Tissoires 		}
124471d1714SBenjamin Tissoires 		break;
1255ae6e89fSBenjamin Tissoires 	case HID_DG_INPUTMODE:
1265ae6e89fSBenjamin Tissoires 		/* Ignore if value index is out of bounds. */
1275ae6e89fSBenjamin Tissoires 		if (usage->usage_index >= field->report_count) {
1285ae6e89fSBenjamin Tissoires 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
1295ae6e89fSBenjamin Tissoires 			break;
1305ae6e89fSBenjamin Tissoires 		}
1315ae6e89fSBenjamin Tissoires 
1325ae6e89fSBenjamin Tissoires 		hid_data->inputmode = field->report->id;
1335ae6e89fSBenjamin Tissoires 		hid_data->inputmode_index = usage->usage_index;
1345ae6e89fSBenjamin Tissoires 		break;
135471d1714SBenjamin Tissoires 	}
136471d1714SBenjamin Tissoires }
137471d1714SBenjamin Tissoires 
138471d1714SBenjamin Tissoires /*
139471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
140471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
141471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
142471d1714SBenjamin Tissoires  * resolution based on product ID's.
143471d1714SBenjamin Tissoires  *
144471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
145471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
146471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
147471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
148471d1714SBenjamin Tissoires  * an additional table.
149471d1714SBenjamin Tissoires  *
150471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
151471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
152471d1714SBenjamin Tissoires  * function will ignore it.
153471d1714SBenjamin Tissoires  *
154471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
155471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
156471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
157471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
158471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
159471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
160471d1714SBenjamin Tissoires  *
161471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
162471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
163471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
164471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
165471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
166471d1714SBenjamin Tissoires  *
167471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
168471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
169471d1714SBenjamin Tissoires  */
170471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
171471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
172471d1714SBenjamin Tissoires {
173471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
174471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
175471d1714SBenjamin Tissoires 	bool finger = (field->logical == HID_DG_FINGER) ||
176471d1714SBenjamin Tissoires 		      (field->physical == HID_DG_FINGER);
177471d1714SBenjamin Tissoires 	bool pen = (field->logical == HID_DG_STYLUS) ||
178471d1714SBenjamin Tissoires 		   (field->physical == HID_DG_STYLUS);
179471d1714SBenjamin Tissoires 
180471d1714SBenjamin Tissoires 	/*
181471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
182471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
183471d1714SBenjamin Tissoires 	* values commonly reported.
184471d1714SBenjamin Tissoires 	*/
185471d1714SBenjamin Tissoires 	if (!pen && !finger)
186471d1714SBenjamin Tissoires 		return;
187471d1714SBenjamin Tissoires 
188471d1714SBenjamin Tissoires 	if (finger && !features->touch_max)
189471d1714SBenjamin Tissoires 		/* touch device at least supports one touch point */
190471d1714SBenjamin Tissoires 		features->touch_max = 1;
191471d1714SBenjamin Tissoires 
192471d1714SBenjamin Tissoires 	switch (usage->hid) {
193471d1714SBenjamin Tissoires 	case HID_GD_X:
194471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
195471d1714SBenjamin Tissoires 		if (finger) {
196471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
197471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
198471d1714SBenjamin Tissoires 			if (features->type != BAMBOO_PT) {
199471d1714SBenjamin Tissoires 				features->unit = field->unit;
200471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
201471d1714SBenjamin Tissoires 			}
202471d1714SBenjamin Tissoires 		} else {
203471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_PEN;
204471d1714SBenjamin Tissoires 		}
205471d1714SBenjamin Tissoires 		break;
206471d1714SBenjamin Tissoires 	case HID_GD_Y:
207471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
208471d1714SBenjamin Tissoires 		if (finger) {
209471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
210471d1714SBenjamin Tissoires 			if (features->type != BAMBOO_PT) {
211471d1714SBenjamin Tissoires 				features->unit = field->unit;
212471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
213471d1714SBenjamin Tissoires 			}
214471d1714SBenjamin Tissoires 		}
215471d1714SBenjamin Tissoires 		break;
216471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
217471d1714SBenjamin Tissoires 		if (pen)
218471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
219471d1714SBenjamin Tissoires 		break;
220471d1714SBenjamin Tissoires 	}
2217704ac93SBenjamin Tissoires 
2227704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
2237704ac93SBenjamin Tissoires 		wacom_wac_usage_mapping(hdev, field, usage);
224471d1714SBenjamin Tissoires }
225471d1714SBenjamin Tissoires 
226471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
227471d1714SBenjamin Tissoires 			   struct wacom_features *features)
228471d1714SBenjamin Tissoires {
229471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
230471d1714SBenjamin Tissoires 	struct hid_report *hreport;
231471d1714SBenjamin Tissoires 	int i, j;
232471d1714SBenjamin Tissoires 
233471d1714SBenjamin Tissoires 	/* check features first */
234471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
235471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
236471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
237471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
238471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
239471d1714SBenjamin Tissoires 				continue;
240471d1714SBenjamin Tissoires 
241471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
242471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
243471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
244471d1714SBenjamin Tissoires 			}
245471d1714SBenjamin Tissoires 		}
246471d1714SBenjamin Tissoires 	}
247471d1714SBenjamin Tissoires 
248471d1714SBenjamin Tissoires 	/* now check the input usages */
249471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
250471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
251471d1714SBenjamin Tissoires 
252471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
253471d1714SBenjamin Tissoires 			continue;
254471d1714SBenjamin Tissoires 
255471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
256471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
257471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
258471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
259471d1714SBenjamin Tissoires 	}
260471d1714SBenjamin Tissoires }
261471d1714SBenjamin Tissoires 
2625ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev)
2635ae6e89fSBenjamin Tissoires {
2645ae6e89fSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2655ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
2665ae6e89fSBenjamin Tissoires 	struct hid_report *r;
2675ae6e89fSBenjamin Tissoires 	struct hid_report_enum *re;
2685ae6e89fSBenjamin Tissoires 
2695ae6e89fSBenjamin Tissoires 	if (hid_data->inputmode < 0)
2705ae6e89fSBenjamin Tissoires 		return 0;
2715ae6e89fSBenjamin Tissoires 
2725ae6e89fSBenjamin Tissoires 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
2735ae6e89fSBenjamin Tissoires 	r = re->report_id_hash[hid_data->inputmode];
2745ae6e89fSBenjamin Tissoires 	if (r) {
2755ae6e89fSBenjamin Tissoires 		r->field[0]->value[hid_data->inputmode_index] = 2;
2765ae6e89fSBenjamin Tissoires 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
2775ae6e89fSBenjamin Tissoires 	}
2785ae6e89fSBenjamin Tissoires 	return 0;
2795ae6e89fSBenjamin Tissoires }
2805ae6e89fSBenjamin Tissoires 
281471d1714SBenjamin Tissoires static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
282471d1714SBenjamin Tissoires 		int length, int mode)
283471d1714SBenjamin Tissoires {
284471d1714SBenjamin Tissoires 	unsigned char *rep_data;
285471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
286471d1714SBenjamin Tissoires 
287471d1714SBenjamin Tissoires 	rep_data = kzalloc(length, GFP_KERNEL);
288471d1714SBenjamin Tissoires 	if (!rep_data)
289471d1714SBenjamin Tissoires 		return error;
290471d1714SBenjamin Tissoires 
291471d1714SBenjamin Tissoires 	do {
292471d1714SBenjamin Tissoires 		rep_data[0] = report_id;
293471d1714SBenjamin Tissoires 		rep_data[1] = mode;
294471d1714SBenjamin Tissoires 
295296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
296296b7378SPrzemo Firszt 					 length, 1);
297471d1714SBenjamin Tissoires 		if (error >= 0)
298471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
299c64d8834SPing Cheng 			                         rep_data, length, 1);
300471d1714SBenjamin Tissoires 	} while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
301471d1714SBenjamin Tissoires 
302471d1714SBenjamin Tissoires 	kfree(rep_data);
303471d1714SBenjamin Tissoires 
304471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
305471d1714SBenjamin Tissoires }
306471d1714SBenjamin Tissoires 
307f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
308f81a1295SBenjamin Tissoires 		struct wacom_features *features)
309f81a1295SBenjamin Tissoires {
310387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
311387142bbSBenjamin Tissoires 	int ret;
312387142bbSBenjamin Tissoires 	u8 rep_data[2];
313387142bbSBenjamin Tissoires 
314387142bbSBenjamin Tissoires 	switch (features->type) {
315387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
316387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
317387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
318296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
319296b7378SPrzemo Firszt 					3);
320387142bbSBenjamin Tissoires 
321387142bbSBenjamin Tissoires 		if (ret >= 0) {
322387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
323387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
324387142bbSBenjamin Tissoires 
325387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
326296b7378SPrzemo Firszt 						rep_data, 2, 3);
327387142bbSBenjamin Tissoires 
328387142bbSBenjamin Tissoires 			if (ret >= 0) {
329387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
330387142bbSBenjamin Tissoires 				return 0;
331387142bbSBenjamin Tissoires 			}
332387142bbSBenjamin Tissoires 		}
333387142bbSBenjamin Tissoires 
334387142bbSBenjamin Tissoires 		/*
335387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
336387142bbSBenjamin Tissoires 		 * and it is safe to continue
337387142bbSBenjamin Tissoires 		 */
338387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
339387142bbSBenjamin Tissoires 			 rep_data[0], ret);
340387142bbSBenjamin Tissoires 		break;
34181af7e61SBenjamin Tissoires 	case INTUOS4WL:
34281af7e61SBenjamin Tissoires 		if (speed == 1)
34381af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
34481af7e61SBenjamin Tissoires 		else
34581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
34681af7e61SBenjamin Tissoires 
34781af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
34881af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
34981af7e61SBenjamin Tissoires 
350296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
351296b7378SPrzemo Firszt 					1);
35281af7e61SBenjamin Tissoires 		if (ret >= 0)
35381af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
35481af7e61SBenjamin Tissoires 		break;
355387142bbSBenjamin Tissoires 	}
356387142bbSBenjamin Tissoires 
357f81a1295SBenjamin Tissoires 	return 0;
358f81a1295SBenjamin Tissoires }
359f81a1295SBenjamin Tissoires 
360471d1714SBenjamin Tissoires /*
361471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
362471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
363471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
364471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
365471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
366471d1714SBenjamin Tissoires  */
367471d1714SBenjamin Tissoires static int wacom_query_tablet_data(struct hid_device *hdev,
368471d1714SBenjamin Tissoires 		struct wacom_features *features)
369471d1714SBenjamin Tissoires {
370f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
371f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
372f81a1295SBenjamin Tissoires 
3735ae6e89fSBenjamin Tissoires 	if (features->type == HID_GENERIC)
3745ae6e89fSBenjamin Tissoires 		return wacom_hid_set_device_mode(hdev);
3755ae6e89fSBenjamin Tissoires 
376471d1714SBenjamin Tissoires 	if (features->device_type == BTN_TOOL_FINGER) {
377471d1714SBenjamin Tissoires 		if (features->type > TABLETPC) {
378471d1714SBenjamin Tissoires 			/* MT Tablet PC touch */
379471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 3, 4, 4);
380471d1714SBenjamin Tissoires 		}
381471d1714SBenjamin Tissoires 		else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
382471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 18, 3, 2);
383471d1714SBenjamin Tissoires 		}
384471d1714SBenjamin Tissoires 	} else if (features->device_type == BTN_TOOL_PEN) {
385471d1714SBenjamin Tissoires 		if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
386471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 2, 2, 2);
387471d1714SBenjamin Tissoires 		}
388471d1714SBenjamin Tissoires 	}
389471d1714SBenjamin Tissoires 
390471d1714SBenjamin Tissoires 	return 0;
391471d1714SBenjamin Tissoires }
392471d1714SBenjamin Tissoires 
393471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
394471d1714SBenjamin Tissoires 					 struct wacom_features *features)
395471d1714SBenjamin Tissoires {
396471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
397471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
398471d1714SBenjamin Tissoires 
399471d1714SBenjamin Tissoires 	/* default features */
400471d1714SBenjamin Tissoires 	features->device_type = BTN_TOOL_PEN;
401471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
402471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
403471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
404471d1714SBenjamin Tissoires 	features->distance_fuzz = 0;
405471d1714SBenjamin Tissoires 
406471d1714SBenjamin Tissoires 	/*
407471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
408471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
409471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
410471d1714SBenjamin Tissoires 	 * interface number.
411471d1714SBenjamin Tissoires 	 */
412471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
413471d1714SBenjamin Tissoires 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
414471d1714SBenjamin Tissoires 			features->device_type = 0;
415471d1714SBenjamin Tissoires 		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
416471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
417471d1714SBenjamin Tissoires 			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
418471d1714SBenjamin Tissoires 		}
419471d1714SBenjamin Tissoires 	}
420471d1714SBenjamin Tissoires 
421471d1714SBenjamin Tissoires 	/* only devices that support touch need to retrieve the info */
422471d1714SBenjamin Tissoires 	if (features->type < BAMBOO_PT)
423471d1714SBenjamin Tissoires 		return;
424471d1714SBenjamin Tissoires 
425471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
426471d1714SBenjamin Tissoires }
427471d1714SBenjamin Tissoires 
428471d1714SBenjamin Tissoires struct wacom_hdev_data {
429471d1714SBenjamin Tissoires 	struct list_head list;
430471d1714SBenjamin Tissoires 	struct kref kref;
431471d1714SBenjamin Tissoires 	struct hid_device *dev;
432471d1714SBenjamin Tissoires 	struct wacom_shared shared;
433471d1714SBenjamin Tissoires };
434471d1714SBenjamin Tissoires 
435471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
436471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
437471d1714SBenjamin Tissoires 
438471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
439471d1714SBenjamin Tissoires 		struct hid_device *sibling)
440471d1714SBenjamin Tissoires {
441471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
442471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
443471d1714SBenjamin Tissoires 	int vid = features->oVid;
444471d1714SBenjamin Tissoires 	int pid = features->oPid;
445471d1714SBenjamin Tissoires 	int n1,n2;
446471d1714SBenjamin Tissoires 
447471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
448471d1714SBenjamin Tissoires 		vid = hdev->vendor;
449471d1714SBenjamin Tissoires 		pid = hdev->product;
450471d1714SBenjamin Tissoires 	}
451471d1714SBenjamin Tissoires 
452471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
453471d1714SBenjamin Tissoires 		return false;
454471d1714SBenjamin Tissoires 
455471d1714SBenjamin Tissoires 	/* Compare the physical path. */
456471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
457471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
458471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
459471d1714SBenjamin Tissoires 		return false;
460471d1714SBenjamin Tissoires 
461471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
462471d1714SBenjamin Tissoires }
463471d1714SBenjamin Tissoires 
464471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
465471d1714SBenjamin Tissoires {
466471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
467471d1714SBenjamin Tissoires 
468471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
469471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
470471d1714SBenjamin Tissoires 			kref_get(&data->kref);
471471d1714SBenjamin Tissoires 			return data;
472471d1714SBenjamin Tissoires 		}
473471d1714SBenjamin Tissoires 	}
474471d1714SBenjamin Tissoires 
475471d1714SBenjamin Tissoires 	return NULL;
476471d1714SBenjamin Tissoires }
477471d1714SBenjamin Tissoires 
478471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
479471d1714SBenjamin Tissoires {
480471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
481471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
482471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
483471d1714SBenjamin Tissoires 	int retval = 0;
484471d1714SBenjamin Tissoires 
485471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
486471d1714SBenjamin Tissoires 
487471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
488471d1714SBenjamin Tissoires 	if (!data) {
489471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
490471d1714SBenjamin Tissoires 		if (!data) {
491471d1714SBenjamin Tissoires 			retval = -ENOMEM;
492471d1714SBenjamin Tissoires 			goto out;
493471d1714SBenjamin Tissoires 		}
494471d1714SBenjamin Tissoires 
495471d1714SBenjamin Tissoires 		kref_init(&data->kref);
496471d1714SBenjamin Tissoires 		data->dev = hdev;
497471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
498471d1714SBenjamin Tissoires 	}
499471d1714SBenjamin Tissoires 
500471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
501471d1714SBenjamin Tissoires 
502471d1714SBenjamin Tissoires out:
503471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
504471d1714SBenjamin Tissoires 	return retval;
505471d1714SBenjamin Tissoires }
506471d1714SBenjamin Tissoires 
507471d1714SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
508471d1714SBenjamin Tissoires {
509471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data =
510471d1714SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
511471d1714SBenjamin Tissoires 
512471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
513471d1714SBenjamin Tissoires 	list_del(&data->list);
514471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
515471d1714SBenjamin Tissoires 
516471d1714SBenjamin Tissoires 	kfree(data);
517471d1714SBenjamin Tissoires }
518471d1714SBenjamin Tissoires 
519471d1714SBenjamin Tissoires static void wacom_remove_shared_data(struct wacom_wac *wacom)
520471d1714SBenjamin Tissoires {
521471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
522471d1714SBenjamin Tissoires 
523471d1714SBenjamin Tissoires 	if (wacom->shared) {
524471d1714SBenjamin Tissoires 		data = container_of(wacom->shared, struct wacom_hdev_data, shared);
525471d1714SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
526471d1714SBenjamin Tissoires 		wacom->shared = NULL;
527471d1714SBenjamin Tissoires 	}
528471d1714SBenjamin Tissoires }
529471d1714SBenjamin Tissoires 
530471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
531471d1714SBenjamin Tissoires {
532471d1714SBenjamin Tissoires 	unsigned char *buf;
533471d1714SBenjamin Tissoires 	int retval;
534912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
535912ca216SPing Cheng 	int buf_size = 9;
536471d1714SBenjamin Tissoires 
537912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
538912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
539912ca216SPing Cheng 		buf_size = 13;
540912ca216SPing Cheng 	}
541912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
542471d1714SBenjamin Tissoires 	if (!buf)
543471d1714SBenjamin Tissoires 		return -ENOMEM;
544471d1714SBenjamin Tissoires 
545471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
546471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
547471d1714SBenjamin Tissoires 		/*
548471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
549471d1714SBenjamin Tissoires 		 * one of four values:
550471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
551471d1714SBenjamin Tissoires 		 */
552471d1714SBenjamin Tissoires 		int ring_led = wacom->led.select[0] & 0x03;
553471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
554471d1714SBenjamin Tissoires 		int crop_lum = 0;
555912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
556471d1714SBenjamin Tissoires 
557912ca216SPing Cheng 		buf[0] = report_id;
558912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
559912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
560912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
561912ca216SPing Cheng 			buf[0] = report_id;
562912ca216SPing Cheng 			buf[4] = led_bits;
563912ca216SPing Cheng 		} else
564912ca216SPing Cheng 			buf[1] = led_bits;
565471d1714SBenjamin Tissoires 	}
566471d1714SBenjamin Tissoires 	else {
567471d1714SBenjamin Tissoires 		int led = wacom->led.select[0] | 0x4;
568471d1714SBenjamin Tissoires 
569471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
570471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
571471d1714SBenjamin Tissoires 			led |= (wacom->led.select[1] << 4) | 0x40;
572471d1714SBenjamin Tissoires 
573912ca216SPing Cheng 		buf[0] = report_id;
574471d1714SBenjamin Tissoires 		buf[1] = led;
575471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
576471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
577471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
578471d1714SBenjamin Tissoires 	}
579471d1714SBenjamin Tissoires 
580912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
581296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
582471d1714SBenjamin Tissoires 	kfree(buf);
583471d1714SBenjamin Tissoires 
584471d1714SBenjamin Tissoires 	return retval;
585471d1714SBenjamin Tissoires }
586471d1714SBenjamin Tissoires 
587849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
588849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
589471d1714SBenjamin Tissoires {
590471d1714SBenjamin Tissoires 	unsigned char *buf;
591471d1714SBenjamin Tissoires 	int i, retval;
592849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
593471d1714SBenjamin Tissoires 
594849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
595471d1714SBenjamin Tissoires 	if (!buf)
596471d1714SBenjamin Tissoires 		return -ENOMEM;
597471d1714SBenjamin Tissoires 
598471d1714SBenjamin Tissoires 	/* Send 'start' command */
599471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
600471d1714SBenjamin Tissoires 	buf[1] = 1;
601296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
602296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
603471d1714SBenjamin Tissoires 	if (retval < 0)
604471d1714SBenjamin Tissoires 		goto out;
605471d1714SBenjamin Tissoires 
606849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
607471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
608471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
609471d1714SBenjamin Tissoires 		buf[2] = i;
610849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
611471d1714SBenjamin Tissoires 
612471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
613296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
614471d1714SBenjamin Tissoires 		if (retval < 0)
615471d1714SBenjamin Tissoires 			break;
616471d1714SBenjamin Tissoires 	}
617471d1714SBenjamin Tissoires 
618471d1714SBenjamin Tissoires 	/* Send 'stop' */
619471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
620471d1714SBenjamin Tissoires 	buf[1] = 0;
621296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
622296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
623471d1714SBenjamin Tissoires 
624471d1714SBenjamin Tissoires out:
625471d1714SBenjamin Tissoires 	kfree(buf);
626471d1714SBenjamin Tissoires 	return retval;
627471d1714SBenjamin Tissoires }
628471d1714SBenjamin Tissoires 
629471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
630471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
631471d1714SBenjamin Tissoires {
632471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
633471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
634471d1714SBenjamin Tissoires 	unsigned int id;
635471d1714SBenjamin Tissoires 	int err;
636471d1714SBenjamin Tissoires 
637471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
638471d1714SBenjamin Tissoires 	if (err)
639471d1714SBenjamin Tissoires 		return err;
640471d1714SBenjamin Tissoires 
641471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
642471d1714SBenjamin Tissoires 
643471d1714SBenjamin Tissoires 	wacom->led.select[set_id] = id & 0x3;
644471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
645471d1714SBenjamin Tissoires 
646471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
647471d1714SBenjamin Tissoires 
648471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
649471d1714SBenjamin Tissoires }
650471d1714SBenjamin Tissoires 
651471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
652471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
653471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
654471d1714SBenjamin Tissoires {									\
655471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
656471d1714SBenjamin Tissoires }									\
657471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
658471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
659471d1714SBenjamin Tissoires {									\
660471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
661471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
66237449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
66337449adcSPing Cheng 			 wacom->led.select[SET_ID]);			\
664471d1714SBenjamin Tissoires }									\
665e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
666471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
667471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
668471d1714SBenjamin Tissoires 
669471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
670471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
671471d1714SBenjamin Tissoires 
672471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
673471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
674471d1714SBenjamin Tissoires {
675471d1714SBenjamin Tissoires 	unsigned int value;
676471d1714SBenjamin Tissoires 	int err;
677471d1714SBenjamin Tissoires 
678471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
679471d1714SBenjamin Tissoires 	if (err)
680471d1714SBenjamin Tissoires 		return err;
681471d1714SBenjamin Tissoires 
682471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
683471d1714SBenjamin Tissoires 
684471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
685471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
686471d1714SBenjamin Tissoires 
687471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
688471d1714SBenjamin Tissoires 
689471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
690471d1714SBenjamin Tissoires }
691471d1714SBenjamin Tissoires 
692471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
693471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
694471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
695471d1714SBenjamin Tissoires {									\
696471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
697471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
698471d1714SBenjamin Tissoires 									\
699471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
700471d1714SBenjamin Tissoires 				     buf, count);			\
701471d1714SBenjamin Tissoires }									\
70237449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
70337449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
70437449adcSPing Cheng {									\
70537449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
70637449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
70737449adcSPing Cheng }									\
708e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
70937449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
71037449adcSPing Cheng 		   wacom_##name##_luminance_store)
711471d1714SBenjamin Tissoires 
712471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
713471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
714471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
715471d1714SBenjamin Tissoires 
716471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
717471d1714SBenjamin Tissoires 					const char *buf, size_t count)
718471d1714SBenjamin Tissoires {
719471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
720471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
721471d1714SBenjamin Tissoires 	int err;
722849e2f06SBenjamin Tissoires 	unsigned len;
723849e2f06SBenjamin Tissoires 	u8 xfer_id;
724471d1714SBenjamin Tissoires 
725849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
726849e2f06SBenjamin Tissoires 		len = 256;
727849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
728849e2f06SBenjamin Tissoires 	} else {
729849e2f06SBenjamin Tissoires 		len = 1024;
730849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
731849e2f06SBenjamin Tissoires 	}
732849e2f06SBenjamin Tissoires 
733849e2f06SBenjamin Tissoires 	if (count != len)
734471d1714SBenjamin Tissoires 		return -EINVAL;
735471d1714SBenjamin Tissoires 
736471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
737471d1714SBenjamin Tissoires 
738849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
739471d1714SBenjamin Tissoires 
740471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
741471d1714SBenjamin Tissoires 
742471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
743471d1714SBenjamin Tissoires }
744471d1714SBenjamin Tissoires 
745471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
746471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
747471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
748471d1714SBenjamin Tissoires {									\
749471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
750471d1714SBenjamin Tissoires }									\
751e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
752471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
753471d1714SBenjamin Tissoires 
754471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
755471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
756471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
757471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
758471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
759471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
760471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
761471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
762471d1714SBenjamin Tissoires 
763471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
764471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
765471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
766471d1714SBenjamin Tissoires 	NULL
767471d1714SBenjamin Tissoires };
768471d1714SBenjamin Tissoires 
769471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
770471d1714SBenjamin Tissoires 	.name = "wacom_led",
771471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
772471d1714SBenjamin Tissoires };
773471d1714SBenjamin Tissoires 
774471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
775471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
776471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
777471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
778471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
779471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
780471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
781471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
782471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
783471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
784471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
785471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
786471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
787471d1714SBenjamin Tissoires 	NULL
788471d1714SBenjamin Tissoires };
789471d1714SBenjamin Tissoires 
790471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
791471d1714SBenjamin Tissoires 	.name = "wacom_led",
792471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
793471d1714SBenjamin Tissoires };
794471d1714SBenjamin Tissoires 
795471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
796471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
797471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
798471d1714SBenjamin Tissoires 	NULL
799471d1714SBenjamin Tissoires };
800471d1714SBenjamin Tissoires 
801471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
802471d1714SBenjamin Tissoires 	.name = "wacom_led",
803471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
804471d1714SBenjamin Tissoires };
805471d1714SBenjamin Tissoires 
806471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
807471d1714SBenjamin Tissoires {
808471d1714SBenjamin Tissoires 	int error;
809471d1714SBenjamin Tissoires 
810471d1714SBenjamin Tissoires 	/* Initialize default values */
811471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
812471d1714SBenjamin Tissoires 	case INTUOS4S:
813471d1714SBenjamin Tissoires 	case INTUOS4:
81481af7e61SBenjamin Tissoires 	case INTUOS4WL:
815471d1714SBenjamin Tissoires 	case INTUOS4L:
816471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
817471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
818471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
819471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
820471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
821471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
822471d1714SBenjamin Tissoires 					   &intuos4_led_attr_group);
823471d1714SBenjamin Tissoires 		break;
824471d1714SBenjamin Tissoires 
825471d1714SBenjamin Tissoires 	case WACOM_24HD:
826471d1714SBenjamin Tissoires 	case WACOM_21UX2:
827471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
828471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
829471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
830471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
831471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
832471d1714SBenjamin Tissoires 
833471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
834471d1714SBenjamin Tissoires 					   &cintiq_led_attr_group);
835471d1714SBenjamin Tissoires 		break;
836471d1714SBenjamin Tissoires 
837471d1714SBenjamin Tissoires 	case INTUOS5S:
838471d1714SBenjamin Tissoires 	case INTUOS5:
839471d1714SBenjamin Tissoires 	case INTUOS5L:
840471d1714SBenjamin Tissoires 	case INTUOSPS:
841471d1714SBenjamin Tissoires 	case INTUOSPM:
842471d1714SBenjamin Tissoires 	case INTUOSPL:
843471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
844471d1714SBenjamin Tissoires 			wacom->led.select[0] = 0;
845471d1714SBenjamin Tissoires 			wacom->led.select[1] = 0;
846471d1714SBenjamin Tissoires 			wacom->led.llv = 32;
847471d1714SBenjamin Tissoires 			wacom->led.hlv = 0;
848471d1714SBenjamin Tissoires 			wacom->led.img_lum = 0;
849471d1714SBenjamin Tissoires 
850471d1714SBenjamin Tissoires 			error = sysfs_create_group(&wacom->hdev->dev.kobj,
851471d1714SBenjamin Tissoires 						  &intuos5_led_attr_group);
852471d1714SBenjamin Tissoires 		} else
853471d1714SBenjamin Tissoires 			return 0;
854471d1714SBenjamin Tissoires 		break;
855471d1714SBenjamin Tissoires 
856471d1714SBenjamin Tissoires 	default:
857471d1714SBenjamin Tissoires 		return 0;
858471d1714SBenjamin Tissoires 	}
859471d1714SBenjamin Tissoires 
860471d1714SBenjamin Tissoires 	if (error) {
861471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
862471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
863471d1714SBenjamin Tissoires 		return error;
864471d1714SBenjamin Tissoires 	}
865471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
866c757cbafSBenjamin Tissoires 	wacom->led_initialized = true;
867471d1714SBenjamin Tissoires 
868471d1714SBenjamin Tissoires 	return 0;
869471d1714SBenjamin Tissoires }
870471d1714SBenjamin Tissoires 
871471d1714SBenjamin Tissoires static void wacom_destroy_leds(struct wacom *wacom)
872471d1714SBenjamin Tissoires {
873c757cbafSBenjamin Tissoires 	if (!wacom->led_initialized)
874c757cbafSBenjamin Tissoires 		return;
875c757cbafSBenjamin Tissoires 
876c757cbafSBenjamin Tissoires 	wacom->led_initialized = false;
877c757cbafSBenjamin Tissoires 
878471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
879471d1714SBenjamin Tissoires 	case INTUOS4S:
880471d1714SBenjamin Tissoires 	case INTUOS4:
88181af7e61SBenjamin Tissoires 	case INTUOS4WL:
882471d1714SBenjamin Tissoires 	case INTUOS4L:
883471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
884471d1714SBenjamin Tissoires 				   &intuos4_led_attr_group);
885471d1714SBenjamin Tissoires 		break;
886471d1714SBenjamin Tissoires 
887471d1714SBenjamin Tissoires 	case WACOM_24HD:
888471d1714SBenjamin Tissoires 	case WACOM_21UX2:
889471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
890471d1714SBenjamin Tissoires 				   &cintiq_led_attr_group);
891471d1714SBenjamin Tissoires 		break;
892471d1714SBenjamin Tissoires 
893471d1714SBenjamin Tissoires 	case INTUOS5S:
894471d1714SBenjamin Tissoires 	case INTUOS5:
895471d1714SBenjamin Tissoires 	case INTUOS5L:
896471d1714SBenjamin Tissoires 	case INTUOSPS:
897471d1714SBenjamin Tissoires 	case INTUOSPM:
898471d1714SBenjamin Tissoires 	case INTUOSPL:
899471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
900471d1714SBenjamin Tissoires 			sysfs_remove_group(&wacom->hdev->dev.kobj,
901471d1714SBenjamin Tissoires 					   &intuos5_led_attr_group);
902471d1714SBenjamin Tissoires 		break;
903471d1714SBenjamin Tissoires 	}
904471d1714SBenjamin Tissoires }
905471d1714SBenjamin Tissoires 
906471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
907ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
908471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
909471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
910471d1714SBenjamin Tissoires };
911471d1714SBenjamin Tissoires 
9127dbd229eSBenjamin Tissoires static enum power_supply_property wacom_ac_props[] = {
9137dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_PRESENT,
9147dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_ONLINE,
9157dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
9167dbd229eSBenjamin Tissoires };
9177dbd229eSBenjamin Tissoires 
918471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
919471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
920471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
921471d1714SBenjamin Tissoires {
922471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(psy, struct wacom, battery);
923471d1714SBenjamin Tissoires 	int ret = 0;
924471d1714SBenjamin Tissoires 
925471d1714SBenjamin Tissoires 	switch (psp) {
926471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
927471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
928471d1714SBenjamin Tissoires 			break;
929471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
930471d1714SBenjamin Tissoires 			val->intval =
931ac8d1010SBenjamin Tissoires 				wacom->wacom_wac.battery_capacity;
932ac8d1010SBenjamin Tissoires 			break;
933ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
934ac8d1010SBenjamin Tissoires 			if (wacom->wacom_wac.bat_charging)
935ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
936ac8d1010SBenjamin Tissoires 			else if (wacom->wacom_wac.battery_capacity == 100 &&
937ac8d1010SBenjamin Tissoires 				    wacom->wacom_wac.ps_connected)
938ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
939ac8d1010SBenjamin Tissoires 			else
940ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
941471d1714SBenjamin Tissoires 			break;
942471d1714SBenjamin Tissoires 		default:
943471d1714SBenjamin Tissoires 			ret = -EINVAL;
944471d1714SBenjamin Tissoires 			break;
945471d1714SBenjamin Tissoires 	}
946471d1714SBenjamin Tissoires 
947471d1714SBenjamin Tissoires 	return ret;
948471d1714SBenjamin Tissoires }
949471d1714SBenjamin Tissoires 
9507dbd229eSBenjamin Tissoires static int wacom_ac_get_property(struct power_supply *psy,
9517dbd229eSBenjamin Tissoires 				enum power_supply_property psp,
9527dbd229eSBenjamin Tissoires 				union power_supply_propval *val)
9537dbd229eSBenjamin Tissoires {
9547dbd229eSBenjamin Tissoires 	struct wacom *wacom = container_of(psy, struct wacom, ac);
9557dbd229eSBenjamin Tissoires 	int ret = 0;
9567dbd229eSBenjamin Tissoires 
9577dbd229eSBenjamin Tissoires 	switch (psp) {
9587dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_PRESENT:
9597dbd229eSBenjamin Tissoires 		/* fall through */
9607dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_ONLINE:
9617dbd229eSBenjamin Tissoires 		val->intval = wacom->wacom_wac.ps_connected;
9627dbd229eSBenjamin Tissoires 		break;
9637dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_SCOPE:
9647dbd229eSBenjamin Tissoires 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
9657dbd229eSBenjamin Tissoires 		break;
9667dbd229eSBenjamin Tissoires 	default:
9677dbd229eSBenjamin Tissoires 		ret = -EINVAL;
9687dbd229eSBenjamin Tissoires 		break;
9697dbd229eSBenjamin Tissoires 	}
9707dbd229eSBenjamin Tissoires 	return ret;
9717dbd229eSBenjamin Tissoires }
9727dbd229eSBenjamin Tissoires 
973471d1714SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
974471d1714SBenjamin Tissoires {
975d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
9767dbd229eSBenjamin Tissoires 	int error;
977d70420b9SBenjamin Tissoires 	unsigned long n;
978471d1714SBenjamin Tissoires 
979ac8d1010SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
980d70420b9SBenjamin Tissoires 		n = atomic_inc_return(&battery_no) - 1;
9817dbd229eSBenjamin Tissoires 
982471d1714SBenjamin Tissoires 		wacom->battery.properties = wacom_battery_props;
983471d1714SBenjamin Tissoires 		wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
984471d1714SBenjamin Tissoires 		wacom->battery.get_property = wacom_battery_get_property;
985d70420b9SBenjamin Tissoires 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
986d70420b9SBenjamin Tissoires 		wacom->battery.name = wacom->wacom_wac.bat_name;
987471d1714SBenjamin Tissoires 		wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
988471d1714SBenjamin Tissoires 		wacom->battery.use_for_apm = 0;
989471d1714SBenjamin Tissoires 
9907dbd229eSBenjamin Tissoires 		wacom->ac.properties = wacom_ac_props;
9917dbd229eSBenjamin Tissoires 		wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
9927dbd229eSBenjamin Tissoires 		wacom->ac.get_property = wacom_ac_get_property;
9937dbd229eSBenjamin Tissoires 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
9947dbd229eSBenjamin Tissoires 		wacom->ac.name = wacom->wacom_wac.ac_name;
9957dbd229eSBenjamin Tissoires 		wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
9967dbd229eSBenjamin Tissoires 		wacom->ac.use_for_apm = 0;
9977dbd229eSBenjamin Tissoires 
998471d1714SBenjamin Tissoires 		error = power_supply_register(&wacom->hdev->dev,
999471d1714SBenjamin Tissoires 					      &wacom->battery);
10007dbd229eSBenjamin Tissoires 		if (error)
10017dbd229eSBenjamin Tissoires 			return error;
1002471d1714SBenjamin Tissoires 
10037dbd229eSBenjamin Tissoires 		power_supply_powers(&wacom->battery, &wacom->hdev->dev);
10047dbd229eSBenjamin Tissoires 
10057dbd229eSBenjamin Tissoires 		error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
10067dbd229eSBenjamin Tissoires 		if (error) {
10077dbd229eSBenjamin Tissoires 			power_supply_unregister(&wacom->battery);
10087dbd229eSBenjamin Tissoires 			return error;
1009471d1714SBenjamin Tissoires 		}
1010471d1714SBenjamin Tissoires 
10117dbd229eSBenjamin Tissoires 		power_supply_powers(&wacom->ac, &wacom->hdev->dev);
10127dbd229eSBenjamin Tissoires 	}
10137dbd229eSBenjamin Tissoires 
10147dbd229eSBenjamin Tissoires 	return 0;
1015471d1714SBenjamin Tissoires }
1016471d1714SBenjamin Tissoires 
1017471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1018471d1714SBenjamin Tissoires {
1019ac8d1010SBenjamin Tissoires 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1020471d1714SBenjamin Tissoires 	     wacom->battery.dev) {
1021471d1714SBenjamin Tissoires 		power_supply_unregister(&wacom->battery);
1022471d1714SBenjamin Tissoires 		wacom->battery.dev = NULL;
10237dbd229eSBenjamin Tissoires 		power_supply_unregister(&wacom->ac);
10247dbd229eSBenjamin Tissoires 		wacom->ac.dev = NULL;
1025471d1714SBenjamin Tissoires 	}
1026471d1714SBenjamin Tissoires }
1027471d1714SBenjamin Tissoires 
1028f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1029f81a1295SBenjamin Tissoires 				struct device_attribute
1030f81a1295SBenjamin Tissoires 				*attr, char *buf)
1031f81a1295SBenjamin Tissoires {
1032f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1033f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1034f81a1295SBenjamin Tissoires 
1035f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1036f81a1295SBenjamin Tissoires }
1037f81a1295SBenjamin Tissoires 
1038f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1039f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1040f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1041f81a1295SBenjamin Tissoires {
1042f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1043f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1044f81a1295SBenjamin Tissoires 	u8 new_speed;
1045f81a1295SBenjamin Tissoires 
1046f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1047f81a1295SBenjamin Tissoires 		return -EINVAL;
1048f81a1295SBenjamin Tissoires 
1049f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1050f81a1295SBenjamin Tissoires 		return -EINVAL;
1051f81a1295SBenjamin Tissoires 
1052f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1053f81a1295SBenjamin Tissoires 
1054f81a1295SBenjamin Tissoires 	return count;
1055f81a1295SBenjamin Tissoires }
1056f81a1295SBenjamin Tissoires 
1057e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1058f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1059f81a1295SBenjamin Tissoires 
1060471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1061471d1714SBenjamin Tissoires {
1062471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1063471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1064471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1065471d1714SBenjamin Tissoires 
1066471d1714SBenjamin Tissoires 	input_dev = input_allocate_device();
1067471d1714SBenjamin Tissoires 	if (!input_dev)
1068471d1714SBenjamin Tissoires 		return NULL;
1069471d1714SBenjamin Tissoires 
1070471d1714SBenjamin Tissoires 	input_dev->name = wacom_wac->name;
1071471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1072471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1073471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1074471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1075471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1076471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1077471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
107812969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1079471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1080471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1081471d1714SBenjamin Tissoires 
1082471d1714SBenjamin Tissoires 	return input_dev;
1083471d1714SBenjamin Tissoires }
1084471d1714SBenjamin Tissoires 
10852546dacdSBenjamin Tissoires static void wacom_free_inputs(struct wacom *wacom)
1086471d1714SBenjamin Tissoires {
10872546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
10882546dacdSBenjamin Tissoires 
10892546dacdSBenjamin Tissoires 	if (wacom_wac->input)
10902546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->input);
10912546dacdSBenjamin Tissoires 	if (wacom_wac->pad_input)
10922546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->pad_input);
10932546dacdSBenjamin Tissoires 	wacom_wac->input = NULL;
10942546dacdSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
10952546dacdSBenjamin Tissoires }
10962546dacdSBenjamin Tissoires 
10972546dacdSBenjamin Tissoires static int wacom_allocate_inputs(struct wacom *wacom)
10982546dacdSBenjamin Tissoires {
10992546dacdSBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
11002546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11012546dacdSBenjamin Tissoires 
11022546dacdSBenjamin Tissoires 	input_dev = wacom_allocate_input(wacom);
11032546dacdSBenjamin Tissoires 	pad_input_dev = wacom_allocate_input(wacom);
11042546dacdSBenjamin Tissoires 	if (!input_dev || !pad_input_dev) {
11052546dacdSBenjamin Tissoires 		wacom_free_inputs(wacom);
11062546dacdSBenjamin Tissoires 		return -ENOMEM;
11072546dacdSBenjamin Tissoires 	}
11082546dacdSBenjamin Tissoires 
11092546dacdSBenjamin Tissoires 	wacom_wac->input = input_dev;
11102546dacdSBenjamin Tissoires 	wacom_wac->pad_input = pad_input_dev;
11112546dacdSBenjamin Tissoires 	wacom_wac->pad_input->name = wacom_wac->pad_name;
11122546dacdSBenjamin Tissoires 
11132546dacdSBenjamin Tissoires 	return 0;
11142546dacdSBenjamin Tissoires }
11152546dacdSBenjamin Tissoires 
11162546dacdSBenjamin Tissoires static void wacom_clean_inputs(struct wacom *wacom)
11172546dacdSBenjamin Tissoires {
11182546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.input) {
11192546dacdSBenjamin Tissoires 		if (wacom->wacom_wac.input_registered)
1120471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.input);
11212546dacdSBenjamin Tissoires 		else
11222546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.input);
11232546dacdSBenjamin Tissoires 	}
11242546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.pad_input) {
11252546dacdSBenjamin Tissoires 		if (wacom->wacom_wac.input_registered)
1126471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.pad_input);
11272546dacdSBenjamin Tissoires 		else
11282546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.pad_input);
11292546dacdSBenjamin Tissoires 	}
1130471d1714SBenjamin Tissoires 	wacom->wacom_wac.input = NULL;
1131471d1714SBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
1132912ca216SPing Cheng 	wacom_destroy_leds(wacom);
1133471d1714SBenjamin Tissoires }
1134471d1714SBenjamin Tissoires 
1135471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1136471d1714SBenjamin Tissoires {
1137471d1714SBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
1138471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1139471d1714SBenjamin Tissoires 	int error;
1140471d1714SBenjamin Tissoires 
11412546dacdSBenjamin Tissoires 	input_dev = wacom_wac->input;
11422546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1143471d1714SBenjamin Tissoires 
11442546dacdSBenjamin Tissoires 	if (!input_dev || !pad_input_dev)
11452546dacdSBenjamin Tissoires 		return -EINVAL;
1146471d1714SBenjamin Tissoires 
1147471d1714SBenjamin Tissoires 	error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1148471d1714SBenjamin Tissoires 	if (error)
11492546dacdSBenjamin Tissoires 		return error;
1150471d1714SBenjamin Tissoires 
1151471d1714SBenjamin Tissoires 	error = input_register_device(input_dev);
1152471d1714SBenjamin Tissoires 	if (error)
11532546dacdSBenjamin Tissoires 		return error;
1154471d1714SBenjamin Tissoires 
1155471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1156471d1714SBenjamin Tissoires 	if (error) {
1157471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1158471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1159471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1160471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1161471d1714SBenjamin Tissoires 	} else {
1162471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1163471d1714SBenjamin Tissoires 		if (error)
11647fefeec5SBenjamin Tissoires 			goto fail_register_pad_input;
1165912ca216SPing Cheng 
1166912ca216SPing Cheng 		error = wacom_initialize_leds(wacom);
1167912ca216SPing Cheng 		if (error)
11687fefeec5SBenjamin Tissoires 			goto fail_leds;
1169471d1714SBenjamin Tissoires 	}
1170471d1714SBenjamin Tissoires 
11712546dacdSBenjamin Tissoires 	wacom_wac->input_registered = true;
11722546dacdSBenjamin Tissoires 
1173471d1714SBenjamin Tissoires 	return 0;
1174471d1714SBenjamin Tissoires 
11757fefeec5SBenjamin Tissoires fail_leds:
1176912ca216SPing Cheng 	input_unregister_device(pad_input_dev);
1177912ca216SPing Cheng 	pad_input_dev = NULL;
11787fefeec5SBenjamin Tissoires fail_register_pad_input:
1179471d1714SBenjamin Tissoires 	input_unregister_device(input_dev);
1180471d1714SBenjamin Tissoires 	wacom_wac->input = NULL;
1181471d1714SBenjamin Tissoires 	return error;
1182471d1714SBenjamin Tissoires }
1183471d1714SBenjamin Tissoires 
1184471d1714SBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
1185471d1714SBenjamin Tissoires {
1186471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, work);
1187471d1714SBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
1188471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1189471d1714SBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
1190471d1714SBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
1191471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1192471d1714SBenjamin Tissoires 	int error;
1193471d1714SBenjamin Tissoires 
1194471d1714SBenjamin Tissoires 	/*
1195471d1714SBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
1196471d1714SBenjamin Tissoires 	 * remove any existing input and battery devices.
1197471d1714SBenjamin Tissoires 	 */
1198471d1714SBenjamin Tissoires 
1199471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1200471d1714SBenjamin Tissoires 
1201471d1714SBenjamin Tissoires 	/* Stylus interface */
1202471d1714SBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1203471d1714SBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
1204471d1714SBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
12052546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
1206471d1714SBenjamin Tissoires 
1207471d1714SBenjamin Tissoires 	/* Touch interface */
1208471d1714SBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1209471d1714SBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
1210471d1714SBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
12112546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1212471d1714SBenjamin Tissoires 
1213471d1714SBenjamin Tissoires 	if (wacom_wac->pid == 0) {
1214471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1215ac8d1010SBenjamin Tissoires 		wacom_wac1->shared->type = 0;
1216471d1714SBenjamin Tissoires 	} else {
1217471d1714SBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
1218471d1714SBenjamin Tissoires 
1219471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1220471d1714SBenjamin Tissoires 			 wacom_wac->pid);
1221471d1714SBenjamin Tissoires 
1222471d1714SBenjamin Tissoires 		while (id->bus) {
1223471d1714SBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1224471d1714SBenjamin Tissoires 			    id->product == wacom_wac->pid)
1225471d1714SBenjamin Tissoires 				break;
1226471d1714SBenjamin Tissoires 			id++;
1227471d1714SBenjamin Tissoires 		}
1228471d1714SBenjamin Tissoires 
1229471d1714SBenjamin Tissoires 		if (!id->bus) {
1230471d1714SBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1231471d1714SBenjamin Tissoires 			return;
1232471d1714SBenjamin Tissoires 		}
1233471d1714SBenjamin Tissoires 
1234471d1714SBenjamin Tissoires 		/* Stylus interface */
1235471d1714SBenjamin Tissoires 		wacom_wac1->features =
1236471d1714SBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
1237471d1714SBenjamin Tissoires 		wacom_wac1->features.device_type = BTN_TOOL_PEN;
1238471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1239471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1240471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1241471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1242471d1714SBenjamin Tissoires 		wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1243471d1714SBenjamin Tissoires 		wacom_wac1->shared->type = wacom_wac1->features.type;
1244912ca216SPing Cheng 		wacom_wac1->pid = wacom_wac->pid;
12452546dacdSBenjamin Tissoires 		error = wacom_allocate_inputs(wacom1) ||
12462546dacdSBenjamin Tissoires 			wacom_register_inputs(wacom1);
1247471d1714SBenjamin Tissoires 		if (error)
1248471d1714SBenjamin Tissoires 			goto fail;
1249471d1714SBenjamin Tissoires 
1250471d1714SBenjamin Tissoires 		/* Touch interface */
1251471d1714SBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
1252471d1714SBenjamin Tissoires 		    wacom_wac1->features.type == INTUOSHT) {
1253471d1714SBenjamin Tissoires 			wacom_wac2->features =
1254471d1714SBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
1255471d1714SBenjamin Tissoires 			wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1256471d1714SBenjamin Tissoires 			wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1257471d1714SBenjamin Tissoires 			wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1258471d1714SBenjamin Tissoires 			if (wacom_wac2->features.touch_max)
1259471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1260471d1714SBenjamin Tissoires 					 "%s (WL) Finger",wacom_wac2->features.name);
1261471d1714SBenjamin Tissoires 			else
1262471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1263471d1714SBenjamin Tissoires 					 "%s (WL) Pad",wacom_wac2->features.name);
1264471d1714SBenjamin Tissoires 			snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1265471d1714SBenjamin Tissoires 				 "%s (WL) Pad", wacom_wac2->features.name);
1266912ca216SPing Cheng 			wacom_wac2->pid = wacom_wac->pid;
12672546dacdSBenjamin Tissoires 			error = wacom_allocate_inputs(wacom2) ||
12682546dacdSBenjamin Tissoires 				wacom_register_inputs(wacom2);
1269471d1714SBenjamin Tissoires 			if (error)
1270471d1714SBenjamin Tissoires 				goto fail;
1271471d1714SBenjamin Tissoires 
1272471d1714SBenjamin Tissoires 			if (wacom_wac1->features.type == INTUOSHT &&
1273471d1714SBenjamin Tissoires 			    wacom_wac1->features.touch_max)
1274471d1714SBenjamin Tissoires 				wacom_wac->shared->touch_input = wacom_wac2->input;
1275471d1714SBenjamin Tissoires 		}
1276471d1714SBenjamin Tissoires 
1277471d1714SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1278471d1714SBenjamin Tissoires 		if (error)
1279471d1714SBenjamin Tissoires 			goto fail;
1280471d1714SBenjamin Tissoires 	}
1281471d1714SBenjamin Tissoires 
1282471d1714SBenjamin Tissoires 	return;
1283471d1714SBenjamin Tissoires 
1284471d1714SBenjamin Tissoires fail:
12852546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
12862546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1287471d1714SBenjamin Tissoires 	return;
1288471d1714SBenjamin Tissoires }
1289471d1714SBenjamin Tissoires 
1290471d1714SBenjamin Tissoires /*
1291471d1714SBenjamin Tissoires  * Not all devices report physical dimensions from HID.
1292471d1714SBenjamin Tissoires  * Compute the default from hardcoded logical dimension
1293471d1714SBenjamin Tissoires  * and resolution before driver overwrites them.
1294471d1714SBenjamin Tissoires  */
1295471d1714SBenjamin Tissoires static void wacom_set_default_phy(struct wacom_features *features)
1296471d1714SBenjamin Tissoires {
1297471d1714SBenjamin Tissoires 	if (features->x_resolution) {
1298471d1714SBenjamin Tissoires 		features->x_phy = (features->x_max * 100) /
1299471d1714SBenjamin Tissoires 					features->x_resolution;
1300471d1714SBenjamin Tissoires 		features->y_phy = (features->y_max * 100) /
1301471d1714SBenjamin Tissoires 					features->y_resolution;
1302471d1714SBenjamin Tissoires 	}
1303471d1714SBenjamin Tissoires }
1304471d1714SBenjamin Tissoires 
1305471d1714SBenjamin Tissoires static void wacom_calculate_res(struct wacom_features *features)
1306471d1714SBenjamin Tissoires {
1307471d1714SBenjamin Tissoires 	features->x_resolution = wacom_calc_hid_res(features->x_max,
1308471d1714SBenjamin Tissoires 						    features->x_phy,
1309471d1714SBenjamin Tissoires 						    features->unit,
1310471d1714SBenjamin Tissoires 						    features->unitExpo);
1311471d1714SBenjamin Tissoires 	features->y_resolution = wacom_calc_hid_res(features->y_max,
1312471d1714SBenjamin Tissoires 						    features->y_phy,
1313471d1714SBenjamin Tissoires 						    features->unit,
1314471d1714SBenjamin Tissoires 						    features->unitExpo);
1315471d1714SBenjamin Tissoires }
1316471d1714SBenjamin Tissoires 
1317471d1714SBenjamin Tissoires static int wacom_hid_report_len(struct hid_report *report)
1318471d1714SBenjamin Tissoires {
1319471d1714SBenjamin Tissoires 	/* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1320471d1714SBenjamin Tissoires 	return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1321471d1714SBenjamin Tissoires }
1322471d1714SBenjamin Tissoires 
1323471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1324471d1714SBenjamin Tissoires {
1325471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1326471d1714SBenjamin Tissoires 	struct hid_report *report;
1327471d1714SBenjamin Tissoires 	size_t size = 0;
1328471d1714SBenjamin Tissoires 
1329471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1330471d1714SBenjamin Tissoires 
1331471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1332471d1714SBenjamin Tissoires 		size_t report_size = wacom_hid_report_len(report);
1333471d1714SBenjamin Tissoires 		if (report_size > size)
1334471d1714SBenjamin Tissoires 			size = report_size;
1335471d1714SBenjamin Tissoires 	}
1336471d1714SBenjamin Tissoires 
1337471d1714SBenjamin Tissoires 	return size;
1338471d1714SBenjamin Tissoires }
1339471d1714SBenjamin Tissoires 
1340471d1714SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
1341471d1714SBenjamin Tissoires 		const struct hid_device_id *id)
1342471d1714SBenjamin Tissoires {
1343471d1714SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1344471d1714SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
1345471d1714SBenjamin Tissoires 	struct wacom *wacom;
1346471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
1347471d1714SBenjamin Tissoires 	struct wacom_features *features;
1348471d1714SBenjamin Tissoires 	int error;
13497704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1350471d1714SBenjamin Tissoires 
1351471d1714SBenjamin Tissoires 	if (!id->driver_data)
1352471d1714SBenjamin Tissoires 		return -EINVAL;
1353471d1714SBenjamin Tissoires 
13548ffffd52SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
13558ffffd52SBenjamin Tissoires 
1356471d1714SBenjamin Tissoires 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1357471d1714SBenjamin Tissoires 	if (!wacom)
1358471d1714SBenjamin Tissoires 		return -ENOMEM;
1359471d1714SBenjamin Tissoires 
1360471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
1361471d1714SBenjamin Tissoires 	wacom->hdev = hdev;
1362471d1714SBenjamin Tissoires 
1363471d1714SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
1364471d1714SBenjamin Tissoires 	error = hid_parse(hdev);
1365471d1714SBenjamin Tissoires 	if (error) {
1366471d1714SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
13677fefeec5SBenjamin Tissoires 		goto fail_parse;
1368471d1714SBenjamin Tissoires 	}
1369471d1714SBenjamin Tissoires 
1370471d1714SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
1371471d1714SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
1372471d1714SBenjamin Tissoires 	features = &wacom_wac->features;
1373471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1374471d1714SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX) {
1375471d1714SBenjamin Tissoires 		error = -EINVAL;
13767fefeec5SBenjamin Tissoires 		goto fail_pktlen;
1377471d1714SBenjamin Tissoires 	}
1378471d1714SBenjamin Tissoires 
1379471d1714SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
1380471d1714SBenjamin Tissoires 		error = -ENODEV;
13817fefeec5SBenjamin Tissoires 		goto fail_type;
1382471d1714SBenjamin Tissoires 	}
1383471d1714SBenjamin Tissoires 
1384471d1714SBenjamin Tissoires 	wacom->usbdev = dev;
1385471d1714SBenjamin Tissoires 	wacom->intf = intf;
1386471d1714SBenjamin Tissoires 	mutex_init(&wacom->lock);
1387471d1714SBenjamin Tissoires 	INIT_WORK(&wacom->work, wacom_wireless_work);
1388471d1714SBenjamin Tissoires 
1389494078b0SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1390494078b0SBenjamin Tissoires 		error = wacom_allocate_inputs(wacom);
1391494078b0SBenjamin Tissoires 		if (error)
1392494078b0SBenjamin Tissoires 			goto fail_allocate_inputs;
1393494078b0SBenjamin Tissoires 	}
1394494078b0SBenjamin Tissoires 
1395471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1396471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1397471d1714SBenjamin Tissoires 
1398471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1399471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
1400471d1714SBenjamin Tissoires 
1401471d1714SBenjamin Tissoires 	/*
1402471d1714SBenjamin Tissoires 	 * Intuos5 has no useful data about its touch interface in its
1403471d1714SBenjamin Tissoires 	 * HID descriptor. If this is the touch interface (PacketSize
1404471d1714SBenjamin Tissoires 	 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1405471d1714SBenjamin Tissoires 	 */
1406471d1714SBenjamin Tissoires 	if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1407471d1714SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1408471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
1409471d1714SBenjamin Tissoires 
1410471d1714SBenjamin Tissoires 			features->x_max = 4096;
1411471d1714SBenjamin Tissoires 			features->y_max = 4096;
1412471d1714SBenjamin Tissoires 		} else {
1413471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_PEN;
1414471d1714SBenjamin Tissoires 		}
1415471d1714SBenjamin Tissoires 	}
1416471d1714SBenjamin Tissoires 
1417471d1714SBenjamin Tissoires 	/*
1418471d1714SBenjamin Tissoires 	 * Same thing for Bamboo 3rd gen.
1419471d1714SBenjamin Tissoires 	 */
1420471d1714SBenjamin Tissoires 	if ((features->type == BAMBOO_PT) &&
1421471d1714SBenjamin Tissoires 	    (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1422471d1714SBenjamin Tissoires 	    (features->device_type == BTN_TOOL_PEN)) {
1423471d1714SBenjamin Tissoires 		features->device_type = BTN_TOOL_FINGER;
1424471d1714SBenjamin Tissoires 
1425471d1714SBenjamin Tissoires 		features->x_max = 4096;
1426471d1714SBenjamin Tissoires 		features->y_max = 4096;
1427471d1714SBenjamin Tissoires 	}
1428471d1714SBenjamin Tissoires 
1429f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1430f81a1295SBenjamin Tissoires 		features->quirks |= WACOM_QUIRK_BATTERY;
1431f81a1295SBenjamin Tissoires 
1432471d1714SBenjamin Tissoires 	wacom_setup_device_quirks(features);
1433471d1714SBenjamin Tissoires 
1434471d1714SBenjamin Tissoires 	/* set unit to "100th of a mm" for devices not reported by HID */
1435471d1714SBenjamin Tissoires 	if (!features->unit) {
1436471d1714SBenjamin Tissoires 		features->unit = 0x11;
1437471d1714SBenjamin Tissoires 		features->unitExpo = -3;
1438471d1714SBenjamin Tissoires 	}
1439471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1440471d1714SBenjamin Tissoires 
1441471d1714SBenjamin Tissoires 	strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1442471d1714SBenjamin Tissoires 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1443471d1714SBenjamin Tissoires 		"%s Pad", features->name);
1444471d1714SBenjamin Tissoires 
1445471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1446471d1714SBenjamin Tissoires 		/* Append the device type to the name */
1447471d1714SBenjamin Tissoires 		if (features->device_type != BTN_TOOL_FINGER)
1448471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1449471d1714SBenjamin Tissoires 		else if (features->touch_max)
1450471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1451471d1714SBenjamin Tissoires 		else
1452471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1453471d1714SBenjamin Tissoires 
1454471d1714SBenjamin Tissoires 		error = wacom_add_shared_data(hdev);
1455471d1714SBenjamin Tissoires 		if (error)
14567fefeec5SBenjamin Tissoires 			goto fail_shared_data;
1457471d1714SBenjamin Tissoires 	}
1458471d1714SBenjamin Tissoires 
1459f81a1295SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1460f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1461f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1462f81a1295SBenjamin Tissoires 		if (error)
14637fefeec5SBenjamin Tissoires 			goto fail_battery;
1464f81a1295SBenjamin Tissoires 	}
1465f81a1295SBenjamin Tissoires 
1466471d1714SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1467494078b0SBenjamin Tissoires 		error = wacom_register_inputs(wacom);
1468471d1714SBenjamin Tissoires 		if (error)
14697fefeec5SBenjamin Tissoires 			goto fail_register_inputs;
1470f81a1295SBenjamin Tissoires 	}
1471f81a1295SBenjamin Tissoires 
1472f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1473f81a1295SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
1474f81a1295SBenjamin Tissoires 		if (error)
1475f81a1295SBenjamin Tissoires 			hid_warn(hdev,
1476f81a1295SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
1477f81a1295SBenjamin Tissoires 				 error);
1478471d1714SBenjamin Tissoires 	}
1479471d1714SBenjamin Tissoires 
14807704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
14817704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
14827704ac93SBenjamin Tissoires 
1483471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
14847704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
1485471d1714SBenjamin Tissoires 	if (error) {
1486471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
14877fefeec5SBenjamin Tissoires 		goto fail_hw_start;
1488471d1714SBenjamin Tissoires 	}
1489471d1714SBenjamin Tissoires 
14905ae6e89fSBenjamin Tissoires 	/* Note that if query fails it is not a hard failure */
14915ae6e89fSBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
14925ae6e89fSBenjamin Tissoires 
1493471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MONITOR)
1494471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
1495471d1714SBenjamin Tissoires 
1496471d1714SBenjamin Tissoires 	if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1497471d1714SBenjamin Tissoires 		if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1498471d1714SBenjamin Tissoires 			wacom_wac->shared->touch_input = wacom_wac->input;
1499471d1714SBenjamin Tissoires 	}
1500471d1714SBenjamin Tissoires 
1501471d1714SBenjamin Tissoires 	return 0;
1502471d1714SBenjamin Tissoires 
15037fefeec5SBenjamin Tissoires fail_hw_start:
15047fefeec5SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
15057fefeec5SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
15067fefeec5SBenjamin Tissoires fail_register_inputs:
15072546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
15087fefeec5SBenjamin Tissoires 	wacom_destroy_battery(wacom);
15097fefeec5SBenjamin Tissoires fail_battery:
15107fefeec5SBenjamin Tissoires 	wacom_remove_shared_data(wacom_wac);
15117fefeec5SBenjamin Tissoires fail_shared_data:
1512494078b0SBenjamin Tissoires 	wacom_clean_inputs(wacom);
1513494078b0SBenjamin Tissoires fail_allocate_inputs:
15147fefeec5SBenjamin Tissoires fail_type:
15157fefeec5SBenjamin Tissoires fail_pktlen:
15167fefeec5SBenjamin Tissoires fail_parse:
15177fefeec5SBenjamin Tissoires 	kfree(wacom);
1518471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1519471d1714SBenjamin Tissoires 	return error;
1520471d1714SBenjamin Tissoires }
1521471d1714SBenjamin Tissoires 
1522471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
1523471d1714SBenjamin Tissoires {
1524471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1525471d1714SBenjamin Tissoires 
1526471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
1527471d1714SBenjamin Tissoires 
1528471d1714SBenjamin Tissoires 	cancel_work_sync(&wacom->work);
15292546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
1530f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1531f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
1532471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1533471d1714SBenjamin Tissoires 	wacom_remove_shared_data(&wacom->wacom_wac);
1534471d1714SBenjamin Tissoires 
1535471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1536471d1714SBenjamin Tissoires 	kfree(wacom);
1537471d1714SBenjamin Tissoires }
1538471d1714SBenjamin Tissoires 
153941a74581SGeert Uytterhoeven #ifdef CONFIG_PM
1540471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
1541471d1714SBenjamin Tissoires {
1542471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1543471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1544471d1714SBenjamin Tissoires 
1545471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1546471d1714SBenjamin Tissoires 
1547471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
1548471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
1549471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1550471d1714SBenjamin Tissoires 
1551471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1552471d1714SBenjamin Tissoires 
1553471d1714SBenjamin Tissoires 	return 0;
1554471d1714SBenjamin Tissoires }
1555471d1714SBenjamin Tissoires 
1556471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
1557471d1714SBenjamin Tissoires {
1558471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
1559471d1714SBenjamin Tissoires }
156041a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
1561471d1714SBenjamin Tissoires 
1562471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
1563471d1714SBenjamin Tissoires 	.name =		"wacom",
1564471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
1565471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
1566471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
15677704ac93SBenjamin Tissoires 	.event =	wacom_wac_event,
15687704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
1569471d1714SBenjamin Tissoires #ifdef CONFIG_PM
1570471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
1571471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
1572471d1714SBenjamin Tissoires #endif
1573471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
1574471d1714SBenjamin Tissoires };
1575471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
1576f2e0a7d4SBenjamin Tissoires 
1577f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
1578f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
1579f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
1580f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
1581