1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Logitech Mouse Module - Streams
*/
#include "sys/param.h"
#include "sys/types.h"
#include "sys/kmem.h"
#include "sys/signal.h"
#include "sys/errno.h"
#include "sys/file.h"
#include "sys/termio.h"
#include "sys/stream.h"
#include "sys/stropts.h"
#include "sys/strtty.h"
#include "sys/debug.h"
#include "sys/ddi.h"
#include "sys/cred.h"
#include <sys/stat.h>
#include <sys/sunddi.h>
#include "sys/proc.h"
#include "sys/cmn_err.h"
#include "sys/mouse.h"
#include "sys/mse.h"
#define PRF printf
struct mouseconfig mse_config = {
0, 0
};
/*
*
* Local Static Data
*
*/
#define LOGI_MAXUNIT 1
#define LOGIUNIT(dev) ((dev) & 0xf)
#define M_OPEN 1
static dev_info_t *logiunits[LOGI_MAXUNIT];
static struct driver_minor_data {
char *name;
int minor;
int type;
} logi_minor_data[] = {
{"l", 0, S_IFCHR},
{0}
};
int logidevflag = 1;
/*
* static struct strmseinfo *logiptr = 0;
*/
static unsigned BASE_IOA = 0x23C; /* Set to base I/O addr of bus mouse */
static uint_t logiintr();
static int logiopen(queue_t *q, dev_t *devp, int flag, int sflag,
cred_t *cred_p);
static int logiclose(queue_t *q, int flag, cred_t *cred_p);
static int logi_wput(queue_t *q, mblk_t *mp);
static int logiinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
void **result);
static int logiprobe(dev_info_t *dev);
static int logiattach(dev_info_t *dev, ddi_attach_cmd_t cmd);
static int logidetach(dev_info_t *dev, ddi_detach_cmd_t cmd);
static int logiinit(dev_info_t *dip);
struct module_info logiminfo = { 23, "logi", 0, INFPSZ, 256, 128};
static struct qinit logi_rinit = {
NULL, NULL, logiopen, logiclose, NULL, &logiminfo, NULL};
static struct qinit logi_winit = {
logi_wput, NULL, NULL, NULL, NULL, &logiminfo, NULL};
struct streamtab logi_info = { &logi_rinit, &logi_winit, NULL, NULL};
static int xmotion, /* current position and .. */
ymotion, /* button status, used .. */
oldbuttons = 1,
status; /* for DOS mode. */
extern int nulldev(), nodev();
/*
* Local Function Declarations
*/
struct cb_ops logi_cb_ops = {
nodev, /* open */
nodev, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
nodev, /* read */
nodev, /* write */
nodev, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
(&logi_info), /* streamtab */
D_NEW | D_MP | D_MTPERMOD /* Driver compatibility flag */
};
struct dev_ops logi_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
logiinfo, /* info */
nulldev, /* identify */
logiprobe, /* probe */
logiattach, /* attach */
logidetach, /* detach */
nodev, /* reset */
&logi_cb_ops, /* driver operations */
(struct bus_ops *)0 /* bus operations */
};
#ifndef BUILD_STATIC
/*
* This is the loadable module wrapper.
*/
#include <sys/modctl.h>
extern struct mod_ops mod_driverops;
/*
* Module linkage information for the kernel.
*/
static struct modldrv modldrv = {
&mod_driverops, /* Type of module. This one is a driver */
"Logitech Mouse driver %I%",
&logi_ops, /* driver ops */
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
/*
* This is the driver initialization routine.
*/
int
_init(void)
{
int rv;
rv = mod_install(&modlinkage);
return (rv);
}
#ifdef SUNDEV
int
_fini(void)
{
return (EBUSY);
}
#else
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
#endif
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
#endif /* !BUILD_STATIC */
static int
logiprobe(dev_info_t *dip)
{
int unit;
#ifdef LOGI_DEBUG
if (logi_debug) {
PRF("logiprobe: entry\n");
}
#endif
unit = ddi_get_instance(dip);
#ifdef LOGI_DEBUG
if (logi_debug)
PRF("unit is %x\n", unit);
#endif
if (unit >= LOGI_MAXUNIT || logiunits[unit])
return (DDI_PROBE_FAILURE);
return (logiinit(dip));
}
/*ARGSUSED*/
static int
logiattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int unit;
struct driver_minor_data *dmdp;
struct strmseinfo *logiptr = 0;
#ifdef LOGI_DEBUG
if (logi_debug) {
PRF("logiattach entry\n");
}
#endif
switch (cmd) {
case DDI_ATTACH:
unit = ddi_get_instance(dip);
for (dmdp = logi_minor_data; dmdp->name != NULL; dmdp++) {
if (ddi_create_minor_node(dip, dmdp->name, dmdp->type,
dmdp->minor, DDI_PSEUDO, NULL) == DDI_FAILURE) {
ddi_remove_minor_node(dip, NULL);
ddi_prop_remove_all(dip);
#ifdef LOGI_DEBUG
if (logi_debug)
PRF("logiattach: "
"ddi_create_minor_node failed\n");
#endif
return (DDI_FAILURE);
}
}
logiunits[unit] = dip;
/* allocate and initialize state structure */
logiptr = kmem_zalloc(sizeof (struct strmseinfo), KM_SLEEP);
logiptr->state = 0; /* not opened */
ddi_set_driver_private(dip, logiptr);
if (ddi_add_intr(dip, (uint_t)0, &logiptr->iblock,
(ddi_idevice_cookie_t *)0, logiintr,
(caddr_t)logiptr) != DDI_SUCCESS) {
#ifdef LOGI_DEBUG
if (logi_debug)
PRF("logiattach: ddi_add_intr failed\n");
#endif
cmn_err(CE_WARN, "logi: cannot add intr\n");
return (DDI_FAILURE);
}
mutex_init(&logiptr->lock, NULL, MUTEX_DRIVER,
(void *)logiptr->iblock);
ddi_report_dev(dip);
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*
* logidetach:
*/
/*ARGSUSED*/
static int
logidetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
dev_info_t *ldevi;
struct strmseinfo *logiptr;
int instance;
switch (cmd) {
case DDI_DETACH:
/*
* check if every instance can be unloaded before actually
* starting to unload this one; this prevents the needless
* detach/re-attach sequence
*/
for (instance = 0; instance < LOGI_MAXUNIT; instance++) {
if (((ldevi = logiunits[instance]) == NULL) ||
(logiptr = ddi_get_driver_private(ldevi)) == NULL)
continue;
}
/*
* Undo what we did in logiattach & logiprobe, freeing resources
* and removing things we installed. The system
* framework guarantees we are not active with this devinfo
* node in any other entry points at this time.
*/
instance = ddi_get_instance(dip);
if ((instance >= LOGI_MAXUNIT) ||
(logiptr = ddi_get_driver_private(dip)) == NULL)
return (DDI_FAILURE);
logiunits[instance] = 0;
ddi_prop_remove_all(dip);
ddi_remove_minor_node(dip, NULL);
mutex_destroy(&logiptr->lock);
ddi_remove_intr(dip, 0, logiptr->iblock);
kmem_free(logiptr, sizeof (struct strmseinfo));
return (DDI_SUCCESS);
default:
#ifdef LOGI_DEBUG
if (logi_debug) {
PRF("logidetach: cmd = %d unknown\n", cmd);
}
#endif
return (DDI_FAILURE);
}
}
/* ARGSUSED */
static int
logiinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
dev_t dev = (dev_t)arg;
int unit;
dev_info_t *devi;
#ifdef LOGI_DEBUG
if (logi_debug)
PRF("logiinfo: call\n");
#endif
if (((unit = LOGIUNIT(dev)) >= LOGI_MAXUNIT) ||
(devi = logiunits[unit]) == NULL)
return (DDI_FAILURE);
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
*result = (void *)devi;
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)(uintptr_t)unit;
break;
default:
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
logiinit(dev_info_t *dip)
{
int i;
int ioaddr;
int len;
int old_probe;
#ifdef LOGI_DEBUG
if (logi_debug)
PRF("logiinit: call BASE_IOA = %x\n", BASE_IOA);
#endif
old_probe = ddi_getprop(DDI_DEV_T_ANY, dip, 0,
"ignore-hardware-nodes", 0);
if (old_probe) {
len = sizeof (int);
/*
* check if ioaddr is set in .conf file, it should be. If it
* isn't then try the default i/o addr
*/
if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
DDI_PROP_DONTPASS, "ioaddr", (caddr_t)&ioaddr,
&len) == DDI_PROP_SUCCESS)
BASE_IOA = ioaddr;
} else {
int reglen, nregs;
int i;
struct {
int bustype;
int base;
int size;
} *reglist;
/* new probe */
if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"reg", (caddr_t)®list, ®len) != DDI_PROP_SUCCESS)
return (DDI_PROBE_FAILURE);
nregs = reglen / sizeof (*reglist);
for (i = 0; i < nregs; i++)
if (reglist[i].bustype == 1) {
ioaddr = reglist[i].base;
BASE_IOA = ioaddr;
break;
}
kmem_free(reglist, reglen);
}
#ifdef LOGI_DEBUG
if (logi_debug)
PRF("logiinit: call BASE_IOA = %x\n", BASE_IOA);
#endif
mse_config.present = 0;
/* Check if the mouse board exists */
outb(CONFIGURATOR_PORT, 0x91);
drv_usecwait(10);
outb(SIGNATURE_PORT, 0xC);
drv_usecwait(10);
i = inb(SIGNATURE_PORT);
drv_usecwait(10);
outb(SIGNATURE_PORT, 0x50);
drv_usecwait(10);
if (i == 0xC && ((inb(SIGNATURE_PORT)) == 0x50)) {
mse_config.present = 1;
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logiinit:Disable interrupts ioaddr %x\n", BASE_IOA);
#endif
control_port(INTR_DISABLE); /* Disable interrupts */
#ifdef LOGI_DEBUG
if (logi_debug)
PRF("logiinit: succeeded\n");
#endif
return (DDI_SUCCESS);
} else {
#ifdef LOGI_DEBUG
if (logi_debug)
PRF("logiinit: failed\n");
#endif
return (DDI_PROBE_FAILURE);
}
}
/*ARGSUSED2*/
static int
logiopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *cred_p)
{
struct strmseinfo *logiptr;
int unit;
dev_info_t *dip;
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logiopen:entered\n");
#endif
if (((unit = LOGIUNIT(*devp)) >= LOGI_MAXUNIT) ||
(dip = logiunits[unit]) == NULL)
return (ENODEV);
if (!mse_config.present)
return (EIO);
if ((logiptr = ddi_get_driver_private(dip)) == NULL)
return (EIO);
if (logiptr->state & M_OPEN)
return (EBUSY);
mutex_enter(&logiptr->lock);
if (q->q_ptr != NULL) {
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logiopen:already open\n");
#endif
mutex_exit(&logiptr->lock);
return (EBUSY); /* already attached */
}
q->q_ptr = (caddr_t)logiptr;
WR(q)->q_ptr = (caddr_t)logiptr;
logiptr->rqp = q;
logiptr->wqp = WR(q);
qprocson(q);
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logiopen:Enable interrupts ioaddr %x\n", BASE_IOA);
#endif
control_port(0); /* Enable interrupts */
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logiopen:leaving\n");
#endif
oldbuttons = 0;
logiptr->state = M_OPEN;
mutex_exit(&logiptr->lock);
return (0);
}
/*ARGSUSED1*/
static int
logiclose(queue_t *q, int flag, cred_t *cred_p)
{
struct strmseinfo *logiptr;
qprocsoff(q);
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logiclose:entered\n");
#endif
logiptr = q->q_ptr;
mutex_enter(&logiptr->lock);
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logiclose:Disable interrupts ioaddr %x\n", BASE_IOA);
#endif
control_port(INTR_DISABLE); /* Disable interrupts */
q->q_ptr = NULL;
WR(q)->q_ptr = NULL;
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logiclose:leaving\n");
#endif
logiptr->state = 0; /* Not opened */
mutex_exit(&logiptr->lock);
return (0);
}
static int
logi_wput(queue_t *q, mblk_t *mp)
{
struct iocblk *iocbp;
struct strmseinfo *logiptr;
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logi_wput:entered\n");
#endif
logiptr = q->q_ptr;
if (logiptr == 0) {
freemsg(mp);
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logi_wput:logiptr == NULL\n");
#endif
return (0);
}
iocbp = (struct iocblk *)mp->b_rptr;
switch (mp->b_datap->db_type) {
case M_FLUSH:
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logi_wput:M_FLUSH\n");
#endif
if (*mp->b_rptr & FLUSHW)
flushq(q, FLUSHDATA);
qreply(q, mp);
break;
case M_IOCTL:
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logi_wput:M_IOCTL\n");
#endif
mp->b_datap->db_type = M_IOCNAK;
iocbp->ioc_rval = 0;
iocbp->ioc_error = EINVAL;
qreply(q, mp);
break;
case M_IOCDATA:
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logi_wput:M_IOCDATA\n");
#endif
mp->b_datap->db_type = M_IOCNAK;
iocbp->ioc_rval = 0;
iocbp->ioc_error = EINVAL;
qreply(q, mp);
break;
default:
freemsg(mp);
break;
}
#ifdef LOGI_DEBUG
if (logi_debug)
printf("logi_wput:leaving\n");
#endif
return (0);
}
static uint_t
logiintr(caddr_t arg)
{
int stat = 0;
char x_hi,
x_lo,
y_hi,
y_lo,
buttons,
x,
y;
struct strmseinfo *qp = (struct strmseinfo *)arg;
mblk_t *bp;
mutex_enter(&qp->lock);
if (!(qp->state & M_OPEN)) {
#ifdef LOGI_DEBUG
if (logi_debug > 5)
printf("logiintr:Enable interrupts ioaddr %x\n", BASE_IOA);
#endif
control_port(INTR_DISABLE); /* disable interrupts */
mutex_exit(&qp->lock);
return (DDI_INTR_UNCLAIMED);
}
/* Get the mouse's status and put it into the appropriate virtual structure */
control_port(INTR_DISABLE | HC | LOW_NIBBLE | X_COUNTER);
x_lo = data_port;
buttons = x_lo & (LEFT|MIDDLE|RIGHT); /* buttons in high nibble */
control_port(INTR_DISABLE | HC | HIGH_NIBBLE | X_COUNTER);
x_hi = data_port;
control_port(INTR_DISABLE | HC | LOW_NIBBLE | Y_COUNTER);
y_lo = data_port;
control_port(INTR_DISABLE | HC | HIGH_NIBBLE | Y_COUNTER);
y_hi = (data_port);
/*
* Piece the coordinate nibbles together.
*/
x = ((x_hi & 0x0f) << 4) | (x_lo & 0x0f);
y = ((y_hi & 0x0f) << 4) | (y_lo & 0x0f);
/* figure button change values */
if ((buttons & LEFT) != (oldbuttons & LEFT))
stat |= BUT1CHNG;
if ((buttons & MIDDLE) != (oldbuttons & MIDDLE))
stat |= BUT2CHNG;
if ((buttons & RIGHT) != (oldbuttons & RIGHT))
stat |= BUT3CHNG;
/* now convert button status values */
/* Note that the bit is 1 if the button is NOT pressed */
if ((buttons & LEFT) == 0)
stat |= BUT1STAT;
if ((buttons & MIDDLE) == 0)
stat |= BUT2STAT;
if ((buttons & RIGHT) == 0)
stat |= BUT3STAT;
/* did movement occur? */
if (x || y)
stat |= MOVEMENT;
/* do we need to deal with PACKETDONE ? */
/* clear old button status, add in new status */
status = (status & ~BUTSTATMASK) | stat;
xmotion += x;
ymotion += y;
if (buttons != oldbuttons || x || y) {
char c;
/*
* Emulate a Mouse Systems Corp. mouse.
* push buttons, x, y, x-delta, y-delta onto clist
*/
c = (char)0x80; /* sync byte pattern = 1000 0LMR */
if (buttons & LEFT) /* not pressed */
c |= 0x04;
if (buttons & MIDDLE)
c |= 0x02;
if (buttons & RIGHT)
c |= 0x01;
if ((bp = allocb(5, BPRI_MED)) == NULL) {
mutex_exit(&qp->lock);
return (DDI_INTR_UNCLAIMED);
}
*(bp->b_wptr)++ = c; /* sync */
*(bp->b_wptr)++ = x; /* x coordinate */
*(bp->b_wptr)++ = -y; /* y coordinate */
*(bp->b_wptr)++ = 0; /* delta x */
*(bp->b_wptr)++ = 0; /* delta y */
putnext(qp->rqp, bp);
}
oldbuttons = buttons;
/* Re-enable interrupts on the mouse and return */
#ifdef LOGI_DEBUG
if (logi_debug > 5)
printf("logiintr:Enable interrupts ioaddr %x\n", BASE_IOA);
#endif
control_port(0);
mutex_exit(&qp->lock);
return (DDI_INTR_UNCLAIMED);
}
|