74 #include <linux/kernel.h>
75 #include <linux/slab.h>
76 #include <linux/module.h>
79 #include <asm/uaccess.h>
80 #include <asm/unaligned.h>
85 #define DRIVER_VERSION "v2.3 (May 2, 2007)"
86 #define DRIVER_AUTHOR "Bryan W. Headley/Chris Atenasio/Cedric Brun/Rene van Paassen"
87 #define DRIVER_DESC "Aiptek HyperPen USB Tablet Driver (Linux 2.6.x)"
186 #define USB_VENDOR_ID_AIPTEK 0x08ca
187 #define USB_VENDOR_ID_KYE 0x0458
188 #define USB_REQ_GET_REPORT 0x01
189 #define USB_REQ_SET_REPORT 0x09
193 #define AIPTEK_POINTER_ONLY_MOUSE_MODE 0
194 #define AIPTEK_POINTER_ONLY_STYLUS_MODE 1
195 #define AIPTEK_POINTER_EITHER_MODE 2
197 #define AIPTEK_POINTER_ALLOW_MOUSE_MODE(a) \
198 (a == AIPTEK_POINTER_ONLY_MOUSE_MODE || \
199 a == AIPTEK_POINTER_EITHER_MODE)
200 #define AIPTEK_POINTER_ALLOW_STYLUS_MODE(a) \
201 (a == AIPTEK_POINTER_ONLY_STYLUS_MODE || \
202 a == AIPTEK_POINTER_EITHER_MODE)
206 #define AIPTEK_COORDINATE_RELATIVE_MODE 0
207 #define AIPTEK_COORDINATE_ABSOLUTE_MODE 1
211 #define AIPTEK_TILT_MIN (-128)
212 #define AIPTEK_TILT_MAX 127
213 #define AIPTEK_TILT_DISABLE (-10101)
217 #define AIPTEK_WHEEL_MIN 0
218 #define AIPTEK_WHEEL_MAX 1024
219 #define AIPTEK_WHEEL_DISABLE (-10101)
227 #define AIPTEK_TOOL_BUTTON_PEN_MODE BTN_TOOL_PEN
228 #define AIPTEK_TOOL_BUTTON_PENCIL_MODE BTN_TOOL_PENCIL
229 #define AIPTEK_TOOL_BUTTON_BRUSH_MODE BTN_TOOL_BRUSH
230 #define AIPTEK_TOOL_BUTTON_AIRBRUSH_MODE BTN_TOOL_AIRBRUSH
231 #define AIPTEK_TOOL_BUTTON_ERASER_MODE BTN_TOOL_RUBBER
232 #define AIPTEK_TOOL_BUTTON_MOUSE_MODE BTN_TOOL_MOUSE
233 #define AIPTEK_TOOL_BUTTON_LENS_MODE BTN_TOOL_LENS
237 #define AIPTEK_DIAGNOSTIC_NA 0
238 #define AIPTEK_DIAGNOSTIC_SENDING_RELATIVE_IN_ABSOLUTE 1
239 #define AIPTEK_DIAGNOSTIC_SENDING_ABSOLUTE_IN_RELATIVE 2
240 #define AIPTEK_DIAGNOSTIC_TOOL_DISALLOWED 3
245 #define AIPTEK_JITTER_DELAY_DEFAULT 50
251 #define AIPTEK_PROGRAMMABLE_DELAY_25 25
252 #define AIPTEK_PROGRAMMABLE_DELAY_50 50
253 #define AIPTEK_PROGRAMMABLE_DELAY_100 100
254 #define AIPTEK_PROGRAMMABLE_DELAY_200 200
255 #define AIPTEK_PROGRAMMABLE_DELAY_300 300
256 #define AIPTEK_PROGRAMMABLE_DELAY_400 400
257 #define AIPTEK_PROGRAMMABLE_DELAY_DEFAULT AIPTEK_PROGRAMMABLE_DELAY_400
261 #define AIPTEK_MOUSE_LEFT_BUTTON 0x04
262 #define AIPTEK_MOUSE_RIGHT_BUTTON 0x08
263 #define AIPTEK_MOUSE_MIDDLE_BUTTON 0x10
267 #define AIPTEK_STYLUS_LOWER_BUTTON 0x08
268 #define AIPTEK_STYLUS_UPPER_BUTTON 0x10
272 #define AIPTEK_PACKET_LENGTH 8
279 #define AIPTEK_REPORT_TOOL_UNKNOWN 0x10
280 #define AIPTEK_REPORT_TOOL_STYLUS 0x20
281 #define AIPTEK_REPORT_TOOL_MOUSE 0x40
330 static const int eventTypes[] = {
334 static const int absEvents[] = {
339 static const int relEvents[] = {
343 static const int buttonEvents[] = {
355 static const int macroKeyEvents[] = {
368 #define AIPTEK_INVALID_VALUE -1
379 if (str[count - 1] ==
'\n')
382 for (p = map; p->
string; p++)
434 static void aiptek_irq(
struct urb *
urb)
438 struct input_dev *inputdev = aiptek->
inputdev;
441 int retval, macro,
x,
y, z,
left,
right, middle,
p, dv, tip, bs, pck;
443 switch (urb->status) {
452 dev_dbg(&intf->dev,
"%s - urb shutting down with status: %d\n",
453 __func__, urb->status);
457 dev_dbg(&intf->dev,
"%s - nonzero urb status received: %d\n",
458 __func__, urb->status);
481 x = (
signed char) data[2];
482 y = (
signed char) data[3];
491 jitterable = data[1] & 0x07;
493 left = (data[1] & aiptek->
curSetting.mouseButtonLeft >> 2) != 0 ? 1 : 0;
494 right = (data[1] & aiptek->
curSetting.mouseButtonRight >> 2) != 0 ? 1 : 0;
495 middle = (data[1] & aiptek->
curSetting.mouseButtonMiddle >> 2) != 0 ? 1 : 0;
497 input_report_key(inputdev,
BTN_LEFT, left);
498 input_report_key(inputdev,
BTN_MIDDLE, middle);
499 input_report_key(inputdev,
BTN_RIGHT, right);
501 input_report_abs(inputdev,
ABS_MISC,
503 input_report_rel(inputdev,
REL_X, x);
504 input_report_rel(inputdev,
REL_Y, y);
515 input_report_key(inputdev,
519 input_sync(inputdev);
525 else if (data[0] == 2) {
532 x = get_unaligned_le16(data + 1);
533 y = get_unaligned_le16(data + 3);
534 z = get_unaligned_le16(data + 6);
536 dv = (data[5] & 0x01) != 0 ? 1 : 0;
537 p = (data[5] & 0x02) != 0 ? 1 : 0;
538 tip = (data[5] & 0x04) != 0 ? 1 : 0;
542 jitterable = data[5] & 0x18;
544 bs = (data[5] & aiptek->
curSetting.stylusButtonLower) != 0 ? 1 : 0;
545 pck = (data[5] & aiptek->
curSetting.stylusButtonUpper) != 0 ? 1 : 0;
557 input_report_key(inputdev,
559 input_report_key(inputdev,
567 input_report_abs(inputdev,
ABS_X, x);
568 input_report_abs(inputdev,
ABS_Y, y);
571 input_report_key(inputdev,
BTN_TOUCH, tip);
577 input_report_abs(inputdev,
582 input_report_abs(inputdev,
592 input_report_abs(inputdev,
600 input_report_key(inputdev,
604 input_sync(inputdev);
610 else if (data[0] == 3) {
617 x = get_unaligned_le16(data + 1);
618 y = get_unaligned_le16(data + 3);
620 jitterable = data[5] & 0x1c;
622 dv = (data[5] & 0x01) != 0 ? 1 : 0;
623 p = (data[5] & 0x02) != 0 ? 1 : 0;
624 left = (data[5] & aiptek->
curSetting.mouseButtonLeft) != 0 ? 1 : 0;
625 right = (data[5] & aiptek->
curSetting.mouseButtonRight) != 0 ? 1 : 0;
626 middle = (data[5] & aiptek->
curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
634 input_report_key(inputdev,
636 input_report_key(inputdev,
644 input_report_abs(inputdev,
ABS_X, x);
645 input_report_abs(inputdev,
ABS_Y, y);
647 input_report_key(inputdev,
BTN_LEFT, left);
648 input_report_key(inputdev,
BTN_MIDDLE, middle);
649 input_report_key(inputdev,
BTN_RIGHT, right);
655 input_report_abs(inputdev,
663 input_report_key(inputdev,
667 input_sync(inputdev);
673 else if (data[0] == 4) {
674 jitterable = data[1] & 0x18;
676 dv = (data[1] & 0x01) != 0 ? 1 : 0;
677 p = (data[1] & 0x02) != 0 ? 1 : 0;
678 tip = (data[1] & 0x04) != 0 ? 1 : 0;
679 bs = (data[1] & aiptek->
curSetting.stylusButtonLower) != 0 ? 1 : 0;
680 pck = (data[1] & aiptek->
curSetting.stylusButtonUpper) != 0 ? 1 : 0;
682 macro = dv && p && tip && !(data[3] & 1) ? (data[3] >> 1) : -1;
683 z = get_unaligned_le16(data + 4);
691 input_report_key(inputdev,
693 input_report_key(inputdev,
702 input_report_key(inputdev, macroKeyEvents[aiptek->
lastMacro], 0);
706 if (macro != -1 && macro != aiptek->
lastMacro) {
707 input_report_key(inputdev, macroKeyEvents[macro], 1);
710 input_report_abs(inputdev,
ABS_MISC,
712 input_sync(inputdev);
716 else if (data[0] == 5) {
717 jitterable = data[1] & 0x1c;
719 dv = (data[1] & 0x01) != 0 ? 1 : 0;
720 p = (data[1] & 0x02) != 0 ? 1 : 0;
721 left = (data[1]& aiptek->
curSetting.mouseButtonLeft) != 0 ? 1 : 0;
722 right = (data[1] & aiptek->
curSetting.mouseButtonRight) != 0 ? 1 : 0;
723 middle = (data[1] & aiptek->
curSetting.mouseButtonMiddle) != 0 ? 1 : 0;
724 macro = dv && p && left && !(data[3] & 1) ? (data[3] >> 1) : 0;
732 input_report_key(inputdev,
734 input_report_key(inputdev,
741 input_report_key(inputdev, macroKeyEvents[aiptek->
lastMacro], 0);
745 if (macro != -1 && macro != aiptek->
lastMacro) {
746 input_report_key(inputdev, macroKeyEvents[macro], 1);
750 input_report_abs(inputdev,
ABS_MISC,
752 input_sync(inputdev);
760 else if (data[0] == 6) {
761 macro = get_unaligned_le16(data + 1);
763 input_report_key(inputdev, macroKeyEvents[macro - 1],
767 input_report_key(inputdev, macroKeyEvents[macro + 1],
776 input_report_key(inputdev,
778 input_report_key(inputdev,
785 input_report_key(inputdev, macroKeyEvents[macro], 1);
786 input_report_abs(inputdev,
ABS_MISC,
788 input_sync(inputdev);
790 dev_dbg(&intf->dev,
"Unknown report %d\n", data[0]);
817 "%s - usb_submit_urb failed with result %d\n",
847 static int aiptek_open(
struct input_dev *inputdev)
849 struct aiptek *aiptek = input_get_drvdata(inputdev);
861 static void aiptek_close(
struct input_dev *inputdev)
863 struct aiptek *aiptek = input_get_drvdata(inputdev);
873 aiptek_set_report(
struct aiptek *aiptek,
874 unsigned char report_type,
875 unsigned char report_id,
void *
buffer,
int size)
878 usb_sndctrlpipe(aiptek->
usbdev, 0),
882 aiptek->
ifnum, buffer, size, 5000);
886 aiptek_get_report(
struct aiptek *aiptek,
887 unsigned char report_type,
888 unsigned char report_id,
void *buffer,
int size)
891 usb_rcvctrlpipe(aiptek->
usbdev, 0),
895 aiptek->
ifnum, buffer, size, 5000);
902 aiptek_command(
struct aiptek *aiptek,
unsigned char command,
unsigned char data)
904 const int sizeof_buf = 3 *
sizeof(
u8);
917 aiptek_set_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
919 "aiptek_program: failed, tried to send: 0x%02x 0x%02x\n",
923 return ret < 0 ? ret : 0;
932 aiptek_query(
struct aiptek *aiptek,
unsigned char command,
unsigned char data)
934 const int sizeof_buf = 3 *
sizeof(
u8);
946 if (aiptek_command(aiptek, command, data) != 0) {
953 aiptek_get_report(aiptek, 3, 2, buf, sizeof_buf)) != sizeof_buf) {
955 "aiptek_query failed: returned 0x%02x 0x%02x 0x%02x\n",
956 buf[0], buf[1], buf[2]);
959 ret = get_unaligned_le16(buf + 1);
969 static int aiptek_program_tablet(
struct aiptek *aiptek)
973 if ((ret = aiptek_command(aiptek, 0x18, 0x04)) < 0)
977 if ((ret = aiptek_query(aiptek, 0x02, 0x00)) < 0)
979 aiptek->
features.modelCode = ret & 0xff;
982 if ((ret = aiptek_query(aiptek, 0x03, 0x00)) < 0)
987 if ((ret = aiptek_query(aiptek, 0x04, 0x00)) < 0)
992 if ((ret = aiptek_query(aiptek, 0x01, 0x00)) < 0)
994 input_set_abs_params(aiptek->
inputdev,
ABS_X, 0, ret - 1, 0, 0);
997 if ((ret = aiptek_query(aiptek, 0x01, 0x01)) < 0)
999 input_set_abs_params(aiptek->
inputdev,
ABS_Y, 0, ret - 1, 0, 0);
1002 if ((ret = aiptek_query(aiptek, 0x08, 0x00)) < 0)
1012 if ((ret = aiptek_command(aiptek, 0x10, 0x01)) < 0) {
1017 if ((ret = aiptek_command(aiptek, 0x10, 0x00)) < 0) {
1023 if ((ret = aiptek_command(aiptek, 0x11, 0x02)) < 0)
1027 if ((ret = aiptek_command(aiptek, 0x17, 0x00)) < 0)
1032 if ((ret = aiptek_command(aiptek, 0x12, 0xff)) < 0)
1074 static struct aiptek_map pointer_mode_map[] = {
1086 map_val_to_str(pointer_mode_map,
1094 int new_mode = map_str_to_val(pointer_mode_map, buf, count);
1105 show_tabletPointerMode, store_tabletPointerMode);
1112 static struct aiptek_map coordinate_mode_map[] = {
1123 map_val_to_str(coordinate_mode_map,
1128 store_tabletCoordinateMode(
struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
1131 int new_mode = map_str_to_val(coordinate_mode_map, buf, count);
1136 aiptek->
newSetting.coordinateMode = new_mode;
1142 show_tabletCoordinateMode, store_tabletCoordinateMode);
1165 map_val_to_str(tool_mode_map,
1173 int new_mode = map_str_to_val(tool_mode_map, buf, count);
1184 show_tabletToolMode, store_tabletToolMode);
1209 size_t len = buf[count - 1] ==
'\n' ? count - 1 :
count;
1211 if (
strncmp(buf,
"disable", len))
1251 size_t len = buf[count - 1] ==
'\n' ? count - 1 :
count;
1253 if (
strncmp(buf,
"disable", len))
1282 store_tabletJitterDelay(
struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
1297 show_tabletJitterDelay, store_tabletJitterDelay);
1312 store_tabletProgrammableDelay(
struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
1327 show_tabletProgrammableDelay, store_tabletProgrammableDelay);
1353 retMsg =
"no errors\n";
1357 retMsg =
"Error: receiving relative reports\n";
1361 retMsg =
"Error: receiving absolute reports\n";
1367 retMsg =
"Error: receiving stylus reports\n";
1369 retMsg =
"Error: receiving mouse reports\n";
1386 static struct aiptek_map stylus_button_map[] = {
1397 map_val_to_str(stylus_button_map,
1402 store_tabletStylusUpper(
struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
1405 int new_button = map_str_to_val(stylus_button_map, buf, count);
1410 aiptek->
newSetting.stylusButtonUpper = new_button;
1416 show_tabletStylusUpper, store_tabletStylusUpper);
1428 map_val_to_str(stylus_button_map,
1433 store_tabletStylusLower(
struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
1436 int new_button = map_str_to_val(stylus_button_map, buf, count);
1441 aiptek->
newSetting.stylusButtonLower = new_button;
1447 show_tabletStylusLower, store_tabletStylusLower);
1454 static struct aiptek_map mouse_button_map[] = {
1466 map_val_to_str(mouse_button_map,
1474 int new_button = map_str_to_val(mouse_button_map, buf, count);
1479 aiptek->
newSetting.mouseButtonLeft = new_button;
1485 show_tabletMouseLeft, store_tabletMouseLeft);
1496 map_val_to_str(mouse_button_map,
1501 store_tabletMouseMiddle(
struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count)
1504 int new_button = map_str_to_val(mouse_button_map, buf, count);
1509 aiptek->
newSetting.mouseButtonMiddle = new_button;
1515 show_tabletMouseMiddle, store_tabletMouseMiddle);
1526 map_val_to_str(mouse_button_map,
1534 int new_button = map_str_to_val(mouse_button_map, buf, count);
1539 aiptek->
newSetting.mouseButtonRight = new_button;
1545 show_tabletMouseRight, store_tabletMouseRight);
1590 "Write anything to this file to program your tablet.\n");
1604 if (aiptek_program_tablet(aiptek) < 0)
1653 static struct attribute *aiptek_attributes[] = {
1654 &dev_attr_size.attr,
1655 &dev_attr_pointer_mode.attr,
1656 &dev_attr_coordinate_mode.attr,
1657 &dev_attr_tool_mode.attr,
1658 &dev_attr_xtilt.attr,
1659 &dev_attr_ytilt.attr,
1660 &dev_attr_jitter.attr,
1661 &dev_attr_delay.attr,
1662 &dev_attr_event_count.attr,
1663 &dev_attr_diagnostic.attr,
1664 &dev_attr_odm_code.attr,
1665 &dev_attr_model_code.attr,
1666 &dev_attr_firmware_code.attr,
1667 &dev_attr_stylus_lower.attr,
1668 &dev_attr_stylus_upper.attr,
1669 &dev_attr_mouse_left.attr,
1670 &dev_attr_mouse_middle.attr,
1671 &dev_attr_mouse_right.attr,
1672 &dev_attr_wheel.attr,
1673 &dev_attr_execute.attr,
1678 .attrs = aiptek_attributes,
1688 struct usb_device *usbdev = interface_to_usbdev(intf);
1690 struct aiptek *aiptek;
1691 struct input_dev *inputdev;
1709 speeds[0] = programmableDelay;
1711 aiptek = kzalloc(
sizeof(
struct aiptek),
GFP_KERNEL);
1712 inputdev = input_allocate_device();
1713 if (!aiptek || !inputdev) {
1715 "cannot allocate memory or input device\n");
1721 if (!aiptek->
data) {
1722 dev_warn(&intf->dev,
"cannot allocate usb buffer\n");
1728 dev_warn(&intf->dev,
"cannot allocate urb\n");
1735 aiptek->
ifnum = intf->altsetting[0].desc.bInterfaceNumber;
1757 aiptek->
curSetting.jitterDelay = jitterDelay;
1758 aiptek->
curSetting.programmableDelay = programmableDelay;
1771 usb_make_path(usbdev, aiptek->
features.usbPath,
1779 inputdev->name =
"Aiptek";
1780 inputdev->phys = aiptek->
features.usbPath;
1781 usb_to_input_id(usbdev, &inputdev->id);
1782 inputdev->dev.parent = &intf->dev;
1784 input_set_drvdata(inputdev, aiptek);
1786 inputdev->open = aiptek_open;
1787 inputdev->close = aiptek_close;
1793 __set_bit(eventTypes[i], inputdev->evbit);
1796 __set_bit(absEvents[i], inputdev->absbit);
1799 __set_bit(relEvents[i], inputdev->relbit);
1805 __set_bit(buttonEvents[i], inputdev->keybit);
1808 __set_bit(macroKeyEvents[i], inputdev->keybit);
1816 input_set_abs_params(inputdev,
ABS_X, 0, 2999, 0, 0);
1817 input_set_abs_params(inputdev,
ABS_Y, 0, 2249, 0, 0);
1818 input_set_abs_params(inputdev,
ABS_PRESSURE, 0, 511, 0, 0);
1823 endpoint = &intf->altsetting[0].endpoint[0].desc;
1828 usb_fill_int_urb(aiptek->
urb,
1830 usb_rcvintpipe(aiptek->
usbdev,
1832 aiptek->
data, 8, aiptek_irq, aiptek,
1836 aiptek->
urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1851 (
void)aiptek_program_tablet(aiptek);
1854 "Aiptek using %d ms programming speed\n",
1864 "Aiptek tried all speeds, no sane response\n");
1870 usb_set_intfdata(intf, aiptek);
1876 dev_warn(&intf->dev,
"cannot create sysfs group err: %d\n",
1883 err = input_register_device(aiptek->
inputdev);
1886 "input_register_device returned err: %d\n", err);
1895 fail1: usb_set_intfdata(intf,
NULL);
1896 input_free_device(inputdev);
1906 struct aiptek *aiptek = usb_get_intfdata(intf);
1910 usb_set_intfdata(intf,
NULL);
1911 if (aiptek !=
NULL) {
1915 input_unregister_device(aiptek->
inputdev);
1925 static struct usb_driver aiptek_driver = {
1927 .probe = aiptek_probe,
1928 .disconnect = aiptek_disconnect,
1929 .id_table = aiptek_ids,
1939 MODULE_PARM_DESC(programmableDelay,
"delay used during tablet programming");