From owner-freebsd-hackers Sun Dec 2 17:28: 8 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from rover.village.org (rover.bsdimp.com [204.144.255.66]) by hub.freebsd.org (Postfix) with ESMTP id D405137B417 for ; Sun, 2 Dec 2001 17:28:02 -0800 (PST) Received: from harmony.village.org (harmony.village.org [10.0.0.6]) by rover.village.org (8.11.3/8.11.3) with ESMTP id fB31S1a36619; Sun, 2 Dec 2001 18:28:02 -0700 (MST) (envelope-from imp@harmony.village.org) Received: from harmony.village.org (localhost [127.0.0.1]) by harmony.village.org (8.11.6/8.11.6) with ESMTP id fB31S0M62172; Sun, 2 Dec 2001 18:28:01 -0700 (MST) (envelope-from imp@harmony.village.org) Message-Id: <200112030128.fB31S0M62172@harmony.village.org> To: "Dragon Fire" Subject: Re: device object, driver object, cdevsw Cc: freebsd-hackers@FreeBSD.ORG In-reply-to: Your message of "Sun, 02 Dec 2001 14:56:48 EST." <000601c17b6b$7a89c190$037d6041@gandalf> References: <000601c17b6b$7a89c190$037d6041@gandalf> Date: Sun, 02 Dec 2001 18:28:00 -0700 From: Warner Losh Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG In message <000601c17b6b$7a89c190$037d6041@gandalf> "Dragon Fire" writes: : I'm writing a PCI character device driver and need some clairification. : : As I see the FreeBSD driver structure there are really two components to a : device driver, there is the KLD component which contains the device methods : for probe, attach, detach, etc and handles the dynamic componet of the : driver. Then there is the traditional Unix cdevsw structure which handles : the read, write, ioctl etc. : : The few places the KLD is documented really doesn't cover the link between : the two elements KLD and cdevsw. I understand that if successsfully probed : the attach creates the dev_t using the cdevw table. But I'm having : difficulty seeing the relationship. : : Would it be fair to say the KLD components represent the dynamics kernel : facilities and the cdevsw implments what we consider the "traditional" Unix : device driver. Could somebody shed light on this subject. The matter is actually fairly simple. Your make_dev() calls assocaite the cdevsw to use with the device nodes you create (which also must be created using mknod in -stable or current w/o devfs). Typically, you won't have to worry too much about the link between the two. It just happens. I typically have something like: ... static d_open_t xxx_open; static d_close_t xxx_close; static d_ioctl_t xxx_ioctl; static d_read_t xxx_read; static d_write_t xxx_write; static d_mmap_t xxx_mmap; static devclass_t xxx_devclass; #if __FreeBSD_version < 500023 typedef struct proc d_thread_t; #endif #define CDEV_MAJOR 238 /* Must match dev entries in /dev */ static struct cdevsw xxx_cdevsw = { /* open */ xxx_open, /* close */ xxx_close, /* read */ xxx_read, /* write */ xxx_write, /* ioctl */ xxx_ioctl, /* poll */ nopoll, /* mmap */ xxx_mmap, /* strategy */ nostrategy, /* name */ "tscpci", /* maj */ CDEV_MAJOR, /* dump */ nodump, /* psize */ nopsize, /* flags */ 0, /* bmaj */ -1 }; static int xxx_probe(device_t dev); static int xxx_attach(device_t dev); static int xxx_detach(device_t dev); static device_method_t xxx_methods[] = { /* Device interface */ DEVMETHOD(device_probe, xxx_probe), DEVMETHOD(device_attach, xxx_attach), DEVMETHOD(device_detach, xxx_detach), { 0, 0 } }; static driver_t xxx_driver = { "tscpci", xxx_methods, sizeof(xxx_softc), }; DRIVER_MODULE(tscpci, pci, xxx_driver, xxx_devclass, 0, 0); In xxx_attach, I have: sc->d = make_dev(&xxx_cdevsw, unit, 0, 0, 0666, "xxx%d", unit); where sc is a softc pointer (which you have to define, by convention it is xxx_softc or something similar). That's all it takes to do the association. Warner To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message