~ubuntu-branches/ubuntu/quantal/lirc/quantal

« back to all changes in this revision

Viewing changes to daemons/hw_i2cuser.c

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2011-05-05 14:45:59 UTC
  • mfrom: (1.2.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20110505144559-egifvzgs2l7o6p08
Tags: 0.9.0-0ubuntu1
* New upstream version (0.9.0)
* Drop lirc-modules-source (LP: #778026)
* Change to v3 package (quilt)
* debian/control:
  - Build-deps on newer debhelper
  - bump standards version.
* debian/compat:
  - Bump to 7
* Drop Patches:
  - debian/patches/13-warning-cleanup
  - debian/patches/define_current_cpu_data.patch
  - debian/patches/lirc-in-kernel-ioctls.patch
  - debian/patches/updated-driver-names.patch
* Move extra remotes to our remotes DB.
* debian/rules: 
  - Remove references to quilt (as it's source package that does it now)
  - remove deprecated dh_clean -k

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
#include <time.h>
54
54
#include <signal.h>
55
55
#include <linux/i2c-dev.h>
56
 
#ifndef I2C_SLAVE /* hack */
 
56
#ifndef I2C_SLAVE               /* hack */
57
57
#include <linux/i2c.h>
58
58
#endif
59
59
 
78
78
static char *i2cuser_rec(struct ir_remote *remotes);
79
79
 
80
80
struct hardware hw_i2cuser = {
81
 
        NULL,                   /* determine device by probing */
82
 
        -1,                     /* fd */
83
 
        LIRC_CAN_REC_LIRCCODE,  /* features */
84
 
        0,                      /* send_mode */
85
 
        LIRC_MODE_LIRCCODE,     /* rec_mode */
86
 
        CODE_SIZE_BITS,         /* code_length */
87
 
        i2cuser_init,           /* init_func */
88
 
        i2cuser_deinit,         /* deinit_func */
89
 
        NULL,                   /* send_func */
90
 
        i2cuser_rec,            /* rec_func */
91
 
        receive_decode,         /* decode_func */
92
 
        NULL,                   /* ioctl_func */
 
81
        NULL,                   /* determine device by probing */
 
82
        -1,                     /* fd */
 
83
        LIRC_CAN_REC_LIRCCODE,  /* features */
 
84
        0,                      /* send_mode */
 
85
        LIRC_MODE_LIRCCODE,     /* rec_mode */
 
86
        CODE_SIZE_BITS,         /* code_length */
 
87
        i2cuser_init,           /* init_func */
 
88
        i2cuser_deinit,         /* deinit_func */
 
89
        NULL,                   /* send_func */
 
90
        i2cuser_rec,            /* rec_func */
 
91
        receive_decode,         /* decode_func */
 
92
        NULL,                   /* ioctl_func */
93
93
        NULL,
94
94
        "i2cuser"
95
95
};
103
103
static pid_t child = -1;
104
104
 
105
105
/* Hunt for the appropriate i2c device and open it. */
106
 
static int open_i2c_device(void) {
 
106
static int open_i2c_device(void)
 
107
{
107
108
        const char *adapter_dir = "/sys/class/i2c-adapter";
108
109
        DIR *dir;
109
110
        int found;
110
111
 
111
112
        dir = opendir(adapter_dir);
112
113
        if (dir == NULL) {
113
 
                logprintf(LOG_ERR, "Cannot list i2c-adapter dir %s",
114
 
                          adapter_dir);
 
114
                logprintf(LOG_ERR, "Cannot list i2c-adapter dir %s", adapter_dir);
115
115
                return -1;
116
116
        }
117
117
        found = -1;
128
128
                        continue;
129
129
 
130
130
                /* Kernels 2.6.22 and later had the name here: */
131
 
                snprintf(s, sizeof s, "%s/%s/name",
132
 
                         adapter_dir, de->d_name);
133
 
                
 
131
                snprintf(s, sizeof s, "%s/%s/name", adapter_dir, de->d_name);
 
132
 
134
133
                f = fopen(s, "r");
135
134
                if (f == NULL) {
136
135
                        /* ... and kernels prior to 2.6.22 have it here: */
137
 
                        snprintf(s, sizeof s, "%s/%s/device/name",
138
 
                                 adapter_dir, de->d_name);
 
136
                        snprintf(s, sizeof s, "%s/%s/device/name", adapter_dir, de->d_name);
139
137
 
140
138
                        f = fopen(s, "r");
141
139
                }
149
147
 
150
148
                if (strncmp(s, "bt878", 5) == 0) {
151
149
                        if (strncmp(de->d_name, "i2c-", 4) != 0) {
152
 
                                logprintf(LOG_ERR, "i2c adapter dir %s "
153
 
                                          "has unexpected name", de->d_name);
 
150
                                logprintf(LOG_ERR, "i2c adapter dir %s has unexpected name", de->d_name);
154
151
                                return -1;
155
152
                        }
156
153
                        found = atoi(de->d_name + 4);
170
167
        return open(device_name, O_RDWR);
171
168
}
172
169
 
173
 
static int i2cuser_init(void) {
 
170
static int i2cuser_init(void)
 
171
{
174
172
        int pipe_fd[2] = { -1, -1 };
175
173
 
176
174
        if (pipe(pipe_fd) != 0) {
192
190
 
193
191
        child = fork();
194
192
        if (child == -1) {
195
 
                logprintf(LOG_ERR, "Cannot fork child process: %s",
196
 
                          strerror(errno));
 
193
                logprintf(LOG_ERR, "Cannot fork child process: %s", strerror(errno));
197
194
                goto fail;
198
195
        } else if (child == 0) {
199
196
                close(pipe_fd[0]);
214
211
        return 0;
215
212
}
216
213
 
217
 
static int i2cuser_deinit(void) {
 
214
static int i2cuser_deinit(void)
 
215
{
218
216
        if (child != -1) {
219
217
                if (kill(child, SIGTERM) == -1)
220
218
                        return 0;
228
226
        return 1;
229
227
}
230
228
 
231
 
static void i2cuser_read_loop(int out_fd) {
 
229
static void i2cuser_read_loop(int out_fd)
 
230
{
232
231
        ir_code last_code = 0;
233
232
        double last_time = 0.0;
234
233
 
248
247
 
249
248
                do {
250
249
                        /* Poll 20 times per second. */
251
 
                        struct timespec ts = {0, 50000000};
 
250
                        struct timespec ts = { 0, 50000000 };
252
251
                        nanosleep(&ts, NULL);
253
252
 
254
253
                        rc = read(i2c_fd, &buf, sizeof buf);
255
254
                        if (rc < 0 && errno != EREMOTEIO) {
256
 
                                logprintf(LOG_ERR, "Error reading from i2c "
257
 
                                          "device: %s", strerror(errno));
 
255
                                logprintf(LOG_ERR, "Error reading from i2c device: %s", strerror(errno));
258
256
                                goto fail;
259
257
                        } else if (rc != sizeof buf) {
260
258
                                continue;
283
281
                        new_code >>= 8;
284
282
                }
285
283
                if (write(out_fd, code_buf, CODE_SIZE) != CODE_SIZE) {
286
 
                        logprintf(LOG_ERR, "Write to i2cuser pipe failed: %s",
287
 
                                  strerror(errno));
 
284
                        logprintf(LOG_ERR, "Write to i2cuser pipe failed: %s", strerror(errno));
288
285
                        goto fail;
289
286
                }
290
287
        }
293
290
        _exit(1);
294
291
}
295
292
 
296
 
static char *i2cuser_rec(struct ir_remote *remotes) {
297
 
        if (!clear_rec_buffer()) return NULL;
 
293
static char *i2cuser_rec(struct ir_remote *remotes)
 
294
{
 
295
        if (!clear_rec_buffer())
 
296
                return NULL;
298
297
        return decode_all(remotes);
299
298
}