add buffalo whr2-a54g54 information to diag
[openwrt/svn-archive/openwrt.git] / openwrt / target / linux / package / diag / src / diag.c
1 /*
2 * diag.c - GPIO interface driver for Broadcom boards
3 *
4 * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
5 * Felix Fietkau <nbd@openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *
21 * $Id:$
22 */
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/kmod.h>
26 #include <linux/proc_fs.h>
27 #include <linux/moduleparam.h>
28 #include <linux/tqueue.h>
29 #include <linux/timer.h>
30 #include <asm/uaccess.h>
31
32 #include <osl.h>
33 #include <bcmdevs.h>
34 #include <sbutils.h>
35 #include <sbconfig.h>
36 #include <sbchipc.h>
37
38 #define MODULE_NAME "diag"
39
40 #define MAX_GPIO 8
41 #define FLASH_TIME HZ/6
42
43 static unsigned int gpiomask = 0;
44 module_param(gpiomask, int, 0644);
45
46 enum polarity_t {
47 REVERSE = 0,
48 NORMAL = 1,
49 };
50
51 enum {
52 PROC_BUTTON,
53 PROC_LED,
54 PROC_MODEL,
55 PROC_GPIOMASK
56 };
57
58 struct prochandler_t {
59 int type;
60 void *ptr;
61 };
62
63 struct button_t {
64 struct prochandler_t proc;
65 char *name;
66 u16 gpio;
67 u8 polarity;
68 u8 pressed;
69 unsigned long seen;
70 };
71
72 struct led_t {
73 struct prochandler_t proc;
74 char *name;
75 u16 gpio;
76 u8 polarity;
77 u8 flash;
78 };
79
80 struct platform_t {
81 char *name;
82 struct button_t buttons[MAX_GPIO];
83 struct led_t leds[MAX_GPIO];
84 };
85
86 enum {
87 /* Linksys */
88 WAP54GV1,
89 WAP54GV3,
90 WRT54GV1,
91 WRT54G,
92 WRTSL54GS,
93 WRT54G3G,
94
95 /* ASUS */
96 WLHDD,
97 WL300G,
98 WL500G,
99 WL500GD,
100 WL500GP,
101 ASUS_4702,
102
103 /* Buffalo */
104 WBR2_G54,
105 WHR_G54S,
106 WHR_HP_G54,
107 WHR2_A54G54,
108 WLA2_G54L,
109 BUFFALO_UNKNOWN,
110 BUFFALO_UNKNOWN_4710,
111
112 /* Siemens */
113 SE505V1,
114 SE505V2,
115
116 /* US Robotics */
117 USR5461,
118
119 /* Dell */
120 TM2300,
121
122 /* Motorola */
123 WR850GV1,
124 WR850GV2,
125
126 /* Belkin */
127 BELKIN_UNKNOWN,
128 };
129
130 static struct platform_t __init platforms[] = {
131 /* Linksys */
132 [WAP54GV1] = {
133 .name = "Linksys WAP54G V1",
134 .buttons = {
135 { .name = "reset", .gpio = 1 << 0 },
136 },
137 .leds = {
138 { .name = "diag", .gpio = 1 << 3 },
139 { .name = "wlan", .gpio = 1 << 4 },
140 },
141 },
142 [WAP54GV3] = {
143 .name = "Linksys WAP54G V3",
144 .buttons = {
145 /* FIXME: verify this */
146 { .name = "reset", .gpio = 1 << 7 },
147 { .name = "ses", .gpio = 1 << 0 },
148 },
149 .leds = {
150 /* FIXME: diag? */
151 { .name = "ses", .gpio = 1 << 1 },
152 },
153 },
154 [WRT54GV1] = {
155 .name = "Linksys WRT54G V1.x",
156 .buttons = {
157 { .name = "reset", .gpio = 1 << 6 },
158 },
159 .leds = {
160 /* FIXME */
161 { .name = "diag", .gpio = 1 << 1 },
162 { .name = "dmz", .gpio = 1 << 7 },
163 { .name = "wlan", .gpio = 1 << 0 },
164 },
165 },
166 [WRT54G] = {
167 .name = "Linksys WRT54G*",
168 .buttons = {
169 { .name = "reset", .gpio = 1 << 6 },
170 { .name = "ses", .gpio = 1 << 4 },
171 },
172 .leds = {
173 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
174 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
175 { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
176 { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
177 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
178 },
179 },
180 [WRTSL54GS] = {
181 .name = "Linksys WRTSL54GS",
182 .buttons = {
183 { .name = "reset", .gpio = 1 << 6 },
184 { .name = "ses", .gpio = 1 << 4 },
185 },
186 .leds = {
187 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
188 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
189 { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
190 { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
191 },
192 },
193 [WRT54G3G] = {
194 .name = "Linksys WRT54G3G",
195 .buttons = {
196 { .name = "reset", .gpio = 1 << 6 },
197 { .name = "3g", .gpio = 1 << 4 },
198 },
199 .leds = {
200 { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
201 { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
202 { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
203 { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
204 { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
205 },
206 },
207 /* Asus */
208 [WLHDD] = {
209 .name = "ASUS WL-HDD",
210 .buttons = {
211 { .name = "reset", .gpio = 1 << 6 },
212 },
213 .leds = {
214 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
215 },
216 },
217 [WL300G] = {
218 .name = "ASUS WL-500g",
219 .buttons = {
220 { .name = "reset", .gpio = 1 << 6 },
221 },
222 .leds = {
223 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
224 },
225 },
226 [WL500G] = {
227 .name = "ASUS WL-500g",
228 .buttons = {
229 { .name = "reset", .gpio = 1 << 6 },
230 },
231 .leds = {
232 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
233 },
234 },
235 [WL500GD] = {
236 .name = "ASUS WL-500g Deluxe",
237 .buttons = {
238 { .name = "reset", .gpio = 1 << 6 },
239 },
240 .leds = {
241 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
242 },
243 },
244 [WL500GP] = {
245 .name = "ASUS WL-500g Premium",
246 .buttons = {
247 { .name = "reset", .gpio = 1 << 0 },
248 },
249 .leds = {
250 { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
251 { .name = "ses", .gpio = 1 << 4, .polarity = REVERSE },
252 },
253 },
254 [ASUS_4702] = {
255 .name = "ASUS (unknown, BCM4702)",
256 .buttons = {
257 { .name = "reset", .gpio = 1 << 6 },
258 },
259 .leds = {
260 { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
261 },
262 },
263 /* Buffalo */
264 [WHR_G54S] = {
265 .name = "Buffalo WHR-G54S",
266 .buttons = {
267 { .name = "reset", .gpio = 1 << 4 },
268 { .name = "ses", .gpio = 1 << 0 },
269 },
270 .leds = {
271 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
272 { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
273 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
274 },
275 },
276 [WBR2_G54] = {
277 .name = "Buffalo WBR2-G54",
278 /* FIXME: verify */
279 .buttons = {
280 { .name = "reset", .gpio = 1 << 7 },
281 },
282 .leds = {
283 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
284 },
285 },
286 [WHR_HP_G54] = {
287 .name = "Buffalo WHR-HP-G54",
288 .buttons = {
289 { .name = "reset", .gpio = 1 << 4 },
290 { .name = "bridge", .gpio = 1 << 5 },
291 { .name = "ses", .gpio = 1 << 0 },
292 },
293 .leds = {
294 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
295 { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
296 { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
297 },
298 },
299 [WHR2_A54G54] = {
300 .name = "Buffalo WHR2-A54G54",
301 .buttons = {
302 { .name = "reset", .gpio = 1 << 4 },
303 },
304 .leds = {
305 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
306 },
307 },
308 [WLA2_G54L] = {
309 .name = "Buffalo WLA2-G54L",
310 /* FIXME: verify */
311 .buttons = {
312 { .name = "reset", .gpio = 1 << 7 },
313 },
314 .leds = {
315 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
316 },
317 },
318 [BUFFALO_UNKNOWN] = {
319 .name = "Buffalo (unknown)",
320 .buttons = {
321 { .name = "reset", .gpio = 1 << 7 },
322 },
323 .leds = {
324 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
325 },
326 },
327 [BUFFALO_UNKNOWN_4710] = {
328 .name = "Buffalo (unknown, BCM4710)",
329 .buttons = {
330 { .name = "reset", .gpio = 1 << 4 },
331 },
332 .leds = {
333 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
334 },
335 },
336 /* Siemens */
337 [SE505V1] = {
338 .name = "Siemens SE505 V1",
339 .buttons = {
340 /* No usable buttons */
341 },
342 .leds = {
343 { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE },
344 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
345 },
346 },
347 [SE505V2] = {
348 .name = "Siemens SE505 V2",
349 .buttons = {
350 /* No usable buttons */
351 },
352 .leds = {
353 { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
354 { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
355 { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
356 },
357 },
358 /* US Robotics */
359 [USR5461] = {
360 .name = "U.S. Robotics USR5461",
361 .buttons = {
362 /* No usable buttons */
363 },
364 .leds = {
365 { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
366 { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
367 },
368 },
369 /* Dell */
370 [TM2300] = {
371 .name = "Dell TrueMobile 2300",
372 .buttons = {
373 { .name = "reset", .gpio = 1 << 0 },
374 },
375 .leds = {
376 { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
377 },
378 },
379 /* Motorola */
380 [WR850GV1] = {
381 .name = "Motorola WR850G V1",
382 .buttons = {
383 { .name = "reset", .gpio = 1 << 0 },
384 },
385 .leds = {
386 { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
387 { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
388 { .name = "modem", .gpio = 1 << 6, .polarity = NORMAL },
389 { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
390 { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
391 },
392 },
393 [WR850GV2] = {
394 .name = "Motorola WR850G V2",
395 .buttons = {
396 { .name = "reset", .gpio = 1 << 5 },
397 },
398 .leds = {
399 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
400 { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
401 { .name = "modem_green",.gpio = 1 << 6, .polarity = REVERSE },
402 { .name = "modem_red", .gpio = 1 << 7, .polarity = REVERSE },
403 },
404 },
405 /* Belkin */
406 [BELKIN_UNKNOWN] = {
407 .name = "Belkin (unknown)",
408 /* FIXME: verify & add detection */
409 .buttons = {
410 { .name = "reset", .gpio = 1 << 7 },
411 },
412 .leds = {
413 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
414 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
415 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
416 },
417 },
418 };
419
420 static struct proc_dir_entry *diag, *leds;
421
422 extern void *bcm947xx_sbh;
423 #define sbh bcm947xx_sbh
424
425 static int sb_irq(void *sbh);
426 static struct platform_t platform;
427
428 extern char *nvram_get(char *str);
429
430 static void led_flash(unsigned long dummy);
431
432 static inline char __init *getvar(char *str)
433 {
434 return nvram_get(str)?:"";
435 }
436
437 static struct platform_t __init *platform_detect(void)
438 {
439 char *boardnum, *boardtype, *buf;
440
441 boardnum = getvar("boardnum");
442 boardtype = getvar("boardtype");
443 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
444 /* CFE based - newer hardware */
445 if (!strcmp(boardnum, "42")) { /* Linksys */
446 if (!strcmp(boardtype, "0x0101"))
447 return &platforms[WRT54G3G];
448
449 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
450 return &platforms[WRTSL54GS];
451
452 /* default to WRT54G */
453 return &platforms[WRT54G];
454 }
455
456 if (!strcmp(boardnum, "45")) { /* ASUS */
457 if (!strcmp(boardtype,"0x042f"))
458 return &platforms[WL500GP];
459 else
460 return &platforms[WL500GD];
461 }
462
463 if (!strcmp(boardnum, "10496"))
464 return &platforms[USR5461];
465 } else { /* PMON based - old stuff */
466 if (!strncmp(boardtype, "bcm94710dev", 11)) {
467 if (!strcmp(boardtype, "42"))
468 return &platforms[WRT54GV1];
469 if (simple_strtoul(boardnum, NULL, 9) == 2)
470 return &platforms[WAP54GV1];
471 }
472 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
473 return &platforms[WL500G];
474 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
475 /* Either WL-300g or WL-HDD, do more extensive checks */
476 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
477 (simple_strtoul(getvar("et1phyaddr"), NULL, 9) == 1))
478 return &platforms[WLHDD];
479 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
480 (simple_strtoul(getvar("et1phyaddr"), NULL, 9) == 10))
481 return &platforms[WL300G];
482 }
483
484 /* unknown asus stuff, probably bcm4702 */
485 if (!strncmp(boardnum, "asusX", 5))
486 return &platforms[ASUS_4702];
487
488 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
489 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30))
490 return &platforms[WR850GV1];
491 }
492
493 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
494 /* Buffalo hardware, check id for specific hardware matches */
495 if (!strcmp(buf, "29bb0332"))
496 return &platforms[WBR2_G54];
497 if (!strcmp(buf, "29129"))
498 return &platforms[WLA2_G54L];
499 if (!strcmp(buf, "30189"))
500 return &platforms[WHR_HP_G54];
501 if (!strcmp(buf, "30182"))
502 return &platforms[WHR_G54S];
503 if (!strcmp(buf, "290441dd"))
504 return &platforms[WHR2_A54G54];
505 }
506
507 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
508 if (!strncmp(boardtype, "bcm94710ap", 10))
509 return &platforms[BUFFALO_UNKNOWN_4710];
510 else
511 return &platforms[BUFFALO_UNKNOWN];
512 }
513
514 if (!strcmp(getvar("CFEver"), "MotoWRv203"))
515 return &platforms[WR850GV2];
516
517 /* not found */
518 return NULL;
519 }
520
521 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos);
522 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, void *data);
523 static struct file_operations diag_proc_fops = {
524 read: diag_proc_read,
525 write: diag_proc_write
526 };
527
528
529
530 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
531 {
532 #ifdef LINUX_2_4
533 struct inode *inode = file->f_dentry->d_inode;
534 struct proc_dir_entry *dent = inode->u.generic_ip;
535 #else
536 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
537 #endif
538 char *page;
539 int len = 0;
540
541 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
542 return -ENOBUFS;
543
544 if (dent->data != NULL) {
545 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
546 switch (handler->type) {
547 case PROC_LED: {
548 struct led_t * led = (struct led_t *) handler->ptr;
549 if (led->flash) {
550 len = sprintf(page, "f\n");
551 } else {
552 int in = (sb_gpioin(sbh) & led->gpio ? 1 : 0);
553 int p = (led->polarity == NORMAL ? 0 : 1);
554 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
555 }
556 break;
557 }
558 case PROC_MODEL:
559 len = sprintf(page, "%s\n", platform.name);
560 break;
561 case PROC_GPIOMASK:
562 len = sprintf(page, "%d\n", gpiomask);
563 break;
564 }
565 }
566 len += 1;
567
568 if (*ppos < len) {
569 len = min_t(int, len - *ppos, count);
570 if (copy_to_user(buf, (page + *ppos), len)) {
571 kfree(page);
572 return -EFAULT;
573 }
574 *ppos += len;
575 } else {
576 len = 0;
577 }
578
579 return len;
580 }
581
582
583 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, void *data)
584 {
585 #ifdef LINUX_2_4
586 struct inode *inode = file->f_dentry->d_inode;
587 struct proc_dir_entry *dent = inode->u.generic_ip;
588 #else
589 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
590 #endif
591 char *page;
592 int ret = -EINVAL;
593
594 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
595 return -ENOBUFS;
596
597 if (copy_from_user(page, buf, count)) {
598 kfree(page);
599 return -EINVAL;
600 }
601 page[count] = 0;
602
603 if (dent->data != NULL) {
604 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
605 switch (handler->type) {
606 case PROC_LED: {
607 struct led_t *led = (struct led_t *) handler->ptr;
608 int p = (led->polarity == NORMAL ? 0 : 1);
609
610 if (led->gpio & gpiomask)
611 break;
612
613 if (page[0] == 'f') {
614 led->flash = 1;
615 led_flash(0);
616 } else {
617 led->flash = 0;
618 sb_gpioouten(sbh, led->gpio, led->gpio);
619 sb_gpiocontrol(sbh, led->gpio, 0);
620 sb_gpioout(sbh, led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
621 }
622 break;
623 }
624 case PROC_GPIOMASK:
625 gpiomask = simple_strtoul(page, NULL, 16);
626 break;
627 }
628 ret = count;
629 }
630
631 kfree(page);
632 return ret;
633 }
634
635 struct event_t {
636 struct tq_struct tq;
637 char buf[256];
638 char *argv[3];
639 char *envp[6];
640 };
641
642 static void hotplug_button(struct event_t *event)
643 {
644 call_usermodehelper (event->argv[0], event->argv, event->envp);
645 kfree(event);
646 }
647
648 static void button_handler(int irq, void *dev_id, struct pt_regs *regs)
649 {
650 struct button_t *b;
651 int in = sb_gpioin(sbh);
652 struct event_t *event;
653
654 for (b = platform.buttons; b->name; b++) {
655 if (b->gpio & gpiomask)
656 continue;
657
658 if (b->polarity != (in & b->gpio)) {
659
660 b->pressed ^= 1;
661
662 if ((event = (struct event_t *)kmalloc (256, GFP_KERNEL))) {
663 int i;
664 char *scratch = event->buf;
665
666 i = 0;
667 event->argv[i++] = hotplug_path;
668 event->argv[i++] = "button";
669 event->argv[i] = 0;
670
671 i = 0;
672 event->envp[i++] = "HOME=/";
673 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
674 event->envp[i++] = scratch;
675 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
676 event->envp[i++] = scratch;
677 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
678 event->envp[i++] = scratch;
679 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
680 event->envp[i] = 0;
681
682 INIT_TQUEUE(&event->tq, (void *)(void *)hotplug_button, (void *)event);
683 schedule_task(&event->tq);
684 }
685
686 b->seen = jiffies;
687 b->polarity ^= b->gpio;
688 sb_gpiointpolarity(sbh, b->gpio, b->polarity);
689 }
690 }
691 }
692
693 static struct timer_list led_timer = {
694 function: &led_flash
695 };
696
697 static void led_flash(unsigned long dummy) {
698 struct led_t *l;
699 unsigned mask = 0;
700
701 for (l = platform.leds; l->name; l++) {
702 if (l->flash)
703 mask |= l->gpio;
704 }
705
706 mask &= ~gpiomask;
707
708 if (mask) {
709 unsigned val;
710
711 val = ~sb_gpioin(sbh);
712 val &= mask;
713
714 sb_gpioouten(sbh, mask, mask);
715 sb_gpiocontrol(sbh, mask, 0);
716 sb_gpioout(sbh, mask, val);
717
718 mod_timer(&led_timer, jiffies + FLASH_TIME);
719 }
720 }
721
722 static void __init register_buttons(struct button_t *b)
723 {
724 int irq = sb_irq(sbh) + 2;
725 chipcregs_t *cc;
726
727 request_irq(irq, button_handler, SA_SHIRQ | SA_SAMPLE_RANDOM, "gpio", button_handler);
728
729 for (; b->name; b++) {
730 if (b->gpio & gpiomask)
731 continue;
732
733 sb_gpioouten(sbh, b->gpio,0);
734 sb_gpiocontrol(sbh, b->gpio,0);
735 b->polarity = sb_gpioin(sbh) & b->gpio;
736 sb_gpiointpolarity(sbh, b->gpio, b->polarity);
737 sb_gpiointmask(sbh, b->gpio, b->gpio);
738 }
739
740 if ((cc = sb_setcore(sbh, SB_CC, 0))) {
741 int intmask;
742
743 intmask = readl(&cc->intmask);
744 intmask |= CI_GPIO;
745 writel(intmask, &cc->intmask);
746 }
747 }
748
749 static void __exit unregister_buttons(struct button_t *b)
750 {
751 int irq = sb_irq(sbh) + 2;
752
753 for (; b->name; b++)
754 sb_gpiointmask(sbh, b->gpio, 0);
755
756 free_irq(irq, button_handler);
757 }
758
759 static void __init register_leds(struct led_t *l)
760 {
761 struct proc_dir_entry *p;
762
763 leds = proc_mkdir("led", diag);
764 if (!leds)
765 return;
766
767 for(; l->name; l++) {
768 if (l->gpio & gpiomask)
769 continue;
770
771 sb_gpioouten(sbh, l->gpio, l->gpio);
772 sb_gpiocontrol(sbh, l->gpio, 0);
773 sb_gpioout(sbh, l->gpio, (l->polarity == NORMAL)?0:l->gpio);
774
775 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
776 l->proc.type = PROC_LED;
777 l->proc.ptr = l;
778 p->data = (void *) &l->proc;
779 p->proc_fops = &diag_proc_fops;
780 }
781 }
782 }
783
784 static void __exit unregister_leds(struct led_t *l)
785 {
786 for(; l->name; l++)
787 remove_proc_entry(l->name, leds);
788
789 remove_proc_entry("led", diag);
790 }
791
792 static void __exit diag_exit(void)
793 {
794
795 del_timer(&led_timer);
796
797 if (platform.buttons)
798 unregister_buttons(platform.buttons);
799
800 if (platform.leds)
801 unregister_leds(platform.leds);
802
803 remove_proc_entry("model", diag);
804 remove_proc_entry("gpiomask", diag);
805 remove_proc_entry("diag", NULL);
806 }
807
808 static struct prochandler_t proc_model = { .type = PROC_MODEL };
809 static struct prochandler_t proc_gpiomask = { .type = PROC_GPIOMASK };
810
811 static int __init diag_init(void)
812 {
813 static struct proc_dir_entry *p;
814 static struct platform_t *detected;
815
816 detected = platform_detect();
817 if (!detected) {
818 printk(MODULE_NAME ": Router model not detected.\n");
819 return -ENODEV;
820 }
821 memcpy(&platform, detected, sizeof(struct platform_t));
822
823 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
824
825 if (!(diag = proc_mkdir("diag", NULL))) {
826 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
827 return -EINVAL;
828 }
829
830 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
831 p->data = (void *) &proc_model;
832 p->proc_fops = &diag_proc_fops;
833 }
834
835 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
836 p->data = (void *) &proc_gpiomask;
837 p->proc_fops = &diag_proc_fops;
838 }
839
840 if (platform.buttons)
841 register_buttons(platform.buttons);
842
843 if (platform.leds)
844 register_leds(platform.leds);
845
846 return 0;
847 }
848
849 EXPORT_NO_SYMBOLS;
850
851 module_init(diag_init);
852 module_exit(diag_exit);
853
854 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
855 MODULE_LICENSE("GPL");
856
857 /* TODO: export existing sb_irq instead */
858 static int sb_irq(void *sbh)
859 {
860 uint idx;
861 void *regs;
862 sbconfig_t *sb;
863 uint32 flag, sbipsflag;
864 uint irq = 0;
865
866 regs = sb_coreregs(sbh);
867 sb = (sbconfig_t *)((ulong) regs + SBCONFIGOFF);
868 flag = (R_REG(&sb->sbtpsflag) & SBTPS_NUM0_MASK);
869
870 idx = sb_coreidx(sbh);
871
872 if ((regs = sb_setcore(sbh, SB_MIPS, 0)) ||
873 (regs = sb_setcore(sbh, SB_MIPS33, 0))) {
874 sb = (sbconfig_t *)((ulong) regs + SBCONFIGOFF);
875
876 /* sbipsflag specifies which core is routed to interrupts 1 to 4 */
877 sbipsflag = R_REG(&sb->sbipsflag);
878 for (irq = 1; irq <= 4; irq++, sbipsflag >>= 8) {
879 if ((sbipsflag & 0x3f) == flag)
880 break;
881 }
882 if (irq == 5)
883 irq = 0;
884 }
885
886 sb_setcoreidx(sbh, idx);
887
888 return irq;
889 }