2016年6月30日 星期四

Ubuntu修改主機名稱

1. 修改/etc/hostname裡的主機名稱
$ sudo vi /etc/hostname
2. 修改/etc/hosts裡的主機名稱
$ sudo vi /etc/hosts
3. 重新啟動
sudo reboot

Ubuntu設定固定IP

1. 修改網路設定,加入一個固定IP於介面eth1
$ sudo vi /etc/network/interfaces

auto lo
iface lo inet loopback

auto eth1
iface eth1 inet static
address 192.168.56.200
netmask 255.255.255.0
2. 重啟網路設定
$ sudo /etc/init.d/networking restart

2016年6月23日 星期四

Linux kernel - platform_driver 和 platform_device 用法

1. include/linux/platform_device.h
struct platform_device {
        const char      *name;
        int             id;
        bool            id_auto;
        struct device   dev;
        u32             num_resources;
        struct resource *resource;

        const struct platform_device_id *id_entry;
        char *driver_override; /* Driver name to force a match */

        /* MFD cell pointer */
        struct mfd_cell *mfd_cell;

        /* arch specific additions */
        struct pdev_archdata    archdata;
};

struct platform_driver {
        int (*probe)(struct platform_device *);
        int (*remove)(struct platform_device *);
        void (*shutdown)(struct platform_device *);
        int (*suspend)(struct platform_device *, pm_message_t state);
        int (*resume)(struct platform_device *);
        struct device_driver driver;
        const struct platform_device_id *id_table;
        bool prevent_deferred_probe;
};

/*
 * use a macro to avoid include chaining to get THIS_MODULE
 */
#define platform_driver_register(drv) \
        __platform_driver_register(drv, THIS_MODULE)
2. drivers/base/platform.c
**
 * __platform_driver_register - register a driver for platform-level devices
 * @drv: platform driver structure
 * @owner: owning module/driver
 */
int __platform_driver_register(struct platform_driver *drv,
                                struct module *owner)
{
        drv->driver.owner = owner;
        drv->driver.bus = &platform_bus_type;
        if (drv->probe)
                drv->driver.probe = platform_drv_probe;
        if (drv->remove)
                drv->driver.remove = platform_drv_remove;
        if (drv->shutdown)
                drv->driver.shutdown = platform_drv_shutdown;

        return driver_register(&drv->driver);
}
EXPORT_SYMBOL_GPL(__platform_driver_register);

/**
 * platform_driver_unregister - unregister a driver for platform-level devices
 * @drv: platform driver structure
 */
void platform_driver_unregister(struct platform_driver *drv)
{
        driver_unregister(&drv->driver);
}
EXPORT_SYMBOL_GPL(platform_driver_unregister);
3. drivers/base/driver.c
/**
 * driver_register - register driver with bus
 * @drv: driver to register
 *
 * We pass off most of the work to the bus_add_driver() call,
 * since most of the things we have to do deal with the bus
 * structures.
 */
int driver_register(struct device_driver *drv)
{
        int ret;
        struct device_driver *other;

        BUG_ON(!drv->bus->p);

        if ((drv->bus->probe && drv->probe) ||
            (drv->bus->remove && drv->remove) ||
            (drv->bus->shutdown && drv->shutdown))
                printk(KERN_WARNING "Driver '%s' needs updating - please use "
                        "bus_type methods\n", drv->name);

        other = driver_find(drv->name, drv->bus);
        if (other) {
                printk(KERN_ERR "Error: Driver '%s' is already registered, "
                        "aborting...\n", drv->name);
                return -EBUSY;
        }

        ret = bus_add_driver(drv);
        if (ret)
                return ret;
        ret = driver_add_groups(drv, drv->groups);
        if (ret) {
                bus_remove_driver(drv);
                return ret;
        }
        kobject_uevent(&drv->p->kobj, KOBJ_ADD);

        return ret;
}
EXPORT_SYMBOL_GPL(driver_register);

/**
 * driver_unregister - remove driver from system.
 * @drv: driver.
 *
 * Again, we pass off most of the work to the bus-level call.
 */
void driver_unregister(struct device_driver *drv)
{
        if (!drv || !drv->p) {
                WARN(1, "Unexpected driver unregister!\n");
                return;
        }
        driver_remove_groups(drv, drv->groups);
        bus_remove_driver(drv);
}
EXPORT_SYMBOL_GPL(driver_unregister);
4. 範例
#include <linux/module.h>
#include <linux/kernel.h>

#include <linux/platform_device.h>

static int sample_probe(struct platform_device *pdev)
{
        pr_alert("%s\n", __FUNCTION__);

        return 0;
}

static int sample_remove(struct platform_device *pdev)
{
        return 0;
}

#define sample_suspend NULL
#define sample_resume NULL

static const struct of_device_id sample_dt_ids[] = {
        {.compatible = "sample,timer",},
        { }
};

MODULE_DEVICE_TABLE(of, sample_dt_ids);

static struct platform_driver sample_driver = {
        .probe = sample_probe,
        .remove = sample_remove,
        .suspend = sample_suspend,
        .resume = sample_resume,
        .driver = {
                .name = "sample",
                .owner = THIS_MODULE,
                .of_match_table = sample_dt_ids,
        },
};

static int __init sample_init(void)
{
        int ret;

        ret = platform_driver_register(&sample_driver);

        return ret;
}

static void __exit sample_exit(void)
{
        platform_driver_unregister(&sample_driver);
}

module_init(sample_init);
module_exit(sample_exit);

MODULE_AUTHOR("Allen");
MODULE_DESCRIPTION("sample");
MODULE_LICENSE("GPL");