sysfs esc chicago 2010

47
The Sysfs Virtual Filesystem Exploring the Linux Device Model Bill Gatliff [email protected] Freelance Embedded Systems Developer Copyright © Bill Gatliff, 2010 Sysfs Overview 1 / 47

Upload: victor-gomes

Post on 16-Oct-2014

35 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Sysfs Esc Chicago 2010

The Sysfs Virtual FilesystemExploring the Linux Device Model

Bill [email protected]

Freelance Embedded Systems Developer

Copyright © Bill Gatliff, 2010 Sysfs Overview 1 / 47

Page 2: Sysfs Esc Chicago 2010

Overview

Roadmap:

• What is sysfs?

• What is a virtual filesystem?

• What are attributes?

• Examples!

Copyright © Bill Gatliff, 2010 Sysfs Overview 2 / 47

Page 3: Sysfs Esc Chicago 2010

Sysfs

A key component of 2.6 kernels:

• A virtual filesystem

• Reflects the kernel’s device model

• Tightly coupled with Device Model API

• Usually mounted under /texttt/sys

Copyright © Bill Gatliff, 2010 Sysfs Overview 3 / 47

Page 4: Sysfs Esc Chicago 2010

Sysfs

# ls -l /systotal 0drwxr-xr-x 2 root root 0 May 28 01:23 blockdrwxr-xr-x 14 root root 0 Jan 1 1970 busdrwxr-xr-x 46 root root 0 Jan 1 1970 classdrwxr-xr-x 4 root root 0 May 28 01:23 devdrwxr-xr-x 6 root root 0 Jan 1 1970 devicesdrwxr-xr-x 2 root root 0 May 28 01:23 firmwaredrwxr-xr-x 3 root root 0 May 28 01:23 fsdrwxr-xr-x 5 root root 0 Jan 1 1970 kerneldrwxr-xr-x 62 root root 0 May 28 01:23 moduledrwxr-xr-x 2 root root 0 May 27 11:27 power

Copyright © Bill Gatliff, 2010 Sysfs Overview 4 / 47

Page 5: Sysfs Esc Chicago 2010

Sysfs

# ls -l /sys/devices/platformehci-omap.0 mmci-omap-hs.0 omap-previewergpio-keys mmci-omap-hs.1 omap-resizer.2i2c_omap.1 musb_hdrc omap2-nandi2c_omap.2 omap-iommu.0 omap2_mcspi.1i2c_omap.3 omap-mcbsp.1 omap2_mcspi.2leds-gpio omap-mcbsp.2 omap2_mcspi.3...

Copyright © Bill Gatliff, 2010 Sysfs Overview 5 / 47

Page 6: Sysfs Esc Chicago 2010

Sysfs

Virtual filesystem:

• Look like files to user applications

• No storage on persistent media

• Similar to “ramdisks”, etc.

Copyright © Bill Gatliff, 2010 Sysfs Overview 6 / 47

Page 7: Sysfs Esc Chicago 2010

Classes, Busses, etc.

There are many “devices” in /sys:

• True devices, like musb_hdrc

• Synthetic devices, like power

• Virtual devices, like input

• ??? devices, like modules

Copyright © Bill Gatliff, 2010 Sysfs Overview 7 / 47

Page 8: Sysfs Esc Chicago 2010

Classes, Busses, etc.

Sysfs is more than devices:

• It’s really a database of kernel objects

• struct kobject

Copyright © Bill Gatliff, 2010 Sysfs Overview 8 / 47

Page 9: Sysfs Esc Chicago 2010

Classes, Busses, etc.

Kobjects represent:

• Modules

• Devices

• Interfaces

• Memory

... Everything!

Copyright © Bill Gatliff, 2010 Sysfs Overview 9 / 47

Page 10: Sysfs Esc Chicago 2010

Classes, Busses, etc.

Sysfs entries are sorted:

• ... by bus type

• ... by object type

• ... by device type

• ... by parent/child relationships

• ...

Redundancies are reduced with symlinks

