Ejemplo n.º 1
0
static int m920x_rc_core_query(struct dvb_usb_device *d)
{
	int ret = 0;
	u8 *rc_state;
	int state;

	rc_state = kmalloc(2, GFP_KERNEL);
	if (!rc_state)
		return -ENOMEM;

	if ((ret = m920x_read(d->udev, M9206_CORE, 0x0, M9206_RC_STATE, &rc_state[0], 1)) != 0)
		goto out;

	if ((ret = m920x_read(d->udev, M9206_CORE, 0x0, M9206_RC_KEY, &rc_state[1], 1)) != 0)
		goto out;

	deb("state=0x%02x keycode=0x%02x\n", rc_state[0], rc_state[1]);

	m920x_parse_rc_state(d, rc_state[0], &state);

	if (state == REMOTE_NO_KEY_PRESSED)
		rc_keyup(d->rc_dev);
	else if (state == REMOTE_KEY_REPEAT)
		rc_repeat(d->rc_dev);
	else
		rc_keydown(d->rc_dev, RC_PROTO_UNKNOWN, rc_state[1], 0);

out:
	kfree(rc_state);
	return ret;
}
static ssize_t user_rc_input_write(struct file *file, const char __user *buffer,
						size_t count, loff_t *ppos)
{
	int ret;
	struct user_rc_input_dev *input_dev = file->private_data;
	__u8 *buf;

	buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
	if (!buf) {
		dev_err(input_dev->dev,
			"kmalloc failed...Insufficient memory\n");
		ret = -ENOMEM;
		goto out;
	}

	if (copy_from_user(buf, buffer, count)) {
		dev_err(input_dev->dev, "Copy from user failed\n");
		ret = -EFAULT;
		goto out_free;
	}

	switch (buf[0])	{
	case USER_CONTROL_PRESSED:
		dev_dbg(input_dev->dev, "user controlled"
					" pressed 0x%x\n", buf[1]);
		rc_keydown(input_dev->rcdev, buf[1], 0);
		break;
	case USER_CONTROL_REPEATED:
		dev_dbg(input_dev->dev, "user controlled"
					" repeated 0x%x\n", buf[1]);
		rc_repeat(input_dev->rcdev);
		break;
	case USER_CONTROL_RELEASED:
		dev_dbg(input_dev->dev, "user controlled"
					" released 0x%x\n", buf[1]);
		rc_keyup(input_dev->rcdev);
		break;
	}

out_free:
	kfree(buf);
out:
	return ret;
}
/**
 * ir_sanyo_decode() - Decode one SANYO pulse or space
 * @dev:	the struct rc_dev descriptor of the device
 * @duration:	the struct ir_raw_event descriptor of the pulse/space
 *
 * This function returns -EINVAL if the pulse violates the state machine
 */
