USB driver development

Discussion in 'Mac Programming' started by MrFusion, Nov 8, 2007.

  1. MrFusion macrumors 6502a

    Jun 8, 2005

    I am learning how to develop a USB driver. The code below is how far I get before running into errors. The error being "plugin failed e00002c7".
    I am also not sure what "score" does or means.
    From what I read in the sample codes, documentation and the net, my guess is that I can't get exclusive access to any of the USB devices I have.
    Apple is great, it works with many different USB devices right out of the box. But it sure sucks when you try to learn driver development yourself.

    Any ideas on how I can get exclusieve access to a device? Or do you know a cheap USB-device that Mac OS X doesn't know?

    	kern_return_t		err;	//error
    	//get comm port
    	mach_port_t 	masterPort = 0;	//requires mach.h
    	err = IOMasterPort(MACH_PORT_NULL, &masterPort); //get comm port
    	//build matching dictionary
    	CFMutableDictionaryRef 	matchingDictionary = 0;		// requires <IOKit/IOKitLib.h>
    	matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName); //USB class devices
    	[self printDic:matchingDictionary];
        io_iterator_t 		iterator = 0;
    	IOServiceGetMatchingServices(masterPort,matchingDictionary,&iterator); //matchingDictionary retaincount--
        io_service_t	usbDeviceRef;
    	int i=0;
        while (usbDeviceRef = IOIteratorNext(iterator)) {i++;
    		io_connect_t    dataPort;
    		err = IOServiceOpen(usbDeviceRef, mach_task_self(), 0, &dataPort);//open connection
    			printf("Found device %i: %p - %p - ",i, (void*)usbDeviceRef, (void*)dataPort);
    			io_name_t myclass;
    			printf("class: %s - ",myclass);
    			io_name_t devName;
    			printf("name: %s - ", devName);
    		SInt32 				score;
    		IOCFPlugInInterface 		**iodev;		// requires <IOKit/IOCFPlugIn.h>
    		err = IOCreatePlugInInterfaceForService(usbDeviceRef, kIOUSBDeviceInterfaceID182, kIOCFPlugInInterfaceID, &iodev, &score);		
    		if (err){
    			printf("plugin failed %08x \n",err);
    		} else {printf("plugin ok \n");}
    		IOObjectRelease(usbDeviceRef);			// no longer need this reference
    		IOServiceClose(dataPort);//close connection
  2. psychofreak Retired


    May 16, 2006
    If you can get hold of it, my grandad has an old HP Scanjet 4400c with no OSX driver...
  3. ChrisA macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    It's a lot of work because you need to get and learn another development environment but..... You get a small microcontroller like a PIC or AVR with a USB interface and then you flash the controller with code that makes it do "whatever". This way you have control over both ends of the USB cable. It helps when you own the code inside the USB device that can drive a diagnostic display. There is sample code you can use for the controllers and they are not expensive. An Atmel "butteryfly" is a small device has a USB port a five way "joy switch" and an LCD for $21 at Digikey. I like the Atmel AVR over the PIC because the AVR can be programmed with GNU gcc in C but PIC is the market leader and there is a free (open source) SDCC C compiler for it. either works.
  4. MrFusion thread starter macrumors 6502a

    Jun 8, 2005
    Actually I am also interested in learning PIC programming. But I have no electronics at home, yet.

    Could you give me a checklist of equipment I have to buy in order to flash pic controllers and create a cheap usb-device (like you mention above).
    Soldering iron
    Soldering bolt
    LCD display
    Atmel "butteryfly

    Also what kind of software do I need to pull this of. Can I do it from within xCode?
    I would like to stay out of the kernel if possible. Will your suggestion allow for just a user space driver?

    Thanks for the offer, but it's unlikely I am in London anytime soon.
  5. Krevnik macrumors 68040


    Sep 8, 2003
    I agree that a PIC or other USB client microcontroller is the way to go for learning this stuff if you can.

    "score" is the score placed onto the kernel driver that matched the device. It is used to help the OS determine which driver to use in the case of conflicts.

    And there is a problem with asking how you can get exclusive access... EVERYTHING is exclusive access by default. You have to /disable/ exclusive access at the driver level, which most kernel clients do when they export an interface into userland.

    And there is documentation on how to connect/control USB devices from a user application found here:

    It should help out a bit.
  6. MrFusion thread starter macrumors 6502a

    Jun 8, 2005
    As a start, thanks for the reply.

    I have been through that linked doc already a few times. I have also been through a few others, I have even delved into the header files for iokit framework. I have "USBSimpleExample" as an example. Yet I still get stuck with that exclusieve access problem, as seemingly many other noobs on the net. How do I have to ask for access? I don't want to mess around with the kernel. The most basic USB kernel driver that I can send commands from within user space is more than enough.
  7. ChrisA macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    Don't settle so fast in the PIC. I'd choose which to get based on
    the software development environment associated with the controllers. Most
    of the time you spend will be software design, code and debugging.
    Find a system that allows easy on-chip debugging or simulation

    You can buy a device pre-built for $21. Cheaper than you can buy the parts.
    All of the controller companies make a "development board" that has a controller
    and some other stuff on it. Many times these are sold cheap because the company
    wants to make it easy for you to get hooked on their line of controllers

    How to equip a lab so you cn build your own? At a dead minimum you will need a
    a meter, soldering equipment "bread boards" both solderless and those PCBs,
    an assortment of parts. hand tools and a small power supply. From there you might want to expand with more and better test equipment or maybe software for schematic
    capture symuation an PCB layout.. Used test equipmnt is good. recently bought
    a 1980's vintage tektronix scope for just over $100. it was $3,400 when new.
    Working on eletronics is SO much easier when you can see what is inside the wire.
    'scopes are usfull.
  8. ChrisA macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    Without knowing anything about this, coming from a UNIX/Linux background and guessing.... I'd say to try fcntl() and "F_SETLK". In other works the device should lock just like any other file. See man fcntl(2)

Share This Page