Staging: mimio: checkpatch.pl line > 80 chars fixes
[safe/jmp/linux-2.6] / drivers / staging / mimio / mimio.c
1 /*
2  * Hardware event => input event mapping:
3  *
4  * BTN_TOOL_PEN            0x140 black
5  * BTN_TOOL_RUBBER         0x141 blue
6  * BTN_TOOL_BRUSH          0x142 green
7  * BTN_TOOL_PENCIL         0x143 red
8  * BTN_TOOL_AIRBRUSH       0x144 eraser
9  * BTN_TOOL_FINGER         0x145 small eraser
10  * BTN_TOOL_MOUSE          0x146 mimio interactive
11  * BTN_TOOL_LENS           0x147 mimio interactive but1
12  * LOCALBTN_TOOL_EXTRA1    0x14a mimio interactive but2 == BTN_TOUCH
13  * LOCALBTN_TOOL_EXTRA2    0x14b mimio extra pens (orange, brown, yellow,
14  *                               purple) == BTN_STYLUS
15  * LOCALBTN_TOOL_EXTRA3    0x14c unused == BTN_STYLUS2
16  * BTN_TOOL_DOUBLETAP      0x14d unused
17  * BTN_TOOL_TRIPLETAP      0x14e unused
18  *
19  * MIMIO_EV_PENDOWN(MIMIO_PEN_K)        => EV_KEY BIT(BTN_TOOL_PEN)
20  * MIMIO_EV_PENDOWN(MIMIO_PEN_B)        => EV_KEY BIT(BTN_TOOL_RUBBER)
21  * MIMIO_EV_PENDOWN(MIMIO_PEN_G)        => EV_KEY BIT(BTN_TOOL_BRUSH)
22  * MIMIO_EV_PENDOWN(MIMIO_PEN_R)        => EV_KEY BIT(BTN_TOOL_PENCIL)
23  * MIMIO_EV_PENDOWN(MIMIO_PEN_E)        => EV_KEY BIT(BTN_TOOL_AIRBRUSH)
24  * MIMIO_EV_PENDOWN(MIMIO_PEN_ES)       => EV_KEY BIT(BTN_TOOL_FINGER)
25  * MIMIO_EV_PENDOWN(MIMIO_PEN_I)        => EV_KEY BIT(BTN_TOOL_MOUSE)
26  * MIMIO_EV_PENDOWN(MIMIO_PEN_IL)       => EV_KEY BIT(BTN_TOOL_LENS)
27  * MIMIO_EV_PENDOWN(MIMIO_PEN_IR)       => EV_KEY BIT(BTN_TOOL_DOUBLETAP)
28  * MIMIO_EV_PENDOWN(MIMIO_PEN_EX)       => EV_KEY BIT(BTN_TOOL_TRIPLETAP)
29  * MIMIO_EV_PENDATA             => EV_ABS BIT(ABS_X), BIT(ABS_Y)
30  * MIMIO_EV_MEMRESET            => EV_KEY BIT(BTN_0)
31  * MIMIO_EV_ACC(ACC_NEWPAGE)    => EV_KEY BIT(BTN_1)
32  * MIMIO_EV_ACC(ACC_TAGPAGE)    => EV_KEY BIT(BTN_2)
33  * MIMIO_EV_ACC(ACC_PRINTPAGE)  => EV_KEY BIT(BTN_3)
34  * MIMIO_EV_ACC(ACC_MAXIMIZE)   => EV_KEY BIT(BTN_4)
35  * MIMIO_EV_ACC(ACC_FINDCTLPNL) => EV_KEY BIT(BTN_5)
36  *
37  * open issues:
38  * - cold-load of data captured when mimio in standalone mode not yet
39  *   supported; need to snoop Win32 box to see datastream for this.
40  * - mimio mouse not yet supported; need to snoop Win32 box to see the
41  *   datastream for this.
42  */
43
44 #include <linux/kernel.h>
45 #include <linux/init.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48 #include <linux/input.h>
49 #include <linux/usb.h>
50
51 #define DRIVER_VERSION          "v0.031"
52 #define DRIVER_AUTHOR           "mwilder@cs.nmsu.edu"
53 #define DRIVER_DESC             "USB mimio-xi driver"
54
55 enum {UPVALUE, DOWNVALUE, MOVEVALUE};
56
57 #define MIMIO_XRANGE_MAX        9600
58 #define MIMIO_YRANGE_MAX        4800
59
60 #define LOCALBTN_TOOL_EXTRA1    BTN_TOUCH
61 #define LOCALBTN_TOOL_EXTRA2    BTN_STYLUS
62 #define LOCALBTN_TOOL_EXTRA3    BTN_STYLUS2
63
64 #define MIMIO_VENDOR_ID         0x08d3
65 #define MIMIO_PRODUCT_ID        0x0001
66 #define MIMIO_MAXPAYLOAD        (8)
67 #define MIMIO_MAXNAMELEN        (64)
68 #define MIMIO_TXWAIT            (1)
69 #define MIMIO_TXDONE            (2)
70
71 #define MIMIO_EV_PENDOWN        (0x22)
72 #define MIMIO_EV_PENDATA        (0x24)
73 #define MIMIO_EV_PENUP          (0x51)
74 #define MIMIO_EV_MEMRESET       (0x45)
75 #define MIMIO_EV_ACC            (0xb2)
76
77 #define MIMIO_PEN_K             (1)     /* black pen */
78 #define MIMIO_PEN_B             (2)     /* blue pen */
79 #define MIMIO_PEN_G             (3)     /* green pen */
80 #define MIMIO_PEN_R             (4)     /* red pen */
81 /* 5, 6, 7, 8 are extra pens */
82 #define MIMIO_PEN_E             (9)     /* big eraser */
83 #define MIMIO_PEN_ES            (10)    /* lil eraser */
84 #define MIMIO_PENJUMP_START     (10)
85 #define MIMIO_PENJUMP           (6)
86 #define MIMIO_PEN_I             (17)    /* mimio interactive */
87 #define MIMIO_PEN_IL            (18)    /* mimio interactive button 1 */
88 #define MIMIO_PEN_IR            (19)    /* mimio interactive button 2 */
89
90 #define MIMIO_PEN_MAX           (MIMIO_PEN_IR)
91
92 #define ACC_DONE                (0)
93 #define ACC_NEWPAGE             (1)
94 #define ACC_TAGPAGE             (2)
95 #define ACC_PRINTPAGE           (4)
96 #define ACC_MAXIMIZE            (8)
97 #define ACC_FINDCTLPNL          (16)
98
99 #define isvalidtxsize(n)        ((n) > 0 && (n) <= MIMIO_MAXPAYLOAD)
100
101
102 struct pktbuf {
103         unsigned char instr;
104         unsigned char buf[16];
105         unsigned char *p;
106         unsigned char *q;
107 };
108
109 struct usbintendpt {
110         dma_addr_t dma;
111         struct urb *urb;
112         unsigned char *buf;
113         struct usb_endpoint_descriptor *desc;
114 };
115
116 struct mimio {
117         struct input_dev *idev;
118         struct usb_device *udev;
119         struct usb_interface *uifc;
120         int open;
121         int present;
122         int greeted;
123         int txflags;
124         char phys[MIMIO_MAXNAMELEN];
125         struct usbintendpt in;
126         struct usbintendpt out;
127         struct pktbuf pktbuf;
128         unsigned char minor;
129         wait_queue_head_t waitq;
130         spinlock_t txlock;
131         void (*rxhandler)(struct mimio *, unsigned char *, unsigned int);
132         int last_pen_down;
133 };
134
135 static void mimio_close(struct input_dev *);
136 static void mimio_dealloc(struct mimio *);
137 static void mimio_disconnect(struct usb_interface *);
138 static int mimio_greet(struct mimio *);
139 static void mimio_irq_in(struct urb *);
140 static void mimio_irq_out(struct urb *);
141 static int mimio_open(struct input_dev *);
142 static int mimio_probe(struct usb_interface *, const struct usb_device_id *);
143 static void mimio_rx_handler(struct mimio *, unsigned char *, unsigned int);
144 static int mimio_tx(struct mimio *, const char *, int);
145
146 static char mimio_name[] = "VirtualInk mimio-Xi";
147 static struct usb_device_id mimio_table[] = {
148         { USB_DEVICE(MIMIO_VENDOR_ID, MIMIO_PRODUCT_ID) },
149         { USB_DEVICE(0x0525, 0xa4a0) }, /* gadget zero firmware */
150         { }
151 };
152
153 MODULE_DEVICE_TABLE(usb, mimio_table);
154
155 static struct usb_driver mimio_driver = {
156         .name = "mimio",
157         .probe = mimio_probe,
158         .disconnect = mimio_disconnect,
159         .id_table = mimio_table,
160 };
161
162 static DECLARE_MUTEX(disconnect_sem);
163
164 static void mimio_close(struct input_dev *idev)
165 {
166         struct mimio *mimio;
167
168         mimio = input_get_drvdata(idev);
169         if (!mimio) {
170                 dev_err(&idev->dev, "null mimio attached to input device\n");
171                 return;
172         }
173
174         if (mimio->open <= 0)
175                 dev_err(&idev->dev, "mimio not open.\n");
176         else
177                 mimio->open--;
178
179         if (mimio->present == 0 && mimio->open == 0)
180                 mimio_dealloc(mimio);
181 }
182
183 static void mimio_dealloc(struct mimio *mimio)
184 {
185         if (mimio == NULL)
186                 return;
187
188         usb_kill_urb(mimio->in.urb);
189
190         usb_kill_urb(mimio->out.urb);
191
192         if (mimio->idev) {
193                 input_unregister_device(mimio->idev);
194                 if (mimio->idev->grab)
195                         input_close_device(mimio->idev->grab);
196                 else
197                         dev_dbg(&mimio->idev->dev, "mimio->idev->grab == NULL"
198                                 " -- didn't call input_close_device\n");
199         }
200
201         usb_free_urb(mimio->in.urb);
202
203         usb_free_urb(mimio->out.urb);
204
205         if (mimio->in.buf) {
206                 usb_buffer_free(mimio->udev, MIMIO_MAXPAYLOAD, mimio->in.buf,
207                                 mimio->in.dma);
208         }
209
210         if (mimio->out.buf)
211                 usb_buffer_free(mimio->udev, MIMIO_MAXPAYLOAD, mimio->out.buf,
212                                 mimio->out.dma);
213
214         if (mimio->idev)
215                 input_free_device(mimio->idev);
216
217         kfree(mimio);
218 }
219
220 static void mimio_disconnect(struct usb_interface *ifc)
221 {
222         struct mimio *mimio;
223
224         down(&disconnect_sem);
225
226         mimio = usb_get_intfdata(ifc);
227         usb_set_intfdata(ifc, NULL);
228         dev_dbg(&mimio->idev->dev, "disconnect\n");
229
230         if (mimio) {
231                 mimio->present = 0;
232
233                 if (mimio->open <= 0)
234                         mimio_dealloc(mimio);
235         }
236
237         up(&disconnect_sem);
238 }
239
240 static int mimio_greet(struct mimio *mimio)
241 {
242         const struct grtpkt {
243                 int nbytes;
244                 unsigned delay;
245                 char data[8];
246         } grtpkts[] = {
247                 { 3, 0, { 0x11, 0x55, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00 } },
248                 { 5, 0, { 0x53, 0x55, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00 } },
249                 { 5, 0, { 0x43, 0x55, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00 } },
250                 { 5, 0, { 0x33, 0x55, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00 } },
251                 { 5, 0, { 0x13, 0x00, 0x5e, 0x02, 0x4f, 0x00, 0x00, 0x00 } },
252                 { 5, 0, { 0x13, 0x00, 0x04, 0x03, 0x14, 0x00, 0x00, 0x00 } },
253                 { 5, 2, { 0x13, 0x00, 0x00, 0x04, 0x17, 0x00, 0x00, 0x00 } },
254                 { 5, 0, { 0x13, 0x00, 0x0d, 0x08, 0x16, 0x00, 0x00, 0x00 } },
255                 { 5, 0, { 0x13, 0x00, 0x4d, 0x01, 0x5f, 0x00, 0x00, 0x00 } },
256                 { 3, 0, { 0xf1, 0x55, 0xa4, 0x00, 0x00, 0x00, 0x00, 0x00 } },
257                 { 7, 2, { 0x52, 0x55, 0x00, 0x07, 0x31, 0x55, 0x64, 0x00 } },
258                 { 0, 0, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
259         };
260         int rslt;
261         const struct grtpkt *pkt;
262
263         for (pkt = grtpkts; pkt->nbytes; pkt++) {
264                 rslt = mimio_tx(mimio, pkt->data, pkt->nbytes);
265                 if (rslt)
266                         return rslt;
267                 if (pkt->delay)
268                         msleep(pkt->delay);
269         }
270
271         return 0;
272 }
273
274 static void mimio_irq_in(struct urb *urb)
275 {
276         int rslt;
277         char *data;
278         const char *reason = "going down";
279         struct mimio *mimio;
280
281         mimio = urb->context;
282
283         if (mimio == NULL)
284                 /* paranoia */
285                 return;
286
287         switch (urb->status) {
288         case 0:
289                 /* success */
290                 break;
291         case -ETIMEDOUT:
292                 reason = "timeout -- unplugged?";
293         case -ECONNRESET:
294         case -ENOENT:
295         case -ESHUTDOWN:
296                 dev_dbg(&mimio->idev->dev, "%s.\n", reason);
297                 return;
298         default:
299                 dev_dbg(&mimio->idev->dev, "unknown urb-status: %d.\n",
300                         urb->status);
301                 goto exit;
302         }
303         data = mimio->in.buf;
304
305         if (mimio->rxhandler)
306                 mimio->rxhandler(mimio, data, urb->actual_length);
307 exit:
308         /*
309          * Keep listening to device on same urb.
310          */
311         rslt = usb_submit_urb(urb, GFP_ATOMIC);
312         if (rslt)
313                 dev_err(&mimio->idev->dev, "usb_submit_urb failure: %d.\n",
314                         rslt);
315 }
316
317 static void mimio_irq_out(struct urb *urb)
318 {
319         unsigned long flags;
320         struct mimio *mimio;
321
322         mimio = urb->context;
323
324         if (urb->status)
325                 dev_dbg(&mimio->idev->dev, "urb-status: %d.\n", urb->status);
326
327         spin_lock_irqsave(&mimio->txlock, flags);
328         mimio->txflags |= MIMIO_TXDONE;
329         spin_unlock_irqrestore(&mimio->txlock, flags);
330         wmb();
331         wake_up(&mimio->waitq);
332 }
333
334 static int mimio_open(struct input_dev *idev)
335 {
336         int rslt;
337         struct mimio *mimio;
338
339         rslt = 0;
340         down(&disconnect_sem);
341         mimio = input_get_drvdata(idev);
342         dev_dbg(&idev->dev, "mimio_open\n");
343
344         if (mimio == NULL) {
345                 dev_err(&idev->dev, "null mimio.\n");
346                 rslt = -ENODEV;
347                 goto exit;
348         }
349
350         if (mimio->open++)
351                 goto exit;
352
353         if (mimio->present && !mimio->greeted) {
354                 struct urb *urb = mimio->in.urb;
355                 mimio->in.urb->dev = mimio->udev;
356                 rslt = usb_submit_urb(mimio->in.urb, GFP_KERNEL);
357                 if (rslt) {
358                         dev_err(&idev->dev, "usb_submit_urb failure "
359                                         "(res = %d: ", rslt);
360                         if (!urb)
361                                 dev_err(&idev->dev, "urb is NULL");
362                         else if (urb->hcpriv)
363                                 dev_err(&idev->dev, "urb->hcpriv is non-NULL");
364                         else if (!urb->complete)
365                                 dev_err(&idev->dev, "urb is not complete");
366                         else if (urb->number_of_packets <= 0)
367                                 dev_err(&idev->dev, "urb has no packets");
368                         else if (urb->interval <= 0)
369                                 dev_err(&idev->dev, "urb interval too small");
370                         else
371                                 dev_err(&idev->dev, "urb interval too large " \
372                                                 "or some other error");
373                         dev_err(&idev->dev, "). Not greeting.\n");
374                         rslt = -EIO;
375                         goto exit;
376                 }
377                 rslt = mimio_greet(mimio);
378                 if (rslt == 0) {
379                         dev_dbg(&idev->dev, "Mimio greeted OK.\n");
380                         mimio->greeted = 1;
381                 } else {
382                         dev_dbg(&idev->dev, "Mimio greet Failure (%d)\n",
383                                 rslt);
384                 }
385         }
386
387 exit:
388         up(&disconnect_sem);
389         return rslt;
390 }
391
392 static int mimio_probe(struct usb_interface *ifc,
393                        const struct usb_device_id *id)
394 {
395         char path[64];
396         int pipe, maxp;
397         struct mimio *mimio;
398         struct usb_device *udev;
399         struct usb_host_interface *hostifc;
400         struct input_dev *input_dev;
401         int res = 0;
402         int i;
403
404         udev = interface_to_usbdev(ifc);
405
406         mimio = kzalloc(sizeof(struct mimio), GFP_KERNEL);
407         if (!mimio)
408                 return -ENOMEM;
409
410         input_dev = input_allocate_device();
411         if (!input_dev) {
412                 mimio_dealloc(mimio);
413                 return -ENOMEM;
414         }
415
416         mimio->uifc = ifc;
417         mimio->udev = udev;
418         mimio->pktbuf.p = mimio->pktbuf.buf;
419         mimio->pktbuf.q = mimio->pktbuf.buf;
420         /* init_input_dev(mimio->idev); */
421         mimio->idev = input_dev;
422         init_waitqueue_head(&mimio->waitq);
423         spin_lock_init(&mimio->txlock);
424         hostifc = ifc->cur_altsetting;
425
426         if (hostifc->desc.bNumEndpoints != 2) {
427                 dev_err(&udev->dev, "Unexpected endpoint count: %d.\n",
428                         hostifc->desc.bNumEndpoints);
429                 mimio_dealloc(mimio);
430                 return -ENODEV;
431         }
432
433         mimio->in.desc = &(hostifc->endpoint[0].desc);
434         mimio->out.desc = &(hostifc->endpoint[1].desc);
435
436         mimio->in.buf = usb_buffer_alloc(udev, MIMIO_MAXPAYLOAD, GFP_KERNEL,
437                                          &mimio->in.dma);
438         mimio->out.buf = usb_buffer_alloc(udev, MIMIO_MAXPAYLOAD, GFP_KERNEL,
439                                           &mimio->out.dma);
440
441         if (mimio->in.buf == NULL || mimio->out.buf == NULL) {
442                 dev_err(&udev->dev, "usb_buffer_alloc failure.\n");
443                 mimio_dealloc(mimio);
444                 return -ENOMEM;
445         }
446
447         mimio->in.urb = usb_alloc_urb(0, GFP_KERNEL);
448         mimio->out.urb = usb_alloc_urb(0, GFP_KERNEL);
449
450         if (mimio->in.urb == NULL || mimio->out.urb == NULL) {
451                 dev_err(&udev->dev, "usb_alloc_urb failure.\n");
452                 mimio_dealloc(mimio);
453                 return -ENOMEM;
454         }
455
456         /*
457          * Build the input urb.
458          */
459         pipe = usb_rcvintpipe(udev, mimio->in.desc->bEndpointAddress);
460         maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
461         if (maxp > MIMIO_MAXPAYLOAD)
462                 maxp = MIMIO_MAXPAYLOAD;
463         usb_fill_int_urb(mimio->in.urb, udev, pipe, mimio->in.buf, maxp,
464                          mimio_irq_in, mimio, mimio->in.desc->bInterval);
465         mimio->in.urb->transfer_dma = mimio->in.dma;
466         mimio->in.urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
467
468         /*
469          * Build the output urb.
470          */
471         pipe = usb_sndintpipe(udev, mimio->out.desc->bEndpointAddress);
472         maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
473         if (maxp > MIMIO_MAXPAYLOAD)
474                 maxp = MIMIO_MAXPAYLOAD;
475         usb_fill_int_urb(mimio->out.urb, udev, pipe, mimio->out.buf, maxp,
476                          mimio_irq_out, mimio, mimio->out.desc->bInterval);
477         mimio->out.urb->transfer_dma = mimio->out.dma;
478         mimio->out.urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
479
480         /*
481          * Build input device info
482          */
483         usb_make_path(udev, path, 64);
484         snprintf(mimio->phys, MIMIO_MAXNAMELEN, "%s/input0", path);
485         input_set_drvdata(input_dev, mimio);
486         /* input_dev->dev = &ifc->dev; */
487         input_dev->open = mimio_open;
488         input_dev->close = mimio_close;
489         input_dev->name = mimio_name;
490         input_dev->phys = mimio->phys;
491         input_dev->dev.parent = &ifc->dev;
492
493         input_dev->id.bustype = BUS_USB;
494         input_dev->id.vendor = le16_to_cpu(udev->descriptor.idVendor);
495         input_dev->id.product = le16_to_cpu(udev->descriptor.idProduct);
496         input_dev->id.version = le16_to_cpu(udev->descriptor.bcdDevice);
497
498         input_dev->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS);
499         for (i = BTN_TOOL_PEN; i <= LOCALBTN_TOOL_EXTRA2; ++i)
500                 set_bit(i, input_dev->keybit);
501
502         input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) |
503                                                  BIT_MASK(BTN_1) |
504                                                  BIT_MASK(BTN_2) |
505                                                  BIT_MASK(BTN_3) |
506                                                  BIT_MASK(BTN_4) |
507                                                  BIT_MASK(BTN_5);
508         /*   input_dev->keybit[BTN_MOUSE] |= BIT(BTN_LEFT); */
509         input_dev->absbit[0] |= BIT_MASK(ABS_X) | BIT_MASK(ABS_Y);
510         input_set_abs_params(input_dev, ABS_X, 0, MIMIO_XRANGE_MAX, 0, 0);
511         input_set_abs_params(input_dev, ABS_Y, 0, MIMIO_YRANGE_MAX, 0, 0);
512         input_dev->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
513
514         /*
515          * Register the input device.
516          */
517         res = input_register_device(mimio->idev);
518         if (res) {
519                 dev_err(&udev->dev, "input_register_device failure (%d)\n",
520                         res);
521                 mimio_dealloc(mimio);
522                 return -EIO;
523         }
524         dev_dbg(&mimio->idev->dev, "input: %s on %s (res = %d).\n",
525                 input_dev->name, input_dev->phys, res);
526
527         usb_set_intfdata(ifc, mimio);
528         mimio->present = 1;
529
530         /*
531          * Submit the input urb to the usb subsystem.
532          */
533         mimio->in.urb->dev = mimio->udev;
534         res = usb_submit_urb(mimio->in.urb, GFP_KERNEL);
535         if (res) {
536                 dev_err(&mimio->idev->dev, "usb_submit_urb failure (%d)\n",
537                         res);
538                 mimio_dealloc(mimio);
539                 return -EIO;
540         }
541
542         /*
543          * Attempt to greet the mimio after giving
544          * it some post-init settling time.
545          *
546          * note: sometimes this sleep interval isn't
547          * long enough to permit the device to re-init
548          * after a hot-swap; maybe need to bump it up.
549          *
550          * As it is, this probably breaks module unloading support!
551          */
552         msleep(1024);
553
554         res = mimio_greet(mimio);
555         if (res == 0) {
556                 dev_dbg(&mimio->idev->dev, "Mimio greeted OK.\n");
557                 mimio->greeted = 1;
558                 mimio->rxhandler = mimio_rx_handler;
559         } else {
560                 dev_dbg(&mimio->idev->dev, "Mimio greet Failure (%d)\n", res);
561         }
562
563         return 0;
564 }
565
566 static int handle_mimio_rx_penupdown(struct mimio *mimio,
567                                      int down,
568                                      const char *const instr[],
569                                      const int instr_ofst[])
570 {
571         int penid, x;
572         if (mimio->pktbuf.q - mimio->pktbuf.p < (down ? 4 : 3))
573                 return 1;               /* partial pkt */
574
575         if (down) {
576                 x = *mimio->pktbuf.p ^ *(mimio->pktbuf.p + 1) ^
577                         *(mimio->pktbuf.p + 2);
578                 if (x != *(mimio->pktbuf.p + 3)) {
579                         dev_dbg(&mimio->idev->dev, "EV_PEN%s: bad xsum.\n",
580                                 down ? "DOWN" : "UP");
581                         /* skip this event data */
582                         mimio->pktbuf.p += 4;
583                         /* decode any remaining events */
584                         return 0;
585                 }
586                 penid = mimio->pktbuf.instr = *(mimio->pktbuf.p + 2);
587                 if (penid > MIMIO_PEN_MAX) {
588                         dev_dbg(&mimio->idev->dev,
589                                 "Unmapped penID (not in [0, %d]): %d\n",
590                                 MIMIO_PEN_MAX, (int)mimio->pktbuf.instr);
591                         penid = mimio->pktbuf.instr = 0;
592                 }
593                 mimio->last_pen_down = penid;
594         } else {
595                 penid = mimio->last_pen_down;
596         }
597         dev_dbg(&mimio->idev->dev, "%s (id %d, code %d) %s.\n", instr[penid],
598                 instr_ofst[penid], penid, down ? "down" : "up");
599
600         if (instr_ofst[penid] >= 0) {
601                 int code = BTN_TOOL_PEN + instr_ofst[penid];
602                 int value = down ? DOWNVALUE : UPVALUE;
603                 if (code > KEY_MAX)
604                         dev_dbg(&mimio->idev->dev, "input_event will ignore "
605                                 "-- code (%d) > KEY_MAX\n", code);
606                 if (!test_bit(code, mimio->idev->keybit))
607                         dev_dbg(&mimio->idev->dev, "input_event will ignore "
608                                 "-- bit for code (%d) not enabled\n", code);
609                 if (!!test_bit(code, mimio->idev->key) == value)
610                         dev_dbg(&mimio->idev->dev, "input_event will ignore "
611                                 "-- bit for code (%d) already set to %d\n",
612                                 code, value);
613                 if (value != DOWNVALUE) {
614                         /* input_regs(mimio->idev, regs); */
615                         input_report_key(mimio->idev, code, value);
616                         input_sync(mimio->idev);
617                 } else {
618                         /* wait until we get some coordinates */
619                 }
620         } else {
621                 dev_dbg(&mimio->idev->dev, "penID offset[%d] == %d is < 0 "
622                         "- not sending\n", penid, instr_ofst[penid]);
623         }
624         mimio->pktbuf.p += down ? 4 : 3; /* 3 for up, 4 for down */
625         return 0;
626 }
627
628 /*
629  * Stay tuned for partial-packet excitement.
630  *
631  * This routine buffers data packets received from the mimio device
632  * in the mimio's data space.  This buffering is necessary because
633  * the mimio's in endpoint can serve us partial packets of data, and
634  * we want the driver to support the servicing of multiple mimios.
635  * Empirical evidence gathered so far suggests that the method of
636  * buffering packet data in the mimio's data space works.  Previous
637  * versions of this driver did not buffer packet data in each mimio's
638  * data-space, and were therefore not able to service multiple mimios.
639  * Note that since the caller of this routine is running in interrupt
640  * context, care needs to be taken to ensure that this routine does not
641  * become bloated, and it may be that another spinlock is needed in each
642  * mimio to guard the buffered packet data properly.
643  */
644 static void mimio_rx_handler(struct mimio *mimio,
645                              unsigned char *data,
646                              unsigned int nbytes)
647 {
648         struct device *dev = &mimio->idev->dev;
649         unsigned int x;
650         unsigned int y;
651         static const char * const instr[] = {
652                 "?0",
653                 "black pen", "blue pen", "green pen", "red pen",
654                 "brown pen", "orange pen", "purple pen", "yellow pen",
655                 "big eraser", "lil eraser",
656                 "?11", "?12", "?13", "?14", "?15", "?16",
657                 "mimio interactive", "interactive button1",
658                 "interactive button2"
659         };
660
661         /* Mimio Interactive gives:
662          * down: [0x22 0x01 0x11 0x32 0x24]
663          * b1  : [0x22 0x01 0x12 0x31 0x24]
664          * b2  : [0x22 0x01 0x13 0x30 0x24]
665          */
666         static const int instr_ofst[] = {
667                 -1,
668                 0, 1, 2, 3,
669                 9, 9, 9, 9,
670                 4, 5,
671                 -1, -1, -1, -1, -1, -1,
672                 6, 7, 8,
673         };
674
675         memcpy(mimio->pktbuf.q, data, nbytes);
676         mimio->pktbuf.q += nbytes;
677
678         while (mimio->pktbuf.p < mimio->pktbuf.q) {
679                 int t = *mimio->pktbuf.p;
680                 switch (t) {
681                 case MIMIO_EV_PENUP:
682                 case MIMIO_EV_PENDOWN:
683                         if (handle_mimio_rx_penupdown(mimio,
684                                                       t == MIMIO_EV_PENDOWN,
685                                                       instr, instr_ofst))
686                                 return; /* partial packet */
687                         break;
688
689                 case MIMIO_EV_PENDATA:
690                         if (mimio->pktbuf.q - mimio->pktbuf.p < 6)
691                                 /* partial pkt */
692                                 return;
693                         x = *mimio->pktbuf.p ^ *(mimio->pktbuf.p + 1) ^
694                                 *(mimio->pktbuf.p + 2) ^
695                                 *(mimio->pktbuf.p + 3) ^
696                                 *(mimio->pktbuf.p + 4);
697                         if (x != *(mimio->pktbuf.p + 5)) {
698                                 dev_dbg(dev, "EV_PENDATA: bad xsum.\n");
699                                 mimio->pktbuf.p += 6; /* skip this event data */
700                                 break; /* decode any remaining events */
701                         }
702                         x = *(mimio->pktbuf.p + 1);
703                         x <<= 8;
704                         x |= *(mimio->pktbuf.p + 2);
705                         y = *(mimio->pktbuf.p + 3);
706                         y <<= 8;
707                         y |= *(mimio->pktbuf.p + 4);
708                         dev_dbg(dev, "coord: (%d, %d)\n", x, y);
709                         if (instr_ofst[mimio->pktbuf.instr] >= 0) {
710                                 int code = BTN_TOOL_PEN +
711                                            instr_ofst[mimio->last_pen_down];
712                                 /* input_regs(mimio->idev, regs); */
713                                 input_report_abs(mimio->idev, ABS_X, x);
714                                 input_report_abs(mimio->idev, ABS_Y, y);
715                                 /* fake a penup */
716                                 change_bit(code, mimio->idev->key);
717                                 input_report_key(mimio->idev,
718                                                  code,
719                                                  DOWNVALUE);
720                                 /* always sync here */
721                                 mimio->idev->sync = 0;
722                                 input_sync(mimio->idev);
723                         }
724                         mimio->pktbuf.p += 6;
725                         break;
726                 case MIMIO_EV_MEMRESET:
727                         if (mimio->pktbuf.q - mimio->pktbuf.p < 7)
728                                 /* partial pkt */
729                                 return;
730                         dev_dbg(dev, "mem-reset.\n");
731                         /* input_regs(mimio->idev, regs); */
732                         input_event(mimio->idev, EV_KEY, BTN_0, 1);
733                         input_event(mimio->idev, EV_KEY, BTN_0, 0);
734                         input_sync(mimio->idev);
735                         mimio->pktbuf.p += 7;
736                         break;
737                 case MIMIO_EV_ACC:
738                         if (mimio->pktbuf.q - mimio->pktbuf.p < 4)
739                                 /* partial pkt */
740                                 return;
741                         x = *mimio->pktbuf.p ^ *(mimio->pktbuf.p + 1) ^
742                                 *(mimio->pktbuf.p + 2);
743                         if (x != *(mimio->pktbuf.p + 3)) {
744                                 dev_dbg(dev, "EV_ACC: bad xsum.\n");
745                                 mimio->pktbuf.p += 4; /* skip this event data */
746                                 break; /* decode any remaining events */
747                         }
748                         switch (*(mimio->pktbuf.p + 2)) {
749                         case ACC_NEWPAGE:
750                                 dev_dbg(&mimio->idev->dev, "new-page.\n");
751                                 /* input_regs(mimio->idev, regs); */
752                                 input_event(mimio->idev, EV_KEY, BTN_1, 1);
753                                 input_event(mimio->idev, EV_KEY, BTN_1, 0);
754                                 input_sync(mimio->idev);
755                                 break;
756                         case ACC_TAGPAGE:
757                                 dev_dbg(&mimio->idev->dev, "tag-page.\n");
758                                 /* input_regs(mimio->idev, regs); */
759                                 input_event(mimio->idev, EV_KEY, BTN_2, 1);
760                                 input_event(mimio->idev, EV_KEY, BTN_2, 0);
761                                 input_sync(mimio->idev);
762                                 break;
763                         case ACC_PRINTPAGE:
764                                 dev_dbg(&mimio->idev->dev, "print-page.\n");
765                                 /* input_regs(mimio->idev, regs);*/
766                                 input_event(mimio->idev, EV_KEY, BTN_3, 1);
767                                 input_event(mimio->idev, EV_KEY, BTN_3, 0);
768                                 input_sync(mimio->idev);
769                                 break;
770                         case ACC_MAXIMIZE:
771                                 dev_dbg(&mimio->idev->dev,
772                                         "maximize-window.\n");
773                                 /* input_regs(mimio->idev, regs); */
774                                 input_event(mimio->idev, EV_KEY, BTN_4, 1);
775                                 input_event(mimio->idev, EV_KEY, BTN_4, 0);
776                                 input_sync(mimio->idev);
777                                 break;
778                         case ACC_FINDCTLPNL:
779                                 dev_dbg(&mimio->idev->dev, "find-ctl-panel.\n");
780                                 /* input_regs(mimio->idev, regs); */
781                                 input_event(mimio->idev, EV_KEY, BTN_5, 1);
782                                 input_event(mimio->idev, EV_KEY, BTN_5, 0);
783                                 input_sync(mimio->idev);
784                                 break;
785                         case ACC_DONE:
786                                 dev_dbg(&mimio->idev->dev, "acc-done.\n");
787                                 /* no event is dispatched to the input
788                                  * subsystem for this device event.
789                                  */
790                                 break;
791                         default:
792                                 dev_dbg(dev, "unknown acc event.\n");
793                                 break;
794                         }
795                         mimio->pktbuf.p += 4;
796                         break;
797                 default:
798                         mimio->pktbuf.p++;
799                         break;
800                 }
801         }
802
803         /*
804          * No partial event was received, so reset mimio's pktbuf ptrs.
805          */
806         mimio->pktbuf.p = mimio->pktbuf.q = mimio->pktbuf.buf;
807 }
808
809 static int mimio_tx(struct mimio *mimio, const char *buf, int nbytes)
810 {
811         int rslt;
812         int timeout;
813         unsigned long flags;
814         DECLARE_WAITQUEUE(wait, current);
815
816         if (!(isvalidtxsize(nbytes))) {
817                 dev_err(&mimio->idev->dev, "invalid arg: nbytes: %d.\n",
818                         nbytes);
819                 return -EINVAL;
820         }
821
822         /*
823          * Init the out urb and copy the data to send.
824          */
825         mimio->out.urb->dev = mimio->udev;
826         mimio->out.urb->transfer_buffer_length = nbytes;
827         memcpy(mimio->out.urb->transfer_buffer, buf, nbytes);
828
829         /*
830          * Send the data.
831          */
832         spin_lock_irqsave(&mimio->txlock, flags);
833         mimio->txflags = MIMIO_TXWAIT;
834         rslt = usb_submit_urb(mimio->out.urb, GFP_ATOMIC);
835         spin_unlock_irqrestore(&mimio->txlock, flags);
836         dev_dbg(&mimio->idev->dev, "rslt: %d.\n", rslt);
837
838         if (rslt) {
839                 dev_err(&mimio->idev->dev, "usb_submit_urb failure: %d.\n",
840                         rslt);
841                 return rslt;
842         }
843
844         /*
845          * Wait for completion to be signalled (the mimio_irq_out
846          * completion routine will or MIMIO_TXDONE in with txflags).
847          */
848         timeout = HZ;
849         set_current_state(TASK_INTERRUPTIBLE);
850         add_wait_queue(&mimio->waitq, &wait);
851
852         while (timeout && ((mimio->txflags & MIMIO_TXDONE) == 0)) {
853                 timeout = schedule_timeout(timeout);
854                 rmb();
855         }
856
857         if ((mimio->txflags & MIMIO_TXDONE) == 0)
858                 dev_dbg(&mimio->idev->dev, "tx timed out.\n");
859
860         /*
861          * Now that completion has been signalled,
862          * unlink the urb so that it can be recycled.
863          */
864         set_current_state(TASK_RUNNING);
865         remove_wait_queue(&mimio->waitq, &wait);
866         usb_unlink_urb(mimio->out.urb);
867
868         return rslt;
869 }
870
871 static int __init mimio_init(void)
872 {
873         int rslt;
874
875         rslt = usb_register(&mimio_driver);
876         if (rslt != 0) {
877                 err("%s: usb_register failure: %d", __func__, rslt);
878                 return rslt;
879         }
880
881         printk(KERN_INFO KBUILD_MODNAME ":"
882                DRIVER_DESC " " DRIVER_VERSION "\n");
883         return rslt;
884 }
885
886 static void __exit mimio_exit(void)
887 {
888         usb_deregister(&mimio_driver);
889 }
890
891 module_init(mimio_init);
892 module_exit(mimio_exit);
893
894 MODULE_AUTHOR(DRIVER_AUTHOR);
895 MODULE_DESCRIPTION(DRIVER_DESC);
896 MODULE_LICENSE("GPL");