1 /****************************************************************************
2  ** hw_commandir.h **********************************************************
3  ****************************************************************************
4  *
5  * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
6  * -- Original hw_default.h
7  * Modified for CommandIR Transceivers, April-June 2008, Matthew Bodkin
8  * Modified for CommandIR III - March-August 2010 - Matthew Bodkin
9  */
10 
11 #ifndef HW_COMMANDIR_H
12 #define HW_COMMANDIR_H
13 
14 #include <usb.h>
15 
16 extern struct ir_remote *repeat_remote;
17 extern char *progname;
18 
19 #define TRUE	0
20 #define FALSE	1
21 
22 #define RX_BUFFER_SIZE 1024
23 #define TX_BUFFER_SIZE 1024
24 #define TX_QUEUE 1
25 #define RX_QUEUE 0
26 #define MAX_COMMANDIRS 4
27 #define MAX_COMMAND 8192
28 
29 /* transmitter channel control */
30 #define MAX_DEVICES		4
31 #define MAX_TX_TIMERS    16
32 #define DEVICE_CHANNELS	4
33 #define MAX_MASK 		0xffff
34 #define MAX_SIGNALQ		100
35 // 32-bits is the most emitters we can support on one CommandIR:
36 #define MAX_EMITTERS	32
37 
38 /* CommandIR control codes */
39 #define CHANNEL_EN_MASK	1
40 #define FREQ_HEADER     2
41 #define MCU_CTRL_SIZE   3
42 #define TX_HEADER       7
43 #define TX_HEADER_NEW	8
44 /* New for CommandIR II  */
45 
46 #define READ_INPUTS		10
47 #define PROC_SET		11
48 #define INIT_FUNCTION	12
49 #define RX_SELECT		13
50 #define TX_COMMANDIR_II 14
51 /* Internal to driver */
52 #define TX_LIRC_T	    15
53 #define FREQ_HEADER_LIRC 16
54 #define RXDECODE_HEADER_LIRC 17
55 #define INIT_HEADER_LIRC 18
56 #define DEINIT_HEADER_LIRC 19
57 #define GET_VERSION 	20
58 
59 #define COMMANDIR_2_PULSE_MASK 0x8000
60 #define COMMANDIR_2_OVERFLOW_MASK 0x4000
61 
62 #define DEFAULT_PULSE_WIDTH 13
63 
64 #define USB_CMDIR_VENDOR_ID		0x10c4
65 #define USB_CMDIR_PRODUCT_ID	0x0003
66 #define USB_CMDIR_MINOR_BASE	192
67 
68 #define HW_COMMANDIR_MINI 	1
69 #define HW_COMMANDIR_2		2
70 #define HW_COMMANDIR_3		3
71 #define HW_COMMANDIR_UNKNOWN 127
72 
73 #define MAX_HW_MINI_PACKET 64
74 
75 // CommandIR has lots of buffer room, we don't need to poll constantly
76 #define USB_TIMEOUT_MS 5000
77 #define USB_TIMEOUT_US 1000
78 #define WAIT_BETWEEN_READS_US 10000
79 #define MAX_WAIT_BETWEEN_READS_US 5000000
80 #define MIN_WAIT_BETWEEN_READS_US 5000
81 
82 #define USB_MAX_BUSES	8
83 #define USB_MAX_BUSDEV	127
84 
85 #define RX_HEADER_DATA 		0x01
86 #define RX_HEADER_EVENTS 	0x02
87 #define RX_HEADER_TXAVAIL 	0x03
88 
89 // We keep CommandIR's OPEN even on -deinit for speed and to monitor
90 // Other non-LIRC events (plugin, suspend, etc) - and settings!
91 #define USB_KEEP_WARM 1
92 
93 // CommandIR lircd.conf event driven code definitions
94 #define LIRCCODE_GAP  125000
95 #define JACK_PLUG_1		0x01
96 #define JACK_PLUG_2		0x02
97 #define JACK_PLUG_3		0x03
98 #define JACK_PLUG_4		0x04
99 #define JACK_PLUG_5		0x11
100 #define JACK_PLUG_6		0x12
101 #define JACK_PLUG_7		0x13
102 #define JACK_PLUG_8		0x14
103 #define JACK_PLUG_9		0x21
104 #define JACK_PLUG_10	0x22
105 #define JACK_PLUG_11	0x23
106 #define JACK_PLUG_12	0x24
107 #define JACK_PLUG_13	0x31
108 #define JACK_PLUG_14	0x32
109 #define JACK_PLUG_15	0x33
110 #define JACK_PLUG_16	0x34
111 
112 #define JACK_UNPLUG_1	0x05
113 #define JACK_UNPLUG_2	0x06
114 #define JACK_UNPLUG_3	0x07
115 #define JACK_UNPLUG_4	0x08
116 #define JACK_UNPLUG_5	0x15
117 #define JACK_UNPLUG_6	0x16
118 #define JACK_UNPLUG_7	0x17
119 #define JACK_UNPLUG_8	0x18
120 #define JACK_UNPLUG_9	0x25
121 #define JACK_UNPLUG_10	0x26
122 #define JACK_UNPLUG_11	0x27
123 #define JACK_UNPLUG_12	0x28
124 #define JACK_UNPLUG_13	0x35
125 #define JACK_UNPLUG_14	0x36
126 #define JACK_UNPLUG_15	0x37
127 #define JACK_UNPLUG_16	0x38
128 
129 #define SELECT_TX_INTERNAL	0x09
130 #define SELECT_TX_ExTERNAL	0x0A
131 
132 #define SELECT_TX_ON_1		0x0D
133 #define SELECT_TX_ON_2		0x1D
134 #define SELECT_TX_ON_3		0x2D
135 #define SELECT_TX_ON_4		0x3D
136 
137 #define JACK_PLUG_RX_1		0x0B
138 #define JACK_UNPLUG_RX_1	0x0C
139 #define JACK_PLUG_RX_2		0x1B
140 #define JACK_UNPLUG_RX_2	0x1C
141 #define JACK_PLUG_RX_3		0x2B
142 #define JACK_UNPLUG_RX_3	0x2C
143 #define JACK_PLUG_RX_4		0x3B
144 #define JACK_UNPLUG_RX_4	0x3C
145 
146 #define COMMANDIR_PLUG_1	0x41
147 #define COMMANDIR_PLUG_2	0x42
148 #define COMMANDIR_PLUG_3	0x43
149 #define COMMANDIR_PLUG_4	0x44
150 
151 #define COMMANDIR_UNPLUG_1	0x45
152 #define COMMANDIR_UNPLUG_2	0x46
153 #define COMMANDIR_UNPLUG_3	0x47
154 #define COMMANDIR_UNPLUG_4	0x48
155 
156 #define COMMANDIR_REORDERED	0x50
157 #define COMMANDIR_READY		0x51
158 #define COMMANDIR_STOPPED	0x52
159 #define COMMANDIR_POLL_FASTER	0x53
160 #define COMMANDIR_POLL_SLOWER	0x54
161 
162 #define SETTRANSMITTERS_1	0xf0
163 #define SETTRANSMITTERS_2	0xf1
164 #define SETTRANSMITTERS_3	0xf2
165 #define SETTRANSMITTERS_4	0xf3
166 #define SETTRANSMITTERS_5	0xf4
167 #define SETTRANSMITTERS_6	0xf5
168 #define SETTRANSMITTERS_7	0xf6
169 #define SETTRANSMITTERS_8	0xf7
170 #define SETTRANSMITTERS_9	0xf8
171 #define SETTRANSMITTERS_10	0xf9
172 #define SETTRANSMITTERS_11	0xfa
173 #define SETTRANSMITTERS_12	0xfb
174 #define SETTRANSMITTERS_13	0xfc
175 #define SETTRANSMITTERS_14	0xfd
176 #define SETTRANSMITTERS_15	0xfe
177 #define SETTRANSMITTERS_16	0xff
178 
179 // What's in a returning data packet
180 #define COMMANDIR_RX_EVENTS 		0x02
181 #define COMMANDIR_RX_DATA			0x01
182 
183 /**********************************************************************
184  *
185  * internal function prototypes
186  *
187  **********************************************************************/
188 
189 struct send_tx_mask {
190 	unsigned char numBytes[2];
191 	unsigned char idByte;
192 	__u32 new_tx_mask;
193 };
194 
195 struct tx_signal {
196 	char *raw_signal;
197 	int raw_signal_len;
198 	int raw_signal_tx_bitmask;
199 	int *bitmask_emitters_list;
200 	int num_bitmask_emitters_list;
201 	int raw_signal_frequency;
202 	struct tx_signal *next;
203 };
204 
205 struct commandir_3_tx_signal {
206 	unsigned short tx_bit_mask1;
207 	unsigned short tx_bit_mask2;
208 	unsigned short tx_min_gap;
209 	unsigned short tx_signal_count;
210 	unsigned short pulse_width;
211 	unsigned short pwm_offset;
212 };
213 
214 struct commandir_device {
215 	usb_dev_handle *cmdir_udev;
216 	int interface;
217 	int hw_type;
218 	int hw_revision;
219 	int hw_subversion;
220 	int busnum;
221 	int devnum;
222 	int endpoint_max[4];
223 	int num_transmitters;
224 	int num_receivers;
225 	int num_timers;
226 	int tx_jack_sense;
227 	unsigned char rx_jack_sense;
228 	unsigned char rx_data_available;
229 
230 	int *next_enabled_emitters_list;
231 	int num_next_enabled_emitters;
232 	char signalid;
233 
234 	struct tx_signal *next_tx_signal;
235 	struct tx_signal *last_tx_signal;
236 
237 	unsigned char lastSendSignalID;
238 	unsigned char commandir_last_signal_id;
239 	unsigned char flush_buffer;
240 
241 	// CommandIR Mini Specific:
242 	int mini_freq;
243 
244 	unsigned char commandir_tx_start[MAX_TX_TIMERS * 4];
245 	unsigned char commandir_tx_end[MAX_TX_TIMERS * 4];
246 	unsigned char commandir_tx_available[MAX_TX_TIMERS];
247 	unsigned char tx_timer_to_channel_map[MAX_TX_TIMERS];
248 
249 	struct commandir_device *next_commandir_device;
250 };
251 
252 struct commandirIII_status {
253 	unsigned char jack_status[4];
254 	unsigned char rx_status;
255 	unsigned char tx_status;
256 	unsigned char versionByte;
257 	unsigned char expansionByte;
258 };
259 
260 static void hardware_disconnect(struct commandir_device *a);
261 
262 /*** Parent Thread Functions ***/
263 static int commandir_init();
264 static int commandir_send(struct ir_remote *remote, struct ir_ncode *code);
265 static char *commandir_rec(struct ir_remote *remotes);
266 static int commandir_ioctl(unsigned int cmd, void *arg);
267 static lirc_t commandir_readdata(lirc_t timeout);
268 static int commandir_deinit(void);
269 static int commandir_receive_decode(struct ir_remote *remote, ir_code * prep, ir_code * codep, ir_code * postp,
270 				    int *repeat_flagp, lirc_t * min_remaining_gapp, lirc_t * max_remaining_gapp);
271 
272 /*** USB Thread Functions ***/
273 static void commandir_child_init();
274 int do_we_know_device(unsigned int bus_num, int devnum);
275 int claim_and_setup_commandir(unsigned int bus_num, int devnum, struct usb_device *dev);
276 static void hardware_scan();
277 static void hardware_setorder();
278 static void hardware_disconnect(struct commandir_device *a);
279 static void software_disconnects();
280 static void set_detected(unsigned int bus_num, int devnum);
281 static void commandir_read_loop();
282 static void shutdown_usb();
283 
284 /*** Processing Functions ***/
285 static void add_to_tx_pipeline(unsigned char *buffer, int bytes, unsigned int frequency);
286 static int check_irsend_commandir(unsigned char *command);
287 static void recalc_tx_available(struct commandir_device *pcd);
288 static int cmdir_convert_RX(unsigned char *orig_rxbuffer);
289 static int commandir2_convert_RX(unsigned short *bufferrx, unsigned char numvalues);
290 static void pipeline_check(struct commandir_device *pcd);
291 static void commandir_2_transmit_next(struct commandir_device *pcd);
292 
293 static int get_hardware_tx_bitmask(struct commandir_device *pcd);
294 
295 static void set_convert_int_bitmask_to_list_of_enabled_bits(__u32 * bitmask, int bitmask_len);
296 static void set_all_next_tx_mask(int *ar_new_tx_mask, int new_tx_len, __u32 bitmask);
297 static void set_new_signal_bitmasks(struct commandir_device *pcd, struct tx_signal *ptx);
298 
299 static void update_tx_available(struct commandir_device *pcd);
300 static int commandir_read();
301 
302 /** CommandIR III Specific **/
303 static int commandir3_convert_RX(unsigned char *rxBuffer, int numNewValues);
304 static void raise_event(unsigned int eventid);
305 
306 #define MAX_FIRMWARE_PACKET 64
307 #define MAX_RX_PACKET 512
308 
309 #define MAX_INCOMING_BUFFER 1024
310 
311 #define RX_MODE_INTERNAL	1
312 #define RX_MODE_XANTECH		2
313 #define RX_MODE_HAUPPAUGE	3
314 #define REC_TIMESTAMPS 5
315 
316 struct pulse_timestamps {
317 	unsigned char idbyte;
318 	unsigned short pca_fall_at[REC_TIMESTAMPS];
319 	unsigned short pca_rise_at[REC_TIMESTAMPS];
320 	unsigned short PCA_overflow_counter[REC_TIMESTAMPS];
321 };
322 
323 #define USB_RX_PULSE_DEF 31
324 #define USB_RX_PULSE 32
325 #define USB_RX_SPACE 33
326 #define USB_RX_PULSE_DEMOD 34
327 #define USB_RX_SPACE_DEMOD 35
328 #define USB_NO_DATA_BYTE 36
329 
330 struct commandir3_tx_signal {
331 	unsigned short tx_bit_mask1;
332 	unsigned short tx_bit_mask2;
333 	unsigned short tx_min_gap;
334 	unsigned short tx_signal_count;
335 	unsigned short pulse_width;
336 	unsigned short pwm_offset;
337 };
338 
339 struct usb_rx_space3 {
340 	unsigned short pca_overflow_count;
341 	unsigned short pca_offset;
342 };
343 
344 struct usb_rx_pulse3 {
345 	unsigned short t0_count;
346 };
347 
348 struct usb_rx_pulse_def3 {
349 	unsigned short frequency;
350 	unsigned short pwm;
351 };
352 
353 struct usb_rx_demod_pulse {
354 	unsigned short pca_overflow_count;
355 	unsigned short pca_offset;
356 };
357 
358 #endif
359