• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * drivers/input/tablet/wacom_sys.c
3  *
4  *  USB Wacom tablet support - system specific code
5  */
6 
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include "wacom_wac.h"
15 #include "wacom.h"
16 
17 #define WAC_MSG_RETRIES		5
18 
19 #define WAC_CMD_WL_LED_CONTROL	0x03
20 #define WAC_CMD_LED_CONTROL	0x20
21 #define WAC_CMD_ICON_START	0x21
22 #define WAC_CMD_ICON_XFER	0x23
23 #define WAC_CMD_ICON_BT_XFER	0x26
24 #define WAC_CMD_RETRIES		10
25 
26 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
27 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
28 
wacom_get_report(struct hid_device * hdev,u8 type,u8 * buf,size_t size,unsigned int retries)29 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
30 			    size_t size, unsigned int retries)
31 {
32 	int retval;
33 
34 	do {
35 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
36 				HID_REQ_GET_REPORT);
37 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
38 
39 	return retval;
40 }
41 
wacom_set_report(struct hid_device * hdev,u8 type,u8 * buf,size_t size,unsigned int retries)42 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 			    size_t size, unsigned int retries)
44 {
45 	int retval;
46 
47 	do {
48 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
49 				HID_REQ_SET_REPORT);
50 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
51 
52 	return retval;
53 }
54 
wacom_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * raw_data,int size)55 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
56 		u8 *raw_data, int size)
57 {
58 	struct wacom *wacom = hid_get_drvdata(hdev);
59 
60 	if (size > WACOM_PKGLEN_MAX)
61 		return 1;
62 
63 	memcpy(wacom->wacom_wac.data, raw_data, size);
64 
65 	wacom_wac_irq(&wacom->wacom_wac, size);
66 
67 	return 0;
68 }
69 
wacom_open(struct input_dev * dev)70 static int wacom_open(struct input_dev *dev)
71 {
72 	struct wacom *wacom = input_get_drvdata(dev);
73 
74 	return hid_hw_open(wacom->hdev);
75 }
76 
wacom_close(struct input_dev * dev)77 static void wacom_close(struct input_dev *dev)
78 {
79 	struct wacom *wacom = input_get_drvdata(dev);
80 
81 	hid_hw_close(wacom->hdev);
82 }
83 
84 /*
85  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
86  */
wacom_calc_hid_res(int logical_extents,int physical_extents,unsigned unit,int exponent)87 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
88 			       unsigned unit, int exponent)
89 {
90 	struct hid_field field = {
91 		.logical_maximum = logical_extents,
92 		.physical_maximum = physical_extents,
93 		.unit = unit,
94 		.unit_exponent = exponent,
95 	};
96 
97 	return hidinput_calc_abs_res(&field, ABS_X);
98 }
99 
wacom_feature_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)100 static void wacom_feature_mapping(struct hid_device *hdev,
101 		struct hid_field *field, struct hid_usage *usage)
102 {
103 	struct wacom *wacom = hid_get_drvdata(hdev);
104 	struct wacom_features *features = &wacom->wacom_wac.features;
105 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
106 	u8 *data;
107 	int ret;
108 
109 	switch (usage->hid) {
110 	case HID_DG_CONTACTMAX:
111 		/* leave touch_max as is if predefined */
112 		if (!features->touch_max) {
113 			/* read manually */
114 			data = kzalloc(2, GFP_KERNEL);
115 			if (!data)
116 				break;
117 			data[0] = field->report->id;
118 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
119 						data, 2, 0);
120 			if (ret == 2)
121 				features->touch_max = data[1];
122 			kfree(data);
123 		}
124 		break;
125 	case HID_DG_INPUTMODE:
126 		/* Ignore if value index is out of bounds. */
127 		if (usage->usage_index >= field->report_count) {
128 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
129 			break;
130 		}
131 
132 		hid_data->inputmode = field->report->id;
133 		hid_data->inputmode_index = usage->usage_index;
134 		break;
135 	}
136 }
137 
138 /*
139  * Interface Descriptor of wacom devices can be incomplete and
140  * inconsistent so wacom_features table is used to store stylus
141  * device's packet lengths, various maximum values, and tablet
142  * resolution based on product ID's.
143  *
144  * For devices that contain 2 interfaces, wacom_features table is
145  * inaccurate for the touch interface.  Since the Interface Descriptor
146  * for touch interfaces has pretty complete data, this function exists
147  * to query tablet for this missing information instead of hard coding in
148  * an additional table.
149  *
150  * A typical Interface Descriptor for a stylus will contain a
151  * boot mouse application collection that is not of interest and this
152  * function will ignore it.
153  *
154  * It also contains a digitizer application collection that also is not
155  * of interest since any information it contains would be duplicate
156  * of what is in wacom_features. Usually it defines a report of an array
157  * of bytes that could be used as max length of the stylus packet returned.
158  * If it happens to define a Digitizer-Stylus Physical Collection then
159  * the X and Y logical values contain valid data but it is ignored.
160  *
161  * A typical Interface Descriptor for a touch interface will contain a
162  * Digitizer-Finger Physical Collection which will define both logical
163  * X/Y maximum as well as the physical size of tablet. Since touch
164  * interfaces haven't supported pressure or distance, this is enough
165  * information to override invalid values in the wacom_features table.
166  *
167  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
168  * data. We deal with them after returning from this function.
169  */
wacom_usage_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)170 static void wacom_usage_mapping(struct hid_device *hdev,
171 		struct hid_field *field, struct hid_usage *usage)
172 {
173 	struct wacom *wacom = hid_get_drvdata(hdev);
174 	struct wacom_features *features = &wacom->wacom_wac.features;
175 	bool finger = (field->logical == HID_DG_FINGER) ||
176 		      (field->physical == HID_DG_FINGER);
177 	bool pen = (field->logical == HID_DG_STYLUS) ||
178 		   (field->physical == HID_DG_STYLUS);
179 
180 	/*
181 	* Requiring Stylus Usage will ignore boot mouse
182 	* X/Y values and some cases of invalid Digitizer X/Y
183 	* values commonly reported.
184 	*/
185 	if (!pen && !finger)
186 		return;
187 
188 	if (finger && !features->touch_max)
189 		/* touch device at least supports one touch point */
190 		features->touch_max = 1;
191 
192 	switch (usage->hid) {
193 	case HID_GD_X:
194 		features->x_max = field->logical_maximum;
195 		if (finger) {
196 			features->device_type = BTN_TOOL_FINGER;
197 			features->x_phy = field->physical_maximum;
198 			if (features->type != BAMBOO_PT) {
199 				features->unit = field->unit;
200 				features->unitExpo = field->unit_exponent;
201 			}
202 		} else {
203 			features->device_type = BTN_TOOL_PEN;
204 		}
205 		break;
206 	case HID_GD_Y:
207 		features->y_max = field->logical_maximum;
208 		if (finger) {
209 			features->y_phy = field->physical_maximum;
210 			if (features->type != BAMBOO_PT) {
211 				features->unit = field->unit;
212 				features->unitExpo = field->unit_exponent;
213 			}
214 		}
215 		break;
216 	case HID_DG_TIPPRESSURE:
217 		if (pen)
218 			features->pressure_max = field->logical_maximum;
219 		break;
220 	}
221 
222 	if (features->type == HID_GENERIC)
223 		wacom_wac_usage_mapping(hdev, field, usage);
224 }
225 
wacom_parse_hid(struct hid_device * hdev,struct wacom_features * features)226 static void wacom_parse_hid(struct hid_device *hdev,
227 			   struct wacom_features *features)
228 {
229 	struct hid_report_enum *rep_enum;
230 	struct hid_report *hreport;
231 	int i, j;
232 
233 	/* check features first */
234 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
235 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
236 		for (i = 0; i < hreport->maxfield; i++) {
237 			/* Ignore if report count is out of bounds. */
238 			if (hreport->field[i]->report_count < 1)
239 				continue;
240 
241 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
242 				wacom_feature_mapping(hdev, hreport->field[i],
243 						hreport->field[i]->usage + j);
244 			}
245 		}
246 	}
247 
248 	/* now check the input usages */
249 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
250 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
251 
252 		if (!hreport->maxfield)
253 			continue;
254 
255 		for (i = 0; i < hreport->maxfield; i++)
256 			for (j = 0; j < hreport->field[i]->maxusage; j++)
257 				wacom_usage_mapping(hdev, hreport->field[i],
258 						hreport->field[i]->usage + j);
259 	}
260 }
261 
wacom_hid_set_device_mode(struct hid_device * hdev)262 static int wacom_hid_set_device_mode(struct hid_device *hdev)
263 {
264 	struct wacom *wacom = hid_get_drvdata(hdev);
265 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
266 	struct hid_report *r;
267 	struct hid_report_enum *re;
268 
269 	if (hid_data->inputmode < 0)
270 		return 0;
271 
272 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
273 	r = re->report_id_hash[hid_data->inputmode];
274 	if (r) {
275 		r->field[0]->value[hid_data->inputmode_index] = 2;
276 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
277 	}
278 	return 0;
279 }
280 
wacom_set_device_mode(struct hid_device * hdev,int report_id,int length,int mode)281 static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
282 		int length, int mode)
283 {
284 	unsigned char *rep_data;
285 	int error = -ENOMEM, limit = 0;
286 
287 	rep_data = kzalloc(length, GFP_KERNEL);
288 	if (!rep_data)
289 		return error;
290 
291 	do {
292 		rep_data[0] = report_id;
293 		rep_data[1] = mode;
294 
295 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
296 					 length, 1);
297 		if (error >= 0)
298 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
299 			                         rep_data, length, 1);
300 	} while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
301 
302 	kfree(rep_data);
303 
304 	return error < 0 ? error : 0;
305 }
306 
wacom_bt_query_tablet_data(struct hid_device * hdev,u8 speed,struct wacom_features * features)307 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
308 		struct wacom_features *features)
309 {
310 	struct wacom *wacom = hid_get_drvdata(hdev);
311 	int ret;
312 	u8 rep_data[2];
313 
314 	switch (features->type) {
315 	case GRAPHIRE_BT:
316 		rep_data[0] = 0x03;
317 		rep_data[1] = 0x00;
318 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
319 					3);
320 
321 		if (ret >= 0) {
322 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
323 			rep_data[1] = 0x00;
324 
325 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
326 						rep_data, 2, 3);
327 
328 			if (ret >= 0) {
329 				wacom->wacom_wac.bt_high_speed = speed;
330 				return 0;
331 			}
332 		}
333 
334 		/*
335 		 * Note that if the raw queries fail, it's not a hard failure
336 		 * and it is safe to continue
337 		 */
338 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
339 			 rep_data[0], ret);
340 		break;
341 	case INTUOS4WL:
342 		if (speed == 1)
343 			wacom->wacom_wac.bt_features &= ~0x20;
344 		else
345 			wacom->wacom_wac.bt_features |= 0x20;
346 
347 		rep_data[0] = 0x03;
348 		rep_data[1] = wacom->wacom_wac.bt_features;
349 
350 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
351 					1);
352 		if (ret >= 0)
353 			wacom->wacom_wac.bt_high_speed = speed;
354 		break;
355 	}
356 
357 	return 0;
358 }
359 
360 /*
361  * Switch the tablet into its most-capable mode. Wacom tablets are
362  * typically configured to power-up in a mode which sends mouse-like
363  * reports to the OS. To get absolute position, pressure data, etc.
364  * from the tablet, it is necessary to switch the tablet out of this
365  * mode and into one which sends the full range of tablet data.
366  */
wacom_query_tablet_data(struct hid_device * hdev,struct wacom_features * features)367 static int wacom_query_tablet_data(struct hid_device *hdev,
368 		struct wacom_features *features)
369 {
370 	if (hdev->bus == BUS_BLUETOOTH)
371 		return wacom_bt_query_tablet_data(hdev, 1, features);
372 
373 	if (features->type == HID_GENERIC)
374 		return wacom_hid_set_device_mode(hdev);
375 
376 	if (features->device_type == BTN_TOOL_FINGER) {
377 		if (features->type > TABLETPC) {
378 			/* MT Tablet PC touch */
379 			return wacom_set_device_mode(hdev, 3, 4, 4);
380 		}
381 		else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
382 			return wacom_set_device_mode(hdev, 18, 3, 2);
383 		}
384 	} else if (features->device_type == BTN_TOOL_PEN) {
385 		if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
386 			return wacom_set_device_mode(hdev, 2, 2, 2);
387 		}
388 	}
389 
390 	return 0;
391 }
392 
wacom_retrieve_hid_descriptor(struct hid_device * hdev,struct wacom_features * features)393 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
394 					 struct wacom_features *features)
395 {
396 	struct wacom *wacom = hid_get_drvdata(hdev);
397 	struct usb_interface *intf = wacom->intf;
398 
399 	/* default features */
400 	features->device_type = BTN_TOOL_PEN;
401 	features->x_fuzz = 4;
402 	features->y_fuzz = 4;
403 	features->pressure_fuzz = 0;
404 	features->distance_fuzz = 0;
405 
406 	/*
407 	 * The wireless device HID is basic and layout conflicts with
408 	 * other tablets (monitor and touch interface can look like pen).
409 	 * Skip the query for this type and modify defaults based on
410 	 * interface number.
411 	 */
412 	if (features->type == WIRELESS) {
413 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
414 			features->device_type = 0;
415 		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
416 			features->device_type = BTN_TOOL_FINGER;
417 			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
418 		}
419 	}
420 
421 	/* only devices that support touch need to retrieve the info */
422 	if (features->type < BAMBOO_PT)
423 		return;
424 
425 	wacom_parse_hid(hdev, features);
426 }
427 
428 struct wacom_hdev_data {
429 	struct list_head list;
430 	struct kref kref;
431 	struct hid_device *dev;
432 	struct wacom_shared shared;
433 };
434 
435 static LIST_HEAD(wacom_udev_list);
436 static DEFINE_MUTEX(wacom_udev_list_lock);
437 
wacom_are_sibling(struct hid_device * hdev,struct hid_device * sibling)438 static bool wacom_are_sibling(struct hid_device *hdev,
439 		struct hid_device *sibling)
440 {
441 	struct wacom *wacom = hid_get_drvdata(hdev);
442 	struct wacom_features *features = &wacom->wacom_wac.features;
443 	int vid = features->oVid;
444 	int pid = features->oPid;
445 	int n1,n2;
446 
447 	if (vid == 0 && pid == 0) {
448 		vid = hdev->vendor;
449 		pid = hdev->product;
450 	}
451 
452 	if (vid != sibling->vendor || pid != sibling->product)
453 		return false;
454 
455 	/* Compare the physical path. */
456 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
457 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
458 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
459 		return false;
460 
461 	return !strncmp(hdev->phys, sibling->phys, n1);
462 }
463 
wacom_get_hdev_data(struct hid_device * hdev)464 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
465 {
466 	struct wacom_hdev_data *data;
467 
468 	list_for_each_entry(data, &wacom_udev_list, list) {
469 		if (wacom_are_sibling(hdev, data->dev)) {
470 			kref_get(&data->kref);
471 			return data;
472 		}
473 	}
474 
475 	return NULL;
476 }
477 
wacom_add_shared_data(struct hid_device * hdev)478 static int wacom_add_shared_data(struct hid_device *hdev)
479 {
480 	struct wacom *wacom = hid_get_drvdata(hdev);
481 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
482 	struct wacom_hdev_data *data;
483 	int retval = 0;
484 
485 	mutex_lock(&wacom_udev_list_lock);
486 
487 	data = wacom_get_hdev_data(hdev);
488 	if (!data) {
489 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
490 		if (!data) {
491 			retval = -ENOMEM;
492 			goto out;
493 		}
494 
495 		kref_init(&data->kref);
496 		data->dev = hdev;
497 		list_add_tail(&data->list, &wacom_udev_list);
498 	}
499 
500 	wacom_wac->shared = &data->shared;
501 
502 out:
503 	mutex_unlock(&wacom_udev_list_lock);
504 	return retval;
505 }
506 
wacom_release_shared_data(struct kref * kref)507 static void wacom_release_shared_data(struct kref *kref)
508 {
509 	struct wacom_hdev_data *data =
510 		container_of(kref, struct wacom_hdev_data, kref);
511 
512 	mutex_lock(&wacom_udev_list_lock);
513 	list_del(&data->list);
514 	mutex_unlock(&wacom_udev_list_lock);
515 
516 	kfree(data);
517 }
518 
wacom_remove_shared_data(struct wacom_wac * wacom)519 static void wacom_remove_shared_data(struct wacom_wac *wacom)
520 {
521 	struct wacom_hdev_data *data;
522 
523 	if (wacom->shared) {
524 		data = container_of(wacom->shared, struct wacom_hdev_data, shared);
525 		kref_put(&data->kref, wacom_release_shared_data);
526 		wacom->shared = NULL;
527 	}
528 }
529 
wacom_led_control(struct wacom * wacom)530 static int wacom_led_control(struct wacom *wacom)
531 {
532 	unsigned char *buf;
533 	int retval;
534 	unsigned char report_id = WAC_CMD_LED_CONTROL;
535 	int buf_size = 9;
536 
537 	if (wacom->wacom_wac.pid) { /* wireless connected */
538 		report_id = WAC_CMD_WL_LED_CONTROL;
539 		buf_size = 13;
540 	}
541 	buf = kzalloc(buf_size, GFP_KERNEL);
542 	if (!buf)
543 		return -ENOMEM;
544 
545 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
546 	    wacom->wacom_wac.features.type <= INTUOSPL) {
547 		/*
548 		 * Touch Ring and crop mark LED luminance may take on
549 		 * one of four values:
550 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
551 		 */
552 		int ring_led = wacom->led.select[0] & 0x03;
553 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
554 		int crop_lum = 0;
555 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
556 
557 		buf[0] = report_id;
558 		if (wacom->wacom_wac.pid) {
559 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
560 					 buf, buf_size, WAC_CMD_RETRIES);
561 			buf[0] = report_id;
562 			buf[4] = led_bits;
563 		} else
564 			buf[1] = led_bits;
565 	}
566 	else {
567 		int led = wacom->led.select[0] | 0x4;
568 
569 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
570 		    wacom->wacom_wac.features.type == WACOM_24HD)
571 			led |= (wacom->led.select[1] << 4) | 0x40;
572 
573 		buf[0] = report_id;
574 		buf[1] = led;
575 		buf[2] = wacom->led.llv;
576 		buf[3] = wacom->led.hlv;
577 		buf[4] = wacom->led.img_lum;
578 	}
579 
580 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
581 				  WAC_CMD_RETRIES);
582 	kfree(buf);
583 
584 	return retval;
585 }
586 
wacom_led_putimage(struct wacom * wacom,int button_id,u8 xfer_id,const unsigned len,const void * img)587 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
588 		const unsigned len, const void *img)
589 {
590 	unsigned char *buf;
591 	int i, retval;
592 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
593 
594 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
595 	if (!buf)
596 		return -ENOMEM;
597 
598 	/* Send 'start' command */
599 	buf[0] = WAC_CMD_ICON_START;
600 	buf[1] = 1;
601 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
602 				  WAC_CMD_RETRIES);
603 	if (retval < 0)
604 		goto out;
605 
606 	buf[0] = xfer_id;
607 	buf[1] = button_id & 0x07;
608 	for (i = 0; i < 4; i++) {
609 		buf[2] = i;
610 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
611 
612 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
613 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
614 		if (retval < 0)
615 			break;
616 	}
617 
618 	/* Send 'stop' */
619 	buf[0] = WAC_CMD_ICON_START;
620 	buf[1] = 0;
621 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
622 			 WAC_CMD_RETRIES);
623 
624 out:
625 	kfree(buf);
626 	return retval;
627 }
628 
wacom_led_select_store(struct device * dev,int set_id,const char * buf,size_t count)629 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
630 				      const char *buf, size_t count)
631 {
632 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
633 	struct wacom *wacom = hid_get_drvdata(hdev);
634 	unsigned int id;
635 	int err;
636 
637 	err = kstrtouint(buf, 10, &id);
638 	if (err)
639 		return err;
640 
641 	mutex_lock(&wacom->lock);
642 
643 	wacom->led.select[set_id] = id & 0x3;
644 	err = wacom_led_control(wacom);
645 
646 	mutex_unlock(&wacom->lock);
647 
648 	return err < 0 ? err : count;
649 }
650 
651 #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
652 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
653 	struct device_attribute *attr, const char *buf, size_t count)	\
654 {									\
655 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
656 }									\
657 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
658 	struct device_attribute *attr, char *buf)			\
659 {									\
660 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
661 	struct wacom *wacom = hid_get_drvdata(hdev);			\
662 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
663 			 wacom->led.select[SET_ID]);			\
664 }									\
665 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
666 		    wacom_led##SET_ID##_select_show,			\
667 		    wacom_led##SET_ID##_select_store)
668 
669 DEVICE_LED_SELECT_ATTR(0);
670 DEVICE_LED_SELECT_ATTR(1);
671 
wacom_luminance_store(struct wacom * wacom,u8 * dest,const char * buf,size_t count)672 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
673 				     const char *buf, size_t count)
674 {
675 	unsigned int value;
676 	int err;
677 
678 	err = kstrtouint(buf, 10, &value);
679 	if (err)
680 		return err;
681 
682 	mutex_lock(&wacom->lock);
683 
684 	*dest = value & 0x7f;
685 	err = wacom_led_control(wacom);
686 
687 	mutex_unlock(&wacom->lock);
688 
689 	return err < 0 ? err : count;
690 }
691 
692 #define DEVICE_LUMINANCE_ATTR(name, field)				\
693 static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
694 	struct device_attribute *attr, const char *buf, size_t count)	\
695 {									\
696 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
697 	struct wacom *wacom = hid_get_drvdata(hdev);			\
698 									\
699 	return wacom_luminance_store(wacom, &wacom->led.field,		\
700 				     buf, count);			\
701 }									\
702 static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
703 	struct device_attribute *attr, char *buf)			\
704 {									\
705 	struct wacom *wacom = dev_get_drvdata(dev);			\
706 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
707 }									\
708 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
709 		   wacom_##name##_luminance_show,			\
710 		   wacom_##name##_luminance_store)
711 
712 DEVICE_LUMINANCE_ATTR(status0, llv);
713 DEVICE_LUMINANCE_ATTR(status1, hlv);
714 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
715 
wacom_button_image_store(struct device * dev,int button_id,const char * buf,size_t count)716 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
717 					const char *buf, size_t count)
718 {
719 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
720 	struct wacom *wacom = hid_get_drvdata(hdev);
721 	int err;
722 	unsigned len;
723 	u8 xfer_id;
724 
725 	if (hdev->bus == BUS_BLUETOOTH) {
726 		len = 256;
727 		xfer_id = WAC_CMD_ICON_BT_XFER;
728 	} else {
729 		len = 1024;
730 		xfer_id = WAC_CMD_ICON_XFER;
731 	}
732 
733 	if (count != len)
734 		return -EINVAL;
735 
736 	mutex_lock(&wacom->lock);
737 
738 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
739 
740 	mutex_unlock(&wacom->lock);
741 
742 	return err < 0 ? err : count;
743 }
744 
745 #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
746 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
747 	struct device_attribute *attr, const char *buf, size_t count)	\
748 {									\
749 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
750 }									\
751 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
752 		   NULL, wacom_btnimg##BUTTON_ID##_store)
753 
754 DEVICE_BTNIMG_ATTR(0);
755 DEVICE_BTNIMG_ATTR(1);
756 DEVICE_BTNIMG_ATTR(2);
757 DEVICE_BTNIMG_ATTR(3);
758 DEVICE_BTNIMG_ATTR(4);
759 DEVICE_BTNIMG_ATTR(5);
760 DEVICE_BTNIMG_ATTR(6);
761 DEVICE_BTNIMG_ATTR(7);
762 
763 static struct attribute *cintiq_led_attrs[] = {
764 	&dev_attr_status_led0_select.attr,
765 	&dev_attr_status_led1_select.attr,
766 	NULL
767 };
768 
769 static struct attribute_group cintiq_led_attr_group = {
770 	.name = "wacom_led",
771 	.attrs = cintiq_led_attrs,
772 };
773 
774 static struct attribute *intuos4_led_attrs[] = {
775 	&dev_attr_status0_luminance.attr,
776 	&dev_attr_status1_luminance.attr,
777 	&dev_attr_status_led0_select.attr,
778 	&dev_attr_buttons_luminance.attr,
779 	&dev_attr_button0_rawimg.attr,
780 	&dev_attr_button1_rawimg.attr,
781 	&dev_attr_button2_rawimg.attr,
782 	&dev_attr_button3_rawimg.attr,
783 	&dev_attr_button4_rawimg.attr,
784 	&dev_attr_button5_rawimg.attr,
785 	&dev_attr_button6_rawimg.attr,
786 	&dev_attr_button7_rawimg.attr,
787 	NULL
788 };
789 
790 static struct attribute_group intuos4_led_attr_group = {
791 	.name = "wacom_led",
792 	.attrs = intuos4_led_attrs,
793 };
794 
795 static struct attribute *intuos5_led_attrs[] = {
796 	&dev_attr_status0_luminance.attr,
797 	&dev_attr_status_led0_select.attr,
798 	NULL
799 };
800 
801 static struct attribute_group intuos5_led_attr_group = {
802 	.name = "wacom_led",
803 	.attrs = intuos5_led_attrs,
804 };
805 
wacom_initialize_leds(struct wacom * wacom)806 static int wacom_initialize_leds(struct wacom *wacom)
807 {
808 	int error;
809 
810 	/* Initialize default values */
811 	switch (wacom->wacom_wac.features.type) {
812 	case INTUOS4S:
813 	case INTUOS4:
814 	case INTUOS4WL:
815 	case INTUOS4L:
816 		wacom->led.select[0] = 0;
817 		wacom->led.select[1] = 0;
818 		wacom->led.llv = 10;
819 		wacom->led.hlv = 20;
820 		wacom->led.img_lum = 10;
821 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
822 					   &intuos4_led_attr_group);
823 		break;
824 
825 	case WACOM_24HD:
826 	case WACOM_21UX2:
827 		wacom->led.select[0] = 0;
828 		wacom->led.select[1] = 0;
829 		wacom->led.llv = 0;
830 		wacom->led.hlv = 0;
831 		wacom->led.img_lum = 0;
832 
833 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
834 					   &cintiq_led_attr_group);
835 		break;
836 
837 	case INTUOS5S:
838 	case INTUOS5:
839 	case INTUOS5L:
840 	case INTUOSPS:
841 	case INTUOSPM:
842 	case INTUOSPL:
843 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
844 			wacom->led.select[0] = 0;
845 			wacom->led.select[1] = 0;
846 			wacom->led.llv = 32;
847 			wacom->led.hlv = 0;
848 			wacom->led.img_lum = 0;
849 
850 			error = sysfs_create_group(&wacom->hdev->dev.kobj,
851 						  &intuos5_led_attr_group);
852 		} else
853 			return 0;
854 		break;
855 
856 	default:
857 		return 0;
858 	}
859 
860 	if (error) {
861 		hid_err(wacom->hdev,
862 			"cannot create sysfs group err: %d\n", error);
863 		return error;
864 	}
865 	wacom_led_control(wacom);
866 	wacom->led_initialized = true;
867 
868 	return 0;
869 }
870 
wacom_destroy_leds(struct wacom * wacom)871 static void wacom_destroy_leds(struct wacom *wacom)
872 {
873 	if (!wacom->led_initialized)
874 		return;
875 
876 	wacom->led_initialized = false;
877 
878 	switch (wacom->wacom_wac.features.type) {
879 	case INTUOS4S:
880 	case INTUOS4:
881 	case INTUOS4WL:
882 	case INTUOS4L:
883 		sysfs_remove_group(&wacom->hdev->dev.kobj,
884 				   &intuos4_led_attr_group);
885 		break;
886 
887 	case WACOM_24HD:
888 	case WACOM_21UX2:
889 		sysfs_remove_group(&wacom->hdev->dev.kobj,
890 				   &cintiq_led_attr_group);
891 		break;
892 
893 	case INTUOS5S:
894 	case INTUOS5:
895 	case INTUOS5L:
896 	case INTUOSPS:
897 	case INTUOSPM:
898 	case INTUOSPL:
899 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
900 			sysfs_remove_group(&wacom->hdev->dev.kobj,
901 					   &intuos5_led_attr_group);
902 		break;
903 	}
904 }
905 
906 static enum power_supply_property wacom_battery_props[] = {
907 	POWER_SUPPLY_PROP_STATUS,
908 	POWER_SUPPLY_PROP_SCOPE,
909 	POWER_SUPPLY_PROP_CAPACITY
910 };
911 
912 static enum power_supply_property wacom_ac_props[] = {
913 	POWER_SUPPLY_PROP_PRESENT,
914 	POWER_SUPPLY_PROP_ONLINE,
915 	POWER_SUPPLY_PROP_SCOPE,
916 };
917 
wacom_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)918 static int wacom_battery_get_property(struct power_supply *psy,
919 				      enum power_supply_property psp,
920 				      union power_supply_propval *val)
921 {
922 	struct wacom *wacom = container_of(psy, struct wacom, battery);
923 	int ret = 0;
924 
925 	switch (psp) {
926 		case POWER_SUPPLY_PROP_SCOPE:
927 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
928 			break;
929 		case POWER_SUPPLY_PROP_CAPACITY:
930 			val->intval =
931 				wacom->wacom_wac.battery_capacity;
932 			break;
933 		case POWER_SUPPLY_PROP_STATUS:
934 			if (wacom->wacom_wac.bat_charging)
935 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
936 			else if (wacom->wacom_wac.battery_capacity == 100 &&
937 				    wacom->wacom_wac.ps_connected)
938 				val->intval = POWER_SUPPLY_STATUS_FULL;
939 			else
940 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
941 			break;
942 		default:
943 			ret = -EINVAL;
944 			break;
945 	}
946 
947 	return ret;
948 }
949 
wacom_ac_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)950 static int wacom_ac_get_property(struct power_supply *psy,
951 				enum power_supply_property psp,
952 				union power_supply_propval *val)
953 {
954 	struct wacom *wacom = container_of(psy, struct wacom, ac);
955 	int ret = 0;
956 
957 	switch (psp) {
958 	case POWER_SUPPLY_PROP_PRESENT:
959 		/* fall through */
960 	case POWER_SUPPLY_PROP_ONLINE:
961 		val->intval = wacom->wacom_wac.ps_connected;
962 		break;
963 	case POWER_SUPPLY_PROP_SCOPE:
964 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
965 		break;
966 	default:
967 		ret = -EINVAL;
968 		break;
969 	}
970 	return ret;
971 }
972 
wacom_initialize_battery(struct wacom * wacom)973 static int wacom_initialize_battery(struct wacom *wacom)
974 {
975 	static atomic_t battery_no = ATOMIC_INIT(0);
976 	int error;
977 	unsigned long n;
978 
979 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
980 		n = atomic_inc_return(&battery_no) - 1;
981 
982 		wacom->battery.properties = wacom_battery_props;
983 		wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
984 		wacom->battery.get_property = wacom_battery_get_property;
985 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
986 		wacom->battery.name = wacom->wacom_wac.bat_name;
987 		wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
988 		wacom->battery.use_for_apm = 0;
989 
990 		wacom->ac.properties = wacom_ac_props;
991 		wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
992 		wacom->ac.get_property = wacom_ac_get_property;
993 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
994 		wacom->ac.name = wacom->wacom_wac.ac_name;
995 		wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
996 		wacom->ac.use_for_apm = 0;
997 
998 		error = power_supply_register(&wacom->hdev->dev,
999 					      &wacom->battery);
1000 		if (error)
1001 			return error;
1002 
1003 		power_supply_powers(&wacom->battery, &wacom->hdev->dev);
1004 
1005 		error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
1006 		if (error) {
1007 			power_supply_unregister(&wacom->battery);
1008 			return error;
1009 		}
1010 
1011 		power_supply_powers(&wacom->ac, &wacom->hdev->dev);
1012 	}
1013 
1014 	return 0;
1015 }
1016 
wacom_destroy_battery(struct wacom * wacom)1017 static void wacom_destroy_battery(struct wacom *wacom)
1018 {
1019 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1020 	     wacom->battery.dev) {
1021 		power_supply_unregister(&wacom->battery);
1022 		wacom->battery.dev = NULL;
1023 		power_supply_unregister(&wacom->ac);
1024 		wacom->ac.dev = NULL;
1025 	}
1026 }
1027 
wacom_show_speed(struct device * dev,struct device_attribute * attr,char * buf)1028 static ssize_t wacom_show_speed(struct device *dev,
1029 				struct device_attribute
1030 				*attr, char *buf)
1031 {
1032 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1033 	struct wacom *wacom = hid_get_drvdata(hdev);
1034 
1035 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1036 }
1037 
wacom_store_speed(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1038 static ssize_t wacom_store_speed(struct device *dev,
1039 				struct device_attribute *attr,
1040 				const char *buf, size_t count)
1041 {
1042 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1043 	struct wacom *wacom = hid_get_drvdata(hdev);
1044 	u8 new_speed;
1045 
1046 	if (kstrtou8(buf, 0, &new_speed))
1047 		return -EINVAL;
1048 
1049 	if (new_speed != 0 && new_speed != 1)
1050 		return -EINVAL;
1051 
1052 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1053 
1054 	return count;
1055 }
1056 
1057 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1058 		wacom_show_speed, wacom_store_speed);
1059 
wacom_allocate_input(struct wacom * wacom)1060 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1061 {
1062 	struct input_dev *input_dev;
1063 	struct hid_device *hdev = wacom->hdev;
1064 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1065 
1066 	input_dev = input_allocate_device();
1067 	if (!input_dev)
1068 		return NULL;
1069 
1070 	input_dev->name = wacom_wac->name;
1071 	input_dev->phys = hdev->phys;
1072 	input_dev->dev.parent = &hdev->dev;
1073 	input_dev->open = wacom_open;
1074 	input_dev->close = wacom_close;
1075 	input_dev->uniq = hdev->uniq;
1076 	input_dev->id.bustype = hdev->bus;
1077 	input_dev->id.vendor  = hdev->vendor;
1078 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1079 	input_dev->id.version = hdev->version;
1080 	input_set_drvdata(input_dev, wacom);
1081 
1082 	return input_dev;
1083 }
1084 
wacom_free_inputs(struct wacom * wacom)1085 static void wacom_free_inputs(struct wacom *wacom)
1086 {
1087 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1088 
1089 	if (wacom_wac->input)
1090 		input_free_device(wacom_wac->input);
1091 	if (wacom_wac->pad_input)
1092 		input_free_device(wacom_wac->pad_input);
1093 	wacom_wac->input = NULL;
1094 	wacom_wac->pad_input = NULL;
1095 }
1096 
wacom_allocate_inputs(struct wacom * wacom)1097 static int wacom_allocate_inputs(struct wacom *wacom)
1098 {
1099 	struct input_dev *input_dev, *pad_input_dev;
1100 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1101 
1102 	input_dev = wacom_allocate_input(wacom);
1103 	pad_input_dev = wacom_allocate_input(wacom);
1104 	if (!input_dev || !pad_input_dev) {
1105 		wacom_free_inputs(wacom);
1106 		return -ENOMEM;
1107 	}
1108 
1109 	wacom_wac->input = input_dev;
1110 	wacom_wac->pad_input = pad_input_dev;
1111 	wacom_wac->pad_input->name = wacom_wac->pad_name;
1112 
1113 	return 0;
1114 }
1115 
wacom_clean_inputs(struct wacom * wacom)1116 static void wacom_clean_inputs(struct wacom *wacom)
1117 {
1118 	if (wacom->wacom_wac.input) {
1119 		if (wacom->wacom_wac.input_registered)
1120 			input_unregister_device(wacom->wacom_wac.input);
1121 		else
1122 			input_free_device(wacom->wacom_wac.input);
1123 	}
1124 	if (wacom->wacom_wac.pad_input) {
1125 		if (wacom->wacom_wac.input_registered)
1126 			input_unregister_device(wacom->wacom_wac.pad_input);
1127 		else
1128 			input_free_device(wacom->wacom_wac.pad_input);
1129 	}
1130 	wacom->wacom_wac.input = NULL;
1131 	wacom->wacom_wac.pad_input = NULL;
1132 	wacom_destroy_leds(wacom);
1133 }
1134 
wacom_register_inputs(struct wacom * wacom)1135 static int wacom_register_inputs(struct wacom *wacom)
1136 {
1137 	struct input_dev *input_dev, *pad_input_dev;
1138 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1139 	int error;
1140 
1141 	input_dev = wacom_wac->input;
1142 	pad_input_dev = wacom_wac->pad_input;
1143 
1144 	if (!input_dev || !pad_input_dev)
1145 		return -EINVAL;
1146 
1147 	error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1148 	if (error)
1149 		return error;
1150 
1151 	error = input_register_device(input_dev);
1152 	if (error)
1153 		return error;
1154 
1155 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1156 	if (error) {
1157 		/* no pad in use on this interface */
1158 		input_free_device(pad_input_dev);
1159 		wacom_wac->pad_input = NULL;
1160 		pad_input_dev = NULL;
1161 	} else {
1162 		error = input_register_device(pad_input_dev);
1163 		if (error)
1164 			goto fail_register_pad_input;
1165 
1166 		error = wacom_initialize_leds(wacom);
1167 		if (error)
1168 			goto fail_leds;
1169 	}
1170 
1171 	wacom_wac->input_registered = true;
1172 
1173 	return 0;
1174 
1175 fail_leds:
1176 	input_unregister_device(pad_input_dev);
1177 	pad_input_dev = NULL;
1178 fail_register_pad_input:
1179 	input_unregister_device(input_dev);
1180 	wacom_wac->input = NULL;
1181 	return error;
1182 }
1183 
wacom_wireless_work(struct work_struct * work)1184 static void wacom_wireless_work(struct work_struct *work)
1185 {
1186 	struct wacom *wacom = container_of(work, struct wacom, work);
1187 	struct usb_device *usbdev = wacom->usbdev;
1188 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1189 	struct hid_device *hdev1, *hdev2;
1190 	struct wacom *wacom1, *wacom2;
1191 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1192 	int error;
1193 
1194 	/*
1195 	 * Regardless if this is a disconnect or a new tablet,
1196 	 * remove any existing input and battery devices.
1197 	 */
1198 
1199 	wacom_destroy_battery(wacom);
1200 
1201 	/* Stylus interface */
1202 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1203 	wacom1 = hid_get_drvdata(hdev1);
1204 	wacom_wac1 = &(wacom1->wacom_wac);
1205 	wacom_clean_inputs(wacom1);
1206 
1207 	/* Touch interface */
1208 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1209 	wacom2 = hid_get_drvdata(hdev2);
1210 	wacom_wac2 = &(wacom2->wacom_wac);
1211 	wacom_clean_inputs(wacom2);
1212 
1213 	if (wacom_wac->pid == 0) {
1214 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1215 		wacom_wac1->shared->type = 0;
1216 	} else {
1217 		const struct hid_device_id *id = wacom_ids;
1218 
1219 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1220 			 wacom_wac->pid);
1221 
1222 		while (id->bus) {
1223 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1224 			    id->product == wacom_wac->pid)
1225 				break;
1226 			id++;
1227 		}
1228 
1229 		if (!id->bus) {
1230 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1231 			return;
1232 		}
1233 
1234 		/* Stylus interface */
1235 		wacom_wac1->features =
1236 			*((struct wacom_features *)id->driver_data);
1237 		wacom_wac1->features.device_type = BTN_TOOL_PEN;
1238 		snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1239 			 wacom_wac1->features.name);
1240 		snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1241 			 wacom_wac1->features.name);
1242 		wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1243 		wacom_wac1->shared->type = wacom_wac1->features.type;
1244 		wacom_wac1->pid = wacom_wac->pid;
1245 		error = wacom_allocate_inputs(wacom1) ||
1246 			wacom_register_inputs(wacom1);
1247 		if (error)
1248 			goto fail;
1249 
1250 		/* Touch interface */
1251 		if (wacom_wac1->features.touch_max ||
1252 		    wacom_wac1->features.type == INTUOSHT) {
1253 			wacom_wac2->features =
1254 				*((struct wacom_features *)id->driver_data);
1255 			wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1256 			wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1257 			wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1258 			if (wacom_wac2->features.touch_max)
1259 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1260 					 "%s (WL) Finger",wacom_wac2->features.name);
1261 			else
1262 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1263 					 "%s (WL) Pad",wacom_wac2->features.name);
1264 			snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1265 				 "%s (WL) Pad", wacom_wac2->features.name);
1266 			wacom_wac2->pid = wacom_wac->pid;
1267 			error = wacom_allocate_inputs(wacom2) ||
1268 				wacom_register_inputs(wacom2);
1269 			if (error)
1270 				goto fail;
1271 
1272 			if (wacom_wac1->features.type == INTUOSHT &&
1273 			    wacom_wac1->features.touch_max)
1274 				wacom_wac->shared->touch_input = wacom_wac2->input;
1275 		}
1276 
1277 		error = wacom_initialize_battery(wacom);
1278 		if (error)
1279 			goto fail;
1280 	}
1281 
1282 	return;
1283 
1284 fail:
1285 	wacom_clean_inputs(wacom1);
1286 	wacom_clean_inputs(wacom2);
1287 	return;
1288 }
1289 
1290 /*
1291  * Not all devices report physical dimensions from HID.
1292  * Compute the default from hardcoded logical dimension
1293  * and resolution before driver overwrites them.
1294  */
wacom_set_default_phy(struct wacom_features * features)1295 static void wacom_set_default_phy(struct wacom_features *features)
1296 {
1297 	if (features->x_resolution) {
1298 		features->x_phy = (features->x_max * 100) /
1299 					features->x_resolution;
1300 		features->y_phy = (features->y_max * 100) /
1301 					features->y_resolution;
1302 	}
1303 }
1304 
wacom_calculate_res(struct wacom_features * features)1305 static void wacom_calculate_res(struct wacom_features *features)
1306 {
1307 	features->x_resolution = wacom_calc_hid_res(features->x_max,
1308 						    features->x_phy,
1309 						    features->unit,
1310 						    features->unitExpo);
1311 	features->y_resolution = wacom_calc_hid_res(features->y_max,
1312 						    features->y_phy,
1313 						    features->unit,
1314 						    features->unitExpo);
1315 }
1316 
wacom_hid_report_len(struct hid_report * report)1317 static int wacom_hid_report_len(struct hid_report *report)
1318 {
1319 	/* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1320 	return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1321 }
1322 
wacom_compute_pktlen(struct hid_device * hdev)1323 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1324 {
1325 	struct hid_report_enum *report_enum;
1326 	struct hid_report *report;
1327 	size_t size = 0;
1328 
1329 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1330 
1331 	list_for_each_entry(report, &report_enum->report_list, list) {
1332 		size_t report_size = wacom_hid_report_len(report);
1333 		if (report_size > size)
1334 			size = report_size;
1335 	}
1336 
1337 	return size;
1338 }
1339 
wacom_probe(struct hid_device * hdev,const struct hid_device_id * id)1340 static int wacom_probe(struct hid_device *hdev,
1341 		const struct hid_device_id *id)
1342 {
1343 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1344 	struct usb_device *dev = interface_to_usbdev(intf);
1345 	struct wacom *wacom;
1346 	struct wacom_wac *wacom_wac;
1347 	struct wacom_features *features;
1348 	int error;
1349 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1350 
1351 	if (!id->driver_data)
1352 		return -EINVAL;
1353 
1354 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1355 
1356 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1357 	if (!wacom)
1358 		return -ENOMEM;
1359 
1360 	hid_set_drvdata(hdev, wacom);
1361 	wacom->hdev = hdev;
1362 
1363 	/* ask for the report descriptor to be loaded by HID */
1364 	error = hid_parse(hdev);
1365 	if (error) {
1366 		hid_err(hdev, "parse failed\n");
1367 		goto fail_parse;
1368 	}
1369 
1370 	wacom_wac = &wacom->wacom_wac;
1371 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
1372 	features = &wacom_wac->features;
1373 	features->pktlen = wacom_compute_pktlen(hdev);
1374 	if (features->pktlen > WACOM_PKGLEN_MAX) {
1375 		error = -EINVAL;
1376 		goto fail_pktlen;
1377 	}
1378 
1379 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
1380 		error = -ENODEV;
1381 		goto fail_type;
1382 	}
1383 
1384 	wacom->usbdev = dev;
1385 	wacom->intf = intf;
1386 	mutex_init(&wacom->lock);
1387 	INIT_WORK(&wacom->work, wacom_wireless_work);
1388 
1389 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1390 		error = wacom_allocate_inputs(wacom);
1391 		if (error)
1392 			goto fail_allocate_inputs;
1393 	}
1394 
1395 	/* set the default size in case we do not get them from hid */
1396 	wacom_set_default_phy(features);
1397 
1398 	/* Retrieve the physical and logical size for touch devices */
1399 	wacom_retrieve_hid_descriptor(hdev, features);
1400 
1401 	/*
1402 	 * Intuos5 has no useful data about its touch interface in its
1403 	 * HID descriptor. If this is the touch interface (PacketSize
1404 	 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1405 	 */
1406 	if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1407 		if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1408 			features->device_type = BTN_TOOL_FINGER;
1409 
1410 			features->x_max = 4096;
1411 			features->y_max = 4096;
1412 		} else {
1413 			features->device_type = BTN_TOOL_PEN;
1414 		}
1415 	}
1416 
1417 	/*
1418 	 * Same thing for Bamboo 3rd gen.
1419 	 */
1420 	if ((features->type == BAMBOO_PT) &&
1421 	    (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1422 	    (features->device_type == BTN_TOOL_PEN)) {
1423 		features->device_type = BTN_TOOL_FINGER;
1424 
1425 		features->x_max = 4096;
1426 		features->y_max = 4096;
1427 	}
1428 
1429 	if (hdev->bus == BUS_BLUETOOTH)
1430 		features->quirks |= WACOM_QUIRK_BATTERY;
1431 
1432 	wacom_setup_device_quirks(features);
1433 
1434 	/* set unit to "100th of a mm" for devices not reported by HID */
1435 	if (!features->unit) {
1436 		features->unit = 0x11;
1437 		features->unitExpo = -3;
1438 	}
1439 	wacom_calculate_res(features);
1440 
1441 	strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1442 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1443 		"%s Pad", features->name);
1444 
1445 	if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1446 		/* Append the device type to the name */
1447 		if (features->device_type != BTN_TOOL_FINGER)
1448 			strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1449 		else if (features->touch_max)
1450 			strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1451 		else
1452 			strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1453 
1454 		error = wacom_add_shared_data(hdev);
1455 		if (error)
1456 			goto fail_shared_data;
1457 	}
1458 
1459 	if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1460 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1461 		error = wacom_initialize_battery(wacom);
1462 		if (error)
1463 			goto fail_battery;
1464 	}
1465 
1466 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1467 		error = wacom_register_inputs(wacom);
1468 		if (error)
1469 			goto fail_register_inputs;
1470 	}
1471 
1472 	if (hdev->bus == BUS_BLUETOOTH) {
1473 		error = device_create_file(&hdev->dev, &dev_attr_speed);
1474 		if (error)
1475 			hid_warn(hdev,
1476 				 "can't create sysfs speed attribute err: %d\n",
1477 				 error);
1478 	}
1479 
1480 	if (features->type == HID_GENERIC)
1481 		connect_mask |= HID_CONNECT_DRIVER;
1482 
1483 	/* Regular HID work starts now */
1484 	error = hid_hw_start(hdev, connect_mask);
1485 	if (error) {
1486 		hid_err(hdev, "hw start failed\n");
1487 		goto fail_hw_start;
1488 	}
1489 
1490 	/* Note that if query fails it is not a hard failure */
1491 	wacom_query_tablet_data(hdev, features);
1492 
1493 	if (features->quirks & WACOM_QUIRK_MONITOR)
1494 		error = hid_hw_open(hdev);
1495 
1496 	if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1497 		if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1498 			wacom_wac->shared->touch_input = wacom_wac->input;
1499 	}
1500 
1501 	return 0;
1502 
1503 fail_hw_start:
1504 	if (hdev->bus == BUS_BLUETOOTH)
1505 		device_remove_file(&hdev->dev, &dev_attr_speed);
1506 fail_register_inputs:
1507 	wacom_clean_inputs(wacom);
1508 	wacom_destroy_battery(wacom);
1509 fail_battery:
1510 	wacom_remove_shared_data(wacom_wac);
1511 fail_shared_data:
1512 	wacom_clean_inputs(wacom);
1513 fail_allocate_inputs:
1514 fail_type:
1515 fail_pktlen:
1516 fail_parse:
1517 	kfree(wacom);
1518 	hid_set_drvdata(hdev, NULL);
1519 	return error;
1520 }
1521 
wacom_remove(struct hid_device * hdev)1522 static void wacom_remove(struct hid_device *hdev)
1523 {
1524 	struct wacom *wacom = hid_get_drvdata(hdev);
1525 
1526 	hid_hw_stop(hdev);
1527 
1528 	cancel_work_sync(&wacom->work);
1529 	wacom_clean_inputs(wacom);
1530 	if (hdev->bus == BUS_BLUETOOTH)
1531 		device_remove_file(&hdev->dev, &dev_attr_speed);
1532 	wacom_destroy_battery(wacom);
1533 	wacom_remove_shared_data(&wacom->wacom_wac);
1534 
1535 	hid_set_drvdata(hdev, NULL);
1536 	kfree(wacom);
1537 }
1538 
1539 #ifdef CONFIG_PM
wacom_resume(struct hid_device * hdev)1540 static int wacom_resume(struct hid_device *hdev)
1541 {
1542 	struct wacom *wacom = hid_get_drvdata(hdev);
1543 	struct wacom_features *features = &wacom->wacom_wac.features;
1544 
1545 	mutex_lock(&wacom->lock);
1546 
1547 	/* switch to wacom mode first */
1548 	wacom_query_tablet_data(hdev, features);
1549 	wacom_led_control(wacom);
1550 
1551 	mutex_unlock(&wacom->lock);
1552 
1553 	return 0;
1554 }
1555 
wacom_reset_resume(struct hid_device * hdev)1556 static int wacom_reset_resume(struct hid_device *hdev)
1557 {
1558 	return wacom_resume(hdev);
1559 }
1560 #endif /* CONFIG_PM */
1561 
1562 static struct hid_driver wacom_driver = {
1563 	.name =		"wacom",
1564 	.id_table =	wacom_ids,
1565 	.probe =	wacom_probe,
1566 	.remove =	wacom_remove,
1567 	.event =	wacom_wac_event,
1568 	.report =	wacom_wac_report,
1569 #ifdef CONFIG_PM
1570 	.resume =	wacom_resume,
1571 	.reset_resume =	wacom_reset_resume,
1572 #endif
1573 	.raw_event =	wacom_raw_event,
1574 };
1575 module_hid_driver(wacom_driver);
1576 
1577 MODULE_VERSION(DRIVER_VERSION);
1578 MODULE_AUTHOR(DRIVER_AUTHOR);
1579 MODULE_DESCRIPTION(DRIVER_DESC);
1580 MODULE_LICENSE(DRIVER_LICENSE);
1581