blob: 9554585df8b88a56d6b5b609b997e92707107923 [file] [log] [blame]
Sean Young26ff6312012-07-15 13:31:00 -03001/*
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.
Sean Young26ff6312012-07-15 13:31:00 -030015 */
16
17#include <linux/device.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/usb.h>
21#include <linux/usb/input.h>
22#include <linux/slab.h>
23#include <linux/completion.h>
24#include <media/rc-core.h>
25
26#define DRIVER_NAME "iguanair"
Sean Young48b0fa62012-09-28 04:44:29 -030027#define BUF_SIZE 152
Sean Young26ff6312012-07-15 13:31:00 -030028
29struct iguanair {
30 struct rc_dev *rc;
31
32 struct device *dev;
33 struct usb_device *udev;
34
Sean Young0797b482012-08-13 08:59:40 -030035 uint16_t version;
Sean Young26ff6312012-07-15 13:31:00 -030036 uint8_t bufsize;
Sean Young48b0fa62012-09-28 04:44:29 -030037 uint8_t cycle_overhead;
Sean Young26ff6312012-07-15 13:31:00 -030038
39 struct mutex lock;
40
41 /* receiver support */
42 bool receiver_on;
Sean Young48b0fa62012-09-28 04:44:29 -030043 dma_addr_t dma_in, dma_out;
Sean Young26ff6312012-07-15 13:31:00 -030044 uint8_t *buf_in;
Sean Young48b0fa62012-09-28 04:44:29 -030045 struct urb *urb_in, *urb_out;
Sean Young26ff6312012-07-15 13:31:00 -030046 struct completion completion;
47
48 /* transmit support */
49 bool tx_overflow;
50 uint32_t carrier;
Sean Young48b0fa62012-09-28 04:44:29 -030051 struct send_packet *packet;
Sean Young26ff6312012-07-15 13:31:00 -030052
53 char name[64];
54 char phys[64];
55};
56
Sean Youngc6a3ea52013-01-14 05:51:44 -030057#define CMD_NOP 0x00
Sean Young26ff6312012-07-15 13:31:00 -030058#define CMD_GET_VERSION 0x01
59#define CMD_GET_BUFSIZE 0x11
60#define CMD_GET_FEATURES 0x10
61#define CMD_SEND 0x15
62#define CMD_EXECUTE 0x1f
63#define CMD_RX_OVERFLOW 0x31
64#define CMD_TX_OVERFLOW 0x32
65#define CMD_RECEIVER_ON 0x12
66#define CMD_RECEIVER_OFF 0x14
67
68#define DIR_IN 0xdc
69#define DIR_OUT 0xcd
70
Sean Young48b0fa62012-09-28 04:44:29 -030071#define MAX_IN_PACKET 8u
72#define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
Sean Young26ff6312012-07-15 13:31:00 -030073#define TIMEOUT 1000
Sean Young2eec6762012-08-13 08:59:41 -030074#define RX_RESOLUTION 21333
Sean Young26ff6312012-07-15 13:31:00 -030075
76struct packet {
77 uint16_t start;
78 uint8_t direction;
79 uint8_t cmd;
80};
81
Sean Young26ff6312012-07-15 13:31:00 -030082struct send_packet {
83 struct packet header;
84 uint8_t length;
85 uint8_t channels;
86 uint8_t busy7;
87 uint8_t busy4;
88 uint8_t payload[0];
89};
90
91static void process_ir_data(struct iguanair *ir, unsigned len)
92{
93 if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
94 switch (ir->buf_in[3]) {
Sean Younge99a7cf2012-08-13 08:59:39 -030095 case CMD_GET_VERSION:
96 if (len == 6) {
Sean Young0797b482012-08-13 08:59:40 -030097 ir->version = (ir->buf_in[5] << 8) |
98 ir->buf_in[4];
Sean Younge99a7cf2012-08-13 08:59:39 -030099 complete(&ir->completion);
100 }
101 break;
102 case CMD_GET_BUFSIZE:
103 if (len >= 5) {
104 ir->bufsize = ir->buf_in[4];
105 complete(&ir->completion);
106 }
107 break;
108 case CMD_GET_FEATURES:
109 if (len > 5) {
Sean Young0797b482012-08-13 08:59:40 -0300110 ir->cycle_overhead = ir->buf_in[5];
Sean Younge99a7cf2012-08-13 08:59:39 -0300111 complete(&ir->completion);
112 }
113 break;
Sean Young26ff6312012-07-15 13:31:00 -0300114 case CMD_TX_OVERFLOW:
115 ir->tx_overflow = true;
116 case CMD_RECEIVER_OFF:
117 case CMD_RECEIVER_ON:
118 case CMD_SEND:
119 complete(&ir->completion);
120 break;
121 case CMD_RX_OVERFLOW:
122 dev_warn(ir->dev, "receive overflow\n");
Sean Young116e4f52012-08-13 08:59:44 -0300123 ir_raw_event_reset(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300124 break;
125 default:
126 dev_warn(ir->dev, "control code %02x received\n",
127 ir->buf_in[3]);
128 break;
129 }
130 } else if (len >= 7) {
131 DEFINE_IR_RAW_EVENT(rawir);
132 unsigned i;
Sean Youngb83bfd12012-08-13 08:59:47 -0300133 bool event = false;
Sean Young26ff6312012-07-15 13:31:00 -0300134
135 init_ir_raw_event(&rawir);
136
137 for (i = 0; i < 7; i++) {
138 if (ir->buf_in[i] == 0x80) {
139 rawir.pulse = false;
140 rawir.duration = US_TO_NS(21845);
141 } else {
142 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
143 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
Sean Young2eec6762012-08-13 08:59:41 -0300144 RX_RESOLUTION;
Sean Young26ff6312012-07-15 13:31:00 -0300145 }
146
Sean Youngb83bfd12012-08-13 08:59:47 -0300147 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
148 event = true;
Sean Young26ff6312012-07-15 13:31:00 -0300149 }
150
Sean Youngb83bfd12012-08-13 08:59:47 -0300151 if (event)
152 ir_raw_event_handle(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300153 }
154}
155
156static void iguanair_rx(struct urb *urb)
157{
158 struct iguanair *ir;
Sean Young7c0bd962012-08-13 08:59:43 -0300159 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300160
161 if (!urb)
162 return;
163
164 ir = urb->context;
165 if (!ir) {
166 usb_unlink_urb(urb);
167 return;
168 }
169
170 switch (urb->status) {
171 case 0:
172 process_ir_data(ir, urb->actual_length);
173 break;
174 case -ECONNRESET:
175 case -ENOENT:
176 case -ESHUTDOWN:
177 usb_unlink_urb(urb);
178 return;
179 case -EPIPE:
180 default:
181 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
182 break;
183 }
184
Sean Young7c0bd962012-08-13 08:59:43 -0300185 rc = usb_submit_urb(urb, GFP_ATOMIC);
186 if (rc && rc != -ENODEV)
187 dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
Sean Young26ff6312012-07-15 13:31:00 -0300188}
189
Sean Young48b0fa62012-09-28 04:44:29 -0300190static void iguanair_irq_out(struct urb *urb)
Sean Young26ff6312012-07-15 13:31:00 -0300191{
Sean Young48b0fa62012-09-28 04:44:29 -0300192 struct iguanair *ir = urb->context;
193
194 if (urb->status)
195 dev_dbg(ir->dev, "Error: out urb status = %d\n", urb->status);
Sean Youngc6a3ea52013-01-14 05:51:44 -0300196
197 /* if we sent an nop packet, do not expect a response */
198 if (urb->status == 0 && ir->packet->header.cmd == CMD_NOP)
199 complete(&ir->completion);
Sean Young48b0fa62012-09-28 04:44:29 -0300200}
201
202static int iguanair_send(struct iguanair *ir, unsigned size)
203{
204 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300205
Wolfram Sang16735d02013-11-14 14:32:02 -0800206 reinit_completion(&ir->completion);
Sean Young26ff6312012-07-15 13:31:00 -0300207
Sean Young48b0fa62012-09-28 04:44:29 -0300208 ir->urb_out->transfer_buffer_length = size;
209 rc = usb_submit_urb(ir->urb_out, GFP_KERNEL);
Sean Younge99a7cf2012-08-13 08:59:39 -0300210 if (rc)
211 return rc;
Sean Young26ff6312012-07-15 13:31:00 -0300212
Sean Younge99a7cf2012-08-13 08:59:39 -0300213 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
214 return -ETIMEDOUT;
Sean Young26ff6312012-07-15 13:31:00 -0300215
216 return rc;
217}
218
219static int iguanair_get_features(struct iguanair *ir)
220{
Sean Younge99a7cf2012-08-13 08:59:39 -0300221 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300222
Sean Youngc6a3ea52013-01-14 05:51:44 -0300223 /*
224 * On cold boot, the iguanair initializes on the first packet
225 * received but does not process that packet. Send an empty
226 * packet.
227 */
Sean Young48b0fa62012-09-28 04:44:29 -0300228 ir->packet->header.start = 0;
229 ir->packet->header.direction = DIR_OUT;
Sean Youngc6a3ea52013-01-14 05:51:44 -0300230 ir->packet->header.cmd = CMD_NOP;
231 iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300232
Sean Youngc6a3ea52013-01-14 05:51:44 -0300233 ir->packet->header.cmd = CMD_GET_VERSION;
Sean Young48b0fa62012-09-28 04:44:29 -0300234 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300235 if (rc) {
236 dev_info(ir->dev, "failed to get version\n");
237 goto out;
238 }
239
Sean Young0797b482012-08-13 08:59:40 -0300240 if (ir->version < 0x205) {
241 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
242 rc = -ENODEV;
243 goto out;
244 }
245
Sean Young26ff6312012-07-15 13:31:00 -0300246 ir->bufsize = 150;
247 ir->cycle_overhead = 65;
248
Sean Young48b0fa62012-09-28 04:44:29 -0300249 ir->packet->header.cmd = CMD_GET_BUFSIZE;
Sean Young26ff6312012-07-15 13:31:00 -0300250
Sean Young48b0fa62012-09-28 04:44:29 -0300251 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300252 if (rc) {
253 dev_info(ir->dev, "failed to get buffer size\n");
254 goto out;
255 }
256
Sean Young48b0fa62012-09-28 04:44:29 -0300257 if (ir->bufsize > BUF_SIZE) {
258 dev_info(ir->dev, "buffer size %u larger than expected\n",
259 ir->bufsize);
260 ir->bufsize = BUF_SIZE;
261 }
Sean Young26ff6312012-07-15 13:31:00 -0300262
Sean Young48b0fa62012-09-28 04:44:29 -0300263 ir->packet->header.cmd = CMD_GET_FEATURES;
264
265 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Youngc6a3ea52013-01-14 05:51:44 -0300266 if (rc)
Sean Young26ff6312012-07-15 13:31:00 -0300267 dev_info(ir->dev, "failed to get features\n");
Sean Young26ff6312012-07-15 13:31:00 -0300268out:
269 return rc;
270}
271
272static int iguanair_receiver(struct iguanair *ir, bool enable)
273{
Sean Young48b0fa62012-09-28 04:44:29 -0300274 ir->packet->header.start = 0;
275 ir->packet->header.direction = DIR_OUT;
276 ir->packet->header.cmd = enable ? CMD_RECEIVER_ON : CMD_RECEIVER_OFF;
Sean Young26ff6312012-07-15 13:31:00 -0300277
Sean Young116e4f52012-08-13 08:59:44 -0300278 if (enable)
279 ir_raw_event_reset(ir->rc);
280
Sean Youngc6a3ea52013-01-14 05:51:44 -0300281 return iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300282}
283
284/*
Sean Youngdd3a5a12014-01-20 19:10:38 -0300285 * The iguanair creates the carrier by busy spinning after each half period.
286 * This is counted in CPU cycles, with the CPU running at 24MHz. It is
Sean Young26ff6312012-07-15 13:31:00 -0300287 * broken down into 7-cycles and 4-cyles delays, with a preference for
Sean Youngdd3a5a12014-01-20 19:10:38 -0300288 * 4-cycle delays, minus the overhead of the loop itself (cycle_overhead).
Sean Young26ff6312012-07-15 13:31:00 -0300289 */
290static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
291{
292 struct iguanair *ir = dev->priv;
293
294 if (carrier < 25000 || carrier > 150000)
295 return -EINVAL;
296
297 mutex_lock(&ir->lock);
298
299 if (carrier != ir->carrier) {
300 uint32_t cycles, fours, sevens;
301
302 ir->carrier = carrier;
303
304 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
305 ir->cycle_overhead;
306
Sean Young9bd766b2013-11-03 19:13:57 -0300307 /*
308 * Calculate minimum number of 7 cycles needed so
309 * we are left with a multiple of 4; so we want to have
310 * (sevens * 7) & 3 == cycles & 3
311 */
312 sevens = (4 - cycles) & 3;
Sean Young26ff6312012-07-15 13:31:00 -0300313 fours = (cycles - sevens * 7) / 4;
314
Sean Youngdd3a5a12014-01-20 19:10:38 -0300315 /*
316 * The firmware interprets these values as a relative offset
317 * for a branch. Immediately following the branches, there
318 * 4 instructions of 7 cycles (2 bytes each) and 110
319 * instructions of 4 cycles (1 byte each). A relative branch
320 * of 0 will execute all of them, branch further for less
321 * cycle burning.
322 */
Sean Young48b0fa62012-09-28 04:44:29 -0300323 ir->packet->busy7 = (4 - sevens) * 2;
324 ir->packet->busy4 = 110 - fours;
Sean Young26ff6312012-07-15 13:31:00 -0300325 }
326
327 mutex_unlock(&ir->lock);
328
Sean Young14d81882016-07-10 13:34:33 -0300329 return 0;
Sean Young26ff6312012-07-15 13:31:00 -0300330}
331
332static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
333{
334 struct iguanair *ir = dev->priv;
335
336 if (mask > 15)
337 return 4;
338
339 mutex_lock(&ir->lock);
Sean Young48b0fa62012-09-28 04:44:29 -0300340 ir->packet->channels = mask << 4;
Sean Young26ff6312012-07-15 13:31:00 -0300341 mutex_unlock(&ir->lock);
342
343 return 0;
344}
345
346static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
347{
348 struct iguanair *ir = dev->priv;
Sean Young39206312012-08-25 07:01:45 -0300349 uint8_t space;
350 unsigned i, size, periods, bytes;
351 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300352
353 mutex_lock(&ir->lock);
354
355 /* convert from us to carrier periods */
Sean Young39206312012-08-25 07:01:45 -0300356 for (i = space = size = 0; i < count; i++) {
357 periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
358 bytes = DIV_ROUND_UP(periods, 127);
Sean Young884bfd02012-08-13 08:59:42 -0300359 if (size + bytes > ir->bufsize) {
Sean Young671ea672013-07-08 17:33:09 -0300360 rc = -EINVAL;
361 goto out;
Sean Young884bfd02012-08-13 08:59:42 -0300362 }
Sean Young776eced2014-01-20 19:10:39 -0300363 while (periods) {
364 unsigned p = min(periods, 127u);
365 ir->packet->payload[size++] = p | space;
366 periods -= p;
Sean Young39206312012-08-25 07:01:45 -0300367 }
Sean Young39206312012-08-25 07:01:45 -0300368 space ^= 0x80;
Sean Young884bfd02012-08-13 08:59:42 -0300369 }
370
Sean Young48b0fa62012-09-28 04:44:29 -0300371 ir->packet->header.start = 0;
372 ir->packet->header.direction = DIR_OUT;
373 ir->packet->header.cmd = CMD_SEND;
374 ir->packet->length = size;
Sean Young26ff6312012-07-15 13:31:00 -0300375
376 ir->tx_overflow = false;
377
Sean Young48b0fa62012-09-28 04:44:29 -0300378 rc = iguanair_send(ir, sizeof(*ir->packet) + size);
Sean Young26ff6312012-07-15 13:31:00 -0300379
Sean Younge99a7cf2012-08-13 08:59:39 -0300380 if (rc == 0 && ir->tx_overflow)
381 rc = -EOVERFLOW;
Sean Young26ff6312012-07-15 13:31:00 -0300382
Sean Young26ff6312012-07-15 13:31:00 -0300383out:
384 mutex_unlock(&ir->lock);
Sean Young26ff6312012-07-15 13:31:00 -0300385
Sean Young884bfd02012-08-13 08:59:42 -0300386 return rc ? rc : count;
Sean Young26ff6312012-07-15 13:31:00 -0300387}
388
389static int iguanair_open(struct rc_dev *rdev)
390{
391 struct iguanair *ir = rdev->priv;
392 int rc;
393
394 mutex_lock(&ir->lock);
395
Sean Young26ff6312012-07-15 13:31:00 -0300396 rc = iguanair_receiver(ir, true);
397 if (rc == 0)
398 ir->receiver_on = true;
399
400 mutex_unlock(&ir->lock);
401
402 return rc;
403}
404
405static void iguanair_close(struct rc_dev *rdev)
406{
407 struct iguanair *ir = rdev->priv;
408 int rc;
409
410 mutex_lock(&ir->lock);
411
412 rc = iguanair_receiver(ir, false);
413 ir->receiver_on = false;
Sean Young7c0bd962012-08-13 08:59:43 -0300414 if (rc && rc != -ENODEV)
Sean Young26ff6312012-07-15 13:31:00 -0300415 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
416
Sean Young26ff6312012-07-15 13:31:00 -0300417 mutex_unlock(&ir->lock);
418}
419
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800420static int iguanair_probe(struct usb_interface *intf,
421 const struct usb_device_id *id)
Sean Young26ff6312012-07-15 13:31:00 -0300422{
423 struct usb_device *udev = interface_to_usbdev(intf);
424 struct iguanair *ir;
425 struct rc_dev *rc;
Sean Young48b0fa62012-09-28 04:44:29 -0300426 int ret, pipein, pipeout;
Sean Young26ff6312012-07-15 13:31:00 -0300427 struct usb_host_interface *idesc;
428
429 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
430 rc = rc_allocate_device();
431 if (!ir || !rc) {
Sean Young884bfd02012-08-13 08:59:42 -0300432 ret = -ENOMEM;
Sean Young26ff6312012-07-15 13:31:00 -0300433 goto out;
434 }
435
Sean Young48b0fa62012-09-28 04:44:29 -0300436 ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL,
Sean Young26ff6312012-07-15 13:31:00 -0300437 &ir->dma_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300438 ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL,
439 &ir->dma_out);
Sean Young26ff6312012-07-15 13:31:00 -0300440 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Sean Young48b0fa62012-09-28 04:44:29 -0300441 ir->urb_out = usb_alloc_urb(0, GFP_KERNEL);
Sean Young26ff6312012-07-15 13:31:00 -0300442
Sean Young48b0fa62012-09-28 04:44:29 -0300443 if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out) {
Sean Young884bfd02012-08-13 08:59:42 -0300444 ret = -ENOMEM;
Sean Young26ff6312012-07-15 13:31:00 -0300445 goto out;
446 }
447
448 idesc = intf->altsetting;
449
450 if (idesc->desc.bNumEndpoints < 2) {
451 ret = -ENODEV;
452 goto out;
453 }
454
455 ir->rc = rc;
456 ir->dev = &intf->dev;
457 ir->udev = udev;
Sean Young26ff6312012-07-15 13:31:00 -0300458 mutex_init(&ir->lock);
Sean Young48b0fa62012-09-28 04:44:29 -0300459
Sean Young26ff6312012-07-15 13:31:00 -0300460 init_completion(&ir->completion);
Sean Young48b0fa62012-09-28 04:44:29 -0300461 pipeout = usb_sndintpipe(udev,
462 idesc->endpoint[1].desc.bEndpointAddress);
463 usb_fill_int_urb(ir->urb_out, udev, pipeout, ir->packet, MAX_OUT_PACKET,
464 iguanair_irq_out, ir, 1);
465 ir->urb_out->transfer_dma = ir->dma_out;
466 ir->urb_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Sean Young26ff6312012-07-15 13:31:00 -0300467
Sean Younge99a7cf2012-08-13 08:59:39 -0300468 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
Sean Young48b0fa62012-09-28 04:44:29 -0300469 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET,
Sean Young64058382012-08-13 08:59:45 -0300470 iguanair_rx, ir, 1);
Sean Young26ff6312012-07-15 13:31:00 -0300471 ir->urb_in->transfer_dma = ir->dma_in;
472 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
473
Sean Younge99a7cf2012-08-13 08:59:39 -0300474 ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
475 if (ret) {
476 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
477 goto out;
478 }
479
480 ret = iguanair_get_features(ir);
481 if (ret)
482 goto out2;
483
Sean Young26ff6312012-07-15 13:31:00 -0300484 snprintf(ir->name, sizeof(ir->name),
Sean Young0797b482012-08-13 08:59:40 -0300485 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
Sean Young26ff6312012-07-15 13:31:00 -0300486
487 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
488
489 rc->input_name = ir->name;
490 rc->input_phys = ir->phys;
491 usb_to_input_id(ir->udev, &rc->input_id);
492 rc->dev.parent = &intf->dev;
493 rc->driver_type = RC_DRIVER_IR_RAW;
David Härdemanc5540fb2014-04-03 20:32:21 -0300494 rc->allowed_protocols = RC_BIT_ALL;
Sean Young26ff6312012-07-15 13:31:00 -0300495 rc->priv = ir;
496 rc->open = iguanair_open;
497 rc->close = iguanair_close;
498 rc->s_tx_mask = iguanair_set_tx_mask;
499 rc->s_tx_carrier = iguanair_set_tx_carrier;
500 rc->tx_ir = iguanair_tx;
501 rc->driver_name = DRIVER_NAME;
Sean Young2eec6762012-08-13 08:59:41 -0300502 rc->map_name = RC_MAP_RC6_MCE;
Sean Young56a60362016-12-02 15:16:12 -0200503 rc->min_timeout = 1;
504 rc->timeout = IR_DEFAULT_TIMEOUT;
505 rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
Sean Young2eec6762012-08-13 08:59:41 -0300506 rc->rx_resolution = RX_RESOLUTION;
Sean Young26ff6312012-07-15 13:31:00 -0300507
508 iguanair_set_tx_carrier(rc, 38000);
Sean Youngd0aab652013-01-06 13:19:44 -0300509 iguanair_set_tx_mask(rc, 0);
Sean Young26ff6312012-07-15 13:31:00 -0300510
511 ret = rc_register_device(rc);
512 if (ret < 0) {
513 dev_err(&intf->dev, "failed to register rc device %d", ret);
Sean Younge99a7cf2012-08-13 08:59:39 -0300514 goto out2;
Sean Young26ff6312012-07-15 13:31:00 -0300515 }
516
517 usb_set_intfdata(intf, ir);
518
Sean Young26ff6312012-07-15 13:31:00 -0300519 return 0;
Sean Younge99a7cf2012-08-13 08:59:39 -0300520out2:
521 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300522 usb_kill_urb(ir->urb_out);
Sean Young26ff6312012-07-15 13:31:00 -0300523out:
524 if (ir) {
525 usb_free_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300526 usb_free_urb(ir->urb_out);
527 usb_free_coherent(udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
528 usb_free_coherent(udev, MAX_OUT_PACKET, ir->packet,
529 ir->dma_out);
Sean Young26ff6312012-07-15 13:31:00 -0300530 }
531 rc_free_device(rc);
532 kfree(ir);
533 return ret;
534}
535
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800536static void iguanair_disconnect(struct usb_interface *intf)
Sean Young26ff6312012-07-15 13:31:00 -0300537{
538 struct iguanair *ir = usb_get_intfdata(intf);
539
Sean Young7c0bd962012-08-13 08:59:43 -0300540 rc_unregister_device(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300541 usb_set_intfdata(intf, NULL);
Sean Young26ff6312012-07-15 13:31:00 -0300542 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300543 usb_kill_urb(ir->urb_out);
Sean Young26ff6312012-07-15 13:31:00 -0300544 usb_free_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300545 usb_free_urb(ir->urb_out);
546 usb_free_coherent(ir->udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
547 usb_free_coherent(ir->udev, MAX_OUT_PACKET, ir->packet, ir->dma_out);
Sean Young26ff6312012-07-15 13:31:00 -0300548 kfree(ir);
549}
550
551static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
552{
553 struct iguanair *ir = usb_get_intfdata(intf);
554 int rc = 0;
555
556 mutex_lock(&ir->lock);
557
558 if (ir->receiver_on) {
559 rc = iguanair_receiver(ir, false);
560 if (rc)
561 dev_warn(ir->dev, "failed to disable receiver for suspend\n");
562 }
563
Sean Young7c0bd962012-08-13 08:59:43 -0300564 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300565 usb_kill_urb(ir->urb_out);
Sean Young7c0bd962012-08-13 08:59:43 -0300566
Sean Young26ff6312012-07-15 13:31:00 -0300567 mutex_unlock(&ir->lock);
568
569 return rc;
570}
571
572static int iguanair_resume(struct usb_interface *intf)
573{
574 struct iguanair *ir = usb_get_intfdata(intf);
575 int rc = 0;
576
577 mutex_lock(&ir->lock);
578
Sean Young7c0bd962012-08-13 08:59:43 -0300579 rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
580 if (rc)
581 dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
582
Sean Young26ff6312012-07-15 13:31:00 -0300583 if (ir->receiver_on) {
584 rc = iguanair_receiver(ir, true);
585 if (rc)
586 dev_warn(ir->dev, "failed to enable receiver after resume\n");
587 }
588
589 mutex_unlock(&ir->lock);
590
591 return rc;
592}
593
594static const struct usb_device_id iguanair_table[] = {
595 { USB_DEVICE(0x1781, 0x0938) },
596 { }
597};
598
599static struct usb_driver iguanair_driver = {
600 .name = DRIVER_NAME,
601 .probe = iguanair_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800602 .disconnect = iguanair_disconnect,
Sean Young26ff6312012-07-15 13:31:00 -0300603 .suspend = iguanair_suspend,
604 .resume = iguanair_resume,
605 .reset_resume = iguanair_resume,
Sean Young7c0bd962012-08-13 08:59:43 -0300606 .id_table = iguanair_table,
607 .soft_unbind = 1 /* we want to disable receiver on unbind */
Sean Young26ff6312012-07-15 13:31:00 -0300608};
609
610module_usb_driver(iguanair_driver);
611
612MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
613MODULE_AUTHOR("Sean Young <sean@mess.org>");
614MODULE_LICENSE("GPL");
615MODULE_DEVICE_TABLE(usb, iguanair_table);
616