Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 23 Dec 2005 00:00:43 +0100
From:      "Marius Nuennerich" <marius.nuennerich@gmx.net>
To:        "FreeBSD gnats submit" <FreeBSD-gnats-submit@FreeBSD.org>
Subject:   docs/90830: Update for books/arch-handbook/pci
Message-ID:  <1135292443.0@sol.hackerzberg.local>
Resent-Message-ID: <200512222310.jBMNA3Wp022516@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         90830
>Category:       docs
>Synopsis:       Update for books/arch-handbook/pci
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-doc
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          doc-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Dec 22 23:10:02 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator:     Marius Nuennerich
>Release:        FreeBSD 5.4-RELEASE-p5 i386
>Organization:
>Environment:





>Description:


The attached patch is an update to the PCI chapter in the Architecture Handbook.
I tried to compile the old code but that isn't possible anymore
because of phk's dev_t -> struct cdev * change. Being new to PCI programming
I changed a few other things in the sample that confused me and that I learned
while reading other documentation about the topic.
Being also new to docbook my xml probably isn't the best.


>How-To-Repeat:





>Fix:


--- pci.diff begins here ---
--- chapter.sgml.bak	Sun Dec 18 23:13:36 2005
+++ chapter.sgml	Thu Dec 22 23:38:34 2005
@@ -18,138 +18,131 @@
     <para>Information here about how the PCI bus code iterates through
       the unattached devices and see if a newly loaded kld will attach
       to any of them.</para>
+    <sect2>
+      <title>Sample code:</title>
 
-<programlisting>/*
+      <programlisting>/*
  * Simple KLD to play with the PCI functions.
  *
  * Murray Stokely
  */
 
-#define MIN(a,b) (((a) < (b)) ? (a) : (b))
-
 #include &lt;sys/param.h&gt;  /* defines used in kernel.h */
 #include &lt;sys/module.h&gt;
 #include &lt;sys/systm.h&gt;
-#include &lt;sys/errno.h&gt;
+#include &lt;sys/errno.h&gt;  /* ENXIO */
 #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;
-#include &lt;sys/bus.h&gt;	/* structs, prototypes for pci bus stuff */
+#include &lt;sys/bus.h&gt;    /* structs, prototypes for pci bus stuff */
 
 #include &lt;machine/bus.h&gt;
 #include &lt;sys/rman.h&gt;
 #include &lt;machine/resource.h&gt;
 
-#include &lt;dev/pci/pcivar.h&gt; /* For get_pci macros! */
+#include &lt;dev/pci/pcivar.h&gt; /* For pci_get macros! */
 #include &lt;dev/pci/pcireg.h&gt;
 
 /* Function prototypes */
-d_open_t	mypci_open;
-d_close_t	mypci_close;
-d_read_t	mypci_read;
-d_write_t	mypci_write;
+static d_open_t		mypci_open;
+static d_close_t	mypci_close;
+static d_read_t		mypci_read;
+static d_write_t	mypci_write;
 
 /* Character device entry points */
-
 static struct cdevsw mypci_cdevsw = {
 	.d_open =	mypci_open,
 	.d_close =	mypci_close,
 	.d_read =	mypci_read,
 	.d_write =	mypci_write,
-	.d_name =	"mypci",
+	.d_name =	"mypci"
 };
 
 /* vars */
-static dev_t sdev;
+static struct cdev *mypci_dev;  /* for make_dev */
 
-/* We're more interested in probe/attach than with
-   open/close/read/write at this point */
+/*
+ * We're more interested in probe/attach than with
+ * open/close/read/write at this point
+ */
 
