iguanair.c 14 KB

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