serial_ir.c 21 KB

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