From owner-freebsd-hackers@FreeBSD.ORG Mon Jul 25 13:21:43 2005 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B6C6716A41F for ; Mon, 25 Jul 2005 13:21:43 +0000 (GMT) (envelope-from NKoch@demig.de) Received: from server.absolute-media.de (server.absolute-media.de [213.239.231.9]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0F81743D46 for ; Mon, 25 Jul 2005 13:21:42 +0000 (GMT) (envelope-from NKoch@demig.de) Received: from localhost (unknown [127.0.0.1]) by server.absolute-media.de (Postfix) with ESMTP id 1569A8C167; Mon, 25 Jul 2005 15:21:40 +0200 (CEST) Received: from server.absolute-media.de ([127.0.0.1]) by localhost (server [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 17977-09; Mon, 25 Jul 2005 15:21:36 +0200 (CEST) Received: from firewall.demig (p50839592.dip0.t-ipconnect.de [80.131.149.146]) by server.absolute-media.de (Postfix) with ESMTP id 02905796D; Mon, 25 Jul 2005 15:21:34 +0200 (CEST) Received: from ws-ew-3 (ws-ew-3.w2kdemig [192.168.1.72]) by firewall.demig (8.13.4/8.13.1) with SMTP id j6PDIjNB035525; Mon, 25 Jul 2005 15:18:45 +0200 (CEST) (envelope-from NKoch@demig.de) From: "Norbert Koch" To: , Date: Mon, 25 Jul 2005 15:18:44 +0200 Message-ID: <000101c5911b$62441260$4801a8c0@ws-ew-3.W2KDEMIG> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0 In-Reply-To: <42E4DC88.000006.21996@camay.yandex.ru> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2120.0 Importance: Normal X-Virus-Scanned: by amavisd-new X-Virus-Scanned: by amavisd-new at absolute-media.de Cc: Subject: RE: how to use the function copyout() X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jul 2005 13:21:43 -0000 > #define IOCTL_GET_B _IOWR("F", 127, 0x4) I think the third parameter to _IOWR should directly specify a type, e.g. _IOWR("F", 127, int) or _IOWR("F", 127, struct MyStruct). > > ---- driver ---- > > struct my_softc { > ... > short unsigned int B; > }; > > ... > > static int > my_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, > struct thread *td) > { > struct my_softc *my_sc; > int unit, error; > unit = minor(dev); > my_sc = (struct my_softc *)devclass_get_softc(my_devclass, unit); > if (my_sc == NULL) > return (ENXIO); > switch(cmd) > { > ... > case IOCTL_GET_B: > error = copyout(&my_sc->B, data, sizeof(my_sc->B)); > switch (error) > { > case 0: > printf(" IOCTL_GET_B: %d\n", my_sc->B); > break; > case EFAULT: > printf("EFAULT\n"); > break; > case EIO: > printf("EIO\n"); > break; > case ENOMEM: > printf("ENOMEM\n"); > break; > case ENOSPC: > printf("ENOSPC\n"); > break; > } > break; > default: > break; > } > return 0; > } > > ---user program ---------------------- > > ... > > short unsigned int Data[32768]; > > int > main(int argc, char *argv[]) > { ... > > if (ioctl(fd0, IOCTL_GET_B, Data) == -1) > err(1, "IOCTL_GET_B"); > > ... > } > > ------- > > Here I get EFAULT. > > What have I done wrong? How can I do it correctly? The caddr_t data in your ioctl is already mapped into kernel memory. Look into the source of other device drivers. You'll find a lot of *(int *) data = ... So your copyout() has to fail because it tries to address memory which is not a part of your application's memory. >From errno(2): EFAULT: Bad address... I have no idea if it is possible for ioctls to have mapped more than a few 100 bytes for data exchange. You should use read and uiomove() instead. Norbert