dcdbas.c
来自「linux 内核源代码」· C语言 代码 · 共 662 行 · 第 1/2 页
C
662 行
/* * dcdbas.c: Dell Systems Management Base Driver * * The Dell Systems Management Base Driver provides a sysfs interface for * systems management software to perform System Management Interrupts (SMIs) * and Host Control Actions (power cycle or power off after OS shutdown) on * Dell systems. * * See Documentation/dcdbas.txt for more information. * * Copyright (C) 1995-2006 Dell Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2.0 as published by * the Free Software Foundation. * * 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. */#include <linux/platform_device.h>#include <linux/dma-mapping.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/mc146818rtc.h>#include <linux/module.h>#include <linux/reboot.h>#include <linux/sched.h>#include <linux/smp.h>#include <linux/spinlock.h>#include <linux/string.h>#include <linux/types.h>#include <linux/mutex.h>#include <asm/io.h>#include <asm/semaphore.h>#include "dcdbas.h"#define DRIVER_NAME "dcdbas"#define DRIVER_VERSION "5.6.0-3.2"#define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"static struct platform_device *dcdbas_pdev;static u8 *smi_data_buf;static dma_addr_t smi_data_buf_handle;static unsigned long smi_data_buf_size;static u32 smi_data_buf_phys_addr;static DEFINE_MUTEX(smi_data_lock);static unsigned int host_control_action;static unsigned int host_control_smi_type;static unsigned int host_control_on_shutdown;/** * smi_data_buf_free: free SMI data buffer */static void smi_data_buf_free(void){ if (!smi_data_buf) return; dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n", __FUNCTION__, smi_data_buf_phys_addr, smi_data_buf_size); dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf, smi_data_buf_handle); smi_data_buf = NULL; smi_data_buf_handle = 0; smi_data_buf_phys_addr = 0; smi_data_buf_size = 0;}/** * smi_data_buf_realloc: grow SMI data buffer if needed */static int smi_data_buf_realloc(unsigned long size){ void *buf; dma_addr_t handle; if (smi_data_buf_size >= size) return 0; if (size > MAX_SMI_DATA_BUF_SIZE) return -EINVAL; /* new buffer is needed */ buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL); if (!buf) { dev_dbg(&dcdbas_pdev->dev, "%s: failed to allocate memory size %lu\n", __FUNCTION__, size); return -ENOMEM; } /* memory zeroed by dma_alloc_coherent */ if (smi_data_buf) memcpy(buf, smi_data_buf, smi_data_buf_size); /* free any existing buffer */ smi_data_buf_free(); /* set up new buffer for use */ smi_data_buf = buf; smi_data_buf_handle = handle; smi_data_buf_phys_addr = (u32) virt_to_phys(buf); smi_data_buf_size = size; dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n", __FUNCTION__, smi_data_buf_phys_addr, smi_data_buf_size); return 0;}static ssize_t smi_data_buf_phys_addr_show(struct device *dev, struct device_attribute *attr, char *buf){ return sprintf(buf, "%x\n", smi_data_buf_phys_addr);}static ssize_t smi_data_buf_size_show(struct device *dev, struct device_attribute *attr, char *buf){ return sprintf(buf, "%lu\n", smi_data_buf_size);}static ssize_t smi_data_buf_size_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ unsigned long buf_size; ssize_t ret; buf_size = simple_strtoul(buf, NULL, 10); /* make sure SMI data buffer is at least buf_size */ mutex_lock(&smi_data_lock); ret = smi_data_buf_realloc(buf_size); mutex_unlock(&smi_data_lock); if (ret) return ret; return count;}static ssize_t smi_data_read(struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t count){ size_t max_read; ssize_t ret; mutex_lock(&smi_data_lock); if (pos >= smi_data_buf_size) { ret = 0; goto out; } max_read = smi_data_buf_size - pos; ret = min(max_read, count); memcpy(buf, smi_data_buf + pos, ret);out: mutex_unlock(&smi_data_lock); return ret;}static ssize_t smi_data_write(struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t count){ ssize_t ret; if ((pos + count) > MAX_SMI_DATA_BUF_SIZE) return -EINVAL; mutex_lock(&smi_data_lock); ret = smi_data_buf_realloc(pos + count); if (ret) goto out; memcpy(smi_data_buf + pos, buf, count); ret = count;out: mutex_unlock(&smi_data_lock); return ret;}static ssize_t host_control_action_show(struct device *dev, struct device_attribute *attr, char *buf){ return sprintf(buf, "%u\n", host_control_action);}static ssize_t host_control_action_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ ssize_t ret; /* make sure buffer is available for host control command */ mutex_lock(&smi_data_lock); ret = smi_data_buf_realloc(sizeof(struct apm_cmd)); mutex_unlock(&smi_data_lock); if (ret) return ret; host_control_action = simple_strtoul(buf, NULL, 10); return count;}static ssize_t host_control_smi_type_show(struct device *dev, struct device_attribute *attr, char *buf){ return sprintf(buf, "%u\n", host_control_smi_type);}static ssize_t host_control_smi_type_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ host_control_smi_type = simple_strtoul(buf, NULL, 10); return count;}static ssize_t host_control_on_shutdown_show(struct device *dev, struct device_attribute *attr, char *buf){ return sprintf(buf, "%u\n", host_control_on_shutdown);}static ssize_t host_control_on_shutdown_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ host_control_on_shutdown = simple_strtoul(buf, NULL, 10); return count;}/** * smi_request: generate SMI request * * Called with smi_data_lock. */static int smi_request(struct smi_cmd *smi_cmd){ cpumask_t old_mask; int ret = 0; if (smi_cmd->magic != SMI_CMD_MAGIC) { dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n", __FUNCTION__); return -EBADR; } /* SMI requires CPU 0 */ old_mask = current->cpus_allowed; set_cpus_allowed(current, cpumask_of_cpu(0)); if (smp_processor_id() != 0) { dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n", __FUNCTION__); ret = -EBUSY; goto out; } /* generate SMI */ asm volatile ( "outb %b0,%w1" : /* no output args */ : "a" (smi_cmd->command_code), "d" (smi_cmd->command_address), "b" (smi_cmd->ebx), "c" (smi_cmd->ecx) : "memory" );out: set_cpus_allowed(current, old_mask); return ret;}/** * smi_request_store: * * The valid values are: * 0: zero SMI data buffer * 1: generate calling interface SMI * 2: generate raw SMI * * User application writes smi_cmd to smi_data before telling driver * to generate SMI. */static ssize_t smi_request_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ struct smi_cmd *smi_cmd; unsigned long val = simple_strtoul(buf, NULL, 10); ssize_t ret; mutex_lock(&smi_data_lock); if (smi_data_buf_size < sizeof(struct smi_cmd)) { ret = -ENODEV; goto out; } smi_cmd = (struct smi_cmd *)smi_data_buf; switch (val) { case 2: /* Raw SMI */ ret = smi_request(smi_cmd); if (!ret) ret = count; break; case 1: /* Calling Interface SMI */ smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer); ret = smi_request(smi_cmd); if (!ret) ret = count;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?