Debian Codegraph @

unix/osx/libfli-usb-sys.c

raw
1 /*
2
3 Copyright (c) 2002 Finger Lakes Instrumentation (FLI), L.L.C.
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12
13 Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials
16 provided with the distribution.
17
18 Neither the name of Finger Lakes Instrumentation (FLI), LLC
19 nor the names of its contributors may be used to endorse or
20 promote products derived from this software without specific
21 prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35
36 ======================================================================
37
38 Finger Lakes Instrumentation, L.L.C. (FLI)
39 web: http://www.fli-cam.com
40 email: [email protected]
41
42 */
43
44 #include "libfli-usb-sys.h"
45
46 //==========================================================================
47 // MAC_FLI_LIST
48 // Function to enumerate connected devices
49 //
50 //==========================================================================
51 long mac_fli_list(flidomain_t domain, char ***names)
52 {
53 char **list = NULL;
54 if((list = malloc((MAX_SEARCH + 1) * sizeof(char *))) == NULL)
55 {
56 return -ENOMEM;
57 }
58
59 int id = 0;
60
61 kern_return_t kret;
62 IOReturn ioret;
63
64 io_name_t deviceName;
65
66 UInt16 usbProduct, usbVendor;
67
68 UInt32 locationID;
69
70 SInt32 score;
71 io_service_t usbDevice;
72
73 io_iterator_t dev_iterator;
74
75 IOUSBDeviceInterface182 **dev_int;
76 IOCFPlugInInterface **plugInInterface;
77
78 CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
79 if(!matchingDict)
80 {
81 debug(FLIDEBUG_FAIL, "mac_fli_list: could not get matching dictionary");
82 return -1;
83 }
84
85 IONotificationPortRef gNotifyPort = IONotificationPortCreate(kIOMasterPortDefault);
86 CFRunLoopSourceRef runLoopSource = IONotificationPortGetRunLoopSource(gNotifyPort);
87 CFRunLoopRef gRunLoop = CFRunLoopGetCurrent();
88 CFRunLoopAddSource(gRunLoop, runLoopSource, kCFRunLoopDefaultMode);
89
90 kret = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &dev_iterator);
91 if(kret)
92 {
93 debug(FLIDEBUG_FAIL, "mac_fli_list: could not get Matching Services");
94 return -1;
95 }
96
97 while((usbDevice = IOIteratorNext(dev_iterator)))
98 {
99 ioret = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);
100 if(ioret)
101 {
102 debug(FLIDEBUG_FAIL, "mac_fli_list: could not get Plug In Interface");
103 return -1;
104 }
105
106 kret = IORegistryEntryGetName(usbDevice, deviceName);
107 if(kret)
108 {
109 debug(FLIDEBUG_FAIL, "mac_fli_list: could not get Registry Entry Name");
110 return -1;
111 }
112
113 kret = IOObjectRelease(usbDevice);
114 if(kret)
115 {
116 debug(FLIDEBUG_FAIL, "mac_fli_list: could not release device");
117 return -1;
118 }
119
120 ioret = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID182), (LPVOID*)&dev_int);
121 if(ioret)
122 {
123 debug(FLIDEBUG_FAIL, "mac_fli_list: could not Query Plug In Interface");
124 return -1;
125 }
126
127 (*plugInInterface)->Release(plugInInterface);
128
129 ioret = (*dev_int)->GetDeviceVendor(dev_int, &usbVendor);
130 if(ioret)
131 {
132 debug(FLIDEBUG_FAIL, "mac_fli_list: could not get Device Vendor");
133 return -1;
134 }
135
136 // Check if its an FLI device
137 if(usbVendor == FLI_VENDOR_ID)
138 {
139 ioret = (*dev_int)->GetDeviceProduct(dev_int, &usbProduct);
140 if(ioret)
141 {
142 debug(FLIDEBUG_FAIL, "mac_fli_list: could not get Device Product");
143 return -1;
144 }
145
146 switch(usbProduct)
147 {
148 FLIUSB_PRODUCTS
149 break;
150 default:
151 continue;
152 }
153
154 ioret = (*dev_int)->GetLocationID(dev_int, &locationID);
155 if(ioret)
156 {
157 debug(FLIDEBUG_FAIL, "mac_fli_list: could not get location id");
158 return -1;
159 }
160
161 asprintf(&list[id], "%08x;%s", (unsigned int)locationID, deviceName);
162
163 id++;
164 }
165
166 }
167
168 IOObjectRelease(dev_iterator);
169
170 if(id == 0)
171 {
172 *names = NULL;
173 free(list);
174 return 0;
175 }
176
177 list[id] = NULL;
178 *names = list;
179
180 return 0;
181 }
182
183 //-------------------------------------------------------------------------
184
185
186 //==========================================================================
187 // MAC_FLI_CONNECT
188 // Function to enumerate and establish connection to device
189 //
190 //==========================================================================
191 long mac_fli_connect(flidev_t dev, char *name, long domain)
192 {
193 debug(FLIDEBUG_INFO, "mac_fli_connect");
194
195 CHKDEVICE(dev);
196
197 fli_unixio_t *io;
198
199 if(name == NULL)
200 {
201 return -EINVAL;
202 }
203
204 /* Lock functions should be set before any other functions used */
205 DEVICE->fli_lock = mac_fli_lock;
206 DEVICE->fli_unlock = mac_fli_unlock;
207
208 DEVICE->domain = domain & 0x00ff;
209 DEVICE->devinfo.type = domain & 0xff00;
210
211 if((io = calloc(1, sizeof(fli_unixio_t))) == NULL)
212 {
213 return -ENOMEM;
214 }
215
216 if(!(io->fd = open(name, O_RDWR)))
217 {
218 return -ENOMEM;
219 }
220
221 int r;
222
223 // Note: unix_usb_connect is defined to mac_usb_connect in OSX
224
225 if((r = mac_usb_connect(dev, io, name)) != 0)
226 {
227 close(io->fd);
228 free(io);
229 return r;
230 }
231
232 DEVICE->fli_io = unix_usbio;
233
234 switch(DEVICE->devinfo.type)
235 {
236 case FLIDEVICE_CAMERA: //0x100
237 DEVICE->fli_open = fli_camera_open;
238 DEVICE->fli_close = fli_camera_close;
239 DEVICE->fli_command = fli_camera_command;
240 break;
241
242 case FLIDEVICE_FOCUSER: //0x300
243 DEVICE->fli_open = fli_focuser_open;
244 DEVICE->fli_close = fli_focuser_close;
245 DEVICE->fli_command = fli_focuser_command;
246 break;
247
248 case FLIDEVICE_FILTERWHEEL: //0x200
249 DEVICE->fli_open = fli_filter_open;
250 DEVICE->fli_close = fli_filter_close;
251 DEVICE->fli_command = fli_filter_command;
252 break;
253
254 default:
255 mac_usb_disconnect(dev,io);
256 close(io->fd);
257 free(io);
258 return -EINVAL;
259 }
260
261 DEVICE->io_data = io;
262
263 //char *nameCopy = NULL;
264 //asprintf(&nameCopy, "%s", name);
265
266 // xstrdup(name);
267 DEVICE->name = strdup(name); //nameCopy;
268
269 DEVICE->io_timeout = 3000; // 60 * 1000; /* 1 min. */
270
271 debug(FLIDEBUG_INFO, "mac_fli_connect: connected");
272
273 return 0;
274 }
275 //-------------------------------------------------------------------------
276
277
278 //==========================================================================
279 // MAC_FLI_DISCONNECT
280 // Disconnect device
281 //
282 //==========================================================================
283 long mac_fli_disconnect(flidev_t dev, fli_unixio_t *io)
284 {
285 int err = 0;
286
287 CHKDEVICE(dev);
288
289 if((io = DEVICE->io_data) == NULL)
290 {
291 debug(FLIDEBUG_FAIL, "mac_fli_disconnect: io data error");
292 return -EINVAL;
293 }
294
295 switch(DEVICE->domain)
296 {
297 case FLIDOMAIN_USB:
298 err = mac_usb_disconnect(dev,io);
299 break;
300
301 default:
302 break;
303 }
304
305 if(close(io->fd))
306 {
307 if(!err)
308 {
309 err = -errno;
310 }
311 }
312
313 free(DEVICE->io_data);
314 free(DEVICE->sys_data);
315
316 DEVICE->io_data = NULL;
317 DEVICE->sys_data = NULL;
318
319 DEVICE->fli_lock = NULL;
320 DEVICE->fli_unlock = NULL;
321 DEVICE->fli_io = NULL;
322 DEVICE->fli_open = NULL;
323 DEVICE->fli_close = NULL;
324 DEVICE->fli_command = NULL;
325
326 free(DEVICE->name);
327 DEVICE->name = NULL;
328
329 debug(FLIDEBUG_INFO, "mac_fli_disconnect: disconnected");
330
331 return err;
332 }
333 //-------------------------------------------------------------------------
334
335
336 //==========================================================================
337 // MAC_USB_CONNECT
338 // connect device
339 //
340 //==========================================================================
341 long mac_usb_connect(flidev_t dev, fli_unixio_t *io, char *name)
342 {
343 bool found = false;
344
345 kern_return_t kret;
346 IOReturn ioret;
347
348 io_name_t deviceName;
349
350 UInt16 usbRelease, usbProduct, usbVendor;
351 UInt32 locationID;
352
353 SInt32 score;
354 io_service_t usbDevice;
355
356 io_iterator_t dev_iterator;
357
358 IOUSBDeviceInterface182 **dev_int;
359 IOCFPlugInInterface **plugInInterface;
360
361 CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
362 if(!matchingDict)
363 {
364 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not get matching dictionary");
365 return -ENODEV;
366 }
367
368 IONotificationPortRef gNotifyPort = IONotificationPortCreate(kIOMasterPortDefault);
369 CFRunLoopSourceRef runLoopSource = IONotificationPortGetRunLoopSource(gNotifyPort);
370 CFRunLoopRef gRunLoop = CFRunLoopGetCurrent();
371 CFRunLoopAddSource(gRunLoop, runLoopSource, kCFRunLoopDefaultMode);
372
373 kret = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &dev_iterator);
374 if(kret)
375 {
376 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not get Matching Services");
377 return -ENODEV;
378 }
379
380 while((usbDevice = IOIteratorNext(dev_iterator)))
381 {
382 ioret = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);
383 if(ioret)
384 {
385 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not get Plug In Interface");
386 return -ENODEV;
387 }
388
389 kret = IORegistryEntryGetName(usbDevice, deviceName);
390 if(kret)
391 {
392 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not get Registry Entry Name");
393 return -ENODEV;
394 }
395
396 kret = IOObjectRelease(usbDevice);
397 if(kret)
398 {
399 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not release device");
400 return -ENODEV;
401 }
402
403 ioret = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID182), (LPVOID*)&dev_int);
404 if(ioret)
405 {
406 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not Query Plug In Interface");
407 return -ENODEV;
408 }
409
410 (*plugInInterface)->Release(plugInInterface);
411
412 ioret = (*dev_int)->GetDeviceVendor(dev_int, &usbVendor);
413 if(ioret)
414 {
415 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not get Device Vendor");
416 return -ENODEV;
417 }
418
419 // Check if its an FLI device
420 if(usbVendor == FLI_VENDOR_ID)
421 {
422 ioret = (*dev_int)->GetDeviceProduct(dev_int, &usbProduct);
423 if(ioret)
424 {
425 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not get Device Product");
426 return -ENODEV;
427 }
428
429 switch(usbProduct)
430 {
431 FLIUSB_PRODUCTS
432 break;
433 default:
434 continue;
435 }
436
437 ioret = (*dev_int)->GetDeviceReleaseNumber(dev_int, &usbRelease);
438 if(ioret)
439 {
440 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not get Device Release Number");
441 return -ENODEV;
442 }
443
444 ioret = (*dev_int)->GetLocationID(dev_int, &locationID);
445 if(ioret)
446 {
447 debug(FLIDEBUG_FAIL, "mac_usb_connect: could not get location id");
448 return -ENODEV;
449 }
450
451 char *deviceLocIDName = NULL;
452
453 asprintf(&deviceLocIDName, "%08x", (unsigned int)locationID);
454
455 if(strcmp(name, deviceLocIDName) == 0)
456 {
457 found = true;
458
459 debug(FLIDEBUG_INFO, "mac_usb_connect: connecting to usb device");
460
461 //asprintf(&DEVICE->devinfo.model, "%s", deviceName);
462 //asprintf(&DEVICE->devinfo.devnam, "%08x", locationID);
463
464 DEVICE->devinfo.devid = usbProduct;
465 DEVICE->devinfo.fwrev = usbRelease;
466
467 ioret = (*dev_int)->USBDeviceOpenSeize(dev_int);
468 if(ioret)
469 {
470 debug(FLIDEBUG_FAIL, "mac_usb_connect: USBDeviceOpenSeize failed");
471 return -ENODEV;
472 }
473
474 // Reset the usb device so you will be able to fully configure the device
475 ioret = (*dev_int)->ResetDevice(dev_int);
476 if(ioret)
477 {
478 debug(FLIDEBUG_FAIL, "mac_usb_connect: ResetDevice failed");
479 return -ENODEV;
480 }
481
482 if(mac_usb_find_interfaces(dev, dev_int) != kIOReturnSuccess)
483 {
484 debug(FLIDEBUG_FAIL, "mac_usb_connect: mac_usb_find_interfaces failed");
485 (*dev_int)->USBDeviceClose(dev_int);
486 (*dev_int)->Release(dev_int);
487 return -ENODEV;
488 }
489
490 debug(FLIDEBUG_INFO, "mac_usb_connect: connected to usb device!");
491
492 break;
493 }
494
495 }
496 }
497
498 IOObjectRelease(dev_iterator);
499
500 if(!found)
501 {
502 return -ENODEV;
503 }
504
505 return 0;
506 }
507
508 IOReturn mac_usb_configure_device(IOUSBDeviceInterface182 **dev)
509 {
510 //UInt8 numConfig; // configNum
511 IOReturn kr;
512 IOUSBConfigurationDescriptorPtr configDesc;
513
514 /*
515 kr = (*dev)->GetConfiguration(dev, &configNum);
516 if(kr)
517 {
518 debug(FLIDEBUG_FAIL, "mac_usb_configure_device: could not get configuration");
519 return -1;
520 }
521 */
522 // Device is already configured (Was already plugged in and connected to in the past, reconfiguring will break it)
523 /*if(configNum == 0)
524 {
525 debug(FLIDEBUG_INFO, "mac_usb_configure_device: already configured %d", configNum);
526 return kIOReturnSuccess;
527 }*/
528
529 /*
530 kr = (*dev)->GetNumberOfConfigurations(dev, &numConfig);
531 if(!numConfig)
532 {
533 debug(FLIDEBUG_FAIL, "mac_usb_configure_device: could not get number of configurations");
534 return -1;
535 }
536 */
537
538 kr = (*dev)->GetConfigurationDescriptorPtr(dev, 0, &configDesc);
539 if(kr)
540 {
541 debug(FLIDEBUG_FAIL, "mac_usb_configure_device: could not get configuration descriptor");
542 return -1;
543 }
544
545 kr = (*dev)->SetConfiguration(dev, configDesc->bConfigurationValue);
546 if(kr)
547 {
548 debug(FLIDEBUG_FAIL, "mac_usb_configure_device: could not set configuration");
549 return -1;
550 }
551 /*
552 UInt8 configNum2;
553 kr = (*dev)->GetConfiguration(dev, &configNum2);
554 if(kr)
555 {
556 debug(FLIDEBUG_FAIL, "mac_usb_configure_device: could not get configuration");
557 return -1;
558 }*/
559
560 debug(FLIDEBUG_INFO, "mac_usb_configure_device: Configured to %d", configDesc->bConfigurationValue);
561 //debug(FLIDEBUG_INFO, "mac_usb_configure_device: %d %d %d %d", configNum, configNum2, numConfig, configDesc->bConfigurationValue);
562
563 return kIOReturnSuccess;
564 }
565
566 IOReturn mac_usb_find_interfaces(flidev_t dev, IOUSBDeviceInterface182 **device)
567 {
568 IOReturn kr;
569 IOUSBFindInterfaceRequest request;
570 io_iterator_t iterator;
571 io_service_t usbInterface;
572 IOCFPlugInInterface **plugInInterface = NULL;
573 IOUSBInterfaceInterface190 **interface = NULL;
574 HRESULT result;
575 SInt32 score;
576 UInt8 interfaceClass;
577 UInt8 interfaceSubClass;
578 UInt8 interfaceNumEndpoints;
579
580 request.bInterfaceClass = kIOUSBFindInterfaceDontCare;
581 request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
582 request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
583 request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
584
585 kr = (*device)->CreateInterfaceIterator(device, &request, &iterator);
586
587 while((usbInterface = IOIteratorNext(iterator)))
588 {
589 kr = IOCreatePlugInInterfaceForService(usbInterface, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);
590
591 kr = IOObjectRelease(usbInterface);
592 if((kr != kIOReturnSuccess) || !plugInInterface)
593 {
594 debug(FLIDEBUG_FAIL, "mac_usb_find_interfaces: Unable to create a plugin");
595 return kr;
596 }
597
598 result = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID190), (LPVOID *) &interface);
599 (*plugInInterface)->Release(plugInInterface);
600
601 if(result || !interface)
602 {
603 debug(FLIDEBUG_FAIL, "mac_usb_find_interfaces: Couldn’t create a device interface");
604 return kr;
605 }
606
607 kr = (*interface)->GetInterfaceClass(interface, &interfaceClass);
608 kr = (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass);
609
610 kr = (*interface)->USBInterfaceOpen(interface);
611 if(kr != kIOReturnSuccess)
612 {
613 debug(FLIDEBUG_FAIL, "mac_usb_find_interfaces: Unable to open interface (%08x)", kr);
614 (*interface)->Release(interface);
615 return kr;
616 }
617
618 kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
619 if(kr != kIOReturnSuccess)
620 {
621 debug(FLIDEBUG_FAIL, "mac_usb_find_interfaces: Unable to get number of endpoints (%08x)", kr);
622 (*interface)->USBInterfaceClose(interface);
623 (*interface)->Release(interface);
624 return kr;
625 }
626
627 mac_device_info *deviceInfo = (mac_device_info*) malloc(sizeof(mac_device_info));
628 DEVICE->sys_data = deviceInfo;
629
630 DEVICE_DATA->interface = interface;
631 DEVICE_DATA->device = device;
632 DEVICE_DATA->interfaceNumEndpoints = interfaceNumEndpoints;
633
634 if(DEVICE->devinfo.devid == FLIUSB_PROLINECAM)
635 {
636 DEVICE_DATA->epRead = 2;
637 DEVICE_DATA->epWrite = 1;
638 DEVICE_DATA->epReadBulk = 3;
639 }
640 else if(DEVICE->devinfo.devid == FLIUSB_MAXCAM)
641 {
642 DEVICE_DATA->epRead = 2;
643 DEVICE_DATA->epWrite = 3;
644 DEVICE_DATA->epReadBulk = 2;
645 }
646 else if(DEVICE->devinfo.devid == FLIUSB_FILTERWHEEL || FLIUSB_FOCUSER || FLIUSB_STEPPER)
647 {
648 DEVICE_DATA->epRead = 1;
649 DEVICE_DATA->epWrite = 2;
650 DEVICE_DATA->epReadBulk = 1;
651 }
652
653 // Debug pipes
654 /*
655 int pipeRef;
656 for (pipeRef = 1; pipeRef <= interfaceNumEndpoints; pipeRef++)
657 {
658 IOReturn kr2;
659 UInt8 direction;
660 UInt8 number;
661 UInt8 transferType;
662 UInt16 maxPacketSize;
663 UInt8 interval;
664 char *message;
665
666 kr2 = (*interface)->GetPipeProperties(interface,
667 pipeRef, &direction,
668 &number, &transferType,
669 &maxPacketSize, &interval);
670 if (kr2 != kIOReturnSuccess)
671 printf("Unable to get properties of pipe %d (%08x)\n",
672 pipeRef, kr2);
673 else
674 {
675 printf("PipeRef %d: ", pipeRef);
676 switch (direction)
677 {
678 case kUSBOut:
679 message = "out";
680 break;
681 case kUSBIn:
682 message = "in";
683 break;
684 case kUSBNone:
685 message = "none";
686 break;
687 case kUSBAnyDirn:
688 message = "any";
689 break;
690 default:
691 message = "???";
692 }
693 printf("direction %s, ", message);
694
695 switch (transferType)
696 {
697 case kUSBControl:
698 message = "control";
699 break;
700 case kUSBIsoc:
701 message = "isoc";
702 break;
703 case kUSBBulk:
704 message = "bulk";
705 break;
706 case kUSBInterrupt:
707 message = "interrupt";
708 break;
709 case kUSBAnyType:
710 message = "any";
711 break;
712 default:
713 message = "???";
714 }
715 printf("transfer type %s, maxPacketSize %d\n", message,
716 maxPacketSize);
717 }
718 }
719 */
720 // We only need 1 interface for FLI devices
721 return kIOReturnSuccess;
722 }
723
724 debug(FLIDEBUG_WARN, "mac_usb_find_interfaces: Found 0 interfaces, setting configuration");
725
726 if(mac_usb_configure_device(device) == kIOReturnSuccess)
727 {
728 return mac_usb_find_interfaces(dev, device);
729 }
730
731 return -1;
732 }
733
734
735 //==========================================================================
736 // MAC_USB_DISCONNECT
737 // Delete USB interface Data
738 //
739 //==========================================================================
740 long mac_usb_disconnect(flidev_t dev, fli_unixio_t *io)
741 {
742 debug(FLIDEBUG_INFO, "mac_usb_disconnect");
743
744 (*DEVICE_DATA->interface)->USBInterfaceClose(DEVICE_DATA->interface);
745 (*DEVICE_DATA->interface)->Release(DEVICE_DATA->interface);
746
747 (*DEVICE_DATA->device)->USBDeviceClose(DEVICE_DATA->device);
748 (*DEVICE_DATA->device)->Release(DEVICE_DATA->device);
749
750 return 0;
751 }
752 //-------------------------------------------------------------------------
753
754
755 //==========================================================================
756 // MAC_BULKTRANSFER
757 // IO control call to start piperead/pipewrite
758 //
759 //==========================================================================
760 long mac_bulktransfer(flidev_t dev, int ep, void *buf, long *tlen)
761 {
762 if(ep & USB_DIR_IN)
763 {
764 mac_usb_piperead(dev, buf, *tlen, DEVICE_DATA->epReadBulk, DEVICE->io_timeout);
765 }
766 else
767 {
768 mac_usb_pipewrite(dev, buf, *tlen, DEVICE_DATA->epWrite, DEVICE->io_timeout);
769 }
770 return 0;
771 }
772
773
774 //==========================================================================
775 // MAC_BULKREAD
776 // IO control call to read
777 //
778 //==========================================================================
779 long mac_bulkread(flidev_t dev, void *buf, long *rlen)
780 {
781 mac_usb_piperead(dev, buf, *rlen, DEVICE_DATA->epRead, DEVICE->io_timeout);
782 return 0;
783 }
784 //-------------------------------------------------------------------------
785
786
787 //==========================================================================
788 // MAC_BULKWRITE
789 // IO control call to write
790 //
791 //==========================================================================
792 long mac_bulkwrite(flidev_t dev, void *buf, long *wlen)
793 {
794 mac_usb_pipewrite(dev, buf, *wlen, DEVICE_DATA->epWrite, DEVICE->io_timeout);
795 return 0;
796 }
797
798 //==========================================================================
799 // PIPEREAD
800 // Function to Read input calls ReadPipeTO
801 //
802 //
803 //==========================================================================
804 ssize_t mac_usb_piperead(flidev_t dev, void *buf, size_t size, unsigned pipe, unsigned timeout)
805 {
806 CHKDEVICE(dev);
807
808 IOReturn ioret;
809
810 if(pipe <= 0 || pipe > DEVICE_DATA->interfaceNumEndpoints)
811 {
812 debug(FLIDEBUG_FAIL, "mac_usb_piperead: invalid pipe number (%u of %u)", pipe, DEVICE_DATA->interfaceNumEndpoints);
813 return -EINVAL;
814 }
815
816 ioret = (*DEVICE_DATA->interface)->ReadPipeTO(DEVICE_DATA->interface, pipe, buf, (UInt32*) &size, 0, timeout);
817 if(ioret)
818 {
819 debug(FLIDEBUG_FAIL, "mac_usb_piperead: read error: %x, size: %d", ioret, (int)size);
820
821 if(ioret != kIOReturnSuccess)
822 {
823 debug(FLIDEBUG_FAIL, "mac_usb_piperead: aborted");
824 ioret = (*DEVICE_DATA->interface)->ClearPipeStallBothEnds(DEVICE_DATA->interface, pipe);
825 //ioret = (*DEVICE_DATA->interface)->ClearPipeStall(DEVICE_DATA->interface, true);
826 if(ioret)
827 {
828 debug(FLIDEBUG_FAIL, "mac_usb_piperead: Pipe Stalled: %x", ioret);
829 }
830 }
831
832 return -EINVAL;
833 }
834
835 return size;
836 }
837 //-------------------------------------------------------------------------
838
839
840 //==========================================================================
841 // PIPEWRITE
842 // Function to write data to device calls WritePipeTo
843 //
844 //==========================================================================
845 ssize_t mac_usb_pipewrite(flidev_t dev, void *buf, size_t size, unsigned pipe, unsigned timeout)
846 {
847 CHKDEVICE(dev);
848
849 IOReturn ioret;
850
851 int piperef = DEVICE_DATA->epWrite;
852
853 if(piperef <= 0 || piperef > DEVICE_DATA->interfaceNumEndpoints)
854 {
855 debug(FLIDEBUG_FAIL, "mac_usb_pipewrite: invalid pipe number (%u of %u)", piperef, DEVICE_DATA->interfaceNumEndpoints);
856 return -EINVAL;
857 }
858
859 ioret = (*DEVICE_DATA->interface)->WritePipeTO(DEVICE_DATA->interface, piperef, buf, size, timeout, 0);
860 if(ioret)
861 {
862 debug(FLIDEBUG_FAIL, "mac_usb_pipewrite: write error %x", ioret);
863 return -EINVAL;
864 }
865
866 return size;
867 }
868 //-------------------------------------------------------------------------
869
870
871 //==========================================================================
872 // MAC_FLI_LOCK
873 // Function to lock access to device
874 //
875 //==========================================================================
876 long mac_fli_lock(flidev_t dev)
877 {
878
879 fli_unixio_t *io = DEVICE->io_data;
880
881 if(io == NULL)
882 {
883 return -ENODEV;
884 }
885
886 if(!flock(io->fd, LOCK_EX))
887 {
888 return -errno;
889 }
890
891 return 0;
892 }
893 //-------------------------------------------------------------------------
894
895
896 //==========================================================================
897 // MAC_FLI_UNLOCK
898 // Function to unlock access to device
899 //
900 //==========================================================================
901 long mac_fli_unlock(flidev_t dev)
902 {
903 fli_unixio_t *io = DEVICE->io_data;
904 if(io == NULL)
905 {
906 return -ENODEV;
907 }
908
909 if(!flock(io->fd, LOCK_UN))
910 {
911 return -errno;
912 }
913
914 return 0;
915 }
916 //-------------------------------------------------------------------------