serial_ir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 bool 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. unsigned int freq;
  128. unsigned int duty_cycle;
  129. unsigned int pulse_width, space_width;
  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 init_timing_params(unsigned int new_duty_cycle,
  163. unsigned int new_freq)
  164. {
  165. serial_ir.duty_cycle = new_duty_cycle;
  166. serial_ir.freq = new_freq;
  167. serial_ir.pulse_width = DIV_ROUND_CLOSEST(
  168. new_duty_cycle * NSEC_PER_SEC, new_freq * 100l);
  169. serial_ir.space_width = DIV_ROUND_CLOSEST(
  170. (100l - new_duty_cycle) * NSEC_PER_SEC, new_freq * 100l);
  171. }
  172. static void send_pulse_irdeo(unsigned int length, ktime_t target)
  173. {
  174. long rawbits;
  175. int i;
  176. unsigned char output;
  177. unsigned char chunk, shifted;
  178. /* how many bits have to be sent ? */
  179. rawbits = length * 1152 / 10000;
  180. if (serial_ir.duty_cycle > 50)
  181. chunk = 3;
  182. else
  183. chunk = 1;
  184. for (i = 0, output = 0x7f; rawbits > 0; rawbits -= 3) {
  185. shifted = chunk << (i * 3);
  186. shifted >>= 1;
  187. output &= (~shifted);
  188. i++;
  189. if (i == 3) {
  190. soutp(UART_TX, output);
  191. while (!(sinp(UART_LSR) & UART_LSR_THRE))
  192. ;
  193. output = 0x7f;
  194. i = 0;
  195. }
  196. }
  197. if (i != 0) {
  198. soutp(UART_TX, output);
  199. while (!(sinp(UART_LSR) & UART_LSR_TEMT))
  200. ;
  201. }
  202. }
  203. static void send_space_irdeo(void)
  204. {
  205. }
  206. #ifdef CONFIG_IR_SERIAL_TRANSMITTER
  207. static void send_pulse_homebrew_softcarrier(unsigned int length, ktime_t edge)
  208. {
  209. ktime_t now, target = ktime_add_us(edge, length);
  210. /*
  211. * delta should never exceed 4 seconds and on m68k
  212. * ndelay(s64) does not compile; so use s32 rather than s64.
  213. */
  214. s32 delta;
  215. for (;;) {
  216. now = ktime_get();
  217. if (ktime_compare(now, target) >= 0)
  218. break;
  219. on();
  220. edge = ktime_add_ns(edge, serial_ir.pulse_width);
  221. delta = ktime_to_ns(ktime_sub(edge, now));
  222. if (delta > 0)
  223. ndelay(delta);
  224. now = ktime_get();
  225. off();
  226. if (ktime_compare(now, target) >= 0)
  227. break;
  228. edge = ktime_add_ns(edge, serial_ir.space_width);
  229. delta = ktime_to_ns(ktime_sub(edge, now));
  230. if (delta > 0)
  231. ndelay(delta);
  232. }
  233. }
  234. static void send_pulse_homebrew(unsigned int length, ktime_t edge)
  235. {
  236. if (softcarrier)
  237. send_pulse_homebrew_softcarrier(length, edge);
  238. else
  239. on();
  240. }
  241. static void send_space_homebrew(void)
  242. {
  243. off();
  244. }
  245. #endif
  246. static void frbwrite(unsigned int l, bool is_pulse)
  247. {
  248. /* simple noise filter */
  249. static unsigned int ptr, pulse, space;
  250. DEFINE_IR_RAW_EVENT(ev);
  251. if (ptr > 0 && is_pulse) {
  252. pulse += l;
  253. if (pulse > 250000) {
  254. ev.duration = space;
  255. ev.pulse = false;
  256. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  257. ev.duration = pulse;
  258. ev.pulse = true;
  259. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  260. ptr = 0;
  261. pulse = 0;
  262. }
  263. return;
  264. }
  265. if (!is_pulse) {
  266. if (ptr == 0) {
  267. if (l > 20000000) {
  268. space = l;
  269. ptr++;
  270. return;
  271. }
  272. } else {
  273. if (l > 20000000) {
  274. space += pulse;
  275. if (space > IR_MAX_DURATION)
  276. space = IR_MAX_DURATION;
  277. space += l;
  278. if (space > IR_MAX_DURATION)
  279. space = IR_MAX_DURATION;
  280. pulse = 0;
  281. return;
  282. }
  283. ev.duration = space;
  284. ev.pulse = false;
  285. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  286. ev.duration = pulse;
  287. ev.pulse = true;
  288. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  289. ptr = 0;
  290. pulse = 0;
  291. }
  292. }
  293. ev.duration = l;
  294. ev.pulse = is_pulse;
  295. ir_raw_event_store_with_filter(serial_ir.rcdev, &ev);
  296. }
  297. static irqreturn_t serial_ir_irq_handler(int i, void *blah)
  298. {
  299. ktime_t kt;
  300. int counter, dcd;
  301. u8 status;
  302. ktime_t delkt;
  303. unsigned int data;
  304. static int last_dcd = -1;
  305. if ((sinp(UART_IIR) & UART_IIR_NO_INT)) {
  306. /* not our interrupt */
  307. return IRQ_NONE;
  308. }
  309. counter = 0;
  310. do {
  311. counter++;
  312. status = sinp(UART_MSR);
  313. if (counter > RS_ISR_PASS_LIMIT) {
  314. dev_err(&serial_ir.pdev->dev, "Trapped in interrupt");
  315. break;
  316. }
  317. if ((status & hardware[type].signal_pin_change) &&
  318. sense != -1) {
  319. /* get current time */
  320. kt = ktime_get();
  321. /*
  322. * The driver needs to know if your receiver is
  323. * active high or active low, or the space/pulse
  324. * sense could be inverted.
  325. */
  326. /* calc time since last interrupt in nanoseconds */
  327. dcd = (status & hardware[type].signal_pin) ? 1 : 0;
  328. if (dcd == last_dcd) {
  329. dev_err(&serial_ir.pdev->dev,
  330. "ignoring spike: %d %d %lldns %lldns\n",
  331. dcd, sense, ktime_to_ns(kt),
  332. ktime_to_ns(serial_ir.lastkt));
  333. continue;
  334. }
  335. delkt = ktime_sub(kt, serial_ir.lastkt);
  336. if (ktime_compare(delkt, ktime_set(15, 0)) > 0) {
  337. data = IR_MAX_DURATION; /* really long time */
  338. if (!(dcd ^ sense)) {
  339. /* sanity check */
  340. dev_err(&serial_ir.pdev->dev,
  341. "dcd unexpected: %d %d %lldns %lldns\n",
  342. dcd, sense, ktime_to_ns(kt),
  343. ktime_to_ns(serial_ir.lastkt));
  344. /*
  345. * detecting pulse while this
  346. * MUST be a space!
  347. */
  348. sense = sense ? 0 : 1;
  349. }
  350. } else {
  351. data = ktime_to_ns(delkt);
  352. }
  353. frbwrite(data, !(dcd ^ sense));
  354. serial_ir.lastkt = kt;
  355. last_dcd = dcd;
  356. ir_raw_event_handle(serial_ir.rcdev);
  357. }
  358. } while (!(sinp(UART_IIR) & UART_IIR_NO_INT)); /* still pending ? */
  359. return IRQ_HANDLED;
  360. }
  361. static int hardware_init_port(void)
  362. {
  363. u8 scratch, scratch2, scratch3;
  364. /*
  365. * This is a simple port existence test, borrowed from the autoconfig
  366. * function in drivers/tty/serial/8250/8250_port.c
  367. */
  368. scratch = sinp(UART_IER);
  369. soutp(UART_IER, 0);
  370. #ifdef __i386__
  371. outb(0xff, 0x080);
  372. #endif
  373. scratch2 = sinp(UART_IER) & 0x0f;
  374. soutp(UART_IER, 0x0f);
  375. #ifdef __i386__
  376. outb(0x00, 0x080);
  377. #endif
  378. scratch3 = sinp(UART_IER) & 0x0f;
  379. soutp(UART_IER, scratch);
  380. if (scratch2 != 0 || scratch3 != 0x0f) {
  381. /* we fail, there's nothing here */
  382. pr_err("port existence test failed, cannot continue\n");
  383. return -ENODEV;
  384. }
  385. /* Set DLAB 0. */
  386. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  387. /* First of all, disable all interrupts */
  388. soutp(UART_IER, sinp(UART_IER) &
  389. (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
  390. /* Clear registers. */
  391. sinp(UART_LSR);
  392. sinp(UART_RX);
  393. sinp(UART_IIR);
  394. sinp(UART_MSR);
  395. /* Set line for power source */
  396. off();
  397. /* Clear registers again to be sure. */
  398. sinp(UART_LSR);
  399. sinp(UART_RX);
  400. sinp(UART_IIR);
  401. sinp(UART_MSR);
  402. switch (type) {
  403. case IR_IRDEO:
  404. case IR_IRDEO_REMOTE:
  405. /* setup port to 7N1 @ 115200 Baud */
  406. /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
  407. /* Set DLAB 1. */
  408. soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
  409. /* Set divisor to 1 => 115200 Baud */
  410. soutp(UART_DLM, 0);
  411. soutp(UART_DLL, 1);
  412. /* Set DLAB 0 + 7N1 */
  413. soutp(UART_LCR, UART_LCR_WLEN7);
  414. /* THR interrupt already disabled at this point */
  415. break;
  416. default:
  417. break;
  418. }
  419. return 0;
  420. }
  421. static int serial_ir_probe(struct platform_device *dev)
  422. {
  423. int i, nlow, nhigh, result;
  424. result = devm_request_irq(&dev->dev, irq, serial_ir_irq_handler,
  425. share_irq ? IRQF_SHARED : 0,
  426. KBUILD_MODNAME, &hardware);
  427. if (result < 0) {
  428. if (result == -EBUSY)
  429. dev_err(&dev->dev, "IRQ %d busy\n", irq);
  430. else if (result == -EINVAL)
  431. dev_err(&dev->dev, "Bad irq number or handler\n");
  432. return result;
  433. }
  434. /* Reserve io region. */
  435. if ((iommap &&
  436. (devm_request_mem_region(&dev->dev, iommap, 8 << ioshift,
  437. KBUILD_MODNAME) == NULL)) ||
  438. (!iommap && (devm_request_region(&dev->dev, io, 8,
  439. KBUILD_MODNAME) == NULL))) {
  440. dev_err(&dev->dev, "port %04x already in use\n", io);
  441. dev_warn(&dev->dev, "use 'setserial /dev/ttySX uart none'\n");
  442. dev_warn(&dev->dev,
  443. "or compile the serial port driver as module and\n");
  444. dev_warn(&dev->dev, "make sure this module is loaded first\n");
  445. return -EBUSY;
  446. }
  447. result = hardware_init_port();
  448. if (result < 0)
  449. return result;
  450. /* Initialize pulse/space widths */
  451. init_timing_params(50, 38000);
  452. /* If pin is high, then this must be an active low receiver. */
  453. if (sense == -1) {
  454. /* wait 1/2 sec for the power supply */
  455. msleep(500);
  456. /*
  457. * probe 9 times every 0.04s, collect "votes" for
  458. * active high/low
  459. */
  460. nlow = 0;
  461. nhigh = 0;
  462. for (i = 0; i < 9; i++) {
  463. if (sinp(UART_MSR) & hardware[type].signal_pin)
  464. nlow++;
  465. else
  466. nhigh++;
  467. msleep(40);
  468. }
  469. sense = nlow >= nhigh ? 1 : 0;
  470. dev_info(&dev->dev, "auto-detected active %s receiver\n",
  471. sense ? "low" : "high");
  472. } else
  473. dev_info(&dev->dev, "Manually using active %s receiver\n",
  474. sense ? "low" : "high");
  475. dev_dbg(&dev->dev, "Interrupt %d, port %04x obtained\n", irq, io);
  476. return 0;
  477. }
  478. static int serial_ir_open(struct rc_dev *rcdev)
  479. {
  480. unsigned long flags;
  481. /* initialize timestamp */
  482. serial_ir.lastkt = ktime_get();
  483. spin_lock_irqsave(&hardware[type].lock, flags);
  484. /* Set DLAB 0. */
  485. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  486. soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
  487. spin_unlock_irqrestore(&hardware[type].lock, flags);
  488. return 0;
  489. }
  490. static void serial_ir_close(struct rc_dev *rcdev)
  491. {
  492. unsigned long flags;
  493. spin_lock_irqsave(&hardware[type].lock, flags);
  494. /* Set DLAB 0. */
  495. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  496. /* First of all, disable all interrupts */
  497. soutp(UART_IER, sinp(UART_IER) &
  498. (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
  499. spin_unlock_irqrestore(&hardware[type].lock, flags);
  500. }
  501. static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
  502. unsigned int count)
  503. {
  504. unsigned long flags;
  505. ktime_t edge;
  506. s64 delta;
  507. int i;
  508. spin_lock_irqsave(&hardware[type].lock, flags);
  509. if (type == IR_IRDEO) {
  510. /* DTR, RTS down */
  511. on();
  512. }
  513. edge = ktime_get();
  514. for (i = 0; i < count; i++) {
  515. if (i % 2)
  516. hardware[type].send_space();
  517. else
  518. hardware[type].send_pulse(txbuf[i], edge);
  519. edge = ktime_add_us(edge, txbuf[i]);
  520. delta = ktime_us_delta(edge, ktime_get());
  521. if (delta > 25) {
  522. spin_unlock_irqrestore(&hardware[type].lock, flags);
  523. usleep_range(delta - 25, delta + 25);
  524. spin_lock_irqsave(&hardware[type].lock, flags);
  525. } else if (delta > 0) {
  526. udelay(delta);
  527. }
  528. }
  529. off();
  530. spin_unlock_irqrestore(&hardware[type].lock, flags);
  531. return count;
  532. }
  533. static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle)
  534. {
  535. init_timing_params(cycle, serial_ir.freq);
  536. return 0;
  537. }
  538. static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier)
  539. {
  540. if (carrier > 500000 || carrier < 20000)
  541. return -EINVAL;
  542. init_timing_params(serial_ir.duty_cycle, carrier);
  543. return 0;
  544. }
  545. static int serial_ir_suspend(struct platform_device *dev,
  546. pm_message_t state)
  547. {
  548. /* Set DLAB 0. */
  549. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  550. /* Disable all interrupts */
  551. soutp(UART_IER, sinp(UART_IER) &
  552. (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
  553. /* Clear registers. */
  554. sinp(UART_LSR);
  555. sinp(UART_RX);
  556. sinp(UART_IIR);
  557. sinp(UART_MSR);
  558. return 0;
  559. }
  560. static int serial_ir_resume(struct platform_device *dev)
  561. {
  562. unsigned long flags;
  563. int result;
  564. result = hardware_init_port();
  565. if (result < 0)
  566. return result;
  567. spin_lock_irqsave(&hardware[type].lock, flags);
  568. /* Enable Interrupt */
  569. serial_ir.lastkt = ktime_get();
  570. soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
  571. off();
  572. spin_unlock_irqrestore(&hardware[type].lock, flags);
  573. return 0;
  574. }
  575. static struct platform_driver serial_ir_driver = {
  576. .probe = serial_ir_probe,
  577. .suspend = serial_ir_suspend,
  578. .resume = serial_ir_resume,
  579. .driver = {
  580. .name = "serial_ir",
  581. },
  582. };
  583. static int __init serial_ir_init(void)
  584. {
  585. int result;
  586. result = platform_driver_register(&serial_ir_driver);
  587. if (result)
  588. return result;
  589. serial_ir.pdev = platform_device_alloc("serial_ir", 0);
  590. if (!serial_ir.pdev) {
  591. result = -ENOMEM;
  592. goto exit_driver_unregister;
  593. }
  594. result = platform_device_add(serial_ir.pdev);
  595. if (result)
  596. goto exit_device_put;
  597. return 0;
  598. exit_device_put:
  599. platform_device_put(serial_ir.pdev);
  600. exit_driver_unregister:
  601. platform_driver_unregister(&serial_ir_driver);
  602. return result;
  603. }
  604. static void serial_ir_exit(void)
  605. {
  606. platform_device_unregister(serial_ir.pdev);
  607. platform_driver_unregister(&serial_ir_driver);
  608. }
  609. static int __init serial_ir_init_module(void)
  610. {
  611. struct rc_dev *rcdev;
  612. int result;
  613. switch (type) {
  614. case IR_HOMEBREW:
  615. case IR_IRDEO:
  616. case IR_IRDEO_REMOTE:
  617. case IR_ANIMAX:
  618. case IR_IGOR:
  619. /* if nothing specified, use ttyS0/com1 and irq 4 */
  620. io = io ? io : 0x3f8;
  621. irq = irq ? irq : 4;
  622. break;
  623. default:
  624. return -EINVAL;
  625. }
  626. if (!softcarrier) {
  627. switch (type) {
  628. case IR_HOMEBREW:
  629. case IR_IGOR:
  630. hardware[type].set_send_carrier = false;
  631. hardware[type].set_duty_cycle = false;
  632. break;
  633. }
  634. }
  635. /* make sure sense is either -1, 0, or 1 */
  636. if (sense != -1)
  637. sense = !!sense;
  638. result = serial_ir_init();
  639. if (result)
  640. return result;
  641. rcdev = devm_rc_allocate_device(&serial_ir.pdev->dev);
  642. if (!rcdev) {
  643. result = -ENOMEM;
  644. goto serial_cleanup;
  645. }
  646. if (hardware[type].send_pulse && hardware[type].send_space)
  647. rcdev->tx_ir = serial_ir_tx;
  648. if (hardware[type].set_send_carrier)
  649. rcdev->s_tx_carrier = serial_ir_tx_carrier;
  650. if (hardware[type].set_duty_cycle)
  651. rcdev->s_tx_duty_cycle = serial_ir_tx_duty_cycle;
  652. switch (type) {
  653. case IR_HOMEBREW:
  654. rcdev->input_name = "Serial IR type home-brew";
  655. break;
  656. case IR_IRDEO:
  657. rcdev->input_name = "Serial IR type IRdeo";
  658. break;
  659. case IR_IRDEO_REMOTE:
  660. rcdev->input_name = "Serial IR type IRdeo remote";
  661. break;
  662. case IR_ANIMAX:
  663. rcdev->input_name = "Serial IR type AnimaX";
  664. break;
  665. case IR_IGOR:
  666. rcdev->input_name = "Serial IR type IgorPlug";
  667. break;
  668. }
  669. rcdev->input_phys = KBUILD_MODNAME "/input0";
  670. rcdev->input_id.bustype = BUS_HOST;
  671. rcdev->input_id.vendor = 0x0001;
  672. rcdev->input_id.product = 0x0001;
  673. rcdev->input_id.version = 0x0100;
  674. rcdev->open = serial_ir_open;
  675. rcdev->close = serial_ir_close;
  676. rcdev->dev.parent = &serial_ir.pdev->dev;
  677. rcdev->driver_type = RC_DRIVER_IR_RAW;
  678. rcdev->allowed_protocols = RC_BIT_ALL;
  679. rcdev->driver_name = KBUILD_MODNAME;
  680. rcdev->map_name = RC_MAP_RC6_MCE;
  681. rcdev->timeout = IR_DEFAULT_TIMEOUT;
  682. rcdev->rx_resolution = 250000;
  683. serial_ir.rcdev = rcdev;
  684. result = rc_register_device(rcdev);
  685. if (!result)
  686. return 0;
  687. serial_cleanup:
  688. serial_ir_exit();
  689. return result;
  690. }
  691. static void __exit serial_ir_exit_module(void)
  692. {
  693. rc_unregister_device(serial_ir.rcdev);
  694. serial_ir_exit();
  695. }
  696. module_init(serial_ir_init_module);
  697. module_exit(serial_ir_exit_module);
  698. MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
  699. MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus, Andrei Tanas");
  700. MODULE_LICENSE("GPL");
  701. module_param(type, int, 0444);
  702. MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo, 2 = IRdeo Remote, 3 = AnimaX, 4 = IgorPlug");
  703. module_param(io, int, 0444);
  704. MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
  705. /* some architectures (e.g. intel xscale) have memory mapped registers */
  706. module_param(iommap, bool, 0444);
  707. MODULE_PARM_DESC(iommap, "physical base for memory mapped I/O (0 = no memory mapped io)");
  708. /*
  709. * some architectures (e.g. intel xscale) align the 8bit serial registers
  710. * on 32bit word boundaries.
  711. * See linux-kernel/drivers/tty/serial/8250/8250.c serial_in()/out()
  712. */
  713. module_param(ioshift, int, 0444);
  714. MODULE_PARM_DESC(ioshift, "shift I/O register offset (0 = no shift)");
  715. module_param(irq, int, 0444);
  716. MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
  717. module_param(share_irq, bool, 0444);
  718. MODULE_PARM_DESC(share_irq, "Share interrupts (0 = off, 1 = on)");
  719. module_param(sense, int, 0444);
  720. MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit (0 = active high, 1 = active low )");
  721. #ifdef CONFIG_IR_SERIAL_TRANSMITTER
  722. module_param(txsense, bool, 0444);
  723. MODULE_PARM_DESC(txsense, "Sense of transmitter circuit (0 = active high, 1 = active low )");
  724. #endif
  725. module_param(softcarrier, bool, 0444);
  726. MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");