aboutsummaryrefslogtreecommitdiff
path: root/en_US.ISO8859-1/books/arch-handbook/driverbasics/chapter.sgml
blob: 2dda46284966424a37b2fd7db453f69634892b1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<?xml version="1.0" encoding="ISO8859-1" standalone="no"?>
<!--
     The FreeBSD Documentation Project

     $FreeBSD$
-->

<chapter id="driverbasics">
  <chapterinfo>
    <authorgroup>
      <author>
        <firstname>Murray</firstname>
    	<surname>Stokely</surname>
        <contrib>Written by </contrib>
      </author>
    </authorgroup>
    <authorgroup>
      <author>
        <firstname>J&ouml;rg</firstname>
    	<surname>Wunsch</surname>
        <contrib>Based on intro(4) manual page by </contrib>
      </author>
    </authorgroup>
  </chapterinfo>
  <title>Writing FreeBSD Device Drivers</title>

  <sect1 id="driverbasics-intro">
    <title>Introduction</title>

    <indexterm><primary>device driver</primary></indexterm>
    <indexterm><primary>pseudo-device</primary></indexterm>

    <para>This chapter provides a brief introduction to writing device
      drivers for FreeBSD.  A device in this context is a term used
      mostly for hardware-related stuff that belongs to the system,
      like disks, printers, or a graphics display with its keyboard.
      A device driver is the software component of the operating
      system that controls a specific device.  There are also
      so-called pseudo-devices where a device driver emulates the
      behavior of a device in software without any particular
      underlying hardware.  Device drivers can be compiled into the
      system statically or loaded on demand through the dynamic kernel
      linker facility `kld'.</para>

    <indexterm><primary>device nodes</primary></indexterm>
    <indexterm><primary>MAKEDEV</primary></indexterm>

    <para>Most devices in a &unix;-like operating system are accessed
      through device-nodes, sometimes also called special files.
      These files are usually located under the directory
      <filename>/dev</filename> in the filesystem hierarchy.
      In releases of FreeBSD older than 5.0-RELEASE, where
      &man.devfs.5; support is not integrated into FreeBSD,
      each device node must be
      created statically and independent of the existence of the
      associated device driver.  Most device nodes on the system are
      created by running <command>MAKEDEV</command>.</para>

    <para>Device drivers can roughly be broken down into two 
      categories; character and network device drivers.</para>

  </sect1>

  <sect1 id="driverbasics-kld">
    <title>Dynamic Kernel Linker Facility - KLD</title> 

    <indexterm><primary>kernel linking</primary><secondary>dynamic</secondary></indexterm>
    <indexterm><primary>kernel loadable modules (KLD)</primary></indexterm>

    <para>The kld interface allows system administrators to
      dynamically add and remove functionality from a running system.
      This allows device driver writers to load their new changes into
      a running kernel without constantly rebooting to test
      changes.</para>

    <para>The kld interface is used through the following
      privileged commands:

    <indexterm><primary>kernel
    modules</primary><secondary>loading</secondary></indexterm>
    <indexterm><primary>kernel modules</primary><secondary>unloading</secondary></indexterm>
    <indexterm><primary>kernel modules</primary><secondary>listing</secondary></indexterm>
    <itemizedlist>
      <listitem><simpara><command>kldload</command> - loads a new kernel
	module</simpara></listitem>
      <listitem><simpara><command>kldunload</command> - unloads a kernel
	module</simpara></listitem>
      <listitem><simpara><command>kldstat</command> - lists the currently loaded
	modules</simpara></listitem>
    </itemizedlist>
    </para>

    <para>Skeleton Layout of a kernel module</para>

<programlisting>/*
 * KLD Skeleton
 * Inspired by Andrew Reiter's Daemonnews article
 */

#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 */

/* 
 * Load handler that deals with the loading and unloading of a KLD.
 */

static int
skel_loader(struct module *m, int what, void *arg)
{
  int err = 0;
  
  switch (what) {
  case MOD_LOAD:                /* kldload */
    uprintf("Skeleton KLD loaded.\n");
    break;
  case MOD_UNLOAD:
    uprintf("Skeleton KLD unloaded.\n");
    break;
  default:
    err = EOPNOTSUPP;
    break;
  }
  return(err);
}

/* Declare this module to the rest of the kernel */

static moduledata_t skel_mod = {
  "skel",
  skel_loader,
  NULL
};  

DECLARE_MODULE(skeleton, skel_mod, SI_SUB_KLD, SI_ORDER_ANY);</programlisting>


    <sect2>
      <title>Makefile</title>

      <para>FreeBSD provides a makefile include that you can use to
	quickly compile your kernel addition.</para>

      <programlisting>SRCS=skeleton.c
KMOD=skeleton

.include &lt;bsd.kmod.mk&gt;</programlisting>

      <para>Simply running <command>make</command> with this makefile
        will create a file <filename>skeleton.ko</filename> that can
        be loaded into your system by typing:
<screen>&prompt.root; <userinput>kldload -v ./skeleton.ko</userinput></screen>
      </para>
    </sect2>
  </sect1>

  <sect1 id="driverbasics-access">
    <title>Accessing a device driver</title>

    <para>&unix; provides a common set of system calls for user
      applications to use.  The upper layers of the kernel dispatch
      these calls to the corresponding device driver when a user
      accesses a device node.  The <command>/dev/MAKEDEV</command>
      script makes most of the device nodes for your system but if you
      are doing your own driver development it may be necessary to
      create your own device nodes with <command>mknod</command>.
    </para>

    <sect2>
      <title>Creating static device nodes</title>

      <indexterm><primary>device nodes</primary><secondary>static</secondary></indexterm>
      <indexterm><primary>mknod</primary></indexterm>

      <para>The <command>mknod</command> command requires four
	arguments to create a device node.  You must specify the name
	of the device node, the type of device, the major number of
	the device, and the minor number of the device.</para>
    </sect2>

    <sect2>
      <title>Dynamic device nodes</title>

      <indexterm><primary>device nodes</primary><secondary>dynamic</secondary></indexterm>
      <indexterm><primary>devfs</primary></indexterm>

      <para>The device filesystem, or devfs, provides access to the
	kernel's device namespace in the global filesystem namespace.
	This eliminates the problems of potentially having a device
	driver without a static device node, or a device node without
	an installed device driver.  Devfs is still a work in
	progress, but it is already working quite nicely.</para>
    </sect2>

  </sect1>

  <sect1 id="driverbasics-char">
    <title>Character Devices</title>

    <indexterm><primary>character devices</primary></indexterm>
    <para>A character device driver is one that transfers data
      directly to and from a user process.  This is the most common
      type of device driver and there are plenty of simple examples in
      the source tree.</para>

    <para>This simple example pseudo-device remembers whatever values
      you write to it and can then supply them back to you when you
      read from it.  Two versions are shown, one for &os;&nbsp;4.X and
      one for &os;&nbsp;5.X.</para>

    <example>
      <title>Example of a Sample Echo Pseudo-Device Driver for
        &os;&nbsp;4.X</title>

      <programlisting>/*
 * Simple `echo' pseudo-device KLD
 *
 * Murray Stokely
 */

