[media] rc-core: add separate defines for protocol bitmaps and numbers
[deliverable/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 #define BUF_SIZE 152
32
33 struct iguanair {
34 struct rc_dev *rc;
35
36 struct device *dev;
37 struct usb_device *udev;
38
39 uint16_t version;
40 uint8_t bufsize;
41 uint8_t cycle_overhead;
42
43 struct mutex lock;
44
45 /* receiver support */
46 bool receiver_on;
47 dma_addr_t dma_in, dma_out;
48 uint8_t *buf_in;
49 struct urb *urb_in, *urb_out;
50 struct completion completion;
51
52 /* transmit support */
53 bool tx_overflow;
54 uint32_t carrier;
55 struct send_packet *packet;
56
57 char name[64];
58 char phys[64];
59 };
60
61 #define CMD_GET_VERSION 0x01
62 #define CMD_GET_BUFSIZE 0x11
63 #define CMD_GET_FEATURES 0x10
64 #define CMD_SEND 0x15
65 #define CMD_EXECUTE 0x1f
66 #define CMD_RX_OVERFLOW 0x31
67 #define CMD_TX_OVERFLOW 0x32
68 #define CMD_RECEIVER_ON 0x12
69 #define CMD_RECEIVER_OFF 0x14
70
71 #define DIR_IN 0xdc
72 #define DIR_OUT 0xcd
73
74 #define MAX_IN_PACKET 8u
75 #define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
76 #define TIMEOUT 1000
77 #define RX_RESOLUTION 21333
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 ir_raw_event_reset(ir->rc);
127 break;
128 default:
129 dev_warn(ir->dev, "control code %02x received\n",
130 ir->buf_in[3]);
131 break;
132 }
133 } else if (len >= 7) {
134 DEFINE_IR_RAW_EVENT(rawir);
135 unsigned i;
136 bool event = false;
137
138 init_ir_raw_event(&rawir);
139
140 for (i = 0; i < 7; i++) {
141 if (ir->buf_in[i] == 0x80) {
142 rawir.pulse = false;
143 rawir.duration = US_TO_NS(21845);
144 } else {
145 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
146 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
147 RX_RESOLUTION;
148 }
149
150 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
151 event = true;
152 }
153
154 if (event)
155 ir_raw_event_handle(ir->rc);
156 }
157 }
158
159 static void iguanair_rx(struct urb *urb)
160 {
161 struct iguanair *ir;
162 int rc;
163
164 if (!urb)
165 return;
166
167 ir = urb->context;
168 if (!ir) {
169 usb_unlink_urb(urb);
170 return;
171 }
172
173 switch (urb->status) {
174 case 0:
175 process_ir_data(ir, urb->actual_length);
176 break;
177 case -ECONNRESET:
178 case -ENOENT:
179 case -ESHUTDOWN:
180 usb_unlink_urb(urb);
181 return;
182 case -EPIPE:
183 default:
184 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
185 break;
186 }
187
188 rc = usb_submit_urb(urb, GFP_ATOMIC);
189 if (rc && rc != -ENODEV)
190 dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
191 }
192
193 static void iguanair_irq_out(struct urb *urb)
194 {
195 struct iguanair *ir = urb->context;
196
197 if (urb->status)
198 dev_dbg(ir->dev, "Error: out urb status = %d\n", urb->status);
199 }
200
201 static int iguanair_send(struct iguanair *ir, unsigned size)
202 {
203 int rc;
204
205 INIT_COMPLETION(ir->completion);
206
207 ir->urb_out->transfer_buffer_length = size;
208 rc = usb_submit_urb(ir->urb_out, GFP_KERNEL);
209 if (rc)
210 return rc;
211
212 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
213 return -ETIMEDOUT;
214
215 return rc;
216 }
217
218 static int iguanair_get_features(struct iguanair *ir)
219 {
220 int rc;
221
222 ir->packet->header.start = 0;
223 ir->packet->header.direction = DIR_OUT;
224 ir->packet->header.cmd = CMD_GET_VERSION;
225
226 rc = iguanair_send(ir, sizeof(ir->packet->header));
227 if (rc) {
228 dev_info(ir->dev, "failed to get version\n");
229 goto out;
230 }
231
232 if (ir->version < 0x205) {
233 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
234 rc = -ENODEV;
235 goto out;
236 }
237
238 ir->bufsize = 150;
239 ir->cycle_overhead = 65;
240
241 ir->packet->header.cmd = CMD_GET_BUFSIZE;
242
243 rc = iguanair_send(ir, sizeof(ir->packet->header));
244 if (rc) {
245 dev_info(ir->dev, "failed to get buffer size\n");
246 goto out;
247 }
248
249 if (ir->bufsize > BUF_SIZE) {
250 dev_info(ir->dev, "buffer size %u larger than expected\n",
251 ir->bufsize);
252 ir->bufsize = BUF_SIZE;
253 }
254
255 ir->packet->header.cmd = CMD_GET_FEATURES;
256
257 rc = iguanair_send(ir, sizeof(ir->packet->header));
258 if (rc) {
259 dev_info(ir->dev, "failed to get features\n");
260 goto out;
261 }
262
263 out:
264 return rc;
265 }
266
267 static int iguanair_receiver(struct iguanair *ir, bool enable)
268 {
269 int rc;
270
271 ir->packet->header.start = 0;
272 ir->packet->header.direction = DIR_OUT;
273 ir->packet->header.cmd = enable ? CMD_RECEIVER_ON : CMD_RECEIVER_OFF;
274
275 if (enable)
276 ir_raw_event_reset(ir->rc);
277
278 rc = iguanair_send(ir, sizeof(ir->packet->header));
279
280 return rc;
281 }
282
283 /*
284 * The iguana ir creates the carrier by busy spinning after each pulse or
285 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
286 * broken down into 7-cycles and 4-cyles delays, with a preference for
287 * 4-cycle delays.
288 */
289 static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
290 {
291 struct iguanair *ir = dev->priv;
292
293 if (carrier < 25000 || carrier > 150000)
294 return -EINVAL;
295
296 mutex_lock(&ir->lock);
297
298 if (carrier != ir->carrier) {
299 uint32_t cycles, fours, sevens;
300
301 ir->carrier = carrier;
302
303 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
304 ir->cycle_overhead;
305
306 /* make up the the remainer of 4-cycle blocks */
307 switch (cycles & 3) {
308 case 0:
309 sevens = 0;
310 break;
311 case 1:
312 sevens = 3;
313 break;
314 case 2:
315 sevens = 2;
316 break;
317 case 3:
318 sevens = 1;
319 break;
320 }
321
322 fours = (cycles - sevens * 7) / 4;
323
324 /* magic happens here */
325 ir->packet->busy7 = (4 - sevens) * 2;
326 ir->packet->busy4 = 110 - fours;
327 }
328
329 mutex_unlock(&ir->lock);
330
331 return carrier;
332 }
333
334 static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
335 {
336 struct iguanair *ir = dev->priv;
337
338 if (mask > 15)
339 return 4;
340
341 mutex_lock(&ir->lock);
342 ir->packet->channels = mask << 4;
343 mutex_unlock(&ir->lock);
344
345 return 0;
346 }
347
348 static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
349 {
350 struct iguanair *ir = dev->priv;
351 uint8_t space;
352 unsigned i, size, periods, bytes;
353 int rc;
354
355 mutex_lock(&ir->lock);
356
357 /* convert from us to carrier periods */
358 for (i = space = size = 0; i < count; i++) {
359 periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
360 bytes = DIV_ROUND_UP(periods, 127);
361 if (size + bytes > ir->bufsize) {
362 count = i;
363 break;
364 }
365 while (periods > 127) {
366 ir->packet->payload[size++] = 127 | space;
367 periods -= 127;
368 }
369
370 ir->packet->payload[size++] = periods | space;
371 space ^= 0x80;
372 }
373
374 if (count == 0) {
375 rc = -EINVAL;
376 goto out;
377 }
378
379 ir->packet->header.start = 0;
380 ir->packet->header.direction = DIR_OUT;
381 ir->packet->header.cmd = CMD_SEND;
382 ir->packet->length = size;
383
384 ir->tx_overflow = false;
385
386 rc = iguanair_send(ir, sizeof(*ir->packet) + size);
387
388 if (rc == 0 && ir->tx_overflow)
389 rc = -EOVERFLOW;
390
391 out:
392 mutex_unlock(&ir->lock);
393
394 return rc ? rc : count;
395 }
396
397 static int iguanair_open(struct rc_dev *rdev)
398 {
399 struct iguanair *ir = rdev->priv;
400 int rc;
401
402 mutex_lock(&ir->lock);
403
404 rc = iguanair_receiver(ir, true);
405 if (rc == 0)
406 ir->receiver_on = true;
407
408 mutex_unlock(&ir->lock);
409
410 return rc;
411 }
412
413 static void iguanair_close(struct rc_dev *rdev)
414 {
415 struct iguanair *ir = rdev->priv;
416 int rc;
417
418 mutex_lock(&ir->lock);
419
420 rc = iguanair_receiver(ir, false);
421 ir->receiver_on = false;
422 if (rc && rc != -ENODEV)
423 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
424
425 mutex_unlock(&ir->lock);
426 }
427
428 static int __devinit iguanair_probe(struct usb_interface *intf,
429 const struct usb_device_id *id)
430 {
431 struct usb_device *udev = interface_to_usbdev(intf);
432 struct iguanair *ir;
433 struct rc_dev *rc;
434 int ret, pipein, pipeout;
435 struct usb_host_interface *idesc;
436
437 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
438 rc = rc_allocate_device();
439 if (!ir || !rc) {
440 ret = -ENOMEM;
441 goto out;
442 }
443
444 ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL,
445 &ir->dma_in);
446 ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL,
447 &ir->dma_out);
448 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
449 ir->urb_out = usb_alloc_urb(0, GFP_KERNEL);
450
451 if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out) {
452 ret = -ENOMEM;
453 goto out;
454 }
455
456 idesc = intf->altsetting;
457
458 if (idesc->desc.bNumEndpoints < 2) {
459 ret = -ENODEV;
460 goto out;
461 }
462
463 ir->rc = rc;
464 ir->dev = &intf->dev;
465 ir->udev = udev;
466 mutex_init(&ir->lock);
467
468 init_completion(&ir->completion);
469 pipeout = usb_sndintpipe(udev,
470 idesc->endpoint[1].desc.bEndpointAddress);
471 usb_fill_int_urb(ir->urb_out, udev, pipeout, ir->packet, MAX_OUT_PACKET,
472 iguanair_irq_out, ir, 1);
473 ir->urb_out->transfer_dma = ir->dma_out;
474 ir->urb_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
475
476 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
477 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET,
478 iguanair_rx, ir, 1);
479 ir->urb_in->transfer_dma = ir->dma_in;
480 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
481
482 ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
483 if (ret) {
484 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
485 goto out;
486 }
487
488 ret = iguanair_get_features(ir);
489 if (ret)
490 goto out2;
491
492 snprintf(ir->name, sizeof(ir->name),
493 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
494
495 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
496
497 rc->input_name = ir->name;
498 rc->input_phys = ir->phys;
499 usb_to_input_id(ir->udev, &rc->input_id);
500 rc->dev.parent = &intf->dev;
501 rc->driver_type = RC_DRIVER_IR_RAW;
502 rc->allowed_protos = RC_BIT_ALL;
503 rc->priv = ir;
504 rc->open = iguanair_open;
505 rc->close = iguanair_close;
506 rc->s_tx_mask = iguanair_set_tx_mask;
507 rc->s_tx_carrier = iguanair_set_tx_carrier;
508 rc->tx_ir = iguanair_tx;
509 rc->driver_name = DRIVER_NAME;
510 rc->map_name = RC_MAP_RC6_MCE;
511 rc->timeout = MS_TO_NS(100);
512 rc->rx_resolution = RX_RESOLUTION;
513
514 iguanair_set_tx_carrier(rc, 38000);
515
516 ret = rc_register_device(rc);
517 if (ret < 0) {
518 dev_err(&intf->dev, "failed to register rc device %d", ret);
519 goto out2;
520 }
521
522 usb_set_intfdata(intf, ir);
523
524 return 0;
525 out2:
526 usb_kill_urb(ir->urb_in);
527 usb_kill_urb(ir->urb_out);
528 out:
529 if (ir) {
530 usb_free_urb(ir->urb_in);
531 usb_free_urb(ir->urb_out);
532 usb_free_coherent(udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
533 usb_free_coherent(udev, MAX_OUT_PACKET, ir->packet,
534 ir->dma_out);
535 }
536 rc_free_device(rc);
537 kfree(ir);
538 return ret;
539 }
540
541 static void __devexit iguanair_disconnect(struct usb_interface *intf)
542 {
543 struct iguanair *ir = usb_get_intfdata(intf);
544
545 rc_unregister_device(ir->rc);
546 usb_set_intfdata(intf, NULL);
547 usb_kill_urb(ir->urb_in);
548 usb_kill_urb(ir->urb_out);
549 usb_free_urb(ir->urb_in);
550 usb_free_urb(ir->urb_out);
551 usb_free_coherent(ir->udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
552 usb_free_coherent(ir->udev, MAX_OUT_PACKET, ir->packet, ir->dma_out);
553 kfree(ir);
554 }
555
556 static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
557 {
558 struct iguanair *ir = usb_get_intfdata(intf);
559 int rc = 0;
560
561 mutex_lock(&ir->lock);
562
563 if (ir->receiver_on) {
564 rc = iguanair_receiver(ir, false);
565 if (rc)
566 dev_warn(ir->dev, "failed to disable receiver for suspend\n");
567 }
568
569 usb_kill_urb(ir->urb_in);
570 usb_kill_urb(ir->urb_out);
571
572 mutex_unlock(&ir->lock);
573
574 return rc;
575 }
576
577 static int iguanair_resume(struct usb_interface *intf)
578 {
579 struct iguanair *ir = usb_get_intfdata(intf);
580 int rc = 0;
581
582 mutex_lock(&ir->lock);
583
584 rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
585 if (rc)
586 dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
587
588 if (ir->receiver_on) {
589 rc = iguanair_receiver(ir, true);
590 if (rc)
591 dev_warn(ir->dev, "failed to enable receiver after resume\n");
592 }
593
594 mutex_unlock(&ir->lock);
595
596 return rc;
597 }
598
599 static const struct usb_device_id iguanair_table[] = {
600 { USB_DEVICE(0x1781, 0x0938) },
601 { }
602 };
603
604 static struct usb_driver iguanair_driver = {
605 .name = DRIVER_NAME,
606 .probe = iguanair_probe,
607 .disconnect = __devexit_p(iguanair_disconnect),
608 .suspend = iguanair_suspend,
609 .resume = iguanair_resume,
610 .reset_resume = iguanair_resume,
611 .id_table = iguanair_table,
612 .soft_unbind = 1 /* we want to disable receiver on unbind */
613 };
614
615 module_usb_driver(iguanair_driver);
616
617 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
618 MODULE_AUTHOR("Sean Young <sean@mess.org>");
619 MODULE_LICENSE("GPL");
620 MODULE_DEVICE_TABLE(usb, iguanair_table);
621
This page took 0.047283 seconds and 5 git commands to generate.