iguanair.c 13 KB

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