dcdbas.c
来自「linux 内核源代码」· C语言 代码 · 共 662 行 · 第 1/2 页
C
662 行
break; case 0: memset(smi_data_buf, 0, smi_data_buf_size); ret = count; break; default: ret = -EINVAL; break; }out: mutex_unlock(&smi_data_lock); return ret;}/** * host_control_smi: generate host control SMI * * Caller must set up the host control command in smi_data_buf. */static int host_control_smi(void){ struct apm_cmd *apm_cmd; u8 *data; unsigned long flags; u32 num_ticks; s8 cmd_status; u8 index; apm_cmd = (struct apm_cmd *)smi_data_buf; apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL; switch (host_control_smi_type) { case HC_SMITYPE_TYPE1: spin_lock_irqsave(&rtc_lock, flags); /* write SMI data buffer physical address */ data = (u8 *)&smi_data_buf_phys_addr; for (index = PE1300_CMOS_CMD_STRUCT_PTR; index < (PE1300_CMOS_CMD_STRUCT_PTR + 4); index++, data++) { outb(index, (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4)); outb(*data, (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4)); } /* first set status to -1 as called by spec */ cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL; outb((u8) cmd_status, PCAT_APM_STATUS_PORT); /* generate SMM call */ outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT); spin_unlock_irqrestore(&rtc_lock, flags); /* wait a few to see if it executed */ num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING; while ((cmd_status = inb(PCAT_APM_STATUS_PORT)) == ESM_STATUS_CMD_UNSUCCESSFUL) { num_ticks--; if (num_ticks == EXPIRED_TIMER) return -ETIME; } break; case HC_SMITYPE_TYPE2: case HC_SMITYPE_TYPE3: spin_lock_irqsave(&rtc_lock, flags); /* write SMI data buffer physical address */ data = (u8 *)&smi_data_buf_phys_addr; for (index = PE1400_CMOS_CMD_STRUCT_PTR; index < (PE1400_CMOS_CMD_STRUCT_PTR + 4); index++, data++) { outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT)); outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT)); } /* generate SMM call */ if (host_control_smi_type == HC_SMITYPE_TYPE3) outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT); else outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT); /* restore RTC index pointer since it was written to above */ CMOS_READ(RTC_REG_C); spin_unlock_irqrestore(&rtc_lock, flags); /* read control port back to serialize write */ cmd_status = inb(PE1400_APM_CONTROL_PORT); /* wait a few to see if it executed */ num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING; while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) { num_ticks--; if (num_ticks == EXPIRED_TIMER) return -ETIME; } break; default: dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n", __FUNCTION__, host_control_smi_type); return -ENOSYS; } return 0;}/** * dcdbas_host_control: initiate host control * * This function is called by the driver after the system has * finished shutting down if the user application specified a * host control action to perform on shutdown. It is safe to * use smi_data_buf at this point because the system has finished * shutting down and no userspace apps are running. */static void dcdbas_host_control(void){ struct apm_cmd *apm_cmd; u8 action; if (host_control_action == HC_ACTION_NONE) return; action = host_control_action; host_control_action = HC_ACTION_NONE; if (!smi_data_buf) { dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __FUNCTION__); return; } if (smi_data_buf_size < sizeof(struct apm_cmd)) { dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n", __FUNCTION__); return; } apm_cmd = (struct apm_cmd *)smi_data_buf; /* power off takes precedence */ if (action & HC_ACTION_HOST_CONTROL_POWEROFF) { apm_cmd->command = ESM_APM_POWER_CYCLE; apm_cmd->reserved = 0; *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0; host_control_smi(); } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) { apm_cmd->command = ESM_APM_POWER_CYCLE; apm_cmd->reserved = 0; *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20; host_control_smi(); }}/** * dcdbas_reboot_notify: handle reboot notification for host control */static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code, void *unused){ switch (code) { case SYS_DOWN: case SYS_HALT: case SYS_POWER_OFF: if (host_control_on_shutdown) { /* firmware is going to perform host control action */ printk(KERN_WARNING "Please wait for shutdown " "action to complete...\n"); dcdbas_host_control(); } break; } return NOTIFY_DONE;}static struct notifier_block dcdbas_reboot_nb = { .notifier_call = dcdbas_reboot_notify, .next = NULL, .priority = INT_MIN};static DCDBAS_BIN_ATTR_RW(smi_data);static struct bin_attribute *dcdbas_bin_attrs[] = { &bin_attr_smi_data, NULL};static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);static DCDBAS_DEV_ATTR_WO(smi_request);static DCDBAS_DEV_ATTR_RW(host_control_action);static DCDBAS_DEV_ATTR_RW(host_control_smi_type);static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);static struct attribute *dcdbas_dev_attrs[] = { &dev_attr_smi_data_buf_size.attr, &dev_attr_smi_data_buf_phys_addr.attr, &dev_attr_smi_request.attr, &dev_attr_host_control_action.attr, &dev_attr_host_control_smi_type.attr, &dev_attr_host_control_on_shutdown.attr, NULL};static struct attribute_group dcdbas_attr_group = { .attrs = dcdbas_dev_attrs,};static int __devinit dcdbas_probe(struct platform_device *dev){ int i, error; host_control_action = HC_ACTION_NONE; host_control_smi_type = HC_SMITYPE_NONE; /* * BIOS SMI calls require buffer addresses be in 32-bit address space. * This is done by setting the DMA mask below. */ dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK; dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask; error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group); if (error) return error; for (i = 0; dcdbas_bin_attrs[i]; i++) { error = sysfs_create_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]); if (error) { while (--i >= 0) sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]); sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group); return error; } } register_reboot_notifier(&dcdbas_reboot_nb); dev_info(&dev->dev, "%s (version %s)\n", DRIVER_DESCRIPTION, DRIVER_VERSION); return 0;}static int __devexit dcdbas_remove(struct platform_device *dev){ int i; unregister_reboot_notifier(&dcdbas_reboot_nb); for (i = 0; dcdbas_bin_attrs[i]; i++) sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]); sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group); return 0;}static struct platform_driver dcdbas_driver = { .driver = { .name = DRIVER_NAME, .owner = THIS_MODULE, }, .probe = dcdbas_probe, .remove = __devexit_p(dcdbas_remove),};/** * dcdbas_init: initialize driver */static int __init dcdbas_init(void){ int error; error = platform_driver_register(&dcdbas_driver); if (error) return error; dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1); if (!dcdbas_pdev) { error = -ENOMEM; goto err_unregister_driver; } error = platform_device_add(dcdbas_pdev); if (error) goto err_free_device; return 0; err_free_device: platform_device_put(dcdbas_pdev); err_unregister_driver: platform_driver_unregister(&dcdbas_driver); return error;}/** * dcdbas_exit: perform driver cleanup */static void __exit dcdbas_exit(void){ /* * make sure functions that use dcdbas_pdev are called * before platform_device_unregister */ unregister_reboot_notifier(&dcdbas_reboot_nb); smi_data_buf_free(); platform_device_unregister(dcdbas_pdev); platform_driver_unregister(&dcdbas_driver); /* * We have to free the buffer here instead of dcdbas_remove * because only in module exit function we can be sure that * all sysfs attributes belonging to this module have been * released. */ smi_data_buf_free();}module_init(dcdbas_init);module_exit(dcdbas_exit);MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");MODULE_VERSION(DRIVER_VERSION);MODULE_AUTHOR("Dell Inc.");MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?