]> Pileus Git - ~andy/linux/blob - drivers/staging/tm6000/tm6000-input.c
[media] ir-core: remove remaining users of the ir-functions keyhandlers
[~andy/linux] / drivers / staging / tm6000 / tm6000-input.c
1 /*
2  *  tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3  *
4  *  Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de>
5  *
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
9  *
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.
14  *
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.
18  */
19
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23
24 #include <linux/input.h>
25 #include <linux/usb.h>
26
27 #include <media/ir-core.h>
28
29 #include "tm6000.h"
30 #include "tm6000-regs.h"
31
32 static unsigned int ir_debug;
33 module_param(ir_debug, int, 0644);
34 MODULE_PARM_DESC(ir_debug, "enable debug message [IR]");
35
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)");
39
40 #undef dprintk
41
42 #define dprintk(fmt, arg...) \
43         if (ir_debug) { \
44                 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
45         }
46
47 struct tm6000_ir_poll_result {
48         u16 rc_data;
49 };
50
51 struct tm6000_IR {
52         struct tm6000_core      *dev;
53         struct ir_input_dev     *input;
54         char                    name[32];
55         char                    phys[32];
56
57         /* poll expernal decoder */
58         int                     polling;
59         struct delayed_work     work;
60         u8                      wait:1;
61         u8                      key:1;
62         struct urb              *int_urb;
63         u8                      *urb_data;
64
65         int (*get_key) (struct tm6000_IR *, struct tm6000_ir_poll_result *);
66
67         /* IR device properties */
68         u64                     ir_type;
69         struct ir_dev_props     props;
70 };
71
72
73 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
74 {
75         struct tm6000_IR *ir = dev->ir;
76
77         if (!dev->ir)
78                 return;
79
80         if (state)
81                 ir->wait = 1;
82         else
83                 ir->wait = 0;
84 }
85
86
87 static int tm6000_ir_config(struct tm6000_IR *ir)
88 {
89         struct tm6000_core *dev = ir->dev;
90         u8 buf[10];
91         int rc;
92
93         /* hack */
94         buf[0] = 0xff;
95         buf[1] = 0xff;
96         buf[2] = 0xf2;
97         buf[3] = 0x2b;
98         buf[4] = 0x20;
99         buf[5] = 0x35;
100         buf[6] = 0x60;
101         buf[7] = 0x04;
102         buf[8] = 0xc0;
103         buf[9] = 0x08;
104
105         rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR |
106                 USB_RECIP_DEVICE, REQ_00_SET_IR_VALUE, 0, 0, buf, 0x0a);
107         msleep(100);
108
109         if (rc < 0) {
110                 printk(KERN_INFO "IR configuration failed");
111                 return rc;
112         }
113         return 0;
114 }
115
116 static void tm6000_ir_urb_received(struct urb *urb)
117 {
118         struct tm6000_core *dev = urb->context;
119         struct tm6000_IR *ir = dev->ir;
120         int rc;
121
122         if (urb->status != 0)
123                 printk(KERN_INFO "not ready\n");
124         else if (urb->actual_length > 0) {
125                 memcpy(ir->urb_data, urb->transfer_buffer, urb->actual_length);
126
127                 dprintk("data %02x %02x %02x %02x\n", ir->urb_data[0],
128                         ir->urb_data[1], ir->urb_data[2], ir->urb_data[3]);
129
130                 ir->key = 1;
131         }
132
133         rc = usb_submit_urb(urb, GFP_ATOMIC);
134 }
135
136 static int default_polling_getkey(struct tm6000_IR *ir,
137                                 struct tm6000_ir_poll_result *poll_result)
138 {
139         struct tm6000_core *dev = ir->dev;
140         int rc;
141         u8 buf[2];
142
143         if (ir->wait && !&dev->int_in)
144                 return 0;
145
146         if (&dev->int_in) {
147                 if (ir->ir_type == IR_TYPE_RC5)
148                         poll_result->rc_data = ir->urb_data[0];
149                 else
150                         poll_result->rc_data = ir->urb_data[0] | ir->urb_data[1] << 8;
151         } else {
152                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
153                 msleep(10);
154                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
155                 msleep(10);
156
157                 if (ir->ir_type == IR_TYPE_RC5) {
158                         rc = tm6000_read_write_usb(dev, USB_DIR_IN |
159                                 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
160                                 REQ_02_GET_IR_CODE, 0, 0, buf, 1);
161
162                         msleep(10);
163
164                         dprintk("read data=%02x\n", buf[0]);
165                         if (rc < 0)
166                                 return rc;
167
168                         poll_result->rc_data = buf[0];
169                 } else {
170                         rc = tm6000_read_write_usb(dev, USB_DIR_IN |
171                                 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
172                                 REQ_02_GET_IR_CODE, 0, 0, buf, 2);
173
174                         msleep(10);
175
176                         dprintk("read data=%04x\n", buf[0] | buf[1] << 8);
177                         if (rc < 0)
178                                 return rc;
179
180                         poll_result->rc_data = buf[0] | buf[1] << 8;
181                 }
182                 if ((poll_result->rc_data & 0x00ff) != 0xff)
183                         ir->key = 1;
184         }
185         return 0;
186 }
187
188 static void tm6000_ir_handle_key(struct tm6000_IR *ir)
189 {
190         int result;
191         struct tm6000_ir_poll_result poll_result;
192
193         /* read the registers containing the IR status */
194         result = ir->get_key(ir, &poll_result);
195         if (result < 0) {
196                 printk(KERN_INFO "ir->get_key() failed %d\n", result);
197                 return;
198         }
199
200         dprintk("ir->get_key result data=%04x\n", poll_result.rc_data);
201
202         if (ir->key) {
203                 ir_keydown(ir->input->input_dev, poll_result.rc_data, 0);
204                 ir->key = 0;
205         }
206         return;
207 }
208
209 static void tm6000_ir_work(struct work_struct *work)
210 {
211         struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
212
213         tm6000_ir_handle_key(ir);
214         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
215 }
216
217 static int tm6000_ir_start(void *priv)
218 {
219         struct tm6000_IR *ir = priv;
220
221         INIT_DELAYED_WORK(&ir->work, tm6000_ir_work);
222         schedule_delayed_work(&ir->work, 0);
223
224         return 0;
225 }
226
227 static void tm6000_ir_stop(void *priv)
228 {
229         struct tm6000_IR *ir = priv;
230
231         cancel_delayed_work_sync(&ir->work);
232 }
233
234 int tm6000_ir_change_protocol(void *priv, u64 ir_type)
235 {
236         struct tm6000_IR *ir = priv;
237
238         ir->get_key = default_polling_getkey;
239
240         tm6000_ir_config(ir);
241         /* TODO */
242         return 0;
243 }
244
245 int tm6000_ir_init(struct tm6000_core *dev)
246 {
247         struct tm6000_IR *ir;
248         struct ir_input_dev *ir_input_dev;
249         int err = -ENOMEM;
250         int pipe, size, rc;
251
252         if (!enable_ir)
253                 return -ENODEV;
254
255         if (!dev->caps.has_remote)
256                 return 0;
257
258         if (!dev->ir_codes)
259                 return 0;
260
261         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
262         ir_input_dev = kzalloc(sizeof(*ir_input_dev), GFP_KERNEL);
263         ir_input_dev->input_dev = input_allocate_device();
264         if (!ir || !ir_input_dev || !ir_input_dev->input_dev)
265                 goto err_out_free;
266
267         /* record handles to ourself */
268         ir->dev = dev;
269         dev->ir = ir;
270
271         ir->input = ir_input_dev;
272
273         /* input einrichten */
274         ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
275         ir->props.priv = ir;
276         ir->props.change_protocol = tm6000_ir_change_protocol;
277         ir->props.open = tm6000_ir_start;
278         ir->props.close = tm6000_ir_stop;
279         ir->props.driver_type = RC_DRIVER_SCANCODE;
280
281         ir->polling = 50;
282
283         snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
284                                                 dev->name);
285
286         usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
287         strlcat(ir->phys, "/input0", sizeof(ir->phys));
288
289         tm6000_ir_change_protocol(ir, IR_TYPE_UNKNOWN);
290
291         ir_input_dev->input_dev->name = ir->name;
292         ir_input_dev->input_dev->phys = ir->phys;
293         ir_input_dev->input_dev->id.bustype = BUS_USB;
294         ir_input_dev->input_dev->id.version = 1;
295         ir_input_dev->input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
296         ir_input_dev->input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
297
298         ir_input_dev->input_dev->dev.parent = &dev->udev->dev;
299
300         if (&dev->int_in) {
301                 dprintk("IR over int\n");
302
303                 ir->int_urb = usb_alloc_urb(0, GFP_KERNEL);
304
305                 pipe = usb_rcvintpipe(dev->udev,
306                         dev->int_in.endp->desc.bEndpointAddress
307                         & USB_ENDPOINT_NUMBER_MASK);
308
309                 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
310                 dprintk("IR max size: %d\n", size);
311
312                 ir->int_urb->transfer_buffer = kzalloc(size, GFP_KERNEL);
313                 if (ir->int_urb->transfer_buffer == NULL) {
314                         usb_free_urb(ir->int_urb);
315                         goto err_out_stop;
316                 }
317                 dprintk("int interval: %d\n", dev->int_in.endp->desc.bInterval);
318                 usb_fill_int_urb(ir->int_urb, dev->udev, pipe,
319                         ir->int_urb->transfer_buffer, size,
320                         tm6000_ir_urb_received, dev,
321                         dev->int_in.endp->desc.bInterval);
322                 rc = usb_submit_urb(ir->int_urb, GFP_KERNEL);
323                 if (rc) {
324                         kfree(ir->int_urb->transfer_buffer);
325                         usb_free_urb(ir->int_urb);
326                         err = rc;
327                         goto err_out_stop;
328                 }
329                 ir->urb_data = kzalloc(size, GFP_KERNEL);
330         }
331
332         /* ir register */
333         err = ir_input_register(ir->input->input_dev, dev->ir_codes,
334                 &ir->props, "tm6000");
335         if (err)
336                 goto err_out_stop;
337
338         return 0;
339
340 err_out_stop:
341         dev->ir = NULL;
342 err_out_free:
343         kfree(ir_input_dev);
344         kfree(ir);
345         return err;
346 }
347
348 int tm6000_ir_fini(struct tm6000_core *dev)
349 {
350         struct tm6000_IR *ir = dev->ir;
351
352         /* skip detach on non attached board */
353
354         if (!ir)
355                 return 0;
356
357         ir_input_unregister(ir->input->input_dev);
358
359         if (ir->int_urb) {
360                 usb_kill_urb(ir->int_urb);
361                 kfree(ir->int_urb->transfer_buffer);
362                 usb_free_urb(ir->int_urb);
363                 ir->int_urb = NULL;
364                 kfree(ir->urb_data);
365                 ir->urb_data = NULL;
366         }
367
368         kfree(ir->input);
369         ir->input = NULL;
370         kfree(ir);
371         dev->ir = NULL;
372
373         return 0;
374 }