From owner-freebsd-drivers@FreeBSD.ORG Mon Dec 19 13:34:55 2005 Return-Path: X-Original-To: freebsd-drivers@freebsd.org Delivered-To: freebsd-drivers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1CF6016A41F for ; Mon, 19 Dec 2005 13:34:55 +0000 (GMT) (envelope-from hide_yama@jcom.home.ne.jp) Received: from smtp22.m2.home.ne.jp (smtp22.m2.home.ne.jp [220.152.32.145]) by mx1.FreeBSD.org (Postfix) with ESMTP id 44F5343D46 for ; Mon, 19 Dec 2005 13:34:53 +0000 (GMT) (envelope-from hide_yama@jcom.home.ne.jp) Received: from CJ3087924A ([210.20.46.222]) by smtp22.m2.home.ne.jp with SMTP id <20051219133452.XQFH21991.smtp22.m2.home.ne.jp@CJ3087924A> for ; Mon, 19 Dec 2005 22:34:52 +0900 From: "hide_yama" To: Date: Mon, 19 Dec 2005 22:34:44 +0900 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506 Subject: Close never called. (Open same device file from 2 Apps.) X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Dec 2005 13:34:55 -0000 Hi everybody Now, I'm writing device driver on 5.4R. But it doesn't act that I unexpected. So, if you know the reason and the avoidance, please tell to me. Work is as follows. 1) Open device file "/dev/mypci" by App1. This App1 just calls open routine and sleep for a while in it's program. 2) Open and close the same device file by App2 befor App1 is expired. App2 calls just open and close routines in it's program. I expect that App2 calls open and close routines, however App2 just calls open, doesn't call close. Why isn't the close routine called? And are there any avoidance for this problem? Thanks. --- Chaki [test driver] ----(/usr/srs/sys/dev/mypci/mypci.c start)---- #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* Function prototypes */ static d_open_t mypci_open; static d_close_t mypci_close; static d_read_t mypci_read; static d_write_t mypci_write; static d_ioctl_t mypci_ioctl; /* Character device entry points */ static struct cdevsw mypci_cdevsw = { .d_version = D_VERSION, .d_flags = D_NEEDGIANT, .d_open = mypci_open, .d_close = mypci_close, .d_read = mypci_read, .d_write = mypci_write, .d_ioctl = mypci_ioctl, .d_name = "mypci", }; static int mypci_open(struct cdev *dev, int flags, int fmt, struct thread *td) { int err = 0; printf("Opened device \"mypci\" successfully.\n"); return 0; } static int mypci_close(struct cdev *dev, int flags, int fmt, struct thread *td) { int err = 0; printf("Closing device \"mypci.\"\n"); return 0; } static int mypci_read(struct cdev *dev, struct uio *uio, int ioflag) { int err = 0; printf("mypci read!\n"); return 0; } static int mypci_write(struct cdev *dev, struct uio *uio, int ioflag) { int err = 0; printf("mypci write!\n"); return 0; } static int mypci_probe(device_t dev) { return (0); } static int mypci_attach(device_t dev) { make_dev(&mypci_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "mypci"); printf("mypci device loaded.\n"); return (0); } static int mypci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) { printf("mypci ioctl!\n"); return (0); } static device_method_t mypci_methods[] = { DEVMETHOD(device_probe, mypci_probe), DEVMETHOD(device_attach, mypci_attach), { 0, 0 } }; static driver_t mypci_driver = { "mypci", mypci_methods, 0, }; static devclass_t mypci_devclass; DRIVER_MODULE(mypci, pci, mypci_driver, mypci_devclass, 0, 0); ----(/usr/srs/sys/dev/mypci/mypci.c end)---- [test driver Makefile] ----(/usr/srs/sys/modules/mypci/Makefile start)---- .PATH: ${.CURDIR}/../../dev/mypci SRCS= bus_if.h device_if.h pci_if.h mypci.c KMOD= mypci .include ----(/usr/srs/sys/modules/mypci/Makefile end)---- [App1] ----(/tmp/App1.c start)---- #include #include #include #include #include #include int main(void) { int fd; if ((fd = open("/dev/mypci", O_RDWR)) < 0){ printf("open error\n"); return -1; } printf("open OK\n"); sleep(100000); return 0; } ----(/tmp/App1.c end)---- [App2] ----(/tmp/App2.c start)---- #include #include #include #include #include #include int main(void) { int fd; int ret; if ((fd = open("/dev/mypci", O_RDWR)) < 0){ printf("open error = %d\n", errno); return -1; } printf("open OK\n"); if ((ret = close(fd)) < 0){ printf("close errno = %d\n", errno); return -1; } printf("close OK\n"); return 0; } ----(/tmp/App2.c end)----