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>
30 #include "tm6000-regs.h"
32 static unsigned int ir_debug
;
33 module_param(ir_debug
, int, 0644);
34 MODULE_PARM_DESC(ir_debug
, "enable debug message [IR]");
36 static unsigned int enable_ir
= 1;
37 module_param(enable_ir
, int, 0644);
38 MODULE_PARM_DESC(enable_ir
, "enable ir (default is enable)");
42 #define dprintk(fmt, arg...) \
44 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
47 struct tm6000_ir_poll_result
{
52 struct tm6000_core
*dev
;
57 /* poll expernal decoder */
59 struct delayed_work work
;
65 int (*get_key
) (struct tm6000_IR
*, struct tm6000_ir_poll_result
*);
67 /* IR device properties */
72 void tm6000_ir_wait(struct tm6000_core
*dev
, u8 state
)
74 struct tm6000_IR
*ir
= dev
->ir
;
86 static int tm6000_ir_config(struct tm6000_IR
*ir
)
88 struct tm6000_core
*dev
= ir
->dev
;
104 rc
= tm6000_read_write_usb(dev
, USB_DIR_OUT
| USB_TYPE_VENDOR
|
105 USB_RECIP_DEVICE
, REQ_00_SET_IR_VALUE
, 0, 0, buf
, 0x0a);
109 printk(KERN_INFO
"IR configuration failed");
115 static void tm6000_ir_urb_received(struct urb
*urb
)
117 struct tm6000_core
*dev
= urb
->context
;
118 struct tm6000_IR
*ir
= dev
->ir
;
121 if (urb
->status
!= 0)
122 printk(KERN_INFO
"not ready\n");
123 else if (urb
->actual_length
> 0) {
124 memcpy(ir
->urb_data
, urb
->transfer_buffer
, urb
->actual_length
);
126 dprintk("data %02x %02x %02x %02x\n", ir
->urb_data
[0],
127 ir
->urb_data
[1], ir
->urb_data
[2], ir
->urb_data
[3]);
132 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
135 static int default_polling_getkey(struct tm6000_IR
*ir
,
136 struct tm6000_ir_poll_result
*poll_result
)
138 struct tm6000_core
*dev
= ir
->dev
;
142 if (ir
->wait
&& !&dev
->int_in
)
146 if (ir
->ir_type
== IR_TYPE_RC5
)
147 poll_result
->rc_data
= ir
->urb_data
[0];
149 poll_result
->rc_data
= ir
->urb_data
[0] | ir
->urb_data
[1] << 8;
151 tm6000_set_reg(dev
, REQ_04_EN_DISABLE_MCU_INT
, 2, 0);
153 tm6000_set_reg(dev
, REQ_04_EN_DISABLE_MCU_INT
, 2, 1);
156 if (ir
->ir_type
== IR_TYPE_RC5
) {
157 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
|
158 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
159 REQ_02_GET_IR_CODE
, 0, 0, buf
, 1);
163 dprintk("read data=%02x\n", buf
[0]);
167 poll_result
->rc_data
= buf
[0];
169 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
|
170 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
171 REQ_02_GET_IR_CODE
, 0, 0, buf
, 2);
175 dprintk("read data=%04x\n", buf
[0] | buf
[1] << 8);
179 poll_result
->rc_data
= buf
[0] | buf
[1] << 8;
181 if ((poll_result
->rc_data
& 0x00ff) != 0xff)
187 static void tm6000_ir_handle_key(struct tm6000_IR
*ir
)
190 struct tm6000_ir_poll_result poll_result
;
192 /* read the registers containing the IR status */
193 result
= ir
->get_key(ir
, &poll_result
);
195 printk(KERN_INFO
"ir->get_key() failed %d\n", result
);
199 dprintk("ir->get_key result data=%04x\n", poll_result
.rc_data
);
202 ir_keydown(ir
->rc
, poll_result
.rc_data
, 0);
208 static void tm6000_ir_work(struct work_struct
*work
)
210 struct tm6000_IR
*ir
= container_of(work
, struct tm6000_IR
, work
.work
);
212 tm6000_ir_handle_key(ir
);
213 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(ir
->polling
));
216 static int tm6000_ir_start(struct rc_dev
*rc
)
218 struct tm6000_IR
*ir
= rc
->priv
;
220 INIT_DELAYED_WORK(&ir
->work
, tm6000_ir_work
);
221 schedule_delayed_work(&ir
->work
, 0);
226 static void tm6000_ir_stop(struct rc_dev
*rc
)
228 struct tm6000_IR
*ir
= rc
->priv
;
230 cancel_delayed_work_sync(&ir
->work
);
233 int tm6000_ir_change_protocol(struct rc_dev
*rc
, u64 ir_type
)
235 struct tm6000_IR
*ir
= rc
->priv
;
237 ir
->get_key
= default_polling_getkey
;
239 tm6000_ir_config(ir
);
244 int tm6000_ir_init(struct tm6000_core
*dev
)
246 struct tm6000_IR
*ir
;
254 if (!dev
->caps
.has_remote
)
260 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
261 rc
= rc_allocate_device();
265 /* record handles to ourself */
270 /* input einrichten */
271 rc
->allowed_protos
= IR_TYPE_RC5
| IR_TYPE_NEC
;
273 rc
->change_protocol
= tm6000_ir_change_protocol
;
274 rc
->open
= tm6000_ir_start
;
275 rc
->close
= tm6000_ir_stop
;
276 rc
->driver_type
= RC_DRIVER_SCANCODE
;
280 snprintf(ir
->name
, sizeof(ir
->name
), "tm5600/60x0 IR (%s)",
283 usb_make_path(dev
->udev
, ir
->phys
, sizeof(ir
->phys
));
284 strlcat(ir
->phys
, "/input0", sizeof(ir
->phys
));
286 tm6000_ir_change_protocol(rc
, IR_TYPE_UNKNOWN
);
288 rc
->input_name
= ir
->name
;
289 rc
->input_phys
= ir
->phys
;
290 rc
->input_id
.bustype
= BUS_USB
;
291 rc
->input_id
.version
= 1;
292 rc
->input_id
.vendor
= le16_to_cpu(dev
->udev
->descriptor
.idVendor
);
293 rc
->input_id
.product
= le16_to_cpu(dev
->udev
->descriptor
.idProduct
);
294 rc
->map_name
= dev
->ir_codes
;
295 rc
->driver_name
= "tm6000";
296 rc
->dev
.parent
= &dev
->udev
->dev
;
299 dprintk("IR over int\n");
301 ir
->int_urb
= usb_alloc_urb(0, GFP_KERNEL
);
303 pipe
= usb_rcvintpipe(dev
->udev
,
304 dev
->int_in
.endp
->desc
.bEndpointAddress
305 & USB_ENDPOINT_NUMBER_MASK
);
307 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
308 dprintk("IR max size: %d\n", size
);
310 ir
->int_urb
->transfer_buffer
= kzalloc(size
, GFP_KERNEL
);
311 if (ir
->int_urb
->transfer_buffer
== NULL
) {
312 usb_free_urb(ir
->int_urb
);
315 dprintk("int interval: %d\n", dev
->int_in
.endp
->desc
.bInterval
);
316 usb_fill_int_urb(ir
->int_urb
, dev
->udev
, pipe
,
317 ir
->int_urb
->transfer_buffer
, size
,
318 tm6000_ir_urb_received
, dev
,
319 dev
->int_in
.endp
->desc
.bInterval
);
320 err
= usb_submit_urb(ir
->int_urb
, GFP_KERNEL
);
322 kfree(ir
->int_urb
->transfer_buffer
);
323 usb_free_urb(ir
->int_urb
);
326 ir
->urb_data
= kzalloc(size
, GFP_KERNEL
);
330 err
= rc_register_device(rc
);
343 int tm6000_ir_fini(struct tm6000_core
*dev
)
345 struct tm6000_IR
*ir
= dev
->ir
;
347 /* skip detach on non attached board */
352 rc_unregister_device(ir
->rc
);
355 usb_kill_urb(ir
->int_urb
);
356 kfree(ir
->int_urb
->transfer_buffer
);
357 usb_free_urb(ir
->int_urb
);