aboutsummaryrefslogtreecommitdiffstats
path: root/io_iokit.c
blob: dfcb0f54b903102cc8f8a6bd661b9df2fa50d108 (plain)
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
/*
 * io.c
 *
 * Copyright (C) 2005 Erik Gilling, all rights reserved
 * Copyright (C) 2011 Holger Freyther, all rights reserved
 *
 *	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, version 2.
 *
 */

#include "config.h"

#include <stdio.h>

#include <mach/mach.h>

#include <CoreFoundation/CFNumber.h>

#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>


#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include "io.h"
#include "samba.h"


static mach_port_t masterPort = 0;
static IOUSBDeviceInterface **usbDev = NULL;
static IOUSBInterfaceInterface **intf = NULL;
static UInt8 inPipeRef = 0;
static UInt8 outPipeRef = 0;

#undef DEBUG_IO


int do_intf(io_service_t usbInterfaceRef)
{
    IOReturn err;
    IOCFPlugInInterface **iodev;
    SInt32 score;
    UInt8 numPipes;
    int i;
    UInt8 direction, number, transferType, interval;
    UInt16 maxPacketSize;
    
    err = IOCreatePlugInInterfaceForService(usbInterfaceRef, 
					    kIOUSBInterfaceUserClientTypeID, 
					    kIOCFPlugInInterfaceID, 
					    &iodev, &score);
    if( err || !iodev ) {
      printf("unable to create plugin. ret = %08x, iodev = %p\n", err, iodev);
      return -1;
    }
    
    err = (*iodev)->QueryInterface(iodev, 
				   CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID),
				   (LPVOID)&intf);
    IODestroyPlugInInterface(iodev);
	
    if (err || !intf) {
      printf("unable to create a device interface. ret = %08x, intf = %p\n", 
	     err, intf);
      return -1;
    }

    err = (*intf)->USBInterfaceOpen(intf);
    if (err) {
      printf("unable to open interface. ret = %08x\n", err);
      return -1;
    }
    
    err = (*intf)->GetNumEndpoints(intf, &numPipes);
    if (err) {
      printf("unable to get number of endpoints. ret = %08x\n", err);
      return -1;
    }

    if (numPipes) {
      for (i=1; i <= numPipes; i++) {
	
	err = (*intf)->GetPipeProperties(intf, i, &direction, 
					 &number, &transferType,
					 &maxPacketSize, &interval);
	if (err) {
	    printf("unable to get pipe properties for pipe %d, err = %08x\n",
		   i, err);
	    continue;
	}

	if (transferType != kUSBBulk) {
	    continue;
	}
	
	if ((direction == kUSBIn) && !inPipeRef) {
	    inPipeRef = i;
	}
	if ((direction == kUSBOut) && !outPipeRef) {
	    outPipeRef = i;
	}
      }
    }

    if( !inPipeRef || !outPipeRef ) {
      (*intf)->USBInterfaceClose(intf);
      (*intf)->Release(intf);
      intf = NULL;
      return -1;
    }

    return 0;
}


static int do_dev( io_service_t usbDeviceRef )
{   
  IOReturn err;
  IOCFPlugInInterface **iodev;		// requires <IOKit/IOCFPlugIn.h>
  SInt32 score;
  UInt8 numConf;
  IOUSBConfigurationDescriptorPtr confDesc;
  IOUSBFindInterfaceRequest interfaceRequest;
  io_iterator_t	iterator;
  io_service_t usbInterfaceRef;
  
  err = IOCreatePlugInInterfaceForService(usbDeviceRef, 
					  kIOUSBDeviceUserClientTypeID,
					  kIOCFPlugInInterfaceID, &iodev, &score);
  if (err || !iodev) {
    printf("unable to create plugin. ret = %08x, iodev = %p\n",
	   err, iodev);
		return -1;
    }
  
  err = (*iodev)->QueryInterface(iodev, 
				 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
				 (LPVOID)&usbDev);
  IODestroyPlugInInterface(iodev);				// done with this
  
  if (err || !usbDev) {
    printf("unable to create a device interface. ret = %08x, dev = %p\n",
	   err, usbDev);
    return -1;
  }
  
  err = (*usbDev)->USBDeviceOpen(usbDev);
  if (err) {
    printf("unable to open device. ret = %08x\n", err);
    return -1;
  }
  err = (*usbDev)->GetNumberOfConfigurations(usbDev, &numConf);
  if (err || !numConf) {
    printf("unable to obtain the number of configurations. ret = %08x\n", err);
    return -1;
  }

  err = (*usbDev)->GetConfigurationDescriptorPtr(usbDev, 0, &confDesc);			// get the first config desc (index 0)
  if (err) {
      printf("unable to get config descriptor for index 0\n");
      return -1;
  }
  

  err = (*usbDev)->SetConfiguration(usbDev, confDesc->bConfigurationValue);
  if (err) {
    printf("unable to set the configuration\n");
    return -1;
  }

  // requested class
  interfaceRequest.bInterfaceClass = kIOUSBFindInterfaceDontCare;
  // requested subclass
  interfaceRequest.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;		
  // requested protocol
  interfaceRequest.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;		
  // requested alt setting
  interfaceRequest.bAlternateSetting = kIOUSBFindInterfaceDontCare;		
  
  err = (*usbDev)->CreateInterfaceIterator(usbDev, &interfaceRequest, &iterator);
  if (err) {
    printf("unable to create interface iterator\n");
    return -1;
  }
    
  while( (usbInterfaceRef = IOIteratorNext(iterator)) ) {
    if( do_intf( usbInterfaceRef ) == 0 ) {
      IOObjectRelease(iterator);
      iterator = 0;
      return 0;
    }
  } 

    
  IOObjectRelease(iterator);
  iterator = 0;
  return -1;


}

