來自社區comments的總結(未完)

一. 格式

1. 函數定義需要這行的話,應該和’(‘對齊
This line should be aligned with the opening ( above.

static inline int handle_lsr_errors(struct uart_port *port,
                                    unsigned int *flag, unsigned int *lsr)


2. 向已有的文件添加內容,應該按照字母排序地去添加

對於社區中有一些老代碼本就不是按照字母排序的方式排列定義,那就保證我們自己的添加是按照字母排序的就好。


3. #include頭文件,按照字母排序羅列

對於社區中有一些老代碼本就不是按照字母排序的方式排列定義,那就保證我們自己的添加是按照字母排序的就好。



二. 變量

1. void類型的指針,不許要做轉換,可直接複製給任意類型指針變量。
    No need to cast a void pointer.

static inline void sprd_rx(int irq, void *dev_id)  
{  
     struct uart_port *port = (struct uart_port *)dev_id;
     ...




三. 函數

1. 請不要使用unlikely做判斷,除非有確鑿的證據說明unlikely能提高效率。編譯器和cpu已經知道0是正常的返回值。
NEVER use unlikely unless you can measure the difference without it.
And even then, you better be able to justify it.  For something as dumb
as this type of check, you are working against the complier and cpu which
already knows that 0 is the usual response.

2. 調用ioremap()函數需要做返回值判斷 

Return value of ioremap() should be checked for NULL.

up->membase = ioremap(mem->start, resource_size(mem));


3. 調用devm_request_irq()函數,需注意irqflags。

如下代碼片段: irqflags = IRQF_SHARED,如果是這樣的情況,就要注意在handle_irq()函數中判斷,當前處理的中斷是否爲this device觸發的,如果不是,需要在handle_irq()函數中返回IRQ_NONE。

ret = devm_request_irq(port->dev, port->irq, handle_irq,
                       IRQF_SHARED, sp->name, port);
static irqreturn_t handle_irq(int irq, void *dev_id)
{
    ...    
    ims = serial_in(port, SPRD_IMSR);
    if (!ims)
        return IRQ_NONE;
    ...
    return IRQ_HANDLED;
}


四. 頭文件

儘量不要包含與體系結構相關的頭文件,

如. 我們在sprd_serial.c中包含了asm/irq.h,遭到吐槽。

> why is a modern driver using asm/irq.h in the
> first place.
>
> We used to include that file when platforms defined IRQ numbers
> statically, but modern platforms don't do that, so it shouldn't be
> required anymore.


五. 註釋說明和文檔

  1. 每個dts中的compitible string都要添加binding說明

每個compitible string要能明確的代表節點,不要用過於通用的compitible string,比如下面是不合格的:

uart1: serial@70100000 {
        compatible = "xxx,serial";
        ...

應該改成"xxx,sc9836-uart"



六. DTS

1. aliases 節點應在所有節點前面定義



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章