]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/lirc/lirc_zilog.c
[media] lirc_zilog: Update IR Rx polling kthread start/stop and some printks
[mv-sheeva.git] / drivers / staging / lirc / lirc_zilog.c
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_rx {
64         /* RX device */
65         struct i2c_client *c;
66
67         /* RX device buffer & lock */
68         struct lirc_buffer buf;
69         struct mutex buf_lock;
70
71         /* RX polling thread data */
72         struct task_struct *task;
73
74         /* RX read data */
75         unsigned char b[3];
76         bool hdpvr_data_fmt;
77 };
78
79 struct IR_tx {
80         /* TX device */
81         struct i2c_client *c;
82
83         /* TX additional actions needed */
84         int need_boot;
85         bool post_tx_ready_poll;
86 };
87
88 struct IR {
89         struct lirc_driver l;
90
91         struct mutex ir_lock;
92         int open;
93
94         struct i2c_adapter *adapter;
95         struct IR_rx *rx;
96         struct IR_tx *tx;
97 };
98
99 /* Minor -> data mapping */
100 static struct mutex ir_devices_lock;
101 static struct IR *ir_devices[MAX_IRCTL_DEVICES];
102
103 /* Block size for IR transmitter */
104 #define TX_BLOCK_SIZE   99
105
106 /* Hauppauge IR transmitter data */
107 struct tx_data_struct {
108         /* Boot block */
109         unsigned char *boot_data;
110
111         /* Start of binary data block */
112         unsigned char *datap;
113
114         /* End of binary data block */
115         unsigned char *endp;
116
117         /* Number of installed codesets */
118         unsigned int num_code_sets;
119
120         /* Pointers to codesets */
121         unsigned char **code_sets;
122
123         /* Global fixed data template */
124         int fixed[TX_BLOCK_SIZE];
125 };
126
127 static struct tx_data_struct *tx_data;
128 static struct mutex tx_data_lock;
129
130 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
131                                         ## args)
132 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
133 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
134
135 /* module parameters */
136 static int debug;       /* debug output */
137 static int tx_only;     /* only handle the IR Tx function */
138 static int minor = -1;  /* minor number */
139
140 #define dprintk(fmt, args...)                                           \
141         do {                                                            \
142                 if (debug)                                              \
143                         printk(KERN_DEBUG KBUILD_MODNAME ": " fmt,      \
144                                  ## args);                              \
145         } while (0)
146
147 static int add_to_buf(struct IR *ir)
148 {
149         __u16 code;
150         unsigned char codes[2];
151         unsigned char keybuf[6];
152         int got_data = 0;
153         int ret;
154         int failures = 0;
155         unsigned char sendbuf[1] = { 0 };
156         struct IR_rx *rx = ir->rx;
157
158         if (rx == NULL)
159                 return -ENXIO;
160
161         if (lirc_buffer_full(&rx->buf)) {
162                 dprintk("buffer overflow\n");
163                 return -EOVERFLOW;
164         }
165
166         /*
167          * service the device as long as it is returning
168          * data and we have space
169          */
170         do {
171                 if (kthread_should_stop())
172                         return -ENODATA;
173
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                 if (kthread_should_stop()) {
181                         mutex_unlock(&ir->ir_lock);
182                         return -ENODATA;
183                 }
184
185                 /*
186                  * Send random "poll command" (?)  Windows driver does this
187                  * and it is a good point to detect chip failure.
188                  */
189                 ret = i2c_master_send(rx->c, sendbuf, 1);
190                 if (ret != 1) {
191                         zilog_error("i2c_master_send failed with %d\n", ret);
192                         if (failures >= 3) {
193                                 mutex_unlock(&ir->ir_lock);
194                                 zilog_error("unable to read from the IR chip "
195                                             "after 3 resets, giving up\n");
196                                 return ret;
197                         }
198
199                         /* Looks like the chip crashed, reset it */
200                         zilog_error("polling the IR receiver chip failed, "
201                                     "trying reset\n");
202
203                         set_current_state(TASK_UNINTERRUPTIBLE);
204                         if (kthread_should_stop()) {
205                                 mutex_unlock(&ir->ir_lock);
206                                 return -ENODATA;
207                         }
208                         schedule_timeout((100 * HZ + 999) / 1000);
209                         if (ir->tx != NULL)
210                                 ir->tx->need_boot = 1;
211
212                         ++failures;
213                         mutex_unlock(&ir->ir_lock);
214                         continue;
215                 }
216
217                 if (kthread_should_stop()) {
218                         mutex_unlock(&ir->ir_lock);
219                         return -ENODATA;
220                 }
221                 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
222                 mutex_unlock(&ir->ir_lock);
223                 if (ret != sizeof(keybuf)) {
224                         zilog_error("i2c_master_recv failed with %d -- "
225                                     "keeping last read buffer\n", ret);
226                 } else {
227                         rx->b[0] = keybuf[3];
228                         rx->b[1] = keybuf[4];
229                         rx->b[2] = keybuf[5];
230                         dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
231                 }
232
233                 /* key pressed ? */
234                 if (rx->hdpvr_data_fmt) {
235                         if (got_data && (keybuf[0] == 0x80))
236                                 return 0;
237                         else if (got_data && (keybuf[0] == 0x00))
238                                 return -ENODATA;
239                 } else if ((rx->b[0] & 0x80) == 0)
240                         return got_data ? 0 : -ENODATA;
241
242                 /* look what we have */
243                 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
244
245                 codes[0] = (code >> 8) & 0xff;
246                 codes[1] = code & 0xff;
247
248                 /* return it */
249                 lirc_buffer_write(&rx->buf, codes);
250                 ++got_data;
251         } while (!lirc_buffer_full(&rx->buf));
252
253         return 0;
254 }
255
256 /*
257  * Main function of the polling thread -- from lirc_dev.
258  * We don't fit the LIRC model at all anymore.  This is horrible, but
259  * basically we have a single RX/TX device with a nasty failure mode
260  * that needs to be accounted for across the pair.  lirc lets us provide
261  * fops, but prevents us from using the internal polling, etc. if we do
262  * so.  Hence the replication.  Might be neater to extend the LIRC model
263  * to account for this but I'd think it's a very special case of seriously
264  * messed up hardware.
265  */
266 static int lirc_thread(void *arg)
267 {
268         struct IR *ir = arg;
269         struct IR_rx *rx = ir->rx;
270
271         dprintk("poll thread started\n");
272
273         while (!kthread_should_stop()) {
274                 set_current_state(TASK_INTERRUPTIBLE);
275
276                 /* if device not opened, we can sleep half a second */
277                 if (!ir->open) {
278                         schedule_timeout(HZ/2);
279                         continue;
280                 }
281
282                 /*
283                  * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
284                  * We use this interval as the chip resets every time you poll
285                  * it (bad!).  This is therefore just sufficient to catch all
286                  * of the button presses.  It makes the remote much more
287                  * responsive.  You can see the difference by running irw and
288                  * holding down a button.  With 100ms, the old polling
289                  * interval, you'll notice breaks in the repeat sequence
290                  * corresponding to lost keypresses.
291                  */
292                 schedule_timeout((260 * HZ) / 1000);
293                 if (kthread_should_stop())
294                         break;
295                 if (!add_to_buf(ir))
296                         wake_up_interruptible(&rx->buf.wait_poll);
297         }
298
299         dprintk("poll thread ended\n");
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          */
315         if (ir->rx != NULL)
316                 i2c_use_client(ir->rx->c);
317         if (ir->tx != NULL)
318                 i2c_use_client(ir->tx->c);
319
320         return 0;
321 }
322
323 static void set_use_dec(void *data)
324 {
325         struct IR *ir = data;
326
327         if (ir->rx)
328                 i2c_release_client(ir->rx->c);
329         if (ir->tx)
330                 i2c_release_client(ir->tx->c);
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 */
469 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
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]);
483                 ret = i2c_master_send(tx->c, buf, tosend + 1);
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 */
494 static int send_boot_data(struct IR_tx *tx)
495 {
496         int ret;
497         unsigned char buf[4];
498
499         /* send the boot block */
500         ret = send_data_block(tx, tx_data->boot_data);
501         if (ret != 0)
502                 return ret;
503
504         /* kick it off? */
505         buf[0] = 0x00;
506         buf[1] = 0x20;
507         ret = i2c_master_send(tx->c, buf, 2);
508         if (ret != 2) {
509                 zilog_error("i2c_master_send failed with %d\n", ret);
510                 return ret < 0 ? ret : -EFAULT;
511         }
512         ret = i2c_master_send(tx->c, buf, 1);
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) */
519         ret = i2c_master_recv(tx->c, buf, 4);
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 */
559 static int fw_load(struct IR_tx *tx)
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 */
574         ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
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 */
701 static int tx_init(struct IR_tx *tx)
702 {
703         int ret;
704
705         /* Load 'firmware' */
706         ret = fw_load(tx);
707         if (ret != 0)
708                 return ret;
709
710         /* Send boot block */
711         ret = send_boot_data(tx);
712         if (ret != 0)
713                 return ret;
714         tx->need_boot = 0;
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 {
729         struct IR *ir = filep->private_data;
730         struct IR_rx *rx = ir->rx;
731         int ret = 0, written = 0;
732         DECLARE_WAITQUEUE(wait, current);
733
734         dprintk("read called\n");
735         if (rx == NULL)
736                 return -ENODEV;
737
738         if (mutex_lock_interruptible(&rx->buf_lock))
739                 return -ERESTARTSYS;
740
741         if (n % rx->buf.chunk_size) {
742                 dprintk("read result = -EINVAL\n");
743                 mutex_unlock(&rx->buf_lock);
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          */
752         add_wait_queue(&rx->buf.wait_poll, &wait);
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) {
760                 if (lirc_buffer_empty(&rx->buf)) {
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 {
780                         unsigned char buf[rx->buf.chunk_size];
781                         lirc_buffer_read(&rx->buf, buf);
782                         ret = copy_to_user((void *)outbuf+written, buf,
783                                            rx->buf.chunk_size);
784                         written += rx->buf.chunk_size;
785                 }
786         }
787
788         remove_wait_queue(&rx->buf.wait_poll, &wait);
789         set_current_state(TASK_RUNNING);
790         mutex_unlock(&rx->buf_lock);
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 */
799 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
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 */
816         ret = send_data_block(tx, data_block);
817         if (ret != 0)
818                 return ret;
819
820         /* Send data block length? */
821         buf[0] = 0x00;
822         buf[1] = 0x40;
823         ret = i2c_master_send(tx->c, buf, 2);
824         if (ret != 2) {
825                 zilog_error("i2c_master_send failed with %d\n", ret);
826                 return ret < 0 ? ret : -EFAULT;
827         }
828         ret = i2c_master_send(tx->c, buf, 1);
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? */
835         ret = i2c_master_recv(tx->c, buf, 1);
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;
849         ret = i2c_master_send(tx->c, buf, 2);
850         if (ret != 2) {
851                 zilog_error("i2c_master_send failed with %d\n", ret);
852                 return ret < 0 ? ret : -EFAULT;
853         }
854
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          */
860         if (!tx->post_tx_ready_poll) {
861                 dprintk("sent code %u, key %u\n", code, key);
862                 return 0;
863         }
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);
874                 ret = i2c_master_send(tx->c, buf, 1);
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 */
887         i = i2c_master_recv(tx->c, buf, 1);
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
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 {
911         struct IR *ir = filep->private_data;
912         struct IR_tx *tx = ir->tx;
913         size_t i;
914         int failures = 0;
915
916         if (tx == NULL)
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 */
937                 if (tx->need_boot == 1) {
938                         ret = send_boot_data(tx);
939                         if (ret == 0)
940                                 tx->need_boot = 0;
941                 }
942
943                 /* Send the code */
944                 if (ret == 0) {
945                         ret = send_code(tx, (unsigned)command >> 16,
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);
970                         tx->need_boot = 1;
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 {
986         struct IR *ir = filep->private_data;
987         struct IR_rx *rx = ir->rx;
988         unsigned int ret;
989
990         dprintk("poll called\n");
991         if (rx == NULL)
992                 return -ENODEV;
993
994         mutex_lock(&rx->buf_lock);
995
996         poll_wait(filep, &rx->buf.wait_poll, wait);
997
998         dprintk("poll result = %s\n",
999                 lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
1000
1001         ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
1002
1003         mutex_unlock(&rx->buf_lock);
1004         return ret;
1005 }
1006
1007 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1008 {
1009         struct IR *ir = filep->private_data;
1010         int result;
1011         unsigned long mode, features = 0;
1012
1013         if (ir->rx != NULL)
1014                 features |= LIRC_CAN_REC_LIRCCODE;
1015         if (ir->tx != NULL)
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
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
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;
1079         unsigned int minor = MINOR(node->i_rdev);
1080
1081         /* find our IR struct */
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)
1087                 return -ENODEV;
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 */
1110         struct IR *ir = filep->private_data;
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
1136 #define ID_FLAG_TX      0x01
1137 #define ID_FLAG_HDPVR   0x02
1138
1139 static const struct i2c_device_id ir_transceiver_id[] = {
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              },
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
1158 static const struct file_operations lirc_fops = {
1159         .owner          = THIS_MODULE,
1160         .llseek         = lseek,
1161         .read           = read,
1162         .write          = write,
1163         .poll           = poll,
1164         .unlocked_ioctl = ioctl,
1165 #ifdef CONFIG_COMPAT
1166         .compat_ioctl   = ioctl,
1167 #endif
1168         .open           = open,
1169         .release        = close
1170 };
1171
1172 static void destroy_rx_kthread(struct IR_rx *rx)
1173 {
1174         /* end up polling thread */
1175         if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1176                 kthread_stop(rx->task);
1177                 rx->task = NULL;
1178         }
1179 }
1180
1181 /* ir_devices_lock must be held */
1182 static int add_ir_device(struct IR *ir)
1183 {
1184         int i;
1185
1186         for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1187                 if (ir_devices[i] == NULL) {
1188                         ir_devices[i] = ir;
1189                         break;
1190                 }
1191
1192         return i == MAX_IRCTL_DEVICES ? -ENOMEM : i;
1193 }
1194
1195 /* ir_devices_lock must be held */
1196 static void del_ir_device(struct IR *ir)
1197 {
1198         int i;
1199
1200         for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1201                 if (ir_devices[i] == ir) {
1202                         ir_devices[i] = NULL;
1203                         break;
1204                 }
1205 }
1206
1207 static int ir_remove(struct i2c_client *client)
1208 {
1209         struct IR *ir = i2c_get_clientdata(client);
1210
1211         mutex_lock(&ir_devices_lock);
1212
1213         if (ir == NULL) {
1214                 /* We destroyed everything when the first client came through */
1215                 mutex_unlock(&ir_devices_lock);
1216                 return 0;
1217         }
1218
1219         /* Good-bye LIRC */
1220         lirc_unregister_driver(ir->l.minor);
1221
1222         /* Good-bye Rx */
1223         destroy_rx_kthread(ir->rx);
1224         if (ir->rx != NULL) {
1225                 if (ir->rx->buf.fifo_initialized)
1226                         lirc_buffer_free(&ir->rx->buf);
1227                 i2c_set_clientdata(ir->rx->c, NULL);
1228                 kfree(ir->rx);
1229         }
1230
1231         /* Good-bye Tx */
1232         i2c_set_clientdata(ir->tx->c, NULL);
1233         kfree(ir->tx);
1234
1235         /* Good-bye IR */
1236         del_ir_device(ir);
1237         kfree(ir);
1238
1239         mutex_unlock(&ir_devices_lock);
1240         return 0;
1241 }
1242
1243
1244 /* ir_devices_lock must be held */
1245 static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
1246 {
1247         int i;
1248         struct IR *ir = NULL;
1249
1250         for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1251                 if (ir_devices[i] != NULL &&
1252                     ir_devices[i]->adapter == adapter) {
1253                         ir = ir_devices[i];
1254                         break;
1255                 }
1256
1257         return ir;
1258 }
1259
1260 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1261 {
1262         struct IR *ir;
1263         struct i2c_adapter *adap = client->adapter;
1264         int ret;
1265         bool tx_probe = false;
1266
1267         dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1268                 __func__, id->name, adap->nr, adap->name, client->addr);
1269
1270         /*
1271          * The IR receiver    is at i2c address 0x71.
1272          * The IR transmitter is at i2c address 0x70.
1273          */
1274
1275         if (id->driver_data & ID_FLAG_TX)
1276                 tx_probe = true;
1277         else if (tx_only) /* module option */
1278                 return -ENXIO;
1279
1280         zilog_info("probing IR %s on %s (i2c-%d)\n",
1281                    tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1282
1283         mutex_lock(&ir_devices_lock);
1284
1285         /* Use a single struct IR instance for both the Rx and Tx functions */
1286         ir = find_ir_device_by_adapter(adap);
1287         if (ir == NULL) {
1288                 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1289                 if (ir == NULL) {
1290                         ret = -ENOMEM;
1291                         goto out_no_ir;
1292                 }
1293                 /* store for use in ir_probe() again, and open() later on */
1294                 ret = add_ir_device(ir);
1295                 if (ret)
1296                         goto out_free_ir;
1297
1298                 ir->adapter = adap;
1299                 mutex_init(&ir->ir_lock);
1300
1301                 /* set lirc_dev stuff */
1302                 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1303                 ir->l.minor       = minor; /* module option */
1304                 ir->l.code_length = 13;
1305                 ir->l.rbuf        = NULL;
1306                 ir->l.fops        = &lirc_fops;
1307                 ir->l.data        = ir;
1308                 ir->l.dev         = &adap->dev;
1309                 ir->l.sample_rate = 0;
1310         }
1311
1312         if (tx_probe) {
1313                 /* Set up a struct IR_tx instance */
1314                 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1315                 if (ir->tx == NULL) {
1316                         ret = -ENOMEM;
1317                         goto out_free_xx;
1318                 }
1319
1320                 ir->tx->c = client;
1321                 ir->tx->need_boot = 1;
1322                 ir->tx->post_tx_ready_poll =
1323                                (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1324         } else {
1325                 /* Set up a struct IR_rx instance */
1326                 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1327                 if (ir->rx == NULL) {
1328                         ret = -ENOMEM;
1329                         goto out_free_xx;
1330                 }
1331
1332                 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1333                 if (ret)
1334                         goto out_free_xx;
1335
1336                 mutex_init(&ir->rx->buf_lock);
1337                 ir->rx->c = client;
1338                 ir->rx->hdpvr_data_fmt =
1339                                (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1340
1341                 /* set lirc_dev stuff */
1342                 ir->l.rbuf = &ir->rx->buf;
1343         }
1344
1345         i2c_set_clientdata(client, ir);
1346
1347         /* Proceed only if we have the required Tx and Rx clients ready to go */
1348         if (ir->tx == NULL ||
1349             (ir->rx == NULL && !tx_only)) {
1350                 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1351                            "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
1352                            adap->nr, tx_probe ? "Rx" : "Tx");
1353                 goto out_ok;
1354         }
1355
1356         /* initialise RX device */
1357         if (ir->rx != NULL) {
1358                 /* try to fire up polling thread */
1359                 ir->rx->task = kthread_run(lirc_thread, ir,
1360                                            "zilog-rx-i2c-%d", adap->nr);
1361                 if (IS_ERR(ir->rx->task)) {
1362                         ret = PTR_ERR(ir->rx->task);
1363                         zilog_error("%s: could not start IR Rx polling thread"
1364                                     "\n", __func__);
1365                         goto out_free_xx;
1366                 }
1367         }
1368
1369         /* register with lirc */
1370         ir->l.minor = lirc_register_driver(&ir->l);
1371         if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1372                 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1373                             __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1374                 ret = -EBADRQC;
1375                 goto out_free_thread;
1376         }
1377
1378         /*
1379          * if we have the tx device, load the 'firmware'.  We do this
1380          * after registering with lirc as otherwise hotplug seems to take
1381          * 10s to create the lirc device.
1382          */
1383         if (ir->tx != NULL) {
1384                 /* Special TX init */
1385                 ret = tx_init(ir->tx);
1386                 if (ret != 0)
1387                         goto out_unregister;
1388         }
1389
1390         zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1391                    tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1392 out_ok:
1393         mutex_unlock(&ir_devices_lock);
1394         return 0;
1395
1396 out_unregister:
1397         lirc_unregister_driver(ir->l.minor);
1398 out_free_thread:
1399         destroy_rx_kthread(ir->rx);
1400 out_free_xx:
1401         if (ir->rx != NULL) {
1402                 if (ir->rx->buf.fifo_initialized)
1403                         lirc_buffer_free(&ir->rx->buf);
1404                 if (ir->rx->c != NULL)
1405                         i2c_set_clientdata(ir->rx->c, NULL);
1406                 kfree(ir->rx);
1407         }
1408         if (ir->tx != NULL) {
1409                 if (ir->tx->c != NULL)
1410                         i2c_set_clientdata(ir->tx->c, NULL);
1411                 kfree(ir->tx);
1412         }
1413 out_free_ir:
1414         del_ir_device(ir);
1415         kfree(ir);
1416 out_no_ir:
1417         zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1418                     __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1419                    ret);
1420         mutex_unlock(&ir_devices_lock);
1421         return ret;
1422 }
1423
1424 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg)
1425 {
1426         /* nothing */
1427         return 0;
1428 }
1429
1430 static int __init zilog_init(void)
1431 {
1432         int ret;
1433
1434         zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1435
1436         mutex_init(&tx_data_lock);
1437         mutex_init(&ir_devices_lock);
1438
1439         request_module("firmware_class");
1440
1441         ret = i2c_add_driver(&driver);
1442         if (ret)
1443                 zilog_error("initialization failed\n");
1444         else
1445                 zilog_notify("initialization complete\n");
1446
1447         return ret;
1448 }
1449
1450 static void __exit zilog_exit(void)
1451 {
1452         i2c_del_driver(&driver);
1453         /* if loaded */
1454         fw_unload();
1455         zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1456 }
1457
1458 module_init(zilog_init);
1459 module_exit(zilog_exit);
1460
1461 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1462 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1463               "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver");
1464 MODULE_LICENSE("GPL");
1465 /* for compat with old name, which isn't all that accurate anymore */
1466 MODULE_ALIAS("lirc_pvr150");
1467
1468 module_param(minor, int, 0444);
1469 MODULE_PARM_DESC(minor, "Preferred minor device number");
1470
1471 module_param(debug, bool, 0644);
1472 MODULE_PARM_DESC(debug, "Enable debugging messages");
1473
1474 module_param(tx_only, bool, 0644);
1475 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");