|
|
1.1 root 1: /*
2: * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
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.
11: *
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
18: * under the License.
19: *
20: * @APPLE_LICENSE_HEADER_END@
21: */
22: /*
23: * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
24: * All Rights Reserved
25: *
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.
31: *
32: * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
33: * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34: * FOR A PARTICULAR PURPOSE.
35: *
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.
41: *
42: */
43: /*
44: * Copyright 1996 1995 by Apple Computer, Inc. 1997 1996 1995 1994 1993 1992 1991
45: * All Rights Reserved
46: *
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.
52: *
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.
56: *
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.
62: */
63: /*
64: * MKLINUX-1.0DR2
65: */
66: /*
67: * 18 June 1998 sdouglas Start IOKit version.
68: * 16 Nov 1998 suurballe Port to c++
69: */
70:
71:
72: #include <mach/mach_types.h>
73:
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 "IOADBBusPriv.h"
81:
82:
83: #define super IOADBBus
84:
85: OSDefineMetaClass(IOADBController,IOADBBus)
86: OSDefineAbstractStructors(IOADBController,IOADBBus)
87:
88:
89: // **********************************************************************************
90: // start
91: //
92: // **********************************************************************************
93: bool IOADBController::start ( IOService * nub )
94: {
95: if( !super::start(nub)) {
96: return false;
97: }
98:
99: probeBus();
100:
101: return true;
102: }
103:
104:
105: // **********************************************************************************
106: // probeAddress
107: //
108: // **********************************************************************************
109: bool IOADBController::probeAddress ( IOADBAddress addr )
110: {
111: IOReturn err;
112: ADBDeviceControl * deviceInfo;
113: UInt16 value;
114: IOByteCount length;
115:
116: length = 2;
117: err = readFromDevice(addr,3,(UInt8 *)&value,&length);
118:
119: if (err == ADB_RET_OK) {
120: if( NULL == (deviceInfo = adbDevices[ addr ])) {
121:
122: deviceInfo = (ADBDeviceControl *)IOMalloc(sizeof(ADBDeviceControl));
123: bzero(deviceInfo, sizeof(ADBDeviceControl));
124:
125: adbDevices[ addr ] = deviceInfo;
126: deviceInfo->defaultAddress = addr;
127: deviceInfo->handlerID = deviceInfo->defaultHandlerID = (value & 0xff);
128: }
129: deviceInfo->address = addr;
130: }
131: return( (err == ADB_RET_OK));
132: }
133:
134:
135: // **********************************************************************************
136: // firstBit
137: //
138: // **********************************************************************************
139: unsigned int IOADBController::firstBit ( unsigned int mask )
140: {
141: int bit = 15;
142:
143: while( 0 == (mask & (1 << bit))) {
144: bit--;
145: }
146: return(bit);
147: }
148:
149:
150: // **********************************************************************************
151: // moveDeviceFrom
152: //
153: // **********************************************************************************
154: bool IOADBController::moveDeviceFrom ( IOADBAddress from, IOADBAddress to, bool check )
155: {
156: IOReturn err;
157: UInt16 value;
158: IOByteCount length;
159: bool moved;
160:
161: length = 2;
162: value = ((to << 8) | ADB_DEVCMD_CHANGE_ID);
163:
164: err = writeToDevice(from,3,(UInt8 *)&value,&length);
165:
166: adbDevices[ to ] = adbDevices[ from ];
167:
168: moved = probeAddress(to);
169:
170: if( moved || (!check)) {
171: adbDevices[ from ] = NULL;
172: }
173: else {
174: adbDevices[ to ] = NULL;
175: }
176:
177: return moved;
178: }
179:
180:
181: // **********************************************************************************
182: // probeBus
183: //
184: // **********************************************************************************
185: IOReturn IOADBController::probeBus ( void )
186: {
187: int i;
188: UInt32 unresolvedAddrs;
189: UInt32 freeAddrs;
190: IOADBAddress freeNum, devNum;
191: IOADBDevice * newDev;
192: OSDictionary * newProps;
193: char nameStr[ 10 ];
194: const OSNumber * object;
195: const OSSymbol * key;
196:
197: /* Kill the auto poll until a new dev id's have been setup */
198:
199: setAutoPollEnable(false);
200:
201: /*
202: * Send a ADB bus reset - reply is sent after bus has reset,
203: * so there is no need to wait for the reset to complete.
204: */
205:
206: resetBus();
207:
208: /*
209: * Okay, now attempt reassign the
210: * bus
211: */
212:
213: unresolvedAddrs = 0;
214: freeAddrs = 0xfffe;
215:
216: /* Skip 0 -- it's special! */
217: for (i = 1; i < ADB_DEVICE_COUNT; i++) {
218: if( probeAddress(i) ) {
219: unresolvedAddrs |= ( 1 << i );
220: freeAddrs &= ~( 1 << i );
221: }
222: }
223:
224: /* Now attempt to reassign the addresses */
225: while( unresolvedAddrs) {
226: if( !freeAddrs) {
227: panic("ADB: Cannot find a free ADB slot for reassignment!");
228: }
229:
230: freeNum = firstBit(freeAddrs);
231: devNum = firstBit(unresolvedAddrs);
232:
233: if( !moveDeviceFrom(devNum, freeNum, true) ) {
234:
235: /* It didn't move.. bad! */
236: IOLog("WARNING : ADB DEVICE %d having problems "
237: "probing!\n", devNum);
238: }
239: else {
240: if( probeAddress(devNum) ) {
241: /* Found another device at the address, leave
242: * the first device moved to one side and set up
243: * newly found device for probing
244: */
245: freeAddrs &= ~( 1 << freeNum );
246:
247: devNum = 0;
248:
249: }
250: else {
251: /* no more at this address, good !*/
252: /* Move it back.. */
253: moveDeviceFrom(freeNum,devNum,false);
254: }
255: }
256: if(devNum) {
257: unresolvedAddrs &= ~( 1 << devNum );
258: }
259: }
260:
261: IOLog("ADB present:%lx\n", (freeAddrs ^ 0xfffe));
262:
263: setAutoPollList(freeAddrs ^ 0xfffe);
264:
265: setAutoPollPeriod(11111);
266:
267: setAutoPollEnable(true);
268:
269: // publish the nubs
270: for ( i = 1; i < ADB_DEVICE_COUNT; i++ ) {
271: if( 0 == adbDevices[ i ] ) {
272: continue;
273: }
274: newDev = new IOADBDevice; // make a nub
275: if ( newDev == NULL ) {
276: continue;
277: }
278: newProps = OSDictionary::withCapacity( 10 ); // create a property table for it
279: if ( newProps == NULL ) {
280: newDev->free();
281: continue;
282: }
283:
284: key = OSSymbol::withCString(ADBaddressProperty); // make key/object for address
285: if ( key == NULL ) {
286: newDev->free();
287: newProps->free();
288: continue;
289: }
290:
291: object = OSNumber::withNumber((unsigned long long)adbDevices[i]->address,8);
292: if ( object == NULL ) {
293: key->release();
294: newDev->free();
295: newProps->free();
296: continue;
297: }
298: newProps->setObject(key, (OSObject *)object); // put it in newProps
299: key->release();
300: object->release();
301:
302: key = OSSymbol::withCString(ADBhandlerIDProperty); // make key/object for handlerID
303: if ( key == NULL ) {
304: newDev->free();
305: newProps->free();
306: continue;
307: }
308: object = OSNumber::withNumber((unsigned long long)adbDevices[i]->handlerID,8);
309: if ( object == NULL ) {
310: key->release();
311: newDev->free();
312: newProps->free();
313: continue;
314: }
315: newProps->setObject(key, (OSObject *)object); // put it in newProps
316: key->release();
317: object->release();
318:
319: key = OSSymbol::withCString(ADBdefAddressProperty); // make key/object for default addr
320: if ( key == NULL ) {
321: newDev->free();
322: newProps->free();
323: continue;
324: }
325: object = OSNumber::withNumber((unsigned long long)adbDevices[i]->defaultAddress,8);
326: if ( object == NULL ) {
327: key->release();
328: newDev->free();
329: newProps->free();
330: continue;
331: }
332: newProps->setObject(key, (OSObject *)object); // put it in newProps
333: key->release();
334: object->release();
335:
336: key = OSSymbol::withCString(ADBdefHandlerProperty); // make key/object for default h id
337: if ( key == NULL ) {
338: newDev->free();
339: newProps->free();
340: continue;
341: }
342: object = OSNumber::withNumber((unsigned long long)adbDevices[i]->defaultHandlerID,8);
343: if ( object == NULL ) {
344: key->release();
345: newDev->free();
346: newProps->free();
347: continue;
348: }
349: newProps->setObject(key, (OSObject *)object); // put it in newProps
350: key->release();
351: object->release();
352:
353: if ( ! newDev->init(newProps,adbDevices[i]) ) { // give it to our new nub
354: kprintf("adb nub init failed\n");
355: newDev->release();
356: continue;
357: }
358:
359: sprintf(nameStr,"%x-%02x",adbDevices[i]->defaultAddress,adbDevices[i]->handlerID);
360: newDev->setName(nameStr);
361: sprintf(nameStr, "%x", adbDevices[i]->defaultAddress);
362: newDev->setLocation(nameStr);
363:
364: newProps->release(); // we're done with it
365: if ( !newDev->attach(this) ) {
366: kprintf("adb nub attach failed\n");
367: newDev->release();
368: continue;
369: }
370: newDev->registerService();
371: newDev->start(this);
372: } // repeat loop
373: return kIOReturnSuccess;
374: }
375:
376:
377: // **********************************************************************************
378: // autopollHandler
379: //
380: // **********************************************************************************
381: void autopollHandler ( IOService * us, UInt8 adbCommand, IOByteCount length, UInt8 * data )
382: {
383: ((IOADBController *)us)->packet(data,length,adbCommand);
384: }
385:
386:
387: // **********************************************************************************
388: // packet
389: //
390: // **********************************************************************************
391: void IOADBController::packet ( UInt8 * data, IOByteCount length, UInt8 adbCommand )
392: {
393: ADBDeviceControl * deviceInfo;
394:
395: deviceInfo = adbDevices[ adbCommand >> 4 ];
396: if( deviceInfo != NULL ) {
397: if( deviceInfo->owner != NULL ) {
398: deviceInfo->handler(deviceInfo->owner, adbCommand, length, data);
399: }
400: }
401: else {
402: // new device arrival?
403: // IOLog("IOADBBus: new device @%x\n", address);
404: }
405: }
406:
407:
408: // **********************************************************************************
409: // matchDevice
410: //
411: // **********************************************************************************
412: bool IOADBController::matchNubWithPropertyTable( IOService * device, OSDictionary * propTable )
413: {
414: bool matched = false;
415: const char * keys;
416: ADBDeviceControl * deviceInfo = (ADBDeviceControl *)(((IOADBDevice *)device)->busRef());
417: OSObject * X;
418:
419: do {
420: X = propTable->getObject("ADB Match");
421: if( !X ) {
422: break;
423: }
424: keys = ((OSString *)X)->getCStringNoCopy();
425: if( *keys == '*' ) {
426: keys++;
427: }
428: else {
429: if( deviceInfo->defaultAddress != strtol(keys, &keys, 16)) {
430: break;
431: }
432: }
433: if( *keys++ == '-' ) {
434: if( deviceInfo->defaultHandlerID != strtol(keys, &keys, 16)) {
435: break;
436: }
437: }
438: matched = true;
439:
440: } while ( false );
441: return matched;
442: }
443:
444:
445: /////// nub -> bus
446:
447: // **********************************************************************************
448: // setOwner
449: //
450: // **********************************************************************************
451: IOReturn IOADBController::setOwner ( void * device, IOService * client, ADB_callback_func handler )
452: {
453: ADBDeviceControl * deviceInfo = (ADBDeviceControl *)device;
454:
455: deviceInfo->owner = client;
456: deviceInfo->handler = handler;
457: return kIOReturnSuccess;
458: }
459:
460:
461: // **********************************************************************************
462: // clearOwner
463: //
464: // **********************************************************************************
465: IOReturn IOADBController::clearOwner ( void * device )
466: {
467: ADBDeviceControl * deviceInfo = (ADBDeviceControl *)device;
468: kprintf("IOADBController::clearOwner\n");
469:
470: deviceInfo->owner = NULL;
471: deviceInfo->handler = NULL;
472: return kIOReturnSuccess;
473: }
474:
475:
476: // **********************************************************************************
477: // claimDevice
478: //
479: // Called by the user client
480: // **********************************************************************************
481: IOReturn IOADBController::claimDevice (unsigned long ADBaddress, IOService * client, ADB_callback_func handler )
482: {
483: if ( claimed_devices[ADBaddress] == true ) { // is this address already claimed by the user?
484: return kIOReturnExclusiveAccess; // yes
485: }
486: if ( adbDevices[ADBaddress] == NULL ) { // no, is there a device at that address?
487: return kIOReturnNoDevice; // no
488: }
489: if (adbDevices[ADBaddress]->handler != NULL ) { // yes, is it already owned by the kernel?
490: return kIOReturnExclusiveAccess; // yes
491: }
492: claimed_devices[ADBaddress] = true; // no, user can have it
493: return kIOReturnSuccess;
494: }
495:
496:
497: // **********************************************************************************
498: // releaseDevice
499: //
500: // Called by the user client
501: // **********************************************************************************
502: IOReturn IOADBController::releaseDevice (unsigned long ADBaddress )
503: {
504: if ( claimed_devices[ADBaddress] == false ) {
505: return kIOReturnBadArgument;
506: }
507:
508: claimed_devices[ADBaddress] = false;
509:
510: return kIOReturnSuccess;
511: }
512:
513:
514: // **********************************************************************************
515: // readDeviceForUser
516: //
517: // Called by the user client
518: // **********************************************************************************
519: IOReturn IOADBController::readDeviceForUser (unsigned long address, unsigned long adbRegister,
520: UInt8 * data, IOByteCount * length)
521: {
522: if ( claimed_devices[address] == false ) {
523: return kIOReturnBadArgument;
524: }
525:
526: return (readFromDevice((IOADBAddress)address,(IOADBRegister)adbRegister,data,length));
527: }
528:
529:
530: // **********************************************************************************
531: // writeDeviceForUser
532: //
533: // Called by the user client
534: // **********************************************************************************
535: IOReturn IOADBController::writeDeviceForUser (unsigned long address, unsigned long adbRegister,
536: UInt8 * data, IOByteCount * length)
537: {
538: if ( claimed_devices[address] == false ) {
539: return kIOReturnBadArgument;
540: }
541:
542: return (writeToDevice((IOADBAddress)address,(IOADBRegister)adbRegister,data,length));
543: }
544:
545:
546: // **********************************************************************************
547: // address
548: //
549: // **********************************************************************************
550: IOADBAddress IOADBController::address ( ADBDeviceControl * busRef )
551: {
552: return busRef->address;
553: }
554:
555:
556: // **********************************************************************************
557: // defaultAddress
558: //
559: // **********************************************************************************
560: IOADBAddress IOADBController::defaultAddress ( ADBDeviceControl * busRef )
561: {
562: return busRef->defaultAddress;
563: }
564:
565:
566: // **********************************************************************************
567: // handlerID
568: //
569: // **********************************************************************************
570: UInt8 IOADBController::handlerID ( ADBDeviceControl * busRef )
571: {
572: return busRef->handlerID;
573: }
574:
575:
576: // **********************************************************************************
577: // defaultHandlerID
578: //
579: // **********************************************************************************
580: UInt8 IOADBController::defaultHandlerID ( ADBDeviceControl * busRef )
581: {
582: return busRef->defaultHandlerID;
583: }
584:
585:
586: // **********************************************************************************
587: // flush
588: //
589: // **********************************************************************************
590: IOReturn IOADBController::flush ( ADBDeviceControl * busRef )
591: {
592: return(flushDevice(busRef->address));
593: }
594:
595:
596: // **********************************************************************************
597: // readRegister
598: //
599: // **********************************************************************************
600: IOReturn IOADBController::readRegister ( ADBDeviceControl * busRef, IOADBRegister adbRegister,
601: UInt8 * data, IOByteCount * length )
602: {
603: return readFromDevice(busRef->address,adbRegister,data,length);
604: }
605:
606:
607: // **********************************************************************************
608: // writeRegister
609: //
610: // **********************************************************************************
611: IOReturn IOADBController::writeRegister ( ADBDeviceControl * busRef, IOADBRegister adbRegister,
612: UInt8 * data, IOByteCount * length )
613: {
614: return writeToDevice(busRef->address,adbRegister,data,length);
615: }
616:
617:
618: // **********************************************************************************
619: // setHandlerID
620: //
621: // **********************************************************************************
622: IOReturn IOADBController::setHandlerID ( ADBDeviceControl * deviceInfo, UInt8 handlerID )
623: {
624: IOReturn err;
625: UInt16 value;
626: IOByteCount length;
627: IOADBAddress addr = deviceInfo->address;
628:
629: length = 2;
630: err = readFromDevice(addr,3,(UInt8 *)&value,&length);
631:
632: if ( err ) {
633: return err;
634: }
635:
636: value = (value & 0xf000) | handlerID | (addr << 8);
637: length = 2;
638: err = writeToDevice(addr,3,(UInt8 *)&value,&length);
639:
640: length = 2;
641: err = readFromDevice(addr,3,(UInt8 *)&value,&length);
642:
643: if ( err == kIOReturnSuccess ) {
644: deviceInfo->handlerID = value & 0xff;
645: }
646:
647: if ( deviceInfo->handlerID == handlerID ) {
648: err = kIOReturnSuccess;
649: }
650: else {
651: err = kIOReturnNoResources;
652: }
653:
654: return err;
655: }
656:
657:
658: // **********************************************************************************
659: // getURLComponentUnit
660: //
661: // **********************************************************************************
662: int IOADBController::getURLComponentUnit ( IOService * device, char * path, int maxLen )
663: {
664: ADBDeviceControl * deviceInfo = (ADBDeviceControl *)((IOADBDevice *)device)->busRef();
665:
666: if( maxLen > 1 ) {
667: sprintf( path, "%x", deviceInfo->address );
668: return(1);
669: }
670: else {
671: return(0);
672: }
673: }
674:
675:
676: // **********************************************************************************
677: // newUserClient
678: //
679: // **********************************************************************************
680: IOReturn IOADBController::newUserClient( task_t owningTask, void * /* security_id */, UInt32 type, IOUserClient ** handler )
681: {
682: IOReturn err = kIOReturnSuccess;
683: IOADBControllerUserClient * client;
684:
685: client = IOADBControllerUserClient::withTask(owningTask);
686:
687: if( !client || (false == client->attach( this )) ||
688: (false == client->start( this )) ) {
689: if(client) {
690: client->detach( this );
691: client->release();
692: client = NULL;
693: }
694: err = kIOReturnNoMemory;
695: }
696: *handler = client;
697: return err;
698: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.