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. |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/usb.h> |
| 21 | #include <linux/usb/input.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/completion.h> |
| 24 | #include <media/rc-core.h> |
| 25 | |
| 26 | #define DRIVER_NAME "iguanair" |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 27 | #define BUF_SIZE 152 |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 28 | |
| 29 | struct iguanair { |
| 30 | struct rc_dev *rc; |
| 31 | |
| 32 | struct device *dev; |
| 33 | struct usb_device *udev; |
| 34 | |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 35 | uint16_t version; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 36 | uint8_t bufsize; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 37 | uint8_t cycle_overhead; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 38 | |
| 39 | struct mutex lock; |
| 40 | |
| 41 | /* receiver support */ |
| 42 | bool receiver_on; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 43 | dma_addr_t dma_in, dma_out; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 44 | uint8_t *buf_in; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 45 | struct urb *urb_in, *urb_out; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 46 | struct completion completion; |
| 47 | |
| 48 | /* transmit support */ |
| 49 | bool tx_overflow; |
| 50 | uint32_t carrier; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 51 | struct send_packet *packet; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 52 | |
| 53 | char name[64]; |
| 54 | char phys[64]; |
| 55 | }; |
| 56 | |
Sean Young | c6a3ea5 | 2013-01-14 05:51:44 -0300 | [diff] [blame] | 57 | #define CMD_NOP 0x00 |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 58 | #define CMD_GET_VERSION 0x01 |
| 59 | #define CMD_GET_BUFSIZE 0x11 |
| 60 | #define CMD_GET_FEATURES 0x10 |
| 61 | #define CMD_SEND 0x15 |
| 62 | #define CMD_EXECUTE 0x1f |
| 63 | #define CMD_RX_OVERFLOW 0x31 |
| 64 | #define CMD_TX_OVERFLOW 0x32 |
| 65 | #define CMD_RECEIVER_ON 0x12 |
| 66 | #define CMD_RECEIVER_OFF 0x14 |
| 67 | |
| 68 | #define DIR_IN 0xdc |
| 69 | #define DIR_OUT 0xcd |
| 70 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 71 | #define MAX_IN_PACKET 8u |
| 72 | #define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 73 | #define TIMEOUT 1000 |
Sean Young | 2eec676 | 2012-08-13 08:59:41 -0300 | [diff] [blame] | 74 | #define RX_RESOLUTION 21333 |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 75 | |
| 76 | struct packet { |
| 77 | uint16_t start; |
| 78 | uint8_t direction; |
| 79 | uint8_t cmd; |
| 80 | }; |
| 81 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 82 | struct send_packet { |
| 83 | struct packet header; |
| 84 | uint8_t length; |
| 85 | uint8_t channels; |
| 86 | uint8_t busy7; |
| 87 | uint8_t busy4; |
| 88 | uint8_t payload[0]; |
| 89 | }; |
| 90 | |
| 91 | static void process_ir_data(struct iguanair *ir, unsigned len) |
| 92 | { |
| 93 | if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) { |
| 94 | switch (ir->buf_in[3]) { |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 95 | case CMD_GET_VERSION: |
| 96 | if (len == 6) { |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 97 | ir->version = (ir->buf_in[5] << 8) | |
| 98 | ir->buf_in[4]; |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 99 | complete(&ir->completion); |
| 100 | } |
| 101 | break; |
| 102 | case CMD_GET_BUFSIZE: |
| 103 | if (len >= 5) { |
| 104 | ir->bufsize = ir->buf_in[4]; |
| 105 | complete(&ir->completion); |
| 106 | } |
| 107 | break; |
| 108 | case CMD_GET_FEATURES: |
| 109 | if (len > 5) { |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 110 | ir->cycle_overhead = ir->buf_in[5]; |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 111 | complete(&ir->completion); |
| 112 | } |
| 113 | break; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 114 | case CMD_TX_OVERFLOW: |
| 115 | ir->tx_overflow = true; |
Mauro Carvalho Chehab | 06eeefe | 2017-05-18 08:13:28 -0300 | [diff] [blame] | 116 | /* fall through */ |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 117 | case CMD_RECEIVER_OFF: |
| 118 | case CMD_RECEIVER_ON: |
| 119 | case CMD_SEND: |
| 120 | complete(&ir->completion); |
| 121 | break; |
| 122 | case CMD_RX_OVERFLOW: |
| 123 | dev_warn(ir->dev, "receive overflow\n"); |
Sean Young | 116e4f5 | 2012-08-13 08:59:44 -0300 | [diff] [blame] | 124 | ir_raw_event_reset(ir->rc); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 125 | break; |
| 126 | default: |
| 127 | dev_warn(ir->dev, "control code %02x received\n", |
| 128 | ir->buf_in[3]); |
| 129 | break; |
| 130 | } |
| 131 | } else if (len >= 7) { |
Sean Young | 183e19f | 2018-08-21 15:57:52 -0400 | [diff] [blame] | 132 | struct ir_raw_event rawir = {}; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 133 | unsigned i; |
Sean Young | b83bfd1 | 2012-08-13 08:59:47 -0300 | [diff] [blame] | 134 | bool event = false; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 135 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 136 | for (i = 0; i < 7; i++) { |
| 137 | if (ir->buf_in[i] == 0x80) { |
| 138 | rawir.pulse = false; |
| 139 | rawir.duration = US_TO_NS(21845); |
| 140 | } else { |
| 141 | rawir.pulse = (ir->buf_in[i] & 0x80) == 0; |
| 142 | rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) * |
Sean Young | 2eec676 | 2012-08-13 08:59:41 -0300 | [diff] [blame] | 143 | RX_RESOLUTION; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 144 | } |
| 145 | |
Sean Young | b83bfd1 | 2012-08-13 08:59:47 -0300 | [diff] [blame] | 146 | if (ir_raw_event_store_with_filter(ir->rc, &rawir)) |
| 147 | event = true; |
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 (event) |
| 151 | ir_raw_event_handle(ir->rc); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | static void iguanair_rx(struct urb *urb) |
| 156 | { |
| 157 | struct iguanair *ir; |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 158 | int rc; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 159 | |
| 160 | if (!urb) |
| 161 | return; |
| 162 | |
| 163 | ir = urb->context; |
| 164 | if (!ir) { |
| 165 | usb_unlink_urb(urb); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | switch (urb->status) { |
| 170 | case 0: |
| 171 | process_ir_data(ir, urb->actual_length); |
| 172 | break; |
| 173 | case -ECONNRESET: |
| 174 | case -ENOENT: |
| 175 | case -ESHUTDOWN: |
| 176 | usb_unlink_urb(urb); |
| 177 | return; |
| 178 | case -EPIPE: |
| 179 | default: |
| 180 | dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status); |
| 181 | break; |
| 182 | } |
| 183 | |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 184 | rc = usb_submit_urb(urb, GFP_ATOMIC); |
| 185 | if (rc && rc != -ENODEV) |
| 186 | dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 187 | } |
| 188 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 189 | static void iguanair_irq_out(struct urb *urb) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 190 | { |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 191 | struct iguanair *ir = urb->context; |
| 192 | |
| 193 | if (urb->status) |
| 194 | dev_dbg(ir->dev, "Error: out urb status = %d\n", urb->status); |
Sean Young | c6a3ea5 | 2013-01-14 05:51:44 -0300 | [diff] [blame] | 195 | |
| 196 | /* if we sent an nop packet, do not expect a response */ |
| 197 | if (urb->status == 0 && ir->packet->header.cmd == CMD_NOP) |
| 198 | complete(&ir->completion); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 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 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 205 | reinit_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 | c6a3ea5 | 2013-01-14 05:51:44 -0300 | [diff] [blame] | 222 | /* |
| 223 | * On cold boot, the iguanair initializes on the first packet |
| 224 | * received but does not process that packet. Send an empty |
| 225 | * packet. |
| 226 | */ |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 227 | ir->packet->header.start = 0; |
| 228 | ir->packet->header.direction = DIR_OUT; |
Sean Young | c6a3ea5 | 2013-01-14 05:51:44 -0300 | [diff] [blame] | 229 | ir->packet->header.cmd = CMD_NOP; |
| 230 | iguanair_send(ir, sizeof(ir->packet->header)); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 231 | |
Sean Young | c6a3ea5 | 2013-01-14 05:51:44 -0300 | [diff] [blame] | 232 | ir->packet->header.cmd = CMD_GET_VERSION; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 233 | rc = iguanair_send(ir, sizeof(ir->packet->header)); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 234 | if (rc) { |
| 235 | dev_info(ir->dev, "failed to get version\n"); |
| 236 | goto out; |
| 237 | } |
| 238 | |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 239 | if (ir->version < 0x205) { |
| 240 | dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version); |
| 241 | rc = -ENODEV; |
| 242 | goto out; |
| 243 | } |
| 244 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 245 | ir->bufsize = 150; |
| 246 | ir->cycle_overhead = 65; |
| 247 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 248 | ir->packet->header.cmd = CMD_GET_BUFSIZE; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 249 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 250 | rc = iguanair_send(ir, sizeof(ir->packet->header)); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 251 | if (rc) { |
| 252 | dev_info(ir->dev, "failed to get buffer size\n"); |
| 253 | goto out; |
| 254 | } |
| 255 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 256 | if (ir->bufsize > BUF_SIZE) { |
| 257 | dev_info(ir->dev, "buffer size %u larger than expected\n", |
| 258 | ir->bufsize); |
| 259 | ir->bufsize = BUF_SIZE; |
| 260 | } |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 261 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 262 | ir->packet->header.cmd = CMD_GET_FEATURES; |
| 263 | |
| 264 | rc = iguanair_send(ir, sizeof(ir->packet->header)); |
Sean Young | c6a3ea5 | 2013-01-14 05:51:44 -0300 | [diff] [blame] | 265 | if (rc) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 266 | dev_info(ir->dev, "failed to get features\n"); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 267 | out: |
| 268 | return rc; |
| 269 | } |
| 270 | |
| 271 | static int iguanair_receiver(struct iguanair *ir, bool enable) |
| 272 | { |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 273 | ir->packet->header.start = 0; |
| 274 | ir->packet->header.direction = DIR_OUT; |
| 275 | ir->packet->header.cmd = enable ? CMD_RECEIVER_ON : CMD_RECEIVER_OFF; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 276 | |
Sean Young | 116e4f5 | 2012-08-13 08:59:44 -0300 | [diff] [blame] | 277 | if (enable) |
| 278 | ir_raw_event_reset(ir->rc); |
| 279 | |
Sean Young | c6a3ea5 | 2013-01-14 05:51:44 -0300 | [diff] [blame] | 280 | return iguanair_send(ir, sizeof(ir->packet->header)); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* |
Sean Young | dd3a5a1 | 2014-01-20 19:10:38 -0300 | [diff] [blame] | 284 | * The iguanair creates the carrier by busy spinning after each half period. |
| 285 | * This is counted in CPU cycles, with the CPU running at 24MHz. It is |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 286 | * broken down into 7-cycles and 4-cyles delays, with a preference for |
Sean Young | dd3a5a1 | 2014-01-20 19:10:38 -0300 | [diff] [blame] | 287 | * 4-cycle delays, minus the overhead of the loop itself (cycle_overhead). |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 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 | |
Sean Young | 9bd766b | 2013-11-03 19:13:57 -0300 | [diff] [blame] | 306 | /* |
| 307 | * Calculate minimum number of 7 cycles needed so |
| 308 | * we are left with a multiple of 4; so we want to have |
| 309 | * (sevens * 7) & 3 == cycles & 3 |
| 310 | */ |
| 311 | sevens = (4 - cycles) & 3; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 312 | fours = (cycles - sevens * 7) / 4; |
| 313 | |
Sean Young | dd3a5a1 | 2014-01-20 19:10:38 -0300 | [diff] [blame] | 314 | /* |
| 315 | * The firmware interprets these values as a relative offset |
| 316 | * for a branch. Immediately following the branches, there |
| 317 | * 4 instructions of 7 cycles (2 bytes each) and 110 |
| 318 | * instructions of 4 cycles (1 byte each). A relative branch |
| 319 | * of 0 will execute all of them, branch further for less |
| 320 | * cycle burning. |
| 321 | */ |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 322 | ir->packet->busy7 = (4 - sevens) * 2; |
| 323 | ir->packet->busy4 = 110 - fours; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | mutex_unlock(&ir->lock); |
| 327 | |
Sean Young | 14d8188 | 2016-07-10 13:34:33 -0300 | [diff] [blame] | 328 | return 0; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask) |
| 332 | { |
| 333 | struct iguanair *ir = dev->priv; |
| 334 | |
| 335 | if (mask > 15) |
| 336 | return 4; |
| 337 | |
| 338 | mutex_lock(&ir->lock); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 339 | ir->packet->channels = mask << 4; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 340 | mutex_unlock(&ir->lock); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count) |
| 346 | { |
| 347 | struct iguanair *ir = dev->priv; |
Sean Young | b996157 | 2017-12-11 17:21:28 -0500 | [diff] [blame] | 348 | unsigned int i, size, p, periods; |
Sean Young | 3920631 | 2012-08-25 07:01:45 -0300 | [diff] [blame] | 349 | int rc; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 350 | |
| 351 | mutex_lock(&ir->lock); |
| 352 | |
| 353 | /* convert from us to carrier periods */ |
Sean Young | b996157 | 2017-12-11 17:21:28 -0500 | [diff] [blame] | 354 | for (i = size = 0; i < count; i++) { |
Sean Young | 3920631 | 2012-08-25 07:01:45 -0300 | [diff] [blame] | 355 | periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000); |
Sean Young | 776eced | 2014-01-20 19:10:39 -0300 | [diff] [blame] | 356 | while (periods) { |
Sean Young | b996157 | 2017-12-11 17:21:28 -0500 | [diff] [blame] | 357 | p = min(periods, 127u); |
| 358 | if (size >= ir->bufsize) { |
| 359 | rc = -EINVAL; |
| 360 | goto out; |
| 361 | } |
| 362 | ir->packet->payload[size++] = p | ((i & 1) ? 0x80 : 0); |
Sean Young | 776eced | 2014-01-20 19:10:39 -0300 | [diff] [blame] | 363 | periods -= p; |
Sean Young | 3920631 | 2012-08-25 07:01:45 -0300 | [diff] [blame] | 364 | } |
Sean Young | 884bfd0 | 2012-08-13 08:59:42 -0300 | [diff] [blame] | 365 | } |
| 366 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 367 | ir->packet->header.start = 0; |
| 368 | ir->packet->header.direction = DIR_OUT; |
| 369 | ir->packet->header.cmd = CMD_SEND; |
| 370 | ir->packet->length = size; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 371 | |
| 372 | ir->tx_overflow = false; |
| 373 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 374 | rc = iguanair_send(ir, sizeof(*ir->packet) + size); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 375 | |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 376 | if (rc == 0 && ir->tx_overflow) |
| 377 | rc = -EOVERFLOW; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 378 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 379 | out: |
| 380 | mutex_unlock(&ir->lock); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 381 | |
Sean Young | 884bfd0 | 2012-08-13 08:59:42 -0300 | [diff] [blame] | 382 | return rc ? rc : count; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static int iguanair_open(struct rc_dev *rdev) |
| 386 | { |
| 387 | struct iguanair *ir = rdev->priv; |
| 388 | int rc; |
| 389 | |
| 390 | mutex_lock(&ir->lock); |
| 391 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 392 | rc = iguanair_receiver(ir, true); |
| 393 | if (rc == 0) |
| 394 | ir->receiver_on = true; |
| 395 | |
| 396 | mutex_unlock(&ir->lock); |
| 397 | |
| 398 | return rc; |
| 399 | } |
| 400 | |
| 401 | static void iguanair_close(struct rc_dev *rdev) |
| 402 | { |
| 403 | struct iguanair *ir = rdev->priv; |
| 404 | int rc; |
| 405 | |
| 406 | mutex_lock(&ir->lock); |
| 407 | |
| 408 | rc = iguanair_receiver(ir, false); |
| 409 | ir->receiver_on = false; |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 410 | if (rc && rc != -ENODEV) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 411 | dev_warn(ir->dev, "failed to disable receiver: %d\n", rc); |
| 412 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 413 | mutex_unlock(&ir->lock); |
| 414 | } |
| 415 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 416 | static int iguanair_probe(struct usb_interface *intf, |
| 417 | const struct usb_device_id *id) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 418 | { |
| 419 | struct usb_device *udev = interface_to_usbdev(intf); |
| 420 | struct iguanair *ir; |
| 421 | struct rc_dev *rc; |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 422 | int ret, pipein, pipeout; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 423 | struct usb_host_interface *idesc; |
| 424 | |
| 425 | ir = kzalloc(sizeof(*ir), GFP_KERNEL); |
Andi Shyti | 0f7499f | 2016-12-16 06:50:58 -0200 | [diff] [blame] | 426 | rc = rc_allocate_device(RC_DRIVER_IR_RAW); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 427 | if (!ir || !rc) { |
Sean Young | 884bfd0 | 2012-08-13 08:59:42 -0300 | [diff] [blame] | 428 | ret = -ENOMEM; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 429 | goto out; |
| 430 | } |
| 431 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 432 | ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL, |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 433 | &ir->dma_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 434 | ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL, |
| 435 | &ir->dma_out); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 436 | ir->urb_in = usb_alloc_urb(0, GFP_KERNEL); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 437 | ir->urb_out = usb_alloc_urb(0, GFP_KERNEL); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 438 | |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 439 | if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out) { |
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 | |
| 444 | idesc = intf->altsetting; |
| 445 | |
| 446 | if (idesc->desc.bNumEndpoints < 2) { |
| 447 | ret = -ENODEV; |
| 448 | goto out; |
| 449 | } |
| 450 | |
| 451 | ir->rc = rc; |
| 452 | ir->dev = &intf->dev; |
| 453 | ir->udev = udev; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 454 | mutex_init(&ir->lock); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 455 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 456 | init_completion(&ir->completion); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 457 | pipeout = usb_sndintpipe(udev, |
| 458 | idesc->endpoint[1].desc.bEndpointAddress); |
| 459 | usb_fill_int_urb(ir->urb_out, udev, pipeout, ir->packet, MAX_OUT_PACKET, |
| 460 | iguanair_irq_out, ir, 1); |
| 461 | ir->urb_out->transfer_dma = ir->dma_out; |
| 462 | ir->urb_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 463 | |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 464 | pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 465 | 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] | 466 | iguanair_rx, ir, 1); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 467 | ir->urb_in->transfer_dma = ir->dma_in; |
| 468 | ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 469 | |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 470 | ret = usb_submit_urb(ir->urb_in, GFP_KERNEL); |
| 471 | if (ret) { |
| 472 | dev_warn(&intf->dev, "failed to submit urb: %d\n", ret); |
| 473 | goto out; |
| 474 | } |
| 475 | |
| 476 | ret = iguanair_get_features(ir); |
| 477 | if (ret) |
| 478 | goto out2; |
| 479 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 480 | snprintf(ir->name, sizeof(ir->name), |
Sean Young | 0797b48 | 2012-08-13 08:59:40 -0300 | [diff] [blame] | 481 | "IguanaWorks USB IR Transceiver version 0x%04x", ir->version); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 482 | |
| 483 | usb_make_path(ir->udev, ir->phys, sizeof(ir->phys)); |
| 484 | |
Sean Young | 518f4b2 | 2017-07-01 12:13:19 -0400 | [diff] [blame] | 485 | rc->device_name = ir->name; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 486 | rc->input_phys = ir->phys; |
| 487 | usb_to_input_id(ir->udev, &rc->input_id); |
| 488 | rc->dev.parent = &intf->dev; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 489 | rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 490 | rc->priv = ir; |
| 491 | rc->open = iguanair_open; |
| 492 | rc->close = iguanair_close; |
| 493 | rc->s_tx_mask = iguanair_set_tx_mask; |
| 494 | rc->s_tx_carrier = iguanair_set_tx_carrier; |
| 495 | rc->tx_ir = iguanair_tx; |
| 496 | rc->driver_name = DRIVER_NAME; |
Sean Young | 2eec676 | 2012-08-13 08:59:41 -0300 | [diff] [blame] | 497 | rc->map_name = RC_MAP_RC6_MCE; |
Sean Young | 56a6036 | 2016-12-02 15:16:12 -0200 | [diff] [blame] | 498 | rc->min_timeout = 1; |
| 499 | rc->timeout = IR_DEFAULT_TIMEOUT; |
| 500 | rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT; |
Sean Young | 2eec676 | 2012-08-13 08:59:41 -0300 | [diff] [blame] | 501 | rc->rx_resolution = RX_RESOLUTION; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 502 | |
| 503 | iguanair_set_tx_carrier(rc, 38000); |
Sean Young | d0aab65 | 2013-01-06 13:19:44 -0300 | [diff] [blame] | 504 | iguanair_set_tx_mask(rc, 0); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 505 | |
| 506 | ret = rc_register_device(rc); |
| 507 | if (ret < 0) { |
| 508 | dev_err(&intf->dev, "failed to register rc device %d", ret); |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 509 | goto out2; |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | usb_set_intfdata(intf, ir); |
| 513 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 514 | return 0; |
Sean Young | e99a7cf | 2012-08-13 08:59:39 -0300 | [diff] [blame] | 515 | out2: |
| 516 | usb_kill_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 517 | usb_kill_urb(ir->urb_out); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 518 | out: |
| 519 | if (ir) { |
| 520 | usb_free_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 521 | usb_free_urb(ir->urb_out); |
| 522 | usb_free_coherent(udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in); |
| 523 | usb_free_coherent(udev, MAX_OUT_PACKET, ir->packet, |
| 524 | ir->dma_out); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 525 | } |
| 526 | rc_free_device(rc); |
| 527 | kfree(ir); |
| 528 | return ret; |
| 529 | } |
| 530 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 531 | static void iguanair_disconnect(struct usb_interface *intf) |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 532 | { |
| 533 | struct iguanair *ir = usb_get_intfdata(intf); |
| 534 | |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 535 | rc_unregister_device(ir->rc); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 536 | usb_set_intfdata(intf, NULL); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 537 | usb_kill_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 538 | usb_kill_urb(ir->urb_out); |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 539 | usb_free_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 540 | usb_free_urb(ir->urb_out); |
| 541 | usb_free_coherent(ir->udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in); |
| 542 | 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] | 543 | kfree(ir); |
| 544 | } |
| 545 | |
| 546 | static int iguanair_suspend(struct usb_interface *intf, pm_message_t message) |
| 547 | { |
| 548 | struct iguanair *ir = usb_get_intfdata(intf); |
| 549 | int rc = 0; |
| 550 | |
| 551 | mutex_lock(&ir->lock); |
| 552 | |
| 553 | if (ir->receiver_on) { |
| 554 | rc = iguanair_receiver(ir, false); |
| 555 | if (rc) |
| 556 | dev_warn(ir->dev, "failed to disable receiver for suspend\n"); |
| 557 | } |
| 558 | |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 559 | usb_kill_urb(ir->urb_in); |
Sean Young | 48b0fa6 | 2012-09-28 04:44:29 -0300 | [diff] [blame] | 560 | usb_kill_urb(ir->urb_out); |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 561 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 562 | mutex_unlock(&ir->lock); |
| 563 | |
| 564 | return rc; |
| 565 | } |
| 566 | |
| 567 | static int iguanair_resume(struct usb_interface *intf) |
| 568 | { |
| 569 | struct iguanair *ir = usb_get_intfdata(intf); |
| 570 | int rc = 0; |
| 571 | |
| 572 | mutex_lock(&ir->lock); |
| 573 | |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 574 | rc = usb_submit_urb(ir->urb_in, GFP_KERNEL); |
| 575 | if (rc) |
| 576 | dev_warn(&intf->dev, "failed to submit urb: %d\n", rc); |
| 577 | |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 578 | if (ir->receiver_on) { |
| 579 | rc = iguanair_receiver(ir, true); |
| 580 | if (rc) |
| 581 | dev_warn(ir->dev, "failed to enable receiver after resume\n"); |
| 582 | } |
| 583 | |
| 584 | mutex_unlock(&ir->lock); |
| 585 | |
| 586 | return rc; |
| 587 | } |
| 588 | |
| 589 | static const struct usb_device_id iguanair_table[] = { |
| 590 | { USB_DEVICE(0x1781, 0x0938) }, |
| 591 | { } |
| 592 | }; |
| 593 | |
| 594 | static struct usb_driver iguanair_driver = { |
| 595 | .name = DRIVER_NAME, |
| 596 | .probe = iguanair_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 597 | .disconnect = iguanair_disconnect, |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 598 | .suspend = iguanair_suspend, |
| 599 | .resume = iguanair_resume, |
| 600 | .reset_resume = iguanair_resume, |
Sean Young | 7c0bd96 | 2012-08-13 08:59:43 -0300 | [diff] [blame] | 601 | .id_table = iguanair_table, |
| 602 | .soft_unbind = 1 /* we want to disable receiver on unbind */ |
Sean Young | 26ff631 | 2012-07-15 13:31:00 -0300 | [diff] [blame] | 603 | }; |
| 604 | |
| 605 | module_usb_driver(iguanair_driver); |
| 606 | |
| 607 | MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver"); |
| 608 | MODULE_AUTHOR("Sean Young <sean@mess.org>"); |
| 609 | MODULE_LICENSE("GPL"); |
| 610 | MODULE_DEVICE_TABLE(usb, iguanair_table); |
| 611 | |