4.13.2009

How Linux kernel Init

要了解kernel 如何init
先了解kernel image 的架構


vmlinux 是kernel 本身再加了一些符號什麼的
然後把那些符號拿掉就成了image 的binary
之後用gzip 壓成piggy.gz
然後用asmbaly build 成piggy.o


*bootstrap loader and bootloader的不同
bootloader 只負責POWER ON

*bootstrap loader(second-stage loader),head.o @bootstrap loader
1.creat the proper environment to decompress,
2.relocate the kernel,
3.pass control to head.o@kernel




*head.o @kernel

1.Checks for valid processor and architecture
2.Creates initial page table entries
3.Enables the processor's memory management unit (MMU)
4.Establishes limited error detection and reporting
5.Jumps to the start of the kernel proper, main.c

在這個時候只有有限的memory maping.. 所以如果想在這加個firmwave 去down load到特定的device,是會crash的
要init driver 就透過device driver model

*start_kernel() @ .../init/main.c

USB 裝置會挑電腦?

常聽人說我的手機接我的電腦不能連 ActiveSync,但接別台電腦就可連
就說這隻手機會挑電腦

其實這只說對了一部份

當連線的時候,如果USB的訊號差,則USB的protocol 一般都可cover 掉,只要重傳就好了,只是效能沒那麼好而已
但如果訊號差都一定的程度,那就會連不起來
所以我們知道USB的protocol 對壞的訊號有一定的容忍度。

而訊號的好壞取決於三個部份,手機端(device), cable/cradle, 電腦端(host)
手機的lay out 是不是符合標準
cable/ cradle 是不是有太多connector 轉接
電腦端是不是也轉接connector (一般桌機,USB port 前面的比後面的訊號差 )
這三個部份加起來,如果訊號壞到超過potocol的容忍度,就連不起來了

而理論上來說,device 及cable 再出貨時就是要有最佳的匹配,將errors 降到最低,
這樣就有空間,留給電腦的部份,也就比較不會挑電腦了

4.12.2009

用英文,而不是學英文

一直覺得英文只是個工具
而工具,有人用的好,也有人用的不好
重點就是"用"

一個字或片語或句子結構 會不會用就決定你英文的好壞

但沒人規定說要用一個工具就一定要很會用
重點就是看你用它來做什麼


以TOEFL 為例,
就是要了解各個學科的字
及熟練如何表達自己的意思

而TOEIC,
則要多了解商業領域的字
及日常生活的用語


一些有趣的元素可以加入
1. 玩-動機, 新奇好玩
2. 演戲-角色扮演
3.

4.09.2009

工人還是功能

最近同事去巴里島度蜜月,回來帶了些雕刻飾品。
說到,當地的人雕刻東西很厲害,一面石牆不打草稿,就刻出了很漂亮的圖案。

我想這有兩種形況
1. 他已經看到他想要的圖案在石牆上,所以他就刻出他要的東西,而且也可以刻出任何他想要的東西,是藝術家,東西是藝術品。
2. 這個圖案他已經做了很多遍,已經很熟每個需要刻的位置,他只是repeat 出每個動作,可以做的很快,甚至於可以寫出SOP,只是會的圖案有限,這種人是工人,東西是產品。

我相信很多時候,是兩個同時存在在同一個人上,只是比例問題而已。
想想自己,身為工程師,有多少時間是再trace code, debug, try and error, 又有多少時間是真的從無到有做出自己想要的東西出來。

4.07.2009

測USBIF 注意事項

最近再整理筆記,趁這個機會整理一下測USBIF 的部份

1. 還沒插入USB cable 之前,USB充電應關閉,一直到set configured 之後才可開啟。
2. 用電池的產品,USB充電不可超過100mA(會影響test J/K)。
3. D+D- 的訊號線應離PHY愈近愈好,且不可串接或並接任何東西(例如bead),Handy 用coaxial cable 在機子裡跑是一大敗筆, (影響Rx sensitivity)。
4. Connector 如果可以的話,盡量用標準的USB connector,如果不是標準的,Vbus和Ground 也要先touch 在cable 插入時。
5. 有cable 的產品,再測眼圖時用FAR END來測,沒cable 的產品(大母哥) 用NEAR END測。
6. 連線完cable 拔掉之後,D+ D-應為low。
7. 在Ch9裡, default power 應該為 “BUS/SELF POWER” ,而current power 如果是SELF POWER,則Vbus 的電流不可超過100mA,如果是BUS POWER 則不可超過500mA. 但在PC suspend 時,兩者定義都不可超過2.5mA。

4.06.2009

Android HAL

有人問說有沒有一個標準的方法來加一個driver 給上層android AP用,答案是沒有。



如果我要建一個新的device, 符合Android HAL, 我知道, 我應該使用下列方法的其中一個:
1. App - Runtime Service - lib
2. App - Runtime Service - Native Service - lib
3. App - Runtime Service - Native Daemon - lib

