ma600-sir.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. *
  32. ********************************************************************/
  33. #include <linux/module.h>
  34. #include <linux/delay.h>
  35. #include <linux/init.h>
  36. #include <net/irda/irda.h>
  37. #include "sir-dev.h"
  38. static int ma600_open(struct sir_dev *);
  39. static int ma600_close(struct sir_dev *);
  40. static int ma600_change_speed(struct sir_dev *, unsigned);
  41. static int ma600_reset(struct sir_dev *);
  42. /* control byte for MA600 */
  43. #define MA600_9600 0x00
  44. #define MA600_19200 0x01
  45. #define MA600_38400 0x02
  46. #define MA600_57600 0x03
  47. #define MA600_115200 0x04
  48. #define MA600_DEV_ID1 0x05
  49. #define MA600_DEV_ID2 0x06
  50. #define MA600_2400 0x08
  51. static struct dongle_driver ma600 = {
  52. .owner = THIS_MODULE,
  53. .driver_name = "MA600",
  54. .type = IRDA_MA600_DONGLE,
  55. .open = ma600_open,
  56. .close = ma600_close,
  57. .reset = ma600_reset,
  58. .set_speed = ma600_change_speed,
  59. };
  60. static int __init ma600_sir_init(void)
  61. {
  62. IRDA_DEBUG(2, "%s()\n", __func__);
  63. return irda_register_dongle(&ma600);
  64. }
  65. static void __exit ma600_sir_cleanup(void)
  66. {
  67. IRDA_DEBUG(2, "%s()\n", __func__);
  68. irda_unregister_dongle(&ma600);
  69. }
  70. /*
  71. Power on:
  72. (0) Clear RTS and DTR for 1 second
  73. (1) Set RTS and DTR for 1 second
  74. (2) 9600 bps now
  75. Note: assume RTS, DTR are clear before
  76. */
  77. static int ma600_open(struct sir_dev *dev)
  78. {
  79. struct qos_info *qos = &dev->qos;
  80. IRDA_DEBUG(2, "%s()\n", __func__);
  81. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  82. /* Explicitly set the speeds we can accept */
  83. qos->baud_rate.bits &= IR_2400|IR_9600|IR_19200|IR_38400
  84. |IR_57600|IR_115200;
  85. /* Hm, 0x01 means 10ms - for >= 1ms we would need 0x07 */
  86. qos->min_turn_time.bits = 0x01; /* Needs at least 1 ms */
  87. irda_qos_bits_to_value(qos);
  88. /* irda thread waits 50 msec for power settling */
  89. return 0;
  90. }
  91. static int ma600_close(struct sir_dev *dev)
  92. {
  93. IRDA_DEBUG(2, "%s()\n", __func__);
  94. /* Power off dongle */
  95. sirdev_set_dtr_rts(dev, FALSE, FALSE);
  96. return 0;
  97. }
  98. static __u8 get_control_byte(__u32 speed)
  99. {
  100. __u8 byte;
  101. switch (speed) {
  102. default:
  103. case 115200:
  104. byte = MA600_115200;
  105. break;
  106. case 57600:
  107. byte = MA600_57600;
  108. break;
  109. case 38400:
  110. byte = MA600_38400;
  111. break;
  112. case 19200:
  113. byte = MA600_19200;
  114. break;
  115. case 9600:
  116. byte = MA600_9600;
  117. break;
  118. case 2400:
  119. byte = MA600_2400;
  120. break;
  121. }
  122. return byte;
  123. }
  124. /*
  125. * Function ma600_change_speed (dev, speed)
  126. *
  127. * Set the speed for the MA600 type dongle.
  128. *
  129. * The dongle has already been reset to a known state (dongle default)
  130. * We cycle through speeds by pulsing RTS low and then high.
  131. */
  132. /*
  133. * Function ma600_change_speed (dev, speed)
  134. *
  135. * Set the speed for the MA600 type dongle.
  136. *
  137. * Algorithm
  138. * 1. Reset (already done by irda thread state machine)
  139. * 2. clear RTS, set DTR and wait for 1ms
  140. * 3. send Control Byte to the MA600 through TXD to set new baud rate
  141. * wait until the stop bit of Control Byte is sent (for 9600 baud rate,
  142. * it takes about 10 msec)
  143. * 4. set RTS, set DTR (return to NORMAL Operation)
  144. * 5. wait at least 10 ms, new setting (baud rate, etc) takes effect here
  145. * after
  146. */
  147. /* total delays are only about 20ms - let's just sleep for now to
  148. * avoid the state machine complexity before we get things working
  149. */
  150. static int ma600_change_speed(struct sir_dev *dev, unsigned speed)
  151. {
  152. u8 byte;
  153. IRDA_DEBUG(2, "%s(), speed=%d (was %d)\n", __func__,
  154. speed, dev->speed);
  155. /* dongle already reset, dongle and port at default speed (9600) */
  156. /* Set RTS low for 1 ms */
  157. sirdev_set_dtr_rts(dev, TRUE, FALSE);
  158. mdelay(1);
  159. /* Write control byte */
  160. byte = get_control_byte(speed);
  161. sirdev_raw_write(dev, &byte, sizeof(byte));
  162. /* Wait at least 10ms: fake wait_until_sent - 10 bits at 9600 baud*/
  163. msleep(15); /* old ma600 uses 15ms */
  164. #if 1
  165. /* read-back of the control byte. ma600 is the first dongle driver
  166. * which uses this so there might be some unidentified issues.
  167. * Disable this in case of problems with readback.
  168. */
  169. sirdev_raw_read(dev, &byte, sizeof(byte));
  170. if (byte != get_control_byte(speed)) {
  171. IRDA_WARNING("%s(): bad control byte read-back %02x != %02x\n",
  172. __func__, (unsigned) byte,
  173. (unsigned) get_control_byte(speed));
  174. return -1;
  175. }
  176. else
  177. IRDA_DEBUG(2, "%s() control byte write read OK\n", __func__);
  178. #endif
  179. /* Set DTR, Set RTS */
  180. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  181. /* Wait at least 10ms */
  182. msleep(10);
  183. /* dongle is now switched to the new speed */
  184. dev->speed = speed;
  185. return 0;
  186. }
  187. /*
  188. * Function ma600_reset (dev)
  189. *
  190. * This function resets the ma600 dongle.
  191. *
  192. * Algorithm:
  193. * 0. DTR=0, RTS=1 and wait 10 ms
  194. * 1. DTR=1, RTS=1 and wait 10 ms
  195. * 2. 9600 bps now
  196. */
  197. /* total delays are only about 20ms - let's just sleep for now to
  198. * avoid the state machine complexity before we get things working
  199. */
  200. static int ma600_reset(struct sir_dev *dev)
  201. {
  202. IRDA_DEBUG(2, "%s()\n", __func__);
  203. /* Reset the dongle : set DTR low for 10 ms */
  204. sirdev_set_dtr_rts(dev, FALSE, TRUE);
  205. msleep(10);
  206. /* Go back to normal mode */
  207. sirdev_set_dtr_rts(dev, TRUE, TRUE);
  208. msleep(10);
  209. dev->speed = 9600; /* That's the dongle-default */
  210. return 0;
  211. }
  212. MODULE_AUTHOR("Leung <95Etwl@alumni.ee.ust.hk> http://www.engsvr.ust/~eetwl95");
  213. MODULE_DESCRIPTION("MA600 dongle driver version 0.1");
  214. MODULE_LICENSE("GPL");
  215. MODULE_ALIAS("irda-dongle-11"); /* IRDA_MA600_DONGLE */
  216. module_init(ma600_sir_init);
  217. module_exit(ma600_sir_cleanup);