コード例 #1
0
ファイル: main.c プロジェクト: pratikmankawde/HelenOS_Nano
static int amdm37x_dispc_dev_add(ddf_dev_t *dev)
{
	assert(dev);
	/* Visualizer part */
	ddf_fun_t *fun = ddf_fun_create(dev, fun_exposed, "viz");
	if (!fun) {
		ddf_log_error("Failed to create visualizer function\n");
		return ENOMEM;
	}

	visualizer_t *vis = ddf_fun_data_alloc(fun, sizeof(visualizer_t));
	if (!vis) {
		ddf_log_error("Failed to allocate visualizer structure\n");
		ddf_fun_destroy(fun);
		return ENOMEM;
	}

	graph_init_visualizer(vis);
	vis->reg_svc_handle = ddf_fun_get_handle(fun);

	ddf_fun_set_ops(fun, &graph_fun_ops);
	/* Hw part */
	amdm37x_dispc_t *dispc =
	    ddf_dev_data_alloc(dev, sizeof(amdm37x_dispc_t));
	if (!dispc) {
		ddf_log_error("Failed to allocate dispc structure\n");
		ddf_fun_destroy(fun);
		return ENOMEM;
	}

	int ret = amdm37x_dispc_init(dispc, vis);
	if (ret != EOK) {
		ddf_log_error("Failed to init dispc: %s\n", str_error(ret));
		ddf_fun_destroy(fun);
		return ret;
	}

	/* Report to devman */
	ret = ddf_fun_bind(fun);
	if (ret != EOK) {
		ddf_log_error("Failed to bind function: %s\n", str_error(ret));
		amdm37x_dispc_fini(dispc);
		ddf_fun_destroy(fun);
		return ret;
	}
	ddf_fun_add_to_category(fun, "visualizer");

	ddf_log_note("Added device `%s'\n", ddf_dev_get_name(dev));
	return EOK;
}
コード例 #2
0
ファイル: main.c プロジェクト: jvesely/helenos
/** Initialize new SB16 driver instance.
 *
 * @param[in] device DDF instance of the device to initialize.
 * @return Error code.
 */
static int sb_add_device(ddf_dev_t *device)
{
	bool handler_regd = false;
	const size_t irq_cmd_count = sb16_irq_code_size();
	irq_cmd_t irq_cmds[irq_cmd_count];
	irq_pio_range_t irq_ranges[1];

	sb16_t *soft_state = ddf_dev_data_alloc(device, sizeof(sb16_t));
	int rc = soft_state ? EOK : ENOMEM;
	if (rc != EOK) {
		ddf_log_error("Failed to allocate sb16 structure.");
		goto error;
	}

	addr_range_t sb_regs;
	addr_range_t *p_sb_regs = &sb_regs;
	addr_range_t mpu_regs;
	addr_range_t *p_mpu_regs = &mpu_regs;
	int irq = 0, dma8 = 0, dma16 = 0;

	rc = sb_get_res(device, &p_sb_regs, &p_mpu_regs, &irq, &dma8, &dma16);
	if (rc != EOK) {
		ddf_log_error("Failed to get resources: %s.", str_error(rc));
		goto error;
	}

	sb16_irq_code(p_sb_regs, dma8, dma16, irq_cmds, irq_ranges);

	irq_code_t irq_code = {
		.cmdcount = irq_cmd_count,
		.cmds = irq_cmds,
		.rangecount = 1,
		.ranges = irq_ranges
	};

	rc = register_interrupt_handler(device, irq, irq_handler, &irq_code);
	if (rc != EOK) {
		ddf_log_error("Failed to register irq handler: %s.",
		    str_error(rc));
		goto error;
	}

	handler_regd = true;

	rc = sb_enable_interrupts(device);
	if (rc != EOK) {
		ddf_log_error("Failed to enable interrupts: %s.",
		    str_error(rc));
		goto error;
	}

	rc = sb16_init_sb16(soft_state, p_sb_regs, device, dma8, dma16);
	if (rc != EOK) {
		ddf_log_error("Failed to init sb16 driver: %s.",
		    str_error(rc));
		goto error;
	}

	rc = sb16_init_mpu(soft_state, p_mpu_regs);
	if (rc == EOK) {
		ddf_fun_t *mpu_fun =
		    ddf_fun_create(device, fun_exposed, "midi");
		if (mpu_fun) {
			rc = ddf_fun_bind(mpu_fun);
			if (rc != EOK)
				ddf_log_error(
				    "Failed to bind midi function: %s.",
				    str_error(rc));
		} else {
			ddf_log_error("Failed to create midi function.");
		}
	} else {
		ddf_log_warning("Failed to init mpu driver: %s.",
		    str_error(rc));
	}

	/* MPU state does not matter */
	return EOK;
error:
	if (handler_regd)
		unregister_interrupt_handler(device, irq);
	return rc;
}

static int sb_get_res(ddf_dev_t *device, addr_range_t **pp_sb_regs,
    addr_range_t **pp_mpu_regs, int *irq, int *dma8, int *dma16)
{
	assert(device);

	async_sess_t *parent_sess = devman_parent_device_connect(
	    ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
	if (!parent_sess)
		return ENOMEM;

	hw_res_list_parsed_t hw_res;
	hw_res_list_parsed_init(&hw_res);
	const int ret = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
	async_hangup(parent_sess);
	if (ret != EOK) {
		return ret;
	}

	/* 1x IRQ, 1-2x DMA(8,16), 1-2x IO (MPU is separate). */
	if (hw_res.irqs.count != 1 ||
	   (hw_res.io_ranges.count != 1 && hw_res.io_ranges.count != 2) ||
	   (hw_res.dma_channels.count != 1 && hw_res.dma_channels.count != 2)) {
		hw_res_list_parsed_clean(&hw_res);
		return EINVAL;
	}

	if (irq)
		*irq = hw_res.irqs.irqs[0];

	if (dma8) {
		if (hw_res.dma_channels.channels[0] < 4) {
			*dma8 = hw_res.dma_channels.channels[0];
		} else {
			if (hw_res.dma_channels.count == 2 &&
			    hw_res.dma_channels.channels[1] < 4) {
				*dma8 = hw_res.dma_channels.channels[1];
			}
		}
	}

	if (dma16) {
		if (hw_res.dma_channels.channels[0] > 4) {
			*dma16 = hw_res.dma_channels.channels[0];
		} else {
			if (hw_res.dma_channels.count == 2 &&
			    hw_res.dma_channels.channels[1] > 4) {
				*dma16 = hw_res.dma_channels.channels[1];
			}
		}
	}

	if (hw_res.io_ranges.count == 1) {
		if (pp_sb_regs && *pp_sb_regs)
			**pp_sb_regs = hw_res.io_ranges.ranges[0];
		if (pp_mpu_regs)
			*pp_mpu_regs = NULL;
	} else {
		const int sb =
		    (hw_res.io_ranges.ranges[0].size >= sizeof(sb16_regs_t))
		        ? 0 : 1;
		const int mpu = 1 - sb;
		if (pp_sb_regs && *pp_sb_regs)
			**pp_sb_regs = hw_res.io_ranges.ranges[sb];
		if (pp_mpu_regs && *pp_mpu_regs)
			**pp_mpu_regs = hw_res.io_ranges.ranges[mpu];
	}

	return EOK;
}