一台机器上的/dev/spidev2.0 可以相应另外一台机器上spi master发出的命令,从而可以控制slave的机器来进行关机重启等动作。
具体源代码在drivers/spi/spi-slave-system-control.c中
目前支持的命令有CMD_REBOOT/CMD_POWEROFF/CMD_HALT/CMD_SUSPEND 这四个命令
static void spi_slave_system_control_complete(void *arg)
{
struct spi_slave_system_control_priv *priv = arg;
u16 cmd;
int ret;
if (priv->msg.status)
goto terminate;
cmd = be16_to_cpu(priv->cmd);
switch (cmd) {
case CMD_REBOOT:
dev_info(&priv->spi->dev, "Rebooting system...\n");
kernel_restart(NULL);
break;
case CMD_POWEROFF:
dev_info(&priv->spi->dev, "Powering off system...\n");
kernel_power_off();
break;
case CMD_HALT:
dev_info(&priv->spi->dev, "Halting system...\n");
kernel_halt();
break;
case CMD_SUSPEND:
dev_info(&priv->spi->dev, "Suspending system...\n");
pm_suspend(PM_SUSPEND_MEM);
break;
default:
dev_warn(&priv->spi->dev, "Unknown command 0x%x\n", cmd);
break;
}
ret = spi_slave_system_control_submit(priv);
if (ret)
goto terminate;
return;
terminate:
dev_info(&priv->spi->dev, "Terminating\n");
complete(&priv->finished);
}
spi_slave_system_control_submit 中有对complete 赋值
static
int spi_slave_system_control_submit(struct spi_slave_system_control_priv *priv)
{
int ret;
spi_message_init_with_transfers(&priv->msg, &priv->xfer, 1);
priv->msg.complete = spi_slave_system_control_complete;
priv->msg.context = priv;
ret = spi_async(priv->spi, &priv->msg);
if (ret)
dev_err(&priv->spi->dev, "spi_async() failed %d\n", ret);
return ret;
}
这里的complete会在driver/spi/spi.c 中的spi_finalize_current_message中调用,其会检查complete是否位null,如果不会null则调用
void spi_finalize_current_message(struct spi_controller *ctlr)
{
mesg->state = NULL;
if (mesg->complete)
mesg->complete(mesg->context);
}