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 #define WAC_CMD_RETRIES 10 20471d1714SBenjamin Tissoires 21e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP) 22e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP) 2372b236d6SAaron Skomra #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP) 24e0984bc3SPing Cheng 25c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf, 26c64d8834SPing Cheng size_t size, unsigned int retries) 27471d1714SBenjamin Tissoires { 28471d1714SBenjamin Tissoires int retval; 29471d1714SBenjamin Tissoires 30471d1714SBenjamin Tissoires do { 31c64d8834SPing Cheng retval = hid_hw_raw_request(hdev, buf[0], buf, size, type, 32471d1714SBenjamin Tissoires HID_REQ_GET_REPORT); 33aef3156dSJason Gerecke } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries); 34aef3156dSJason Gerecke 35aef3156dSJason Gerecke if (retval < 0) 36aef3156dSJason Gerecke hid_err(hdev, "wacom_get_report: ran out of retries " 37aef3156dSJason Gerecke "(last error = %d)\n", retval); 38471d1714SBenjamin Tissoires 39471d1714SBenjamin Tissoires return retval; 40471d1714SBenjamin Tissoires } 41471d1714SBenjamin Tissoires 42296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf, 43296b7378SPrzemo Firszt size_t size, unsigned int retries) 44471d1714SBenjamin Tissoires { 45471d1714SBenjamin Tissoires int retval; 46471d1714SBenjamin Tissoires 47471d1714SBenjamin Tissoires do { 48296b7378SPrzemo Firszt retval = hid_hw_raw_request(hdev, buf[0], buf, size, type, 49471d1714SBenjamin Tissoires HID_REQ_SET_REPORT); 50aef3156dSJason Gerecke } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries); 51aef3156dSJason Gerecke 52aef3156dSJason Gerecke if (retval < 0) 53aef3156dSJason Gerecke hid_err(hdev, "wacom_set_report: ran out of retries " 54aef3156dSJason Gerecke "(last error = %d)\n", retval); 55471d1714SBenjamin Tissoires 56471d1714SBenjamin Tissoires return retval; 57471d1714SBenjamin Tissoires } 58471d1714SBenjamin Tissoires 59471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report, 60471d1714SBenjamin Tissoires u8 *raw_data, int size) 61471d1714SBenjamin Tissoires { 62471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 63471d1714SBenjamin Tissoires 64471d1714SBenjamin Tissoires if (size > WACOM_PKGLEN_MAX) 65471d1714SBenjamin Tissoires return 1; 66471d1714SBenjamin Tissoires 67471d1714SBenjamin Tissoires memcpy(wacom->wacom_wac.data, raw_data, size); 68471d1714SBenjamin Tissoires 69471d1714SBenjamin Tissoires wacom_wac_irq(&wacom->wacom_wac, size); 70471d1714SBenjamin Tissoires 71471d1714SBenjamin Tissoires return 0; 72471d1714SBenjamin Tissoires } 73471d1714SBenjamin Tissoires 74471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev) 75471d1714SBenjamin Tissoires { 76471d1714SBenjamin Tissoires struct wacom *wacom = input_get_drvdata(dev); 77471d1714SBenjamin Tissoires 78dff67416SBenjamin Tissoires return hid_hw_open(wacom->hdev); 79471d1714SBenjamin Tissoires } 80471d1714SBenjamin Tissoires 81471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev) 82471d1714SBenjamin Tissoires { 83471d1714SBenjamin Tissoires struct wacom *wacom = input_get_drvdata(dev); 84471d1714SBenjamin Tissoires 853dad188eSBenjamin Tissoires /* 863dad188eSBenjamin Tissoires * wacom->hdev should never be null, but surprisingly, I had the case 873dad188eSBenjamin Tissoires * once while unplugging the Wacom Wireless Receiver. 883dad188eSBenjamin Tissoires */ 893dad188eSBenjamin Tissoires if (wacom->hdev) 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; 115ac2423c9SAaron Armstrong Skomra unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid); 1168ffffd52SBenjamin Tissoires u8 *data; 1178ffffd52SBenjamin Tissoires int ret; 118345857bbSJason Gerecke int n; 119471d1714SBenjamin Tissoires 120ac2423c9SAaron Armstrong Skomra switch (equivalent_usage) { 121471d1714SBenjamin Tissoires case HID_DG_CONTACTMAX: 122471d1714SBenjamin Tissoires /* leave touch_max as is if predefined */ 1238ffffd52SBenjamin Tissoires if (!features->touch_max) { 1248ffffd52SBenjamin Tissoires /* read manually */ 1258ffffd52SBenjamin Tissoires data = kzalloc(2, GFP_KERNEL); 1268ffffd52SBenjamin Tissoires if (!data) 1278ffffd52SBenjamin Tissoires break; 1288ffffd52SBenjamin Tissoires data[0] = field->report->id; 1298ffffd52SBenjamin Tissoires ret = wacom_get_report(hdev, HID_FEATURE_REPORT, 13005e8fd92SJason Gerecke data, 2, WAC_CMD_RETRIES); 13105e8fd92SJason Gerecke if (ret == 2) { 1328ffffd52SBenjamin Tissoires features->touch_max = data[1]; 13305e8fd92SJason Gerecke } else { 13405e8fd92SJason Gerecke features->touch_max = 16; 13505e8fd92SJason Gerecke hid_warn(hdev, "wacom_feature_mapping: " 13605e8fd92SJason Gerecke "could not get HID_DG_CONTACTMAX, " 13705e8fd92SJason Gerecke "defaulting to %d\n", 13805e8fd92SJason Gerecke features->touch_max); 13905e8fd92SJason Gerecke } 1408ffffd52SBenjamin Tissoires kfree(data); 1418ffffd52SBenjamin Tissoires } 142471d1714SBenjamin Tissoires break; 1435ae6e89fSBenjamin Tissoires case HID_DG_INPUTMODE: 1445ae6e89fSBenjamin Tissoires /* Ignore if value index is out of bounds. */ 1455ae6e89fSBenjamin Tissoires if (usage->usage_index >= field->report_count) { 1465ae6e89fSBenjamin Tissoires dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n"); 1475ae6e89fSBenjamin Tissoires break; 1485ae6e89fSBenjamin Tissoires } 1495ae6e89fSBenjamin Tissoires 1505ae6e89fSBenjamin Tissoires hid_data->inputmode = field->report->id; 1515ae6e89fSBenjamin Tissoires hid_data->inputmode_index = usage->usage_index; 1525ae6e89fSBenjamin Tissoires break; 153326ea2a9SJason Gerecke 154326ea2a9SJason Gerecke case HID_UP_DIGITIZER: 155326ea2a9SJason Gerecke if (field->report->id == 0x0B && 1568de82280SJason Gerecke (field->application == WACOM_HID_G9_PEN || 1578de82280SJason Gerecke field->application == WACOM_HID_G11_PEN)) { 158326ea2a9SJason Gerecke wacom->wacom_wac.mode_report = field->report->id; 159326ea2a9SJason Gerecke wacom->wacom_wac.mode_value = 0; 160326ea2a9SJason Gerecke } 161326ea2a9SJason Gerecke break; 162326ea2a9SJason Gerecke 163c9c09587SJason Gerecke case WACOM_HID_WD_DATAMODE: 164c9c09587SJason Gerecke wacom->wacom_wac.mode_report = field->report->id; 165c9c09587SJason Gerecke wacom->wacom_wac.mode_value = 2; 166c9c09587SJason Gerecke break; 167c9c09587SJason Gerecke 1688de82280SJason Gerecke case WACOM_HID_UP_G9: 1698de82280SJason Gerecke case WACOM_HID_UP_G11: 170326ea2a9SJason Gerecke if (field->report->id == 0x03 && 1718de82280SJason Gerecke (field->application == WACOM_HID_G9_TOUCHSCREEN || 1728de82280SJason Gerecke field->application == WACOM_HID_G11_TOUCHSCREEN)) { 173326ea2a9SJason Gerecke wacom->wacom_wac.mode_report = field->report->id; 174326ea2a9SJason Gerecke wacom->wacom_wac.mode_value = 0; 175326ea2a9SJason Gerecke } 176326ea2a9SJason Gerecke break; 177345857bbSJason Gerecke case WACOM_HID_WD_OFFSETLEFT: 178345857bbSJason Gerecke case WACOM_HID_WD_OFFSETTOP: 179345857bbSJason Gerecke case WACOM_HID_WD_OFFSETRIGHT: 180345857bbSJason Gerecke case WACOM_HID_WD_OFFSETBOTTOM: 181345857bbSJason Gerecke /* read manually */ 182345857bbSJason Gerecke n = hid_report_len(field->report); 183345857bbSJason Gerecke data = hid_alloc_report_buf(field->report, GFP_KERNEL); 184345857bbSJason Gerecke if (!data) 185345857bbSJason Gerecke break; 186345857bbSJason Gerecke data[0] = field->report->id; 187345857bbSJason Gerecke ret = wacom_get_report(hdev, HID_FEATURE_REPORT, 188345857bbSJason Gerecke data, n, WAC_CMD_RETRIES); 189345857bbSJason Gerecke if (ret == n) { 190345857bbSJason Gerecke ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, 191345857bbSJason Gerecke data, n, 0); 192345857bbSJason Gerecke } else { 193345857bbSJason Gerecke hid_warn(hdev, "%s: could not retrieve sensor offsets\n", 194345857bbSJason Gerecke __func__); 195345857bbSJason Gerecke } 196345857bbSJason Gerecke kfree(data); 197345857bbSJason Gerecke break; 198471d1714SBenjamin Tissoires } 199471d1714SBenjamin Tissoires } 200471d1714SBenjamin Tissoires 201471d1714SBenjamin Tissoires /* 202471d1714SBenjamin Tissoires * Interface Descriptor of wacom devices can be incomplete and 203471d1714SBenjamin Tissoires * inconsistent so wacom_features table is used to store stylus 204471d1714SBenjamin Tissoires * device's packet lengths, various maximum values, and tablet 205471d1714SBenjamin Tissoires * resolution based on product ID's. 206471d1714SBenjamin Tissoires * 207471d1714SBenjamin Tissoires * For devices that contain 2 interfaces, wacom_features table is 208471d1714SBenjamin Tissoires * inaccurate for the touch interface. Since the Interface Descriptor 209471d1714SBenjamin Tissoires * for touch interfaces has pretty complete data, this function exists 210471d1714SBenjamin Tissoires * to query tablet for this missing information instead of hard coding in 211471d1714SBenjamin Tissoires * an additional table. 212471d1714SBenjamin Tissoires * 213471d1714SBenjamin Tissoires * A typical Interface Descriptor for a stylus will contain a 214471d1714SBenjamin Tissoires * boot mouse application collection that is not of interest and this 215471d1714SBenjamin Tissoires * function will ignore it. 216471d1714SBenjamin Tissoires * 217471d1714SBenjamin Tissoires * It also contains a digitizer application collection that also is not 218471d1714SBenjamin Tissoires * of interest since any information it contains would be duplicate 219471d1714SBenjamin Tissoires * of what is in wacom_features. Usually it defines a report of an array 220471d1714SBenjamin Tissoires * of bytes that could be used as max length of the stylus packet returned. 221471d1714SBenjamin Tissoires * If it happens to define a Digitizer-Stylus Physical Collection then 222471d1714SBenjamin Tissoires * the X and Y logical values contain valid data but it is ignored. 223471d1714SBenjamin Tissoires * 224471d1714SBenjamin Tissoires * A typical Interface Descriptor for a touch interface will contain a 225471d1714SBenjamin Tissoires * Digitizer-Finger Physical Collection which will define both logical 226471d1714SBenjamin Tissoires * X/Y maximum as well as the physical size of tablet. Since touch 227471d1714SBenjamin Tissoires * interfaces haven't supported pressure or distance, this is enough 228471d1714SBenjamin Tissoires * information to override invalid values in the wacom_features table. 229471d1714SBenjamin Tissoires * 230471d1714SBenjamin Tissoires * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful 231471d1714SBenjamin Tissoires * data. We deal with them after returning from this function. 232471d1714SBenjamin Tissoires */ 233471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev, 234471d1714SBenjamin Tissoires struct hid_field *field, struct hid_usage *usage) 235471d1714SBenjamin Tissoires { 236471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 237471d1714SBenjamin Tissoires struct wacom_features *features = &wacom->wacom_wac.features; 238d97a5522SBenjamin Tissoires bool finger = WACOM_FINGER_FIELD(field); 239d97a5522SBenjamin Tissoires bool pen = WACOM_PEN_FIELD(field); 240471d1714SBenjamin Tissoires 241471d1714SBenjamin Tissoires /* 242471d1714SBenjamin Tissoires * Requiring Stylus Usage will ignore boot mouse 243471d1714SBenjamin Tissoires * X/Y values and some cases of invalid Digitizer X/Y 244471d1714SBenjamin Tissoires * values commonly reported. 245471d1714SBenjamin Tissoires */ 246042628abSJason Gerecke if (pen) 247aa86b18cSJason Gerecke features->device_type |= WACOM_DEVICETYPE_PEN; 248042628abSJason Gerecke else if (finger) 249aa86b18cSJason Gerecke features->device_type |= WACOM_DEVICETYPE_TOUCH; 250042628abSJason Gerecke else 251471d1714SBenjamin Tissoires return; 252471d1714SBenjamin Tissoires 25330ebc1aeSPing Cheng /* 25430ebc1aeSPing Cheng * Bamboo models do not support HID_DG_CONTACTMAX. 25530ebc1aeSPing Cheng * And, Bamboo Pen only descriptor contains touch. 25630ebc1aeSPing Cheng */ 2573b164a00SPing Cheng if (features->type > BAMBOO_PT) { 25830ebc1aeSPing Cheng /* ISDv4 touch devices at least supports one touch point */ 259471d1714SBenjamin Tissoires if (finger && !features->touch_max) 260471d1714SBenjamin Tissoires features->touch_max = 1; 26130ebc1aeSPing Cheng } 262471d1714SBenjamin Tissoires 2636005a13cSJason Gerecke /* 2646005a13cSJason Gerecke * ISDv4 devices which predate HID's adoption of the 2656005a13cSJason Gerecke * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its 2666005a13cSJason Gerecke * position instead. We can accurately detect if a 2676005a13cSJason Gerecke * usage with that value should be HID_DG_BARRELSWITCH2 2686005a13cSJason Gerecke * based on the surrounding usages, which have remained 2696005a13cSJason Gerecke * constant across generations. 2706005a13cSJason Gerecke */ 2716005a13cSJason Gerecke if (features->type == HID_GENERIC && 2726005a13cSJason Gerecke usage->hid == 0x000D0000 && 2736005a13cSJason Gerecke field->application == HID_DG_PEN && 2746005a13cSJason Gerecke field->physical == HID_DG_STYLUS) { 2756005a13cSJason Gerecke int i = usage->usage_index; 2766005a13cSJason Gerecke 2776005a13cSJason Gerecke if (i-4 >= 0 && i+1 < field->maxusage && 2786005a13cSJason Gerecke field->usage[i-4].hid == HID_DG_TIPSWITCH && 2796005a13cSJason Gerecke field->usage[i-3].hid == HID_DG_BARRELSWITCH && 2806005a13cSJason Gerecke field->usage[i-2].hid == HID_DG_ERASER && 2816005a13cSJason Gerecke field->usage[i-1].hid == HID_DG_INVERT && 2826005a13cSJason Gerecke field->usage[i+1].hid == HID_DG_INRANGE) { 2836005a13cSJason Gerecke usage->hid = HID_DG_BARRELSWITCH2; 2846005a13cSJason Gerecke } 2856005a13cSJason Gerecke } 2866005a13cSJason Gerecke 287471d1714SBenjamin Tissoires switch (usage->hid) { 288471d1714SBenjamin Tissoires case HID_GD_X: 289471d1714SBenjamin Tissoires features->x_max = field->logical_maximum; 290471d1714SBenjamin Tissoires if (finger) { 291471d1714SBenjamin Tissoires features->x_phy = field->physical_maximum; 2923b164a00SPing Cheng if ((features->type != BAMBOO_PT) && 2933b164a00SPing Cheng (features->type != BAMBOO_TOUCH)) { 294471d1714SBenjamin Tissoires features->unit = field->unit; 295471d1714SBenjamin Tissoires features->unitExpo = field->unit_exponent; 296471d1714SBenjamin Tissoires } 297471d1714SBenjamin Tissoires } 298471d1714SBenjamin Tissoires break; 299471d1714SBenjamin Tissoires case HID_GD_Y: 300471d1714SBenjamin Tissoires features->y_max = field->logical_maximum; 301471d1714SBenjamin Tissoires if (finger) { 302471d1714SBenjamin Tissoires features->y_phy = field->physical_maximum; 3033b164a00SPing Cheng if ((features->type != BAMBOO_PT) && 3043b164a00SPing Cheng (features->type != BAMBOO_TOUCH)) { 305471d1714SBenjamin Tissoires features->unit = field->unit; 306471d1714SBenjamin Tissoires features->unitExpo = field->unit_exponent; 307471d1714SBenjamin Tissoires } 308471d1714SBenjamin Tissoires } 309471d1714SBenjamin Tissoires break; 310471d1714SBenjamin Tissoires case HID_DG_TIPPRESSURE: 311471d1714SBenjamin Tissoires if (pen) 312471d1714SBenjamin Tissoires features->pressure_max = field->logical_maximum; 313471d1714SBenjamin Tissoires break; 314471d1714SBenjamin Tissoires } 3157704ac93SBenjamin Tissoires 3167704ac93SBenjamin Tissoires if (features->type == HID_GENERIC) 3177704ac93SBenjamin Tissoires wacom_wac_usage_mapping(hdev, field, usage); 318471d1714SBenjamin Tissoires } 319471d1714SBenjamin Tissoires 320b58ba1baSJason Gerecke static void wacom_post_parse_hid(struct hid_device *hdev, 321b58ba1baSJason Gerecke struct wacom_features *features) 322b58ba1baSJason Gerecke { 323b58ba1baSJason Gerecke struct wacom *wacom = hid_get_drvdata(hdev); 324b58ba1baSJason Gerecke struct wacom_wac *wacom_wac = &wacom->wacom_wac; 325b58ba1baSJason Gerecke 326b58ba1baSJason Gerecke if (features->type == HID_GENERIC) { 327b58ba1baSJason Gerecke /* Any last-minute generic device setup */ 3284082da80SBenjamin Tissoires if (wacom_wac->has_mode_change) { 3294082da80SBenjamin Tissoires if (wacom_wac->is_direct_mode) 3304082da80SBenjamin Tissoires features->device_type |= WACOM_DEVICETYPE_DIRECT; 3314082da80SBenjamin Tissoires else 3324082da80SBenjamin Tissoires features->device_type &= ~WACOM_DEVICETYPE_DIRECT; 3334082da80SBenjamin Tissoires } 3344082da80SBenjamin Tissoires 335b58ba1baSJason Gerecke if (features->touch_max > 1) { 336ac2423c9SAaron Armstrong Skomra if (features->device_type & WACOM_DEVICETYPE_DIRECT) 337ac2423c9SAaron Armstrong Skomra input_mt_init_slots(wacom_wac->touch_input, 338ac2423c9SAaron Armstrong Skomra wacom_wac->features.touch_max, 339b58ba1baSJason Gerecke INPUT_MT_DIRECT); 340ac2423c9SAaron Armstrong Skomra else 341ac2423c9SAaron Armstrong Skomra input_mt_init_slots(wacom_wac->touch_input, 342ac2423c9SAaron Armstrong Skomra wacom_wac->features.touch_max, 343ac2423c9SAaron Armstrong Skomra INPUT_MT_POINTER); 344b58ba1baSJason Gerecke } 345b58ba1baSJason Gerecke } 346b58ba1baSJason Gerecke } 347b58ba1baSJason Gerecke 348471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev, 349471d1714SBenjamin Tissoires struct wacom_features *features) 350471d1714SBenjamin Tissoires { 351471d1714SBenjamin Tissoires struct hid_report_enum *rep_enum; 352471d1714SBenjamin Tissoires struct hid_report *hreport; 353471d1714SBenjamin Tissoires int i, j; 354471d1714SBenjamin Tissoires 355471d1714SBenjamin Tissoires /* check features first */ 356471d1714SBenjamin Tissoires rep_enum = &hdev->report_enum[HID_FEATURE_REPORT]; 357471d1714SBenjamin Tissoires list_for_each_entry(hreport, &rep_enum->report_list, list) { 358471d1714SBenjamin Tissoires for (i = 0; i < hreport->maxfield; i++) { 359471d1714SBenjamin Tissoires /* Ignore if report count is out of bounds. */ 360471d1714SBenjamin Tissoires if (hreport->field[i]->report_count < 1) 361471d1714SBenjamin Tissoires continue; 362471d1714SBenjamin Tissoires 363471d1714SBenjamin Tissoires for (j = 0; j < hreport->field[i]->maxusage; j++) { 364471d1714SBenjamin Tissoires wacom_feature_mapping(hdev, hreport->field[i], 365471d1714SBenjamin Tissoires hreport->field[i]->usage + j); 366471d1714SBenjamin Tissoires } 367471d1714SBenjamin Tissoires } 368471d1714SBenjamin Tissoires } 369471d1714SBenjamin Tissoires 370471d1714SBenjamin Tissoires /* now check the input usages */ 371471d1714SBenjamin Tissoires rep_enum = &hdev->report_enum[HID_INPUT_REPORT]; 372471d1714SBenjamin Tissoires list_for_each_entry(hreport, &rep_enum->report_list, list) { 373471d1714SBenjamin Tissoires 374471d1714SBenjamin Tissoires if (!hreport->maxfield) 375471d1714SBenjamin Tissoires continue; 376471d1714SBenjamin Tissoires 377471d1714SBenjamin Tissoires for (i = 0; i < hreport->maxfield; i++) 378471d1714SBenjamin Tissoires for (j = 0; j < hreport->field[i]->maxusage; j++) 379471d1714SBenjamin Tissoires wacom_usage_mapping(hdev, hreport->field[i], 380471d1714SBenjamin Tissoires hreport->field[i]->usage + j); 381471d1714SBenjamin Tissoires } 382b58ba1baSJason Gerecke 383b58ba1baSJason Gerecke wacom_post_parse_hid(hdev, features); 384471d1714SBenjamin Tissoires } 385471d1714SBenjamin Tissoires 3865ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev) 3875ae6e89fSBenjamin Tissoires { 3885ae6e89fSBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 3895ae6e89fSBenjamin Tissoires struct hid_data *hid_data = &wacom->wacom_wac.hid_data; 3905ae6e89fSBenjamin Tissoires struct hid_report *r; 3915ae6e89fSBenjamin Tissoires struct hid_report_enum *re; 3925ae6e89fSBenjamin Tissoires 3935ae6e89fSBenjamin Tissoires if (hid_data->inputmode < 0) 3945ae6e89fSBenjamin Tissoires return 0; 3955ae6e89fSBenjamin Tissoires 3965ae6e89fSBenjamin Tissoires re = &(hdev->report_enum[HID_FEATURE_REPORT]); 3975ae6e89fSBenjamin Tissoires r = re->report_id_hash[hid_data->inputmode]; 3985ae6e89fSBenjamin Tissoires if (r) { 3995ae6e89fSBenjamin Tissoires r->field[0]->value[hid_data->inputmode_index] = 2; 4005ae6e89fSBenjamin Tissoires hid_hw_request(hdev, r, HID_REQ_SET_REPORT); 4015ae6e89fSBenjamin Tissoires } 4025ae6e89fSBenjamin Tissoires return 0; 4035ae6e89fSBenjamin Tissoires } 4045ae6e89fSBenjamin Tissoires 405326ea2a9SJason Gerecke static int wacom_set_device_mode(struct hid_device *hdev, 406326ea2a9SJason Gerecke struct wacom_wac *wacom_wac) 407471d1714SBenjamin Tissoires { 408326ea2a9SJason Gerecke u8 *rep_data; 409326ea2a9SJason Gerecke struct hid_report *r; 410326ea2a9SJason Gerecke struct hid_report_enum *re; 411326ea2a9SJason Gerecke int length; 412471d1714SBenjamin Tissoires int error = -ENOMEM, limit = 0; 413471d1714SBenjamin Tissoires 414326ea2a9SJason Gerecke if (wacom_wac->mode_report < 0) 415326ea2a9SJason Gerecke return 0; 416326ea2a9SJason Gerecke 417326ea2a9SJason Gerecke re = &(hdev->report_enum[HID_FEATURE_REPORT]); 418326ea2a9SJason Gerecke r = re->report_id_hash[wacom_wac->mode_report]; 419326ea2a9SJason Gerecke if (!r) 420326ea2a9SJason Gerecke return -EINVAL; 421326ea2a9SJason Gerecke 422326ea2a9SJason Gerecke rep_data = hid_alloc_report_buf(r, GFP_KERNEL); 423471d1714SBenjamin Tissoires if (!rep_data) 424326ea2a9SJason Gerecke return -ENOMEM; 425326ea2a9SJason Gerecke 426326ea2a9SJason Gerecke length = hid_report_len(r); 427471d1714SBenjamin Tissoires 428471d1714SBenjamin Tissoires do { 429326ea2a9SJason Gerecke rep_data[0] = wacom_wac->mode_report; 430326ea2a9SJason Gerecke rep_data[1] = wacom_wac->mode_value; 431471d1714SBenjamin Tissoires 432296b7378SPrzemo Firszt error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 433296b7378SPrzemo Firszt length, 1); 434471d1714SBenjamin Tissoires if (error >= 0) 435471d1714SBenjamin Tissoires error = wacom_get_report(hdev, HID_FEATURE_REPORT, 436c64d8834SPing Cheng rep_data, length, 1); 437326ea2a9SJason Gerecke } while (error >= 0 && 438326ea2a9SJason Gerecke rep_data[1] != wacom_wac->mode_report && 439326ea2a9SJason Gerecke limit++ < WAC_MSG_RETRIES); 440471d1714SBenjamin Tissoires 441471d1714SBenjamin Tissoires kfree(rep_data); 442471d1714SBenjamin Tissoires 443471d1714SBenjamin Tissoires return error < 0 ? error : 0; 444471d1714SBenjamin Tissoires } 445471d1714SBenjamin Tissoires 446f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed, 447f81a1295SBenjamin Tissoires struct wacom_features *features) 448f81a1295SBenjamin Tissoires { 449387142bbSBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 450387142bbSBenjamin Tissoires int ret; 451387142bbSBenjamin Tissoires u8 rep_data[2]; 452387142bbSBenjamin Tissoires 453387142bbSBenjamin Tissoires switch (features->type) { 454387142bbSBenjamin Tissoires case GRAPHIRE_BT: 455387142bbSBenjamin Tissoires rep_data[0] = 0x03; 456387142bbSBenjamin Tissoires rep_data[1] = 0x00; 457296b7378SPrzemo Firszt ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2, 458296b7378SPrzemo Firszt 3); 459387142bbSBenjamin Tissoires 460387142bbSBenjamin Tissoires if (ret >= 0) { 461387142bbSBenjamin Tissoires rep_data[0] = speed == 0 ? 0x05 : 0x06; 462387142bbSBenjamin Tissoires rep_data[1] = 0x00; 463387142bbSBenjamin Tissoires 464387142bbSBenjamin Tissoires ret = wacom_set_report(hdev, HID_FEATURE_REPORT, 465296b7378SPrzemo Firszt rep_data, 2, 3); 466387142bbSBenjamin Tissoires 467387142bbSBenjamin Tissoires if (ret >= 0) { 468387142bbSBenjamin Tissoires wacom->wacom_wac.bt_high_speed = speed; 469387142bbSBenjamin Tissoires return 0; 470387142bbSBenjamin Tissoires } 471387142bbSBenjamin Tissoires } 472387142bbSBenjamin Tissoires 473387142bbSBenjamin Tissoires /* 474387142bbSBenjamin Tissoires * Note that if the raw queries fail, it's not a hard failure 475387142bbSBenjamin Tissoires * and it is safe to continue 476387142bbSBenjamin Tissoires */ 477387142bbSBenjamin Tissoires hid_warn(hdev, "failed to poke device, command %d, err %d\n", 478387142bbSBenjamin Tissoires rep_data[0], ret); 479387142bbSBenjamin Tissoires break; 48081af7e61SBenjamin Tissoires case INTUOS4WL: 48181af7e61SBenjamin Tissoires if (speed == 1) 48281af7e61SBenjamin Tissoires wacom->wacom_wac.bt_features &= ~0x20; 48381af7e61SBenjamin Tissoires else 48481af7e61SBenjamin Tissoires wacom->wacom_wac.bt_features |= 0x20; 48581af7e61SBenjamin Tissoires 48681af7e61SBenjamin Tissoires rep_data[0] = 0x03; 48781af7e61SBenjamin Tissoires rep_data[1] = wacom->wacom_wac.bt_features; 48881af7e61SBenjamin Tissoires 489296b7378SPrzemo Firszt ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2, 490296b7378SPrzemo Firszt 1); 49181af7e61SBenjamin Tissoires if (ret >= 0) 49281af7e61SBenjamin Tissoires wacom->wacom_wac.bt_high_speed = speed; 49381af7e61SBenjamin Tissoires break; 494387142bbSBenjamin Tissoires } 495387142bbSBenjamin Tissoires 496f81a1295SBenjamin Tissoires return 0; 497f81a1295SBenjamin Tissoires } 498f81a1295SBenjamin Tissoires 499471d1714SBenjamin Tissoires /* 500471d1714SBenjamin Tissoires * Switch the tablet into its most-capable mode. Wacom tablets are 501471d1714SBenjamin Tissoires * typically configured to power-up in a mode which sends mouse-like 502471d1714SBenjamin Tissoires * reports to the OS. To get absolute position, pressure data, etc. 503471d1714SBenjamin Tissoires * from the tablet, it is necessary to switch the tablet out of this 504471d1714SBenjamin Tissoires * mode and into one which sends the full range of tablet data. 505471d1714SBenjamin Tissoires */ 506a544c619SBenjamin Tissoires static int _wacom_query_tablet_data(struct wacom *wacom) 507471d1714SBenjamin Tissoires { 508a544c619SBenjamin Tissoires struct hid_device *hdev = wacom->hdev; 509326ea2a9SJason Gerecke struct wacom_wac *wacom_wac = &wacom->wacom_wac; 510a544c619SBenjamin Tissoires struct wacom_features *features = &wacom_wac->features; 511326ea2a9SJason Gerecke 512f81a1295SBenjamin Tissoires if (hdev->bus == BUS_BLUETOOTH) 513f81a1295SBenjamin Tissoires return wacom_bt_query_tablet_data(hdev, 1, features); 514f81a1295SBenjamin Tissoires 515326ea2a9SJason Gerecke if (features->type != HID_GENERIC) { 516aa86b18cSJason Gerecke if (features->device_type & WACOM_DEVICETYPE_TOUCH) { 517471d1714SBenjamin Tissoires if (features->type > TABLETPC) { 518471d1714SBenjamin Tissoires /* MT Tablet PC touch */ 519326ea2a9SJason Gerecke wacom_wac->mode_report = 3; 520326ea2a9SJason Gerecke wacom_wac->mode_value = 4; 521326ea2a9SJason Gerecke } else if (features->type == WACOM_24HDT) { 522326ea2a9SJason Gerecke wacom_wac->mode_report = 18; 523326ea2a9SJason Gerecke wacom_wac->mode_value = 2; 524326ea2a9SJason Gerecke } else if (features->type == WACOM_27QHDT) { 525326ea2a9SJason Gerecke wacom_wac->mode_report = 131; 526326ea2a9SJason Gerecke wacom_wac->mode_value = 2; 527326ea2a9SJason Gerecke } else if (features->type == BAMBOO_PAD) { 528326ea2a9SJason Gerecke wacom_wac->mode_report = 2; 529326ea2a9SJason Gerecke wacom_wac->mode_value = 2; 5308c97a765SBenjamin Tissoires } 531aa86b18cSJason Gerecke } else if (features->device_type & WACOM_DEVICETYPE_PEN) { 5327adb91bdSPing Cheng if (features->type <= BAMBOO_PT) { 533326ea2a9SJason Gerecke wacom_wac->mode_report = 2; 534326ea2a9SJason Gerecke wacom_wac->mode_value = 2; 535471d1714SBenjamin Tissoires } 536471d1714SBenjamin Tissoires } 537326ea2a9SJason Gerecke } 538326ea2a9SJason Gerecke 539326ea2a9SJason Gerecke wacom_set_device_mode(hdev, wacom_wac); 540326ea2a9SJason Gerecke 541326ea2a9SJason Gerecke if (features->type == HID_GENERIC) 542326ea2a9SJason Gerecke return wacom_hid_set_device_mode(hdev); 543471d1714SBenjamin Tissoires 544471d1714SBenjamin Tissoires return 0; 545471d1714SBenjamin Tissoires } 546471d1714SBenjamin Tissoires 547471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev, 548471d1714SBenjamin Tissoires struct wacom_features *features) 549471d1714SBenjamin Tissoires { 550471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 551471d1714SBenjamin Tissoires struct usb_interface *intf = wacom->intf; 552471d1714SBenjamin Tissoires 553471d1714SBenjamin Tissoires /* default features */ 554471d1714SBenjamin Tissoires features->x_fuzz = 4; 555471d1714SBenjamin Tissoires features->y_fuzz = 4; 556471d1714SBenjamin Tissoires features->pressure_fuzz = 0; 557bef7e200SJason Gerecke features->distance_fuzz = 1; 558bef7e200SJason Gerecke features->tilt_fuzz = 1; 559471d1714SBenjamin Tissoires 560471d1714SBenjamin Tissoires /* 561471d1714SBenjamin Tissoires * The wireless device HID is basic and layout conflicts with 562471d1714SBenjamin Tissoires * other tablets (monitor and touch interface can look like pen). 563471d1714SBenjamin Tissoires * Skip the query for this type and modify defaults based on 564471d1714SBenjamin Tissoires * interface number. 565471d1714SBenjamin Tissoires */ 566471d1714SBenjamin Tissoires if (features->type == WIRELESS) { 5673f14a63aSJason Gerecke if (intf->cur_altsetting->desc.bInterfaceNumber == 0) 568ccad85ccSJason Gerecke features->device_type = WACOM_DEVICETYPE_WL_MONITOR; 5693f14a63aSJason Gerecke else 570aa86b18cSJason Gerecke features->device_type = WACOM_DEVICETYPE_NONE; 5713f14a63aSJason Gerecke return; 572471d1714SBenjamin Tissoires } 573471d1714SBenjamin Tissoires 574471d1714SBenjamin Tissoires wacom_parse_hid(hdev, features); 575471d1714SBenjamin Tissoires } 576471d1714SBenjamin Tissoires 577471d1714SBenjamin Tissoires struct wacom_hdev_data { 578471d1714SBenjamin Tissoires struct list_head list; 579471d1714SBenjamin Tissoires struct kref kref; 580471d1714SBenjamin Tissoires struct hid_device *dev; 581471d1714SBenjamin Tissoires struct wacom_shared shared; 582471d1714SBenjamin Tissoires }; 583471d1714SBenjamin Tissoires 584471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list); 585471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock); 586471d1714SBenjamin Tissoires 58741372d5dSJason Gerecke static bool compare_device_paths(struct hid_device *hdev_a, 58841372d5dSJason Gerecke struct hid_device *hdev_b, char separator) 58941372d5dSJason Gerecke { 59041372d5dSJason Gerecke int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys; 59141372d5dSJason Gerecke int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys; 59241372d5dSJason Gerecke 59341372d5dSJason Gerecke if (n1 != n2 || n1 <= 0 || n2 <= 0) 59441372d5dSJason Gerecke return false; 59541372d5dSJason Gerecke 59641372d5dSJason Gerecke return !strncmp(hdev_a->phys, hdev_b->phys, n1); 59741372d5dSJason Gerecke } 59841372d5dSJason Gerecke 599471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev, 600471d1714SBenjamin Tissoires struct hid_device *sibling) 601471d1714SBenjamin Tissoires { 602471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 603471d1714SBenjamin Tissoires struct wacom_features *features = &wacom->wacom_wac.features; 60441372d5dSJason Gerecke struct wacom *sibling_wacom = hid_get_drvdata(sibling); 60541372d5dSJason Gerecke struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features; 60641372d5dSJason Gerecke __u32 oVid = features->oVid ? features->oVid : hdev->vendor; 60741372d5dSJason Gerecke __u32 oPid = features->oPid ? features->oPid : hdev->product; 608471d1714SBenjamin Tissoires 60941372d5dSJason Gerecke /* The defined oVid/oPid must match that of the sibling */ 61041372d5dSJason Gerecke if (features->oVid != HID_ANY_ID && sibling->vendor != oVid) 61141372d5dSJason Gerecke return false; 61241372d5dSJason Gerecke if (features->oPid != HID_ANY_ID && sibling->product != oPid) 61341372d5dSJason Gerecke return false; 61441372d5dSJason Gerecke 61541372d5dSJason Gerecke /* 61641372d5dSJason Gerecke * Devices with the same VID/PID must share the same physical 61741372d5dSJason Gerecke * device path, while those with different VID/PID must share 61841372d5dSJason Gerecke * the same physical parent device path. 61941372d5dSJason Gerecke */ 62041372d5dSJason Gerecke if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) { 62141372d5dSJason Gerecke if (!compare_device_paths(hdev, sibling, '/')) 62241372d5dSJason Gerecke return false; 62341372d5dSJason Gerecke } else { 62441372d5dSJason Gerecke if (!compare_device_paths(hdev, sibling, '.')) 62541372d5dSJason Gerecke return false; 626471d1714SBenjamin Tissoires } 627471d1714SBenjamin Tissoires 62841372d5dSJason Gerecke /* Skip the remaining heuristics unless you are a HID_GENERIC device */ 62941372d5dSJason Gerecke if (features->type != HID_GENERIC) 63041372d5dSJason Gerecke return true; 63141372d5dSJason Gerecke 63241372d5dSJason Gerecke /* 63341372d5dSJason Gerecke * Direct-input devices may not be siblings of indirect-input 63441372d5dSJason Gerecke * devices. 63541372d5dSJason Gerecke */ 63641372d5dSJason Gerecke if ((features->device_type & WACOM_DEVICETYPE_DIRECT) && 63741372d5dSJason Gerecke !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT)) 638471d1714SBenjamin Tissoires return false; 639471d1714SBenjamin Tissoires 64041372d5dSJason Gerecke /* 64141372d5dSJason Gerecke * Indirect-input devices may not be siblings of direct-input 64241372d5dSJason Gerecke * devices. 64341372d5dSJason Gerecke */ 64441372d5dSJason Gerecke if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) && 64541372d5dSJason Gerecke (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT)) 646471d1714SBenjamin Tissoires return false; 647471d1714SBenjamin Tissoires 64841372d5dSJason Gerecke /* Pen devices may only be siblings of touch devices */ 64941372d5dSJason Gerecke if ((features->device_type & WACOM_DEVICETYPE_PEN) && 65041372d5dSJason Gerecke !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH)) 65141372d5dSJason Gerecke return false; 65241372d5dSJason Gerecke 65341372d5dSJason Gerecke /* Touch devices may only be siblings of pen devices */ 65441372d5dSJason Gerecke if ((features->device_type & WACOM_DEVICETYPE_TOUCH) && 65541372d5dSJason Gerecke !(sibling_features->device_type & WACOM_DEVICETYPE_PEN)) 65641372d5dSJason Gerecke return false; 65741372d5dSJason Gerecke 65841372d5dSJason Gerecke /* 65941372d5dSJason Gerecke * No reason could be found for these two devices to NOT be 66041372d5dSJason Gerecke * siblings, so there's a good chance they ARE siblings 66141372d5dSJason Gerecke */ 66241372d5dSJason Gerecke return true; 663471d1714SBenjamin Tissoires } 664471d1714SBenjamin Tissoires 665471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev) 666471d1714SBenjamin Tissoires { 667471d1714SBenjamin Tissoires struct wacom_hdev_data *data; 668471d1714SBenjamin Tissoires 66941372d5dSJason Gerecke /* Try to find an already-probed interface from the same device */ 67041372d5dSJason Gerecke list_for_each_entry(data, &wacom_udev_list, list) { 67141372d5dSJason Gerecke if (compare_device_paths(hdev, data->dev, '/')) 67241372d5dSJason Gerecke return data; 67341372d5dSJason Gerecke } 67441372d5dSJason Gerecke 67541372d5dSJason Gerecke /* Fallback to finding devices that appear to be "siblings" */ 676471d1714SBenjamin Tissoires list_for_each_entry(data, &wacom_udev_list, list) { 677471d1714SBenjamin Tissoires if (wacom_are_sibling(hdev, data->dev)) { 678471d1714SBenjamin Tissoires kref_get(&data->kref); 679471d1714SBenjamin Tissoires return data; 680471d1714SBenjamin Tissoires } 681471d1714SBenjamin Tissoires } 682471d1714SBenjamin Tissoires 683471d1714SBenjamin Tissoires return NULL; 684471d1714SBenjamin Tissoires } 685471d1714SBenjamin Tissoires 6861c817c83SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref) 6871c817c83SBenjamin Tissoires { 6881c817c83SBenjamin Tissoires struct wacom_hdev_data *data = 6891c817c83SBenjamin Tissoires container_of(kref, struct wacom_hdev_data, kref); 6901c817c83SBenjamin Tissoires 6911c817c83SBenjamin Tissoires mutex_lock(&wacom_udev_list_lock); 6921c817c83SBenjamin Tissoires list_del(&data->list); 6931c817c83SBenjamin Tissoires mutex_unlock(&wacom_udev_list_lock); 6941c817c83SBenjamin Tissoires 6951c817c83SBenjamin Tissoires kfree(data); 6961c817c83SBenjamin Tissoires } 6971c817c83SBenjamin Tissoires 6981c817c83SBenjamin Tissoires static void wacom_remove_shared_data(void *res) 6991c817c83SBenjamin Tissoires { 7001c817c83SBenjamin Tissoires struct wacom *wacom = res; 7011c817c83SBenjamin Tissoires struct wacom_hdev_data *data; 7021c817c83SBenjamin Tissoires struct wacom_wac *wacom_wac = &wacom->wacom_wac; 7031c817c83SBenjamin Tissoires 7041c817c83SBenjamin Tissoires if (wacom_wac->shared) { 7051c817c83SBenjamin Tissoires data = container_of(wacom_wac->shared, struct wacom_hdev_data, 7061c817c83SBenjamin Tissoires shared); 7071c817c83SBenjamin Tissoires 7081c817c83SBenjamin Tissoires if (wacom_wac->shared->touch == wacom->hdev) 7091c817c83SBenjamin Tissoires wacom_wac->shared->touch = NULL; 7101c817c83SBenjamin Tissoires else if (wacom_wac->shared->pen == wacom->hdev) 7111c817c83SBenjamin Tissoires wacom_wac->shared->pen = NULL; 7121c817c83SBenjamin Tissoires 7131c817c83SBenjamin Tissoires kref_put(&data->kref, wacom_release_shared_data); 7141c817c83SBenjamin Tissoires wacom_wac->shared = NULL; 7151c817c83SBenjamin Tissoires } 7161c817c83SBenjamin Tissoires } 7171c817c83SBenjamin Tissoires 718471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev) 719471d1714SBenjamin Tissoires { 720471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 721471d1714SBenjamin Tissoires struct wacom_wac *wacom_wac = &wacom->wacom_wac; 722471d1714SBenjamin Tissoires struct wacom_hdev_data *data; 723471d1714SBenjamin Tissoires int retval = 0; 724471d1714SBenjamin Tissoires 725471d1714SBenjamin Tissoires mutex_lock(&wacom_udev_list_lock); 726471d1714SBenjamin Tissoires 727471d1714SBenjamin Tissoires data = wacom_get_hdev_data(hdev); 728471d1714SBenjamin Tissoires if (!data) { 729471d1714SBenjamin Tissoires data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL); 730471d1714SBenjamin Tissoires if (!data) { 731471d1714SBenjamin Tissoires retval = -ENOMEM; 732471d1714SBenjamin Tissoires goto out; 733471d1714SBenjamin Tissoires } 734471d1714SBenjamin Tissoires 735471d1714SBenjamin Tissoires kref_init(&data->kref); 736471d1714SBenjamin Tissoires data->dev = hdev; 737471d1714SBenjamin Tissoires list_add_tail(&data->list, &wacom_udev_list); 738471d1714SBenjamin Tissoires } 739471d1714SBenjamin Tissoires 740471d1714SBenjamin Tissoires wacom_wac->shared = &data->shared; 741471d1714SBenjamin Tissoires 7421c817c83SBenjamin Tissoires retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom); 7431c817c83SBenjamin Tissoires if (retval) { 7441c817c83SBenjamin Tissoires mutex_unlock(&wacom_udev_list_lock); 7451c817c83SBenjamin Tissoires wacom_remove_shared_data(wacom); 7461c817c83SBenjamin Tissoires return retval; 7471c817c83SBenjamin Tissoires } 7481c817c83SBenjamin Tissoires 749a9ce7856SJason Gerecke if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) 750a9ce7856SJason Gerecke wacom_wac->shared->touch = hdev; 751a9ce7856SJason Gerecke else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN) 752a9ce7856SJason Gerecke wacom_wac->shared->pen = hdev; 753a9ce7856SJason Gerecke 754471d1714SBenjamin Tissoires out: 755471d1714SBenjamin Tissoires mutex_unlock(&wacom_udev_list_lock); 756471d1714SBenjamin Tissoires return retval; 757471d1714SBenjamin Tissoires } 758471d1714SBenjamin Tissoires 759471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom) 760471d1714SBenjamin Tissoires { 761471d1714SBenjamin Tissoires unsigned char *buf; 762471d1714SBenjamin Tissoires int retval; 763912ca216SPing Cheng unsigned char report_id = WAC_CMD_LED_CONTROL; 764912ca216SPing Cheng int buf_size = 9; 765471d1714SBenjamin Tissoires 766a50aac71SBenjamin Tissoires if (!wacom->led.groups) 767a50aac71SBenjamin Tissoires return -ENOTSUPP; 768a50aac71SBenjamin Tissoires 769912ca216SPing Cheng if (wacom->wacom_wac.pid) { /* wireless connected */ 770912ca216SPing Cheng report_id = WAC_CMD_WL_LED_CONTROL; 771912ca216SPing Cheng buf_size = 13; 772912ca216SPing Cheng } 7734922cd26SJason Gerecke else if (wacom->wacom_wac.features.type == INTUOSP2_BT) { 7744922cd26SJason Gerecke report_id = WAC_CMD_WL_INTUOSP2; 7754922cd26SJason Gerecke buf_size = 51; 7764922cd26SJason Gerecke } 777912ca216SPing Cheng buf = kzalloc(buf_size, GFP_KERNEL); 778471d1714SBenjamin Tissoires if (!buf) 779471d1714SBenjamin Tissoires return -ENOMEM; 780471d1714SBenjamin Tissoires 78110c55cacSAaron Armstrong Skomra if (wacom->wacom_wac.features.type == HID_GENERIC) { 78210c55cacSAaron Armstrong Skomra buf[0] = WAC_CMD_LED_CONTROL_GENERIC; 78310c55cacSAaron Armstrong Skomra buf[1] = wacom->led.llv; 78410c55cacSAaron Armstrong Skomra buf[2] = wacom->led.groups[0].select & 0x03; 78510c55cacSAaron Armstrong Skomra 78610c55cacSAaron Armstrong Skomra } else if ((wacom->wacom_wac.features.type >= INTUOS5S && 78710c55cacSAaron Armstrong Skomra wacom->wacom_wac.features.type <= INTUOSPL)) { 788471d1714SBenjamin Tissoires /* 789471d1714SBenjamin Tissoires * Touch Ring and crop mark LED luminance may take on 790471d1714SBenjamin Tissoires * one of four values: 791471d1714SBenjamin Tissoires * 0 = Low; 1 = Medium; 2 = High; 3 = Off 792471d1714SBenjamin Tissoires */ 793a50aac71SBenjamin Tissoires int ring_led = wacom->led.groups[0].select & 0x03; 794471d1714SBenjamin Tissoires int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03; 795471d1714SBenjamin Tissoires int crop_lum = 0; 796912ca216SPing Cheng unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led); 797471d1714SBenjamin Tissoires 798912ca216SPing Cheng buf[0] = report_id; 799912ca216SPing Cheng if (wacom->wacom_wac.pid) { 800912ca216SPing Cheng wacom_get_report(wacom->hdev, HID_FEATURE_REPORT, 801912ca216SPing Cheng buf, buf_size, WAC_CMD_RETRIES); 802912ca216SPing Cheng buf[0] = report_id; 803912ca216SPing Cheng buf[4] = led_bits; 804912ca216SPing Cheng } else 805912ca216SPing Cheng buf[1] = led_bits; 806471d1714SBenjamin Tissoires } 8074922cd26SJason Gerecke else if (wacom->wacom_wac.features.type == INTUOSP2_BT) { 8084922cd26SJason Gerecke buf[0] = report_id; 8094922cd26SJason Gerecke buf[4] = 100; // Power Connection LED (ORANGE) 8104922cd26SJason Gerecke buf[5] = 100; // BT Connection LED (BLUE) 8114922cd26SJason Gerecke buf[6] = 100; // Paper Mode (RED?) 8124922cd26SJason Gerecke buf[7] = 100; // Paper Mode (GREEN?) 8134922cd26SJason Gerecke buf[8] = 100; // Paper Mode (BLUE?) 8144922cd26SJason Gerecke buf[9] = wacom->led.llv; 8154922cd26SJason Gerecke buf[10] = wacom->led.groups[0].select & 0x03; 8164922cd26SJason Gerecke } 817471d1714SBenjamin Tissoires else { 818a50aac71SBenjamin Tissoires int led = wacom->led.groups[0].select | 0x4; 819471d1714SBenjamin Tissoires 820471d1714SBenjamin Tissoires if (wacom->wacom_wac.features.type == WACOM_21UX2 || 821471d1714SBenjamin Tissoires wacom->wacom_wac.features.type == WACOM_24HD) 822a50aac71SBenjamin Tissoires led |= (wacom->led.groups[1].select << 4) | 0x40; 823471d1714SBenjamin Tissoires 824912ca216SPing Cheng buf[0] = report_id; 825471d1714SBenjamin Tissoires buf[1] = led; 826471d1714SBenjamin Tissoires buf[2] = wacom->led.llv; 827471d1714SBenjamin Tissoires buf[3] = wacom->led.hlv; 828471d1714SBenjamin Tissoires buf[4] = wacom->led.img_lum; 829471d1714SBenjamin Tissoires } 830471d1714SBenjamin Tissoires 831912ca216SPing Cheng retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size, 832296b7378SPrzemo Firszt WAC_CMD_RETRIES); 833471d1714SBenjamin Tissoires kfree(buf); 834471d1714SBenjamin Tissoires 835471d1714SBenjamin Tissoires return retval; 836471d1714SBenjamin Tissoires } 837471d1714SBenjamin Tissoires 838849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id, 839849e2f06SBenjamin Tissoires const unsigned len, const void *img) 840471d1714SBenjamin Tissoires { 841471d1714SBenjamin Tissoires unsigned char *buf; 842471d1714SBenjamin Tissoires int i, retval; 843849e2f06SBenjamin Tissoires const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */ 844471d1714SBenjamin Tissoires 845849e2f06SBenjamin Tissoires buf = kzalloc(chunk_len + 3 , GFP_KERNEL); 846471d1714SBenjamin Tissoires if (!buf) 847471d1714SBenjamin Tissoires return -ENOMEM; 848471d1714SBenjamin Tissoires 849471d1714SBenjamin Tissoires /* Send 'start' command */ 850471d1714SBenjamin Tissoires buf[0] = WAC_CMD_ICON_START; 851471d1714SBenjamin Tissoires buf[1] = 1; 852296b7378SPrzemo Firszt retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2, 853296b7378SPrzemo Firszt WAC_CMD_RETRIES); 854471d1714SBenjamin Tissoires if (retval < 0) 855471d1714SBenjamin Tissoires goto out; 856471d1714SBenjamin Tissoires 857849e2f06SBenjamin Tissoires buf[0] = xfer_id; 858471d1714SBenjamin Tissoires buf[1] = button_id & 0x07; 859471d1714SBenjamin Tissoires for (i = 0; i < 4; i++) { 860471d1714SBenjamin Tissoires buf[2] = i; 861849e2f06SBenjamin Tissoires memcpy(buf + 3, img + i * chunk_len, chunk_len); 862471d1714SBenjamin Tissoires 863471d1714SBenjamin Tissoires retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, 864296b7378SPrzemo Firszt buf, chunk_len + 3, WAC_CMD_RETRIES); 865471d1714SBenjamin Tissoires if (retval < 0) 866471d1714SBenjamin Tissoires break; 867471d1714SBenjamin Tissoires } 868471d1714SBenjamin Tissoires 869471d1714SBenjamin Tissoires /* Send 'stop' */ 870471d1714SBenjamin Tissoires buf[0] = WAC_CMD_ICON_START; 871471d1714SBenjamin Tissoires buf[1] = 0; 872296b7378SPrzemo Firszt wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2, 873296b7378SPrzemo Firszt WAC_CMD_RETRIES); 874471d1714SBenjamin Tissoires 875471d1714SBenjamin Tissoires out: 876471d1714SBenjamin Tissoires kfree(buf); 877471d1714SBenjamin Tissoires return retval; 878471d1714SBenjamin Tissoires } 879471d1714SBenjamin Tissoires 880471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id, 881471d1714SBenjamin Tissoires const char *buf, size_t count) 882471d1714SBenjamin Tissoires { 883ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev); 884471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 885471d1714SBenjamin Tissoires unsigned int id; 886471d1714SBenjamin Tissoires int err; 887471d1714SBenjamin Tissoires 888471d1714SBenjamin Tissoires err = kstrtouint(buf, 10, &id); 889471d1714SBenjamin Tissoires if (err) 890471d1714SBenjamin Tissoires return err; 891471d1714SBenjamin Tissoires 892471d1714SBenjamin Tissoires mutex_lock(&wacom->lock); 893471d1714SBenjamin Tissoires 894a50aac71SBenjamin Tissoires wacom->led.groups[set_id].select = id & 0x3; 895471d1714SBenjamin Tissoires err = wacom_led_control(wacom); 896471d1714SBenjamin Tissoires 897471d1714SBenjamin Tissoires mutex_unlock(&wacom->lock); 898471d1714SBenjamin Tissoires 899471d1714SBenjamin Tissoires return err < 0 ? err : count; 900471d1714SBenjamin Tissoires } 901471d1714SBenjamin Tissoires 902471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID) \ 903471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \ 904471d1714SBenjamin Tissoires struct device_attribute *attr, const char *buf, size_t count) \ 905471d1714SBenjamin Tissoires { \ 906471d1714SBenjamin Tissoires return wacom_led_select_store(dev, SET_ID, buf, count); \ 907471d1714SBenjamin Tissoires } \ 908471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \ 909471d1714SBenjamin Tissoires struct device_attribute *attr, char *buf) \ 910471d1714SBenjamin Tissoires { \ 911ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev);\ 912471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); \ 91337449adcSPing Cheng return scnprintf(buf, PAGE_SIZE, "%d\n", \ 914a50aac71SBenjamin Tissoires wacom->led.groups[SET_ID].select); \ 915471d1714SBenjamin Tissoires } \ 916e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \ 917471d1714SBenjamin Tissoires wacom_led##SET_ID##_select_show, \ 918471d1714SBenjamin Tissoires wacom_led##SET_ID##_select_store) 919471d1714SBenjamin Tissoires 920471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0); 921471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1); 922471d1714SBenjamin Tissoires 923471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest, 924471d1714SBenjamin Tissoires const char *buf, size_t count) 925471d1714SBenjamin Tissoires { 926471d1714SBenjamin Tissoires unsigned int value; 927471d1714SBenjamin Tissoires int err; 928471d1714SBenjamin Tissoires 929471d1714SBenjamin Tissoires err = kstrtouint(buf, 10, &value); 930471d1714SBenjamin Tissoires if (err) 931471d1714SBenjamin Tissoires return err; 932471d1714SBenjamin Tissoires 933471d1714SBenjamin Tissoires mutex_lock(&wacom->lock); 934471d1714SBenjamin Tissoires 935471d1714SBenjamin Tissoires *dest = value & 0x7f; 936471d1714SBenjamin Tissoires err = wacom_led_control(wacom); 937471d1714SBenjamin Tissoires 938471d1714SBenjamin Tissoires mutex_unlock(&wacom->lock); 939471d1714SBenjamin Tissoires 940471d1714SBenjamin Tissoires return err < 0 ? err : count; 941471d1714SBenjamin Tissoires } 942471d1714SBenjamin Tissoires 943471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field) \ 944471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev, \ 945471d1714SBenjamin Tissoires struct device_attribute *attr, const char *buf, size_t count) \ 946471d1714SBenjamin Tissoires { \ 947ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev);\ 948471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); \ 949471d1714SBenjamin Tissoires \ 950471d1714SBenjamin Tissoires return wacom_luminance_store(wacom, &wacom->led.field, \ 951471d1714SBenjamin Tissoires buf, count); \ 952471d1714SBenjamin Tissoires } \ 95337449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev, \ 95437449adcSPing Cheng struct device_attribute *attr, char *buf) \ 95537449adcSPing Cheng { \ 95637449adcSPing Cheng struct wacom *wacom = dev_get_drvdata(dev); \ 95737449adcSPing Cheng return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \ 95837449adcSPing Cheng } \ 959e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \ 96037449adcSPing Cheng wacom_##name##_luminance_show, \ 96137449adcSPing Cheng wacom_##name##_luminance_store) 962471d1714SBenjamin Tissoires 963471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv); 964471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv); 965471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum); 966471d1714SBenjamin Tissoires 967471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id, 968471d1714SBenjamin Tissoires const char *buf, size_t count) 969471d1714SBenjamin Tissoires { 970ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev); 971471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 972471d1714SBenjamin Tissoires int err; 973849e2f06SBenjamin Tissoires unsigned len; 974849e2f06SBenjamin Tissoires u8 xfer_id; 975471d1714SBenjamin Tissoires 976849e2f06SBenjamin Tissoires if (hdev->bus == BUS_BLUETOOTH) { 977849e2f06SBenjamin Tissoires len = 256; 978849e2f06SBenjamin Tissoires xfer_id = WAC_CMD_ICON_BT_XFER; 979849e2f06SBenjamin Tissoires } else { 980849e2f06SBenjamin Tissoires len = 1024; 981849e2f06SBenjamin Tissoires xfer_id = WAC_CMD_ICON_XFER; 982849e2f06SBenjamin Tissoires } 983849e2f06SBenjamin Tissoires 984849e2f06SBenjamin Tissoires if (count != len) 985471d1714SBenjamin Tissoires return -EINVAL; 986471d1714SBenjamin Tissoires 987471d1714SBenjamin Tissoires mutex_lock(&wacom->lock); 988471d1714SBenjamin Tissoires 989849e2f06SBenjamin Tissoires err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf); 990471d1714SBenjamin Tissoires 991471d1714SBenjamin Tissoires mutex_unlock(&wacom->lock); 992471d1714SBenjamin Tissoires 993471d1714SBenjamin Tissoires return err < 0 ? err : count; 994471d1714SBenjamin Tissoires } 995471d1714SBenjamin Tissoires 996471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \ 997471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \ 998471d1714SBenjamin Tissoires struct device_attribute *attr, const char *buf, size_t count) \ 999471d1714SBenjamin Tissoires { \ 1000471d1714SBenjamin Tissoires return wacom_button_image_store(dev, BUTTON_ID, buf, count); \ 1001471d1714SBenjamin Tissoires } \ 1002e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \ 1003471d1714SBenjamin Tissoires NULL, wacom_btnimg##BUTTON_ID##_store) 1004471d1714SBenjamin Tissoires 1005471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0); 1006471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1); 1007471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2); 1008471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3); 1009471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4); 1010471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5); 1011471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6); 1012471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7); 1013471d1714SBenjamin Tissoires 1014471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = { 1015471d1714SBenjamin Tissoires &dev_attr_status_led0_select.attr, 1016471d1714SBenjamin Tissoires &dev_attr_status_led1_select.attr, 1017471d1714SBenjamin Tissoires NULL 1018471d1714SBenjamin Tissoires }; 1019471d1714SBenjamin Tissoires 1020471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = { 1021471d1714SBenjamin Tissoires .name = "wacom_led", 1022471d1714SBenjamin Tissoires .attrs = cintiq_led_attrs, 1023471d1714SBenjamin Tissoires }; 1024471d1714SBenjamin Tissoires 1025471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = { 1026471d1714SBenjamin Tissoires &dev_attr_status0_luminance.attr, 1027471d1714SBenjamin Tissoires &dev_attr_status1_luminance.attr, 1028471d1714SBenjamin Tissoires &dev_attr_status_led0_select.attr, 1029471d1714SBenjamin Tissoires &dev_attr_buttons_luminance.attr, 1030471d1714SBenjamin Tissoires &dev_attr_button0_rawimg.attr, 1031471d1714SBenjamin Tissoires &dev_attr_button1_rawimg.attr, 1032471d1714SBenjamin Tissoires &dev_attr_button2_rawimg.attr, 1033471d1714SBenjamin Tissoires &dev_attr_button3_rawimg.attr, 1034471d1714SBenjamin Tissoires &dev_attr_button4_rawimg.attr, 1035471d1714SBenjamin Tissoires &dev_attr_button5_rawimg.attr, 1036471d1714SBenjamin Tissoires &dev_attr_button6_rawimg.attr, 1037471d1714SBenjamin Tissoires &dev_attr_button7_rawimg.attr, 1038471d1714SBenjamin Tissoires NULL 1039471d1714SBenjamin Tissoires }; 1040471d1714SBenjamin Tissoires 1041471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = { 1042471d1714SBenjamin Tissoires .name = "wacom_led", 1043471d1714SBenjamin Tissoires .attrs = intuos4_led_attrs, 1044471d1714SBenjamin Tissoires }; 1045471d1714SBenjamin Tissoires 1046471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = { 1047471d1714SBenjamin Tissoires &dev_attr_status0_luminance.attr, 1048471d1714SBenjamin Tissoires &dev_attr_status_led0_select.attr, 1049471d1714SBenjamin Tissoires NULL 1050471d1714SBenjamin Tissoires }; 1051471d1714SBenjamin Tissoires 1052471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = { 1053471d1714SBenjamin Tissoires .name = "wacom_led", 1054471d1714SBenjamin Tissoires .attrs = intuos5_led_attrs, 1055471d1714SBenjamin Tissoires }; 1056471d1714SBenjamin Tissoires 105710c55cacSAaron Armstrong Skomra static struct attribute *generic_led_attrs[] = { 105810c55cacSAaron Armstrong Skomra &dev_attr_status0_luminance.attr, 105910c55cacSAaron Armstrong Skomra &dev_attr_status_led0_select.attr, 106010c55cacSAaron Armstrong Skomra NULL 106110c55cacSAaron Armstrong Skomra }; 106210c55cacSAaron Armstrong Skomra 106310c55cacSAaron Armstrong Skomra static struct attribute_group generic_led_attr_group = { 106410c55cacSAaron Armstrong Skomra .name = "wacom_led", 106510c55cacSAaron Armstrong Skomra .attrs = generic_led_attrs, 106610c55cacSAaron Armstrong Skomra }; 106710c55cacSAaron Armstrong Skomra 10682df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres { 10692df68a88SBenjamin Tissoires struct attribute_group *group; 10702df68a88SBenjamin Tissoires struct kobject *root; 10712df68a88SBenjamin Tissoires }; 10722df68a88SBenjamin Tissoires 10732df68a88SBenjamin Tissoires static void wacom_devm_sysfs_group_release(struct device *dev, void *res) 10742df68a88SBenjamin Tissoires { 10752df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres *devres = res; 10762df68a88SBenjamin Tissoires struct kobject *kobj = devres->root; 10772df68a88SBenjamin Tissoires 10782df68a88SBenjamin Tissoires dev_dbg(dev, "%s: dropping reference to %s\n", 10792df68a88SBenjamin Tissoires __func__, devres->group->name); 10802df68a88SBenjamin Tissoires sysfs_remove_group(kobj, devres->group); 10812df68a88SBenjamin Tissoires } 10822df68a88SBenjamin Tissoires 1083f9036bd4SBenjamin Tissoires static int __wacom_devm_sysfs_create_group(struct wacom *wacom, 1084f9036bd4SBenjamin Tissoires struct kobject *root, 10852df68a88SBenjamin Tissoires struct attribute_group *group) 10862df68a88SBenjamin Tissoires { 10872df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres *devres; 10882df68a88SBenjamin Tissoires int error; 10892df68a88SBenjamin Tissoires 10902df68a88SBenjamin Tissoires devres = devres_alloc(wacom_devm_sysfs_group_release, 10912df68a88SBenjamin Tissoires sizeof(struct wacom_sysfs_group_devres), 10922df68a88SBenjamin Tissoires GFP_KERNEL); 10932df68a88SBenjamin Tissoires if (!devres) 10942df68a88SBenjamin Tissoires return -ENOMEM; 10952df68a88SBenjamin Tissoires 10962df68a88SBenjamin Tissoires devres->group = group; 1097f9036bd4SBenjamin Tissoires devres->root = root; 10982df68a88SBenjamin Tissoires 10992df68a88SBenjamin Tissoires error = sysfs_create_group(devres->root, group); 11002df68a88SBenjamin Tissoires if (error) 11012df68a88SBenjamin Tissoires return error; 11022df68a88SBenjamin Tissoires 11032df68a88SBenjamin Tissoires devres_add(&wacom->hdev->dev, devres); 11042df68a88SBenjamin Tissoires 11052df68a88SBenjamin Tissoires return 0; 11062df68a88SBenjamin Tissoires } 11072df68a88SBenjamin Tissoires 1108f9036bd4SBenjamin Tissoires static int wacom_devm_sysfs_create_group(struct wacom *wacom, 1109f9036bd4SBenjamin Tissoires struct attribute_group *group) 1110f9036bd4SBenjamin Tissoires { 1111f9036bd4SBenjamin Tissoires return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj, 1112f9036bd4SBenjamin Tissoires group); 1113f9036bd4SBenjamin Tissoires } 1114f9036bd4SBenjamin Tissoires 111534736aa9SBenjamin Tissoires enum led_brightness wacom_leds_brightness_get(struct wacom_led *led) 111697f5541fSBenjamin Tissoires { 111797f5541fSBenjamin Tissoires struct wacom *wacom = led->wacom; 111897f5541fSBenjamin Tissoires 111997f5541fSBenjamin Tissoires if (wacom->led.max_hlv) 112097f5541fSBenjamin Tissoires return led->hlv * LED_FULL / wacom->led.max_hlv; 112197f5541fSBenjamin Tissoires 112297f5541fSBenjamin Tissoires if (wacom->led.max_llv) 112397f5541fSBenjamin Tissoires return led->llv * LED_FULL / wacom->led.max_llv; 112497f5541fSBenjamin Tissoires 112597f5541fSBenjamin Tissoires /* device doesn't support brightness tuning */ 112697f5541fSBenjamin Tissoires return LED_FULL; 112797f5541fSBenjamin Tissoires } 112897f5541fSBenjamin Tissoires 112997f5541fSBenjamin Tissoires static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev) 113097f5541fSBenjamin Tissoires { 113197f5541fSBenjamin Tissoires struct wacom_led *led = container_of(cdev, struct wacom_led, cdev); 113297f5541fSBenjamin Tissoires struct wacom *wacom = led->wacom; 113397f5541fSBenjamin Tissoires 113497f5541fSBenjamin Tissoires if (wacom->led.groups[led->group].select != led->id) 113597f5541fSBenjamin Tissoires return LED_OFF; 113697f5541fSBenjamin Tissoires 113797f5541fSBenjamin Tissoires return wacom_leds_brightness_get(led); 113897f5541fSBenjamin Tissoires } 113997f5541fSBenjamin Tissoires 114097f5541fSBenjamin Tissoires static int wacom_led_brightness_set(struct led_classdev *cdev, 114197f5541fSBenjamin Tissoires enum led_brightness brightness) 114297f5541fSBenjamin Tissoires { 114397f5541fSBenjamin Tissoires struct wacom_led *led = container_of(cdev, struct wacom_led, cdev); 114497f5541fSBenjamin Tissoires struct wacom *wacom = led->wacom; 114597f5541fSBenjamin Tissoires int error; 114697f5541fSBenjamin Tissoires 114797f5541fSBenjamin Tissoires mutex_lock(&wacom->lock); 114897f5541fSBenjamin Tissoires 114997f5541fSBenjamin Tissoires if (!wacom->led.groups || (brightness == LED_OFF && 115097f5541fSBenjamin Tissoires wacom->led.groups[led->group].select != led->id)) { 115197f5541fSBenjamin Tissoires error = 0; 115297f5541fSBenjamin Tissoires goto out; 115397f5541fSBenjamin Tissoires } 115497f5541fSBenjamin Tissoires 115597f5541fSBenjamin Tissoires led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL; 115697f5541fSBenjamin Tissoires led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL; 115797f5541fSBenjamin Tissoires 115897f5541fSBenjamin Tissoires wacom->led.groups[led->group].select = led->id; 115997f5541fSBenjamin Tissoires 116097f5541fSBenjamin Tissoires error = wacom_led_control(wacom); 116197f5541fSBenjamin Tissoires 116297f5541fSBenjamin Tissoires out: 116397f5541fSBenjamin Tissoires mutex_unlock(&wacom->lock); 116497f5541fSBenjamin Tissoires 116597f5541fSBenjamin Tissoires return error; 116697f5541fSBenjamin Tissoires } 116797f5541fSBenjamin Tissoires 116897f5541fSBenjamin Tissoires static void wacom_led_readonly_brightness_set(struct led_classdev *cdev, 116997f5541fSBenjamin Tissoires enum led_brightness brightness) 117097f5541fSBenjamin Tissoires { 117197f5541fSBenjamin Tissoires } 117297f5541fSBenjamin Tissoires 117397f5541fSBenjamin Tissoires static int wacom_led_register_one(struct device *dev, struct wacom *wacom, 117497f5541fSBenjamin Tissoires struct wacom_led *led, unsigned int group, 117597f5541fSBenjamin Tissoires unsigned int id, bool read_only) 117697f5541fSBenjamin Tissoires { 117797f5541fSBenjamin Tissoires int error; 117897f5541fSBenjamin Tissoires char *name; 117997f5541fSBenjamin Tissoires 118097f5541fSBenjamin Tissoires name = devm_kasprintf(dev, GFP_KERNEL, 118197f5541fSBenjamin Tissoires "%s::wacom-%d.%d", 118297f5541fSBenjamin Tissoires dev_name(dev), 118397f5541fSBenjamin Tissoires group, 118497f5541fSBenjamin Tissoires id); 118597f5541fSBenjamin Tissoires if (!name) 118697f5541fSBenjamin Tissoires return -ENOMEM; 118797f5541fSBenjamin Tissoires 118834736aa9SBenjamin Tissoires if (!read_only) { 118934736aa9SBenjamin Tissoires led->trigger.name = name; 119034736aa9SBenjamin Tissoires error = devm_led_trigger_register(dev, &led->trigger); 119134736aa9SBenjamin Tissoires if (error) { 119234736aa9SBenjamin Tissoires hid_err(wacom->hdev, 119334736aa9SBenjamin Tissoires "failed to register LED trigger %s: %d\n", 119434736aa9SBenjamin Tissoires led->cdev.name, error); 119534736aa9SBenjamin Tissoires return error; 119634736aa9SBenjamin Tissoires } 119734736aa9SBenjamin Tissoires } 119834736aa9SBenjamin Tissoires 119997f5541fSBenjamin Tissoires led->group = group; 120097f5541fSBenjamin Tissoires led->id = id; 120197f5541fSBenjamin Tissoires led->wacom = wacom; 120297f5541fSBenjamin Tissoires led->llv = wacom->led.llv; 120397f5541fSBenjamin Tissoires led->hlv = wacom->led.hlv; 120497f5541fSBenjamin Tissoires led->cdev.name = name; 120597f5541fSBenjamin Tissoires led->cdev.max_brightness = LED_FULL; 120697f5541fSBenjamin Tissoires led->cdev.flags = LED_HW_PLUGGABLE; 120797f5541fSBenjamin Tissoires led->cdev.brightness_get = __wacom_led_brightness_get; 120834736aa9SBenjamin Tissoires if (!read_only) { 120997f5541fSBenjamin Tissoires led->cdev.brightness_set_blocking = wacom_led_brightness_set; 121034736aa9SBenjamin Tissoires led->cdev.default_trigger = led->cdev.name; 121134736aa9SBenjamin Tissoires } else { 121297f5541fSBenjamin Tissoires led->cdev.brightness_set = wacom_led_readonly_brightness_set; 121334736aa9SBenjamin Tissoires } 121497f5541fSBenjamin Tissoires 121597f5541fSBenjamin Tissoires error = devm_led_classdev_register(dev, &led->cdev); 121697f5541fSBenjamin Tissoires if (error) { 121797f5541fSBenjamin Tissoires hid_err(wacom->hdev, 121897f5541fSBenjamin Tissoires "failed to register LED %s: %d\n", 121997f5541fSBenjamin Tissoires led->cdev.name, error); 122097f5541fSBenjamin Tissoires led->cdev.name = NULL; 122197f5541fSBenjamin Tissoires return error; 122297f5541fSBenjamin Tissoires } 122397f5541fSBenjamin Tissoires 122497f5541fSBenjamin Tissoires return 0; 122597f5541fSBenjamin Tissoires } 122697f5541fSBenjamin Tissoires 1227589e5060SBenjamin Tissoires static void wacom_led_groups_release_one(void *data) 1228589e5060SBenjamin Tissoires { 1229589e5060SBenjamin Tissoires struct wacom_group_leds *group = data; 1230589e5060SBenjamin Tissoires 1231589e5060SBenjamin Tissoires devres_release_group(group->dev, group); 1232589e5060SBenjamin Tissoires } 1233589e5060SBenjamin Tissoires 123497f5541fSBenjamin Tissoires static int wacom_led_groups_alloc_and_register_one(struct device *dev, 123597f5541fSBenjamin Tissoires struct wacom *wacom, 123697f5541fSBenjamin Tissoires int group_id, int count, 123797f5541fSBenjamin Tissoires bool read_only) 123897f5541fSBenjamin Tissoires { 123997f5541fSBenjamin Tissoires struct wacom_led *leds; 124097f5541fSBenjamin Tissoires int i, error; 124197f5541fSBenjamin Tissoires 124297f5541fSBenjamin Tissoires if (group_id >= wacom->led.count || count <= 0) 124397f5541fSBenjamin Tissoires return -EINVAL; 124497f5541fSBenjamin Tissoires 124597f5541fSBenjamin Tissoires if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL)) 124697f5541fSBenjamin Tissoires return -ENOMEM; 124797f5541fSBenjamin Tissoires 124897f5541fSBenjamin Tissoires leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL); 124997f5541fSBenjamin Tissoires if (!leds) { 125097f5541fSBenjamin Tissoires error = -ENOMEM; 125197f5541fSBenjamin Tissoires goto err; 125297f5541fSBenjamin Tissoires } 125397f5541fSBenjamin Tissoires 125497f5541fSBenjamin Tissoires wacom->led.groups[group_id].leds = leds; 125597f5541fSBenjamin Tissoires wacom->led.groups[group_id].count = count; 125697f5541fSBenjamin Tissoires 125797f5541fSBenjamin Tissoires for (i = 0; i < count; i++) { 125897f5541fSBenjamin Tissoires error = wacom_led_register_one(dev, wacom, &leds[i], 125997f5541fSBenjamin Tissoires group_id, i, read_only); 126097f5541fSBenjamin Tissoires if (error) 126197f5541fSBenjamin Tissoires goto err; 126297f5541fSBenjamin Tissoires } 126397f5541fSBenjamin Tissoires 1264589e5060SBenjamin Tissoires wacom->led.groups[group_id].dev = dev; 1265589e5060SBenjamin Tissoires 1266589e5060SBenjamin Tissoires devres_close_group(dev, &wacom->led.groups[group_id]); 1267589e5060SBenjamin Tissoires 1268589e5060SBenjamin Tissoires /* 1269589e5060SBenjamin Tissoires * There is a bug (?) in devm_led_classdev_register() in which its 1270589e5060SBenjamin Tissoires * increments the refcount of the parent. If the parent is an input 1271589e5060SBenjamin Tissoires * device, that means the ref count never reaches 0 when 1272589e5060SBenjamin Tissoires * devm_input_device_release() gets called. 1273589e5060SBenjamin Tissoires * This means that the LEDs are still there after disconnect. 1274589e5060SBenjamin Tissoires * Manually force the release of the group so that the leds are released 1275589e5060SBenjamin Tissoires * once we are done using them. 1276589e5060SBenjamin Tissoires */ 1277589e5060SBenjamin Tissoires error = devm_add_action_or_reset(&wacom->hdev->dev, 1278589e5060SBenjamin Tissoires wacom_led_groups_release_one, 1279589e5060SBenjamin Tissoires &wacom->led.groups[group_id]); 1280589e5060SBenjamin Tissoires if (error) 1281589e5060SBenjamin Tissoires return error; 1282589e5060SBenjamin Tissoires 128397f5541fSBenjamin Tissoires return 0; 128497f5541fSBenjamin Tissoires 128597f5541fSBenjamin Tissoires err: 128697f5541fSBenjamin Tissoires devres_release_group(dev, &wacom->led.groups[group_id]); 128797f5541fSBenjamin Tissoires return error; 128897f5541fSBenjamin Tissoires } 128997f5541fSBenjamin Tissoires 129034736aa9SBenjamin Tissoires struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id, 129134736aa9SBenjamin Tissoires unsigned int id) 129234736aa9SBenjamin Tissoires { 129334736aa9SBenjamin Tissoires struct wacom_group_leds *group; 129434736aa9SBenjamin Tissoires 129534736aa9SBenjamin Tissoires if (group_id >= wacom->led.count) 129634736aa9SBenjamin Tissoires return NULL; 129734736aa9SBenjamin Tissoires 129834736aa9SBenjamin Tissoires group = &wacom->led.groups[group_id]; 129934736aa9SBenjamin Tissoires 130034736aa9SBenjamin Tissoires if (!group->leds) 130134736aa9SBenjamin Tissoires return NULL; 130234736aa9SBenjamin Tissoires 130334736aa9SBenjamin Tissoires id %= group->count; 130434736aa9SBenjamin Tissoires 130534736aa9SBenjamin Tissoires return &group->leds[id]; 130634736aa9SBenjamin Tissoires } 130734736aa9SBenjamin Tissoires 130834736aa9SBenjamin Tissoires /** 130934736aa9SBenjamin Tissoires * wacom_led_next: gives the next available led with a wacom trigger. 131034736aa9SBenjamin Tissoires * 131134736aa9SBenjamin Tissoires * returns the next available struct wacom_led which has its default trigger 131234736aa9SBenjamin Tissoires * or the current one if none is available. 131334736aa9SBenjamin Tissoires */ 131434736aa9SBenjamin Tissoires struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur) 131534736aa9SBenjamin Tissoires { 131634736aa9SBenjamin Tissoires struct wacom_led *next_led; 131734736aa9SBenjamin Tissoires int group, next; 131834736aa9SBenjamin Tissoires 131934736aa9SBenjamin Tissoires if (!wacom || !cur) 132034736aa9SBenjamin Tissoires return NULL; 132134736aa9SBenjamin Tissoires 132234736aa9SBenjamin Tissoires group = cur->group; 132334736aa9SBenjamin Tissoires next = cur->id; 132434736aa9SBenjamin Tissoires 132534736aa9SBenjamin Tissoires do { 132634736aa9SBenjamin Tissoires next_led = wacom_led_find(wacom, group, ++next); 132734736aa9SBenjamin Tissoires if (!next_led || next_led == cur) 132834736aa9SBenjamin Tissoires return next_led; 132934736aa9SBenjamin Tissoires } while (next_led->cdev.trigger != &next_led->trigger); 133034736aa9SBenjamin Tissoires 133134736aa9SBenjamin Tissoires return next_led; 133234736aa9SBenjamin Tissoires } 133334736aa9SBenjamin Tissoires 1334a50aac71SBenjamin Tissoires static void wacom_led_groups_release(void *data) 1335a50aac71SBenjamin Tissoires { 1336a50aac71SBenjamin Tissoires struct wacom *wacom = data; 1337a50aac71SBenjamin Tissoires 1338a50aac71SBenjamin Tissoires wacom->led.groups = NULL; 133997f5541fSBenjamin Tissoires wacom->led.count = 0; 1340a50aac71SBenjamin Tissoires } 1341a50aac71SBenjamin Tissoires 1342a50aac71SBenjamin Tissoires static int wacom_led_groups_allocate(struct wacom *wacom, int count) 1343a50aac71SBenjamin Tissoires { 134497f5541fSBenjamin Tissoires struct device *dev = &wacom->hdev->dev; 1345a50aac71SBenjamin Tissoires struct wacom_group_leds *groups; 1346a50aac71SBenjamin Tissoires int error; 1347a50aac71SBenjamin Tissoires 134897f5541fSBenjamin Tissoires groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count, 1349a50aac71SBenjamin Tissoires GFP_KERNEL); 1350a50aac71SBenjamin Tissoires if (!groups) 1351a50aac71SBenjamin Tissoires return -ENOMEM; 1352a50aac71SBenjamin Tissoires 135397f5541fSBenjamin Tissoires error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom); 1354a50aac71SBenjamin Tissoires if (error) 1355a50aac71SBenjamin Tissoires return error; 1356a50aac71SBenjamin Tissoires 1357a50aac71SBenjamin Tissoires wacom->led.groups = groups; 135897f5541fSBenjamin Tissoires wacom->led.count = count; 135997f5541fSBenjamin Tissoires 136097f5541fSBenjamin Tissoires return 0; 136197f5541fSBenjamin Tissoires } 136297f5541fSBenjamin Tissoires 136397f5541fSBenjamin Tissoires static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count, 136497f5541fSBenjamin Tissoires int led_per_group, bool read_only) 136597f5541fSBenjamin Tissoires { 136697f5541fSBenjamin Tissoires struct device *dev; 136797f5541fSBenjamin Tissoires int i, error; 136897f5541fSBenjamin Tissoires 136997f5541fSBenjamin Tissoires if (!wacom->wacom_wac.pad_input) 137097f5541fSBenjamin Tissoires return -EINVAL; 137197f5541fSBenjamin Tissoires 137297f5541fSBenjamin Tissoires dev = &wacom->wacom_wac.pad_input->dev; 137397f5541fSBenjamin Tissoires 137497f5541fSBenjamin Tissoires error = wacom_led_groups_allocate(wacom, group_count); 137597f5541fSBenjamin Tissoires if (error) 137697f5541fSBenjamin Tissoires return error; 137797f5541fSBenjamin Tissoires 137897f5541fSBenjamin Tissoires for (i = 0; i < group_count; i++) { 137997f5541fSBenjamin Tissoires error = wacom_led_groups_alloc_and_register_one(dev, wacom, i, 138097f5541fSBenjamin Tissoires led_per_group, 138197f5541fSBenjamin Tissoires read_only); 138297f5541fSBenjamin Tissoires if (error) 138397f5541fSBenjamin Tissoires return error; 138497f5541fSBenjamin Tissoires } 1385a50aac71SBenjamin Tissoires 1386a50aac71SBenjamin Tissoires return 0; 1387a50aac71SBenjamin Tissoires } 1388a50aac71SBenjamin Tissoires 138910c55cacSAaron Armstrong Skomra int wacom_initialize_leds(struct wacom *wacom) 1390471d1714SBenjamin Tissoires { 1391471d1714SBenjamin Tissoires int error; 1392471d1714SBenjamin Tissoires 1393862cf553SJason Gerecke if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD)) 1394862cf553SJason Gerecke return 0; 1395862cf553SJason Gerecke 1396471d1714SBenjamin Tissoires /* Initialize default values */ 1397471d1714SBenjamin Tissoires switch (wacom->wacom_wac.features.type) { 139810c55cacSAaron Armstrong Skomra case HID_GENERIC: 139910c55cacSAaron Armstrong Skomra if (!wacom->generic_has_leds) 140010c55cacSAaron Armstrong Skomra return 0; 140110c55cacSAaron Armstrong Skomra wacom->led.llv = 100; 140210c55cacSAaron Armstrong Skomra wacom->led.max_llv = 100; 140310c55cacSAaron Armstrong Skomra 140410c55cacSAaron Armstrong Skomra error = wacom_leds_alloc_and_register(wacom, 1, 4, false); 140510c55cacSAaron Armstrong Skomra if (error) { 140610c55cacSAaron Armstrong Skomra hid_err(wacom->hdev, 140710c55cacSAaron Armstrong Skomra "cannot create leds err: %d\n", error); 140810c55cacSAaron Armstrong Skomra return error; 140910c55cacSAaron Armstrong Skomra } 141010c55cacSAaron Armstrong Skomra 141110c55cacSAaron Armstrong Skomra error = wacom_devm_sysfs_create_group(wacom, 141210c55cacSAaron Armstrong Skomra &generic_led_attr_group); 141310c55cacSAaron Armstrong Skomra break; 141410c55cacSAaron Armstrong Skomra 1415471d1714SBenjamin Tissoires case INTUOS4S: 1416471d1714SBenjamin Tissoires case INTUOS4: 141781af7e61SBenjamin Tissoires case INTUOS4WL: 1418471d1714SBenjamin Tissoires case INTUOS4L: 1419471d1714SBenjamin Tissoires wacom->led.llv = 10; 1420471d1714SBenjamin Tissoires wacom->led.hlv = 20; 142197f5541fSBenjamin Tissoires wacom->led.max_llv = 127; 142297f5541fSBenjamin Tissoires wacom->led.max_hlv = 127; 1423471d1714SBenjamin Tissoires wacom->led.img_lum = 10; 1424a50aac71SBenjamin Tissoires 142597f5541fSBenjamin Tissoires error = wacom_leds_alloc_and_register(wacom, 1, 4, false); 1426a50aac71SBenjamin Tissoires if (error) { 1427a50aac71SBenjamin Tissoires hid_err(wacom->hdev, 1428a50aac71SBenjamin Tissoires "cannot create leds err: %d\n", error); 1429a50aac71SBenjamin Tissoires return error; 1430a50aac71SBenjamin Tissoires } 1431a50aac71SBenjamin Tissoires 14322df68a88SBenjamin Tissoires error = wacom_devm_sysfs_create_group(wacom, 1433471d1714SBenjamin Tissoires &intuos4_led_attr_group); 1434471d1714SBenjamin Tissoires break; 1435471d1714SBenjamin Tissoires 1436471d1714SBenjamin Tissoires case WACOM_24HD: 1437471d1714SBenjamin Tissoires case WACOM_21UX2: 1438471d1714SBenjamin Tissoires wacom->led.llv = 0; 1439471d1714SBenjamin Tissoires wacom->led.hlv = 0; 1440471d1714SBenjamin Tissoires wacom->led.img_lum = 0; 1441471d1714SBenjamin Tissoires 144297f5541fSBenjamin Tissoires error = wacom_leds_alloc_and_register(wacom, 2, 4, false); 1443a50aac71SBenjamin Tissoires if (error) { 1444a50aac71SBenjamin Tissoires hid_err(wacom->hdev, 1445a50aac71SBenjamin Tissoires "cannot create leds err: %d\n", error); 1446a50aac71SBenjamin Tissoires return error; 1447a50aac71SBenjamin Tissoires } 1448a50aac71SBenjamin Tissoires 14492df68a88SBenjamin Tissoires error = wacom_devm_sysfs_create_group(wacom, 1450471d1714SBenjamin Tissoires &cintiq_led_attr_group); 1451471d1714SBenjamin Tissoires break; 1452471d1714SBenjamin Tissoires 1453471d1714SBenjamin Tissoires case INTUOS5S: 1454471d1714SBenjamin Tissoires case INTUOS5: 1455471d1714SBenjamin Tissoires case INTUOS5L: 1456471d1714SBenjamin Tissoires case INTUOSPS: 1457471d1714SBenjamin Tissoires case INTUOSPM: 1458471d1714SBenjamin Tissoires case INTUOSPL: 1459471d1714SBenjamin Tissoires wacom->led.llv = 32; 146097f5541fSBenjamin Tissoires wacom->led.max_llv = 96; 1461471d1714SBenjamin Tissoires 146297f5541fSBenjamin Tissoires error = wacom_leds_alloc_and_register(wacom, 1, 4, false); 1463a50aac71SBenjamin Tissoires if (error) { 1464a50aac71SBenjamin Tissoires hid_err(wacom->hdev, 1465a50aac71SBenjamin Tissoires "cannot create leds err: %d\n", error); 1466a50aac71SBenjamin Tissoires return error; 1467a50aac71SBenjamin Tissoires } 1468a50aac71SBenjamin Tissoires 14692df68a88SBenjamin Tissoires error = wacom_devm_sysfs_create_group(wacom, 1470471d1714SBenjamin Tissoires &intuos5_led_attr_group); 1471471d1714SBenjamin Tissoires break; 1472471d1714SBenjamin Tissoires 14734922cd26SJason Gerecke case INTUOSP2_BT: 14744922cd26SJason Gerecke wacom->led.llv = 50; 14754922cd26SJason Gerecke wacom->led.max_llv = 100; 14764922cd26SJason Gerecke error = wacom_leds_alloc_and_register(wacom, 1, 4, false); 14774922cd26SJason Gerecke if (error) { 14784922cd26SJason Gerecke hid_err(wacom->hdev, 14794922cd26SJason Gerecke "cannot create leds err: %d\n", error); 14804922cd26SJason Gerecke return error; 14814922cd26SJason Gerecke } 14824922cd26SJason Gerecke return 0; 14834922cd26SJason Gerecke 1484a50aac71SBenjamin Tissoires case REMOTE: 148597f5541fSBenjamin Tissoires wacom->led.llv = 255; 148697f5541fSBenjamin Tissoires wacom->led.max_llv = 255; 1487a50aac71SBenjamin Tissoires error = wacom_led_groups_allocate(wacom, 5); 1488a50aac71SBenjamin Tissoires if (error) { 1489a50aac71SBenjamin Tissoires hid_err(wacom->hdev, 1490a50aac71SBenjamin Tissoires "cannot create leds err: %d\n", error); 1491a50aac71SBenjamin Tissoires return error; 1492a50aac71SBenjamin Tissoires } 1493a50aac71SBenjamin Tissoires return 0; 1494a50aac71SBenjamin Tissoires 1495471d1714SBenjamin Tissoires default: 1496471d1714SBenjamin Tissoires return 0; 1497471d1714SBenjamin Tissoires } 1498471d1714SBenjamin Tissoires 1499471d1714SBenjamin Tissoires if (error) { 1500471d1714SBenjamin Tissoires hid_err(wacom->hdev, 1501471d1714SBenjamin Tissoires "cannot create sysfs group err: %d\n", error); 1502471d1714SBenjamin Tissoires return error; 1503471d1714SBenjamin Tissoires } 1504471d1714SBenjamin Tissoires 1505471d1714SBenjamin Tissoires return 0; 1506471d1714SBenjamin Tissoires } 1507471d1714SBenjamin Tissoires 1508a544c619SBenjamin Tissoires static void wacom_init_work(struct work_struct *work) 1509a544c619SBenjamin Tissoires { 1510a544c619SBenjamin Tissoires struct wacom *wacom = container_of(work, struct wacom, init_work.work); 1511a544c619SBenjamin Tissoires 1512a544c619SBenjamin Tissoires _wacom_query_tablet_data(wacom); 1513a544c619SBenjamin Tissoires wacom_led_control(wacom); 1514a544c619SBenjamin Tissoires } 1515a544c619SBenjamin Tissoires 1516a544c619SBenjamin Tissoires static void wacom_query_tablet_data(struct wacom *wacom) 1517a544c619SBenjamin Tissoires { 1518a544c619SBenjamin Tissoires schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000)); 1519a544c619SBenjamin Tissoires } 1520a544c619SBenjamin Tissoires 1521471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = { 15229956953eSBenjamin Tissoires POWER_SUPPLY_PROP_MODEL_NAME, 152371fa641eSJason Gerecke POWER_SUPPLY_PROP_PRESENT, 1524ac8d1010SBenjamin Tissoires POWER_SUPPLY_PROP_STATUS, 1525471d1714SBenjamin Tissoires POWER_SUPPLY_PROP_SCOPE, 1526471d1714SBenjamin Tissoires POWER_SUPPLY_PROP_CAPACITY 1527471d1714SBenjamin Tissoires }; 1528471d1714SBenjamin Tissoires 1529471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy, 1530471d1714SBenjamin Tissoires enum power_supply_property psp, 1531471d1714SBenjamin Tissoires union power_supply_propval *val) 1532471d1714SBenjamin Tissoires { 153359d69bc8SBenjamin Tissoires struct wacom_battery *battery = power_supply_get_drvdata(psy); 1534471d1714SBenjamin Tissoires int ret = 0; 1535471d1714SBenjamin Tissoires 1536471d1714SBenjamin Tissoires switch (psp) { 15379956953eSBenjamin Tissoires case POWER_SUPPLY_PROP_MODEL_NAME: 15389956953eSBenjamin Tissoires val->strval = battery->wacom->wacom_wac.name; 15399956953eSBenjamin Tissoires break; 154071fa641eSJason Gerecke case POWER_SUPPLY_PROP_PRESENT: 154159d69bc8SBenjamin Tissoires val->intval = battery->bat_connected; 154271fa641eSJason Gerecke break; 1543471d1714SBenjamin Tissoires case POWER_SUPPLY_PROP_SCOPE: 1544471d1714SBenjamin Tissoires val->intval = POWER_SUPPLY_SCOPE_DEVICE; 1545471d1714SBenjamin Tissoires break; 1546471d1714SBenjamin Tissoires case POWER_SUPPLY_PROP_CAPACITY: 154759d69bc8SBenjamin Tissoires val->intval = battery->battery_capacity; 1548ac8d1010SBenjamin Tissoires break; 1549ac8d1010SBenjamin Tissoires case POWER_SUPPLY_PROP_STATUS: 1550*16e45989SJason Gerecke if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO) 1551*16e45989SJason Gerecke val->intval = battery->bat_status; 1552*16e45989SJason Gerecke else if (battery->bat_charging) 1553ac8d1010SBenjamin Tissoires val->intval = POWER_SUPPLY_STATUS_CHARGING; 155459d69bc8SBenjamin Tissoires else if (battery->battery_capacity == 100 && 155559d69bc8SBenjamin Tissoires battery->ps_connected) 1556ac8d1010SBenjamin Tissoires val->intval = POWER_SUPPLY_STATUS_FULL; 155759d69bc8SBenjamin Tissoires else if (battery->ps_connected) 1558b0882cb7SJason Gerecke val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 1559ac8d1010SBenjamin Tissoires else 1560ac8d1010SBenjamin Tissoires val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 1561471d1714SBenjamin Tissoires break; 1562471d1714SBenjamin Tissoires default: 1563471d1714SBenjamin Tissoires ret = -EINVAL; 1564471d1714SBenjamin Tissoires break; 1565471d1714SBenjamin Tissoires } 1566471d1714SBenjamin Tissoires 1567471d1714SBenjamin Tissoires return ret; 1568471d1714SBenjamin Tissoires } 1569471d1714SBenjamin Tissoires 157059d69bc8SBenjamin Tissoires static int __wacom_initialize_battery(struct wacom *wacom, 157159d69bc8SBenjamin Tissoires struct wacom_battery *battery) 1572471d1714SBenjamin Tissoires { 1573d70420b9SBenjamin Tissoires static atomic_t battery_no = ATOMIC_INIT(0); 1574b189da90SBenjamin Tissoires struct device *dev = &wacom->hdev->dev; 157559d69bc8SBenjamin Tissoires struct power_supply_config psy_cfg = { .drv_data = battery, }; 1576136ae5e9SBenjamin Tissoires struct power_supply *ps_bat; 157759d69bc8SBenjamin Tissoires struct power_supply_desc *bat_desc = &battery->bat_desc; 1578d70420b9SBenjamin Tissoires unsigned long n; 1579b189da90SBenjamin Tissoires int error; 1580b189da90SBenjamin Tissoires 1581b189da90SBenjamin Tissoires if (!devres_open_group(dev, bat_desc, GFP_KERNEL)) 1582b189da90SBenjamin Tissoires return -ENOMEM; 1583471d1714SBenjamin Tissoires 15849956953eSBenjamin Tissoires battery->wacom = wacom; 15859956953eSBenjamin Tissoires 1586d70420b9SBenjamin Tissoires n = atomic_inc_return(&battery_no) - 1; 15877dbd229eSBenjamin Tissoires 1588297d716fSKrzysztof Kozlowski bat_desc->properties = wacom_battery_props; 1589297d716fSKrzysztof Kozlowski bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props); 1590297d716fSKrzysztof Kozlowski bat_desc->get_property = wacom_battery_get_property; 159159d69bc8SBenjamin Tissoires sprintf(battery->bat_name, "wacom_battery_%ld", n); 159259d69bc8SBenjamin Tissoires bat_desc->name = battery->bat_name; 159396983296SBenjamin Tissoires bat_desc->type = POWER_SUPPLY_TYPE_USB; 1594297d716fSKrzysztof Kozlowski bat_desc->use_for_apm = 0; 1595471d1714SBenjamin Tissoires 159659d69bc8SBenjamin Tissoires ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg); 159759d69bc8SBenjamin Tissoires if (IS_ERR(ps_bat)) { 159859d69bc8SBenjamin Tissoires error = PTR_ERR(ps_bat); 1599b189da90SBenjamin Tissoires goto err; 1600b189da90SBenjamin Tissoires } 1601471d1714SBenjamin Tissoires 160259d69bc8SBenjamin Tissoires power_supply_powers(ps_bat, &wacom->hdev->dev); 160359d69bc8SBenjamin Tissoires 160459d69bc8SBenjamin Tissoires battery->battery = ps_bat; 16057dbd229eSBenjamin Tissoires 1606b189da90SBenjamin Tissoires devres_close_group(dev, bat_desc); 16077dbd229eSBenjamin Tissoires return 0; 1608b189da90SBenjamin Tissoires 1609b189da90SBenjamin Tissoires err: 1610b189da90SBenjamin Tissoires devres_release_group(dev, bat_desc); 1611b189da90SBenjamin Tissoires return error; 1612471d1714SBenjamin Tissoires } 1613471d1714SBenjamin Tissoires 161459d69bc8SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom) 161559d69bc8SBenjamin Tissoires { 161659d69bc8SBenjamin Tissoires if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) 161759d69bc8SBenjamin Tissoires return __wacom_initialize_battery(wacom, &wacom->battery); 161859d69bc8SBenjamin Tissoires 161959d69bc8SBenjamin Tissoires return 0; 162059d69bc8SBenjamin Tissoires } 162159d69bc8SBenjamin Tissoires 1622471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom) 1623471d1714SBenjamin Tissoires { 162459d69bc8SBenjamin Tissoires if (wacom->battery.battery) { 162559d69bc8SBenjamin Tissoires devres_release_group(&wacom->hdev->dev, 162659d69bc8SBenjamin Tissoires &wacom->battery.bat_desc); 162759d69bc8SBenjamin Tissoires wacom->battery.battery = NULL; 1628471d1714SBenjamin Tissoires } 1629471d1714SBenjamin Tissoires } 1630471d1714SBenjamin Tissoires 1631f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev, 1632f81a1295SBenjamin Tissoires struct device_attribute 1633f81a1295SBenjamin Tissoires *attr, char *buf) 1634f81a1295SBenjamin Tissoires { 1635ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev); 1636f81a1295SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 1637f81a1295SBenjamin Tissoires 1638f81a1295SBenjamin Tissoires return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed); 1639f81a1295SBenjamin Tissoires } 1640f81a1295SBenjamin Tissoires 1641f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev, 1642f81a1295SBenjamin Tissoires struct device_attribute *attr, 1643f81a1295SBenjamin Tissoires const char *buf, size_t count) 1644f81a1295SBenjamin Tissoires { 1645ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev); 1646f81a1295SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 1647f81a1295SBenjamin Tissoires u8 new_speed; 1648f81a1295SBenjamin Tissoires 1649f81a1295SBenjamin Tissoires if (kstrtou8(buf, 0, &new_speed)) 1650f81a1295SBenjamin Tissoires return -EINVAL; 1651f81a1295SBenjamin Tissoires 1652f81a1295SBenjamin Tissoires if (new_speed != 0 && new_speed != 1) 1653f81a1295SBenjamin Tissoires return -EINVAL; 1654f81a1295SBenjamin Tissoires 1655f81a1295SBenjamin Tissoires wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features); 1656f81a1295SBenjamin Tissoires 1657f81a1295SBenjamin Tissoires return count; 1658f81a1295SBenjamin Tissoires } 1659f81a1295SBenjamin Tissoires 1660e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM, 1661f81a1295SBenjamin Tissoires wacom_show_speed, wacom_store_speed); 1662f81a1295SBenjamin Tissoires 166372b236d6SAaron Skomra 166472b236d6SAaron Skomra static ssize_t wacom_show_remote_mode(struct kobject *kobj, 166572b236d6SAaron Skomra struct kobj_attribute *kattr, 166672b236d6SAaron Skomra char *buf, int index) 166772b236d6SAaron Skomra { 16682cf83833SGeliang Tang struct device *dev = kobj_to_dev(kobj->parent); 1669ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev); 167072b236d6SAaron Skomra struct wacom *wacom = hid_get_drvdata(hdev); 167172b236d6SAaron Skomra u8 mode; 167272b236d6SAaron Skomra 1673a50aac71SBenjamin Tissoires mode = wacom->led.groups[index].select; 167472b236d6SAaron Skomra if (mode >= 0 && mode < 3) 167572b236d6SAaron Skomra return snprintf(buf, PAGE_SIZE, "%d\n", mode); 167672b236d6SAaron Skomra else 167772b236d6SAaron Skomra return snprintf(buf, PAGE_SIZE, "%d\n", -1); 167872b236d6SAaron Skomra } 167972b236d6SAaron Skomra 168072b236d6SAaron Skomra #define DEVICE_EKR_ATTR_GROUP(SET_ID) \ 168172b236d6SAaron Skomra static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \ 168272b236d6SAaron Skomra struct kobj_attribute *kattr, char *buf) \ 168372b236d6SAaron Skomra { \ 168472b236d6SAaron Skomra return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \ 168572b236d6SAaron Skomra } \ 168672b236d6SAaron Skomra static struct kobj_attribute remote##SET_ID##_mode_attr = { \ 168772b236d6SAaron Skomra .attr = {.name = "remote_mode", \ 168872b236d6SAaron Skomra .mode = DEV_ATTR_RO_PERM}, \ 168972b236d6SAaron Skomra .show = wacom_show_remote##SET_ID##_mode, \ 169072b236d6SAaron Skomra }; \ 169172b236d6SAaron Skomra static struct attribute *remote##SET_ID##_serial_attrs[] = { \ 169272b236d6SAaron Skomra &remote##SET_ID##_mode_attr.attr, \ 169372b236d6SAaron Skomra NULL \ 169472b236d6SAaron Skomra }; \ 169572b236d6SAaron Skomra static struct attribute_group remote##SET_ID##_serial_group = { \ 169672b236d6SAaron Skomra .name = NULL, \ 169772b236d6SAaron Skomra .attrs = remote##SET_ID##_serial_attrs, \ 169872b236d6SAaron Skomra } 169972b236d6SAaron Skomra 170072b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(0); 170172b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(1); 170272b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(2); 170372b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(3); 170472b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(4); 170572b236d6SAaron Skomra 1706e6f2813aSBenjamin Tissoires static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, 1707e6f2813aSBenjamin Tissoires int index) 170872b236d6SAaron Skomra { 170972b236d6SAaron Skomra int error = 0; 171083e6b40eSBenjamin Tissoires struct wacom_remote *remote = wacom->remote; 171172b236d6SAaron Skomra 1712e7749f6eSBenjamin Tissoires remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev, 1713c1f5409bSBenjamin Tissoires GFP_KERNEL, 1714c1f5409bSBenjamin Tissoires "%d", serial); 1715e7749f6eSBenjamin Tissoires if (!remote->remotes[index].group.name) 171672b236d6SAaron Skomra return -ENOMEM; 171772b236d6SAaron Skomra 1718f9036bd4SBenjamin Tissoires error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir, 1719e7749f6eSBenjamin Tissoires &remote->remotes[index].group); 172072b236d6SAaron Skomra if (error) { 1721e7749f6eSBenjamin Tissoires remote->remotes[index].group.name = NULL; 172272b236d6SAaron Skomra hid_err(wacom->hdev, 172372b236d6SAaron Skomra "cannot create sysfs group err: %d\n", error); 172472b236d6SAaron Skomra return error; 172572b236d6SAaron Skomra } 172672b236d6SAaron Skomra 172772b236d6SAaron Skomra return 0; 172872b236d6SAaron Skomra } 172972b236d6SAaron Skomra 173072b236d6SAaron Skomra static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector) 173172b236d6SAaron Skomra { 173272b236d6SAaron Skomra const size_t buf_size = 2; 173372b236d6SAaron Skomra unsigned char *buf; 173472b236d6SAaron Skomra int retval; 173572b236d6SAaron Skomra 173672b236d6SAaron Skomra buf = kzalloc(buf_size, GFP_KERNEL); 173772b236d6SAaron Skomra if (!buf) 173872b236d6SAaron Skomra return -ENOMEM; 173972b236d6SAaron Skomra 174072b236d6SAaron Skomra buf[0] = WAC_CMD_DELETE_PAIRING; 174172b236d6SAaron Skomra buf[1] = selector; 174272b236d6SAaron Skomra 174372b236d6SAaron Skomra retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf, 174472b236d6SAaron Skomra buf_size, WAC_CMD_RETRIES); 174572b236d6SAaron Skomra kfree(buf); 174672b236d6SAaron Skomra 174772b236d6SAaron Skomra return retval; 174872b236d6SAaron Skomra } 174972b236d6SAaron Skomra 175072b236d6SAaron Skomra static ssize_t wacom_store_unpair_remote(struct kobject *kobj, 175172b236d6SAaron Skomra struct kobj_attribute *attr, 175272b236d6SAaron Skomra const char *buf, size_t count) 175372b236d6SAaron Skomra { 175472b236d6SAaron Skomra unsigned char selector = 0; 17552cf83833SGeliang Tang struct device *dev = kobj_to_dev(kobj->parent); 1756ee79a8f8SGeliang Tang struct hid_device *hdev = to_hid_device(dev); 175772b236d6SAaron Skomra struct wacom *wacom = hid_get_drvdata(hdev); 175872b236d6SAaron Skomra int err; 175972b236d6SAaron Skomra 176072b236d6SAaron Skomra if (!strncmp(buf, "*\n", 2)) { 176172b236d6SAaron Skomra selector = WAC_CMD_UNPAIR_ALL; 176272b236d6SAaron Skomra } else { 176372b236d6SAaron Skomra hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n", 176472b236d6SAaron Skomra buf); 176572b236d6SAaron Skomra return -1; 176672b236d6SAaron Skomra } 176772b236d6SAaron Skomra 176872b236d6SAaron Skomra mutex_lock(&wacom->lock); 176972b236d6SAaron Skomra 177072b236d6SAaron Skomra err = wacom_cmd_unpair_remote(wacom, selector); 177172b236d6SAaron Skomra mutex_unlock(&wacom->lock); 177272b236d6SAaron Skomra 177372b236d6SAaron Skomra return err < 0 ? err : count; 177472b236d6SAaron Skomra } 177572b236d6SAaron Skomra 177672b236d6SAaron Skomra static struct kobj_attribute unpair_remote_attr = { 177772b236d6SAaron Skomra .attr = {.name = "unpair_remote", .mode = 0200}, 177872b236d6SAaron Skomra .store = wacom_store_unpair_remote, 177972b236d6SAaron Skomra }; 178072b236d6SAaron Skomra 178172b236d6SAaron Skomra static const struct attribute *remote_unpair_attrs[] = { 178272b236d6SAaron Skomra &unpair_remote_attr.attr, 178372b236d6SAaron Skomra NULL 178472b236d6SAaron Skomra }; 178572b236d6SAaron Skomra 178683e6b40eSBenjamin Tissoires static void wacom_remotes_destroy(void *data) 178783e6b40eSBenjamin Tissoires { 178883e6b40eSBenjamin Tissoires struct wacom *wacom = data; 178983e6b40eSBenjamin Tissoires struct wacom_remote *remote = wacom->remote; 179083e6b40eSBenjamin Tissoires 179183e6b40eSBenjamin Tissoires if (!remote) 179283e6b40eSBenjamin Tissoires return; 179383e6b40eSBenjamin Tissoires 179483e6b40eSBenjamin Tissoires kobject_put(remote->remote_dir); 179583e6b40eSBenjamin Tissoires kfifo_free(&remote->remote_fifo); 179683e6b40eSBenjamin Tissoires wacom->remote = NULL; 179783e6b40eSBenjamin Tissoires } 179883e6b40eSBenjamin Tissoires 179983e6b40eSBenjamin Tissoires static int wacom_initialize_remotes(struct wacom *wacom) 180072b236d6SAaron Skomra { 180172b236d6SAaron Skomra int error = 0; 180283e6b40eSBenjamin Tissoires struct wacom_remote *remote; 180372b236d6SAaron Skomra int i; 180472b236d6SAaron Skomra 180572b236d6SAaron Skomra if (wacom->wacom_wac.features.type != REMOTE) 180672b236d6SAaron Skomra return 0; 180772b236d6SAaron Skomra 180883e6b40eSBenjamin Tissoires remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote), 180983e6b40eSBenjamin Tissoires GFP_KERNEL); 181083e6b40eSBenjamin Tissoires if (!remote) 181172b236d6SAaron Skomra return -ENOMEM; 181272b236d6SAaron Skomra 181383e6b40eSBenjamin Tissoires wacom->remote = remote; 181483e6b40eSBenjamin Tissoires 181583e6b40eSBenjamin Tissoires spin_lock_init(&remote->remote_lock); 181683e6b40eSBenjamin Tissoires 181783e6b40eSBenjamin Tissoires error = kfifo_alloc(&remote->remote_fifo, 181883e6b40eSBenjamin Tissoires 5 * sizeof(struct wacom_remote_data), 181983e6b40eSBenjamin Tissoires GFP_KERNEL); 182083e6b40eSBenjamin Tissoires if (error) { 182183e6b40eSBenjamin Tissoires hid_err(wacom->hdev, "failed allocating remote_fifo\n"); 182283e6b40eSBenjamin Tissoires return -ENOMEM; 182383e6b40eSBenjamin Tissoires } 182483e6b40eSBenjamin Tissoires 1825e7749f6eSBenjamin Tissoires remote->remotes[0].group = remote0_serial_group; 1826e7749f6eSBenjamin Tissoires remote->remotes[1].group = remote1_serial_group; 1827e7749f6eSBenjamin Tissoires remote->remotes[2].group = remote2_serial_group; 1828e7749f6eSBenjamin Tissoires remote->remotes[3].group = remote3_serial_group; 1829e7749f6eSBenjamin Tissoires remote->remotes[4].group = remote4_serial_group; 183083e6b40eSBenjamin Tissoires 183183e6b40eSBenjamin Tissoires remote->remote_dir = kobject_create_and_add("wacom_remote", 183283e6b40eSBenjamin Tissoires &wacom->hdev->dev.kobj); 183383e6b40eSBenjamin Tissoires if (!remote->remote_dir) 183483e6b40eSBenjamin Tissoires return -ENOMEM; 183583e6b40eSBenjamin Tissoires 183683e6b40eSBenjamin Tissoires error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs); 183772b236d6SAaron Skomra 183872b236d6SAaron Skomra if (error) { 183972b236d6SAaron Skomra hid_err(wacom->hdev, 184072b236d6SAaron Skomra "cannot create sysfs group err: %d\n", error); 184172b236d6SAaron Skomra return error; 184272b236d6SAaron Skomra } 184372b236d6SAaron Skomra 184472b236d6SAaron Skomra for (i = 0; i < WACOM_MAX_REMOTES; i++) { 1845a50aac71SBenjamin Tissoires wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN; 1846e7749f6eSBenjamin Tissoires remote->remotes[i].serial = 0; 184772b236d6SAaron Skomra } 184872b236d6SAaron Skomra 184983e6b40eSBenjamin Tissoires error = devm_add_action_or_reset(&wacom->hdev->dev, 185083e6b40eSBenjamin Tissoires wacom_remotes_destroy, wacom); 185183e6b40eSBenjamin Tissoires if (error) 185283e6b40eSBenjamin Tissoires return error; 185383e6b40eSBenjamin Tissoires 185472b236d6SAaron Skomra return 0; 185572b236d6SAaron Skomra } 185672b236d6SAaron Skomra 1857471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom) 1858471d1714SBenjamin Tissoires { 1859471d1714SBenjamin Tissoires struct input_dev *input_dev; 1860471d1714SBenjamin Tissoires struct hid_device *hdev = wacom->hdev; 1861471d1714SBenjamin Tissoires struct wacom_wac *wacom_wac = &(wacom->wacom_wac); 1862471d1714SBenjamin Tissoires 18633dad188eSBenjamin Tissoires input_dev = devm_input_allocate_device(&hdev->dev); 1864471d1714SBenjamin Tissoires if (!input_dev) 1865471d1714SBenjamin Tissoires return NULL; 1866471d1714SBenjamin Tissoires 18672bdd163cSJason Gerecke input_dev->name = wacom_wac->features.name; 1868471d1714SBenjamin Tissoires input_dev->phys = hdev->phys; 1869471d1714SBenjamin Tissoires input_dev->dev.parent = &hdev->dev; 1870471d1714SBenjamin Tissoires input_dev->open = wacom_open; 1871471d1714SBenjamin Tissoires input_dev->close = wacom_close; 1872471d1714SBenjamin Tissoires input_dev->uniq = hdev->uniq; 1873471d1714SBenjamin Tissoires input_dev->id.bustype = hdev->bus; 1874471d1714SBenjamin Tissoires input_dev->id.vendor = hdev->vendor; 187512969e3bSBenjamin Tissoires input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product; 1876471d1714SBenjamin Tissoires input_dev->id.version = hdev->version; 1877471d1714SBenjamin Tissoires input_set_drvdata(input_dev, wacom); 1878471d1714SBenjamin Tissoires 1879471d1714SBenjamin Tissoires return input_dev; 1880471d1714SBenjamin Tissoires } 1881471d1714SBenjamin Tissoires 1882d9f2d203SJason Gerecke static int wacom_allocate_inputs(struct wacom *wacom) 1883d9f2d203SJason Gerecke { 1884d9f2d203SJason Gerecke struct wacom_wac *wacom_wac = &(wacom->wacom_wac); 1885d9f2d203SJason Gerecke 1886d9f2d203SJason Gerecke wacom_wac->pen_input = wacom_allocate_input(wacom); 1887d9f2d203SJason Gerecke wacom_wac->touch_input = wacom_allocate_input(wacom); 1888d9f2d203SJason Gerecke wacom_wac->pad_input = wacom_allocate_input(wacom); 18893dad188eSBenjamin Tissoires if (!wacom_wac->pen_input || 18903dad188eSBenjamin Tissoires !wacom_wac->touch_input || 18913dad188eSBenjamin Tissoires !wacom_wac->pad_input) 1892d9f2d203SJason Gerecke return -ENOMEM; 1893d9f2d203SJason Gerecke 18942bdd163cSJason Gerecke wacom_wac->pen_input->name = wacom_wac->pen_name; 1895d9f2d203SJason Gerecke wacom_wac->touch_input->name = wacom_wac->touch_name; 1896d9f2d203SJason Gerecke wacom_wac->pad_input->name = wacom_wac->pad_name; 1897d9f2d203SJason Gerecke 1898d9f2d203SJason Gerecke return 0; 1899d9f2d203SJason Gerecke } 1900d9f2d203SJason Gerecke 1901471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom) 1902471d1714SBenjamin Tissoires { 19032a6cdbddSJason Gerecke struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev; 1904471d1714SBenjamin Tissoires struct wacom_wac *wacom_wac = &(wacom->wacom_wac); 19052636a3f2SJason Gerecke int error = 0; 1906471d1714SBenjamin Tissoires 19072a6cdbddSJason Gerecke pen_input_dev = wacom_wac->pen_input; 19082a6cdbddSJason Gerecke touch_input_dev = wacom_wac->touch_input; 19092546dacdSBenjamin Tissoires pad_input_dev = wacom_wac->pad_input; 1910471d1714SBenjamin Tissoires 19112a6cdbddSJason Gerecke if (!pen_input_dev || !touch_input_dev || !pad_input_dev) 19122546dacdSBenjamin Tissoires return -EINVAL; 1913471d1714SBenjamin Tissoires 19142a6cdbddSJason Gerecke error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac); 19152a6cdbddSJason Gerecke if (error) { 19162a6cdbddSJason Gerecke /* no pen in use on this interface */ 19172a6cdbddSJason Gerecke input_free_device(pen_input_dev); 19182a6cdbddSJason Gerecke wacom_wac->pen_input = NULL; 19192a6cdbddSJason Gerecke pen_input_dev = NULL; 19202a6cdbddSJason Gerecke } else { 19212a6cdbddSJason Gerecke error = input_register_device(pen_input_dev); 1922471d1714SBenjamin Tissoires if (error) 19233dad188eSBenjamin Tissoires goto fail; 19242a6cdbddSJason Gerecke } 19252a6cdbddSJason Gerecke 19262a6cdbddSJason Gerecke error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac); 19272a6cdbddSJason Gerecke if (error) { 19282a6cdbddSJason Gerecke /* no touch in use on this interface */ 19292a6cdbddSJason Gerecke input_free_device(touch_input_dev); 19302a6cdbddSJason Gerecke wacom_wac->touch_input = NULL; 19312a6cdbddSJason Gerecke touch_input_dev = NULL; 19322a6cdbddSJason Gerecke } else { 19332a6cdbddSJason Gerecke error = input_register_device(touch_input_dev); 19342a6cdbddSJason Gerecke if (error) 19353dad188eSBenjamin Tissoires goto fail; 193630ebc1aeSPing Cheng } 1937471d1714SBenjamin Tissoires 1938471d1714SBenjamin Tissoires error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac); 1939471d1714SBenjamin Tissoires if (error) { 1940471d1714SBenjamin Tissoires /* no pad in use on this interface */ 1941471d1714SBenjamin Tissoires input_free_device(pad_input_dev); 1942471d1714SBenjamin Tissoires wacom_wac->pad_input = NULL; 1943471d1714SBenjamin Tissoires pad_input_dev = NULL; 1944471d1714SBenjamin Tissoires } else { 1945471d1714SBenjamin Tissoires error = input_register_device(pad_input_dev); 1946471d1714SBenjamin Tissoires if (error) 19473dad188eSBenjamin Tissoires goto fail; 1948471d1714SBenjamin Tissoires } 1949471d1714SBenjamin Tissoires 1950471d1714SBenjamin Tissoires return 0; 1951471d1714SBenjamin Tissoires 19523dad188eSBenjamin Tissoires fail: 19533dad188eSBenjamin Tissoires wacom_wac->pad_input = NULL; 19542a6cdbddSJason Gerecke wacom_wac->touch_input = NULL; 19552a6cdbddSJason Gerecke wacom_wac->pen_input = NULL; 1956471d1714SBenjamin Tissoires return error; 1957471d1714SBenjamin Tissoires } 1958471d1714SBenjamin Tissoires 19590be01712SJason Gerecke /* 19600be01712SJason Gerecke * Not all devices report physical dimensions from HID. 19610be01712SJason Gerecke * Compute the default from hardcoded logical dimension 19620be01712SJason Gerecke * and resolution before driver overwrites them. 19630be01712SJason Gerecke */ 19640be01712SJason Gerecke static void wacom_set_default_phy(struct wacom_features *features) 19650be01712SJason Gerecke { 19660be01712SJason Gerecke if (features->x_resolution) { 19670be01712SJason Gerecke features->x_phy = (features->x_max * 100) / 19680be01712SJason Gerecke features->x_resolution; 19690be01712SJason Gerecke features->y_phy = (features->y_max * 100) / 19700be01712SJason Gerecke features->y_resolution; 19710be01712SJason Gerecke } 19720be01712SJason Gerecke } 19730be01712SJason Gerecke 19740be01712SJason Gerecke static void wacom_calculate_res(struct wacom_features *features) 19750be01712SJason Gerecke { 19760be01712SJason Gerecke /* set unit to "100th of a mm" for devices not reported by HID */ 19770be01712SJason Gerecke if (!features->unit) { 19780be01712SJason Gerecke features->unit = 0x11; 19790be01712SJason Gerecke features->unitExpo = -3; 19800be01712SJason Gerecke } 19810be01712SJason Gerecke 19820be01712SJason Gerecke features->x_resolution = wacom_calc_hid_res(features->x_max, 19830be01712SJason Gerecke features->x_phy, 19840be01712SJason Gerecke features->unit, 19850be01712SJason Gerecke features->unitExpo); 19860be01712SJason Gerecke features->y_resolution = wacom_calc_hid_res(features->y_max, 19870be01712SJason Gerecke features->y_phy, 19880be01712SJason Gerecke features->unit, 19890be01712SJason Gerecke features->unitExpo); 19900be01712SJason Gerecke } 19910be01712SJason Gerecke 1992fce9957dSJason Gerecke void wacom_battery_work(struct work_struct *work) 1993fce9957dSJason Gerecke { 1994d17d1f17SBenjamin Tissoires struct wacom *wacom = container_of(work, struct wacom, battery_work); 1995fce9957dSJason Gerecke 1996fce9957dSJason Gerecke if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) && 199759d69bc8SBenjamin Tissoires !wacom->battery.battery) { 1998fce9957dSJason Gerecke wacom_initialize_battery(wacom); 1999fce9957dSJason Gerecke } 2000fce9957dSJason Gerecke else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) && 200159d69bc8SBenjamin Tissoires wacom->battery.battery) { 2002fce9957dSJason Gerecke wacom_destroy_battery(wacom); 2003fce9957dSJason Gerecke } 2004fce9957dSJason Gerecke } 2005fce9957dSJason Gerecke 2006471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev) 2007471d1714SBenjamin Tissoires { 2008471d1714SBenjamin Tissoires struct hid_report_enum *report_enum; 2009471d1714SBenjamin Tissoires struct hid_report *report; 2010471d1714SBenjamin Tissoires size_t size = 0; 2011471d1714SBenjamin Tissoires 2012471d1714SBenjamin Tissoires report_enum = hdev->report_enum + HID_INPUT_REPORT; 2013471d1714SBenjamin Tissoires 2014471d1714SBenjamin Tissoires list_for_each_entry(report, &report_enum->report_list, list) { 2015dabb05c6SMathieu Magnaudet size_t report_size = hid_report_len(report); 2016471d1714SBenjamin Tissoires if (report_size > size) 2017471d1714SBenjamin Tissoires size = report_size; 2018471d1714SBenjamin Tissoires } 2019471d1714SBenjamin Tissoires 2020471d1714SBenjamin Tissoires return size; 2021471d1714SBenjamin Tissoires } 2022471d1714SBenjamin Tissoires 2023fd5f92b6SBenjamin Tissoires static void wacom_update_name(struct wacom *wacom, const char *suffix) 2024c24eab4eSPing Cheng { 2025c24eab4eSPing Cheng struct wacom_wac *wacom_wac = &wacom->wacom_wac; 2026c24eab4eSPing Cheng struct wacom_features *features = &wacom_wac->features; 202744b5250bSJason Gerecke char name[WACOM_NAME_MAX]; 2028c24eab4eSPing Cheng 2029c24eab4eSPing Cheng /* Generic devices name unspecified */ 2030c24eab4eSPing Cheng if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) { 2031c24eab4eSPing Cheng if (strstr(wacom->hdev->name, "Wacom") || 2032c24eab4eSPing Cheng strstr(wacom->hdev->name, "wacom") || 2033c24eab4eSPing Cheng strstr(wacom->hdev->name, "WACOM")) { 2034c24eab4eSPing Cheng /* name is in HID descriptor, use it */ 203544b5250bSJason Gerecke strlcpy(name, wacom->hdev->name, sizeof(name)); 2036c24eab4eSPing Cheng 2037c24eab4eSPing Cheng /* strip out excess whitespaces */ 2038c24eab4eSPing Cheng while (1) { 203944b5250bSJason Gerecke char *gap = strstr(name, " "); 2040c24eab4eSPing Cheng if (gap == NULL) 2041c24eab4eSPing Cheng break; 2042c24eab4eSPing Cheng /* shift everything including the terminator */ 2043c24eab4eSPing Cheng memmove(gap, gap+1, strlen(gap)); 2044c24eab4eSPing Cheng } 2045f2209d4aSJason Gerecke 2046f2209d4aSJason Gerecke /* strip off excessive prefixing */ 2047f2209d4aSJason Gerecke if (strstr(name, "Wacom Co.,Ltd. Wacom ") == name) { 2048f2209d4aSJason Gerecke int n = strlen(name); 2049f2209d4aSJason Gerecke int x = strlen("Wacom Co.,Ltd. "); 2050f2209d4aSJason Gerecke memmove(name, name+x, n-x+1); 2051f2209d4aSJason Gerecke } 2052f2209d4aSJason Gerecke if (strstr(name, "Wacom Co., Ltd. Wacom ") == name) { 2053f2209d4aSJason Gerecke int n = strlen(name); 2054f2209d4aSJason Gerecke int x = strlen("Wacom Co., Ltd. "); 2055f2209d4aSJason Gerecke memmove(name, name+x, n-x+1); 2056f2209d4aSJason Gerecke } 2057f2209d4aSJason Gerecke 2058c24eab4eSPing Cheng /* get rid of trailing whitespace */ 205944b5250bSJason Gerecke if (name[strlen(name)-1] == ' ') 206044b5250bSJason Gerecke name[strlen(name)-1] = '\0'; 2061c24eab4eSPing Cheng } else { 2062c24eab4eSPing Cheng /* no meaningful name retrieved. use product ID */ 206344b5250bSJason Gerecke snprintf(name, sizeof(name), 2064c24eab4eSPing Cheng "%s %X", features->name, wacom->hdev->product); 2065c24eab4eSPing Cheng } 2066c24eab4eSPing Cheng } else { 206744b5250bSJason Gerecke strlcpy(name, features->name, sizeof(name)); 2068c24eab4eSPing Cheng } 2069c24eab4eSPing Cheng 20709956953eSBenjamin Tissoires snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s", 20719956953eSBenjamin Tissoires name, suffix); 20729956953eSBenjamin Tissoires 2073c24eab4eSPing Cheng /* Append the device type to the name */ 20742a6cdbddSJason Gerecke snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name), 2075fd5f92b6SBenjamin Tissoires "%s%s Pen", name, suffix); 20762a6cdbddSJason Gerecke snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name), 2077fd5f92b6SBenjamin Tissoires "%s%s Finger", name, suffix); 2078c24eab4eSPing Cheng snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name), 2079fd5f92b6SBenjamin Tissoires "%s%s Pad", name, suffix); 20808d80f790SJason Gerecke } 2081c24eab4eSPing Cheng 208284dfbd7fSBenjamin Tissoires static void wacom_release_resources(struct wacom *wacom) 208384dfbd7fSBenjamin Tissoires { 208484dfbd7fSBenjamin Tissoires struct hid_device *hdev = wacom->hdev; 208584dfbd7fSBenjamin Tissoires 208684dfbd7fSBenjamin Tissoires if (!wacom->resources) 208784dfbd7fSBenjamin Tissoires return; 208884dfbd7fSBenjamin Tissoires 208984dfbd7fSBenjamin Tissoires devres_release_group(&hdev->dev, wacom); 209084dfbd7fSBenjamin Tissoires 209184dfbd7fSBenjamin Tissoires wacom->resources = false; 209284dfbd7fSBenjamin Tissoires 209384dfbd7fSBenjamin Tissoires wacom->wacom_wac.pen_input = NULL; 209484dfbd7fSBenjamin Tissoires wacom->wacom_wac.touch_input = NULL; 209584dfbd7fSBenjamin Tissoires wacom->wacom_wac.pad_input = NULL; 209684dfbd7fSBenjamin Tissoires } 209784dfbd7fSBenjamin Tissoires 2098d2ec58aeSAaron Armstrong Skomra static void wacom_set_shared_values(struct wacom_wac *wacom_wac) 2099d2ec58aeSAaron Armstrong Skomra { 2100d2ec58aeSAaron Armstrong Skomra if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) { 2101d2ec58aeSAaron Armstrong Skomra wacom_wac->shared->type = wacom_wac->features.type; 2102d2ec58aeSAaron Armstrong Skomra wacom_wac->shared->touch_input = wacom_wac->touch_input; 2103d2ec58aeSAaron Armstrong Skomra } 2104d2ec58aeSAaron Armstrong Skomra 2105d793ff81SPing Cheng if (wacom_wac->has_mute_touch_switch) { 2106d2ec58aeSAaron Armstrong Skomra wacom_wac->shared->has_mute_touch_switch = true; 2107d793ff81SPing Cheng wacom_wac->shared->is_touch_on = true; 2108d793ff81SPing Cheng } 2109d2ec58aeSAaron Armstrong Skomra 2110d2ec58aeSAaron Armstrong Skomra if (wacom_wac->shared->has_mute_touch_switch && 2111d2ec58aeSAaron Armstrong Skomra wacom_wac->shared->touch_input) { 2112d2ec58aeSAaron Armstrong Skomra set_bit(EV_SW, wacom_wac->shared->touch_input->evbit); 2113d2ec58aeSAaron Armstrong Skomra input_set_capability(wacom_wac->shared->touch_input, EV_SW, 2114d2ec58aeSAaron Armstrong Skomra SW_MUTE_DEVICE); 2115d2ec58aeSAaron Armstrong Skomra } 2116d2ec58aeSAaron Armstrong Skomra } 2117d2ec58aeSAaron Armstrong Skomra 2118fd5f92b6SBenjamin Tissoires static int wacom_parse_and_register(struct wacom *wacom, bool wireless) 2119471d1714SBenjamin Tissoires { 2120c58ac3a8SBenjamin Tissoires struct wacom_wac *wacom_wac = &wacom->wacom_wac; 2121c58ac3a8SBenjamin Tissoires struct wacom_features *features = &wacom_wac->features; 2122c58ac3a8SBenjamin Tissoires struct hid_device *hdev = wacom->hdev; 2123471d1714SBenjamin Tissoires int error; 21247704ac93SBenjamin Tissoires unsigned int connect_mask = HID_CONNECT_HIDRAW; 2125471d1714SBenjamin Tissoires 2126471d1714SBenjamin Tissoires features->pktlen = wacom_compute_pktlen(hdev); 2127c58ac3a8SBenjamin Tissoires if (features->pktlen > WACOM_PKGLEN_MAX) 2128c58ac3a8SBenjamin Tissoires return -EINVAL; 2129471d1714SBenjamin Tissoires 213084dfbd7fSBenjamin Tissoires if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL)) 213184dfbd7fSBenjamin Tissoires return -ENOMEM; 213284dfbd7fSBenjamin Tissoires 213384dfbd7fSBenjamin Tissoires wacom->resources = true; 213484dfbd7fSBenjamin Tissoires 2135494078b0SBenjamin Tissoires error = wacom_allocate_inputs(wacom); 2136494078b0SBenjamin Tissoires if (error) 21373888b0d5SBenjamin Tissoires goto fail; 2138494078b0SBenjamin Tissoires 21398c97a765SBenjamin Tissoires /* 21408c97a765SBenjamin Tissoires * Bamboo Pad has a generic hid handling for the Pen, and we switch it 21418c97a765SBenjamin Tissoires * into debug mode for the touch part. 21428c97a765SBenjamin Tissoires * We ignore the other interfaces. 21438c97a765SBenjamin Tissoires */ 21448c97a765SBenjamin Tissoires if (features->type == BAMBOO_PAD) { 21458c97a765SBenjamin Tissoires if (features->pktlen == WACOM_PKGLEN_PENABLED) { 21468c97a765SBenjamin Tissoires features->type = HID_GENERIC; 21478c97a765SBenjamin Tissoires } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) && 21488c97a765SBenjamin Tissoires (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) { 21498c97a765SBenjamin Tissoires error = -ENODEV; 21503888b0d5SBenjamin Tissoires goto fail; 21518c97a765SBenjamin Tissoires } 21528c97a765SBenjamin Tissoires } 21538c97a765SBenjamin Tissoires 2154471d1714SBenjamin Tissoires /* set the default size in case we do not get them from hid */ 2155471d1714SBenjamin Tissoires wacom_set_default_phy(features); 2156471d1714SBenjamin Tissoires 2157471d1714SBenjamin Tissoires /* Retrieve the physical and logical size for touch devices */ 2158471d1714SBenjamin Tissoires wacom_retrieve_hid_descriptor(hdev, features); 215942f4f272SPing Cheng wacom_setup_device_quirks(wacom); 2160042628abSJason Gerecke 2161aa86b18cSJason Gerecke if (features->device_type == WACOM_DEVICETYPE_NONE && 2162aa86b18cSJason Gerecke features->type != WIRELESS) { 21638e116d31SJason Gerecke error = features->type == HID_GENERIC ? -ENODEV : 0; 21648e116d31SJason Gerecke 2165042628abSJason Gerecke dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.", 21668e116d31SJason Gerecke hdev->name, 21678e116d31SJason Gerecke error ? "Ignoring" : "Assuming pen"); 21688e116d31SJason Gerecke 21698e116d31SJason Gerecke if (error) 21703888b0d5SBenjamin Tissoires goto fail; 2171042628abSJason Gerecke 2172aa86b18cSJason Gerecke features->device_type |= WACOM_DEVICETYPE_PEN; 2173042628abSJason Gerecke } 2174042628abSJason Gerecke 2175471d1714SBenjamin Tissoires wacom_calculate_res(features); 2176471d1714SBenjamin Tissoires 2177fd5f92b6SBenjamin Tissoires wacom_update_name(wacom, wireless ? " (WL)" : ""); 2178471d1714SBenjamin Tissoires 21798b407359SAaron Armstrong Skomra /* pen only Bamboo neither support touch nor pad */ 21808b407359SAaron Armstrong Skomra if ((features->type == BAMBOO_PEN) && 21818b407359SAaron Armstrong Skomra ((features->device_type & WACOM_DEVICETYPE_TOUCH) || 21828b407359SAaron Armstrong Skomra (features->device_type & WACOM_DEVICETYPE_PAD))) { 21838b407359SAaron Armstrong Skomra error = -ENODEV; 21848b407359SAaron Armstrong Skomra goto fail; 21858b407359SAaron Armstrong Skomra } 21868b407359SAaron Armstrong Skomra 2187a9ce7856SJason Gerecke error = wacom_add_shared_data(hdev); 2188a9ce7856SJason Gerecke if (error) 2189a9ce7856SJason Gerecke goto fail; 2190471d1714SBenjamin Tissoires 2191ccad85ccSJason Gerecke if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) && 2192f81a1295SBenjamin Tissoires (features->quirks & WACOM_QUIRK_BATTERY)) { 2193f81a1295SBenjamin Tissoires error = wacom_initialize_battery(wacom); 2194f81a1295SBenjamin Tissoires if (error) 21953888b0d5SBenjamin Tissoires goto fail; 2196f81a1295SBenjamin Tissoires } 2197f81a1295SBenjamin Tissoires 2198494078b0SBenjamin Tissoires error = wacom_register_inputs(wacom); 2199471d1714SBenjamin Tissoires if (error) 22003888b0d5SBenjamin Tissoires goto fail; 2201f81a1295SBenjamin Tissoires 2202b62f6465SBenjamin Tissoires if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) { 220385d2c77bSBenjamin Tissoires error = wacom_initialize_leds(wacom); 220485d2c77bSBenjamin Tissoires if (error) 22053888b0d5SBenjamin Tissoires goto fail; 220685d2c77bSBenjamin Tissoires 220783e6b40eSBenjamin Tissoires error = wacom_initialize_remotes(wacom); 2208b62f6465SBenjamin Tissoires if (error) 22093888b0d5SBenjamin Tissoires goto fail; 2210b62f6465SBenjamin Tissoires } 2211b62f6465SBenjamin Tissoires 22127704ac93SBenjamin Tissoires if (features->type == HID_GENERIC) 22137704ac93SBenjamin Tissoires connect_mask |= HID_CONNECT_DRIVER; 22147704ac93SBenjamin Tissoires 2215471d1714SBenjamin Tissoires /* Regular HID work starts now */ 22167704ac93SBenjamin Tissoires error = hid_hw_start(hdev, connect_mask); 2217471d1714SBenjamin Tissoires if (error) { 2218471d1714SBenjamin Tissoires hid_err(hdev, "hw start failed\n"); 22193888b0d5SBenjamin Tissoires goto fail; 2220471d1714SBenjamin Tissoires } 2221471d1714SBenjamin Tissoires 2222fd5f92b6SBenjamin Tissoires if (!wireless) { 222386e88f0eSJason Gerecke /* Note that if query fails it is not a hard failure */ 2224a544c619SBenjamin Tissoires wacom_query_tablet_data(wacom); 2225fd5f92b6SBenjamin Tissoires } 222686e88f0eSJason Gerecke 222786e88f0eSJason Gerecke /* touch only Bamboo doesn't support pen */ 222886e88f0eSJason Gerecke if ((features->type == BAMBOO_TOUCH) && 222986e88f0eSJason Gerecke (features->device_type & WACOM_DEVICETYPE_PEN)) { 22304d20c332SAaron Armstrong Skomra cancel_delayed_work_sync(&wacom->init_work); 22314d20c332SAaron Armstrong Skomra _wacom_query_tablet_data(wacom); 223286e88f0eSJason Gerecke error = -ENODEV; 2233b62f6465SBenjamin Tissoires goto fail_quirks; 223486e88f0eSJason Gerecke } 223586e88f0eSJason Gerecke 2236ccad85ccSJason Gerecke if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) 2237471d1714SBenjamin Tissoires error = hid_hw_open(hdev); 2238471d1714SBenjamin Tissoires 2239d2ec58aeSAaron Armstrong Skomra wacom_set_shared_values(wacom_wac); 224084dfbd7fSBenjamin Tissoires devres_close_group(&hdev->dev, wacom); 224184dfbd7fSBenjamin Tissoires 2242471d1714SBenjamin Tissoires return 0; 2243471d1714SBenjamin Tissoires 2244b62f6465SBenjamin Tissoires fail_quirks: 2245c58ac3a8SBenjamin Tissoires hid_hw_stop(hdev); 22463888b0d5SBenjamin Tissoires fail: 224784dfbd7fSBenjamin Tissoires wacom_release_resources(wacom); 2248c58ac3a8SBenjamin Tissoires return error; 2249c58ac3a8SBenjamin Tissoires } 2250c58ac3a8SBenjamin Tissoires 2251a2f091afSBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work) 2252a2f091afSBenjamin Tissoires { 2253d17d1f17SBenjamin Tissoires struct wacom *wacom = container_of(work, struct wacom, wireless_work); 2254a2f091afSBenjamin Tissoires struct usb_device *usbdev = wacom->usbdev; 2255a2f091afSBenjamin Tissoires struct wacom_wac *wacom_wac = &wacom->wacom_wac; 2256a2f091afSBenjamin Tissoires struct hid_device *hdev1, *hdev2; 2257a2f091afSBenjamin Tissoires struct wacom *wacom1, *wacom2; 2258a2f091afSBenjamin Tissoires struct wacom_wac *wacom_wac1, *wacom_wac2; 2259a2f091afSBenjamin Tissoires int error; 2260a2f091afSBenjamin Tissoires 2261a2f091afSBenjamin Tissoires /* 2262a2f091afSBenjamin Tissoires * Regardless if this is a disconnect or a new tablet, 2263a2f091afSBenjamin Tissoires * remove any existing input and battery devices. 2264a2f091afSBenjamin Tissoires */ 2265a2f091afSBenjamin Tissoires 2266a2f091afSBenjamin Tissoires wacom_destroy_battery(wacom); 2267a2f091afSBenjamin Tissoires 2268a2f091afSBenjamin Tissoires /* Stylus interface */ 2269a2f091afSBenjamin Tissoires hdev1 = usb_get_intfdata(usbdev->config->interface[1]); 2270a2f091afSBenjamin Tissoires wacom1 = hid_get_drvdata(hdev1); 2271a2f091afSBenjamin Tissoires wacom_wac1 = &(wacom1->wacom_wac); 227284dfbd7fSBenjamin Tissoires wacom_release_resources(wacom1); 2273a2f091afSBenjamin Tissoires 2274a2f091afSBenjamin Tissoires /* Touch interface */ 2275a2f091afSBenjamin Tissoires hdev2 = usb_get_intfdata(usbdev->config->interface[2]); 2276a2f091afSBenjamin Tissoires wacom2 = hid_get_drvdata(hdev2); 2277a2f091afSBenjamin Tissoires wacom_wac2 = &(wacom2->wacom_wac); 227884dfbd7fSBenjamin Tissoires wacom_release_resources(wacom2); 2279a2f091afSBenjamin Tissoires 2280a2f091afSBenjamin Tissoires if (wacom_wac->pid == 0) { 2281a2f091afSBenjamin Tissoires hid_info(wacom->hdev, "wireless tablet disconnected\n"); 2282a2f091afSBenjamin Tissoires } else { 2283a2f091afSBenjamin Tissoires const struct hid_device_id *id = wacom_ids; 2284a2f091afSBenjamin Tissoires 2285a2f091afSBenjamin Tissoires hid_info(wacom->hdev, "wireless tablet connected with PID %x\n", 2286a2f091afSBenjamin Tissoires wacom_wac->pid); 2287a2f091afSBenjamin Tissoires 2288a2f091afSBenjamin Tissoires while (id->bus) { 2289a2f091afSBenjamin Tissoires if (id->vendor == USB_VENDOR_ID_WACOM && 2290a2f091afSBenjamin Tissoires id->product == wacom_wac->pid) 2291a2f091afSBenjamin Tissoires break; 2292a2f091afSBenjamin Tissoires id++; 2293a2f091afSBenjamin Tissoires } 2294a2f091afSBenjamin Tissoires 2295a2f091afSBenjamin Tissoires if (!id->bus) { 2296a2f091afSBenjamin Tissoires hid_info(wacom->hdev, "ignoring unknown PID.\n"); 2297a2f091afSBenjamin Tissoires return; 2298a2f091afSBenjamin Tissoires } 2299a2f091afSBenjamin Tissoires 2300a2f091afSBenjamin Tissoires /* Stylus interface */ 2301a2f091afSBenjamin Tissoires wacom_wac1->features = 2302a2f091afSBenjamin Tissoires *((struct wacom_features *)id->driver_data); 2303fd5f92b6SBenjamin Tissoires 2304a2f091afSBenjamin Tissoires wacom_wac1->pid = wacom_wac->pid; 2305fd5f92b6SBenjamin Tissoires hid_hw_stop(hdev1); 2306fd5f92b6SBenjamin Tissoires error = wacom_parse_and_register(wacom1, true); 2307a2f091afSBenjamin Tissoires if (error) 2308a2f091afSBenjamin Tissoires goto fail; 2309a2f091afSBenjamin Tissoires 2310a2f091afSBenjamin Tissoires /* Touch interface */ 2311a2f091afSBenjamin Tissoires if (wacom_wac1->features.touch_max || 2312a2f091afSBenjamin Tissoires (wacom_wac1->features.type >= INTUOSHT && 2313a2f091afSBenjamin Tissoires wacom_wac1->features.type <= BAMBOO_PT)) { 2314a2f091afSBenjamin Tissoires wacom_wac2->features = 2315a2f091afSBenjamin Tissoires *((struct wacom_features *)id->driver_data); 2316a2f091afSBenjamin Tissoires wacom_wac2->pid = wacom_wac->pid; 2317fd5f92b6SBenjamin Tissoires hid_hw_stop(hdev2); 2318fd5f92b6SBenjamin Tissoires error = wacom_parse_and_register(wacom2, true); 2319a2f091afSBenjamin Tissoires if (error) 2320a2f091afSBenjamin Tissoires goto fail; 2321a2f091afSBenjamin Tissoires } 2322a2f091afSBenjamin Tissoires 23239956953eSBenjamin Tissoires strlcpy(wacom_wac->name, wacom_wac1->name, 23249956953eSBenjamin Tissoires sizeof(wacom_wac->name)); 2325a2f091afSBenjamin Tissoires error = wacom_initialize_battery(wacom); 2326a2f091afSBenjamin Tissoires if (error) 2327a2f091afSBenjamin Tissoires goto fail; 2328a2f091afSBenjamin Tissoires } 2329a2f091afSBenjamin Tissoires 2330a2f091afSBenjamin Tissoires return; 2331a2f091afSBenjamin Tissoires 2332a2f091afSBenjamin Tissoires fail: 233384dfbd7fSBenjamin Tissoires wacom_release_resources(wacom1); 233484dfbd7fSBenjamin Tissoires wacom_release_resources(wacom2); 2335a2f091afSBenjamin Tissoires return; 2336a2f091afSBenjamin Tissoires } 2337a2f091afSBenjamin Tissoires 233804bfa27bSBenjamin Tissoires static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index) 233904bfa27bSBenjamin Tissoires { 234004bfa27bSBenjamin Tissoires struct wacom_remote *remote = wacom->remote; 2341e7749f6eSBenjamin Tissoires u32 serial = remote->remotes[index].serial; 234204bfa27bSBenjamin Tissoires int i; 23437c35dc3cSBenjamin Tissoires unsigned long flags; 23447c35dc3cSBenjamin Tissoires 23457c35dc3cSBenjamin Tissoires spin_lock_irqsave(&remote->remote_lock, flags); 23467c35dc3cSBenjamin Tissoires remote->remotes[index].registered = false; 23477c35dc3cSBenjamin Tissoires spin_unlock_irqrestore(&remote->remote_lock, flags); 234804bfa27bSBenjamin Tissoires 23499f1015d4SBenjamin Tissoires if (remote->remotes[index].battery.battery) 23509f1015d4SBenjamin Tissoires devres_release_group(&wacom->hdev->dev, 23519f1015d4SBenjamin Tissoires &remote->remotes[index].battery.bat_desc); 23529f1015d4SBenjamin Tissoires 2353e7749f6eSBenjamin Tissoires if (remote->remotes[index].group.name) 2354e7749f6eSBenjamin Tissoires devres_release_group(&wacom->hdev->dev, 2355e7749f6eSBenjamin Tissoires &remote->remotes[index]); 235604bfa27bSBenjamin Tissoires 235704bfa27bSBenjamin Tissoires for (i = 0; i < WACOM_MAX_REMOTES; i++) { 2358e7749f6eSBenjamin Tissoires if (remote->remotes[i].serial == serial) { 2359e7749f6eSBenjamin Tissoires remote->remotes[i].serial = 0; 2360e7749f6eSBenjamin Tissoires remote->remotes[i].group.name = NULL; 23617c35dc3cSBenjamin Tissoires remote->remotes[i].registered = false; 23629f1015d4SBenjamin Tissoires remote->remotes[i].battery.battery = NULL; 236304bfa27bSBenjamin Tissoires wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN; 236404bfa27bSBenjamin Tissoires } 236504bfa27bSBenjamin Tissoires } 236604bfa27bSBenjamin Tissoires } 236704bfa27bSBenjamin Tissoires 236804bfa27bSBenjamin Tissoires static int wacom_remote_create_one(struct wacom *wacom, u32 serial, 236904bfa27bSBenjamin Tissoires unsigned int index) 237004bfa27bSBenjamin Tissoires { 237104bfa27bSBenjamin Tissoires struct wacom_remote *remote = wacom->remote; 2372f9036bd4SBenjamin Tissoires struct device *dev = &wacom->hdev->dev; 237304bfa27bSBenjamin Tissoires int error, k; 237404bfa27bSBenjamin Tissoires 237504bfa27bSBenjamin Tissoires /* A remote can pair more than once with an EKR, 237604bfa27bSBenjamin Tissoires * check to make sure this serial isn't already paired. 237704bfa27bSBenjamin Tissoires */ 237804bfa27bSBenjamin Tissoires for (k = 0; k < WACOM_MAX_REMOTES; k++) { 2379e7749f6eSBenjamin Tissoires if (remote->remotes[k].serial == serial) 238004bfa27bSBenjamin Tissoires break; 238104bfa27bSBenjamin Tissoires } 238204bfa27bSBenjamin Tissoires 238304bfa27bSBenjamin Tissoires if (k < WACOM_MAX_REMOTES) { 2384e7749f6eSBenjamin Tissoires remote->remotes[index].serial = serial; 238504bfa27bSBenjamin Tissoires return 0; 238604bfa27bSBenjamin Tissoires } 238704bfa27bSBenjamin Tissoires 2388e7749f6eSBenjamin Tissoires if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL)) 2389f9036bd4SBenjamin Tissoires return -ENOMEM; 2390f9036bd4SBenjamin Tissoires 239104bfa27bSBenjamin Tissoires error = wacom_remote_create_attr_group(wacom, serial, index); 239204bfa27bSBenjamin Tissoires if (error) 2393f9036bd4SBenjamin Tissoires goto fail; 239404bfa27bSBenjamin Tissoires 23957c35dc3cSBenjamin Tissoires remote->remotes[index].input = wacom_allocate_input(wacom); 23967c35dc3cSBenjamin Tissoires if (!remote->remotes[index].input) { 23977c35dc3cSBenjamin Tissoires error = -ENOMEM; 23987c35dc3cSBenjamin Tissoires goto fail; 23997c35dc3cSBenjamin Tissoires } 24007c35dc3cSBenjamin Tissoires remote->remotes[index].input->uniq = remote->remotes[index].group.name; 24017c35dc3cSBenjamin Tissoires remote->remotes[index].input->name = wacom->wacom_wac.pad_name; 24027c35dc3cSBenjamin Tissoires 24037c35dc3cSBenjamin Tissoires if (!remote->remotes[index].input->name) { 24047c35dc3cSBenjamin Tissoires error = -EINVAL; 24057c35dc3cSBenjamin Tissoires goto fail; 24067c35dc3cSBenjamin Tissoires } 24077c35dc3cSBenjamin Tissoires 24087c35dc3cSBenjamin Tissoires error = wacom_setup_pad_input_capabilities(remote->remotes[index].input, 24097c35dc3cSBenjamin Tissoires &wacom->wacom_wac); 24107c35dc3cSBenjamin Tissoires if (error) 24117c35dc3cSBenjamin Tissoires goto fail; 24127c35dc3cSBenjamin Tissoires 2413e7749f6eSBenjamin Tissoires remote->remotes[index].serial = serial; 241404bfa27bSBenjamin Tissoires 24157c35dc3cSBenjamin Tissoires error = input_register_device(remote->remotes[index].input); 24167c35dc3cSBenjamin Tissoires if (error) 24177c35dc3cSBenjamin Tissoires goto fail; 24187c35dc3cSBenjamin Tissoires 241997f5541fSBenjamin Tissoires error = wacom_led_groups_alloc_and_register_one( 242097f5541fSBenjamin Tissoires &remote->remotes[index].input->dev, 242197f5541fSBenjamin Tissoires wacom, index, 3, true); 242297f5541fSBenjamin Tissoires if (error) 242397f5541fSBenjamin Tissoires goto fail; 242497f5541fSBenjamin Tissoires 24257c35dc3cSBenjamin Tissoires remote->remotes[index].registered = true; 24267c35dc3cSBenjamin Tissoires 2427e7749f6eSBenjamin Tissoires devres_close_group(dev, &remote->remotes[index]); 242804bfa27bSBenjamin Tissoires return 0; 2429f9036bd4SBenjamin Tissoires 2430f9036bd4SBenjamin Tissoires fail: 2431e7749f6eSBenjamin Tissoires devres_release_group(dev, &remote->remotes[index]); 2432e7749f6eSBenjamin Tissoires remote->remotes[index].serial = 0; 2433f9036bd4SBenjamin Tissoires return error; 243404bfa27bSBenjamin Tissoires } 243504bfa27bSBenjamin Tissoires 24369f1015d4SBenjamin Tissoires static int wacom_remote_attach_battery(struct wacom *wacom, int index) 24379f1015d4SBenjamin Tissoires { 24389f1015d4SBenjamin Tissoires struct wacom_remote *remote = wacom->remote; 24399f1015d4SBenjamin Tissoires int error; 24409f1015d4SBenjamin Tissoires 24419f1015d4SBenjamin Tissoires if (!remote->remotes[index].registered) 24429f1015d4SBenjamin Tissoires return 0; 24439f1015d4SBenjamin Tissoires 24449f1015d4SBenjamin Tissoires if (remote->remotes[index].battery.battery) 24459f1015d4SBenjamin Tissoires return 0; 24469f1015d4SBenjamin Tissoires 24479f1015d4SBenjamin Tissoires if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN) 24489f1015d4SBenjamin Tissoires return 0; 24499f1015d4SBenjamin Tissoires 24509f1015d4SBenjamin Tissoires error = __wacom_initialize_battery(wacom, 24519f1015d4SBenjamin Tissoires &wacom->remote->remotes[index].battery); 24529f1015d4SBenjamin Tissoires if (error) 24539f1015d4SBenjamin Tissoires return error; 24549f1015d4SBenjamin Tissoires 24559f1015d4SBenjamin Tissoires return 0; 24569f1015d4SBenjamin Tissoires } 24579f1015d4SBenjamin Tissoires 2458e6f2813aSBenjamin Tissoires static void wacom_remote_work(struct work_struct *work) 2459e6f2813aSBenjamin Tissoires { 2460e6f2813aSBenjamin Tissoires struct wacom *wacom = container_of(work, struct wacom, remote_work); 246183e6b40eSBenjamin Tissoires struct wacom_remote *remote = wacom->remote; 2462e6f2813aSBenjamin Tissoires struct wacom_remote_data data; 2463e6f2813aSBenjamin Tissoires unsigned long flags; 2464e6f2813aSBenjamin Tissoires unsigned int count; 2465e6f2813aSBenjamin Tissoires u32 serial; 246604bfa27bSBenjamin Tissoires int i; 2467e6f2813aSBenjamin Tissoires 246883e6b40eSBenjamin Tissoires spin_lock_irqsave(&remote->remote_lock, flags); 2469e6f2813aSBenjamin Tissoires 247083e6b40eSBenjamin Tissoires count = kfifo_out(&remote->remote_fifo, &data, sizeof(data)); 2471e6f2813aSBenjamin Tissoires 2472e6f2813aSBenjamin Tissoires if (count != sizeof(data)) { 2473e6f2813aSBenjamin Tissoires hid_err(wacom->hdev, 2474e6f2813aSBenjamin Tissoires "workitem triggered without status available\n"); 247583e6b40eSBenjamin Tissoires spin_unlock_irqrestore(&remote->remote_lock, flags); 2476e6f2813aSBenjamin Tissoires return; 2477e6f2813aSBenjamin Tissoires } 2478e6f2813aSBenjamin Tissoires 247983e6b40eSBenjamin Tissoires if (!kfifo_is_empty(&remote->remote_fifo)) 2480e6f2813aSBenjamin Tissoires wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE); 2481e6f2813aSBenjamin Tissoires 248283e6b40eSBenjamin Tissoires spin_unlock_irqrestore(&remote->remote_lock, flags); 2483e6f2813aSBenjamin Tissoires 2484e6f2813aSBenjamin Tissoires for (i = 0; i < WACOM_MAX_REMOTES; i++) { 2485e6f2813aSBenjamin Tissoires serial = data.remote[i].serial; 2486e6f2813aSBenjamin Tissoires if (data.remote[i].connected) { 2487e6f2813aSBenjamin Tissoires 24889f1015d4SBenjamin Tissoires if (remote->remotes[i].serial == serial) { 24899f1015d4SBenjamin Tissoires wacom_remote_attach_battery(wacom, i); 2490e6f2813aSBenjamin Tissoires continue; 24919f1015d4SBenjamin Tissoires } 2492e6f2813aSBenjamin Tissoires 2493e7749f6eSBenjamin Tissoires if (remote->remotes[i].serial) 249404bfa27bSBenjamin Tissoires wacom_remote_destroy_one(wacom, i); 2495e6f2813aSBenjamin Tissoires 249604bfa27bSBenjamin Tissoires wacom_remote_create_one(wacom, serial, i); 2497e6f2813aSBenjamin Tissoires 2498e7749f6eSBenjamin Tissoires } else if (remote->remotes[i].serial) { 249904bfa27bSBenjamin Tissoires wacom_remote_destroy_one(wacom, i); 2500e6f2813aSBenjamin Tissoires } 2501e6f2813aSBenjamin Tissoires } 2502e6f2813aSBenjamin Tissoires } 2503e6f2813aSBenjamin Tissoires 25044082da80SBenjamin Tissoires static void wacom_mode_change_work(struct work_struct *work) 25054082da80SBenjamin Tissoires { 25064082da80SBenjamin Tissoires struct wacom *wacom = container_of(work, struct wacom, mode_change_work); 25074082da80SBenjamin Tissoires struct wacom_shared *shared = wacom->wacom_wac.shared; 25084082da80SBenjamin Tissoires struct wacom *wacom1 = NULL; 25094082da80SBenjamin Tissoires struct wacom *wacom2 = NULL; 25104082da80SBenjamin Tissoires bool is_direct = wacom->wacom_wac.is_direct_mode; 25114082da80SBenjamin Tissoires int error = 0; 25124082da80SBenjamin Tissoires 25134082da80SBenjamin Tissoires if (shared->pen) { 25144082da80SBenjamin Tissoires wacom1 = hid_get_drvdata(shared->pen); 25154082da80SBenjamin Tissoires wacom_release_resources(wacom1); 25164082da80SBenjamin Tissoires hid_hw_stop(wacom1->hdev); 25174082da80SBenjamin Tissoires wacom1->wacom_wac.has_mode_change = true; 25184082da80SBenjamin Tissoires wacom1->wacom_wac.is_direct_mode = is_direct; 25194082da80SBenjamin Tissoires } 25204082da80SBenjamin Tissoires 25214082da80SBenjamin Tissoires if (shared->touch) { 25224082da80SBenjamin Tissoires wacom2 = hid_get_drvdata(shared->touch); 25234082da80SBenjamin Tissoires wacom_release_resources(wacom2); 25244082da80SBenjamin Tissoires hid_hw_stop(wacom2->hdev); 25254082da80SBenjamin Tissoires wacom2->wacom_wac.has_mode_change = true; 25264082da80SBenjamin Tissoires wacom2->wacom_wac.is_direct_mode = is_direct; 25274082da80SBenjamin Tissoires } 25284082da80SBenjamin Tissoires 25294082da80SBenjamin Tissoires if (wacom1) { 25304082da80SBenjamin Tissoires error = wacom_parse_and_register(wacom1, false); 25314082da80SBenjamin Tissoires if (error) 25324082da80SBenjamin Tissoires return; 25334082da80SBenjamin Tissoires } 25344082da80SBenjamin Tissoires 25354082da80SBenjamin Tissoires if (wacom2) { 25364082da80SBenjamin Tissoires error = wacom_parse_and_register(wacom2, false); 25374082da80SBenjamin Tissoires if (error) 25384082da80SBenjamin Tissoires return; 25394082da80SBenjamin Tissoires } 25404082da80SBenjamin Tissoires 2541c58ac3a8SBenjamin Tissoires return; 2542c58ac3a8SBenjamin Tissoires } 2543c58ac3a8SBenjamin Tissoires 2544c58ac3a8SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev, 2545c58ac3a8SBenjamin Tissoires const struct hid_device_id *id) 2546c58ac3a8SBenjamin Tissoires { 2547c58ac3a8SBenjamin Tissoires struct usb_interface *intf = to_usb_interface(hdev->dev.parent); 2548c58ac3a8SBenjamin Tissoires struct usb_device *dev = interface_to_usbdev(intf); 2549c58ac3a8SBenjamin Tissoires struct wacom *wacom; 2550c58ac3a8SBenjamin Tissoires struct wacom_wac *wacom_wac; 2551c58ac3a8SBenjamin Tissoires struct wacom_features *features; 2552c58ac3a8SBenjamin Tissoires int error; 2553c58ac3a8SBenjamin Tissoires 2554c58ac3a8SBenjamin Tissoires if (!id->driver_data) 2555c58ac3a8SBenjamin Tissoires return -EINVAL; 2556c58ac3a8SBenjamin Tissoires 2557c58ac3a8SBenjamin Tissoires hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS; 2558c58ac3a8SBenjamin Tissoires 2559c58ac3a8SBenjamin Tissoires /* hid-core sets this quirk for the boot interface */ 2560c58ac3a8SBenjamin Tissoires hdev->quirks &= ~HID_QUIRK_NOGET; 2561c58ac3a8SBenjamin Tissoires 256219b64330SBenjamin Tissoires wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL); 2563c58ac3a8SBenjamin Tissoires if (!wacom) 2564c58ac3a8SBenjamin Tissoires return -ENOMEM; 2565c58ac3a8SBenjamin Tissoires 2566c58ac3a8SBenjamin Tissoires hid_set_drvdata(hdev, wacom); 2567c58ac3a8SBenjamin Tissoires wacom->hdev = hdev; 2568c58ac3a8SBenjamin Tissoires 2569c58ac3a8SBenjamin Tissoires wacom_wac = &wacom->wacom_wac; 2570c58ac3a8SBenjamin Tissoires wacom_wac->features = *((struct wacom_features *)id->driver_data); 2571c58ac3a8SBenjamin Tissoires features = &wacom_wac->features; 2572c58ac3a8SBenjamin Tissoires 2573c58ac3a8SBenjamin Tissoires if (features->check_for_hid_type && features->hid_type != hdev->type) { 2574c58ac3a8SBenjamin Tissoires error = -ENODEV; 25753888b0d5SBenjamin Tissoires goto fail; 2576c58ac3a8SBenjamin Tissoires } 2577c58ac3a8SBenjamin Tissoires 2578c6fa1aebSJason Gerecke wacom_wac->hid_data.inputmode = -1; 2579326ea2a9SJason Gerecke wacom_wac->mode_report = -1; 2580c6fa1aebSJason Gerecke 2581c58ac3a8SBenjamin Tissoires wacom->usbdev = dev; 2582c58ac3a8SBenjamin Tissoires wacom->intf = intf; 2583c58ac3a8SBenjamin Tissoires mutex_init(&wacom->lock); 2584a544c619SBenjamin Tissoires INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work); 2585d17d1f17SBenjamin Tissoires INIT_WORK(&wacom->wireless_work, wacom_wireless_work); 2586d17d1f17SBenjamin Tissoires INIT_WORK(&wacom->battery_work, wacom_battery_work); 2587e6f2813aSBenjamin Tissoires INIT_WORK(&wacom->remote_work, wacom_remote_work); 25884082da80SBenjamin Tissoires INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work); 2589c58ac3a8SBenjamin Tissoires 2590c58ac3a8SBenjamin Tissoires /* ask for the report descriptor to be loaded by HID */ 2591c58ac3a8SBenjamin Tissoires error = hid_parse(hdev); 2592c58ac3a8SBenjamin Tissoires if (error) { 2593c58ac3a8SBenjamin Tissoires hid_err(hdev, "parse failed\n"); 25943888b0d5SBenjamin Tissoires goto fail; 2595c58ac3a8SBenjamin Tissoires } 2596c58ac3a8SBenjamin Tissoires 2597fd5f92b6SBenjamin Tissoires error = wacom_parse_and_register(wacom, false); 2598c58ac3a8SBenjamin Tissoires if (error) 25993888b0d5SBenjamin Tissoires goto fail; 2600c58ac3a8SBenjamin Tissoires 2601c58ac3a8SBenjamin Tissoires if (hdev->bus == BUS_BLUETOOTH) { 2602c58ac3a8SBenjamin Tissoires error = device_create_file(&hdev->dev, &dev_attr_speed); 2603c58ac3a8SBenjamin Tissoires if (error) 2604c58ac3a8SBenjamin Tissoires hid_warn(hdev, 2605c58ac3a8SBenjamin Tissoires "can't create sysfs speed attribute err: %d\n", 2606c58ac3a8SBenjamin Tissoires error); 2607c58ac3a8SBenjamin Tissoires } 2608c58ac3a8SBenjamin Tissoires 2609c58ac3a8SBenjamin Tissoires return 0; 2610c58ac3a8SBenjamin Tissoires 26113888b0d5SBenjamin Tissoires fail: 2612471d1714SBenjamin Tissoires hid_set_drvdata(hdev, NULL); 2613471d1714SBenjamin Tissoires return error; 2614471d1714SBenjamin Tissoires } 2615471d1714SBenjamin Tissoires 2616471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev) 2617471d1714SBenjamin Tissoires { 2618471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 2619f6205161SBenjamin Tissoires struct wacom_wac *wacom_wac = &wacom->wacom_wac; 2620f6205161SBenjamin Tissoires struct wacom_features *features = &wacom_wac->features; 2621f6205161SBenjamin Tissoires 2622f6205161SBenjamin Tissoires if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) 2623f6205161SBenjamin Tissoires hid_hw_close(hdev); 2624471d1714SBenjamin Tissoires 2625471d1714SBenjamin Tissoires hid_hw_stop(hdev); 2626471d1714SBenjamin Tissoires 2627a544c619SBenjamin Tissoires cancel_delayed_work_sync(&wacom->init_work); 2628d17d1f17SBenjamin Tissoires cancel_work_sync(&wacom->wireless_work); 2629d17d1f17SBenjamin Tissoires cancel_work_sync(&wacom->battery_work); 2630e6f2813aSBenjamin Tissoires cancel_work_sync(&wacom->remote_work); 26314082da80SBenjamin Tissoires cancel_work_sync(&wacom->mode_change_work); 2632f81a1295SBenjamin Tissoires if (hdev->bus == BUS_BLUETOOTH) 2633f81a1295SBenjamin Tissoires device_remove_file(&hdev->dev, &dev_attr_speed); 2634471d1714SBenjamin Tissoires 2635c0265a94SBenjamin Tissoires /* make sure we don't trigger the LEDs */ 2636c0265a94SBenjamin Tissoires wacom_led_groups_release(wacom); 2637b6b1f19bSAaron Armstrong Skomra 2638b6b1f19bSAaron Armstrong Skomra if (wacom->wacom_wac.features.type != REMOTE) 26395b779fc5SBenjamin Tissoires wacom_release_resources(wacom); 26405b779fc5SBenjamin Tissoires 2641471d1714SBenjamin Tissoires hid_set_drvdata(hdev, NULL); 2642471d1714SBenjamin Tissoires } 2643471d1714SBenjamin Tissoires 264441a74581SGeert Uytterhoeven #ifdef CONFIG_PM 2645471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev) 2646471d1714SBenjamin Tissoires { 2647471d1714SBenjamin Tissoires struct wacom *wacom = hid_get_drvdata(hdev); 2648471d1714SBenjamin Tissoires 2649471d1714SBenjamin Tissoires mutex_lock(&wacom->lock); 2650471d1714SBenjamin Tissoires 2651471d1714SBenjamin Tissoires /* switch to wacom mode first */ 2652a544c619SBenjamin Tissoires _wacom_query_tablet_data(wacom); 2653471d1714SBenjamin Tissoires wacom_led_control(wacom); 2654471d1714SBenjamin Tissoires 2655471d1714SBenjamin Tissoires mutex_unlock(&wacom->lock); 2656471d1714SBenjamin Tissoires 2657471d1714SBenjamin Tissoires return 0; 2658471d1714SBenjamin Tissoires } 2659471d1714SBenjamin Tissoires 2660471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev) 2661471d1714SBenjamin Tissoires { 2662471d1714SBenjamin Tissoires return wacom_resume(hdev); 2663471d1714SBenjamin Tissoires } 266441a74581SGeert Uytterhoeven #endif /* CONFIG_PM */ 2665471d1714SBenjamin Tissoires 2666471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = { 2667471d1714SBenjamin Tissoires .name = "wacom", 2668471d1714SBenjamin Tissoires .id_table = wacom_ids, 2669471d1714SBenjamin Tissoires .probe = wacom_probe, 2670471d1714SBenjamin Tissoires .remove = wacom_remove, 26717704ac93SBenjamin Tissoires .report = wacom_wac_report, 2672471d1714SBenjamin Tissoires #ifdef CONFIG_PM 2673471d1714SBenjamin Tissoires .resume = wacom_resume, 2674471d1714SBenjamin Tissoires .reset_resume = wacom_reset_resume, 2675471d1714SBenjamin Tissoires #endif 2676471d1714SBenjamin Tissoires .raw_event = wacom_raw_event, 2677471d1714SBenjamin Tissoires }; 2678471d1714SBenjamin Tissoires module_hid_driver(wacom_driver); 2679f2e0a7d4SBenjamin Tissoires 2680f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION); 2681f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR); 2682f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC); 26837021b600SGrant Grundler MODULE_LICENSE("GPL"); 2684