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