### Board Setup Code Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/sh/new-machine.txt Provides definitions for get_system_type() and platform_setup() required for board-specific setup code. This example demonstrates initializing a custom timer and starting up imaginary PCI devices. ```c #include #include /* for board_time_init() */ const char *get_system_type(void) { return "FooTech Vaporboard"; } int __init platform_setup(void) { /* * If our hardware actually existed, we would do real * setup here. Though it's also sane to leave this empty * if there's no real init work that has to be done for * this board. */ /* * Presume all FooTech boards have the same broken timer, * and also presume that we've defined foo_timer_init to * do something useful. */ board_time_init = foo_timer_init; /* Start-up imaginary PCI ... */ /* And whatever else ... */ return 0; } ``` -------------------------------- ### Start Espeakup (Manual Installation) Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/drivers/staging/speakup/spkguide.txt Manually starts the Espeakup synthesizer binary after it has been built and installed from source. ```bash /usr/bin/espeakup ``` -------------------------------- ### Start Espeakup (Package Installation) Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/drivers/staging/speakup/spkguide.txt Starts the Espeakup synthesizer daemon if it was installed as a system package. Replace 'init.d' with 'rc.d' if your system uses that directory structure. ```bash /etc/init.d/espeakup start ``` -------------------------------- ### LUKS Disk Encryption Setup Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/device-mapper/dm-crypt.txt This is a basic shell script example demonstrating how to set up LUKS disk encryption using cryptsetup. It is intended as a starting point for users familiar with the cryptsetup utility. ```shell #!/bin/sh ``` -------------------------------- ### Example Boot Setup Command (LILO) Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/scsi/sym53c8xx_2.txt Demonstrates how to pass driver configuration options via the LILO boot loader prompt. This example enables tagged commands, sets synchronous negotiation speed, and enables a specific debug flag. ```bash lilo: linux root=/dev/sda2 sym53c8xx.cmd_per_lun=4 sym53c8xx.sync=10 sym53c8xx.debug=0x200 ``` -------------------------------- ### Initrd fstab Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/power/swsusp-dmcrypt.txt An example /etc/fstab for an initrd, mounting necessary file systems like proc, sysfs, and the partition containing crypto setup keys. This defines how the initrd's temporary filesystem is structured. ```text /dev/hda1 /mnt ext3 ro 0 0 none /proc proc defaults,noatime,nodiratime 0 0 none /sys sysfs defaults,noatime,nodiratime 0 0 ``` -------------------------------- ### V4L2 Capture Example Initialization and Setup Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/driver-api/80211/uapi/v4l/capture.c.md This C code snippet demonstrates the initialization and setup for a V4L2 video capture application. It includes necessary headers, defines I/O methods, and structures for buffers. It also contains helper functions for error handling and I/O control. ```c #include #include #include #include #include /* getopt_long() */ #include /* low-level i/o */ #include #include #include #include #include #include #include #include #define CLEAR(x) memset(&(x), 0, sizeof(x)) enum io_method { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR, }; struct buffer { void *start; size_t length; }; static char *dev_name; static enum io_method io = IO_METHOD_MMAP; static int fd = -1; struct buffer *buffers; static unsigned int n_buffers; static int out_buf; static int force_format; static int frame_count = 70; static void errno_exit(const char *s) { fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno)); exit(EXIT_FAILURE); } static int xioctl(int fh, int request, void *arg) { int r; do { r = ioctl(fh, request, arg); } while (-1 == r && EINTR == errno); return r; } static void process_image(const void *p, int size) { if (out_buf) fwrite(p, size, 1, stdout); fflush(stderr); fprintf(stderr, "."); fflush(stdout); } ``` -------------------------------- ### IIO Triggered Buffer Setup Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/core-api/iio/triggered-buffers.md Demonstrates the setup of a triggered buffer, including buffer setup operations, poll function, and trigger handler. This is typically used within a device's probe function. ```c const struct iio_buffer_setup_ops sensor_buffer_setup_ops = { .preenable = sensor_buffer_preenable, .postenable = sensor_buffer_postenable, .postdisable = sensor_buffer_postdisable, .predisable = sensor_buffer_predisable, }; irqreturn_t sensor_iio_pollfunc(int irq, void *p) { pf->timestamp = iio_get_time_ns((struct indio_dev *)p); return IRQ_WAKE_THREAD; } irqreturn_t sensor_trigger_handler(int irq, void *p) { u16 buf[8]; int i = 0; /* read data for each active channel */ for_each_set_bit(bit, active_scan_mask, masklength) buf[i++] = sensor_get_data(bit) iio_push_to_buffers_with_timestamp(indio_dev, buf, timestamp); iio_trigger_notify_done(trigger); return IRQ_HANDLED; } /* setup triggered buffer, usually in probe function */ iio_triggered_buffer_setup(indio_dev, sensor_iio_polfunc, sensor_trigger_handler, sensor_buffer_setup_ops); ``` -------------------------------- ### Setup and Monitor L3 Cache Occupancy (Example 1) Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/x86/intel_rdt_ui.txt Demonstrates setting up resource control groups (p0, p1) and monitor groups (m11, m12) on a two-socket machine, then reading L3 cache occupancy data. ```bash # mount -t resctrl resctrl /sys/fs/resctrl # cd /sys/fs/resctrl # mkdir p0 p1 # echo "L3:0=3;1=c" > /sys/fs/resctrl/p0/schemata # echo "L3:0=3;1=3" > /sys/fs/resctrl/p1/schemata # echo 5678 > p1/tasks # echo 5679 > p1/tasks # cd /sys/fs/resctrl/p1/mon_groups # mkdir m11 m12 # echo 5678 > m11/tasks # echo 5679 > m12/tasks # cat m11/mon_data/mon_L3_00/llc_occupancy 16234000 # cat m11/mon_data/mon_L3_01/llc_occupancy 14789000 # cat m12/mon_data/mon_L3_00/llc_occupancy 16789000 # cat /sys/fs/resctrl/p1/mon_data/mon_l3_00/llc_occupancy 31234000 ``` -------------------------------- ### Example CPU Topology Strings Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/tools/perf/Documentation/perf.data-file-format.txt Illustrative example of how CPU topology strings might appear. ```text sibling cores : 0-3 sibling threads : 0-1 sibling threads : 2-3 ``` -------------------------------- ### V4L Device Initialization and Capture Setup Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/crypto/uapi/v4l/capture.c.md Initializes the video device, sets up capture parameters, and starts the capture process. This function is called after the device has been opened. ```c static void init_device(void) { struct v4l2_capability cap; struct v4l2_cropcap cropcap; struct v4l2_crop crop; struct v4l2_format fmt; unsigned int i; if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) { if (EINVAL == errno) { fprintf(stderr, "%s is not a V4L2 device\n", dev_name); exit(EXIT_FAILURE); } else errno_exit("VIDIOC_QUERYCAP"); } if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { fprintf(stderr, "%s is not a video capture device\n", dev_name); exit(EXIT_FAILURE); } switch (io) { case IO_METHOD_MMAP: if (!(cap.capabilities & V4L2_CAP_STREAMING)) { fprintf(stderr, "%s does not support streaming i/o\n", dev_name); exit(EXIT_FAILURE); } break; case IO_METHOD_READ: if (!(cap.capabilities & V4L2_CAP_READWRITE)) { fprintf(stderr, "%s does not support read i/o\n", dev_name); exit(EXIT_FAILURE); } break; case IO_METHOD_USERPTR: if (!(cap.capabilities & V4L2_CAP_STREAMING)) { fprintf(stderr, "%s does not support streaming i/o\n", dev_name); exit(EXIT_FAILURE); } break; } CLEAR(cropcap); cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap.c)) { struct v4l2_crop crop; if (cropcap.defrect.type != V4L2_FIELD_ANY) { crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; crop.c = cropcap.defrect; if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) { memset(&crop, 0, sizeof(crop)); } else { char value[PROPERTY_VALUE_MAX]; property_get("debug.sf.latch_underrun", value, "0"); if (strcmp(value, "1") == 0) { // This is a workaround for a bug in the driver // that causes underruns when the crop is set. // We disable the crop to avoid the underrun. memset(&crop, 0, sizeof(crop)); } } } } CLEAR(fmt); fmt.C.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (force_format) { fmt.C.pix.width = 640; fmt.C.pix.height = 480; fmt.C.pix.pixelformat = V4L2_PIX_FMT_YUYV; fmt.C.pix.field = V4L2_FIELD_INTERLACED; if (-1 == xioctl(fd, VIDIOC_S_fmt, &fmt)) { fprintf(stderr, "Failed to set format to 640x480 YUYV\n"); exit(EXIT_FAILURE); } } else { fmt.C.pix.width = 0; /* use largest size possible */ fmt.C.pix.height = 0; fmt.C.pix.pixelformat = V4L2_PIX_FMT_MJPEG; fmt.C.pix.field = V4L2_FIELD_ANY; if (-1 == xioctl(fd, VIDIOC_S_fmt, &fmt)) { fmt.C.pix.pixelformat = V4L2_PIX_FMT_YUYV; if (-1 == xioctl(fd, VIDIOC_S_fmt, &fmt)) { fprintf(stderr, "Failed to set format to YUYV\n"); exit(EXIT_FAILURE); } } } /* Check the format */ unsigned int min; if (xioctl(fd, VIDIOC_G_fmt, &fmt) < 0) errno_exit("VIDIOC_G_fmt"); if (fmt.C.pix.pixelformat == V4L2_PIX_FMT_MJPEG) fprintf(stderr, "Using MJPEG format\n"); else if (fmt.C.pix.pixelformat == V4L2_PIX_FMT_YUYV) fprintf(stderr, "Using YUYV format\n"); else fprintf(stderr, "Unknown format 0x%X\n", fmt.C.pix.pixelformat); /* check the minimum buffer size */ if (fmt.C.pix.sizeimage == 0) { /* this should not happen */ fprintf(stderr, "Invalid sizeimage %u!\n", fmt.C.pix.sizeimage); exit(EXIT_FAILURE); } min = fmt.C.pix.sizeimage; switch (io) { case IO_METHOD_READ: init_read(fmt.C.pix.sizeimage); break; case IO_METHOD_MMAP: init_mmap(); break; case IO_METHOD_USERPTR: init_userp(fmt.C.pix.sizeimage); break; } } ``` -------------------------------- ### Setup BTT Namespace with ndctl Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/nvdimm/btt.txt Use the 'ndctl' utility to set up a BTT namespace on a disk. This example creates a namespace with a 4k sector size. ```bash ndctl create-namespace -f -e namespace0.0 -m sector -l 4k ``` -------------------------------- ### AX.25 Port Configuration Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/networking/z8530drv.txt Example configuration for /etc/ax25/axports file, defining different AX.25 links with their parameters. ```text 9k6 dl0tha-9 9600 255 4 9600 baud port (scc3) axlink dl0tha-15 38400 255 4 Link to NOS ``` -------------------------------- ### Creating and Testing a Simple Initramfs with 'hello world' Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/filesystems/ramfs-rootfs-initramfs.txt This snippet demonstrates how to create a minimal C program, compile it statically, package it as a cpio.gz archive, and test it as an external initramfs using qemu. This is useful for initial setup and testing. ```c #include #include int main(int argc, char *argv[]) { printf("Hello world!\n"); sleep(999999999); } ``` ```bash gcc -static hello.c -o init echo init | cpio -o -H newc | gzip > test.cpio.gz # Testing external initramfs using the initrd loading mechanism. qemu -kernel /boot/vmlinuz -initrd test.cpio.gz /dev/zero ``` -------------------------------- ### Setting up and Monitoring Real-time Tasks Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/x86/intel_rdt_ui.txt This example demonstrates setting up the resctrl filesystem, creating a new resource control group 'p1', and moving specific CPUs to this group to monitor their cache occupancy. ```bash # mount -t resctrl resctrl /sys/fs/resctrl # cd /sys/fs/resctrl # mkdir p1 # Move the cpus 4-7 over to p1 # echo f0 > p0/cpus # View the llc occupancy snapshot # cat /sys/fs/resctrl/p1/mon_data/mon_L3_00/llc_occupancy 11234000 ``` -------------------------------- ### ALSA Mixer Get Single Value Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/admin-guide/kernel-api/writing-an-alsa-driver.md Example of retrieving register, shift, and mask values from private_value in an ALSA mixer get callback. Used for single-value controls. ```c static int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 16) & 0xff; int mask = (kcontrol->private_value >> 24) & 0xff; .... } ``` -------------------------------- ### Renesas R-Car VIN Board Setup Example (Composite Video Input) Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/devicetree/bindings/media/rcar_vin.txt Example of a board setup configuration for a composite video input using the rcar_vin driver and an ADV7180 video decoder. ```devicetree &i2c2 { status = "ok"; pinctrl-0 = <&i2c2_pins>; pinctrl-names = "default"; adv7180@20 { compatible = "adi,adv7180"; reg = <0x20>; remote = <&vin1>; port { adv7180: endpoint { bus-width = <8>; remote-endpoint = <&vin1ep0>; }; }; }; }; /* composite video input */ &vin1 { pinctrl-0 = <&vin1_pins>; pinctrl-names = "default"; status = "ok"; port { #address-cells = <1>; #size-cells = <0>; vin1ep0: endpoint { remote-endpoint = <&adv7180>; bus-width = <8>; }; }; }; ``` -------------------------------- ### Install Sphinx in a Virtual Environment Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/admin-guide/sphinx.md Install Sphinx and its dependencies within a virtual environment to avoid conflicts with system packages. This example installs Sphinx version 1.4.9. ```bash $ virtualenv sphinx_1.4 $ . sphinx_1.4/bin/activate (sphinx_1.4) $ pip install -r Documentation/sphinx/requirements.txt ``` -------------------------------- ### MDIO Managed Mode Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/devicetree/bindings/net/dsa/lan9303.txt Example DeviceTree configuration for the LAN9303 in MDIO managed mode. Includes phy-handle and MDIO bus setup. ```dts master: masterdevice@X { phy-handle = <&switch>; mdio { #address-cells = <1>; #size-cells = <0>; switch: switch-phy@0 { compatible = "smsc,lan9303-mdio"; reg = <0>; reset-gpios = <&gpio7 6 GPIO_ACTIVE_LOW>; reset-duration = <100>; ports { #address-cells = <1>; #size-cells = <0>; port@0 { reg = <0>; label = "cpu"; ethernet = <&master>; }; port@1 { /* external port 1 */ reg = <1>; label = "lan1; }; port@2 { /* external port 2 */ reg = <2>; label = "lan2"; }; }; }; }; }; ``` -------------------------------- ### Example Defconfig Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/sh/new-machine.txt Provide a default configuration file for the new board, allowing others to easily build for it. ```text arch/sh/configs/vapor_defconfig ``` -------------------------------- ### I2C Managed Mode Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/devicetree/bindings/net/dsa/lan9303.txt Example DeviceTree configuration for the LAN9303 in I2C managed mode. Includes fixed-link setup and port definitions. ```dts master: masterdevice@X { fixed-link { /* RMII fixed link to LAN9303 */ speed = <100>; full-duplex; }; }; switch: switch@a { compatible = "smsc,lan9303-i2c"; reg = <0xa>; reset-gpios = <&gpio7 6 GPIO_ACTIVE_LOW>; reset-duration = <200>; ports { #address-cells = <1>; #size-cells = <0>; port@0 { /* RMII fixed link to master */ reg = <0>; label = "cpu"; ethernet = <&master>; }; port@1 { /* external port 1 */ reg = <1>; label = "lan1; }; port@2 { /* external port 2 */ reg = <2>; label = "lan2"; }; }; }; ``` -------------------------------- ### Unbindable Mount Use Case - Initial Setup Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/filesystems/sharedsubtree.txt Demonstrates the initial setup for replicating a mount tree using bind mounts and shared subtrees, before introducing unbindable mounts. ```bash mount --bind /root /root mount --make-shared /root mkdir -p /tmp/m1 mount --rbind /root /tmp/m1 ``` -------------------------------- ### Kernel Command Line Example with dyndbg Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/admin-guide/dynamic-debug-howto.md Illustrates complex dyndbg configurations using kernel command line arguments, including comments and multiple settings. ```text Kernel command line: ... // see whats going on in dyndbg=value processing dynamic_debug.verbose=1 // enable pr_debugs in 2 builtins, #cmt is stripped dyndbg="module params +p #cmt ; module sys +p" // enable pr_debugs in 2 functions in a module loaded later pc87360.dyndbg="func pc87360_init_device +p; func pc87360_find +p" ``` -------------------------------- ### TI Composite Divider Clock Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/devicetree/bindings/clock/ti/composite.txt Example of a composite divider clock. It includes properties for bit shifting, maximum division, and index starting. ```devicetree usb_l4_div_ick: usb_l4_div_ick { #clock-cells = <0>; compatible = "ti,composite-divider-clock"; clocks = <&l4_ick>; ti,bit-shift = <4>; ti,max-div = <1>; reg = <0x0a40>; ti,index-starts-at-one; }; ``` -------------------------------- ### Client Driver Demonstration Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/mailbox.txt Demonstrates the setup and usage of both asynchronous and synchronous mailbox clients. It shows how to request channels, send messages, and wait for completion. Requires platform device initialization. ```c static void client_demo(struct platform_device *pdev) { struct demo_client *dc_sync, *dc_async; /* The controller already knows async_pkt and sync_pkt */ struct async_pkt ap; struct sync_pkt sp; dc_sync = kzalloc(sizeof(*dc_sync), GFP_KERNEL); dc_async = kzalloc(sizeof(*dc_async), GFP_KERNEL); /* Populate non-blocking mode client */ dc_async->cl.dev = &pdev->dev; dc_async->cl.rx_callback = message_from_remote; dc_async->cl.tx_done = sample_sent; dc_async->cl.tx_block = false; dc_async->cl.tx_tout = 0; /* doesn't matter here */ dc_async->cl.knows_txdone = false; /* depending upon protocol */ dc_async->async = true; init_completion(&dc_async->c); /* Populate blocking mode client */ dc_sync->cl.dev = &pdev->dev; dc_sync->cl.rx_callback = message_from_remote; dc_sync->cl.tx_done = NULL; /* operate in blocking mode */ dc_sync->cl.tx_block = true; dc_sync->cl.tx_tout = 500; /* by half a second */ dc_sync->cl.knows_txdone = false; /* depending upon protocol */ dc_sync->async = false; /* ASync mailbox is listed second in 'mboxes' property */ dc_async->mbox = mbox_request_channel(&dc_async->cl, 1); /* Populate data packet */ /* ap.xxx = 123; etc */ /* Send async message to remote */ mbox_send_message(dc_async->mbox, &ap); /* Sync mailbox is listed first in 'mboxes' property */ dc_sync->mbox = mbox_request_channel(&dc_sync->cl, 0); /* Populate data packet */ /* sp.abc = 123; etc */ /* Send message to remote in blocking mode */ mbox_send_message(dc_sync->mbox, &sp); /* At this point 'sp' has been sent */ /* Now wait for async chan to be done */ wait_for_completion(&dc_async->c); } ``` -------------------------------- ### Starting CAN Device with Bitrate Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/networking/can.txt Start a CAN network device after defining proper bit-timing parameters to avoid error-prone default settings. This example sets the bitrate. ```bash $ ip link set canX up type can bitrate 125000 ``` -------------------------------- ### Driver Initialization Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/admin-guide/drm-internals.md Demonstrates the typical steps for initializing a DRM driver: allocating a device instance, initializing it, and registering it with the kernel. ```c struct drm_device *dev; int ret; dev = drm_dev_alloc(&my_driver, sizeof(struct my_driver_private)); if (IS_ERR(dev)) { return PTR_ERR(dev); } ret = my_driver_init(dev); if (ret) { drm_dev_put(dev); return ret; } ret = drm_dev_register(dev, 0); if (ret)) { drm_dev_put(dev); return ret; } // Device is now registered and accessible from userspace. ``` -------------------------------- ### RK805 PMIC Devicetree Node Example Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/Documentation/devicetree/bindings/pinctrl/pinctrl-rk805.txt Example Devicetree node for the RK805 PMIC, demonstrating GPIO controller setup and pinmux configuration for the default state. ```devicetree rk805: rk805@18 { compatible = "rockchip,rk805"; ... gpio-controller; #gpio-cells = <2>; pinctrl-names = "default"; pinctrl-0 = <&pmic_int_l>, <&rk805_default>; rk805_default: pinmux { gpio01 { pins = "gpio0", "gpio1"; function = "gpio"; output-high; }; }; }; ``` -------------------------------- ### Example of help format configuration Source: https://github.com/cirruslogic/linux-drivers/blob/v4.14-madera/tools/perf/Documentation/perf-config.txt This configuration specifies the format for displaying help information. Options include 'man', 'info', 'web', or 'html'. ```ini [help] format = man ```