#define MIN(a,b) (((a) &lt; (b)) ? (a) : (b))

#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

/* Function prototypes */
d_open_t	echo_open;
d_close_t	echo_close;
d_read_t	echo_read;
d_write_t	echo_write;

/* Character device entry points */
static struct cdevsw echo_cdevsw = {
	echo_open,
	echo_close,
	echo_read,
	echo_write,
	noioctl,
	nopoll,
	nommap,
	nostrategy,
	"echo",
	33,              /* reserved for lkms - /usr/src/sys/conf/majors */
	nodump,
	nopsize,
	D_TTY,
	-1
};

typedef struct s_echo {
	char msg[BUFFERSIZE];
	int len;
} t_echo;

/* vars */
static dev_t sdev;
static int count;
static t_echo *echomsg;

MALLOC_DECLARE(M_ECHOBUF);
MALLOC_DEFINE(M_ECHOBUF, "echobuffer", "buffer for echo module");

/*
 * This function 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 */
		sdev = make_dev(<literal>&amp;</literal>echo_cdevsw,
		    0,
		    UID_ROOT,
		    GID_WHEEL,
		    0600,
		    "echo");
		/* kmalloc memory for use by this driver */
		MALLOC(echomsg, t_echo *, sizeof(t_echo), M_ECHOBUF, M_WAITOK);
		printf("Echo device loaded.\n");
		break;
	case MOD_UNLOAD:
		destroy_dev(sdev);
		FREE(echomsg,M_ECHOBUF);
		printf("Echo device unloaded.\n");
		break;
	default:
		err = EOPNOTSUPP;
		break;
	}
	return(err);
}

