Changed e-mail adress to berndporr@f2s.com
[comedilib.git] / doc / driverwriting.sgml
1 <section id="driverwriting">
2 <title>
3 Writing a &comedi; driver
4 </title>
5
6 <para>
7 This Section explains the most important implementations aspects of
8 the &comedi; device drivers. It tries to give the interested device
9 driver writer an overview of the different steps required to write a
10 new device driver.
11 </para>
12 <para>
13 This Section does <emphasis>not</emphasis> explain all implementation
14 details of the &comedi; software itself: &comedi; has once and for
15 all solved lots of boring but indispensable infrastructural things,
16 such as: timers, management of which drivers
17 are active, memory management for drivers and buffers, wrapping
18 of RTOS-specific interfaces, interrupt handler management, general
19 error handling, the <filename role=directory>/proc</filename>
20 interface, etc. So,
21 the device driver writers can concentrate on the interesting stuff:
22 implementing their specific interface card's DAQ functionalities.
23 </para>
24 <para>
25 In order to make a decent &comedi; device driver, you must
26 know the answers to the following questions:
27 <itemizedlist>
28
29 <listitem>
30 <para>
31 How does the 
32 <link linkend="userkernelhow">communication</link> between user space
33 and kernel space work?
34 </para>
35 </listitem>
36
37 <listitem>
38 <para>
39 What functionality is provided by the
40 <link linkend="comedikernelgeneric">generic</link> kernel-space
41 &comedi; functions, and what must be provided for each 
42 <link linkend="boardspecific">specific new driver</link>?
43 </para>
44 </listitem>
45
46 <listitem>
47 <para>
48 How to use <link linkend="drivercallbacks">DMA and interrupts</link>?
49 </para>
50 </listitem>
51
52 <listitem>
53 <para>
54 What are the addresses and meanings of all the card's registers?
55 </para>
56 <para>
57 This information is to be found in the so-called <quote>register level
58 manual</quote> of the card. Without it, coding a device driver is close
59 to hopeless. It is also something that &comedi; (and hence also this
60 handbook) cannot give any support or information for: board
61 manufacturers all use their own design and nomenclature.
62 </para>
63 </listitem>
64
65 </itemizedlist>
66 </para>
67
68 <section id="userkernelhow">
69 <title>
70 Communication user space-kernel space
71 </title>
72
73 <para>
74 In user space, you interact with the functions implemented in the
75 <filename role=directory>/usr/src/comedilib</filename> directory. Most
76 of the device driver core of the Comedilib library is found in
77 <filename role=directory>lib</filename> subdirectory.
78 </para>
79 <para>
80 All user-space &comedi; 
81 <link linkend="instructions">instructions</link> and
82 <link linkend="commandsstreaming">commands</link> 
83 are transmitted to kernel space through a traditional
84 <function>ioctl</function> system call.  
85 (See <filename>/usr/src/comedilib/lib/ioctl.c</filename>.)
86 The user space information command is <emphasis>encoded</emphasis> as
87 a number in the <function>ioctl</function> call, and decoded in the
88 kernel space library.  There, they are executed by their kernel-space
89 counterparts.  This is done in the
90 <filename>/usr/src/comedi/comedi/comedi_fops.c</filename> file: the
91 <function>comedi_ioctl()</function> function processes the results of
92 the <function>ioctl</function> system call, interprets its contents,
93 and then calls the corresponding kernel space
94 <function>do_&hellip;_ioctl</function> function(s).
95 For example, a &comedi; 
96 <link linkend="instructions">instruction</link> is further processed
97 by the <function>do_insn_ioctl()</function>function. (Which, in turn,
98 uses <function>parse_insn()</function> for further detailed processing.)
99 </para>
100 <para>
101 The data corresponding to instructions and commands is transmitted
102 with the <function>copy_from_user()</function> system call;
103 acquisition data captured by the interface card passes the kernel-user
104 space boundary with the help of a <function>copy_to_user()</function>
105 system call.
106 </para>
107
108 </section>
109
110 <section id="comedikernelgeneric">
111 <title>
112 Generic functionality
113 </title>
114
115 <para>
116 The major include files of the kernel-space part of &comedi; are:
117 <itemizedlist>
118
119 <listitem>
120 <para>
121 <filename>include/linux/comedidev.h</filename>: the
122 header file for kernel-only structures (device, subdevice, async
123 (i.e., buffer/event/interrupt/callback functionality for asynchronous
124 DAQ in a &comedi; command), driver, lrange), variables, inline functions
125 and constants.
126 </para>
127 </listitem>
128
129 <listitem>
130 <para>
131 <filename>include/linux/comedi_rt.h</filename>:
132 all the real-time stuff, such as management of ISR in RTAI and 
133 RTLinux/Free, and spinlocks for atomic sections.
134 </para>
135 </listitem>
136                                                                                 
137 <listitem>
138 <para>
139 <filename>include/linux/comedilib.h</filename>: the header file for
140 the kernel library of &comedi;.
141 </para>
142 </listitem>
143
144 </itemizedlist>
145 </para>
146 <para>
147 From all the relevant &comedi; device driver code that is found in the 
148 <filename role=directory>/usr/src/comedi/comedi</filename> directory
149 (<emphasis>if</emphasis> the &comedi; source has been installed in its
150 normal <filename role=directory>/usr/src/comedi</filename> location),
151 the <emphasis role="strong">generic</emphasis> functionality is
152 contained in two parts:
153  <itemizedlist>
154
155  <listitem>
156  <para>
157 A couple of <filename>C</filename> files contain the <emphasis
158 role="strong">infrastructural support</emphasis>.
159 From these <filename>C</filename> files, it's especially the
160 <filename>comedi_fops.c</filename> file that implements what makes
161 &comedi; into what people want to use it for: a library that has
162 solved 90% of the DAQ device driver efforts, once and for all. 
163  </para>
164  </listitem>
165
166  <listitem>
167  <para>
168 For <emphasis role="strong">real-time</emphasis> applications,
169 the subdirectory <filename role=directory>kcomedilib</filename> 
170 implements an interface in the kernel that is similar to the &comedi;
171 interface accessible through the 
172 <link linkend="functionreference">user-space Comedi library</link>.
173 </para>
174 <para>
175 There are some differences in what is possible and/or needed
176 in kernel space and in user space, so the functionalities offered in
177 <filename role=directory>kcomedilib</filename> are not an exact copy
178 of the user-space library. For example, locking, interrupt handling,
179 real-time execution, callback handling, etc., are only available in
180 kernel space.
181  </para>
182  <para>
183 Most drivers don't make use (yet) of these real-time functionalities.
184  </para>
185  </listitem>
186
187  </itemizedlist>
188 </para>
189
190
191 <section id="driverdatastructures">
192 <title>
193 Data structures
194 </title>
195
196 <para>
197 This Section explains the generic data structures that a device driver
198 interacts with:
199 <programlisting>
200 typedef struct comedi_lrange_struct    <link linkend="comedilrange">comedi_lrange</link>;
201 typedef struct comedi_subdevice_struct <link linkend="comedisubdevice">comedi_subdevice</link>;
202 typedef struct comedi_device_struct    <link linkend="comedidevice">comedi_device</link>:
203 typedef struct comedi_async_struct     <link linkend="comediasync">comedi_async</link> 
204 typedef struct comedi_driver_struct    <link linkend="comedidriver">comedi_driver</link>;
205 </programlisting>
206 They can be found in
207 <filename>/usr/src/comedi/include/linux/comedidev.h</filename>.
208 Most of the fields are filled in by the &comedi; infrastructure, but
209 there are still quite a handful that your driver must provide or use.
210 As for the user-level &comedi;, each of the hierarchical layers has
211 its own data structures: channel (<function>comedi_lrange</function>),
212 subdevice, and device.
213 </para>
214 <para>
215 Note that these kernel-space data structures have similar names as
216 their
217 <link linkend="datatypesstructures">user-space equivalents</link>, but
218 they have a different (kernel-side) view on the DAQ problem and a
219 different meaning: they encode the interaction with the
220 <emphasis>hardware</emphasis>, not with the <emphasis>user</emphasis>.
221 </para>
222 <para>
223 However, the <link linkend="ref-type-comedi-insn">comedi_insn</link> 
224 and <link linkend="ref-type-comedi-cmd">comedi_cmd</link> 
225 data structures are shared between user space and kernel space: this
226 should come as no surprise, since these data structures contain all
227 information that the user-space program must transfer to the
228 kernel-space driver for each acquisition.
229 </para>
230 <para>
231 In addition to these data entities that are also known at the user
232 level (device, sub-device, channel), the device driver level provides
233 two more data structures which the application programmer doesn't get
234 in touch with: the data structure
235 <link linkend="comedidriver">comedi_driver</link>
236 that stores the device driver information that is relevant at the
237 operating system level, and the data structure
238 <link linkend="comediasync">comedi_async</link> that stores the
239 information about all <emphasis>asynchronous</emphasis> activities
240 (interrupts, callbacks and events). 
241 </para>
242
243 <section id="comedilrange">
244 <title>
245 <function>comedi_lrange</function>
246 </title>
247 <para>
248 The channel information is simple, since it contains only the signal
249 range information:
250 <programlisting>
251 struct comedi_lrange_struct{
252   int           length;
253   <link linkend="ref-type-comedi-krange">comedi_krange</link> range[GCC_ZERO_LENGTH_ARRAY];
254 };
255 </programlisting>
256 </para>
257
258 </section>
259
260
261 <section id="comedisubdevice">
262 <title>
263 <function>comedi_subdevice</function>
264 </title>
265 <para>
266 The subdevice is the smallest &comedi; entity that can be used for
267 <quote>stand-alone</quote> DAQ, so it is no surprise that it is
268 quite big:
269 <programlisting>
270 struct comedi_subdevice_struct{
271   int  type;
272   int  n_chan;
273   int  subdev_flags;
274   int  len_chanlist;            /* maximum length of channel/gain list */
275
276   void *private;
277
278   <link linkend="comediasync">comedi_async</link> *async;
279
280   void         *lock;
281   void         *busy;
282   unsigned int runflags;
283
284   int          io_bits;
285
286   <link linkend="ref-type-lsampl-t">lsampl_t</link> maxdata;       /* if maxdata==0, use list */
287   <link linkend="ref-type-lsampl-t">lsampl_t</link> *maxdata_list; /* list is channel specific */
288   
289   unsigned int flags;
290   unsigned int *flaglist;
291   
292   <link linkend="comedilrange">comedi_lrange</link> *range_table;
293   <link linkend="comedilrange">comedi_lrange</link> **range_table_list;
294   
295   unsigned int *chanlist;               /* driver-owned chanlist (not used) */
296   
297   int (*insn_read)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *,<link linkend="ref-type-comedi-insn">comedi_insn</link> *,<link linkend="ref-type-lsampl-t">lsampl_t</link> *);
298   int (*insn_write)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *,<link linkend="ref-type-comedi-insn">comedi_insn</link> *,<link linkend="ref-type-lsampl-t">lsampl_t</link> *);
299   int (*insn_bits)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *,<link linkend="ref-type-comedi-insn">comedi_insn</link> *,<link linkend="ref-type-lsampl-t">lsampl_t</link> *);
300   int (*insn_config)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *,<link linkend="ref-type-comedi-insn">comedi_insn</link> *,<link linkend="ref-type-lsampl-t">lsampl_t</link> *);
301   
302   int (*do_cmd)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *);
303   int (*do_cmdtest)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *,<link linkend="ref-type-comedi-cmd">comedi_cmd</link> *);
304   int (*poll)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *);
305   int (*cancel)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *);
306   
307   int (*buf_change)(<link linkend="comedidevice">comedi_device</link> *,<link linkend="comedisubdevice">comedi_subdevice</link> *s,unsigned long new_size);
308   void (*munge)(<link linkend="comedidevice">comedi_device</link> *, <link linkend="comedisubdevice">comedi_subdevice</link> *s, void *data, unsigned int num_bytes, unsigned int start_chan_index );
309   
310   unsigned int state;
311 };
312 </programlisting>
313 The function pointers <function>(*insn_read)</function> &hellip;
314 <function>(*cancel)</function> .
315 offer (pointers to) the standardized 
316 <link linkend="functionreference">user-visible API</link>
317 that every subdevice should offer; every device driver has to fill
318 in these functions with their board-specific implementations.
319 (Functionality for which &comedi; provides generic functions will, by
320 definition, not show up in the device driver data structures.)
321 </para>
322 <para>
323 The <function>buf_change()</function> and <function>munge()</function>
324 functions offer functionality that is not visible to the user and for
325 which the device driver writer must provide a board-specific
326 implementation: 
327 <function>buf_change()</function> is called when a change in the
328 data buffer requires handling; <function>munge()</function> transforms
329 different bit-representations of DAQ values, for example from
330 <emphasis>unsigned</emphasis> to <emphasis>2's complement</emphasis>.
331 </para>
332
333 </section>
334
335 <section id="comedidevice">
336 <title>
337 <function>comedi_device</function>
338 </title>
339
340 <para>
341 The last data structure stores the information at the
342 <emphasis>device</emphasis> level:
343 <programlisting>
344 struct comedi_device_struct{
345   int           use_count;
346   <link linkend="comedidriver">comedi_driver</link> *driver;
347   void          *private;
348   kdev_t        minor;
349   char          *board_name;
350   const void    *board_ptr;
351   int           attached;
352   int           rt;
353   spinlock_t    spinlock;
354   int           in_request_module;
355   
356   int               n_subdevices;
357   <link linkend="comedisubdevice">comedi_subdevice</link> *subdevices;
358   int              options[COMEDI_NDEVCONFOPTS];
359   
360   /* dumb */
361   int iobase;
362   int irq;
363   
364   <link linkend="comedisubdevice">comedi_subdevice</link> *read_subdev;
365   wait_queue_head_t read_wait;
366   
367   <link linkend="comedisubdevice">comedi_subdevice</link> *write_subdev;
368   wait_queue_head_t write_wait;
369   
370   struct fasync_struct *async_queue;
371   
372   void (*open)(<link linkend="comedidevice">comedi_device</link> *dev);
373   void (*close)(<link linkend="comedidevice">comedi_device</link> *dev);
374 };
375 </programlisting>
376 </para>
377
378 </section>
379
380 <section id="comediasync">
381 <title>
382 <function>comedi_async</function>
383 </title>
384
385 <para>
386 The following data structure contains all relevant information:
387 addresses and sizes of buffers, pointers to the actual data, and the
388 information needed for 
389 <link linkend="drivercallbacks">event handling</link>:
390 <programlisting>
391 struct comedi_async_struct{
392   void          *prealloc_buf;          /* pre-allocated buffer */
393   unsigned int  prealloc_bufsz;         /* buffer size, in bytes */
394   unsigned long *buf_page_list;         /* physical address of each page */
395   unsigned int  max_bufsize;            /* maximum buffer size, bytes */
396   unsigned int  mmap_count;     /* current number of mmaps of prealloc_buf */
397   
398   volatile unsigned int buf_write_count;        /* byte count for writer (write completed) */
399   volatile unsigned int buf_write_alloc_count;  /* byte count for writer (allocated for writing) */
400   volatile unsigned int buf_read_count; /* byte count for reader (read completed)*/
401   
402   unsigned int buf_write_ptr;   /* buffer marker for writer */
403   unsigned int buf_read_ptr;    /* buffer marker for reader */
404   
405   unsigned int cur_chan;                /* useless channel marker for interrupt */
406   /* number of bytes that have been received for current scan */
407   unsigned int scan_progress;
408   /* keeps track of where we are in chanlist as for munging */
409   unsigned int munge_chan;
410   
411   unsigned int  events;         /* events that have occurred */
412   
413   <link linkend="ref-type-comedi-cmd">comedi_cmd</link> cmd;
414   
415   // callback stuff
416   unsigned int cb_mask;
417   int (*cb_func)(unsigned int flags,void *);
418   void *cb_arg;
419   
420   int (*inttrig)(<link linkend="comedidevice">comedi_device</link> *dev,<link linkend="comedisubdevice">comedi_subdevice</link> *s,unsigned int x); 
421 };
422 </programlisting>
423 </para>
424
425 </section>
426
427 <section id="comedidriver">
428 <title>
429 <function>comedi_driver</function>
430 </title>
431
432 <para>
433 <programlisting>
434 struct comedi_driver_struct{
435         struct comedi_driver_struct *next;
436
437         char *driver_name;
438         struct module *module;
439         int (*attach)(<link linkend="comedidevice">comedi_device</link> *,comedi_devconfig *);
440         int (*detach)(<link linkend="comedidevice">comedi_device</link> *);
441
442         /* number of elements in board_name and board_id arrays */
443         unsigned int num_names;
444         void *board_name;
445         /* offset in bytes from one board name pointer to the next */
446         int offset;
447 };
448 </programlisting>
449 </para>
450
451 </section>
452
453 </section>
454
455
456 <section id="driversupportfunctions">
457 <title>
458 Generic driver support functions
459 </title>
460
461 <para>
462 The directory
463 <filename role=directory>comedi</filename> contains a large set of
464 support functions. Some of the most important ones are given below.
465 </para>
466 <para>
467 From <filename>comedi/comedi_fops.c</filename>, functions to handle the
468 hardware events (which also runs the registered callback function), to
469 get data in and out of the software data buffer, and to parse the
470 incoming functional requests:
471 <programlisting>
472   void comedi_event(<link linkend="comedidevice">comedi_device</link> *dev,<link linkend="comedisubdevice">comedi_subdevice</link> *s,unsigned int mask);
473
474   int comedi_buf_put(<link linkend="comediasync">comedi_async</link> *async, <link linkend="ref-type-sampl-t">sampl_t</link> x);
475   int comedi_buf_get(<link linkend="comediasync">comedi_async</link> *async, <link linkend="ref-type-sampl-t">sampl_t</link> *x);
476
477   static int parse_insn(<link linkend="comedidevice">comedi_device</link> *dev,<link linkend="ref-type-comedi-insn">comedi_insn</link> *insn,<link linkend="ref-type-lsampl-t">lsampl_t</link> *data,void *file);
478 </programlisting>
479 The file <filename>comedi/kcomedilib/kcomedilib_main.c</filename> provides
480 functions to register a callback, to poll an ongoing data acquisition,
481 and to print an error message:
482 <programlisting>
483   int comedi_register_callback(<link linkend="ref-type-comedi-t">comedi_t</link> *d,unsigned int subdevice, unsigned int mask,int (*cb)(unsigned int,void *),void *arg);
484
485   int comedi_poll(<link linkend="ref-type-comedi-t">comedi_t</link> *d, unsigned int subdevice);
486
487   void comedi_perror(const char *message);
488 </programlisting>
489 The file <filename>comedi/rt.c</filename> provides interrupt handling
490 for real-time tasks (one interrupt per <emphasis>device</emphasis>!):
491 <programlisting>
492   int comedi_request_irq(unsigned irq,void (*handler)(int, void *,struct pt_regs *), unsigned long flags,const char *device,<link linkend="comedidevice">comedi_device</link> *dev_id);
493   void comedi_free_irq(unsigned int irq,<link linkend="comedidevice">comedi_device</link> *dev_id)
494 </programlisting>
495 </para>
496
497 </section>
498
499 </section>
500
501
502 <section id="boardspecific">
503 <title>
504 Board-specific functionality
505 </title>
506
507 <para>
508 The <filename role=directory>/usr/src/comedi/comedi/drivers</filename>
509 subdirectory contains
510 the <emphasis role="strong">board-specific</emphasis> device driver
511 code. Each new card must get an entry in this directory.
512 <emphasis role="strong">Or</emphasis>
513 extend the functionality of an already existing driver file if the new
514 card is quite similar to that implemented in an already existing
515 driver. For example, many of the National Instruments DAQ cards use
516 the same driver files.
517 </para>
518 <para>
519 To help device driver writers,
520 &comedi; provides the <quote>skeleton</quote> of a new device driver,
521 in the <filename>comedi/drivers/skel.c</filename> file. Before
522 starting to write a new driver, make sure you understand this file,
523 and compare it to what you find in the other already available
524 board-specific files in the same directory.
525 </para>
526 <para>
527 The first thing you notice in <filename>skel.c</filename> is the
528 documentation section: the &comedi; documentation is partially
529 generated automatically, from the information that is given in this
530 section. So, please comply with the structure and the keywords
531 provided as &comedi; standards.
532 </para>
533 <para>
534 The second part of the device driver contains board-specific static
535 data structure and defines: addresses of hardware registers; defines and
536 function prototypes for functionality that is only used inside of the
537 device driver for this board; the encoding of the types and number of
538 available channels; PCI information; etc.
539 </para>
540 <para>
541 Each driver has to register two functions which are called when you
542 load and unload your board's device driver (typically via a kernel
543 module):
544 <programlisting>
545   mydriver_attach();
546   mydriver_detach();
547 </programlisting>
548 In the <quote>attach</quote> function, memory is allocated for the
549 necessary <link linkend="driverdatastructures">data structures</link>,
550 all properties of a device and its subdevices are defined, and filled
551 in in the generic &comedi; data structures. As part of this, pointers
552 to the low level instructions being supported by the subdevice have to
553 be set, which define the basic functionality. In somewhat more detail, 
554 the <function>mydriver_attach()</function> function must:
555 <itemizedlist>
556
557 <listitem>
558 <para>
559 check and request the I/O port region, IRQ, DMA, and other hardware
560 resources.  It is convenient here if you verify the existence of the
561 hardware and the correctness of the other information given.
562 Sometimes, unfortunately, this cannot be done.
563 </para>
564 </listitem>
565
566 <listitem>
567 <para>
568 allocate memory for the private data structures.
569 </para>
570 </listitem>
571
572 <listitem>
573 <para>
574 initialize the board registers and possible subdevices (timer, DMA, PCI,
575 hardware FIFO, etc.).
576 </para>
577 </listitem>
578
579 <listitem>
580 <para>
581 return 1, indicating success. If there were any errors along the way,
582 you should return the appropriate error number.  If an error is
583 returned, the <function>mydriver_detach()</function> function is
584 called.  The <function>mydriver_detach()</function> function should
585 check any resources that may have been allocated and release them as
586 necessary.  The &comedi; core frees
587 <function>dev->subdevices</function> and
588 <function>dev->private</function>, so this does not need to be done in
589 <function>detach</function>.
590 </para>
591 </listitem>
592
593 <listitem>
594 <para>
595 If the driver has the possibility to offer asynchronous data
596 acquisition, you have to code an interrupt service routine, event
597 handling routines, and/or callback routines.
598 </para>
599 </listitem>
600
601 </itemizedlist>
602 Typically, you will be able to implement most of
603 the above-mentioned functionality by
604 <emphasis>cut-and-paste</emphasis> from already existing drivers. The
605 <function>mydriver_attach()</function> function needs most of your
606 attention, because it must correctly define and allocate the (private
607 and generic) data structures that are needed for this device. That is,
608 each sub-device and each channel must get appropriate data fields, and
609 an appropriate initialization. The good news, of course, is that
610 &comedi; provides the data structures and the defines that fit very
611 well with almost all DAQ functionalities found on interface cards.
612 These can be found in the 
613 <link linkend="comedikernelgeneric">header files</link> of the
614 <filename role=directory>/usr/src/comedi/include/linux/</filename>
615 directory.
616 </para>
617 <para>
618 Drivers for digital IOs should implement the following functions:
619 <itemizedlist>
620
621 <listitem>
622 <para>
623 <function>insn_bits()</function>: drivers set this if they have a
624 function that supports reading and writing multiple bits in a digital
625 I/O subdevice at the same time.  Most (if not all) of the drivers use
626 this interface instead of insn_read and insn_write for DIO subdevices.
627 </para>
628 </listitem>
629
630 <listitem>
631 <para>
632 <function>insn_config()</function>: implements INSN_CONFIG
633 instructions.  Currently used for configuring the direction of digital
634 I/O lines, although will eventually be used for generic configuration
635 of drivers that is outside the scope of the currently defined &comedi;
636 interface.
637 </para>
638 </listitem>
639
640 </itemizedlist>
641 Finally, the device driver writer must implement the
642 <function>read</function> and <function>write</function> functions for
643 the analog channels on the card:
644 <itemizedlist>
645
646 <listitem>
647 <para>
648 <function>insn_read()</function>: acquire the inputs on the board and
649 transfer them to the software buffer of the driver.
650 </para>
651 </listitem>
652
653 <listitem>
654 <para>
655 <function>insn_write()</function>: transfer data from the software
656 buffer to the card, and execute the appropriate output conversions.
657 </para>
658 </listitem>
659
660 </itemizedlist>
661 In some drivers, you want to catch interrupts, and/or want to use the
662 <link linkend="insn-inttrig">INSN_INTTRIG</link> instruction. In this
663 case, you must provide and register these 
664 <link linkend="drivercallbacks">callback</link> functions.
665 </para>
666 <para>
667 Implementation of all of the above-mentioned functions requires
668 perfect knowledge about the hardware registers and addresses of the
669 interface card. In general, you can find
670 <emphasis>some</emphasis> inspiration in the already available device
671 drivers, but don't trust that blind
672 <emphasis>cut-and-paste</emphasis> will bring you far&hellip;
673 </para>
674
675 </section>
676
677 <section id="drivercallbacks">
678 <title>
679 Callbacks, events and interrupts
680 </title>
681
682 <para>
683 Continuous acquisition is tyically an
684 <emphasis>asynchronous</emphasis> activity: the function call that
685 has set the acquisition in motion has returned before the acquisition
686 has finished (or even started). So, not only the acquired data must be
687 sent back to the user's buffer <quote>in the background</quote>, but
688 various types of asynchronous <emphasis>event handling</emphasis> can
689 be needed during the acquisition: 
690 <itemizedlist>
691
692 <listitem>
693 <para>
694 The <emphasis>hardware</emphasis> can generate some error or
695 warning events.
696 </para>
697 </listitem>
698
699 <listitem>
700 <para>
701 Normal functional interrupts are generated by the hardware, e.g.,
702 signalling the filling-up of the card's hardware buffer, or the end of
703 an acquisition <link linkend="scan">scan</link>, etc.
704 </para>
705 </listitem>
706
707 <listitem>
708 <para>
709 The device driver writer can register a driver-supplied
710 <quote>callback</quote> function, that is called at the end of each
711 hardware interrupt routine.
712 </para>
713 </listitem>
714
715 <listitem>
716 <para>
717 Another driver-supplied callback function is executed when the user
718 program launches an <link linkend="insn-inttrig">INSN_INTTRIG</link>
719 instruction. This event handling is executed
720 <emphasis>synchronously</emphasis> with the execution of the
721 triggering instruction.
722 </para>
723 </listitem>
724
725 </itemizedlist>
726 </para>
727 <para>
728 The interrupt handlers are registered through the functions mentioned
729 <link linkend="driversupportfunctions">before</link>
730 The event handling is done in the existing &comedi; drivers in
731 statements such as this one:
732 <programlisting>
733 <anchor id="async-events">
734    s->async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR
735 </programlisting>
736 It fills in the bits corresponding to particular events in the 
737 <link linkend="comediasync">comedi_async</link> data structure.
738 The possible event bits are:
739 <itemizedlist>
740
741 <listitem>
742 <para>
743 <anchor id="comedi-cb-eoa">
744 <parameter>COMEDI_CB_EOA</parameter>: execute the callback at the
745 <quote>End Of-Acquisition</quote>.
746 </para>
747 </listitem>
748
749 <listitem>
750 <para>
751 <anchor id="comedi-cb-eos">
752 <parameter>COMEDI_CB_EOS</parameter>: execute the callback at the
753 <quote>End-Of-Scan</quote>.
754 </para>
755 </listitem>
756
757 <listitem>
758 <para>
759 <anchor id="comedi-cb-overflow">
760 <parameter>COMEDI_CB_OVERFLOW</parameter>: execute the callback when a
761 buffer overflow has occurred.
762 </para>
763 </listitem>
764
765 <listitem>
766 <para>
767 <anchor id="comedi-cb-error">
768 <parameter>COMEDI_CB_ERROR</parameter>: execute the callback at the
769 occurrence of an (undetermined) error.
770 </para>
771 </listitem>
772
773 </itemizedlist>
774 </para>
775
776 </section>
777
778
779 <section id="drivercaveats">
780 <title>
781 Device driver caveats
782 </title>
783
784 <para>
785 A few things to strive for when writing a new driver:
786 <itemizedlist>
787
788 <listitem>
789 <para>
790 Some DAQ cards consist of different <quote>layers</quote> of hardware,
791 which can each be given their own device driver. Examples are:
792 some of the National Instruments cards, that all share the same
793 <emphasis>Mite</emphasis> PCI driver chip; the ubiquitous parallel
794 port, that can be used for simple digital IO acquisitions. If your
795 new card has such a multi-layer design too, please take the effort to
796 provide drivers for each layer separately.
797 </para>
798 </listitem>
799
800 <listitem>
801 <para>
802 Your hardware driver should be functional appropriate to the resources
803 allocated.  I.e., if the driver is fully functional when configured
804 with an IRQ and DMA, it should still function moderately well with
805 just an IRQ, or still do minor tasks without IRQ or DMA.  Does your
806 driver really require an IRQ to do digital I/O?  Maybe someone will
807 want to use your driver <emphasis>just</emphasis> to do digital I/O
808 and has no interrupts available.
809 </para>
810 </listitem>
811                                                                                 
812 <listitem>
813 <para>
814 Drivers are to have absolutely <emphasis role="strong">no</emphasis>
815 global variables, mainly because the existence of global variables
816 immediately negates any possibility of using the driver for two
817 devices. The pointer <function>dev->private</function> should be used
818 to point to a structure containing any additional variables needed by
819 a driver/device combination.
820 </para>
821 </listitem>
822                                                                                 
823 <listitem>
824 <para>
825 Drivers should report errors and warnings via the
826 <function>comedi_error()</function> function.
827 (This is <emphasis>not</emphasis> the same function as the user-space
828 <link linkend="func-ref-comedi-perror">comedi_perror()</link> function.)
829
830 </para>
831 </listitem>
832
833 </itemizedlist>
834 </para>
835 </section>
836
837 <section id="integratingdriver">
838 <title>
839 Integrating the driver in the &comedi; library
840 </title>
841
842 <para>
843 For integrating new drivers in the &comedi;'s source tree the following
844 things have to be done:
845 <itemizedlist>
846
847 <listitem>
848 <para>
849 Choose a senseful name for the source code file. Let's assume here
850 that you call it <quote>mydriver.c</quote>
851 </para>
852 </listitem>
853
854 <listitem>
855 <para>
856 Put your new driver into <quote>comedi/drivers/mydriver.c</quote>.
857 </para>
858 </listitem>
859                                                                                 
860 <listitem>
861 <para>
862 Edit <quote>comedi/Config.in</quote> and add a new
863 <quote>dep_tristate</quote> line (look at the other examples). Invent a
864 senseful name for the driver's variable.  For example:
865 <programlisting>
866      dep_tristate 'MYDRIVER' CONFIG_COMEDI_MYDRIVER $CONFIG_COMEDI
867 </programlisting>
868 </para>
869 </listitem>
870
871 <listitem>
872 <para>
873 Add a line to <quote>comedi/drivers/Makefile.in</quote>, using your
874 freshly defined variable, i.e., CONFIG_COMEDI_MYDRIVER.
875 </para>
876 </listitem>
877                                                                                 
878 <listitem>
879 <para>
880 Now <command>make distclean</command>, reconfigure &comedi; with a new
881 <command>make</command>, rebuild and be happy.
882 </para>
883 <para>
884 If you want to have your driver included in the &comedi; distribution
885 (you <emphasis>definitely</emphasis> want to :-) ) send it to David
886 Schleef <address><email>ds@schleef.org</email></address> for
887 review and integration.
888 </para>
889 </listitem>
890 </itemizedlist>
891 </para>
892
893 </section>
894
895 </section>