iguanair.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. #include <linux/device.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/input.h>
  21. #include <linux/slab.h>
  22. #include <linux/completion.h>
  23. #include <media/rc-core.h>
  24. #define DRIVER_NAME "iguanair"
  25. #define BUF_SIZE 152
  26. struct iguanair {
  27. struct rc_dev *rc;
  28. struct device *dev;
  29. struct usb_device *udev;
  30. uint16_t version;
  31. uint8_t bufsize;
  32. uint8_t cycle_overhead;
  33. struct mutex lock;
  34. /* receiver support */
  35. bool receiver_on;
  36. dma_addr_t dma_in, dma_out;
  37. uint8_t *buf_in;
  38. struct urb *urb_in, *urb_out;
  39. struct completion completion;
  40. /* transmit support */
  41. bool tx_overflow;
  42. uint32_t carrier;
  43. struct send_packet *packet;
  44. char name[64];
  45. char phys[64];
  46. };
  47. #define CMD_NOP 0x00
  48. #define CMD_GET_VERSION 0x01
  49. #define CMD_GET_BUFSIZE 0x11
  50. #define CMD_GET_FEATURES 0x10
  51. #define CMD_SEND 0x15
  52. #define CMD_EXECUTE 0x1f
  53. #define CMD_RX_OVERFLOW 0x31
  54. #define CMD_TX_OVERFLOW 0x32
  55. #define CMD_RECEIVER_ON 0x12
  56. #define CMD_RECEIVER_OFF 0x14
  57. #define DIR_IN 0xdc
  58. #define DIR_OUT 0xcd
  59. #define MAX_IN_PACKET 8u
  60. #define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
  61. #define TIMEOUT 1000
  62. #define RX_RESOLUTION 21333
  63. struct packet {
  64. uint16_t start;
  65. uint8_t direction;
  66. uint8_t cmd;
  67. };
  68. struct send_packet {
  69. struct packet header;
  70. uint8_t length;
  71. uint8_t channels;
  72. uint8_t busy7;
  73. uint8_t busy4;
  74. uint8_t payload[0];
  75. };
  76. static void process_ir_data(struct iguanair *ir, unsigned len)
  77. {
  78. if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
  79. switch (ir->buf_in[3]) {
  80. case CMD_GET_VERSION:
  81. if (len == 6) {
  82. ir->version = (ir->buf_in[5] << 8) |
  83. ir->buf_in[4];
  84. complete(&ir->completion);
  85. }
  86. break;
  87. case CMD_GET_BUFSIZE:
  88. if (len >= 5) {
  89. ir->bufsize = ir->buf_in[4];
  90. complete(&ir->completion);
  91. }
  92. break;
  93. case CMD_GET_FEATURES:
  94. if (len > 5) {
  95. ir->cycle_overhead = ir->buf_in[5];
  96. complete(&ir->completion);
  97. }
  98. break;
  99. case CMD_TX_OVERFLOW:
  100. ir->tx_overflow = true;
  101. /* fall through */
  102. case CMD_RECEIVER_OFF:
  103. case CMD_RECEIVER_ON:
  104. case CMD_SEND:
  105. complete(&ir->completion);
  106. break;
  107. case CMD_RX_OVERFLOW:
  108. dev_warn(ir->dev, "receive overflow\n");
  109. ir_raw_event_reset(ir->rc);
  110. break;
  111. default:
  112. dev_warn(ir->dev, "control code %02x received\n",
  113. ir->buf_in[3]);
  114. break;
  115. }
  116. } else if (len >= 7) {
  117. DEFINE_IR_RAW_EVENT(rawir);
  118. unsigned i;
  119. bool event = false;
  120. init_ir_raw_event(&rawir);
  121. for (i = 0; i < 7; i++) {
  122. if (ir->buf_in[i] == 0x80) {
  123. rawir.pulse = false;
  124. rawir.duration = US_TO_NS(21845);
  125. } else {
  126. rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
  127. rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
  128. RX_RESOLUTION;
  129. }
  130. if (ir_raw_event_store_with_filter(ir->rc, &rawir))
  131. event = true;
  132. }
  133. if (event)
  134. ir_raw_event_handle(ir->rc);
  135. }
  136. }
  137. static void iguanair_rx(struct urb *urb)
  138. {
  139. struct iguanair *ir;
  140. int rc;
  141. if (!urb)
  142. return;
  143. ir = urb->context;
  144. if (!ir) {
  145. usb_unlink_urb(urb);
  146. return;
  147. }
  148. switch (urb->status) {
  149. case 0:
  150. process_ir_data(ir, urb->actual_length);
  151. break;
  152. case -ECONNRESET:
  153. case -ENOENT:
  154. case -ESHUTDOWN:
  155. usb_unlink_urb(urb);
  156. return;
  157. case -EPIPE:
  158. default:
  159. dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
  160. break;
  161. }
  162. rc = usb_submit_urb(urb, GFP_ATOMIC);
  163. if (rc && rc != -ENODEV)
  164. dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
  165. }
  166. static void iguanair_irq_out(struct urb *urb)
  167. {
  168. struct iguanair *ir = urb->context;
  169. if (urb->status)
  170. dev_dbg(ir->dev, "Error: out urb status = %d\n", urb->status);
  171. /* if we sent an nop packet, do not expect a response */
  172. if (urb->status == 0 && ir->packet->header.cmd == CMD_NOP)
  173. complete(&ir->completion);
  174. }
  175. static int iguanair_send(struct iguanair *ir, unsigned size)
  176. {
  177. int rc;
  178. reinit_completion(&ir->completion);
  179. ir->urb_out->transfer_buffer_length = size;
  180. rc = usb_submit_urb(ir->urb_out, GFP_KERNEL);
  181. if (rc)
  182. return rc;
  183. if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
  184. return -ETIMEDOUT;
  185. return rc;
  186. }
  187. static int iguanair_get_features(struct iguanair *ir)
  188. {
  189. int rc;
  190. /*
  191. * On cold boot, the iguanair initializes on the first packet
  192. * received but does not process that packet. Send an empty
  193. * packet.
  194. */
  195. ir->packet->header.start = 0;
  196. ir->packet->header.direction = DIR_OUT;
  197. ir->packet->header.cmd = CMD_NOP;
  198. iguanair_send(ir, sizeof(ir->packet->header));
  199. ir->packet->header.cmd = CMD_GET_VERSION;
  200. rc = iguanair_send(ir, sizeof(ir->packet->header));
  201. if (rc) {
  202. dev_info(ir->dev, "failed to get version\n");
  203. goto out;
  204. }
  205. if (ir->version < 0x205) {
  206. dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
  207. rc = -ENODEV;
  208. goto out;
  209. }
  210. ir->bufsize = 150;
  211. ir->cycle_overhead = 65;
  212. ir->packet->header.cmd = CMD_GET_BUFSIZE;
  213. rc = iguanair_send(ir, sizeof(ir->packet->header));
  214. if (rc) {
  215. dev_info(ir->dev, "failed to get buffer size\n");
  216. goto out;
  217. }
  218. if (ir->bufsize > BUF_SIZE) {
  219. dev_info(ir->dev, "buffer size %u larger than expected\n",
  220. ir->bufsize);
  221. ir->bufsize = BUF_SIZE;
  222. }
  223. ir->packet->header.cmd = CMD_GET_FEATURES;
  224. rc = iguanair_send(ir, sizeof(ir->packet->header));
  225. if (rc)
  226. dev_info(ir->dev, "failed to get features\n");
  227. out:
  228. return rc;
  229. }
  230. static int iguanair_receiver(struct iguanair *ir, bool enable)
  231. {
  232. ir->packet->header.start = 0;
  233. ir->packet->header.direction = DIR_OUT;
  234. ir->packet->header.cmd = enable ? CMD_RECEIVER_ON : CMD_RECEIVER_OFF;
  235. if (enable)
  236. ir_raw_event_reset(ir->rc);
  237. return iguanair_send(ir, sizeof(ir->packet->header));
  238. }
  239. /*
  240. * The iguanair creates the carrier by busy spinning after each half period.
  241. * This is counted in CPU cycles, with the CPU running at 24MHz. It is
  242. * broken down into 7-cycles and 4-cyles delays, with a preference for
  243. * 4-cycle delays, minus the overhead of the loop itself (cycle_overhead).
  244. */
  245. static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
  246. {
  247. struct iguanair *ir = dev->priv;
  248. if (carrier < 25000 || carrier > 150000)
  249. return -EINVAL;
  250. mutex_lock(&ir->lock);
  251. if (carrier != ir->carrier) {
  252. uint32_t cycles, fours, sevens;
  253. ir->carrier = carrier;
  254. cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
  255. ir->cycle_overhead;
  256. /*
  257. * Calculate minimum number of 7 cycles needed so
  258. * we are left with a multiple of 4; so we want to have
  259. * (sevens * 7) & 3 == cycles & 3
  260. */
  261. sevens = (4 - cycles) & 3;
  262. fours = (cycles - sevens * 7) / 4;
  263. /*
  264. * The firmware interprets these values as a relative offset
  265. * for a branch. Immediately following the branches, there
  266. * 4 instructions of 7 cycles (2 bytes each) and 110
  267. * instructions of 4 cycles (1 byte each). A relative branch
  268. * of 0 will execute all of them, branch further for less
  269. * cycle burning.
  270. */
  271. ir->packet->busy7 = (4 - sevens) * 2;
  272. ir->packet->busy4 = 110 - fours;
  273. }
  274. mutex_unlock(&ir->lock);
  275. return 0;
  276. }
  277. static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
  278. {
  279. struct iguanair *ir = dev->priv;
  280. if (mask > 15)
  281. return 4;
  282. mutex_lock(&ir->lock);
  283. ir->packet->channels = mask << 4;
  284. mutex_unlock(&ir->lock);
  285. return 0;
  286. }
  287. static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
  288. {
  289. struct iguanair *ir = dev->priv;
  290. unsigned int i, size, p, periods;
  291. int rc;
  292. mutex_lock(&ir->lock);
  293. /* convert from us to carrier periods */
  294. for (i = size = 0; i < count; i++) {
  295. periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
  296. while (periods) {
  297. p = min(periods, 127u);
  298. if (size >= ir->bufsize) {
  299. rc = -EINVAL;
  300. goto out;
  301. }
  302. ir->packet->payload[size++] = p | ((i & 1) ? 0x80 : 0);
  303. periods -= p;
  304. }
  305. }
  306. ir->packet->header.start = 0;
  307. ir->packet->header.direction = DIR_OUT;
  308. ir->packet->header.cmd = CMD_SEND;
  309. ir->packet->length = size;
  310. ir->tx_overflow = false;
  311. rc = iguanair_send(ir, sizeof(*ir->packet) + size);
  312. if (rc == 0 && ir->tx_overflow)
  313. rc = -EOVERFLOW;
  314. out:
  315. mutex_unlock(&ir->lock);
  316. return rc ? rc : count;
  317. }
  318. static int iguanair_open(struct rc_dev *rdev)
  319. {
  320. struct iguanair *ir = rdev->priv;
  321. int rc;
  322. mutex_lock(&ir->lock);
  323. rc = iguanair_receiver(ir, true);
  324. if (rc == 0)
  325. ir->receiver_on = true;
  326. mutex_unlock(&ir->lock);
  327. return rc;
  328. }
  329. static void iguanair_close(struct rc_dev *rdev)
  330. {
  331. struct iguanair *ir = rdev->priv;
  332. int rc;
  333. mutex_lock(&ir->lock);
  334. rc = iguanair_receiver(ir, false);
  335. ir->receiver_on = false;
  336. if (rc && rc != -ENODEV)
  337. dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
  338. mutex_unlock(&ir->lock);
  339. }
  340. static int iguanair_probe(struct usb_interface *intf,
  341. const struct usb_device_id *id)
  342. {
  343. struct usb_device *udev = interface_to_usbdev(intf);
  344. struct iguanair *ir;
  345. struct rc_dev *rc;
  346. int ret, pipein, pipeout;
  347. struct usb_host_interface *idesc;
  348. idesc = intf->cur_altsetting;
  349. if (idesc->desc.bNumEndpoints < 2)
  350. return -ENODEV;
  351. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  352. rc = rc_allocate_device(RC_DRIVER_IR_RAW);
  353. if (!ir || !rc) {
  354. ret = -ENOMEM;
  355. goto out;
  356. }
  357. ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL,
  358. &ir->dma_in);
  359. ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL,
  360. &ir->dma_out);
  361. ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
  362. ir->urb_out = usb_alloc_urb(0, GFP_KERNEL);
  363. if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out ||
  364. !usb_endpoint_is_int_in(&idesc->endpoint[0].desc) ||
  365. !usb_endpoint_is_int_out(&idesc->endpoint[1].desc)) {
  366. ret = -ENOMEM;
  367. goto out;
  368. }
  369. ir->rc = rc;
  370. ir->dev = &intf->dev;
  371. ir->udev = udev;
  372. mutex_init(&ir->lock);
  373. init_completion(&ir->completion);
  374. pipeout = usb_sndintpipe(udev,
  375. idesc->endpoint[1].desc.bEndpointAddress);
  376. usb_fill_int_urb(ir->urb_out, udev, pipeout, ir->packet, MAX_OUT_PACKET,
  377. iguanair_irq_out, ir, 1);
  378. ir->urb_out->transfer_dma = ir->dma_out;
  379. ir->urb_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  380. pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
  381. usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET,
  382. iguanair_rx, ir, 1);
  383. ir->urb_in->transfer_dma = ir->dma_in;
  384. ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  385. ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
  386. if (ret) {
  387. dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
  388. goto out;
  389. }
  390. ret = iguanair_get_features(ir);
  391. if (ret)
  392. goto out2;
  393. snprintf(ir->name, sizeof(ir->name),
  394. "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
  395. usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
  396. rc->device_name = ir->name;
  397. rc->input_phys = ir->phys;
  398. usb_to_input_id(ir->udev, &rc->input_id);
  399. rc->dev.parent = &intf->dev;
  400. rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  401. rc->priv = ir;
  402. rc->open = iguanair_open;
  403. rc->close = iguanair_close;
  404. rc->s_tx_mask = iguanair_set_tx_mask;
  405. rc->s_tx_carrier = iguanair_set_tx_carrier;
  406. rc->tx_ir = iguanair_tx;
  407. rc->driver_name = DRIVER_NAME;
  408. rc->map_name = RC_MAP_RC6_MCE;
  409. rc->min_timeout = 1;
  410. rc->timeout = IR_DEFAULT_TIMEOUT;
  411. rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  412. rc->rx_resolution = RX_RESOLUTION;
  413. iguanair_set_tx_carrier(rc, 38000);
  414. iguanair_set_tx_mask(rc, 0);
  415. ret = rc_register_device(rc);
  416. if (ret < 0) {
  417. dev_err(&intf->dev, "failed to register rc device %d", ret);
  418. goto out2;
  419. }
  420. usb_set_intfdata(intf, ir);
  421. return 0;
  422. out2:
  423. usb_kill_urb(ir->urb_in);
  424. usb_kill_urb(ir->urb_out);
  425. out:
  426. if (ir) {
  427. usb_free_urb(ir->urb_in);
  428. usb_free_urb(ir->urb_out);
  429. usb_free_coherent(udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
  430. usb_free_coherent(udev, MAX_OUT_PACKET, ir->packet,
  431. ir->dma_out);
  432. }
  433. rc_free_device(rc);
  434. kfree(ir);
  435. return ret;
  436. }
  437. static void iguanair_disconnect(struct usb_interface *intf)
  438. {
  439. struct iguanair *ir = usb_get_intfdata(intf);
  440. rc_unregister_device(ir->rc);
  441. usb_set_intfdata(intf, NULL);
  442. usb_kill_urb(ir->urb_in);
  443. usb_kill_urb(ir->urb_out);
  444. usb_free_urb(ir->urb_in);
  445. usb_free_urb(ir->urb_out);
  446. usb_free_coherent(ir->udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
  447. usb_free_coherent(ir->udev, MAX_OUT_PACKET, ir->packet, ir->dma_out);
  448. kfree(ir);
  449. }
  450. static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
  451. {
  452. struct iguanair *ir = usb_get_intfdata(intf);
  453. int rc = 0;
  454. mutex_lock(&ir->lock);
  455. if (ir->receiver_on) {
  456. rc = iguanair_receiver(ir, false);
  457. if (rc)
  458. dev_warn(ir->dev, "failed to disable receiver for suspend\n");
  459. }
  460. usb_kill_urb(ir->urb_in);
  461. usb_kill_urb(ir->urb_out);
  462. mutex_unlock(&ir->lock);
  463. return rc;
  464. }
  465. static int iguanair_resume(struct usb_interface *intf)
  466. {
  467. struct iguanair *ir = usb_get_intfdata(intf);
  468. int rc = 0;
  469. mutex_lock(&ir->lock);
  470. rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
  471. if (rc)
  472. dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
  473. if (ir->receiver_on) {
  474. rc = iguanair_receiver(ir, true);
  475. if (rc)
  476. dev_warn(ir->dev, "failed to enable receiver after resume\n");
  477. }
  478. mutex_unlock(&ir->lock);
  479. return rc;
  480. }
  481. static const struct usb_device_id iguanair_table[] = {
  482. { USB_DEVICE(0x1781, 0x0938) },
  483. { }
  484. };
  485. static struct usb_driver iguanair_driver = {
  486. .name = DRIVER_NAME,
  487. .probe = iguanair_probe,
  488. .disconnect = iguanair_disconnect,
  489. .suspend = iguanair_suspend,
  490. .resume = iguanair_resume,
  491. .reset_resume = iguanair_resume,
  492. .id_table = iguanair_table,
  493. .soft_unbind = 1 /* we want to disable receiver on unbind */
  494. };
  495. module_usb_driver(iguanair_driver);
  496. MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
  497. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  498. MODULE_LICENSE("GPL");
  499. MODULE_DEVICE_TABLE(usb, iguanair_table);