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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
--- hurd-20040301.orig/hurd/subsystems
+++ hurd-20040301/hurd/subsystems
@@ -37,6 +37,7 @@
pfinet 37000 Internet configuration calls
password 38000 Password checker
<ioctl space> 100000- First subsystem of ioctl class 'f' (lowest class)
+kdioctl 120000 Ioctl class 'k' (kd)
tioctl 156000 Ioctl class 't' (terminals)
tioctl 156200 (continued)
<ioctl space> 164200 Last subsystem of ioctl class 'v' (highest class)
--- hurd-20040301.orig/hurd/kdioctl.defs
+++ hurd-20040301/hurd/kdioctl.defs
@@ -0,0 +1,39 @@
+/* Definitions for kd ioctls
+ Copyright (C) 1991, 1993, 1994, 1995, 1996, 1998 Free Software Foundation, Inc.
+
+This file is part of the GNU Hurd.
+
+The GNU Hurd is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+The GNU Hurd is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with the GNU Hurd; see the file COPYING. If not, write to
+the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <hurd/hurd_types.defs>
+
+/* Ioctl class `k'; the subsystem is derived from calculations in
+ <ioctls.h>. */
+subsystem kdioctl 120000;
+
+import <hurd/ioctl_types.h>;
+
+skip; /* 0 unused */
+
+/* 1 KDSKBDMODE */
+routine kdioctl_kdskbdmode (
+ port: io_t;
+ in mode: int);
+/* 2 KDGKBDMODE */
+routine kdioctl_kdgkbdmode (
+ port: io_t;
+ out mode: int);
+
+/* 3 - 256 unused */
--- hurd-20040301.orig/sutils/MAKEDEV.sh
+++ hurd-20040301/sutils/MAKEDEV.sh
@@ -86,7 +86,7 @@
;;
std)
- mkdev console tty null zero full fd time mem klog shm
+ mkdev console tty null zero full fd time mem klog shm kbd
;;
console|com[0-9])
st $I root 600 /hurd/term ${DEVDIR}/$I device $I;;
@@ -115,6 +115,9 @@
st $I root 660 /hurd/storeio --no-cache mem ;;
klog)
st $I root 660 /hurd/streamio kmsg;;
+ kbd)
+ st $I root 666 /hurd/kbd;;
+
# ptys
[pt]ty[pqrstuvwxyzPQRS]?)
# Make one pty, both the master and slave halves.
--- hurd-20040301.orig/trans/kd.h
+++ hurd-20040301/trans/kd.h
@@ -0,0 +1,72 @@
+
+/*
+ * Format definitions for 'ioctl' commands in device definitions.
+ *
+ * From BSD4.4.
+ */
+/*
+ * Ioctl's have the command encoded in the lower word, and the size of
+ * any in or out parameters in the upper word. The high 3 bits of the
+ * upper word are used to encode the in/out status of the parameter.
+ */
+#define MACH_IOCPARM_MASK 0x1fff /* parameter length, at most 13 bits */
+#define MACH_IOC_VOID 0x20000000 /* no parameters */
+#define MACH_IOC_OUT 0x40000000 /* copy out parameters */
+#define MACH_IOC_IN 0x80000000U /* copy in parameters */
+#define MACH_IOC_INOUT (MACH_IOC_IN|MACH_IOC_OUT)
+
+#define _MACH_IOC(inout,group,num,len) \
+ (inout | ((len & MACH_IOCPARM_MASK) << 16) | ((group) << 8) | (num))
+#define _MACH_IO(g,n) _MACH_IOC(MACH_IOC_VOID, (g), (n), 0)
+#define _MACH_IOR(g,n,t) _MACH_IOC(MACH_IOC_OUT, (g), (n), sizeof(t))
+#define _MACH_IOW(g,n,t) _MACH_IOC(MACH_IOC_IN, (g), (n), sizeof(t))
+#define _MACH_IOWR(g,n,t) _MACH_IOC(MACH_IOC_INOUT, (g), (n), sizeof(t))
+
+/* taken from gnumach-1.1.3/i386/i386at/kd.h */
+
+#define KDSKBDMODE _MACH_IOW('K', 1, int) /* set keyboard mode */
+#define KB_EVENT 1
+#define KB_ASCII 2
+
+#define KDGKBDTYPE _MACH_IOR('K', 2, int) /* get keyboard type */
+
+typedef unsigned short kev_type; /* kd event type */
+typedef unsigned char Scancode;
+/* (used for event records) */
+struct mouse_motion {
+ short mm_deltaX; /* units? */
+ short mm_deltaY;
+};
+
+typedef struct {
+ kev_type type; /* see below */
+ struct timeval time; /* timestamp */
+ union { /* value associated with event */
+ boolean_t up; /* MOUSE_LEFT .. MOUSE_RIGHT */
+ Scancode sc; /* KEYBD_EVENT */
+ struct mouse_motion mmotion; /* MOUSE_MOTION */
+ } value;
+} kd_event;
+
+
+/*
+ * kd_event ID's.
+ */
+#define MOUSE_LEFT 1 /* mouse left button up/down */
+#define MOUSE_MIDDLE 2
+#define MOUSE_RIGHT 3
+#define MOUSE_MOTION 4 /* mouse motion */
+#define KEYBD_EVENT 5 /* key up/down */
+
+/*
+ * Low 3 bits of minor are the com port #.
+ * The high 5 bits of minor are the mouse type
+ */
+#define MOUSE_SYSTEM_MOUSE 0
+#define MICROSOFT_MOUSE 1
+#define IBM_MOUSE 2
+#define NO_MOUSE 3
+#define LOGITECH_TRACKMAN 4
+#define MICROSOFT_MOUSE7 5
+
+
--- hurd-20040301.orig/trans/Makefile
+++ hurd-20040301/trans/Makefile
@@ -19,13 +19,15 @@
makemode := servers
targets = symlink firmlink ifsock magic null fifo new-fifo fwd crash \
- password hello hello-mt streamio fakeroot proxy-defpager
+ password hello hello-mt streamio fakeroot proxy-defpager \
+ kbd mouse
SRCS = ifsock.c symlink.c magic.c null.c fifo.c new-fifo.c fwd.c \
crash.c firmlink.c password.c hello.c hello-mt.c streamio.c \
- fakeroot.c proxy-defpager.c
+ fakeroot.c proxy-defpager.c kbd.c mouse.c
OBJS = $(SRCS:.c=.o) fsysServer.o ifsockServer.o passwordServer.o \
crashServer.o crash_replyUser.o msgServer.o \
default_pagerServer.o default_pagerUser.o \
+ kdioctlServer.o \
device_replyServer.o elfcore.o
HURDLIBS = threads ports trivfs fshelp pipe ihash shouldbeinlibc
password-LDLIBS = $(LIBCRYPT)
@@ -40,6 +42,7 @@
symlink: fsysServer.o
ifsock: ifsockServer.o
+kbd: kdioctlServer.o
crash: crashServer.o crash_replyUser.o msgServer.o elfcore.o
password: passwordServer.o
streamio: device_replyServer.o
@@ -48,7 +51,7 @@
proxy-defpager crash password streamio: ../libthreads/libthreads.a ../libports/libports.a ../libtrivfs/libtrivfs.a ../libthreads/libthreads.a ../libfshelp/libfshelp.a
fifo new-fifo: ../libpipe/libpipe.a
fwd: ../libfshelp/libfshelp.a ../libports/libports.a
-hello-mt magic null ifsock fifo new-fifo firmlink: ../libtrivfs/libtrivfs.a ../libfshelp/libfshelp.a ../libthreads/libthreads.a ../libports/libports.a ../libihash/libihash.a
+kbd mouse hello-mt magic null ifsock fifo new-fifo firmlink: ../libtrivfs/libtrivfs.a ../libfshelp/libfshelp.a ../libthreads/libthreads.a ../libports/libports.a ../libihash/libihash.a
hello: ../libtrivfs/libtrivfs.a ../libfshelp/libfshelp.a ../libports/libports.a ../libihash/libihash.a
fakeroot: ../libnetfs/libnetfs.a ../libfshelp/libfshelp.a ../libiohelp/libiohelp.a ../libthreads/libthreads.a ../libports/libports.a ../libihash/libihash.a
$(targets): ../libshouldbeinlibc/libshouldbeinlibc.a
--- hurd-20040301.orig/trans/kbd.c
+++ hurd-20040301/trans/kbd.c
@@ -0,0 +1,306 @@
+/* A translator for kbd
+
+ Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
+
+ Written by UCHIYAMA Yasushi <uch@nop.or.jp>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <hurd.h>
+#include <hurd/ports.h>
+#include <hurd/trivfs.h>
+#include <hurd/fsys.h>
+#include <version.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <error.h>
+#include <string.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <argp.h>
+
+
+#include <device/device.h>
+#include "kdioctl_S.h"
+#include "kd.h"
+
+#define KBD_BUF_SIZE 1
+static struct kbd_buffer
+{
+ mach_port_t port;
+ struct mutex lock;
+ struct condition fill;
+ struct condition notfull;
+ kd_event buf[KBD_BUF_SIZE];
+ int cnt;
+ int in;
+ int out;
+} kbd;
+
+static int
+demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp)
+{
+ extern int kdioctl_server (mach_msg_header_t *, mach_msg_header_t *);
+
+ return (trivfs_demuxer (inp, outp)
+ || kdioctl_server (inp, outp));
+}
+
+static any_t
+kbd_thread (any_t arg)
+{
+ for(;;)
+ {
+ kd_event *event;
+ vm_offset_t buf;
+ mach_msg_type_number_t buf_size;
+ int ncopy;
+ kern_return_t err;
+
+ err = device_read (kbd.port, 0, 0, sizeof (kd_event), (char **)&buf, &buf_size);
+ if (err)
+ return err;
+
+ mutex_lock (&kbd.lock);
+ while (kbd.cnt == KBD_BUF_SIZE)
+ condition_wait (&kbd.notfull, &kbd.lock);
+ ncopy = buf_size / sizeof (kd_event);
+ if (KBD_BUF_SIZE - kbd.cnt < ncopy)
+ ncopy = KBD_BUF_SIZE - kbd.cnt;
+ event = (kd_event*)buf;
+ while (ncopy)
+ {
+ kbd.buf[kbd.in] = *event++;
+ kbd.in = (kbd.in + 1) % KBD_BUF_SIZE;
+ kbd.cnt++;
+ ncopy--;
+ }
+ vm_deallocate (mach_task_self(), buf, buf_size);
+ condition_broadcast (&kbd.fill);
+ mutex_unlock (&kbd.lock);
+ }
+}
+
+/**********************************************************************
+ *open(2), close(2)
+ **********************************************************************/
+static error_t
+open_hook (struct trivfs_peropen *po)
+{
+ kern_return_t err = 0;
+ mach_port_t master;
+
+ mutex_lock (&kbd.lock);
+
+ if (po->openmodes & O_READ)
+ {
+ if( kbd.port != MACH_PORT_NULL)
+ return EBUSY;
+ err = get_privileged_ports (NULL, &master);
+ if (err)
+ return err;
+ err = device_open (master, D_READ, "kbd" , &kbd.port);
+ mach_port_deallocate (mach_task_self(), master);
+ }
+ kbd.cnt = 0;
+ cthread_detach (cthread_fork (kbd_thread, 0));
+ mutex_unlock (&kbd.lock);
+ return err;
+}
+
+static void
+close_hook (struct trivfs_peropen *po)
+{
+ int cnt = 1;
+ int mode = KB_ASCII;
+
+ device_set_status (kbd.port, KDSKBDMODE, (dev_status_t)&mode, cnt);
+ device_close (kbd.port);
+ kbd.port = MACH_PORT_NULL;
+}
+
+/* Trivfs hooks */
+
+error_t (*trivfs_peropen_create_hook) (struct trivfs_peropen *) = open_hook;
+void (*trivfs_peropen_destroy_hook) (struct trivfs_peropen *) = close_hook;
+
+int
+main (int argc, char **argv)
+{
+ error_t err;
+ mach_port_t bootstrap;
+ struct trivfs_control *fsys;
+
+ task_get_bootstrap_port (mach_task_self (), &bootstrap);
+ if (bootstrap == MACH_PORT_NULL)
+ error(1, 0, "Must be started as a translator");
+
+ /* Reply to our parent */
+ err = trivfs_startup (bootstrap, 0, 0, 0, 0, 0, &fsys);
+ if (err)
+ error(3, err, "Contacting parent");
+ /* Launch. */
+ mutex_init( &kbd.lock );
+ ports_manage_port_operations_multithread (fsys->pi.bucket,demuxer,2 * 60 * 1000, 0, 0);
+
+ exit(0);
+}
+
+/* Trivfs hooks */
+
+int trivfs_fstype = FSTYPE_DEV;
+int trivfs_fsid = 0;
+
+int trivfs_support_read = 1;
+int trivfs_support_write = 0;
+int trivfs_support_exec = 0;
+
+int trivfs_allow_open = O_READ;
+
+void
+trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
+{
+ st->st_blksize = vm_page_size * 256; /* Make transfers LARRRRRGE */
+
+ st->st_size = 0;
+ st->st_blocks = 0;
+
+ st->st_mode &= ~S_IFMT;
+ st->st_mode |= S_IFCHR;
+}
+
+error_t
+trivfs_goaway (struct trivfs_control *fsys, int flags)
+{
+ exit (0);
+}
+/***********************************************************************
+ * read(2), select(2)
+ ***********************************************************************/
+/* Read data from an IO object. If offset if -1, read from the object
+ maintained file pointer. If the object is not seekable, offset is
+ ignored. The amount desired to be read is in AMT. */
+kern_return_t
+trivfs_S_io_read(struct trivfs_protid *cred,
+ mach_port_t reply, mach_msg_type_name_t replytype,
+ data_t *data,
+ mach_msg_type_number_t *datalen,
+ loff_t offs,
+ vm_size_t amt)
+{
+ int i,n;
+ if (!cred)
+ return EOPNOTSUPP;
+ else if (!(cred->po->openmodes & O_READ))
+ return EBADF;
+
+ mutex_lock (&kbd.lock);
+ while (!kbd.cnt)
+ {
+ if (cred->po->openmodes & O_NONBLOCK)
+ {
+ mutex_unlock (&kbd.lock);
+ return EWOULDBLOCK;
+ }
+ if (hurd_condition_wait (&kbd.fill, &kbd.lock))
+ {
+ mutex_unlock (&kbd.lock);
+ return EINTR;
+ }
+ }
+ if ((n = amt / sizeof (kd_event)) > kbd.cnt)
+ n = kbd.cnt;
+ for (i = 0; i < n ; i++)
+ {
+ (*(kd_event **)data)[i] = kbd.buf[kbd.out];
+ kbd.out = (kbd.out + 1) % KBD_BUF_SIZE;
+ kbd.cnt--;
+ }
+ *datalen = n * sizeof (kd_event);
+ if (n)
+ condition_signal (&kbd.notfull);
+ mutex_unlock (&kbd.lock);
+ return 0;
+}
+
+/* SELECT_TYPE is the bitwise OR of SELECT_READ, SELECT_WRITE, and SELECT_URG.
+ Block until one of the indicated types of i/o can be done "quickly", and
+ return the types that are then available. ID_TAG is returned as passed; it
+ is just for the convenience of the user in matching up reply messages with
+ specific requests sent. */
+
+kern_return_t
+trivfs_S_io_select (struct trivfs_protid *cred,
+ mach_port_t reply, mach_msg_type_name_t replytype,
+ int *type)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+ if(*type & ~SELECT_READ )
+ return EINVAL;
+
+ mutex_lock (&kbd.lock);
+ while (1)
+ {
+ if (kbd.cnt)
+ {
+ *type = SELECT_READ;
+ mutex_unlock (&kbd.lock);
+ return 0;
+ }
+ ports_interrupt_self_on_port_death (cred, reply);
+ if (hurd_condition_wait (&kbd.fill, &kbd.lock))
+ {
+ *type = 0;
+ mutex_unlock (&kbd.lock);
+ return EINTR;
+ }
+ }
+}
+/***********************************************************************
+ * ioctl(2)
+***********************************************************************/
+kern_return_t
+S_kdioctl_kdskbdmode( io_t port,int mode )
+{
+ kern_return_t err;
+ int cnt = 1;
+ err = device_set_status (kbd.port, KDSKBDMODE, (dev_status_t)&mode, cnt);
+ return err;
+}
+
+kern_return_t
+S_kdioctl_kdgkbdmode( io_t port,int *mode )
+{
+ kern_return_t err;
+ int cnt;
+
+ err = device_get_status (kbd.port, KDGKBDTYPE, (dev_status_t)mode, &cnt);
+ return err;
+}
+
+kern_return_t
+S_kdioctl_kdkdsetled( io_t port,int mode )
+{
+ return EOPNOTSUPP; /* XXX */
+}
+
+kern_return_t
+S_kdioctl_kdkdgetled( io_t port,int *mode )
+{
+ return EOPNOTSUPP; /* XXX */
+}
+
--- hurd-20040301.orig/trans/mouse.c
+++ hurd-20040301/trans/mouse.c
@@ -0,0 +1,318 @@
+/* A translator for mouse.
+
+ Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
+
+ Written by UCHIYAMA Yasushi <uch@nop.or.jp>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <hurd.h>
+#include <hurd/ports.h>
+#include <hurd/trivfs.h>
+#include <hurd/fsys.h>
+#include <version.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <error.h>
+#include <string.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <argp.h>
+
+const char *argp_program_version = STANDARD_HURD_VERSION (mouse);
+
+#include <device/device.h>
+#include "kd.h"
+
+static char *arg_mouse_protocol,*arg_mouse_device;
+static char mouse_device_name[16];
+
+#define MOUSE_BUF_SIZE 64
+static struct mouse_buffer
+{
+ mach_port_t port;
+ struct mutex lock;
+ struct condition fill;
+ struct condition notfull;
+ kd_event buf[MOUSE_BUF_SIZE];
+ int cnt;
+ int in;
+ int out;
+} mouse;
+
+/* mouse options. Used for startup */
+static const struct argp_option options[] =
+{
+ {"protocol", 'p' ,"PROTOCOL", 0, "mouse PROTOCOL(ps/2,microsoft etc..."},
+ {"device", 'd' ,"DEVICE" , 0, "mouse DEVICE (com0,com1,none)"},
+ {0,0}
+};
+
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ switch (key)
+ {
+ case 'p': arg_mouse_protocol = arg ; break;
+ case 'd': arg_mouse_device = arg ; break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+static int
+set_mouse_device_name()
+{
+ /* See gnumach/i386/i386at/kd_mouse.c */
+ struct {
+ int type;
+ char *name;
+ } mouse_tab[] = {
+ { MOUSE_SYSTEM_MOUSE, "mousesystem"},
+ { MICROSOFT_MOUSE, "microsoft" },
+ { IBM_MOUSE, "ps/2" },
+ { NO_MOUSE, "nomouse" },
+ { LOGITECH_TRACKMAN, "logitech" },
+ { MICROSOFT_MOUSE7, "mouse7" },
+ };
+ int i,n = sizeof mouse_tab / sizeof (mouse_tab[0]);
+ for (i = 0; i < n; i++)
+ if (!strcmp (mouse_tab[i].name, arg_mouse_protocol))
+ {
+ int type, unit, device_minor;
+ type = mouse_tab[i].type;
+ if (arg_mouse_device && !strcmp (arg_mouse_device, "com1"))
+ unit = 1;
+ else
+ unit = 0;
+ device_minor = 0;
+ device_minor |= (type<<3);
+ device_minor |= unit;
+ device_minor = device_minor % 100;
+
+ sprintf (mouse_device_name, "mouse%d", device_minor);
+ return 1;
+ }
+ return 0;
+}
+
+int
+main (int argc, char **argv)
+{
+ error_t err;
+ mach_port_t bootstrap;
+ struct trivfs_control *fsys;
+
+ const struct argp argp = { options , parse_opt };
+
+ if(argp_parse (&argp, argc, argv, 0, 0, 0) )
+ error(1, 0, "argp_parse failed.");
+
+ set_mouse_device_name();
+
+ task_get_bootstrap_port (mach_task_self (), &bootstrap);
+ if (bootstrap == MACH_PORT_NULL)
+ error(1, 0, "Must be started as a translator");
+
+ /* Reply to our parent */
+ err = trivfs_startup (bootstrap, 0, 0, 0, 0, 0, &fsys);
+ if (err)
+ error(3, err, "Contacting parent");
+ /* Launch. */
+ mutex_init (&mouse.lock);
+ ports_manage_port_operations_multithread (fsys->pi.bucket,trivfs_demuxer, 2 * 60 * 1000, 0, 0);
+
+ exit(0);
+}
+
+static any_t
+mouse_thread (any_t arg)
+{
+ for(;;)
+ {
+ kd_event *event;
+ vm_offset_t buf;
+ mach_msg_type_number_t buf_size;
+ int ncopy;
+ kern_return_t err;
+
+ err = device_read (mouse.port, 0, 0, sizeof (kd_event), (char **)&buf, &buf_size);
+ if (err)
+ return err;
+
+ mutex_lock (&mouse.lock);
+ while (mouse.cnt == MOUSE_BUF_SIZE)
+ condition_wait (&mouse.notfull, &mouse.lock);
+ ncopy = buf_size / sizeof (kd_event);
+ if (MOUSE_BUF_SIZE - mouse.cnt < ncopy )
+ ncopy = MOUSE_BUF_SIZE - mouse.cnt;
+ event = (kd_event*)buf;
+ while (ncopy--)
+ {
+ mouse.buf[mouse.in] = *event++;
+ mouse.in = (mouse.in + 1) % MOUSE_BUF_SIZE;
+ mouse.cnt++;
+ }
+ vm_deallocate (mach_task_self(), buf, buf_size);
+ condition_broadcast (&mouse.fill);
+ mutex_unlock (&mouse.lock);
+ }
+}
+
+static error_t
+open_hook (struct trivfs_peropen *po)
+{
+ kern_return_t err = 0;
+ mach_port_t master;
+ mutex_lock (&mouse.lock);
+ if (po->openmodes & O_READ)
+ {
+ if( mouse.port )
+ return EBUSY;
+ err = get_privileged_ports( NULL, &master );
+ if( err )
+ return err;
+ err = device_open (master, D_READ, mouse_device_name, &mouse.port);
+ mach_port_deallocate (mach_task_self(), master);
+ }
+ mouse.cnt = 0;
+ cthread_detach (cthread_fork (mouse_thread, 0));
+ mutex_unlock (&mouse.lock);
+ return err;
+}
+
+static void
+close_hook (struct trivfs_peropen *po)
+{
+ device_close (mouse.port);
+ mouse.port = (mach_port_t)NULL;
+}
+
+/* Trivfs hooks */
+
+error_t (*trivfs_peropen_create_hook) (struct trivfs_peropen *) = open_hook;
+void (*trivfs_peropen_destroy_hook) (struct trivfs_peropen *) = close_hook;
+
+int trivfs_fstype = FSTYPE_DEV;
+int trivfs_fsid = 0;
+
+int trivfs_support_read = 1;
+int trivfs_support_write = 0;
+int trivfs_support_exec = 0;
+
+int trivfs_allow_open = O_READ | O_WRITE;
+
+void
+trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
+{
+ st->st_blksize = vm_page_size * 256; /* Make transfers LARRRRRGE */
+
+ st->st_size = 0;
+ st->st_blocks = 0;
+
+ st->st_mode &= ~S_IFMT;
+ st->st_mode |= S_IFCHR;
+}
+
+error_t
+trivfs_goaway (struct trivfs_control *fsys, int flags)
+{
+ exit (0);
+}
+
+/* Read data from an IO object. If offset if -1, read from the object
+ maintained file pointer. If the object is not seekable, offset is
+ ignored. The amount desired to be read is in AMT. */
+kern_return_t
+trivfs_S_io_read(struct trivfs_protid *cred,
+ mach_port_t reply, mach_msg_type_name_t replytype,
+ data_t *data,
+ mach_msg_type_number_t *datalen,
+ loff_t offs,
+ vm_size_t amt)
+{
+ int i,n;
+ if (!cred)
+ return EOPNOTSUPP;
+ else if (!(cred->po->openmodes & O_READ))
+ return EBADF;
+
+ mutex_lock (&mouse.lock);
+ while (!mouse.cnt)
+ {
+ if (cred->po->openmodes & O_NONBLOCK)
+ {
+ mutex_unlock (&mouse.lock);
+ return EWOULDBLOCK;
+ }
+ if (hurd_condition_wait (&mouse.fill, &mouse.lock))
+ {
+ mutex_unlock (&mouse.lock);
+ return EINTR;
+ }
+ }
+ if ((n = amt / sizeof (kd_event)) > mouse.cnt)
+ n = mouse.cnt;
+ for (i = 0; i < n; i++)
+ {
+ (*(kd_event **)data)[i] = mouse.buf[mouse.out];
+ mouse.out = (mouse.out + 1 ) % MOUSE_BUF_SIZE;
+ mouse.cnt--;
+ }
+ *datalen = n * sizeof (kd_event);
+ if (n)
+ condition_signal (&mouse.notfull);
+ mutex_unlock (&mouse.lock);
+ return 0;
+}
+
+
+/* SELECT_TYPE is the bitwise OR of SELECT_READ, SELECT_WRITE, and SELECT_URG.
+ Block until one of the indicated types of i/o can be done "quickly", and
+ return the types that are then available. ID_TAG is returned as passed; it
+ is just for the convenience of the user in matching up reply messages with
+ specific requests sent. */
+
+kern_return_t
+trivfs_S_io_select (struct trivfs_protid *cred,
+ mach_port_t reply, mach_msg_type_name_t replytype,
+ int *type)
+{
+ if (!cred)
+ return EOPNOTSUPP;
+ if(*type & ~SELECT_READ )
+ return EINVAL;
+
+ mutex_lock (&mouse.lock);
+ while (1)
+ {
+ if (mouse.cnt)
+ {
+ *type = SELECT_READ;
+ mutex_unlock (&mouse.lock);
+ return 0;
+ }
+ ports_interrupt_self_on_port_death (cred, reply);
+ if (hurd_condition_wait (&mouse.fill, &mouse.lock))
+ {
+ *type = 0;
+ mutex_unlock (&mouse.lock);
+ return EINTR;
+ }
+ }
+}
+
|