]>
Commit | Line | Data |
---|---|---|
69b1214c JW |
1 | /* |
2 | * i2c IR lirc driver for devices with zilog IR processors | |
3 | * | |
4 | * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de> | |
5 | * modified for PixelView (BT878P+W/FM) by | |
6 | * Michal Kochanowicz <mkochano@pld.org.pl> | |
7 | * Christoph Bartelmus <lirc@bartelmus.de> | |
8 | * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by | |
9 | * Ulrich Mueller <ulrich.mueller42@web.de> | |
10 | * modified for Asus TV-Box and Creative/VisionTek BreakOut-Box by | |
11 | * Stefan Jahn <stefan@lkcc.org> | |
12 | * modified for inclusion into kernel sources by | |
13 | * Jerome Brock <jbrock@users.sourceforge.net> | |
14 | * modified for Leadtek Winfast PVR2000 by | |
15 | * Thomas Reitmayr (treitmayr@yahoo.com) | |
16 | * modified for Hauppauge PVR-150 IR TX device by | |
17 | * Mark Weaver <mark@npsl.co.uk> | |
18 | * changed name from lirc_pvr150 to lirc_zilog, works on more than pvr-150 | |
19 | * Jarod Wilson <jarod@redhat.com> | |
20 | * | |
21 | * parts are cut&pasted from the lirc_i2c.c driver | |
22 | * | |
c2790c71 AW |
23 | * Numerous changes updating lirc_zilog.c in kernel 2.6.38 and later are |
24 | * Copyright (C) 2011 Andy Walls <awalls@md.metrocast.net> | |
25 | * | |
69b1214c JW |
26 | * This program is free software; you can redistribute it and/or modify |
27 | * it under the terms of the GNU General Public License as published by | |
28 | * the Free Software Foundation; either version 2 of the License, or | |
29 | * (at your option) any later version. | |
30 | * | |
31 | * This program is distributed in the hope that it will be useful, | |
32 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
33 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
34 | * GNU General Public License for more details. | |
35 | * | |
36 | * You should have received a copy of the GNU General Public License | |
37 | * along with this program; if not, write to the Free Software | |
38 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
39 | * | |
40 | */ | |
41 | ||
42 | ||
43 | #include <linux/version.h> | |
44 | #include <linux/module.h> | |
45 | #include <linux/kmod.h> | |
46 | #include <linux/kernel.h> | |
47 | #include <linux/sched.h> | |
48 | #include <linux/fs.h> | |
49 | #include <linux/poll.h> | |
50 | #include <linux/string.h> | |
51 | #include <linux/timer.h> | |
52 | #include <linux/delay.h> | |
53 | #include <linux/completion.h> | |
54 | #include <linux/errno.h> | |
55 | #include <linux/slab.h> | |
56 | #include <linux/i2c.h> | |
57 | #include <linux/firmware.h> | |
58 | #include <linux/vmalloc.h> | |
59 | ||
60 | #include <linux/mutex.h> | |
61 | #include <linux/kthread.h> | |
62 | ||
63 | #include <media/lirc_dev.h> | |
64 | #include <media/lirc.h> | |
65 | ||
06da95a3 | 66 | struct IR_rx { |
69b1214c | 67 | /* RX device */ |
e9b351f6 | 68 | struct i2c_client *c; |
69b1214c JW |
69 | |
70 | /* RX device buffer & lock */ | |
71 | struct lirc_buffer buf; | |
72 | struct mutex buf_lock; | |
73 | ||
74 | /* RX polling thread data */ | |
69b1214c JW |
75 | struct task_struct *task; |
76 | ||
77 | /* RX read data */ | |
78 | unsigned char b[3]; | |
06da95a3 AW |
79 | bool hdpvr_data_fmt; |
80 | }; | |
69b1214c | 81 | |
06da95a3 | 82 | struct IR_tx { |
69b1214c | 83 | /* TX device */ |
e9b351f6 | 84 | struct i2c_client *c; |
06da95a3 AW |
85 | |
86 | /* TX additional actions needed */ | |
69b1214c | 87 | int need_boot; |
06da95a3 AW |
88 | bool post_tx_ready_poll; |
89 | }; | |
90 | ||
91 | struct IR { | |
5c07134f AW |
92 | struct list_head list; |
93 | ||
06da95a3 AW |
94 | struct lirc_driver l; |
95 | ||
96 | struct mutex ir_lock; | |
97 | int open; | |
98 | ||
a68a9b73 | 99 | struct i2c_adapter *adapter; |
06da95a3 AW |
100 | struct IR_rx *rx; |
101 | struct IR_tx *tx; | |
69b1214c JW |
102 | }; |
103 | ||
5c07134f AW |
104 | /* IR transceiver instance object list */ |
105 | static DEFINE_MUTEX(ir_devices_lock); | |
106 | static LIST_HEAD(ir_devices_list); | |
69b1214c JW |
107 | |
108 | /* Block size for IR transmitter */ | |
109 | #define TX_BLOCK_SIZE 99 | |
110 | ||
111 | /* Hauppauge IR transmitter data */ | |
112 | struct tx_data_struct { | |
113 | /* Boot block */ | |
114 | unsigned char *boot_data; | |
115 | ||
116 | /* Start of binary data block */ | |
117 | unsigned char *datap; | |
118 | ||
119 | /* End of binary data block */ | |
120 | unsigned char *endp; | |
121 | ||
122 | /* Number of installed codesets */ | |
123 | unsigned int num_code_sets; | |
124 | ||
125 | /* Pointers to codesets */ | |
126 | unsigned char **code_sets; | |
127 | ||
128 | /* Global fixed data template */ | |
129 | int fixed[TX_BLOCK_SIZE]; | |
130 | }; | |
131 | ||
132 | static struct tx_data_struct *tx_data; | |
133 | static struct mutex tx_data_lock; | |
134 | ||
135 | #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \ | |
136 | ## args) | |
137 | #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args) | |
a68a9b73 | 138 | #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args) |
69b1214c | 139 | |
69b1214c JW |
140 | /* module parameters */ |
141 | static int debug; /* debug output */ | |
a68a9b73 | 142 | static int tx_only; /* only handle the IR Tx function */ |
69b1214c JW |
143 | static int minor = -1; /* minor number */ |
144 | ||
145 | #define dprintk(fmt, args...) \ | |
146 | do { \ | |
147 | if (debug) \ | |
148 | printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \ | |
149 | ## args); \ | |
150 | } while (0) | |
151 | ||
152 | static int add_to_buf(struct IR *ir) | |
153 | { | |
154 | __u16 code; | |
155 | unsigned char codes[2]; | |
156 | unsigned char keybuf[6]; | |
157 | int got_data = 0; | |
158 | int ret; | |
159 | int failures = 0; | |
160 | unsigned char sendbuf[1] = { 0 }; | |
06da95a3 AW |
161 | struct IR_rx *rx = ir->rx; |
162 | ||
163 | if (rx == NULL) | |
164 | return -ENXIO; | |
69b1214c | 165 | |
06da95a3 | 166 | if (lirc_buffer_full(&rx->buf)) { |
69b1214c JW |
167 | dprintk("buffer overflow\n"); |
168 | return -EOVERFLOW; | |
169 | } | |
170 | ||
171 | /* | |
172 | * service the device as long as it is returning | |
173 | * data and we have space | |
174 | */ | |
175 | do { | |
b757730b AW |
176 | if (kthread_should_stop()) |
177 | return -ENODATA; | |
178 | ||
69b1214c JW |
179 | /* |
180 | * Lock i2c bus for the duration. RX/TX chips interfere so | |
181 | * this is worth it | |
182 | */ | |
183 | mutex_lock(&ir->ir_lock); | |
184 | ||
b757730b AW |
185 | if (kthread_should_stop()) { |
186 | mutex_unlock(&ir->ir_lock); | |
187 | return -ENODATA; | |
188 | } | |
189 | ||
69b1214c JW |
190 | /* |
191 | * Send random "poll command" (?) Windows driver does this | |
192 | * and it is a good point to detect chip failure. | |
193 | */ | |
e9b351f6 | 194 | ret = i2c_master_send(rx->c, sendbuf, 1); |
69b1214c JW |
195 | if (ret != 1) { |
196 | zilog_error("i2c_master_send failed with %d\n", ret); | |
197 | if (failures >= 3) { | |
198 | mutex_unlock(&ir->ir_lock); | |
199 | zilog_error("unable to read from the IR chip " | |
200 | "after 3 resets, giving up\n"); | |
201 | return ret; | |
202 | } | |
203 | ||
204 | /* Looks like the chip crashed, reset it */ | |
205 | zilog_error("polling the IR receiver chip failed, " | |
206 | "trying reset\n"); | |
207 | ||
208 | set_current_state(TASK_UNINTERRUPTIBLE); | |
b757730b AW |
209 | if (kthread_should_stop()) { |
210 | mutex_unlock(&ir->ir_lock); | |
211 | return -ENODATA; | |
212 | } | |
69b1214c | 213 | schedule_timeout((100 * HZ + 999) / 1000); |
12d896e1 AW |
214 | if (ir->tx != NULL) |
215 | ir->tx->need_boot = 1; | |
69b1214c JW |
216 | |
217 | ++failures; | |
218 | mutex_unlock(&ir->ir_lock); | |
219 | continue; | |
220 | } | |
221 | ||
b757730b AW |
222 | if (kthread_should_stop()) { |
223 | mutex_unlock(&ir->ir_lock); | |
224 | return -ENODATA; | |
225 | } | |
e9b351f6 | 226 | ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf)); |
69b1214c JW |
227 | mutex_unlock(&ir->ir_lock); |
228 | if (ret != sizeof(keybuf)) { | |
229 | zilog_error("i2c_master_recv failed with %d -- " | |
230 | "keeping last read buffer\n", ret); | |
231 | } else { | |
06da95a3 AW |
232 | rx->b[0] = keybuf[3]; |
233 | rx->b[1] = keybuf[4]; | |
234 | rx->b[2] = keybuf[5]; | |
235 | dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]); | |
69b1214c JW |
236 | } |
237 | ||
238 | /* key pressed ? */ | |
06da95a3 | 239 | if (rx->hdpvr_data_fmt) { |
69b1214c JW |
240 | if (got_data && (keybuf[0] == 0x80)) |
241 | return 0; | |
242 | else if (got_data && (keybuf[0] == 0x00)) | |
243 | return -ENODATA; | |
06da95a3 | 244 | } else if ((rx->b[0] & 0x80) == 0) |
69b1214c JW |
245 | return got_data ? 0 : -ENODATA; |
246 | ||
247 | /* look what we have */ | |
06da95a3 | 248 | code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2); |
69b1214c JW |
249 | |
250 | codes[0] = (code >> 8) & 0xff; | |
251 | codes[1] = code & 0xff; | |
252 | ||
253 | /* return it */ | |
06da95a3 | 254 | lirc_buffer_write(&rx->buf, codes); |
69b1214c | 255 | ++got_data; |
06da95a3 | 256 | } while (!lirc_buffer_full(&rx->buf)); |
69b1214c JW |
257 | |
258 | return 0; | |
259 | } | |
260 | ||
261 | /* | |
262 | * Main function of the polling thread -- from lirc_dev. | |
263 | * We don't fit the LIRC model at all anymore. This is horrible, but | |
264 | * basically we have a single RX/TX device with a nasty failure mode | |
265 | * that needs to be accounted for across the pair. lirc lets us provide | |
266 | * fops, but prevents us from using the internal polling, etc. if we do | |
267 | * so. Hence the replication. Might be neater to extend the LIRC model | |
268 | * to account for this but I'd think it's a very special case of seriously | |
269 | * messed up hardware. | |
270 | */ | |
271 | static int lirc_thread(void *arg) | |
272 | { | |
273 | struct IR *ir = arg; | |
06da95a3 | 274 | struct IR_rx *rx = ir->rx; |
69b1214c | 275 | |
69b1214c JW |
276 | dprintk("poll thread started\n"); |
277 | ||
b757730b AW |
278 | while (!kthread_should_stop()) { |
279 | set_current_state(TASK_INTERRUPTIBLE); | |
69b1214c | 280 | |
b757730b AW |
281 | /* if device not opened, we can sleep half a second */ |
282 | if (!ir->open) { | |
69b1214c | 283 | schedule_timeout(HZ/2); |
b757730b | 284 | continue; |
69b1214c | 285 | } |
69b1214c | 286 | |
b757730b AW |
287 | /* |
288 | * This is ~113*2 + 24 + jitter (2*repeat gap + code length). | |
289 | * We use this interval as the chip resets every time you poll | |
290 | * it (bad!). This is therefore just sufficient to catch all | |
291 | * of the button presses. It makes the remote much more | |
292 | * responsive. You can see the difference by running irw and | |
293 | * holding down a button. With 100ms, the old polling | |
294 | * interval, you'll notice breaks in the repeat sequence | |
295 | * corresponding to lost keypresses. | |
296 | */ | |
297 | schedule_timeout((260 * HZ) / 1000); | |
298 | if (kthread_should_stop()) | |
299 | break; | |
300 | if (!add_to_buf(ir)) | |
301 | wake_up_interruptible(&rx->buf.wait_poll); | |
302 | } | |
69b1214c JW |
303 | |
304 | dprintk("poll thread ended\n"); | |
305 | return 0; | |
306 | } | |
307 | ||
308 | static int set_use_inc(void *data) | |
309 | { | |
69b1214c JW |
310 | return 0; |
311 | } | |
312 | ||
313 | static void set_use_dec(void *data) | |
314 | { | |
9b28500a | 315 | return; |
69b1214c JW |
316 | } |
317 | ||
318 | /* safe read of a uint32 (always network byte order) */ | |
319 | static int read_uint32(unsigned char **data, | |
320 | unsigned char *endp, unsigned int *val) | |
321 | { | |
322 | if (*data + 4 > endp) | |
323 | return 0; | |
324 | *val = ((*data)[0] << 24) | ((*data)[1] << 16) | | |
325 | ((*data)[2] << 8) | (*data)[3]; | |
326 | *data += 4; | |
327 | return 1; | |
328 | } | |
329 | ||
330 | /* safe read of a uint8 */ | |
331 | static int read_uint8(unsigned char **data, | |
332 | unsigned char *endp, unsigned char *val) | |
333 | { | |
334 | if (*data + 1 > endp) | |
335 | return 0; | |
336 | *val = *((*data)++); | |
337 | return 1; | |
338 | } | |
339 | ||
340 | /* safe skipping of N bytes */ | |
341 | static int skip(unsigned char **data, | |
342 | unsigned char *endp, unsigned int distance) | |
343 | { | |
344 | if (*data + distance > endp) | |
345 | return 0; | |
346 | *data += distance; | |
347 | return 1; | |
348 | } | |
349 | ||
350 | /* decompress key data into the given buffer */ | |
351 | static int get_key_data(unsigned char *buf, | |
352 | unsigned int codeset, unsigned int key) | |
353 | { | |
354 | unsigned char *data, *endp, *diffs, *key_block; | |
355 | unsigned char keys, ndiffs, id; | |
356 | unsigned int base, lim, pos, i; | |
357 | ||
358 | /* Binary search for the codeset */ | |
359 | for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) { | |
360 | pos = base + (lim >> 1); | |
361 | data = tx_data->code_sets[pos]; | |
362 | ||
363 | if (!read_uint32(&data, tx_data->endp, &i)) | |
364 | goto corrupt; | |
365 | ||
366 | if (i == codeset) | |
367 | break; | |
368 | else if (codeset > i) { | |
369 | base = pos + 1; | |
370 | --lim; | |
371 | } | |
372 | } | |
373 | /* Not found? */ | |
374 | if (!lim) | |
375 | return -EPROTO; | |
376 | ||
377 | /* Set end of data block */ | |
378 | endp = pos < tx_data->num_code_sets - 1 ? | |
379 | tx_data->code_sets[pos + 1] : tx_data->endp; | |
380 | ||
381 | /* Read the block header */ | |
382 | if (!read_uint8(&data, endp, &keys) || | |
383 | !read_uint8(&data, endp, &ndiffs) || | |
384 | ndiffs > TX_BLOCK_SIZE || keys == 0) | |
385 | goto corrupt; | |
386 | ||
387 | /* Save diffs & skip */ | |
388 | diffs = data; | |
389 | if (!skip(&data, endp, ndiffs)) | |
390 | goto corrupt; | |
391 | ||
392 | /* Read the id of the first key */ | |
393 | if (!read_uint8(&data, endp, &id)) | |
394 | goto corrupt; | |
395 | ||
396 | /* Unpack the first key's data */ | |
397 | for (i = 0; i < TX_BLOCK_SIZE; ++i) { | |
398 | if (tx_data->fixed[i] == -1) { | |
399 | if (!read_uint8(&data, endp, &buf[i])) | |
400 | goto corrupt; | |
401 | } else { | |
402 | buf[i] = (unsigned char)tx_data->fixed[i]; | |
403 | } | |
404 | } | |
405 | ||
406 | /* Early out key found/not found */ | |
407 | if (key == id) | |
408 | return 0; | |
409 | if (keys == 1) | |
410 | return -EPROTO; | |
411 | ||
412 | /* Sanity check */ | |
413 | key_block = data; | |
414 | if (!skip(&data, endp, (keys - 1) * (ndiffs + 1))) | |
415 | goto corrupt; | |
416 | ||
417 | /* Binary search for the key */ | |
418 | for (base = 0, lim = keys - 1; lim; lim >>= 1) { | |
419 | /* Seek to block */ | |
420 | unsigned char *key_data; | |
421 | pos = base + (lim >> 1); | |
422 | key_data = key_block + (ndiffs + 1) * pos; | |
423 | ||
424 | if (*key_data == key) { | |
425 | /* skip key id */ | |
426 | ++key_data; | |
427 | ||
428 | /* found, so unpack the diffs */ | |
429 | for (i = 0; i < ndiffs; ++i) { | |
430 | unsigned char val; | |
431 | if (!read_uint8(&key_data, endp, &val) || | |
432 | diffs[i] >= TX_BLOCK_SIZE) | |
433 | goto corrupt; | |
434 | buf[diffs[i]] = val; | |
435 | } | |
436 | ||
437 | return 0; | |
438 | } else if (key > *key_data) { | |
439 | base = pos + 1; | |
440 | --lim; | |
441 | } | |
442 | } | |
443 | /* Key not found */ | |
444 | return -EPROTO; | |
445 | ||
446 | corrupt: | |
447 | zilog_error("firmware is corrupt\n"); | |
448 | return -EFAULT; | |
449 | } | |
450 | ||
451 | /* send a block of data to the IR TX device */ | |
06da95a3 | 452 | static int send_data_block(struct IR_tx *tx, unsigned char *data_block) |
69b1214c JW |
453 | { |
454 | int i, j, ret; | |
455 | unsigned char buf[5]; | |
456 | ||
457 | for (i = 0; i < TX_BLOCK_SIZE;) { | |
458 | int tosend = TX_BLOCK_SIZE - i; | |
459 | if (tosend > 4) | |
460 | tosend = 4; | |
461 | buf[0] = (unsigned char)(i + 1); | |
462 | for (j = 0; j < tosend; ++j) | |
463 | buf[1 + j] = data_block[i + j]; | |
464 | dprintk("%02x %02x %02x %02x %02x", | |
465 | buf[0], buf[1], buf[2], buf[3], buf[4]); | |
e9b351f6 | 466 | ret = i2c_master_send(tx->c, buf, tosend + 1); |
69b1214c JW |
467 | if (ret != tosend + 1) { |
468 | zilog_error("i2c_master_send failed with %d\n", ret); | |
469 | return ret < 0 ? ret : -EFAULT; | |
470 | } | |
471 | i += tosend; | |
472 | } | |
473 | return 0; | |
474 | } | |
475 | ||
476 | /* send boot data to the IR TX device */ | |
06da95a3 | 477 | static int send_boot_data(struct IR_tx *tx) |
69b1214c | 478 | { |
5766d204 | 479 | int ret, i; |
69b1214c JW |
480 | unsigned char buf[4]; |
481 | ||
482 | /* send the boot block */ | |
06da95a3 | 483 | ret = send_data_block(tx, tx_data->boot_data); |
69b1214c JW |
484 | if (ret != 0) |
485 | return ret; | |
486 | ||
5766d204 | 487 | /* Hit the go button to activate the new boot data */ |
69b1214c JW |
488 | buf[0] = 0x00; |
489 | buf[1] = 0x20; | |
e9b351f6 | 490 | ret = i2c_master_send(tx->c, buf, 2); |
69b1214c JW |
491 | if (ret != 2) { |
492 | zilog_error("i2c_master_send failed with %d\n", ret); | |
493 | return ret < 0 ? ret : -EFAULT; | |
494 | } | |
5766d204 JW |
495 | |
496 | /* | |
497 | * Wait for zilog to settle after hitting go post boot block upload. | |
498 | * Without this delay, the HD-PVR and HVR-1950 both return an -EIO | |
499 | * upon attempting to get firmware revision, and tx probe thus fails. | |
500 | */ | |
501 | for (i = 0; i < 10; i++) { | |
502 | ret = i2c_master_send(tx->c, buf, 1); | |
503 | if (ret == 1) | |
504 | break; | |
505 | udelay(100); | |
506 | } | |
507 | ||
69b1214c JW |
508 | if (ret != 1) { |
509 | zilog_error("i2c_master_send failed with %d\n", ret); | |
510 | return ret < 0 ? ret : -EFAULT; | |
511 | } | |
512 | ||
513 | /* Here comes the firmware version... (hopefully) */ | |
e9b351f6 | 514 | ret = i2c_master_recv(tx->c, buf, 4); |
69b1214c JW |
515 | if (ret != 4) { |
516 | zilog_error("i2c_master_recv failed with %d\n", ret); | |
517 | return 0; | |
518 | } | |
5766d204 JW |
519 | if ((buf[0] != 0x80) && (buf[0] != 0xa0)) { |
520 | zilog_error("unexpected IR TX init response: %02x\n", buf[0]); | |
69b1214c JW |
521 | return 0; |
522 | } | |
523 | zilog_notify("Zilog/Hauppauge IR blaster firmware version " | |
524 | "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]); | |
525 | ||
526 | return 0; | |
527 | } | |
528 | ||
529 | /* unload "firmware", lock held */ | |
530 | static void fw_unload_locked(void) | |
531 | { | |
532 | if (tx_data) { | |
533 | if (tx_data->code_sets) | |
534 | vfree(tx_data->code_sets); | |
535 | ||
536 | if (tx_data->datap) | |
537 | vfree(tx_data->datap); | |
538 | ||
539 | vfree(tx_data); | |
540 | tx_data = NULL; | |
541 | dprintk("successfully unloaded IR blaster firmware\n"); | |
542 | } | |
543 | } | |
544 | ||
545 | /* unload "firmware" for the IR TX device */ | |
546 | static void fw_unload(void) | |
547 | { | |
548 | mutex_lock(&tx_data_lock); | |
549 | fw_unload_locked(); | |
550 | mutex_unlock(&tx_data_lock); | |
551 | } | |
552 | ||
553 | /* load "firmware" for the IR TX device */ | |
06da95a3 | 554 | static int fw_load(struct IR_tx *tx) |
69b1214c JW |
555 | { |
556 | int ret; | |
557 | unsigned int i; | |
558 | unsigned char *data, version, num_global_fixed; | |
559 | const struct firmware *fw_entry; | |
560 | ||
561 | /* Already loaded? */ | |
562 | mutex_lock(&tx_data_lock); | |
563 | if (tx_data) { | |
564 | ret = 0; | |
565 | goto out; | |
566 | } | |
567 | ||
568 | /* Request codeset data file */ | |
e9b351f6 | 569 | ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev); |
69b1214c JW |
570 | if (ret != 0) { |
571 | zilog_error("firmware haup-ir-blaster.bin not available " | |
572 | "(%d)\n", ret); | |
573 | ret = ret < 0 ? ret : -EFAULT; | |
574 | goto out; | |
575 | } | |
576 | dprintk("firmware of size %zu loaded\n", fw_entry->size); | |
577 | ||
578 | /* Parse the file */ | |
579 | tx_data = vmalloc(sizeof(*tx_data)); | |
580 | if (tx_data == NULL) { | |
581 | zilog_error("out of memory\n"); | |
582 | release_firmware(fw_entry); | |
583 | ret = -ENOMEM; | |
584 | goto out; | |
585 | } | |
586 | tx_data->code_sets = NULL; | |
587 | ||
588 | /* Copy the data so hotplug doesn't get confused and timeout */ | |
589 | tx_data->datap = vmalloc(fw_entry->size); | |
590 | if (tx_data->datap == NULL) { | |
591 | zilog_error("out of memory\n"); | |
592 | release_firmware(fw_entry); | |
593 | vfree(tx_data); | |
594 | ret = -ENOMEM; | |
595 | goto out; | |
596 | } | |
597 | memcpy(tx_data->datap, fw_entry->data, fw_entry->size); | |
598 | tx_data->endp = tx_data->datap + fw_entry->size; | |
599 | release_firmware(fw_entry); fw_entry = NULL; | |
600 | ||
601 | /* Check version */ | |
602 | data = tx_data->datap; | |
603 | if (!read_uint8(&data, tx_data->endp, &version)) | |
604 | goto corrupt; | |
605 | if (version != 1) { | |
606 | zilog_error("unsupported code set file version (%u, expected" | |
607 | "1) -- please upgrade to a newer driver", | |
608 | version); | |
609 | fw_unload_locked(); | |
610 | ret = -EFAULT; | |
611 | goto out; | |
612 | } | |
613 | ||
614 | /* Save boot block for later */ | |
615 | tx_data->boot_data = data; | |
616 | if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE)) | |
617 | goto corrupt; | |
618 | ||
619 | if (!read_uint32(&data, tx_data->endp, | |
620 | &tx_data->num_code_sets)) | |
621 | goto corrupt; | |
622 | ||
623 | dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets); | |
624 | ||
625 | tx_data->code_sets = vmalloc( | |
626 | tx_data->num_code_sets * sizeof(char *)); | |
627 | if (tx_data->code_sets == NULL) { | |
628 | fw_unload_locked(); | |
629 | ret = -ENOMEM; | |
630 | goto out; | |
631 | } | |
632 | ||
633 | for (i = 0; i < TX_BLOCK_SIZE; ++i) | |
634 | tx_data->fixed[i] = -1; | |
635 | ||
636 | /* Read global fixed data template */ | |
637 | if (!read_uint8(&data, tx_data->endp, &num_global_fixed) || | |
638 | num_global_fixed > TX_BLOCK_SIZE) | |
639 | goto corrupt; | |
640 | for (i = 0; i < num_global_fixed; ++i) { | |
641 | unsigned char pos, val; | |
642 | if (!read_uint8(&data, tx_data->endp, &pos) || | |
643 | !read_uint8(&data, tx_data->endp, &val) || | |
644 | pos >= TX_BLOCK_SIZE) | |
645 | goto corrupt; | |
646 | tx_data->fixed[pos] = (int)val; | |
647 | } | |
648 | ||
649 | /* Filch out the position of each code set */ | |
650 | for (i = 0; i < tx_data->num_code_sets; ++i) { | |
651 | unsigned int id; | |
652 | unsigned char keys; | |
653 | unsigned char ndiffs; | |
654 | ||
655 | /* Save the codeset position */ | |
656 | tx_data->code_sets[i] = data; | |
657 | ||
658 | /* Read header */ | |
659 | if (!read_uint32(&data, tx_data->endp, &id) || | |
660 | !read_uint8(&data, tx_data->endp, &keys) || | |
661 | !read_uint8(&data, tx_data->endp, &ndiffs) || | |
662 | ndiffs > TX_BLOCK_SIZE || keys == 0) | |
663 | goto corrupt; | |
664 | ||
665 | /* skip diff positions */ | |
666 | if (!skip(&data, tx_data->endp, ndiffs)) | |
667 | goto corrupt; | |
668 | ||
669 | /* | |
670 | * After the diffs we have the first key id + data - | |
671 | * global fixed | |
672 | */ | |
673 | if (!skip(&data, tx_data->endp, | |
674 | 1 + TX_BLOCK_SIZE - num_global_fixed)) | |
675 | goto corrupt; | |
676 | ||
677 | /* Then we have keys-1 blocks of key id+diffs */ | |
678 | if (!skip(&data, tx_data->endp, | |
679 | (ndiffs + 1) * (keys - 1))) | |
680 | goto corrupt; | |
681 | } | |
682 | ret = 0; | |
683 | goto out; | |
684 | ||
685 | corrupt: | |
686 | zilog_error("firmware is corrupt\n"); | |
687 | fw_unload_locked(); | |
688 | ret = -EFAULT; | |
689 | ||
690 | out: | |
691 | mutex_unlock(&tx_data_lock); | |
692 | return ret; | |
693 | } | |
694 | ||
695 | /* initialise the IR TX device */ | |
06da95a3 | 696 | static int tx_init(struct IR_tx *tx) |
69b1214c JW |
697 | { |
698 | int ret; | |
699 | ||
700 | /* Load 'firmware' */ | |
06da95a3 | 701 | ret = fw_load(tx); |
69b1214c JW |
702 | if (ret != 0) |
703 | return ret; | |
704 | ||
705 | /* Send boot block */ | |
06da95a3 | 706 | ret = send_boot_data(tx); |
69b1214c JW |
707 | if (ret != 0) |
708 | return ret; | |
06da95a3 | 709 | tx->need_boot = 0; |
69b1214c JW |
710 | |
711 | /* Looks good */ | |
712 | return 0; | |
713 | } | |
714 | ||
715 | /* do nothing stub to make LIRC happy */ | |
716 | static loff_t lseek(struct file *filep, loff_t offset, int orig) | |
717 | { | |
718 | return -ESPIPE; | |
719 | } | |
720 | ||
721 | /* copied from lirc_dev */ | |
722 | static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos) | |
723 | { | |
e0ac7da0 | 724 | struct IR *ir = filep->private_data; |
06da95a3 | 725 | struct IR_rx *rx = ir->rx; |
69b1214c JW |
726 | int ret = 0, written = 0; |
727 | DECLARE_WAITQUEUE(wait, current); | |
728 | ||
729 | dprintk("read called\n"); | |
06da95a3 | 730 | if (rx == NULL) |
69b1214c JW |
731 | return -ENODEV; |
732 | ||
06da95a3 | 733 | if (mutex_lock_interruptible(&rx->buf_lock)) |
69b1214c JW |
734 | return -ERESTARTSYS; |
735 | ||
06da95a3 | 736 | if (n % rx->buf.chunk_size) { |
69b1214c | 737 | dprintk("read result = -EINVAL\n"); |
06da95a3 | 738 | mutex_unlock(&rx->buf_lock); |
69b1214c JW |
739 | return -EINVAL; |
740 | } | |
741 | ||
742 | /* | |
743 | * we add ourselves to the task queue before buffer check | |
744 | * to avoid losing scan code (in case when queue is awaken somewhere | |
745 | * between while condition checking and scheduling) | |
746 | */ | |
06da95a3 | 747 | add_wait_queue(&rx->buf.wait_poll, &wait); |
69b1214c JW |
748 | set_current_state(TASK_INTERRUPTIBLE); |
749 | ||
750 | /* | |
751 | * while we didn't provide 'length' bytes, device is opened in blocking | |
752 | * mode and 'copy_to_user' is happy, wait for data. | |
753 | */ | |
754 | while (written < n && ret == 0) { | |
06da95a3 | 755 | if (lirc_buffer_empty(&rx->buf)) { |
69b1214c JW |
756 | /* |
757 | * According to the read(2) man page, 'written' can be | |
758 | * returned as less than 'n', instead of blocking | |
759 | * again, returning -EWOULDBLOCK, or returning | |
760 | * -ERESTARTSYS | |
761 | */ | |
762 | if (written) | |
763 | break; | |
764 | if (filep->f_flags & O_NONBLOCK) { | |
765 | ret = -EWOULDBLOCK; | |
766 | break; | |
767 | } | |
768 | if (signal_pending(current)) { | |
769 | ret = -ERESTARTSYS; | |
770 | break; | |
771 | } | |
772 | schedule(); | |
773 | set_current_state(TASK_INTERRUPTIBLE); | |
774 | } else { | |
06da95a3 AW |
775 | unsigned char buf[rx->buf.chunk_size]; |
776 | lirc_buffer_read(&rx->buf, buf); | |
69b1214c | 777 | ret = copy_to_user((void *)outbuf+written, buf, |
06da95a3 AW |
778 | rx->buf.chunk_size); |
779 | written += rx->buf.chunk_size; | |
69b1214c JW |
780 | } |
781 | } | |
782 | ||
06da95a3 | 783 | remove_wait_queue(&rx->buf.wait_poll, &wait); |
69b1214c | 784 | set_current_state(TASK_RUNNING); |
06da95a3 | 785 | mutex_unlock(&rx->buf_lock); |
69b1214c JW |
786 | |
787 | dprintk("read result = %s (%d)\n", | |
788 | ret ? "-EFAULT" : "OK", ret); | |
789 | ||
790 | return ret ? ret : written; | |
791 | } | |
792 | ||
793 | /* send a keypress to the IR TX device */ | |
06da95a3 | 794 | static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key) |
69b1214c JW |
795 | { |
796 | unsigned char data_block[TX_BLOCK_SIZE]; | |
797 | unsigned char buf[2]; | |
798 | int i, ret; | |
799 | ||
800 | /* Get data for the codeset/key */ | |
801 | ret = get_key_data(data_block, code, key); | |
802 | ||
803 | if (ret == -EPROTO) { | |
804 | zilog_error("failed to get data for code %u, key %u -- check " | |
805 | "lircd.conf entries\n", code, key); | |
806 | return ret; | |
807 | } else if (ret != 0) | |
808 | return ret; | |
809 | ||
810 | /* Send the data block */ | |
06da95a3 | 811 | ret = send_data_block(tx, data_block); |
69b1214c JW |
812 | if (ret != 0) |
813 | return ret; | |
814 | ||
815 | /* Send data block length? */ | |
816 | buf[0] = 0x00; | |
817 | buf[1] = 0x40; | |
e9b351f6 | 818 | ret = i2c_master_send(tx->c, buf, 2); |
69b1214c JW |
819 | if (ret != 2) { |
820 | zilog_error("i2c_master_send failed with %d\n", ret); | |
821 | return ret < 0 ? ret : -EFAULT; | |
822 | } | |
5766d204 JW |
823 | |
824 | /* Give the z8 a moment to process data block */ | |
825 | for (i = 0; i < 10; i++) { | |
826 | ret = i2c_master_send(tx->c, buf, 1); | |
827 | if (ret == 1) | |
828 | break; | |
829 | udelay(100); | |
830 | } | |
831 | ||
69b1214c JW |
832 | if (ret != 1) { |
833 | zilog_error("i2c_master_send failed with %d\n", ret); | |
834 | return ret < 0 ? ret : -EFAULT; | |
835 | } | |
836 | ||
837 | /* Send finished download? */ | |
e9b351f6 | 838 | ret = i2c_master_recv(tx->c, buf, 1); |
69b1214c JW |
839 | if (ret != 1) { |
840 | zilog_error("i2c_master_recv failed with %d\n", ret); | |
841 | return ret < 0 ? ret : -EFAULT; | |
842 | } | |
843 | if (buf[0] != 0xA0) { | |
844 | zilog_error("unexpected IR TX response #1: %02x\n", | |
845 | buf[0]); | |
846 | return -EFAULT; | |
847 | } | |
848 | ||
849 | /* Send prepare command? */ | |
850 | buf[0] = 0x00; | |
851 | buf[1] = 0x80; | |
e9b351f6 | 852 | ret = i2c_master_send(tx->c, buf, 2); |
69b1214c JW |
853 | if (ret != 2) { |
854 | zilog_error("i2c_master_send failed with %d\n", ret); | |
855 | return ret < 0 ? ret : -EFAULT; | |
856 | } | |
857 | ||
69b1214c JW |
858 | /* |
859 | * The sleep bits aren't necessary on the HD PVR, and in fact, the | |
860 | * last i2c_master_recv always fails with a -5, so for now, we're | |
861 | * going to skip this whole mess and say we're done on the HD PVR | |
862 | */ | |
06da95a3 | 863 | if (!tx->post_tx_ready_poll) { |
d7c72356 AW |
864 | dprintk("sent code %u, key %u\n", code, key); |
865 | return 0; | |
866 | } | |
69b1214c JW |
867 | |
868 | /* | |
869 | * This bit NAKs until the device is ready, so we retry it | |
870 | * sleeping a bit each time. This seems to be what the windows | |
871 | * driver does, approximately. | |
872 | * Try for up to 1s. | |
873 | */ | |
874 | for (i = 0; i < 20; ++i) { | |
875 | set_current_state(TASK_UNINTERRUPTIBLE); | |
876 | schedule_timeout((50 * HZ + 999) / 1000); | |
e9b351f6 | 877 | ret = i2c_master_send(tx->c, buf, 1); |
69b1214c JW |
878 | if (ret == 1) |
879 | break; | |
880 | dprintk("NAK expected: i2c_master_send " | |
881 | "failed with %d (try %d)\n", ret, i+1); | |
882 | } | |
883 | if (ret != 1) { | |
884 | zilog_error("IR TX chip never got ready: last i2c_master_send " | |
885 | "failed with %d\n", ret); | |
886 | return ret < 0 ? ret : -EFAULT; | |
887 | } | |
888 | ||
889 | /* Seems to be an 'ok' response */ | |
e9b351f6 | 890 | i = i2c_master_recv(tx->c, buf, 1); |
69b1214c JW |
891 | if (i != 1) { |
892 | zilog_error("i2c_master_recv failed with %d\n", ret); | |
893 | return -EFAULT; | |
894 | } | |
895 | if (buf[0] != 0x80) { | |
896 | zilog_error("unexpected IR TX response #2: %02x\n", buf[0]); | |
897 | return -EFAULT; | |
898 | } | |
899 | ||
69b1214c JW |
900 | /* Oh good, it worked */ |
901 | dprintk("sent code %u, key %u\n", code, key); | |
902 | return 0; | |
903 | } | |
904 | ||
905 | /* | |
906 | * Write a code to the device. We take in a 32-bit number (an int) and then | |
907 | * decode this to a codeset/key index. The key data is then decompressed and | |
908 | * sent to the device. We have a spin lock as per i2c documentation to prevent | |
909 | * multiple concurrent sends which would probably cause the device to explode. | |
910 | */ | |
911 | static ssize_t write(struct file *filep, const char *buf, size_t n, | |
912 | loff_t *ppos) | |
913 | { | |
e0ac7da0 | 914 | struct IR *ir = filep->private_data; |
06da95a3 | 915 | struct IR_tx *tx = ir->tx; |
69b1214c JW |
916 | size_t i; |
917 | int failures = 0; | |
918 | ||
06da95a3 | 919 | if (tx == NULL) |
69b1214c JW |
920 | return -ENODEV; |
921 | ||
922 | /* Validate user parameters */ | |
923 | if (n % sizeof(int)) | |
924 | return -EINVAL; | |
925 | ||
926 | /* Lock i2c bus for the duration */ | |
927 | mutex_lock(&ir->ir_lock); | |
928 | ||
929 | /* Send each keypress */ | |
930 | for (i = 0; i < n;) { | |
931 | int ret = 0; | |
932 | int command; | |
933 | ||
934 | if (copy_from_user(&command, buf + i, sizeof(command))) { | |
935 | mutex_unlock(&ir->ir_lock); | |
936 | return -EFAULT; | |
937 | } | |
938 | ||
939 | /* Send boot data first if required */ | |
06da95a3 AW |
940 | if (tx->need_boot == 1) { |
941 | ret = send_boot_data(tx); | |
69b1214c | 942 | if (ret == 0) |
06da95a3 | 943 | tx->need_boot = 0; |
69b1214c JW |
944 | } |
945 | ||
946 | /* Send the code */ | |
947 | if (ret == 0) { | |
06da95a3 | 948 | ret = send_code(tx, (unsigned)command >> 16, |
69b1214c JW |
949 | (unsigned)command & 0xFFFF); |
950 | if (ret == -EPROTO) { | |
951 | mutex_unlock(&ir->ir_lock); | |
952 | return ret; | |
953 | } | |
954 | } | |
955 | ||
956 | /* | |
957 | * Hmm, a failure. If we've had a few then give up, otherwise | |
958 | * try a reset | |
959 | */ | |
960 | if (ret != 0) { | |
961 | /* Looks like the chip crashed, reset it */ | |
962 | zilog_error("sending to the IR transmitter chip " | |
963 | "failed, trying reset\n"); | |
964 | ||
965 | if (failures >= 3) { | |
966 | zilog_error("unable to send to the IR chip " | |
967 | "after 3 resets, giving up\n"); | |
968 | mutex_unlock(&ir->ir_lock); | |
969 | return ret; | |
970 | } | |
971 | set_current_state(TASK_UNINTERRUPTIBLE); | |
972 | schedule_timeout((100 * HZ + 999) / 1000); | |
06da95a3 | 973 | tx->need_boot = 1; |
69b1214c JW |
974 | ++failures; |
975 | } else | |
976 | i += sizeof(int); | |
977 | } | |
978 | ||
979 | /* Release i2c bus */ | |
980 | mutex_unlock(&ir->ir_lock); | |
981 | ||
982 | /* All looks good */ | |
983 | return n; | |
984 | } | |
985 | ||
986 | /* copied from lirc_dev */ | |
987 | static unsigned int poll(struct file *filep, poll_table *wait) | |
988 | { | |
e0ac7da0 | 989 | struct IR *ir = filep->private_data; |
06da95a3 | 990 | struct IR_rx *rx = ir->rx; |
69b1214c JW |
991 | unsigned int ret; |
992 | ||
993 | dprintk("poll called\n"); | |
06da95a3 | 994 | if (rx == NULL) |
69b1214c JW |
995 | return -ENODEV; |
996 | ||
06da95a3 | 997 | mutex_lock(&rx->buf_lock); |
69b1214c | 998 | |
06da95a3 | 999 | poll_wait(filep, &rx->buf.wait_poll, wait); |
69b1214c JW |
1000 | |
1001 | dprintk("poll result = %s\n", | |
06da95a3 | 1002 | lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM"); |
69b1214c | 1003 | |
06da95a3 | 1004 | ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM); |
69b1214c | 1005 | |
06da95a3 | 1006 | mutex_unlock(&rx->buf_lock); |
69b1214c JW |
1007 | return ret; |
1008 | } | |
1009 | ||
1010 | static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg) | |
1011 | { | |
e0ac7da0 | 1012 | struct IR *ir = filep->private_data; |
69b1214c JW |
1013 | int result; |
1014 | unsigned long mode, features = 0; | |
1015 | ||
06da95a3 | 1016 | if (ir->rx != NULL) |
69b1214c | 1017 | features |= LIRC_CAN_REC_LIRCCODE; |
12d896e1 AW |
1018 | if (ir->tx != NULL) |
1019 | features |= LIRC_CAN_SEND_PULSE; | |
69b1214c JW |
1020 | |
1021 | switch (cmd) { | |
1022 | case LIRC_GET_LENGTH: | |
1023 | result = put_user((unsigned long)13, | |
1024 | (unsigned long *)arg); | |
1025 | break; | |
1026 | case LIRC_GET_FEATURES: | |
1027 | result = put_user(features, (unsigned long *) arg); | |
1028 | break; | |
1029 | case LIRC_GET_REC_MODE: | |
1030 | if (!(features&LIRC_CAN_REC_MASK)) | |
1031 | return -ENOSYS; | |
1032 | ||
1033 | result = put_user(LIRC_REC2MODE | |
1034 | (features&LIRC_CAN_REC_MASK), | |
1035 | (unsigned long *)arg); | |
1036 | break; | |
1037 | case LIRC_SET_REC_MODE: | |
1038 | if (!(features&LIRC_CAN_REC_MASK)) | |
1039 | return -ENOSYS; | |
1040 | ||
1041 | result = get_user(mode, (unsigned long *)arg); | |
1042 | if (!result && !(LIRC_MODE2REC(mode) & features)) | |
1043 | result = -EINVAL; | |
1044 | break; | |
1045 | case LIRC_GET_SEND_MODE: | |
12d896e1 AW |
1046 | if (!(features&LIRC_CAN_SEND_MASK)) |
1047 | return -ENOSYS; | |
1048 | ||
69b1214c JW |
1049 | result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg); |
1050 | break; | |
1051 | case LIRC_SET_SEND_MODE: | |
12d896e1 AW |
1052 | if (!(features&LIRC_CAN_SEND_MASK)) |
1053 | return -ENOSYS; | |
1054 | ||
69b1214c JW |
1055 | result = get_user(mode, (unsigned long *) arg); |
1056 | if (!result && mode != LIRC_MODE_PULSE) | |
1057 | return -EINVAL; | |
1058 | break; | |
1059 | default: | |
1060 | return -EINVAL; | |
1061 | } | |
1062 | return result; | |
1063 | } | |
1064 | ||
a68a9b73 AW |
1065 | /* ir_devices_lock must be held */ |
1066 | static struct IR *find_ir_device_by_minor(unsigned int minor) | |
1067 | { | |
5c07134f AW |
1068 | struct IR *ir; |
1069 | ||
1070 | if (list_empty(&ir_devices_list)) | |
a68a9b73 AW |
1071 | return NULL; |
1072 | ||
5c07134f AW |
1073 | list_for_each_entry(ir, &ir_devices_list, list) |
1074 | if (ir->l.minor == minor) | |
1075 | return ir; | |
1076 | ||
1077 | return NULL; | |
a68a9b73 AW |
1078 | } |
1079 | ||
69b1214c JW |
1080 | /* |
1081 | * Open the IR device. Get hold of our IR structure and | |
1082 | * stash it in private_data for the file | |
1083 | */ | |
1084 | static int open(struct inode *node, struct file *filep) | |
1085 | { | |
1086 | struct IR *ir; | |
a68a9b73 | 1087 | unsigned int minor = MINOR(node->i_rdev); |
69b1214c JW |
1088 | |
1089 | /* find our IR struct */ | |
a68a9b73 AW |
1090 | mutex_lock(&ir_devices_lock); |
1091 | ir = find_ir_device_by_minor(minor); | |
1092 | mutex_unlock(&ir_devices_lock); | |
1093 | ||
1094 | if (ir == NULL) | |
69b1214c | 1095 | return -ENODEV; |
69b1214c JW |
1096 | |
1097 | /* increment in use count */ | |
1098 | mutex_lock(&ir->ir_lock); | |
1099 | ++ir->open; | |
69b1214c JW |
1100 | mutex_unlock(&ir->ir_lock); |
1101 | ||
1102 | /* stash our IR struct */ | |
1103 | filep->private_data = ir; | |
1104 | ||
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | /* Close the IR device */ | |
1109 | static int close(struct inode *node, struct file *filep) | |
1110 | { | |
1111 | /* find our IR struct */ | |
e0ac7da0 | 1112 | struct IR *ir = filep->private_data; |
69b1214c JW |
1113 | if (ir == NULL) { |
1114 | zilog_error("close: no private_data attached to the file!\n"); | |
1115 | return -ENODEV; | |
1116 | } | |
1117 | ||
1118 | /* decrement in use count */ | |
1119 | mutex_lock(&ir->ir_lock); | |
1120 | --ir->open; | |
69b1214c JW |
1121 | mutex_unlock(&ir->ir_lock); |
1122 | ||
1123 | return 0; | |
1124 | } | |
1125 | ||
1126 | static struct lirc_driver lirc_template = { | |
1127 | .name = "lirc_zilog", | |
1128 | .set_use_inc = set_use_inc, | |
1129 | .set_use_dec = set_use_dec, | |
1130 | .owner = THIS_MODULE | |
1131 | }; | |
1132 | ||
1133 | static int ir_remove(struct i2c_client *client); | |
1134 | static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id); | |
69b1214c | 1135 | |
d7c72356 AW |
1136 | #define ID_FLAG_TX 0x01 |
1137 | #define ID_FLAG_HDPVR 0x02 | |
1138 | ||
69b1214c | 1139 | static const struct i2c_device_id ir_transceiver_id[] = { |
d7c72356 AW |
1140 | { "ir_tx_z8f0811_haup", ID_FLAG_TX }, |
1141 | { "ir_rx_z8f0811_haup", 0 }, | |
1142 | { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX }, | |
1143 | { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR }, | |
69b1214c JW |
1144 | { } |
1145 | }; | |
1146 | ||
1147 | static struct i2c_driver driver = { | |
1148 | .driver = { | |
1149 | .owner = THIS_MODULE, | |
1150 | .name = "Zilog/Hauppauge i2c IR", | |
1151 | }, | |
1152 | .probe = ir_probe, | |
1153 | .remove = ir_remove, | |
69b1214c JW |
1154 | .id_table = ir_transceiver_id, |
1155 | }; | |
1156 | ||
0f9313ad | 1157 | static const struct file_operations lirc_fops = { |
69b1214c JW |
1158 | .owner = THIS_MODULE, |
1159 | .llseek = lseek, | |
1160 | .read = read, | |
1161 | .write = write, | |
1162 | .poll = poll, | |
1163 | .unlocked_ioctl = ioctl, | |
8be292cc JW |
1164 | #ifdef CONFIG_COMPAT |
1165 | .compat_ioctl = ioctl, | |
1166 | #endif | |
69b1214c JW |
1167 | .open = open, |
1168 | .release = close | |
1169 | }; | |
1170 | ||
a68a9b73 | 1171 | static void destroy_rx_kthread(struct IR_rx *rx) |
69b1214c | 1172 | { |
a68a9b73 | 1173 | /* end up polling thread */ |
b757730b AW |
1174 | if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) { |
1175 | kthread_stop(rx->task); | |
1176 | rx->task = NULL; | |
a68a9b73 AW |
1177 | } |
1178 | } | |
69b1214c | 1179 | |
a68a9b73 AW |
1180 | /* ir_devices_lock must be held */ |
1181 | static int add_ir_device(struct IR *ir) | |
1182 | { | |
5c07134f AW |
1183 | list_add_tail(&ir->list, &ir_devices_list); |
1184 | return 0; | |
a68a9b73 AW |
1185 | } |
1186 | ||
1187 | /* ir_devices_lock must be held */ | |
1188 | static void del_ir_device(struct IR *ir) | |
1189 | { | |
5c07134f AW |
1190 | struct IR *p; |
1191 | ||
1192 | if (list_empty(&ir_devices_list)) | |
1193 | return; | |
a68a9b73 | 1194 | |
5c07134f AW |
1195 | list_for_each_entry(p, &ir_devices_list, list) |
1196 | if (p == ir) { | |
1197 | list_del(&p->list); | |
a68a9b73 AW |
1198 | break; |
1199 | } | |
1200 | } | |
1201 | ||
1202 | static int ir_remove(struct i2c_client *client) | |
1203 | { | |
1204 | struct IR *ir = i2c_get_clientdata(client); | |
1205 | ||
1206 | mutex_lock(&ir_devices_lock); | |
1207 | ||
1208 | if (ir == NULL) { | |
1209 | /* We destroyed everything when the first client came through */ | |
1210 | mutex_unlock(&ir_devices_lock); | |
1211 | return 0; | |
69b1214c JW |
1212 | } |
1213 | ||
a68a9b73 AW |
1214 | /* Good-bye LIRC */ |
1215 | lirc_unregister_driver(ir->l.minor); | |
1216 | ||
1217 | /* Good-bye Rx */ | |
1218 | destroy_rx_kthread(ir->rx); | |
1219 | if (ir->rx != NULL) { | |
1220 | if (ir->rx->buf.fifo_initialized) | |
1221 | lirc_buffer_free(&ir->rx->buf); | |
1222 | i2c_set_clientdata(ir->rx->c, NULL); | |
1223 | kfree(ir->rx); | |
69b1214c JW |
1224 | } |
1225 | ||
a68a9b73 | 1226 | /* Good-bye Tx */ |
12d896e1 AW |
1227 | if (ir->tx != NULL) { |
1228 | i2c_set_clientdata(ir->tx->c, NULL); | |
1229 | kfree(ir->tx); | |
1230 | } | |
a68a9b73 AW |
1231 | |
1232 | /* Good-bye IR */ | |
1233 | del_ir_device(ir); | |
69b1214c JW |
1234 | kfree(ir); |
1235 | ||
a68a9b73 | 1236 | mutex_unlock(&ir_devices_lock); |
69b1214c JW |
1237 | return 0; |
1238 | } | |
1239 | ||
a68a9b73 AW |
1240 | |
1241 | /* ir_devices_lock must be held */ | |
1242 | static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter) | |
69b1214c | 1243 | { |
5c07134f | 1244 | struct IR *ir; |
a68a9b73 | 1245 | |
5c07134f AW |
1246 | if (list_empty(&ir_devices_list)) |
1247 | return NULL; | |
1248 | ||
1249 | list_for_each_entry(ir, &ir_devices_list, list) | |
1250 | if (ir->adapter == adapter) | |
1251 | return ir; | |
a68a9b73 | 1252 | |
5c07134f | 1253 | return NULL; |
a68a9b73 AW |
1254 | } |
1255 | ||
1256 | static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id) | |
1257 | { | |
1258 | struct IR *ir; | |
69b1214c | 1259 | struct i2c_adapter *adap = client->adapter; |
69b1214c | 1260 | int ret; |
a68a9b73 | 1261 | bool tx_probe = false; |
69b1214c | 1262 | |
86e52428 AW |
1263 | dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n", |
1264 | __func__, id->name, adap->nr, adap->name, client->addr); | |
69b1214c | 1265 | |
d7c72356 | 1266 | /* |
a68a9b73 AW |
1267 | * The IR receiver is at i2c address 0x71. |
1268 | * The IR transmitter is at i2c address 0x70. | |
69b1214c | 1269 | */ |
69b1214c | 1270 | |
a68a9b73 AW |
1271 | if (id->driver_data & ID_FLAG_TX) |
1272 | tx_probe = true; | |
1273 | else if (tx_only) /* module option */ | |
e9b351f6 | 1274 | return -ENXIO; |
69b1214c | 1275 | |
b757730b AW |
1276 | zilog_info("probing IR %s on %s (i2c-%d)\n", |
1277 | tx_probe ? "Tx" : "Rx", adap->name, adap->nr); | |
69b1214c | 1278 | |
a68a9b73 | 1279 | mutex_lock(&ir_devices_lock); |
69b1214c | 1280 | |
a68a9b73 AW |
1281 | /* Use a single struct IR instance for both the Rx and Tx functions */ |
1282 | ir = find_ir_device_by_adapter(adap); | |
1283 | if (ir == NULL) { | |
1284 | ir = kzalloc(sizeof(struct IR), GFP_KERNEL); | |
1285 | if (ir == NULL) { | |
1286 | ret = -ENOMEM; | |
1287 | goto out_no_ir; | |
06da95a3 | 1288 | } |
a68a9b73 | 1289 | /* store for use in ir_probe() again, and open() later on */ |
5c07134f | 1290 | INIT_LIST_HEAD(&ir->list); |
a68a9b73 AW |
1291 | ret = add_ir_device(ir); |
1292 | if (ret) | |
1293 | goto out_free_ir; | |
1294 | ||
1295 | ir->adapter = adap; | |
1296 | mutex_init(&ir->ir_lock); | |
1297 | ||
1298 | /* set lirc_dev stuff */ | |
1299 | memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver)); | |
1300 | ir->l.minor = minor; /* module option */ | |
1301 | ir->l.code_length = 13; | |
1302 | ir->l.rbuf = NULL; | |
1303 | ir->l.fops = &lirc_fops; | |
1304 | ir->l.data = ir; | |
1305 | ir->l.dev = &adap->dev; | |
1306 | ir->l.sample_rate = 0; | |
06da95a3 AW |
1307 | } |
1308 | ||
a68a9b73 AW |
1309 | if (tx_probe) { |
1310 | /* Set up a struct IR_tx instance */ | |
1311 | ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL); | |
1312 | if (ir->tx == NULL) { | |
1313 | ret = -ENOMEM; | |
1314 | goto out_free_xx; | |
1315 | } | |
06da95a3 | 1316 | |
a68a9b73 AW |
1317 | ir->tx->c = client; |
1318 | ir->tx->need_boot = 1; | |
1319 | ir->tx->post_tx_ready_poll = | |
1320 | (id->driver_data & ID_FLAG_HDPVR) ? false : true; | |
1321 | } else { | |
1322 | /* Set up a struct IR_rx instance */ | |
1323 | ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL); | |
06da95a3 AW |
1324 | if (ir->rx == NULL) { |
1325 | ret = -ENOMEM; | |
a68a9b73 | 1326 | goto out_free_xx; |
06da95a3 AW |
1327 | } |
1328 | ||
a68a9b73 AW |
1329 | ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2); |
1330 | if (ret) | |
1331 | goto out_free_xx; | |
69b1214c | 1332 | |
a68a9b73 AW |
1333 | mutex_init(&ir->rx->buf_lock); |
1334 | ir->rx->c = client; | |
1335 | ir->rx->hdpvr_data_fmt = | |
1336 | (id->driver_data & ID_FLAG_HDPVR) ? true : false; | |
69b1214c | 1337 | |
a68a9b73 AW |
1338 | /* set lirc_dev stuff */ |
1339 | ir->l.rbuf = &ir->rx->buf; | |
1340 | } | |
69b1214c | 1341 | |
69b1214c JW |
1342 | i2c_set_clientdata(client, ir); |
1343 | ||
a68a9b73 AW |
1344 | /* Proceed only if we have the required Tx and Rx clients ready to go */ |
1345 | if (ir->tx == NULL || | |
1346 | (ir->rx == NULL && !tx_only)) { | |
b757730b AW |
1347 | zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on " |
1348 | "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name, | |
1349 | adap->nr, tx_probe ? "Rx" : "Tx"); | |
a68a9b73 AW |
1350 | goto out_ok; |
1351 | } | |
1352 | ||
69b1214c | 1353 | /* initialise RX device */ |
06da95a3 | 1354 | if (ir->rx != NULL) { |
69b1214c | 1355 | /* try to fire up polling thread */ |
b757730b AW |
1356 | ir->rx->task = kthread_run(lirc_thread, ir, |
1357 | "zilog-rx-i2c-%d", adap->nr); | |
06da95a3 AW |
1358 | if (IS_ERR(ir->rx->task)) { |
1359 | ret = PTR_ERR(ir->rx->task); | |
a68a9b73 AW |
1360 | zilog_error("%s: could not start IR Rx polling thread" |
1361 | "\n", __func__); | |
1362 | goto out_free_xx; | |
69b1214c | 1363 | } |
69b1214c JW |
1364 | } |
1365 | ||
69b1214c JW |
1366 | /* register with lirc */ |
1367 | ir->l.minor = lirc_register_driver(&ir->l); | |
1368 | if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) { | |
a68a9b73 AW |
1369 | zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n", |
1370 | __func__, MAX_IRCTL_DEVICES-1, ir->l.minor); | |
69b1214c | 1371 | ret = -EBADRQC; |
a68a9b73 | 1372 | goto out_free_thread; |
69b1214c JW |
1373 | } |
1374 | ||
69b1214c JW |
1375 | /* |
1376 | * if we have the tx device, load the 'firmware'. We do this | |
1377 | * after registering with lirc as otherwise hotplug seems to take | |
1378 | * 10s to create the lirc device. | |
1379 | */ | |
12d896e1 AW |
1380 | if (ir->tx != NULL) { |
1381 | /* Special TX init */ | |
1382 | ret = tx_init(ir->tx); | |
1383 | if (ret != 0) | |
1384 | goto out_unregister; | |
1385 | } | |
69b1214c | 1386 | |
b757730b AW |
1387 | zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n", |
1388 | tx_probe ? "Tx" : "Rx", adap->name, adap->nr); | |
a68a9b73 AW |
1389 | out_ok: |
1390 | mutex_unlock(&ir_devices_lock); | |
69b1214c JW |
1391 | return 0; |
1392 | ||
a68a9b73 AW |
1393 | out_unregister: |
1394 | lirc_unregister_driver(ir->l.minor); | |
1395 | out_free_thread: | |
1396 | destroy_rx_kthread(ir->rx); | |
1397 | out_free_xx: | |
1398 | if (ir->rx != NULL) { | |
1399 | if (ir->rx->buf.fifo_initialized) | |
1400 | lirc_buffer_free(&ir->rx->buf); | |
1401 | if (ir->rx->c != NULL) | |
1402 | i2c_set_clientdata(ir->rx->c, NULL); | |
1403 | kfree(ir->rx); | |
1404 | } | |
1405 | if (ir->tx != NULL) { | |
1406 | if (ir->tx->c != NULL) | |
1407 | i2c_set_clientdata(ir->tx->c, NULL); | |
1408 | kfree(ir->tx); | |
1409 | } | |
1410 | out_free_ir: | |
1411 | del_ir_device(ir); | |
69b1214c | 1412 | kfree(ir); |
a68a9b73 AW |
1413 | out_no_ir: |
1414 | zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n", | |
1415 | __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr, | |
1416 | ret); | |
1417 | mutex_unlock(&ir_devices_lock); | |
1418 | return ret; | |
69b1214c JW |
1419 | } |
1420 | ||
69b1214c JW |
1421 | static int __init zilog_init(void) |
1422 | { | |
1423 | int ret; | |
1424 | ||
1425 | zilog_notify("Zilog/Hauppauge IR driver initializing\n"); | |
1426 | ||
1427 | mutex_init(&tx_data_lock); | |
1428 | ||
1429 | request_module("firmware_class"); | |
1430 | ||
1431 | ret = i2c_add_driver(&driver); | |
1432 | if (ret) | |
1433 | zilog_error("initialization failed\n"); | |
1434 | else | |
1435 | zilog_notify("initialization complete\n"); | |
1436 | ||
1437 | return ret; | |
1438 | } | |
1439 | ||
1440 | static void __exit zilog_exit(void) | |
1441 | { | |
1442 | i2c_del_driver(&driver); | |
1443 | /* if loaded */ | |
1444 | fw_unload(); | |
1445 | zilog_notify("Zilog/Hauppauge IR driver unloaded\n"); | |
1446 | } | |
1447 | ||
1448 | module_init(zilog_init); | |
1449 | module_exit(zilog_exit); | |
1450 | ||
1451 | MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)"); | |
1452 | MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, " | |
c2790c71 AW |
1453 | "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, " |
1454 | "Andy Walls"); | |
69b1214c JW |
1455 | MODULE_LICENSE("GPL"); |
1456 | /* for compat with old name, which isn't all that accurate anymore */ | |
1457 | MODULE_ALIAS("lirc_pvr150"); | |
1458 | ||
1459 | module_param(minor, int, 0444); | |
1460 | MODULE_PARM_DESC(minor, "Preferred minor device number"); | |
1461 | ||
1462 | module_param(debug, bool, 0644); | |
1463 | MODULE_PARM_DESC(debug, "Enable debugging messages"); | |
1464 | ||
a68a9b73 AW |
1465 | module_param(tx_only, bool, 0644); |
1466 | MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function"); |