Root/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 * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
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#ifndef LINUX_2_4
32#include <linux/workqueue.h>
33#include <linux/skbuff.h>
34#include <linux/netlink.h>
35#include <net/sock.h>
36extern struct sock *uevent_sock;
37extern u64 uevent_next_seqnum(void);
38#else
39#include <linux/tqueue.h>
40#define INIT_WORK INIT_TQUEUE
41#define schedule_work schedule_task
42#define work_struct tq_struct
43#endif
44
45#include "gpio.h"
46#include "diag.h"
47#define getvar(str) (nvram_get(str)?:"")
48
49static inline int startswith (char *source, char *cmp) { return !strncmp(source,cmp,strlen(cmp)); }
50static int fill_event(struct event_t *);
51static unsigned int gpiomask = 0;
52module_param(gpiomask, int, 0644);
53
54enum {
55    /* Linksys */
56    WAP54GV1,
57    WAP54GV2,
58    WAP54GV3,
59    WRT54GV1,
60    WRT54G,
61    WRTSL54GS,
62    WRT54G3G,
63    WRT160N,
64    WRT300NV11,
65    WRT350N,
66    WRT600N,
67    WRT600NV11,
68    WRT610N,
69
70    /* ASUS */
71    WLHDD,
72    WL300G,
73    WL320GE,
74    WL330GE,
75    WL500G,
76    WL500GD,
77    WL500GP,
78    WL500GPV2,
79    WL500W,
80    WL520GC,
81    WL520GU,
82    ASUS_4702,
83    WL700GE,
84
85    /* Buffalo */
86    WBR2_G54,
87    WHR_G54S,
88    WHR_HP_G54,
89    WHR_G125,
90    WHR2_A54G54,
91    WLA2_G54L,
92    WZR_G300N,
93    WZR_RS_G54,
94    WZR_RS_G54HP,
95    BUFFALO_UNKNOWN,
96    BUFFALO_UNKNOWN_4710,
97
98    /* Siemens */
99    SE505V1,
100    SE505V2,
101
102    /* US Robotics */
103    USR5461,
104
105    /* Dell */
106    TM2300,
107    TM2300V2,
108
109    /* Motorola */
110    WE800G,
111    WR850GV1,
112    WR850GV2V3,
113    WR850GP,
114
115    /* Belkin */
116    BELKIN_UNKNOWN,
117
118    /* Netgear */
119    WGT634U,
120
121    /* Trendware */
122    TEW411BRPP,
123
124    /* SimpleTech */
125    STI_NAS,
126
127    /* D-Link */
128    DIR130,
129    DIR320,
130    DIR330,
131    DWL3150,
132
133    /* Sitecom */
134    WL105B,
135
136    /* Western Digital */
137    WDNetCenter,
138
139    /* Askey */
140    RT210W,
141
142    /* OvisLink */
143    WL1600GL,
144
145    /* Microsoft */
146    MN700,
147};
148
149static void __init bcm4780_init(void) {
150        int pin = 1 << 3;
151
152        /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
153        printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
154        gpio_outen(pin, pin);
155        gpio_control(pin, 0);
156        gpio_out(pin, pin);
157
158        /* Wait 5s, so the HDD can spin up */
159        set_current_state(TASK_INTERRUPTIBLE);
160        schedule_timeout(HZ * 5);
161}
162
163static void __init NetCenter_init(void) {
164        /* unset pin 6 (+12V) */
165        int pin = 1 << 6;
166        gpio_outen(pin, pin);
167        gpio_control(pin, 0);
168        gpio_out(pin, pin);
169        /* unset pin 1 (turn off red led, blue will light alone if +5V comes up) */
170        pin = 1 << 1;
171        gpio_outen(pin, pin);
172        gpio_control(pin, 0);
173        gpio_out(pin, pin);
174        /* unset pin 3 (+5V) and wait 5 seconds (harddisk spin up) */
175        bcm4780_init();
176}
177
178static void __init bcm57xx_init(void) {
179    int pin = 1 << 2;
180
181#ifndef LINUX_2_4
182    /* FIXME: switch comes up, but port mappings/vlans not right */
183    gpio_outen(pin, pin);
184    gpio_control(pin, 0);
185    gpio_out(pin, pin);
186#endif
187}
188
189static struct platform_t __initdata platforms[] = {
190    /* Linksys */
191    [WAP54GV1] = {
192        .name = "Linksys WAP54G V1",
193        .buttons = {
194            { .name = "reset", .gpio = 1 << 0 },
195        },
196        .leds = {
197            { .name = "diag", .gpio = 1 << 3 },
198            { .name = "wlan", .gpio = 1 << 4 },
199        },
200    },
201    [WAP54GV2] = {
202        .name = "Linksys WAP54G V2",
203        .buttons = {
204            { .name = "reset", .gpio = 1 << 0 },
205        },
206        .leds = {
207            { .name = "wlan", .gpio = 1 << 5, .polarity = REVERSE },
208            /* GPIO 6 is b44 (eth0, LAN) PHY power */
209        },
210    },
211    [WAP54GV3] = {
212        .name = "Linksys WAP54G V3",
213        .buttons = {
214            /* FIXME: verify this */
215            { .name = "reset", .gpio = 1 << 7 },
216            { .name = "ses", .gpio = 1 << 0 },
217        },
218        .leds = {
219            /* FIXME: diag? */
220            { .name = "ses", .gpio = 1 << 1 },
221        },
222    },
223    [WRT54GV1] = {
224        .name = "Linksys WRT54G V1.x",
225        .buttons = {
226            { .name = "reset", .gpio = 1 << 6 },
227        },
228        .leds = {
229            { .name = "diag", .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
230            { .name = "dmz", .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
231        },
232    },
233    [WRT54G] = {
234        .name = "Linksys WRT54G/GS/GL",
235        .buttons = {
236            { .name = "reset", .gpio = 1 << 6 },
237            { .name = "ses", .gpio = 1 << 4 },
238        },
239        .leds = {
240            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
241            { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
242            { .name = "ses_white", .gpio = 1 << 2, .polarity = REVERSE },
243            { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
244            { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
245        },
246    },
247    [WRTSL54GS] = {
248        .name = "Linksys WRTSL54GS",
249        .buttons = {
250            { .name = "reset", .gpio = 1 << 6 },
251            { .name = "ses", .gpio = 1 << 4 },
252        },
253        .leds = {
254            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
255            { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE },
256            { .name = "ses_white", .gpio = 1 << 5, .polarity = REVERSE },
257            { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
258        },
259    },
260    [WRT54G3G] = {
261        .name = "Linksys WRT54G3G",
262        .buttons = {
263            { .name = "reset", .gpio = 1 << 6 },
264            { .name = "3g", .gpio = 1 << 4 },
265        },
266        .leds = {
267            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
268            { .name = "dmz", .gpio = 1 << 7, .polarity = REVERSE },
269            { .name = "3g_green", .gpio = 1 << 2, .polarity = NORMAL },
270            { .name = "3g_blue", .gpio = 1 << 3, .polarity = NORMAL },
271            { .name = "3g_blink", .gpio = 1 << 5, .polarity = NORMAL },
272        },
273    },
274    [WRT160N] = {
275        .name = "Linksys WRT160N",
276        .buttons = {
277            { .name = "reset", .gpio = 1 << 6 },
278            { .name = "ses", .gpio = 1 << 4 },
279        },
280        .leds = {
281            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
282            { .name = "ses_blue", .gpio = 1 << 5, .polarity = REVERSE },
283            { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
284        },
285    },
286    [WRT300NV11] = {
287        .name = "Linksys WRT300N V1.1",
288        .buttons = {
289            { .name = "reset", .gpio = 1 << 6 }, // "Reset" on back panel
290            { .name = "ses", .gpio = 1 << 4 }, // "Reserved" on top panel
291        },
292        .leds = {
293            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // "Power"
294            { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // "Security" Amber
295            { .name = "ses_green", .gpio = 1 << 5, .polarity = REVERSE }, // "Security" Green
296        },
297        .platform_init = bcm57xx_init,
298    },
299    [WRT350N] = {
300        .name = "Linksys WRT350N",
301        .buttons = {
302            { .name = "reset", .gpio = 1 << 6 },
303            { .name = "ses", .gpio = 1 << 8 },
304        },
305        .leds = {
306            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
307            { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE },
308            { .name = "ses_green", .gpio = 1 << 9, .polarity = REVERSE },
309            { .name = "usb_blink", .gpio = 1 << 10, .polarity = REVERSE },
310            { .name = "usb", .gpio = 1 << 11, .polarity = REVERSE },
311        },
312        .platform_init = bcm57xx_init,
313    },
314    [WRT600N] = {
315        .name = "Linksys WRT600N",
316        .buttons = {
317            { .name = "reset", .gpio = 1 << 6 },
318            { .name = "ses", .gpio = 1 << 7 },
319        },
320        .leds = {
321            { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
322            { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
323            { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
324            { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
325            { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
326            { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
327        },
328        .platform_init = bcm57xx_init,
329    },
330    [WRT600NV11] = {
331        .name = "Linksys WRT600N V1.1",
332        .buttons = {
333            { .name = "reset", .gpio = 1 << 6 },
334            { .name = "ses", .gpio = 1 << 7 },
335        },
336        .leds = {
337            { .name = "power", .gpio = 1 << 2, .polarity = REVERSE }, // Power LED
338            { .name = "usb", .gpio = 1 << 3, .polarity = REVERSE }, // USB LED
339            { .name = "wl0_ses_amber", .gpio = 1 << 8, .polarity = REVERSE }, // 2.4Ghz LED Amber
340            { .name = "wl0_ses_green", .gpio = 1 << 9, .polarity = REVERSE }, // 2.4Ghz LED Green
341            { .name = "wl1_ses_amber", .gpio = 1 << 10, .polarity = REVERSE }, // 5.6Ghz LED Amber
342            { .name = "wl1_ses_green", .gpio = 1 << 11, .polarity = REVERSE }, // 5.6Ghz LED Green
343        },
344        .platform_init = bcm57xx_init,
345    },
346    [WRT610N] = {
347        .name = "Linksys WRT610N",
348        .buttons = {
349            { .name = "reset", .gpio = 1 << 6 },
350            { .name = "ses", .gpio = 1 << 8 },
351        },
352        .leds = {
353            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL }, // Power LED
354            { .name = "usb", .gpio = 1 << 0, .polarity = REVERSE }, // USB LED
355            { .name = "ses_amber", .gpio = 1 << 3, .polarity = REVERSE }, // WiFi protected setup LED amber
356            { .name = "ses_blue", .gpio = 1 << 9, .polarity = REVERSE }, // WiFi protected setup LED blue
357        },
358    },
359    /* Asus */
360    [WLHDD] = {
361        .name = "ASUS WL-HDD",
362        .buttons = {
363            { .name = "reset", .gpio = 1 << 6 },
364        },
365        .leds = {
366            { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
367            { .name = "usb", .gpio = 1 << 2, .polarity = REVERSE },
368        },
369    },
370    [WL300G] = {
371        .name = "ASUS WL-300g",
372        .buttons = {
373            { .name = "reset", .gpio = 1 << 6 },
374        },
375        .leds = {
376            { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
377        },
378    },
379    [WL320GE] = {
380        .name = "ASUS WL-320gE/WL-320gP",
381        .buttons = {
382            { .name = "reset", .gpio = 1 << 6 },
383        },
384        .leds = {
385            { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
386            { .name = "power", .gpio = 1 << 2, .polarity = REVERSE },
387            { .name = "link", .gpio = 1 << 11, .polarity = REVERSE },
388        },
389    },
390    [WL330GE] = {
391        .name = "ASUS WL-330gE",
392        .buttons = {
393            { .name = "reset", .gpio = 1 << 2 },
394        },
395        .leds = {
396            { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
397        },
398    },
399    [WL500G] = {
400        .name = "ASUS WL-500g",
401        .buttons = {
402            { .name = "reset", .gpio = 1 << 6 },
403        },
404        .leds = {
405            { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
406        },
407    },
408    [WL500GD] = {
409        .name = "ASUS WL-500g Deluxe",
410        .buttons = {
411            { .name = "reset", .gpio = 1 << 6 },
412        },
413        .leds = {
414            { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
415        },
416    },
417    [WL500GP] = {
418        .name = "ASUS WL-500g Premium",
419        .buttons = {
420            { .name = "reset", .gpio = 1 << 0 },
421            { .name = "ses", .gpio = 1 << 4 },
422        },
423        .leds = {
424            { .name = "power", .gpio = 1 << 1, .polarity = REVERSE },
425        },
426    },
427    [WL500GPV2] = {
428        .name = "ASUS WL-500g Premium V2",
429        .buttons = {
430            { .name = "reset", .gpio = 1 << 2 },
431            { .name = "ses", .gpio = 1 << 3 },
432        },
433        .leds = {
434            { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
435            { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
436        },
437    },
438    [WL500W] = {
439        .name = "ASUS WL-500W",
440        .buttons = {
441            { .name = "reset", .gpio = 1 << 6 },
442            { .name = "ses", .gpio = 1 << 7 },
443        },
444        .leds = {
445            { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
446        },
447    },
448    [WL520GC] = {
449        .name = "ASUS WL-520GC",
450        .buttons = {
451            { .name = "reset", .gpio = 1 << 2 },
452            { .name = "ses", .gpio = 1 << 3 },
453        },
454        .leds = {
455        { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
456            { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
457        },
458    },
459    [WL520GU] = {
460        .name = "ASUS WL-520gU",
461        .buttons = {
462            { .name = "reset", .gpio = 1 << 2 },
463            { .name = "ses", .gpio = 1 << 3 },
464        },
465        .leds = {
466            { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
467            { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
468        },
469    },
470    [ASUS_4702] = {
471        .name = "ASUS (unknown, BCM4702)",
472        .buttons = {
473            { .name = "reset", .gpio = 1 << 6 },
474        },
475        .leds = {
476            { .name = "power", .gpio = 1 << 0, .polarity = REVERSE },
477        },
478    },
479    [WL700GE] = {
480        .name = "ASUS WL-700gE",
481        .buttons = {
482            { .name = "reset", .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
483            { .name = "ses", .gpio = 1 << 4 }, // on back, actual name ezsetup
484            { .name = "power", .gpio = 1 << 0 }, // on front
485            { .name = "copy", .gpio = 1 << 6 }, // on front
486        },
487        .leds = {
488#if 0
489            // GPIO that controls power led also enables/disables some essential functions
490            // - power to HDD
491            // - switch leds
492            { .name = "power", .gpio = 1 << 3, .polarity = NORMAL }, // actual name power
493#endif
494            { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
495        },
496        .platform_init = bcm4780_init,
497    },
498    /* Buffalo */
499    [WHR_G54S] = {
500        .name = "Buffalo WHR-G54S",
501        .buttons = {
502            { .name = "reset", .gpio = 1 << 4 },
503            { .name = "bridge", .gpio = 1 << 5 },
504            { .name = "ses", .gpio = 1 << 0 },
505        },
506        .leds = {
507            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
508            { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
509            { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
510            { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
511            { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
512        },
513    },
514    [WBR2_G54] = {
515        .name = "Buffalo WBR2-G54",
516        /* FIXME: verify */
517        .buttons = {
518            { .name = "reset", .gpio = 1 << 7 },
519        },
520        .leds = {
521            { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
522        },
523    },
524    [WHR_HP_G54] = {
525        .name = "Buffalo WHR-HP-G54",
526        .buttons = {
527            { .name = "reset", .gpio = 1 << 4 },
528            { .name = "bridge", .gpio = 1 << 5 },
529            { .name = "ses", .gpio = 1 << 0 },
530        },
531        .leds = {
532            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
533            { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
534            { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
535            { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
536            { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
537        },
538    },
539    [WHR_G125] = {
540        .name = "Buffalo WHR-G125",
541        .buttons = {
542            { .name = "reset", .gpio = 1 << 4 },
543            { .name = "bridge", .gpio = 1 << 5 },
544            { .name = "ses", .gpio = 1 << 0 },
545        },
546        .leds = {
547            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
548            { .name = "internal", .gpio = 1 << 3, .polarity = REVERSE },
549            { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
550            { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
551            { .name = "wlan", .gpio = 1 << 2, .polarity = REVERSE },
552        },
553    },
554    [WHR2_A54G54] = {
555        .name = "Buffalo WHR2-A54G54",
556        .buttons = {
557            { .name = "reset", .gpio = 1 << 4 },
558        },
559        .leds = {
560            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
561        },
562    },
563    [WLA2_G54L] = {
564        .name = "Buffalo WLA2-G54L",
565        /* FIXME: verify */
566        .buttons = {
567            { .name = "reset", .gpio = 1 << 7 },
568        },
569        .leds = {
570            { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
571        },
572    },
573    [WZR_G300N] = {
574        .name = "Buffalo WZR-G300N",
575        .buttons = {
576            { .name = "reset", .gpio = 1 << 4 },
577        },
578        .leds = {
579            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
580            { .name = "bridge", .gpio = 1 << 1, .polarity = REVERSE },
581            { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
582        },
583    },
584    [WZR_RS_G54] = {
585        .name = "Buffalo WZR-RS-G54",
586        .buttons = {
587            { .name = "ses", .gpio = 1 << 0 },
588            { .name = "reset", .gpio = 1 << 4 },
589        },
590        .leds = {
591            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
592            { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
593            { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
594        },
595    },
596    [WZR_RS_G54HP] = {
597        .name = "Buffalo WZR-RS-G54HP",
598        .buttons = {
599            { .name = "ses", .gpio = 1 << 0 },
600            { .name = "reset", .gpio = 1 << 4 },
601        },
602        .leds = {
603            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
604            { .name = "ses", .gpio = 1 << 6, .polarity = REVERSE },
605            { .name = "vpn", .gpio = 1 << 1, .polarity = REVERSE },
606        },
607    },
608    [BUFFALO_UNKNOWN] = {
609        .name = "Buffalo (unknown)",
610        .buttons = {
611            { .name = "reset", .gpio = 1 << 7 },
612        },
613        .leds = {
614            { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
615        },
616    },
617    [BUFFALO_UNKNOWN_4710] = {
618        .name = "Buffalo (unknown, BCM4710)",
619        .buttons = {
620            { .name = "reset", .gpio = 1 << 4 },
621        },
622        .leds = {
623            { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE },
624        },
625    },
626    /* Siemens */
627    [SE505V1] = {
628        .name = "Siemens SE505 V1",
629        .buttons = {
630            /* No usable buttons */
631        },
632        .leds = {
633// { .name = "power", .gpio = 1 << 0 .polarity = REVERSE }, // Usable when retrofitting D26 (?)
634            { .name = "dmz", .gpio = 1 << 4, .polarity = REVERSE }, // actual name WWW
635            { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
636        },
637    },
638    [SE505V2] = {
639        .name = "Siemens SE505 V2",
640        .buttons = {
641            /* No usable buttons */
642        },
643        .leds = {
644            { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
645            { .name = "dmz", .gpio = 1 << 0, .polarity = REVERSE }, // actual name WWW
646            { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
647        },
648    },
649    /* US Robotics */
650    [USR5461] = {
651        .name = "U.S. Robotics USR5461",
652        .buttons = {
653            /* No usable buttons */
654        },
655        .leds = {
656            { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
657            { .name = "printer", .gpio = 1 << 1, .polarity = REVERSE },
658        },
659    },
660    /* Dell */
661    [TM2300] = {
662        .name = "Dell TrueMobile 2300",
663        .buttons = {
664            { .name = "reset", .gpio = 1 << 0 },
665        },
666        .leds = {
667            { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
668            { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
669        },
670    },
671    [TM2300V2] = {
672        .name = "Dell TrueMobile 2300 v2",
673        .buttons = {
674            { .name = "reset", .gpio = 1 << 0 },
675        },
676        .leds = {
677            { .name = "wlan", .gpio = 1 << 6, .polarity = REVERSE },
678            { .name = "power", .gpio = 1 << 7, .polarity = REVERSE },
679        },
680    },
681    /* Motorola */
682    [WE800G] = {
683        .name = "Motorola WE800G",
684        .buttons = {
685            { .name = "reset", .gpio = 1 << 0 },
686        },
687        .leds = {
688            { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
689            { .name = "diag", .gpio = 1 << 2, .polarity = REVERSE },
690            { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
691        },
692    },
693    [WR850GV1] = {
694        .name = "Motorola WR850G V1",
695        .buttons = {
696            { .name = "reset", .gpio = 1 << 0 },
697        },
698        .leds = {
699            { .name = "power", .gpio = 1 << 4, .polarity = NORMAL },
700            { .name = "diag", .gpio = 1 << 3, .polarity = REVERSE },
701            { .name = "dmz", .gpio = 1 << 6, .polarity = NORMAL },
702            { .name = "wlan_red", .gpio = 1 << 5, .polarity = REVERSE },
703            { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
704        },
705    },
706    [WR850GV2V3] = {
707        .name = "Motorola WR850G V2/V3",
708        .buttons = {
709            { .name = "reset", .gpio = 1 << 5 },
710        },
711        .leds = {
712            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
713            { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
714            { .name = "wan", .gpio = 1 << 6, .polarity = INPUT },
715            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
716        },
717    },
718    [WR850GP] = {
719        .name = "Motorola WR850GP",
720        .buttons = {
721            { .name = "reset", .gpio = 1 << 5 },
722        },
723        .leds = {
724            { .name = "power", .gpio = 1 << 1, .polarity = NORMAL },
725            { .name = "wlan", .gpio = 1 << 0, .polarity = REVERSE },
726            { .name = "dmz", .gpio = 1 << 6, .polarity = REVERSE },
727            { .name = "diag", .gpio = 1 << 7, .polarity = REVERSE },
728        },
729    },
730
731    /* Belkin */
732    [BELKIN_UNKNOWN] = {
733        .name = "Belkin (unknown)",
734        /* FIXME: verify & add detection */
735        .buttons = {
736            { .name = "reset", .gpio = 1 << 7 },
737        },
738        .leds = {
739            { .name = "power", .gpio = 1 << 5, .polarity = NORMAL },
740            { .name = "wlan", .gpio = 1 << 3, .polarity = NORMAL },
741            { .name = "connected", .gpio = 1 << 0, .polarity = NORMAL },
742        },
743    },
744    /* Netgear */
745    [WGT634U] = {
746        .name = "Netgear WGT634U",
747        .buttons = {
748            { .name = "reset", .gpio = 1 << 2 },
749        },
750        .leds = {
751            { .name = "power", .gpio = 1 << 3, .polarity = NORMAL },
752        },
753    },
754    /* Trendware */
755    [TEW411BRPP] = {
756        .name = "Trendware TEW411BRP+",
757        .buttons = {
758            { /* No usable buttons */ },
759        },
760        .leds = {
761            { .name = "power", .gpio = 1 << 7, .polarity = NORMAL },
762            { .name = "wlan", .gpio = 1 << 1, .polarity = NORMAL },
763            { .name = "bridge", .gpio = 1 << 6, .polarity = NORMAL },
764        },
765    },
766    /* SimpleTech */
767    [STI_NAS] = {
768        .name = "SimpleTech SimpleShare NAS",
769        .buttons = {
770            { .name = "reset", .gpio = 1 << 0 }, // Power button on back, named reset to enable failsafe.
771        },
772        .leds = {
773            { .name = "diag", .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
774        },
775        .platform_init = bcm4780_init,
776    },
777    /* D-Link */
778    [DIR130] = {
779        .name = "D-Link DIR-130",
780        .buttons = {
781            { .name = "reset", .gpio = 1 << 3},
782            { .name = "reserved", .gpio = 1 << 7},
783        },
784        .leds = {
785            { .name = "diag", .gpio = 1 << 0},
786            { .name = "blue", .gpio = 1 << 6},
787        },
788    },
789    [DIR320] = {
790        .name = "D-Link DIR-320",
791        .buttons = {
792            { .name = "reserved", .gpio = 1 << 6},
793            { .name = "reset", .gpio = 1 << 7},
794        },
795        .leds = {
796            { .name = "wlan", .gpio = 1 << 0, .polarity = NORMAL },
797            { .name = "diag", .gpio = 1 << 1, .polarity = NORMAL }, /* "status led */
798            { .name = "red", .gpio = 1 << 3, .polarity = REVERSE },
799            { .name = "blue", .gpio = 1 << 4, .polarity = REVERSE },
800            { .name = "usb", .gpio = 1 << 5, .polarity = NORMAL },
801        },
802    },
803    [DIR330] = {
804        .name = "D-Link DIR-330",
805        .buttons = {
806            { .name = "reset", .gpio = 1 << 3},
807            { .name = "reserved", .gpio = 1 << 7},
808        },
809        .leds = {
810            { .name = "diag", .gpio = 1 << 0},
811            { .name = "usb", .gpio = 1 << 4},
812            { .name = "blue", .gpio = 1 << 6},
813        },
814    },
815    [DWL3150] = {
816        .name = "D-Link DWL-3150",
817        .buttons = {
818            { .name = "reset", .gpio = 1 << 7},
819        },
820        .leds = {
821            { .name = "diag", .gpio = 1 << 2},
822            { .name = "status", .gpio = 1 << 1},
823        },
824    },
825    /* Double check */
826    [WL105B] = {
827        .name = "Sitecom WL-105b",
828        .buttons = {
829            { .name = "reset", .gpio = 1 << 10},
830        },
831        .leds = {
832            { .name = "wlan", .gpio = 1 << 4},
833            { .name = "power", .gpio = 1 << 3},
834        },
835    },
836    /* Western Digital Net Center */
837    [WDNetCenter] = {
838        .name = "Western Digital NetCenter",
839        .buttons = {
840            { .name = "power", .gpio = 1 << 0},
841            { .name = "reset", .gpio = 1 << 7},
842        },
843        .platform_init = NetCenter_init,
844    },
845    /* Askey (and clones) */
846    [RT210W] = {
847        .name = "Askey RT210W",
848        .buttons = {
849            /* Power button is hard-wired to hardware reset */
850            /* but is also connected to GPIO 7 (probably for bootloader recovery) */
851            { .name = "power", .gpio = 1 << 7},
852        },
853        .leds = {
854             /* These were verified and named based on Belkin F5D4230-4 v1112 */
855            { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
856            { .name = "wlan", .gpio = 1 << 3, .polarity = REVERSE },
857            { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
858        },
859    },
860    [WL1600GL] = {
861        .name = "OvisLink WL-1600GL",
862        .buttons = {
863            { .name = "reset", .gpio = 1 << 3 },
864            { .name = "ses", .gpio = 1 << 4 },
865        },
866        .leds = {
867            { .name = "power", .gpio = 1 << 5, .polarity = REVERSE },
868            { .name = "wps", .gpio = 1 << 2, .polarity = REVERSE },
869            { .name = "wlan", .gpio = 1 << 1, .polarity = REVERSE },
870            { .name = "connected", .gpio = 1 << 0, .polarity = REVERSE },
871        },
872    },
873    /* Microsoft */
874    [MN700] = {
875        .name = "Microsoft MN-700",
876        .buttons = {
877            { .name = "reset", .gpio = 1 << 7 },
878        },
879        .leds = {
880            { .name = "power", .gpio = 1 << 6, .polarity = NORMAL },
881        },
882    },
883};
884
885static struct platform_t __init *platform_detect(void)
886{
887    char *boardnum, *boardtype, *buf;
888
889    if (strcmp(getvar("nvram_type"), "cfe") == 0)
890        return &platforms[WGT634U];
891
892    /* Look for a model identifier */
893
894    /* Based on "model_name" */
895    if ((buf = nvram_get("model_name"))) {
896        if (!strcmp(buf, "DIR-130"))
897            return &platforms[DIR130];
898        if (!strcmp(buf, "DIR-330"))
899            return &platforms[DIR330];
900    }
901
902    /* Based on "wsc_modelname */
903    if ((buf = nvram_get("wsc_modelname"))) {
904        if (!strcmp(buf, "WRT610N"))
905            return &platforms[WRT610N];
906    }
907
908    /* Based on "model_no" */
909    if ((buf = nvram_get("model_no"))) {
910        if (startswith(buf,"WL700")) /* WL700* */
911            return &platforms[WL700GE];
912    }
913
914    /* Based on "hardware_version" */
915    if ((buf = nvram_get("hardware_version"))) {
916        if (startswith(buf,"WL500GPV2-")) /* WL500GPV2-* */
917            return &platforms[WL500GPV2];
918        if (startswith(buf,"WL520GC-")) /* WL520GU-* */
919            return &platforms[WL520GC];
920        if (startswith(buf,"WL520GU-")) /* WL520GU-* */
921            return &platforms[WL520GU];
922        if (startswith(buf,"WL330GE-")) /* WL330GE-* */
923            return &platforms[WL330GE];
924    }
925
926    /* Based on "ModelId" */
927    if ((buf = nvram_get("ModelId"))) {
928        if (!strcmp(buf, "WR850GP"))
929            return &platforms[WR850GP];
930        if (!strcmp(buf, "WR850G"))
931            return &platforms[WR850GV2V3];
932        if (!strcmp(buf, "WX-5565") && !strcmp(getvar("boardtype"),"bcm94710ap"))
933            return &platforms[TM2300]; /* Dell TrueMobile 2300 */
934        if (startswith(buf,"WE800G")) /* WE800G* */
935            return &platforms[WE800G];
936    }
937
938    /* Buffalo */
939    if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
940        /* Buffalo hardware, check id for specific hardware matches */
941        if (!strcmp(buf, "29bb0332"))
942            return &platforms[WBR2_G54];
943        if (!strcmp(buf, "29129"))
944            return &platforms[WLA2_G54L];
945        if (!strcmp(buf, "30189"))
946            return &platforms[WHR_HP_G54];
947        if (!strcmp(buf, "32093"))
948            return &platforms[WHR_G125];
949        if (!strcmp(buf, "30182"))
950            return &platforms[WHR_G54S];
951        if (!strcmp(buf, "290441dd"))
952            return &platforms[WHR2_A54G54];
953        if (!strcmp(buf, "31120"))
954            return &platforms[WZR_G300N];
955        if (!strcmp(buf, "30083"))
956            return &platforms[WZR_RS_G54];
957        if (!strcmp(buf, "30103"))
958            return &platforms[WZR_RS_G54HP];
959    }
960
961    /* no easy model number, attempt to guess */
962    boardnum = getvar("boardnum");
963    boardtype = getvar("boardtype");
964
965    if (!strcmp(boardnum, "20070615")) { /* Linksys WRT600N v1/V1.1 */
966        if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0") && !strcmp(getvar("switch_type"),"BCM5395"))
967            return &platforms[WRT600NV11];
968
969    if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "0"))
970            return &platforms[WRT600N];
971    }
972
973    if (startswith(getvar("pmon_ver"), "CFE")) {
974        /* CFE based - newer hardware */
975        if (!strcmp(boardnum, "42")) { /* Linksys */
976            if (!strcmp(boardtype, "0x478") && !strcmp(getvar("boot_hw_model"), "WRT300N") && !strcmp(getvar("boot_hw_ver"), "1.1"))
977                return &platforms[WRT300NV11];
978
979            if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
980                return &platforms[WRT350N];
981
982            if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
983                return &platforms[WRT54G3G];
984
985            if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
986                return &platforms[WRTSL54GS];
987
988            if (!strcmp(boardtype, "0x0472"))
989                return &platforms[WRT160N];
990
991            /* default to WRT54G */
992            return &platforms[WRT54G];
993        }
994        if (!strcmp(boardnum, "1024") && !strcmp(boardtype, "0x0446"))
995            return &platforms[WAP54GV2];
996
997        if (!strcmp(boardnum, "8") && !strcmp(boardtype, "0x048e"))
998            return &platforms[WL1600GL];
999
1000
1001        if (!strcmp(boardnum, "44") || !strcmp(boardnum, "44\r")) {
1002            if (!strcmp(boardtype,"0x0101") || !strcmp(boardtype, "0x0101\r"))
1003                return &platforms[TM2300V2]; /* Dell TrueMobile 2300 v2 */
1004        }
1005
1006        if (!strcmp(boardnum, "45")) { /* ASUS */
1007            if (!strcmp(boardtype,"0x042f"))
1008                return &platforms[WL500GP];
1009            else if (!strcmp(boardtype,"0x0472"))
1010                return &platforms[WL500W];
1011            else if (!strcmp(boardtype,"0x467"))
1012                return &platforms[WL320GE];
1013            else
1014                return &platforms[WL500GD];
1015        }
1016
1017        if (!strcmp(boardnum, "10496"))
1018            return &platforms[USR5461];
1019
1020        if (!strcmp(getvar("boardtype"), "0x0101") && !strcmp(getvar("boardrev"), "0x10")) /* SE505V2 With Modified CFE */
1021            return &platforms[SE505V2];
1022
1023        if (!strcmp(boardtype, "0x048e") && !strcmp(getvar("boardrev"),"0x35") &&
1024                !strcmp(getvar("boardflags"), "0x750")) /* D-Link DIR-320 */
1025            return &platforms[DIR320];
1026
1027        if (!strncmp(boardnum, "TH",2) && !strcmp(boardtype,"0x042f")) {
1028            return &platforms[WDNetCenter];
1029        }
1030
1031    } else { /* PMON based - old stuff */
1032        if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
1033            (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
1034            return &platforms[WR850GV1];
1035        }
1036        if (startswith(boardtype, "bcm94710dev")) {
1037            if (!strcmp(boardnum, "42"))
1038                return &platforms[WRT54GV1];
1039            if (simple_strtoul(boardnum, NULL, 0) == 2)
1040                return &platforms[WAP54GV1];
1041        }
1042        /* MN-700 has also hardware_version 'WL500-...', so use boardnum */
1043        if (startswith(getvar("hardware_version"), "WL500-")) {
1044            if (!strcmp(getvar("boardnum"), "mn700"))
1045                return &platforms[MN700];
1046            else
1047                return &platforms[WL500G];
1048        }
1049        if (startswith(getvar("hardware_version"), "WL300-")) {
1050            /* Either WL-300g or WL-HDD, do more extensive checks */
1051            if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1052                (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
1053                return &platforms[WLHDD];
1054            if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
1055                (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
1056                return &platforms[WL300G];
1057        }
1058        /* Sitecom WL-105b */
1059        if (startswith(boardnum, "2") && simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 1)
1060            return &platforms[WL105B];
1061
1062        /* unknown asus stuff, probably bcm4702 */
1063        if (startswith(boardnum, "asusX"))
1064            return &platforms[ASUS_4702];
1065
1066        /* bcm4702 based Askey RT210W clones, Including:
1067         * Askey RT210W (duh?)
1068         * Siemens SE505v1
1069         * Belkin F5D7230-4 before version v1444 (MiniPCI slot, not integrated)
1070         */
1071        if (!strcmp(boardtype,"bcm94710r4")
1072         && !strcmp(boardnum ,"100")
1073         && !strcmp(getvar("pmon_ver"),"v1.03.12.bk")
1074           ){
1075            return &platforms[RT210W];
1076        }
1077    }
1078
1079    if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
1080        if (startswith(boardtype, "bcm94710ap"))
1081            return &platforms[BUFFALO_UNKNOWN_4710];
1082        else
1083            return &platforms[BUFFALO_UNKNOWN];
1084    }
1085
1086    if (startswith(getvar("CFEver"), "MotoWRv2") ||
1087        startswith(getvar("CFEver"), "MotoWRv3") ||
1088        !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
1089
1090        return &platforms[WR850GV2V3];
1091    }
1092
1093    if (!strcmp(boardnum, "44") && !strcmp(getvar("boardflags"),"0x0388")) { /* Trendware TEW-411BRP+ */
1094        return &platforms[TEW411BRPP];
1095    }
1096
1097    if (startswith(boardnum, "04FN")) /* SimpleTech SimpleShare */
1098        return &platforms[STI_NAS];
1099
1100    if (!strcmp(getvar("boardnum"), "10") && !strcmp(getvar("boardrev"), "0x13")) /* D-Link DWL-3150 */
1101        return &platforms[DWL3150];
1102
1103    /* not found */
1104    return NULL;
1105}
1106
1107static void register_buttons(struct button_t *b)
1108{
1109    for (; b->name; b++)
1110        platform.button_mask |= b->gpio;
1111
1112    platform.button_mask &= ~gpiomask;
1113
1114    gpio_outen(platform.button_mask, 0);
1115    gpio_control(platform.button_mask, 0);
1116    platform.button_polarity = gpio_in() & platform.button_mask;
1117    gpio_intpolarity(platform.button_mask, platform.button_polarity);
1118    gpio_setintmask(platform.button_mask, platform.button_mask);
1119
1120    gpio_set_irqenable(1, button_handler);
1121}
1122
1123static void unregister_buttons(struct button_t *b)
1124{
1125    gpio_setintmask(platform.button_mask, 0);
1126
1127    gpio_set_irqenable(0, button_handler);
1128}
1129
1130
1131#ifndef LINUX_2_4
1132static void add_msg(struct event_t *event, char *msg, int argv)
1133{
1134    char *s;
1135
1136    if (argv)
1137        return;
1138
1139    s = skb_put(event->skb, strlen(msg) + 1);
1140    strcpy(s, msg);
1141}
1142
1143static void hotplug_button(struct work_struct *work)
1144{
1145    struct event_t *event = container_of(work, struct event_t, wq);
1146    char *s;
1147
1148    if (!uevent_sock)
1149        return;
1150
1151    event->skb = alloc_skb(2048, GFP_KERNEL);
1152
1153    s = skb_put(event->skb, strlen(event->action) + 2);
1154    sprintf(s, "%s@", event->action);
1155    fill_event(event);
1156
1157    NETLINK_CB(event->skb).dst_group = 1;
1158    netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
1159
1160    kfree(event);
1161}
1162
1163#else /* !LINUX_2_4 */
1164static inline char *kzalloc(unsigned int size, unsigned int gfp)
1165{
1166    char *p;
1167
1168    p = kmalloc(size, gfp);
1169    if (p == NULL)
1170        return NULL;
1171
1172    memset(p, 0, size);
1173
1174    return p;
1175}
1176
1177static void add_msg(struct event_t *event, char *msg, int argv)
1178{
1179    if (argv)
1180        event->argv[event->anr++] = event->scratch;
1181    else
1182        event->envp[event->enr++] = event->scratch;
1183
1184    event->scratch += sprintf(event->scratch, "%s", msg) + 1;
1185}
1186
1187static void hotplug_button(struct event_t *event)
1188{
1189    char *scratch = kzalloc(256, GFP_KERNEL);
1190    event->scratch = scratch;
1191
1192    add_msg(event, hotplug_path, 1);
1193    add_msg(event, "button", 1);
1194    fill_event(event);
1195    call_usermodehelper (event->argv[0], event->argv, event->envp);
1196    kfree(scratch);
1197    kfree(event);
1198}
1199#endif /* !LINUX_2_4 */
1200
1201static int fill_event (struct event_t *event)
1202{
1203    static char buf[128];
1204
1205    add_msg(event, "HOME=/", 0);
1206    add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
1207    add_msg(event, "SUBSYSTEM=button", 0);
1208    snprintf(buf, 128, "ACTION=%s", event->action);
1209    add_msg(event, buf, 0);
1210    snprintf(buf, 128, "BUTTON=%s", event->name);
1211    add_msg(event, buf, 0);
1212    snprintf(buf, 128, "SEEN=%ld", event->seen);
1213    add_msg(event, buf, 0);
1214#ifndef LINUX_2_4
1215    snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
1216    add_msg(event, buf, 0);
1217#endif
1218
1219    return 0;
1220}
1221
1222
1223#ifndef LINUX_2_4
1224static irqreturn_t button_handler(int irq, void *dev_id)
1225#else
1226static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
1227#endif
1228{
1229    struct button_t *b;
1230    u32 in, changed;
1231
1232    in = gpio_in() & platform.button_mask;
1233    gpio_intpolarity(platform.button_mask, in);
1234    changed = platform.button_polarity ^ in;
1235    platform.button_polarity = in;
1236
1237    changed &= ~gpio_outen(0, 0);
1238
1239    for (b = platform.buttons; b->name; b++) {
1240        struct event_t *event;
1241
1242        if (!(b->gpio & changed)) continue;
1243
1244        b->pressed ^= 1;
1245
1246        if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
1247            event->seen = (jiffies - b->seen)/HZ;
1248            event->name = b->name;
1249            event->action = b->pressed ? "pressed" : "released";
1250#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
1251            INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
1252#else
1253            INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
1254#endif
1255            schedule_work(&event->wq);
1256        }
1257
1258        b->seen = jiffies;
1259    }
1260    return IRQ_HANDLED;
1261}
1262
1263static void register_leds(struct led_t *l)
1264{
1265    struct proc_dir_entry *p;
1266    u32 mask = 0;
1267    u32 oe_mask = 0;
1268    u32 val = 0;
1269
1270    leds = proc_mkdir("led", diag);
1271    if (!leds)
1272        return;
1273
1274    for(; l->name; l++) {
1275        if (l->gpio & gpiomask)
1276            continue;
1277
1278        if (l->gpio & GPIO_TYPE_EXTIF) {
1279            l->state = 0;
1280            set_led_extif(l);
1281        } else {
1282            if (l->polarity != INPUT) oe_mask |= l->gpio;
1283            mask |= l->gpio;
1284            val |= (l->polarity == NORMAL)?0:l->gpio;
1285        }
1286
1287        if (l->polarity == INPUT) continue;
1288
1289        if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
1290            l->proc.type = PROC_LED;
1291            l->proc.ptr = l;
1292            p->data = (void *) &l->proc;
1293            p->proc_fops = &diag_proc_fops;
1294        }
1295    }
1296
1297    gpio_outen(mask, oe_mask);
1298    gpio_control(mask, 0);
1299    gpio_out(mask, val);
1300    gpio_setintmask(mask, 0);
1301}
1302
1303static void unregister_leds(struct led_t *l)
1304{
1305    for(; l->name; l++)
1306        remove_proc_entry(l->name, leds);
1307
1308    remove_proc_entry("led", diag);
1309}
1310
1311static void set_led_extif(struct led_t *led)
1312{
1313    gpio_set_extif(led->gpio, led->state);
1314}
1315
1316static void led_flash(unsigned long dummy) {
1317    struct led_t *l;
1318    u32 mask = 0;
1319    u8 extif_blink = 0;
1320
1321    for (l = platform.leds; l->name; l++) {
1322        if (l->flash) {
1323            if (l->gpio & GPIO_TYPE_EXTIF) {
1324                extif_blink = 1;
1325                l->state = !l->state;
1326                set_led_extif(l);
1327            } else {
1328                mask |= l->gpio;
1329            }
1330        }
1331    }
1332
1333    mask &= ~gpiomask;
1334    if (mask) {
1335        u32 val = ~gpio_in();
1336
1337        gpio_outen(mask, mask);
1338        gpio_control(mask, 0);
1339        gpio_out(mask, val);
1340    }
1341    if (mask || extif_blink) {
1342        mod_timer(&led_timer, jiffies + FLASH_TIME);
1343    }
1344}
1345
1346static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1347{
1348#ifdef LINUX_2_4
1349    struct inode *inode = file->f_dentry->d_inode;
1350    struct proc_dir_entry *dent = inode->u.generic_ip;
1351#else
1352    struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1353#endif
1354    char *page;
1355    int len = 0;
1356
1357    if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
1358        return -ENOBUFS;
1359
1360    if (dent->data != NULL) {
1361        struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1362        switch (handler->type) {
1363            case PROC_LED: {
1364                struct led_t * led = (struct led_t *) handler->ptr;
1365                if (led->flash) {
1366                    len = sprintf(page, "f\n");
1367                } else {
1368                    if (led->gpio & GPIO_TYPE_EXTIF) {
1369                        len = sprintf(page, "%d\n", led->state);
1370                    } else {
1371                        u32 in = (gpio_in() & led->gpio ? 1 : 0);
1372                        u8 p = (led->polarity == NORMAL ? 0 : 1);
1373                        len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
1374                    }
1375                }
1376                break;
1377            }
1378            case PROC_MODEL:
1379                len = sprintf(page, "%s\n", platform.name);
1380                break;
1381            case PROC_GPIOMASK:
1382                len = sprintf(page, "0x%04x\n", gpiomask);
1383                break;
1384        }
1385    }
1386    len += 1;
1387
1388    if (*ppos < len) {
1389        len = min_t(int, len - *ppos, count);
1390        if (copy_to_user(buf, (page + *ppos), len)) {
1391            kfree(page);
1392            return -EFAULT;
1393        }
1394        *ppos += len;
1395    } else {
1396        len = 0;
1397    }
1398
1399    kfree(page);
1400    return len;
1401}
1402
1403
1404static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1405{
1406#ifdef LINUX_2_4
1407    struct inode *inode = file->f_dentry->d_inode;
1408    struct proc_dir_entry *dent = inode->u.generic_ip;
1409#else
1410    struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1411#endif
1412    char *page;
1413    int ret = -EINVAL;
1414
1415    if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1416        return -ENOBUFS;
1417
1418    if (copy_from_user(page, buf, count)) {
1419        kfree(page);
1420        return -EINVAL;
1421    }
1422    page[count] = 0;
1423
1424    if (dent->data != NULL) {
1425        struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1426        switch (handler->type) {
1427            case PROC_LED: {
1428                struct led_t *led = (struct led_t *) handler->ptr;
1429                int p = (led->polarity == NORMAL ? 0 : 1);
1430
1431                if (page[0] == 'f') {
1432                    led->flash = 1;
1433                    led_flash(0);
1434                } else {
1435                    led->flash = 0;
1436                    if (led->gpio & GPIO_TYPE_EXTIF) {
1437                        led->state = p ^ ((page[0] == '1') ? 1 : 0);
1438                        set_led_extif(led);
1439                    } else {
1440                        gpio_outen(led->gpio, led->gpio);
1441                        gpio_control(led->gpio, 0);
1442                        gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1443                    }
1444                }
1445                break;
1446            }
1447            case PROC_GPIOMASK:
1448                gpiomask = simple_strtoul(page, NULL, 0);
1449
1450                if (platform.buttons) {
1451                    unregister_buttons(platform.buttons);
1452                    register_buttons(platform.buttons);
1453                }
1454
1455                if (platform.leds) {
1456                    unregister_leds(platform.leds);
1457                    register_leds(platform.leds);
1458                }
1459                break;
1460        }
1461        ret = count;
1462    }
1463
1464    kfree(page);
1465    return ret;
1466}
1467
1468static int __init diag_init(void)
1469{
1470    static struct proc_dir_entry *p;
1471    static struct platform_t *detected;
1472
1473    detected = platform_detect();
1474    if (!detected) {
1475        printk(MODULE_NAME ": Router model not detected.\n");
1476        return -ENODEV;
1477    }
1478    memcpy(&platform, detected, sizeof(struct platform_t));
1479
1480    printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1481    if (platform.platform_init != NULL) {
1482        platform.platform_init();
1483    }
1484
1485    if (!(diag = proc_mkdir("diag", NULL))) {
1486        printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1487        return -EINVAL;
1488    }
1489
1490    if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1491        p->data = (void *) &proc_model;
1492        p->proc_fops = &diag_proc_fops;
1493    }
1494
1495    if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1496        p->data = (void *) &proc_gpiomask;
1497        p->proc_fops = &diag_proc_fops;
1498    }
1499
1500    if (platform.buttons)
1501        register_buttons(platform.buttons);
1502
1503    if (platform.leds)
1504        register_leds(platform.leds);
1505
1506    return 0;
1507}
1508
1509static void __exit diag_exit(void)
1510{
1511    del_timer(&led_timer);
1512
1513    if (platform.buttons)
1514        unregister_buttons(platform.buttons);
1515
1516    if (platform.leds)
1517        unregister_leds(platform.leds);
1518
1519    remove_proc_entry("model", diag);
1520    remove_proc_entry("gpiomask", diag);
1521    remove_proc_entry("diag", NULL);
1522}
1523
1524module_init(diag_init);
1525module_exit(diag_exit);
1526
1527MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1528MODULE_LICENSE("GPL");
1529

Archive Download this file



interactive