1 /*
2  * darwin backend for libusb 1.0
3  * Copyright (C) 2008-2009 Nathan Hjelm <hjelmn@users.sourceforge.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #if !defined(LIBUSB_DARWIN_H)
21 #define LIBUSB_DARWIN_H
22 
23 #include "libusbi.h"
24 
25 #include <IOKit/IOCFBundle.h>
26 #include <IOKit/usb/IOUSBLib.h>
27 #include <IOKit/IOCFPlugIn.h>
28 
29 /* IOUSBInterfaceInferface */
30 #if defined (kIOUSBInterfaceInterfaceID300)
31 
32 #define usb_interface_t IOUSBInterfaceInterface300
33 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID300
34 #define InterfaceVersion 300
35 
36 #elif defined (kIOUSBInterfaceInterfaceID245)
37 
38 #define usb_interface_t IOUSBInterfaceInterface245
39 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID245
40 #define InterfaceVersion 245
41 
42 #elif defined (kIOUSBInterfaceInterfaceID220)
43 
44 #define usb_interface_t IOUSBInterfaceInterface220
45 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID220
46 #define InterfaceVersion 220
47 
48 #elif defined (kIOUSBInterfaceInterfaceID197)
49 
50 #define usb_interface_t IOUSBInterfaceInterface197
51 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID197
52 #define InterfaceVersion 197
53 
54 #elif defined (kIOUSBInterfaceInterfaceID190)
55 
56 #define usb_interface_t IOUSBInterfaceInterface190
57 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID190
58 #define InterfaceVersion 190
59 
60 #elif defined (kIOUSBInterfaceInterfaceID182)
61 
62 #define usb_interface_t IOUSBInterfaceInterface182
63 #define InterfaceInterfaceID kIOUSBInterfaceInterfaceID182
64 #define InterfaceVersion 182
65 
66 #else
67 
68 #error "IOUSBFamily is too old. Please upgrade your OS"
69 
70 #endif
71 
72 /* IOUSBDeviceInterface */
73 #if defined (kIOUSBDeviceInterfaceID320)
74 
75 #define usb_device_t    IOUSBDeviceInterface320
76 #define DeviceInterfaceID kIOUSBDeviceInterfaceID320
77 #define DeviceVersion 320
78 
79 #elif defined (kIOUSBDeviceInterfaceID300)
80 
81 #define usb_device_t    IOUSBDeviceInterface300
82 #define DeviceInterfaceID kIOUSBDeviceInterfaceID300
83 #define DeviceVersion 300
84 
85 #elif defined (kIOUSBDeviceInterfaceID245)
86 
87 #define usb_device_t    IOUSBDeviceInterface245
88 #define DeviceInterfaceID kIOUSBDeviceInterfaceID245
89 #define DeviceVersion 245
90 
91 #elif defined (kIOUSBDeviceInterfaceID197)
92 
93 #define usb_device_t    IOUSBDeviceInterface197
94 #define DeviceInterfaceID kIOUSBDeviceInterfaceID197
95 #define DeviceVersion 197
96 
97 #elif defined (kIOUSBDeviceInterfaceID187)
98 
99 #define usb_device_t    IOUSBDeviceInterface187
100 #define DeviceInterfaceID kIOUSBDeviceInterfaceID187
101 #define DeviceVersion 187
102 
103 #elif defined (kIOUSBDeviceInterfaceID182)
104 
105 #define usb_device_t    IOUSBDeviceInterface182
106 #define DeviceInterfaceID kIOUSBDeviceInterfaceID182
107 #define DeviceVersion 182
108 
109 #else
110 
111 #error "IOUSBFamily is too old. Please upgrade your OS"
112 
113 #endif
114 
115 typedef IOCFPlugInInterface *io_cf_plugin_ref_t;
116 typedef IONotificationPortRef io_notification_port_t;
117 
118 /* private structures */
119 struct darwin_device_priv {
120   IOUSBDeviceDescriptor dev_descriptor;
121   UInt32                location;
122   char                  sys_path[21];
123   usb_device_t        **device;
124   int                  open_count;
125 };
126 
127 struct darwin_device_handle_priv {
128   int                  is_open;
129   CFRunLoopSourceRef   cfSource;
130   int                  fds[2];
131 
132   struct __darwin_interface {
133     usb_interface_t    **interface;
134     uint8_t              num_endpoints;
135     CFRunLoopSourceRef   cfSource;
136     uint8_t            endpoint_addrs[USB_MAXENDPOINTS];
137   } interfaces[USB_MAXINTERFACES];
138 };
139 
140 struct darwin_transfer_priv {
141   /* Isoc */
142   IOUSBIsocFrame *isoc_framelist;
143 
144   /* Control */
145 #if !defined (LIBUSB_NO_TIMEOUT_DEVICE)
146   IOUSBDevRequestTO req;
147 #else
148   IOUSBDevRequest req;
149 #endif
150 
151   /* Bulk */
152 };
153 
154 enum {
155   MESSAGE_DEVICE_GONE,
156   MESSAGE_ASYNC_IO_COMPLETE
157 };
158 
159 
160 
161 #endif
162