⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 device.c

📁 Bycore是一个嵌入式操作系统内核。Bycore包括内存管理、任务管理、中断管理、任务互斥、同步与通信管理等功能。Bycore全部由C语言完成
💻 C
字号:
/** *  device.h * *  Copyright (C) 2008  ZhangHu *  All rights reserved. *  E-MAIL: anmnmnly@gmail.com * *  This program is free software: you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation, either version 3 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program.  If not, see <http://www.gnu.org/licenses/>. */#include "include/irq.h"#include "common/string.h"#include "device.h"/* Device list that store all device nodes. */static list_t device_list[DEVICE_NR];/* All entrys of device are stored here. */extern device_init_func dev_init_func[DEVICE_NR];/** * device_add - Add a device to device list. * @Dev: Pointer to device node. * @Name: Device name. * @Mid: Main ID. * @Sid: Secondary ID. * @return: DEV_OK if successful, otherwise DEV_NO_MEM. * * @notes: */static word_t device_add(device_t *Dev, const char_t *Name,                         char_t Mid, char_t Sid) {    char_t *pname;    if((pname = (char_t*)kmalloc(sizeof(Name) + 1)) == NULL) {        return DEV_NO_MEM;    }    lib_strcpy(pname, Name);    Dev->name = pname;    Dev->mid = Mid;    Dev->sid = Sid;    spin_init(&Dev->lock);    mac_disable_irq();    add_node_list_rear(&device_list[Dev->mid], &Dev->link);    mac_enable_irq();    return DEV_OK;}/** * device_init - Initialize device list and all devices. * @return: * * @notes: */void device_init(void) {    word_t i;    for(i=0; i<DEVICE_NR; i++) {        device_list[i].next = &device_list[i];        device_list[i].prev = &device_list[i];    }	i = 0;	while((dev_init_func[i] != NULL) && (i < DEVICE_NR)) {		dev_init_func[i]();		i++;	}}/** * char_device_register - Add a device to device list. * @Dev: Pointer to device node. * @Name: Device name. * @Mid: Main ID. * @Sid: Secondary ID. * @return: DEV_OK if successful, otherwise DEV_NO_MEM. * * @notes: */word_t char_device_register(device_t *dev, const char_t *Name,                       char_t Mid, char_t Sid) {    dev->dev_type = CHDEV;    dev->used_cnt = 0;    dev->delete_flag = FALSE;    return device_add(dev, Name, Mid, Sid);}/** * device_delete - Delete a device from device list. * @Mid: Main ID. * @Sid: Secondary ID. * @return: DEV_OK if successful, otherwise DEV_NO_MEM. * * @notes: */device_t *device_delete(uword_t chdevMID, uword_t chdevSID) {    list_t *plist;    device_t *dev;    if(chdevMID >= DEVICE_NR) {        return NULL;    }    mac_disable_irq();    plist = device_list[chdevMID].next;    if(plist == &device_list[chdevMID]) {        mac_enable_irq();        return NULL;    }    do {        dev = mac_find_entry(plist, device_t, link);        if(dev->sid == chdevSID) {            if(dev->delete_flag == TRUE && dev->used_cnt <= 0) {                del_node_list(&device_list[chdevMID], plist);                mac_enable_irq();                kfree(dev->name);                return dev;            } else {                dev->delete_flag = TRUE;                break;            }        }        plist = plist->next;    } while(plist !=  &device_list[chdevMID]);    mac_enable_irq();    return NULL;}/** * device_open - Open a device. * @Mid: Main ID. * @Sid: Secondary ID. * @return: Device node. * * @notes: */device_t *device_open(const char_t *name) {    word_t i;    list_t *list;    device_t *dev;    mac_disable_irq();    for(i = 0; i < DEVICE_NR; i++) {        list = device_list[i].next;        do {            dev = mac_find_entry(list, device_t, link);            if(lib_strcmp(dev->name, name) == 0) {                mac_enable_irq();                dev->delete_flag = FALSE;                dev->used_cnt++;                return dev;            } else {                list = list->next;            }        }while(list != device_list[i].next);    }    mac_disable_irq();    return NULL;}/** * device_close - Close a device. * @Dev: Pointer to device node. * @return: * * @notes: Just descread used count. */void device_close(device_t *dev) {    dev->used_cnt--;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -