【问题标题】:The DriverKit problems to develop multiple serial USB deviceDriverKit开发多串口USB设备的问题
【发布时间】:2022-11-27 20:17:35
【问题描述】:

我正在为连接了多个串行 UART 的 USB 设备开发 DriverKit 驱动程序。每个 UART 将代表 Mac 上的一个 cu.USBX 端口。我的驱动程序继承了 IOUSBHostDevice 类,它与设备 ID 匹配得很好。现在,我将创建一个继承 IOUserSerial 的新类来实现串口。但是,编译器说基类上没有新的运算符。似乎基本 OSObject 类阻止了像我在 IOKit 驱动程序中那样新建子类。由于类似的IOUserSerial/IOUserUSBSerial的例子很难找到,想请问有没有人能帮我解决这个问题。任何反馈和线索表示赞赏。以下是一些 sn-ps 来显示我的情况。

我原来的 IOKit 端口驱动继承了 IORS232SerialStreamSync。

class KextSerial : public IORS232SerialStreamSync
{
    OSDeclareDefaultStructors( KextSerial )   ;    // Constructor & Destructor stuff
     :
}

我的 USB 驱动程序可以创建新的 KextSerials 并启动它们。

KextSerial * newSerial = new KextSerial;
   
if( !newSerial->init(0, 0) ){
    goto FailExit;
}

但是,在我的 DriverKit 端口驱动程序继承了 IOUserSerial。

class DextSerial : public IOUserSerial
{
      :
}

当我尝试按以下方式更新 DextSerial 时。

DextSerial * newSerial = new DextSerial;

编译器说“没有匹配函数来调用‘operator new’”

也许我不能在 DriverKit 中执行此操作,但我无法从 Apple 的开发网站上找到文档。

同时我尝试了 IOUserUSBSerial 和 OSObject,我得到了相同的错误消息。

【问题讨论】:

  • 看了this project.我觉得我对DriverKit有误解。我应该使用 IOService::Create 而不是 new 运算符。我的后续问题是如何在没有 NewUserClient 方法的情况下使用 IOService::Create ?我必须有一个应用程序来触发驱动程序来实例化子服务吗?

标签: macos device-driver iokit driverkit


【解决方案1】:

你的问题不是特别清楚,但我会尽力通过各种选项。

  • 你的一个实例主要的当您的驱动程序匹配其提供者设备时,系统会创建驱动程序类。
  • 如果您自己实现NewUserClient,或者如果您想要创建客户端对象以附加到主驱动程序对象实例下方,则在代码中显式创建实例主要针对用户客户端对象。

主驱动对象

在创建主驱动程序实例时,这就是 I/O Kit 匹配的用武之地,它都在 dext 的 Info.plist 文件中定义。这部分与 kexts 非常相似,除了一些更改/新的 IOKitPersonalities 键:

Key Value Type Description (dext) Description (dext)
IOClass string DriverKit kernel-side IOService subclass, e.g. IOUserService, IOUserSCSIParallelInterfaceController, … The driver's own IOService (or deeper) subclass
IOUserClass string Dext-side name of the driver's IOService (or deeper) subclass N/A
CFBundleIdentifier string Bundle identifier of the dext Bundle identifier of the kext
CFBundleIdentifierKernel string Bundle identifier of the kext hosting the DriverKit kernel-side class (IOClass), e.g. com.apple.kpi.iokit, com.apple.iokit.IOSCSIParallelFamily, … N/A
IOUserServerName string Bundle identifier of the dext N/A

除此之外,IOKitPersonalities 字典采用与 kexts 相同的形式。

因此,如果您的驱动程序的主类称为 KextSerial,您可以将其作为 IOUserClass 的值。

从属对象:

如果你真的需要明确地创建IOService对象实例,你使用IOService::Create()。不能只使用标准 C++ 语法的原因是对象需要在 I/O Kit 注册表的内核视图中进行镜像。因此,对于主驱动程序对象,您需要指定要实例化的内核和 DriverKit 端类。

Apple 希望您这样做的方式是在您的驱动程序的 IOKit 个性中提供一个字典属性,因此它成为您的主驱动程序对象实例上的一个属性。在 Create 调用中,您只需指定属性的名称,字典在 Info.plist 中指定,看起来像这样:

<key>MyDriverSubService</key>
<dict>
    <key>IOClass</key>
    <string>IOUserUserClient</string>
    <key>IOUserClass</key>
    <string>MyDriverSubService</string>
</dict>

键和值与主要人格具有相同的含义。

最后说明:

  • 你做不是想要继承来自IOUSBHostDevice,您想要匹配代表要驱动的设备的现有实例。 (使用它或IOUSBHostInterface作为IOProviderClass。)
  • 您可能会发现使用 IOService::Create() 从单个驱动程序实例中创建某些 DriverKit IOService 子类的多个实例无法正常工作。例如,请参阅 this question and answer 了解如何为具有多个端口的网络适配器创建驱动程序,因为从同一实例注册多个 IOUserNetworkEthernet 对象似乎不会像您预期的那样工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2012-07-30
    • 2014-05-07
    相关资源
    最近更新 更多