Commit | Line | Data |
---|---|---|
26ff6313 SY |
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 | ||
e99a7cfe | 38 | int pipe_out; |
26ff6313 SY |
39 | uint8_t bufsize; |
40 | uint8_t version[2]; | |
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 | ||
26ff6313 SY |
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]) { | |
e99a7cfe SY |
98 | case CMD_GET_VERSION: |
99 | if (len == 6) { | |
100 | ir->version[0] = ir->buf_in[4]; | |
101 | ir->version[1] = ir->buf_in[5]; | |
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 | if (ir->version[0] >= 4) | |
114 | ir->cycle_overhead = ir->buf_in[5]; | |
115 | complete(&ir->completion); | |
116 | } | |
117 | break; | |
26ff6313 SY |
118 | case CMD_TX_OVERFLOW: |
119 | ir->tx_overflow = true; | |
120 | case CMD_RECEIVER_OFF: | |
121 | case CMD_RECEIVER_ON: | |
122 | case CMD_SEND: | |
123 | complete(&ir->completion); | |
124 | break; | |
125 | case CMD_RX_OVERFLOW: | |
126 | dev_warn(ir->dev, "receive overflow\n"); | |
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 | ||
137 | init_ir_raw_event(&rawir); | |
138 | ||
139 | for (i = 0; i < 7; i++) { | |
140 | if (ir->buf_in[i] == 0x80) { | |
141 | rawir.pulse = false; | |
142 | rawir.duration = US_TO_NS(21845); | |
143 | } else { | |
144 | rawir.pulse = (ir->buf_in[i] & 0x80) == 0; | |
145 | rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) * | |
146 | 21330; | |
147 | } | |
148 | ||
149 | ir_raw_event_store_with_filter(ir->rc, &rawir); | |
150 | } | |
151 | ||
152 | ir_raw_event_handle(ir->rc); | |
153 | } | |
154 | } | |
155 | ||
156 | static void iguanair_rx(struct urb *urb) | |
157 | { | |
158 | struct iguanair *ir; | |
159 | ||
160 | if (!urb) | |
161 | return; | |
162 | ||
163 | ir = urb->context; | |
164 | if (!ir) { | |
165 | usb_unlink_urb(urb); | |
166 | return; | |
167 | } | |
168 | ||
169 | switch (urb->status) { | |
170 | case 0: | |
171 | process_ir_data(ir, urb->actual_length); | |
172 | break; | |
173 | case -ECONNRESET: | |
174 | case -ENOENT: | |
175 | case -ESHUTDOWN: | |
176 | usb_unlink_urb(urb); | |
177 | return; | |
178 | case -EPIPE: | |
179 | default: | |
180 | dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status); | |
181 | break; | |
182 | } | |
183 | ||
184 | usb_submit_urb(urb, GFP_ATOMIC); | |
185 | } | |
186 | ||
e99a7cfe | 187 | static int iguanair_send(struct iguanair *ir, void *data, unsigned size) |
26ff6313 | 188 | { |
26ff6313 SY |
189 | int rc, transferred; |
190 | ||
e99a7cfe | 191 | INIT_COMPLETION(ir->completion); |
26ff6313 | 192 | |
e99a7cfe SY |
193 | rc = usb_interrupt_msg(ir->udev, ir->pipe_out, data, size, |
194 | &transferred, TIMEOUT); | |
195 | if (rc) | |
196 | return rc; | |
26ff6313 | 197 | |
e99a7cfe SY |
198 | if (transferred != size) |
199 | return -EIO; | |
26ff6313 | 200 | |
e99a7cfe SY |
201 | if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0) |
202 | return -ETIMEDOUT; | |
26ff6313 SY |
203 | |
204 | return rc; | |
205 | } | |
206 | ||
207 | static int iguanair_get_features(struct iguanair *ir) | |
208 | { | |
209 | struct packet packet; | |
e99a7cfe | 210 | int rc; |
26ff6313 SY |
211 | |
212 | packet.start = 0; | |
213 | packet.direction = DIR_OUT; | |
214 | packet.cmd = CMD_GET_VERSION; | |
215 | ||
e99a7cfe | 216 | rc = iguanair_send(ir, &packet, sizeof(packet)); |
26ff6313 SY |
217 | if (rc) { |
218 | dev_info(ir->dev, "failed to get version\n"); | |
219 | goto out; | |
220 | } | |
221 | ||
26ff6313 SY |
222 | ir->bufsize = 150; |
223 | ir->cycle_overhead = 65; | |
224 | ||
225 | packet.cmd = CMD_GET_BUFSIZE; | |
226 | ||
e99a7cfe | 227 | rc = iguanair_send(ir, &packet, sizeof(packet)); |
26ff6313 SY |
228 | if (rc) { |
229 | dev_info(ir->dev, "failed to get buffer size\n"); | |
230 | goto out; | |
231 | } | |
232 | ||
26ff6313 SY |
233 | if (ir->version[0] == 0 || ir->version[1] == 0) |
234 | goto out; | |
235 | ||
236 | packet.cmd = CMD_GET_FEATURES; | |
237 | ||
e99a7cfe | 238 | rc = iguanair_send(ir, &packet, sizeof(packet)); |
26ff6313 SY |
239 | if (rc) { |
240 | dev_info(ir->dev, "failed to get features\n"); | |
241 | goto out; | |
242 | } | |
243 | ||
26ff6313 SY |
244 | out: |
245 | return rc; | |
246 | } | |
247 | ||
248 | static int iguanair_receiver(struct iguanair *ir, bool enable) | |
249 | { | |
250 | struct packet packet = { 0, DIR_OUT, enable ? | |
251 | CMD_RECEIVER_ON : CMD_RECEIVER_OFF }; | |
26ff6313 | 252 | |
e99a7cfe | 253 | return iguanair_send(ir, &packet, sizeof(packet)); |
26ff6313 SY |
254 | } |
255 | ||
256 | /* | |
257 | * The iguana ir creates the carrier by busy spinning after each pulse or | |
258 | * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is | |
259 | * broken down into 7-cycles and 4-cyles delays, with a preference for | |
260 | * 4-cycle delays. | |
261 | */ | |
262 | static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier) | |
263 | { | |
264 | struct iguanair *ir = dev->priv; | |
265 | ||
266 | if (carrier < 25000 || carrier > 150000) | |
267 | return -EINVAL; | |
268 | ||
269 | mutex_lock(&ir->lock); | |
270 | ||
271 | if (carrier != ir->carrier) { | |
272 | uint32_t cycles, fours, sevens; | |
273 | ||
274 | ir->carrier = carrier; | |
275 | ||
276 | cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) - | |
277 | ir->cycle_overhead; | |
278 | ||
279 | /* make up the the remainer of 4-cycle blocks */ | |
280 | switch (cycles & 3) { | |
281 | case 0: | |
282 | sevens = 0; | |
283 | break; | |
284 | case 1: | |
285 | sevens = 3; | |
286 | break; | |
287 | case 2: | |
288 | sevens = 2; | |
289 | break; | |
290 | case 3: | |
291 | sevens = 1; | |
292 | break; | |
293 | } | |
294 | ||
295 | fours = (cycles - sevens * 7) / 4; | |
296 | ||
297 | /* magic happens here */ | |
298 | ir->busy7 = (4 - sevens) * 2; | |
299 | ir->busy4 = 110 - fours; | |
300 | } | |
301 | ||
302 | mutex_unlock(&ir->lock); | |
303 | ||
304 | return carrier; | |
305 | } | |
306 | ||
307 | static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask) | |
308 | { | |
309 | struct iguanair *ir = dev->priv; | |
310 | ||
311 | if (mask > 15) | |
312 | return 4; | |
313 | ||
314 | mutex_lock(&ir->lock); | |
315 | ir->channels = mask; | |
316 | mutex_unlock(&ir->lock); | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count) | |
322 | { | |
323 | struct iguanair *ir = dev->priv; | |
324 | uint8_t space, *payload; | |
325 | unsigned i, size, rc; | |
326 | struct send_packet *packet; | |
327 | ||
328 | mutex_lock(&ir->lock); | |
329 | ||
330 | /* convert from us to carrier periods */ | |
331 | for (i = size = 0; i < count; i++) { | |
332 | txbuf[i] = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000); | |
333 | size += (txbuf[i] + 126) / 127; | |
334 | } | |
335 | ||
336 | packet = kmalloc(sizeof(*packet) + size, GFP_KERNEL); | |
337 | if (!packet) { | |
338 | rc = -ENOMEM; | |
339 | goto out; | |
340 | } | |
341 | ||
342 | if (size > ir->bufsize) { | |
343 | rc = -E2BIG; | |
344 | goto out; | |
345 | } | |
346 | ||
347 | packet->header.start = 0; | |
348 | packet->header.direction = DIR_OUT; | |
349 | packet->header.cmd = CMD_SEND; | |
350 | packet->length = size; | |
351 | packet->channels = ir->channels << 4; | |
352 | packet->busy7 = ir->busy7; | |
353 | packet->busy4 = ir->busy4; | |
354 | ||
355 | space = 0; | |
356 | payload = packet->payload; | |
357 | ||
358 | for (i = 0; i < count; i++) { | |
359 | unsigned periods = txbuf[i]; | |
360 | ||
361 | while (periods > 127) { | |
362 | *payload++ = 127 | space; | |
363 | periods -= 127; | |
364 | } | |
365 | ||
366 | *payload++ = periods | space; | |
367 | space ^= 0x80; | |
368 | } | |
369 | ||
370 | if (ir->receiver_on) { | |
371 | rc = iguanair_receiver(ir, false); | |
372 | if (rc) { | |
373 | dev_warn(ir->dev, "disable receiver before transmit failed\n"); | |
374 | goto out; | |
375 | } | |
376 | } | |
377 | ||
378 | ir->tx_overflow = false; | |
379 | ||
e99a7cfe | 380 | rc = iguanair_send(ir, packet, size + 8); |
26ff6313 | 381 | |
e99a7cfe SY |
382 | if (rc == 0 && ir->tx_overflow) |
383 | rc = -EOVERFLOW; | |
26ff6313 SY |
384 | |
385 | if (ir->receiver_on) { | |
386 | if (iguanair_receiver(ir, true)) | |
387 | dev_warn(ir->dev, "re-enable receiver after transmit failed\n"); | |
388 | } | |
389 | ||
390 | out: | |
391 | mutex_unlock(&ir->lock); | |
392 | kfree(packet); | |
393 | ||
394 | return rc; | |
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 | ||
26ff6313 SY |
404 | BUG_ON(ir->receiver_on); |
405 | ||
406 | rc = iguanair_receiver(ir, true); | |
407 | if (rc == 0) | |
408 | ir->receiver_on = true; | |
409 | ||
410 | mutex_unlock(&ir->lock); | |
411 | ||
412 | return rc; | |
413 | } | |
414 | ||
415 | static void iguanair_close(struct rc_dev *rdev) | |
416 | { | |
417 | struct iguanair *ir = rdev->priv; | |
418 | int rc; | |
419 | ||
420 | mutex_lock(&ir->lock); | |
421 | ||
422 | rc = iguanair_receiver(ir, false); | |
423 | ir->receiver_on = false; | |
424 | if (rc) | |
425 | dev_warn(ir->dev, "failed to disable receiver: %d\n", rc); | |
426 | ||
26ff6313 SY |
427 | mutex_unlock(&ir->lock); |
428 | } | |
429 | ||
430 | static int __devinit iguanair_probe(struct usb_interface *intf, | |
431 | const struct usb_device_id *id) | |
432 | { | |
433 | struct usb_device *udev = interface_to_usbdev(intf); | |
434 | struct iguanair *ir; | |
435 | struct rc_dev *rc; | |
e99a7cfe | 436 | int ret, pipein; |
26ff6313 SY |
437 | struct usb_host_interface *idesc; |
438 | ||
439 | ir = kzalloc(sizeof(*ir), GFP_KERNEL); | |
440 | rc = rc_allocate_device(); | |
441 | if (!ir || !rc) { | |
442 | ret = ENOMEM; | |
443 | goto out; | |
444 | } | |
445 | ||
e99a7cfe | 446 | ir->buf_in = usb_alloc_coherent(udev, MAX_PACKET_SIZE, GFP_KERNEL, |
26ff6313 SY |
447 | &ir->dma_in); |
448 | ir->urb_in = usb_alloc_urb(0, GFP_KERNEL); | |
449 | ||
450 | if (!ir->buf_in || !ir->urb_in) { | |
451 | ret = ENOMEM; | |
452 | goto out; | |
453 | } | |
454 | ||
455 | idesc = intf->altsetting; | |
456 | ||
457 | if (idesc->desc.bNumEndpoints < 2) { | |
458 | ret = -ENODEV; | |
459 | goto out; | |
460 | } | |
461 | ||
462 | ir->rc = rc; | |
463 | ir->dev = &intf->dev; | |
464 | ir->udev = udev; | |
26ff6313 SY |
465 | ir->pipe_out = usb_sndintpipe(udev, |
466 | idesc->endpoint[1].desc.bEndpointAddress); | |
467 | mutex_init(&ir->lock); | |
468 | init_completion(&ir->completion); | |
469 | ||
e99a7cfe SY |
470 | pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress); |
471 | usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, | |
26ff6313 SY |
472 | MAX_PACKET_SIZE, iguanair_rx, ir, |
473 | idesc->endpoint[0].desc.bInterval); | |
474 | ir->urb_in->transfer_dma = ir->dma_in; | |
475 | ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
476 | ||
e99a7cfe SY |
477 | ret = usb_submit_urb(ir->urb_in, GFP_KERNEL); |
478 | if (ret) { | |
479 | dev_warn(&intf->dev, "failed to submit urb: %d\n", ret); | |
480 | goto out; | |
481 | } | |
482 | ||
483 | ret = iguanair_get_features(ir); | |
484 | if (ret) | |
485 | goto out2; | |
486 | ||
26ff6313 SY |
487 | snprintf(ir->name, sizeof(ir->name), |
488 | "IguanaWorks USB IR Transceiver version %d.%d", | |
489 | ir->version[0], ir->version[1]); | |
490 | ||
491 | usb_make_path(ir->udev, ir->phys, sizeof(ir->phys)); | |
492 | ||
493 | rc->input_name = ir->name; | |
494 | rc->input_phys = ir->phys; | |
495 | usb_to_input_id(ir->udev, &rc->input_id); | |
496 | rc->dev.parent = &intf->dev; | |
497 | rc->driver_type = RC_DRIVER_IR_RAW; | |
498 | rc->allowed_protos = RC_TYPE_ALL; | |
499 | rc->priv = ir; | |
500 | rc->open = iguanair_open; | |
501 | rc->close = iguanair_close; | |
502 | rc->s_tx_mask = iguanair_set_tx_mask; | |
503 | rc->s_tx_carrier = iguanair_set_tx_carrier; | |
504 | rc->tx_ir = iguanair_tx; | |
505 | rc->driver_name = DRIVER_NAME; | |
506 | rc->map_name = RC_MAP_EMPTY; | |
507 | ||
508 | iguanair_set_tx_carrier(rc, 38000); | |
509 | ||
510 | ret = rc_register_device(rc); | |
511 | if (ret < 0) { | |
512 | dev_err(&intf->dev, "failed to register rc device %d", ret); | |
e99a7cfe | 513 | goto out2; |
26ff6313 SY |
514 | } |
515 | ||
516 | usb_set_intfdata(intf, ir); | |
517 | ||
518 | dev_info(&intf->dev, "Registered %s", ir->name); | |
519 | ||
520 | return 0; | |
e99a7cfe SY |
521 | out2: |
522 | usb_kill_urb(ir->urb_in); | |
26ff6313 SY |
523 | out: |
524 | if (ir) { | |
525 | usb_free_urb(ir->urb_in); | |
526 | usb_free_coherent(udev, MAX_PACKET_SIZE, ir->buf_in, | |
527 | ir->dma_in); | |
528 | } | |
529 | rc_free_device(rc); | |
530 | kfree(ir); | |
531 | return ret; | |
532 | } | |
533 | ||
534 | static void __devexit iguanair_disconnect(struct usb_interface *intf) | |
535 | { | |
536 | struct iguanair *ir = usb_get_intfdata(intf); | |
537 | ||
538 | usb_set_intfdata(intf, NULL); | |
539 | ||
540 | usb_kill_urb(ir->urb_in); | |
541 | usb_free_urb(ir->urb_in); | |
542 | usb_free_coherent(ir->udev, MAX_PACKET_SIZE, ir->buf_in, ir->dma_in); | |
543 | rc_unregister_device(ir->rc); | |
544 | kfree(ir); | |
545 | } | |
546 | ||
547 | static int iguanair_suspend(struct usb_interface *intf, pm_message_t message) | |
548 | { | |
549 | struct iguanair *ir = usb_get_intfdata(intf); | |
550 | int rc = 0; | |
551 | ||
552 | mutex_lock(&ir->lock); | |
553 | ||
554 | if (ir->receiver_on) { | |
555 | rc = iguanair_receiver(ir, false); | |
556 | if (rc) | |
557 | dev_warn(ir->dev, "failed to disable receiver for suspend\n"); | |
558 | } | |
559 | ||
560 | mutex_unlock(&ir->lock); | |
561 | ||
562 | return rc; | |
563 | } | |
564 | ||
565 | static int iguanair_resume(struct usb_interface *intf) | |
566 | { | |
567 | struct iguanair *ir = usb_get_intfdata(intf); | |
568 | int rc = 0; | |
569 | ||
570 | mutex_lock(&ir->lock); | |
571 | ||
572 | if (ir->receiver_on) { | |
573 | rc = iguanair_receiver(ir, true); | |
574 | if (rc) | |
575 | dev_warn(ir->dev, "failed to enable receiver after resume\n"); | |
576 | } | |
577 | ||
578 | mutex_unlock(&ir->lock); | |
579 | ||
580 | return rc; | |
581 | } | |
582 | ||
583 | static const struct usb_device_id iguanair_table[] = { | |
584 | { USB_DEVICE(0x1781, 0x0938) }, | |
585 | { } | |
586 | }; | |
587 | ||
588 | static struct usb_driver iguanair_driver = { | |
589 | .name = DRIVER_NAME, | |
590 | .probe = iguanair_probe, | |
591 | .disconnect = __devexit_p(iguanair_disconnect), | |
592 | .suspend = iguanair_suspend, | |
593 | .resume = iguanair_resume, | |
594 | .reset_resume = iguanair_resume, | |
595 | .id_table = iguanair_table | |
596 | }; | |
597 | ||
598 | module_usb_driver(iguanair_driver); | |
599 | ||
600 | MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver"); | |
601 | MODULE_AUTHOR("Sean Young <sean@mess.org>"); | |
602 | MODULE_LICENSE("GPL"); | |
603 | MODULE_DEVICE_TABLE(usb, iguanair_table); | |
604 |