static int gb_lights_request_handler(struct gb_operation *op) { struct gb_connection *connection = op->connection; struct device *dev = &connection->bundle->dev; struct gb_lights *glights = gb_connection_get_data(connection); struct gb_light *light; struct gb_message *request; struct gb_lights_event_request *payload; int ret = 0; u8 light_id; u8 event; if (op->type != GB_LIGHTS_TYPE_EVENT) { dev_err(dev, "Unsupported unsolicited event: %u\n", op->type); return -EINVAL; } request = op->request; if (request->payload_size < sizeof(*payload)) { dev_err(dev, "Wrong event size received (%zu < %zu)\n", request->payload_size, sizeof(*payload)); return -EINVAL; } payload = request->payload; light_id = payload->light_id; if (light_id >= glights->lights_count || !glights->lights[light_id].ready) { dev_err(dev, "Event received for unconfigured light id: %d\n", light_id); return -EINVAL; } event = payload->event; if (event & GB_LIGHTS_LIGHT_CONFIG) { light = &glights->lights[light_id]; mutex_lock(&glights->lights_lock); gb_lights_light_release(light); ret = gb_lights_light_config(glights, light_id); if (!ret) ret = gb_lights_light_register(light); if (ret < 0) gb_lights_light_release(light); mutex_unlock(&glights->lights_lock); } return ret; }
static int gb_bootrom_firmware_size_request(struct gb_operation *op) { struct gb_bootrom *bootrom = gb_connection_get_data(op->connection); struct gb_bootrom_firmware_size_request *size_request = op->request->payload; struct gb_bootrom_firmware_size_response *size_response; struct device *dev = &op->connection->bundle->dev; int ret; /* Disable timeouts */ gb_bootrom_cancel_timeout(bootrom); if (op->request->payload_size != sizeof(*size_request)) { dev_err(dev, "%s: illegal size of firmware size request (%zu != %zu)\n", __func__, op->request->payload_size, sizeof(*size_request)); ret = -EINVAL; goto queue_work; } mutex_lock(&bootrom->mutex); ret = find_firmware(bootrom, size_request->stage); if (ret) goto unlock; if (!gb_operation_response_alloc(op, sizeof(*size_response), GFP_KERNEL)) { dev_err(dev, "%s: error allocating response\n", __func__); free_firmware(bootrom); ret = -ENOMEM; goto unlock; } size_response = op->response->payload; size_response->size = cpu_to_le32(bootrom->fw->size); dev_dbg(dev, "%s: firmware size %d bytes\n", __func__, size_response->size); unlock: mutex_unlock(&bootrom->mutex); queue_work: if (!ret) { /* Refresh timeout */ gb_bootrom_set_timeout(bootrom, NEXT_REQ_GET_FIRMWARE, NEXT_REQ_TIMEOUT_MS); } return ret; }
static void gb_gpio_connection_exit(struct gb_connection *connection) { struct gb_gpio_controller *ggc = gb_connection_get_data(connection); if (!ggc) return; gb_gpio_irqchip_remove(ggc); gb_gpiochip_remove(&ggc->chip); /* kref_put(ggc->connection) */ kfree(ggc->lines); kfree(ggc); }
static int gb_gpio_request_recv(u8 type, struct gb_operation *op) { struct gb_connection *connection = op->connection; struct device *dev = &connection->bundle->dev; struct gb_gpio_controller *ggc = gb_connection_get_data(connection); struct gb_message *request; struct gb_gpio_irq_event_request *event; int irq; struct irq_desc *desc; if (type != GB_GPIO_TYPE_IRQ_EVENT) { dev_err(dev, "unsupported unsolicited request: %u\n", type); return -EINVAL; } request = op->request; if (request->payload_size < sizeof(*event)) { dev_err(dev, "short event received (%zu < %zu)\n", request->payload_size, sizeof(*event)); return -EINVAL; } event = request->payload; if (event->which > ggc->line_max) { dev_err(dev, "invalid hw irq: %d\n", event->which); return -EINVAL; } irq = irq_find_mapping(ggc->irqdomain, event->which); if (!irq) { dev_err(dev, "failed to find IRQ\n"); return -EINVAL; } desc = irq_to_desc(irq); if (!desc) { dev_err(dev, "failed to look up irq\n"); return -EINVAL; } local_irq_disable(); #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) generic_handle_irq_desc(irq, desc); #else generic_handle_irq_desc(desc); #endif local_irq_enable(); return 0; }
static int gb_firmware_get_firmware(struct gb_operation *op) { struct gb_firmware *firmware = gb_connection_get_data(op->connection); const struct firmware *fw = firmware->fw; struct gb_firmware_get_firmware_request *firmware_request; struct gb_firmware_get_firmware_response *firmware_response; struct device *dev = &op->connection->bundle->dev; unsigned int offset, size; if (op->request->payload_size != sizeof(*firmware_request)) { dev_err(dev, "%s: Illegal size of get firmware request (%zu %zu)\n", __func__, op->request->payload_size, sizeof(*firmware_request)); return -EINVAL; } if (!fw) { dev_err(dev, "%s: firmware not available\n", __func__); return -EINVAL; } firmware_request = op->request->payload; offset = le32_to_cpu(firmware_request->offset); size = le32_to_cpu(firmware_request->size); if (offset >= fw->size || size > fw->size - offset) { dev_warn(dev, "bad firmware request (offs = %u, size = %u)\n", offset, size); return -EINVAL; } if (!gb_operation_response_alloc(op, sizeof(*firmware_response) + size, GFP_KERNEL)) { dev_err(dev, "%s: error allocating response\n", __func__); return -ENOMEM; } firmware_response = op->response->payload; memcpy(firmware_response->data, fw->data + offset, size); dev_dbg(dev, "responding with firmware (offs = %u, size = %u)\n", offset, size); return 0; }
static int gb_bootrom_ready_to_boot(struct gb_operation *op) { struct gb_connection *connection = op->connection; struct gb_bootrom *bootrom = gb_connection_get_data(connection); struct gb_bootrom_ready_to_boot_request *rtb_request; struct device *dev = &connection->bundle->dev; u8 status; int ret = 0; /* Disable timeouts */ gb_bootrom_cancel_timeout(bootrom); if (op->request->payload_size != sizeof(*rtb_request)) { dev_err(dev, "%s: Illegal size of ready to boot request (%zu %zu)\n", __func__, op->request->payload_size, sizeof(*rtb_request)); ret = -EINVAL; goto queue_work; } rtb_request = op->request->payload; status = rtb_request->status; /* Return error if the blob was invalid */ if (status == GB_BOOTROM_BOOT_STATUS_INVALID) { ret = -EINVAL; goto queue_work; } /* * XXX Should we return error for insecure firmware? */ dev_dbg(dev, "ready to boot: 0x%x, 0\n", status); queue_work: /* * Refresh timeout, the Interface shall load the new personality and * send a new hotplug request, which shall get rid of the bootrom * connection. As that can take some time, increase the timeout a bit. */ gb_bootrom_set_timeout(bootrom, NEXT_REQ_MODE_SWITCH, MODE_SWITCH_TIMEOUT_MS); return ret; }
static int gb_firmware_size_request(struct gb_operation *op) { struct gb_firmware *firmware = gb_connection_get_data(op->connection); struct gb_firmware_size_request *size_request = op->request->payload; struct gb_firmware_size_response *size_response; struct device *dev = &op->connection->bundle->dev; int ret; if (op->request->payload_size != sizeof(*size_request)) { dev_err(dev, "%s: illegal size of firmware size request (%zu != %zu)\n", __func__, op->request->payload_size, sizeof(*size_request)); return -EINVAL; } ret = download_firmware(firmware, size_request->stage); if (ret) { dev_err(dev, "%s: failed to download firmware (%d)\n", __func__, ret); return ret; } if (!gb_operation_response_alloc(op, sizeof(*size_response), GFP_KERNEL)) { dev_err(dev, "%s: error allocating response\n", __func__); free_firmware(firmware); return -ENOMEM; } size_response = op->response->payload; size_response->size = cpu_to_le32(firmware->fw->size); dev_dbg(dev, "%s: firmware size %d bytes\n", __func__, size_response->size); return 0; }
static int gb_bootrom_get_firmware(struct gb_operation *op) { struct gb_bootrom *bootrom = gb_connection_get_data(op->connection); const struct firmware *fw; struct gb_bootrom_get_firmware_request *firmware_request; struct gb_bootrom_get_firmware_response *firmware_response; struct device *dev = &op->connection->bundle->dev; unsigned int offset, size; enum next_request_type next_request; int ret = 0; /* Disable timeouts */ gb_bootrom_cancel_timeout(bootrom); if (op->request->payload_size != sizeof(*firmware_request)) { dev_err(dev, "%s: Illegal size of get firmware request (%zu %zu)\n", __func__, op->request->payload_size, sizeof(*firmware_request)); ret = -EINVAL; goto queue_work; } mutex_lock(&bootrom->mutex); fw = bootrom->fw; if (!fw) { dev_err(dev, "%s: firmware not available\n", __func__); ret = -EINVAL; goto unlock; } firmware_request = op->request->payload; offset = le32_to_cpu(firmware_request->offset); size = le32_to_cpu(firmware_request->size); if (offset >= fw->size || size > fw->size - offset) { dev_warn(dev, "bad firmware request (offs = %u, size = %u)\n", offset, size); ret = -EINVAL; goto unlock; } if (!gb_operation_response_alloc(op, sizeof(*firmware_response) + size, GFP_KERNEL)) { dev_err(dev, "%s: error allocating response\n", __func__); ret = -ENOMEM; goto unlock; } firmware_response = op->response->payload; memcpy(firmware_response->data, fw->data + offset, size); dev_dbg(dev, "responding with firmware (offs = %u, size = %u)\n", offset, size); unlock: mutex_unlock(&bootrom->mutex); queue_work: /* Refresh timeout */ if (!ret && (offset + size == fw->size)) next_request = NEXT_REQ_READY_TO_BOOT; else next_request = NEXT_REQ_GET_FIRMWARE; gb_bootrom_set_timeout(bootrom, next_request, NEXT_REQ_TIMEOUT_MS); return ret; }