0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/types.h>
0012 #include <linux/kernel.h>
0013 #include <linux/slab.h>
0014 #include <linux/module.h>
0015 #include <linux/usb/input.h>
0016
0017 MODULE_AUTHOR("Xing Wei <weixing@hanwang.com.cn>");
0018 MODULE_DESCRIPTION("USB Hanwang tablet driver");
0019 MODULE_LICENSE("GPL");
0020
0021 #define USB_VENDOR_ID_HANWANG 0x0b57
0022 #define HANWANG_TABLET_INT_CLASS 0x0003
0023 #define HANWANG_TABLET_INT_SUB_CLASS 0x0001
0024 #define HANWANG_TABLET_INT_PROTOCOL 0x0002
0025
0026 #define ART_MASTER_PKGLEN_MAX 10
0027
0028
0029 #define STYLUS_DEVICE_ID 0x02
0030 #define TOUCH_DEVICE_ID 0x03
0031 #define CURSOR_DEVICE_ID 0x06
0032 #define ERASER_DEVICE_ID 0x0A
0033 #define PAD_DEVICE_ID 0x0F
0034
0035
0036 #define HANWANG_TABLET_DEVICE(vend, cl, sc, pr) \
0037 .match_flags = USB_DEVICE_ID_MATCH_VENDOR \
0038 | USB_DEVICE_ID_MATCH_INT_INFO, \
0039 .idVendor = (vend), \
0040 .bInterfaceClass = (cl), \
0041 .bInterfaceSubClass = (sc), \
0042 .bInterfaceProtocol = (pr)
0043
0044 enum hanwang_tablet_type {
0045 HANWANG_ART_MASTER_III,
0046 HANWANG_ART_MASTER_HD,
0047 HANWANG_ART_MASTER_II,
0048 };
0049
0050 struct hanwang {
0051 unsigned char *data;
0052 dma_addr_t data_dma;
0053 struct input_dev *dev;
0054 struct usb_device *usbdev;
0055 struct urb *irq;
0056 const struct hanwang_features *features;
0057 unsigned int current_tool;
0058 unsigned int current_id;
0059 char name[64];
0060 char phys[32];
0061 };
0062
0063 struct hanwang_features {
0064 unsigned short pid;
0065 char *name;
0066 enum hanwang_tablet_type type;
0067 int pkg_len;
0068 int max_x;
0069 int max_y;
0070 int max_tilt_x;
0071 int max_tilt_y;
0072 int max_pressure;
0073 };
0074
0075 static const struct hanwang_features features_array[] = {
0076 { 0x8528, "Hanwang Art Master III 0906", HANWANG_ART_MASTER_III,
0077 ART_MASTER_PKGLEN_MAX, 0x5757, 0x3692, 0x3f, 0x7f, 2048 },
0078 { 0x8529, "Hanwang Art Master III 0604", HANWANG_ART_MASTER_III,
0079 ART_MASTER_PKGLEN_MAX, 0x3d84, 0x2672, 0x3f, 0x7f, 2048 },
0080 { 0x852a, "Hanwang Art Master III 1308", HANWANG_ART_MASTER_III,
0081 ART_MASTER_PKGLEN_MAX, 0x7f00, 0x4f60, 0x3f, 0x7f, 2048 },
0082 { 0x8401, "Hanwang Art Master HD 5012", HANWANG_ART_MASTER_HD,
0083 ART_MASTER_PKGLEN_MAX, 0x678e, 0x4150, 0x3f, 0x7f, 1024 },
0084 { 0x8503, "Hanwang Art Master II", HANWANG_ART_MASTER_II,
0085 ART_MASTER_PKGLEN_MAX, 0x27de, 0x1cfe, 0x3f, 0x7f, 1024 },
0086 };
0087
0088 static const int hw_eventtypes[] = {
0089 EV_KEY, EV_ABS, EV_MSC,
0090 };
0091
0092 static const int hw_absevents[] = {
0093 ABS_X, ABS_Y, ABS_TILT_X, ABS_TILT_Y, ABS_WHEEL,
0094 ABS_RX, ABS_RY, ABS_PRESSURE, ABS_MISC,
0095 };
0096
0097 static const int hw_btnevents[] = {
0098 BTN_STYLUS, BTN_STYLUS2, BTN_TOOL_PEN, BTN_TOOL_RUBBER,
0099 BTN_TOOL_MOUSE, BTN_TOOL_FINGER,
0100 BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8,
0101 };
0102
0103 static const int hw_mscevents[] = {
0104 MSC_SERIAL,
0105 };
0106
0107 static void hanwang_parse_packet(struct hanwang *hanwang)
0108 {
0109 unsigned char *data = hanwang->data;
0110 struct input_dev *input_dev = hanwang->dev;
0111 struct usb_device *dev = hanwang->usbdev;
0112 enum hanwang_tablet_type type = hanwang->features->type;
0113 int i;
0114 u16 p;
0115
0116 if (type == HANWANG_ART_MASTER_II) {
0117 hanwang->current_tool = BTN_TOOL_PEN;
0118 hanwang->current_id = STYLUS_DEVICE_ID;
0119 }
0120
0121 switch (data[0]) {
0122 case 0x02:
0123 switch (data[1]) {
0124 case 0x80:
0125 if (type != HANWANG_ART_MASTER_II) {
0126 hanwang->current_id = 0;
0127 input_report_key(input_dev,
0128 hanwang->current_tool, 0);
0129 }
0130 break;
0131
0132 case 0x00:
0133 if (type == HANWANG_ART_MASTER_II) {
0134 hanwang->current_id = 0;
0135 input_report_key(input_dev,
0136 hanwang->current_tool, 0);
0137 }
0138 break;
0139
0140 case 0xc2:
0141 switch (data[3] & 0xf0) {
0142 case 0x20:
0143 case 0x30:
0144 hanwang->current_id = STYLUS_DEVICE_ID;
0145 hanwang->current_tool = BTN_TOOL_PEN;
0146 input_report_key(input_dev, BTN_TOOL_PEN, 1);
0147 break;
0148 case 0xa0:
0149 case 0xb0:
0150 hanwang->current_id = ERASER_DEVICE_ID;
0151 hanwang->current_tool = BTN_TOOL_RUBBER;
0152 input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
0153 break;
0154 default:
0155 hanwang->current_id = 0;
0156 dev_dbg(&dev->dev,
0157 "unknown tablet tool %02x\n", data[0]);
0158 break;
0159 }
0160 break;
0161
0162 default:
0163 switch (type) {
0164 case HANWANG_ART_MASTER_III:
0165 p = (data[6] << 3) |
0166 ((data[7] & 0xc0) >> 5) |
0167 (data[1] & 0x01);
0168 break;
0169
0170 case HANWANG_ART_MASTER_HD:
0171 case HANWANG_ART_MASTER_II:
0172 p = (data[7] >> 6) | (data[6] << 2);
0173 break;
0174
0175 default:
0176 p = 0;
0177 break;
0178 }
0179
0180 input_report_abs(input_dev, ABS_X,
0181 be16_to_cpup((__be16 *)&data[2]));
0182 input_report_abs(input_dev, ABS_Y,
0183 be16_to_cpup((__be16 *)&data[4]));
0184 input_report_abs(input_dev, ABS_PRESSURE, p);
0185 input_report_abs(input_dev, ABS_TILT_X, data[7] & 0x3f);
0186 input_report_abs(input_dev, ABS_TILT_Y, data[8] & 0x7f);
0187 input_report_key(input_dev, BTN_STYLUS, data[1] & 0x02);
0188
0189 if (type != HANWANG_ART_MASTER_II)
0190 input_report_key(input_dev, BTN_STYLUS2,
0191 data[1] & 0x04);
0192 else
0193 input_report_key(input_dev, BTN_TOOL_PEN, 1);
0194
0195 break;
0196 }
0197
0198 input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
0199 input_event(input_dev, EV_MSC, MSC_SERIAL,
0200 hanwang->features->pid);
0201 break;
0202
0203 case 0x0c:
0204
0205 hanwang->current_id = PAD_DEVICE_ID;
0206
0207 switch (type) {
0208 case HANWANG_ART_MASTER_III:
0209 input_report_key(input_dev, BTN_TOOL_FINGER,
0210 data[1] || data[2] || data[3]);
0211 input_report_abs(input_dev, ABS_WHEEL, data[1]);
0212 input_report_key(input_dev, BTN_0, data[2]);
0213 for (i = 0; i < 8; i++)
0214 input_report_key(input_dev,
0215 BTN_1 + i, data[3] & (1 << i));
0216 break;
0217
0218 case HANWANG_ART_MASTER_HD:
0219 input_report_key(input_dev, BTN_TOOL_FINGER, data[1] ||
0220 data[2] || data[3] || data[4] ||
0221 data[5] || data[6]);
0222 input_report_abs(input_dev, ABS_RX,
0223 ((data[1] & 0x1f) << 8) | data[2]);
0224 input_report_abs(input_dev, ABS_RY,
0225 ((data[3] & 0x1f) << 8) | data[4]);
0226 input_report_key(input_dev, BTN_0, data[5] & 0x01);
0227 for (i = 0; i < 4; i++) {
0228 input_report_key(input_dev,
0229 BTN_1 + i, data[5] & (1 << i));
0230 input_report_key(input_dev,
0231 BTN_5 + i, data[6] & (1 << i));
0232 }
0233 break;
0234
0235 case HANWANG_ART_MASTER_II:
0236 dev_dbg(&dev->dev, "error packet %02x\n", data[0]);
0237 return;
0238 }
0239
0240 input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
0241 input_event(input_dev, EV_MSC, MSC_SERIAL, 0xffffffff);
0242 break;
0243
0244 default:
0245 dev_dbg(&dev->dev, "error packet %02x\n", data[0]);
0246 break;
0247 }
0248
0249 input_sync(input_dev);
0250 }
0251
0252 static void hanwang_irq(struct urb *urb)
0253 {
0254 struct hanwang *hanwang = urb->context;
0255 struct usb_device *dev = hanwang->usbdev;
0256 int retval;
0257
0258 switch (urb->status) {
0259 case 0:
0260 ;
0261 hanwang_parse_packet(hanwang);
0262 break;
0263 case -ECONNRESET:
0264 case -ENOENT:
0265 case -ESHUTDOWN:
0266
0267 dev_err(&dev->dev, "%s - urb shutting down with status: %d",
0268 __func__, urb->status);
0269 return;
0270 default:
0271 dev_err(&dev->dev, "%s - nonzero urb status received: %d",
0272 __func__, urb->status);
0273 break;
0274 }
0275
0276 retval = usb_submit_urb(urb, GFP_ATOMIC);
0277 if (retval)
0278 dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
0279 __func__, retval);
0280 }
0281
0282 static int hanwang_open(struct input_dev *dev)
0283 {
0284 struct hanwang *hanwang = input_get_drvdata(dev);
0285
0286 hanwang->irq->dev = hanwang->usbdev;
0287 if (usb_submit_urb(hanwang->irq, GFP_KERNEL))
0288 return -EIO;
0289
0290 return 0;
0291 }
0292
0293 static void hanwang_close(struct input_dev *dev)
0294 {
0295 struct hanwang *hanwang = input_get_drvdata(dev);
0296
0297 usb_kill_urb(hanwang->irq);
0298 }
0299
0300 static bool get_features(struct usb_device *dev, struct hanwang *hanwang)
0301 {
0302 int i;
0303
0304 for (i = 0; i < ARRAY_SIZE(features_array); i++) {
0305 if (le16_to_cpu(dev->descriptor.idProduct) ==
0306 features_array[i].pid) {
0307 hanwang->features = &features_array[i];
0308 return true;
0309 }
0310 }
0311
0312 return false;
0313 }
0314
0315
0316 static int hanwang_probe(struct usb_interface *intf, const struct usb_device_id *id)
0317 {
0318 struct usb_device *dev = interface_to_usbdev(intf);
0319 struct usb_endpoint_descriptor *endpoint;
0320 struct hanwang *hanwang;
0321 struct input_dev *input_dev;
0322 int error;
0323 int i;
0324
0325 if (intf->cur_altsetting->desc.bNumEndpoints < 1)
0326 return -ENODEV;
0327
0328 hanwang = kzalloc(sizeof(struct hanwang), GFP_KERNEL);
0329 input_dev = input_allocate_device();
0330 if (!hanwang || !input_dev) {
0331 error = -ENOMEM;
0332 goto fail1;
0333 }
0334
0335 if (!get_features(dev, hanwang)) {
0336 error = -ENXIO;
0337 goto fail1;
0338 }
0339
0340 hanwang->data = usb_alloc_coherent(dev, hanwang->features->pkg_len,
0341 GFP_KERNEL, &hanwang->data_dma);
0342 if (!hanwang->data) {
0343 error = -ENOMEM;
0344 goto fail1;
0345 }
0346
0347 hanwang->irq = usb_alloc_urb(0, GFP_KERNEL);
0348 if (!hanwang->irq) {
0349 error = -ENOMEM;
0350 goto fail2;
0351 }
0352
0353 hanwang->usbdev = dev;
0354 hanwang->dev = input_dev;
0355
0356 usb_make_path(dev, hanwang->phys, sizeof(hanwang->phys));
0357 strlcat(hanwang->phys, "/input0", sizeof(hanwang->phys));
0358
0359 strlcpy(hanwang->name, hanwang->features->name, sizeof(hanwang->name));
0360 input_dev->name = hanwang->name;
0361 input_dev->phys = hanwang->phys;
0362 usb_to_input_id(dev, &input_dev->id);
0363 input_dev->dev.parent = &intf->dev;
0364
0365 input_set_drvdata(input_dev, hanwang);
0366
0367 input_dev->open = hanwang_open;
0368 input_dev->close = hanwang_close;
0369
0370 for (i = 0; i < ARRAY_SIZE(hw_eventtypes); ++i)
0371 __set_bit(hw_eventtypes[i], input_dev->evbit);
0372
0373 for (i = 0; i < ARRAY_SIZE(hw_absevents); ++i)
0374 __set_bit(hw_absevents[i], input_dev->absbit);
0375
0376 for (i = 0; i < ARRAY_SIZE(hw_btnevents); ++i)
0377 __set_bit(hw_btnevents[i], input_dev->keybit);
0378
0379 for (i = 0; i < ARRAY_SIZE(hw_mscevents); ++i)
0380 __set_bit(hw_mscevents[i], input_dev->mscbit);
0381
0382 input_set_abs_params(input_dev, ABS_X,
0383 0, hanwang->features->max_x, 4, 0);
0384 input_set_abs_params(input_dev, ABS_Y,
0385 0, hanwang->features->max_y, 4, 0);
0386 input_set_abs_params(input_dev, ABS_TILT_X,
0387 0, hanwang->features->max_tilt_x, 0, 0);
0388 input_set_abs_params(input_dev, ABS_TILT_Y,
0389 0, hanwang->features->max_tilt_y, 0, 0);
0390 input_set_abs_params(input_dev, ABS_PRESSURE,
0391 0, hanwang->features->max_pressure, 0, 0);
0392
0393 endpoint = &intf->cur_altsetting->endpoint[0].desc;
0394 usb_fill_int_urb(hanwang->irq, dev,
0395 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
0396 hanwang->data, hanwang->features->pkg_len,
0397 hanwang_irq, hanwang, endpoint->bInterval);
0398 hanwang->irq->transfer_dma = hanwang->data_dma;
0399 hanwang->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0400
0401 error = input_register_device(hanwang->dev);
0402 if (error)
0403 goto fail3;
0404
0405 usb_set_intfdata(intf, hanwang);
0406
0407 return 0;
0408
0409 fail3: usb_free_urb(hanwang->irq);
0410 fail2: usb_free_coherent(dev, hanwang->features->pkg_len,
0411 hanwang->data, hanwang->data_dma);
0412 fail1: input_free_device(input_dev);
0413 kfree(hanwang);
0414 return error;
0415
0416 }
0417
0418 static void hanwang_disconnect(struct usb_interface *intf)
0419 {
0420 struct hanwang *hanwang = usb_get_intfdata(intf);
0421
0422 input_unregister_device(hanwang->dev);
0423 usb_free_urb(hanwang->irq);
0424 usb_free_coherent(interface_to_usbdev(intf),
0425 hanwang->features->pkg_len, hanwang->data,
0426 hanwang->data_dma);
0427 kfree(hanwang);
0428 usb_set_intfdata(intf, NULL);
0429 }
0430
0431 static const struct usb_device_id hanwang_ids[] = {
0432 { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG, HANWANG_TABLET_INT_CLASS,
0433 HANWANG_TABLET_INT_SUB_CLASS, HANWANG_TABLET_INT_PROTOCOL) },
0434 {}
0435 };
0436
0437 MODULE_DEVICE_TABLE(usb, hanwang_ids);
0438
0439 static struct usb_driver hanwang_driver = {
0440 .name = "hanwang",
0441 .probe = hanwang_probe,
0442 .disconnect = hanwang_disconnect,
0443 .id_table = hanwang_ids,
0444 };
0445
0446 module_usb_driver(hanwang_driver);