static int ir_sanyo_decode(struct rc_dev *dev, struct ir_raw_event ev)
{
    struct sanyo_dec *data = &dev->raw->sanyo;
    u32 scancode;
    u8 address, command, not_command;

    if (!(dev->raw->enabled_protocols & RC_BIT_SANYO))
        return 0;

    if (!is_timing_event(ev)) {
        if (ev.reset) {
            IR_dprintk(1, "SANYO event reset received. reset to state 0\n");
            data->state = STATE_INACTIVE;
        }
        return 0;
    }

    IR_dprintk(2, "SANYO decode started at state %d (%uus %s)\n",
               data->state, TO_US(ev.duration), TO_STR(ev.pulse));

    switch (data->state) {

    case STATE_INACTIVE:
        if (!ev.pulse)
            break;

        if (eq_margin(ev.duration, SANYO_HEADER_PULSE, SANYO_UNIT / 2)) {
            data->count = 0;
            data->state = STATE_HEADER_SPACE;
            return 0;
        }
        break;


    case STATE_HEADER_SPACE:
        if (ev.pulse)
            break;

        if (eq_margin(ev.duration, SANYO_HEADER_SPACE, SANYO_UNIT / 2)) {
            data->state = STATE_BIT_PULSE;
            return 0;
        }

        break;

    case STATE_BIT_PULSE:
        if (!ev.pulse)
            break;

        if (!eq_margin(ev.duration, SANYO_BIT_PULSE, SANYO_UNIT / 2))
            break;

        data->state = STATE_BIT_SPACE;
        return 0;

    case STATE_BIT_SPACE:
        if (ev.pulse)
            break;

        if (!data->count && geq_margin(ev.duration, SANYO_REPEAT_SPACE, SANYO_UNIT / 2)) {
            if (!dev->keypressed) {
                IR_dprintk(1, "SANYO discarding last key repeat: event after key up\n");
            } else {
                rc_repeat(dev);
                IR_dprintk(1, "SANYO repeat last key\n");
                data->state = STATE_INACTIVE;
            }
            return 0;
        }

        data->bits <<= 1;
        if (eq_margin(ev.duration, SANYO_BIT_1_SPACE, SANYO_UNIT / 2))
            data->bits |= 1;
        else if (!eq_margin(ev.duration, SANYO_BIT_0_SPACE, SANYO_UNIT / 2))
            break;
        data->count++;

        if (data->count == SANYO_NBITS)
            data->state = STATE_TRAILER_PULSE;
        else
            data->state = STATE_BIT_PULSE;

        return 0;

    case STATE_TRAILER_PULSE:
        if (!ev.pulse)
            break;

        if (!eq_margin(ev.duration, SANYO_TRAILER_PULSE, SANYO_UNIT / 2))
            break;

        data->state = STATE_TRAILER_SPACE;
        return 0;

    case STATE_TRAILER_SPACE:
        if (ev.pulse)
            break;

        if (!geq_margin(ev.duration, SANYO_TRAILER_SPACE, SANYO_UNIT / 2))
            break;

        address     = bitrev16((data->bits >> 29) & 0x1fff) >> 3;
        /* not_address = bitrev16((data->bits >> 16) & 0x1fff) >> 3; */
        command	    = bitrev8((data->bits >>  8) & 0xff);
        not_command = bitrev8((data->bits >>  0) & 0xff);

        if ((command ^ not_command) != 0xff) {
            IR_dprintk(1, "SANYO checksum error: received 0x%08Lx\n",
                       data->bits);
            data->state = STATE_INACTIVE;
            return 0;
        }

        scancode = address << 8 | command;
        IR_dprintk(1, "SANYO scancode: 0x%06x\n", scancode);
        rc_keydown(dev, scancode, 0);
        data->state = STATE_INACTIVE;
        return 0;
    }

    IR_dprintk(1, "SANYO decode failed at count %d state %d (%uus %s)\n",
               data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
    data->state = STATE_INACTIVE;
    return -EINVAL;
}
Ejemplo n.º 4
0
static int af9015_rc_query(struct dvb_usb_device *d)
{
    struct af9015_state *state = d_to_priv(d);
    int ret;
    u8 buf[17];

    /* read registers needed to detect remote controller code */
    ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
    if (ret)
        goto error;

    /* If any of these are non-zero, assume invalid data */
    if (buf[1] || buf[2] || buf[3]) {
        dev_dbg(&d->udev->dev, "%s: invalid data\n", __func__);
        return ret;
    }

    /* Check for repeat of previous code */
    if ((state->rc_repeat != buf[6] || buf[0]) &&
            !memcmp(&buf[12], state->rc_last, 4)) {
        dev_dbg(&d->udev->dev, "%s: key repeated\n", __func__);
        rc_repeat(d->rc_dev);
        state->rc_repeat = buf[6];
        return ret;
    }

    /* Only process key if canary killed */
    if (buf[16] != 0xff && buf[0] != 0x01) {
        dev_dbg(&d->udev->dev, "%s: key pressed %*ph\n",
                __func__, 4, buf + 12);

        /* Reset the canary */
        ret = af9015_write_reg(d, 0x98e9, 0xff);
        if (ret)
            goto error;

        /* Remember this key */
        memcpy(state->rc_last, &buf[12], 4);
        if (buf[14] == (u8) ~buf[15]) {
            if (buf[12] == (u8) ~buf[13]) {
                /* NEC */
                state->rc_keycode = RC_SCANCODE_NEC(buf[12],
                                                    buf[14]);
            } else {
                /* NEC extended*/
                state->rc_keycode = RC_SCANCODE_NECX(buf[12] << 8 |
                                                     buf[13],
                                                     buf[14]);
            }
        } else {
            /* 32 bit NEC */
            state->rc_keycode = RC_SCANCODE_NEC32(buf[12] << 24 |
                                                  buf[13] << 16 |
                                                  buf[14] << 8  |
                                                  buf[15]);
        }
        rc_keydown(d->rc_dev, RC_TYPE_NEC, state->rc_keycode, 0);
    } else {
        dev_dbg(&d->udev->dev, "%s: no key press\n", __func__);
        /* Invalidate last keypress */
        /* Not really needed, but helps with debug */
        state->rc_last[2] = state->rc_last[3];
    }

    state->rc_repeat = buf[6];
    state->rc_failed = false;

error:
    if (ret) {
        dev_warn(&d->udev->dev, "%s: rc query failed=%d\n",
                 KBUILD_MODNAME, ret);

        /* allow random errors as dvb-usb will stop polling on error */
        if (!state->rc_failed)
            ret = 0;

        state->rc_failed = true;
    }

    return ret;
}
Ejemplo n.º 5
0
/**
 * ir_nec_decode() - Decode one NEC pulse or space
 * @dev:	the struct rc_dev descriptor of the device
 * @duration:	the struct ir_raw_event descriptor of the pulse/space
 *
 * This function returns -EINVAL if the pulse violates the state machine
 */
static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
{
	struct nec_dec *data = &dev->raw->nec;
	u32 scancode;
	u8 address, not_address, command, not_command;
	bool send_32bits = false;

	if (!(dev->raw->enabled_protocols & RC_TYPE_NEC))
		return 0;

	if (!is_timing_event(ev)) {
		if (ev.reset)
			data->state = STATE_INACTIVE;
		return 0;
	}

	IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));

	switch (data->state) {

	case STATE_INACTIVE:
		if (!ev.pulse)
			break;

		if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2)) {
			data->is_nec_x = false;
			data->necx_repeat = false;
		} else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
			data->is_nec_x = true;
		else
			break;

		data->count = 0;
		data->state = STATE_HEADER_SPACE;
		return 0;

	case STATE_HEADER_SPACE:
		if (ev.pulse)
			break;

		if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
			data->state = STATE_BIT_PULSE;
			return 0;
		} else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
			if (!dev->keypressed) {
				IR_dprintk(1, "Discarding last key repeat: event after key up\n");
			} else {
				rc_repeat(dev);
				IR_dprintk(1, "Repeat last key\n");
				data->state = STATE_TRAILER_PULSE;
			}
			return 0;
		}

		break;

	case STATE_BIT_PULSE:
		if (!ev.pulse)
			break;

		if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
			break;

		data->state = STATE_BIT_SPACE;
		return 0;

	case STATE_BIT_SPACE:
		if (ev.pulse)
			break;

		if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
			geq_margin(ev.duration,
			NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
				IR_dprintk(1, "Repeat last key\n");
				rc_repeat(dev);
				data->state = STATE_INACTIVE;
				return 0;

		} else if (data->count > NECX_REPEAT_BITS)
			data->necx_repeat = false;

		data->bits <<= 1;
		if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
			data->bits |= 1;
		else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
			break;
		data->count++;

		if (data->count == NEC_NBITS)
			data->state = STATE_TRAILER_PULSE;
		else
			data->state = STATE_BIT_PULSE;

		return 0;

	case STATE_TRAILER_PULSE:
		if (!ev.pulse)
			break;

		if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
			break;

		data->state = STATE_TRAILER_SPACE;
		return 0;

	case STATE_TRAILER_SPACE:
		if (ev.pulse)
			break;

		if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
			break;

		address     = bitrev8((data->bits >> 24) & 0xff);
		not_address = bitrev8((data->bits >> 16) & 0xff);
		command	    = bitrev8((data->bits >>  8) & 0xff);
		not_command = bitrev8((data->bits >>  0) & 0xff);

		if ((command ^ not_command) != 0xff) {
			IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
				   data->bits);
			send_32bits = true;
		}

		if (send_32bits) {
			/* NEC transport, but modified protocol, used by at
			 * least Apple and TiVo remotes */
			scancode = data->bits;
			IR_dprintk(1, "NEC (modified) scancode 0x%08x\n", scancode);
		} else if ((address ^ not_address) != 0xff) {
			/* Extended NEC */
			scancode = address     << 16 |
				   not_address <<  8 |
				   command;
			IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
		} else {
			/* Normal NEC */
			scancode = address << 8 | command;
			IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
		}

		if (data->is_nec_x)
			data->necx_repeat = true;

		rc_keydown(dev, scancode, 0);
		data->state = STATE_INACTIVE;
		return 0;
	}

	IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",
		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));
	data->state = STATE_INACTIVE;
	return -EINVAL;
}
Ejemplo n.º 6
0
/**
 * ir_xmp_decode() - Decode one XMP pulse or space
 * @dev:	the struct rc_dev descriptor of the device
 * @duration:	the struct ir_raw_event descriptor of the pulse/space
 *
 * This function returns -EINVAL if the pulse violates the state machine
 */
