serial_ir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 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. /* Needed by serial_ir_probe() */
  433. static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
  434. unsigned int count);
  435. static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle);
  436. static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier);
  437. static int serial_ir_open(struct rc_dev *rcdev);
  438. static void serial_ir_close(struct rc_dev *rcdev);
  439. static int serial_ir_probe(struct platform_device *dev)
  440. {
  441. struct rc_dev *rcdev;
  442. int i, nlow, nhigh, result;
  443. rcdev = devm_rc_allocate_device(&dev->dev, RC_DRIVER_IR_RAW);
  444. if (!rcdev)
  445. return -ENOMEM;
  446. if (hardware[type].send_pulse && hardware[type].send_space)
  447. rcdev->tx_ir = serial_ir_tx;
  448. if (hardware[type].set_send_carrier)
  449. rcdev->s_tx_carrier = serial_ir_tx_carrier;
  450. if (hardware[type].set_duty_cycle)
  451. rcdev->s_tx_duty_cycle = serial_ir_tx_duty_cycle;
  452. switch (type) {
  453. case IR_HOMEBREW:
  454. rcdev->input_name = "Serial IR type home-brew";
  455. break;
  456. case IR_IRDEO:
  457. rcdev->input_name = "Serial IR type IRdeo";
  458. break;
  459. case IR_IRDEO_REMOTE:
  460. rcdev->input_name = "Serial IR type IRdeo remote";
  461. break;
  462. case IR_ANIMAX:
  463. rcdev->input_name = "Serial IR type AnimaX";
  464. break;
  465. case IR_IGOR:
  466. rcdev->input_name = "Serial IR type IgorPlug";
  467. break;
  468. }
  469. rcdev->input_phys = KBUILD_MODNAME "/input0";
  470. rcdev->input_id.bustype = BUS_HOST;
  471. rcdev->input_id.vendor = 0x0001;
  472. rcdev->input_id.product = 0x0001;
  473. rcdev->input_id.version = 0x0100;
  474. rcdev->open = serial_ir_open;
  475. rcdev->close = serial_ir_close;
  476. rcdev->dev.parent = &serial_ir.pdev->dev;
  477. rcdev->allowed_protocols = RC_BIT_ALL_IR_DECODER;
  478. rcdev->driver_name = KBUILD_MODNAME;
  479. rcdev->map_name = RC_MAP_RC6_MCE;
  480. rcdev->min_timeout = 1;
  481. rcdev->timeout = IR_DEFAULT_TIMEOUT;
  482. rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  483. rcdev->rx_resolution = 250000;
  484. serial_ir.rcdev = rcdev;
  485. setup_timer(&serial_ir.timeout_timer, serial_ir_timeout,
  486. (unsigned long)&serial_ir);
  487. result = devm_request_irq(&dev->dev, irq, serial_ir_irq_handler,
  488. share_irq ? IRQF_SHARED : 0,
  489. KBUILD_MODNAME, &hardware);
  490. if (result < 0) {
  491. if (result == -EBUSY)
  492. dev_err(&dev->dev, "IRQ %d busy\n", irq);
  493. else if (result == -EINVAL)
  494. dev_err(&dev->dev, "Bad irq number or handler\n");
  495. return result;
  496. }
  497. /* Reserve io region. */
  498. if ((iommap &&
  499. (devm_request_mem_region(&dev->dev, iommap, 8 << ioshift,
  500. KBUILD_MODNAME) == NULL)) ||
  501. (!iommap && (devm_request_region(&dev->dev, io, 8,
  502. KBUILD_MODNAME) == NULL))) {
  503. dev_err(&dev->dev, "port %04x already in use\n", io);
  504. dev_warn(&dev->dev, "use 'setserial /dev/ttySX uart none'\n");
  505. dev_warn(&dev->dev,
  506. "or compile the serial port driver as module and\n");
  507. dev_warn(&dev->dev, "make sure this module is loaded first\n");
  508. return -EBUSY;
  509. }
  510. result = hardware_init_port();
  511. if (result < 0)
  512. return result;
  513. /* Initialize pulse/space widths */
  514. init_timing_params(50, 38000);
  515. /* If pin is high, then this must be an active low receiver. */
  516. if (sense == -1) {
  517. /* wait 1/2 sec for the power supply */
  518. msleep(500);
  519. /*
  520. * probe 9 times every 0.04s, collect "votes" for
  521. * active high/low
  522. */
  523. nlow = 0;
  524. nhigh = 0;
  525. for (i = 0; i < 9; i++) {
  526. if (sinp(UART_MSR) & hardware[type].signal_pin)
  527. nlow++;
  528. else
  529. nhigh++;
  530. msleep(40);
  531. }
  532. sense = nlow >= nhigh ? 1 : 0;
  533. dev_info(&dev->dev, "auto-detected active %s receiver\n",
  534. sense ? "low" : "high");
  535. } else
  536. dev_info(&dev->dev, "Manually using active %s receiver\n",
  537. sense ? "low" : "high");
  538. dev_dbg(&dev->dev, "Interrupt %d, port %04x obtained\n", irq, io);
  539. return devm_rc_register_device(&dev->dev, rcdev);
  540. }
  541. static int serial_ir_open(struct rc_dev *rcdev)
  542. {
  543. unsigned long flags;
  544. /* initialize timestamp */
  545. serial_ir.lastkt = ktime_get();
  546. spin_lock_irqsave(&hardware[type].lock, flags);
  547. /* Set DLAB 0. */
  548. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  549. soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
  550. spin_unlock_irqrestore(&hardware[type].lock, flags);
  551. return 0;
  552. }
  553. static void serial_ir_close(struct rc_dev *rcdev)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(&hardware[type].lock, flags);
  557. /* Set DLAB 0. */
  558. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  559. /* First of all, disable all interrupts */
  560. soutp(UART_IER, sinp(UART_IER) &
  561. (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
  562. spin_unlock_irqrestore(&hardware[type].lock, flags);
  563. }
  564. static int serial_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
  565. unsigned int count)
  566. {
  567. unsigned long flags;
  568. ktime_t edge;
  569. s64 delta;
  570. int i;
  571. spin_lock_irqsave(&hardware[type].lock, flags);
  572. if (type == IR_IRDEO) {
  573. /* DTR, RTS down */
  574. on();
  575. }
  576. edge = ktime_get();
  577. for (i = 0; i < count; i++) {
  578. if (i % 2)
  579. hardware[type].send_space();
  580. else
  581. hardware[type].send_pulse(txbuf[i], edge);
  582. edge = ktime_add_us(edge, txbuf[i]);
  583. delta = ktime_us_delta(edge, ktime_get());
  584. if (delta > 25) {
  585. spin_unlock_irqrestore(&hardware[type].lock, flags);
  586. usleep_range(delta - 25, delta + 25);
  587. spin_lock_irqsave(&hardware[type].lock, flags);
  588. } else if (delta > 0) {
  589. udelay(delta);
  590. }
  591. }
  592. off();
  593. spin_unlock_irqrestore(&hardware[type].lock, flags);
  594. return count;
  595. }
  596. static int serial_ir_tx_duty_cycle(struct rc_dev *dev, u32 cycle)
  597. {
  598. init_timing_params(cycle, serial_ir.freq);
  599. return 0;
  600. }
  601. static int serial_ir_tx_carrier(struct rc_dev *dev, u32 carrier)
  602. {
  603. if (carrier > 500000 || carrier < 20000)
  604. return -EINVAL;
  605. init_timing_params(serial_ir.duty_cycle, carrier);
  606. return 0;
  607. }
  608. static int serial_ir_suspend(struct platform_device *dev,
  609. pm_message_t state)
  610. {
  611. /* Set DLAB 0. */
  612. soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
  613. /* Disable all interrupts */
  614. soutp(UART_IER, sinp(UART_IER) &
  615. (~(UART_IER_MSI | UART_IER_RLSI | UART_IER_THRI | UART_IER_RDI)));
  616. /* Clear registers. */
  617. sinp(UART_LSR);
  618. sinp(UART_RX);
  619. sinp(UART_IIR);
  620. sinp(UART_MSR);
  621. return 0;
  622. }
  623. static int serial_ir_resume(struct platform_device *dev)
  624. {
  625. unsigned long flags;
  626. int result;
  627. result = hardware_init_port();
  628. if (result < 0)
  629. return result;
  630. spin_lock_irqsave(&hardware[type].lock, flags);
  631. /* Enable Interrupt */
  632. serial_ir.lastkt = ktime_get();
  633. soutp(UART_IER, sinp(UART_IER) | UART_IER_MSI);
  634. off();
  635. spin_unlock_irqrestore(&hardware[type].lock, flags);
  636. return 0;
  637. }
  638. static struct platform_driver serial_ir_driver = {
  639. .probe = serial_ir_probe,
  640. .suspend = serial_ir_suspend,
  641. .resume = serial_ir_resume,
  642. .driver = {
  643. .name = "serial_ir",
  644. },
  645. };
  646. static int __init serial_ir_init(void)
  647. {
  648. int result;
  649. result = platform_driver_register(&serial_ir_driver);
  650. if (result)
  651. return result;
  652. serial_ir.pdev = platform_device_alloc("serial_ir", 0);
  653. if (!serial_ir.pdev) {
  654. result = -ENOMEM;
  655. goto exit_driver_unregister;
  656. }
  657. result = platform_device_add(serial_ir.pdev);
  658. if (result)
  659. goto exit_device_put;
  660. return 0;
  661. exit_device_put:
  662. platform_device_put(serial_ir.pdev);
  663. exit_driver_unregister:
  664. platform_driver_unregister(&serial_ir_driver);
  665. return result;
  666. }
  667. static void serial_ir_exit(void)
  668. {
  669. platform_device_unregister(serial_ir.pdev);
  670. platform_driver_unregister(&serial_ir_driver);
  671. }
  672. static int __init serial_ir_init_module(void)
  673. {
  674. int result;
  675. switch (type) {
  676. case IR_HOMEBREW:
  677. case IR_IRDEO:
  678. case IR_IRDEO_REMOTE:
  679. case IR_ANIMAX:
  680. case IR_IGOR:
  681. /* if nothing specified, use ttyS0/com1 and irq 4 */
  682. io = io ? io : 0x3f8;
  683. irq = irq ? irq : 4;
  684. break;
  685. default:
  686. return -EINVAL;
  687. }
  688. if (!softcarrier) {
  689. switch (type) {
  690. case IR_HOMEBREW:
  691. case IR_IGOR:
  692. hardware[type].set_send_carrier = false;
  693. hardware[type].set_duty_cycle = false;
  694. break;
  695. }
  696. }
  697. /* make sure sense is either -1, 0, or 1 */
  698. if (sense != -1)
  699. sense = !!sense;
  700. result = serial_ir_init();
  701. if (!result)
  702. return 0;
  703. serial_ir_exit();
  704. return result;
  705. }
  706. static void __exit serial_ir_exit_module(void)
  707. {
  708. del_timer_sync(&serial_ir.timeout_timer);
  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, ulong, 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)");