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" |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 31 | #define BUF_SIZE 152 |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 32 | |
| 33 | struct iguanair { |
| 34 | struct rc_dev *rc; |
| 35 | |
| 36 | struct device *dev; |
| 37 | struct usb_device *udev; |
| 38 | |
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 | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 41 | uint8_t cycle_overhead; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 42 | |
| 43 | struct mutex lock; |
| 44 | |
| 45 | /* receiver support */ |
| 46 | bool receiver_on; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 47 | dma_addr_t dma_in, dma_out; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 48 | uint8_t *buf_in; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 49 | struct urb *urb_in, *urb_out; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 50 | struct completion completion; |
| 51 | |
| 52 | /* transmit support */ |
| 53 | bool tx_overflow; |
| 54 | uint32_t carrier; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 55 | struct send_packet *packet; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 56 | |
| 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 Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 74 | #define MAX_IN_PACKET 8u |
| 75 | #define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 76 | #define TIMEOUT 1000 |
Sean Young | 2eec676 | 2012-08-13 08:59:41 -0300 | [diff] [blame] | 77 | #define RX_RESOLUTION 21333 |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 78 | |
| 79 | struct packet { |
| 80 | uint16_t start; |
| 81 | uint8_t direction; |
| 82 | uint8_t cmd; |
| 83 | }; |
| 84 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 85 | struct send_packet { |
| 86 | struct packet header; |
| 87 | uint8_t length; |
| 88 | uint8_t channels; |
| 89 | uint8_t busy7; |
| 90 | uint8_t busy4; |
| 91 | uint8_t payload[0]; |
| 92 | }; |
| 93 | |
| 94 | static void process_ir_data(struct iguanair *ir, unsigned len) |
| 95 | { |
| 96 | if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) { |
| 97 | switch (ir->buf_in[3]) { |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 98 | case CMD_GET_VERSION: |
| 99 | if (len == 6) { |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 100 | ir->version = (ir->buf_in[5] << 8) | |
| 101 | ir->buf_in[4]; |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 102 | complete(&ir->completion); |
| 103 | } |
| 104 | break; |
| 105 | case CMD_GET_BUFSIZE: |
| 106 | if (len >= 5) { |
| 107 | ir->bufsize = ir->buf_in[4]; |
| 108 | complete(&ir->completion); |
| 109 | } |
| 110 | break; |
| 111 | case CMD_GET_FEATURES: |
| 112 | if (len > 5) { |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 113 | ir->cycle_overhead = ir->buf_in[5]; |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 114 | complete(&ir->completion); |
| 115 | } |
| 116 | break; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 117 | 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 Young | 116e4f5 | 2012-08-13 08:59:44 -0300 | [diff] [blame] | 126 | ir_raw_event_reset(ir->rc); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 127 | break; |
| 128 | default: |
| 129 | dev_warn(ir->dev, "control code %02x received\n", |
| 130 | ir->buf_in[3]); |
| 131 | break; |
| 132 | } |
| 133 | } else if (len >= 7) { |
| 134 | DEFINE_IR_RAW_EVENT(rawir); |
| 135 | unsigned i; |
Sean Young | b83bfd1 | 2012-08-13 08:59:47 -0300 | [diff] [blame] | 136 | bool event = false; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 137 | |
| 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 Young | 2eec676 | 2012-08-13 08:59:41 -0300 | [diff] [blame] | 147 | RX_RESOLUTION; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 148 | } |
| 149 | |
Sean Young | b83bfd1 | 2012-08-13 08:59:47 -0300 | [diff] [blame] | 150 | if (ir_raw_event_store_with_filter(ir->rc, &rawir)) |
| 151 | event = true; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 152 | } |
| 153 | |
Sean Young | b83bfd1 | 2012-08-13 08:59:47 -0300 | [diff] [blame] | 154 | if (event) |
| 155 | ir_raw_event_handle(ir->rc); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | static void iguanair_rx(struct urb *urb) |
| 160 | { |
| 161 | struct iguanair *ir; |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 162 | int rc; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 163 | |
| 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 Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 188 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 191 | } |
| 192 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 193 | static void iguanair_irq_out(struct urb *urb) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 194 | { |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 195 | 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 | |
| 201 | static int iguanair_send(struct iguanair *ir, unsigned size) |
| 202 | { |
| 203 | int 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 | INIT_COMPLETION(ir->completion); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 206 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 207 | ir->urb_out->transfer_buffer_length = size; |
| 208 | rc = usb_submit_urb(ir->urb_out, GFP_KERNEL); |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 209 | if (rc) |
| 210 | return rc; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 211 | |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 212 | if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0) |
| 213 | return -ETIMEDOUT; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 214 | |
| 215 | return rc; |
| 216 | } |
| 217 | |
| 218 | static int iguanair_get_features(struct iguanair *ir) |
| 219 | { |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 220 | int rc; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 221 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 222 | ir->packet->header.start = 0; |
| 223 | ir->packet->header.direction = DIR_OUT; |
| 224 | ir->packet->header.cmd = CMD_GET_VERSION; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 225 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 226 | rc = iguanair_send(ir, sizeof(ir->packet->header)); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 227 | if (rc) { |
| 228 | dev_info(ir->dev, "failed to get version\n"); |
| 229 | goto out; |
| 230 | } |
| 231 | |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 232 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 238 | ir->bufsize = 150; |
| 239 | ir->cycle_overhead = 65; |
| 240 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 241 | ir->packet->header.cmd = CMD_GET_BUFSIZE; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 242 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 243 | rc = iguanair_send(ir, sizeof(ir->packet->header)); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 244 | if (rc) { |
| 245 | dev_info(ir->dev, "failed to get buffer size\n"); |
| 246 | goto out; |
| 247 | } |
| 248 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 249 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 254 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 255 | ir->packet->header.cmd = CMD_GET_FEATURES; |
| 256 | |
| 257 | rc = iguanair_send(ir, sizeof(ir->packet->header)); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 258 | if (rc) { |
| 259 | dev_info(ir->dev, "failed to get features\n"); |
| 260 | goto out; |
| 261 | } |
| 262 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 263 | out: |
| 264 | return rc; |
| 265 | } |
| 266 | |
| 267 | static int iguanair_receiver(struct iguanair *ir, bool enable) |
| 268 | { |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 269 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 274 | |
Sean Young | 116e4f5 | 2012-08-13 08:59:44 -0300 | [diff] [blame] | 275 | if (enable) |
| 276 | ir_raw_event_reset(ir->rc); |
| 277 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 278 | rc = iguanair_send(ir, sizeof(ir->packet->header)); |
| 279 | |
| 280 | return rc; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 281 | } |
| 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 | */ |
| 289 | static 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 Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 325 | ir->packet->busy7 = (4 - sevens) * 2; |
| 326 | ir->packet->busy4 = 110 - fours; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | mutex_unlock(&ir->lock); |
| 330 | |
| 331 | return carrier; |
| 332 | } |
| 333 | |
| 334 | static 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 Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 342 | ir->packet->channels = mask << 4; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 343 | mutex_unlock(&ir->lock); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count) |
| 349 | { |
| 350 | struct iguanair *ir = dev->priv; |
Sean Young | 3920631 | 2012-08-25 07:01:45 -0300 | [diff] [blame] | 351 | uint8_t space; |
| 352 | unsigned i, size, periods, bytes; |
| 353 | int rc; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 354 | |
| 355 | mutex_lock(&ir->lock); |
| 356 | |
| 357 | /* convert from us to carrier periods */ |
Sean Young | 3920631 | 2012-08-25 07:01:45 -0300 | [diff] [blame] | 358 | 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 Young | 884bfd0 | 2012-08-13 08:59:42 -0300 | [diff] [blame] | 361 | if (size + bytes > ir->bufsize) { |
| 362 | count = i; |
| 363 | break; |
| 364 | } |
Sean Young | 3920631 | 2012-08-25 07:01:45 -0300 | [diff] [blame] | 365 | while (periods > 127) { |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 366 | ir->packet->payload[size++] = 127 | space; |
Sean Young | 3920631 | 2012-08-25 07:01:45 -0300 | [diff] [blame] | 367 | periods -= 127; |
| 368 | } |
| 369 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 370 | ir->packet->payload[size++] = periods | space; |
Sean Young | 3920631 | 2012-08-25 07:01:45 -0300 | [diff] [blame] | 371 | space ^= 0x80; |
Sean Young | 884bfd0 | 2012-08-13 08:59:42 -0300 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if (count == 0) { |
| 375 | rc = -EINVAL; |
| 376 | goto out; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 377 | } |
| 378 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 379 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 383 | |
| 384 | ir->tx_overflow = false; |
| 385 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 386 | rc = iguanair_send(ir, sizeof(*ir->packet) + size); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 387 | |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 388 | if (rc == 0 && ir->tx_overflow) |
| 389 | rc = -EOVERFLOW; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 390 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 391 | out: |
| 392 | mutex_unlock(&ir->lock); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 393 | |
Sean Young | 884bfd0 | 2012-08-13 08:59:42 -0300 | [diff] [blame] | 394 | return rc ? rc : count; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int iguanair_open(struct rc_dev *rdev) |
| 398 | { |
| 399 | struct iguanair *ir = rdev->priv; |
| 400 | int rc; |
| 401 | |
| 402 | mutex_lock(&ir->lock); |
| 403 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 404 | 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 | |
| 413 | static 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 Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 422 | if (rc && rc != -ENODEV) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 423 | dev_warn(ir->dev, "failed to disable receiver: %d\n", rc); |
| 424 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 425 | mutex_unlock(&ir->lock); |
| 426 | } |
| 427 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 428 | static int iguanair_probe(struct usb_interface *intf, |
| 429 | const struct usb_device_id *id) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 430 | { |
| 431 | struct usb_device *udev = interface_to_usbdev(intf); |
| 432 | struct iguanair *ir; |
| 433 | struct rc_dev *rc; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 434 | int ret, pipein, pipeout; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 435 | struct usb_host_interface *idesc; |
| 436 | |
| 437 | ir = kzalloc(sizeof(*ir), GFP_KERNEL); |
| 438 | rc = rc_allocate_device(); |
| 439 | if (!ir || !rc) { |
Sean Young | 884bfd0 | 2012-08-13 08:59:42 -0300 | [diff] [blame] | 440 | ret = -ENOMEM; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 441 | goto out; |
| 442 | } |
| 443 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 444 | ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL, |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 445 | &ir->dma_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 446 | ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL, |
| 447 | &ir->dma_out); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 448 | ir->urb_in = usb_alloc_urb(0, GFP_KERNEL); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 449 | ir->urb_out = usb_alloc_urb(0, GFP_KERNEL); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 450 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 451 | if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out) { |
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 | |
| 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 466 | mutex_init(&ir->lock); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 467 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 468 | init_completion(&ir->completion); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 469 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 475 | |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 476 | pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 477 | usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET, |
Sean Young | 6405838 | 2012-08-13 08:59:45 -0300 | [diff] [blame] | 478 | iguanair_rx, ir, 1); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 479 | ir->urb_in->transfer_dma = ir->dma_in; |
| 480 | ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 481 | |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 482 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 492 | snprintf(ir->name, sizeof(ir->name), |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 493 | "IguanaWorks USB IR Transceiver version 0x%04x", ir->version); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 494 | |
| 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ärdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 502 | rc->allowed_protos = RC_BIT_ALL; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 503 | 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 Young | 2eec676 | 2012-08-13 08:59:41 -0300 | [diff] [blame] | 510 | rc->map_name = RC_MAP_RC6_MCE; |
| 511 | rc->timeout = MS_TO_NS(100); |
| 512 | rc->rx_resolution = RX_RESOLUTION; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 513 | |
| 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 Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 519 | goto out2; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | usb_set_intfdata(intf, ir); |
| 523 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 524 | return 0; |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 525 | out2: |
| 526 | usb_kill_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 527 | usb_kill_urb(ir->urb_out); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 528 | out: |
| 529 | if (ir) { |
| 530 | usb_free_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 531 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 535 | } |
| 536 | rc_free_device(rc); |
| 537 | kfree(ir); |
| 538 | return ret; |
| 539 | } |
| 540 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 541 | static void iguanair_disconnect(struct usb_interface *intf) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 542 | { |
| 543 | struct iguanair *ir = usb_get_intfdata(intf); |
| 544 | |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 545 | rc_unregister_device(ir->rc); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 546 | usb_set_intfdata(intf, NULL); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 547 | usb_kill_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 548 | usb_kill_urb(ir->urb_out); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 549 | usb_free_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 550 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 553 | kfree(ir); |
| 554 | } |
| 555 | |
| 556 | static 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 Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 569 | usb_kill_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 570 | usb_kill_urb(ir->urb_out); |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 571 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 572 | mutex_unlock(&ir->lock); |
| 573 | |
| 574 | return rc; |
| 575 | } |
| 576 | |
| 577 | static 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 Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 584 | 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 Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 588 | 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 | |
| 599 | static const struct usb_device_id iguanair_table[] = { |
| 600 | { USB_DEVICE(0x1781, 0x0938) }, |
| 601 | { } |
| 602 | }; |
| 603 | |
| 604 | static struct usb_driver iguanair_driver = { |
| 605 | .name = DRIVER_NAME, |
| 606 | .probe = iguanair_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 607 | .disconnect = iguanair_disconnect, |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 608 | .suspend = iguanair_suspend, |
| 609 | .resume = iguanair_resume, |
| 610 | .reset_resume = iguanair_resume, |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 611 | .id_table = iguanair_table, |
| 612 | .soft_unbind = 1 /* we want to disable receiver on unbind */ |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | module_usb_driver(iguanair_driver); |
| 616 | |
| 617 | MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver"); |
| 618 | MODULE_AUTHOR("Sean Young <sean@mess.org>"); |
| 619 | MODULE_LICENSE("GPL"); |
| 620 | MODULE_DEVICE_TABLE(usb, iguanair_table); |
| 621 | |