Thursday, May 27, 2010

LDD3 notes: Driver Model

My notes while reading Linux Device Drivers 3rd edition.

2.6 device model addresses aspects common to all types of devices:
  • Power management
  • Userspace communications /sysfs
  • Hotplugging w/ udev
  • Device classes
  • Object lifecycle

Kobject, Kset, Subsystem
  • kobject per object, belongs to a ktype.
  • kset for a collection of kobjects of same type
  • Subsystem as a high-level portion of the kernel as a whole: block, devices, usb, pci
  • Class as high-level view of what a device does
Kobject
  • embed struct kobject in your structure
  • initialize name, ktype, kset and parent
  • implement a destructor (release)
  • implement reference counting by wrapping kobject_{get|put}
  • back-cast kobject references to your structure using using container_of()
Adding a Kobject to a Kset
  • point kobject.kset to set
  • point kobject.parent to the set
  • kobject_register()
Subsystem
  • a kset with a semaphore
  • rooted at the top of the sysfs hierarchy
  • subsystem_{init|register|unregister|get|put}

Bus, Device, Driver, Class, Class device
  • Bus as a channel between processor and devices: physical or virtual
  • Device as an instance to be controlled by a driver, connected to a bus
  • Driver
Bus type
  • bus_[un]register() - confusing name
  • match - compare whether a given device can be handled by the given driver
  • typically implement bus type specific functions to register devices to a specific bus instance
  • on bus enumeration
  • on platform initialization
Bus instance
  • each instance is a device
  • bus_id naming the bus instance
  • parent and/or bus could be null for a singleton top level bus, parent could be the host controller
Device
  • parent pointers reflect a tree of device attachments through buses and host controllers
  • bus_id for unique identification within a bus
  • type of bus, for matching devices and drivers
  • driver and driver private data
Class & Class device
  • Typically handled by high-level code when you register some facilities you implement
  • Device membership in a class is represented by a struct class_device

Attributes

Kobject Attributes, low-level interface
  • struct attribute: owner module and filesystem mode.
  • sysfs_ops: show/store
  • sysfs_create[_bin]_[file|link]
Attributes for high-level objects
  • [bus|driver|device]_create_file
  • BUS_ATTR(name,mode,show,store)
  • DRIVER_ATTR()
  • DEVICE_ATTR()
  • CLASS_DEVICE_ATTR()

Firmware

Driver
  • request_firmware(name)
  • release_firmware()
Userspace reacts to hotplug and special sysfs files
  • loading state
  • binary data
  • device identification
Probably implemented in udev?

Thursday, May 20, 2010

LDD3 notes: USB

My notes while reading Linux Device Drivers 3rd edition.

General

  • naming: root_hub-hub_port[-hub_port]:config.interface
  • write to bConfigurationValue will switch configuration & bound driver


URBs

  • skbuff analogy
  • queued, async, acknowledged
  • usb_alloc_urb(), usb_fill_[int|bulk]_urb(), usb_submit_urb()
  • transfer buffers must be kmalloc():ed, not static or automatic


device_id's can match

  • specific vendor/product
  • whole device class / subclass /protocol
  • individual interface class

MODULE_DEVICE_TABLE(usb, usb_device_id table)



Wednesday, May 19, 2010

vmware player network configuration

Vmware player 3.0.1 is a surprisingly capable and free virtualization environment. "Player" in the name is a bit misleading as many of the restrictions in earlier versions have been lifted.

However, network configuration tool is not installed by default and needs to be manually extracted.

Richard at f5 devcentral
provides the details:

  • You can install it manually by extracting it from the install file.
  • VMware-player-3.0.1-227600.exe /e .\vmwareextract
  • Open network.cab and copy vmnetcfg.exe to the root of your player install. You now have a nice graphical editor for your virtual network.

Thursday, May 13, 2010

LDD3 notes: Data types

My notes while reading Linux Device Drivers 3rd edition.

size_t vs ssize_t for negative values as error codes


C99 [u]intptr_t, integer which would fit a pointer. Not used in Linux. Similar to eCos cyg_addrword.


fixed size integers

  • u8..s32 within kernel
  • ioctl structures shared with userspace use prefix __u8

_t Typedefs

  • for portability only
  • out of fashion
  • inconvenient printk use

Important constants: HZ. PAGE_SIZE/SHIFT


Endianness

  • cpu_to_le32() .. be64_to_cpu()

Padding

  • force struct to match hardware expectations, without padding for performance: __attribute__ ((packed))

Error codes in pointers

  • ERR_PTR() -> IS_ERR() -> PTR_ERR()

Lists

  • list_del_init() - re-init for future re-insertion
  • list_for_each_entry() - foreach and list_entry combined


Thursday, May 6, 2010

LDD3 notes: Memory

My notes while reading Linux Device Drivers 3rd edition.

Allocation Flags

- GFP_KERNEL - system call, i.e. behalf of a process running in kernel -> may sleep

- GFP_ATOMIC - won't sleep, but has limited memory available


Slab caches for quick allocation of constant size objects ( == object pool )

- a.k.a lookaside cache

- ctor/dtor can be called in atomic context, indicated by a flag


Mempool

- a wrapper for slab cache which keeps some free memory ready

- try to avoid


vmalloc

  • contiguous region of virtual memory
  • non-hardware related work only, like loading modules
  • arbitrary size
  • GFP_KERNEL internally, cannot use in atomic context
  • limited address space available, avoid.

kmalloc

  • arbitrary size
  • contiguous region of physical memory
  • with 1:1 mapped virtual addresses

get_free_page[s]() & get_order()

  • power of 2 number of pages
  • more than 1 is failure prone
  • with 1:1 mapped virtual addresses

alloc_pages()

  • indirect access through struct page

get/put_cpu_var()

  • good for counters: percpu_counter.h
  • how to iterate over all cpus?

request_region()

  • i/o ports in separate i/o address space
  • inb() ... outsl()
  • or adapt them for memory mapped i/o using ioport_map()

request_mem_region() & ioremap()

  • memory mapped i/o
  • access through ioread8()/iowrite32_rep() ( == legacy readb() )


Chapter 8 recommends obsolete readb()? :"Rather, you should always use readb and the other I/O functions introduced in Chapter 9."


Tuesday, May 4, 2010

Insert a picture by reference in MS Word

I've always disliked manual, repetitive tasks with MS Word. There is one less to worry about, now that I've found about inserting and linking pictures.

In insert picture file dialog, select "Insert and Link", to
  • make a link referencing the original file to keep it automatically updated when the picture changes.
  • have an embedded copy in the document to avoid broken links when sending out the Word document.
How to Insert Images in a Word Document without Embedding