Update motorola 850 leds (please verify!) and added placeholder for belkin
[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 /* FIXME: verify */
288 .buttons = {
289 { .name = "reset", .gpio = 1 << 4 },
290 { .name = "ses", .gpio = 1 << 0 },
291 },
292 .leds = {
293 { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
294 { .name = "internal", .gpio = 1 << 3, .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 /* Belkin */
395 [BELKIN_UNKNOWN] = {
396 .name = "Belkin (unknown)"
397 /* FIXME: verify & add detection */
398 .buttons = {
399 { .name = "reset", .gpio = 1 << 7 },
400 },
401 .leds = {
402 { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
403 { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
404 { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
405 },
406 },
407 };
408
409 static struct proc_dir_entry *diag, *leds;
410
411 extern void *bcm947xx_sbh;
412 #define sbh bcm947xx_sbh
413
414 static int sb_irq(void *sbh);
415 static struct platform_t platform;
416
417 extern char *nvram_get(char *str);
418
419 static void led_flash(unsigned long dummy);
420
421 static inline char __init *getvar(char *str)
422 {
423 return nvram_get(str)?:"";
424 }
425
426 static struct platform_t __init *platform_detect(void)
427 {
428 char *boardnum, *boardtype, *buf;
429
430 boardnum = getvar("boardnum");
431 boardtype = getvar("boardtype");
432 if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
433 /* CFE based - newer hardware */
434 if (!strcmp(boardnum, "42")) { /* Linksys */
435 if (!strcmp(boardtype, "0x0101"))
436 return &platforms[WRT54G3G];
437
438 if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
439 return &platforms[WRTSL54GS];
440
441 /* default to WRT54G */
442 return &platforms[WRT54G];
443 }
444
445 if (!strcmp(boardnum, "45")) { /* ASUS */
446 if (!strcmp(boardtype,"0x042f"))
447 return &platforms[WL500GP];
448 else
449 return &platforms[WL500GD];
450 }
451
452 if (!strcmp(boardnum, "10496"))
453 return &platforms[USR5461];
454 } else { /* PMON based - old stuff */
455 if (!strncmp(boardtype, "bcm94710dev", 11)) {
456 if (!strcmp(boardtype, "42"))
457 return &platforms[WRT54GV1];
458 if (simple_strtoul(boardnum, NULL, 9) == 2)
459 return &platforms[WAP54GV1];
460 }
461 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
462 return &platforms[WL500G];
463 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
464 /* Either WL-300g or WL-HDD, do more extensive checks */
465 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
466 (simple_strtoul(getvar("et1phyaddr"), NULL, 9) == 1))
467 return &platforms[WLHDD];
468 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
469 (simple_strtoul(getvar("et1phyaddr"), NULL, 9) == 10))
470 return &platforms[WL300G];
471 }
472
473 /* unknown asus stuff, probably bcm4702 */
474 if (!strncmp(boardnum, "asusX", 5))
475 return &platforms[ASUS_4702];
476
477 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
478 (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30))
479 return &platforms[WR850GV1];
480 }
481
482 if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
483 /* Buffalo hardware, check id for specific hardware matches */
484 if (!strcmp(buf, "29bb0332"))
485 return &platforms[WBR2_G54];
486 if (!strcmp(buf, "29129"))
487 return &platforms[WLA2_G54L];
488 if (!strcmp(buf, "30189"))
489 return &platforms[WHR_HP_G54];
490 if (!strcmp(buf, "30182"))
491 return &platforms[WHR_G54S];
492 }
493
494 if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
495 if (!strncmp(boardtype, "bcm94710ap", 10))
496 return &platforms[BUFFALO_UNKNOWN_4710];
497 else
498 return &platforms[BUFFALO_UNKNOWN];
499 }
500
501 if (!strcmp(getvar("CFEver"), "MotoWRv203"))
502 return &platforms[WR850GV2];
503
504 /* not found */
505 return NULL;
506 }
507
508 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos);
509 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, void *data);
510 static struct file_operations diag_proc_fops = {
511 read: diag_proc_read,
512 write: diag_proc_write
513 };
514
515
516
517 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
518 {
519 #ifdef LINUX_2_4
520 struct inode *inode = file->f_dentry->d_inode;
521 struct proc_dir_entry *dent = inode->u.generic_ip;
522 #else
523 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
524 #endif
525 char *page;
526 int len = 0;
527
528 if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
529 return -ENOBUFS;
530
531 if (dent->data != NULL) {
532 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
533 switch (handler->type) {
534 case PROC_LED: {
535 struct led_t * led = (struct led_t *) handler->ptr;
536 if (led->flash) {
537 len = sprintf(page, "f\n");
538 } else {
539 int in = (sb_gpioin(sbh) & led->gpio ? 1 : 0);
540 int p = (led->polarity == NORMAL ? 0 : 1);
541 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
542 }
543 break;
544 }
545 case PROC_MODEL:
546 len = sprintf(page, "%s\n", platform.name);
547 break;
548 case PROC_GPIOMASK:
549 len = sprintf(page, "%d\n", gpiomask);
550 break;
551 }
552 }
553 len += 1;
554
555 if (*ppos < len) {
556 len = min_t(int, len - *ppos, count);
557 if (copy_to_user(buf, (page + *ppos), len)) {
558 kfree(page);
559 return -EFAULT;
560 }
561 *ppos += len;
562 } else {
563 len = 0;
564 }
565
566 return len;
567 }
568
569
570 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, void *data)
571 {
572 #ifdef LINUX_2_4
573 struct inode *inode = file->f_dentry->d_inode;
574 struct proc_dir_entry *dent = inode->u.generic_ip;
575 #else
576 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
577 #endif
578 char *page;
579 int ret = -EINVAL;
580
581 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
582 return -ENOBUFS;
583
584 if (copy_from_user(page, buf, count)) {
585 kfree(page);
586 return -EINVAL;
587 }
588 page[count] = 0;
589
590 if (dent->data != NULL) {
591 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
592 switch (handler->type) {
593 case PROC_LED: {
594 struct led_t *led = (struct led_t *) handler->ptr;
595 int p = (led->polarity == NORMAL ? 0 : 1);
596
597 if (led->gpio & gpiomask)
598 break;
599
600 if (page[0] == 'f') {
601 led->flash = 1;
602 led_flash(0);
603 } else {
604 led->flash = 0;
605 sb_gpioouten(sbh, led->gpio, led->gpio);
606 sb_gpiocontrol(sbh, led->gpio, 0);
607 sb_gpioout(sbh, led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
608 }
609 break;
610 }
611 case PROC_GPIOMASK:
612 gpiomask = simple_strtoul(page, NULL, 16);
613 break;
614 }
615 ret = count;
616 }
617
618 kfree(page);
619 return ret;
620 }
621
622 struct event_t {
623 struct tq_struct tq;
624 char buf[256];
625 char *argv[3];
626 char *envp[6];
627 };
628
629 static void hotplug_button(struct event_t *event)
630 {
631 call_usermodehelper (event->argv[0], event->argv, event->envp);
632 kfree(event);
633 }
634
635 static void button_handler(int irq, void *dev_id, struct pt_regs *regs)
636 {
637 struct button_t *b;
638 int in = sb_gpioin(sbh);
639 struct event_t *event;
640
641 for (b = platform.buttons; b->name; b++) {
642 if (b->gpio & gpiomask)
643 continue;
644
645 if (b->polarity != (in & b->gpio)) {
646
647 b->pressed ^= 1;
648
649 if ((event = (struct event_t *)kmalloc (256, GFP_KERNEL))) {
650 int i;
651 char *scratch = event->buf;
652
653 i = 0;
654 event->argv[i++] = hotplug_path;
655 event->argv[i++] = "button";
656 event->argv[i] = 0;
657
658 i = 0;
659 event->envp[i++] = "HOME=/";
660 event->envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
661 event->envp[i++] = scratch;
662 scratch += sprintf (scratch, "ACTION=%s", b->pressed?"pressed":"released") + 1;
663 event->envp[i++] = scratch;
664 scratch += sprintf (scratch, "BUTTON=%s", b->name) + 1;
665 event->envp[i++] = scratch;
666 scratch += sprintf (scratch, "SEEN=%ld", (jiffies - b->seen)/HZ) + 1;
667 event->envp[i] = 0;
668
669 INIT_TQUEUE(&event->tq, (void *)(void *)hotplug_button, (void *)event);
670 schedule_task(&event->tq);
671 }
672
673 b->seen = jiffies;
674 b->polarity ^= b->gpio;
675 sb_gpiointpolarity(sbh, b->gpio, b->polarity);
676 }
677 }
678 }
679
680 static struct timer_list led_timer = {
681 function: &led_flash
682 };
683
684 static void led_flash(unsigned long dummy) {
685 struct led_t *l;
686 unsigned mask = 0;
687
688 for (l = platform.leds; l->name; l++) {
689 if (l->flash)
690 mask |= l->gpio;
691 }
692
693 mask &= ~gpiomask;
694
695 if (mask) {
696 unsigned val;
697
698 val = ~sb_gpioin(sbh);
699 val &= mask;
700
701 sb_gpioouten(sbh, mask, mask);
702 sb_gpiocontrol(sbh, mask, 0);
703 sb_gpioout(sbh, mask, val);
704
705 mod_timer(&led_timer, jiffies + FLASH_TIME);
706 }
707 }
708
709 static void __init register_buttons(struct button_t *b)
710 {
711 int irq = sb_irq(sbh) + 2;
712 chipcregs_t *cc;
713
714 request_irq(irq, button_handler, SA_SHIRQ | SA_SAMPLE_RANDOM, "gpio", button_handler);
715
716 for (; b->name; b++) {
717 if (b->gpio & gpiomask)
718 continue;
719
720 sb_gpioouten(sbh, b->gpio,0);
721 sb_gpiocontrol(sbh, b->gpio,0);
722 b->polarity = sb_gpioin(sbh) & b->gpio;
723 sb_gpiointpolarity(sbh, b->gpio, b->polarity);
724 sb_gpiointmask(sbh, b->gpio, b->gpio);
725 }
726
727 if ((cc = sb_setcore(sbh, SB_CC, 0))) {
728 int intmask;
729
730 intmask = readl(&cc->intmask);
731 intmask |= CI_GPIO;
732 writel(intmask, &cc->intmask);
733 }
734 }
735
736 static void __exit unregister_buttons(struct button_t *b)
737 {
738 int irq = sb_irq(sbh) + 2;
739
740 for (; b->name; b++)
741 sb_gpiointmask(sbh, b->gpio, 0);
742
743 free_irq(irq, button_handler);
744 }
745
746 static void __init register_leds(struct led_t *l)
747 {
748 struct proc_dir_entry *p;
749
750 leds = proc_mkdir("led", diag);
751 if (!leds)
752 return;
753
754 for(; l->name; l++) {
755 if (l->gpio & gpiomask)
756 continue;
757
758 sb_gpioouten(sbh, l->gpio, l->gpio);
759 sb_gpiocontrol(sbh, l->gpio, 0);
760 sb_gpioout(sbh, l->gpio, (l->polarity == NORMAL)?0:l->gpio);
761
762 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
763 l->proc.type = PROC_LED;
764 l->proc.ptr = l;
765 p->data = (void *) &l->proc;
766 p->proc_fops = &diag_proc_fops;
767 }
768 }
769 }
770
771 static void __exit unregister_leds(struct led_t *l)
772 {
773 for(; l->name; l++)
774 remove_proc_entry(l->name, leds);
775
776 remove_proc_entry("led", diag);
777 }
778
779 static void __exit diag_exit(void)
780 {
781
782 del_timer(&led_timer);
783
784 if (platform.buttons)
785 unregister_buttons(platform.buttons);
786
787 if (platform.leds)
788 unregister_leds(platform.leds);
789
790 remove_proc_entry("model", diag);
791 remove_proc_entry("gpiomask", diag);
792 remove_proc_entry("diag", NULL);
793 }
794
795 static struct prochandler_t proc_model = { .type = PROC_MODEL };
796 static struct prochandler_t proc_gpiomask = { .type = PROC_GPIOMASK };
797
798 static int __init diag_init(void)
799 {
800 static struct proc_dir_entry *p;
801 static struct platform_t *detected;
802
803 detected = platform_detect();
804 if (!detected) {
805 printk(MODULE_NAME ": Router model not detected.\n");
806 return -ENODEV;
807 }
808 memcpy(&platform, detected, sizeof(struct platform_t));
809
810 printk(MODULE_NAME ": Detected '%s'\n", platform.name);
811
812 if (!(diag = proc_mkdir("diag", NULL))) {
813 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
814 return -EINVAL;
815 }
816
817 if ((p = create_proc_entry("model", S_IRUSR, diag))) {
818 p->data = (void *) &proc_model;
819 p->proc_fops = &diag_proc_fops;
820 }
821
822 if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
823 p->data = (void *) &proc_gpiomask;
824 p->proc_fops = &diag_proc_fops;
825 }
826
827 if (platform.buttons)
828 register_buttons(platform.buttons);
829
830 if (platform.leds)
831 register_leds(platform.leds);
832
833 return 0;
834 }
835
836 EXPORT_NO_SYMBOLS;
837
838 module_init(diag_init);
839 module_exit(diag_exit);
840
841 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
842 MODULE_LICENSE("GPL");
843
844 /* TODO: export existing sb_irq instead */
845 static int sb_irq(void *sbh)
846 {
847 uint idx;
848 void *regs;
849 sbconfig_t *sb;
850 uint32 flag, sbipsflag;
851 uint irq = 0;
852
853 regs = sb_coreregs(sbh);
854 sb = (sbconfig_t *)((ulong) regs + SBCONFIGOFF);
855 flag = (R_REG(&sb->sbtpsflag) & SBTPS_NUM0_MASK);
856
857 idx = sb_coreidx(sbh);
858
859 if ((regs = sb_setcore(sbh, SB_MIPS, 0)) ||
860 (regs = sb_setcore(sbh, SB_MIPS33, 0))) {
861 sb = (sbconfig_t *)((ulong) regs + SBCONFIGOFF);
862
863 /* sbipsflag specifies which core is routed to interrupts 1 to 4 */
864 sbipsflag = R_REG(&sb->sbipsflag);
865 for (irq = 1; irq <= 4; irq++, sbipsflag >>= 8) {
866 if ((sbipsflag & 0x3f) == flag)
867 break;
868 }
869 if (irq == 5)
870 irq = 0;
871 }
872
873 sb_setcoreidx(sbh, idx);
874
875 return irq;
876 }