blob: 99a3a5a509e6a00839c22490c7349643f238e31d [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.
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"
Sean Young48b0fa62012-09-28 04:44:29 -030031#define BUF_SIZE 152
Sean Young26ff6312012-07-15 13:31:00 -030032
33struct iguanair {
34 struct rc_dev *rc;
35
36 struct device *dev;
37 struct usb_device *udev;
38
Sean Young0797b482012-08-13 08:59:40 -030039 uint16_t version;
Sean Young26ff6312012-07-15 13:31:00 -030040 uint8_t bufsize;
Sean Young48b0fa62012-09-28 04:44:29 -030041 uint8_t cycle_overhead;
Sean Young26ff6312012-07-15 13:31:00 -030042
43 struct mutex lock;
44
45 /* receiver support */
46 bool receiver_on;
Sean Young48b0fa62012-09-28 04:44:29 -030047 dma_addr_t dma_in, dma_out;
Sean Young26ff6312012-07-15 13:31:00 -030048 uint8_t *buf_in;
Sean Young48b0fa62012-09-28 04:44:29 -030049 struct urb *urb_in, *urb_out;
Sean Young26ff6312012-07-15 13:31:00 -030050 struct completion completion;
51
52 /* transmit support */
53 bool tx_overflow;
54 uint32_t carrier;
Sean Young48b0fa62012-09-28 04:44:29 -030055 struct send_packet *packet;
Sean Young26ff6312012-07-15 13:31:00 -030056
57 char name[64];
58 char phys[64];
59};
60
Sean Youngc6a3ea52013-01-14 05:51:44 -030061#define CMD_NOP 0x00
Sean Young26ff6312012-07-15 13:31:00 -030062#define CMD_GET_VERSION 0x01
63#define CMD_GET_BUFSIZE 0x11
64#define CMD_GET_FEATURES 0x10
65#define CMD_SEND 0x15
66#define CMD_EXECUTE 0x1f
67#define CMD_RX_OVERFLOW 0x31
68#define CMD_TX_OVERFLOW 0x32
69#define CMD_RECEIVER_ON 0x12
70#define CMD_RECEIVER_OFF 0x14
71
72#define DIR_IN 0xdc
73#define DIR_OUT 0xcd
74
Sean Young48b0fa62012-09-28 04:44:29 -030075#define MAX_IN_PACKET 8u
76#define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
Sean Young26ff6312012-07-15 13:31:00 -030077#define TIMEOUT 1000
Sean Young2eec6762012-08-13 08:59:41 -030078#define RX_RESOLUTION 21333
Sean Young26ff6312012-07-15 13:31:00 -030079
80struct packet {
81 uint16_t start;
82 uint8_t direction;
83 uint8_t cmd;
84};
85
Sean Young26ff6312012-07-15 13:31:00 -030086struct send_packet {
87 struct packet header;
88 uint8_t length;
89 uint8_t channels;
90 uint8_t busy7;
91 uint8_t busy4;
92 uint8_t payload[0];
93};
94
95static void process_ir_data(struct iguanair *ir, unsigned len)
96{
97 if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
98 switch (ir->buf_in[3]) {
Sean Younge99a7cf2012-08-13 08:59:39 -030099 case CMD_GET_VERSION:
100 if (len == 6) {
Sean Young0797b482012-08-13 08:59:40 -0300101 ir->version = (ir->buf_in[5] << 8) |
102 ir->buf_in[4];
Sean Younge99a7cf2012-08-13 08:59:39 -0300103 complete(&ir->completion);
104 }
105 break;
106 case CMD_GET_BUFSIZE:
107 if (len >= 5) {
108 ir->bufsize = ir->buf_in[4];
109 complete(&ir->completion);
110 }
111 break;
112 case CMD_GET_FEATURES:
113 if (len > 5) {
Sean Young0797b482012-08-13 08:59:40 -0300114 ir->cycle_overhead = ir->buf_in[5];
Sean Younge99a7cf2012-08-13 08:59:39 -0300115 complete(&ir->completion);
116 }
117 break;
Sean Young26ff6312012-07-15 13:31:00 -0300118 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");
Sean Young116e4f52012-08-13 08:59:44 -0300127 ir_raw_event_reset(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300128 break;
129 default:
130 dev_warn(ir->dev, "control code %02x received\n",
131 ir->buf_in[3]);
132 break;
133 }
134 } else if (len >= 7) {
135 DEFINE_IR_RAW_EVENT(rawir);
136 unsigned i;
Sean Youngb83bfd12012-08-13 08:59:47 -0300137 bool event = false;
Sean Young26ff6312012-07-15 13:31:00 -0300138
139 init_ir_raw_event(&rawir);
140
141 for (i = 0; i < 7; i++) {
142 if (ir->buf_in[i] == 0x80) {
143 rawir.pulse = false;
144 rawir.duration = US_TO_NS(21845);
145 } else {
146 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
147 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
Sean Young2eec6762012-08-13 08:59:41 -0300148 RX_RESOLUTION;
Sean Young26ff6312012-07-15 13:31:00 -0300149 }
150
Sean Youngb83bfd12012-08-13 08:59:47 -0300151 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
152 event = true;
Sean Young26ff6312012-07-15 13:31:00 -0300153 }
154
Sean Youngb83bfd12012-08-13 08:59:47 -0300155 if (event)
156 ir_raw_event_handle(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300157 }
158}
159
160static void iguanair_rx(struct urb *urb)
161{
162 struct iguanair *ir;
Sean Young7c0bd962012-08-13 08:59:43 -0300163 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300164
165 if (!urb)
166 return;
167
168 ir = urb->context;
169 if (!ir) {
170 usb_unlink_urb(urb);
171 return;
172 }
173
174 switch (urb->status) {
175 case 0:
176 process_ir_data(ir, urb->actual_length);
177 break;
178 case -ECONNRESET:
179 case -ENOENT:
180 case -ESHUTDOWN:
181 usb_unlink_urb(urb);
182 return;
183 case -EPIPE:
184 default:
185 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
186 break;
187 }
188
Sean Young7c0bd962012-08-13 08:59:43 -0300189 rc = usb_submit_urb(urb, GFP_ATOMIC);
190 if (rc && rc != -ENODEV)
191 dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
Sean Young26ff6312012-07-15 13:31:00 -0300192}
193
Sean Young48b0fa62012-09-28 04:44:29 -0300194static void iguanair_irq_out(struct urb *urb)
Sean Young26ff6312012-07-15 13:31:00 -0300195{
Sean Young48b0fa62012-09-28 04:44:29 -0300196 struct iguanair *ir = urb->context;
197
198 if (urb->status)
199 dev_dbg(ir->dev, "Error: out urb status = %d\n", urb->status);
Sean Youngc6a3ea52013-01-14 05:51:44 -0300200
201 /* if we sent an nop packet, do not expect a response */
202 if (urb->status == 0 && ir->packet->header.cmd == CMD_NOP)
203 complete(&ir->completion);
Sean Young48b0fa62012-09-28 04:44:29 -0300204}
205
206static int iguanair_send(struct iguanair *ir, unsigned size)
207{
208 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300209
Wolfram Sang16735d02013-11-14 14:32:02 -0800210 reinit_completion(&ir->completion);
Sean Young26ff6312012-07-15 13:31:00 -0300211
Sean Young48b0fa62012-09-28 04:44:29 -0300212 ir->urb_out->transfer_buffer_length = size;
213 rc = usb_submit_urb(ir->urb_out, GFP_KERNEL);
Sean Younge99a7cf2012-08-13 08:59:39 -0300214 if (rc)
215 return rc;
Sean Young26ff6312012-07-15 13:31:00 -0300216
Sean Younge99a7cf2012-08-13 08:59:39 -0300217 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
218 return -ETIMEDOUT;
Sean Young26ff6312012-07-15 13:31:00 -0300219
220 return rc;
221}
222
223static int iguanair_get_features(struct iguanair *ir)
224{
Sean Younge99a7cf2012-08-13 08:59:39 -0300225 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300226
Sean Youngc6a3ea52013-01-14 05:51:44 -0300227 /*
228 * On cold boot, the iguanair initializes on the first packet
229 * received but does not process that packet. Send an empty
230 * packet.
231 */
Sean Young48b0fa62012-09-28 04:44:29 -0300232 ir->packet->header.start = 0;
233 ir->packet->header.direction = DIR_OUT;
Sean Youngc6a3ea52013-01-14 05:51:44 -0300234 ir->packet->header.cmd = CMD_NOP;
235 iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300236
Sean Youngc6a3ea52013-01-14 05:51:44 -0300237 ir->packet->header.cmd = CMD_GET_VERSION;
Sean Young48b0fa62012-09-28 04:44:29 -0300238 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300239 if (rc) {
240 dev_info(ir->dev, "failed to get version\n");
241 goto out;
242 }
243
Sean Young0797b482012-08-13 08:59:40 -0300244 if (ir->version < 0x205) {
245 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
246 rc = -ENODEV;
247 goto out;
248 }
249
Sean Young26ff6312012-07-15 13:31:00 -0300250 ir->bufsize = 150;
251 ir->cycle_overhead = 65;
252
Sean Young48b0fa62012-09-28 04:44:29 -0300253 ir->packet->header.cmd = CMD_GET_BUFSIZE;
Sean Young26ff6312012-07-15 13:31:00 -0300254
Sean Young48b0fa62012-09-28 04:44:29 -0300255 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300256 if (rc) {
257 dev_info(ir->dev, "failed to get buffer size\n");
258 goto out;
259 }
260
Sean Young48b0fa62012-09-28 04:44:29 -0300261 if (ir->bufsize > BUF_SIZE) {
262 dev_info(ir->dev, "buffer size %u larger than expected\n",
263 ir->bufsize);
264 ir->bufsize = BUF_SIZE;
265 }
Sean Young26ff6312012-07-15 13:31:00 -0300266
Sean Young48b0fa62012-09-28 04:44:29 -0300267 ir->packet->header.cmd = CMD_GET_FEATURES;
268
269 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Youngc6a3ea52013-01-14 05:51:44 -0300270 if (rc)
Sean Young26ff6312012-07-15 13:31:00 -0300271 dev_info(ir->dev, "failed to get features\n");
Sean Young26ff6312012-07-15 13:31:00 -0300272out:
273 return rc;
274}
275
276static int iguanair_receiver(struct iguanair *ir, bool enable)
277{
Sean Young48b0fa62012-09-28 04:44:29 -0300278 ir->packet->header.start = 0;
279 ir->packet->header.direction = DIR_OUT;
280 ir->packet->header.cmd = enable ? CMD_RECEIVER_ON : CMD_RECEIVER_OFF;
Sean Young26ff6312012-07-15 13:31:00 -0300281
Sean Young116e4f52012-08-13 08:59:44 -0300282 if (enable)
283 ir_raw_event_reset(ir->rc);
284
Sean Youngc6a3ea52013-01-14 05:51:44 -0300285 return iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300286}
287
288/*
Sean Youngdd3a5a12014-01-20 19:10:38 -0300289 * The iguanair creates the carrier by busy spinning after each half period.
290 * This is counted in CPU cycles, with the CPU running at 24MHz. It is
Sean Young26ff6312012-07-15 13:31:00 -0300291 * broken down into 7-cycles and 4-cyles delays, with a preference for
Sean Youngdd3a5a12014-01-20 19:10:38 -0300292 * 4-cycle delays, minus the overhead of the loop itself (cycle_overhead).
Sean Young26ff6312012-07-15 13:31:00 -0300293 */
294static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
295{
296 struct iguanair *ir = dev->priv;
297
298 if (carrier < 25000 || carrier > 150000)
299 return -EINVAL;
300
301 mutex_lock(&ir->lock);
302
303 if (carrier != ir->carrier) {
304 uint32_t cycles, fours, sevens;
305
306 ir->carrier = carrier;
307
308 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
309 ir->cycle_overhead;
310
Sean Young9bd766b2013-11-03 19:13:57 -0300311 /*
312 * Calculate minimum number of 7 cycles needed so
313 * we are left with a multiple of 4; so we want to have
314 * (sevens * 7) & 3 == cycles & 3
315 */
316 sevens = (4 - cycles) & 3;
Sean Young26ff6312012-07-15 13:31:00 -0300317 fours = (cycles - sevens * 7) / 4;
318
Sean Youngdd3a5a12014-01-20 19:10:38 -0300319 /*
320 * The firmware interprets these values as a relative offset
321 * for a branch. Immediately following the branches, there
322 * 4 instructions of 7 cycles (2 bytes each) and 110
323 * instructions of 4 cycles (1 byte each). A relative branch
324 * of 0 will execute all of them, branch further for less
325 * cycle burning.
326 */
Sean Young48b0fa62012-09-28 04:44:29 -0300327 ir->packet->busy7 = (4 - sevens) * 2;
328 ir->packet->busy4 = 110 - fours;
Sean Young26ff6312012-07-15 13:31:00 -0300329 }
330
331 mutex_unlock(&ir->lock);
332
333 return carrier;
334}
335
336static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
337{
338 struct iguanair *ir = dev->priv;
339
340 if (mask > 15)
341 return 4;
342
343 mutex_lock(&ir->lock);
Sean Young48b0fa62012-09-28 04:44:29 -0300344 ir->packet->channels = mask << 4;
Sean Young26ff6312012-07-15 13:31:00 -0300345 mutex_unlock(&ir->lock);
346
347 return 0;
348}
349
350static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
351{
352 struct iguanair *ir = dev->priv;
Sean Young39206312012-08-25 07:01:45 -0300353 uint8_t space;
354 unsigned i, size, periods, bytes;
355 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300356
357 mutex_lock(&ir->lock);
358
359 /* convert from us to carrier periods */
Sean Young39206312012-08-25 07:01:45 -0300360 for (i = space = size = 0; i < count; i++) {
361 periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
362 bytes = DIV_ROUND_UP(periods, 127);
Sean Young884bfd02012-08-13 08:59:42 -0300363 if (size + bytes > ir->bufsize) {
Sean Young671ea672013-07-08 17:33:09 -0300364 rc = -EINVAL;
365 goto out;
Sean Young884bfd02012-08-13 08:59:42 -0300366 }
Sean Young39206312012-08-25 07:01:45 -0300367 while (periods > 127) {
Sean Young48b0fa62012-09-28 04:44:29 -0300368 ir->packet->payload[size++] = 127 | space;
Sean Young39206312012-08-25 07:01:45 -0300369 periods -= 127;
370 }
371
Sean Young48b0fa62012-09-28 04:44:29 -0300372 ir->packet->payload[size++] = periods | space;
Sean Young39206312012-08-25 07:01:45 -0300373 space ^= 0x80;
Sean Young884bfd02012-08-13 08:59:42 -0300374 }
375
376 if (count == 0) {
377 rc = -EINVAL;
378 goto out;
Sean Young26ff6312012-07-15 13:31:00 -0300379 }
380
Sean Young48b0fa62012-09-28 04:44:29 -0300381 ir->packet->header.start = 0;
382 ir->packet->header.direction = DIR_OUT;
383 ir->packet->header.cmd = CMD_SEND;
384 ir->packet->length = size;
Sean Young26ff6312012-07-15 13:31:00 -0300385
386 ir->tx_overflow = false;
387
Sean Young48b0fa62012-09-28 04:44:29 -0300388 rc = iguanair_send(ir, sizeof(*ir->packet) + size);
Sean Young26ff6312012-07-15 13:31:00 -0300389
Sean Younge99a7cf2012-08-13 08:59:39 -0300390 if (rc == 0 && ir->tx_overflow)
391 rc = -EOVERFLOW;
Sean Young26ff6312012-07-15 13:31:00 -0300392
Sean Young26ff6312012-07-15 13:31:00 -0300393out:
394 mutex_unlock(&ir->lock);
Sean Young26ff6312012-07-15 13:31:00 -0300395
Sean Young884bfd02012-08-13 08:59:42 -0300396 return rc ? rc : count;
Sean Young26ff6312012-07-15 13:31:00 -0300397}
398
399static int iguanair_open(struct rc_dev *rdev)
400{
401 struct iguanair *ir = rdev->priv;
402 int rc;
403
404 mutex_lock(&ir->lock);
405
Sean Young26ff6312012-07-15 13:31:00 -0300406 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
415static 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;
Sean Young7c0bd962012-08-13 08:59:43 -0300424 if (rc && rc != -ENODEV)
Sean Young26ff6312012-07-15 13:31:00 -0300425 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
426
Sean Young26ff6312012-07-15 13:31:00 -0300427 mutex_unlock(&ir->lock);
428}
429
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800430static int iguanair_probe(struct usb_interface *intf,
431 const struct usb_device_id *id)
Sean Young26ff6312012-07-15 13:31:00 -0300432{
433 struct usb_device *udev = interface_to_usbdev(intf);
434 struct iguanair *ir;
435 struct rc_dev *rc;
Sean Young48b0fa62012-09-28 04:44:29 -0300436 int ret, pipein, pipeout;
Sean Young26ff6312012-07-15 13:31:00 -0300437 struct usb_host_interface *idesc;
438
439 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
440 rc = rc_allocate_device();
441 if (!ir || !rc) {
Sean Young884bfd02012-08-13 08:59:42 -0300442 ret = -ENOMEM;
Sean Young26ff6312012-07-15 13:31:00 -0300443 goto out;
444 }
445
Sean Young48b0fa62012-09-28 04:44:29 -0300446 ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL,
Sean Young26ff6312012-07-15 13:31:00 -0300447 &ir->dma_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300448 ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL,
449 &ir->dma_out);
Sean Young26ff6312012-07-15 13:31:00 -0300450 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Sean Young48b0fa62012-09-28 04:44:29 -0300451 ir->urb_out = usb_alloc_urb(0, GFP_KERNEL);
Sean Young26ff6312012-07-15 13:31:00 -0300452
Sean Young48b0fa62012-09-28 04:44:29 -0300453 if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out) {
Sean Young884bfd02012-08-13 08:59:42 -0300454 ret = -ENOMEM;
Sean Young26ff6312012-07-15 13:31:00 -0300455 goto out;
456 }
457
458 idesc = intf->altsetting;
459
460 if (idesc->desc.bNumEndpoints < 2) {
461 ret = -ENODEV;
462 goto out;
463 }
464
465 ir->rc = rc;
466 ir->dev = &intf->dev;
467 ir->udev = udev;
Sean Young26ff6312012-07-15 13:31:00 -0300468 mutex_init(&ir->lock);
Sean Young48b0fa62012-09-28 04:44:29 -0300469
Sean Young26ff6312012-07-15 13:31:00 -0300470 init_completion(&ir->completion);
Sean Young48b0fa62012-09-28 04:44:29 -0300471 pipeout = usb_sndintpipe(udev,
472 idesc->endpoint[1].desc.bEndpointAddress);
473 usb_fill_int_urb(ir->urb_out, udev, pipeout, ir->packet, MAX_OUT_PACKET,
474 iguanair_irq_out, ir, 1);
475 ir->urb_out->transfer_dma = ir->dma_out;
476 ir->urb_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Sean Young26ff6312012-07-15 13:31:00 -0300477
Sean Younge99a7cf2012-08-13 08:59:39 -0300478 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
Sean Young48b0fa62012-09-28 04:44:29 -0300479 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET,
Sean Young64058382012-08-13 08:59:45 -0300480 iguanair_rx, ir, 1);
Sean Young26ff6312012-07-15 13:31:00 -0300481 ir->urb_in->transfer_dma = ir->dma_in;
482 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
483
Sean Younge99a7cf2012-08-13 08:59:39 -0300484 ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
485 if (ret) {
486 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
487 goto out;
488 }
489
490 ret = iguanair_get_features(ir);
491 if (ret)
492 goto out2;
493
Sean Young26ff6312012-07-15 13:31:00 -0300494 snprintf(ir->name, sizeof(ir->name),
Sean Young0797b482012-08-13 08:59:40 -0300495 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
Sean Young26ff6312012-07-15 13:31:00 -0300496
497 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
498
499 rc->input_name = ir->name;
500 rc->input_phys = ir->phys;
501 usb_to_input_id(ir->udev, &rc->input_id);
502 rc->dev.parent = &intf->dev;
503 rc->driver_type = RC_DRIVER_IR_RAW;
David Härdemanc003ab12012-10-11 19:11:54 -0300504 rc->allowed_protos = RC_BIT_ALL;
Sean Young26ff6312012-07-15 13:31:00 -0300505 rc->priv = ir;
506 rc->open = iguanair_open;
507 rc->close = iguanair_close;
508 rc->s_tx_mask = iguanair_set_tx_mask;
509 rc->s_tx_carrier = iguanair_set_tx_carrier;
510 rc->tx_ir = iguanair_tx;
511 rc->driver_name = DRIVER_NAME;
Sean Young2eec6762012-08-13 08:59:41 -0300512 rc->map_name = RC_MAP_RC6_MCE;
513 rc->timeout = MS_TO_NS(100);
514 rc->rx_resolution = RX_RESOLUTION;
Sean Young26ff6312012-07-15 13:31:00 -0300515
516 iguanair_set_tx_carrier(rc, 38000);
Sean Youngd0aab652013-01-06 13:19:44 -0300517 iguanair_set_tx_mask(rc, 0);
Sean Young26ff6312012-07-15 13:31:00 -0300518
519 ret = rc_register_device(rc);
520 if (ret < 0) {
521 dev_err(&intf->dev, "failed to register rc device %d", ret);
Sean Younge99a7cf2012-08-13 08:59:39 -0300522 goto out2;
Sean Young26ff6312012-07-15 13:31:00 -0300523 }
524
525 usb_set_intfdata(intf, ir);
526
Sean Young26ff6312012-07-15 13:31:00 -0300527 return 0;
Sean Younge99a7cf2012-08-13 08:59:39 -0300528out2:
529 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300530 usb_kill_urb(ir->urb_out);
Sean Young26ff6312012-07-15 13:31:00 -0300531out:
532 if (ir) {
533 usb_free_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300534 usb_free_urb(ir->urb_out);
535 usb_free_coherent(udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
536 usb_free_coherent(udev, MAX_OUT_PACKET, ir->packet,
537 ir->dma_out);
Sean Young26ff6312012-07-15 13:31:00 -0300538 }
539 rc_free_device(rc);
540 kfree(ir);
541 return ret;
542}
543
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800544static void iguanair_disconnect(struct usb_interface *intf)
Sean Young26ff6312012-07-15 13:31:00 -0300545{
546 struct iguanair *ir = usb_get_intfdata(intf);
547
Sean Young7c0bd962012-08-13 08:59:43 -0300548 rc_unregister_device(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300549 usb_set_intfdata(intf, NULL);
Sean Young26ff6312012-07-15 13:31:00 -0300550 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300551 usb_kill_urb(ir->urb_out);
Sean Young26ff6312012-07-15 13:31:00 -0300552 usb_free_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300553 usb_free_urb(ir->urb_out);
554 usb_free_coherent(ir->udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
555 usb_free_coherent(ir->udev, MAX_OUT_PACKET, ir->packet, ir->dma_out);
Sean Young26ff6312012-07-15 13:31:00 -0300556 kfree(ir);
557}
558
559static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
560{
561 struct iguanair *ir = usb_get_intfdata(intf);
562 int rc = 0;
563
564 mutex_lock(&ir->lock);
565
566 if (ir->receiver_on) {
567 rc = iguanair_receiver(ir, false);
568 if (rc)
569 dev_warn(ir->dev, "failed to disable receiver for suspend\n");
570 }
571
Sean Young7c0bd962012-08-13 08:59:43 -0300572 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300573 usb_kill_urb(ir->urb_out);
Sean Young7c0bd962012-08-13 08:59:43 -0300574
Sean Young26ff6312012-07-15 13:31:00 -0300575 mutex_unlock(&ir->lock);
576
577 return rc;
578}
579
580static int iguanair_resume(struct usb_interface *intf)
581{
582 struct iguanair *ir = usb_get_intfdata(intf);
583 int rc = 0;
584
585 mutex_lock(&ir->lock);
586
Sean Young7c0bd962012-08-13 08:59:43 -0300587 rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
588 if (rc)
589 dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
590
Sean Young26ff6312012-07-15 13:31:00 -0300591 if (ir->receiver_on) {
592 rc = iguanair_receiver(ir, true);
593 if (rc)
594 dev_warn(ir->dev, "failed to enable receiver after resume\n");
595 }
596
597 mutex_unlock(&ir->lock);
598
599 return rc;
600}
601
602static const struct usb_device_id iguanair_table[] = {
603 { USB_DEVICE(0x1781, 0x0938) },
604 { }
605};
606
607static struct usb_driver iguanair_driver = {
608 .name = DRIVER_NAME,
609 .probe = iguanair_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800610 .disconnect = iguanair_disconnect,
Sean Young26ff6312012-07-15 13:31:00 -0300611 .suspend = iguanair_suspend,
612 .resume = iguanair_resume,
613 .reset_resume = iguanair_resume,
Sean Young7c0bd962012-08-13 08:59:43 -0300614 .id_table = iguanair_table,
615 .soft_unbind = 1 /* we want to disable receiver on unbind */
Sean Young26ff6312012-07-15 13:31:00 -0300616};
617
618module_usb_driver(iguanair_driver);
619
620MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
621MODULE_AUTHOR("Sean Young <sean@mess.org>");
622MODULE_LICENSE("GPL");
623MODULE_DEVICE_TABLE(usb, iguanair_table);
624