int
echo_open(dev_t dev, int oflags, int devtype, struct proc *p)
{
	int err = 0;

	uprintf("Opened device \"echo\" successfully.\n");
	return(err);
}

int
echo_close(dev_t dev, int fflag, int devtype, struct proc *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)
 */

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-&gt;len - uio-&gt;uio_offset &gt; 0) ?
	    echomsg-&gt;len - uio-&gt;uio_offset : 0);
	if ((err = uiomove(echomsg-&gt;msg + uio-&gt;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.
 */

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-&gt;iov_base, echomsg-&gt;msg,
	    MIN(uio-&gt;uio_iov-&gt;iov_len, BUFFERSIZE - 1));

	/* Now we need to null terminate, then record the length */
	*(echomsg-&gt;msg + MIN(uio-&gt;uio_iov-&gt;iov_len, BUFFERSIZE - 1)) = 0;
	echomsg-&gt;len = MIN(uio-&gt;uio_iov-&gt;iov_len, BUFFERSIZE);

	if (err != 0) {
		uprintf("Write failed: bad address!\n");
	}
	count++;
	return(err);
}

DEV_MODULE(echo,echo_loader,NULL);</programlisting>
    </example>

    <example>
      <title>Example of a Sample Echo Pseudo-Device Driver for
        &os;&nbsp;5.X</title>

      <programlisting>/*
 * Simple `echo' pseudo-device KLD
 *
 * Murray Stokely
 *
 * Converted to 5.X by S&oslash;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


/* 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_version = D_VERSION,
	.d_open = echo_open,
	.d_close = echo_close,
	.d_read = echo_read,
	.d_write = echo_write,
	.d_name = "echo",
};

typedef struct s_echo {
	char msg[BUFFERSIZE];
	int len;
} t_echo;

/* vars */
static struct cdev *echo_dev;
static int count;
static t_echo *echomsg;

MALLOC_DECLARE(M_ECHOBUF);
MALLOC_DEFINE(M_ECHOBUF, "echobuffer", "buffer for echo module");

/*
 * This function 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(<literal>&amp;</literal>echo_cdevsw,
		    0,
		    UID_ROOT,
		    GID_WHEEL,
		    0600,
		    "echo");
		/* kmalloc memory for use by this driver */
		echomsg = malloc(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 = EOPNOTSUPP;
		break;
	}
	return(err);
}

static int
echo_open(struct cdev *dev, int oflags, int devtype, struct thread *p)
{
	int err = 0;

	uprintf("Opened device \"echo\" successfully.\n");
	return(err);
}