Copyright © Bill Gatliff, 2010 Sysfs Overview 10 / 47

Page 11: Sysfs Esc Chicago 2010

Classes, Busses, etc.

# ls -l .../i2c_omap.1/i2c-1/subsystem.../subsystem -> ../../../../bus/i2c

Copyright © Bill Gatliff, 2010 Sysfs Overview 11 / 47

Page 12: Sysfs Esc Chicago 2010

Attributes

A characteristic of the target object:

• Name, power state, bus

• “Parent”, “children” of the object

• Tuneable parameters for the object

Many make sense only for devices

Copyright © Bill Gatliff, 2010 Sysfs Overview 12 / 47

Page 13: Sysfs Esc Chicago 2010

Attributes

# ls /sys/devices/platform/i2c_omap.1/i2c-11-0048 delete_device new_device1-0049 device power1-004a i2c-dev subsystem1-004b name uevent

Copyright © Bill Gatliff, 2010 Sysfs Overview 13 / 47

Page 14: Sysfs Esc Chicago 2010

I2C-Bus Devices

Inter-Integrated Circuit:

• Multi-master, single-ended serial bus

• Attaches low-speed peripherals to a host controller

• Attaches peripherals to each other

• Ideal for embedded systems (and very popular there!)

Copyright © Bill Gatliff, 2010 Sysfs Overview 14 / 47

Page 15: Sysfs Esc Chicago 2010

I2C-Bus Devices

Bus interface:

• The part that connects the device to the bus

Device address:

• Unique for each device on a bus

Copyright © Bill Gatliff, 2010 Sysfs Overview 15 / 47

Page 16: Sysfs Esc Chicago 2010

I2C-Bus Devices

Linux is always a master device:

• Other devices are slaves to Linux

• Other devices can be masters to each other

• (This is mostly an implementation issue)

Copyright © Bill Gatliff, 2010 Sysfs Overview 16 / 47

Page 17: Sysfs Esc Chicago 2010

I2C-Bus Devices

# ls -F /sys/devices/platform/i2c_omap.1/i2c-11-0048/ delete_device new_device1-0049/ device@ power/1-004a/ i2c-dev/ subsystem@1-004b/ name uevent

Copyright © Bill Gatliff, 2010 Sysfs Overview 17 / 47

Page 18: Sysfs Esc Chicago 2010

I2C-Bus Devices

.../devices/

• It’s a device

.../platform/

• It’s a platform device

.../i2c_omap.1/

• The kobject itself

Copyright © Bill Gatliff, 2010 Sysfs Overview 18 / 47

Page 19: Sysfs Esc Chicago 2010

I2C-Bus Devices

.../i2c-dev/

• It’s an i2c host bus adapter device

• (A virtual device with its own attributes)

# cat i2c-dev/i2c-1/nameOMAP I2C adapter# cat i2c-dev/i2c-1/dev89:1

Copyright © Bill Gatliff, 2010 Sysfs Overview 19 / 47

Page 20: Sysfs Esc Chicago 2010

I2C-Bus Devices

.../1-0048/

• An attached device with address 0x48

# ls 1-0048driver modalias name power subsystem twl4030_usb uevent

# cat 1-0048/nametwl4030

Copyright © Bill Gatliff, 2010 Sysfs Overview 20 / 47

Page 21: Sysfs Esc Chicago 2010

I2C-Bus Devices

# ls 1-0048/twl4030_usbdriver subsystem vbusmicroamps_requested_usb3v1 modaliasmicroamps_requested_usb1v5 powermicroamps_requested_usb1v8 uevent

# cat 1-0048/twl4030_usb/vbusoff

Copyright © Bill Gatliff, 2010 Sysfs Overview 21 / 47

Page 22: Sysfs Esc Chicago 2010

I2C-Bus Devices

“Can I turn vbus on?”

• Nope!

# ls -l 1-0048/twl4030_usb/vbus-r--r--r-- 1 root root 4096 May 28 02:29 vbus

Copyright © Bill Gatliff, 2010 Sysfs Overview 22 / 47

