serial_ir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * serial_ir.c
  3. *
  4. * serial_ir - Device driver that records pulse- and pause-lengths
  5. * (space-lengths) between DDCD event on a serial port.
  6. *
  7. * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
  8. * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
  9. * Copyright (C) 1998 Ben Pfaff <blp@gnu.org>
  10. * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
  11. * Copyright (C) 2007 Andrei Tanas <andrei@tanas.ca> (suspend/resume support)
  12. * Copyright (C) 2016 Sean Young <sean@mess.org> (port to rc-core)
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/module.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/kernel.h>
  28. #include <linux/serial_reg.h>
  29. #include <linux/types.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/spinlock.h>
  33. #include <media/rc-core.h>
  34. struct serial_ir_hw {
  35. int signal_pin;
  36. int signal_pin_change;
  37. u8 on;
  38. u8 off;
  39. unsigned set_send_carrier:1;
  40. unsigned set_duty_cycle:1;
  41. void (*send_pulse)(unsigned int length, ktime_t edge);
  42. void (*send_space)(void);
  43. spinlock_t lock;
  44. };
  45. #define IR_HOMEBREW 0
  46. #define IR_IRDEO 1
  47. #define IR_IRDEO_REMOTE 2
  48. #define IR_ANIMAX 3
  49. #define IR_IGOR 4
  50. /* module parameters */
  51. static int type;
  52. static int io;
  53. static int irq;
  54. static ulong iommap;
  55. static int ioshift;
  56. static bool softcarrier = true;
  57. static bool share_irq;
  58. static int sense = -1; /* -1 = auto, 0 = active high, 1 = active low */
  59. static bool txsense; /* 0 = active high, 1 = active low */
  60. /* forward declarations */
  61. static void send_pulse_irdeo(unsigned int length, ktime_t edge);
  62. static void send_space_irdeo(void);
  63. #ifdef CONFIG_IR_SERIAL_TRANSMITTER
  64. static void send_pulse_homebrew(unsigned int length, ktime_t edge);
  65. static void send_space_homebrew(void);
  66. #endif
  67. static struct serial_ir_hw hardware[] = {
  68. [IR_HOMEBREW] = {
  69. .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_HOMEBREW].lock),
  70. .signal_pin = UART_MSR_DCD,
  71. .signal_pin_change = UART_MSR_DDCD,
  72. .on = (UART_MCR_RTS | UART_MCR_OUT2 | UART_MCR_DTR),
  73. .off = (UART_MCR_RTS | UART_MCR_OUT2),
  74. #ifdef CONFIG_IR_SERIAL_TRANSMITTER
  75. .send_pulse = send_pulse_homebrew,
  76. .send_space = send_space_homebrew,
  77. .set_send_carrier = true,
  78. .set_duty_cycle = true,
  79. #endif
  80. },
  81. [IR_IRDEO] = {
  82. .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IRDEO].lock),
  83. .signal_pin = UART_MSR_DSR,
  84. .signal_pin_change = UART_MSR_DDSR,
  85. .on = UART_MCR_OUT2,
  86. .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
  87. .send_pulse = send_pulse_irdeo,
  88. .send_space = send_space_irdeo,
  89. .set_duty_cycle = true,
  90. },
  91. [IR_IRDEO_REMOTE] = {
  92. .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IRDEO_REMOTE].lock),
  93. .signal_pin = UART_MSR_DSR,
  94. .signal_pin_change = UART_MSR_DDSR,
  95. .on = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
  96. .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
  97. .send_pulse = send_pulse_irdeo,
  98. .send_space = send_space_irdeo,
  99. .set_duty_cycle = true,
  100. },
  101. [IR_ANIMAX] = {
  102. .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_ANIMAX].lock),
  103. .signal_pin = UART_MSR_DCD,
  104. .signal_pin_change = UART_MSR_DDCD,
  105. .on = 0,
  106. .off = (UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2),
  107. },
  108. [IR_IGOR] = {
  109. .lock = __SPIN_LOCK_UNLOCKED(hardware[IR_IGOR].lock),
  110. .signal_pin = UART_MSR_DSR,
  111. .signal_pin_change = UART_MSR_DDSR,
  112. .on = (UART_MCR_RTS | UART_MCR_OUT2 | UART_MCR_DTR),
  113. .off = (UART_MCR_RTS | UART_MCR_OUT2),
  114. #ifdef CONFIG_IR_SERIAL_TRANSMITTER
  115. .send_pulse = send_pulse_homebrew,
  116. .send_space = send_space_homebrew,
  117. .set_send_carrier = true,
  118. .set_duty_cycle = true,
  119. #endif
  120. },
  121. };
  122. #define RS_ISR_PASS_LIMIT 256
  123. struct serial_ir {
  124. ktime_t lastkt;
  125. struct rc_dev *rcdev;
  126. struct platform_device *pdev;
  127. struct timer_list timeout_timer;
  128. unsigned int carrier;
  129. unsigned int duty_cycle;
  130. };
  131. static struct serial_ir serial_ir;
  132. /* fetch serial input packet (1 byte) from register offset */
  133. static u8 sinp(int offset)
  134. {
  135. if (iommap)
  136. /* the register is memory-mapped */
  137. offset <<= ioshift;
  138. return inb(io + offset);
  139. }
  140. /* write serial output packet (1 byte) of value to register offset */
  141. static void soutp(int offset, u8 value)
  142. {
  143. if (iommap)
  144. /* the register is memory-mapped */
  145. offset <<= ioshift;
  146. outb(value, io + offset);
  147. }
  148. static void on(void)
  149. {
  150. if (txsense)
  151. soutp(UART_MCR, hardware[type].off);
  152. else
  153. soutp(UART_MCR, hardware[type].on);
  154. }
  155. static void off(void)
  156. {
  157. if (txsense)
  158. soutp(UART_MCR, hardware[type].on);
  159. else
  160. soutp(UART_MCR, hardware[type].off);
  161. }
  162. static void send_pulse_irdeo(unsigned int length, ktime_t target)
  163. {
  164. long rawbits;
  165. int i;
  166. unsigned char output;
  167. unsigned char chunk, shifted;
  168. /* how many bits have to be sent ? */
  169. rawbits = length * 1152 / 10000;
  170. if (serial_ir.duty_cycle > 50)
  171. chunk = 3;
  172. else
  173. chunk = 1;
  174. for (i = 0, output = 0x7f; rawbits > 0; rawbits -= 3) {
  175. shifted = chunk << (i * 3);
  176. shifted >>= 1;
  177. output &= (~shifted);
  178. i++;
  179. if (i == 3) {
  180. soutp(UART_TX, output);
  181. while (!(sinp(UART_LSR) & UART_LSR_THRE))
  182. ;
  183. output = 0x7f;
  184. i = 0;
  185. }
  186. }
  187. if (i != 0) {
  188. soutp(UART_TX, output);
  189. while (!(sinp(UART_LSR) & UART_LSR_TEMT))
  190. ;
  191. }
  192. }
  193. static void send_space_irdeo(void)
  194. {
  195. }
  196. #ifdef CONFIG_IR_SERIAL_TRANSMITTER
  197. static void send_pulse_homebrew_softcarrier(unsigned int length, ktime_t edge)
  198. {
  199. ktime_t now, target = ktime_add_us(edge, length);
  200. /*
  201. * delta should never exceed 4 seconds and on m68k
  202. * ndelay(s64) does not compile; so use s32 rather than s64.
  203. */
  204. s32 delta;
  205. unsigned int pulse, space;
  206. /* Ensure the dividend fits into 32 bit */
  207. pulse = DIV_ROUND_CLOSEST(serial_ir.duty_cycle * (NSEC_PER_SEC / 100),
  208. serial_ir.carrier);
  209. space = DIV_ROUND_CLOSEST((100 - serial_ir.duty_cycle) *
  210. (NSEC_PER_SEC / 100), serial_ir.carrier);
  211. for (;;) {
  212. now = ktime_get();
  213. if (ktime_compare(now, target) >= 0)
  214. break;
  215. on();
  216. edge = ktime_add_ns(edge, pulse);
  217. delta = ktime_to_ns(ktime_sub(edge, now));
  218. if (delta > 0)
  219. ndelay(delta);
  220. now = ktime_get();
  221. off();
  222. if (ktime_compare(now, target) >= 0)
  223. break;
  224. edge = ktime_add_ns(edge, space);
  225. delta = ktime_to_ns(ktime_sub(edge, now));
  226. if (delta > 0)
  227. ndelay(delta);
  228. }
  229. }
  230. static void send_pulse_homebrew(unsigned int length, ktime_t edge)
  231. {
  232. if (softcarrier)
  233. send_pulse_homebrew_softcarrier(length, edge);
  234. else
  235. on();
  236. }
  237. static void send_space_homebrew(void)
  238. {
  239. off();
  240. }
  241. #endif
  242. static void frbwrite(unsigned int l, bool is_pulse)
  243. {
  244. /* simple noise filter */
  245. static unsigned int ptr, pulse, space;
  246. DEFINE_IR_RAW_EVENT(ev);
  247. if (ptr > 0 && is_pulse) {
  248. pulse += l;
  249. if (pulse > 250000) {
  250. ev.duration = space;
  251. ev.pulse = false;
  252. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  253. ev.duration = pulse;
  254. ev.pulse = true;
  255. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  256. ptr = 0;
  257. pulse = 0;
  258. }
  259. return;
  260. }
  261. if (!is_pulse) {
  262. if (ptr == 0) {
  263. if (l > 20000000) {
  264. space = l;
  265. ptr++;
  266. return;
  267. }
  268. } else {
  269. if (l > 20000000) {
  270. space += pulse;
  271. if (space > IR_MAX_DURATION)
  272. space = IR_MAX_DURATION;
  273. space += l;
  274. if (space > IR_MAX_DURATION)
  275. space = IR_MAX_DURATION;
  276. pulse = 0;
  277. return;
  278. }
  279. ev.duration = space;
  280. ev.pulse = false;
  281. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  282. ev.duration = pulse;
  283. ev.pulse = true;
  284. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  285. ptr = 0;
  286. pulse = 0;
  287. }
  288. }
  289. ev.duration = l;
  290. ev.pulse = is_pulse;
  291. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  292. }
  293. static irqreturn_t serial_ir_irq_handler(int i, void *blah)
  294. {
  295. ktime_t kt;
  296. int counter, dcd;
  297. u8 status;
  298. ktime_t delkt;
  299. unsigned int data;
  300. static int last_dcd = -1;
  301. if ((sinp(UART_IIR) & UART_IIR_NO_INT)) {
  302. /* not our interrupt */
  303. return IRQ_NONE;
  304. }
  305. counter = 0;
  306. do {
  307. counter++;
  308. status = sinp(UART_MSR);
  309. if (counter > RS_ISR_PASS_LIMIT) {
  310. dev_err(&serial_ir.pdev->dev, "Trapped in interrupt");
  311. break;
  312. }
  313. if ((status & hardware[type].signal_pin_change) &&
  314. sense != -1) {
  315. /* get current time */
  316. kt = ktime_get();
  317. /*
  318. * The driver needs to know if your receiver is
  319. * active high or active low, or the space/pulse
  320. * sense could be inverted.
  321. */
  322. /* calc time since last interrupt in nanoseconds */
  323. dcd = (status & hardware[type].signal_pin) ? 1 : 0;
  324. if (dcd == last_dcd) {
  325. dev_err(&serial_ir.pdev->dev,
  326. "ignoring spike: %d %d %lldns %lldns\n",
  327. dcd, sense, ktime_to_ns(kt),
  328. ktime_to_ns(serial_ir.lastkt));
  329. continue;
  330. }
  331. delkt = ktime_sub(kt, serial_ir.lastkt);
  332. if (ktime_compare(delkt, ktime_set(15, 0)) > 0) {
  333. data = IR_MAX_DURATION; /* really long time */
  334. if (!(dcd ^ sense)) {
  335. /* sanity check */
  336. dev_err(&serial_ir.pdev->dev,
  337. "dcd unexpected: %d %d %lldns %lldns\n",
  338. dcd, sense, ktime_to_ns(kt),
  339. ktime_to_ns(serial_ir.lastkt));
  340. /*
  341. * detecting pulse while this
  342. * MUST be a space!
  343. */
  344. sense = sense ? 0 : 1;
  345. }
  346. } else {
  347. data = ktime_to_ns(delkt);
  348. }
  349. frbwrite(data, !(dcd ^ sense));
  350. serial_ir.lastkt = kt;
  351. last_dcd = dcd;
  352. }
  353. } while (!(sinp(UART_IIR) & UART_IIR_NO_INT)); /* still pending ? */
  354. mod_timer(&serial_ir.timeout_timer,
  355. jiffies + nsecs_to_jiffies(serial_ir.rcdev->timeout));
  356. ir_raw_event_handle(serial_ir.rcdev);
  357. return IRQ_HANDLED;
  358. }
  359. static int hardware_init_port(void)
  360. {
  361. u8 scratch, scratch2, scratch3;
  362. /*
  363. * This is a simple port existence test, borrowed from the autoconfig
  364. * function in drivers/tty/serial/8250/8250_port.c
  365. */
  366. scratch = sinp(UART_IER);
  367. soutp(UART_IER, 0);
  368. #ifdef __i386__
  369. outb(0xff, 0x080);
  370. #endif
  371. scratch2 = sinp(UART_IER) & 0x0f;
  372. soutp(UART_IER, 0x0f);
  373. #ifdef __i386__
  374. outb(0x00, 0x080);
  375. #endif
  376. scratch3 = sinp(UART_IER) & 0x0f;
  377. soutp(UART_IER, scratch);
  378. if (scratch2 != 0 || scratch3 != 0x0f) {
  379. /* we fail, there's nothing here */
  380. pr_err("port existence test failed, cannot continue\n");
  381. return -ENODEV;
  382. }
  383. /* Set DLAB 0. */
  384. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  385. /* First of all, disable all interrupts */
  386. soutp(UART_IER, sinp(UART_IER) &
  387. (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
  388. /* Clear registers. */
  389. sinp(UART_LSR);
  390. sinp(UART_RX);
  391. sinp(UART_IIR);
  392. sinp(UART_MSR);
  393. /* Set line for power source */
  394. off();
  395. /* Clear registers again to be sure. */
  396. sinp(UART_LSR);
  397. sinp(UART_RX);
  398. sinp(UART_IIR);
  399. sinp(UART_MSR);
  400. switch (type) {
  401. case IR_IRDEO:
  402. case IR_IRDEO_REMOTE:
  403. /* setup port to 7N1 @ 115200 Baud */
  404. /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
  405. /* Set DLAB 1. */
  406. soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
  407. /* Set divisor to 1 => 115200 Baud */
  408. soutp(UART_DLM, 0);
  409. soutp(UART_DLL, 1);
  410. /* Set DLAB 0 + 7N1 */
  411. soutp(UART_LCR, UART_LCR_WLEN7);
  412. /* THR interrupt already disabled at this point */
  413. break;
  414. default:
  415. break;
  416. }
  417. return 0;
  418. }
  419. static void serial_ir_timeout(struct timer_list *unused)
  420. {
  421. DEFINE_IR_RAW_EVENT(ev);
  422. ev.timeout = true;
  423. ev.duration = serial_ir.rcdev->timeout;
  424. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  425. ir_raw_event_handle(serial_ir.rcdev);
  426. }
  427. /* Needed by serial_ir_probe() */
  428. static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
  429. unsigned int count);
  430. static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle);
  431. static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier);
  432. static int serial_ir_open(struct rc_dev *rcdev);
  433. static void serial_ir_close(struct rc_dev *rcdev);
  434. static int serial_ir_probe(struct platform_device *dev)
  435. {
  436. struct rc_dev *rcdev;
  437. int i, nlow, nhigh, result;
  438. rcdev = devm_rc_allocate_device(&dev->dev, RC_DRIVER_IR_RAW);
  439. if (!rcdev)
  440. return -ENOMEM;
  441. if (hardware[type].send_pulse && hardware[type].send_space)
  442. rcdev->tx_ir = serial_ir_tx;
  443. if (hardware[type].set_send_carrier)
  444. rcdev->s_tx_carrier = serial_ir_tx_carrier;
  445. if (hardware[type].set_duty_cycle)
  446. rcdev->s_tx_duty_cycle = serial_ir_tx_duty_cycle;
  447. switch (type) {
  448. case IR_HOMEBREW:
  449. rcdev->device_name = "Serial IR type home-brew";
  450. break;
  451. case IR_IRDEO:
  452. rcdev->device_name = "Serial IR type IRdeo";
  453. break;
  454. case IR_IRDEO_REMOTE:
  455. rcdev->device_name = "Serial IR type IRdeo remote";
  456. break;
  457. case IR_ANIMAX:
  458. rcdev->device_name = "Serial IR type AnimaX";
  459. break;
  460. case IR_IGOR:
  461. rcdev->device_name = "Serial IR type IgorPlug";
  462. break;
  463. }
  464. rcdev->input_phys = KBUILD_MODNAME "/input0";
  465. rcdev->input_id.bustype = BUS_HOST;
  466. rcdev->input_id.vendor = 0x0001;
  467. rcdev->input_id.product = 0x0001;
  468. rcdev->input_id.version = 0x0100;
  469. rcdev->open = serial_ir_open;
  470. rcdev->close = serial_ir_close;
  471. rcdev->dev.parent = &serial_ir.pdev->dev;
  472. rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  473. rcdev->driver_name = KBUILD_MODNAME;
  474. rcdev->map_name = RC_MAP_RC6_MCE;
  475. rcdev->min_timeout = 1;
  476. rcdev->timeout = IR_DEFAULT_TIMEOUT;
  477. rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  478. rcdev->rx_resolution = 250000;
  479. serial_ir.rcdev = rcdev;
  480. timer_setup(&serial_ir.timeout_timer, serial_ir_timeout, 0);
  481. result = devm_request_irq(&dev->dev, irq, serial_ir_irq_handler,
  482. share_irq ? IRQF_SHARED : 0,
  483. KBUILD_MODNAME, &hardware);
  484. if (result < 0) {
  485. if (result == -EBUSY)
  486. dev_err(&dev->dev, "IRQ %d busy\n", irq);
  487. else if (result == -EINVAL)
  488. dev_err(&dev->dev, "Bad irq number or handler\n");
  489. return result;
  490. }
  491. /* Reserve io region. */
  492. if ((iommap &&
  493. (devm_request_mem_region(&dev->dev, iommap, 8 << ioshift,
  494. KBUILD_MODNAME) == NULL)) ||
  495. (!iommap && (devm_request_region(&dev->dev, io, 8,
  496. KBUILD_MODNAME) == NULL))) {
  497. dev_err(&dev->dev, "port %04x already in use\n", io);
  498. dev_warn(&dev->dev, "use 'setserial /dev/ttySX uart none'\n");
  499. dev_warn(&dev->dev,
  500. "or compile the serial port driver as module and\n");
  501. dev_warn(&dev->dev, "make sure this module is loaded first\n");
  502. return -EBUSY;
  503. }
  504. result = hardware_init_port();
  505. if (result < 0)
  506. return result;
  507. /* Initialize pulse/space widths */
  508. serial_ir.duty_cycle = 50;
  509. serial_ir.carrier = 38000;
  510. /* If pin is high, then this must be an active low receiver. */
  511. if (sense == -1) {
  512. /* wait 1/2 sec for the power supply */
  513. msleep(500);
  514. /*
  515. * probe 9 times every 0.04s, collect "votes" for
  516. * active high/low
  517. */
  518. nlow = 0;
  519. nhigh = 0;
  520. for (i = 0; i < 9; i++) {
  521. if (sinp(UART_MSR) & hardware[type].signal_pin)
  522. nlow++;
  523. else
  524. nhigh++;
  525. msleep(40);
  526. }
  527. sense = nlow >= nhigh ? 1 : 0;
  528. dev_info(&dev->dev, "auto-detected active %s receiver\n",
  529. sense ? "low" : "high");
  530. } else
  531. dev_info(&dev->dev, "Manually using active %s receiver\n",
  532. sense ? "low" : "high");
  533. dev_dbg(&dev->dev, "Interrupt %d, port %04x obtained\n", irq, io);
  534. return devm_rc_register_device(&dev->dev, rcdev);
  535. }
  536. static int serial_ir_open(struct rc_dev *rcdev)
  537. {
  538. unsigned long flags;
  539. /* initialize timestamp */
  540. serial_ir.lastkt = ktime_get();
  541. spin_lock_irqsave(&hardware[type].lock, flags);
  542. /* Set DLAB 0. */
  543. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  544. soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
  545. spin_unlock_irqrestore(&hardware[type].lock, flags);
  546. return 0;
  547. }
  548. static void serial_ir_close(struct rc_dev *rcdev)
  549. {
  550. unsigned long flags;
  551. spin_lock_irqsave(&hardware[type].lock, flags);
  552. /* Set DLAB 0. */
  553. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  554. /* First of all, disable all interrupts */
  555. soutp(UART_IER, sinp(UART_IER) &
  556. (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
  557. spin_unlock_irqrestore(&hardware[type].lock, flags);
  558. }
  559. static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
  560. unsigned int count)
  561. {
  562. unsigned long flags;
  563. ktime_t edge;
  564. s64 delta;
  565. int i;
  566. spin_lock_irqsave(&hardware[type].lock, flags);
  567. if (type == IR_IRDEO) {
  568. /* DTR, RTS down */
  569. on();
  570. }
  571. edge = ktime_get();
  572. for (i = 0; i < count; i++) {
  573. if (i % 2)
  574. hardware[type].send_space();
  575. else
  576. hardware[type].send_pulse(txbuf[i], edge);
  577. edge = ktime_add_us(edge, txbuf[i]);
  578. delta = ktime_us_delta(edge, ktime_get());
  579. if (delta > 25) {
  580. spin_unlock_irqrestore(&hardware[type].lock, flags);
  581. usleep_range(delta - 25, delta + 25);
  582. spin_lock_irqsave(&hardware[type].lock, flags);
  583. } else if (delta > 0) {
  584. udelay(delta);
  585. }
  586. }
  587. off();
  588. spin_unlock_irqrestore(&hardware[type].lock, flags);
  589. return count;
  590. }
  591. static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle)
  592. {
  593. serial_ir.duty_cycle = cycle;
  594. return 0;
  595. }
  596. static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier)
  597. {
  598. if (carrier > 500000 || carrier < 20000)
  599. return -EINVAL;
  600. serial_ir.carrier = carrier;
  601. return 0;
  602. }
  603. static int serial_ir_suspend(struct platform_device *dev,
  604. pm_message_t state)
  605. {
  606. /* Set DLAB 0. */
  607. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  608. /* Disable all interrupts */
  609. soutp(UART_IER, sinp(UART_IER) &
  610. (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
  611. /* Clear registers. */
  612. sinp(UART_LSR);
  613. sinp(UART_RX);
  614. sinp(UART_IIR);
  615. sinp(UART_MSR);
  616. return 0;
  617. }
  618. static int serial_ir_resume(struct platform_device *dev)
  619. {
  620. unsigned long flags;
  621. int result;
  622. result = hardware_init_port();
  623. if (result < 0)
  624. return result;
  625. spin_lock_irqsave(&hardware[type].lock, flags);
  626. /* Enable Interrupt */
  627. serial_ir.lastkt = ktime_get();
  628. soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
  629. off();
  630. spin_unlock_irqrestore(&hardware[type].lock, flags);
  631. return 0;
  632. }
  633. static struct platform_driver serial_ir_driver = {
  634. .probe = serial_ir_probe,
  635. .suspend = serial_ir_suspend,
  636. .resume = serial_ir_resume,
  637. .driver = {
  638. .name = "serial_ir",
  639. },
  640. };
  641. static int __init serial_ir_init(void)
  642. {
  643. int result;
  644. result = platform_driver_register(&serial_ir_driver);
  645. if (result)
  646. return result;
  647. serial_ir.pdev = platform_device_alloc("serial_ir", 0);
  648. if (!serial_ir.pdev) {
  649. result = -ENOMEM;
  650. goto exit_driver_unregister;
  651. }
  652. result = platform_device_add(serial_ir.pdev);
  653. if (result)
  654. goto exit_device_put;
  655. return 0;
  656. exit_device_put:
  657. platform_device_put(serial_ir.pdev);
  658. exit_driver_unregister:
  659. platform_driver_unregister(&serial_ir_driver);
  660. return result;
  661. }
  662. static void serial_ir_exit(void)
  663. {
  664. platform_device_unregister(serial_ir.pdev);
  665. platform_driver_unregister(&serial_ir_driver);
  666. }
  667. static int __init serial_ir_init_module(void)
  668. {
  669. switch (type) {
  670. case IR_HOMEBREW:
  671. case IR_IRDEO:
  672. case IR_IRDEO_REMOTE:
  673. case IR_ANIMAX:
  674. case IR_IGOR:
  675. /* if nothing specified, use ttyS0/com1 and irq 4 */
  676. io = io ? io : 0x3f8;
  677. irq = irq ? irq : 4;
  678. break;
  679. default:
  680. return -EINVAL;
  681. }
  682. if (!softcarrier) {
  683. switch (type) {
  684. case IR_HOMEBREW:
  685. case IR_IGOR:
  686. hardware[type].set_send_carrier = false;
  687. hardware[type].set_duty_cycle = false;
  688. break;
  689. }
  690. }
  691. /* make sure sense is either -1, 0, or 1 */
  692. if (sense != -1)
  693. sense = !!sense;
  694. return serial_ir_init();
  695. }
  696. static void __exit serial_ir_exit_module(void)
  697. {
  698. del_timer_sync(&serial_ir.timeout_timer);
  699. serial_ir_exit();
  700. }
  701. module_init(serial_ir_init_module);
  702. module_exit(serial_ir_exit_module);
  703. MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
  704. MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus, Andrei Tanas");
  705. MODULE_LICENSE("GPL");
  706. module_param(type, int, 0444);
  707. MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo, 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug");
  708. module_param_hw(io, int, ioport, 0444);
  709. MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
  710. /* some architectures (e.g. intel xscale) have memory mapped registers */
  711. module_param_hw(iommap, ulong, other, 0444);
  712. MODULE_PARM_DESC(iommap, "physical base for memory mapped I/O (0 = no memory mapped io)");
  713. /*
  714. * some architectures (e.g. intel xscale) align the 8bit serial registers
  715. * on 32bit word boundaries.
  716. * See linux-kernel/drivers/tty/serial/8250/8250.c serial_in()/out()
  717. */
  718. module_param_hw(ioshift, int, other, 0444);
  719. MODULE_PARM_DESC(ioshift, "shift I/O register offset (0 = no shift)");
  720. module_param_hw(irq, int, irq, 0444);
  721. MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
  722. module_param_hw(share_irq, bool, other, 0444);
  723. MODULE_PARM_DESC(share_irq, "Share interrupts (0 = off, 1 = on)");
  724. module_param(sense, int, 0444);
  725. MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit (0 = active high, 1 = active low )");
  726. #ifdef CONFIG_IR_SERIAL_TRANSMITTER
  727. module_param(txsense, bool, 0444);
  728. MODULE_PARM_DESC(txsense, "Sense of transmitter circuit (0 = active high, 1 = active low )");
  729. #endif
  730. module_param(softcarrier, bool, 0444);
  731. MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");