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 /* defines to get HID report descriptor */
18 #define HID_DEVICET_HID		(USB_TYPE_CLASS | 0x01)
19 #define HID_DEVICET_REPORT	(USB_TYPE_CLASS | 0x02)
20 #define HID_USAGE_UNDEFINED		0x00
21 #define HID_USAGE_PAGE			0x05
22 #define HID_USAGE_PAGE_DIGITIZER	0x0d
23 #define HID_USAGE_PAGE_DESKTOP		0x01
24 #define HID_USAGE			0x09
25 #define HID_USAGE_X			0x30
26 #define HID_USAGE_Y			0x31
27 #define HID_USAGE_X_TILT		0x3d
28 #define HID_USAGE_Y_TILT		0x3e
29 #define HID_USAGE_FINGER		0x22
30 #define HID_USAGE_STYLUS		0x20
31 #define HID_COLLECTION			0xa1
32 #define HID_COLLECTION_LOGICAL		0x02
33 #define HID_COLLECTION_END		0xc0
34 
35 enum {
36 	WCM_UNDEFINED = 0,
37 	WCM_DESKTOP,
38 	WCM_DIGITIZER,
39 };
40 
41 struct hid_descriptor {
42 	struct usb_descriptor_header header;
43 	__le16   bcdHID;
44 	u8       bCountryCode;
45 	u8       bNumDescriptors;
46 	u8       bDescriptorType;
47 	__le16   wDescriptorLength;
48 } __attribute__ ((packed));
49 
50 /* defines to get/set USB message */
51 #define USB_REQ_GET_REPORT	0x01
52 #define USB_REQ_SET_REPORT	0x09
53 
54 #define WAC_HID_FEATURE_REPORT	0x03
55 #define WAC_MSG_RETRIES		5
56 
57 #define WAC_CMD_LED_CONTROL	0x20
58 #define WAC_CMD_ICON_START	0x21
59 #define WAC_CMD_ICON_XFER	0x23
60 #define WAC_CMD_RETRIES		10
61 
wacom_get_report(struct usb_interface * intf,u8 type,u8 id,void * buf,size_t size,unsigned int retries)62 static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
63 			    void *buf, size_t size, unsigned int retries)
64 {
65 	struct usb_device *dev = interface_to_usbdev(intf);
66 	int retval;
67 
68 	do {
69 		retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
70 				USB_REQ_GET_REPORT,
71 				USB_DIR_IN | USB_TYPE_CLASS |
72 				USB_RECIP_INTERFACE,
73 				(type << 8) + id,
74 				intf->altsetting[0].desc.bInterfaceNumber,
75 				buf, size, 100);
76 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
77 
78 	return retval;
79 }
80 
wacom_set_report(struct usb_interface * intf,u8 type,u8 id,void * buf,size_t size,unsigned int retries)81 static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
82 			    void *buf, size_t size, unsigned int retries)
83 {
84 	struct usb_device *dev = interface_to_usbdev(intf);
85 	int retval;
86 
87 	do {
88 		retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
89 				USB_REQ_SET_REPORT,
90 				USB_TYPE_CLASS | USB_RECIP_INTERFACE,
91 				(type << 8) + id,
92 				intf->altsetting[0].desc.bInterfaceNumber,
93 				buf, size, 1000);
94 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
95 
96 	return retval;
97 }
98 
wacom_sys_irq(struct urb * urb)99 static void wacom_sys_irq(struct urb *urb)
100 {
101 	struct wacom *wacom = urb->context;
102 	int retval;
103 
104 	switch (urb->status) {
105 	case 0:
106 		/* success */
107 		break;
108 	case -ECONNRESET:
109 	case -ENOENT:
110 	case -ESHUTDOWN:
111 		/* this urb is terminated, clean up */
112 		dbg("%s - urb shutting down with status: %d", __func__, urb->status);
113 		return;
114 	default:
115 		dbg("%s - nonzero urb status received: %d", __func__, urb->status);
116 		goto exit;
117 	}
118 
119 	wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
120 
121  exit:
122 	usb_mark_last_busy(wacom->usbdev);
123 	retval = usb_submit_urb(urb, GFP_ATOMIC);
124 	if (retval)
125 		err ("%s - usb_submit_urb failed with result %d",
126 		     __func__, retval);
127 }
128 
wacom_open(struct input_dev * dev)129 static int wacom_open(struct input_dev *dev)
130 {
131 	struct wacom *wacom = input_get_drvdata(dev);
132 	int retval = 0;
133 
134 	if (usb_autopm_get_interface(wacom->intf) < 0)
135 		return -EIO;
136 
137 	mutex_lock(&wacom->lock);
138 
139 	if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
140 		retval = -EIO;
141 		goto out;
142 	}
143 
144 	wacom->open = true;
145 	wacom->intf->needs_remote_wakeup = 1;
146 
147 out:
148 	mutex_unlock(&wacom->lock);
149 	usb_autopm_put_interface(wacom->intf);
150 	return retval;
151 }
152 
wacom_close(struct input_dev * dev)153 static void wacom_close(struct input_dev *dev)
154 {
155 	struct wacom *wacom = input_get_drvdata(dev);
156 	int autopm_error;
157 
158 	autopm_error = usb_autopm_get_interface(wacom->intf);
159 
160 	mutex_lock(&wacom->lock);
161 	usb_kill_urb(wacom->irq);
162 	wacom->open = false;
163 	wacom->intf->needs_remote_wakeup = 0;
164 	mutex_unlock(&wacom->lock);
165 
166 	if (!autopm_error)
167 		usb_autopm_put_interface(wacom->intf);
168 }
169 
170 /*
171  * Static values for max X/Y and resolution of Pen interface is stored in
172  * features. This mean physical size of active area can be computed.
173  * This is useful to do when Pen and Touch have same active area of tablet.
174  * This means for Touch device, we only need to find max X/Y value and we
175  * have enough information to compute resolution of touch.
176  */
wacom_set_phy_from_res(struct wacom_features * features)177 static void wacom_set_phy_from_res(struct wacom_features *features)
178 {
179 	features->x_phy = (features->x_max * 100) / features->x_resolution;
180 	features->y_phy = (features->y_max * 100) / features->y_resolution;
181 }
182 
wacom_parse_logical_collection(unsigned char * report,struct wacom_features * features)183 static int wacom_parse_logical_collection(unsigned char *report,
184 					  struct wacom_features *features)
185 {
186 	int length = 0;
187 
188 	if (features->type == BAMBOO_PT) {
189 
190 		/* Logical collection is only used by 3rd gen Bamboo Touch */
191 		features->pktlen = WACOM_PKGLEN_BBTOUCH3;
192 		features->device_type = BTN_TOOL_FINGER;
193 
194 		wacom_set_phy_from_res(features);
195 
196 		features->x_max = features->y_max =
197 			get_unaligned_le16(&report[10]);
198 
199 		length = 11;
200 	}
201 	return length;
202 }
203 
204 /*
205  * Interface Descriptor of wacom devices can be incomplete and
206  * inconsistent so wacom_features table is used to store stylus
207  * device's packet lengths, various maximum values, and tablet
208  * resolution based on product ID's.
209  *
210  * For devices that contain 2 interfaces, wacom_features table is
211  * inaccurate for the touch interface.  Since the Interface Descriptor
212  * for touch interfaces has pretty complete data, this function exists
213  * to query tablet for this missing information instead of hard coding in
214  * an additional table.
215  *
216  * A typical Interface Descriptor for a stylus will contain a
217  * boot mouse application collection that is not of interest and this
218  * function will ignore it.
219  *
220  * It also contains a digitizer application collection that also is not
221  * of interest since any information it contains would be duplicate
222  * of what is in wacom_features. Usually it defines a report of an array
223  * of bytes that could be used as max length of the stylus packet returned.
224  * If it happens to define a Digitizer-Stylus Physical Collection then
225  * the X and Y logical values contain valid data but it is ignored.
226  *
227  * A typical Interface Descriptor for a touch interface will contain a
228  * Digitizer-Finger Physical Collection which will define both logical
229  * X/Y maximum as well as the physical size of tablet. Since touch
230  * interfaces haven't supported pressure or distance, this is enough
231  * information to override invalid values in the wacom_features table.
232  *
233  * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
234  * Collection. Instead they define a Logical Collection with a single
235  * Logical Maximum for both X and Y.
236  */
wacom_parse_hid(struct usb_interface * intf,struct hid_descriptor * hid_desc,struct wacom_features * features)237 static int wacom_parse_hid(struct usb_interface *intf,
238 			   struct hid_descriptor *hid_desc,
239 			   struct wacom_features *features)
240 {
241 	struct usb_device *dev = interface_to_usbdev(intf);
242 	char limit = 0;
243 	/* result has to be defined as int for some devices */
244 	int result = 0;
245 	int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
246 	unsigned char *report;
247 
248 	report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
249 	if (!report)
250 		return -ENOMEM;
251 
252 	/* retrive report descriptors */
253 	do {
254 		result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
255 			USB_REQ_GET_DESCRIPTOR,
256 			USB_RECIP_INTERFACE | USB_DIR_IN,
257 			HID_DEVICET_REPORT << 8,
258 			intf->altsetting[0].desc.bInterfaceNumber, /* interface */
259 			report,
260 			hid_desc->wDescriptorLength,
261 			5000); /* 5 secs */
262 	} while (result < 0 && limit++ < WAC_MSG_RETRIES);
263 
264 	/* No need to parse the Descriptor. It isn't an error though */
265 	if (result < 0)
266 		goto out;
267 
268 	for (i = 0; i < hid_desc->wDescriptorLength; i++) {
269 
270 		switch (report[i]) {
271 		case HID_USAGE_PAGE:
272 			switch (report[i + 1]) {
273 			case HID_USAGE_PAGE_DIGITIZER:
274 				usage = WCM_DIGITIZER;
275 				i++;
276 				break;
277 
278 			case HID_USAGE_PAGE_DESKTOP:
279 				usage = WCM_DESKTOP;
280 				i++;
281 				break;
282 			}
283 			break;
284 
285 		case HID_USAGE:
286 			switch (report[i + 1]) {
287 			case HID_USAGE_X:
288 				if (usage == WCM_DESKTOP) {
289 					if (finger) {
290 						features->device_type = BTN_TOOL_FINGER;
291 						if (features->type == TABLETPC2FG) {
292 							/* need to reset back */
293 							features->pktlen = WACOM_PKGLEN_TPC2FG;
294 						}
295 						if (features->type == BAMBOO_PT) {
296 							/* need to reset back */
297 							features->pktlen = WACOM_PKGLEN_BBTOUCH;
298 							features->x_phy =
299 								get_unaligned_le16(&report[i + 5]);
300 							features->x_max =
301 								get_unaligned_le16(&report[i + 8]);
302 							i += 15;
303 						} else {
304 							features->x_max =
305 								get_unaligned_le16(&report[i + 3]);
306 							features->x_phy =
307 								get_unaligned_le16(&report[i + 6]);
308 							features->unit = report[i + 9];
309 							features->unitExpo = report[i + 11];
310 							i += 12;
311 						}
312 					} else if (pen) {
313 						/* penabled only accepts exact bytes of data */
314 						if (features->type == TABLETPC2FG)
315 							features->pktlen = WACOM_PKGLEN_GRAPHIRE;
316 						features->device_type = BTN_TOOL_PEN;
317 						features->x_max =
318 							get_unaligned_le16(&report[i + 3]);
319 						i += 4;
320 					}
321 				}
322 				break;
323 
324 			case HID_USAGE_Y:
325 				if (usage == WCM_DESKTOP) {
326 					if (finger) {
327 						features->device_type = BTN_TOOL_FINGER;
328 						if (features->type == TABLETPC2FG) {
329 							/* need to reset back */
330 							features->pktlen = WACOM_PKGLEN_TPC2FG;
331 							features->y_max =
332 								get_unaligned_le16(&report[i + 3]);
333 							features->y_phy =
334 								get_unaligned_le16(&report[i + 6]);
335 							i += 7;
336 						} else if (features->type == BAMBOO_PT) {
337 							/* need to reset back */
338 							features->pktlen = WACOM_PKGLEN_BBTOUCH;
339 							features->y_phy =
340 								get_unaligned_le16(&report[i + 3]);
341 							features->y_max =
342 								get_unaligned_le16(&report[i + 6]);
343 							i += 12;
344 						} else {
345 							features->y_max =
346 								features->x_max;
347 							features->y_phy =
348 								get_unaligned_le16(&report[i + 3]);
349 							i += 4;
350 						}
351 					} else if (pen) {
352 						/* penabled only accepts exact bytes of data */
353 						if (features->type == TABLETPC2FG)
354 							features->pktlen = WACOM_PKGLEN_GRAPHIRE;
355 						features->device_type = BTN_TOOL_PEN;
356 						features->y_max =
357 							get_unaligned_le16(&report[i + 3]);
358 						i += 4;
359 					}
360 				}
361 				break;
362 
363 			case HID_USAGE_FINGER:
364 				finger = 1;
365 				i++;
366 				break;
367 
368 			/*
369 			 * Requiring Stylus Usage will ignore boot mouse
370 			 * X/Y values and some cases of invalid Digitizer X/Y
371 			 * values commonly reported.
372 			 */
373 			case HID_USAGE_STYLUS:
374 				pen = 1;
375 				i++;
376 				break;
377 			}
378 			break;
379 
380 		case HID_COLLECTION_END:
381 			/* reset UsagePage and Finger */
382 			finger = usage = 0;
383 			break;
384 
385 		case HID_COLLECTION:
386 			i++;
387 			switch (report[i]) {
388 			case HID_COLLECTION_LOGICAL:
389 				i += wacom_parse_logical_collection(&report[i],
390 								    features);
391 				break;
392 			}
393 			break;
394 		}
395 	}
396 
397  out:
398 	result = 0;
399 	kfree(report);
400 	return result;
401 }
402 
wacom_query_tablet_data(struct usb_interface * intf,struct wacom_features * features)403 static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
404 {
405 	unsigned char *rep_data;
406 	int limit = 0, report_id = 2;
407 	int error = -ENOMEM;
408 
409 	rep_data = kmalloc(4, GFP_KERNEL);
410 	if (!rep_data)
411 		return error;
412 
413 	/* ask to report tablet data if it is MT Tablet PC or
414 	 * not a Tablet PC */
415 	if (features->type == TABLETPC2FG) {
416 		do {
417 			rep_data[0] = 3;
418 			rep_data[1] = 4;
419 			rep_data[2] = 0;
420 			rep_data[3] = 0;
421 			report_id = 3;
422 			error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
423 						 report_id, rep_data, 4, 1);
424 			if (error >= 0)
425 				error = wacom_get_report(intf,
426 						WAC_HID_FEATURE_REPORT,
427 						report_id, rep_data, 4, 1);
428 		} while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
429 	} else if (features->type != TABLETPC &&
430 		   features->type != WIRELESS &&
431 		   features->device_type == BTN_TOOL_PEN) {
432 		do {
433 			rep_data[0] = 2;
434 			rep_data[1] = 2;
435 			error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
436 						 report_id, rep_data, 2, 1);
437 			if (error >= 0)
438 				error = wacom_get_report(intf,
439 						WAC_HID_FEATURE_REPORT,
440 						report_id, rep_data, 2, 1);
441 		} while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
442 	}
443 
444 	kfree(rep_data);
445 
446 	return error < 0 ? error : 0;
447 }
448 
wacom_retrieve_hid_descriptor(struct usb_interface * intf,struct wacom_features * features)449 static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
450 		struct wacom_features *features)
451 {
452 	int error = 0;
453 	struct usb_host_interface *interface = intf->cur_altsetting;
454 	struct hid_descriptor *hid_desc;
455 
456 	/* default features */
457 	features->device_type = BTN_TOOL_PEN;
458 	features->x_fuzz = 4;
459 	features->y_fuzz = 4;
460 	features->pressure_fuzz = 0;
461 	features->distance_fuzz = 0;
462 
463 	/*
464 	 * The wireless device HID is basic and layout conflicts with
465 	 * other tablets (monitor and touch interface can look like pen).
466 	 * Skip the query for this type and modify defaults based on
467 	 * interface number.
468 	 */
469 	if (features->type == WIRELESS) {
470 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
471 			features->device_type = 0;
472 		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
473 			features->device_type = BTN_TOOL_DOUBLETAP;
474 			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
475 		}
476 	}
477 
478 	/* only Tablet PCs and Bamboo P&T need to retrieve the info */
479 	if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
480 	    (features->type != BAMBOO_PT))
481 		goto out;
482 
483 	if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
484 		if (usb_get_extra_descriptor(&interface->endpoint[0],
485 				HID_DEVICET_REPORT, &hid_desc)) {
486 			printk("wacom: can not retrieve extra class descriptor\n");
487 			error = 1;
488 			goto out;
489 		}
490 	}
491 	error = wacom_parse_hid(intf, hid_desc, features);
492 	if (error)
493 		goto out;
494 
495  out:
496 	return error;
497 }
498 
499 struct wacom_usbdev_data {
500 	struct list_head list;
501 	struct kref kref;
502 	struct usb_device *dev;
503 	struct wacom_shared shared;
504 };
505 
506 static LIST_HEAD(wacom_udev_list);
507 static DEFINE_MUTEX(wacom_udev_list_lock);
508 
wacom_get_usbdev_data(struct usb_device * dev)509 static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
510 {
511 	struct wacom_usbdev_data *data;
512 
513 	list_for_each_entry(data, &wacom_udev_list, list) {
514 		if (data->dev == dev) {
515 			kref_get(&data->kref);
516 			return data;
517 		}
518 	}
519 
520 	return NULL;
521 }
522 
wacom_add_shared_data(struct wacom_wac * wacom,struct usb_device * dev)523 static int wacom_add_shared_data(struct wacom_wac *wacom,
524 				 struct usb_device *dev)
525 {
526 	struct wacom_usbdev_data *data;
527 	int retval = 0;
528 
529 	mutex_lock(&wacom_udev_list_lock);
530 
531 	data = wacom_get_usbdev_data(dev);
532 	if (!data) {
533 		data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
534 		if (!data) {
535 			retval = -ENOMEM;
536 			goto out;
537 		}
538 
539 		kref_init(&data->kref);
540 		data->dev = dev;
541 		list_add_tail(&data->list, &wacom_udev_list);
542 	}
543 
544 	wacom->shared = &data->shared;
545 
546 out:
547 	mutex_unlock(&wacom_udev_list_lock);
548 	return retval;
549 }
550 
wacom_release_shared_data(struct kref * kref)551 static void wacom_release_shared_data(struct kref *kref)
552 {
553 	struct wacom_usbdev_data *data =
554 		container_of(kref, struct wacom_usbdev_data, kref);
555 
556 	mutex_lock(&wacom_udev_list_lock);
557 	list_del(&data->list);
558 	mutex_unlock(&wacom_udev_list_lock);
559 
560 	kfree(data);
561 }
562 
wacom_remove_shared_data(struct wacom_wac * wacom)563 static void wacom_remove_shared_data(struct wacom_wac *wacom)
564 {
565 	struct wacom_usbdev_data *data;
566 
567 	if (wacom->shared) {
568 		data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
569 		kref_put(&data->kref, wacom_release_shared_data);
570 		wacom->shared = NULL;
571 	}
572 }
573 
wacom_led_control(struct wacom * wacom)574 static int wacom_led_control(struct wacom *wacom)
575 {
576 	unsigned char *buf;
577 	int retval, led = 0;
578 
579 	buf = kzalloc(9, GFP_KERNEL);
580 	if (!buf)
581 		return -ENOMEM;
582 
583 	if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
584 	    wacom->wacom_wac.features.type == WACOM_24HD)
585 		led = (wacom->led.select[1] << 4) | 0x40;
586 
587 	led |=  wacom->led.select[0] | 0x4;
588 
589 	buf[0] = WAC_CMD_LED_CONTROL;
590 	buf[1] = led;
591 	buf[2] = wacom->led.llv;
592 	buf[3] = wacom->led.hlv;
593 	buf[4] = wacom->led.img_lum;
594 
595 	retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
596 				  buf, 9, WAC_CMD_RETRIES);
597 	kfree(buf);
598 
599 	return retval;
600 }
601 
wacom_led_putimage(struct wacom * wacom,int button_id,const void * img)602 static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
603 {
604 	unsigned char *buf;
605 	int i, retval;
606 
607 	buf = kzalloc(259, GFP_KERNEL);
608 	if (!buf)
609 		return -ENOMEM;
610 
611 	/* Send 'start' command */
612 	buf[0] = WAC_CMD_ICON_START;
613 	buf[1] = 1;
614 	retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
615 				  buf, 2, WAC_CMD_RETRIES);
616 	if (retval < 0)
617 		goto out;
618 
619 	buf[0] = WAC_CMD_ICON_XFER;
620 	buf[1] = button_id & 0x07;
621 	for (i = 0; i < 4; i++) {
622 		buf[2] = i;
623 		memcpy(buf + 3, img + i * 256, 256);
624 
625 		retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
626 					  buf, 259, WAC_CMD_RETRIES);
627 		if (retval < 0)
628 			break;
629 	}
630 
631 	/* Send 'stop' */
632 	buf[0] = WAC_CMD_ICON_START;
633 	buf[1] = 0;
634 	wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
635 			 buf, 2, WAC_CMD_RETRIES);
636 
637 out:
638 	kfree(buf);
639 	return retval;
640 }
641 
wacom_led_select_store(struct device * dev,int set_id,const char * buf,size_t count)642 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
643 				      const char *buf, size_t count)
644 {
645 	struct wacom *wacom = dev_get_drvdata(dev);
646 	unsigned int id;
647 	int err;
648 
649 	err = kstrtouint(buf, 10, &id);
650 	if (err)
651 		return err;
652 
653 	mutex_lock(&wacom->lock);
654 
655 	wacom->led.select[set_id] = id & 0x3;
656 	err = wacom_led_control(wacom);
657 
658 	mutex_unlock(&wacom->lock);
659 
660 	return err < 0 ? err : count;
661 }
662 
663 #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
664 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
665 	struct device_attribute *attr, const char *buf, size_t count)	\
666 {									\
667 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
668 }									\
669 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
670 	struct device_attribute *attr, char *buf)			\
671 {									\
672 	struct wacom *wacom = dev_get_drvdata(dev);			\
673 	return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]);	\
674 }									\
675 static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR,	\
676 		    wacom_led##SET_ID##_select_show,			\
677 		    wacom_led##SET_ID##_select_store)
678 
679 DEVICE_LED_SELECT_ATTR(0);
680 DEVICE_LED_SELECT_ATTR(1);
681 
wacom_luminance_store(struct wacom * wacom,u8 * dest,const char * buf,size_t count)682 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
683 				     const char *buf, size_t count)
684 {
685 	unsigned int value;
686 	int err;
687 
688 	err = kstrtouint(buf, 10, &value);
689 	if (err)
690 		return err;
691 
692 	mutex_lock(&wacom->lock);
693 
694 	*dest = value & 0x7f;
695 	err = wacom_led_control(wacom);
696 
697 	mutex_unlock(&wacom->lock);
698 
699 	return err < 0 ? err : count;
700 }
701 
702 #define DEVICE_LUMINANCE_ATTR(name, field)				\
703 static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
704 	struct device_attribute *attr, const char *buf, size_t count)	\
705 {									\
706 	struct wacom *wacom = dev_get_drvdata(dev);			\
707 									\
708 	return wacom_luminance_store(wacom, &wacom->led.field,		\
709 				     buf, count);			\
710 }									\
711 static DEVICE_ATTR(name##_luminance, S_IWUSR,				\
712 		   NULL, wacom_##name##_luminance_store)
713 
714 DEVICE_LUMINANCE_ATTR(status0, llv);
715 DEVICE_LUMINANCE_ATTR(status1, hlv);
716 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
717 
wacom_button_image_store(struct device * dev,int button_id,const char * buf,size_t count)718 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
719 					const char *buf, size_t count)
720 {
721 	struct wacom *wacom = dev_get_drvdata(dev);
722 	int err;
723 
724 	if (count != 1024)
725 		return -EINVAL;
726 
727 	mutex_lock(&wacom->lock);
728 
729 	err = wacom_led_putimage(wacom, button_id, buf);
730 
731 	mutex_unlock(&wacom->lock);
732 
733 	return err < 0 ? err : count;
734 }
735 
736 #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
737 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
738 	struct device_attribute *attr, const char *buf, size_t count)	\
739 {									\
740 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
741 }									\
742 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR,			\
743 		   NULL, wacom_btnimg##BUTTON_ID##_store)
744 
745 DEVICE_BTNIMG_ATTR(0);
746 DEVICE_BTNIMG_ATTR(1);
747 DEVICE_BTNIMG_ATTR(2);
748 DEVICE_BTNIMG_ATTR(3);
749 DEVICE_BTNIMG_ATTR(4);
750 DEVICE_BTNIMG_ATTR(5);
751 DEVICE_BTNIMG_ATTR(6);
752 DEVICE_BTNIMG_ATTR(7);
753 
754 static struct attribute *cintiq_led_attrs[] = {
755 	&dev_attr_status_led0_select.attr,
756 	&dev_attr_status_led1_select.attr,
757 	NULL
758 };
759 
760 static struct attribute_group cintiq_led_attr_group = {
761 	.name = "wacom_led",
762 	.attrs = cintiq_led_attrs,
763 };
764 
765 static struct attribute *intuos4_led_attrs[] = {
766 	&dev_attr_status0_luminance.attr,
767 	&dev_attr_status1_luminance.attr,
768 	&dev_attr_status_led0_select.attr,
769 	&dev_attr_buttons_luminance.attr,
770 	&dev_attr_button0_rawimg.attr,
771 	&dev_attr_button1_rawimg.attr,
772 	&dev_attr_button2_rawimg.attr,
773 	&dev_attr_button3_rawimg.attr,
774 	&dev_attr_button4_rawimg.attr,
775 	&dev_attr_button5_rawimg.attr,
776 	&dev_attr_button6_rawimg.attr,
777 	&dev_attr_button7_rawimg.attr,
778 	NULL
779 };
780 
781 static struct attribute_group intuos4_led_attr_group = {
782 	.name = "wacom_led",
783 	.attrs = intuos4_led_attrs,
784 };
785 
wacom_initialize_leds(struct wacom * wacom)786 static int wacom_initialize_leds(struct wacom *wacom)
787 {
788 	int error;
789 
790 	/* Initialize default values */
791 	switch (wacom->wacom_wac.features.type) {
792 	case INTUOS4:
793 	case INTUOS4L:
794 		wacom->led.select[0] = 0;
795 		wacom->led.select[1] = 0;
796 		wacom->led.llv = 10;
797 		wacom->led.hlv = 20;
798 		wacom->led.img_lum = 10;
799 		error = sysfs_create_group(&wacom->intf->dev.kobj,
800 					   &intuos4_led_attr_group);
801 		break;
802 
803 	case WACOM_24HD:
804 	case WACOM_21UX2:
805 		wacom->led.select[0] = 0;
806 		wacom->led.select[1] = 0;
807 		wacom->led.llv = 0;
808 		wacom->led.hlv = 0;
809 		wacom->led.img_lum = 0;
810 
811 		error = sysfs_create_group(&wacom->intf->dev.kobj,
812 					   &cintiq_led_attr_group);
813 		break;
814 
815 	default:
816 		return 0;
817 	}
818 
819 	if (error) {
820 		dev_err(&wacom->intf->dev,
821 			"cannot create sysfs group err: %d\n", error);
822 		return error;
823 	}
824 	wacom_led_control(wacom);
825 
826 	return 0;
827 }
828 
wacom_destroy_leds(struct wacom * wacom)829 static void wacom_destroy_leds(struct wacom *wacom)
830 {
831 	switch (wacom->wacom_wac.features.type) {
832 	case INTUOS4:
833 	case INTUOS4L:
834 		sysfs_remove_group(&wacom->intf->dev.kobj,
835 				   &intuos4_led_attr_group);
836 		break;
837 
838 	case WACOM_24HD:
839 	case WACOM_21UX2:
840 		sysfs_remove_group(&wacom->intf->dev.kobj,
841 				   &cintiq_led_attr_group);
842 		break;
843 	}
844 }
845 
846 static enum power_supply_property wacom_battery_props[] = {
847 	POWER_SUPPLY_PROP_CAPACITY
848 };
849 
wacom_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)850 static int wacom_battery_get_property(struct power_supply *psy,
851 				      enum power_supply_property psp,
852 				      union power_supply_propval *val)
853 {
854 	struct wacom *wacom = container_of(psy, struct wacom, battery);
855 	int ret = 0;
856 
857 	switch (psp) {
858 		case POWER_SUPPLY_PROP_CAPACITY:
859 			val->intval =
860 				wacom->wacom_wac.battery_capacity * 100 / 31;
861 			break;
862 		default:
863 			ret = -EINVAL;
864 			break;
865 	}
866 
867 	return ret;
868 }
869 
wacom_initialize_battery(struct wacom * wacom)870 static int wacom_initialize_battery(struct wacom *wacom)
871 {
872 	int error = 0;
873 
874 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
875 		wacom->battery.properties = wacom_battery_props;
876 		wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
877 		wacom->battery.get_property = wacom_battery_get_property;
878 		wacom->battery.name = "wacom_battery";
879 		wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
880 		wacom->battery.use_for_apm = 0;
881 
882 		error = power_supply_register(&wacom->usbdev->dev,
883 					      &wacom->battery);
884 	}
885 
886 	return error;
887 }
888 
wacom_destroy_battery(struct wacom * wacom)889 static void wacom_destroy_battery(struct wacom *wacom)
890 {
891 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR)
892 		power_supply_unregister(&wacom->battery);
893 }
894 
wacom_register_input(struct wacom * wacom)895 static int wacom_register_input(struct wacom *wacom)
896 {
897 	struct input_dev *input_dev;
898 	struct usb_interface *intf = wacom->intf;
899 	struct usb_device *dev = interface_to_usbdev(intf);
900 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
901 	int error;
902 
903 	input_dev = input_allocate_device();
904 	if (!input_dev)
905 		return -ENOMEM;
906 
907 	input_dev->name = wacom_wac->name;
908 	input_dev->dev.parent = &intf->dev;
909 	input_dev->open = wacom_open;
910 	input_dev->close = wacom_close;
911 	usb_to_input_id(dev, &input_dev->id);
912 	input_set_drvdata(input_dev, wacom);
913 
914 	wacom_wac->input = input_dev;
915 	wacom_setup_input_capabilities(input_dev, wacom_wac);
916 
917 	error = input_register_device(input_dev);
918 	if (error) {
919 		input_free_device(input_dev);
920 		wacom_wac->input = NULL;
921 	}
922 
923 	return error;
924 }
925 
wacom_wireless_work(struct work_struct * work)926 static void wacom_wireless_work(struct work_struct *work)
927 {
928 	struct wacom *wacom = container_of(work, struct wacom, work);
929 	struct usb_device *usbdev = wacom->usbdev;
930 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
931 
932 	/*
933 	 * Regardless if this is a disconnect or a new tablet,
934 	 * remove any existing input devices.
935 	 */
936 
937 	/* Stylus interface */
938 	wacom = usb_get_intfdata(usbdev->config->interface[1]);
939 	if (wacom->wacom_wac.input)
940 		input_unregister_device(wacom->wacom_wac.input);
941 	wacom->wacom_wac.input = 0;
942 
943 	/* Touch interface */
944 	wacom = usb_get_intfdata(usbdev->config->interface[2]);
945 	if (wacom->wacom_wac.input)
946 		input_unregister_device(wacom->wacom_wac.input);
947 	wacom->wacom_wac.input = 0;
948 
949 	if (wacom_wac->pid == 0) {
950 		printk(KERN_INFO "wacom: wireless tablet disconnected\n");
951 	} else {
952 		const struct usb_device_id *id = wacom_ids;
953 
954 		printk(KERN_INFO
955 		       "wacom: wireless tablet connected with PID %x\n",
956 		       wacom_wac->pid);
957 
958 		while (id->match_flags) {
959 			if (id->idVendor == USB_VENDOR_ID_WACOM &&
960 			    id->idProduct == wacom_wac->pid)
961 				break;
962 			id++;
963 		}
964 
965 		if (!id->match_flags) {
966 			printk(KERN_INFO
967 			       "wacom: ignorning unknown PID.\n");
968 			return;
969 		}
970 
971 		/* Stylus interface */
972 		wacom = usb_get_intfdata(usbdev->config->interface[1]);
973 		wacom_wac = &wacom->wacom_wac;
974 		wacom_wac->features =
975 			*((struct wacom_features *)id->driver_info);
976 		wacom_wac->features.device_type = BTN_TOOL_PEN;
977 		wacom_register_input(wacom);
978 
979 		/* Touch interface */
980 		wacom = usb_get_intfdata(usbdev->config->interface[2]);
981 		wacom_wac = &wacom->wacom_wac;
982 		wacom_wac->features =
983 			*((struct wacom_features *)id->driver_info);
984 		wacom_wac->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
985 		wacom_wac->features.device_type = BTN_TOOL_FINGER;
986 		wacom_set_phy_from_res(&wacom_wac->features);
987 		wacom_wac->features.x_max = wacom_wac->features.y_max = 4096;
988 		wacom_register_input(wacom);
989 	}
990 }
991 
wacom_probe(struct usb_interface * intf,const struct usb_device_id * id)992 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
993 {
994 	struct usb_device *dev = interface_to_usbdev(intf);
995 	struct usb_endpoint_descriptor *endpoint;
996 	struct wacom *wacom;
997 	struct wacom_wac *wacom_wac;
998 	struct wacom_features *features;
999 	int error;
1000 
1001 	if (!id->driver_info)
1002 		return -EINVAL;
1003 
1004 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1005 	if (!wacom)
1006 		return -ENOMEM;
1007 
1008 	wacom_wac = &wacom->wacom_wac;
1009 	wacom_wac->features = *((struct wacom_features *)id->driver_info);
1010 	features = &wacom_wac->features;
1011 	if (features->pktlen > WACOM_PKGLEN_MAX) {
1012 		error = -EINVAL;
1013 		goto fail1;
1014 	}
1015 
1016 	wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
1017 					     GFP_KERNEL, &wacom->data_dma);
1018 	if (!wacom_wac->data) {
1019 		error = -ENOMEM;
1020 		goto fail1;
1021 	}
1022 
1023 	wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
1024 	if (!wacom->irq) {
1025 		error = -ENOMEM;
1026 		goto fail2;
1027 	}
1028 
1029 	wacom->usbdev = dev;
1030 	wacom->intf = intf;
1031 	mutex_init(&wacom->lock);
1032 	INIT_WORK(&wacom->work, wacom_wireless_work);
1033 	usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1034 	strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1035 
1036 	endpoint = &intf->cur_altsetting->endpoint[0].desc;
1037 
1038 	/* Retrieve the physical and logical size for OEM devices */
1039 	error = wacom_retrieve_hid_descriptor(intf, features);
1040 	if (error)
1041 		goto fail3;
1042 
1043 	wacom_setup_device_quirks(features);
1044 
1045 	strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1046 
1047 	if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1048 		/* Append the device type to the name */
1049 		strlcat(wacom_wac->name,
1050 			features->device_type == BTN_TOOL_PEN ?
1051 				" Pen" : " Finger",
1052 			sizeof(wacom_wac->name));
1053 
1054 		error = wacom_add_shared_data(wacom_wac, dev);
1055 		if (error)
1056 			goto fail3;
1057 	}
1058 
1059 	usb_fill_int_urb(wacom->irq, dev,
1060 			 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1061 			 wacom_wac->data, features->pktlen,
1062 			 wacom_sys_irq, wacom, endpoint->bInterval);
1063 	wacom->irq->transfer_dma = wacom->data_dma;
1064 	wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1065 
1066 	error = wacom_initialize_leds(wacom);
1067 	if (error)
1068 		goto fail4;
1069 
1070 	error = wacom_initialize_battery(wacom);
1071 	if (error)
1072 		goto fail5;
1073 
1074 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1075 		error = wacom_register_input(wacom);
1076 		if (error)
1077 			goto fail6;
1078 	}
1079 
1080 	/* Note that if query fails it is not a hard failure */
1081 	wacom_query_tablet_data(intf, features);
1082 
1083 	usb_set_intfdata(intf, wacom);
1084 
1085 	if (features->quirks & WACOM_QUIRK_MONITOR) {
1086 		if (usb_submit_urb(wacom->irq, GFP_KERNEL))
1087 			goto fail5;
1088 	}
1089 
1090 	return 0;
1091 
1092  fail6: wacom_destroy_battery(wacom);
1093  fail5: wacom_destroy_leds(wacom);
1094  fail4:	wacom_remove_shared_data(wacom_wac);
1095  fail3:	usb_free_urb(wacom->irq);
1096  fail2:	usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
1097  fail1:	kfree(wacom);
1098 	return error;
1099 }
1100 
wacom_disconnect(struct usb_interface * intf)1101 static void wacom_disconnect(struct usb_interface *intf)
1102 {
1103 	struct wacom *wacom = usb_get_intfdata(intf);
1104 
1105 	usb_set_intfdata(intf, NULL);
1106 
1107 	usb_kill_urb(wacom->irq);
1108 	cancel_work_sync(&wacom->work);
1109 	if (wacom->wacom_wac.input)
1110 		input_unregister_device(wacom->wacom_wac.input);
1111 	wacom_destroy_battery(wacom);
1112 	wacom_destroy_leds(wacom);
1113 	usb_free_urb(wacom->irq);
1114 	usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
1115 			wacom->wacom_wac.data, wacom->data_dma);
1116 	wacom_remove_shared_data(&wacom->wacom_wac);
1117 	kfree(wacom);
1118 }
1119 
wacom_suspend(struct usb_interface * intf,pm_message_t message)1120 static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
1121 {
1122 	struct wacom *wacom = usb_get_intfdata(intf);
1123 
1124 	mutex_lock(&wacom->lock);
1125 	usb_kill_urb(wacom->irq);
1126 	mutex_unlock(&wacom->lock);
1127 
1128 	return 0;
1129 }
1130 
wacom_resume(struct usb_interface * intf)1131 static int wacom_resume(struct usb_interface *intf)
1132 {
1133 	struct wacom *wacom = usb_get_intfdata(intf);
1134 	struct wacom_features *features = &wacom->wacom_wac.features;
1135 	int rv = 0;
1136 
1137 	mutex_lock(&wacom->lock);
1138 
1139 	/* switch to wacom mode first */
1140 	wacom_query_tablet_data(intf, features);
1141 	wacom_led_control(wacom);
1142 
1143 	if ((wacom->open || features->quirks & WACOM_QUIRK_MONITOR)
1144 	     && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
1145 		rv = -EIO;
1146 
1147 	mutex_unlock(&wacom->lock);
1148 
1149 	return rv;
1150 }
1151 
wacom_reset_resume(struct usb_interface * intf)1152 static int wacom_reset_resume(struct usb_interface *intf)
1153 {
1154 	return wacom_resume(intf);
1155 }
1156 
1157 static struct usb_driver wacom_driver = {
1158 	.name =		"wacom",
1159 	.id_table =	wacom_ids,
1160 	.probe =	wacom_probe,
1161 	.disconnect =	wacom_disconnect,
1162 	.suspend =	wacom_suspend,
1163 	.resume =	wacom_resume,
1164 	.reset_resume =	wacom_reset_resume,
1165 	.supports_autosuspend = 1,
1166 };
1167 
1168 module_usb_driver(wacom_driver);
1169