+static void register_buttons(struct button_t *b)
+{
+ for (; b->name; b++)
+ platform.button_mask |= b->gpio;
+
+ platform.button_mask &= ~gpiomask;
+
+ gpio_outen(platform.button_mask, 0);
+ gpio_control(platform.button_mask, 0);
+ platform.button_polarity = gpio_in() & platform.button_mask;
+ gpio_intpolarity(platform.button_mask, platform.button_polarity);
+ gpio_setintmask(platform.button_mask, platform.button_mask);
+
+ gpio_set_irqenable(1, button_handler);
+}
+
+static void unregister_buttons(struct button_t *b)
+{
+ gpio_setintmask(platform.button_mask, 0);
+
+ gpio_set_irqenable(0, button_handler);
+}
+
+
+#ifndef LINUX_2_4
+static void add_msg(struct event_t *event, char *msg, int argv)
+{
+ char *s;
+
+ if (argv)
+ return;
+
+ s = skb_put(event->skb, strlen(msg) + 1);
+ strcpy(s, msg);
+}
+
+static void hotplug_button(struct work_struct *work)
+{
+ struct event_t *event = container_of(work, struct event_t, wq);
+ char *s;
+
+ if (!uevent_sock)
+ return;
+
+ event->skb = alloc_skb(2048, GFP_KERNEL);
+
+ s = skb_put(event->skb, strlen(event->action) + 2);
+ sprintf(s, "%s@", event->action);
+ fill_event(event);
+
+ NETLINK_CB(event->skb).dst_group = 1;
+ netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
+
+ kfree(event);
+}
+
+#else /* !LINUX_2_4 */
+static inline char *kzalloc(unsigned int size, unsigned int gfp)
+{
+ char *p;
+
+ p = kmalloc(size, gfp);
+ if (p == NULL)
+ return NULL;
+
+ memset(p, 0, size);
+
+ return p;
+}
+
+static void add_msg(struct event_t *event, char *msg, int argv)
+{
+ if (argv)
+ event->argv[event->anr++] = event->scratch;
+ else
+ event->envp[event->enr++] = event->scratch;
+
+ event->scratch += sprintf(event->scratch, "%s", msg) + 1;
+}
+
+static void hotplug_button(struct event_t *event)
+{
+ char *scratch = kzalloc(256, GFP_KERNEL);
+ event->scratch = scratch;
+
+ add_msg(event, hotplug_path, 1);
+ add_msg(event, "button", 1);
+ fill_event(event);
+ call_usermodehelper (event->argv[0], event->argv, event->envp);
+ kfree(scratch);
+ kfree(event);
+}
+#endif /* !LINUX_2_4 */
+
+static int fill_event (struct event_t *event)
+{
+ static char buf[128];
+
+ add_msg(event, "HOME=/", 0);
+ add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
+ add_msg(event, "SUBSYSTEM=button", 0);
+ snprintf(buf, 128, "ACTION=%s", event->action);
+ add_msg(event, buf, 0);
+ snprintf(buf, 128, "BUTTON=%s", event->name);
+ add_msg(event, buf, 0);
+ snprintf(buf, 128, "SEEN=%ld", event->seen);
+ add_msg(event, buf, 0);
+#ifndef LINUX_2_4
+ snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
+ add_msg(event, buf, 0);
+#endif
+
+ return 0;
+}
+
+
+#ifndef LINUX_2_4
+static irqreturn_t button_handler(int irq, void *dev_id)
+#else
+static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
+#endif
+{
+ struct button_t *b;
+ u32 in, changed;
+
+ in = gpio_in() & platform.button_mask;
+ gpio_intpolarity(platform.button_mask, in);
+ changed = platform.button_polarity ^ in;
+ platform.button_polarity = in;
+
+ changed &= ~gpio_outen(0, 0);
+
+ for (b = platform.buttons; b->name; b++) {
+ struct event_t *event;
+
+ if (!(b->gpio & changed)) continue;
+
+ b->pressed ^= 1;
+
+ if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
+ event->seen = (jiffies - b->seen)/HZ;
+ event->name = b->name;
+ event->action = b->pressed ? "pressed" : "released";
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
+ INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
+#else
+ INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
+#endif
+ schedule_work(&event->wq);
+ }
+
+ b->seen = jiffies;
+ }
+ return IRQ_HANDLED;
+}
+
+static void register_leds(struct led_t *l)
+{
+ struct proc_dir_entry *p;
+ u32 mask = 0;
+ u32 oe_mask = 0;
+ u32 val = 0;
+
+ leds = proc_mkdir("led", diag);
+ if (!leds)
+ return;
+
+ for(; l->name; l++) {
+ if (l->gpio & gpiomask)
+ continue;
+
+ if (l->gpio & GPIO_TYPE_EXTIF) {
+ l->state = 0;
+ set_led_extif(l);
+ } else {
+ if (l->polarity != INPUT) oe_mask |= l->gpio;
+ mask |= l->gpio;
+ val |= (l->polarity == NORMAL)?0:l->gpio;
+ }
+
+ if (l->polarity == INPUT) continue;
+
+ if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
+ l->proc.type = PROC_LED;
+ l->proc.ptr = l;
+ p->data = (void *) &l->proc;
+ p->proc_fops = &diag_proc_fops;
+ }
+ }
+
+ gpio_outen(mask, oe_mask);
+ gpio_control(mask, 0);
+ gpio_out(mask, val);
+ gpio_setintmask(mask, 0);
+}
+
+static void unregister_leds(struct led_t *l)
+{
+ for(; l->name; l++)
+ remove_proc_entry(l->name, leds);
+
+ remove_proc_entry("led", diag);
+}
+
+static void set_led_extif(struct led_t *led)
+{
+ gpio_set_extif(led->gpio, led->state);
+}
+
+static void led_flash(unsigned long dummy) {
+ struct led_t *l;
+ u32 mask = 0;
+ u8 extif_blink = 0;
+
+ for (l = platform.leds; l->name; l++) {
+ if (l->flash) {
+ if (l->gpio & GPIO_TYPE_EXTIF) {
+ extif_blink = 1;
+ l->state = !l->state;
+ set_led_extif(l);
+ } else {
+ mask |= l->gpio;
+ }
+ }
+ }