Writing a USB driver

Hi,

I am fairly new to programming for Mac, but I have been trying to write code to get the file path (/dev) of a USB device. I am not even sure if it is showing up in /dev, however the device is being detected by the IOregistry and named as IOVendorSpecific.
I am able to retrieve the name and location ID from the registry but when I attempt to get the file path, nothing is returned using this piece of code:

deviceNameAsCFString = IORegistryEntryCreateCFProperty(device, CFSTR(kIOBSDNameKey), kCFAllocatorDefault,0);

anyone have any ideas how to fix this?
btw I am trying to retrive the file path so I can read incoming data from the USB port using the POSIX API. If there is a better way of doing this, please let me know.

thanks in advance

Mac OS X (10.4.6)

Posted on Jun 11, 2006 8:43 PM

Reply
13 replies

Jun 13, 2006 3:26 PM in response to hybrid11

there is no driver for it, i am just trying to read data from it. As to the other required steps, I will post my whole code, I think I got them all, please let me know if I am missing anything, as I am fairly new to writing drivers for Mac. Most of the code comes from the sample code in the Mac developer section. Here it is:

while(device = IOIteratorNext(iterator)) {

if(device) {
printf("Found a matching USB device. Status: in IF-getFilePath().\n");

kr = IORegistryEntryGetName(device, deviceName);
if (KERN_SUCCESS != kr)
{
deviceName[0] = '\0';
}

//get device name
deviceNameAsCFString = CFStringCreateWithCString(kCFAllocatorDefault, deviceName,
kCFStringEncodingASCII);
printf("Device name: %s.\n", deviceName);
bzero(deviceNameAsCFString, sizeof(deviceNameAsCFString));

//get file path in registry
deviceNameAsCFString = IORegistryEntryCreateCFProperty(device, CFSTR(kIOBSDNameKey),
kCFAllocatorDefault,0);


if (deviceNameAsCFString) {
printf("Found a device name as a CF string. Status: in IF2-getFilePath().\n");

bzero(deviceFilePath,sizeof(deviceFilePath));
devPathLength = strlen( PATHDEV); // PATHDEV is defined in paths.h
strcpy(deviceFilePath, PATHDEV);

//Add "r" before the BSD node name from the I/O Registry
//to specify the raw disk node. The raw disk node receives
//I/O requests directly and does not go through the
//buffer cache.

strcat(deviceFilePath, "r");
gotString = CFStringGetCString( deviceNameAsCFString,
deviceFilePath + strlen(deviceFilePath),
MAXPATHLEN - strlen(deviceFilePath),
kCFStringEncodingASCII);

if (gotString) {
printf("Device file path: %s\nsize: %ld\n", deviceFilePath,sizeof(deviceFilePath));
strcpy(data->path,deviceFilePath);
}
//deviceFilePath will look something like /dev/rdisk1

}
}


thanks in advance.

Mac OS X (10.4.6)

Jun 16, 2006 6:48 PM in response to hybrid11

I am looking up the device by using the IOUSBDevice family, I think that's part of the generic USB mac driver? The device appears in the registry, with the name IOSpecificVendorDevice, hope this helps to explain what I have done so far?
thanks in advance for the help, below is the code I use to create an iterator matching the vendor and product ID dictionary.


matchingDict = IOServiceMatching(kIOUSBDeviceClassName); // Interested in instances of class
// IOUSBDevice and its subclasses
if (!matchingDict)
{
printf("Can't create a USB matching dictionary\n");
mach port_deallocate(mach_taskself(), masterPort);
return -1;
}

// We are interested in all USB Devices (as opposed to USB interfaces). The Common Class Specification
// tells us that we need to specify the idVendor, idProduct, and bcdDevice fields, or, if we're not interested
// in particular bcdDevices, just the idVendor and idProduct. Note that if we were trying to match an
// IOUSBInterface, we would need to set more values in the matching dictionary (e.g. idVendor, idProduct,
// bInterfaceNumber and bConfigurationValue.
//

// Create a CFNumber for the idVendor and set the value in the dictionary
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usbVendor);
CFDictionarySetValue(matchingDict,
CFSTR(kUSBVendorID),
numberRef);
CFRelease(numberRef);

// Create a CFNumber for the idProduct and set the value in the dictionary
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usbProduct);
CFDictionarySetValue(matchingDict,
CFSTR(kUSBProductID),
numberRef);
CFRelease(numberRef);
numberRef = 0;

// Create a notification port and add its run loop event source to our run loop
// This is how async notifications get set up.

gNotifyPort = IONotificationPortCreate(masterPort);
runLoopSource = IONotificationPortGetRunLoopSource(gNotifyPort);

gRunLoop = CFRunLoopGetCurrent();
CFRunLoopAddSource(gRunLoop, runLoopSource, kCFRunLoopDefaultMode);








// Now set up a notification to be called when a device is first matched by I/O Kit.
kr = IOServiceAddMatchingNotification(gNotifyPort, // notifyPort
kIOFirstMatchNotification, // notificationType
matchingDict, // matching
DeviceAdded, // callback
NULL, // refCon
&gAddedIter // notification
);

Mac OS X (10.4.6)

Jun 16, 2006 8:44 PM in response to hybrid11

To answer Dale Ranta, I went through that guide (thats where most my code came from actually) but I am stuck at the last step where I am not able to find the /dev/ file path of the device. Could it be that because it is an unknown device, the mac OS is not creating a path for it? If so how could you fix this? anyone know by any chance?
thanks

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Writing a USB driver

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.