[Android General] llrraa2010:
1. mkdir hello

USER-NAME@MACHINE-NAME:~/Android$ cd kernel/common/drivers

USER-NAME@MACHINE-NAME:~/Android/kernel/common/drivers$ mkdir hello

2. hello.h

#ifndef _HELLO_ANDROID_H_

#define _HELLO_ANDROID_H_

#include <linux/cdev.h>

#include <linux/semaphore.h>

#define HELLO_DEVICE_NODE_NAME "hello"

#define HELLO_DEVICE_FILE_NAME "hello"

#define HELLO_DEVICE_PROC_NAME "hello"

#define HELLO_DEVICE_CLASS_NAME "hello"

struct hello_android_dev {

int val;

struct semaphore sem;

struct cdev dev;

};

#endif

3. hello.c

#include <linux/init.h>

#include <linux/module.h>

#include <linux/types.h>

#include <linux/fs.h>

#include <linux/proc_fs.h>

#include <linux/device.h>

#include <asm/uaccess.h>

#include "hello.h"

static int hello_major = 0;

static int hello_minor = 0;

static struct class* hello_class = NULL;

static struct hello_android_dev* hello_dev = NULL;

static int hello_open(struct inode* inode, struct file* filp);

static int hello_release(struct inode* inode, struct file* filp);

static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos);

static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);

static struct file_operations hello_fops = {

.owner = THIS_MODULE,

.open = hello_open,

.release = hello_release,

.read = hello_read,

.write = hello_write,

};

static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf);

static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);

static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store);

static int hello_open(struct inode* inode, struct file* filp) {

struct hello_android_dev* dev;

dev = container_of(inode->i_cdev, struct hello_android_dev, dev);

filp->private_data = dev;

return 0;

}

static int hello_release(struct inode* inode, struct file* filp) {

return 0;

}

static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) {

ssize_t err = 0;

struct hello_android_dev* dev = filp->private_data;

if(down_interruptible(&(dev->sem))) {

return -ERESTARTSYS;

}

if(count < sizeof(dev->val)) {

goto out;

}

if(copy_to_user(buf, &(dev->val), sizeof(dev->val))) {

err = -EFAULT;

goto out;

}

err = sizeof(dev->val);

out:

up(&(dev->sem));

return err;

}

static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) {

struct hello_android_dev* dev = filp->private_data;

ssize_t err = 0;

if(down_interruptible(&(dev->sem))) {

return -ERESTARTSYS;

}

if(count != sizeof(dev->val)) {

goto out;

}

if(copy_from_user(&(dev->val), buf, count)) {

err = -EFAULT;

goto out;

}

err = sizeof(dev->val);

out:

up(&(dev->sem));

return err;

}

static ssize_t __hello_get_val(struct hello_android_dev* dev, char* buf) {

int val = 0;

if(down_interruptible(&(dev->sem))) {

return -ERESTARTSYS;

}

val = dev->val;

up(&(dev->sem));

return snprintf(buf, PAGE_SIZE, "%d\n", val);

}

static ssize_t __hello_set_val(struct hello_android_dev* dev, const char* buf, size_t count) {

int val = 0;

val = simple_strtol(buf, NULL, 10);

if(down_interruptible(&(dev->sem))) {

return -ERESTARTSYS;

}

dev->val = val;

up(&(dev->sem));

return count;

}

static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf) {

struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);

return __hello_get_val(hdev, buf);

}

static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {

struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);

return __hello_set_val(hdev, buf, count);

}

static ssize_t hello_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) {

if(off > 0) {

*eof = 1;

return 0;

}

return __hello_get_val(hello_dev, page);

}

static ssize_t hello_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data) {

int err = 0;

char* page = NULL;

if(len > PAGE_SIZE) {

printk(KERN_ALERT"The buff is too large: %lu.\n", len);

return -EFAULT;

}

page = (char*)__get_free_page(GFP_KERNEL);

if(!page) {

printk(KERN_ALERT"Failed to alloc page.\n");

return -ENOMEM;

}

if(copy_from_user(page, buff, len)) {

printk(KERN_ALERT"Failed to copy buff from user.\n");

err = -EFAULT;

goto out;

}

err = __hello_set_val(hello_dev, page, len);

out:

free_page((unsigned long)page);

return err;

}

static void hello_create_proc(void) {

struct proc_dir_entry* entry;

entry = create_proc_entry(HELLO_DEVICE_PROC_NAME, 0, NULL);

if(entry) {

entry->owner = THIS_MODULE;

entry->read_proc = hello_proc_read;

entry->write_proc = hello_proc_write;

}

}

