]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/staging/tm6000/tm6000-input.c
[media] rc: rename the remaining things to rc_core
[linux-beck.git] / 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/rc-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 rc_dev           *rc;
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 };
70
71
72 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
73 {
74         struct tm6000_IR *ir = dev->ir;
75
76         if (!dev->ir)
77                 return;
78
79         if (state)
80                 ir->wait = 1;
81         else
82                 ir->wait = 0;
83 }
84
85
86 static int tm6000_ir_config(struct tm6000_IR *ir)
87 {
88         struct tm6000_core *dev = ir->dev;
89         u8 buf[10];
90         int rc;
91
92         /* hack */
93         buf[0] = 0xff;
94         buf[1] = 0xff;
95         buf[2] = 0xf2;
96         buf[3] = 0x2b;
97         buf[4] = 0x20;
98         buf[5] = 0x35;
99         buf[6] = 0x60;
100         buf[7] = 0x04;
101         buf[8] = 0xc0;
102         buf[9] = 0x08;
103
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);
106         msleep(100);
107
108         if (rc < 0) {
109                 printk(KERN_INFO "IR configuration failed");
110                 return rc;
111         }
112         return 0;
113 }
114
115 static void tm6000_ir_urb_received(struct urb *urb)
116 {
117         struct tm6000_core *dev = urb->context;
118         struct tm6000_IR *ir = dev->ir;
119         int rc;
120
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);
125
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]);
128
129                 ir->key = 1;
130         }
131
132         rc = usb_submit_urb(urb, GFP_ATOMIC);
133 }
134
135 static int default_polling_getkey(struct tm6000_IR *ir,
136                                 struct tm6000_ir_poll_result *poll_result)
137 {
138         struct tm6000_core *dev = ir->dev;
139         int rc;
140         u8 buf[2];
141
142         if (ir->wait && !&dev->int_in)
143                 return 0;
144
145         if (&dev->int_in) {
146                 if (ir->ir_type == IR_TYPE_RC5)
147                         poll_result->rc_data = ir->urb_data[0];
148                 else
149                         poll_result->rc_data = ir->urb_data[0] | ir->urb_data[1] << 8;
150         } else {
151                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
152                 msleep(10);
153                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
154                 msleep(10);
155
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);
160
161                         msleep(10);
162
163                         dprintk("read data=%02x\n", buf[0]);
164                         if (rc < 0)
165                                 return rc;
166
167                         poll_result->rc_data = buf[0];
168                 } else {
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);
172
173                         msleep(10);
174
175                         dprintk("read data=%04x\n", buf[0] | buf[1] << 8);
176                         if (rc < 0)
177                                 return rc;
178
179                         poll_result->rc_data = buf[0] | buf[1] << 8;
180                 }
181                 if ((poll_result->rc_data & 0x00ff) != 0xff)
182                         ir->key = 1;
183         }
184         return 0;
185 }
186
187 static void tm6000_ir_handle_key(struct tm6000_IR *ir)
188 {
189         int result;
190         struct tm6000_ir_poll_result poll_result;
191
192         /* read the registers containing the IR status */
193         result = ir->get_key(ir, &poll_result);
194         if (result < 0) {
195                 printk(KERN_INFO "ir->get_key() failed %d\n", result);
196                 return;
197         }
198
199         dprintk("ir->get_key result data=%04x\n", poll_result.rc_data);
200
201         if (ir->key) {
202                 ir_keydown(ir->rc, poll_result.rc_data, 0);
203                 ir->key = 0;
204         }
205         return;
206 }
207
208 static void tm6000_ir_work(struct work_struct *work)
209 {
210         struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
211
212         tm6000_ir_handle_key(ir);
213         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
214 }
215
216 static int tm6000_ir_start(struct rc_dev *rc)
217 {
218         struct tm6000_IR *ir = rc->priv;
219
220         INIT_DELAYED_WORK(&ir->work, tm6000_ir_work);
221         schedule_delayed_work(&ir->work, 0);
222
223         return 0;
224 }
225
226 static void tm6000_ir_stop(struct rc_dev *rc)
227 {
228         struct tm6000_IR *ir = rc->priv;
229
230         cancel_delayed_work_sync(&ir->work);
231 }
232
233 int tm6000_ir_change_protocol(struct rc_dev *rc, u64 ir_type)
234 {
235         struct tm6000_IR *ir = rc->priv;
236
237         ir->get_key = default_polling_getkey;
238
239         tm6000_ir_config(ir);
240         /* TODO */
241         return 0;
242 }
243
244 int tm6000_ir_init(struct tm6000_core *dev)
245 {
246         struct tm6000_IR *ir;
247         struct rc_dev *rc;
248         int err = -ENOMEM;
249         int pipe, size;
250
251         if (!enable_ir)
252                 return -ENODEV;
253
254         if (!dev->caps.has_remote)
255                 return 0;
256
257         if (!dev->ir_codes)
258                 return 0;
259
260         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
261         rc = rc_allocate_device();
262         if (!ir | !rc)
263                 goto out;
264
265         /* record handles to ourself */
266         ir->dev = dev;
267         dev->ir = ir;
268         ir->rc = rc;
269
270         /* input einrichten */
271         rc->allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
272         rc->priv = ir;
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;
277
278         ir->polling = 50;
279
280         snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
281                                                 dev->name);
282
283         usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
284         strlcat(ir->phys, "/input0", sizeof(ir->phys));
285
286         tm6000_ir_change_protocol(rc, IR_TYPE_UNKNOWN);
287
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;
297
298         if (&dev->int_in) {
299                 dprintk("IR over int\n");
300
301                 ir->int_urb = usb_alloc_urb(0, GFP_KERNEL);
302
303                 pipe = usb_rcvintpipe(dev->udev,
304                         dev->int_in.endp->desc.bEndpointAddress
305                         & USB_ENDPOINT_NUMBER_MASK);
306
307                 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
308                 dprintk("IR max size: %d\n", size);
309
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);
313                         goto out;
314                 }
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);
321                 if (err) {
322                         kfree(ir->int_urb->transfer_buffer);
323                         usb_free_urb(ir->int_urb);
324                         goto out;
325                 }
326                 ir->urb_data = kzalloc(size, GFP_KERNEL);
327         }
328
329         /* ir register */
330         err = rc_register_device(rc);
331         if (err)
332                 goto out;
333
334         return 0;
335
336 out:
337         dev->ir = NULL;
338         rc_free_device(rc);
339         kfree(ir);
340         return err;
341 }
342
343 int tm6000_ir_fini(struct tm6000_core *dev)
344 {
345         struct tm6000_IR *ir = dev->ir;
346
347         /* skip detach on non attached board */
348
349         if (!ir)
350                 return 0;
351
352         rc_unregister_device(ir->rc);
353
354         if (ir->int_urb) {
355                 usb_kill_urb(ir->int_urb);
356                 kfree(ir->int_urb->transfer_buffer);
357                 usb_free_urb(ir->int_urb);
358                 ir->int_urb = NULL;
359                 kfree(ir->urb_data);
360                 ir->urb_data = NULL;
361         }
362
363         kfree(ir);
364         dev->ir = NULL;
365
366         return 0;
367 }