ir_toy.c 13 KB

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