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