ir_toy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Infrared Toy and IR Droid RC core driver
  4. *
  5. * Copyright (C) 2020 Sean Young <sean@mess.org>
  6. * This driver is based on the lirc driver which can be found here:
  7. * https://sourceforge.net/p/lirc/git/ci/master/tree/plugins/irtoy.c
  8. * Copyright (C) 2011 Peter Kooiman <pkooiman@gmail.com>
  9. */
  10. #include <asm/unaligned.h>
  11. #include <linux/completion.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/usb.h>
  15. #include <linux/slab.h>
  16. #include <linux/usb/input.h>
  17. #include <media/rc-core.h>
  18. static const u8 COMMAND_VERSION[] = { 'v' };
  19. // End transmit and repeat reset command so we exit sump mode
  20. static const u8 COMMAND_RESET[] = { 0xff, 0xff, 0, 0, 0, 0, 0 };
  21. static const u8 COMMAND_SMODE_ENTER[] = { 's' };
  22. static const u8 COMMAND_SMODE_EXIT[] = { 0 };
  23. static const u8 COMMAND_TXSTART[] = { 0x26, 0x24, 0x25, 0x03 };
  24. #define REPLY_XMITCOUNT 't'
  25. #define REPLY_XMITSUCCESS 'C'
  26. #define REPLY_VERSION 'V'
  27. #define REPLY_SAMPLEMODEPROTO 'S'
  28. #define TIMEOUT 500
  29. #define LEN_XMITRES 3
  30. #define LEN_VERSION 4
  31. #define LEN_SAMPLEMODEPROTO 3
  32. #define MIN_FW_VERSION 20
  33. #define UNIT_US 21
  34. #define MAX_TIMEOUT_US (UNIT_US * U16_MAX)
  35. #define MAX_PACKET 64
  36. enum state {
  37. STATE_IRDATA,
  38. STATE_RESET,
  39. STATE_COMMAND,
  40. STATE_TX,
  41. };
  42. struct irtoy {
  43. struct device *dev;
  44. struct usb_device *usbdev;
  45. struct rc_dev *rc;
  46. struct urb *urb_in, *urb_out;
  47. u8 *in;
  48. u8 *out;
  49. struct completion command_done;
  50. bool pulse;
  51. enum state state;
  52. void *tx_buf;
  53. uint tx_len;
  54. uint emitted;
  55. uint hw_version;
  56. uint sw_version;
  57. uint proto_version;
  58. char phys[64];
  59. };
  60. static void irtoy_response(struct irtoy *irtoy, u32 len)
  61. {
  62. switch (irtoy->state) {
  63. case STATE_COMMAND:
  64. if (len == LEN_VERSION && irtoy->in[0] == REPLY_VERSION) {
  65. uint version;
  66. irtoy->in[LEN_VERSION] = 0;
  67. if (kstrtouint(irtoy->in + 1, 10, &version)) {
  68. dev_err(irtoy->dev, "invalid version %*phN. Please make sure you are using firmware v20 or higher",
  69. LEN_VERSION, irtoy->in);
  70. break;
  71. }
  72. dev_dbg(irtoy->dev, "version %s\n", irtoy->in);
  73. irtoy->hw_version = version / 100;
  74. irtoy->sw_version = version % 100;
  75. irtoy->state = STATE_IRDATA;
  76. complete(&irtoy->command_done);
  77. } else if (len == LEN_SAMPLEMODEPROTO &&
  78. irtoy->in[0] == REPLY_SAMPLEMODEPROTO) {
  79. uint version;
  80. irtoy->in[LEN_SAMPLEMODEPROTO] = 0;
  81. if (kstrtouint(irtoy->in + 1, 10, &version)) {
  82. dev_err(irtoy->dev, "invalid sample mode response %*phN",
  83. LEN_SAMPLEMODEPROTO, irtoy->in);
  84. return;
  85. }
  86. dev_dbg(irtoy->dev, "protocol %s\n", irtoy->in);
  87. irtoy->proto_version = version;
  88. irtoy->state = STATE_IRDATA;
  89. complete(&irtoy->command_done);
  90. } else {
  91. dev_err(irtoy->dev, "unexpected response to command: %*phN\n",
  92. len, irtoy->in);
  93. }
  94. break;
  95. case STATE_IRDATA: {
  96. struct ir_raw_event rawir = { .pulse = irtoy->pulse };
  97. __be16 *in = (__be16 *)irtoy->in;
  98. int i;
  99. for (i = 0; i < len / sizeof(__be16); i++) {
  100. u16 v = be16_to_cpu(in[i]);
  101. if (v == 0xffff) {
  102. rawir.pulse = false;
  103. } else {
  104. rawir.duration = v * UNIT_US;
  105. ir_raw_event_store_with_timeout(irtoy->rc,
  106. &rawir);
  107. }
  108. rawir.pulse = !rawir.pulse;
  109. }
  110. irtoy->pulse = rawir.pulse;
  111. ir_raw_event_handle(irtoy->rc);
  112. break;
  113. }
  114. case STATE_TX:
  115. if (irtoy->tx_len == 0) {
  116. if (len == LEN_XMITRES &&
  117. irtoy->in[0] == REPLY_XMITCOUNT) {
  118. u16 emitted = get_unaligned_be16(irtoy->in + 1);
  119. dev_dbg(irtoy->dev, "emitted:%u\n", emitted);
  120. irtoy->emitted = emitted;
  121. } else if (len == 1 &&
  122. irtoy->in[0] == REPLY_XMITSUCCESS) {
  123. irtoy->state = STATE_IRDATA;
  124. complete(&irtoy->command_done);
  125. }
  126. } else {
  127. // send next part of tx buffer
  128. uint space = irtoy->in[0];
  129. uint buf_len;
  130. int err;
  131. if (len != 1 || space > MAX_PACKET || space == 0) {
  132. dev_err(irtoy->dev, "packet length expected: %*phN\n",
  133. len, irtoy->in);
  134. irtoy->state = STATE_IRDATA;
  135. complete(&irtoy->command_done);
  136. break;
  137. }
  138. buf_len = min(space, irtoy->tx_len);
  139. dev_dbg(irtoy->dev, "remaining:%u sending:%u\n",
  140. irtoy->tx_len, buf_len);
  141. memcpy(irtoy->out, irtoy->tx_buf, buf_len);
  142. irtoy->urb_out->transfer_buffer_length = buf_len;
  143. err = usb_submit_urb(irtoy->urb_out, GFP_ATOMIC);
  144. if (err != 0) {
  145. dev_err(irtoy->dev, "fail to submit tx buf urb: %d\n",
  146. err);
  147. irtoy->state = STATE_IRDATA;
  148. complete(&irtoy->command_done);
  149. break;
  150. }
  151. irtoy->tx_buf += buf_len;
  152. irtoy->tx_len -= buf_len;
  153. }
  154. break;
  155. case STATE_RESET:
  156. dev_err(irtoy->dev, "unexpected response to reset: %*phN\n",
  157. len, irtoy->in);
  158. }
  159. }
  160. static void irtoy_out_callback(struct urb *urb)
  161. {
  162. struct irtoy *irtoy = urb->context;
  163. if (urb->status == 0) {
  164. if (irtoy->state == STATE_RESET)
  165. complete(&irtoy->command_done);
  166. } else {
  167. dev_warn(irtoy->dev, "out urb status: %d\n", urb->status);
  168. }
  169. }
  170. static void irtoy_in_callback(struct urb *urb)
  171. {
  172. struct irtoy *irtoy = urb->context;
  173. int ret;
  174. if (urb->status == 0)
  175. irtoy_response(irtoy, urb->actual_length);
  176. else
  177. dev_dbg(irtoy->dev, "in urb status: %d\n", urb->status);
  178. ret = usb_submit_urb(urb, GFP_ATOMIC);
  179. if (ret && ret != -ENODEV)
  180. dev_warn(irtoy->dev, "failed to resubmit urb: %d\n", ret);
  181. }
  182. static int irtoy_command(struct irtoy *irtoy, const u8 *cmd, int cmd_len,
  183. enum state state)
  184. {
  185. int err;
  186. init_completion(&irtoy->command_done);
  187. irtoy->state = state;
  188. memcpy(irtoy->out, cmd, cmd_len);
  189. irtoy->urb_out->transfer_buffer_length = cmd_len;
  190. err = usb_submit_urb(irtoy->urb_out, GFP_KERNEL);
  191. if (err != 0)
  192. return err;
  193. if (!wait_for_completion_timeout(&irtoy->command_done,
  194. msecs_to_jiffies(TIMEOUT))) {
  195. usb_kill_urb(irtoy->urb_out);
  196. return -ETIMEDOUT;
  197. }
  198. return 0;
  199. }
  200. static int irtoy_setup(struct irtoy *irtoy)
  201. {
  202. int err;
  203. err = irtoy_command(irtoy, COMMAND_RESET, sizeof(COMMAND_RESET),
  204. STATE_RESET);
  205. if (err != 0) {
  206. dev_err(irtoy->dev, "could not write reset command: %d\n",
  207. err);
  208. return err;
  209. }
  210. usleep_range(50, 50);
  211. // get version
  212. err = irtoy_command(irtoy, COMMAND_VERSION, sizeof(COMMAND_VERSION),
  213. STATE_COMMAND);
  214. if (err) {
  215. dev_err(irtoy->dev, "could not write version command: %d\n",
  216. err);
  217. return err;
  218. }
  219. // enter sample mode
  220. err = irtoy_command(irtoy, COMMAND_SMODE_ENTER,
  221. sizeof(COMMAND_SMODE_ENTER), STATE_COMMAND);
  222. if (err)
  223. dev_err(irtoy->dev, "could not write sample command: %d\n",
  224. err);
  225. return err;
  226. }
  227. /*
  228. * When sending IR, it is imperative that we send the IR data as quickly
  229. * as possible to the device, so it does not run out of IR data and
  230. * introduce gaps. Allocate the buffer here, and then feed the data from
  231. * the urb callback handler.
  232. */
  233. static int irtoy_tx(struct rc_dev *rc, uint *txbuf, uint count)
  234. {
  235. struct irtoy *irtoy = rc->priv;
  236. unsigned int i, size;
  237. __be16 *buf;
  238. int err;
  239. size = sizeof(u16) * (count + 1);
  240. buf = kmalloc(size, GFP_KERNEL);
  241. if (!buf)
  242. return -ENOMEM;
  243. for (i = 0; i < count; i++) {
  244. u16 v = DIV_ROUND_CLOSEST(txbuf[i], UNIT_US);
  245. if (!v)
  246. v = 1;
  247. buf[i] = cpu_to_be16(v);
  248. }
  249. buf[count] = cpu_to_be16(0xffff);
  250. irtoy->tx_buf = buf;
  251. irtoy->tx_len = size;
  252. irtoy->emitted = 0;
  253. // There is an issue where if the unit is receiving IR while the
  254. // first TXSTART command is sent, the device might end up hanging
  255. // with its led on. It does not respond to any command when this
  256. // happens. To work around this, re-enter sample mode.
  257. err = irtoy_command(irtoy, COMMAND_SMODE_EXIT,
  258. sizeof(COMMAND_SMODE_EXIT), STATE_RESET);
  259. if (err) {
  260. dev_err(irtoy->dev, "exit sample mode: %d\n", err);
  261. return err;
  262. }
  263. err = irtoy_command(irtoy, COMMAND_SMODE_ENTER,
  264. sizeof(COMMAND_SMODE_ENTER), STATE_COMMAND);
  265. if (err) {
  266. dev_err(irtoy->dev, "enter sample mode: %d\n", err);
  267. return err;
  268. }
  269. err = irtoy_command(irtoy, COMMAND_TXSTART, sizeof(COMMAND_TXSTART),
  270. STATE_TX);
  271. kfree(buf);
  272. if (err) {
  273. dev_err(irtoy->dev, "failed to send tx start command: %d\n",
  274. err);
  275. // not sure what state the device is in, reset it
  276. irtoy_setup(irtoy);
  277. return err;
  278. }
  279. if (size != irtoy->emitted) {
  280. dev_err(irtoy->dev, "expected %u emitted, got %u\n", size,
  281. irtoy->emitted);
  282. // not sure what state the device is in, reset it
  283. irtoy_setup(irtoy);
  284. return -EINVAL;
  285. }
  286. return count;
  287. }
  288. static int irtoy_probe(struct usb_interface *intf,
  289. const struct usb_device_id *id)
  290. {
  291. struct usb_host_interface *idesc = intf->cur_altsetting;
  292. struct usb_device *usbdev = interface_to_usbdev(intf);
  293. struct usb_endpoint_descriptor *ep_in = NULL;
  294. struct usb_endpoint_descriptor *ep_out = NULL;
  295. struct usb_endpoint_descriptor *ep = NULL;
  296. struct irtoy *irtoy;
  297. struct rc_dev *rc;
  298. struct urb *urb;
  299. int i, pipe, err = -ENOMEM;
  300. for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
  301. ep = &idesc->endpoint[i].desc;
  302. if (!ep_in && usb_endpoint_is_bulk_in(ep) &&
  303. usb_endpoint_maxp(ep) == MAX_PACKET)
  304. ep_in = ep;
  305. if (!ep_out && usb_endpoint_is_bulk_out(ep) &&
  306. usb_endpoint_maxp(ep) == MAX_PACKET)
  307. ep_out = ep;
  308. }
  309. if (!ep_in || !ep_out) {
  310. dev_err(&intf->dev, "required endpoints not found\n");
  311. return -ENODEV;
  312. }
  313. irtoy = kzalloc(sizeof(*irtoy), GFP_KERNEL);
  314. if (!irtoy)
  315. return -ENOMEM;
  316. irtoy->in = kmalloc(MAX_PACKET, GFP_KERNEL);
  317. if (!irtoy->in)
  318. goto free_irtoy;
  319. irtoy->out = kmalloc(MAX_PACKET, GFP_KERNEL);
  320. if (!irtoy->out)
  321. goto free_irtoy;
  322. rc = rc_allocate_device(RC_DRIVER_IR_RAW);
  323. if (!rc)
  324. goto free_irtoy;
  325. urb = usb_alloc_urb(0, GFP_KERNEL);
  326. if (!urb)
  327. goto free_rcdev;
  328. pipe = usb_rcvbulkpipe(usbdev, ep_in->bEndpointAddress);
  329. usb_fill_bulk_urb(urb, usbdev, pipe, irtoy->in, MAX_PACKET,
  330. irtoy_in_callback, irtoy);
  331. irtoy->urb_in = urb;
  332. urb = usb_alloc_urb(0, GFP_KERNEL);
  333. if (!urb)
  334. goto free_rcdev;
  335. pipe = usb_sndbulkpipe(usbdev, ep_out->bEndpointAddress);
  336. usb_fill_bulk_urb(urb, usbdev, pipe, irtoy->out, MAX_PACKET,
  337. irtoy_out_callback, irtoy);
  338. irtoy->dev = &intf->dev;
  339. irtoy->usbdev = usbdev;
  340. irtoy->rc = rc;
  341. irtoy->urb_out = urb;
  342. irtoy->pulse = true;
  343. err = usb_submit_urb(irtoy->urb_in, GFP_KERNEL);
  344. if (err != 0) {
  345. dev_err(irtoy->dev, "fail to submit in urb: %d\n", err);
  346. goto free_rcdev;
  347. }
  348. err = irtoy_setup(irtoy);
  349. if (err)
  350. goto free_rcdev;
  351. dev_info(irtoy->dev, "version: hardware %u, firmware %u, protocol %u",
  352. irtoy->hw_version, irtoy->sw_version, irtoy->proto_version);
  353. if (irtoy->sw_version < MIN_FW_VERSION) {
  354. dev_err(irtoy->dev, "need firmware V%02u or higher",
  355. MIN_FW_VERSION);
  356. err = -ENODEV;
  357. goto free_rcdev;
  358. }
  359. usb_make_path(usbdev, irtoy->phys, sizeof(irtoy->phys));
  360. rc->device_name = "Infrared Toy";
  361. rc->driver_name = KBUILD_MODNAME;
  362. rc->input_phys = irtoy->phys;
  363. usb_to_input_id(usbdev, &rc->input_id);
  364. rc->dev.parent = &intf->dev;
  365. rc->priv = irtoy;
  366. rc->tx_ir = irtoy_tx;
  367. rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  368. rc->map_name = RC_MAP_RC6_MCE;
  369. rc->rx_resolution = UNIT_US;
  370. rc->timeout = IR_DEFAULT_TIMEOUT;
  371. /*
  372. * end of transmission is detected by absence of a usb packet
  373. * with more pulse/spaces. However, each usb packet sent can
  374. * contain 32 pulse/spaces, which can be quite lengthy, so there
  375. * can be a delay between usb packets. For example with nec there is a
  376. * 17ms gap between packets.
  377. *
  378. * So, make timeout a largish minimum which works with most protocols.
  379. */
  380. rc->min_timeout = MS_TO_US(40);
  381. rc->max_timeout = MAX_TIMEOUT_US;
  382. err = rc_register_device(rc);
  383. if (err)
  384. goto free_rcdev;
  385. usb_set_intfdata(intf, irtoy);
  386. return 0;
  387. free_rcdev:
  388. usb_kill_urb(irtoy->urb_out);
  389. usb_free_urb(irtoy->urb_out);
  390. usb_kill_urb(irtoy->urb_in);
  391. usb_free_urb(irtoy->urb_in);
  392. rc_free_device(rc);
  393. free_irtoy:
  394. kfree(irtoy->in);
  395. kfree(irtoy->out);
  396. kfree(irtoy);
  397. return err;
  398. }
  399. static void irtoy_disconnect(struct usb_interface *intf)
  400. {
  401. struct irtoy *ir = usb_get_intfdata(intf);
  402. rc_unregister_device(ir->rc);
  403. usb_set_intfdata(intf, NULL);
  404. usb_kill_urb(ir->urb_out);
  405. usb_free_urb(ir->urb_out);
  406. usb_kill_urb(ir->urb_in);
  407. usb_free_urb(ir->urb_in);
  408. kfree(ir->in);
  409. kfree(ir->out);
  410. kfree(ir);
  411. }
  412. static const struct usb_device_id irtoy_table[] = {
  413. { USB_DEVICE_INTERFACE_CLASS(0x04d8, 0xfd08, USB_CLASS_CDC_DATA) },
  414. { USB_DEVICE_INTERFACE_CLASS(0x04d8, 0xf58b, USB_CLASS_CDC_DATA) },
  415. { }
  416. };
  417. static struct usb_driver irtoy_driver = {
  418. .name = KBUILD_MODNAME,
  419. .probe = irtoy_probe,
  420. .disconnect = irtoy_disconnect,
  421. .id_table = irtoy_table,
  422. };
  423. module_usb_driver(irtoy_driver);
  424. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  425. MODULE_DESCRIPTION("Infrared Toy and IR Droid driver");
  426. MODULE_LICENSE("GPL");
  427. MODULE_DEVICE_TABLE(usb, irtoy_table);