Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices
0004  *
0005  *  Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/delay.h>
0011 
0012 #include <linux/input.h>
0013 #include <linux/usb.h>
0014 
0015 #include <media/rc-core.h>
0016 
0017 #include "tm6000.h"
0018 #include "tm6000-regs.h"
0019 
0020 static unsigned int ir_debug;
0021 module_param(ir_debug, int, 0644);
0022 MODULE_PARM_DESC(ir_debug, "debug message level");
0023 
0024 static unsigned int enable_ir = 1;
0025 module_param(enable_ir, int, 0644);
0026 MODULE_PARM_DESC(enable_ir, "enable ir (default is enable)");
0027 
0028 static unsigned int ir_clock_mhz = 12;
0029 module_param(ir_clock_mhz, int, 0644);
0030 MODULE_PARM_DESC(ir_clock_mhz, "ir clock, in MHz");
0031 
0032 #define URB_SUBMIT_DELAY    100 /* ms - Delay to submit an URB request on retrial and init */
0033 #define URB_INT_LED_DELAY   100 /* ms - Delay to turn led on again on int mode */
0034 
0035 #undef dprintk
0036 
0037 #define dprintk(level, fmt, arg...) do {\
0038     if (ir_debug >= level) \
0039         printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
0040     } while (0)
0041 
0042 struct tm6000_ir_poll_result {
0043     u16 rc_data;
0044 };
0045 
0046 struct tm6000_IR {
0047     struct tm6000_core  *dev;
0048     struct rc_dev       *rc;
0049     char            name[32];
0050     char            phys[32];
0051 
0052     /* poll expernal decoder */
0053     int         polling;
0054     struct delayed_work work;
0055     u8          wait:1;
0056     u8          pwled:2;
0057     u8          submit_urb:1;
0058     struct urb      *int_urb;
0059 
0060     /* IR device properties */
0061     u64         rc_proto;
0062 };
0063 
0064 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
0065 {
0066     struct tm6000_IR *ir = dev->ir;
0067 
0068     if (!dev->ir)
0069         return;
0070 
0071     dprintk(2, "%s: %i\n",__func__, ir->wait);
0072 
0073     if (state)
0074         ir->wait = 1;
0075     else
0076         ir->wait = 0;
0077 }
0078 
0079 static int tm6000_ir_config(struct tm6000_IR *ir)
0080 {
0081     struct tm6000_core *dev = ir->dev;
0082     u32 pulse = 0, leader = 0;
0083 
0084     dprintk(2, "%s\n",__func__);
0085 
0086     /*
0087      * The IR decoder supports RC-5 or NEC, with a configurable timing.
0088      * The timing configuration there is not that accurate, as it uses
0089      * approximate values. The NEC spec mentions a 562.5 unit period,
0090      * and RC-5 uses a 888.8 period.
0091      * Currently, driver assumes a clock provided by a 12 MHz XTAL, but
0092      * a modprobe parameter can adjust it.
0093      * Adjustments are required for other timings.
0094      * It seems that the 900ms timing for NEC is used to detect a RC-5
0095      * IR, in order to discard such decoding
0096      */
0097 
0098     switch (ir->rc_proto) {
0099     case RC_PROTO_BIT_NEC:
0100         leader = 900;   /* ms */
0101         pulse  = 700;   /* ms - the actual value would be 562 */
0102         break;
0103     default:
0104     case RC_PROTO_BIT_RC5:
0105         leader = 900;   /* ms - from the NEC decoding */
0106         pulse  = 1780;  /* ms - The actual value would be 1776 */
0107         break;
0108     }
0109 
0110     pulse = ir_clock_mhz * pulse;
0111     leader = ir_clock_mhz * leader;
0112     if (ir->rc_proto == RC_PROTO_BIT_NEC)
0113         leader = leader | 0x8000;
0114 
0115     dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
0116         __func__,
0117         (ir->rc_proto == RC_PROTO_BIT_NEC) ? "NEC" : "RC-5",
0118         ir_clock_mhz, leader, pulse);
0119 
0120     /* Remote WAKEUP = enable, normal mode, from IR decoder output */
0121     tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe);
0122 
0123     /* Enable IR reception on non-busrt mode */
0124     tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f);
0125 
0126     /* IR_WKUP_SEL = Low byte in decoded IR data */
0127     tm6000_set_reg(dev, TM6010_REQ07_RDA_IR_WAKEUP_SEL, 0xff);
0128     /* IR_WKU_ADD code */
0129     tm6000_set_reg(dev, TM6010_REQ07_RDB_IR_WAKEUP_ADD, 0xff);
0130 
0131     tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8);
0132     tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader);
0133 
0134     tm6000_set_reg(dev, TM6010_REQ07_RDE_IR_PULSE_CNT1, pulse >> 8);
0135     tm6000_set_reg(dev, TM6010_REQ07_RDF_IR_PULSE_CNT0, pulse);
0136 
0137     if (!ir->polling)
0138         tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
0139     else
0140         tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
0141     msleep(10);
0142 
0143     /* Shows that IR is working via the LED */
0144     tm6000_flash_led(dev, 0);
0145     msleep(100);
0146     tm6000_flash_led(dev, 1);
0147     ir->pwled = 1;
0148 
0149     return 0;
0150 }
0151 
0152 static void tm6000_ir_keydown(struct tm6000_IR *ir,
0153                   const char *buf, unsigned int len)
0154 {
0155     u8 device, command;
0156     u32 scancode;
0157     enum rc_proto protocol;
0158 
0159     if (len < 1)
0160         return;
0161 
0162     command = buf[0];
0163     device = (len > 1 ? buf[1] : 0x0);
0164     switch (ir->rc_proto) {
0165     case RC_PROTO_BIT_RC5:
0166         protocol = RC_PROTO_RC5;
0167         scancode = RC_SCANCODE_RC5(device, command);
0168         break;
0169     case RC_PROTO_BIT_NEC:
0170         protocol = RC_PROTO_NEC;
0171         scancode = RC_SCANCODE_NEC(device, command);
0172         break;
0173     default:
0174         protocol = RC_PROTO_OTHER;
0175         scancode = RC_SCANCODE_OTHER(device << 8 | command);
0176         break;
0177     }
0178 
0179     dprintk(1, "%s, protocol: 0x%04x, scancode: 0x%08x\n",
0180         __func__, protocol, scancode);
0181     rc_keydown(ir->rc, protocol, scancode, 0);
0182 }
0183 
0184 static void tm6000_ir_urb_received(struct urb *urb)
0185 {
0186     struct tm6000_core *dev = urb->context;
0187     struct tm6000_IR *ir = dev->ir;
0188     char *buf;
0189 
0190     dprintk(2, "%s\n",__func__);
0191     if (urb->status < 0 || urb->actual_length <= 0) {
0192         printk(KERN_INFO "tm6000: IR URB failure: status: %i, length %i\n",
0193                urb->status, urb->actual_length);
0194         ir->submit_urb = 1;
0195         schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
0196         return;
0197     }
0198     buf = urb->transfer_buffer;
0199 
0200     if (ir_debug)
0201         print_hex_dump(KERN_DEBUG, "tm6000: IR data: ",
0202                    DUMP_PREFIX_OFFSET,16, 1,
0203                    buf, urb->actual_length, false);
0204 
0205     tm6000_ir_keydown(ir, urb->transfer_buffer, urb->actual_length);
0206 
0207     usb_submit_urb(urb, GFP_ATOMIC);
0208     /*
0209      * Flash the led. We can't do it here, as it is running on IRQ context.
0210      * So, use the scheduler to do it, in a few ms.
0211      */
0212     ir->pwled = 2;
0213     schedule_delayed_work(&ir->work, msecs_to_jiffies(10));
0214 }
0215 
0216 static void tm6000_ir_handle_key(struct work_struct *work)
0217 {
0218     struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
0219     struct tm6000_core *dev = ir->dev;
0220     int rc;
0221     u8 buf[2];
0222 
0223     if (ir->wait)
0224         return;
0225 
0226     dprintk(3, "%s\n",__func__);
0227 
0228     rc = tm6000_read_write_usb(dev, USB_DIR_IN |
0229         USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0230         REQ_02_GET_IR_CODE, 0, 0, buf, 2);
0231     if (rc < 0)
0232         return;
0233 
0234     /* Check if something was read */
0235     if ((buf[0] & 0xff) == 0xff) {
0236         if (!ir->pwled) {
0237             tm6000_flash_led(dev, 1);
0238             ir->pwled = 1;
0239         }
0240         return;
0241     }
0242 
0243     tm6000_ir_keydown(ir, buf, rc);
0244     tm6000_flash_led(dev, 0);
0245     ir->pwled = 0;
0246 
0247     /* Re-schedule polling */
0248     schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
0249 }
0250 
0251 static void tm6000_ir_int_work(struct work_struct *work)
0252 {
0253     struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
0254     struct tm6000_core *dev = ir->dev;
0255     int rc;
0256 
0257     dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
0258         ir->pwled);
0259 
0260     if (ir->submit_urb) {
0261         dprintk(3, "Resubmit urb\n");
0262         tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
0263 
0264         rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC);
0265         if (rc < 0) {
0266             printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
0267                    rc);
0268             /* Retry in 100 ms */
0269             schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
0270             return;
0271         }
0272         ir->submit_urb = 0;
0273     }
0274 
0275     /* Led is enabled only if USB submit doesn't fail */
0276     if (ir->pwled == 2) {
0277         tm6000_flash_led(dev, 0);
0278         ir->pwled = 0;
0279         schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_INT_LED_DELAY));
0280     } else if (!ir->pwled) {
0281         tm6000_flash_led(dev, 1);
0282         ir->pwled = 1;
0283     }
0284 }
0285 
0286 static int tm6000_ir_start(struct rc_dev *rc)
0287 {
0288     struct tm6000_IR *ir = rc->priv;
0289 
0290     dprintk(2, "%s\n",__func__);
0291 
0292     schedule_delayed_work(&ir->work, 0);
0293 
0294     return 0;
0295 }
0296 
0297 static void tm6000_ir_stop(struct rc_dev *rc)
0298 {
0299     struct tm6000_IR *ir = rc->priv;
0300 
0301     dprintk(2, "%s\n",__func__);
0302 
0303     cancel_delayed_work_sync(&ir->work);
0304 }
0305 
0306 static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
0307 {
0308     struct tm6000_IR *ir = rc->priv;
0309 
0310     if (!ir)
0311         return 0;
0312 
0313     dprintk(2, "%s\n",__func__);
0314 
0315     ir->rc_proto = *rc_proto;
0316 
0317     tm6000_ir_config(ir);
0318     /* TODO */
0319     return 0;
0320 }
0321 
0322 static int __tm6000_ir_int_start(struct rc_dev *rc)
0323 {
0324     struct tm6000_IR *ir = rc->priv;
0325     struct tm6000_core *dev;
0326     int pipe, size;
0327     int err = -ENOMEM;
0328 
0329     if (!ir)
0330         return -ENODEV;
0331     dev = ir->dev;
0332 
0333     dprintk(2, "%s\n",__func__);
0334 
0335     ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
0336     if (!ir->int_urb)
0337         return -ENOMEM;
0338 
0339     pipe = usb_rcvintpipe(dev->udev,
0340         dev->int_in.endp->desc.bEndpointAddress
0341         & USB_ENDPOINT_NUMBER_MASK);
0342 
0343     size = usb_maxpacket(dev->udev, pipe);
0344     dprintk(1, "IR max size: %d\n", size);
0345 
0346     ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC);
0347     if (!ir->int_urb->transfer_buffer) {
0348         usb_free_urb(ir->int_urb);
0349         return err;
0350     }
0351     dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval);
0352 
0353     usb_fill_int_urb(ir->int_urb, dev->udev, pipe,
0354         ir->int_urb->transfer_buffer, size,
0355         tm6000_ir_urb_received, dev,
0356         dev->int_in.endp->desc.bInterval);
0357 
0358     ir->submit_urb = 1;
0359     schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
0360 
0361     return 0;
0362 }
0363 
0364 static void __tm6000_ir_int_stop(struct rc_dev *rc)
0365 {
0366     struct tm6000_IR *ir = rc->priv;
0367 
0368     if (!ir || !ir->int_urb)
0369         return;
0370 
0371     dprintk(2, "%s\n",__func__);
0372 
0373     usb_kill_urb(ir->int_urb);
0374     kfree(ir->int_urb->transfer_buffer);
0375     usb_free_urb(ir->int_urb);
0376     ir->int_urb = NULL;
0377 }
0378 
0379 int tm6000_ir_int_start(struct tm6000_core *dev)
0380 {
0381     struct tm6000_IR *ir = dev->ir;
0382 
0383     if (!ir)
0384         return 0;
0385 
0386     return __tm6000_ir_int_start(ir->rc);
0387 }
0388 
0389 void tm6000_ir_int_stop(struct tm6000_core *dev)
0390 {
0391     struct tm6000_IR *ir = dev->ir;
0392 
0393     if (!ir || !ir->rc)
0394         return;
0395 
0396     __tm6000_ir_int_stop(ir->rc);
0397 }
0398 
0399 int tm6000_ir_init(struct tm6000_core *dev)
0400 {
0401     struct tm6000_IR *ir;
0402     struct rc_dev *rc;
0403     int err = -ENOMEM;
0404     u64 rc_proto;
0405 
0406     if (!enable_ir)
0407         return -ENODEV;
0408 
0409     if (!dev->caps.has_remote)
0410         return 0;
0411 
0412     if (!dev->ir_codes)
0413         return 0;
0414 
0415     ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
0416     rc = rc_allocate_device(RC_DRIVER_SCANCODE);
0417     if (!ir || !rc)
0418         goto out;
0419 
0420     dprintk(2, "%s\n", __func__);
0421 
0422     /* record handles to ourself */
0423     ir->dev = dev;
0424     dev->ir = ir;
0425     ir->rc = rc;
0426 
0427     /* input setup */
0428     rc->allowed_protocols = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_NEC;
0429     /* Needed, in order to support NEC remotes with 24 or 32 bits */
0430     rc->scancode_mask = 0xffff;
0431     rc->priv = ir;
0432     rc->change_protocol = tm6000_ir_change_protocol;
0433     if (dev->int_in.endp) {
0434         rc->open    = __tm6000_ir_int_start;
0435         rc->close   = __tm6000_ir_int_stop;
0436         INIT_DELAYED_WORK(&ir->work, tm6000_ir_int_work);
0437     } else {
0438         rc->open  = tm6000_ir_start;
0439         rc->close = tm6000_ir_stop;
0440         ir->polling = 50;
0441         INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
0442     }
0443 
0444     snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
0445                         dev->name);
0446 
0447     usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
0448     strlcat(ir->phys, "/input0", sizeof(ir->phys));
0449 
0450     rc_proto = RC_PROTO_BIT_UNKNOWN;
0451     tm6000_ir_change_protocol(rc, &rc_proto);
0452 
0453     rc->device_name = ir->name;
0454     rc->input_phys = ir->phys;
0455     rc->input_id.bustype = BUS_USB;
0456     rc->input_id.version = 1;
0457     rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
0458     rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
0459     rc->map_name = dev->ir_codes;
0460     rc->driver_name = "tm6000";
0461     rc->dev.parent = &dev->udev->dev;
0462 
0463     /* ir register */
0464     err = rc_register_device(rc);
0465     if (err)
0466         goto out;
0467 
0468     return 0;
0469 
0470 out:
0471     dev->ir = NULL;
0472     rc_free_device(rc);
0473     kfree(ir);
0474     return err;
0475 }
0476 
0477 int tm6000_ir_fini(struct tm6000_core *dev)
0478 {
0479     struct tm6000_IR *ir = dev->ir;
0480 
0481     /* skip detach on non attached board */
0482 
0483     if (!ir)
0484         return 0;
0485 
0486     dprintk(2, "%s\n",__func__);
0487 
0488     if (!ir->polling)
0489         __tm6000_ir_int_stop(ir->rc);
0490 
0491     tm6000_ir_stop(ir->rc);
0492 
0493     /* Turn off the led */
0494     tm6000_flash_led(dev, 0);
0495     ir->pwled = 0;
0496 
0497     rc_unregister_device(ir->rc);
0498 
0499     kfree(ir);
0500     dev->ir = NULL;
0501 
0502     return 0;
0503 }