···748748 Rumble Force or Force Feedback Wheel.749749750750config HID_WACOM751751- tristate "Wacom Bluetooth devices support"751751+ tristate "Wacom Intuos/Graphire tablet support (USB)"752752 depends on HID753753- depends on LEDS_CLASS754753 select POWER_SUPPLY755755- ---help---756756- Support for Wacom Graphire Bluetooth and Intuos4 WL tablets.754754+ select NEW_LEDS755755+ select LEDS_CLASS756756+ help757757+ Say Y here if you want to use the USB or BT version of the Wacom Intuos758758+ or Graphire tablet.759759+760760+ To compile this driver as a module, choose M here: the761761+ module will be called wacom.757762758763config HID_WIIMOTE759764 tristate "Nintendo Wii / Wii U peripherals"
···11-/*22- * Bluetooth Wacom Tablet support33- *44- * Copyright (c) 1999 Andreas Gal55- * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>66- * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc77- * Copyright (c) 2006-2007 Jiri Kosina88- * Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>99- * Copyright (c) 2006 Andrew Zabolotny <zap@homelink.ru>1010- * Copyright (c) 2009 Bastien Nocera <hadess@hadess.net>1111- * Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu>1212- */1313-1414-/*1515- * This program is free software; you can redistribute it and/or modify it1616- * under the terms of the GNU General Public License as published by the Free1717- * Software Foundation; either version 2 of the License, or (at your option)1818- * any later version.1919- */2020-2121-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt2222-2323-#include <linux/device.h>2424-#include <linux/hid.h>2525-#include <linux/module.h>2626-#include <linux/leds.h>2727-#include <linux/slab.h>2828-#include <linux/power_supply.h>2929-3030-#include "hid-ids.h"3131-3232-#define PAD_DEVICE_ID 0x0F3333-3434-#define WAC_CMD_LED_CONTROL 0x203535-#define WAC_CMD_ICON_START_STOP 0x213636-#define WAC_CMD_ICON_TRANSFER 0x263737-3838-struct wacom_data {3939- __u16 tool;4040- __u16 butstate;4141- __u8 whlstate;4242- __u8 features;4343- __u32 id;4444- __u32 serial;4545- unsigned char high_speed;4646- __u8 battery_capacity;4747- __u8 power_raw;4848- __u8 ps_connected;4949- __u8 bat_charging;5050- struct power_supply battery;5151- struct power_supply ac;5252- __u8 led_selector;5353- struct led_classdev *leds[4];5454-};5555-5656-/*percent of battery capacity for Graphire5757- 8th value means AC online and show 100% capacity */5858-static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };5959-/*percent of battery capacity for Intuos4 WL, AC has a separate bit*/6060-static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };6161-6262-static enum power_supply_property wacom_battery_props[] = {6363- POWER_SUPPLY_PROP_PRESENT,6464- POWER_SUPPLY_PROP_CAPACITY,6565- POWER_SUPPLY_PROP_SCOPE,6666- POWER_SUPPLY_PROP_STATUS,6767-};6868-6969-static enum power_supply_property wacom_ac_props[] = {7070- POWER_SUPPLY_PROP_PRESENT,7171- POWER_SUPPLY_PROP_ONLINE,7272- POWER_SUPPLY_PROP_SCOPE,7373-};7474-7575-static void wacom_scramble(__u8 *image)7676-{7777- __u16 mask;7878- __u16 s1;7979- __u16 s2;8080- __u16 r1 ;8181- __u16 r2 ;8282- __u16 r;8383- __u8 buf[256];8484- int i, w, x, y, z;8585-8686- for (x = 0; x < 32; x++) {8787- for (y = 0; y < 8; y++)8888- buf[(8 * x) + (7 - y)] = image[(8 * x) + y];8989- }9090-9191- /* Change 76543210 into GECA6420 as required by Intuos4 WL9292- * HGFEDCBA HFDB75319393- */9494- for (x = 0; x < 4; x++) {9595- for (y = 0; y < 4; y++) {9696- for (z = 0; z < 8; z++) {9797- mask = 0x0001;9898- r1 = 0;9999- r2 = 0;100100- i = (x << 6) + (y << 4) + z;101101- s1 = buf[i];102102- s2 = buf[i+8];103103- for (w = 0; w < 8; w++) {104104- r1 |= (s1 & mask);105105- r2 |= (s2 & mask);106106- s1 <<= 1;107107- s2 <<= 1;108108- mask <<= 2;109109- }110110- r = r1 | (r2 << 1);111111- i = (x << 6) + (y << 4) + (z << 1);112112- image[i] = 0xFF & r;113113- image[i+1] = (0xFF00 & r) >> 8;114114- }115115- }116116- }117117-}118118-119119-static void wacom_set_image(struct hid_device *hdev, const char *image,120120- __u8 icon_no)121121-{122122- __u8 rep_data[68];123123- __u8 p[256];124124- int ret, i, j;125125-126126- for (i = 0; i < 256; i++)127127- p[i] = image[i];128128-129129- rep_data[0] = WAC_CMD_ICON_START_STOP;130130- rep_data[1] = 0;131131- ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 2,132132- HID_FEATURE_REPORT, HID_REQ_SET_REPORT);133133- if (ret < 0)134134- goto err;135135-136136- rep_data[0] = WAC_CMD_ICON_TRANSFER;137137- rep_data[1] = icon_no & 0x07;138138-139139- wacom_scramble(p);140140-141141- for (i = 0; i < 4; i++) {142142- for (j = 0; j < 64; j++)143143- rep_data[j + 3] = p[(i << 6) + j];144144-145145- rep_data[2] = i;146146- ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 67,147147- HID_FEATURE_REPORT, HID_REQ_SET_REPORT);148148- }149149-150150- rep_data[0] = WAC_CMD_ICON_START_STOP;151151- rep_data[1] = 0;152152-153153- ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 2,154154- HID_FEATURE_REPORT, HID_REQ_SET_REPORT);155155-156156-err:157157- return;158158-}159159-160160-static void wacom_leds_set_brightness(struct led_classdev *led_dev,161161- enum led_brightness value)162162-{163163- struct device *dev = led_dev->dev->parent;164164- struct hid_device *hdev;165165- struct wacom_data *wdata;166166- unsigned char *buf;167167- __u8 led = 0;168168- int i;169169-170170- hdev = container_of(dev, struct hid_device, dev);171171- wdata = hid_get_drvdata(hdev);172172- for (i = 0; i < 4; ++i) {173173- if (wdata->leds[i] == led_dev)174174- wdata->led_selector = i;175175- }176176-177177- led = wdata->led_selector | 0x04;178178- buf = kzalloc(9, GFP_KERNEL);179179- if (buf) {180180- buf[0] = WAC_CMD_LED_CONTROL;181181- buf[1] = led;182182- buf[2] = value >> 2;183183- buf[3] = value;184184- /* use fixed brightness for OLEDs */185185- buf[4] = 0x08;186186- hid_hw_raw_request(hdev, buf[0], buf, 9, HID_FEATURE_REPORT,187187- HID_REQ_SET_REPORT);188188- kfree(buf);189189- }190190-191191- return;192192-}193193-194194-static enum led_brightness wacom_leds_get_brightness(struct led_classdev *led_dev)195195-{196196- struct wacom_data *wdata;197197- struct device *dev = led_dev->dev->parent;198198- int value = 0;199199- int i;200200-201201- wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));202202-203203- for (i = 0; i < 4; ++i) {204204- if (wdata->leds[i] == led_dev) {205205- value = wdata->leds[i]->brightness;206206- break;207207- }208208- }209209-210210- return value;211211-}212212-213213-214214-static int wacom_initialize_leds(struct hid_device *hdev)215215-{216216- struct wacom_data *wdata = hid_get_drvdata(hdev);217217- struct led_classdev *led;218218- struct device *dev = &hdev->dev;219219- size_t namesz = strlen(dev_name(dev)) + 12;220220- char *name;221221- int i, ret;222222-223223- wdata->led_selector = 0;224224-225225- for (i = 0; i < 4; i++) {226226- led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);227227- if (!led) {228228- hid_warn(hdev,229229- "can't allocate memory for LED selector\n");230230- ret = -ENOMEM;231231- goto err;232232- }233233-234234- name = (void *)&led[1];235235- snprintf(name, namesz, "%s:selector:%d", dev_name(dev), i);236236- led->name = name;237237- led->brightness = 0;238238- led->max_brightness = 127;239239- led->brightness_get = wacom_leds_get_brightness;240240- led->brightness_set = wacom_leds_set_brightness;241241-242242- wdata->leds[i] = led;243243-244244- ret = led_classdev_register(dev, wdata->leds[i]);245245-246246- if (ret) {247247- wdata->leds[i] = NULL;248248- kfree(led);249249- hid_warn(hdev, "can't register LED\n");250250- goto err;251251- }252252- }253253-254254-err:255255- return ret;256256-}257257-258258-static void wacom_destroy_leds(struct hid_device *hdev)259259-{260260- struct wacom_data *wdata = hid_get_drvdata(hdev);261261- struct led_classdev *led;262262- int i;263263-264264- for (i = 0; i < 4; ++i) {265265- if (wdata->leds[i]) {266266- led = wdata->leds[i];267267- wdata->leds[i] = NULL;268268- led_classdev_unregister(led);269269- kfree(led);270270- }271271- }272272-273273-}274274-275275-static int wacom_battery_get_property(struct power_supply *psy,276276- enum power_supply_property psp,277277- union power_supply_propval *val)278278-{279279- struct wacom_data *wdata = container_of(psy,280280- struct wacom_data, battery);281281- int ret = 0;282282-283283- switch (psp) {284284- case POWER_SUPPLY_PROP_PRESENT:285285- val->intval = 1;286286- break;287287- case POWER_SUPPLY_PROP_SCOPE:288288- val->intval = POWER_SUPPLY_SCOPE_DEVICE;289289- break;290290- case POWER_SUPPLY_PROP_CAPACITY:291291- val->intval = wdata->battery_capacity;292292- break;293293- case POWER_SUPPLY_PROP_STATUS:294294- if (wdata->bat_charging)295295- val->intval = POWER_SUPPLY_STATUS_CHARGING;296296- else297297- if (wdata->battery_capacity == 100 && wdata->ps_connected)298298- val->intval = POWER_SUPPLY_STATUS_FULL;299299- else300300- val->intval = POWER_SUPPLY_STATUS_DISCHARGING;301301- break;302302- default:303303- ret = -EINVAL;304304- break;305305- }306306- return ret;307307-}308308-309309-static int wacom_ac_get_property(struct power_supply *psy,310310- enum power_supply_property psp,311311- union power_supply_propval *val)312312-{313313- struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);314314- int ret = 0;315315-316316- switch (psp) {317317- case POWER_SUPPLY_PROP_PRESENT:318318- /* fall through */319319- case POWER_SUPPLY_PROP_ONLINE:320320- val->intval = wdata->ps_connected;321321- break;322322- case POWER_SUPPLY_PROP_SCOPE:323323- val->intval = POWER_SUPPLY_SCOPE_DEVICE;324324- break;325325- default:326326- ret = -EINVAL;327327- break;328328- }329329- return ret;330330-}331331-332332-static void wacom_set_features(struct hid_device *hdev, u8 speed)333333-{334334- struct wacom_data *wdata = hid_get_drvdata(hdev);335335- int limit, ret;336336- __u8 rep_data[2];337337-338338- switch (hdev->product) {339339- case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:340340- rep_data[0] = 0x03 ; rep_data[1] = 0x00;341341- limit = 3;342342- do {343343- ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 2,344344- HID_FEATURE_REPORT, HID_REQ_SET_REPORT);345345- } while (ret < 0 && limit-- > 0);346346-347347- if (ret >= 0) {348348- if (speed == 0)349349- rep_data[0] = 0x05;350350- else351351- rep_data[0] = 0x06;352352-353353- rep_data[1] = 0x00;354354- limit = 3;355355- do {356356- ret = hid_hw_raw_request(hdev, rep_data[0],357357- rep_data, 2, HID_FEATURE_REPORT,358358- HID_REQ_SET_REPORT);359359- } while (ret < 0 && limit-- > 0);360360-361361- if (ret >= 0) {362362- wdata->high_speed = speed;363363- return;364364- }365365- }366366-367367- /*368368- * Note that if the raw queries fail, it's not a hard failure369369- * and it is safe to continue370370- */371371- hid_warn(hdev, "failed to poke device, command %d, err %d\n",372372- rep_data[0], ret);373373- break;374374- case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:375375- if (speed == 1)376376- wdata->features &= ~0x20;377377- else378378- wdata->features |= 0x20;379379-380380- rep_data[0] = 0x03;381381- rep_data[1] = wdata->features;382382-383383- ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 2,384384- HID_FEATURE_REPORT, HID_REQ_SET_REPORT);385385- if (ret >= 0)386386- wdata->high_speed = speed;387387- break;388388- }389389-390390- return;391391-}392392-393393-static ssize_t wacom_show_speed(struct device *dev,394394- struct device_attribute395395- *attr, char *buf)396396-{397397- struct wacom_data *wdata = dev_get_drvdata(dev);398398-399399- return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);400400-}401401-402402-static ssize_t wacom_store_speed(struct device *dev,403403- struct device_attribute *attr,404404- const char *buf, size_t count)405405-{406406- struct hid_device *hdev = container_of(dev, struct hid_device, dev);407407- int new_speed;408408-409409- if (sscanf(buf, "%1d", &new_speed ) != 1)410410- return -EINVAL;411411-412412- if (new_speed == 0 || new_speed == 1) {413413- wacom_set_features(hdev, new_speed);414414- return strnlen(buf, PAGE_SIZE);415415- } else416416- return -EINVAL;417417-}418418-419419-static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,420420- wacom_show_speed, wacom_store_speed);421421-422422-#define WACOM_STORE(OLED_ID) \423423-static ssize_t wacom_oled##OLED_ID##_store(struct device *dev, \424424- struct device_attribute *attr, \425425- const char *buf, size_t count) \426426-{ \427427- struct hid_device *hdev = container_of(dev, struct hid_device, \428428- dev); \429429- \430430- if (count != 256) \431431- return -EINVAL; \432432- \433433- wacom_set_image(hdev, buf, OLED_ID); \434434- \435435- return count; \436436-} \437437- \438438-static DEVICE_ATTR(oled##OLED_ID##_img, S_IWUSR | S_IWGRP, NULL, \439439- wacom_oled##OLED_ID##_store)440440-441441-WACOM_STORE(0);442442-WACOM_STORE(1);443443-WACOM_STORE(2);444444-WACOM_STORE(3);445445-WACOM_STORE(4);446446-WACOM_STORE(5);447447-WACOM_STORE(6);448448-WACOM_STORE(7);449449-450450-static int wacom_gr_parse_report(struct hid_device *hdev,451451- struct wacom_data *wdata,452452- struct input_dev *input, unsigned char *data)453453-{454454- int tool, x, y, rw;455455-456456- tool = 0;457457- /* Get X & Y positions */458458- x = le16_to_cpu(*(__le16 *) &data[2]);459459- y = le16_to_cpu(*(__le16 *) &data[4]);460460-461461- /* Get current tool identifier */462462- if (data[1] & 0x90) { /* If pen is in the in/active area */463463- switch ((data[1] >> 5) & 3) {464464- case 0: /* Pen */465465- tool = BTN_TOOL_PEN;466466- break;467467-468468- case 1: /* Rubber */469469- tool = BTN_TOOL_RUBBER;470470- break;471471-472472- case 2: /* Mouse with wheel */473473- case 3: /* Mouse without wheel */474474- tool = BTN_TOOL_MOUSE;475475- break;476476- }477477-478478- /* Reset tool if out of active tablet area */479479- if (!(data[1] & 0x10))480480- tool = 0;481481- }482482-483483- /* If tool changed, notify input subsystem */484484- if (wdata->tool != tool) {485485- if (wdata->tool) {486486- /* Completely reset old tool state */487487- if (wdata->tool == BTN_TOOL_MOUSE) {488488- input_report_key(input, BTN_LEFT, 0);489489- input_report_key(input, BTN_RIGHT, 0);490490- input_report_key(input, BTN_MIDDLE, 0);491491- input_report_abs(input, ABS_DISTANCE,492492- input_abs_get_max(input, ABS_DISTANCE));493493- } else {494494- input_report_key(input, BTN_TOUCH, 0);495495- input_report_key(input, BTN_STYLUS, 0);496496- input_report_key(input, BTN_STYLUS2, 0);497497- input_report_abs(input, ABS_PRESSURE, 0);498498- }499499- input_report_key(input, wdata->tool, 0);500500- input_sync(input);501501- }502502- wdata->tool = tool;503503- if (tool)504504- input_report_key(input, tool, 1);505505- }506506-507507- if (tool) {508508- input_report_abs(input, ABS_X, x);509509- input_report_abs(input, ABS_Y, y);510510-511511- switch ((data[1] >> 5) & 3) {512512- case 2: /* Mouse with wheel */513513- input_report_key(input, BTN_MIDDLE, data[1] & 0x04);514514- rw = (data[6] & 0x01) ? -1 :515515- (data[6] & 0x02) ? 1 : 0;516516- input_report_rel(input, REL_WHEEL, rw);517517- /* fall through */518518-519519- case 3: /* Mouse without wheel */520520- input_report_key(input, BTN_LEFT, data[1] & 0x01);521521- input_report_key(input, BTN_RIGHT, data[1] & 0x02);522522- /* Compute distance between mouse and tablet */523523- rw = 44 - (data[6] >> 2);524524- if (rw < 0)525525- rw = 0;526526- else if (rw > 31)527527- rw = 31;528528- input_report_abs(input, ABS_DISTANCE, rw);529529- break;530530-531531- default:532532- input_report_abs(input, ABS_PRESSURE,533533- data[6] | (((__u16) (data[1] & 0x08)) << 5));534534- input_report_key(input, BTN_TOUCH, data[1] & 0x01);535535- input_report_key(input, BTN_STYLUS, data[1] & 0x02);536536- input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);537537- break;538538- }539539-540540- input_sync(input);541541- }542542-543543- /* Report the state of the two buttons at the top of the tablet544544- * as two extra fingerpad keys (buttons 4 & 5). */545545- rw = data[7] & 0x03;546546- if (rw != wdata->butstate) {547547- wdata->butstate = rw;548548- input_report_key(input, BTN_0, rw & 0x02);549549- input_report_key(input, BTN_1, rw & 0x01);550550- input_report_key(input, BTN_TOOL_FINGER, 0xf0);551551- input_event(input, EV_MSC, MSC_SERIAL, 0xf0);552552- input_sync(input);553553- }554554-555555- /* Store current battery capacity and power supply state*/556556- rw = (data[7] >> 2 & 0x07);557557- if (rw != wdata->power_raw) {558558- wdata->power_raw = rw;559559- wdata->battery_capacity = batcap_gr[rw];560560- if (rw == 7)561561- wdata->ps_connected = 1;562562- else563563- wdata->ps_connected = 0;564564- }565565- return 1;566566-}567567-568568-static void wacom_i4_parse_button_report(struct wacom_data *wdata,569569- struct input_dev *input, unsigned char *data)570570-{571571- __u16 new_butstate;572572- __u8 new_whlstate;573573- __u8 sync = 0;574574-575575- new_whlstate = data[1];576576- if (new_whlstate != wdata->whlstate) {577577- wdata->whlstate = new_whlstate;578578- if (new_whlstate & 0x80) {579579- input_report_key(input, BTN_TOUCH, 1);580580- input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));581581- input_report_key(input, BTN_TOOL_FINGER, 1);582582- } else {583583- input_report_key(input, BTN_TOUCH, 0);584584- input_report_abs(input, ABS_WHEEL, 0);585585- input_report_key(input, BTN_TOOL_FINGER, 0);586586- }587587- sync = 1;588588- }589589-590590- new_butstate = (data[3] << 1) | (data[2] & 0x01);591591- if (new_butstate != wdata->butstate) {592592- wdata->butstate = new_butstate;593593- input_report_key(input, BTN_0, new_butstate & 0x001);594594- input_report_key(input, BTN_1, new_butstate & 0x002);595595- input_report_key(input, BTN_2, new_butstate & 0x004);596596- input_report_key(input, BTN_3, new_butstate & 0x008);597597- input_report_key(input, BTN_4, new_butstate & 0x010);598598- input_report_key(input, BTN_5, new_butstate & 0x020);599599- input_report_key(input, BTN_6, new_butstate & 0x040);600600- input_report_key(input, BTN_7, new_butstate & 0x080);601601- input_report_key(input, BTN_8, new_butstate & 0x100);602602- input_report_key(input, BTN_TOOL_FINGER, 1);603603- sync = 1;604604- }605605-606606- if (sync) {607607- input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);608608- input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);609609- input_sync(input);610610- }611611-}612612-613613-static void wacom_i4_parse_pen_report(struct wacom_data *wdata,614614- struct input_dev *input, unsigned char *data)615615-{616616- __u16 x, y, pressure;617617- __u8 distance;618618- __u8 tilt_x, tilt_y;619619-620620- switch (data[1]) {621621- case 0x80: /* Out of proximity report */622622- input_report_key(input, BTN_TOUCH, 0);623623- input_report_abs(input, ABS_PRESSURE, 0);624624- input_report_key(input, BTN_STYLUS, 0);625625- input_report_key(input, BTN_STYLUS2, 0);626626- input_report_key(input, wdata->tool, 0);627627- input_report_abs(input, ABS_MISC, 0);628628- input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);629629- wdata->tool = 0;630630- input_sync(input);631631- break;632632- case 0xC2: /* Tool report */633633- wdata->id = ((data[2] << 4) | (data[3] >> 4) |634634- ((data[7] & 0x0f) << 20) |635635- ((data[8] & 0xf0) << 12));636636- wdata->serial = ((data[3] & 0x0f) << 28) +637637- (data[4] << 20) + (data[5] << 12) +638638- (data[6] << 4) + (data[7] >> 4);639639-640640- switch (wdata->id) {641641- case 0x100802:642642- wdata->tool = BTN_TOOL_PEN;643643- break;644644- case 0x10080A:645645- wdata->tool = BTN_TOOL_RUBBER;646646- break;647647- }648648- break;649649- default: /* Position/pressure report */650650- x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);651651- y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);652652- pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)653653- | (data[1] & 0x01);654654- distance = (data[9] >> 2) & 0x3f;655655- tilt_x = ((data[7] << 1) & 0x7e) | (data[8] >> 7);656656- tilt_y = data[8] & 0x7f;657657-658658- input_report_key(input, BTN_TOUCH, pressure > 1);659659-660660- input_report_key(input, BTN_STYLUS, data[1] & 0x02);661661- input_report_key(input, BTN_STYLUS2, data[1] & 0x04);662662- input_report_key(input, wdata->tool, 1);663663- input_report_abs(input, ABS_X, x);664664- input_report_abs(input, ABS_Y, y);665665- input_report_abs(input, ABS_PRESSURE, pressure);666666- input_report_abs(input, ABS_DISTANCE, distance);667667- input_report_abs(input, ABS_TILT_X, tilt_x);668668- input_report_abs(input, ABS_TILT_Y, tilt_y);669669- input_report_abs(input, ABS_MISC, wdata->id);670670- input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);671671- input_report_key(input, wdata->tool, 1);672672- input_sync(input);673673- break;674674- }675675-676676- return;677677-}678678-679679-static void wacom_i4_parse_report(struct hid_device *hdev,680680- struct wacom_data *wdata,681681- struct input_dev *input, unsigned char *data)682682-{683683- switch (data[0]) {684684- case 0x00: /* Empty report */685685- break;686686- case 0x02: /* Pen report */687687- wacom_i4_parse_pen_report(wdata, input, data);688688- break;689689- case 0x03: /* Features Report */690690- wdata->features = data[2];691691- break;692692- case 0x0C: /* Button report */693693- wacom_i4_parse_button_report(wdata, input, data);694694- break;695695- default:696696- hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);697697- break;698698- }699699-}700700-701701-static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,702702- u8 *raw_data, int size)703703-{704704- struct wacom_data *wdata = hid_get_drvdata(hdev);705705- struct hid_input *hidinput;706706- struct input_dev *input;707707- unsigned char *data = (unsigned char *) raw_data;708708- int i;709709- __u8 power_raw;710710-711711- if (!(hdev->claimed & HID_CLAIMED_INPUT))712712- return 0;713713-714714- hidinput = list_entry(hdev->inputs.next, struct hid_input, list);715715- input = hidinput->input;716716-717717- switch (hdev->product) {718718- case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:719719- if (data[0] == 0x03) {720720- return wacom_gr_parse_report(hdev, wdata, input, data);721721- } else {722722- hid_err(hdev, "Unknown report: %d,%d size:%d\n",723723- data[0], data[1], size);724724- return 0;725725- }726726- break;727727- case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:728728- i = 1;729729-730730- switch (data[0]) {731731- case 0x04:732732- wacom_i4_parse_report(hdev, wdata, input, data + i);733733- i += 10;734734- /* fall through */735735- case 0x03:736736- wacom_i4_parse_report(hdev, wdata, input, data + i);737737- i += 10;738738- wacom_i4_parse_report(hdev, wdata, input, data + i);739739- power_raw = data[i+10];740740- if (power_raw != wdata->power_raw) {741741- wdata->power_raw = power_raw;742742- wdata->battery_capacity = batcap_i4[power_raw & 0x07];743743- wdata->bat_charging = (power_raw & 0x08) ? 1 : 0;744744- wdata->ps_connected = (power_raw & 0x10) ? 1 : 0;745745- }746746-747747- break;748748- default:749749- hid_err(hdev, "Unknown report: %d,%d size:%d\n",750750- data[0], data[1], size);751751- return 0;752752- }753753- }754754- return 1;755755-}756756-757757-static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,758758- struct hid_field *field, struct hid_usage *usage, unsigned long **bit,759759- int *max)760760-{761761- struct input_dev *input = hi->input;762762-763763- __set_bit(INPUT_PROP_POINTER, input->propbit);764764-765765- /* Basics */766766- input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);767767-768768- __set_bit(REL_WHEEL, input->relbit);769769-770770- __set_bit(BTN_TOOL_PEN, input->keybit);771771- __set_bit(BTN_TOUCH, input->keybit);772772- __set_bit(BTN_STYLUS, input->keybit);773773- __set_bit(BTN_STYLUS2, input->keybit);774774- __set_bit(BTN_LEFT, input->keybit);775775- __set_bit(BTN_RIGHT, input->keybit);776776- __set_bit(BTN_MIDDLE, input->keybit);777777-778778- /* Pad */779779- input_set_capability(input, EV_MSC, MSC_SERIAL);780780-781781- __set_bit(BTN_0, input->keybit);782782- __set_bit(BTN_1, input->keybit);783783- __set_bit(BTN_TOOL_FINGER, input->keybit);784784-785785- /* Distance, rubber and mouse */786786- __set_bit(BTN_TOOL_RUBBER, input->keybit);787787- __set_bit(BTN_TOOL_MOUSE, input->keybit);788788-789789- switch (hdev->product) {790790- case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:791791- input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);792792- input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);793793- input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);794794- input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);795795- break;796796- case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:797797- __set_bit(ABS_WHEEL, input->absbit);798798- __set_bit(ABS_MISC, input->absbit);799799- __set_bit(BTN_2, input->keybit);800800- __set_bit(BTN_3, input->keybit);801801- __set_bit(BTN_4, input->keybit);802802- __set_bit(BTN_5, input->keybit);803803- __set_bit(BTN_6, input->keybit);804804- __set_bit(BTN_7, input->keybit);805805- __set_bit(BTN_8, input->keybit);806806- input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);807807- input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);808808- input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);809809- input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);810810- input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);811811- input_set_abs_params(input, ABS_TILT_X, 0, 127, 0, 0);812812- input_set_abs_params(input, ABS_TILT_Y, 0, 127, 0, 0);813813- break;814814- }815815-816816- return 0;817817-}818818-819819-static int wacom_probe(struct hid_device *hdev,820820- const struct hid_device_id *id)821821-{822822- struct wacom_data *wdata;823823- int ret;824824-825825- wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);826826- if (wdata == NULL) {827827- hid_err(hdev, "can't alloc wacom descriptor\n");828828- return -ENOMEM;829829- }830830-831831- hid_set_drvdata(hdev, wdata);832832-833833- /* Parse the HID report now */834834- ret = hid_parse(hdev);835835- if (ret) {836836- hid_err(hdev, "parse failed\n");837837- goto err_free;838838- }839839-840840- ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);841841- if (ret) {842842- hid_err(hdev, "hw start failed\n");843843- goto err_free;844844- }845845-846846- ret = device_create_file(&hdev->dev, &dev_attr_speed);847847- if (ret)848848- hid_warn(hdev,849849- "can't create sysfs speed attribute err: %d\n", ret);850850-851851-#define OLED_INIT(OLED_ID) \852852- do { \853853- ret = device_create_file(&hdev->dev, \854854- &dev_attr_oled##OLED_ID##_img); \855855- if (ret) \856856- hid_warn(hdev, \857857- "can't create sysfs oled attribute, err: %d\n", ret);\858858- } while (0)859859-860860-OLED_INIT(0);861861-OLED_INIT(1);862862-OLED_INIT(2);863863-OLED_INIT(3);864864-OLED_INIT(4);865865-OLED_INIT(5);866866-OLED_INIT(6);867867-OLED_INIT(7);868868-869869- wdata->features = 0;870870- wacom_set_features(hdev, 1);871871-872872- if (hdev->product == USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) {873873- sprintf(hdev->name, "%s", "Wacom Intuos4 WL");874874- ret = wacom_initialize_leds(hdev);875875- if (ret)876876- hid_warn(hdev,877877- "can't create led attribute, err: %d\n", ret);878878- }879879-880880- wdata->battery.properties = wacom_battery_props;881881- wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);882882- wdata->battery.get_property = wacom_battery_get_property;883883- wdata->battery.name = "wacom_battery";884884- wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;885885- wdata->battery.use_for_apm = 0;886886-887887-888888- ret = power_supply_register(&hdev->dev, &wdata->battery);889889- if (ret) {890890- hid_err(hdev, "can't create sysfs battery attribute, err: %d\n",891891- ret);892892- goto err_battery;893893- }894894-895895- power_supply_powers(&wdata->battery, &hdev->dev);896896-897897- wdata->ac.properties = wacom_ac_props;898898- wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);899899- wdata->ac.get_property = wacom_ac_get_property;900900- wdata->ac.name = "wacom_ac";901901- wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;902902- wdata->ac.use_for_apm = 0;903903-904904- ret = power_supply_register(&hdev->dev, &wdata->ac);905905- if (ret) {906906- hid_err(hdev,907907- "can't create ac battery attribute, err: %d\n", ret);908908- goto err_ac;909909- }910910-911911- power_supply_powers(&wdata->ac, &hdev->dev);912912- return 0;913913-914914-err_ac:915915- power_supply_unregister(&wdata->battery);916916-err_battery:917917- wacom_destroy_leds(hdev);918918- device_remove_file(&hdev->dev, &dev_attr_oled0_img);919919- device_remove_file(&hdev->dev, &dev_attr_oled1_img);920920- device_remove_file(&hdev->dev, &dev_attr_oled2_img);921921- device_remove_file(&hdev->dev, &dev_attr_oled3_img);922922- device_remove_file(&hdev->dev, &dev_attr_oled4_img);923923- device_remove_file(&hdev->dev, &dev_attr_oled5_img);924924- device_remove_file(&hdev->dev, &dev_attr_oled6_img);925925- device_remove_file(&hdev->dev, &dev_attr_oled7_img);926926- device_remove_file(&hdev->dev, &dev_attr_speed);927927- hid_hw_stop(hdev);928928-err_free:929929- kfree(wdata);930930- return ret;931931-}932932-933933-static void wacom_remove(struct hid_device *hdev)934934-{935935- struct wacom_data *wdata = hid_get_drvdata(hdev);936936-937937- wacom_destroy_leds(hdev);938938- device_remove_file(&hdev->dev, &dev_attr_oled0_img);939939- device_remove_file(&hdev->dev, &dev_attr_oled1_img);940940- device_remove_file(&hdev->dev, &dev_attr_oled2_img);941941- device_remove_file(&hdev->dev, &dev_attr_oled3_img);942942- device_remove_file(&hdev->dev, &dev_attr_oled4_img);943943- device_remove_file(&hdev->dev, &dev_attr_oled5_img);944944- device_remove_file(&hdev->dev, &dev_attr_oled6_img);945945- device_remove_file(&hdev->dev, &dev_attr_oled7_img);946946- device_remove_file(&hdev->dev, &dev_attr_speed);947947- hid_hw_stop(hdev);948948-949949- power_supply_unregister(&wdata->battery);950950- power_supply_unregister(&wdata->ac);951951- kfree(hid_get_drvdata(hdev));952952-}953953-954954-static const struct hid_device_id wacom_devices[] = {955955- { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },956956- { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },957957-958958- { }959959-};960960-MODULE_DEVICE_TABLE(hid, wacom_devices);961961-962962-static struct hid_driver wacom_driver = {963963- .name = "wacom",964964- .id_table = wacom_devices,965965- .probe = wacom_probe,966966- .remove = wacom_remove,967967- .raw_event = wacom_raw_event,968968- .input_mapped = wacom_input_mapped,969969-};970970-module_hid_driver(wacom_driver);971971-972972-MODULE_DESCRIPTION("Driver for Wacom Graphire Bluetooth and Wacom Intuos4 WL");973973-MODULE_LICENSE("GPL");
+1456
drivers/hid/wacom_sys.c
···11+/*22+ * drivers/input/tablet/wacom_sys.c33+ *44+ * USB Wacom tablet support - system specific code55+ */66+77+/*88+ * This program is free software; you can redistribute it and/or modify99+ * it under the terms of the GNU General Public License as published by1010+ * the Free Software Foundation; either version 2 of the License, or1111+ * (at your option) any later version.1212+ */1313+1414+#include "wacom_wac.h"1515+#include "wacom.h"1616+#include <linux/hid.h>1717+1818+#define WAC_MSG_RETRIES 51919+2020+#define WAC_CMD_LED_CONTROL 0x202121+#define WAC_CMD_ICON_START 0x212222+#define WAC_CMD_ICON_XFER 0x232323+#define WAC_CMD_ICON_BT_XFER 0x262424+#define WAC_CMD_RETRIES 102525+2626+static int wacom_get_report(struct hid_device *hdev, u8 type, u8 id,2727+ void *buf, size_t size, unsigned int retries)2828+{2929+ int retval;3030+3131+ do {3232+ retval = hid_hw_raw_request(hdev, id, buf, size, type,3333+ HID_REQ_GET_REPORT);3434+ } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);3535+3636+ return retval;3737+}3838+3939+static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,4040+ size_t size, unsigned int retries)4141+{4242+ int retval;4343+4444+ do {4545+ retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,4646+ HID_REQ_SET_REPORT);4747+ } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);4848+4949+ return retval;5050+}5151+5252+static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,5353+ u8 *raw_data, int size)5454+{5555+ struct wacom *wacom = hid_get_drvdata(hdev);5656+5757+ if (size > WACOM_PKGLEN_MAX)5858+ return 1;5959+6060+ memcpy(wacom->wacom_wac.data, raw_data, size);6161+6262+ wacom_wac_irq(&wacom->wacom_wac, size);6363+6464+ return 0;6565+}6666+6767+static int wacom_open(struct input_dev *dev)6868+{6969+ struct wacom *wacom = input_get_drvdata(dev);7070+ int retval;7171+7272+ mutex_lock(&wacom->lock);7373+ retval = hid_hw_open(wacom->hdev);7474+ mutex_unlock(&wacom->lock);7575+7676+ return retval;7777+}7878+7979+static void wacom_close(struct input_dev *dev)8080+{8181+ struct wacom *wacom = input_get_drvdata(dev);8282+8383+ mutex_lock(&wacom->lock);8484+ hid_hw_close(wacom->hdev);8585+ mutex_unlock(&wacom->lock);8686+}8787+8888+/*8989+ * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.9090+ */9191+static int wacom_calc_hid_res(int logical_extents, int physical_extents,9292+ unsigned unit, int exponent)9393+{9494+ struct hid_field field = {9595+ .logical_maximum = logical_extents,9696+ .physical_maximum = physical_extents,9797+ .unit = unit,9898+ .unit_exponent = exponent,9999+ };100100+101101+ return hidinput_calc_abs_res(&field, ABS_X);102102+}103103+104104+static void wacom_feature_mapping(struct hid_device *hdev,105105+ struct hid_field *field, struct hid_usage *usage)106106+{107107+ struct wacom *wacom = hid_get_drvdata(hdev);108108+ struct wacom_features *features = &wacom->wacom_wac.features;109109+110110+ switch (usage->hid) {111111+ case HID_DG_CONTACTMAX:112112+ /* leave touch_max as is if predefined */113113+ if (!features->touch_max)114114+ features->touch_max = field->value[0];115115+ break;116116+ }117117+}118118+119119+/*120120+ * Interface Descriptor of wacom devices can be incomplete and121121+ * inconsistent so wacom_features table is used to store stylus122122+ * device's packet lengths, various maximum values, and tablet123123+ * resolution based on product ID's.124124+ *125125+ * For devices that contain 2 interfaces, wacom_features table is126126+ * inaccurate for the touch interface. Since the Interface Descriptor127127+ * for touch interfaces has pretty complete data, this function exists128128+ * to query tablet for this missing information instead of hard coding in129129+ * an additional table.130130+ *131131+ * A typical Interface Descriptor for a stylus will contain a132132+ * boot mouse application collection that is not of interest and this133133+ * function will ignore it.134134+ *135135+ * It also contains a digitizer application collection that also is not136136+ * of interest since any information it contains would be duplicate137137+ * of what is in wacom_features. Usually it defines a report of an array138138+ * of bytes that could be used as max length of the stylus packet returned.139139+ * If it happens to define a Digitizer-Stylus Physical Collection then140140+ * the X and Y logical values contain valid data but it is ignored.141141+ *142142+ * A typical Interface Descriptor for a touch interface will contain a143143+ * Digitizer-Finger Physical Collection which will define both logical144144+ * X/Y maximum as well as the physical size of tablet. Since touch145145+ * interfaces haven't supported pressure or distance, this is enough146146+ * information to override invalid values in the wacom_features table.147147+ *148148+ * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful149149+ * data. We deal with them after returning from this function.150150+ */151151+static void wacom_usage_mapping(struct hid_device *hdev,152152+ struct hid_field *field, struct hid_usage *usage)153153+{154154+ struct wacom *wacom = hid_get_drvdata(hdev);155155+ struct wacom_features *features = &wacom->wacom_wac.features;156156+ bool finger = (field->logical == HID_DG_FINGER) ||157157+ (field->physical == HID_DG_FINGER);158158+ bool pen = (field->logical == HID_DG_STYLUS) ||159159+ (field->physical == HID_DG_STYLUS);160160+161161+ /*162162+ * Requiring Stylus Usage will ignore boot mouse163163+ * X/Y values and some cases of invalid Digitizer X/Y164164+ * values commonly reported.165165+ */166166+ if (!pen && !finger)167167+ return;168168+169169+ if (finger && !features->touch_max)170170+ /* touch device at least supports one touch point */171171+ features->touch_max = 1;172172+173173+ switch (usage->hid) {174174+ case HID_GD_X:175175+ features->x_max = field->logical_maximum;176176+ if (finger) {177177+ features->device_type = BTN_TOOL_FINGER;178178+ features->x_phy = field->physical_maximum;179179+ if (features->type != BAMBOO_PT) {180180+ features->unit = field->unit;181181+ features->unitExpo = field->unit_exponent;182182+ }183183+ } else {184184+ features->device_type = BTN_TOOL_PEN;185185+ }186186+ break;187187+ case HID_GD_Y:188188+ features->y_max = field->logical_maximum;189189+ if (finger) {190190+ features->y_phy = field->physical_maximum;191191+ if (features->type != BAMBOO_PT) {192192+ features->unit = field->unit;193193+ features->unitExpo = field->unit_exponent;194194+ }195195+ }196196+ break;197197+ case HID_DG_TIPPRESSURE:198198+ if (pen)199199+ features->pressure_max = field->logical_maximum;200200+ break;201201+ }202202+}203203+204204+static void wacom_parse_hid(struct hid_device *hdev,205205+ struct wacom_features *features)206206+{207207+ struct hid_report_enum *rep_enum;208208+ struct hid_report *hreport;209209+ int i, j;210210+211211+ /* check features first */212212+ rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];213213+ list_for_each_entry(hreport, &rep_enum->report_list, list) {214214+ for (i = 0; i < hreport->maxfield; i++) {215215+ /* Ignore if report count is out of bounds. */216216+ if (hreport->field[i]->report_count < 1)217217+ continue;218218+219219+ for (j = 0; j < hreport->field[i]->maxusage; j++) {220220+ wacom_feature_mapping(hdev, hreport->field[i],221221+ hreport->field[i]->usage + j);222222+ }223223+ }224224+ }225225+226226+ /* now check the input usages */227227+ rep_enum = &hdev->report_enum[HID_INPUT_REPORT];228228+ list_for_each_entry(hreport, &rep_enum->report_list, list) {229229+230230+ if (!hreport->maxfield)231231+ continue;232232+233233+ for (i = 0; i < hreport->maxfield; i++)234234+ for (j = 0; j < hreport->field[i]->maxusage; j++)235235+ wacom_usage_mapping(hdev, hreport->field[i],236236+ hreport->field[i]->usage + j);237237+ }238238+}239239+240240+static int wacom_set_device_mode(struct hid_device *hdev, int report_id,241241+ int length, int mode)242242+{243243+ unsigned char *rep_data;244244+ int error = -ENOMEM, limit = 0;245245+246246+ rep_data = kzalloc(length, GFP_KERNEL);247247+ if (!rep_data)248248+ return error;249249+250250+ do {251251+ rep_data[0] = report_id;252252+ rep_data[1] = mode;253253+254254+ error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,255255+ length, 1);256256+ if (error >= 0)257257+ error = wacom_get_report(hdev, HID_FEATURE_REPORT,258258+ report_id, rep_data, length, 1);259259+ } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);260260+261261+ kfree(rep_data);262262+263263+ return error < 0 ? error : 0;264264+}265265+266266+static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,267267+ struct wacom_features *features)268268+{269269+ struct wacom *wacom = hid_get_drvdata(hdev);270270+ int ret;271271+ u8 rep_data[2];272272+273273+ switch (features->type) {274274+ case GRAPHIRE_BT:275275+ rep_data[0] = 0x03;276276+ rep_data[1] = 0x00;277277+ ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,278278+ 3);279279+280280+ if (ret >= 0) {281281+ rep_data[0] = speed == 0 ? 0x05 : 0x06;282282+ rep_data[1] = 0x00;283283+284284+ ret = wacom_set_report(hdev, HID_FEATURE_REPORT,285285+ rep_data, 2, 3);286286+287287+ if (ret >= 0) {288288+ wacom->wacom_wac.bt_high_speed = speed;289289+ return 0;290290+ }291291+ }292292+293293+ /*294294+ * Note that if the raw queries fail, it's not a hard failure295295+ * and it is safe to continue296296+ */297297+ hid_warn(hdev, "failed to poke device, command %d, err %d\n",298298+ rep_data[0], ret);299299+ break;300300+ case INTUOS4WL:301301+ if (speed == 1)302302+ wacom->wacom_wac.bt_features &= ~0x20;303303+ else304304+ wacom->wacom_wac.bt_features |= 0x20;305305+306306+ rep_data[0] = 0x03;307307+ rep_data[1] = wacom->wacom_wac.bt_features;308308+309309+ ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,310310+ 1);311311+ if (ret >= 0)312312+ wacom->wacom_wac.bt_high_speed = speed;313313+ break;314314+ }315315+316316+ return 0;317317+}318318+319319+/*320320+ * Switch the tablet into its most-capable mode. Wacom tablets are321321+ * typically configured to power-up in a mode which sends mouse-like322322+ * reports to the OS. To get absolute position, pressure data, etc.323323+ * from the tablet, it is necessary to switch the tablet out of this324324+ * mode and into one which sends the full range of tablet data.325325+ */326326+static int wacom_query_tablet_data(struct hid_device *hdev,327327+ struct wacom_features *features)328328+{329329+ if (hdev->bus == BUS_BLUETOOTH)330330+ return wacom_bt_query_tablet_data(hdev, 1, features);331331+332332+ if (features->device_type == BTN_TOOL_FINGER) {333333+ if (features->type > TABLETPC) {334334+ /* MT Tablet PC touch */335335+ return wacom_set_device_mode(hdev, 3, 4, 4);336336+ }337337+ else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {338338+ return wacom_set_device_mode(hdev, 18, 3, 2);339339+ }340340+ } else if (features->device_type == BTN_TOOL_PEN) {341341+ if (features->type <= BAMBOO_PT && features->type != WIRELESS) {342342+ return wacom_set_device_mode(hdev, 2, 2, 2);343343+ }344344+ }345345+346346+ return 0;347347+}348348+349349+static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,350350+ struct wacom_features *features)351351+{352352+ struct wacom *wacom = hid_get_drvdata(hdev);353353+ struct usb_interface *intf = wacom->intf;354354+355355+ /* default features */356356+ features->device_type = BTN_TOOL_PEN;357357+ features->x_fuzz = 4;358358+ features->y_fuzz = 4;359359+ features->pressure_fuzz = 0;360360+ features->distance_fuzz = 0;361361+362362+ /*363363+ * The wireless device HID is basic and layout conflicts with364364+ * other tablets (monitor and touch interface can look like pen).365365+ * Skip the query for this type and modify defaults based on366366+ * interface number.367367+ */368368+ if (features->type == WIRELESS) {369369+ if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {370370+ features->device_type = 0;371371+ } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {372372+ features->device_type = BTN_TOOL_FINGER;373373+ features->pktlen = WACOM_PKGLEN_BBTOUCH3;374374+ }375375+ }376376+377377+ /* only devices that support touch need to retrieve the info */378378+ if (features->type < BAMBOO_PT)379379+ return;380380+381381+ wacom_parse_hid(hdev, features);382382+}383383+384384+struct wacom_hdev_data {385385+ struct list_head list;386386+ struct kref kref;387387+ struct hid_device *dev;388388+ struct wacom_shared shared;389389+};390390+391391+static LIST_HEAD(wacom_udev_list);392392+static DEFINE_MUTEX(wacom_udev_list_lock);393393+394394+static bool wacom_are_sibling(struct hid_device *hdev,395395+ struct hid_device *sibling)396396+{397397+ struct wacom *wacom = hid_get_drvdata(hdev);398398+ struct wacom_features *features = &wacom->wacom_wac.features;399399+ int vid = features->oVid;400400+ int pid = features->oPid;401401+ int n1,n2;402402+403403+ if (vid == 0 && pid == 0) {404404+ vid = hdev->vendor;405405+ pid = hdev->product;406406+ }407407+408408+ if (vid != sibling->vendor || pid != sibling->product)409409+ return false;410410+411411+ /* Compare the physical path. */412412+ n1 = strrchr(hdev->phys, '.') - hdev->phys;413413+ n2 = strrchr(sibling->phys, '.') - sibling->phys;414414+ if (n1 != n2 || n1 <= 0 || n2 <= 0)415415+ return false;416416+417417+ return !strncmp(hdev->phys, sibling->phys, n1);418418+}419419+420420+static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)421421+{422422+ struct wacom_hdev_data *data;423423+424424+ list_for_each_entry(data, &wacom_udev_list, list) {425425+ if (wacom_are_sibling(hdev, data->dev)) {426426+ kref_get(&data->kref);427427+ return data;428428+ }429429+ }430430+431431+ return NULL;432432+}433433+434434+static int wacom_add_shared_data(struct hid_device *hdev)435435+{436436+ struct wacom *wacom = hid_get_drvdata(hdev);437437+ struct wacom_wac *wacom_wac = &wacom->wacom_wac;438438+ struct wacom_hdev_data *data;439439+ int retval = 0;440440+441441+ mutex_lock(&wacom_udev_list_lock);442442+443443+ data = wacom_get_hdev_data(hdev);444444+ if (!data) {445445+ data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);446446+ if (!data) {447447+ retval = -ENOMEM;448448+ goto out;449449+ }450450+451451+ kref_init(&data->kref);452452+ data->dev = hdev;453453+ list_add_tail(&data->list, &wacom_udev_list);454454+ }455455+456456+ wacom_wac->shared = &data->shared;457457+458458+out:459459+ mutex_unlock(&wacom_udev_list_lock);460460+ return retval;461461+}462462+463463+static void wacom_release_shared_data(struct kref *kref)464464+{465465+ struct wacom_hdev_data *data =466466+ container_of(kref, struct wacom_hdev_data, kref);467467+468468+ mutex_lock(&wacom_udev_list_lock);469469+ list_del(&data->list);470470+ mutex_unlock(&wacom_udev_list_lock);471471+472472+ kfree(data);473473+}474474+475475+static void wacom_remove_shared_data(struct wacom_wac *wacom)476476+{477477+ struct wacom_hdev_data *data;478478+479479+ if (wacom->shared) {480480+ data = container_of(wacom->shared, struct wacom_hdev_data, shared);481481+ kref_put(&data->kref, wacom_release_shared_data);482482+ wacom->shared = NULL;483483+ }484484+}485485+486486+static int wacom_led_control(struct wacom *wacom)487487+{488488+ unsigned char *buf;489489+ int retval;490490+491491+ buf = kzalloc(9, GFP_KERNEL);492492+ if (!buf)493493+ return -ENOMEM;494494+495495+ if (wacom->wacom_wac.features.type >= INTUOS5S &&496496+ wacom->wacom_wac.features.type <= INTUOSPL) {497497+ /*498498+ * Touch Ring and crop mark LED luminance may take on499499+ * one of four values:500500+ * 0 = Low; 1 = Medium; 2 = High; 3 = Off501501+ */502502+ int ring_led = wacom->led.select[0] & 0x03;503503+ int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;504504+ int crop_lum = 0;505505+506506+ buf[0] = WAC_CMD_LED_CONTROL;507507+ buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);508508+ }509509+ else {510510+ int led = wacom->led.select[0] | 0x4;511511+512512+ if (wacom->wacom_wac.features.type == WACOM_21UX2 ||513513+ wacom->wacom_wac.features.type == WACOM_24HD)514514+ led |= (wacom->led.select[1] << 4) | 0x40;515515+516516+ buf[0] = WAC_CMD_LED_CONTROL;517517+ buf[1] = led;518518+ buf[2] = wacom->led.llv;519519+ buf[3] = wacom->led.hlv;520520+ buf[4] = wacom->led.img_lum;521521+ }522522+523523+ retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 9,524524+ WAC_CMD_RETRIES);525525+ kfree(buf);526526+527527+ return retval;528528+}529529+530530+static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,531531+ const unsigned len, const void *img)532532+{533533+ unsigned char *buf;534534+ int i, retval;535535+ const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */536536+537537+ buf = kzalloc(chunk_len + 3 , GFP_KERNEL);538538+ if (!buf)539539+ return -ENOMEM;540540+541541+ /* Send 'start' command */542542+ buf[0] = WAC_CMD_ICON_START;543543+ buf[1] = 1;544544+ retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,545545+ WAC_CMD_RETRIES);546546+ if (retval < 0)547547+ goto out;548548+549549+ buf[0] = xfer_id;550550+ buf[1] = button_id & 0x07;551551+ for (i = 0; i < 4; i++) {552552+ buf[2] = i;553553+ memcpy(buf + 3, img + i * chunk_len, chunk_len);554554+555555+ retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,556556+ buf, chunk_len + 3, WAC_CMD_RETRIES);557557+ if (retval < 0)558558+ break;559559+ }560560+561561+ /* Send 'stop' */562562+ buf[0] = WAC_CMD_ICON_START;563563+ buf[1] = 0;564564+ wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,565565+ WAC_CMD_RETRIES);566566+567567+out:568568+ kfree(buf);569569+ return retval;570570+}571571+572572+static ssize_t wacom_led_select_store(struct device *dev, int set_id,573573+ const char *buf, size_t count)574574+{575575+ struct hid_device *hdev = container_of(dev, struct hid_device, dev);576576+ struct wacom *wacom = hid_get_drvdata(hdev);577577+ unsigned int id;578578+ int err;579579+580580+ err = kstrtouint(buf, 10, &id);581581+ if (err)582582+ return err;583583+584584+ mutex_lock(&wacom->lock);585585+586586+ wacom->led.select[set_id] = id & 0x3;587587+ err = wacom_led_control(wacom);588588+589589+ mutex_unlock(&wacom->lock);590590+591591+ return err < 0 ? err : count;592592+}593593+594594+#define DEVICE_LED_SELECT_ATTR(SET_ID) \595595+static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \596596+ struct device_attribute *attr, const char *buf, size_t count) \597597+{ \598598+ return wacom_led_select_store(dev, SET_ID, buf, count); \599599+} \600600+static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \601601+ struct device_attribute *attr, char *buf) \602602+{ \603603+ struct hid_device *hdev = container_of(dev, struct hid_device, dev);\604604+ struct wacom *wacom = hid_get_drvdata(hdev); \605605+ return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \606606+} \607607+static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \608608+ wacom_led##SET_ID##_select_show, \609609+ wacom_led##SET_ID##_select_store)610610+611611+DEVICE_LED_SELECT_ATTR(0);612612+DEVICE_LED_SELECT_ATTR(1);613613+614614+static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,615615+ const char *buf, size_t count)616616+{617617+ unsigned int value;618618+ int err;619619+620620+ err = kstrtouint(buf, 10, &value);621621+ if (err)622622+ return err;623623+624624+ mutex_lock(&wacom->lock);625625+626626+ *dest = value & 0x7f;627627+ err = wacom_led_control(wacom);628628+629629+ mutex_unlock(&wacom->lock);630630+631631+ return err < 0 ? err : count;632632+}633633+634634+#define DEVICE_LUMINANCE_ATTR(name, field) \635635+static ssize_t wacom_##name##_luminance_store(struct device *dev, \636636+ struct device_attribute *attr, const char *buf, size_t count) \637637+{ \638638+ struct hid_device *hdev = container_of(dev, struct hid_device, dev);\639639+ struct wacom *wacom = hid_get_drvdata(hdev); \640640+ \641641+ return wacom_luminance_store(wacom, &wacom->led.field, \642642+ buf, count); \643643+} \644644+static DEVICE_ATTR(name##_luminance, S_IWUSR, \645645+ NULL, wacom_##name##_luminance_store)646646+647647+DEVICE_LUMINANCE_ATTR(status0, llv);648648+DEVICE_LUMINANCE_ATTR(status1, hlv);649649+DEVICE_LUMINANCE_ATTR(buttons, img_lum);650650+651651+static ssize_t wacom_button_image_store(struct device *dev, int button_id,652652+ const char *buf, size_t count)653653+{654654+ struct hid_device *hdev = container_of(dev, struct hid_device, dev);655655+ struct wacom *wacom = hid_get_drvdata(hdev);656656+ int err;657657+ unsigned len;658658+ u8 xfer_id;659659+660660+ if (hdev->bus == BUS_BLUETOOTH) {661661+ len = 256;662662+ xfer_id = WAC_CMD_ICON_BT_XFER;663663+ } else {664664+ len = 1024;665665+ xfer_id = WAC_CMD_ICON_XFER;666666+ }667667+668668+ if (count != len)669669+ return -EINVAL;670670+671671+ mutex_lock(&wacom->lock);672672+673673+ err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);674674+675675+ mutex_unlock(&wacom->lock);676676+677677+ return err < 0 ? err : count;678678+}679679+680680+#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \681681+static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \682682+ struct device_attribute *attr, const char *buf, size_t count) \683683+{ \684684+ return wacom_button_image_store(dev, BUTTON_ID, buf, count); \685685+} \686686+static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \687687+ NULL, wacom_btnimg##BUTTON_ID##_store)688688+689689+DEVICE_BTNIMG_ATTR(0);690690+DEVICE_BTNIMG_ATTR(1);691691+DEVICE_BTNIMG_ATTR(2);692692+DEVICE_BTNIMG_ATTR(3);693693+DEVICE_BTNIMG_ATTR(4);694694+DEVICE_BTNIMG_ATTR(5);695695+DEVICE_BTNIMG_ATTR(6);696696+DEVICE_BTNIMG_ATTR(7);697697+698698+static struct attribute *cintiq_led_attrs[] = {699699+ &dev_attr_status_led0_select.attr,700700+ &dev_attr_status_led1_select.attr,701701+ NULL702702+};703703+704704+static struct attribute_group cintiq_led_attr_group = {705705+ .name = "wacom_led",706706+ .attrs = cintiq_led_attrs,707707+};708708+709709+static struct attribute *intuos4_led_attrs[] = {710710+ &dev_attr_status0_luminance.attr,711711+ &dev_attr_status1_luminance.attr,712712+ &dev_attr_status_led0_select.attr,713713+ &dev_attr_buttons_luminance.attr,714714+ &dev_attr_button0_rawimg.attr,715715+ &dev_attr_button1_rawimg.attr,716716+ &dev_attr_button2_rawimg.attr,717717+ &dev_attr_button3_rawimg.attr,718718+ &dev_attr_button4_rawimg.attr,719719+ &dev_attr_button5_rawimg.attr,720720+ &dev_attr_button6_rawimg.attr,721721+ &dev_attr_button7_rawimg.attr,722722+ NULL723723+};724724+725725+static struct attribute_group intuos4_led_attr_group = {726726+ .name = "wacom_led",727727+ .attrs = intuos4_led_attrs,728728+};729729+730730+static struct attribute *intuos5_led_attrs[] = {731731+ &dev_attr_status0_luminance.attr,732732+ &dev_attr_status_led0_select.attr,733733+ NULL734734+};735735+736736+static struct attribute_group intuos5_led_attr_group = {737737+ .name = "wacom_led",738738+ .attrs = intuos5_led_attrs,739739+};740740+741741+static int wacom_initialize_leds(struct wacom *wacom)742742+{743743+ int error;744744+745745+ /* Initialize default values */746746+ switch (wacom->wacom_wac.features.type) {747747+ case INTUOS4S:748748+ case INTUOS4:749749+ case INTUOS4WL:750750+ case INTUOS4L:751751+ wacom->led.select[0] = 0;752752+ wacom->led.select[1] = 0;753753+ wacom->led.llv = 10;754754+ wacom->led.hlv = 20;755755+ wacom->led.img_lum = 10;756756+ error = sysfs_create_group(&wacom->hdev->dev.kobj,757757+ &intuos4_led_attr_group);758758+ break;759759+760760+ case WACOM_24HD:761761+ case WACOM_21UX2:762762+ wacom->led.select[0] = 0;763763+ wacom->led.select[1] = 0;764764+ wacom->led.llv = 0;765765+ wacom->led.hlv = 0;766766+ wacom->led.img_lum = 0;767767+768768+ error = sysfs_create_group(&wacom->hdev->dev.kobj,769769+ &cintiq_led_attr_group);770770+ break;771771+772772+ case INTUOS5S:773773+ case INTUOS5:774774+ case INTUOS5L:775775+ case INTUOSPS:776776+ case INTUOSPM:777777+ case INTUOSPL:778778+ if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {779779+ wacom->led.select[0] = 0;780780+ wacom->led.select[1] = 0;781781+ wacom->led.llv = 32;782782+ wacom->led.hlv = 0;783783+ wacom->led.img_lum = 0;784784+785785+ error = sysfs_create_group(&wacom->hdev->dev.kobj,786786+ &intuos5_led_attr_group);787787+ } else788788+ return 0;789789+ break;790790+791791+ default:792792+ return 0;793793+ }794794+795795+ if (error) {796796+ hid_err(wacom->hdev,797797+ "cannot create sysfs group err: %d\n", error);798798+ return error;799799+ }800800+ wacom_led_control(wacom);801801+ wacom->led_initialized = true;802802+803803+ return 0;804804+}805805+806806+static void wacom_destroy_leds(struct wacom *wacom)807807+{808808+ if (!wacom->led_initialized)809809+ return;810810+811811+ wacom->led_initialized = false;812812+813813+ switch (wacom->wacom_wac.features.type) {814814+ case INTUOS4S:815815+ case INTUOS4:816816+ case INTUOS4WL:817817+ case INTUOS4L:818818+ sysfs_remove_group(&wacom->hdev->dev.kobj,819819+ &intuos4_led_attr_group);820820+ break;821821+822822+ case WACOM_24HD:823823+ case WACOM_21UX2:824824+ sysfs_remove_group(&wacom->hdev->dev.kobj,825825+ &cintiq_led_attr_group);826826+ break;827827+828828+ case INTUOS5S:829829+ case INTUOS5:830830+ case INTUOS5L:831831+ case INTUOSPS:832832+ case INTUOSPM:833833+ case INTUOSPL:834834+ if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)835835+ sysfs_remove_group(&wacom->hdev->dev.kobj,836836+ &intuos5_led_attr_group);837837+ break;838838+ }839839+}840840+841841+static enum power_supply_property wacom_battery_props[] = {842842+ POWER_SUPPLY_PROP_STATUS,843843+ POWER_SUPPLY_PROP_SCOPE,844844+ POWER_SUPPLY_PROP_CAPACITY845845+};846846+847847+static enum power_supply_property wacom_ac_props[] = {848848+ POWER_SUPPLY_PROP_PRESENT,849849+ POWER_SUPPLY_PROP_ONLINE,850850+ POWER_SUPPLY_PROP_SCOPE,851851+};852852+853853+static int wacom_battery_get_property(struct power_supply *psy,854854+ enum power_supply_property psp,855855+ union power_supply_propval *val)856856+{857857+ struct wacom *wacom = container_of(psy, struct wacom, battery);858858+ int ret = 0;859859+860860+ switch (psp) {861861+ case POWER_SUPPLY_PROP_SCOPE:862862+ val->intval = POWER_SUPPLY_SCOPE_DEVICE;863863+ break;864864+ case POWER_SUPPLY_PROP_CAPACITY:865865+ val->intval =866866+ wacom->wacom_wac.battery_capacity;867867+ break;868868+ case POWER_SUPPLY_PROP_STATUS:869869+ if (wacom->wacom_wac.bat_charging)870870+ val->intval = POWER_SUPPLY_STATUS_CHARGING;871871+ else if (wacom->wacom_wac.battery_capacity == 100 &&872872+ wacom->wacom_wac.ps_connected)873873+ val->intval = POWER_SUPPLY_STATUS_FULL;874874+ else875875+ val->intval = POWER_SUPPLY_STATUS_DISCHARGING;876876+ break;877877+ default:878878+ ret = -EINVAL;879879+ break;880880+ }881881+882882+ return ret;883883+}884884+885885+static int wacom_ac_get_property(struct power_supply *psy,886886+ enum power_supply_property psp,887887+ union power_supply_propval *val)888888+{889889+ struct wacom *wacom = container_of(psy, struct wacom, ac);890890+ int ret = 0;891891+892892+ switch (psp) {893893+ case POWER_SUPPLY_PROP_PRESENT:894894+ /* fall through */895895+ case POWER_SUPPLY_PROP_ONLINE:896896+ val->intval = wacom->wacom_wac.ps_connected;897897+ break;898898+ case POWER_SUPPLY_PROP_SCOPE:899899+ val->intval = POWER_SUPPLY_SCOPE_DEVICE;900900+ break;901901+ default:902902+ ret = -EINVAL;903903+ break;904904+ }905905+ return ret;906906+}907907+908908+static int wacom_initialize_battery(struct wacom *wacom)909909+{910910+ static atomic_t battery_no = ATOMIC_INIT(0);911911+ int error;912912+ unsigned long n;913913+914914+ if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {915915+ n = atomic_inc_return(&battery_no) - 1;916916+917917+ wacom->battery.properties = wacom_battery_props;918918+ wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);919919+ wacom->battery.get_property = wacom_battery_get_property;920920+ sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);921921+ wacom->battery.name = wacom->wacom_wac.bat_name;922922+ wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;923923+ wacom->battery.use_for_apm = 0;924924+925925+ wacom->ac.properties = wacom_ac_props;926926+ wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);927927+ wacom->ac.get_property = wacom_ac_get_property;928928+ sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);929929+ wacom->ac.name = wacom->wacom_wac.ac_name;930930+ wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;931931+ wacom->ac.use_for_apm = 0;932932+933933+ error = power_supply_register(&wacom->hdev->dev,934934+ &wacom->battery);935935+ if (error)936936+ return error;937937+938938+ power_supply_powers(&wacom->battery, &wacom->hdev->dev);939939+940940+ error = power_supply_register(&wacom->hdev->dev, &wacom->ac);941941+ if (error) {942942+ power_supply_unregister(&wacom->battery);943943+ return error;944944+ }945945+946946+ power_supply_powers(&wacom->ac, &wacom->hdev->dev);947947+ }948948+949949+ return 0;950950+}951951+952952+static void wacom_destroy_battery(struct wacom *wacom)953953+{954954+ if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&955955+ wacom->battery.dev) {956956+ power_supply_unregister(&wacom->battery);957957+ wacom->battery.dev = NULL;958958+ power_supply_unregister(&wacom->ac);959959+ wacom->ac.dev = NULL;960960+ }961961+}962962+963963+static ssize_t wacom_show_speed(struct device *dev,964964+ struct device_attribute965965+ *attr, char *buf)966966+{967967+ struct hid_device *hdev = container_of(dev, struct hid_device, dev);968968+ struct wacom *wacom = hid_get_drvdata(hdev);969969+970970+ return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);971971+}972972+973973+static ssize_t wacom_store_speed(struct device *dev,974974+ struct device_attribute *attr,975975+ const char *buf, size_t count)976976+{977977+ struct hid_device *hdev = container_of(dev, struct hid_device, dev);978978+ struct wacom *wacom = hid_get_drvdata(hdev);979979+ u8 new_speed;980980+981981+ if (kstrtou8(buf, 0, &new_speed))982982+ return -EINVAL;983983+984984+ if (new_speed != 0 && new_speed != 1)985985+ return -EINVAL;986986+987987+ wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);988988+989989+ return count;990990+}991991+992992+static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,993993+ wacom_show_speed, wacom_store_speed);994994+995995+static struct input_dev *wacom_allocate_input(struct wacom *wacom)996996+{997997+ struct input_dev *input_dev;998998+ struct hid_device *hdev = wacom->hdev;999999+ struct wacom_wac *wacom_wac = &(wacom->wacom_wac);10001000+10011001+ input_dev = input_allocate_device();10021002+ if (!input_dev)10031003+ return NULL;10041004+10051005+ input_dev->name = wacom_wac->name;10061006+ input_dev->phys = hdev->phys;10071007+ input_dev->dev.parent = &hdev->dev;10081008+ input_dev->open = wacom_open;10091009+ input_dev->close = wacom_close;10101010+ input_dev->uniq = hdev->uniq;10111011+ input_dev->id.bustype = hdev->bus;10121012+ input_dev->id.vendor = hdev->vendor;10131013+ input_dev->id.product = hdev->product;10141014+ input_dev->id.version = hdev->version;10151015+ input_set_drvdata(input_dev, wacom);10161016+10171017+ return input_dev;10181018+}10191019+10201020+static void wacom_unregister_inputs(struct wacom *wacom)10211021+{10221022+ if (wacom->wacom_wac.input)10231023+ input_unregister_device(wacom->wacom_wac.input);10241024+ if (wacom->wacom_wac.pad_input)10251025+ input_unregister_device(wacom->wacom_wac.pad_input);10261026+ wacom->wacom_wac.input = NULL;10271027+ wacom->wacom_wac.pad_input = NULL;10281028+}10291029+10301030+static int wacom_register_inputs(struct wacom *wacom)10311031+{10321032+ struct input_dev *input_dev, *pad_input_dev;10331033+ struct wacom_wac *wacom_wac = &(wacom->wacom_wac);10341034+ int error;10351035+10361036+ input_dev = wacom_allocate_input(wacom);10371037+ pad_input_dev = wacom_allocate_input(wacom);10381038+ if (!input_dev || !pad_input_dev) {10391039+ error = -ENOMEM;10401040+ goto fail1;10411041+ }10421042+10431043+ wacom_wac->input = input_dev;10441044+ wacom_wac->pad_input = pad_input_dev;10451045+ wacom_wac->pad_input->name = wacom_wac->pad_name;10461046+10471047+ error = wacom_setup_input_capabilities(input_dev, wacom_wac);10481048+ if (error)10491049+ goto fail2;10501050+10511051+ error = input_register_device(input_dev);10521052+ if (error)10531053+ goto fail2;10541054+10551055+ error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);10561056+ if (error) {10571057+ /* no pad in use on this interface */10581058+ input_free_device(pad_input_dev);10591059+ wacom_wac->pad_input = NULL;10601060+ pad_input_dev = NULL;10611061+ } else {10621062+ error = input_register_device(pad_input_dev);10631063+ if (error)10641064+ goto fail3;10651065+ }10661066+10671067+ return 0;10681068+10691069+fail3:10701070+ input_unregister_device(input_dev);10711071+ input_dev = NULL;10721072+fail2:10731073+ wacom_wac->input = NULL;10741074+ wacom_wac->pad_input = NULL;10751075+fail1:10761076+ if (input_dev)10771077+ input_free_device(input_dev);10781078+ if (pad_input_dev)10791079+ input_free_device(pad_input_dev);10801080+ return error;10811081+}10821082+10831083+static void wacom_wireless_work(struct work_struct *work)10841084+{10851085+ struct wacom *wacom = container_of(work, struct wacom, work);10861086+ struct usb_device *usbdev = wacom->usbdev;10871087+ struct wacom_wac *wacom_wac = &wacom->wacom_wac;10881088+ struct hid_device *hdev1, *hdev2;10891089+ struct wacom *wacom1, *wacom2;10901090+ struct wacom_wac *wacom_wac1, *wacom_wac2;10911091+ int error;10921092+10931093+ /*10941094+ * Regardless if this is a disconnect or a new tablet,10951095+ * remove any existing input and battery devices.10961096+ */10971097+10981098+ wacom_destroy_battery(wacom);10991099+11001100+ /* Stylus interface */11011101+ hdev1 = usb_get_intfdata(usbdev->config->interface[1]);11021102+ wacom1 = hid_get_drvdata(hdev1);11031103+ wacom_wac1 = &(wacom1->wacom_wac);11041104+ wacom_unregister_inputs(wacom1);11051105+11061106+ /* Touch interface */11071107+ hdev2 = usb_get_intfdata(usbdev->config->interface[2]);11081108+ wacom2 = hid_get_drvdata(hdev2);11091109+ wacom_wac2 = &(wacom2->wacom_wac);11101110+ wacom_unregister_inputs(wacom2);11111111+11121112+ if (wacom_wac->pid == 0) {11131113+ hid_info(wacom->hdev, "wireless tablet disconnected\n");11141114+ wacom_wac1->shared->type = 0;11151115+ } else {11161116+ const struct hid_device_id *id = wacom_ids;11171117+11181118+ hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",11191119+ wacom_wac->pid);11201120+11211121+ while (id->bus) {11221122+ if (id->vendor == USB_VENDOR_ID_WACOM &&11231123+ id->product == wacom_wac->pid)11241124+ break;11251125+ id++;11261126+ }11271127+11281128+ if (!id->bus) {11291129+ hid_info(wacom->hdev, "ignoring unknown PID.\n");11301130+ return;11311131+ }11321132+11331133+ /* Stylus interface */11341134+ wacom_wac1->features =11351135+ *((struct wacom_features *)id->driver_data);11361136+ wacom_wac1->features.device_type = BTN_TOOL_PEN;11371137+ snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",11381138+ wacom_wac1->features.name);11391139+ snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",11401140+ wacom_wac1->features.name);11411141+ wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;11421142+ wacom_wac1->shared->type = wacom_wac1->features.type;11431143+ error = wacom_register_inputs(wacom1);11441144+ if (error)11451145+ goto fail;11461146+11471147+ /* Touch interface */11481148+ if (wacom_wac1->features.touch_max ||11491149+ wacom_wac1->features.type == INTUOSHT) {11501150+ wacom_wac2->features =11511151+ *((struct wacom_features *)id->driver_data);11521152+ wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;11531153+ wacom_wac2->features.device_type = BTN_TOOL_FINGER;11541154+ wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;11551155+ if (wacom_wac2->features.touch_max)11561156+ snprintf(wacom_wac2->name, WACOM_NAME_MAX,11571157+ "%s (WL) Finger",wacom_wac2->features.name);11581158+ else11591159+ snprintf(wacom_wac2->name, WACOM_NAME_MAX,11601160+ "%s (WL) Pad",wacom_wac2->features.name);11611161+ snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,11621162+ "%s (WL) Pad", wacom_wac2->features.name);11631163+ error = wacom_register_inputs(wacom2);11641164+ if (error)11651165+ goto fail;11661166+11671167+ if (wacom_wac1->features.type == INTUOSHT &&11681168+ wacom_wac1->features.touch_max)11691169+ wacom_wac->shared->touch_input = wacom_wac2->input;11701170+ }11711171+11721172+ error = wacom_initialize_battery(wacom);11731173+ if (error)11741174+ goto fail;11751175+ }11761176+11771177+ return;11781178+11791179+fail:11801180+ wacom_unregister_inputs(wacom1);11811181+ wacom_unregister_inputs(wacom2);11821182+ return;11831183+}11841184+11851185+/*11861186+ * Not all devices report physical dimensions from HID.11871187+ * Compute the default from hardcoded logical dimension11881188+ * and resolution before driver overwrites them.11891189+ */11901190+static void wacom_set_default_phy(struct wacom_features *features)11911191+{11921192+ if (features->x_resolution) {11931193+ features->x_phy = (features->x_max * 100) /11941194+ features->x_resolution;11951195+ features->y_phy = (features->y_max * 100) /11961196+ features->y_resolution;11971197+ }11981198+}11991199+12001200+static void wacom_calculate_res(struct wacom_features *features)12011201+{12021202+ features->x_resolution = wacom_calc_hid_res(features->x_max,12031203+ features->x_phy,12041204+ features->unit,12051205+ features->unitExpo);12061206+ features->y_resolution = wacom_calc_hid_res(features->y_max,12071207+ features->y_phy,12081208+ features->unit,12091209+ features->unitExpo);12101210+}12111211+12121212+static int wacom_hid_report_len(struct hid_report *report)12131213+{12141214+ /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */12151215+ return ((report->size - 1) >> 3) + 1 + (report->id > 0);12161216+}12171217+12181218+static size_t wacom_compute_pktlen(struct hid_device *hdev)12191219+{12201220+ struct hid_report_enum *report_enum;12211221+ struct hid_report *report;12221222+ size_t size = 0;12231223+12241224+ report_enum = hdev->report_enum + HID_INPUT_REPORT;12251225+12261226+ list_for_each_entry(report, &report_enum->report_list, list) {12271227+ size_t report_size = wacom_hid_report_len(report);12281228+ if (report_size > size)12291229+ size = report_size;12301230+ }12311231+12321232+ return size;12331233+}12341234+12351235+static int wacom_probe(struct hid_device *hdev,12361236+ const struct hid_device_id *id)12371237+{12381238+ struct usb_interface *intf = to_usb_interface(hdev->dev.parent);12391239+ struct usb_device *dev = interface_to_usbdev(intf);12401240+ struct wacom *wacom;12411241+ struct wacom_wac *wacom_wac;12421242+ struct wacom_features *features;12431243+ int error;12441244+12451245+ if (!id->driver_data)12461246+ return -EINVAL;12471247+12481248+ wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);12491249+ if (!wacom)12501250+ return -ENOMEM;12511251+12521252+ hid_set_drvdata(hdev, wacom);12531253+ wacom->hdev = hdev;12541254+12551255+ /* ask for the report descriptor to be loaded by HID */12561256+ error = hid_parse(hdev);12571257+ if (error) {12581258+ hid_err(hdev, "parse failed\n");12591259+ goto fail1;12601260+ }12611261+12621262+ wacom_wac = &wacom->wacom_wac;12631263+ wacom_wac->features = *((struct wacom_features *)id->driver_data);12641264+ features = &wacom_wac->features;12651265+ features->pktlen = wacom_compute_pktlen(hdev);12661266+ if (features->pktlen > WACOM_PKGLEN_MAX) {12671267+ error = -EINVAL;12681268+ goto fail1;12691269+ }12701270+12711271+ if (features->check_for_hid_type && features->hid_type != hdev->type) {12721272+ error = -ENODEV;12731273+ goto fail1;12741274+ }12751275+12761276+ wacom->usbdev = dev;12771277+ wacom->intf = intf;12781278+ mutex_init(&wacom->lock);12791279+ INIT_WORK(&wacom->work, wacom_wireless_work);12801280+12811281+ /* set the default size in case we do not get them from hid */12821282+ wacom_set_default_phy(features);12831283+12841284+ /* Retrieve the physical and logical size for touch devices */12851285+ wacom_retrieve_hid_descriptor(hdev, features);12861286+12871287+ /*12881288+ * Intuos5 has no useful data about its touch interface in its12891289+ * HID descriptor. If this is the touch interface (PacketSize12901290+ * of WACOM_PKGLEN_BBTOUCH3), override the table values.12911291+ */12921292+ if (features->type >= INTUOS5S && features->type <= INTUOSHT) {12931293+ if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {12941294+ features->device_type = BTN_TOOL_FINGER;12951295+12961296+ features->x_max = 4096;12971297+ features->y_max = 4096;12981298+ } else {12991299+ features->device_type = BTN_TOOL_PEN;13001300+ }13011301+ }13021302+13031303+ /*13041304+ * Same thing for Bamboo 3rd gen.13051305+ */13061306+ if ((features->type == BAMBOO_PT) &&13071307+ (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&13081308+ (features->device_type == BTN_TOOL_PEN)) {13091309+ features->device_type = BTN_TOOL_FINGER;13101310+13111311+ features->x_max = 4096;13121312+ features->y_max = 4096;13131313+ }13141314+13151315+ if (hdev->bus == BUS_BLUETOOTH)13161316+ features->quirks |= WACOM_QUIRK_BATTERY;13171317+13181318+ wacom_setup_device_quirks(features);13191319+13201320+ /* set unit to "100th of a mm" for devices not reported by HID */13211321+ if (!features->unit) {13221322+ features->unit = 0x11;13231323+ features->unitExpo = -3;13241324+ }13251325+ wacom_calculate_res(features);13261326+13271327+ strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));13281328+ snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),13291329+ "%s Pad", features->name);13301330+13311331+ if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {13321332+ /* Append the device type to the name */13331333+ if (features->device_type != BTN_TOOL_FINGER)13341334+ strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);13351335+ else if (features->touch_max)13361336+ strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);13371337+ else13381338+ strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);13391339+13401340+ error = wacom_add_shared_data(hdev);13411341+ if (error)13421342+ goto fail1;13431343+ }13441344+13451345+ error = wacom_initialize_leds(wacom);13461346+ if (error)13471347+ goto fail2;13481348+13491349+ if (!(features->quirks & WACOM_QUIRK_MONITOR) &&13501350+ (features->quirks & WACOM_QUIRK_BATTERY)) {13511351+ error = wacom_initialize_battery(wacom);13521352+ if (error)13531353+ goto fail3;13541354+ }13551355+13561356+ if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {13571357+ error = wacom_register_inputs(wacom);13581358+ if (error)13591359+ goto fail4;13601360+ }13611361+13621362+ if (hdev->bus == BUS_BLUETOOTH) {13631363+ error = device_create_file(&hdev->dev, &dev_attr_speed);13641364+ if (error)13651365+ hid_warn(hdev,13661366+ "can't create sysfs speed attribute err: %d\n",13671367+ error);13681368+ }13691369+13701370+ /* Note that if query fails it is not a hard failure */13711371+ wacom_query_tablet_data(hdev, features);13721372+13731373+ /* Regular HID work starts now */13741374+ error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);13751375+ if (error) {13761376+ hid_err(hdev, "hw start failed\n");13771377+ goto fail5;13781378+ }13791379+13801380+ if (features->quirks & WACOM_QUIRK_MONITOR)13811381+ error = hid_hw_open(hdev);13821382+13831383+ if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {13841384+ if (wacom_wac->features.device_type == BTN_TOOL_FINGER)13851385+ wacom_wac->shared->touch_input = wacom_wac->input;13861386+ }13871387+13881388+ return 0;13891389+13901390+ fail5: if (hdev->bus == BUS_BLUETOOTH)13911391+ device_remove_file(&hdev->dev, &dev_attr_speed);13921392+ wacom_unregister_inputs(wacom);13931393+ fail4: wacom_destroy_battery(wacom);13941394+ fail3: wacom_destroy_leds(wacom);13951395+ fail2: wacom_remove_shared_data(wacom_wac);13961396+ fail1: kfree(wacom);13971397+ hid_set_drvdata(hdev, NULL);13981398+ return error;13991399+}14001400+14011401+static void wacom_remove(struct hid_device *hdev)14021402+{14031403+ struct wacom *wacom = hid_get_drvdata(hdev);14041404+14051405+ hid_hw_stop(hdev);14061406+14071407+ cancel_work_sync(&wacom->work);14081408+ wacom_unregister_inputs(wacom);14091409+ if (hdev->bus == BUS_BLUETOOTH)14101410+ device_remove_file(&hdev->dev, &dev_attr_speed);14111411+ wacom_destroy_battery(wacom);14121412+ wacom_destroy_leds(wacom);14131413+ wacom_remove_shared_data(&wacom->wacom_wac);14141414+14151415+ hid_set_drvdata(hdev, NULL);14161416+ kfree(wacom);14171417+}14181418+14191419+static int wacom_resume(struct hid_device *hdev)14201420+{14211421+ struct wacom *wacom = hid_get_drvdata(hdev);14221422+ struct wacom_features *features = &wacom->wacom_wac.features;14231423+14241424+ mutex_lock(&wacom->lock);14251425+14261426+ /* switch to wacom mode first */14271427+ wacom_query_tablet_data(hdev, features);14281428+ wacom_led_control(wacom);14291429+14301430+ mutex_unlock(&wacom->lock);14311431+14321432+ return 0;14331433+}14341434+14351435+static int wacom_reset_resume(struct hid_device *hdev)14361436+{14371437+ return wacom_resume(hdev);14381438+}14391439+14401440+static struct hid_driver wacom_driver = {14411441+ .name = "wacom",14421442+ .id_table = wacom_ids,14431443+ .probe = wacom_probe,14441444+ .remove = wacom_remove,14451445+#ifdef CONFIG_PM14461446+ .resume = wacom_resume,14471447+ .reset_resume = wacom_reset_resume,14481448+#endif14491449+ .raw_event = wacom_raw_event,14501450+};14511451+module_hid_driver(wacom_driver);14521452+14531453+MODULE_VERSION(DRIVER_VERSION);14541454+MODULE_AUTHOR(DRIVER_AUTHOR);14551455+MODULE_DESCRIPTION(DRIVER_DESC);14561456+MODULE_LICENSE(DRIVER_LICENSE);
-16
drivers/input/tablet/Kconfig
···7373 To compile this driver as a module, choose M here: the7474 module will be called kbtab.75757676-config TABLET_USB_WACOM7777- tristate "Wacom Intuos/Graphire tablet support (USB)"7878- depends on USB_ARCH_HAS_HCD7979- select POWER_SUPPLY8080- select USB8181- select NEW_LEDS8282- select LEDS_CLASS8383- help8484- Say Y here if you want to use the USB version of the Wacom Intuos8585- or Graphire tablet. Make sure to say Y to "Mouse support"8686- (CONFIG_INPUT_MOUSEDEV) and/or "Event interface support"8787- (CONFIG_INPUT_EVDEV) as well.8888-8989- To compile this driver as a module, choose M here: the9090- module will be called wacom.9191-9276config TABLET_SERIAL_WACOM49377 tristate "Wacom protocol 4 serial tablet support"9478 select SERIO
···46464747/* wacom data packet report IDs */4848#define WACOM_REPORT_PENABLED 24949+#define WACOM_REPORT_PENABLED_BT 34950#define WACOM_REPORT_INTUOSREAD 55051#define WACOM_REPORT_INTUOSWRITE 65152#define WACOM_REPORT_INTUOSPAD 12···6968#define WACOM_QUIRK_BBTOUCH_LOWRES 0x00027069#define WACOM_QUIRK_NO_INPUT 0x00047170#define WACOM_QUIRK_MONITOR 0x00087171+#define WACOM_QUIRK_BATTERY 0x001072727373enum {7474 PENPARTNER = 0,7575 GRAPHIRE,7676+ GRAPHIRE_BT,7677 WACOM_G4,7778 PTU,7879 PL,···8683 INTUOS3L,8784 INTUOS4S,8885 INTUOS4,8686+ INTUOS4WL,8987 INTUOS4L,9088 INTUOS5S,9189 INTUOS5,···118114119115struct wacom_features {120116 const char *name;121121- int pktlen;122117 int x_max;123118 int y_max;124119 int pressure_max;···130127 int device_type;131128 int x_phy;132129 int y_phy;133133- unsigned char unit;134134- unsigned char unitExpo;130130+ unsigned unit;131131+ int unitExpo;135132 int x_fuzz;136133 int y_fuzz;137134 int pressure_fuzz;···140137 unsigned touch_max;141138 int oVid;142139 int oPid;140140+ int pktlen;141141+ bool check_for_hid_type;142142+ int hid_type;143143};144144145145struct wacom_shared {···156150157151struct wacom_wac {158152 char name[WACOM_NAME_MAX];159159- unsigned char *data;153153+ char pad_name[WACOM_NAME_MAX];154154+ char bat_name[WACOM_NAME_MAX];155155+ char ac_name[WACOM_NAME_MAX];156156+ unsigned char data[WACOM_PKGLEN_MAX];160157 int tool[2];161158 int id[2];162159 __u32 serial[2];163160 struct wacom_features features;164161 struct wacom_shared *shared;165162 struct input_dev *input;163163+ struct input_dev *pad_input;166164 int pid;167165 int battery_capacity;168166 int num_contacts_left;167167+ int bat_charging;168168+ int ps_connected;169169+ u8 bt_features;170170+ u8 bt_high_speed;169171};170172171173#endif
+5
include/linux/hid.h
···311311#define HID_GROUP_RMI 0x0100312312313313/*314314+ * Vendor specific HID device groups315315+ */316316+#define HID_GROUP_WACOM 0x0101317317+318318+/*314319 * This is the global environment of the parser. This information is315320 * persistent for main-items. The global environment can be saved and316321 * restored with PUSH/POP statements.