If I want to create a new device, conforming to Android HAL, I should
use one of the following approaches:
1. App - Runtime Service - lib
2. App - Runtime Service - Native Service - lib
3. App - Runtime Service - Native Daemon - lib


但是,除了現存的devices 以外, 要改所以需要改的檔案,來增加一個新的device,似乎不容易。

我想要寫一個user-mode 的driver,而不是用kernel-mode driver model.
然後想要讓它可以在enulator 上跑。

Except for the existing devices, it is not easy to identify all the
files which need to be modified for adding a new one.
I am also interested in writing a User-Mode Driver, instead of using a
Kernel-Mode Driver model.
For now, I want to make it work in the emulator.


所以,我加了一個假的device ,有點像是sensor device。然後,我試著建立一個library (就叫fake)來處理所有必要的功能。然後把它放在 /hardware/libhardware/fake, /hardware/libhardware/include/fake, 另外還有Android.mk 放在裡面,Android.mk相關連到/dev/fak 和 /dev/input/fake(第一個for data, 第二個for 控制)

My fake device will be similar to "sensor" device.
I tried to create a library calling it "fake" and managing all
necessary features.
I put them into /hardware/libhardware/fake, /hardware/libhardware/
include/fake, with its Android.mk.
It relies on /dev/fake and /dev/input/fake (one for data, the second
one for control).


第一個問題,在Android Project Group裡有標準的方式加一個新的device嘛(kernel and/or user mode)?(檔案該放的目錄,如何加makefiles, 等等? device /dev/input/compass 是哪個driver再管理的?)有沒有一個stub 是專門再加新的dvice 及相關的Manager/Services,給上層的Application?

First question. There's a standard way in Android Project Group for
adding a new device (kernel and/or user model)? (Folders where files
should be placed, tips for makefiles, etc? The device /dev/input/
compass is managed by which driver?)
There's a stub for implementing a new device and related Manager/
Services, to higher levels up to Application?




The abstraction layers in Android are admittedly inconsistent. Audio
and camera both use a C++ pure virtual interface (e.g.
AudioHardwareInterface.h), while other places like LED's and blitter
functions have a C struct of function pointers.
Android 的抽像層不可否任地是前後茅盾的。Audio 和camera 都是C++純虛擬介面,但其它地方像LED 和blitter 就有function pointers 的 C 結構.

Because we run the actual device image in the emulator, any devices
that aren't backed by a kernel driver in the emulator need a fake
device (such as camera). For audio, we have a kernel driver for the
emulator, but other devices like the G1 have a user space layer on top
of the kernel driver. In this case, for the emulator, there is a shim
that translates AudioHardwareInterface calls to kernel calls.
因為我們是在模擬器上跑一個實體device 的image,任何在模擬器背後沒有kernel driver 的devices 都需要一個假的device(像是camera)。對audoi來說,我們有kernel driver在模擬器上,但是其它的device 像是G1 在kernel driver 的上方有個user space 層。
就這個case 來說,就有個小程式轉換AudioHardwareInterface 到呼叫kernel calls.


How you surface the driver features to the application will depend on
what you are trying to do. Most hardware features are abstracted by a
native service, for example Surfaceflinger for 2D graphics,
AudioFlinger for audio, CameraService (because CameraFlinger just
sounded wrong) for the camera. This allows us to enforce security
using the binder interface and to abstract away a lot of differences
so that applications don't have to be written to work with specific
hardware.
要如何寫driver 功能的介面給application ,取決於你打算怎麼做。大部份的硬體功能都有抽象層在現有的service裡。例如 2D graphics 有 SurfaceFlinger,audio 有AudioFlinger, camera有CameraService(因為CameraFlinger聽起來很奇怪)。在這樣的架構下,讓我們在用binder interface 時有一定的安全性,並將大部份的不同性抽象化,好讓applications 不用管硬體


I haven't looked at the compass code, but I can take you through the
camera as an example.
我還沒實際去看這個部份的code, 但是我可以給你一個以camera 為例子的例子,來說明


At the top of the stack is android.hardware.Camera, which is the Java
object that the application instantiates when it wants to take a
picture. This is a thin wrapper around android_hardware_Camera.cpp
which is the JNI interface. This in turn is a wrapper around libs/ui/
Camera.cpp which is the proxy for the remote object in the camera
service. And yes, libs/ui is a strange place for it, but it has some
circular dependencies on Surfaceflinger, so that's where it ended up.

在最上層是 android.hardware.Camera, 就是當application 拍照時會跑的Java object。它就是個小的wrapper在 android_hardware_Camera.cpp,也就是JNI介面。接下來,有另一個wrapper 在libs/ui/Camera.cpp,這個cpp是一個代理,專門for camera service 的remote object用。沒錯,放在libs/ui是蠻奇怪的,不過沒辦法,有些環境相依性的問題關於Surfaceflinger, 所以就這麼定了。


