xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 0be017120b80f0fe3da9a8239f989a27e54828f2)
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"
16b58ba1baSJason Gerecke #include <linux/input/mt.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);
38aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
39aef3156dSJason Gerecke 
40aef3156dSJason Gerecke 	if (retval < 0)
41aef3156dSJason Gerecke 		hid_err(hdev, "wacom_get_report: ran out of retries "
42aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
43471d1714SBenjamin Tissoires 
44471d1714SBenjamin Tissoires 	return retval;
45471d1714SBenjamin Tissoires }
46471d1714SBenjamin Tissoires 
47296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
48296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
49471d1714SBenjamin Tissoires {
50471d1714SBenjamin Tissoires 	int retval;
51471d1714SBenjamin Tissoires 
52471d1714SBenjamin Tissoires 	do {
53296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
54471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
55aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
56aef3156dSJason Gerecke 
57aef3156dSJason Gerecke 	if (retval < 0)
58aef3156dSJason Gerecke 		hid_err(hdev, "wacom_set_report: ran out of retries "
59aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
60471d1714SBenjamin Tissoires 
61471d1714SBenjamin Tissoires 	return retval;
62471d1714SBenjamin Tissoires }
63471d1714SBenjamin Tissoires 
64471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
65471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
66471d1714SBenjamin Tissoires {
67471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
68471d1714SBenjamin Tissoires 
69471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
70471d1714SBenjamin Tissoires 		return 1;
71471d1714SBenjamin Tissoires 
72471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
73471d1714SBenjamin Tissoires 
74471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
75471d1714SBenjamin Tissoires 
76471d1714SBenjamin Tissoires 	return 0;
77471d1714SBenjamin Tissoires }
78471d1714SBenjamin Tissoires 
79471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
80471d1714SBenjamin Tissoires {
81471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
82471d1714SBenjamin Tissoires 
83dff67416SBenjamin Tissoires 	return hid_hw_open(wacom->hdev);
84471d1714SBenjamin Tissoires }
85471d1714SBenjamin Tissoires 
86471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
87471d1714SBenjamin Tissoires {
88471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
89471d1714SBenjamin Tissoires 
90471d1714SBenjamin Tissoires 	hid_hw_close(wacom->hdev);
91471d1714SBenjamin Tissoires }
92471d1714SBenjamin Tissoires 
93471d1714SBenjamin Tissoires /*
94471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
95471d1714SBenjamin Tissoires  */
96471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
97471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
98471d1714SBenjamin Tissoires {
99471d1714SBenjamin Tissoires 	struct hid_field field = {
100471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
101471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
102471d1714SBenjamin Tissoires 		.unit = unit,
103471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
104471d1714SBenjamin Tissoires 	};
105471d1714SBenjamin Tissoires 
106471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
107471d1714SBenjamin Tissoires }
108471d1714SBenjamin Tissoires 
109471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
110471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
111471d1714SBenjamin Tissoires {
112471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
113471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1145ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
1158ffffd52SBenjamin Tissoires 	u8 *data;
1168ffffd52SBenjamin Tissoires 	int ret;
117471d1714SBenjamin Tissoires 
118471d1714SBenjamin Tissoires 	switch (usage->hid) {
119471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
120471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
1218ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
1228ffffd52SBenjamin Tissoires 			/* read manually */
1238ffffd52SBenjamin Tissoires 			data = kzalloc(2, GFP_KERNEL);
1248ffffd52SBenjamin Tissoires 			if (!data)
1258ffffd52SBenjamin Tissoires 				break;
1268ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
1278ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
12805e8fd92SJason Gerecke 						data, 2, WAC_CMD_RETRIES);
12905e8fd92SJason Gerecke 			if (ret == 2) {
1308ffffd52SBenjamin Tissoires 				features->touch_max = data[1];
13105e8fd92SJason Gerecke 			} else {
13205e8fd92SJason Gerecke 				features->touch_max = 16;
13305e8fd92SJason Gerecke 				hid_warn(hdev, "wacom_feature_mapping: "
13405e8fd92SJason Gerecke 					 "could not get HID_DG_CONTACTMAX, "
13505e8fd92SJason Gerecke 					 "defaulting to %d\n",
13605e8fd92SJason Gerecke 					  features->touch_max);
13705e8fd92SJason Gerecke 			}
1388ffffd52SBenjamin Tissoires 			kfree(data);
1398ffffd52SBenjamin Tissoires 		}
140471d1714SBenjamin Tissoires 		break;
1415ae6e89fSBenjamin Tissoires 	case HID_DG_INPUTMODE:
1425ae6e89fSBenjamin Tissoires 		/* Ignore if value index is out of bounds. */
1435ae6e89fSBenjamin Tissoires 		if (usage->usage_index >= field->report_count) {
1445ae6e89fSBenjamin Tissoires 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
1455ae6e89fSBenjamin Tissoires 			break;
1465ae6e89fSBenjamin Tissoires 		}
1475ae6e89fSBenjamin Tissoires 
1485ae6e89fSBenjamin Tissoires 		hid_data->inputmode = field->report->id;
1495ae6e89fSBenjamin Tissoires 		hid_data->inputmode_index = usage->usage_index;
1505ae6e89fSBenjamin Tissoires 		break;
151471d1714SBenjamin Tissoires 	}
152471d1714SBenjamin Tissoires }
153471d1714SBenjamin Tissoires 
154471d1714SBenjamin Tissoires /*
155471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
156471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
157471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
158471d1714SBenjamin Tissoires  * resolution based on product ID's.
159471d1714SBenjamin Tissoires  *
160471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
161471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
162471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
163471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
164471d1714SBenjamin Tissoires  * an additional table.
165471d1714SBenjamin Tissoires  *
166471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
167471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
168471d1714SBenjamin Tissoires  * function will ignore it.
169471d1714SBenjamin Tissoires  *
170471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
171471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
172471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
173471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
174471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
175471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
176471d1714SBenjamin Tissoires  *
177471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
178471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
179471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
180471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
181471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
182471d1714SBenjamin Tissoires  *
183471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
184471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
185471d1714SBenjamin Tissoires  */
186471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
187471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
188471d1714SBenjamin Tissoires {
189471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
190471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
191d97a5522SBenjamin Tissoires 	bool finger = WACOM_FINGER_FIELD(field);
192d97a5522SBenjamin Tissoires 	bool pen = WACOM_PEN_FIELD(field);
193471d1714SBenjamin Tissoires 
194471d1714SBenjamin Tissoires 	/*
195471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
196471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
197471d1714SBenjamin Tissoires 	* values commonly reported.
198471d1714SBenjamin Tissoires 	*/
199042628abSJason Gerecke 	if (pen)
200aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
201042628abSJason Gerecke 	else if (finger)
202aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_TOUCH;
203042628abSJason Gerecke 	else
204471d1714SBenjamin Tissoires 		return;
205471d1714SBenjamin Tissoires 
20630ebc1aeSPing Cheng 	/*
20730ebc1aeSPing Cheng 	 * Bamboo models do not support HID_DG_CONTACTMAX.
20830ebc1aeSPing Cheng 	 * And, Bamboo Pen only descriptor contains touch.
20930ebc1aeSPing Cheng 	 */
21030ebc1aeSPing Cheng 	if (features->type != BAMBOO_PT) {
21130ebc1aeSPing Cheng 		/* ISDv4 touch devices at least supports one touch point */
212471d1714SBenjamin Tissoires 		if (finger && !features->touch_max)
213471d1714SBenjamin Tissoires 			features->touch_max = 1;
21430ebc1aeSPing Cheng 	}
215471d1714SBenjamin Tissoires 
216471d1714SBenjamin Tissoires 	switch (usage->hid) {
217471d1714SBenjamin Tissoires 	case HID_GD_X:
218471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
219471d1714SBenjamin Tissoires 		if (finger) {
220471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
221471d1714SBenjamin Tissoires 			if (features->type != BAMBOO_PT) {
222471d1714SBenjamin Tissoires 				features->unit = field->unit;
223471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
224471d1714SBenjamin Tissoires 			}
225471d1714SBenjamin Tissoires 		}
226471d1714SBenjamin Tissoires 		break;
227471d1714SBenjamin Tissoires 	case HID_GD_Y:
228471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
229471d1714SBenjamin Tissoires 		if (finger) {
230471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
231471d1714SBenjamin Tissoires 			if (features->type != BAMBOO_PT) {
232471d1714SBenjamin Tissoires 				features->unit = field->unit;
233471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
234471d1714SBenjamin Tissoires 			}
235471d1714SBenjamin Tissoires 		}
236471d1714SBenjamin Tissoires 		break;
237471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
238471d1714SBenjamin Tissoires 		if (pen)
239471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
240471d1714SBenjamin Tissoires 		break;
241471d1714SBenjamin Tissoires 	}
2427704ac93SBenjamin Tissoires 
2437704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
2447704ac93SBenjamin Tissoires 		wacom_wac_usage_mapping(hdev, field, usage);
245471d1714SBenjamin Tissoires }
246471d1714SBenjamin Tissoires 
247b58ba1baSJason Gerecke static void wacom_post_parse_hid(struct hid_device *hdev,
248b58ba1baSJason Gerecke 				 struct wacom_features *features)
249b58ba1baSJason Gerecke {
250b58ba1baSJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
251b58ba1baSJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
252b58ba1baSJason Gerecke 
253b58ba1baSJason Gerecke 	if (features->type == HID_GENERIC) {
254b58ba1baSJason Gerecke 		/* Any last-minute generic device setup */
255b58ba1baSJason Gerecke 		if (features->touch_max > 1) {
2562a6cdbddSJason Gerecke 			input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
257b58ba1baSJason Gerecke 				    INPUT_MT_DIRECT);
258b58ba1baSJason Gerecke 		}
259b58ba1baSJason Gerecke 	}
260b58ba1baSJason Gerecke }
261b58ba1baSJason Gerecke 
262471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
263471d1714SBenjamin Tissoires 			   struct wacom_features *features)
264471d1714SBenjamin Tissoires {
265471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
266471d1714SBenjamin Tissoires 	struct hid_report *hreport;
267471d1714SBenjamin Tissoires 	int i, j;
268471d1714SBenjamin Tissoires 
269471d1714SBenjamin Tissoires 	/* check features first */
270471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
271471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
272471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
273471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
274471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
275471d1714SBenjamin Tissoires 				continue;
276471d1714SBenjamin Tissoires 
277471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
278471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
279471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
280471d1714SBenjamin Tissoires 			}
281471d1714SBenjamin Tissoires 		}
282471d1714SBenjamin Tissoires 	}
283471d1714SBenjamin Tissoires 
284471d1714SBenjamin Tissoires 	/* now check the input usages */
285471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
286471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
287471d1714SBenjamin Tissoires 
288471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
289471d1714SBenjamin Tissoires 			continue;
290471d1714SBenjamin Tissoires 
291471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
292471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
293471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
294471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
295471d1714SBenjamin Tissoires 	}
296b58ba1baSJason Gerecke 
297b58ba1baSJason Gerecke 	wacom_post_parse_hid(hdev, features);
298471d1714SBenjamin Tissoires }
299471d1714SBenjamin Tissoires 
3005ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev)
3015ae6e89fSBenjamin Tissoires {
3025ae6e89fSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
3035ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
3045ae6e89fSBenjamin Tissoires 	struct hid_report *r;
3055ae6e89fSBenjamin Tissoires 	struct hid_report_enum *re;
3065ae6e89fSBenjamin Tissoires 
3075ae6e89fSBenjamin Tissoires 	if (hid_data->inputmode < 0)
3085ae6e89fSBenjamin Tissoires 		return 0;
3095ae6e89fSBenjamin Tissoires 
3105ae6e89fSBenjamin Tissoires 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
3115ae6e89fSBenjamin Tissoires 	r = re->report_id_hash[hid_data->inputmode];
3125ae6e89fSBenjamin Tissoires 	if (r) {
3135ae6e89fSBenjamin Tissoires 		r->field[0]->value[hid_data->inputmode_index] = 2;
3145ae6e89fSBenjamin Tissoires 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
3155ae6e89fSBenjamin Tissoires 	}
3165ae6e89fSBenjamin Tissoires 	return 0;
3175ae6e89fSBenjamin Tissoires }
3185ae6e89fSBenjamin Tissoires 
319471d1714SBenjamin Tissoires static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
320471d1714SBenjamin Tissoires 		int length, int mode)
321471d1714SBenjamin Tissoires {
322471d1714SBenjamin Tissoires 	unsigned char *rep_data;
323471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
324471d1714SBenjamin Tissoires 
325471d1714SBenjamin Tissoires 	rep_data = kzalloc(length, GFP_KERNEL);
326471d1714SBenjamin Tissoires 	if (!rep_data)
327471d1714SBenjamin Tissoires 		return error;
328471d1714SBenjamin Tissoires 
329471d1714SBenjamin Tissoires 	do {
330471d1714SBenjamin Tissoires 		rep_data[0] = report_id;
331471d1714SBenjamin Tissoires 		rep_data[1] = mode;
332471d1714SBenjamin Tissoires 
333296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
334296b7378SPrzemo Firszt 					 length, 1);
335471d1714SBenjamin Tissoires 		if (error >= 0)
336471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
337c64d8834SPing Cheng 			                         rep_data, length, 1);
338471d1714SBenjamin Tissoires 	} while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
339471d1714SBenjamin Tissoires 
340471d1714SBenjamin Tissoires 	kfree(rep_data);
341471d1714SBenjamin Tissoires 
342471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
343471d1714SBenjamin Tissoires }
344471d1714SBenjamin Tissoires 
345f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
346f81a1295SBenjamin Tissoires 		struct wacom_features *features)
347f81a1295SBenjamin Tissoires {
348387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
349387142bbSBenjamin Tissoires 	int ret;
350387142bbSBenjamin Tissoires 	u8 rep_data[2];
351387142bbSBenjamin Tissoires 
352387142bbSBenjamin Tissoires 	switch (features->type) {
353387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
354387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
355387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
356296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
357296b7378SPrzemo Firszt 					3);
358387142bbSBenjamin Tissoires 
359387142bbSBenjamin Tissoires 		if (ret >= 0) {
360387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
361387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
362387142bbSBenjamin Tissoires 
363387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
364296b7378SPrzemo Firszt 						rep_data, 2, 3);
365387142bbSBenjamin Tissoires 
366387142bbSBenjamin Tissoires 			if (ret >= 0) {
367387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
368387142bbSBenjamin Tissoires 				return 0;
369387142bbSBenjamin Tissoires 			}
370387142bbSBenjamin Tissoires 		}
371387142bbSBenjamin Tissoires 
372387142bbSBenjamin Tissoires 		/*
373387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
374387142bbSBenjamin Tissoires 		 * and it is safe to continue
375387142bbSBenjamin Tissoires 		 */
376387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
377387142bbSBenjamin Tissoires 			 rep_data[0], ret);
378387142bbSBenjamin Tissoires 		break;
37981af7e61SBenjamin Tissoires 	case INTUOS4WL:
38081af7e61SBenjamin Tissoires 		if (speed == 1)
38181af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
38281af7e61SBenjamin Tissoires 		else
38381af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
38481af7e61SBenjamin Tissoires 
38581af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
38681af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
38781af7e61SBenjamin Tissoires 
388296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
389296b7378SPrzemo Firszt 					1);
39081af7e61SBenjamin Tissoires 		if (ret >= 0)
39181af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
39281af7e61SBenjamin Tissoires 		break;
393387142bbSBenjamin Tissoires 	}
394387142bbSBenjamin Tissoires 
395f81a1295SBenjamin Tissoires 	return 0;
396f81a1295SBenjamin Tissoires }
397f81a1295SBenjamin Tissoires 
398471d1714SBenjamin Tissoires /*
399471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
400471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
401471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
402471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
403471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
404471d1714SBenjamin Tissoires  */
405471d1714SBenjamin Tissoires static int wacom_query_tablet_data(struct hid_device *hdev,
406471d1714SBenjamin Tissoires 		struct wacom_features *features)
407471d1714SBenjamin Tissoires {
408f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
409f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
410f81a1295SBenjamin Tissoires 
4115ae6e89fSBenjamin Tissoires 	if (features->type == HID_GENERIC)
4125ae6e89fSBenjamin Tissoires 		return wacom_hid_set_device_mode(hdev);
4135ae6e89fSBenjamin Tissoires 
414aa86b18cSJason Gerecke 	if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
415471d1714SBenjamin Tissoires 		if (features->type > TABLETPC) {
416471d1714SBenjamin Tissoires 			/* MT Tablet PC touch */
417471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 3, 4, 4);
418471d1714SBenjamin Tissoires 		}
419471d1714SBenjamin Tissoires 		else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
420471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 18, 3, 2);
421471d1714SBenjamin Tissoires 		}
422500d4160SPing Cheng 		else if (features->type == WACOM_27QHDT) {
423500d4160SPing Cheng 			return wacom_set_device_mode(hdev, 131, 3, 2);
424500d4160SPing Cheng 		}
4258c97a765SBenjamin Tissoires 		else if (features->type == BAMBOO_PAD) {
4268c97a765SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 2, 2, 2);
4278c97a765SBenjamin Tissoires 		}
428aa86b18cSJason Gerecke 	} else if (features->device_type & WACOM_DEVICETYPE_PEN) {
429471d1714SBenjamin Tissoires 		if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
430471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 2, 2, 2);
431471d1714SBenjamin Tissoires 		}
432471d1714SBenjamin Tissoires 	}
433471d1714SBenjamin Tissoires 
434471d1714SBenjamin Tissoires 	return 0;
435471d1714SBenjamin Tissoires }
436471d1714SBenjamin Tissoires 
437471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
438471d1714SBenjamin Tissoires 					 struct wacom_features *features)
439471d1714SBenjamin Tissoires {
440471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
441471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
442471d1714SBenjamin Tissoires 
443471d1714SBenjamin Tissoires 	/* default features */
444471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
445471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
446471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
447471d1714SBenjamin Tissoires 	features->distance_fuzz = 0;
448471d1714SBenjamin Tissoires 
449471d1714SBenjamin Tissoires 	/*
450471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
451471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
452471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
453471d1714SBenjamin Tissoires 	 * interface number.
454471d1714SBenjamin Tissoires 	 */
455471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
456471d1714SBenjamin Tissoires 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
457aa86b18cSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_NONE;
458471d1714SBenjamin Tissoires 		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
459aa86b18cSJason Gerecke 			features->device_type |= WACOM_DEVICETYPE_TOUCH;
460471d1714SBenjamin Tissoires 			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
461471d1714SBenjamin Tissoires 		}
462471d1714SBenjamin Tissoires 	}
463471d1714SBenjamin Tissoires 
464471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
465471d1714SBenjamin Tissoires }
466471d1714SBenjamin Tissoires 
467471d1714SBenjamin Tissoires struct wacom_hdev_data {
468471d1714SBenjamin Tissoires 	struct list_head list;
469471d1714SBenjamin Tissoires 	struct kref kref;
470471d1714SBenjamin Tissoires 	struct hid_device *dev;
471471d1714SBenjamin Tissoires 	struct wacom_shared shared;
472471d1714SBenjamin Tissoires };
473471d1714SBenjamin Tissoires 
474471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
475471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
476471d1714SBenjamin Tissoires 
477471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
478471d1714SBenjamin Tissoires 		struct hid_device *sibling)
479471d1714SBenjamin Tissoires {
480471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
481471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
482471d1714SBenjamin Tissoires 	int vid = features->oVid;
483471d1714SBenjamin Tissoires 	int pid = features->oPid;
484471d1714SBenjamin Tissoires 	int n1,n2;
485471d1714SBenjamin Tissoires 
486471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
487471d1714SBenjamin Tissoires 		vid = hdev->vendor;
488471d1714SBenjamin Tissoires 		pid = hdev->product;
489471d1714SBenjamin Tissoires 	}
490471d1714SBenjamin Tissoires 
491471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
492471d1714SBenjamin Tissoires 		return false;
493471d1714SBenjamin Tissoires 
494471d1714SBenjamin Tissoires 	/* Compare the physical path. */
495471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
496471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
497471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
498471d1714SBenjamin Tissoires 		return false;
499471d1714SBenjamin Tissoires 
500471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
501471d1714SBenjamin Tissoires }
502471d1714SBenjamin Tissoires 
503471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
504471d1714SBenjamin Tissoires {
505471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
506471d1714SBenjamin Tissoires 
507471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
508471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
509471d1714SBenjamin Tissoires 			kref_get(&data->kref);
510471d1714SBenjamin Tissoires 			return data;
511471d1714SBenjamin Tissoires 		}
512471d1714SBenjamin Tissoires 	}
513471d1714SBenjamin Tissoires 
514471d1714SBenjamin Tissoires 	return NULL;
515471d1714SBenjamin Tissoires }
516471d1714SBenjamin Tissoires 
517471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
518471d1714SBenjamin Tissoires {
519471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
520471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
521471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
522471d1714SBenjamin Tissoires 	int retval = 0;
523471d1714SBenjamin Tissoires 
524471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
525471d1714SBenjamin Tissoires 
526471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
527471d1714SBenjamin Tissoires 	if (!data) {
528471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
529471d1714SBenjamin Tissoires 		if (!data) {
530471d1714SBenjamin Tissoires 			retval = -ENOMEM;
531471d1714SBenjamin Tissoires 			goto out;
532471d1714SBenjamin Tissoires 		}
533471d1714SBenjamin Tissoires 
534471d1714SBenjamin Tissoires 		kref_init(&data->kref);
535471d1714SBenjamin Tissoires 		data->dev = hdev;
536471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
537471d1714SBenjamin Tissoires 	}
538471d1714SBenjamin Tissoires 
539471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
540471d1714SBenjamin Tissoires 
541aa86b18cSJason Gerecke 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
542a97ac104SBenjamin Tissoires 		wacom_wac->shared->touch = hdev;
543aa86b18cSJason Gerecke 	else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
544a97ac104SBenjamin Tissoires 		wacom_wac->shared->pen = hdev;
545a97ac104SBenjamin Tissoires 
546471d1714SBenjamin Tissoires out:
547471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
548471d1714SBenjamin Tissoires 	return retval;
549471d1714SBenjamin Tissoires }
550471d1714SBenjamin Tissoires 
551471d1714SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
552471d1714SBenjamin Tissoires {
553471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data =
554471d1714SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
555471d1714SBenjamin Tissoires 
556471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
557471d1714SBenjamin Tissoires 	list_del(&data->list);
558471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
559471d1714SBenjamin Tissoires 
560471d1714SBenjamin Tissoires 	kfree(data);
561471d1714SBenjamin Tissoires }
562471d1714SBenjamin Tissoires 
563a97ac104SBenjamin Tissoires static void wacom_remove_shared_data(struct wacom *wacom)
564471d1714SBenjamin Tissoires {
565471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
566a97ac104SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
567471d1714SBenjamin Tissoires 
568a97ac104SBenjamin Tissoires 	if (wacom_wac->shared) {
569a97ac104SBenjamin Tissoires 		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
570a97ac104SBenjamin Tissoires 				    shared);
571a97ac104SBenjamin Tissoires 
572a97ac104SBenjamin Tissoires 		if (wacom_wac->shared->touch == wacom->hdev)
573a97ac104SBenjamin Tissoires 			wacom_wac->shared->touch = NULL;
574a97ac104SBenjamin Tissoires 		else if (wacom_wac->shared->pen == wacom->hdev)
575a97ac104SBenjamin Tissoires 			wacom_wac->shared->pen = NULL;
576a97ac104SBenjamin Tissoires 
577471d1714SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
578a97ac104SBenjamin Tissoires 		wacom_wac->shared = NULL;
579471d1714SBenjamin Tissoires 	}
580471d1714SBenjamin Tissoires }
581471d1714SBenjamin Tissoires 
582471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
583471d1714SBenjamin Tissoires {
584471d1714SBenjamin Tissoires 	unsigned char *buf;
585471d1714SBenjamin Tissoires 	int retval;
586912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
587912ca216SPing Cheng 	int buf_size = 9;
588471d1714SBenjamin Tissoires 
589912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
590912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
591912ca216SPing Cheng 		buf_size = 13;
592912ca216SPing Cheng 	}
593912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
594471d1714SBenjamin Tissoires 	if (!buf)
595471d1714SBenjamin Tissoires 		return -ENOMEM;
596471d1714SBenjamin Tissoires 
597471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
598471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
599471d1714SBenjamin Tissoires 		/*
600471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
601471d1714SBenjamin Tissoires 		 * one of four values:
602471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
603471d1714SBenjamin Tissoires 		 */
604471d1714SBenjamin Tissoires 		int ring_led = wacom->led.select[0] & 0x03;
605471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
606471d1714SBenjamin Tissoires 		int crop_lum = 0;
607912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
608471d1714SBenjamin Tissoires 
609912ca216SPing Cheng 		buf[0] = report_id;
610912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
611912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
612912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
613912ca216SPing Cheng 			buf[0] = report_id;
614912ca216SPing Cheng 			buf[4] = led_bits;
615912ca216SPing Cheng 		} else
616912ca216SPing Cheng 			buf[1] = led_bits;
617471d1714SBenjamin Tissoires 	}
618471d1714SBenjamin Tissoires 	else {
619471d1714SBenjamin Tissoires 		int led = wacom->led.select[0] | 0x4;
620471d1714SBenjamin Tissoires 
621471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
622471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
623471d1714SBenjamin Tissoires 			led |= (wacom->led.select[1] << 4) | 0x40;
624471d1714SBenjamin Tissoires 
625912ca216SPing Cheng 		buf[0] = report_id;
626471d1714SBenjamin Tissoires 		buf[1] = led;
627471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
628471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
629471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
630471d1714SBenjamin Tissoires 	}
631471d1714SBenjamin Tissoires 
632912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
633296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
634471d1714SBenjamin Tissoires 	kfree(buf);
635471d1714SBenjamin Tissoires 
636471d1714SBenjamin Tissoires 	return retval;
637471d1714SBenjamin Tissoires }
638471d1714SBenjamin Tissoires 
639849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
640849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
641471d1714SBenjamin Tissoires {
642471d1714SBenjamin Tissoires 	unsigned char *buf;
643471d1714SBenjamin Tissoires 	int i, retval;
644849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
645471d1714SBenjamin Tissoires 
646849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
647471d1714SBenjamin Tissoires 	if (!buf)
648471d1714SBenjamin Tissoires 		return -ENOMEM;
649471d1714SBenjamin Tissoires 
650471d1714SBenjamin Tissoires 	/* Send 'start' command */
651471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
652471d1714SBenjamin Tissoires 	buf[1] = 1;
653296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
654296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
655471d1714SBenjamin Tissoires 	if (retval < 0)
656471d1714SBenjamin Tissoires 		goto out;
657471d1714SBenjamin Tissoires 
658849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
659471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
660471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
661471d1714SBenjamin Tissoires 		buf[2] = i;
662849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
663471d1714SBenjamin Tissoires 
664471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
665296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
666471d1714SBenjamin Tissoires 		if (retval < 0)
667471d1714SBenjamin Tissoires 			break;
668471d1714SBenjamin Tissoires 	}
669471d1714SBenjamin Tissoires 
670471d1714SBenjamin Tissoires 	/* Send 'stop' */
671471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
672471d1714SBenjamin Tissoires 	buf[1] = 0;
673296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
674296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
675471d1714SBenjamin Tissoires 
676471d1714SBenjamin Tissoires out:
677471d1714SBenjamin Tissoires 	kfree(buf);
678471d1714SBenjamin Tissoires 	return retval;
679471d1714SBenjamin Tissoires }
680471d1714SBenjamin Tissoires 
681471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
682471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
683471d1714SBenjamin Tissoires {
684471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
685471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
686471d1714SBenjamin Tissoires 	unsigned int id;
687471d1714SBenjamin Tissoires 	int err;
688471d1714SBenjamin Tissoires 
689471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
690471d1714SBenjamin Tissoires 	if (err)
691471d1714SBenjamin Tissoires 		return err;
692471d1714SBenjamin Tissoires 
693471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
694471d1714SBenjamin Tissoires 
695471d1714SBenjamin Tissoires 	wacom->led.select[set_id] = id & 0x3;
696471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
697471d1714SBenjamin Tissoires 
698471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
699471d1714SBenjamin Tissoires 
700471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
701471d1714SBenjamin Tissoires }
702471d1714SBenjamin Tissoires 
703471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
704471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
705471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
706471d1714SBenjamin Tissoires {									\
707471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
708471d1714SBenjamin Tissoires }									\
709471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
710471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
711471d1714SBenjamin Tissoires {									\
712471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
713471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
71437449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
71537449adcSPing Cheng 			 wacom->led.select[SET_ID]);			\
716471d1714SBenjamin Tissoires }									\
717e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
718471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
719471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
720471d1714SBenjamin Tissoires 
721471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
722471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
723471d1714SBenjamin Tissoires 
724471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
725471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
726471d1714SBenjamin Tissoires {
727471d1714SBenjamin Tissoires 	unsigned int value;
728471d1714SBenjamin Tissoires 	int err;
729471d1714SBenjamin Tissoires 
730471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
731471d1714SBenjamin Tissoires 	if (err)
732471d1714SBenjamin Tissoires 		return err;
733471d1714SBenjamin Tissoires 
734471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
735471d1714SBenjamin Tissoires 
736471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
737471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
738471d1714SBenjamin Tissoires 
739471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
740471d1714SBenjamin Tissoires 
741471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
742471d1714SBenjamin Tissoires }
743471d1714SBenjamin Tissoires 
744471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
745471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
746471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
747471d1714SBenjamin Tissoires {									\
748471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
749471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
750471d1714SBenjamin Tissoires 									\
751471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
752471d1714SBenjamin Tissoires 				     buf, count);			\
753471d1714SBenjamin Tissoires }									\
75437449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
75537449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
75637449adcSPing Cheng {									\
75737449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
75837449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
75937449adcSPing Cheng }									\
760e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
76137449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
76237449adcSPing Cheng 		   wacom_##name##_luminance_store)
763471d1714SBenjamin Tissoires 
764471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
765471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
766471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
767471d1714SBenjamin Tissoires 
768471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
769471d1714SBenjamin Tissoires 					const char *buf, size_t count)
770471d1714SBenjamin Tissoires {
771471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
772471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
773471d1714SBenjamin Tissoires 	int err;
774849e2f06SBenjamin Tissoires 	unsigned len;
775849e2f06SBenjamin Tissoires 	u8 xfer_id;
776471d1714SBenjamin Tissoires 
777849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
778849e2f06SBenjamin Tissoires 		len = 256;
779849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
780849e2f06SBenjamin Tissoires 	} else {
781849e2f06SBenjamin Tissoires 		len = 1024;
782849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
783849e2f06SBenjamin Tissoires 	}
784849e2f06SBenjamin Tissoires 
785849e2f06SBenjamin Tissoires 	if (count != len)
786471d1714SBenjamin Tissoires 		return -EINVAL;
787471d1714SBenjamin Tissoires 
788471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
789471d1714SBenjamin Tissoires 
790849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
791471d1714SBenjamin Tissoires 
792471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
793471d1714SBenjamin Tissoires 
794471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
795471d1714SBenjamin Tissoires }
796471d1714SBenjamin Tissoires 
797471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
798471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
799471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
800471d1714SBenjamin Tissoires {									\
801471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
802471d1714SBenjamin Tissoires }									\
803e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
804471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
805471d1714SBenjamin Tissoires 
806471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
807471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
808471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
809471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
810471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
811471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
812471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
813471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
814471d1714SBenjamin Tissoires 
815471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
816471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
817471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
818471d1714SBenjamin Tissoires 	NULL
819471d1714SBenjamin Tissoires };
820471d1714SBenjamin Tissoires 
821471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
822471d1714SBenjamin Tissoires 	.name = "wacom_led",
823471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
824471d1714SBenjamin Tissoires };
825471d1714SBenjamin Tissoires 
826471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
827471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
828471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
829471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
830471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
831471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
832471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
833471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
834471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
835471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
836471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
837471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
838471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
839471d1714SBenjamin Tissoires 	NULL
840471d1714SBenjamin Tissoires };
841471d1714SBenjamin Tissoires 
842471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
843471d1714SBenjamin Tissoires 	.name = "wacom_led",
844471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
845471d1714SBenjamin Tissoires };
846471d1714SBenjamin Tissoires 
847471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
848471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
849471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
850471d1714SBenjamin Tissoires 	NULL
851471d1714SBenjamin Tissoires };
852471d1714SBenjamin Tissoires 
853471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
854471d1714SBenjamin Tissoires 	.name = "wacom_led",
855471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
856471d1714SBenjamin Tissoires };
857471d1714SBenjamin Tissoires 
858471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
859471d1714SBenjamin Tissoires {
860471d1714SBenjamin Tissoires 	int error;
861471d1714SBenjamin Tissoires 
862862cf553SJason Gerecke 	if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
863862cf553SJason Gerecke 		return 0;
864862cf553SJason Gerecke 
865471d1714SBenjamin Tissoires 	/* Initialize default values */
866471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
867471d1714SBenjamin Tissoires 	case INTUOS4S:
868471d1714SBenjamin Tissoires 	case INTUOS4:
86981af7e61SBenjamin Tissoires 	case INTUOS4WL:
870471d1714SBenjamin Tissoires 	case INTUOS4L:
871471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
872471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
873471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
874471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
875471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
876471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
877471d1714SBenjamin Tissoires 					   &intuos4_led_attr_group);
878471d1714SBenjamin Tissoires 		break;
879471d1714SBenjamin Tissoires 
880471d1714SBenjamin Tissoires 	case WACOM_24HD:
881471d1714SBenjamin Tissoires 	case WACOM_21UX2:
882471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
883471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
884471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
885471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
886471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
887471d1714SBenjamin Tissoires 
888471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
889471d1714SBenjamin Tissoires 					   &cintiq_led_attr_group);
890471d1714SBenjamin Tissoires 		break;
891471d1714SBenjamin Tissoires 
892471d1714SBenjamin Tissoires 	case INTUOS5S:
893471d1714SBenjamin Tissoires 	case INTUOS5:
894471d1714SBenjamin Tissoires 	case INTUOS5L:
895471d1714SBenjamin Tissoires 	case INTUOSPS:
896471d1714SBenjamin Tissoires 	case INTUOSPM:
897471d1714SBenjamin Tissoires 	case INTUOSPL:
898471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
899471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
900471d1714SBenjamin Tissoires 		wacom->led.llv = 32;
901471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
902471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
903471d1714SBenjamin Tissoires 
904471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
905471d1714SBenjamin Tissoires 					  &intuos5_led_attr_group);
906471d1714SBenjamin Tissoires 		break;
907471d1714SBenjamin Tissoires 
908471d1714SBenjamin Tissoires 	default:
909471d1714SBenjamin Tissoires 		return 0;
910471d1714SBenjamin Tissoires 	}
911471d1714SBenjamin Tissoires 
912471d1714SBenjamin Tissoires 	if (error) {
913471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
914471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
915471d1714SBenjamin Tissoires 		return error;
916471d1714SBenjamin Tissoires 	}
917471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
918c757cbafSBenjamin Tissoires 	wacom->led_initialized = true;
919471d1714SBenjamin Tissoires 
920471d1714SBenjamin Tissoires 	return 0;
921471d1714SBenjamin Tissoires }
922471d1714SBenjamin Tissoires 
923471d1714SBenjamin Tissoires static void wacom_destroy_leds(struct wacom *wacom)
924471d1714SBenjamin Tissoires {
925c757cbafSBenjamin Tissoires 	if (!wacom->led_initialized)
926c757cbafSBenjamin Tissoires 		return;
927c757cbafSBenjamin Tissoires 
928862cf553SJason Gerecke 	if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
929862cf553SJason Gerecke 		return;
930862cf553SJason Gerecke 
931c757cbafSBenjamin Tissoires 	wacom->led_initialized = false;
932c757cbafSBenjamin Tissoires 
933471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
934471d1714SBenjamin Tissoires 	case INTUOS4S:
935471d1714SBenjamin Tissoires 	case INTUOS4:
93681af7e61SBenjamin Tissoires 	case INTUOS4WL:
937471d1714SBenjamin Tissoires 	case INTUOS4L:
938471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
939471d1714SBenjamin Tissoires 				   &intuos4_led_attr_group);
940471d1714SBenjamin Tissoires 		break;
941471d1714SBenjamin Tissoires 
942471d1714SBenjamin Tissoires 	case WACOM_24HD:
943471d1714SBenjamin Tissoires 	case WACOM_21UX2:
944471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
945471d1714SBenjamin Tissoires 				   &cintiq_led_attr_group);
946471d1714SBenjamin Tissoires 		break;
947471d1714SBenjamin Tissoires 
948471d1714SBenjamin Tissoires 	case INTUOS5S:
949471d1714SBenjamin Tissoires 	case INTUOS5:
950471d1714SBenjamin Tissoires 	case INTUOS5L:
951471d1714SBenjamin Tissoires 	case INTUOSPS:
952471d1714SBenjamin Tissoires 	case INTUOSPM:
953471d1714SBenjamin Tissoires 	case INTUOSPL:
954471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
955471d1714SBenjamin Tissoires 				   &intuos5_led_attr_group);
956471d1714SBenjamin Tissoires 		break;
957471d1714SBenjamin Tissoires 	}
958471d1714SBenjamin Tissoires }
959471d1714SBenjamin Tissoires 
960471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
96171fa641eSJason Gerecke 	POWER_SUPPLY_PROP_PRESENT,
962ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
963471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
964471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
965471d1714SBenjamin Tissoires };
966471d1714SBenjamin Tissoires 
9677dbd229eSBenjamin Tissoires static enum power_supply_property wacom_ac_props[] = {
9687dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_PRESENT,
9697dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_ONLINE,
9707dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
9717dbd229eSBenjamin Tissoires };
9727dbd229eSBenjamin Tissoires 
973471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
974471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
975471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
976471d1714SBenjamin Tissoires {
977297d716fSKrzysztof Kozlowski 	struct wacom *wacom = power_supply_get_drvdata(psy);
978471d1714SBenjamin Tissoires 	int ret = 0;
979471d1714SBenjamin Tissoires 
980471d1714SBenjamin Tissoires 	switch (psp) {
98171fa641eSJason Gerecke 		case POWER_SUPPLY_PROP_PRESENT:
98271fa641eSJason Gerecke 			val->intval = wacom->wacom_wac.bat_connected;
98371fa641eSJason Gerecke 			break;
984471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
985471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
986471d1714SBenjamin Tissoires 			break;
987471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
988471d1714SBenjamin Tissoires 			val->intval =
989ac8d1010SBenjamin Tissoires 				wacom->wacom_wac.battery_capacity;
990ac8d1010SBenjamin Tissoires 			break;
991ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
992ac8d1010SBenjamin Tissoires 			if (wacom->wacom_wac.bat_charging)
993ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
994ac8d1010SBenjamin Tissoires 			else if (wacom->wacom_wac.battery_capacity == 100 &&
995ac8d1010SBenjamin Tissoires 				    wacom->wacom_wac.ps_connected)
996ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
997b0882cb7SJason Gerecke 			else if (wacom->wacom_wac.ps_connected)
998b0882cb7SJason Gerecke 				val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
999ac8d1010SBenjamin Tissoires 			else
1000ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1001471d1714SBenjamin Tissoires 			break;
1002471d1714SBenjamin Tissoires 		default:
1003471d1714SBenjamin Tissoires 			ret = -EINVAL;
1004471d1714SBenjamin Tissoires 			break;
1005471d1714SBenjamin Tissoires 	}
1006471d1714SBenjamin Tissoires 
1007471d1714SBenjamin Tissoires 	return ret;
1008471d1714SBenjamin Tissoires }
1009471d1714SBenjamin Tissoires 
10107dbd229eSBenjamin Tissoires static int wacom_ac_get_property(struct power_supply *psy,
10117dbd229eSBenjamin Tissoires 				enum power_supply_property psp,
10127dbd229eSBenjamin Tissoires 				union power_supply_propval *val)
10137dbd229eSBenjamin Tissoires {
1014297d716fSKrzysztof Kozlowski 	struct wacom *wacom = power_supply_get_drvdata(psy);
10157dbd229eSBenjamin Tissoires 	int ret = 0;
10167dbd229eSBenjamin Tissoires 
10177dbd229eSBenjamin Tissoires 	switch (psp) {
10187dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_PRESENT:
10197dbd229eSBenjamin Tissoires 		/* fall through */
10207dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_ONLINE:
10217dbd229eSBenjamin Tissoires 		val->intval = wacom->wacom_wac.ps_connected;
10227dbd229eSBenjamin Tissoires 		break;
10237dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_SCOPE:
10247dbd229eSBenjamin Tissoires 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
10257dbd229eSBenjamin Tissoires 		break;
10267dbd229eSBenjamin Tissoires 	default:
10277dbd229eSBenjamin Tissoires 		ret = -EINVAL;
10287dbd229eSBenjamin Tissoires 		break;
10297dbd229eSBenjamin Tissoires 	}
10307dbd229eSBenjamin Tissoires 	return ret;
10317dbd229eSBenjamin Tissoires }
10327dbd229eSBenjamin Tissoires 
1033471d1714SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
1034471d1714SBenjamin Tissoires {
1035d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
1036297d716fSKrzysztof Kozlowski 	struct power_supply_config psy_cfg = { .drv_data = wacom, };
1037d70420b9SBenjamin Tissoires 	unsigned long n;
1038471d1714SBenjamin Tissoires 
1039ac8d1010SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
1040297d716fSKrzysztof Kozlowski 		struct power_supply_desc *bat_desc = &wacom->battery_desc;
1041297d716fSKrzysztof Kozlowski 		struct power_supply_desc *ac_desc = &wacom->ac_desc;
1042d70420b9SBenjamin Tissoires 		n = atomic_inc_return(&battery_no) - 1;
10437dbd229eSBenjamin Tissoires 
1044297d716fSKrzysztof Kozlowski 		bat_desc->properties = wacom_battery_props;
1045297d716fSKrzysztof Kozlowski 		bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1046297d716fSKrzysztof Kozlowski 		bat_desc->get_property = wacom_battery_get_property;
1047d70420b9SBenjamin Tissoires 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
1048297d716fSKrzysztof Kozlowski 		bat_desc->name = wacom->wacom_wac.bat_name;
1049297d716fSKrzysztof Kozlowski 		bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1050297d716fSKrzysztof Kozlowski 		bat_desc->use_for_apm = 0;
1051471d1714SBenjamin Tissoires 
1052297d716fSKrzysztof Kozlowski 		ac_desc->properties = wacom_ac_props;
1053297d716fSKrzysztof Kozlowski 		ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1054297d716fSKrzysztof Kozlowski 		ac_desc->get_property = wacom_ac_get_property;
10557dbd229eSBenjamin Tissoires 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1056297d716fSKrzysztof Kozlowski 		ac_desc->name = wacom->wacom_wac.ac_name;
1057297d716fSKrzysztof Kozlowski 		ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1058297d716fSKrzysztof Kozlowski 		ac_desc->use_for_apm = 0;
10597dbd229eSBenjamin Tissoires 
1060297d716fSKrzysztof Kozlowski 		wacom->battery = power_supply_register(&wacom->hdev->dev,
1061297d716fSKrzysztof Kozlowski 					      &wacom->battery_desc, &psy_cfg);
1062297d716fSKrzysztof Kozlowski 		if (IS_ERR(wacom->battery))
1063297d716fSKrzysztof Kozlowski 			return PTR_ERR(wacom->battery);
1064471d1714SBenjamin Tissoires 
1065297d716fSKrzysztof Kozlowski 		power_supply_powers(wacom->battery, &wacom->hdev->dev);
10667dbd229eSBenjamin Tissoires 
1067297d716fSKrzysztof Kozlowski 		wacom->ac = power_supply_register(&wacom->hdev->dev,
1068297d716fSKrzysztof Kozlowski 						  &wacom->ac_desc,
1069297d716fSKrzysztof Kozlowski 						  &psy_cfg);
1070297d716fSKrzysztof Kozlowski 		if (IS_ERR(wacom->ac)) {
1071297d716fSKrzysztof Kozlowski 			power_supply_unregister(wacom->battery);
1072297d716fSKrzysztof Kozlowski 			return PTR_ERR(wacom->ac);
1073471d1714SBenjamin Tissoires 		}
1074471d1714SBenjamin Tissoires 
1075297d716fSKrzysztof Kozlowski 		power_supply_powers(wacom->ac, &wacom->hdev->dev);
10767dbd229eSBenjamin Tissoires 	}
10777dbd229eSBenjamin Tissoires 
10787dbd229eSBenjamin Tissoires 	return 0;
1079471d1714SBenjamin Tissoires }
1080471d1714SBenjamin Tissoires 
1081471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1082471d1714SBenjamin Tissoires {
10838de29a35SLinus Torvalds 	if (wacom->battery) {
1084297d716fSKrzysztof Kozlowski 		power_supply_unregister(wacom->battery);
1085297d716fSKrzysztof Kozlowski 		wacom->battery = NULL;
1086297d716fSKrzysztof Kozlowski 		power_supply_unregister(wacom->ac);
1087297d716fSKrzysztof Kozlowski 		wacom->ac = NULL;
1088471d1714SBenjamin Tissoires 	}
1089471d1714SBenjamin Tissoires }
1090471d1714SBenjamin Tissoires 
1091f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1092f81a1295SBenjamin Tissoires 				struct device_attribute
1093f81a1295SBenjamin Tissoires 				*attr, char *buf)
1094f81a1295SBenjamin Tissoires {
1095f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1096f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1097f81a1295SBenjamin Tissoires 
1098f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1099f81a1295SBenjamin Tissoires }
1100f81a1295SBenjamin Tissoires 
1101f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1102f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1103f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1104f81a1295SBenjamin Tissoires {
1105f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1106f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1107f81a1295SBenjamin Tissoires 	u8 new_speed;
1108f81a1295SBenjamin Tissoires 
1109f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1110f81a1295SBenjamin Tissoires 		return -EINVAL;
1111f81a1295SBenjamin Tissoires 
1112f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1113f81a1295SBenjamin Tissoires 		return -EINVAL;
1114f81a1295SBenjamin Tissoires 
1115f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1116f81a1295SBenjamin Tissoires 
1117f81a1295SBenjamin Tissoires 	return count;
1118f81a1295SBenjamin Tissoires }
1119f81a1295SBenjamin Tissoires 
1120e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1121f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1122f81a1295SBenjamin Tissoires 
1123471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1124471d1714SBenjamin Tissoires {
1125471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1126471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1127471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1128471d1714SBenjamin Tissoires 
1129471d1714SBenjamin Tissoires 	input_dev = input_allocate_device();
1130471d1714SBenjamin Tissoires 	if (!input_dev)
1131471d1714SBenjamin Tissoires 		return NULL;
1132471d1714SBenjamin Tissoires 
11332a6cdbddSJason Gerecke 	input_dev->name = wacom_wac->pen_name;
1134471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1135471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1136471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1137471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1138471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1139471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1140471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
114112969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1142471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1143471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1144471d1714SBenjamin Tissoires 
1145471d1714SBenjamin Tissoires 	return input_dev;
1146471d1714SBenjamin Tissoires }
1147471d1714SBenjamin Tissoires 
11482546dacdSBenjamin Tissoires static void wacom_free_inputs(struct wacom *wacom)
1149471d1714SBenjamin Tissoires {
11502546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11512546dacdSBenjamin Tissoires 
11522a6cdbddSJason Gerecke 	if (wacom_wac->pen_input)
11532a6cdbddSJason Gerecke 		input_free_device(wacom_wac->pen_input);
11542a6cdbddSJason Gerecke 	if (wacom_wac->touch_input)
11552a6cdbddSJason Gerecke 		input_free_device(wacom_wac->touch_input);
11562546dacdSBenjamin Tissoires 	if (wacom_wac->pad_input)
11572546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->pad_input);
11582a6cdbddSJason Gerecke 	wacom_wac->pen_input = NULL;
11592a6cdbddSJason Gerecke 	wacom_wac->touch_input = NULL;
11602546dacdSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
11612546dacdSBenjamin Tissoires }
11622546dacdSBenjamin Tissoires 
11632546dacdSBenjamin Tissoires static int wacom_allocate_inputs(struct wacom *wacom)
11642546dacdSBenjamin Tissoires {
11652a6cdbddSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
11662546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11672546dacdSBenjamin Tissoires 
11682a6cdbddSJason Gerecke 	pen_input_dev = wacom_allocate_input(wacom);
11692a6cdbddSJason Gerecke 	touch_input_dev = wacom_allocate_input(wacom);
11702546dacdSBenjamin Tissoires 	pad_input_dev = wacom_allocate_input(wacom);
11712a6cdbddSJason Gerecke 	if (!pen_input_dev || !touch_input_dev || !pad_input_dev) {
11722546dacdSBenjamin Tissoires 		wacom_free_inputs(wacom);
11732546dacdSBenjamin Tissoires 		return -ENOMEM;
11742546dacdSBenjamin Tissoires 	}
11752546dacdSBenjamin Tissoires 
11762a6cdbddSJason Gerecke 	wacom_wac->pen_input = pen_input_dev;
11772a6cdbddSJason Gerecke 	wacom_wac->touch_input = touch_input_dev;
11782a6cdbddSJason Gerecke 	wacom_wac->touch_input->name = wacom_wac->touch_name;
11792546dacdSBenjamin Tissoires 	wacom_wac->pad_input = pad_input_dev;
11802546dacdSBenjamin Tissoires 	wacom_wac->pad_input->name = wacom_wac->pad_name;
11812546dacdSBenjamin Tissoires 
11822546dacdSBenjamin Tissoires 	return 0;
11832546dacdSBenjamin Tissoires }
11842546dacdSBenjamin Tissoires 
11852546dacdSBenjamin Tissoires static void wacom_clean_inputs(struct wacom *wacom)
11862546dacdSBenjamin Tissoires {
11872a6cdbddSJason Gerecke 	if (wacom->wacom_wac.pen_input) {
11882a6cdbddSJason Gerecke 		if (wacom->wacom_wac.pen_registered)
11892a6cdbddSJason Gerecke 			input_unregister_device(wacom->wacom_wac.pen_input);
11902546dacdSBenjamin Tissoires 		else
11912a6cdbddSJason Gerecke 			input_free_device(wacom->wacom_wac.pen_input);
11922a6cdbddSJason Gerecke 	}
11932a6cdbddSJason Gerecke 	if (wacom->wacom_wac.touch_input) {
11942a6cdbddSJason Gerecke 		if (wacom->wacom_wac.touch_registered)
11952a6cdbddSJason Gerecke 			input_unregister_device(wacom->wacom_wac.touch_input);
11962a6cdbddSJason Gerecke 		else
11972a6cdbddSJason Gerecke 			input_free_device(wacom->wacom_wac.touch_input);
11982546dacdSBenjamin Tissoires 	}
11992546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.pad_input) {
1200954df6adSPing Cheng 		if (wacom->wacom_wac.pad_registered)
1201471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.pad_input);
12022546dacdSBenjamin Tissoires 		else
12032546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.pad_input);
12042546dacdSBenjamin Tissoires 	}
12052a6cdbddSJason Gerecke 	wacom->wacom_wac.pen_input = NULL;
12062a6cdbddSJason Gerecke 	wacom->wacom_wac.touch_input = NULL;
1207471d1714SBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
1208912ca216SPing Cheng 	wacom_destroy_leds(wacom);
1209471d1714SBenjamin Tissoires }
1210471d1714SBenjamin Tissoires 
1211471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1212471d1714SBenjamin Tissoires {
12132a6cdbddSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1214471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
12152636a3f2SJason Gerecke 	int error = 0;
1216471d1714SBenjamin Tissoires 
12172a6cdbddSJason Gerecke 	pen_input_dev = wacom_wac->pen_input;
12182a6cdbddSJason Gerecke 	touch_input_dev = wacom_wac->touch_input;
12192546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1220471d1714SBenjamin Tissoires 
12212a6cdbddSJason Gerecke 	if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
12222546dacdSBenjamin Tissoires 		return -EINVAL;
1223471d1714SBenjamin Tissoires 
12242a6cdbddSJason Gerecke 	error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
12252a6cdbddSJason Gerecke 	if (error) {
12262a6cdbddSJason Gerecke 		/* no pen in use on this interface */
12272a6cdbddSJason Gerecke 		input_free_device(pen_input_dev);
12282a6cdbddSJason Gerecke 		wacom_wac->pen_input = NULL;
12292a6cdbddSJason Gerecke 		pen_input_dev = NULL;
12302a6cdbddSJason Gerecke 	} else {
12312a6cdbddSJason Gerecke 		error = input_register_device(pen_input_dev);
1232471d1714SBenjamin Tissoires 		if (error)
12332a6cdbddSJason Gerecke 			goto fail_register_pen_input;
12342a6cdbddSJason Gerecke 		wacom_wac->pen_registered = true;
12352a6cdbddSJason Gerecke 	}
12362a6cdbddSJason Gerecke 
12372a6cdbddSJason Gerecke 	error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
12382a6cdbddSJason Gerecke 	if (error) {
12392a6cdbddSJason Gerecke 		/* no touch in use on this interface */
12402a6cdbddSJason Gerecke 		input_free_device(touch_input_dev);
12412a6cdbddSJason Gerecke 		wacom_wac->touch_input = NULL;
12422a6cdbddSJason Gerecke 		touch_input_dev = NULL;
12432a6cdbddSJason Gerecke 	} else {
12442a6cdbddSJason Gerecke 		error = input_register_device(touch_input_dev);
12452a6cdbddSJason Gerecke 		if (error)
12462a6cdbddSJason Gerecke 			goto fail_register_touch_input;
12472a6cdbddSJason Gerecke 		wacom_wac->touch_registered = true;
124830ebc1aeSPing Cheng 	}
1249471d1714SBenjamin Tissoires 
1250471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1251471d1714SBenjamin Tissoires 	if (error) {
1252471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1253471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1254471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1255471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1256471d1714SBenjamin Tissoires 	} else {
1257471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1258471d1714SBenjamin Tissoires 		if (error)
12597fefeec5SBenjamin Tissoires 			goto fail_register_pad_input;
1260954df6adSPing Cheng 		wacom_wac->pad_registered = true;
1261912ca216SPing Cheng 
1262912ca216SPing Cheng 		error = wacom_initialize_leds(wacom);
1263912ca216SPing Cheng 		if (error)
12647fefeec5SBenjamin Tissoires 			goto fail_leds;
1265471d1714SBenjamin Tissoires 	}
1266471d1714SBenjamin Tissoires 
1267471d1714SBenjamin Tissoires 	return 0;
1268471d1714SBenjamin Tissoires 
12697fefeec5SBenjamin Tissoires fail_leds:
1270912ca216SPing Cheng 	input_unregister_device(pad_input_dev);
1271912ca216SPing Cheng 	pad_input_dev = NULL;
1272954df6adSPing Cheng 	wacom_wac->pad_registered = false;
12737fefeec5SBenjamin Tissoires fail_register_pad_input:
12740fd72ff9SDan Carpenter 	if (touch_input_dev)
12752a6cdbddSJason Gerecke 		input_unregister_device(touch_input_dev);
12762a6cdbddSJason Gerecke 	wacom_wac->touch_input = NULL;
12772a6cdbddSJason Gerecke 	wacom_wac->touch_registered = false;
12782a6cdbddSJason Gerecke fail_register_touch_input:
12790fd72ff9SDan Carpenter 	if (pen_input_dev)
12802a6cdbddSJason Gerecke 		input_unregister_device(pen_input_dev);
12812a6cdbddSJason Gerecke 	wacom_wac->pen_input = NULL;
12822a6cdbddSJason Gerecke 	wacom_wac->pen_registered = false;
12832a6cdbddSJason Gerecke fail_register_pen_input:
1284471d1714SBenjamin Tissoires 	return error;
1285471d1714SBenjamin Tissoires }
1286471d1714SBenjamin Tissoires 
1287*0be01712SJason Gerecke /*
1288*0be01712SJason Gerecke  * Not all devices report physical dimensions from HID.
1289*0be01712SJason Gerecke  * Compute the default from hardcoded logical dimension
1290*0be01712SJason Gerecke  * and resolution before driver overwrites them.
1291*0be01712SJason Gerecke  */
1292*0be01712SJason Gerecke static void wacom_set_default_phy(struct wacom_features *features)
1293*0be01712SJason Gerecke {
1294*0be01712SJason Gerecke 	if (features->x_resolution) {
1295*0be01712SJason Gerecke 		features->x_phy = (features->x_max * 100) /
1296*0be01712SJason Gerecke 					features->x_resolution;
1297*0be01712SJason Gerecke 		features->y_phy = (features->y_max * 100) /
1298*0be01712SJason Gerecke 					features->y_resolution;
1299*0be01712SJason Gerecke 	}
1300*0be01712SJason Gerecke }
1301*0be01712SJason Gerecke 
1302*0be01712SJason Gerecke static void wacom_calculate_res(struct wacom_features *features)
1303*0be01712SJason Gerecke {
1304*0be01712SJason Gerecke 	/* set unit to "100th of a mm" for devices not reported by HID */
1305*0be01712SJason Gerecke 	if (!features->unit) {
1306*0be01712SJason Gerecke 		features->unit = 0x11;
1307*0be01712SJason Gerecke 		features->unitExpo = -3;
1308*0be01712SJason Gerecke 	}
1309*0be01712SJason Gerecke 
1310*0be01712SJason Gerecke 	features->x_resolution = wacom_calc_hid_res(features->x_max,
1311*0be01712SJason Gerecke 						    features->x_phy,
1312*0be01712SJason Gerecke 						    features->unit,
1313*0be01712SJason Gerecke 						    features->unitExpo);
1314*0be01712SJason Gerecke 	features->y_resolution = wacom_calc_hid_res(features->y_max,
1315*0be01712SJason Gerecke 						    features->y_phy,
1316*0be01712SJason Gerecke 						    features->unit,
1317*0be01712SJason Gerecke 						    features->unitExpo);
1318*0be01712SJason Gerecke }
1319*0be01712SJason Gerecke 
1320471d1714SBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
1321471d1714SBenjamin Tissoires {
1322471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, work);
1323471d1714SBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
1324471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1325471d1714SBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
1326471d1714SBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
1327471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1328471d1714SBenjamin Tissoires 	int error;
1329471d1714SBenjamin Tissoires 
1330471d1714SBenjamin Tissoires 	/*
1331471d1714SBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
1332471d1714SBenjamin Tissoires 	 * remove any existing input and battery devices.
1333471d1714SBenjamin Tissoires 	 */
1334471d1714SBenjamin Tissoires 
1335471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1336471d1714SBenjamin Tissoires 
1337471d1714SBenjamin Tissoires 	/* Stylus interface */
1338471d1714SBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1339471d1714SBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
1340471d1714SBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
13412546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
1342471d1714SBenjamin Tissoires 
1343471d1714SBenjamin Tissoires 	/* Touch interface */
1344471d1714SBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1345471d1714SBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
1346471d1714SBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
13472546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1348471d1714SBenjamin Tissoires 
1349471d1714SBenjamin Tissoires 	if (wacom_wac->pid == 0) {
1350471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1351ac8d1010SBenjamin Tissoires 		wacom_wac1->shared->type = 0;
1352471d1714SBenjamin Tissoires 	} else {
1353471d1714SBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
1354471d1714SBenjamin Tissoires 
1355471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1356471d1714SBenjamin Tissoires 			 wacom_wac->pid);
1357471d1714SBenjamin Tissoires 
1358471d1714SBenjamin Tissoires 		while (id->bus) {
1359471d1714SBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1360471d1714SBenjamin Tissoires 			    id->product == wacom_wac->pid)
1361471d1714SBenjamin Tissoires 				break;
1362471d1714SBenjamin Tissoires 			id++;
1363471d1714SBenjamin Tissoires 		}
1364471d1714SBenjamin Tissoires 
1365471d1714SBenjamin Tissoires 		if (!id->bus) {
1366471d1714SBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1367471d1714SBenjamin Tissoires 			return;
1368471d1714SBenjamin Tissoires 		}
1369471d1714SBenjamin Tissoires 
1370471d1714SBenjamin Tissoires 		/* Stylus interface */
1371471d1714SBenjamin Tissoires 		wacom_wac1->features =
1372471d1714SBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
1373aa86b18cSJason Gerecke 		wacom_wac1->features.device_type |= WACOM_DEVICETYPE_PEN;
1374862cf553SJason Gerecke 		if (wacom_wac1->features.type != INTUOSHT &&
1375862cf553SJason Gerecke 		    wacom_wac1->features.type != BAMBOO_PT)
1376862cf553SJason Gerecke 			wacom_wac1->features.device_type |= WACOM_DEVICETYPE_PAD;
1377*0be01712SJason Gerecke 		wacom_set_default_phy(&wacom_wac1->features);
1378*0be01712SJason Gerecke 		wacom_calculate_res(&wacom_wac1->features);
13792a6cdbddSJason Gerecke 		snprintf(wacom_wac1->pen_name, WACOM_NAME_MAX, "%s (WL) Pen",
1380471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1381471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1382471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1383471d1714SBenjamin Tissoires 		wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1384471d1714SBenjamin Tissoires 		wacom_wac1->shared->type = wacom_wac1->features.type;
1385912ca216SPing Cheng 		wacom_wac1->pid = wacom_wac->pid;
13862546dacdSBenjamin Tissoires 		error = wacom_allocate_inputs(wacom1) ||
13872546dacdSBenjamin Tissoires 			wacom_register_inputs(wacom1);
1388471d1714SBenjamin Tissoires 		if (error)
1389471d1714SBenjamin Tissoires 			goto fail;
1390471d1714SBenjamin Tissoires 
1391471d1714SBenjamin Tissoires 		/* Touch interface */
1392471d1714SBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
1393471d1714SBenjamin Tissoires 		    wacom_wac1->features.type == INTUOSHT) {
1394471d1714SBenjamin Tissoires 			wacom_wac2->features =
1395471d1714SBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
1396471d1714SBenjamin Tissoires 			wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1397*0be01712SJason Gerecke 			wacom_set_default_phy(&wacom_wac2->features);
1398471d1714SBenjamin Tissoires 			wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1399*0be01712SJason Gerecke 			wacom_calculate_res(&wacom_wac2->features);
14002a6cdbddSJason Gerecke 			snprintf(wacom_wac2->touch_name, WACOM_NAME_MAX,
1401471d1714SBenjamin Tissoires 				 "%s (WL) Finger",wacom_wac2->features.name);
1402471d1714SBenjamin Tissoires 			snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1403471d1714SBenjamin Tissoires 				 "%s (WL) Pad",wacom_wac2->features.name);
1404862cf553SJason Gerecke 			if (wacom_wac1->features.touch_max)
1405862cf553SJason Gerecke 				wacom_wac2->features.device_type |= WACOM_DEVICETYPE_TOUCH;
1406862cf553SJason Gerecke 			if (wacom_wac1->features.type == INTUOSHT ||
1407862cf553SJason Gerecke 			    wacom_wac1->features.type == BAMBOO_PT)
1408862cf553SJason Gerecke 				wacom_wac2->features.device_type |= WACOM_DEVICETYPE_PAD;
1409912ca216SPing Cheng 			wacom_wac2->pid = wacom_wac->pid;
14102546dacdSBenjamin Tissoires 			error = wacom_allocate_inputs(wacom2) ||
14112546dacdSBenjamin Tissoires 				wacom_register_inputs(wacom2);
1412471d1714SBenjamin Tissoires 			if (error)
1413471d1714SBenjamin Tissoires 				goto fail;
1414471d1714SBenjamin Tissoires 
1415471d1714SBenjamin Tissoires 			if (wacom_wac1->features.type == INTUOSHT &&
1416471d1714SBenjamin Tissoires 			    wacom_wac1->features.touch_max)
14172a6cdbddSJason Gerecke 				wacom_wac->shared->touch_input = wacom_wac2->touch_input;
1418471d1714SBenjamin Tissoires 		}
1419471d1714SBenjamin Tissoires 
1420471d1714SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1421471d1714SBenjamin Tissoires 		if (error)
1422471d1714SBenjamin Tissoires 			goto fail;
1423471d1714SBenjamin Tissoires 	}
1424471d1714SBenjamin Tissoires 
1425471d1714SBenjamin Tissoires 	return;
1426471d1714SBenjamin Tissoires 
1427471d1714SBenjamin Tissoires fail:
14282546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
14292546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1430471d1714SBenjamin Tissoires 	return;
1431471d1714SBenjamin Tissoires }
1432471d1714SBenjamin Tissoires 
1433fce9957dSJason Gerecke void wacom_battery_work(struct work_struct *work)
1434fce9957dSJason Gerecke {
1435fce9957dSJason Gerecke 	struct wacom *wacom = container_of(work, struct wacom, work);
1436fce9957dSJason Gerecke 
1437fce9957dSJason Gerecke 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
14388de29a35SLinus Torvalds 	     !wacom->battery) {
1439fce9957dSJason Gerecke 		wacom_initialize_battery(wacom);
1440fce9957dSJason Gerecke 	}
1441fce9957dSJason Gerecke 	else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
14428de29a35SLinus Torvalds 		 wacom->battery) {
1443fce9957dSJason Gerecke 		wacom_destroy_battery(wacom);
1444fce9957dSJason Gerecke 	}
1445fce9957dSJason Gerecke }
1446fce9957dSJason Gerecke 
1447471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1448471d1714SBenjamin Tissoires {
1449471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1450471d1714SBenjamin Tissoires 	struct hid_report *report;
1451471d1714SBenjamin Tissoires 	size_t size = 0;
1452471d1714SBenjamin Tissoires 
1453471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1454471d1714SBenjamin Tissoires 
1455471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1456dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
1457471d1714SBenjamin Tissoires 		if (report_size > size)
1458471d1714SBenjamin Tissoires 			size = report_size;
1459471d1714SBenjamin Tissoires 	}
1460471d1714SBenjamin Tissoires 
1461471d1714SBenjamin Tissoires 	return size;
1462471d1714SBenjamin Tissoires }
1463471d1714SBenjamin Tissoires 
1464c24eab4eSPing Cheng static void wacom_update_name(struct wacom *wacom)
1465c24eab4eSPing Cheng {
1466c24eab4eSPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1467c24eab4eSPing Cheng 	struct wacom_features *features = &wacom_wac->features;
146844b5250bSJason Gerecke 	char name[WACOM_NAME_MAX];
1469c24eab4eSPing Cheng 
1470c24eab4eSPing Cheng 	/* Generic devices name unspecified */
1471c24eab4eSPing Cheng 	if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1472c24eab4eSPing Cheng 		if (strstr(wacom->hdev->name, "Wacom") ||
1473c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "wacom") ||
1474c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "WACOM")) {
1475c24eab4eSPing Cheng 			/* name is in HID descriptor, use it */
147644b5250bSJason Gerecke 			strlcpy(name, wacom->hdev->name, sizeof(name));
1477c24eab4eSPing Cheng 
1478c24eab4eSPing Cheng 			/* strip out excess whitespaces */
1479c24eab4eSPing Cheng 			while (1) {
148044b5250bSJason Gerecke 				char *gap = strstr(name, "  ");
1481c24eab4eSPing Cheng 				if (gap == NULL)
1482c24eab4eSPing Cheng 					break;
1483c24eab4eSPing Cheng 				/* shift everything including the terminator */
1484c24eab4eSPing Cheng 				memmove(gap, gap+1, strlen(gap));
1485c24eab4eSPing Cheng 			}
1486c24eab4eSPing Cheng 			/* get rid of trailing whitespace */
148744b5250bSJason Gerecke 			if (name[strlen(name)-1] == ' ')
148844b5250bSJason Gerecke 				name[strlen(name)-1] = '\0';
1489c24eab4eSPing Cheng 		} else {
1490c24eab4eSPing Cheng 			/* no meaningful name retrieved. use product ID */
149144b5250bSJason Gerecke 			snprintf(name, sizeof(name),
1492c24eab4eSPing Cheng 				 "%s %X", features->name, wacom->hdev->product);
1493c24eab4eSPing Cheng 		}
1494c24eab4eSPing Cheng 	} else {
149544b5250bSJason Gerecke 		strlcpy(name, features->name, sizeof(name));
1496c24eab4eSPing Cheng 	}
1497c24eab4eSPing Cheng 
1498c24eab4eSPing Cheng 	/* Append the device type to the name */
14992a6cdbddSJason Gerecke 	snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
15002a6cdbddSJason Gerecke 		"%s Pen", name);
15012a6cdbddSJason Gerecke 	snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
15022a6cdbddSJason Gerecke 		"%s Finger", name);
1503c24eab4eSPing Cheng 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
150444b5250bSJason Gerecke 		"%s Pad", name);
15058d80f790SJason Gerecke }
1506c24eab4eSPing Cheng 
1507471d1714SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
1508471d1714SBenjamin Tissoires 		const struct hid_device_id *id)
1509471d1714SBenjamin Tissoires {
1510471d1714SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1511471d1714SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
1512471d1714SBenjamin Tissoires 	struct wacom *wacom;
1513471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
1514471d1714SBenjamin Tissoires 	struct wacom_features *features;
1515471d1714SBenjamin Tissoires 	int error;
15167704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1517471d1714SBenjamin Tissoires 
1518471d1714SBenjamin Tissoires 	if (!id->driver_data)
1519471d1714SBenjamin Tissoires 		return -EINVAL;
1520471d1714SBenjamin Tissoires 
15218ffffd52SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
15228ffffd52SBenjamin Tissoires 
15235fcad167SBenjamin Tissoires 	/* hid-core sets this quirk for the boot interface */
15245fcad167SBenjamin Tissoires 	hdev->quirks &= ~HID_QUIRK_NOGET;
15255fcad167SBenjamin Tissoires 
1526471d1714SBenjamin Tissoires 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1527471d1714SBenjamin Tissoires 	if (!wacom)
1528471d1714SBenjamin Tissoires 		return -ENOMEM;
1529471d1714SBenjamin Tissoires 
1530471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
1531471d1714SBenjamin Tissoires 	wacom->hdev = hdev;
1532471d1714SBenjamin Tissoires 
1533471d1714SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
1534471d1714SBenjamin Tissoires 	error = hid_parse(hdev);
1535471d1714SBenjamin Tissoires 	if (error) {
1536471d1714SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
15377fefeec5SBenjamin Tissoires 		goto fail_parse;
1538471d1714SBenjamin Tissoires 	}
1539471d1714SBenjamin Tissoires 
1540471d1714SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
1541471d1714SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
1542471d1714SBenjamin Tissoires 	features = &wacom_wac->features;
1543471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1544471d1714SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX) {
1545471d1714SBenjamin Tissoires 		error = -EINVAL;
15467fefeec5SBenjamin Tissoires 		goto fail_pktlen;
1547471d1714SBenjamin Tissoires 	}
1548471d1714SBenjamin Tissoires 
1549471d1714SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
1550471d1714SBenjamin Tissoires 		error = -ENODEV;
15517fefeec5SBenjamin Tissoires 		goto fail_type;
1552471d1714SBenjamin Tissoires 	}
1553471d1714SBenjamin Tissoires 
1554471d1714SBenjamin Tissoires 	wacom->usbdev = dev;
1555471d1714SBenjamin Tissoires 	wacom->intf = intf;
1556471d1714SBenjamin Tissoires 	mutex_init(&wacom->lock);
1557471d1714SBenjamin Tissoires 	INIT_WORK(&wacom->work, wacom_wireless_work);
1558471d1714SBenjamin Tissoires 
1559494078b0SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1560494078b0SBenjamin Tissoires 		error = wacom_allocate_inputs(wacom);
1561494078b0SBenjamin Tissoires 		if (error)
1562494078b0SBenjamin Tissoires 			goto fail_allocate_inputs;
1563494078b0SBenjamin Tissoires 	}
1564494078b0SBenjamin Tissoires 
15658c97a765SBenjamin Tissoires 	/*
15668c97a765SBenjamin Tissoires 	 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
15678c97a765SBenjamin Tissoires 	 * into debug mode for the touch part.
15688c97a765SBenjamin Tissoires 	 * We ignore the other interfaces.
15698c97a765SBenjamin Tissoires 	 */
15708c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD) {
15718c97a765SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_PENABLED) {
15728c97a765SBenjamin Tissoires 			features->type = HID_GENERIC;
15738c97a765SBenjamin Tissoires 		} else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
15748c97a765SBenjamin Tissoires 			   (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
15758c97a765SBenjamin Tissoires 			error = -ENODEV;
15768c97a765SBenjamin Tissoires 			goto fail_shared_data;
15778c97a765SBenjamin Tissoires 		}
15788c97a765SBenjamin Tissoires 	}
15798c97a765SBenjamin Tissoires 
1580471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1581471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1582471d1714SBenjamin Tissoires 
1583471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1584471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
158542f4f272SPing Cheng 	wacom_setup_device_quirks(wacom);
1586042628abSJason Gerecke 
1587aa86b18cSJason Gerecke 	if (features->device_type == WACOM_DEVICETYPE_NONE &&
1588aa86b18cSJason Gerecke 	    features->type != WIRELESS) {
15898e116d31SJason Gerecke 		error = features->type == HID_GENERIC ? -ENODEV : 0;
15908e116d31SJason Gerecke 
1591042628abSJason Gerecke 		dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
15928e116d31SJason Gerecke 			 hdev->name,
15938e116d31SJason Gerecke 			 error ? "Ignoring" : "Assuming pen");
15948e116d31SJason Gerecke 
15958e116d31SJason Gerecke 		if (error)
15968e116d31SJason Gerecke 			goto fail_shared_data;
1597042628abSJason Gerecke 
1598aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
1599042628abSJason Gerecke 	}
1600042628abSJason Gerecke 
1601471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1602471d1714SBenjamin Tissoires 
1603c24eab4eSPing Cheng 	wacom_update_name(wacom);
1604471d1714SBenjamin Tissoires 
1605471d1714SBenjamin Tissoires 	error = wacom_add_shared_data(hdev);
1606471d1714SBenjamin Tissoires 	if (error)
16077fefeec5SBenjamin Tissoires 		goto fail_shared_data;
1608471d1714SBenjamin Tissoires 
1609f81a1295SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1610f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1611f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1612f81a1295SBenjamin Tissoires 		if (error)
16137fefeec5SBenjamin Tissoires 			goto fail_battery;
1614f81a1295SBenjamin Tissoires 	}
1615f81a1295SBenjamin Tissoires 
1616471d1714SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1617494078b0SBenjamin Tissoires 		error = wacom_register_inputs(wacom);
1618471d1714SBenjamin Tissoires 		if (error)
16197fefeec5SBenjamin Tissoires 			goto fail_register_inputs;
1620f81a1295SBenjamin Tissoires 	}
1621f81a1295SBenjamin Tissoires 
1622f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1623f81a1295SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
1624f81a1295SBenjamin Tissoires 		if (error)
1625f81a1295SBenjamin Tissoires 			hid_warn(hdev,
1626f81a1295SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
1627f81a1295SBenjamin Tissoires 				 error);
1628471d1714SBenjamin Tissoires 	}
1629471d1714SBenjamin Tissoires 
16307704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
16317704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
16327704ac93SBenjamin Tissoires 
1633471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
16347704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
1635471d1714SBenjamin Tissoires 	if (error) {
1636471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
16377fefeec5SBenjamin Tissoires 		goto fail_hw_start;
1638471d1714SBenjamin Tissoires 	}
1639471d1714SBenjamin Tissoires 
16405ae6e89fSBenjamin Tissoires 	/* Note that if query fails it is not a hard failure */
16415ae6e89fSBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
16425ae6e89fSBenjamin Tissoires 
1643471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MONITOR)
1644471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
1645471d1714SBenjamin Tissoires 
1646862cf553SJason Gerecke 	if (wacom_wac->features.type == INTUOSHT &&
1647862cf553SJason Gerecke 	    wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
16482a6cdbddSJason Gerecke 			wacom_wac->shared->touch_input = wacom_wac->touch_input;
1649471d1714SBenjamin Tissoires 	}
1650471d1714SBenjamin Tissoires 
1651471d1714SBenjamin Tissoires 	return 0;
1652471d1714SBenjamin Tissoires 
16537fefeec5SBenjamin Tissoires fail_hw_start:
16547fefeec5SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
16557fefeec5SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
16567fefeec5SBenjamin Tissoires fail_register_inputs:
16572546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
16587fefeec5SBenjamin Tissoires 	wacom_destroy_battery(wacom);
16597fefeec5SBenjamin Tissoires fail_battery:
1660a97ac104SBenjamin Tissoires 	wacom_remove_shared_data(wacom);
16617fefeec5SBenjamin Tissoires fail_shared_data:
1662494078b0SBenjamin Tissoires 	wacom_clean_inputs(wacom);
1663494078b0SBenjamin Tissoires fail_allocate_inputs:
16647fefeec5SBenjamin Tissoires fail_type:
16657fefeec5SBenjamin Tissoires fail_pktlen:
16667fefeec5SBenjamin Tissoires fail_parse:
16677fefeec5SBenjamin Tissoires 	kfree(wacom);
1668471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1669471d1714SBenjamin Tissoires 	return error;
1670471d1714SBenjamin Tissoires }
1671471d1714SBenjamin Tissoires 
1672471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
1673471d1714SBenjamin Tissoires {
1674471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1675471d1714SBenjamin Tissoires 
1676471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
1677471d1714SBenjamin Tissoires 
1678471d1714SBenjamin Tissoires 	cancel_work_sync(&wacom->work);
16792546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
1680f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1681f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
1682471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1683a97ac104SBenjamin Tissoires 	wacom_remove_shared_data(wacom);
1684471d1714SBenjamin Tissoires 
1685471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1686471d1714SBenjamin Tissoires 	kfree(wacom);
1687471d1714SBenjamin Tissoires }
1688471d1714SBenjamin Tissoires 
168941a74581SGeert Uytterhoeven #ifdef CONFIG_PM
1690471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
1691471d1714SBenjamin Tissoires {
1692471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1693471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1694471d1714SBenjamin Tissoires 
1695471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1696471d1714SBenjamin Tissoires 
1697471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
1698471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
1699471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1700471d1714SBenjamin Tissoires 
1701471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1702471d1714SBenjamin Tissoires 
1703471d1714SBenjamin Tissoires 	return 0;
1704471d1714SBenjamin Tissoires }
1705471d1714SBenjamin Tissoires 
1706471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
1707471d1714SBenjamin Tissoires {
1708471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
1709471d1714SBenjamin Tissoires }
171041a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
1711471d1714SBenjamin Tissoires 
1712471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
1713471d1714SBenjamin Tissoires 	.name =		"wacom",
1714471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
1715471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
1716471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
17177704ac93SBenjamin Tissoires 	.event =	wacom_wac_event,
17187704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
1719471d1714SBenjamin Tissoires #ifdef CONFIG_PM
1720471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
1721471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
1722471d1714SBenjamin Tissoires #endif
1723471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
1724471d1714SBenjamin Tissoires };
1725471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
1726f2e0a7d4SBenjamin Tissoires 
1727f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
1728f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
1729f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
1730f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
1731