Ignore:
Timestamp:
08/27/08 20:16:34 (5 years ago)
Author:
BrainSlayer
Message:

gpio support for storm board (wbd-111)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/router/libutils/gpio.c

    r10176 r10227  
    1717#include <stdlib.h> 
    1818#include <fcntl.h> 
    19  
    20 #if  defined(HAVE_AR531X) || defined(HAVE_LSX) 
     19#include <errno.h> 
     20 
     21#if  defined(HAVE_AR531X) || defined(HAVE_LSX) || defined(HAVE_DANUBE) 
    2122 
    2223void set_gpio( int gpio, int value ) 
     
    9394         * ERROR HANDLING; you can check errno to see what went wrong  
    9495         */ 
    95         // fprintf (stderr, "Error: could not open %s (%d)\n", filename, 
    96         // errno); 
     96        fprintf( stderr, "Error: could not open %s (%d)\n", filename, errno ); 
    9797        return; 
    9898    } 
     
    108108         * ERROR HANDLING; you can check errno to see what went wrong  
    109109         */ 
    110         // fprintf (stderr, "Error: ioctl failed: %s (%d)\n", strerror 
    111         // (errno), 
    112         // errno); 
     110        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ), 
     111                 errno ); 
    113112        return; 
    114113    } 
     
    124123         * ERROR HANDLING; you can check errno to see what went wrong  
    125124         */ 
    126         // fprintf (stderr, "Error: ioctl failed: %s (%d)\n", strerror 
    127         // (errno), 
    128         // errno); 
     125        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ), 
     126                 errno ); 
    129127        return; 
    130128    } 
     
    147145         * ERROR HANDLING; you can check errno to see what went wrong  
    148146         */ 
     147        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ), 
     148                 errno ); 
    149149        return 1; 
    150150    } 
     
    160160         * ERROR HANDLING; you can check errno to see what went wrong  
    161161         */ 
     162        fprintf( stderr, "Error: ioctl failed: %s (%d)\n", strerror( errno ), 
     163                 errno ); 
    162164        return 1; 
    163165    } 
     
    172174         * ERROR HANDLING; you can check errno to see what went wrong  
    173175         */ 
     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 ); 
    174306        return 1; 
    175307    } 
Note: See TracChangeset for help on using the changeset viewer.