ma600-sir.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*********************************************************************
  2. *
  3. * Filename: ma600.c
  4. * Version: 0.1
  5. * Description: Implementation of the MA600 dongle
  6. * Status: Experimental.
  7. * Author: Leung <95Etwl@alumni.ee.ust.hk> http://www.engsvr.ust/~eetwl95
  8. * Created at: Sat Jun 10 20:02:35 2000
  9. * Modified at: Sat Aug 16 09:34:13 2003
  10. * Modified by: Martin Diehl <mad@mdiehl.de> (modified for new sir_dev)
  11. *
  12. * Note: very thanks to Mr. Maru Wang <maru@mobileaction.com.tw> for providing
  13. * information on the MA600 dongle
  14. *
  15. * Copyright (c) 2000 Leung, All Rights Reserved.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  29. *
  30. ********************************************************************/
  31. #include <linux/module.h>
  32. #include <linux/delay.h>
  33. #include <linux/init.h>
  34. #include <net/irda/irda.h>
  35. #include "sir-dev.h"
  36. static int ma600_open(struct sir_dev *);
  37. static int ma600_close(struct sir_dev *);
  38. static int ma600_change_speed(struct sir_dev *, unsigned);
  39. static int ma600_reset(struct sir_dev *);
  40. /* control byte for MA600 */
  41. #define MA600_9600 0x00
  42. #define MA600_19200 0x01
  43. #define MA600_38400 0x02
  44. #define MA600_57600 0x03
  45. #define MA600_115200 0x04
  46. #define MA600_DEV_ID1 0x05
  47. #define MA600_DEV_ID2 0x06
  48. #define MA600_2400 0x08
  49. static struct dongle_driver ma600 = {
  50. .owner = THIS_MODULE,
  51. .driver_name = "MA600",
  52. .type = IRDA_MA600_DONGLE,
  53. .open = ma600_open,
  54. .close = ma600_close,
  55. .reset = ma600_reset,
  56. .set_speed = ma600_change_speed,
  57. };
  58. static int __init ma600_sir_init(void)
  59. {
  60. return irda_register_dongle(&ma600);
  61. }
  62. static void __exit ma600_sir_cleanup(void)
  63. {
  64. irda_unregister_dongle(&ma600);
  65. }
  66. /*
  67. Power on:
  68. (0) Clear RTS and DTR for 1 second
  69. (1) Set RTS and DTR for 1 second
  70. (2) 9600 bps now
  71. Note: assume RTS, DTR are clear before
  72. */
  73. static int ma600_open(struct sir_dev *dev)
  74. {
  75. struct qos_info *qos = &dev->qos;
  76. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  77. /* Explicitly set the speeds we can accept */
  78. qos->baud_rate.bits &= IR_2400|IR_9600|IR_19200|IR_38400
  79. |IR_57600|IR_115200;
  80. /* Hm, 0x01 means 10ms - for >= 1ms we would need 0x07 */
  81. qos->min_turn_time.bits = 0x01; /* Needs at least 1 ms */
  82. irda_qos_bits_to_value(qos);
  83. /* irda thread waits 50 msec for power settling */
  84. return 0;
  85. }
  86. static int ma600_close(struct sir_dev *dev)
  87. {
  88. /* Power off dongle */
  89. sirdev_set_dtr_rts(dev, FALSE, FALSE);
  90. return 0;
  91. }
  92. static __u8 get_control_byte(__u32 speed)
  93. {
  94. __u8 byte;
  95. switch (speed) {
  96. default:
  97. case 115200:
  98. byte = MA600_115200;
  99. break;
  100. case 57600:
  101. byte = MA600_57600;
  102. break;
  103. case 38400:
  104. byte = MA600_38400;
  105. break;
  106. case 19200:
  107. byte = MA600_19200;
  108. break;
  109. case 9600:
  110. byte = MA600_9600;
  111. break;
  112. case 2400:
  113. byte = MA600_2400;
  114. break;
  115. }
  116. return byte;
  117. }
  118. /*
  119. * Function ma600_change_speed (dev, speed)
  120. *
  121. * Set the speed for the MA600 type dongle.
  122. *
  123. * The dongle has already been reset to a known state (dongle default)
  124. * We cycle through speeds by pulsing RTS low and then high.
  125. */
  126. /*
  127. * Function ma600_change_speed (dev, speed)
  128. *
  129. * Set the speed for the MA600 type dongle.
  130. *
  131. * Algorithm
  132. * 1. Reset (already done by irda thread state machine)
  133. * 2. clear RTS, set DTR and wait for 1ms
  134. * 3. send Control Byte to the MA600 through TXD to set new baud rate
  135. * wait until the stop bit of Control Byte is sent (for 9600 baud rate,
  136. * it takes about 10 msec)
  137. * 4. set RTS, set DTR (return to NORMAL Operation)
  138. * 5. wait at least 10 ms, new setting (baud rate, etc) takes effect here
  139. * after
  140. */
  141. /* total delays are only about 20ms - let's just sleep for now to
  142. * avoid the state machine complexity before we get things working
  143. */
  144. static int ma600_change_speed(struct sir_dev *dev, unsigned speed)
  145. {
  146. u8 byte;
  147. pr_debug("%s(), speed=%d (was %d)\n", __func__,
  148. speed, dev->speed);
  149. /* dongle already reset, dongle and port at default speed (9600) */
  150. /* Set RTS low for 1 ms */
  151. sirdev_set_dtr_rts(dev, TRUE, FALSE);
  152. mdelay(1);
  153. /* Write control byte */
  154. byte = get_control_byte(speed);
  155. sirdev_raw_write(dev, &byte, sizeof(byte));
  156. /* Wait at least 10ms: fake wait_until_sent - 10 bits at 9600 baud*/
  157. msleep(15); /* old ma600 uses 15ms */
  158. #if 1
  159. /* read-back of the control byte. ma600 is the first dongle driver
  160. * which uses this so there might be some unidentified issues.
  161. * Disable this in case of problems with readback.
  162. */
  163. sirdev_raw_read(dev, &byte, sizeof(byte));
  164. if (byte != get_control_byte(speed)) {
  165. net_warn_ratelimited("%s(): bad control byte read-back %02x != %02x\n",
  166. __func__, (unsigned)byte,
  167. (unsigned)get_control_byte(speed));
  168. return -1;
  169. }
  170. else
  171. pr_debug("%s() control byte write read OK\n", __func__);
  172. #endif
  173. /* Set DTR, Set RTS */
  174. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  175. /* Wait at least 10ms */
  176. msleep(10);
  177. /* dongle is now switched to the new speed */
  178. dev->speed = speed;
  179. return 0;
  180. }
  181. /*
  182. * Function ma600_reset (dev)
  183. *
  184. * This function resets the ma600 dongle.
  185. *
  186. * Algorithm:
  187. * 0. DTR=0, RTS=1 and wait 10 ms
  188. * 1. DTR=1, RTS=1 and wait 10 ms
  189. * 2. 9600 bps now
  190. */
  191. /* total delays are only about 20ms - let's just sleep for now to
  192. * avoid the state machine complexity before we get things working
  193. */
  194. static int ma600_reset(struct sir_dev *dev)
  195. {
  196. /* Reset the dongle : set DTR low for 10 ms */
  197. sirdev_set_dtr_rts(dev, FALSE, TRUE);
  198. msleep(10);
  199. /* Go back to normal mode */
  200. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  201. msleep(10);
  202. dev->speed = 9600; /* That's the dongle-default */
  203. return 0;
  204. }
  205. MODULE_AUTHOR("Leung <95Etwl@alumni.ee.ust.hk> http://www.engsvr.ust/~eetwl95");
  206. MODULE_DESCRIPTION("MA600 dongle driver version 0.1");
  207. MODULE_LICENSE("GPL");
  208. MODULE_ALIAS("irda-dongle-11"); /* IRDA_MA600_DONGLE */
  209. module_init(ma600_sir_init);
  210. module_exit(ma600_sir_cleanup);