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