blob: b99b096d8a8f11d834b82f36061b47599d687712 [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
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
Sean Young48b0fa62012-09-28 04:44:29 -030074#define MAX_IN_PACKET 8u
75#define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
Sean Young26ff6312012-07-15 13:31:00 -030076#define TIMEOUT 1000
Sean Young2eec6762012-08-13 08:59:41 -030077#define RX_RESOLUTION 21333
Sean Young26ff6312012-07-15 13:31:00 -030078
79struct packet {
80 uint16_t start;
81 uint8_t direction;
82 uint8_t cmd;
83};
84
Sean Young26ff6312012-07-15 13:31:00 -030085struct 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
94static 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]) {
Sean Younge99a7cf2012-08-13 08:59:39 -030098 case CMD_GET_VERSION:
99 if (len == 6) {
Sean Young0797b482012-08-13 08:59:40 -0300100 ir->version = (ir->buf_in[5] << 8) |
101 ir->buf_in[4];
Sean Younge99a7cf2012-08-13 08:59:39 -0300102 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) {
Sean Young0797b482012-08-13 08:59:40 -0300113 ir->cycle_overhead = ir->buf_in[5];
Sean Younge99a7cf2012-08-13 08:59:39 -0300114 complete(&ir->completion);
115 }
116 break;
Sean Young26ff6312012-07-15 13:31:00 -0300117 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");
Sean Young116e4f52012-08-13 08:59:44 -0300126 ir_raw_event_reset(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300127 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;
Sean Youngb83bfd12012-08-13 08:59:47 -0300136 bool event = false;
Sean Young26ff6312012-07-15 13:31:00 -0300137
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) *
Sean Young2eec6762012-08-13 08:59:41 -0300147 RX_RESOLUTION;
Sean Young26ff6312012-07-15 13:31:00 -0300148 }
149
Sean Youngb83bfd12012-08-13 08:59:47 -0300150 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
151 event = true;
Sean Young26ff6312012-07-15 13:31:00 -0300152 }
153
Sean Youngb83bfd12012-08-13 08:59:47 -0300154 if (event)
155 ir_raw_event_handle(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300156 }
157}
158
159static void iguanair_rx(struct urb *urb)
160{
161 struct iguanair *ir;
Sean Young7c0bd962012-08-13 08:59:43 -0300162 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300163
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
Sean Young7c0bd962012-08-13 08:59:43 -0300188 rc = usb_submit_urb(urb, GFP_ATOMIC);
189 if (rc && rc != -ENODEV)
190 dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
Sean Young26ff6312012-07-15 13:31:00 -0300191}
192
Sean Young48b0fa62012-09-28 04:44:29 -0300193static void iguanair_irq_out(struct urb *urb)
Sean Young26ff6312012-07-15 13:31:00 -0300194{
Sean Young48b0fa62012-09-28 04:44:29 -0300195 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
201static int iguanair_send(struct iguanair *ir, unsigned size)
202{
203 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300204
Sean Younge99a7cf2012-08-13 08:59:39 -0300205 INIT_COMPLETION(ir->completion);
Sean Young26ff6312012-07-15 13:31:00 -0300206
Sean Young48b0fa62012-09-28 04:44:29 -0300207 ir->urb_out->transfer_buffer_length = size;
208 rc = usb_submit_urb(ir->urb_out, GFP_KERNEL);
Sean Younge99a7cf2012-08-13 08:59:39 -0300209 if (rc)
210 return rc;
Sean Young26ff6312012-07-15 13:31:00 -0300211
Sean Younge99a7cf2012-08-13 08:59:39 -0300212 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
213 return -ETIMEDOUT;
Sean Young26ff6312012-07-15 13:31:00 -0300214
215 return rc;
216}
217
218static int iguanair_get_features(struct iguanair *ir)
219{
Sean Younge99a7cf2012-08-13 08:59:39 -0300220 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300221
Sean Young48b0fa62012-09-28 04:44:29 -0300222 ir->packet->header.start = 0;
223 ir->packet->header.direction = DIR_OUT;
224 ir->packet->header.cmd = CMD_GET_VERSION;
Sean Young26ff6312012-07-15 13:31:00 -0300225
Sean Young48b0fa62012-09-28 04:44:29 -0300226 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300227 if (rc) {
228 dev_info(ir->dev, "failed to get version\n");
229 goto out;
230 }
231
Sean Young0797b482012-08-13 08:59:40 -0300232 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
Sean Young26ff6312012-07-15 13:31:00 -0300238 ir->bufsize = 150;
239 ir->cycle_overhead = 65;
240
Sean Young48b0fa62012-09-28 04:44:29 -0300241 ir->packet->header.cmd = CMD_GET_BUFSIZE;
Sean Young26ff6312012-07-15 13:31:00 -0300242
Sean Young48b0fa62012-09-28 04:44:29 -0300243 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300244 if (rc) {
245 dev_info(ir->dev, "failed to get buffer size\n");
246 goto out;
247 }
248
Sean Young48b0fa62012-09-28 04:44:29 -0300249 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 }
Sean Young26ff6312012-07-15 13:31:00 -0300254
Sean Young48b0fa62012-09-28 04:44:29 -0300255 ir->packet->header.cmd = CMD_GET_FEATURES;
256
257 rc = iguanair_send(ir, sizeof(ir->packet->header));
Sean Young26ff6312012-07-15 13:31:00 -0300258 if (rc) {
259 dev_info(ir->dev, "failed to get features\n");
260 goto out;
261 }
262
Sean Young26ff6312012-07-15 13:31:00 -0300263out:
264 return rc;
265}
266
267static int iguanair_receiver(struct iguanair *ir, bool enable)
268{
Sean Young48b0fa62012-09-28 04:44:29 -0300269 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;
Sean Young26ff6312012-07-15 13:31:00 -0300274
Sean Young116e4f52012-08-13 08:59:44 -0300275 if (enable)
276 ir_raw_event_reset(ir->rc);
277
Sean Young48b0fa62012-09-28 04:44:29 -0300278 rc = iguanair_send(ir, sizeof(ir->packet->header));
279
280 return rc;
Sean Young26ff6312012-07-15 13:31:00 -0300281}
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 */
289static 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 */
Sean Young48b0fa62012-09-28 04:44:29 -0300325 ir->packet->busy7 = (4 - sevens) * 2;
326 ir->packet->busy4 = 110 - fours;
Sean Young26ff6312012-07-15 13:31:00 -0300327 }
328
329 mutex_unlock(&ir->lock);
330
331 return carrier;
332}
333
334static 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);
Sean Young48b0fa62012-09-28 04:44:29 -0300342 ir->packet->channels = mask << 4;
Sean Young26ff6312012-07-15 13:31:00 -0300343 mutex_unlock(&ir->lock);
344
345 return 0;
346}
347
348static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
349{
350 struct iguanair *ir = dev->priv;
Sean Young39206312012-08-25 07:01:45 -0300351 uint8_t space;
352 unsigned i, size, periods, bytes;
353 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300354
355 mutex_lock(&ir->lock);
356
357 /* convert from us to carrier periods */
Sean Young39206312012-08-25 07:01:45 -0300358 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);
Sean Young884bfd02012-08-13 08:59:42 -0300361 if (size + bytes > ir->bufsize) {
362 count = i;
363 break;
364 }
Sean Young39206312012-08-25 07:01:45 -0300365 while (periods > 127) {
Sean Young48b0fa62012-09-28 04:44:29 -0300366 ir->packet->payload[size++] = 127 | space;
Sean Young39206312012-08-25 07:01:45 -0300367 periods -= 127;
368 }
369
Sean Young48b0fa62012-09-28 04:44:29 -0300370 ir->packet->payload[size++] = periods | space;
Sean Young39206312012-08-25 07:01:45 -0300371 space ^= 0x80;
Sean Young884bfd02012-08-13 08:59:42 -0300372 }
373
374 if (count == 0) {
375 rc = -EINVAL;
376 goto out;
Sean Young26ff6312012-07-15 13:31:00 -0300377 }
378
Sean Young48b0fa62012-09-28 04:44:29 -0300379 ir->packet->header.start = 0;
380 ir->packet->header.direction = DIR_OUT;
381 ir->packet->header.cmd = CMD_SEND;
382 ir->packet->length = size;
Sean Young26ff6312012-07-15 13:31:00 -0300383
384 ir->tx_overflow = false;
385
Sean Young48b0fa62012-09-28 04:44:29 -0300386 rc = iguanair_send(ir, sizeof(*ir->packet) + size);
Sean Young26ff6312012-07-15 13:31:00 -0300387
Sean Younge99a7cf2012-08-13 08:59:39 -0300388 if (rc == 0 && ir->tx_overflow)
389 rc = -EOVERFLOW;
Sean Young26ff6312012-07-15 13:31:00 -0300390
Sean Young26ff6312012-07-15 13:31:00 -0300391out:
392 mutex_unlock(&ir->lock);
Sean Young26ff6312012-07-15 13:31:00 -0300393
Sean Young884bfd02012-08-13 08:59:42 -0300394 return rc ? rc : count;
Sean Young26ff6312012-07-15 13:31:00 -0300395}
396
397static int iguanair_open(struct rc_dev *rdev)
398{
399 struct iguanair *ir = rdev->priv;
400 int rc;
401
402 mutex_lock(&ir->lock);
403
Sean Young26ff6312012-07-15 13:31:00 -0300404 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
413static 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;
Sean Young7c0bd962012-08-13 08:59:43 -0300422 if (rc && rc != -ENODEV)
Sean Young26ff6312012-07-15 13:31:00 -0300423 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
424
Sean Young26ff6312012-07-15 13:31:00 -0300425 mutex_unlock(&ir->lock);
426}
427
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800428static int iguanair_probe(struct usb_interface *intf,
429 const struct usb_device_id *id)
Sean Young26ff6312012-07-15 13:31:00 -0300430{
431 struct usb_device *udev = interface_to_usbdev(intf);
432 struct iguanair *ir;
433 struct rc_dev *rc;
Sean Young48b0fa62012-09-28 04:44:29 -0300434 int ret, pipein, pipeout;
Sean Young26ff6312012-07-15 13:31:00 -0300435 struct usb_host_interface *idesc;
436
437 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
438 rc = rc_allocate_device();
439 if (!ir || !rc) {
Sean Young884bfd02012-08-13 08:59:42 -0300440 ret = -ENOMEM;
Sean Young26ff6312012-07-15 13:31:00 -0300441 goto out;
442 }
443
Sean Young48b0fa62012-09-28 04:44:29 -0300444 ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL,
Sean Young26ff6312012-07-15 13:31:00 -0300445 &ir->dma_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300446 ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL,
447 &ir->dma_out);
Sean Young26ff6312012-07-15 13:31:00 -0300448 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Sean Young48b0fa62012-09-28 04:44:29 -0300449 ir->urb_out = usb_alloc_urb(0, GFP_KERNEL);
Sean Young26ff6312012-07-15 13:31:00 -0300450
Sean Young48b0fa62012-09-28 04:44:29 -0300451 if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out) {
Sean Young884bfd02012-08-13 08:59:42 -0300452 ret = -ENOMEM;
Sean Young26ff6312012-07-15 13:31:00 -0300453 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;
Sean Young26ff6312012-07-15 13:31:00 -0300466 mutex_init(&ir->lock);
Sean Young48b0fa62012-09-28 04:44:29 -0300467
Sean Young26ff6312012-07-15 13:31:00 -0300468 init_completion(&ir->completion);
Sean Young48b0fa62012-09-28 04:44:29 -0300469 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;
Sean Young26ff6312012-07-15 13:31:00 -0300475
Sean Younge99a7cf2012-08-13 08:59:39 -0300476 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
Sean Young48b0fa62012-09-28 04:44:29 -0300477 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET,
Sean Young64058382012-08-13 08:59:45 -0300478 iguanair_rx, ir, 1);
Sean Young26ff6312012-07-15 13:31:00 -0300479 ir->urb_in->transfer_dma = ir->dma_in;
480 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
481
Sean Younge99a7cf2012-08-13 08:59:39 -0300482 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
Sean Young26ff6312012-07-15 13:31:00 -0300492 snprintf(ir->name, sizeof(ir->name),
Sean Young0797b482012-08-13 08:59:40 -0300493 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
Sean Young26ff6312012-07-15 13:31:00 -0300494
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;
David Härdemanc003ab12012-10-11 19:11:54 -0300502 rc->allowed_protos = RC_BIT_ALL;
Sean Young26ff6312012-07-15 13:31:00 -0300503 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;
Sean Young2eec6762012-08-13 08:59:41 -0300510 rc->map_name = RC_MAP_RC6_MCE;
511 rc->timeout = MS_TO_NS(100);
512 rc->rx_resolution = RX_RESOLUTION;
Sean Young26ff6312012-07-15 13:31:00 -0300513
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);
Sean Younge99a7cf2012-08-13 08:59:39 -0300519 goto out2;
Sean Young26ff6312012-07-15 13:31:00 -0300520 }
521
522 usb_set_intfdata(intf, ir);
523
Sean Young26ff6312012-07-15 13:31:00 -0300524 return 0;
Sean Younge99a7cf2012-08-13 08:59:39 -0300525out2:
526 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300527 usb_kill_urb(ir->urb_out);
Sean Young26ff6312012-07-15 13:31:00 -0300528out:
529 if (ir) {
530 usb_free_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300531 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);
Sean Young26ff6312012-07-15 13:31:00 -0300535 }
536 rc_free_device(rc);
537 kfree(ir);
538 return ret;
539}
540
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800541static void iguanair_disconnect(struct usb_interface *intf)
Sean Young26ff6312012-07-15 13:31:00 -0300542{
543 struct iguanair *ir = usb_get_intfdata(intf);
544
Sean Young7c0bd962012-08-13 08:59:43 -0300545 rc_unregister_device(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300546 usb_set_intfdata(intf, NULL);
Sean Young26ff6312012-07-15 13:31:00 -0300547 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300548 usb_kill_urb(ir->urb_out);
Sean Young26ff6312012-07-15 13:31:00 -0300549 usb_free_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300550 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);
Sean Young26ff6312012-07-15 13:31:00 -0300553 kfree(ir);
554}
555
556static 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
Sean Young7c0bd962012-08-13 08:59:43 -0300569 usb_kill_urb(ir->urb_in);
Sean Young48b0fa62012-09-28 04:44:29 -0300570 usb_kill_urb(ir->urb_out);
Sean Young7c0bd962012-08-13 08:59:43 -0300571
Sean Young26ff6312012-07-15 13:31:00 -0300572 mutex_unlock(&ir->lock);
573
574 return rc;
575}
576
577static 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
Sean Young7c0bd962012-08-13 08:59:43 -0300584 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
Sean Young26ff6312012-07-15 13:31:00 -0300588 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
599static const struct usb_device_id iguanair_table[] = {
600 { USB_DEVICE(0x1781, 0x0938) },
601 { }
602};
603
604static struct usb_driver iguanair_driver = {
605 .name = DRIVER_NAME,
606 .probe = iguanair_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800607 .disconnect = iguanair_disconnect,
Sean Young26ff6312012-07-15 13:31:00 -0300608 .suspend = iguanair_suspend,
609 .resume = iguanair_resume,
610 .reset_resume = iguanair_resume,
Sean Young7c0bd962012-08-13 08:59:43 -0300611 .id_table = iguanair_table,
612 .soft_unbind = 1 /* we want to disable receiver on unbind */
Sean Young26ff6312012-07-15 13:31:00 -0300613};
614
615module_usb_driver(iguanair_driver);
616
617MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
618MODULE_AUTHOR("Sean Young <sean@mess.org>");
619MODULE_LICENSE("GPL");
620MODULE_DEVICE_TABLE(usb, iguanair_table);
621