iguanair.c 13 KB

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