static void hello_remove_proc(void) {

remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL);

}

static int __hello_setup_dev(struct hello_android_dev* dev) {

int err;

dev_t devno = MKDEV(hello_major, hello_minor);

memset(dev, 0, sizeof(struct hello_android_dev));

cdev_init(&(dev->dev), &hello_fops);

dev->dev.owner = THIS_MODULE;

dev->dev.ops = &hello_fops;

err = cdev_add(&(dev->dev),devno, 1);

if(err) {

return err;

}

init_MUTEX(&(dev->sem));

dev->val = 0;

return 0;

}

static int __init hello_init(void){

int err = -1;

dev_t dev = 0;

struct device* temp = NULL;

printk(KERN_ALERT"Initializing hello device.\n");

err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);

if(err < 0) {

printk(KERN_ALERT"Failed to alloc char dev region.\n");

goto fail;

}

hello_major = MAJOR(dev);

hello_minor = MINOR(dev);

hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);

if(!hello_dev) {

err = -ENOMEM;

printk(KERN_ALERT"Failed to alloc hello_dev.\n");

goto unregister;

}

err = __hello_setup_dev(hello_dev);

if(err) {

printk(KERN_ALERT"Failed to setup dev: %d.\n", err);

goto cleanup;

}

hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);

if(IS_ERR(hello_class)) {

err = PTR_ERR(hello_class);

printk(KERN_ALERT"Failed to create hello class.\n");

goto destroy_cdev;

}

temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);

if(IS_ERR(temp)) {

err = PTR_ERR(temp);

printk(KERN_ALERT"Failed to create hello device.");

goto destroy_class;

}

err = device_create_file(temp, &dev_attr_val);

if(err < 0) {

printk(KERN_ALERT"Failed to create attribute val.");

goto destroy_device;

}

dev_set_drvdata(temp, hello_dev);

hello_create_proc();

printk(KERN_ALERT"Succedded to initialize hello device.\n");

return 0;

destroy_device:

device_destroy(hello_class, dev);

destroy_class:

class_destroy(hello_class);

destroy_cdev:

cdev_del(&(hello_dev->dev));

cleanup:

kfree(hello_dev);

unregister:

unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);

fail:

return err;

}

static void __exit hello_exit(void) {

dev_t devno = MKDEV(hello_major, hello_minor);

printk(KERN_ALERT"Destroy hello device.\n");

hello_remove_proc();

if(hello_class) {

device_destroy(hello_class, MKDEV(hello_major, hello_minor));

class_destroy(hello_class);

}

if(hello_dev) {

cdev_del(&(hello_dev->dev));

kfree(hello_dev);

}

unregister_chrdev_region(devno, 1);

}

MODULE_LICENSE("GPL");

MODULE_DESCRIPTION("First Android Driver");

module_init(hello_init);

module_exit(hello_exit);

4. things added to Kconfig Makefile

Kconfig:

config HELLO

tristate "First Android Driver"

default n

help

This is the first android driver.

Makefile:

obj-$(CONFIG_HELLO) += hello.o

5. menu menuconfig

In arch/arm/Kconfig and drivers/kconfig, add one line between menu "Device Drivers" and endmenu:

source "drivers/hello/Kconfig"

6. change drivers/Makefile, add one line:

obj-$(CONFIG_HELLO) += hello/

7. menuconfig

USER-NAME@MACHINE-NAME:~/Android/kernel/common$ make menuconfig

set option "Device Drivers" => "First Android Drivers" to "y"

8. make

USER-NAME@MACHINE-NAME:~/Android/kernel/common$ make

9. rebuild kernel and test the driver

USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel ./kernel/common/arch/arm/boot/zImage &

USER-NAME@MACHINE-NAME:~/Android$ adb shell

File hello should be available in /dev:

root@android:/ # cd dev

root@android:/dev # ls

File hello should be available in /proc:

root@android:/ # cd proc

root@android:/proc # ls

Get the value:

root@android:/proc # cat hello

0

root@android:/proc # echo '5' > hello

root@android:/proc # cat hello

5

Dir hello should be available in /sys/class:

root@android:/ # cd sys/class

root@android:/sys/class # ls

Dir hello should be available in /sys/class/hello:

root@android:/sys/class # cd hello

root@android:/sys/class/hello # ls

File val should be available in /sys/class/hello/hello:

root@android:/sys/class/hello # cd hello

root@android:/sys/class/hello/hello # ls

access the value of val:

root@android:/sys/class/hello/hello # cat val

