toim3232-sir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*********************************************************************
  2. *
  3. * Filename: toim3232-sir.c
  4. * Version: 1.0
  5. * Description: Implementation of dongles based on the Vishay/Temic
  6. * TOIM3232 SIR Endec chipset. Currently only the
  7. * IRWave IR320ST-2 is tested, although it should work
  8. * with any TOIM3232 or TOIM4232 chipset based RS232
  9. * dongle with minimal modification.
  10. * Based heavily on the Tekram driver (tekram.c),
  11. * with thanks to Dag Brattli and Martin Diehl.
  12. * Status: Experimental.
  13. * Author: David Basden <davidb-irda@rcpt.to>
  14. * Created at: Thu Feb 09 23:47:32 2006
  15. *
  16. * Copyright (c) 2006 David Basden.
  17. * Copyright (c) 1998-1999 Dag Brattli,
  18. * Copyright (c) 2002 Martin Diehl,
  19. * All Rights Reserved.
  20. *
  21. * This program is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License as
  23. * published by the Free Software Foundation; either version 2 of
  24. * the License, or (at your option) any later version.
  25. *
  26. * Neither Dag Brattli nor University of Tromsø admit liability nor
  27. * provide warranty for any of this software. This material is
  28. * provided "AS-IS" and at no charge.
  29. *
  30. ********************************************************************/
  31. /*
  32. * This driver has currently only been tested on the IRWave IR320ST-2
  33. *
  34. * PROTOCOL:
  35. *
  36. * The protocol for talking to the TOIM3232 is quite easy, and is
  37. * designed to interface with RS232 with only level convertors. The
  38. * BR/~D line on the chip is brought high to signal 'command mode',
  39. * where a command byte is sent to select the baudrate of the RS232
  40. * interface and the pulse length of the IRDA output. When BR/~D
  41. * is brought low, the dongle then changes to the selected baudrate,
  42. * and the RS232 interface is used for data until BR/~D is brought
  43. * high again. The initial speed for the TOIMx323 after RESET is
  44. * 9600 baud. The baudrate for command-mode is the last selected
  45. * baud-rate, or 9600 after a RESET.
  46. *
  47. * The dongle I have (below) adds some extra hardware on the front end,
  48. * but this is mostly directed towards pariasitic power from the RS232
  49. * line rather than changing very much about how to communicate with
  50. * the TOIM3232.
  51. *
  52. * The protocol to talk to the TOIM4232 chipset seems to be almost
  53. * identical to the TOIM3232 (and the 4232 datasheet is more detailed)
  54. * so this code will probably work on that as well, although I haven't
  55. * tested it on that hardware.
  56. *
  57. * Target dongle variations that might be common:
  58. *
  59. * DTR and RTS function:
  60. * The data sheet for the 4232 has a sample implementation that hooks the
  61. * DTR and RTS lines to the RESET and BaudRate/~Data lines of the
  62. * chip (through line-converters). Given both DTR and RTS would have to
  63. * be held low in normal operation, and the TOIMx232 requires +5V to
  64. * signal ground, most dongle designers would almost certainly choose
  65. * an implementation that kept at least one of DTR or RTS high in
  66. * normal operation to provide power to the dongle, but will likely
  67. * vary between designs.
  68. *
  69. * User specified command bits:
  70. * There are two user-controllable output lines from the TOIMx232 that
  71. * can be set low or high by setting the appropriate bits in the
  72. * high-nibble of the command byte (when setting speed and pulse length).
  73. * These might be used to switch on and off added hardware or extra
  74. * dongle features.
  75. *
  76. *
  77. * Target hardware: IRWave IR320ST-2
  78. *
  79. * The IRWave IR320ST-2 is a simple dongle based on the Vishay/Temic
  80. * TOIM3232 SIR Endec and the Vishay/Temic TFDS4500 SIR IRDA transceiver.
  81. * It uses a hex inverter and some discrete components to buffer and
  82. * line convert the RS232 down to 5V.
  83. *
  84. * The dongle is powered through a voltage regulator, fed by a large
  85. * capacitor. To switch the dongle on, DTR is brought high to charge
  86. * the capacitor and drive the voltage regulator. DTR isn't associated
  87. * with any control lines on the TOIM3232. Parisitic power is also taken
  88. * from the RTS, TD and RD lines when brought high, but through resistors.
  89. * When DTR is low, the circuit might lose power even with RTS high.
  90. *
  91. * RTS is inverted and attached to the BR/~D input pin. When RTS
  92. * is high, BR/~D is low, and the TOIM3232 is in the normal 'data' mode.
  93. * RTS is brought low, BR/~D is high, and the TOIM3232 is in 'command
  94. * mode'.
  95. *
  96. * For some unknown reason, the RESET line isn't actually connected
  97. * to anything. This means to reset the dongle to get it to a known
  98. * state (9600 baud) you must drop DTR and RTS low, wait for the power
  99. * capacitor to discharge, and then bring DTR (and RTS for data mode)
  100. * high again, and wait for the capacitor to charge, the power supply
  101. * to stabilise, and the oscillator clock to stabilise.
  102. *
  103. * Fortunately, if the current baudrate is known, the chipset can
  104. * easily change speed by entering command mode without having to
  105. * reset the dongle first.
  106. *
  107. * Major Components:
  108. *
  109. * - Vishay/Temic TOIM3232 SIR Endec to change RS232 pulse timings
  110. * to IRDA pulse timings
  111. * - 3.6864MHz crystal to drive TOIM3232 clock oscillator
  112. * - DM74lS04M Inverting Hex line buffer for RS232 input buffering
  113. * and level conversion
  114. * - PJ2951AC 150mA voltage regulator
  115. * - Vishay/Temic TFDS4500 SIR IRDA front-end transceiver
  116. *
  117. */
  118. #include <linux/module.h>
  119. #include <linux/delay.h>
  120. #include <linux/init.h>
  121. #include <linux/sched.h>
  122. #include <net/irda/irda.h>
  123. #include "sir-dev.h"
  124. static int toim3232delay = 150; /* default is 150 ms */
  125. module_param(toim3232delay, int, 0);
  126. MODULE_PARM_DESC(toim3232delay, "toim3232 dongle write complete delay");
  127. #if 0
  128. static int toim3232flipdtr = 0; /* default is DTR high to reset */
  129. module_param(toim3232flipdtr, int, 0);
  130. MODULE_PARM_DESC(toim3232flipdtr, "toim3232 dongle invert DTR (Reset)");
  131. static int toim3232fliprts = 0; /* default is RTS high for baud change */
  132. module_param(toim3232fliptrs, int, 0);
  133. MODULE_PARM_DESC(toim3232fliprts, "toim3232 dongle invert RTS (BR/D)");
  134. #endif
  135. static int toim3232_open(struct sir_dev *);
  136. static int toim3232_close(struct sir_dev *);
  137. static int toim3232_change_speed(struct sir_dev *, unsigned);
  138. static int toim3232_reset(struct sir_dev *);
  139. #define TOIM3232_115200 0x00
  140. #define TOIM3232_57600 0x01
  141. #define TOIM3232_38400 0x02
  142. #define TOIM3232_19200 0x03
  143. #define TOIM3232_9600 0x06
  144. #define TOIM3232_2400 0x0A
  145. #define TOIM3232_PW 0x10 /* Pulse select bit */
  146. static struct dongle_driver toim3232 = {
  147. .owner = THIS_MODULE,
  148. .driver_name = "Vishay TOIM3232",
  149. .type = IRDA_TOIM3232_DONGLE,
  150. .open = toim3232_open,
  151. .close = toim3232_close,
  152. .reset = toim3232_reset,
  153. .set_speed = toim3232_change_speed,
  154. };
  155. static int __init toim3232_sir_init(void)
  156. {
  157. if (toim3232delay < 1 || toim3232delay > 500)
  158. toim3232delay = 200;
  159. pr_debug("%s - using %d ms delay\n",
  160. toim3232.driver_name, toim3232delay);
  161. return irda_register_dongle(&toim3232);
  162. }
  163. static void __exit toim3232_sir_cleanup(void)
  164. {
  165. irda_unregister_dongle(&toim3232);
  166. }
  167. static int toim3232_open(struct sir_dev *dev)
  168. {
  169. struct qos_info *qos = &dev->qos;
  170. /* Pull the lines high to start with.
  171. *
  172. * For the IR320ST-2, we need to charge the main supply capacitor to
  173. * switch the device on. We keep DTR high throughout to do this.
  174. * When RTS, TD and RD are high, they will also trickle-charge the
  175. * cap. RTS is high for data transmission, and low for baud rate select.
  176. * -- DGB
  177. */
  178. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  179. /* The TOI3232 supports many speeds between 1200bps and 115000bps.
  180. * We really only care about those supported by the IRDA spec, but
  181. * 38400 seems to be implemented in many places */
  182. qos->baud_rate.bits &= IR_2400|IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
  183. /* From the tekram driver. Not sure what a reasonable value is -- DGB */
  184. qos->min_turn_time.bits = 0x01; /* Needs at least 10 ms */
  185. irda_qos_bits_to_value(qos);
  186. /* irda thread waits 50 msec for power settling */
  187. return 0;
  188. }
  189. static int toim3232_close(struct sir_dev *dev)
  190. {
  191. /* Power off dongle */
  192. sirdev_set_dtr_rts(dev, FALSE, FALSE);
  193. return 0;
  194. }
  195. /*
  196. * Function toim3232change_speed (dev, state, speed)
  197. *
  198. * Set the speed for the TOIM3232 based dongle. Warning, this
  199. * function must be called with a process context!
  200. *
  201. * Algorithm
  202. * 1. keep DTR high but clear RTS to bring into baud programming mode
  203. * 2. wait at least 7us to enter programming mode
  204. * 3. send control word to set baud rate and timing
  205. * 4. wait at least 1us
  206. * 5. bring RTS high to enter DATA mode (RS232 is passed through to transceiver)
  207. * 6. should take effect immediately (although probably worth waiting)
  208. */
  209. #define TOIM3232_STATE_WAIT_SPEED (SIRDEV_STATE_DONGLE_SPEED + 1)
  210. static int toim3232_change_speed(struct sir_dev *dev, unsigned speed)
  211. {
  212. unsigned state = dev->fsm.substate;
  213. unsigned delay = 0;
  214. u8 byte;
  215. static int ret = 0;
  216. switch(state) {
  217. case SIRDEV_STATE_DONGLE_SPEED:
  218. /* Figure out what we are going to send as a control byte */
  219. switch (speed) {
  220. case 2400:
  221. byte = TOIM3232_PW|TOIM3232_2400;
  222. break;
  223. default:
  224. speed = 9600;
  225. ret = -EINVAL;
  226. /* fall thru */
  227. case 9600:
  228. byte = TOIM3232_PW|TOIM3232_9600;
  229. break;
  230. case 19200:
  231. byte = TOIM3232_PW|TOIM3232_19200;
  232. break;
  233. case 38400:
  234. byte = TOIM3232_PW|TOIM3232_38400;
  235. break;
  236. case 57600:
  237. byte = TOIM3232_PW|TOIM3232_57600;
  238. break;
  239. case 115200:
  240. byte = TOIM3232_115200;
  241. break;
  242. }
  243. /* Set DTR, Clear RTS: Go into baud programming mode */
  244. sirdev_set_dtr_rts(dev, TRUE, FALSE);
  245. /* Wait at least 7us */
  246. udelay(14);
  247. /* Write control byte */
  248. sirdev_raw_write(dev, &byte, 1);
  249. dev->speed = speed;
  250. state = TOIM3232_STATE_WAIT_SPEED;
  251. delay = toim3232delay;
  252. break;
  253. case TOIM3232_STATE_WAIT_SPEED:
  254. /* Have transmitted control byte * Wait for 'at least 1us' */
  255. udelay(14);
  256. /* Set DTR, Set RTS: Go into normal data mode */
  257. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  258. /* Wait (TODO: check this is needed) */
  259. udelay(50);
  260. break;
  261. default:
  262. printk(KERN_ERR "%s - undefined state %d\n", __func__, state);
  263. ret = -EINVAL;
  264. break;
  265. }
  266. dev->fsm.substate = state;
  267. return (delay > 0) ? delay : ret;
  268. }
  269. /*
  270. * Function toim3232reset (driver)
  271. *
  272. * This function resets the toim3232 dongle. Warning, this function
  273. * must be called with a process context!!
  274. *
  275. * What we should do is:
  276. * 0. Pull RESET high
  277. * 1. Wait for at least 7us
  278. * 2. Pull RESET low
  279. * 3. Wait for at least 7us
  280. * 4. Pull BR/~D high
  281. * 5. Wait for at least 7us
  282. * 6. Send control byte to set baud rate
  283. * 7. Wait at least 1us after stop bit
  284. * 8. Pull BR/~D low
  285. * 9. Should then be in data mode
  286. *
  287. * Because the IR320ST-2 doesn't have the RESET line connected for some reason,
  288. * we'll have to do something else.
  289. *
  290. * The default speed after a RESET is 9600, so lets try just bringing it up in
  291. * data mode after switching it off, waiting for the supply capacitor to
  292. * discharge, and then switch it back on. This isn't actually pulling RESET
  293. * high, but it seems to have the same effect.
  294. *
  295. * This behaviour will probably work on dongles that have the RESET line connected,
  296. * but if not, add a flag for the IR320ST-2, and implment the above-listed proper
  297. * behaviour.
  298. *
  299. * RTS is inverted and then fed to BR/~D, so to put it in programming mode, we
  300. * need to have pull RTS low
  301. */
  302. static int toim3232_reset(struct sir_dev *dev)
  303. {
  304. /* Switch off both DTR and RTS to switch off dongle */
  305. sirdev_set_dtr_rts(dev, FALSE, FALSE);
  306. /* Should sleep a while. This might be evil doing it this way.*/
  307. set_current_state(TASK_UNINTERRUPTIBLE);
  308. schedule_timeout(msecs_to_jiffies(50));
  309. /* Set DTR, Set RTS (data mode) */
  310. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  311. /* Wait at least 10 ms for power to stabilize again */
  312. set_current_state(TASK_UNINTERRUPTIBLE);
  313. schedule_timeout(msecs_to_jiffies(10));
  314. /* Speed should now be 9600 */
  315. dev->speed = 9600;
  316. return 0;
  317. }
  318. MODULE_AUTHOR("David Basden <davidb-linux@rcpt.to>");
  319. MODULE_DESCRIPTION("Vishay/Temic TOIM3232 based dongle driver");
  320. MODULE_LICENSE("GPL");
  321. MODULE_ALIAS("irda-dongle-12"); /* IRDA_TOIM3232_DONGLE */
  322. module_init(toim3232_sir_init);
  323. module_exit(toim3232_sir_cleanup);