improved au1000 usb patch
[openwrt.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  * Copyright (C) 2006-2007 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 #ifndef LINUX_2_4
32 #include <linux/workqueue.h>
33 #include <linux/skbuff.h>
34 #include <linux/netlink.h>
35 #include <net/sock.h>
36 extern struct sock *uevent_sock;
37 extern 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
49 static int fill_event(struct event_t *);
50 static unsigned int gpiomask = 0;
51 module_param(gpiomask, int, 0644);
52
53 enum {
54         /* Linksys */
55         WAP54GV1,
56         WAP54GV3,
57         WRT54GV1,
58         WRT54G,
59         WRTSL54GS,
60         WRT54G3G,
61         WRT350N,
62         
63         /* ASUS */
64         WLHDD,
65         WL300G,
66         WL500G,
67         WL500GD,
68         WL500GP,
69         WL500W,
70         ASUS_4702,
71         WL700GE,
72         
73         /* Buffalo */
74         WBR2_G54,
75         WHR_G54S,
76         WHR_HP_G54,
77         WHR2_A54G54,
78         WLA2_G54L,
79         WZR_G300N,
80         WZR_RS_G54,
81         WZR_RS_G54HP,
82         BUFFALO_UNKNOWN,
83         BUFFALO_UNKNOWN_4710,
84
85         /* Siemens */
86         SE505V1,
87         SE505V2,
88         
89         /* US Robotics */
90         USR5461,
91
92         /* Dell */
93         TM2300,
94
95         /* Motorola */
96         WE800G,
97         WR850GV1,
98         WR850GV2V3,
99         WR850GP,
100
101         /* Belkin */
102         BELKIN_UNKNOWN,
103
104         /* Netgear */
105         WGT634U,
106
107         /* Trendware */
108         TEW411BRPP,
109         
110         /* SimpleTech */
111         STI_NAS,
112
113         /* D-Link */
114         DIR130,
115         DIR330,
116 };
117
118 static void __init bcm4780_init(void) {
119                 int pin = 1 << 3;
120
121                 /* Enables GPIO 3 that controls HDD and led power on ASUS WL-700gE */
122                 printk(MODULE_NAME ": Spinning up HDD and enabling leds\n");
123                 gpio_outen(pin, pin);
124                 gpio_control(pin, 0);
125                 gpio_out(pin, pin);
126
127                 /* Wait 5s, so the HDD can spin up */
128                 set_current_state(TASK_INTERRUPTIBLE);
129                 schedule_timeout(HZ * 5);
130 }
131
132 static struct platform_t __initdata platforms[] = {
133         /* Linksys */
134         [WAP54GV1] = {
135                 .name           = "Linksys WAP54G V1",
136                 .buttons        = {
137                         { .name = "reset",      .gpio = 1 << 0 },
138                 },
139                 .leds           = { 
140                         { .name = "diag",       .gpio = 1 << 3 },
141                         { .name = "wlan",       .gpio = 1 << 4 },
142                 },
143         },
144         [WAP54GV3] = {
145                 .name           = "Linksys WAP54G V3",
146                 .buttons        = {
147                         /* FIXME: verify this */
148                         { .name = "reset",      .gpio = 1 << 7 },
149                         { .name = "ses",        .gpio = 1 << 0 },
150                 },
151                 .leds           = { 
152                         /* FIXME: diag? */
153                         { .name = "ses",        .gpio = 1 << 1 },
154                 },
155         },
156         [WRT54GV1] = {
157                 .name           = "Linksys WRT54G V1.x",
158                 .buttons        = {
159                         { .name = "reset",      .gpio = 1 << 6 },
160                 },
161                 .leds           = { 
162                         { .name = "diag",       .gpio = 0x13 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
163                         { .name = "dmz",        .gpio = 0x12 | GPIO_TYPE_EXTIF, .polarity = NORMAL },
164                 },
165         },
166         [WRT54G] = {
167                 .name           = "Linksys WRT54G/GS/GL",
168                 .buttons        = {
169                         { .name = "reset",      .gpio = 1 << 6 },
170                         { .name = "ses",        .gpio = 1 << 4 },
171                 },
172                 .leds           = {
173                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
174                         { .name = "dmz",        .gpio = 1 << 7, .polarity = REVERSE },
175                         { .name = "ses_white",  .gpio = 1 << 2, .polarity = REVERSE },
176                         { .name = "ses_orange", .gpio = 1 << 3, .polarity = REVERSE },
177                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
178                 },
179         },
180         [WRTSL54GS] = {
181                 .name           = "Linksys WRTSL54GS",
182                 .buttons        = {
183                         { .name = "reset",      .gpio = 1 << 6 },
184                         { .name = "ses",        .gpio = 1 << 4 },
185                 },
186                 .leds           = {
187                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
188                         { .name = "dmz",        .gpio = 1 << 0, .polarity = REVERSE },
189                         { .name = "ses_white",  .gpio = 1 << 5, .polarity = REVERSE },
190                         { .name = "ses_orange", .gpio = 1 << 7, .polarity = REVERSE },
191                 },
192         },
193         [WRT54G3G] = {
194                 .name           = "Linksys WRT54G3G",
195                 .buttons        = {
196                         { .name = "reset",      .gpio = 1 << 6 },
197                         { .name = "3g",         .gpio = 1 << 4 },
198                 },
199                 .leds           = {
200                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
201                         { .name = "dmz",        .gpio = 1 << 7, .polarity = REVERSE },
202                         { .name = "3g_green",   .gpio = 1 << 2, .polarity = NORMAL },
203                         { .name = "3g_blue",    .gpio = 1 << 3, .polarity = NORMAL },
204                         { .name = "3g_blink",   .gpio = 1 << 5, .polarity = NORMAL },
205                 },
206         },
207         [WRT350N] = {
208                 .name           = "Linksys WRT350N",
209                 .buttons        = {
210                         { .name = "reset",      .gpio = 1 << 6 },
211                         { .name = "ses",        .gpio = 1 << 8 },
212                 },
213                 .leds           = {
214                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
215                         { .name = "ses",        .gpio = 1 << 3, .polarity = REVERSE },
216                 },
217         },
218         /* Asus */
219         [WLHDD] = {
220                 .name           = "ASUS WL-HDD",
221                 .buttons        = {
222                         { .name = "reset",      .gpio = 1 << 6 },
223                 },
224                 .leds           = {
225                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
226                         { .name = "usb",        .gpio = 1 << 2, .polarity = NORMAL },
227                 },
228         },
229         [WL300G] = {
230                 .name           = "ASUS WL-300g",
231                 .buttons        = {
232                         { .name = "reset",      .gpio = 1 << 6 },
233                 },
234                 .leds           = {
235                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
236                 },
237         },
238         [WL500G] = {
239                 .name           = "ASUS WL-500g",
240                 .buttons        = {
241                         { .name = "reset",      .gpio = 1 << 6 },
242                 },
243                 .leds           = {
244                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
245                 },
246         },
247         [WL500GD] = {
248                 .name           = "ASUS WL-500g Deluxe",
249                 .buttons        = {
250                         { .name = "reset",      .gpio = 1 << 6 },
251                 },
252                 .leds           = {
253                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
254                 },
255         },
256         [WL500GP] = {
257                 .name           = "ASUS WL-500g Premium",
258                 .buttons        = {
259                         { .name = "reset",      .gpio = 1 << 0 },
260                         { .name = "ses",        .gpio = 1 << 4 },
261                 },
262                 .leds           = {
263                         { .name = "power",      .gpio = 1 << 1, .polarity = REVERSE },
264                 },
265         },
266         [WL500W] = {
267                 .name           = "ASUS WL-500W",
268                 .buttons        = {
269                         { .name = "reset",      .gpio = 1 << 6 },
270                         { .name = "ses",        .gpio = 1 << 7 },
271                 },
272                 .leds           = {
273                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
274                 },
275         },
276         [ASUS_4702] = {
277                 .name           = "ASUS (unknown, BCM4702)",
278                 .buttons        = {
279                         { .name = "reset",      .gpio = 1 << 6 },
280                 },
281                 .leds           = {
282                         { .name = "power",      .gpio = 1 << 0, .polarity = REVERSE },
283                 },
284         },
285         [WL700GE] = {
286                 .name           = "ASUS WL-700gE",
287                 .buttons        = {
288                         { .name = "reset",      .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
289                         { .name = "ses",        .gpio = 1 << 4 }, // on back, actual name ezsetup
290                         { .name = "power",      .gpio = 1 << 0 }, // on front
291                         { .name = "copy",       .gpio = 1 << 6 }, // on front
292                 },
293                 .leds           = {
294 #if 0
295                         // GPIO that controls power led also enables/disables some essential functions
296                         // - power to HDD
297                         // - switch leds
298                         { .name = "power",      .gpio = 1 << 3, .polarity = NORMAL },  // actual name power
299 #endif
300                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
301                 },
302                 .platform_init = bcm4780_init,
303         },
304         /* Buffalo */
305         [WHR_G54S] = {
306                 .name           = "Buffalo WHR-G54S",
307                 .buttons        = {
308                         { .name = "reset",      .gpio = 1 << 4 },
309                         { .name = "bridge",     .gpio = 1 << 5 },
310                         { .name = "ses",        .gpio = 1 << 0 },
311                 },
312                 .leds           = {
313                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
314                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
315                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
316                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
317                         { .name = "wlan",       .gpio = 1 << 2, .polarity = REVERSE },
318                 },
319         },
320         [WBR2_G54] = {
321                 .name           = "Buffalo WBR2-G54",
322                 /* FIXME: verify */
323                 .buttons        = {
324                         { .name = "reset",      .gpio = 1 << 7 },
325                 },
326                 .leds           = {
327                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
328                 },
329         },
330         [WHR_HP_G54] = {
331                 .name           = "Buffalo WHR-HP-G54",
332                 .buttons        = {
333                         { .name = "reset",      .gpio = 1 << 4 },
334                         { .name = "bridge",     .gpio = 1 << 5 },
335                         { .name = "ses",        .gpio = 1 << 0 },
336                 },
337                 .leds           = {
338                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
339                         { .name = "internal",   .gpio = 1 << 3, .polarity = REVERSE },
340                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
341                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
342                         { .name = "wlan",       .gpio = 1 << 2, .polarity = REVERSE },
343                 },
344         },
345         [WHR2_A54G54] = {
346                 .name           = "Buffalo WHR2-A54G54",
347                 .buttons        = {
348                         { .name = "reset",      .gpio = 1 << 4 },
349                 },
350                 .leds           = {
351                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
352                 },
353         },
354         [WLA2_G54L] = {
355                 .name           = "Buffalo WLA2-G54L",
356                 /* FIXME: verify */
357                 .buttons        = {
358                         { .name = "reset",      .gpio = 1 << 7 },
359                 },
360                 .leds           = {
361                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
362                 },
363         },
364         [WZR_G300N] = {
365                 .name           = "Buffalo WZR-G300N",
366                 .buttons        = {
367                         { .name = "reset",      .gpio = 1 << 4 },
368                 },
369                 .leds           = {
370                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
371                         { .name = "bridge",     .gpio = 1 << 1, .polarity = REVERSE },
372                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
373                 },
374         },
375         [WZR_RS_G54] = {
376                 .name           = "Buffalo WZR-RS-G54",
377                 .buttons        = {
378                         { .name = "ses",        .gpio = 1 << 0 },
379                         { .name = "reset",      .gpio = 1 << 4 },
380                 },
381                 .leds           = {
382                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
383                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
384                         { .name = "vpn",        .gpio = 1 << 1, .polarity = REVERSE },
385                 },
386         },
387         [WZR_RS_G54HP] = {
388                 .name           = "Buffalo WZR-RS-G54HP",
389                 .buttons        = {
390                         { .name = "ses",        .gpio = 1 << 0 },
391                         { .name = "reset",      .gpio = 1 << 4 },
392                 },
393                 .leds           = {
394                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
395                         { .name = "ses",        .gpio = 1 << 6, .polarity = REVERSE },
396                         { .name = "vpn",        .gpio = 1 << 1, .polarity = REVERSE },
397                 },
398         },
399         [BUFFALO_UNKNOWN] = {
400                 .name           = "Buffalo (unknown)",
401                 .buttons        = {
402                         { .name = "reset",      .gpio = 1 << 7 },
403                 },
404                 .leds           = {
405                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
406                 },
407         },
408         [BUFFALO_UNKNOWN_4710] = {
409                 .name           = "Buffalo (unknown, BCM4710)",
410                 .buttons        = {
411                         { .name = "reset",      .gpio = 1 << 4 },
412                 },
413                 .leds           = {
414                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE },
415                 },
416         },
417         /* Siemens */
418         [SE505V1] = {
419                 .name           = "Siemens SE505 V1",
420                 .buttons        = {
421                         /* No usable buttons */
422                 },
423                 .leds           = {
424                         { .name = "dmz",        .gpio = 1 << 4, .polarity = REVERSE },
425                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
426                 },
427         },
428         [SE505V2] = {
429                 .name           = "Siemens SE505 V2",
430                 .buttons        = {
431                         /* No usable buttons */
432                 },
433                 .leds           = {
434                         { .name = "power",      .gpio = 1 << 5, .polarity = REVERSE },
435                         { .name = "dmz",        .gpio = 1 << 0, .polarity = REVERSE },
436                         { .name = "wlan",       .gpio = 1 << 3, .polarity = REVERSE },
437                 },
438         },
439         /* US Robotics */
440         [USR5461] = {
441                 .name           = "U.S. Robotics USR5461",
442                 .buttons        = {
443                         /* No usable buttons */
444                 },
445                 .leds           = {
446                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
447                         { .name = "printer",    .gpio = 1 << 1, .polarity = REVERSE },
448                 },
449         },
450         /* Dell */
451         [TM2300] = {
452                 .name           = "Dell TrueMobile 2300",
453                 .buttons        = {
454                         { .name = "reset",      .gpio = 1 << 0 },
455                 },
456                 .leds           = {
457                         { .name = "wlan",       .gpio = 1 << 6, .polarity = REVERSE },
458                         { .name = "power",      .gpio = 1 << 7, .polarity = REVERSE },
459                 },
460         },
461         /* Motorola */
462         [WE800G] = {
463                 .name           = "Motorola WE800G",
464                 .buttons        = {
465                         { .name = "reset",      .gpio = 1 << 0 },
466                 },
467                 .leds           = {
468                         { .name = "power",      .gpio = 1 << 4, .polarity = NORMAL },
469                         { .name = "diag",       .gpio = 1 << 2, .polarity = REVERSE },
470                         { .name = "wlan_amber", .gpio = 1 << 1, .polarity = NORMAL },
471                 },
472         },
473         [WR850GV1] = {
474                 .name           = "Motorola WR850G V1",
475                 .buttons        = {
476                         { .name = "reset",      .gpio = 1 << 0 },
477                 },
478                 .leds           = {
479                         { .name = "power",      .gpio = 1 << 4, .polarity = NORMAL },
480                         { .name = "diag",       .gpio = 1 << 3, .polarity = REVERSE },
481                         { .name = "dmz",        .gpio = 1 << 6, .polarity = NORMAL },
482                         { .name = "wlan_red",   .gpio = 1 << 5, .polarity = REVERSE },
483                         { .name = "wlan_green", .gpio = 1 << 7, .polarity = REVERSE },
484                 },
485         },
486         [WR850GV2V3] = {
487                 .name           = "Motorola WR850G V2/V3",
488                 .buttons        = {
489                         { .name = "reset",      .gpio = 1 << 5 },
490                 },
491                 .leds           = {
492                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
493                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
494                         { .name = "dmz",        .gpio = 1 << 6, .polarity = REVERSE },
495                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
496                 },
497         },
498         [WR850GP] = {
499                 .name           = "Motorola WR850GP",
500                 .buttons        = {
501                         { .name = "reset",      .gpio = 1 << 5 },
502                 },
503                 .leds           = {
504                         { .name = "power",      .gpio = 1 << 1, .polarity = NORMAL },
505                         { .name = "wlan",       .gpio = 1 << 0, .polarity = REVERSE },
506                         { .name = "dmz",        .gpio = 1 << 6, .polarity = REVERSE },
507                         { .name = "diag",       .gpio = 1 << 7, .polarity = REVERSE },
508                 },
509         },
510
511         /* Belkin */
512         [BELKIN_UNKNOWN] = {
513                 .name           = "Belkin (unknown)",
514                 /* FIXME: verify & add detection */
515                 .buttons        = {
516                         { .name = "reset",      .gpio = 1 << 7 },
517                 },
518                 .leds           = {
519                         { .name = "power",      .gpio = 1 << 5, .polarity = NORMAL },
520                         { .name = "wlan",       .gpio = 1 << 3, .polarity = NORMAL },
521                         { .name = "connected",  .gpio = 1 << 0, .polarity = NORMAL },
522                 },
523         },
524         /* Netgear */
525         [WGT634U] = {
526                 .name           = "Netgear WGT634U",
527                 .buttons        = {
528                         { .name = "reset",      .gpio = 1 << 2 },
529                 },
530                 .leds           = {
531                         { .name = "power",      .gpio = 1 << 3, .polarity = NORMAL },
532                 },
533         },
534         /* Trendware */
535         [TEW411BRPP] = {
536                 .name           = "Trendware TEW411BRP+",
537                 .buttons        = {
538                         { /* No usable buttons */ },
539                 },
540                 .leds           = {
541                         { .name = "power",      .gpio = 1 << 7, .polarity = NORMAL },
542                         { .name = "wlan",       .gpio = 1 << 1, .polarity = NORMAL },
543                         { .name = "bridge",     .gpio = 1 << 6, .polarity = NORMAL },
544                 },
545         },
546         /* SimpleTech */
547         [STI_NAS] = {
548                 .name      = "SimpleTech SimpleShare NAS",
549                 .buttons        = {
550                         { .name = "reset",      .gpio = 1 << 7 }, // on back, hardwired, always resets device regardless OS state
551                         { .name = "power",      .gpio = 1 << 0 }, // on back
552                 },
553                 .leds      = {
554                         { .name = "diag",       .gpio = 1 << 1, .polarity = REVERSE }, // actual name ready
555                 },
556                 .platform_init = bcm4780_init,
557         },
558         /* D-Link */
559         [DIR130] = {
560                 .name     = "D-Link DIR-130",
561                 .buttons        = {
562                         { .name = "reset",      .gpio = 1 << 3},
563                         { .name = "reserved",   .gpio = 1 << 7},
564                 },
565                 .leds      = {
566                         { .name = "diag",       .gpio = 1 << 0},
567                         { .name = "blue",       .gpio = 1 << 6},
568                 },
569         },
570         [DIR330] = {
571                 .name     = "D-Link DIR-330",
572                 .buttons        = {
573                         { .name = "reset",      .gpio = 1 << 3},
574                         { .name = "reserved",   .gpio = 1 << 7},
575                 },
576                 .leds      = {
577                         { .name = "diag",       .gpio = 1 << 0},
578                         { .name = "usb",        .gpio = 1 << 4},
579                         { .name = "blue",       .gpio = 1 << 6},
580                 },
581         },
582 };
583
584 static struct platform_t __init *platform_detect(void)
585 {
586         char *boardnum, *boardtype, *buf;
587
588         if (strcmp(getvar("nvram_type"), "cfe") == 0)
589                 return &platforms[WGT634U];
590
591         /* Look for a model identifier */
592
593         /* Based on "model_name" */
594         if (buf = nvram_get("model_name")) {
595                 if (!strcmp(buf, "DIR-130"))
596                         return &platforms[DIR130];
597                 if (!strcmp(buf, "DIR-330"))
598                         return &platforms[DIR330];
599         }
600
601         /* Based on "model_no" */
602         if (buf = nvram_get("model_no")) {
603                 if (!strncmp(buf,"WL700", 5)) /* WL700* */
604                         return &platforms[WL700GE];
605         }
606
607         /* Based on "ModelId" */
608         if (buf = nvram_get("ModelId")) {
609                 if (!strcmp(buf, "WR850GP"))
610                         return &platforms[WR850GP];
611                 if (!strcmp(buf,"WX-5565"))
612                         return &platforms[TM2300];
613                 if (!strncmp(buf,"WE800G", 6)) /* WE800G* */
614                         return &platforms[WE800G];
615         }
616
617         /* Buffalo */
618         if ((buf = (nvram_get("melco_id") ?: nvram_get("buffalo_id")))) {
619                 /* Buffalo hardware, check id for specific hardware matches */
620                 if (!strcmp(buf, "29bb0332"))
621                         return &platforms[WBR2_G54];
622                 if (!strcmp(buf, "29129"))
623                         return &platforms[WLA2_G54L];
624                 if (!strcmp(buf, "30189"))
625                         return &platforms[WHR_HP_G54];
626                 if (!strcmp(buf, "30182"))
627                         return &platforms[WHR_G54S];
628                 if (!strcmp(buf, "290441dd"))
629                         return &platforms[WHR2_A54G54];
630                 if (!strcmp(buf, "31120"))
631                         return &platforms[WZR_G300N];
632                 if (!strcmp(buf, "30083"))
633                         return &platforms[WZR_RS_G54];
634                 if (!strcmp(buf, "30103"))
635                         return &platforms[WZR_RS_G54HP];
636         }
637
638         /* no easy model number, attempt to guess */
639         boardnum = getvar("boardnum");
640         boardtype = getvar("boardtype");
641
642         if (strncmp(getvar("pmon_ver"), "CFE", 3) == 0) {
643                 /* CFE based - newer hardware */
644                 if (!strcmp(boardnum, "42")) { /* Linksys */
645                         if (!strcmp(boardtype, "0x478") && !strcmp(getvar("cardbus"), "1"))
646                                 return &platforms[WRT350N];
647
648                         if (!strcmp(boardtype, "0x0101") && !strcmp(getvar("boot_ver"), "v3.6"))
649                                 return &platforms[WRT54G3G];
650
651                         if (!strcmp(getvar("et1phyaddr"),"5") && !strcmp(getvar("et1mdcport"), "1"))
652                                 return &platforms[WRTSL54GS];
653                         
654                         /* default to WRT54G */
655                         return &platforms[WRT54G];
656                 }
657                 
658                 if (!strcmp(boardnum, "45")) { /* ASUS */
659                         if (!strcmp(boardtype,"0x042f"))
660                                 return &platforms[WL500GP];
661                         else if (!strcmp(boardtype,"0x0472"))
662                                 return &platforms[WL500W];
663                         else
664                                 return &platforms[WL500GD];
665                 }
666                 
667                 if (!strcmp(boardnum, "10496"))
668                         return &platforms[USR5461];
669
670         } else { /* PMON based - old stuff */
671                 if ((simple_strtoul(getvar("GemtekPmonVer"), NULL, 0) == 9) &&
672                         (simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 30)) {
673                         return &platforms[WR850GV1];
674                 }
675                 if (!strncmp(boardtype, "bcm94710dev", 11)) {
676                         if (!strcmp(boardnum, "42"))
677                                 return &platforms[WRT54GV1];
678                         if (simple_strtoul(boardnum, NULL, 0) == 2)
679                                 return &platforms[WAP54GV1];
680                 }
681                 if (!strncmp(getvar("hardware_version"), "WL500-", 6))
682                         return &platforms[WL500G];
683                 if (!strncmp(getvar("hardware_version"), "WL300-", 6)) {
684                         /* Either WL-300g or WL-HDD, do more extensive checks */
685                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
686                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1))
687                                 return &platforms[WLHDD];
688                         if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
689                                 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 10))
690                                 return &platforms[WL300G];
691                 }
692
693                 /* unknown asus stuff, probably bcm4702 */
694                 if (!strncmp(boardnum, "asusX", 5))
695                         return &platforms[ASUS_4702];
696         }
697
698         if (buf || !strcmp(boardnum, "00")) {/* probably buffalo */
699                 if (!strncmp(boardtype, "bcm94710ap", 10))
700                         return &platforms[BUFFALO_UNKNOWN_4710];
701                 else
702                         return &platforms[BUFFALO_UNKNOWN];
703         }
704
705         if (!strcmp(getvar("CFEver"), "MotoWRv203") ||
706                 !strcmp(getvar("MOTO_BOARD_TYPE"), "WR_FEM1")) {
707
708                 return &platforms[WR850GV2V3];
709         }
710
711         if (!strcmp(boardnum, "44")) {  /* Trendware TEW-411BRP+ */
712                 return &platforms[TEW411BRPP];
713         }
714
715         if (!strncmp(boardnum, "04FN52", 6)) /* SimpleTech SimpleShare */
716                 return &platforms[STI_NAS];
717
718         /* not found */
719         return NULL;
720 }
721
722 static void register_buttons(struct button_t *b)
723 {
724         for (; b->name; b++)
725                 platform.button_mask |= b->gpio;
726
727         platform.button_mask &= ~gpiomask;
728
729         gpio_outen(platform.button_mask, 0);
730         gpio_control(platform.button_mask, 0);
731         platform.button_polarity = gpio_in() & platform.button_mask;
732         gpio_intpolarity(platform.button_mask, platform.button_polarity);
733         gpio_intmask(platform.button_mask, platform.button_mask);
734
735         gpio_set_irqenable(1, button_handler);
736 }
737
738 static void unregister_buttons(struct button_t *b)
739 {
740         gpio_intmask(platform.button_mask, 0);
741
742         gpio_set_irqenable(0, button_handler);
743 }
744
745
746 #ifndef LINUX_2_4
747 static void add_msg(struct event_t *event, char *msg, int argv)
748 {
749         char *s;
750
751         if (argv)
752                 return;
753
754         s = skb_put(event->skb, strlen(msg) + 1);
755         strcpy(s, msg);
756 }
757
758 static void hotplug_button(struct work_struct *work)
759 {
760         struct event_t *event = container_of(work, struct event_t, wq);
761         char *s;
762
763         if (!uevent_sock)
764                 return;
765
766         event->skb = alloc_skb(2048, GFP_KERNEL);
767
768         s = skb_put(event->skb, strlen(event->action) + 2);
769         sprintf(s, "%s@", event->action);
770         fill_event(event);
771
772         NETLINK_CB(event->skb).dst_group = 1;
773         netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
774
775         kfree(event);
776 }
777
778 #else /* !LINUX_2_4 */
779 static inline char *kzalloc(unsigned int size, unsigned int gfp)
780 {
781         char *p;
782
783         p = kmalloc(size, gfp);
784         if (p == NULL)
785                 return NULL;
786
787         memset(p, 0, size);
788
789         return p;
790 }
791
792 static void add_msg(struct event_t *event, char *msg, int argv)
793 {
794         if (argv)
795                 event->argv[event->anr++] = event->scratch;
796         else
797                 event->envp[event->enr++] = event->scratch;
798
799         event->scratch += sprintf(event->scratch, "%s", msg) + 1;
800 }
801
802 static void hotplug_button(struct event_t *event)
803 {
804         char *scratch = kzalloc(256, GFP_KERNEL);
805         event->scratch = scratch;
806
807         add_msg(event, hotplug_path, 1);
808         add_msg(event, "button", 1);
809         fill_event(event);
810         call_usermodehelper (event->argv[0], event->argv, event->envp);
811         kfree(scratch);
812         kfree(event);
813 }
814 #endif /* !LINUX_2_4 */
815
816 static int fill_event (struct event_t *event)
817 {
818         static char buf[128];
819
820         add_msg(event, "HOME=/", 0);
821         add_msg(event, "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 0);
822         add_msg(event, "SUBSYSTEM=button", 0);
823         snprintf(buf, 128, "ACTION=%s", event->action);
824         add_msg(event, buf, 0);
825         snprintf(buf, 128, "BUTTON=%s", event->name);
826         add_msg(event, buf, 0);
827         snprintf(buf, 128, "SEEN=%ld", event->seen);
828         add_msg(event, buf, 0);
829 #ifndef LINUX_2_4
830         snprintf(buf, 128, "SEQNUM=%llu", uevent_next_seqnum());
831         add_msg(event, buf, 0);
832 #endif
833
834         return 0;
835 }
836
837
838 #ifndef LINUX_2_4
839 static irqreturn_t button_handler(int irq, void *dev_id)
840 #else
841 static irqreturn_t button_handler(int irq, void *dev_id, struct pt_regs *regs)
842 #endif
843 {
844         struct button_t *b;
845         u32 in, changed;
846
847         in = gpio_in() & platform.button_mask;
848         gpio_intpolarity(platform.button_mask, in);
849         changed = platform.button_polarity ^ in;
850         platform.button_polarity = in;
851
852         changed &= ~gpio_outen(0, 0);
853
854         for (b = platform.buttons; b->name; b++) { 
855                 struct event_t *event;
856
857                 if (!(b->gpio & changed)) continue;
858
859                 b->pressed ^= 1;
860
861                 if ((event = (struct event_t *)kzalloc (sizeof(struct event_t), GFP_ATOMIC))) {
862                         event->seen = (jiffies - b->seen)/HZ;
863                         event->name = b->name;
864                         event->action = b->pressed ? "pressed" : "released";
865 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
866                         INIT_WORK(&event->wq, (void *)(void *)hotplug_button);
867 #else
868                         INIT_WORK(&event->wq, (void *)(void *)hotplug_button, (void *)event);
869 #endif
870                         schedule_work(&event->wq);
871                 }
872
873                 b->seen = jiffies;
874         }
875         return IRQ_HANDLED;
876 }
877
878 static void register_leds(struct led_t *l)
879 {
880         struct proc_dir_entry *p;
881         u32 mask = 0;
882         u32 val = 0;
883
884         leds = proc_mkdir("led", diag);
885         if (!leds) 
886                 return;
887
888         for(; l->name; l++) {
889                 if (l->gpio & gpiomask)
890                         continue;
891         
892                 if (l->gpio & GPIO_TYPE_EXTIF) {
893                         l->state = 0;
894                         set_led_extif(l);
895                 } else {
896                         mask |= l->gpio;
897                         val |= (l->polarity == NORMAL)?0:l->gpio;
898                 }
899
900                 if ((p = create_proc_entry(l->name, S_IRUSR, leds))) {
901                         l->proc.type = PROC_LED;
902                         l->proc.ptr = l;
903                         p->data = (void *) &l->proc;
904                         p->proc_fops = &diag_proc_fops;
905                 }
906         }
907
908         gpio_outen(mask, mask);
909         gpio_control(mask, 0);
910         gpio_out(mask, val);
911 }
912
913 static void unregister_leds(struct led_t *l)
914 {
915         for(; l->name; l++)
916                 remove_proc_entry(l->name, leds);
917
918         remove_proc_entry("led", diag);
919 }
920
921 static void set_led_extif(struct led_t *led)
922 {
923         gpio_set_extif(led->gpio, led->state);
924 }
925
926 static void led_flash(unsigned long dummy) {
927         struct led_t *l;
928         u32 mask = 0;
929         u8 extif_blink = 0;
930
931         for (l = platform.leds; l->name; l++) {
932                 if (l->flash) {
933                         if (l->gpio & GPIO_TYPE_EXTIF) {
934                                 extif_blink = 1;
935                                 l->state = !l->state;
936                                 set_led_extif(l);
937                         } else {
938                                 mask |= l->gpio;
939                         }
940                 }
941         }
942
943         mask &= ~gpiomask;
944         if (mask) {
945                 u32 val = ~gpio_in();
946
947                 gpio_outen(mask, mask);
948                 gpio_control(mask, 0);
949                 gpio_out(mask, val);
950         }
951         if (mask || extif_blink) {
952                 mod_timer(&led_timer, jiffies + FLASH_TIME);
953         }
954 }
955
956 static ssize_t diag_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
957 {
958 #ifdef LINUX_2_4
959         struct inode *inode = file->f_dentry->d_inode;
960         struct proc_dir_entry *dent = inode->u.generic_ip;
961 #else
962         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
963 #endif
964         char *page;
965         int len = 0;
966         
967         if ((page = kmalloc(1024, GFP_KERNEL)) == NULL)
968                 return -ENOBUFS;
969         
970         if (dent->data != NULL) {
971                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
972                 switch (handler->type) {
973                         case PROC_LED: {
974                                 struct led_t * led = (struct led_t *) handler->ptr;
975                                 if (led->flash) {
976                                         len = sprintf(page, "f\n");
977                                 } else {
978                                         if (led->gpio & GPIO_TYPE_EXTIF) {
979                                                 len = sprintf(page, "%d\n", led->state);
980                                         } else {
981                                                 u32 in = (gpio_in() & led->gpio ? 1 : 0);
982                                                 u8 p = (led->polarity == NORMAL ? 0 : 1);
983                                                 len = sprintf(page, "%d\n", ((in ^ p) ? 1 : 0));
984                                         }
985                                 }
986                                 break;
987                         }
988                         case PROC_MODEL:
989                                 len = sprintf(page, "%s\n", platform.name);
990                                 break;
991                         case PROC_GPIOMASK:
992                                 len = sprintf(page, "0x%04x\n", gpiomask);
993                                 break;
994                 }
995         }
996         len += 1;
997
998         if (*ppos < len) {
999                 len = min_t(int, len - *ppos, count);
1000                 if (copy_to_user(buf, (page + *ppos), len)) {
1001                         kfree(page);
1002                         return -EFAULT;
1003                 }
1004                 *ppos += len;
1005         } else {
1006                 len = 0;
1007         }
1008
1009         kfree(page);
1010         return len;
1011 }
1012
1013
1014 static ssize_t diag_proc_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
1015 {
1016 #ifdef LINUX_2_4
1017         struct inode *inode = file->f_dentry->d_inode;
1018         struct proc_dir_entry *dent = inode->u.generic_ip;
1019 #else
1020         struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
1021 #endif
1022         char *page;
1023         int ret = -EINVAL;
1024
1025         if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
1026                 return -ENOBUFS;
1027
1028         if (copy_from_user(page, buf, count)) {
1029                 kfree(page);
1030                 return -EINVAL;
1031         }
1032         page[count] = 0;
1033         
1034         if (dent->data != NULL) {
1035                 struct prochandler_t *handler = (struct prochandler_t *) dent->data;
1036                 switch (handler->type) {
1037                         case PROC_LED: {
1038                                 struct led_t *led = (struct led_t *) handler->ptr;
1039                                 int p = (led->polarity == NORMAL ? 0 : 1);
1040                                 
1041                                 if (page[0] == 'f') {
1042                                         led->flash = 1;
1043                                         led_flash(0);
1044                                 } else {
1045                                         led->flash = 0;
1046                                         if (led->gpio & GPIO_TYPE_EXTIF) {
1047                                                 led->state = p ^ ((page[0] == '1') ? 1 : 0);
1048                                                 set_led_extif(led);
1049                                         } else {
1050                                                 gpio_outen(led->gpio, led->gpio);
1051                                                 gpio_control(led->gpio, 0);
1052                                                 gpio_out(led->gpio, ((p ^ (page[0] == '1')) ? led->gpio : 0));
1053                                         }
1054                                 }
1055                                 break;
1056                         }
1057                         case PROC_GPIOMASK:
1058                                 gpiomask = simple_strtoul(page, NULL, 0);
1059
1060                                 if (platform.buttons) {
1061                                         unregister_buttons(platform.buttons);
1062                                         register_buttons(platform.buttons);
1063                                 }
1064
1065                                 if (platform.leds) {
1066                                         unregister_leds(platform.leds);
1067                                         register_leds(platform.leds);
1068                                 }
1069                                 break;
1070                 }
1071                 ret = count;
1072         }
1073
1074         kfree(page);
1075         return ret;
1076 }
1077
1078 static int __init diag_init(void)
1079 {
1080         static struct proc_dir_entry *p;
1081         static struct platform_t *detected;
1082
1083         detected = platform_detect();
1084         if (!detected) {
1085                 printk(MODULE_NAME ": Router model not detected.\n");
1086                 return -ENODEV;
1087         }
1088         memcpy(&platform, detected, sizeof(struct platform_t));
1089
1090         printk(MODULE_NAME ": Detected '%s'\n", platform.name);
1091         if (platform.platform_init != NULL) {
1092                 platform.platform_init();
1093         }
1094
1095         if (!(diag = proc_mkdir("diag", NULL))) {
1096                 printk(MODULE_NAME ": proc_mkdir on /proc/diag failed\n");
1097                 return -EINVAL;
1098         }
1099
1100         if ((p = create_proc_entry("model", S_IRUSR, diag))) {
1101                 p->data = (void *) &proc_model;
1102                 p->proc_fops = &diag_proc_fops;
1103         }
1104
1105         if ((p = create_proc_entry("gpiomask", S_IRUSR | S_IWUSR, diag))) {
1106                 p->data = (void *) &proc_gpiomask;
1107                 p->proc_fops = &diag_proc_fops;
1108         }
1109
1110         if (platform.buttons)
1111                 register_buttons(platform.buttons);
1112
1113         if (platform.leds)
1114                 register_leds(platform.leds);
1115
1116         return 0;
1117 }
1118
1119 static void __exit diag_exit(void)
1120 {
1121         del_timer(&led_timer);
1122
1123         if (platform.buttons)
1124                 unregister_buttons(platform.buttons);
1125
1126         if (platform.leds)
1127                 unregister_leds(platform.leds);
1128
1129         remove_proc_entry("model", diag);
1130         remove_proc_entry("gpiomask", diag);
1131         remove_proc_entry("diag", NULL);
1132 }
1133
1134 module_init(diag_init);
1135 module_exit(diag_exit);
1136
1137 MODULE_AUTHOR("Mike Baker, Felix Fietkau / OpenWrt.org");
1138 MODULE_LICENSE("GPL");