static int
echo_close(struct cdev *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(struct cdev *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-&gt;len - uio-&gt;uio_offset &gt; 0) ?
	     echomsg-&gt;len - uio-&gt;uio_offset : 0);
	if ((err = uiomove(echomsg-&gt;msg + uio-&gt;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(struct cdev *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-&gt;iov_base, echomsg-&gt;msg,
	    MIN(uio-&gt;uio_iov-&gt;iov_len, BUFFERSIZE - 1));

	/* Now we need to null terminate, then record the length */
	*(echomsg-&gt;msg + MIN(uio-&gt;uio_iov-&gt;iov_len, BUFFERSIZE - 1)) = 0;
	echomsg-&gt;len = MIN(uio-&gt;uio_iov-&gt;iov_len, BUFFERSIZE);

	if (err != 0) {
		uprintf("Write failed: bad address!\n");
	}
	count++;
	return(err);
}

DEV_MODULE(echo,echo_loader,NULL);</programlisting>
    </example>

    <para>To install this driver on &os;&nbsp;4.X you will first need to
      make a node on your filesystem with a command such as:</para>

    <screen>&prompt.root; <userinput>mknod /dev/echo c 33 0</userinput></screen>

    <para>With this driver loaded you should now be able to type
      something like:</para>

    <screen>&prompt.root; <userinput>echo -n "Test Data" &gt; /dev/echo</userinput>
&prompt.root; <userinput>cat /dev/echo</userinput>
Test Data</screen>

    <para>Real hardware devices are described in the next chapter.</para>

    <para>Additional Resources
    <itemizedlist>
      <listitem><simpara><ulink
	url="http://ezine.daemonnews.org/200010/blueprints.html">Dynamic
	Kernel Linker (KLD) Facility Programming Tutorial</ulink> -
	<ulink url="http://www.daemonnews.org/">Daemonnews</ulink> October 2000</simpara></listitem>
      <listitem><simpara><ulink
	url="http://ezine.daemonnews.org/200007/newbus-intro.html">How
	to Write Kernel Drivers with NEWBUS</ulink> - <ulink
	url="http://www.daemonnews.org/">Daemonnews</ulink> July
	2000</simpara></listitem>
    </itemizedlist>
    </para>
  </sect1>

  <sect1 id="driverbasics-block">
    <title>Block Devices (Are Gone)</title>

    <indexterm><primary>block devices</primary></indexterm>
    <para>Other &unix; systems may support a second type of disk
      device known as block devices.  Block devices are disk devices
      for which the kernel provides caching.  This caching makes
      block-devices almost unusable, or at least dangerously
      unreliable.  The caching will reorder the sequence of write
      operations, depriving the application of the ability to know
      the exact disk contents at any one instant in time.  This
      makes predictable and reliable crash recovery of on-disk data
      structures (filesystems, databases etc.) impossible.
      Since writes may be delayed, there is no way the kernel can
      report to the application which particular write operation
      encountered a write error, this further compounds the
      consistency problem.  For this reason, no serious applications
      rely on block devices, and in fact, almost all applications
      which access disks directly take great pains to specify that
      character (or <quote>raw</quote>) devices should always be
      used.  Because the implementation of the aliasing of each disk
      (partition) to two devices with different semantics significantly
      complicated the relevant kernel code &os; dropped support for
      cached disk devices as part of the modernization of the disk I/O
      infrastructure.
    </para>
  </sect1>

  <sect1 id="driverbasics-net">
    <title>Network Drivers</title>

    <indexterm><primary>network devices</primary></indexterm>
    <para>Drivers for network devices do not use device nodes in order
      to be accessed.  Their selection is based on other decisions
      made inside the kernel and instead of calling open(), use of a
      network device is generally introduced by using the system call
      socket(2).</para> 

    <para>For more information see ifnet(9), the source of the
      loopback device, and Bill Paul's network drivers.</para>

  </sect1>

</chapter>

<!-- 
     Local Variables:
     mode: sgml
     sgml-declaration: "../chapter.decl"
     sgml-indent-data: t
     sgml-omittag: nil
     sgml-always-quote-attributes: t
     sgml-parent-document: ("../book.sgml" "part" "chapter")
     End:
-->