42 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
47 #include <linux/poll.h>
48 #include <linux/string.h>
52 #include <linux/errno.h>
53 #include <linux/slab.h>
54 #include <linux/i2c.h>
126 #define TX_BLOCK_SIZE 99
150 static struct mutex tx_data_lock;
152 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
154 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
155 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
160 static int minor = -1;
162 #define dprintk(fmt, args...) \
165 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
171 static struct IR *get_ir_device(
struct IR *
ir,
bool ir_devices_lock_held)
173 if (ir_devices_lock_held) {
183 static void release_ir_device(
struct kref *
ref)
199 if (ir->
rbuf.fifo_initialized)
200 lirc_buffer_free(&ir->
rbuf);
205 static int put_ir_device(
struct IR *ir,
bool ir_devices_lock_held)
209 if (ir_devices_lock_held)
210 return kref_put(&ir->
ref, release_ir_device);
213 released = kref_put(&ir->
ref, release_ir_device);
220 static struct IR_rx *get_ir_rx(
struct IR *ir)
232 static void destroy_rx_kthread(
struct IR_rx *rx,
bool ir_devices_lock_held)
235 if (!IS_ERR_OR_NULL(rx->
task)) {
239 put_ir_device(rx->
ir, ir_devices_lock_held);
243 static void release_ir_rx(
struct kref *ref)
246 struct IR *ir = rx->
ir;
261 static int put_ir_rx(
struct IR_rx *rx,
bool ir_devices_lock_held)
264 struct IR *ir = rx->
ir;
267 released = kref_put(&rx->
ref, release_ir_rx);
271 destroy_rx_kthread(rx, ir_devices_lock_held);
278 put_ir_device(ir, ir_devices_lock_held);
283 static struct IR_tx *get_ir_tx(
struct IR *ir)
295 static void release_ir_tx(
struct kref *ref)
298 struct IR *ir = tx->
ir;
306 static int put_ir_tx(
struct IR_tx *tx,
bool ir_devices_lock_held)
309 struct IR *ir = tx->
ir;
312 released = kref_put(&tx->
ref, release_ir_tx);
316 put_ir_device(ir, ir_devices_lock_held);
320 static int add_to_buf(
struct IR *ir)
323 unsigned char codes[2];
324 unsigned char keybuf[6];
328 unsigned char sendbuf[1] = { 0 };
333 if (lirc_buffer_full(rbuf)) {
346 put_ir_rx(rx,
false);
380 zilog_error(
"i2c_master_send failed with %d\n", ret);
384 "after 3 resets, giving up\n");
389 zilog_error(
"polling the IR receiver chip failed, "
415 if (ret !=
sizeof(keybuf)) {
417 "keeping last read buffer\n", ret);
419 rx->
b[0] = keybuf[3];
420 rx->
b[1] = keybuf[4];
421 rx->
b[2] = keybuf[5];
422 dprintk(
"key (0x%02x/0x%02x)\n", rx->
b[0], rx->
b[1]);
427 if (got_data && (keybuf[0] == 0x80)) {
430 }
else if (got_data && (keybuf[0] == 0x00)) {
434 }
else if ((rx->
b[0] & 0x80) == 0) {
440 code = (((
__u16)rx->
b[0] & 0x7f) << 6) | (rx->
b[1] >> 2);
442 codes[0] = (code >> 8) & 0xff;
443 codes[1] = code & 0xff;
446 lirc_buffer_write(rbuf, codes);
449 }
while (!lirc_buffer_full(rbuf));
453 put_ir_tx(tx,
false);
454 put_ir_rx(rx,
false);
468 static int lirc_thread(
void *
arg)
473 dprintk(
"poll thread started\n");
501 dprintk(
"poll thread ended\n");
505 static int set_use_inc(
void *
data)
510 static void set_use_dec(
void *
data)
516 static int read_uint32(
unsigned char **
data,
517 unsigned char *endp,
unsigned int *
val)
519 if (*data + 4 > endp)
521 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
522 ((*data)[2] << 8) | (*data)[3];
528 static int read_uint8(
unsigned char **data,
529 unsigned char *endp,
unsigned char *val)
531 if (*data + 1 > endp)
538 static int skip(
unsigned char **data,
539 unsigned char *endp,
unsigned int distance)
541 if (*data + distance > endp)
548 static int get_key_data(
unsigned char *
buf,
549 unsigned int codeset,
unsigned int key)
551 unsigned char *
data, *endp, *diffs, *key_block;
552 unsigned char keys, ndiffs,
id;
556 for (base = 0, lim = tx_data->
num_code_sets; lim; lim >>= 1) {
557 pos = base + (lim >> 1);
560 if (!read_uint32(&data, tx_data->
endp, &i))
565 else if (codeset > i) {
579 if (!read_uint8(&data, endp, &keys) ||
580 !read_uint8(&data, endp, &ndiffs) ||
586 if (!
skip(&data, endp, ndiffs))
590 if (!read_uint8(&data, endp, &
id))
595 if (tx_data->
fixed[i] == -1) {
596 if (!read_uint8(&data, endp, &buf[i]))
611 if (!
skip(&data, endp, (keys - 1) * (ndiffs + 1)))
615 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
617 unsigned char *key_data;
618 pos = base + (lim >> 1);
619 key_data = key_block + (ndiffs + 1) * pos;
621 if (*key_data == key) {
626 for (i = 0; i < ndiffs; ++
i) {
628 if (!read_uint8(&key_data, endp, &val) ||
635 }
else if (key > *key_data) {
649 static int send_data_block(
struct IR_tx *tx,
unsigned char *data_block)
652 unsigned char buf[5];
655 int tosend = TX_BLOCK_SIZE -
i;
658 buf[0] = (
unsigned char)(i + 1);
659 for (j = 0; j < tosend; ++
j)
660 buf[1 + j] = data_block[i + j];
663 if (ret != tosend + 1) {
664 zilog_error(
"i2c_master_send failed with %d\n", ret);
665 return ret < 0 ? ret : -
EFAULT;
673 static int send_boot_data(
struct IR_tx *tx)
676 unsigned char buf[4];
679 ret = send_data_block(tx, tx_data->
boot_data);
688 zilog_error(
"i2c_master_send failed with %d\n", ret);
689 return ret < 0 ? ret : -
EFAULT;
697 for (i = 0; i < 10; i++) {
705 zilog_error(
"i2c_master_send failed with %d\n", ret);
706 return ret < 0 ? ret : -
EFAULT;
712 zilog_error(
"i2c_master_recv failed with %d\n", ret);
715 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
716 zilog_error(
"unexpected IR TX init response: %02x\n", buf[0]);
719 zilog_notify(
"Zilog/Hauppauge IR blaster firmware version "
720 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
726 static void fw_unload_locked(
void)
737 dprintk(
"successfully unloaded IR blaster firmware\n");
742 static void fw_unload(
void)
750 static int fw_load(
struct IR_tx *tx)
767 zilog_error(
"firmware haup-ir-blaster.bin not available "
769 ret = ret < 0 ? ret : -
EFAULT;
772 dprintk(
"firmware of size %zu loaded\n", fw_entry->
size);
775 tx_data =
vmalloc(
sizeof(*tx_data));
776 if (tx_data ==
NULL) {
798 data = tx_data->
datap;
799 if (!read_uint8(&data, tx_data->
endp, &version))
802 zilog_error(
"unsupported code set file version (%u, expected"
803 "1) -- please upgrade to a newer driver",
812 if (!
skip(&data, tx_data->
endp, TX_BLOCK_SIZE))
815 if (!read_uint32(&data, tx_data->
endp,
830 tx_data->
fixed[i] = -1;
833 if (!read_uint8(&data, tx_data->
endp, &num_global_fixed) ||
834 num_global_fixed > TX_BLOCK_SIZE)
836 for (i = 0; i < num_global_fixed; ++
i) {
838 if (!read_uint8(&data, tx_data->
endp, &pos) ||
839 !read_uint8(&data, tx_data->
endp, &val) ||
840 pos >= TX_BLOCK_SIZE)
849 unsigned char ndiffs;
855 if (!read_uint32(&data, tx_data->
endp, &
id) ||
856 !read_uint8(&data, tx_data->
endp, &keys) ||
857 !read_uint8(&data, tx_data->
endp, &ndiffs) ||
858 ndiffs > TX_BLOCK_SIZE || keys == 0)
862 if (!
skip(&data, tx_data->
endp, ndiffs))
870 1 + TX_BLOCK_SIZE - num_global_fixed))
875 (ndiffs + 1) * (keys - 1)))
892 static ssize_t read(
struct file *filep,
char *outbuf,
size_t n, loff_t *ppos)
897 int ret = 0, written = 0,
retries = 0;
903 dprintk(
"read result = -EINVAL\n");
923 while (written < n && ret == 0) {
924 if (lirc_buffer_empty(rbuf)) {
945 m = lirc_buffer_read(rbuf, buf);
961 put_ir_rx(rx,
false);
964 dprintk(
"read result = %d (%s)\n", ret, ret ?
"Error" :
"OK");
966 return ret ? ret : written;
970 static int send_code(
struct IR_tx *tx,
unsigned int code,
unsigned int key)
973 unsigned char buf[2];
977 ret = get_key_data(data_block, code, key);
980 zilog_error(
"failed to get data for code %u, key %u -- check "
981 "lircd.conf entries\n", code, key);
987 ret = send_data_block(tx, data_block);
996 zilog_error(
"i2c_master_send failed with %d\n", ret);
997 return ret < 0 ? ret : -
EFAULT;
1001 for (i = 0; i < 10; i++) {
1009 zilog_error(
"i2c_master_send failed with %d\n", ret);
1010 return ret < 0 ? ret : -
EFAULT;
1016 zilog_error(
"i2c_master_recv failed with %d\n", ret);
1017 return ret < 0 ? ret : -
EFAULT;
1019 if (buf[0] != 0xA0) {
1020 zilog_error(
"unexpected IR TX response #1: %02x\n",
1030 zilog_error(
"i2c_master_send failed with %d\n", ret);
1031 return ret < 0 ? ret : -
EFAULT;
1040 dprintk(
"sent code %u, key %u\n", code, key);
1050 for (i = 0; i < 20; ++
i) {
1056 dprintk(
"NAK expected: i2c_master_send "
1057 "failed with %d (try %d)\n", ret, i+1);
1060 zilog_error(
"IR TX chip never got ready: last i2c_master_send "
1061 "failed with %d\n", ret);
1062 return ret < 0 ? ret : -
EFAULT;
1068 zilog_error(
"i2c_master_recv failed with %d\n", ret);
1071 if (buf[0] != 0x80) {
1072 zilog_error(
"unexpected IR TX response #2: %02x\n", buf[0]);
1077 dprintk(
"sent code %u, key %u\n", code, key);
1096 if (n %
sizeof(
int))
1106 if (tx->
c ==
NULL) {
1108 put_ir_tx(tx,
false);
1116 for (i = 0; i <
n;) {
1123 put_ir_tx(tx,
false);
1134 put_ir_tx(tx,
false);
1140 ret = send_boot_data(tx);
1147 ret =
send_code(tx, (
unsigned)command >> 16,
1148 (
unsigned)command & 0xFFFF);
1152 put_ir_tx(tx,
false);
1164 "failed, trying reset\n");
1166 if (failures >= 3) {
1168 "after 3 resets, giving up\n");
1171 put_ir_tx(tx,
false);
1188 put_ir_tx(tx,
false);
1210 dprintk(
"poll result = POLLERR\n");
1218 poll_wait(filep, &rbuf->
wait_poll, wait);
1223 dprintk(
"poll result = %s\n", ret ?
"POLLIN|POLLRDNORM" :
"none");
1227 static long ioctl(
struct file *filep,
unsigned int cmd,
unsigned long arg)
1233 features = ir->
l.features;
1237 result =
put_user((
unsigned long)13,
1238 (
unsigned long *)arg);
1241 result =
put_user(features, (
unsigned long *) arg);
1248 (features&LIRC_CAN_REC_MASK),
1249 (
unsigned long *)arg);
1252 if (!(features&LIRC_CAN_REC_MASK))
1255 result =
get_user(mode, (
unsigned long *)arg);
1266 if (!(features&LIRC_CAN_SEND_MASK))
1269 result =
get_user(mode, (
unsigned long *) arg);
1279 static struct IR *get_ir_device_by_minor(
unsigned int minor)
1286 if (!list_empty(&ir_devices_list)) {
1288 if (ir->
l.minor == minor) {
1289 ret = get_ir_device(ir,
true);
1309 ir = get_ir_device_by_minor(minor);
1324 static int close(
struct inode *node,
struct file *filep)
1329 zilog_error(
"close: no private_data attached to the file!\n");
1335 put_ir_device(ir,
false);
1342 #define ID_FLAG_TX 0x01
1343 #define ID_FLAG_HDPVR 0x02
1347 {
"ir_rx_z8f0811_haup", 0 },
1356 .name =
"Zilog/Hauppauge i2c IR",
1359 .remove = ir_remove,
1360 .id_table = ir_transceiver_id,
1369 .unlocked_ioctl = ioctl,
1370 #ifdef CONFIG_COMPAT
1371 .compat_ioctl = ioctl,
1378 .name =
"lirc_zilog",
1381 .buffer_size =
BUFLEN / 2,
1393 struct IR_tx *tx = i2c_get_clientdata(client);
1398 put_ir_tx(tx,
false);
1400 }
else if (
strncmp(
"ir_rx_z8", client->
name, 8) == 0) {
1401 struct IR_rx *rx = i2c_get_clientdata(client);
1406 put_ir_rx(rx,
false);
1418 if (list_empty(&ir_devices_list))
1422 if (ir->adapter == adapter) {
1423 get_ir_device(ir,
true);
1437 bool tx_probe =
false;
1439 dprintk(
"%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1453 tx_probe ?
"Tx" :
"Rx", adap->
name, adap->
nr);
1458 ir = get_ir_device_by_adapter(adap);
1465 kref_init(&ir->
ref);
1468 INIT_LIST_HEAD(&ir->
list);
1487 ir->
l.rbuf = &ir->
rbuf;
1488 ir->
l.dev = &adap->
dev;
1489 ret = lirc_buffer_init(ir->
l.rbuf,
1490 ir->
l.chunk_size, ir->
l.buffer_size);
1505 kref_init(&tx->
ref);
1516 tx->
ir = get_ir_device(ir,
true);
1519 i2c_set_clientdata(client, get_ir_tx(ir));
1533 if (rx ==
NULL && !tx_only) {
1534 zilog_info(
"probe of IR Tx on %s (i2c-%d) done. Waiting"
1535 " on IR Rx.\n", adap->
name, adap->
nr);
1548 kref_init(&rx->
ref);
1558 rx->
ir = get_ir_device(ir,
true);
1561 i2c_set_clientdata(client, get_ir_rx(ir));
1570 "zilog-rx-i2c-%d", adap->
nr);
1571 if (IS_ERR(rx->
task)) {
1572 ret = PTR_ERR(rx->
task);
1573 zilog_error(
"%s: could not start IR Rx polling thread"
1576 put_ir_device(ir,
true);
1578 i2c_set_clientdata(client,
NULL);
1579 put_ir_rx(rx,
true);
1586 zilog_info(
"probe of IR Rx on %s (i2c-%d) done. Waiting"
1587 " on IR Tx.\n", adap->
name, adap->
nr);
1593 ir->
l.minor = minor;
1596 zilog_error(
"%s: \"minor\" must be between 0 and %d (%d)!\n",
1601 zilog_info(
"IR unit on %s (i2c-%d) registered as lirc%d and ready\n",
1602 adap->
name, adap->
nr, ir->
l.minor);
1606 put_ir_rx(rx,
true);
1608 put_ir_tx(tx,
true);
1609 put_ir_device(ir,
true);
1610 zilog_info(
"probe of IR %s on %s (i2c-%d) done\n",
1611 tx_probe ?
"Tx" :
"Rx", adap->
name, adap->
nr);
1617 put_ir_rx(rx,
true);
1619 put_ir_tx(tx,
true);
1621 put_ir_device(ir,
true);
1623 zilog_error(
"%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1624 __func__, tx_probe ?
"Tx" :
"Rx", adap->
name, adap->
nr,
1630 static int __init zilog_init(
void)
1634 zilog_notify(
"Zilog/Hauppauge IR driver initializing\n");
1638 request_module(
"firmware_class");
1640 ret = i2c_add_driver(&driver);
1649 static void __exit zilog_exit(
void)
1661 MODULE_AUTHOR(
"Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1662 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "