1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
5 <book id="LinuxKernelAPI">
7 <title>The Linux Kernel API</title>
11 This documentation is free software; you can redistribute
12 it and/or modify it under the terms of the GNU General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later
19 This program is distributed in the hope that it will be
20 useful, but WITHOUT ANY WARRANTY; without even the implied
21 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 See the GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with this program; if not, write to the Free
28 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 For more details see the file COPYING in the source
34 distribution of Linux.
42 <title>Driver Basics</title>
43 <sect1><title>Driver Entry and Exit points</title>
44 !Iinclude/linux/init.h
47 <sect1><title>Atomic and pointer manipulation</title>
48 !Iinclude/asm-x86/atomic_32.h
49 !Iinclude/asm-x86/unaligned.h
52 <sect1><title>Delaying, scheduling, and timer routines</title>
53 !Iinclude/linux/sched.h
57 <sect1><title>High-resolution timers</title>
58 !Iinclude/linux/ktime.h
59 !Iinclude/linux/hrtimer.h
62 <sect1><title>Workqueues and Kevents</title>
65 <sect1><title>Internal Functions</title>
68 !Iinclude/linux/kthread.h
72 <sect1><title>Kernel objects manipulation</title>
74 X!Iinclude/linux/kobject.h
79 <sect1><title>Kernel utility functions</title>
80 !Iinclude/linux/kernel.h
87 <sect1><title>Device Resource Management</title>
88 !Edrivers/base/devres.c
94 <title>Data Types</title>
95 <sect1><title>Doubly Linked Lists</title>
96 !Iinclude/linux/list.h
101 <title>Basic C Library Functions</title>
104 When writing drivers, you cannot in general use routines which are
105 from the C Library. Some of the functions have been found generally
106 useful and they are listed below. The behaviour of these functions
107 may vary slightly from those defined by ANSI, and these deviations
108 are noted in the text.
111 <sect1><title>String Conversions</title>
115 <sect1><title>String Manipulation</title>
116 <!-- All functions are exported at now
121 <sect1><title>Bit Operations</title>
122 !Iinclude/asm-x86/bitops.h
126 <chapter id="kernel-lib">
127 <title>Basic Kernel Library Functions</title>
130 The Linux kernel provides more basic utility functions.
133 <sect1><title>Bitmap Operations</title>
138 <sect1><title>Command-line Parsing</title>
142 <sect1 id="crc"><title>CRC Functions</title>
152 <title>Memory Management in Linux</title>
153 <sect1><title>The Slab Cache</title>
154 !Iinclude/linux/slab.h
157 <sect1><title>User Space Memory Access</title>
158 !Iinclude/asm-x86/uaccess_32.h
159 !Earch/x86/lib/usercopy_32.c
161 <sect1><title>More Memory Management Functions</title>
169 !Emm/page-writeback.c
176 <title>Kernel IPC facilities</title>
178 <sect1><title>IPC utilities</title>
184 <title>FIFO Buffer</title>
185 <sect1><title>kfifo interface</title>
186 !Iinclude/linux/kfifo.h
191 <chapter id="relayfs">
192 <title>relay interface support</title>
195 Relay interface support
196 is designed to provide an efficient mechanism for tools and
197 facilities to relay large amounts of data from kernel space to
201 <sect1><title>relay interface</title>
207 <chapter id="modload">
208 <title>Module Support</title>
209 <sect1><title>Module Loading</title>
212 <sect1><title>Inter Module support</title>
214 Refer to the file kernel/module.c for more information.
216 <!-- FIXME: Removed for now since no structured comments in source
222 <chapter id="hardware">
223 <title>Hardware Interfaces</title>
224 <sect1><title>Interrupt Handling</title>
225 !Ekernel/irq/manage.c
228 <sect1><title>DMA Channels</title>
232 <sect1><title>Resources Management</title>
237 <sect1><title>MTRR Handling</title>
238 !Earch/x86/kernel/cpu/mtrr/main.c
241 <sect1><title>PCI Support Library</title>
243 !Edrivers/pci/pci-driver.c
244 !Edrivers/pci/remove.c
245 !Edrivers/pci/pci-acpi.c
246 !Edrivers/pci/search.c
249 <!-- FIXME: Removed for now since no structured comments in source
250 X!Edrivers/pci/hotplug.c
252 !Edrivers/pci/probe.c
255 <sect1><title>PCI Hotplug Support Library</title>
256 !Edrivers/pci/hotplug/pci_hotplug_core.c
258 <sect1><title>MCA Architecture</title>
259 <sect2><title>MCA Device Functions</title>
261 Refer to the file arch/x86/kernel/mca_32.c for more information.
263 <!-- FIXME: Removed for now since no structured comments in source
264 X!Earch/x86/kernel/mca_32.c
267 <sect2><title>MCA Bus DMA</title>
268 !Iinclude/asm-x86/mca_dma.h
273 <chapter id="firmware">
274 <title>Firmware Interfaces</title>
275 <sect1><title>DMI Interfaces</title>
276 !Edrivers/firmware/dmi_scan.c
278 <sect1><title>EDD Interfaces</title>
279 !Idrivers/firmware/edd.c
283 <chapter id="security">
284 <title>Security Framework</title>
285 !Isecurity/security.c
289 <title>Audit Interfaces</title>
292 !Ikernel/auditfilter.c
295 <chapter id="accounting">
296 <title>Accounting Framework</title>
300 <chapter id="devdrivers">
301 <title>Device drivers infrastructure</title>
302 <sect1><title>Device Drivers Base</title>
304 X!Iinclude/linux/device.h
306 !Edrivers/base/driver.c
307 !Edrivers/base/core.c
308 !Edrivers/base/class.c
309 !Edrivers/base/firmware_class.c
310 !Edrivers/base/transport_class.c
311 <!-- Cannot be included, because
312 attribute_container_add_class_device_adapter
313 and attribute_container_classdev_to_container
314 exceed allowed 44 characters maximum
315 X!Edrivers/base/attribute_container.c
319 X!Edrivers/base/interface.c
321 !Edrivers/base/platform.c
324 <sect1><title>Device Drivers Power Management</title>
325 !Edrivers/base/power/main.c
327 <sect1><title>Device Drivers ACPI Support</title>
328 <!-- Internal functions only
329 X!Edrivers/acpi/sleep/main.c
330 X!Edrivers/acpi/sleep/wakeup.c
331 X!Edrivers/acpi/motherboard.c
332 X!Edrivers/acpi/bus.c
334 !Edrivers/acpi/scan.c
335 !Idrivers/acpi/scan.c
336 <!-- No correct structured comments
337 X!Edrivers/acpi/pci_bind.c
340 <sect1><title>Device drivers PnP support</title>
342 <!-- No correct structured comments
343 X!Edrivers/pnp/system.c
346 !Idrivers/pnp/driver.c
347 !Edrivers/pnp/manager.c
348 !Edrivers/pnp/support.c
350 <sect1><title>Userspace IO devices</title>
352 !Iinclude/linux/uio_driver.h
356 <chapter id="blkdev">
357 <title>Block Devices</title>
362 !Eblock/blk-settings.c
364 !Eblock/blk-barrier.c
369 <chapter id="chrdev">
370 <title>Char devices</title>
374 <chapter id="miscdev">
375 <title>Miscellaneous Devices</title>
376 !Edrivers/char/misc.c
379 <chapter id="parportdev">
380 <title>Parallel Port Devices</title>
381 !Iinclude/linux/parport.h
382 !Edrivers/parport/ieee1284.c
383 !Edrivers/parport/share.c
384 !Idrivers/parport/daisy.c
387 <chapter id="message_devices">
388 <title>Message-based devices</title>
389 <sect1><title>Fusion message devices</title>
390 !Edrivers/message/fusion/mptbase.c
391 !Idrivers/message/fusion/mptbase.c
392 !Edrivers/message/fusion/mptscsih.c
393 !Idrivers/message/fusion/mptscsih.c
394 !Idrivers/message/fusion/mptctl.c
395 !Idrivers/message/fusion/mptspi.c
396 !Idrivers/message/fusion/mptfc.c
397 !Idrivers/message/fusion/mptlan.c
399 <sect1><title>I2O message devices</title>
400 !Iinclude/linux/i2o.h
401 !Idrivers/message/i2o/core.h
402 !Edrivers/message/i2o/iop.c
403 !Idrivers/message/i2o/iop.c
404 !Idrivers/message/i2o/config-osm.c
405 !Edrivers/message/i2o/exec-osm.c
406 !Idrivers/message/i2o/exec-osm.c
407 !Idrivers/message/i2o/bus-osm.c
408 !Edrivers/message/i2o/device.c
409 !Idrivers/message/i2o/device.c
410 !Idrivers/message/i2o/driver.c
411 !Idrivers/message/i2o/pci.c
412 !Idrivers/message/i2o/i2o_block.c
413 !Idrivers/message/i2o/i2o_scsi.c
414 !Idrivers/message/i2o/i2o_proc.c
418 <chapter id="snddev">
419 <title>Sound Devices</title>
420 !Iinclude/sound/core.h
422 !Iinclude/sound/pcm.h
424 !Esound/core/device.c
426 !Esound/core/rawmidi.c
428 !Esound/core/memory.c
429 !Esound/core/pcm_memory.c
431 !Esound/core/isadma.c
432 !Esound/core/control.c
433 !Esound/core/pcm_lib.c
435 !Esound/core/pcm_native.c
436 !Esound/core/memalloc.c
437 <!-- FIXME: Removed for now since no structured comments in source
438 X!Isound/sound_firmware.c
442 <chapter id="uart16x50">
443 <title>16x50 UART Driver</title>
444 !Iinclude/linux/serial_core.h
445 !Edrivers/serial/serial_core.c
446 !Edrivers/serial/8250.c
450 <title>Frame Buffer Library</title>
453 The frame buffer drivers depend heavily on four data structures.
454 These structures are declared in include/linux/fb.h. They are
455 fb_info, fb_var_screeninfo, fb_fix_screeninfo and fb_monospecs.
456 The last three can be made available to and from userland.
460 fb_info defines the current state of a particular video card.
461 Inside fb_info, there exists a fb_ops structure which is a
462 collection of needed functions to make fbdev and fbcon work.
463 fb_info is only visible to the kernel.
467 fb_var_screeninfo is used to describe the features of a video card
468 that are user defined. With fb_var_screeninfo, things such as
469 depth and the resolution may be defined.
473 The next structure is fb_fix_screeninfo. This defines the
474 properties of a card that are created when a mode is set and can't
475 be changed otherwise. A good example of this is the start of the
476 frame buffer memory. This "locks" the address of the frame buffer
477 memory, so that it cannot be changed or moved.
481 The last structure is fb_monospecs. In the old API, there was
482 little importance for fb_monospecs. This allowed for forbidden things
483 such as setting a mode of 800x600 on a fix frequency monitor. With
484 the new API, fb_monospecs prevents such things, and if used
485 correctly, can prevent a monitor from being cooked. fb_monospecs
486 will not be useful until kernels 2.5.x.
489 <sect1><title>Frame Buffer Memory</title>
490 !Edrivers/video/fbmem.c
493 <sect1><title>Frame Buffer Console</title>
494 X!Edrivers/video/console/fbcon.c
497 <sect1><title>Frame Buffer Colormap</title>
498 !Edrivers/video/fbcmap.c
501 drivers/video/fbgen.c has no docs, which stuffs up the sgml. Comment
502 out until somebody adds docs. KAO
503 <sect1><title>Frame Buffer Generic Functions</title>
504 X!Idrivers/video/fbgen.c
507 <sect1><title>Frame Buffer Video Mode Database</title>
508 !Idrivers/video/modedb.c
509 !Edrivers/video/modedb.c
511 <sect1><title>Frame Buffer Macintosh Video Mode Database</title>
512 !Edrivers/video/macmodes.c
514 <sect1><title>Frame Buffer Fonts</title>
516 Refer to the file drivers/video/console/fonts.c for more information.
518 <!-- FIXME: Removed for now since no structured comments in source
519 X!Idrivers/video/console/fonts.c
524 <chapter id="input_subsystem">
525 <title>Input Subsystem</title>
526 !Iinclude/linux/input.h
527 !Edrivers/input/input.c
528 !Edrivers/input/ff-core.c
529 !Edrivers/input/ff-memless.c
533 <title>Serial Peripheral Interface (SPI)</title>
535 SPI is the "Serial Peripheral Interface", widely used with
536 embedded systems because it is a simple and efficient
537 interface: basically a multiplexed shift register.
538 Its three signal wires hold a clock (SCK, often in the range
539 of 1-20 MHz), a "Master Out, Slave In" (MOSI) data line, and
540 a "Master In, Slave Out" (MISO) data line.
541 SPI is a full duplex protocol; for each bit shifted out the
542 MOSI line (one per clock) another is shifted in on the MISO line.
543 Those bits are assembled into words of various sizes on the
544 way to and from system memory.
545 An additional chipselect line is usually active-low (nCS);
546 four signals are normally used for each peripheral, plus
547 sometimes an interrupt.
550 The SPI bus facilities listed here provide a generalized
551 interface to declare SPI busses and devices, manage them
552 according to the standard Linux driver model, and perform
553 input/output operations.
554 At this time, only "master" side interfaces are supported,
555 where Linux talks to SPI peripherals and does not implement
556 such a peripheral itself.
557 (Interfaces to support implementing SPI slaves would
558 necessarily look different.)
561 The programming interface is structured around two kinds of driver,
562 and two kinds of device.
563 A "Controller Driver" abstracts the controller hardware, which may
564 be as simple as a set of GPIO pins or as complex as a pair of FIFOs
565 connected to dual DMA engines on the other side of the SPI shift
566 register (maximizing throughput). Such drivers bridge between
567 whatever bus they sit on (often the platform bus) and SPI, and
568 expose the SPI side of their device as a
569 <structname>struct spi_master</structname>.
570 SPI devices are children of that master, represented as a
571 <structname>struct spi_device</structname> and manufactured from
572 <structname>struct spi_board_info</structname> descriptors which
573 are usually provided by board-specific initialization code.
574 A <structname>struct spi_driver</structname> is called a
575 "Protocol Driver", and is bound to a spi_device using normal
579 The I/O model is a set of queued messages. Protocol drivers
580 submit one or more <structname>struct spi_message</structname>
581 objects, which are processed and completed asynchronously.
582 (There are synchronous wrappers, however.) Messages are
583 built from one or more <structname>struct spi_transfer</structname>
584 objects, each of which wraps a full duplex SPI transfer.
585 A variety of protocol tweaking options are needed, because
586 different chips adopt very different policies for how they
587 use the bits transferred with SPI.
589 !Iinclude/linux/spi/spi.h
590 !Fdrivers/spi/spi.c spi_register_board_info
595 <title>I<superscript>2</superscript>C and SMBus Subsystem</title>
598 I<superscript>2</superscript>C (or without fancy typography, "I2C")
599 is an acronym for the "Inter-IC" bus, a simple bus protocol which is
600 widely used where low data rate communications suffice.
601 Since it's also a licensed trademark, some vendors use another
602 name (such as "Two-Wire Interface", TWI) for the same bus.
603 I2C only needs two signals (SCL for clock, SDA for data), conserving
604 board real estate and minimizing signal quality issues.
605 Most I2C devices use seven bit addresses, and bus speeds of up
606 to 400 kHz; there's a high speed extension (3.4 MHz) that's not yet
608 I2C is a multi-master bus; open drain signaling is used to
609 arbitrate between masters, as well as to handshake and to
610 synchronize clocks from slower clients.
614 The Linux I2C programming interfaces support only the master
615 side of bus interactions, not the slave side.
616 The programming interface is structured around two kinds of driver,
617 and two kinds of device.
618 An I2C "Adapter Driver" abstracts the controller hardware; it binds
619 to a physical device (perhaps a PCI device or platform_device) and
620 exposes a <structname>struct i2c_adapter</structname> representing
621 each I2C bus segment it manages.
622 On each I2C bus segment will be I2C devices represented by a
623 <structname>struct i2c_client</structname>. Those devices will
624 be bound to a <structname>struct i2c_driver</structname>,
625 which should follow the standard Linux driver model.
626 (At this writing, a legacy model is more widely used.)
627 There are functions to perform various I2C protocol operations; at
628 this writing all such functions are usable only from task context.
632 The System Management Bus (SMBus) is a sibling protocol. Most SMBus
633 systems are also I2C conformant. The electrical constraints are
634 tighter for SMBus, and it standardizes particular protocol messages
635 and idioms. Controllers that support I2C can also support most
636 SMBus operations, but SMBus controllers don't support all the protocol
637 options that an I2C controller will.
638 There are functions to perform various SMBus protocol operations,
639 either using I2C primitives or by issuing SMBus commands to
640 i2c_adapter devices which don't support those I2C operations.
643 !Iinclude/linux/i2c.h
644 !Fdrivers/i2c/i2c-boardinfo.c i2c_register_board_info
645 !Edrivers/i2c/i2c-core.c
649 <title>Clock Framework</title>
652 The clock framework defines programming interfaces to support
653 software management of the system clock tree.
654 This framework is widely used with System-On-Chip (SOC) platforms
655 to support power management and various devices which may need
657 Note that these "clocks" don't relate to timekeeping or real
658 time clocks (RTCs), each of which have separate frameworks.
659 These <structname>struct clk</structname> instances may be used
660 to manage for example a 96 MHz signal that is used to shift bits
661 into and out of peripherals or busses, or otherwise trigger
662 synchronous state machine transitions in system hardware.
666 Power management is supported by explicit software clock gating:
667 unused clocks are disabled, so the system doesn't waste power
668 changing the state of transistors that aren't in active use.
669 On some systems this may be backed by hardware clock gating,
670 where clocks are gated without being disabled in software.
671 Sections of chips that are powered but not clocked may be able
672 to retain their last state.
673 This low power state is often called a <emphasis>retention
675 This mode still incurs leakage currents, especially with finer
676 circuit geometries, but for CMOS circuits power is mostly used
677 by clocked state changes.
681 Power-aware drivers only enable their clocks when the device
682 they manage is in active use. Also, system sleep states often
683 differ according to which clock domains are active: while a
684 "standby" state may allow wakeup from several active domains, a
685 "mem" (suspend-to-RAM) state may require a more wholesale shutdown
686 of clocks derived from higher speed PLLs and oscillators, limiting
687 the number of possible wakeup event sources. A driver's suspend
688 method may need to be aware of system-specific clock constraints
689 on the target sleep state.
693 Some platforms support programmable clock generators. These
694 can be used by external chips of various kinds, such as other
695 CPUs, multimedia codecs, and devices with strict requirements
696 for interface clocking.
699 !Iinclude/linux/clk.h