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