serial_ir.c 20 KB

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