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