Now it gets interesting because there is a binder interface called
ICamera.h (pure virtual) which is implemented in ICamera.cpp which is
the marshalling code for the IPC binder calls. Let's just take it on
faith that the calls from the client result in a marshalled
ICameraClient object appearing on the server side of the interface.
Upon establishing a connection - provided that the application has
permission to use the camera - the camera service instantiates a
CameraHardwareInterface derived object, which is the HAL for the
actual camera hardware. The camera service takes care of preview
display and other low-level house-keeping functions that would be
difficult to do in a Java app.
現在,有趣的是,有個binder interface 叫做ICamera.h (純虛擬的),它被寫在ICamera.cpp裡,而ICamera.cpp 是marshalling code for IPC binder 呼叫的。我們只要相信,從client 來的呼叫,會導致一個marshalled 的ICameraClient object 顯示在server端的介面上。在建立連結上(所謂連結就是application 提到許可來使用camera),camera service會具現化 CameraHardwareInterface 而取得object, 這個object 就是實體camera 硬體用的HAL。而camera service 會處理preview 顯示,還有其它低階的內在功能(Java app會比較困難處理的部份)。


The camera hardware driver can either be implemented as a kernel
driver with a thin user space shim driver, or it can be implemented as
a user space driver with a thin kernel driver (primarily to control
access).
Camera hardware driver 可以寫成一個kernel driver 跟一個小user space 的driver, 或是寫成user space driver 跟一個小kernel dirver(主要是控制存取部份).


This is one of the more complex device models. Other devices (like the
LEDs) have a much simpler model.
這是比較複雜的device models的其中一個。其它的devices(像是LEDs)就有比較簡單model



>如果我要建一個新的device, 符合Android HAL, 我知道, 我應該使用下列方法的其中一個:
> 1. App - Runtime Service - lib
> 2. App - Runtime Service - Native Service - lib
> 3. App - Runtime Service - Native Daemon - lib

這要看形況,大部份的流程會像這樣:
app -> java api (manager) -> java service -> HAL module -> kernel

It depends, most of the time you'll need to do something like:

app -> java api (manager) -> java service -> HAL module -> kernel


有時候,以sensors為case來說,會有一個額外的native daemon, 但是在這個特別的case裡, it's an implementation detail (the sources for that daemon are proprietary).(看不懂什麼意思:p)

Sometimes (it's the case for the sensors), there is an extra native
daemon, but in this particular case, it's an implementation detail
(the sources for that daemon are proprietary).


有時候,native service 可以直接替換到java service,這要歸功於binder interfaces(它在程式語言上是很彈性的,所以service 可以被寫成java to c++,而不會破壞相容性,反之亦然)

Sometimes, the "java service", can be replaced directly by a "native
service", thanks to the binder interfaces (they're agnostic to the
languages involved, so a service can even be rewritten from java to
C++ or vice-versa, without breaking compatibility).


在一些cases 下,也是有可能跳過整個java service,如果HAL module 可以處理多個clients 以及相關的權限。

In some cases, it is also possible to skip the "java service"
entirely, if the HAL module can handle multiple clients and all the
permissions involved.


也有可能兩個方式都混在一起並用,以sensors 的例子來說; service 可以跟硬體建立connection , 但是app 卻可以直接跟HAL module 溝通傳資料。

It is also possible to have a mix and match, this is the case for the
sensors; the service is used to establish a connection with the
hardware, but all the data moving is done by talking directly to the
HAL module from the the app.


所以,結論就是,沒一定的規則。這要看你是想達成什麼樣的結果。

So, in short, there are no rules. It depends on what you're trying to
accomplish.

3.24.2009

PXA310 USB register 設定

Init:
1. reset
2. switch to sync mode
3. set SE0
4. 中斷開起
連起:
1. Set 3319 to Peripheral HS
2. clean XLLP_U2DOTGCR_UTMID
3. Start
4. 連的一開始3319 會變成low power mode(可能是controller 設的?)
此時如果 set XLLP_U2DOTGCR_UTMID,這次連線就是full speed, 可能是斷了chip JK的訊號


XLLP_U2DOTGCR_UTMID 和 XLLP_U2DOTGCR_ULE 有點搞不清礎

XLLP_U2DOTGCR_UTMID 感覺像是個開關, controller to 3319 這條路的開關
SPEC 說 set它是防止U2DC 的UTMI的register 去寫3319的ULPI 的register.
但要連起時卻又要clean 掉?

UDE | XLLP_U2DOTGCR_ULE
---------------------------
0 0 hold reset
0 1 can commulicate to 3319
1 0 can commulicate to 3319?
1 1 can commulicate to 3319?