Skip to content

Commit

Permalink
qdev: allow multiple qdev_init_gpio_in() calls
Browse files Browse the repository at this point in the history
Allow multiple qdev_init_gpio_in() calls for the one device. The first call will
define GPIOs 0-N-1, the next GPIOs N- ... . Allows different GPIOs to be handled
with different handlers. Needed when two levels of the QOM class heirachy both
define GPIO functionality, as a single GPIO handler with an index selecter is
not possible.

Signed-off-by: Peter A. G. Crosthwaite <peter.crosthwaite@petalogix.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
  • Loading branch information
pete128 committed Oct 10, 2012
1 parent 74687e4 commit 1e5b31e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
27 changes: 20 additions & 7 deletions hw/irq.c
Expand Up @@ -38,24 +38,37 @@ void qemu_set_irq(qemu_irq irq, int level)
irq->handler(irq->opaque, irq->n, level);
}

qemu_irq *qemu_allocate_irqs(qemu_irq_handler handler, void *opaque, int n)
qemu_irq *qemu_extend_irqs(qemu_irq *old, int n_old, qemu_irq_handler handler,
void *opaque, int n)
{
qemu_irq *s;
struct IRQState *p;
int i;

s = (qemu_irq *)g_malloc0(sizeof(qemu_irq) * n);
p = (struct IRQState *)g_malloc0(sizeof(struct IRQState) * n);
for (i = 0; i < n; i++) {
p->handler = handler;
p->opaque = opaque;
p->n = i;
if (!old) {
n_old = 0;
}
s = old ? g_renew(qemu_irq, old, n + n_old) : g_new(qemu_irq, n);
p = old ? g_renew(struct IRQState, s[0], n + n_old) :
g_new(struct IRQState, n);
for (i = 0; i < n + n_old; i++) {
if (i >= n_old) {
p->handler = handler;
p->opaque = opaque;
p->n = i;
}
s[i] = p;
p++;
}
return s;
}

qemu_irq *qemu_allocate_irqs(qemu_irq_handler handler, void *opaque, int n)
{
return qemu_extend_irqs(NULL, 0, handler, opaque, n);
}


void qemu_free_irqs(qemu_irq *s)
{
g_free(s[0]);
Expand Down
11 changes: 10 additions & 1 deletion hw/irq.h
Expand Up @@ -23,8 +23,17 @@ static inline void qemu_irq_pulse(qemu_irq irq)
qemu_set_irq(irq, 0);
}

/* Returns an array of N IRQs. */
/* Returns an array of N IRQs. Each IRQ is assigned the argument handler and
* opaque data.
*/
qemu_irq *qemu_allocate_irqs(qemu_irq_handler handler, void *opaque, int n);

/* Extends an Array of IRQs. Old IRQs have their handlers and opaque data
* preserved. New IRQs are assigned the argument handler and opaque data.
*/
qemu_irq *qemu_extend_irqs(qemu_irq *old, int n_old, qemu_irq_handler handler,
void *opaque, int n);

void qemu_free_irqs(qemu_irq *s);

/* Returns a new IRQ with opposite polarity. */
Expand Down
6 changes: 3 additions & 3 deletions hw/qdev.c
Expand Up @@ -285,9 +285,9 @@ BusState *qdev_get_parent_bus(DeviceState *dev)

void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
{
assert(dev->num_gpio_in == 0);
dev->num_gpio_in = n;
dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
dev->gpio_in = qemu_extend_irqs(dev->gpio_in, dev->num_gpio_in, handler,
dev, n);
dev->num_gpio_in += n;
}

void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
Expand Down

0 comments on commit 1e5b31e

Please sign in to comment.