Page 23: Sysfs Esc Chicago 2010

I2C-Bus Devices

Communicating with I2C slaves:

• Not a function of sysfs

• Sysfs isn’t an interface

• Interfaces use device nodes

Device nodes:

• open(), close()

• read(), write()

• mmap(), ioctl()

Copyright © Bill Gatliff, 2010 Sysfs Overview 23 / 47

Page 24: Sysfs Esc Chicago 2010

I2C-Bus Devices

To communicate with a slave:

• Call open() on the adapter’s device node

• Use ioctl() to specify chip address

• Use ioctl() to read, write the chip

#include <i2c-dev.h>

• For i2c_smbus_read_byte(), etc.

• See lm-sensors project, i2c-tools source code

Copyright © Bill Gatliff, 2010 Sysfs Overview 24 / 47

Page 25: Sysfs Esc Chicago 2010

I2C-Bus Devices

1 #include <fcntl.h>2 #include <string.h>3 #include <stdlib.h>4 #include <stdio.h>5 #include <errno.h>6

7 #include "i2c-dev.h"8

9 int main (void)10 {11 int file;

Copyright © Bill Gatliff, 2010 Sysfs Overview 25 / 47

Page 26: Sysfs Esc Chicago 2010

I2C-Bus Devices

1 int adapter_nr = 0;2 char filename[20];3

4 snprintf(filename, sizeof(filename),5 "/dev/i2c-%d", adapter_nr);6 file = open(filename, O_RDWR);7 if (file < 0) {8 perror("Could not open device");9 exit(1);

10 }

Copyright © Bill Gatliff, 2010 Sysfs Overview 26 / 47

Page 27: Sysfs Esc Chicago 2010

I2C-Bus Devices

1

2 if (ioctl(file, I2C_SLAVE, addr) < 0) {3 perror("Could not set I2C_SLAVE");4 exit(2);5 }6

7 __s32 v = 0xdeadbeef;

Copyright © Bill Gatliff, 2010 Sysfs Overview 27 / 47

Page 28: Sysfs Esc Chicago 2010

I2C-Bus Devices

1 v = i2c_smbus_read_byte(file);2 if (v < 0) {3 perror("i2c_smbus_read_word failed (2)");4 exit(3);

Copyright © Bill Gatliff, 2010 Sysfs Overview 28 / 47

Page 29: Sysfs Esc Chicago 2010

GPIO Devices

Dual personalities:

• GPIO “chip”

• GPIO “pin”

The sysfs layout accommodates both

Copyright © Bill Gatliff, 2010 Sysfs Overview 29 / 47

Page 30: Sysfs Esc Chicago 2010

GPIO Devices

# ls /sys/class/gpioexport gpiochip160@ gpiochip64@gpiochip0@ gpiochip192@ gpiochip96@gpiochip128@ gpiochip32@ unexport

# ls -F .../gpiochip192base label power/ ueventdevice@ ngpio subsystem@

Copyright © Bill Gatliff, 2010 Sysfs Overview 30 / 47

Page 31: Sysfs Esc Chicago 2010

GPIO Devices

# cat .../base192

# cat .../ngpio20

Copyright © Bill Gatliff, 2010 Sysfs Overview 31 / 47

Page 32: Sysfs Esc Chicago 2010

GPIO Devices

“Exporting” a GPIO pin:

• Each pin has a unique enumerator

• Creates an attribute directory

• Attributes to set pin direction, state

Copyright © Bill Gatliff, 2010 Sysfs Overview 32 / 47

Page 33: Sysfs Esc Chicago 2010

# echo 160 > .../export

# ls gpio160/direction power/ ueventedge subsystem@ value

Copyright © Bill Gatliff, 2010 Sysfs Overview 33 / 47

Page 34: Sysfs Esc Chicago 2010

GPIO Devices

High vs. low:

• Write “1” or “0” to value

• (Ignored if pin is an input)

Input vs. output:

• The direction attribute

• Semantics address “initial value problem”

Copyright © Bill Gatliff, 2010 Sysfs Overview 34 / 47

Page 35: Sysfs Esc Chicago 2010

GPIO Devices

# echo input > .../direction# echo output > .../direction

# echo 1 > .../value# echo 0 > .../value

# echo high > .../direction# echo low > .../direction

Copyright © Bill Gatliff, 2010 Sysfs Overview 35 / 47

Page 36: Sysfs Esc Chicago 2010

LEDs

Why have a separate API?

• Because We Can (tm)

• The LED might not be a GPIO!

Example:

• LED "triggers"

Copyright © Bill Gatliff, 2010 Sysfs Overview 36 / 47

Page 37: Sysfs Esc Chicago 2010

LEDs

Types of triggers:

• Heartbeat

• MMC, NAND, ethernet activity

• Timer

• None

The list varies depending on config

Copyright © Bill Gatliff, 2010 Sysfs Overview 37 / 47

Page 38: Sysfs Esc Chicago 2010

LEDs

# ls /sys/class/ledsbeagleboard::pmu_stat beagleboard::usr1beagleboard::usr0

# ls -F .../beagleboard::usr1brightness max_brightness subsystem@ ueventdevice@ power/ trigger

Copyright © Bill Gatliff, 2010 Sysfs Overview 38 / 47

Page 39: Sysfs Esc Chicago 2010

LEDs

# ls -l .../devicelrwxrwxrwx ... device -> ../../../leds-gpio

# cat .../triggernone nand-disk [mmc0] heartbeat

Copyright © Bill Gatliff, 2010 Sysfs Overview 39 / 47

Page 40: Sysfs Esc Chicago 2010

LEDs

Changing triggers:

• Can switch only among the available options

# echo heartbeat > .../trigger

Copyright © Bill Gatliff, 2010 Sysfs Overview 40 / 47

Page 41: Sysfs Esc Chicago 2010

Input Devices

How "input" gets into the kernel:

• A specialized char device

Used by:

• Keyboards and mice

• Tablets and touch screens

• Accelerometers, gyroscopes...

Copyright © Bill Gatliff, 2010 Sysfs Overview 41 / 47

Page 42: Sysfs Esc Chicago 2010

Input Devices

# ls -l /sys/class/inputevent0 -> .../gpio-keys/input/input0/event0event1 -> .../twl4030_pwrbutton/input/input1/event1input0 -> .../gpio-keys/input/input0input1 -> .../twl4030_pwrbutton/input/input1mice -> .../virtual/input/mice

Copyright © Bill Gatliff, 2010 Sysfs Overview 42 / 47

Page 43: Sysfs Esc Chicago 2010

Detecting Plug Events

udev(8)

• Waits for a change in /sys/devices/ directory

• Scans attributes, decides what to do next

Copyright © Bill Gatliff, 2010 Sysfs Overview 43 / 47

Page 44: Sysfs Esc Chicago 2010

Creating Attributes

#include <device.h>

int device_create_file(struct device *device,const struct device_attribute *entry);

Copyright © Bill Gatliff, 2010 Sysfs Overview 44 / 47

Page 45: Sysfs Esc Chicago 2010

Creating Attributes

ssize_t show_me(struct device *dev,struct device_attribute *attr,char *buf)

{return sprintf(buf, ‘‘\%d’’, me);

}

ssize_t store_me(struct device *dev,struct device_attribute *attr,const char *buf, size_t count)

{me = simple_strtoul(buf, NULL, 10);return count;

}

Copyright © Bill Gatliff, 2010 Sysfs Overview 45 / 47

Page 46: Sysfs Esc Chicago 2010

Creating Attributes

static DEVICE_ATTR(me, S_IWUSR | S_IRUGO, \show_me, store_me);

...

int probe(struct device *dev){

...device_create_file(dev, &dev_attr_me);...

}

Copyright © Bill Gatliff, 2010 Sysfs Overview 46 / 47

Page 47: Sysfs Esc Chicago 2010

The Sysfs Virtual FilesystemExploring the Linux Device Model

Bill [email protected]

Freelance Embedded Systems Developer