tekram.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*********************************************************************
  2. *
  3. * Filename: tekram.c
  4. * Version: 1.2
  5. * Description: Implementation of the Tekram IrMate IR-210B dongle
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Wed Oct 21 20:02:35 1998
  9. * Modified at: Fri Dec 17 09:13:09 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * Neither Dag Brattli nor University of Tromsø admit liability nor
  20. * provide warranty for any of this software. This material is
  21. * provided "AS-IS" and at no charge.
  22. *
  23. ********************************************************************/
  24. #include <linux/module.h>
  25. #include <linux/delay.h>
  26. #include <linux/tty.h>
  27. #include <linux/init.h>
  28. #include <net/irda/irda.h>
  29. #include <net/irda/irda_device.h>
  30. static void tekram_open(dongle_t *self, struct qos_info *qos);
  31. static void tekram_close(dongle_t *self);
  32. static int tekram_change_speed(struct irda_task *task);
  33. static int tekram_reset(struct irda_task *task);
  34. #define TEKRAM_115200 0x00
  35. #define TEKRAM_57600 0x01
  36. #define TEKRAM_38400 0x02
  37. #define TEKRAM_19200 0x03
  38. #define TEKRAM_9600 0x04
  39. #define TEKRAM_PW 0x10 /* Pulse select bit */
  40. static struct dongle_reg dongle = {
  41. .type = IRDA_TEKRAM_DONGLE,
  42. .open = tekram_open,
  43. .close = tekram_close,
  44. .reset = tekram_reset,
  45. .change_speed = tekram_change_speed,
  46. .owner = THIS_MODULE,
  47. };
  48. static int __init tekram_init(void)
  49. {
  50. return irda_device_register_dongle(&dongle);
  51. }
  52. static void __exit tekram_cleanup(void)
  53. {
  54. irda_device_unregister_dongle(&dongle);
  55. }
  56. static void tekram_open(dongle_t *self, struct qos_info *qos)
  57. {
  58. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  59. qos->baud_rate.bits &= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
  60. qos->min_turn_time.bits = 0x01; /* Needs at least 10 ms */
  61. irda_qos_bits_to_value(qos);
  62. }
  63. static void tekram_close(dongle_t *self)
  64. {
  65. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  66. /* Power off dongle */
  67. self->set_dtr_rts(self->dev, FALSE, FALSE);
  68. if (self->reset_task)
  69. irda_task_delete(self->reset_task);
  70. if (self->speed_task)
  71. irda_task_delete(self->speed_task);
  72. }
  73. /*
  74. * Function tekram_change_speed (dev, state, speed)
  75. *
  76. * Set the speed for the Tekram IRMate 210 type dongle. Warning, this
  77. * function must be called with a process context!
  78. *
  79. * Algorithm
  80. * 1. clear DTR
  81. * 2. set RTS, and wait at least 7 us
  82. * 3. send Control Byte to the IR-210 through TXD to set new baud rate
  83. * wait until the stop bit of Control Byte is sent (for 9600 baud rate,
  84. * it takes about 100 msec)
  85. * 5. clear RTS (return to NORMAL Operation)
  86. * 6. wait at least 50 us, new setting (baud rate, etc) takes effect here
  87. * after
  88. */
  89. static int tekram_change_speed(struct irda_task *task)
  90. {
  91. dongle_t *self = (dongle_t *) task->instance;
  92. __u32 speed = (__u32) task->param;
  93. __u8 byte;
  94. int ret = 0;
  95. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  96. IRDA_ASSERT(task != NULL, return -1;);
  97. if (self->speed_task && self->speed_task != task) {
  98. IRDA_DEBUG(0, "%s(), busy!\n", __FUNCTION__ );
  99. return msecs_to_jiffies(10);
  100. } else
  101. self->speed_task = task;
  102. switch (speed) {
  103. default:
  104. case 9600:
  105. byte = TEKRAM_PW|TEKRAM_9600;
  106. break;
  107. case 19200:
  108. byte = TEKRAM_PW|TEKRAM_19200;
  109. break;
  110. case 38400:
  111. byte = TEKRAM_PW|TEKRAM_38400;
  112. break;
  113. case 57600:
  114. byte = TEKRAM_PW|TEKRAM_57600;
  115. break;
  116. case 115200:
  117. byte = TEKRAM_115200;
  118. break;
  119. }
  120. switch (task->state) {
  121. case IRDA_TASK_INIT:
  122. case IRDA_TASK_CHILD_INIT:
  123. /*
  124. * Need to reset the dongle and go to 9600 bps before
  125. * programming
  126. */
  127. if (irda_task_execute(self, tekram_reset, NULL, task,
  128. (void *) speed))
  129. {
  130. /* Dongle need more time to reset */
  131. irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
  132. /* Give reset 1 sec to finish */
  133. ret = msecs_to_jiffies(1000);
  134. } else
  135. irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
  136. break;
  137. case IRDA_TASK_CHILD_WAIT:
  138. IRDA_WARNING("%s(), resetting dongle timed out!\n",
  139. __FUNCTION__);
  140. ret = -1;
  141. break;
  142. case IRDA_TASK_CHILD_DONE:
  143. /* Set DTR, Clear RTS */
  144. self->set_dtr_rts(self->dev, TRUE, FALSE);
  145. /* Wait at least 7us */
  146. udelay(14);
  147. /* Write control byte */
  148. self->write(self->dev, &byte, 1);
  149. irda_task_next_state(task, IRDA_TASK_WAIT);
  150. /* Wait at least 100 ms */
  151. ret = msecs_to_jiffies(150);
  152. break;
  153. case IRDA_TASK_WAIT:
  154. /* Set DTR, Set RTS */
  155. self->set_dtr_rts(self->dev, TRUE, TRUE);
  156. irda_task_next_state(task, IRDA_TASK_DONE);
  157. self->speed_task = NULL;
  158. break;
  159. default:
  160. IRDA_ERROR("%s(), unknown state %d\n",
  161. __FUNCTION__, task->state);
  162. irda_task_next_state(task, IRDA_TASK_DONE);
  163. self->speed_task = NULL;
  164. ret = -1;
  165. break;
  166. }
  167. return ret;
  168. }
  169. /*
  170. * Function tekram_reset (driver)
  171. *
  172. * This function resets the tekram dongle. Warning, this function
  173. * must be called with a process context!!
  174. *
  175. * Algorithm:
  176. * 0. Clear RTS and DTR, and wait 50 ms (power off the IR-210 )
  177. * 1. clear RTS
  178. * 2. set DTR, and wait at least 1 ms
  179. * 3. clear DTR to SPACE state, wait at least 50 us for further
  180. * operation
  181. */
  182. int tekram_reset(struct irda_task *task)
  183. {
  184. dongle_t *self = (dongle_t *) task->instance;
  185. int ret = 0;
  186. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  187. IRDA_ASSERT(task != NULL, return -1;);
  188. if (self->reset_task && self->reset_task != task) {
  189. IRDA_DEBUG(0, "%s(), busy!\n", __FUNCTION__ );
  190. return msecs_to_jiffies(10);
  191. } else
  192. self->reset_task = task;
  193. /* Power off dongle */
  194. //self->set_dtr_rts(self->dev, FALSE, FALSE);
  195. self->set_dtr_rts(self->dev, TRUE, TRUE);
  196. switch (task->state) {
  197. case IRDA_TASK_INIT:
  198. irda_task_next_state(task, IRDA_TASK_WAIT1);
  199. /* Sleep 50 ms */
  200. ret = msecs_to_jiffies(50);
  201. break;
  202. case IRDA_TASK_WAIT1:
  203. /* Clear DTR, Set RTS */
  204. self->set_dtr_rts(self->dev, FALSE, TRUE);
  205. irda_task_next_state(task, IRDA_TASK_WAIT2);
  206. /* Should sleep 1 ms */
  207. ret = msecs_to_jiffies(1);
  208. break;
  209. case IRDA_TASK_WAIT2:
  210. /* Set DTR, Set RTS */
  211. self->set_dtr_rts(self->dev, TRUE, TRUE);
  212. /* Wait at least 50 us */
  213. udelay(75);
  214. irda_task_next_state(task, IRDA_TASK_DONE);
  215. self->reset_task = NULL;
  216. break;
  217. default:
  218. IRDA_ERROR("%s(), unknown state %d\n",
  219. __FUNCTION__, task->state);
  220. irda_task_next_state(task, IRDA_TASK_DONE);
  221. self->reset_task = NULL;
  222. ret = -1;
  223. }
  224. return ret;
  225. }
  226. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  227. MODULE_DESCRIPTION("Tekram IrMate IR-210B dongle driver");
  228. MODULE_LICENSE("GPL");
  229. MODULE_ALIAS("irda-dongle-0"); /* IRDA_TEKRAM_DONGLE */
  230. /*
  231. * Function init_module (void)
  232. *
  233. * Initialize Tekram module
  234. *
  235. */
  236. module_init(tekram_init);
  237. /*
  238. * Function cleanup_module (void)
  239. *
  240. * Cleanup Tekram module
  241. *
  242. */
  243. module_exit(tekram_cleanup);