iguanair.c 13 KB

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