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