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