ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / input / aiptek.c
1 /*
2  *  Native support for the Aiptek 8000U
3  *
4  *  Copyright (c) 2001 Chris Atenasio           <chris@crud.net>
5  *
6  *  based on wacom.c by
7  *     Vojtech Pavlik      <vojtech@suse.cz>
8  *     Andreas Bach Aaen   <abach@stofanet.dk>
9  *     Clifford Wolf       <clifford@clifford.at>
10  *     Sam Mosel           <sam.mosel@computer.org>
11  *     James E. Blair      <corvus@gnu.org>
12  *     Daniel Egger        <egger@suse.de>
13  *
14  *
15  *  Many thanks to Oliver Kuechemann for his support.
16  *
17  *  ChangeLog:
18  *      v0.1 - Initial release
19  *      v0.2 - Hack to get around fake event 28's.
20  *      v0.3 - Make URB dynamic (Bryan W. Headley, Jun-8-2002)
21  *             (kernel 2.5.x variant, June-14-2002)
22  */
23
24 /*
25  * This program is free software; you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation; either version 2 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38  */
39
40 #include <linux/kernel.h>
41 #include <linux/slab.h>
42 #include <linux/input.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/usb.h>
46 #include <asm/unaligned.h>
47 #include <asm/byteorder.h>
48 /*
49  * Version Information
50  */
51 #define DRIVER_VERSION "v0.3"
52 #define DRIVER_AUTHOR "Chris Atenasio <chris@crud.net>"
53 #define DRIVER_DESC "USB Aiptek 6000U/8000U tablet driver (Linux 2.5.x)"
54
55 MODULE_AUTHOR(DRIVER_AUTHOR);
56 MODULE_DESCRIPTION(DRIVER_DESC);
57 MODULE_LICENSE("GPL");
58
59 /*
60  * Aiptek status packet:
61  *
62  *        bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
63  * byte0   0     0     0     0     0     0     1     0
64  * byte1  X7    X6    X5    X4    X3    X2    X1    X0
65  * byte2  X15   X14   X13   X12   X11   X10   X9    X8
66  * byte3  Y7    Y6    Y5    Y4    Y3    Y2    Y1    Y0
67  * byte4  Y15   Y14   Y13   Y12   Y11   Y10   Y9    Y8
68  * byte5   *     *     *    BS2   BS1   Tip   DV    IR
69  * byte6  P7    P6    P5    P4    P3    P2    P1    P0
70  * byte7  P15   P14   P13   P12   P11   P10   P9    P8
71  *
72  * IR: In Range = Proximity on
73  * DV = Data Valid
74  *
75  * 
76  * Command Summary:
77  *
78  * Command/Data    Description     Return Bytes    Return Value
79  * 0x10/0x00       SwitchToMouse       0
80  * 0x10/0x01       SwitchToTablet      0
81  * 0x18/0x04       Resolution500LPI    0
82  * 0x17/0x00       FilterOn            0
83  * 0x12/0xFF       AutoGainOn          0
84  * 0x01/0x00       GetXExtension       2           MaxX
85  * 0x01/0x01       GetYExtension       2           MaxY
86  * 0x02/0x00       GetModelCode        2           ModelCode = LOBYTE
87  * 0x03/0x00       GetODMCode          2           ODMCode
88  * 0x08/0x00       GetPressureLevels   2           =512
89  * 0x04/0x00       GetFirmwareVersion  2           Firmware Version
90  *
91  *
92  * To initialize the tablet:
93  *
94  * (1) Send command Resolution500LPI
95  * (2) Option Commands (GetXExtension, GetYExtension)
96  * (3) Send command SwitchToTablet
97  */
98
99 #define USB_VENDOR_ID_AIPTEK   0x08ca
100
101 struct aiptek_features {
102         char *name;
103         int pktlen;
104         int x_max;
105         int y_max;
106         int pressure_min;
107         int pressure_max;
108         usb_complete_t irq;
109         unsigned long evbit;
110         unsigned long absbit;
111         unsigned long relbit;
112         unsigned long btnbit;
113         unsigned long digibit;
114 };
115
116 struct aiptek {
117         struct input_dev dev;
118         struct usb_device *usbdev;
119         struct urb *irq;
120         struct aiptek_features *features;
121         int tool;
122         int open;
123
124         signed char *data;
125         dma_addr_t data_dma;
126 };
127
128 static void
129 aiptek_irq(struct urb *urb, struct pt_regs *regs)
130 {
131         struct aiptek *aiptek = urb->context;
132         unsigned char *data = aiptek->data;
133         struct input_dev *dev = &aiptek->dev;
134         int x;
135         int y;
136         int pressure;
137         int proximity;
138         int retval;
139
140         switch (urb->status) {
141         case 0:
142                 /* success */
143                 break;
144         case -ECONNRESET:
145         case -ENOENT:
146         case -ESHUTDOWN:
147                 /* this urb is terminated, clean up */
148                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
149                 return;
150         default:
151                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
152                 goto exit;
153         }
154
155         if ((data[0] & 2) == 0) {
156                 dbg("received unknown report #%d", data[0]);
157         }
158
159         input_regs(dev, regs);
160
161         proximity = data[5] & 0x01;
162         input_report_key(dev, BTN_TOOL_PEN, proximity);
163
164         x = le16_to_cpu(get_unaligned((u16 *) &data[1]));
165         y = le16_to_cpu(get_unaligned((u16 *) &data[3]));
166         pressure = le16_to_cpu(*(u16 *) &data[6]);
167         pressure -= aiptek->features->pressure_min;
168
169         if (pressure < 0) {
170                 pressure = 0;
171         }
172
173         if (proximity) {
174                 input_report_abs(dev, ABS_X, x);
175                 input_report_abs(dev, ABS_Y, y);
176                 input_report_abs(dev, ABS_PRESSURE, pressure);
177                 input_report_key(dev, BTN_TOUCH, data[5] & 0x04);
178                 input_report_key(dev, BTN_STYLUS, data[5] & 0x08);
179                 input_report_key(dev, BTN_STYLUS2, data[5] & 0x10);
180         }
181
182         input_sync(dev);
183
184 exit:
185         retval = usb_submit_urb (urb, GFP_ATOMIC);
186         if (retval)
187                 err ("%s - usb_submit_urb failed with result %d",
188                      __FUNCTION__, retval);
189 }
190
191 struct aiptek_features aiptek_features[] = {
192         {"Aiptek 6000U/8000U",
193          8, 3000, 2250, 26, 511, aiptek_irq, 0, 0, 0, 0},
194         {NULL, 0}
195 };
196
197 struct usb_device_id aiptek_ids[] = {
198         {USB_DEVICE(USB_VENDOR_ID_AIPTEK, 0x20), .driver_info = 0},
199         {}
200 };
201
202 MODULE_DEVICE_TABLE(usb, aiptek_ids);
203
204 static int
205 aiptek_open(struct input_dev *dev)
206 {
207         struct aiptek *aiptek = dev->private;
208
209         if (aiptek->open++)
210                 return 0;
211
212         aiptek->irq->dev = aiptek->usbdev;
213         if (usb_submit_urb(aiptek->irq, GFP_KERNEL)) {
214                 aiptek->open--;
215                 return -EIO;
216         }
217
218         return 0;
219 }
220
221 static void
222 aiptek_close(struct input_dev *dev)
223 {
224         struct aiptek *aiptek = dev->private;
225
226         if (!--aiptek->open)
227                 usb_unlink_urb(aiptek->irq);
228 }
229
230 #define USB_REQ_SET_REPORT      0x09
231 static int
232 usb_set_report(struct usb_device *dev, struct usb_host_interface *inter, unsigned char type,
233                 unsigned char id, void *buf, int size)
234 {
235         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
236                 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
237                 (type << 8) + id, inter->desc.bInterfaceNumber, buf, size, HZ);
238 }
239
240 static int
241 aiptek_command(struct usb_device *dev, struct usb_host_interface *inter,
242                unsigned char command, unsigned char data)
243 {
244         u8 *buf;
245         int err;
246         
247         buf = kmalloc(3, GFP_KERNEL);
248         if (!buf)
249                 return -ENOMEM;
250
251         buf[0] = 4;
252         buf[1] = command;
253         buf[2] = data;
254
255         if ((err = usb_set_report(dev, inter, 3, 2, buf, 3)) != 3) {
256                 dbg("aiptek_command: 0x%x 0x%x\n", command, data);
257         }
258         
259         kfree(buf);
260         return err < 0 ? err : 0;
261 }
262
263 static int 
264 aiptek_probe(struct usb_interface *intf,
265              const struct usb_device_id *id)
266 {
267         struct usb_device *dev = interface_to_usbdev (intf);
268         struct usb_host_interface *interface = intf->cur_altsetting;
269         struct usb_endpoint_descriptor *endpoint;
270         struct aiptek *aiptek;
271         int err = -ENOMEM;
272
273         if (!(aiptek = kmalloc(sizeof (struct aiptek), GFP_KERNEL)))
274                 goto error_out_noalloc;
275
276         memset(aiptek, 0, sizeof (struct aiptek));
277
278         aiptek->data = usb_buffer_alloc(dev, 10, GFP_KERNEL, &aiptek->data_dma);
279         if (!aiptek->data) {
280                 goto error_out_nobuf;
281         }
282
283         aiptek->irq = usb_alloc_urb(0, GFP_KERNEL);
284         if (!aiptek->irq) {
285                 goto error_out_nourb;
286         }
287
288         /* Resolution500LPI */
289         err = aiptek_command(dev, interface, 0x18, 0x04);
290         if (err)
291                 goto error_out;
292
293         /* SwitchToTablet */
294         err = aiptek_command(dev, interface, 0x10, 0x01);
295         if (err)
296                 goto error_out;
297
298         aiptek->features = aiptek_features + id->driver_info;
299
300         aiptek->dev.evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_MSC) |
301             aiptek->features->evbit;
302
303         aiptek->dev.absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) |
304             BIT(ABS_MISC) | aiptek->features->absbit;
305
306         aiptek->dev.relbit[0] |= aiptek->features->relbit;
307
308         aiptek->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT) |
309             BIT(BTN_MIDDLE) | aiptek->features->btnbit;
310
311         aiptek->dev.keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_PEN) |
312             BIT(BTN_TOOL_MOUSE) | BIT(BTN_TOUCH) |
313             BIT(BTN_STYLUS) | BIT(BTN_STYLUS2) | aiptek->features->digibit;
314
315         aiptek->dev.mscbit[0] = BIT(MSC_SERIAL);
316
317         aiptek->dev.absmax[ABS_X] = aiptek->features->x_max;
318         aiptek->dev.absmax[ABS_Y] = aiptek->features->y_max;
319         aiptek->dev.absmax[ABS_PRESSURE] = aiptek->features->pressure_max -
320             aiptek->features->pressure_min;
321
322         aiptek->dev.absfuzz[ABS_X] = 0;
323         aiptek->dev.absfuzz[ABS_Y] = 0;
324
325         aiptek->dev.private = aiptek;
326         aiptek->dev.open = aiptek_open;
327         aiptek->dev.close = aiptek_close;
328
329         aiptek->dev.name = aiptek->features->name;
330         aiptek->dev.id.bustype = BUS_USB;
331         aiptek->dev.id.vendor = dev->descriptor.idVendor;
332         aiptek->dev.id.product = dev->descriptor.idProduct;
333         aiptek->dev.id.version = dev->descriptor.bcdDevice;
334         aiptek->dev.dev = &intf->dev;
335         aiptek->usbdev = dev;
336
337         endpoint = &intf->altsetting[0].endpoint[0].desc;
338
339         if (aiptek->features->pktlen > 10)
340                 BUG();
341
342         usb_fill_int_urb(aiptek->irq, dev,
343                          usb_rcvintpipe(dev, endpoint->bEndpointAddress),
344                          aiptek->data, aiptek->features->pktlen,
345                          aiptek->features->irq, aiptek, endpoint->bInterval);
346         aiptek->irq->transfer_dma = aiptek->data_dma;
347         aiptek->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
348
349         input_register_device(&aiptek->dev);
350
351         printk(KERN_INFO "input: %s on usb%d:%d\n",
352                aiptek->features->name, dev->bus->busnum, dev->devnum);
353
354         usb_set_intfdata(intf, aiptek);
355         return 0;
356
357 error_out:
358         usb_free_urb(aiptek->irq);
359 error_out_nourb:
360         usb_buffer_free(dev, 10, aiptek->data, aiptek->data_dma);
361 error_out_nobuf:
362         kfree(aiptek);
363 error_out_noalloc:
364         return err;
365         
366 }
367
368 static void
369 aiptek_disconnect(struct usb_interface *intf)
370 {
371         struct aiptek *aiptek  = usb_get_intfdata (intf);
372
373         usb_set_intfdata(intf, NULL);
374         if (aiptek) {
375                 usb_unlink_urb(aiptek->irq);
376                 input_unregister_device(&aiptek->dev);
377                 usb_free_urb(aiptek->irq);
378                 usb_buffer_free(interface_to_usbdev(intf), 10, aiptek->data, aiptek->data_dma);
379                 kfree(aiptek);
380         }
381 }
382
383 static struct usb_driver aiptek_driver = {
384         .owner =        THIS_MODULE,
385         .name =         "aiptek",
386         .probe =        aiptek_probe,
387         .disconnect =   aiptek_disconnect,
388         .id_table =     aiptek_ids,
389 };
390
391 static int __init
392 aiptek_init(void)
393 {
394         int result = usb_register(&aiptek_driver);
395         if (result == 0) {
396                 info(DRIVER_VERSION " " DRIVER_AUTHOR);
397                 info(DRIVER_DESC);
398         }
399         return result;
400 }
401
402 static void __exit
403 aiptek_exit(void)
404 {
405         usb_deregister(&aiptek_driver);
406 }
407
408 module_init(aiptek_init);
409 module_exit(aiptek_exit);