summaryrefslogtreecommitdiffstats
path: root/meta-facebook/meta-yosemite/recipes-yosemite/oob-nic/oob-nic/src/i2craw.c
blob: f9d499bd1866c10794a126bda118b57874d2380d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright 2004-present Facebook. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>

#include "facebook/i2c-dev.h"
#include "facebook/log.h"

void usage(const char *prog) {
  printf("Usage: %s [options] <bus number> <slave address>\n", prog);
  printf("\n  Options:\n"
         "\n\t-w 'bytes to write':\n"
         "\t\t i2c write\n"
         "\n\t-r <number of bytes to read>:\n"
         "\t\t if 0 is provided, the first byte of read is used to determine\n"
         "\t\t how many bytes more to read\n"
         "\n\t-p:\n"
         "\t\t Use PEC\n"
         "\n\t-h:\n"
         "\t\t Print this help\n"
         "\n  Note: if both '-w' and '-r' are specified, write will be"
         "\n        performed first, followed by read\n");
}

#define MAX_BYTES 255

int g_use_pec = 0;
int g_has_write = 0;
int g_n_write = 0;
uint8_t g_write_bytes[MAX_BYTES];
int g_has_read = 0;
int g_n_read = -1;
uint8_t g_read_bytes[MAX_BYTES];
uint8_t g_bus = -1;
uint8_t g_slave_addr = 0xff;

static int parse_byte_string(const char *str) {
  const char *startptr = str;
  char *endptr;
  int total = 0;
  unsigned long val;

  do {
    val = strtoul(startptr, &endptr, 0);
    if (startptr == endptr) {
      printf("'%s' is invalid\n", str);
      return -1;
    }
    if (val > MAX_BYTES) {
      printf("'%s' is invalid\n", str);
      return -1;
    }
    g_write_bytes[total++] = val;
    if (*endptr == '\0') {
      break;
    }
    if (total >= MAX_BYTES) {
      printf("'%s' is invalid\n", str);
      return -1;
    }
    startptr = endptr;
  } while(1);

  return total;
}

static int i2c_open() {
  int fd;
  char fn[32];
  int rc;

  snprintf(fn, sizeof(fn), "/dev/i2c-%d", g_bus);
  fd = open(fn, O_RDWR);
  if (fd == -1) {
    LOG_ERR(errno, "Failed to open i2c device %s", fn);
    return -1;
  }

  rc = ioctl(fd, I2C_SLAVE, g_slave_addr);
  if (rc < 0) {
    LOG_ERR(errno, "Failed to open slave @ address 0x%x", g_slave_addr);
    close(fd);
  }

  return fd;
}

static int i2c_io(int fd) {
  struct i2c_rdwr_ioctl_data data;
  struct i2c_msg msg[2];
  int n_msg = 0;
  int rc;

  memset(&msg, 0, sizeof(msg));

  if (g_has_write) {
    msg[n_msg].addr = g_slave_addr;
    msg[n_msg].flags = (g_use_pec) ? I2C_CLIENT_PEC : 0;
    msg[n_msg].len = g_n_write;
    msg[n_msg].buf = g_write_bytes;
    n_msg++;
  }

  if (g_has_read) {
    msg[n_msg].addr = g_slave_addr;
    msg[n_msg].flags = I2C_M_RD
      | ((g_use_pec) ? I2C_CLIENT_PEC : 0)
      | ((g_n_read == 0) ? I2C_M_RECV_LEN : 0);
    /*
     * In case of g_n_read is 0, block length will be added by
     * the underlying bus driver.
     */
    msg[n_msg].len = (g_n_read) ? g_n_read : 256;
    msg[n_msg].buf = g_read_bytes;
    if (g_n_read == 0) {
      /* If we're using variable length block reads, we have to set the
       * first byte of the buffer to at least one or the kernel complains.
       */
      g_read_bytes[0] = 1;
    }
    n_msg++;
  }

  data.msgs = msg;
  data.nmsgs = n_msg;

  rc = ioctl(fd, I2C_RDWR, &data);
  if (rc < 0) {
    LOG_ERR(errno, "Failed to do raw io");
    return -1;
  }

  return 0;
}

int main(int argc, char * const argv[]) {
  int i;
  int fd;
  int opt;
  while ((opt = getopt(argc, argv, "hpw:r:")) != -1) {
    switch (opt) {
    case 'h':
      usage(argv[0]);
      return 0;
    case 'p':
      g_use_pec = 1;
      break;
    case 'w':
      g_has_write = 1;
      if ((g_n_write = parse_byte_string(optarg)) <= 0) {
        usage(argv[0]);
        return -1;
      }
      break;
    case 'r':
      g_has_read = 1;
      g_n_read = atoi(optarg);
      break;
    default:
      usage(argv[0]);
      return -1;
    }
  }

  /* make sure we still have arguments for bus and slave address */
  if (optind + 2 != argc) {
    printf("Bus or slave address is missing\n");
    usage(argv[0]);
    return -1;
  }

  g_bus = atoi(argv[optind]);
  g_slave_addr = strtoul(argv[optind + 1], NULL, 0);
  if ((g_slave_addr & 0x80)) {
    printf("Slave address must be 7-bit\n");
    return -1;
  }

  if (!g_has_write && !g_has_read) {
    /* by default, read, first byte read is the length */
    g_has_read = 1;
    g_n_read = 0;
  }

  printf("Bus: %d\nDevice address: 0x%x\n", g_bus, g_slave_addr);
  if (g_has_write) {
    printf("To write %d bytes:", g_n_write);
    for (i = 0; i < g_n_write; i++) {
      printf(" 0x%x", g_write_bytes[i]);
    }
    printf("\n");
  }
  if (g_has_read) {
    if (g_n_read) {
      printf("To read %d bytes.\n", g_n_read);
    } else {
      printf("To read data.\n");
    }
  }

  fd = i2c_open();
  if (fd < 0) {
    return -1;
  }

  if (i2c_io(fd) < 0) {
    return -1;
  }

  if (g_has_read) {
    printf("Received:\n ");
    if (g_n_read == 0) {
      g_n_read = g_read_bytes[0] + 1;
    }
    for (i = 0; i < g_n_read; i++) {
      printf(" 0x%x", g_read_bytes[i]);
    }
    printf("\n");
  }

  return 0;
}
OpenPOWER on IntegriCloud