Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Copyright (C) 2018 Sean Young <sean@mess.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/usb/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <media/rc-core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) /* Each bit is 250us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #define BIT_DURATION 250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) struct imon {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	struct urb *ir_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	struct rc_dev *rcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	__be64 ir_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	char phys[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * The first 5 bytes of data represent IR pulse or space. Each bit, starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * from highest bit in the first byte, represents 250µs of data. It is 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * for space and 0 for pulse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * The station sends 10 packets, and the 7th byte will be number 1 to 10, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * when we receive 10 we assume all the data has arrived.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static void imon_ir_data(struct imon *imon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct ir_raw_event rawir = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	u64 data = be64_to_cpu(imon->ir_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	u8 packet_no = data & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int offset = 40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (packet_no == 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	dev_dbg(imon->dev, "data: %*ph", 8, &imon->ir_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	 * Only the first 5 bytes contain IR data. Right shift so we move
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	 * the IR bits to the lower 40 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	data >>= 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		 * Find highest set bit which is less or equal to offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		 * offset is the bit above (base 0) where we start looking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		 * data & (BIT_ULL(offset) - 1) masks off any unwanted bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		 * so we have just bits less than offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		 * fls will tell us the highest bit set plus 1 (or 0 if no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		 * bits are set).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		rawir.pulse = !rawir.pulse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		bit = fls64(data & (BIT_ULL(offset) - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		if (bit < offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			dev_dbg(imon->dev, "%s: %d bits",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				rawir.pulse ? "pulse" : "space", offset - bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			rawir.duration = (offset - bit) * BIT_DURATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			ir_raw_event_store_with_filter(imon->rcdev, &rawir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			offset = bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		data = ~data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	} while (offset > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (packet_no == 0x0a && !imon->rcdev->idle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		ir_raw_event_set_idle(imon->rcdev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		ir_raw_event_handle(imon->rcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static void imon_ir_rx(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct imon *imon = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		imon_ir_data(imon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		usb_unlink_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		dev_dbg(imon->dev, "error: urb status = %d", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	ret = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (ret && ret != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		dev_warn(imon->dev, "failed to resubmit urb: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int imon_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		      const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct usb_endpoint_descriptor *ir_ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct usb_host_interface *idesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct rc_dev *rcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct imon *imon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	udev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	idesc = intf->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		struct usb_endpoint_descriptor *ep = &idesc->endpoint[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		if (usb_endpoint_is_int_in(ep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			ir_ep = ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!ir_ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		dev_err(&intf->dev, "IR endpoint missing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	imon = devm_kmalloc(&intf->dev, sizeof(*imon), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (!imon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	imon->ir_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (!imon->ir_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	imon->dev = &intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	usb_fill_int_urb(imon->ir_urb, udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			 usb_rcvintpipe(udev, ir_ep->bEndpointAddress),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			 &imon->ir_buf, sizeof(imon->ir_buf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			 imon_ir_rx, imon, ir_ep->bInterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	rcdev = devm_rc_allocate_device(&intf->dev, RC_DRIVER_IR_RAW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (!rcdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		goto free_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	usb_make_path(udev, imon->phys, sizeof(imon->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	rcdev->device_name = "iMON Station";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	rcdev->driver_name = KBUILD_MODNAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	rcdev->input_phys = imon->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	usb_to_input_id(udev, &rcdev->input_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	rcdev->dev.parent = &intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	rcdev->map_name = RC_MAP_IMON_RSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	rcdev->rx_resolution = BIT_DURATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	rcdev->priv = imon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	ret = devm_rc_register_device(&intf->dev, rcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		goto free_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	imon->rcdev = rcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	ret = usb_submit_urb(imon->ir_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		goto free_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	usb_set_intfdata(intf, imon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) free_urb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	usb_free_urb(imon->ir_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static void imon_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct imon *imon = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	usb_kill_urb(imon->ir_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	usb_free_urb(imon->ir_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static const struct usb_device_id imon_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	/* SoundGraph iMON (IR only) -- sg_imon.inf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	{ USB_DEVICE(0x04e8, 0xff30) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static struct usb_driver imon_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.name = KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.probe = imon_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.disconnect = imon_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.id_table = imon_table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) module_usb_driver(imon_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) MODULE_DESCRIPTION("Early raw iMON IR devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) MODULE_AUTHOR("Sean Young <sean@mess.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_DEVICE_TABLE(usb, imon_table);