source: src/router/libutils/gpio.c @ 10227

Last change on this file since 10227 was 10227, checked in by BrainSlayer, 5 years ago

gpio support for storm board (wbd-111)

  • Property svn:executable set to *
File size: 6.9 KB
Line 
1/*
2 */
3#include <string.h>
4#include <unistd.h>
5
6#include <typedefs.h>
7#include <bcmutils.h>
8#include <wlutils.h>
9#include <shutils.h>
10#include <utils.h>
11#include <bcmnvram.h>
12
13#include <unistd.h>
14#include <sys/types.h>
15#include <stdio.h>
16#include <string.h>
17#include <stdlib.h>
18#include <fcntl.h>
19#include <errno.h>
20
21#if  defined(HAVE_AR531X) || defined(HAVE_LSX) || defined(HAVE_DANUBE)
22
23void set_gpio( int gpio, int value )
24{
25    FILE *in;
26    char buf[64];
27
28    sprintf( buf, "/proc/gpio/%d_dir", gpio );
29    in = fopen( buf, "wb" );
30    if( in == NULL )
31        return;
32    fprintf( in, "1" );
33    fclose( in );
34    sprintf( buf, "/proc/gpio/%d_out", gpio );
35    in = fopen( buf, "wb" );
36    if( in == NULL )
37        return;
38    fprintf( in, "%d", value );
39    fclose( in );
40}
41
42int get_gpio( int gpio )
43{
44    FILE *in;
45    int ret;
46    char buf[64];
47
48    sprintf( buf, "/proc/gpio/%d_dir", gpio );
49    in = fopen( buf, "wb" );
50    if( in == NULL )
51        return 0;
52    fprintf( in, "0" );
53    fclose( in );
54    sprintf( buf, "/proc/gpio/%d_in", gpio );
55    in = fopen( buf, "rb" );
56    if( in == NULL )
57        return 0;
58    fscanf( in, "%d", &ret );
59    fclose( in );
60    return ret;
61}
62
63#elif HAVE_XSCALE
64#define u8 unsigned char
65#define u32 unsigned long
66
67// #include <linux/ixp425-gpio.h>
68
69#include <asm/hardware.h>
70#include <asm-arm/arch-ixp4xx/ixp4xx-regs.h>
71
72#define IXP4XX_GPIO_OUT                 0x1
73#define IXP4XX_GPIO_IN                  0x2
74
75struct gpio_bit
76{
77    unsigned char bit;
78    unsigned char state;
79};
80
81char *filename = "/dev/gpio";
82
83void set_gpio( int gpio, int value )
84{
85    int file;
86    struct gpio_bit _bit;
87
88    /*
89     * open device
90     */
91    if( ( file = open( filename, O_RDWR ) ) == -1 )
92    {
93        /*
94         * ERROR HANDLING; you can check errno to see what went wrong
95         */
96        fprintf( stderr, "Error: could not open %s (%d)\n", filename, errno );
97        return;
98    }
99
100    /*
101     * Config bit as output
102     */
103    _bit.bit = gpio;
104    _bit.state = IXP4XX_GPIO_OUT;
105    if( ioctl( file, GPIO_SET_CONFIG, ( long )&_bit ) < 0 )
106    {
107        /*
108         * ERROR HANDLING; you can check errno to see what went wrong
109         */
110        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
111                 errno );
112        return;
113    }
114
115    /*
116     * Write data
117     */
118    _bit.bit = gpio;
119    _bit.state = value;
120    if( ioctl( file, GPIO_SET_BIT, ( unsigned long )&_bit ) < 0 )
121    {
122        /*
123         * ERROR HANDLING; you can check errno to see what went wrong
124         */
125        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
126                 errno );
127        return;
128    }
129
130    close( file );
131
132}
133
134int get_gpio( int gpio )
135{
136    int file;
137    struct gpio_bit _bit;
138
139    /*
140     * open device
141     */
142    if( ( file = open( filename, O_RDONLY ) ) == -1 )
143    {
144        /*
145         * ERROR HANDLING; you can check errno to see what went wrong
146         */
147        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
148                 errno );
149        return 1;
150    }
151
152    /*
153     * Config pin as input
154     */
155    _bit.bit = gpio;
156    _bit.state = IXP4XX_GPIO_IN;
157    if( ioctl( file, GPIO_SET_CONFIG, ( long )&_bit ) < 0 )
158    {
159        /*
160         * ERROR HANDLING; you can check errno to see what went wrong
161         */
162        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
163                 errno );
164        return 1;
165    }
166
167    /*
168     * Read data
169     */
170    _bit.bit = gpio;
171    if( ioctl( file, GPIO_GET_BIT, ( long )&_bit ) < 0 )
172    {
173        /*
174         * ERROR HANDLING; you can check errno to see what went wrong
175         */
176        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
177                 errno );
178        return 1;
179    }
180
181    close( file );
182    return _bit.state;
183}
184
185#elif HAVE_STORM
186#include <linux/mii.h>
187#include <linux/sockios.h>
188#include <net/if.h>
189#include <arpa/inet.h>
190#include <sys/socket.h>
191#include <linux/sockios.h>
192#include <linux/mii.h>
193#define u8 unsigned char
194#define u32 unsigned long
195
196#define GPIO_GET_BIT    0x0000001
197#define GPIO_SET_BIT    0x0000002
198#define GPIO_GET_CONFIG 0x0000003
199#define GPIO_SET_CONFIG 0x0000004
200
201#define IXP4XX_GPIO_OUT                 0x1
202#define IXP4XX_GPIO_IN                  0x2
203
204struct gpio_bit
205{
206    unsigned char bit;
207    unsigned char state;
208};
209
210char *filename = "/dev/gpio";
211
212void set_gpio( int gpio, int value )
213{
214    int file;
215    struct gpio_bit _bit;
216
217    /*
218     * open device
219     */
220    if( ( file = open( filename, O_RDWR ) ) == -1 )
221    {
222        /*
223         * ERROR HANDLING; you can check errno to see what went wrong
224         */
225        fprintf( stderr, "Error: could not open %s (%d)\n", filename, errno );
226        return;
227    }
228
229    /*
230     * Config bit as output
231     */
232    _bit.bit = gpio;
233    _bit.state = IXP4XX_GPIO_OUT;
234    if( ioctl( file, GPIO_SET_CONFIG, ( long )&_bit ) < 0 )
235    {
236        /*
237         * ERROR HANDLING; you can check errno to see what went wrong
238         */
239        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
240                 errno );
241        return;
242    }
243
244    /*
245     * Write data
246     */
247    _bit.bit = gpio;
248    _bit.state = value;
249    if( ioctl( file, GPIO_SET_BIT, ( unsigned long )&_bit ) < 0 )
250    {
251        /*
252         * ERROR HANDLING; you can check errno to see what went wrong
253         */
254        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
255                 errno );
256        return;
257    }
258
259    close( file );
260
261}
262
263int get_gpio( int gpio )
264{
265    int file;
266    struct gpio_bit _bit;
267
268    /*
269     * open device
270     */
271    if( ( file = open( filename, O_RDONLY ) ) == -1 )
272    {
273        /*
274         * ERROR HANDLING; you can check errno to see what went wrong
275         */
276        fprintf( stderr, "Error: could not open %s (%d)\n", filename, errno );
277        return 1;
278    }
279
280    /*
281     * Config pin as input
282     */
283    _bit.bit = gpio;
284    _bit.state = IXP4XX_GPIO_IN;
285    if( ioctl( file, GPIO_SET_CONFIG, ( long )&_bit ) < 0 )
286    {
287        /*
288         * ERROR HANDLING; you can check errno to see what went wrong
289         */
290        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
291                 errno );
292        return 1;
293    }
294
295    /*
296     * Read data
297     */
298    _bit.bit = gpio;
299    if( ioctl( file, GPIO_GET_BIT, ( long )&_bit ) < 0 )
300    {
301        /*
302         * ERROR HANDLING; you can check errno to see what went wrong
303         */
304        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ),
305                 errno );
306        return 1;
307    }
308
309    close( file );
310    return _bit.state;
311}
312
313#else
314
315void set_gpio( int pin, int value )
316{
317    int gpioouten = open( "/dev/gpio/outen", O_RDWR );
318    int gpioout = open( "/dev/gpio/out", O_RDWR );
319    unsigned int gpio;
320
321    read( gpioouten, &gpio, sizeof( gpio ) );
322    gpio |= 1 << pin;
323    write( gpioouten, &gpio, sizeof( gpio ) );
324
325    read( gpioout, &gpio, sizeof( gpio ) );
326    if( value )
327    {
328        gpio |= ( 1 << pin );
329    }
330    else
331    {
332        gpio &= ~( 1 << pin );
333    }
334    write( gpioout, &gpio, sizeof( gpio ) );
335    close( gpioout );
336    close( gpioouten );
337}
338
339int get_gpio( int pin )
340{
341    unsigned int gpio;
342    int gpioouten = open( "/dev/gpio/outen", O_RDWR );
343    int gpioin = open( "/dev/gpio/in", O_RDWR );
344
345    read( gpioouten, &gpio, sizeof( gpio ) );
346    gpio &= ~( 1 << pin );
347    write( gpioouten, &gpio, sizeof( gpio ) );
348    read( gpioin, &gpio, sizeof( gpio ) );
349    gpio = ( gpio & ( 1 << pin ) ) ? 1 : 0;
350    close( gpioin );
351    close( gpioouten );
352    return gpio;
353}
354
355#endif
Note: See TracBrowser for help on using the repository browser.