-int
-mypci_open(dev_t dev, int oflags, int devtype, d_thread_t *td)
+static int
+mypci_open(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
 {
-	int err = 0;
-
-	printf("Opened device \"mypci\" successfully.\n");
-	return (err);
+	printf("mypci: open!\n");
+	return (0);
 }
 
-int
-mypci_close(dev_t dev, int fflag, int devtype, d_thread_t *td)
+static int
+mypci_close(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
 {
-	int err = 0;
-
-	printf("Closing device \"mypci.\"\n");
-	return (err);
+	printf("mypci: close!\n");
+	return (0);
 }
 
-int
-mypci_read(dev_t dev, struct uio *uio, int ioflag)
+static int
+mypci_read(struct cdev *dev, struct uio *uio, int ioflag)
 {
-	int err = 0;
-
-	printf("mypci read!\n");
-	return (err);
+	printf("mypci: read!\n");
+	return (0);
 }
 
-int
-mypci_write(dev_t dev, struct uio *uio, int ioflag)
+static int
+mypci_write(struct cdev *dev, struct uio *uio, int ioflag)
 {
-	int err = 0;
-
-	printf("mypci write!\n");
-	return (err);
+	printf("mypci: write!\n");
+	return (0);
 }
 
 /* PCI Support Functions */
 
 /*
- * Return identification string if this is device is ours.
+ * Print identification string if this device is ours.
  */
 static int
 mypci_probe(device_t dev)
 {
-	device_printf(dev, "MyPCI Probe\nVendor ID : 0x%x\nDevice ID : 0x%x\n",
+	device_printf(dev, "probe: VendorID: 0x%04x DeviceID: 0x%04x\n",
 	    pci_get_vendor(dev), pci_get_device(dev));
 
 	if (pci_get_vendor(dev) == 0x11c1) {
-		printf("We've got the Winmodem, probe successful!\n");
-		return (0);
+		device_printf(dev, "We've got the Winmodem, probe successful!\n");
+		device_set_desc(dev, "Winmodem");
+		return (BUS_PROBE_DEFAULT);
 	}
 	return (ENXIO);
 }
 
-/* Attach function is only called if the probe is successful */
-
+/*
+ * Attach function is only called if the probe is successful.
+ */
 static int
 mypci_attach(device_t dev)
 {
-
-	printf("MyPCI Attach for : deviceID : 0x%x\n",pci_get_vendor(dev));
-	sdev = make_dev(<literal>&</literal>mypci_cdevsw, 0, UID_ROOT,
+	device_printf(dev, "attach: DeviceID: 0x%04x\n", pci_get_device(dev));
+	mypci_dev = make_dev(<literal>&</literal>mypci_cdevsw, 0, UID_ROOT,
 	    GID_WHEEL, 0600, "mypci");
-	printf("Mypci device loaded.\n");
-	return (ENXIO);
+	return (0);
 }
 
-/* Detach device. */
-
+/*
+ * Detach device.
+ */
 static int
 mypci_detach(device_t dev)
 {
-
-	printf("Mypci detach!\n");
+	device_printf(dev, "detach!\n");
 	return (0);
 }
 
-/* Called during system shutdown after sync. */
-
+/*
+ * Called during system shutdown after sync.
+ */
 static int
 mypci_shutdown(device_t dev)
 {
-
-	printf("Mypci shutdown!\n");
+	device_printf(dev, "shutdown!\n");
 	return (0);
 }
 
@@ -159,20 +152,17 @@
 static int
 mypci_suspend(device_t dev)
 {
-
-	printf("Mypci suspend!\n");
+	device_printf(dev, "suspend!\n");
 	return (0);
 }
 
 /*
  * Device resume routine.
  */
-
 static int
 mypci_resume(device_t dev)
 {
-
-	printf("Mypci resume!\n");
+	device_printf(dev, "resume!\n");
 	return (0);
 }
 
@@ -191,24 +181,41 @@
 static driver_t mypci_driver = {
 	"mypci",
 	mypci_methods,
-	0,
-	/*	sizeof(struct mypci_softc), */
+	0 /* sizeof(struct mypci_softc) */
 };
 
 static devclass_t mypci_devclass;
 
 DRIVER_MODULE(mypci, pci, mypci_driver, mypci_devclass, 0, 0);</programlisting>
 
-    <para>Additional Resources
-    <itemizedlist>
-      <listitem><simpara><ulink url="http://www.pcisig.org/">PCI
-	Special Interest Group</ulink></simpara></listitem>
+    </sect2>
+    <sect2>
+      <title>Makefile</title>
+
+      <programlisting>KMOD=mypci
+SRCS=mypci.c
+SRCS+=device_if.h bus_if.h pci_if.h
+
+.include &lt;bsd.kmod.mk&gt;</programlisting>
+
+      <para>Simply running <command>make</command> with the above
+            <filename>Makefile</filename>
+            will create a file <filename>mypci.ko</filename> that can
+            be loaded into your system by typing:
+            <screen>&prompt.root; <userinput>kldload ./mypci.ko</userinput></screen>
+      </para>
+    </sect2>
+    <sect2>
+      <title>Additional Resources</title>
+      <itemizedlist>
+        <listitem><simpara><ulink url="http://www.pcisig.org/">PCI
+          Special Interest Group</ulink></simpara></listitem>
 
-      <listitem><simpara>PCI System Architecture, Fourth Edition by
-	Tom Shanley, et al.</simpara></listitem>
+        <listitem><simpara>PCI System Architecture, Fourth Edition by
+          Tom Shanley, et al.</simpara></listitem>
 
-    </itemizedlist>
-    </para>
+      </itemizedlist>
+    </sect2>
   </sect1>
 
   <sect1 id="pci-bus">
--- pci.diff ends here ---



>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1135292443.0>