]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/rc/iguanair.c
[media] iguanair: ignore unsupported firmware versions
[karo-tx-linux.git] / drivers / media / rc / iguanair.c
1 /*
2  * IguanaWorks USB IR Transceiver support
3  *
4  * Copyright (C) 2012 Sean Young <sean@mess.org>
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; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/usb/input.h>
26 #include <linux/slab.h>
27 #include <linux/completion.h>
28 #include <media/rc-core.h>
29
30 #define DRIVER_NAME "iguanair"
31
32 struct iguanair {
33         struct rc_dev *rc;
34
35         struct device *dev;
36         struct usb_device *udev;
37
38         int pipe_out;
39         uint16_t version;
40         uint8_t bufsize;
41
42         struct mutex lock;
43
44         /* receiver support */
45         bool receiver_on;
46         dma_addr_t dma_in;
47         uint8_t *buf_in;
48         struct urb *urb_in;
49         struct completion completion;
50
51         /* transmit support */
52         bool tx_overflow;
53         uint32_t carrier;
54         uint8_t cycle_overhead;
55         uint8_t channels;
56         uint8_t busy4;
57         uint8_t busy7;
58
59         char name[64];
60         char phys[64];
61 };
62
63 #define CMD_GET_VERSION         0x01
64 #define CMD_GET_BUFSIZE         0x11
65 #define CMD_GET_FEATURES        0x10
66 #define CMD_SEND                0x15
67 #define CMD_EXECUTE             0x1f
68 #define CMD_RX_OVERFLOW         0x31
69 #define CMD_TX_OVERFLOW         0x32
70 #define CMD_RECEIVER_ON         0x12
71 #define CMD_RECEIVER_OFF        0x14
72
73 #define DIR_IN                  0xdc
74 #define DIR_OUT                 0xcd
75
76 #define MAX_PACKET_SIZE         8u
77 #define TIMEOUT                 1000
78
79 struct packet {
80         uint16_t start;
81         uint8_t direction;
82         uint8_t cmd;
83 };
84
85 struct send_packet {
86         struct packet header;
87         uint8_t length;
88         uint8_t channels;
89         uint8_t busy7;
90         uint8_t busy4;
91         uint8_t payload[0];
92 };
93
94 static void process_ir_data(struct iguanair *ir, unsigned len)
95 {
96         if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
97                 switch (ir->buf_in[3]) {
98                 case CMD_GET_VERSION:
99                         if (len == 6) {
100                                 ir->version = (ir->buf_in[5] << 8) |
101                                                         ir->buf_in[4];
102                                 complete(&ir->completion);
103                         }
104                         break;
105                 case CMD_GET_BUFSIZE:
106                         if (len >= 5) {
107                                 ir->bufsize = ir->buf_in[4];
108                                 complete(&ir->completion);
109                         }
110                         break;
111                 case CMD_GET_FEATURES:
112                         if (len > 5) {
113                                 ir->cycle_overhead = ir->buf_in[5];
114                                 complete(&ir->completion);
115                         }
116                         break;
117                 case CMD_TX_OVERFLOW:
118                         ir->tx_overflow = true;
119                 case CMD_RECEIVER_OFF:
120                 case CMD_RECEIVER_ON:
121                 case CMD_SEND:
122                         complete(&ir->completion);
123                         break;
124                 case CMD_RX_OVERFLOW:
125                         dev_warn(ir->dev, "receive overflow\n");
126                         break;
127                 default:
128                         dev_warn(ir->dev, "control code %02x received\n",
129                                                         ir->buf_in[3]);
130                         break;
131                 }
132         } else if (len >= 7) {
133                 DEFINE_IR_RAW_EVENT(rawir);
134                 unsigned i;
135
136                 init_ir_raw_event(&rawir);
137
138                 for (i = 0; i < 7; i++) {
139                         if (ir->buf_in[i] == 0x80) {
140                                 rawir.pulse = false;
141                                 rawir.duration = US_TO_NS(21845);
142                         } else {
143                                 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
144                                 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
145                                                                          21330;
146                         }
147
148                         ir_raw_event_store_with_filter(ir->rc, &rawir);
149                 }
150
151                 ir_raw_event_handle(ir->rc);
152         }
153 }
154
155 static void iguanair_rx(struct urb *urb)
156 {
157         struct iguanair *ir;
158
159         if (!urb)
160                 return;
161
162         ir = urb->context;
163         if (!ir) {
164                 usb_unlink_urb(urb);
165                 return;
166         }
167
168         switch (urb->status) {
169         case 0:
170                 process_ir_data(ir, urb->actual_length);
171                 break;
172         case -ECONNRESET:
173         case -ENOENT:
174         case -ESHUTDOWN:
175                 usb_unlink_urb(urb);
176                 return;
177         case -EPIPE:
178         default:
179                 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
180                 break;
181         }
182
183         usb_submit_urb(urb, GFP_ATOMIC);
184 }
185
186 static int iguanair_send(struct iguanair *ir, void *data, unsigned size)
187 {
188         int rc, transferred;
189
190         INIT_COMPLETION(ir->completion);
191
192         rc = usb_interrupt_msg(ir->udev, ir->pipe_out, data, size,
193                                                         &transferred, TIMEOUT);
194         if (rc)
195                 return rc;
196
197         if (transferred != size)
198                 return -EIO;
199
200         if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
201                 return -ETIMEDOUT;
202
203         return rc;
204 }
205
206 static int iguanair_get_features(struct iguanair *ir)
207 {
208         struct packet packet;
209         int rc;
210
211         packet.start = 0;
212         packet.direction = DIR_OUT;
213         packet.cmd = CMD_GET_VERSION;
214
215         rc = iguanair_send(ir, &packet, sizeof(packet));
216         if (rc) {
217                 dev_info(ir->dev, "failed to get version\n");
218                 goto out;
219         }
220
221         if (ir->version < 0x205) {
222                 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
223                 rc = -ENODEV;
224                 goto out;
225         }
226
227         ir->bufsize = 150;
228         ir->cycle_overhead = 65;
229
230         packet.cmd = CMD_GET_BUFSIZE;
231
232         rc = iguanair_send(ir, &packet, sizeof(packet));
233         if (rc) {
234                 dev_info(ir->dev, "failed to get buffer size\n");
235                 goto out;
236         }
237
238         packet.cmd = CMD_GET_FEATURES;
239
240         rc = iguanair_send(ir, &packet, sizeof(packet));
241         if (rc) {
242                 dev_info(ir->dev, "failed to get features\n");
243                 goto out;
244         }
245
246 out:
247         return rc;
248 }
249
250 static int iguanair_receiver(struct iguanair *ir, bool enable)
251 {
252         struct packet packet = { 0, DIR_OUT, enable ?
253                                 CMD_RECEIVER_ON : CMD_RECEIVER_OFF };
254
255         return iguanair_send(ir, &packet, sizeof(packet));
256 }
257
258 /*
259  * The iguana ir creates the carrier by busy spinning after each pulse or
260  * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
261  * broken down into 7-cycles and 4-cyles delays, with a preference for
262  * 4-cycle delays.
263  */
264 static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
265 {
266         struct iguanair *ir = dev->priv;
267
268         if (carrier < 25000 || carrier > 150000)
269                 return -EINVAL;
270
271         mutex_lock(&ir->lock);
272
273         if (carrier != ir->carrier) {
274                 uint32_t cycles, fours, sevens;
275
276                 ir->carrier = carrier;
277
278                 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
279                                                         ir->cycle_overhead;
280
281                 /*  make up the the remainer of 4-cycle blocks */
282                 switch (cycles & 3) {
283                 case 0:
284                         sevens = 0;
285                         break;
286                 case 1:
287                         sevens = 3;
288                         break;
289                 case 2:
290                         sevens = 2;
291                         break;
292                 case 3:
293                         sevens = 1;
294                         break;
295                 }
296
297                 fours = (cycles - sevens * 7) / 4;
298
299                 /* magic happens here */
300                 ir->busy7 = (4 - sevens) * 2;
301                 ir->busy4 = 110 - fours;
302         }
303
304         mutex_unlock(&ir->lock);
305
306         return carrier;
307 }
308
309 static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
310 {
311         struct iguanair *ir = dev->priv;
312
313         if (mask > 15)
314                 return 4;
315
316         mutex_lock(&ir->lock);
317         ir->channels = mask;
318         mutex_unlock(&ir->lock);
319
320         return 0;
321 }
322
323 static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
324 {
325         struct iguanair *ir = dev->priv;
326         uint8_t space, *payload;
327         unsigned i, size, rc;
328         struct send_packet *packet;
329
330         mutex_lock(&ir->lock);
331
332         /* convert from us to carrier periods */
333         for (i = size = 0; i < count; i++) {
334                 txbuf[i] = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
335                 size += (txbuf[i] + 126) / 127;
336         }
337
338         packet = kmalloc(sizeof(*packet) + size, GFP_KERNEL);
339         if (!packet) {
340                 rc = -ENOMEM;
341                 goto out;
342         }
343
344         if (size > ir->bufsize) {
345                 rc = -E2BIG;
346                 goto out;
347         }
348
349         packet->header.start = 0;
350         packet->header.direction = DIR_OUT;
351         packet->header.cmd = CMD_SEND;
352         packet->length = size;
353         packet->channels = ir->channels << 4;
354         packet->busy7 = ir->busy7;
355         packet->busy4 = ir->busy4;
356
357         space = 0;
358         payload = packet->payload;
359
360         for (i = 0; i < count; i++) {
361                 unsigned periods = txbuf[i];
362
363                 while (periods > 127) {
364                         *payload++ = 127 | space;
365                         periods -= 127;
366                 }
367
368                 *payload++ = periods | space;
369                 space ^= 0x80;
370         }
371
372         if (ir->receiver_on) {
373                 rc = iguanair_receiver(ir, false);
374                 if (rc) {
375                         dev_warn(ir->dev, "disable receiver before transmit failed\n");
376                         goto out;
377                 }
378         }
379
380         ir->tx_overflow = false;
381
382         rc = iguanair_send(ir, packet, size + 8);
383
384         if (rc == 0 && ir->tx_overflow)
385                 rc = -EOVERFLOW;
386
387         if (ir->receiver_on) {
388                 if (iguanair_receiver(ir, true))
389                         dev_warn(ir->dev, "re-enable receiver after transmit failed\n");
390         }
391
392 out:
393         mutex_unlock(&ir->lock);
394         kfree(packet);
395
396         return rc;
397 }
398
399 static int iguanair_open(struct rc_dev *rdev)
400 {
401         struct iguanair *ir = rdev->priv;
402         int rc;
403
404         mutex_lock(&ir->lock);
405
406         BUG_ON(ir->receiver_on);
407
408         rc = iguanair_receiver(ir, true);
409         if (rc == 0)
410                 ir->receiver_on = true;
411
412         mutex_unlock(&ir->lock);
413
414         return rc;
415 }
416
417 static void iguanair_close(struct rc_dev *rdev)
418 {
419         struct iguanair *ir = rdev->priv;
420         int rc;
421
422         mutex_lock(&ir->lock);
423
424         rc = iguanair_receiver(ir, false);
425         ir->receiver_on = false;
426         if (rc)
427                 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
428
429         mutex_unlock(&ir->lock);
430 }
431
432 static int __devinit iguanair_probe(struct usb_interface *intf,
433                                                 const struct usb_device_id *id)
434 {
435         struct usb_device *udev = interface_to_usbdev(intf);
436         struct iguanair *ir;
437         struct rc_dev *rc;
438         int ret, pipein;
439         struct usb_host_interface *idesc;
440
441         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
442         rc = rc_allocate_device();
443         if (!ir || !rc) {
444                 ret = ENOMEM;
445                 goto out;
446         }
447
448         ir->buf_in = usb_alloc_coherent(udev, MAX_PACKET_SIZE, GFP_KERNEL,
449                                                                 &ir->dma_in);
450         ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
451
452         if (!ir->buf_in || !ir->urb_in) {
453                 ret = ENOMEM;
454                 goto out;
455         }
456
457         idesc = intf->altsetting;
458
459         if (idesc->desc.bNumEndpoints < 2) {
460                 ret = -ENODEV;
461                 goto out;
462         }
463
464         ir->rc = rc;
465         ir->dev = &intf->dev;
466         ir->udev = udev;
467         ir->pipe_out = usb_sndintpipe(udev,
468                                 idesc->endpoint[1].desc.bEndpointAddress);
469         mutex_init(&ir->lock);
470         init_completion(&ir->completion);
471
472         pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
473         usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in,
474                 MAX_PACKET_SIZE, iguanair_rx, ir,
475                 idesc->endpoint[0].desc.bInterval);
476         ir->urb_in->transfer_dma = ir->dma_in;
477         ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
478
479         ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
480         if (ret) {
481                 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
482                 goto out;
483         }
484
485         ret = iguanair_get_features(ir);
486         if (ret)
487                 goto out2;
488
489         snprintf(ir->name, sizeof(ir->name),
490                 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
491
492         usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
493
494         rc->input_name = ir->name;
495         rc->input_phys = ir->phys;
496         usb_to_input_id(ir->udev, &rc->input_id);
497         rc->dev.parent = &intf->dev;
498         rc->driver_type = RC_DRIVER_IR_RAW;
499         rc->allowed_protos = RC_TYPE_ALL;
500         rc->priv = ir;
501         rc->open = iguanair_open;
502         rc->close = iguanair_close;
503         rc->s_tx_mask = iguanair_set_tx_mask;
504         rc->s_tx_carrier = iguanair_set_tx_carrier;
505         rc->tx_ir = iguanair_tx;
506         rc->driver_name = DRIVER_NAME;
507         rc->map_name = RC_MAP_EMPTY;
508
509         iguanair_set_tx_carrier(rc, 38000);
510
511         ret = rc_register_device(rc);
512         if (ret < 0) {
513                 dev_err(&intf->dev, "failed to register rc device %d", ret);
514                 goto out2;
515         }
516
517         usb_set_intfdata(intf, ir);
518
519         dev_info(&intf->dev, "Registered %s", ir->name);
520
521         return 0;
522 out2:
523         usb_kill_urb(ir->urb_in);
524 out:
525         if (ir) {
526                 usb_free_urb(ir->urb_in);
527                 usb_free_coherent(udev, MAX_PACKET_SIZE, ir->buf_in,
528                                                                 ir->dma_in);
529         }
530         rc_free_device(rc);
531         kfree(ir);
532         return ret;
533 }
534
535 static void __devexit iguanair_disconnect(struct usb_interface *intf)
536 {
537         struct iguanair *ir = usb_get_intfdata(intf);
538
539         usb_set_intfdata(intf, NULL);
540
541         usb_kill_urb(ir->urb_in);
542         usb_free_urb(ir->urb_in);
543         usb_free_coherent(ir->udev, MAX_PACKET_SIZE, ir->buf_in, ir->dma_in);
544         rc_unregister_device(ir->rc);
545         kfree(ir);
546 }
547
548 static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
549 {
550         struct iguanair *ir = usb_get_intfdata(intf);
551         int rc = 0;
552
553         mutex_lock(&ir->lock);
554
555         if (ir->receiver_on) {
556                 rc = iguanair_receiver(ir, false);
557                 if (rc)
558                         dev_warn(ir->dev, "failed to disable receiver for suspend\n");
559         }
560
561         mutex_unlock(&ir->lock);
562
563         return rc;
564 }
565
566 static int iguanair_resume(struct usb_interface *intf)
567 {
568         struct iguanair *ir = usb_get_intfdata(intf);
569         int rc = 0;
570
571         mutex_lock(&ir->lock);
572
573         if (ir->receiver_on) {
574                 rc = iguanair_receiver(ir, true);
575                 if (rc)
576                         dev_warn(ir->dev, "failed to enable receiver after resume\n");
577         }
578
579         mutex_unlock(&ir->lock);
580
581         return rc;
582 }
583
584 static const struct usb_device_id iguanair_table[] = {
585         { USB_DEVICE(0x1781, 0x0938) },
586         { }
587 };
588
589 static struct usb_driver iguanair_driver = {
590         .name = DRIVER_NAME,
591         .probe = iguanair_probe,
592         .disconnect = __devexit_p(iguanair_disconnect),
593         .suspend = iguanair_suspend,
594         .resume = iguanair_resume,
595         .reset_resume = iguanair_resume,
596         .id_table = iguanair_table
597 };
598
599 module_usb_driver(iguanair_driver);
600
601 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
602 MODULE_AUTHOR("Sean Young <sean@mess.org>");
603 MODULE_LICENSE("GPL");
604 MODULE_DEVICE_TABLE(usb, iguanair_table);
605