From xride@beacon.x12.dk  Fri Jan 13 18:41:11 2006
Return-Path: <xride@beacon.x12.dk>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 0DCED16A41F
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 18 Aug 2003 18:41:11 +0000 (GMT)
	(envelope-from xride@beacon.x12.dk)
Received: from swip.net (mailfe08.swip.net [212.247.154.225])
	by mx1.FreeBSD.org (Postfix) with ESMTP id EE66243D45
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 18 Aug 2003 18:41:09 +0000 (GMT)
	(envelope-from xride@beacon.x12.dk)
Received: from x12.dk ([83.72.97.237] verified)
  by mailfe08.swip.net (CommuniGate Pro SMTP 5.0.2)
  with ESMTP id 81493697 for FreeBSD-gnats-submit@freebsd.org; Mon, 18 Aug 2003 19:41:07 +0100
Received: from localhost (unknown [127.0.0.1])
	by x12.dk (Postfix) with ESMTP id 841975083D
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 18 Aug 2003 19:41:07 +0100 (CET)
Received: from x12.dk ([127.0.0.1])
 by localhost (x12.dk [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
 id 17330-02 for <FreeBSD-gnats-submit@freebsd.org>;
 Mon, 18 Aug 2003 19:39:43 +0100 (CET)
Received: from beacon.x12.dk (beacon.lan [10.40.61.30])
	by x12.dk (Postfix) with ESMTP
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 18 Aug 2003 19:39:43 +0100 (CET)
Received: by beacon.x12.dk (Postfix, from userid 666)
	id 07BD54159; Mon, 18 Aug 2003 19:39:42 +0100 (CET)
Message-Id: <20060113183942.07BD54159@beacon.x12.dk>
Date: Mon, 18 Aug 2003 19:39:42 +0100 (CET)
From: Soeren Straarup <xride@x12.dk>
Reply-To: Soeren Straarup <xride@x12.dk>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: Howto make a kernel module for a pseudo-device under 5.X
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         55693
>Category:       docs
>Synopsis:       Howto make a kernel module for a pseudo-device under 5.X
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    kensmith
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Mon Aug 18 05:40:15 PDT 2003
>Closed-Date:    Thu Sep 25 06:03:14 PDT 2003
>Last-Modified:  Sun Mar 17 02:02:10 UTC 2013
>Originator:     Xride
>Release:        FreeBSD 5.1-CURRENT i386
>Organization:
>Environment:


Changed the pseudo-device kernel module code, from the arch-handbook, so it
will work under 5.X.


--- chapter.diff begins here ---
--- /usr/doc/en_US.ISO8859-1/books/arch-handbook/driverbasics/chapter.sgml	Thu Aug  7 01:48:02 2003
+++ chapter.sgml	Mon Aug 18 13:42:46 2003
@@ -335,6 +335,168 @@
 
 DEV_MODULE(echo,echo_loader,NULL);</programlisting>
 
+    <para>For the 5.X serie this is how the code for the pseudo-device
+      should look:</para>
+
+<programlisting>/*
+ * Simple `echo' pseudo-device KLD
+ *
+ * Murray Stokely
+ */
+
+/*
+ * Conveted to 5.X by S&oring;ren (Xride) Straarup
+ */
+
+
+#include &lt;sys/types.h&gt;
+#include &lt;sys/module.h&gt;
+#include &lt;sys/systm.h&gt; /* uprintf */ 
+#include &lt;sys/errno.h&gt;
+#include &lt;sys/param.h&gt;  /* defines used in kernel.h */
+#include &lt;sys/kernel.h&gt; /* types used in module initialization */
+#include &lt;sys/conf.h&gt;   /* cdevsw struct */
+#include &lt;sys/uio.h&gt;    /* uio struct */
+#include &lt;sys/malloc.h&gt;
+
+#define BUFFERSIZE 256
+#define CDEV_MAJOR      33
+
+/* Function prototypes */
+static d_open_t      echo_open;
+static d_close_t     echo_close;
+static d_read_t      echo_read;
+static d_write_t     echo_write;
+
+/* Character device entry points */
+static struct cdevsw echo_cdevsw = {
+  .d_open       =       echo_open,
+  .d_close      =       echo_close,
+  .d_maj        =       CDEV_MAJOR,
+  .d_name       =       "echo",
+  .d_read       =       echo_read,
+  .d_write      =       echo_write
+};
+
+typedef struct s_echo {
+  char msg[BUFFERSIZE];
+  int len;
+} t_echo;
+
+/* vars */
+static dev_t echo_dev;
+static int count;
+static t_echo *echomsg;
+static int len;
+
+
+MALLOC_DECLARE(M_ECHOBUF);
+MALLOC_DEFINE(M_ECHOBUF, "echobuffer", "buffer for echo module");
+
+/*
+ * This function acts is called by the kld[un]load(2) system calls to
+ * determine what actions to take when a module is loaded or unloaded.
+ */
+      
+static int
+echo_loader(struct module *m, int what, void *arg)
+{
+  int err = 0;
+  
+  switch (what) {
+  case MOD_LOAD:                /* kldload */
+    echo_dev = make_dev(&echo_cdevsw,
+            0,
+            UID_ROOT,
+            GID_WHEEL,
+            0600,
+            "echo");
+    /* kmalloc memory for use by this driver */
+    /*    malloc(256,M_ECHOBUF,M_WAITOK); */
+    MALLOC(echomsg, t_echo *, sizeof(t_echo), M_ECHOBUF, M_WAITOK);
+    printf("Echo device loaded.\n");
+    break;
+  case MOD_UNLOAD:
+    destroy_dev(echo_dev);
+    FREE(echomsg,M_ECHOBUF);
+    printf("Echo device unloaded.\n");
+    break;
+  default:
+    err = EINVAL;
+    break;
+  }
+  return(err);
+}
+
+static int
+echo_open(dev_t dev, int oflags, int devtype, struct thread *p)
+{
+  int err = 0;
+  
+  uprintf("Opened device \"echo\" successfully.\n");
+  return(err);
+}
+
+static int 
+echo_close(dev_t dev, int fflag, int devtype, struct thread *p)
+{
+  uprintf("Closing device \"echo.\"\n"); 
+  return(0);
+}
+
+/* 
+ * The read function just takes the buf that was saved via
+ * echo_write() and returns it to userland for accessing.
+ * uio(9) 
+ */
+
+static int
+echo_read(dev_t dev, struct uio *uio, int ioflag)
+{
+  int err = 0;
+  int amt;
+
+  /* How big is this read operation?  Either as big as the user wants,
+     or as big as the remaining data */
+  amt = MIN(uio-&gt;uio_resid, (echomsg->len - uio->uio_offset > 0) ? echomsg->l
+en - uio->uio_offset : 0);
+  if ((err = uiomove(echomsg-&gt;msg + uio->uio_offset,amt,uio)) != 0) {
+    uprintf("uiomove failed!\n");
+  }
+
+  return err;
+}
+
+/*
+ * echo_write takes in a character string and saves it
+ * to buf for later accessing.
+ */
+
+static int
+echo_write(dev_t dev, struct uio *uio, int ioflag)
+{
+  int err = 0;
+
+  /* Copy the string in from user memory to kernel memory */
+  err = copyin(uio-&gt;uio_iov->iov_base, echomsg->msg, MIN(uio->uio_iov->iov_le
+n,BUFFERSIZE));
+
+  /* Now we need to null terminate */
+  *(echomsg-&gt;msg + MIN(uio->uio_iov->iov_len,BUFFERSIZE)) = 0;
+  /* Record the length */
+  echomsg-&gt;len = MIN(uio->uio_iov->iov_len,BUFFERSIZE);
+
+  if (err != 0) {
+    uprintf("Write failed: bad address!\n");
+  }
+
+  count++;
+  return(err);
+}
+
+DEV_MODULE(echo,echo_loader,NULL);</programlisting>
+
+
     <para>To install this driver you will first need to make a node on
       your filesystem with a command such as:</para>
 
--- chapter.diff ends here ---


>Description:
 
>How-To-Repeat:
>Fix:
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: kensmith 
State-Changed-When: Thu Sep 25 06:00:11 PDT 2003 
State-Changed-Why:  

Patch committed with minor changes: 
- formatted code to be style(9) compliant 
- minor spelling/grammar changes 
- minor fix of markup tags (</programlisting>) 

Thanks! 


http://www.freebsd.org/cgi/query-pr.cgi?pr=55693 
>Unformatted:
 
 
 
 
