iguanair.c 13 KB

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