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