5

root@android:/sys/class/hello/hello # echo '0' > val

root@android:/sys/class/hello/hello # cat val

0

about "inside android"

Hi, all.

As experienced android developers or just beginners, sometimes you might have questions about what's going on inside android. Such as how activity starts, how service starts, etc. If we look into the code, we might have the answer, but it takes time and much effort. In fact, there's millions of lines of source code in android. Fortunately, Shengyang Luo wrote a book in chinese to clarify all these questions, with all the related knowledge he got in these years as an experienced engineer of linux/android. Now, he and I decide this book belongs to the android family, so to make more people get benefit from it, we will rewrite it in English.

Also, we want this book to be more adapted to your need, we might share the outline and part of the content of the book here, so that we all can discuss it while rewriting it. We might change the book according to your advice. For example, there might be more details for the parts you are most interested. To do this, we will have pilot PPTs and passages soon. The pilot topics would be as below:

Android design philosophy--everything is component

Initializing a Build/developing/debugging Environment for Android

Android architecture

Hardware Abstraction Layer(HAL)

drivers specific to android

process management

messaging handling

input handling

UI architecture

resource management framework

Dalvik virtual machine

Which is most interested to you? Please share with us, and we will make it available ASAP.

We haven't talked with any press for publishing yet. So if any press is interested in publishing this book, please contact me. As you all know, writing is a tough job, especially when the book is regarding such a complicated android os system, so cheers, encouragement and funding are welcomed badly. Funding would help us not worry about making a living and dedicate to the writing or even hire some people to accelerate the progress. For funding, please visit indiegogo.com and search "inside android".

The book would have 3 parts: say hello to android, drivers specific to android and understanding android in scenes. The first 2 parts are foundation knowledge for the third part, which is the major part of the book.

Part I say hello to android includes 3 chapters, and introduces how to download the source code, build it, run it, smart pointer and Hardware Abstraction Layer(HAL). Since smart pointer is widely used in the native code of application framework, it would be very helpful if we get to know it before we dive into the scenes. Knowing HAL would help us understand the architecture of android, and then make the third part easier to learn.

Part II drivers specific to android also includes 3 chapters, introducing 3 drivers specific to android: Logger Driver, Binder IPC Driver and Ashmem Shared Memory Driver. No doubt, these 3 drivers, especially the Binder IPC Driver, are the foundation of android. knowing about them would definitely help understand android.

Part III understanding android in scenes includes more than 10 chapters, and introduces android in different scenes, such as the start of something(activity, service, zygote, service manager, etc.), process management, messaging handling etc.

The book is based on android 2.3. If needed, we can rewrite it on newer version.

Below is the outline of the book:

Part I: say hello to android

chapter 1　prerequisite

1.1　reference books on Linux kernel

1.2　reference books on Android application developing

1.3　download, build and run Android source code

1.3.1　download Android source code

1.3.2　build Android source code

1.3.3　run Android emulator

1.4　download, build and run Android kernel source code

1.4.1　download Android kernel source code

1.4.2　build Android kernel source code

1.4.3　run Android emulator

1.5　develop one Android application(example)

1.6　build and pack Android module independently

1.6.1　mmm: Builds all of the modules in the supplied directories

1.6.2　build one Android module independently

1.6.3　repack Android images

chapter 2 Hardware Abstraction Layer(HAL)

2.1　develop Android hardware driver

2.1.1　implementation of one kernel driver

2.1.2　modify Kconfig of the kernel

2.1.3　modify Makefile of the kernel

2.1.4　build the driver

2.1.5　test the driver

2.2　test the driver with C executable

2.3　develop HAL module

2.3.1　specification for HAL module

2.3.2　interface for HAL module

2.3.3　load the HAL module

2.3.4　access permission for the hardware

2.4　develop service to the Android hardware

2.4.1　defite the service interface to hardware

2.4.2　implementation of the service

2.4.3　JNI interface for the service

2.4.4　start of the service

2.5　Android application on the service to hardware

chapter 3 smart pointer

3.1　light weight pointer

3.1.1　implementation

3.1.2　example

3.2　strong pointer and weak pointer

3.2.1　strong pointer

3.2.2 weak pointer

3.2.3 example

Part II: drivers specific to android

chapter 4 Logger driver

4.1　Logger format

4.2　Logger driver

4.2.1　data structure

4.2.2　initialization

4.2.3　open the Logger driver

4.2.4　read the log

4.2.5　write the log

4.3　runtime library for the Logger driver

4.4　log interface for C/C++

4.5　log interface for Java

