2 tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 2
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
24 #include <linux/input.h>
25 #include <linux/usb.h>
27 #include <media/ir-core.h>
28 #include <media/ir-common.h>
31 #include "tm6000-regs.h"
33 static unsigned int ir_debug;
34 module_param(ir_debug, int, 0644);
35 MODULE_PARM_DESC(ir_debug, "enable debug message [IR]");
37 static unsigned int enable_ir = 1;
38 module_param(enable_ir, int, 0644);
39 MODULE_PARM_DESC(enable_ir, "enable ir (default is enable");
43 #define dprintk(fmt, arg...) \
45 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
48 struct tm6000_ir_poll_result {
53 struct tm6000_core *dev;
54 struct ir_input_dev *input;
55 struct ir_input_state ir;
59 /* poll expernal decoder */
61 struct delayed_work work;
67 int (*get_key) (struct tm6000_IR *, struct tm6000_ir_poll_result *);
69 /* IR device properties */
70 struct ir_dev_props props;
74 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
76 struct tm6000_IR *ir = dev->ir;
88 static int tm6000_ir_config(struct tm6000_IR *ir)
90 struct tm6000_core *dev = ir->dev;
106 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
107 USB_RECIP_DEVICE, REQ_00_SET_IR_VALUE, 0, 0, buf, 0x0a);
111 printk(KERN_INFO "IR configuration failed");
117 static void tm6000_ir_urb_received(struct urb *urb)
119 struct tm6000_core *dev = urb->context;
120 struct tm6000_IR *ir = dev->ir;
123 if (urb->status != 0)
124 printk(KERN_INFO "not ready\n");
125 else if (urb->actual_length > 0)
126 memcpy(ir->urb_data, urb->transfer_buffer, urb->actual_length);
128 dprintk("data %02x %02x %02x %02x\n", ir->urb_data[0],
129 ir->urb_data[1], ir->urb_data[2], ir->urb_data[3]);
133 rc = usb_submit_urb(urb, GFP_ATOMIC);
136 static int default_polling_getkey(struct tm6000_IR *ir,
137 struct tm6000_ir_poll_result *poll_result)
139 struct tm6000_core *dev = ir->dev;
143 if (ir->wait && !&dev->int_in) {
144 poll_result->rc_data[0] = 0xff;
149 poll_result->rc_data[0] = ir->urb_data[0];
150 poll_result->rc_data[1] = ir->urb_data[1];
152 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
154 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
157 rc = tm6000_read_write_usb(dev, USB_DIR_IN | USB_TYPE_VENDOR |
158 USB_RECIP_DEVICE, REQ_02_GET_IR_CODE, 0, 0, buf, 1);
162 dprintk("read data=%02x\n", buf[0]);
166 poll_result->rc_data[0] = buf[0];
171 static void tm6000_ir_handle_key(struct tm6000_IR *ir)
174 struct tm6000_ir_poll_result poll_result;
176 /* read the registers containing the IR status */
177 result = ir->get_key(ir, &poll_result);
179 printk(KERN_INFO "ir->get_key() failed %d\n", result);
183 dprintk("ir->get_key result data=%02x %02x\n",
184 poll_result.rc_data[0], poll_result.rc_data[1]);
186 if (poll_result.rc_data[0] != 0xff && ir->key == 1) {
187 ir_input_keydown(ir->input->input_dev, &ir->ir,
188 poll_result.rc_data[0] | poll_result.rc_data[1] << 8);
190 ir_input_nokey(ir->input->input_dev, &ir->ir);
196 static void tm6000_ir_work(struct work_struct *work)
198 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
200 tm6000_ir_handle_key(ir);
201 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
204 static int tm6000_ir_start(void *priv)
206 struct tm6000_IR *ir = priv;
208 INIT_DELAYED_WORK(&ir->work, tm6000_ir_work);
209 schedule_delayed_work(&ir->work, 0);
214 static void tm6000_ir_stop(void *priv)
216 struct tm6000_IR *ir = priv;
218 cancel_delayed_work_sync(&ir->work);
221 int tm6000_ir_change_protocol(void *priv, u64 ir_type)
223 struct tm6000_IR *ir = priv;
225 ir->get_key = default_polling_getkey;
227 tm6000_ir_config(ir);
232 int tm6000_ir_init(struct tm6000_core *dev)
234 struct tm6000_IR *ir;
235 struct ir_input_dev *ir_input_dev;
242 if (!dev->caps.has_remote)
248 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
249 ir_input_dev = kzalloc(sizeof(*ir_input_dev), GFP_KERNEL);
250 ir_input_dev->input_dev = input_allocate_device();
251 if (!ir || !ir_input_dev || !ir_input_dev->input_dev)
254 /* record handles to ourself */
258 ir->input = ir_input_dev;
260 /* input einrichten */
261 ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
263 ir->props.change_protocol = tm6000_ir_change_protocol;
264 ir->props.open = tm6000_ir_start;
265 ir->props.close = tm6000_ir_stop;
266 ir->props.driver_type = RC_DRIVER_SCANCODE;
270 snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
273 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
274 strlcat(ir->phys, "/input0", sizeof(ir->phys));
276 tm6000_ir_change_protocol(ir, IR_TYPE_UNKNOWN);
277 err = ir_input_init(ir_input_dev->input_dev, &ir->ir, IR_TYPE_OTHER);
281 ir_input_dev->input_dev->name = ir->name;
282 ir_input_dev->input_dev->phys = ir->phys;
283 ir_input_dev->input_dev->id.bustype = BUS_USB;
284 ir_input_dev->input_dev->id.version = 1;
285 ir_input_dev->input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
286 ir_input_dev->input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
288 ir_input_dev->input_dev->dev.parent = &dev->udev->dev;
291 dprintk("IR over int\n");
293 ir->int_urb = usb_alloc_urb(0, GFP_KERNEL);
295 pipe = usb_rcvintpipe(dev->udev,
296 dev->int_in.endp->desc.bEndpointAddress
297 & USB_ENDPOINT_NUMBER_MASK);
299 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
300 dprintk("IR max size: %d\n", size);
302 ir->int_urb->transfer_buffer = kzalloc(size, GFP_KERNEL);
303 if (ir->int_urb->transfer_buffer == NULL) {
304 usb_free_urb(ir->int_urb);
307 dprintk("int interval: %d\n", dev->int_in.endp->desc.bInterval);
308 usb_fill_int_urb(ir->int_urb, dev->udev, pipe,
309 ir->int_urb->transfer_buffer, size,
310 tm6000_ir_urb_received, dev,
311 dev->int_in.endp->desc.bInterval);
312 rc = usb_submit_urb(ir->int_urb, GFP_KERNEL);
314 kfree(ir->int_urb->transfer_buffer);
315 usb_free_urb(ir->int_urb);
319 ir->urb_data = kzalloc(size, GFP_KERNEL);
323 err = ir_input_register(ir->input->input_dev, dev->ir_codes,
324 &ir->props, "tm6000");
338 int tm6000_ir_fini(struct tm6000_core *dev)
340 struct tm6000_IR *ir = dev->ir;
342 /* skip detach on non attached board */
347 ir_input_unregister(ir->input->input_dev);
350 usb_kill_urb(ir->int_urb);
351 kfree(ir->int_urb->transfer_buffer);
352 usb_free_urb(ir->int_urb);