xref: /openbmc/linux/drivers/input/tablet/kbtab.c (revision 82e6fdd6)
1 #include <linux/kernel.h>
2 #include <linux/slab.h>
3 #include <linux/module.h>
4 #include <linux/usb/input.h>
5 #include <asm/unaligned.h>
6 
7 /*
8  * Pressure-threshold modules param code from Alex Perry <alex.perry@ieee.org>
9  */
10 
11 MODULE_AUTHOR("Josh Myer <josh@joshisanerd.com>");
12 MODULE_DESCRIPTION("USB KB Gear JamStudio Tablet driver");
13 MODULE_LICENSE("GPL");
14 
15 #define USB_VENDOR_ID_KBGEAR	0x084e
16 
17 static int kb_pressure_click = 0x10;
18 module_param(kb_pressure_click, int, 0);
19 MODULE_PARM_DESC(kb_pressure_click, "pressure threshold for clicks");
20 
21 struct kbtab {
22 	unsigned char *data;
23 	dma_addr_t data_dma;
24 	struct input_dev *dev;
25 	struct usb_interface *intf;
26 	struct urb *irq;
27 	char phys[32];
28 };
29 
30 static void kbtab_irq(struct urb *urb)
31 {
32 	struct kbtab *kbtab = urb->context;
33 	unsigned char *data = kbtab->data;
34 	struct input_dev *dev = kbtab->dev;
35 	int pressure;
36 	int retval;
37 
38 	switch (urb->status) {
39 	case 0:
40 		/* success */
41 		break;
42 	case -ECONNRESET:
43 	case -ENOENT:
44 	case -ESHUTDOWN:
45 		/* this urb is terminated, clean up */
46 		dev_dbg(&kbtab->intf->dev,
47 			"%s - urb shutting down with status: %d\n",
48 			__func__, urb->status);
49 		return;
50 	default:
51 		dev_dbg(&kbtab->intf->dev,
52 			"%s - nonzero urb status received: %d\n",
53 			__func__, urb->status);
54 		goto exit;
55 	}
56 
57 
58 	input_report_key(dev, BTN_TOOL_PEN, 1);
59 
60 	input_report_abs(dev, ABS_X, get_unaligned_le16(&data[1]));
61 	input_report_abs(dev, ABS_Y, get_unaligned_le16(&data[3]));
62 
63 	/*input_report_key(dev, BTN_TOUCH , data[0] & 0x01);*/
64 	input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
65 
66 	pressure = data[5];
67 	if (kb_pressure_click == -1)
68 		input_report_abs(dev, ABS_PRESSURE, pressure);
69 	else
70 		input_report_key(dev, BTN_LEFT, pressure > kb_pressure_click ? 1 : 0);
71 
72 	input_sync(dev);
73 
74  exit:
75 	retval = usb_submit_urb(urb, GFP_ATOMIC);
76 	if (retval)
77 		dev_err(&kbtab->intf->dev,
78 			"%s - usb_submit_urb failed with result %d\n",
79 			__func__, retval);
80 }
81 
82 static const struct usb_device_id kbtab_ids[] = {
83 	{ USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 },
84 	{ }
85 };
86 
87 MODULE_DEVICE_TABLE(usb, kbtab_ids);
88 
89 static int kbtab_open(struct input_dev *dev)
90 {
91 	struct kbtab *kbtab = input_get_drvdata(dev);
92 	struct usb_device *udev = interface_to_usbdev(kbtab->intf);
93 
94 	kbtab->irq->dev = udev;
95 	if (usb_submit_urb(kbtab->irq, GFP_KERNEL))
96 		return -EIO;
97 
98 	return 0;
99 }
100 
101 static void kbtab_close(struct input_dev *dev)
102 {
103 	struct kbtab *kbtab = input_get_drvdata(dev);
104 
105 	usb_kill_urb(kbtab->irq);
106 }
107 
108 static int kbtab_probe(struct usb_interface *intf, const struct usb_device_id *id)
109 {
110 	struct usb_device *dev = interface_to_usbdev(intf);
111 	struct usb_endpoint_descriptor *endpoint;
112 	struct kbtab *kbtab;
113 	struct input_dev *input_dev;
114 	int error = -ENOMEM;
115 
116 	if (intf->cur_altsetting->desc.bNumEndpoints < 1)
117 		return -ENODEV;
118 
119 	kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL);
120 	input_dev = input_allocate_device();
121 	if (!kbtab || !input_dev)
122 		goto fail1;
123 
124 	kbtab->data = usb_alloc_coherent(dev, 8, GFP_KERNEL, &kbtab->data_dma);
125 	if (!kbtab->data)
126 		goto fail1;
127 
128 	kbtab->irq = usb_alloc_urb(0, GFP_KERNEL);
129 	if (!kbtab->irq)
130 		goto fail2;
131 
132 	kbtab->intf = intf;
133 	kbtab->dev = input_dev;
134 
135 	usb_make_path(dev, kbtab->phys, sizeof(kbtab->phys));
136 	strlcat(kbtab->phys, "/input0", sizeof(kbtab->phys));
137 
138 	input_dev->name = "KB Gear Tablet";
139 	input_dev->phys = kbtab->phys;
140 	usb_to_input_id(dev, &input_dev->id);
141 	input_dev->dev.parent = &intf->dev;
142 
143 	input_set_drvdata(input_dev, kbtab);
144 
145 	input_dev->open = kbtab_open;
146 	input_dev->close = kbtab_close;
147 
148 	input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
149 	input_dev->keybit[BIT_WORD(BTN_LEFT)] |=
150 		BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
151 	input_dev->keybit[BIT_WORD(BTN_DIGI)] |=
152 		BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_TOUCH);
153 	input_set_abs_params(input_dev, ABS_X, 0, 0x2000, 4, 0);
154 	input_set_abs_params(input_dev, ABS_Y, 0, 0x1750, 4, 0);
155 	input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xff, 0, 0);
156 
157 	endpoint = &intf->cur_altsetting->endpoint[0].desc;
158 
159 	usb_fill_int_urb(kbtab->irq, dev,
160 			 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
161 			 kbtab->data, 8,
162 			 kbtab_irq, kbtab, endpoint->bInterval);
163 	kbtab->irq->transfer_dma = kbtab->data_dma;
164 	kbtab->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
165 
166 	error = input_register_device(kbtab->dev);
167 	if (error)
168 		goto fail3;
169 
170 	usb_set_intfdata(intf, kbtab);
171 
172 	return 0;
173 
174  fail3:	usb_free_urb(kbtab->irq);
175  fail2:	usb_free_coherent(dev, 8, kbtab->data, kbtab->data_dma);
176  fail1:	input_free_device(input_dev);
177 	kfree(kbtab);
178 	return error;
179 }
180 
181 static void kbtab_disconnect(struct usb_interface *intf)
182 {
183 	struct kbtab *kbtab = usb_get_intfdata(intf);
184 	struct usb_device *udev = interface_to_usbdev(intf);
185 
186 	usb_set_intfdata(intf, NULL);
187 
188 	input_unregister_device(kbtab->dev);
189 	usb_free_urb(kbtab->irq);
190 	usb_free_coherent(udev, 8, kbtab->data, kbtab->data_dma);
191 	kfree(kbtab);
192 }
193 
194 static struct usb_driver kbtab_driver = {
195 	.name =		"kbtab",
196 	.probe =	kbtab_probe,
197 	.disconnect =	kbtab_disconnect,
198 	.id_table =	kbtab_ids,
199 };
200 
201 module_usb_driver(kbtab_driver);
202