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