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
|
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (C) 2008 Nathan Whitehorn
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <machine/bus.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include "adb.h"
#include "adbvar.h"
static int adb_bus_probe(device_t dev);
static int adb_bus_attach(device_t dev);
static int adb_bus_detach(device_t dev);
static void adb_bus_enumerate(void *xdev);
static void adb_probe_nomatch(device_t dev, device_t child);
static int adb_print_child(device_t dev, device_t child);
static int adb_send_raw_packet_sync(device_t dev, uint8_t to, uint8_t command, uint8_t reg, int len, u_char *data, u_char *reply);
static char *adb_device_string[] = {
"HOST", "dongle", "keyboard", "mouse", "tablet", "modem", "RESERVED", "misc"
};
static device_method_t adb_bus_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, adb_bus_probe),
DEVMETHOD(device_attach, adb_bus_attach),
DEVMETHOD(device_detach, adb_bus_detach),
DEVMETHOD(device_shutdown, bus_generic_shutdown),
DEVMETHOD(device_suspend, bus_generic_suspend),
DEVMETHOD(device_resume, bus_generic_resume),
/* Bus Interface */
DEVMETHOD(bus_probe_nomatch, adb_probe_nomatch),
DEVMETHOD(bus_print_child, adb_print_child),
{ 0, 0 },
};
driver_t adb_driver = {
"adb",
adb_bus_methods,
sizeof(struct adb_softc),
};
devclass_t adb_devclass;
static int
adb_bus_probe(device_t dev)
{
device_set_desc(dev, "Apple Desktop Bus");
return (0);
}
static int
adb_bus_attach(device_t dev)
{
struct adb_softc *sc = device_get_softc(dev);
sc->enum_hook.ich_func = adb_bus_enumerate;
sc->enum_hook.ich_arg = dev;
/*
* We should wait until interrupts are enabled to try to probe
* the bus. Enumerating the ADB involves receiving packets,
* which works best with interrupts enabled.
*/
if (config_intrhook_establish(&sc->enum_hook) != 0)
return (ENOMEM);
return (0);
}
static void
adb_bus_enumerate(void *xdev)
{
device_t dev = (device_t)xdev;
struct adb_softc *sc = device_get_softc(dev);
uint8_t i, next_free;
uint16_t r3;
sc->sc_dev = dev;
sc->parent = device_get_parent(dev);
sc->packet_reply = 0;
sc->autopoll_mask = 0;
sc->sync_packet = 0xffff;
/* Initialize devinfo */
for (i = 0; i < 16; i++) {
sc->devinfo[i].address = i;
sc->devinfo[i].default_address = 0;
}
/* Reset ADB bus */
adb_send_raw_packet_sync(dev,0,ADB_COMMAND_BUS_RESET,0,0,NULL,NULL);
DELAY(1500);
/* Enumerate bus */
next_free = 8;
for (i = 1; i <= 7; i++) {
int8_t first_relocated = -1;
int reply = 0;
do {
reply = adb_send_raw_packet_sync(dev,i,
ADB_COMMAND_TALK,3,0,NULL,NULL);
if (reply) {
/* If we got a response, relocate to next_free */
r3 = sc->devinfo[i].register3;
r3 &= 0xf000;
r3 |= ((uint16_t)(next_free) & 0x000f) << 8;
r3 |= 0x00fe;
adb_send_raw_packet_sync(dev,i, ADB_COMMAND_LISTEN,3,
sizeof(uint16_t),(u_char *)(&r3),NULL);
adb_send_raw_packet_sync(dev,next_free,
ADB_COMMAND_TALK,3,0,NULL,NULL);
sc->devinfo[next_free].default_address = i;
if (first_relocated < 0)
first_relocated = next_free;
next_free++;
} else if (first_relocated > 0) {
/* Collisions removed, relocate first device back */
r3 = sc->devinfo[i].register3;
r3 &= 0xf000;
r3 |= ((uint16_t)(i) & 0x000f) << 8;
adb_send_raw_packet_sync(dev,first_relocated,
ADB_COMMAND_LISTEN,3,
sizeof(uint16_t),(u_char *)(&r3),NULL);
adb_send_raw_packet_sync(dev,i,
ADB_COMMAND_TALK,3,0,NULL,NULL);
sc->devinfo[i].default_address = i;
sc->devinfo[(int)(first_relocated)].default_address = 0;
break;
}
} while (reply);
}
for (i = 0; i < 16; i++) {
if (sc->devinfo[i].default_address) {
sc->children[i] = device_add_child(dev, NULL, -1);
device_set_ivars(sc->children[i], &sc->devinfo[i]);
}
}
bus_generic_attach(dev);
config_intrhook_disestablish(&sc->enum_hook);
}
static int adb_bus_detach(device_t dev)
{
return (bus_generic_detach(dev));
}
static void
adb_probe_nomatch(device_t dev, device_t child)
{
struct adb_devinfo *dinfo;
if (bootverbose) {
dinfo = device_get_ivars(child);
device_printf(dev,"ADB %s at device %d (no driver attached)\n",
adb_device_string[dinfo->default_address],dinfo->address);
}
}
u_int
adb_receive_raw_packet(device_t dev, u_char status, u_char command, int len,
u_char *data)
{
struct adb_softc *sc = device_get_softc(dev);
u_char addr = command >> 4;
if (len > 0 && (command & 0x0f) == ((ADB_COMMAND_TALK << 2) | 3)) {
memcpy(&sc->devinfo[addr].register3,data,2);
sc->devinfo[addr].handler_id = data[1];
}
if (sc->sync_packet == command) {
memcpy(sc->syncreg,data,(len > 8) ? 8 : len);
atomic_store_rel_int(&sc->packet_reply,len + 1);
wakeup(sc);
}
if (sc->children[addr] != NULL) {
ADB_RECEIVE_PACKET(sc->children[addr],status,
(command & 0x0f) >> 2,command & 0x03,len,data);
}
return (0);
}
static int
adb_print_child(device_t dev, device_t child)
{
struct adb_devinfo *dinfo;
int retval = 0;
dinfo = device_get_ivars(child);
retval += bus_print_child_header(dev,child);
printf(" at device %d",dinfo->address);
retval += bus_print_child_footer(dev, child);
return (retval);
}
u_int
adb_send_packet(device_t dev, u_char command, u_char reg, int len, u_char *data)
{
u_char command_byte = 0;
struct adb_devinfo *dinfo;
struct adb_softc *sc;
sc = device_get_softc(device_get_parent(dev));
dinfo = device_get_ivars(dev);
command_byte |= dinfo->address << 4;
command_byte |= command << 2;
command_byte |= reg;
ADB_HB_SEND_RAW_PACKET(sc->parent, command_byte, len, data, 1);
return (0);
}
u_int
adb_set_autopoll(device_t dev, u_char enable)
{
struct adb_devinfo *dinfo;
struct adb_softc *sc;
uint16_t mod = 0;
sc = device_get_softc(device_get_parent(dev));
dinfo = device_get_ivars(dev);
mod = enable << dinfo->address;
if (enable) {
sc->autopoll_mask |= mod;
} else {
mod = ~mod;
sc->autopoll_mask &= mod;
}
ADB_HB_SET_AUTOPOLL_MASK(sc->parent,sc->autopoll_mask);
return (0);
}
uint8_t
adb_get_device_type(device_t dev)
{
struct adb_devinfo *dinfo;
dinfo = device_get_ivars(dev);
return (dinfo->default_address);
}
uint8_t
adb_get_device_handler(device_t dev)
{
struct adb_devinfo *dinfo;
dinfo = device_get_ivars(dev);
return (dinfo->handler_id);
}
static int
adb_send_raw_packet_sync(device_t dev, uint8_t to, uint8_t command,
uint8_t reg, int len, u_char *data, u_char *reply)
{
u_char command_byte = 0;
struct adb_softc *sc;
int result = -1;
int i = 1;
sc = device_get_softc(dev);
command_byte |= to << 4;
command_byte |= command << 2;
command_byte |= reg;
/* Wait if someone else has a synchronous request pending */
while (!atomic_cmpset_int(&sc->sync_packet, 0xffff, command_byte))
tsleep(sc, 0, "ADB sync", hz/10);
sc->packet_reply = 0;
sc->sync_packet = command_byte;
ADB_HB_SEND_RAW_PACKET(sc->parent, command_byte, len, data, 1);
while (!atomic_fetchadd_int(&sc->packet_reply,0)) {
/*
* Maybe the command got lost? Try resending and polling the
* controller.
*/
if (i % 40 == 0)
ADB_HB_SEND_RAW_PACKET(sc->parent, command_byte,
len, data, 1);
tsleep(sc, 0, "ADB sync", hz/10);
i++;
}
result = sc->packet_reply - 1;
if (reply != NULL && result > 0)
memcpy(reply,sc->syncreg,result);
/* Clear packet sync */
sc->packet_reply = 0;
/*
* We can't match a value beyond 8 bits, so set sync_packet to
* 0xffff to avoid collisions.
*/
atomic_set_int(&sc->sync_packet, 0xffff);
return (result);
}
uint8_t
adb_set_device_handler(device_t dev, uint8_t newhandler)
{
struct adb_softc *sc;
struct adb_devinfo *dinfo;
uint16_t newr3;
dinfo = device_get_ivars(dev);
sc = device_get_softc(device_get_parent(dev));
newr3 = dinfo->register3 & 0xff00;
newr3 |= (uint16_t)(newhandler);
adb_send_raw_packet_sync(sc->sc_dev,dinfo->address, ADB_COMMAND_LISTEN,
3, sizeof(uint16_t), (u_char *)(&newr3), NULL);
adb_send_raw_packet_sync(sc->sc_dev,dinfo->address,
ADB_COMMAND_TALK, 3, 0, NULL, NULL);
return (dinfo->handler_id);
}
size_t
adb_read_register(device_t dev, u_char reg, void *data)
{
struct adb_softc *sc;
struct adb_devinfo *dinfo;
size_t result;
dinfo = device_get_ivars(dev);
sc = device_get_softc(device_get_parent(dev));
result = adb_send_raw_packet_sync(sc->sc_dev,dinfo->address,
ADB_COMMAND_TALK, reg, 0, NULL, data);
return (result);
}
size_t
adb_write_register(device_t dev, u_char reg, size_t len, void *data)
{
struct adb_softc *sc;
struct adb_devinfo *dinfo;
size_t result;
dinfo = device_get_ivars(dev);
sc = device_get_softc(device_get_parent(dev));
result = adb_send_raw_packet_sync(sc->sc_dev,dinfo->address,
ADB_COMMAND_LISTEN, reg, len, (u_char *)data, NULL);
result = adb_send_raw_packet_sync(sc->sc_dev,dinfo->address,
ADB_COMMAND_TALK, reg, 0, NULL, NULL);
return (result);
}
|