4.6　Logcat tool

4.6.1　data structure

4.6.2　initialization

4.6.3　read the log

4.6.4　output the log

chapter 5 Binder IPC Driver

5.1　Binder driver

5.1.1　data structure

5.1.2　initialization

5.1.3　open

5.1.4　mapping to the memory

5.1.5　kernel cache management

5.2　runtime library for Binder IPC Driver

5.3　example for Binder IPC Driver application

5.4　counting for Binder object reference

5.4.1　lifecycle of Binder local object

5.4.2　lifecycle of Binder instance

5.4.3　lifecycle of Binder reference

5.4.4　lifecycle of Binder proxy

5.5　Binder object death notification

5.5.1　register death notification

5.5.2　send death notification

5.5.3　unregister death notification

5.6　start of Service Manager

5.6.1　open and mapping on Binder

5.6.2　register as Binder context manager

5.6.3　wait for the Client request in loop

5.7　getting the Service Manager proxy

5.8　start of Service

5.8.1　register Service

5.8.2　start of Binder thread pool

5.9　how to get Service proxy

5.10　Java interface for Binder IPC Driver

5.10.1　getting the Java proxy of Service Manager

5.10.2　define the Java service

5.10.3　start of Java service

5.10.4　getting Java service proxy

5.10.5　calling Java service

chapter 6 Ashmem Shared Memory Driver

6.1　Ashmem driver

6.1.1　data structure

6.1.2　initialization

6.1.3　open

6.1.4　mapping to the memory

6.1.5　lock and unlock

6.1.6　recycle

6.2　Ashmem interface in runtime library cutils

6.3　C++ interface for Ashmem

6.3.1　MemoryHeapBase

6.3.2　MemoryBase

6.3.3　example

6.4　Java interface for Ashmem

6.4.1　MemoryFile

6.4.2　example

6.5　how Ashmem shares memory

Part III: understanding android in scenes

chapter 7 start of Activity

7.1　Activity example

7.2　start of root Activity

7.3　start of child Activity in the same process

7.4　start of child Activity in the new process

chapter 8 start of Service

8.1　Service example

8.2 start of Service in the same process

8.3　start of Service in the new process

chapter 9 Broadcast

9.1　example

9.2　register

9.3　send

chapter 10 Content Provider

10.1　Content Provider example

10.1.1　ArticlesProvider

10.1.2　Article

10.2　start of Content Provider

10.3　data sharing of Content Provider

10.3.1　data sharing model

10.3.2　data transmission

10.4　Content Provider's notification for data Change

10.4.1　register ContentObserver

10.4.2　send notification for data Change

chapter 11 start of Zygote and System process

11.1　start script of Zygote process

11.2　start of Zygote

11.3　start of System

chapter 12 start of Android application

12.1　creation of application process

12.2　start of Binder thread pool

12.3　creation of message loop

chapter 13 message handling

13.1　creation of message queue

13.2　message cycle

13.3　message sending

13.4　message handling

chapter 14 keyboard message handling

14.1　keyboard message handling model

14.2　start of InputManager

14.2.1　creation of InputManager

14.2.2　start of InputManager

14.2.3　start of InputDispatcher

14.2.4　start of InputReader

14.3　register InputChannel

14.3.1　creation of InputChannel

14.3.2　register InputChannel of server side

14.3.3　register the window of currently activated application

14.3.4　register InputChannel of client side

14.4　Dispatch of the keyboard message

14.4.1　InputReader gets keyboard event

14.4.2　InputDispatcher dispatch keyboard event

14.4.3　the window of currently activated application gets keyboard message

14.4.4　InputDispatcher gets notification when the keyboard event handling is done

14.5　unregister InputChannel

14.5.1　destroy application window

14.5.2　unregister InputChannel of client side

14.5.3　unregister InputChannel of server side

chapter 15 message loop model of Android application thread

15.1　message loop model of application main thread

15.2　message loop model of nongraphic application child thread

15.3　message loop model of graphic application child thread

chapter 16 installation and showup of Android application

16.1　installation of application

16.2　showup of application

chapter 17 UI system

17.1 UI architecture

17.2 application UI framework

17.3 WindowManagerService

17.4 SurfaceFlinger

17.5 Multiple Display support

chapter 18 resource management framework

18.1 resource management framework

18.2 compilation of resource

18.3 search for resource

chapter 19 Dalvik virtual machine(DVM)

19.1 introduction

19.2 start of DVM

19.3 execution of DVM

19.4 register JNI methods

19.5 DVM process

19.6 DVM thread