toim3232-sir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. static int toim3232_open(struct sir_dev *);
  128. static int toim3232_close(struct sir_dev *);
  129. static int toim3232_change_speed(struct sir_dev *, unsigned);
  130. static int toim3232_reset(struct sir_dev *);
  131. #define TOIM3232_115200 0x00
  132. #define TOIM3232_57600 0x01
  133. #define TOIM3232_38400 0x02
  134. #define TOIM3232_19200 0x03
  135. #define TOIM3232_9600 0x06
  136. #define TOIM3232_2400 0x0A
  137. #define TOIM3232_PW 0x10 /* Pulse select bit */
  138. static struct dongle_driver toim3232 = {
  139. .owner = THIS_MODULE,
  140. .driver_name = "Vishay TOIM3232",
  141. .type = IRDA_TOIM3232_DONGLE,
  142. .open = toim3232_open,
  143. .close = toim3232_close,
  144. .reset = toim3232_reset,
  145. .set_speed = toim3232_change_speed,
  146. };
  147. static int __init toim3232_sir_init(void)
  148. {
  149. if (toim3232delay < 1 || toim3232delay > 500)
  150. toim3232delay = 200;
  151. pr_debug("%s - using %d ms delay\n",
  152. toim3232.driver_name, toim3232delay);
  153. return irda_register_dongle(&toim3232);
  154. }
  155. static void __exit toim3232_sir_cleanup(void)
  156. {
  157. irda_unregister_dongle(&toim3232);
  158. }
  159. static int toim3232_open(struct sir_dev *dev)
  160. {
  161. struct qos_info *qos = &dev->qos;
  162. /* Pull the lines high to start with.
  163. *
  164. * For the IR320ST-2, we need to charge the main supply capacitor to
  165. * switch the device on. We keep DTR high throughout to do this.
  166. * When RTS, TD and RD are high, they will also trickle-charge the
  167. * cap. RTS is high for data transmission, and low for baud rate select.
  168. * -- DGB
  169. */
  170. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  171. /* The TOI3232 supports many speeds between 1200bps and 115000bps.
  172. * We really only care about those supported by the IRDA spec, but
  173. * 38400 seems to be implemented in many places */
  174. qos->baud_rate.bits &= IR_2400|IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
  175. /* From the tekram driver. Not sure what a reasonable value is -- DGB */
  176. qos->min_turn_time.bits = 0x01; /* Needs at least 10 ms */
  177. irda_qos_bits_to_value(qos);
  178. /* irda thread waits 50 msec for power settling */
  179. return 0;
  180. }
  181. static int toim3232_close(struct sir_dev *dev)
  182. {
  183. /* Power off dongle */
  184. sirdev_set_dtr_rts(dev, FALSE, FALSE);
  185. return 0;
  186. }
  187. /*
  188. * Function toim3232change_speed (dev, state, speed)
  189. *
  190. * Set the speed for the TOIM3232 based dongle. Warning, this
  191. * function must be called with a process context!
  192. *
  193. * Algorithm
  194. * 1. keep DTR high but clear RTS to bring into baud programming mode
  195. * 2. wait at least 7us to enter programming mode
  196. * 3. send control word to set baud rate and timing
  197. * 4. wait at least 1us
  198. * 5. bring RTS high to enter DATA mode (RS232 is passed through to transceiver)
  199. * 6. should take effect immediately (although probably worth waiting)
  200. */
  201. #define TOIM3232_STATE_WAIT_SPEED (SIRDEV_STATE_DONGLE_SPEED + 1)
  202. static int toim3232_change_speed(struct sir_dev *dev, unsigned speed)
  203. {
  204. unsigned state = dev->fsm.substate;
  205. unsigned delay = 0;
  206. u8 byte;
  207. static int ret = 0;
  208. switch(state) {
  209. case SIRDEV_STATE_DONGLE_SPEED:
  210. /* Figure out what we are going to send as a control byte */
  211. switch (speed) {
  212. case 2400:
  213. byte = TOIM3232_PW|TOIM3232_2400;
  214. break;
  215. default:
  216. speed = 9600;
  217. ret = -EINVAL;
  218. /* fall thru */
  219. case 9600:
  220. byte = TOIM3232_PW|TOIM3232_9600;
  221. break;
  222. case 19200:
  223. byte = TOIM3232_PW|TOIM3232_19200;
  224. break;
  225. case 38400:
  226. byte = TOIM3232_PW|TOIM3232_38400;
  227. break;
  228. case 57600:
  229. byte = TOIM3232_PW|TOIM3232_57600;
  230. break;
  231. case 115200:
  232. byte = TOIM3232_115200;
  233. break;
  234. }
  235. /* Set DTR, Clear RTS: Go into baud programming mode */
  236. sirdev_set_dtr_rts(dev, TRUE, FALSE);
  237. /* Wait at least 7us */
  238. udelay(14);
  239. /* Write control byte */
  240. sirdev_raw_write(dev, &byte, 1);
  241. dev->speed = speed;
  242. state = TOIM3232_STATE_WAIT_SPEED;
  243. delay = toim3232delay;
  244. break;
  245. case TOIM3232_STATE_WAIT_SPEED:
  246. /* Have transmitted control byte * Wait for 'at least 1us' */
  247. udelay(14);
  248. /* Set DTR, Set RTS: Go into normal data mode */
  249. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  250. /* Wait (TODO: check this is needed) */
  251. udelay(50);
  252. break;
  253. default:
  254. printk(KERN_ERR "%s - undefined state %d\n", __func__, state);
  255. ret = -EINVAL;
  256. break;
  257. }
  258. dev->fsm.substate = state;
  259. return (delay > 0) ? delay : ret;
  260. }
  261. /*
  262. * Function toim3232reset (driver)
  263. *
  264. * This function resets the toim3232 dongle. Warning, this function
  265. * must be called with a process context!!
  266. *
  267. * What we should do is:
  268. * 0. Pull RESET high
  269. * 1. Wait for at least 7us
  270. * 2. Pull RESET low
  271. * 3. Wait for at least 7us
  272. * 4. Pull BR/~D high
  273. * 5. Wait for at least 7us
  274. * 6. Send control byte to set baud rate
  275. * 7. Wait at least 1us after stop bit
  276. * 8. Pull BR/~D low
  277. * 9. Should then be in data mode
  278. *
  279. * Because the IR320ST-2 doesn't have the RESET line connected for some reason,
  280. * we'll have to do something else.
  281. *
  282. * The default speed after a RESET is 9600, so lets try just bringing it up in
  283. * data mode after switching it off, waiting for the supply capacitor to
  284. * discharge, and then switch it back on. This isn't actually pulling RESET
  285. * high, but it seems to have the same effect.
  286. *
  287. * This behaviour will probably work on dongles that have the RESET line connected,
  288. * but if not, add a flag for the IR320ST-2, and implment the above-listed proper
  289. * behaviour.
  290. *
  291. * RTS is inverted and then fed to BR/~D, so to put it in programming mode, we
  292. * need to have pull RTS low
  293. */
  294. static int toim3232_reset(struct sir_dev *dev)
  295. {
  296. /* Switch off both DTR and RTS to switch off dongle */
  297. sirdev_set_dtr_rts(dev, FALSE, FALSE);
  298. /* Should sleep a while. This might be evil doing it this way.*/
  299. set_current_state(TASK_UNINTERRUPTIBLE);
  300. schedule_timeout(msecs_to_jiffies(50));
  301. /* Set DTR, Set RTS (data mode) */
  302. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  303. /* Wait at least 10 ms for power to stabilize again */
  304. set_current_state(TASK_UNINTERRUPTIBLE);
  305. schedule_timeout(msecs_to_jiffies(10));
  306. /* Speed should now be 9600 */
  307. dev->speed = 9600;
  308. return 0;
  309. }
  310. MODULE_AUTHOR("David Basden <davidb-linux@rcpt.to>");
  311. MODULE_DESCRIPTION("Vishay/Temic TOIM3232 based dongle driver");
  312. MODULE_LICENSE("GPL");
  313. MODULE_ALIAS("irda-dongle-12"); /* IRDA_TOIM3232_DONGLE */
  314. module_init(toim3232_sir_init);
  315. module_exit(toim3232_sir_cleanup);