1 /* $Id: hw_tira.c,v 5.6 2007/07/29 18:20:09 lirc Exp $ */
2 /*****************************************************************************
3 ** hw_tira.c ****************************************************************
4 *****************************************************************************
5 * Routines for the HomeElectronics TIRA-2 USB dongle.
7 * Serial protocol described at:
8 * http://www.home-electro.com/Download/Protocol2.pdf
10 * Copyright (C) 2003 Gregory McLean <gregm@gxsnmp.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
47 #include "ir_remote.h"
51 static struct timeval start
,end
,last
;
52 static unsigned char b
[6];
55 #define CODE_LENGTH 64
56 struct hardware hw_tira
= {
57 "/dev/ttyUSB0", /* Default device */
59 LIRC_CAN_REC_LIRCCODE
, /* Features */
61 LIRC_MODE_LIRCCODE
, /* rec_mode */
62 CODE_LENGTH
, /* code_length */
63 tira_init
, /* init_func */
64 NULL
, /* config_func */
65 tira_deinit
, /* deinit_func */
67 tira_rec
, /* rec_func */
68 tira_decode
, /* decode_func */
69 NULL
, /* ioctl_func */
76 int tira_decode (struct ir_remote
*remote
, ir_code
*prep
, ir_code
*codep
,
77 ir_code
*postp
, int *repeat_flagp
,
78 lirc_t
*min_remaining_gapp
,
79 lirc_t
*max_remaining_gapp
)
81 if(!map_code(remote
, prep
, codep
, postp
,
82 0, 0, CODE_LENGTH
, code
, 0, 0))
87 map_gap(remote
, &start
, &last
, 0, repeat_flagp
,
88 min_remaining_gapp
, max_remaining_gapp
);
99 /* Clear the port of any random data */
100 while (read(hw
.fd
, &ptr
, 1) >= 0) ;
102 /* Start off with the IP command. This was initially used to
103 switch to timing mode on the Tira-1. The Tira-2 also
104 supports this mode, however it does not switch the Tira-2
107 if (write (hw
.fd
, "IP", 2) != 2)
109 logprintf(LOG_ERR
, "failed writing to device");
112 /* Wait till the chars are written, should use tcdrain but
113 that don't seem to work... *shrug*
115 usleep (2 * (100 * 1000));
116 i
= read (hw
.fd
, response
, 3);
117 if (strncmp(response
, "OIP", 3) == 0)
119 read (hw
.fd
, &ptr
, 1); /* read the calibration value */
120 read (hw
.fd
, &ptr
, 1); /* read the version word */
121 /* Bits 4:7 in the version word set to one indicates a
125 logprintf(LOG_INFO
, "Tira-2 detected");
126 /* Lets get the firmware version */
127 write (hw
.fd
, "IV", 2);
128 usleep (2 * (100 * 1000));
129 memset (response
, 0, sizeof(response
));
130 i
= read (hw
.fd
, response
, sizeof(response
)-1);
131 logprintf(LOG_INFO
, "firmware version %s", response
);
135 logprintf(LOG_INFO
, "Ira/Tira-1 detected");
137 /* According to the docs we can do some bit work here
138 and figure out what the device supports from the
139 version word retrived.
141 At this point we have a Device of some sort. Lets
142 kick it into "Six bytes" mode.
144 if (write(hw
.fd
, "IR", 2 ) != 2)
146 logprintf(LOG_ERR
, "failed switching device "
147 "into six byte mod");
150 /* wait for the chars to be written */
151 usleep (2 * (100 * 1000));
153 i
= read (hw
.fd
, response
, 2);
156 logprintf(LOG_ERR
, "failed reading response "
157 "to six byte mode command");
162 if (strncmp(response
, "OK", 2) == 0)
164 logprintf(LOG_INFO
, "device online, "
165 "ready to receive remote codes");
170 logprintf(LOG_ERR
, "unexpected response from device");
176 LOGPRINTF (1, "Tira init");
177 if(!tty_create_lock(hw
.device
))
179 logprintf(LOG_ERR
,"could not create lock files");
182 if ( (hw
.fd
= open (hw
.device
, O_RDWR
| O_NONBLOCK
| O_NOCTTY
)) < 0)
185 logprintf (LOG_ERR
, "Could not open the '%s' device",
189 LOGPRINTF(1, "device '%s' opened", hw
.device
);
191 /* We want 9600 8N1 with CTS/RTS handshaking, lets set that
192 * up. The specs state a baud rate of 100000, looking at the
193 * ftdi_sio driver it forces the issue so we can set to what
194 * we would like. And seeing as this is mapped to 9600 under
195 * windows should be a safe bet.
197 if(!tty_reset(hw
.fd
) ||
198 !tty_setbaud(hw
.fd
, 9600) ||
199 !tty_setrtscts(hw
.fd
, 1))
205 /* Device should be activated by this point... wait... */
216 int tira_deinit (void)
224 char *tira_rec (struct ir_remote
*remotes
)
231 gettimeofday (&start
, NULL
);
232 for (i
= 0 ; i
< 6; i
++)
236 if (!waitfordata(20000))
238 LOGPRINTF(0,"timeout reading byte %d",i
);
239 /* likely to be !=6 bytes, so flush. */
240 tcflush(hw
.fd
, TCIFLUSH
);
244 if (read(hw
.fd
, &b
[i
], 1) != 1)
246 logprintf(LOG_ERR
, "reading of byte %d failed.", i
);
247 logperror(LOG_ERR
,NULL
);
250 LOGPRINTF(1, "byte %d: %02x", i
, b
[i
]);
253 gettimeofday(&end
,NULL
);
255 for ( i
= 0 ; i
< x
; i
++ )
257 code
|= ((ir_code
) b
[i
]);
261 LOGPRINTF(1," -> %0llx",(unsigned long long) code
);
263 m
= decode_all(remotes
);