static int iokit_io_init( char *dev __attribute__((unused)) )
{

  kern_return_t err;
  CFMutableDictionaryRef matchingDictionary = 0;
  CFNumberRef numberRef;
  SInt32 idVendor = USB_VID_ATMEL;
  SInt32 idProduct = USB_PID_SAMBA;
  io_iterator_t iterator = 0;
  io_service_t usbDeviceRef;

  
  if( (err = IOMasterPort( MACH_PORT_NULL, &masterPort )) ) {
    printf( "could not create master port, err = %08x\n", err );
    return -1;
  }

  if( !(matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName)) ) {
    printf( "could not create matching dictionary\n" );
    return -1;
  }
  
  if( !(numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type,
				   &idVendor)) ) {
    printf( "could not create CFNumberRef for vendor\n" );
    return -1;
  }
  CFDictionaryAddValue( matchingDictionary, CFSTR(kUSBVendorID), numberRef);
  CFRelease( numberRef );
  numberRef = 0;

  if( !(numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type,
				   &idProduct)) ) {
    printf( "could not create CFNumberRef for product\n" );
    return -1;
  }
  CFDictionaryAddValue( matchingDictionary, CFSTR(kUSBProductID), numberRef);
  CFRelease( numberRef );
  numberRef = 0;
  
  err = IOServiceGetMatchingServices( masterPort, matchingDictionary, &iterator );
  matchingDictionary = 0;  // consumed by the above call


  
  if( (usbDeviceRef = IOIteratorNext( iterator )) ) {
    printf( "found boot agent\n" );

    do_dev( usbDeviceRef );
  } else {
    printf( "can not find boot agent\n" );
    return -1;
  }



  IOObjectRelease(usbDeviceRef);

  IOObjectRelease(iterator);

  
  return samba_init();


  IOObjectRelease(usbDeviceRef);

  IOObjectRelease(iterator);
  return -1;
}


static int iokit_io_cleanup( void )
{
  if( intf ) {
    (*intf)->USBInterfaceClose(intf);
    (*intf)->Release(intf);
    intf = NULL;
  }

  if( usbDev ) {
    (*usbDev)->USBDeviceClose(usbDev);
    (*usbDev)->Release(usbDev);
    usbDev = NULL;
  }
  
  return 0;
}

static int iokit_io_write( void *buff, int len )
{
  if( (*intf)->WritePipe( intf, outPipeRef, buff, (UInt32) len ) !=
      kIOReturnSuccess ) {
    printf( "write error\n");
  }

  return len;
}

static int iokit_io_read( void *buff, int len )
{

   UInt32 size = len;

  if( (*intf)->ReadPipe( intf, inPipeRef, buff, &size ) !=
      kIOReturnSuccess ) {
    printf( "read error\n");
  }

  
  return (int) size;
}

static struct io_driver iokit_driver = {
	.name		= "iokit",
	.io_init	= iokit_io_init,
	.io_cleanup	= iokit_io_cleanup,
	.io_write	= iokit_io_write,
	.io_read	= iokit_io_read,
};

static void __attribute__((constructor)) iokit_on_load(void)
{
	io_driver_register(&iokit_driver);
}