iguanair.c 14 KB

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