static int ir_xmp_decode(struct rc_dev *dev, struct ir_raw_event ev)
{
	struct xmp_dec *data = &dev->raw->xmp;

	if (!(dev->enabled_protocols & RC_BIT_XMP))
		return 0;

	if (!is_timing_event(ev)) {
		if (ev.reset)
			data->state = STATE_INACTIVE;
		return 0;
	}

	IR_dprintk(2, "XMP decode started at state %d %d (%uus %s)\n",
		   data->state, data->count, TO_US(ev.duration), TO_STR(ev.pulse));

	switch (data->state) {

	case STATE_INACTIVE:
		if (!ev.pulse)
			break;

		if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2)) {
			data->count = 0;
			data->state = STATE_NIBBLE_SPACE;
		}

		return 0;

	case STATE_LEADER_PULSE:
		if (!ev.pulse)
			break;

		if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2))
			data->state = STATE_NIBBLE_SPACE;

		return 0;

	case STATE_NIBBLE_SPACE:
		if (ev.pulse)
			break;

		if (geq_margin(ev.duration, XMP_TRAILER_SPACE, XMP_NIBBLE_PREFIX)) {
			int divider, i;
			u8 addr, subaddr, subaddr2, toggle, oem, obc1, obc2, sum1, sum2;
			u32 *n;
			u32 scancode;

			if (data->count != 16) {
				IR_dprintk(2, "received TRAILER period at index %d: %u\n",
					data->count, ev.duration);
				data->state = STATE_INACTIVE;
				return -EINVAL;
			}

			n = data->durations;
			/*
			 * the 4th nibble should be 15 so base the divider on this
			 * to transform durations into nibbles. Substract 2000 from
			 * the divider to compensate for fluctuations in the signal
			 */
			divider = (n[3] - XMP_NIBBLE_PREFIX) / 15 - 2000;
			if (divider < 50) {
				IR_dprintk(2, "divider to small %d.\n", divider);
				data->state = STATE_INACTIVE;
				return -EINVAL;
			}

			/* convert to nibbles and do some sanity checks */
			for (i = 0; i < 16; i++)
				n[i] = (n[i] - XMP_NIBBLE_PREFIX) / divider;
			sum1 = (15 + n[0] + n[1] + n[2] + n[3] +
				n[4] + n[5] + n[6] + n[7]) % 16;
			sum2 = (15 + n[8] + n[9] + n[10] + n[11] +
				n[12] + n[13] + n[14] + n[15]) % 16;

			if (sum1 != 15 || sum2 != 15) {
				IR_dprintk(2, "checksum errors sum1=0x%X sum2=0x%X\n",
					sum1, sum2);
				data->state = STATE_INACTIVE;
				return -EINVAL;
			}

			subaddr  = n[0] << 4 | n[2];
			subaddr2 = n[8] << 4 | n[11];
			oem      = n[4] << 4 | n[5];
			addr     = n[6] << 4 | n[7];
			toggle   = n[10];
			obc1 = n[12] << 4 | n[13];
			obc2 = n[14] << 4 | n[15];
			if (subaddr != subaddr2) {
				IR_dprintk(2, "subaddress nibbles mismatch 0x%02X != 0x%02X\n",
					subaddr, subaddr2);
				data->state = STATE_INACTIVE;
				return -EINVAL;
			}
			if (oem != 0x44)
				IR_dprintk(1, "Warning: OEM nibbles 0x%02X. Expected 0x44\n",
					oem);

			scancode = addr << 24 | subaddr << 16 |
				   obc1 << 8 | obc2;
			IR_dprintk(1, "XMP scancode 0x%06x\n", scancode);

			if (toggle == 0) {
				rc_keydown(dev, RC_TYPE_XMP, scancode, 0);
			} else {
				rc_repeat(dev);
				IR_dprintk(1, "Repeat last key\n");
			}
			data->state = STATE_INACTIVE;

			return 0;

		} else if (geq_margin(ev.duration, XMP_HALFFRAME_SPACE, XMP_NIBBLE_PREFIX)) {
			/* Expect 8 or 16 nibble pulses. 16 in case of 'final' frame */
			if (data->count == 16) {
				IR_dprintk(2, "received half frame pulse at index %d. Probably a final frame key-up event: %u\n",
					data->count, ev.duration);
				/*
				 * TODO: for now go back to half frame position
				 *	 so trailer can be found and key press
				 *	 can be handled.
				 */
				data->count = 8;
			}

			else if (data->count != 8)
				IR_dprintk(2, "received half frame pulse at index %d: %u\n",
					data->count, ev.duration);
			data->state = STATE_LEADER_PULSE;

			return 0;

		} else if (geq_margin(ev.duration, XMP_NIBBLE_PREFIX, XMP_UNIT)) {
			/* store nibble raw data, decode after trailer */
			if (data->count == 16) {
				IR_dprintk(2, "to many pulses (%d) ignoring: %u\n",
					data->count, ev.duration);
				data->state = STATE_INACTIVE;
				return -EINVAL;
			}
			data->durations[data->count] = ev.duration;
			data->count++;
			data->state = STATE_LEADER_PULSE;

			return 0;

		}

		break;
	}

	IR_dprintk(1, "XMP decode failed at count %d state %d (%uus %s)\n",
		   data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
	data->state = STATE_INACTIVE;
	return -EINVAL;
}