2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
26 * Permission to use, copy, modify, and distribute this software and
27 * its documentation for any purpose and without fee is hereby granted,
28 * provided that the above copyright notice appears in all copies and
29 * that both the copyright notice and this permission notice appear in
30 * supporting documentation.
32 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
33 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34 * FOR A PARTICULAR PURPOSE.
36 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
37 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
38 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
39 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
40 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 * Copyright 1996 1995 by Apple Computer, Inc. 1997 1996 1995 1994 1993 1992 1991
47 * Permission to use, copy, modify, and distribute this software and
48 * its documentation for any purpose and without fee is hereby granted,
49 * provided that the above copyright notice appears in all copies and
50 * that both the copyright notice and this permission notice appear in
51 * supporting documentation.
53 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
54 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
55 * FOR A PARTICULAR PURPOSE.
57 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
58 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
59 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
60 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
61 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
67 * 18 June 1998 sdouglas Start IOKit version.
68 * 16 Nov 1998 suurballe Port to c++
72 #include <mach/mach_types.h>
74 #include "IOADBControllerUserClient.h"
75 #include <IOKit/adb/IOADBController.h>
76 #include <IOKit/adb/IOADBDevice.h>
77 #include <libkern/c++/OSSymbol.h>
78 #include <libkern/c++/OSNumber.h>
79 #include <IOKit/IOLib.h>
80 #include <IOKit/pwr_mgt/RootDomain.h>
81 #include "IOADBBusPriv.h"
83 bool ADBhasRoot( OSObject
*, void *, IOService
* );
84 void doProbe ( thread_call_param_t
, thread_call_param_t
);
86 #define kTenSeconds 10000000
88 #define super IOADBBus
90 OSDefineMetaClass(IOADBController
,IOADBBus
)
91 OSDefineAbstractStructors(IOADBController
,IOADBBus
)
94 // **********************************************************************************
97 // **********************************************************************************
98 bool IOADBController::start ( IOService
* nub
)
100 if( !super::start(nub
)) {
108 // creates the probe thread for when we wake up:
109 probeThread
= thread_call_allocate((thread_call_func_t
)doProbe
, (thread_call_param_t
)this);
110 if (probeThread
== NULL
) {
111 IOLog("IOADBController::start fails to call thread_call_allocate \n");
115 addNotification( gIOPublishNotification
,serviceMatching("IOPMrootDomain"), // look for the Root Domain
116 (IOServiceNotificationHandler
)ADBhasRoot
, this, 0 );
125 // **********************************************************************************
128 // The Root Power Domain has registered.
129 // Register as an interested driver so we find out when the system is
130 // going to sleep and waking up.
131 // **********************************************************************************
132 bool ADBhasRoot( OSObject
* us
, void *, IOService
* yourDevice
)
134 if ( yourDevice
!= NULL
) {
135 ((IOADBController
*)us
)->rootDomain
= (IOPMrootDomain
*)yourDevice
;
136 ((IOADBController
*)us
)->rootDomain
->registerInterestedDriver((IOService
*) us
);
142 //*********************************************************************************
143 // powerStateWillChangeTo
145 // We are notified here of power changes in the root domain.
147 // If power is going down in the root domain, then the system is going to
148 // sleep, and we tear down the ADB stack.
149 //*********************************************************************************
151 IOReturn
IOADBController::powerStateWillChangeTo ( IOPMPowerFlags theFlags
, unsigned long, IOService
*)
155 if ( ! (theFlags
& IOPMPowerOn
) ) {
157 for ( i
= 1; i
< ADB_DEVICE_COUNT
; i
++ ) {
158 if( adbDevices
[ i
] != NULL
) {
159 if ( adbDevices
[ i
]->nub
) {
160 adbDevices
[ i
]->nub
->terminate(kIOServiceRequired
);
161 adbDevices
[ i
]->nub
->release();
163 IOFree( adbDevices
[ i
], sizeof (ADBDeviceControl
));
164 adbDevices
[ i
] = NULL
;
168 return IOPMAckImplied
;
171 //*********************************************************************************
172 // powerStateDidChangeTo
174 // We are notified here of power changes in the root domain
176 // If power is has been brought up, then the system is waking from sleep.
177 // We re-probe the bus
178 //*********************************************************************************
179 IOReturn
IOADBController::powerStateDidChangeTo ( IOPMPowerFlags theFlags
, unsigned long, IOService
*)
181 if ( theFlags
& IOPMPowerOn
) {
183 thread_call_enter(probeThread
);
188 return IOPMAckImplied
;
192 void doProbe ( thread_call_param_t arg
, thread_call_param_t
)
194 ((IOADBController
*)arg
)->probeBus();
195 ((IOADBController
*)arg
)->rootDomain
->acknowledgePowerChange((IOService
*)arg
);
199 // **********************************************************************************
202 // **********************************************************************************
203 bool IOADBController::probeAddress ( IOADBAddress addr
)
206 ADBDeviceControl
* deviceInfo
;
211 err
= readFromDevice(addr
,3,(UInt8
*)&value
,&length
);
213 if (err
== ADB_RET_OK
) {
214 if( NULL
== (deviceInfo
= adbDevices
[ addr
])) {
216 deviceInfo
= (ADBDeviceControl
*)IOMalloc(sizeof(ADBDeviceControl
));
217 bzero(deviceInfo
, sizeof(ADBDeviceControl
));
219 adbDevices
[ addr
] = deviceInfo
;
220 deviceInfo
->defaultAddress
= addr
;
221 deviceInfo
->handlerID
= deviceInfo
->defaultHandlerID
= (value
& 0xff);
223 deviceInfo
->address
= addr
;
225 return( (err
== ADB_RET_OK
));
229 // **********************************************************************************
232 // **********************************************************************************
233 unsigned int IOADBController::firstBit ( unsigned int mask
)
237 while( 0 == (mask
& (1 << bit
))) {
244 // **********************************************************************************
247 // **********************************************************************************
248 bool IOADBController::moveDeviceFrom ( IOADBAddress from
, IOADBAddress to
, bool check
)
256 value
= ((to
<< 8) | ADB_DEVCMD_CHANGE_ID
);
258 err
= writeToDevice(from
,3,(UInt8
*)&value
,&length
);
260 adbDevices
[ to
] = adbDevices
[ from
];
262 moved
= probeAddress(to
);
264 if( moved
|| (!check
)) {
265 adbDevices
[ from
] = NULL
;
268 adbDevices
[ to
] = NULL
;
275 // **********************************************************************************
278 // **********************************************************************************
279 IOReturn
IOADBController::probeBus ( void )
282 UInt32 unresolvedAddrs
;
284 IOADBAddress freeNum
, devNum
;
285 IOADBDevice
* newDev
;
286 OSDictionary
* newProps
;
288 const OSNumber
* object
;
289 const OSSymbol
* key
;
291 /* Waits one second for the trackpads to be up */
295 /* Kill the auto poll until a new dev id's have been setup */
297 setAutoPollEnable(false);
300 * Send a ADB bus reset - reply is sent after bus has reset,
301 * so there is no need to wait for the reset to complete.
306 /* Waits one second for the trackpads to be up */
311 * Okay, now attempt reassign the
318 /* Skip 0 -- it's special! */
319 for (i
= 1; i
< ADB_DEVICE_COUNT
; i
++) {
320 if( probeAddress(i
) ) {
321 unresolvedAddrs
|= ( 1 << i
);
322 freeAddrs
&= ~( 1 << i
);
326 /* Now attempt to reassign the addresses */
327 while( unresolvedAddrs
) {
329 panic("ADB: Cannot find a free ADB slot for reassignment!");
332 freeNum
= firstBit(freeAddrs
);
333 devNum
= firstBit(unresolvedAddrs
);
335 if( !moveDeviceFrom(devNum
, freeNum
, true) ) {
337 /* It didn't move.. bad! */
338 IOLog("WARNING : ADB DEVICE %d having problems "
339 "probing!\n", devNum
);
342 if( probeAddress(devNum
) ) {
343 /* Found another device at the address, leave
344 * the first device moved to one side and set up
345 * newly found device for probing
347 freeAddrs
&= ~( 1 << freeNum
);
353 /* no more at this address, good !*/
355 moveDeviceFrom(freeNum
,devNum
,false);
359 unresolvedAddrs
&= ~( 1 << devNum
);
363 IOLog("ADB present:%lx\n", (freeAddrs
^ 0xfffe));
365 setAutoPollList(freeAddrs
^ 0xfffe);
367 setAutoPollPeriod(11111);
369 setAutoPollEnable(true);
372 for ( i
= 1; i
< ADB_DEVICE_COUNT
; i
++ ) {
373 if( 0 == adbDevices
[ i
] ) {
376 newDev
= new IOADBDevice
; // make a nub
377 if ( newDev
== NULL
) {
380 adbDevices
[ i
]->nub
= newDev
; // keep a pointer to it
382 newProps
= OSDictionary::withCapacity( 10 ); // create a property table for it
383 if ( newProps
== NULL
) {
388 key
= OSSymbol::withCString(ADBaddressProperty
); // make key/object for address
395 object
= OSNumber::withNumber((unsigned long long)adbDevices
[i
]->address
,8);
396 if ( object
== NULL
) {
402 newProps
->setObject(key
, (OSObject
*)object
); // put it in newProps
406 key
= OSSymbol::withCString(ADBhandlerIDProperty
); // make key/object for handlerID
412 object
= OSNumber::withNumber((unsigned long long)adbDevices
[i
]->handlerID
,8);
413 if ( object
== NULL
) {
419 newProps
->setObject(key
, (OSObject
*)object
); // put it in newProps
423 key
= OSSymbol::withCString(ADBdefAddressProperty
); // make key/object for default addr
429 object
= OSNumber::withNumber((unsigned long long)adbDevices
[i
]->defaultAddress
,8);
430 if ( object
== NULL
) {
436 newProps
->setObject(key
, (OSObject
*)object
); // put it in newProps
440 key
= OSSymbol::withCString(ADBdefHandlerProperty
); // make key/object for default h id
446 object
= OSNumber::withNumber((unsigned long long)adbDevices
[i
]->defaultHandlerID
,8);
447 if ( object
== NULL
) {
453 newProps
->setObject(key
, (OSObject
*)object
); // put it in newProps
457 if ( ! newDev
->init(newProps
,adbDevices
[i
]) ) { // give it to our new nub
458 kprintf("adb nub init failed\n");
463 sprintf(nameStr
,"%x-%02x",adbDevices
[i
]->defaultAddress
,adbDevices
[i
]->handlerID
);
464 newDev
->setName(nameStr
);
465 sprintf(nameStr
, "%x", adbDevices
[i
]->defaultAddress
);
466 newDev
->setLocation(nameStr
);
468 newProps
->release(); // we're done with it
469 if ( !newDev
->attach(this) ) {
470 kprintf("adb nub attach failed\n");
474 newDev
->registerService();
477 return kIOReturnSuccess
;
481 // **********************************************************************************
484 // **********************************************************************************
485 void autopollHandler ( IOService
* us
, UInt8 adbCommand
, IOByteCount length
, UInt8
* data
)
487 ((IOADBController
*)us
)->packet(data
,length
,adbCommand
);
491 // **********************************************************************************
494 // **********************************************************************************
495 void IOADBController::packet ( UInt8
* data
, IOByteCount length
, UInt8 adbCommand
)
497 ADBDeviceControl
* deviceInfo
;
499 deviceInfo
= adbDevices
[ adbCommand
>> 4 ];
500 if( deviceInfo
!= NULL
) {
501 if( deviceInfo
->owner
!= NULL
) {
502 deviceInfo
->handler(deviceInfo
->owner
, adbCommand
, length
, data
);
506 // new device arrival?
507 // IOLog("IOADBBus: new device @%x\n", address);
512 // **********************************************************************************
515 // **********************************************************************************
516 bool IOADBController::matchNubWithPropertyTable( IOService
* device
, OSDictionary
* propTable
)
518 bool matched
= false;
520 ADBDeviceControl
* deviceInfo
= (ADBDeviceControl
*)(((IOADBDevice
*)device
)->busRef());
524 X
= propTable
->getObject("ADB Match");
528 keys
= ((OSString
*)X
)->getCStringNoCopy();
533 if( deviceInfo
->defaultAddress
!= strtol(keys
, &keys
, 16)) {
537 if( *keys
++ == '-' ) {
538 if( deviceInfo
->defaultHandlerID
!= strtol(keys
, &keys
, 16)) {
551 // **********************************************************************************
554 // **********************************************************************************
555 IOReturn
IOADBController::setOwner ( void * device
, IOService
* client
, ADB_callback_func handler
)
557 ADBDeviceControl
* deviceInfo
= (ADBDeviceControl
*)device
;
559 deviceInfo
->owner
= client
;
560 deviceInfo
->handler
= handler
;
561 return kIOReturnSuccess
;
565 // **********************************************************************************
568 // **********************************************************************************
569 IOReturn
IOADBController::clearOwner ( void * device
)
571 ADBDeviceControl
* deviceInfo
= (ADBDeviceControl
*)device
;
572 kprintf("IOADBController::clearOwner\n");
574 deviceInfo
->owner
= NULL
;
575 deviceInfo
->handler
= NULL
;
576 return kIOReturnSuccess
;
580 // **********************************************************************************
583 // Called by the user client
584 // **********************************************************************************
585 IOReturn
IOADBController::claimDevice (unsigned long ADBaddress
, IOService
* client
, ADB_callback_func handler
)
587 if ( claimed_devices
[ADBaddress
] == true ) { // is this address already claimed by the user?
588 return kIOReturnExclusiveAccess
; // yes
590 if ( adbDevices
[ADBaddress
] == NULL
) { // no, is there a device at that address?
591 return kIOReturnNoDevice
; // no
593 if (adbDevices
[ADBaddress
]->handler
!= NULL
) { // yes, is it already owned by the kernel?
594 return kIOReturnExclusiveAccess
; // yes
596 claimed_devices
[ADBaddress
] = true; // no, user can have it
597 return kIOReturnSuccess
;
601 // **********************************************************************************
604 // Called by the user client
605 // **********************************************************************************
606 IOReturn
IOADBController::releaseDevice (unsigned long ADBaddress
)
608 if ( claimed_devices
[ADBaddress
] == false ) {
609 return kIOReturnBadArgument
;
612 claimed_devices
[ADBaddress
] = false;
614 return kIOReturnSuccess
;
618 // **********************************************************************************
621 // Called by the user client
622 // **********************************************************************************
623 IOReturn
IOADBController::readDeviceForUser (unsigned long address
, unsigned long adbRegister
,
624 UInt8
* data
, IOByteCount
* length
)
626 if ( claimed_devices
[address
] == false ) {
627 return kIOReturnBadArgument
;
630 return (readFromDevice((IOADBAddress
)address
,(IOADBRegister
)adbRegister
,data
,length
));
634 // **********************************************************************************
635 // writeDeviceForUser
637 // Called by the user client
638 // **********************************************************************************
639 IOReturn
IOADBController::writeDeviceForUser (unsigned long address
, unsigned long adbRegister
,
640 UInt8
* data
, IOByteCount
* length
)
642 if ( claimed_devices
[address
] == false ) {
643 return kIOReturnBadArgument
;
646 return (writeToDevice((IOADBAddress
)address
,(IOADBRegister
)adbRegister
,data
,length
));
650 // **********************************************************************************
653 // **********************************************************************************
654 IOADBAddress
IOADBController::address ( ADBDeviceControl
* busRef
)
656 return busRef
->address
;
660 // **********************************************************************************
663 // **********************************************************************************
664 IOADBAddress
IOADBController::defaultAddress ( ADBDeviceControl
* busRef
)
666 return busRef
->defaultAddress
;
670 // **********************************************************************************
673 // **********************************************************************************
674 UInt8
IOADBController::handlerID ( ADBDeviceControl
* busRef
)
676 return busRef
->handlerID
;
680 // **********************************************************************************
683 // **********************************************************************************
684 UInt8
IOADBController::defaultHandlerID ( ADBDeviceControl
* busRef
)
686 return busRef
->defaultHandlerID
;
690 // **********************************************************************************
693 // **********************************************************************************
694 IOReturn
IOADBController::flush ( ADBDeviceControl
* busRef
)
696 return(flushDevice(busRef
->address
));
700 // **********************************************************************************
703 // **********************************************************************************
704 IOReturn
IOADBController::readRegister ( ADBDeviceControl
* busRef
, IOADBRegister adbRegister
,
705 UInt8
* data
, IOByteCount
* length
)
707 return readFromDevice(busRef
->address
,adbRegister
,data
,length
);
711 // **********************************************************************************
714 // **********************************************************************************
715 IOReturn
IOADBController::writeRegister ( ADBDeviceControl
* busRef
, IOADBRegister adbRegister
,
716 UInt8
* data
, IOByteCount
* length
)
718 return writeToDevice(busRef
->address
,adbRegister
,data
,length
);
722 // **********************************************************************************
725 // **********************************************************************************
726 IOReturn
IOADBController::setHandlerID ( ADBDeviceControl
* deviceInfo
, UInt8 handlerID
)
731 IOADBAddress addr
= deviceInfo
->address
;
734 err
= readFromDevice(addr
,3,(UInt8
*)&value
,&length
);
740 value
= (value
& 0xf000) | handlerID
| (addr
<< 8);
741 length
= sizeof(value
);
742 err
= writeToDevice(addr
,3,(UInt8
*)&value
,&length
);
744 length
= sizeof(value
);
745 err
= readFromDevice(addr
,3,(UInt8
*)&value
,&length
);
747 if ( err
== kIOReturnSuccess
) {
748 deviceInfo
->handlerID
= value
& 0xff;
751 if ( deviceInfo
->handlerID
== handlerID
) {
752 err
= kIOReturnSuccess
;
755 err
= kIOReturnNoResources
;
762 // **********************************************************************************
763 // getURLComponentUnit
765 // **********************************************************************************
766 int IOADBController::getURLComponentUnit ( IOService
* device
, char * path
, int maxLen
)
768 ADBDeviceControl
* deviceInfo
= (ADBDeviceControl
*)((IOADBDevice
*)device
)->busRef();
771 sprintf( path
, "%x", deviceInfo
->address
);
780 // **********************************************************************************
783 // **********************************************************************************
784 IOReturn
IOADBController::newUserClient( task_t owningTask
, void * /* security_id */, UInt32 type
, IOUserClient
** handler
)
786 IOReturn err
= kIOReturnSuccess
;
787 IOADBControllerUserClient
* client
;
789 client
= IOADBControllerUserClient::withTask(owningTask
);
791 if( !client
|| (false == client
->attach( this )) ||
792 (false == client
->start( this )) ) {
794 client
->detach( this );
798 err
= kIOReturnNoMemory
;