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