girbil.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*********************************************************************
  2. *
  3. * Filename: girbil.c
  4. * Version: 1.2
  5. * Description: Implementation for the Greenwich GIrBIL dongle
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sat Feb 6 21:02:33 1999
  9. * Modified at: Fri Dec 17 09:13:20 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 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 int girbil_reset(struct irda_task *task);
  31. static void girbil_open(dongle_t *self, struct qos_info *qos);
  32. static void girbil_close(dongle_t *self);
  33. static int girbil_change_speed(struct irda_task *task);
  34. /* Control register 1 */
  35. #define GIRBIL_TXEN 0x01 /* Enable transmitter */
  36. #define GIRBIL_RXEN 0x02 /* Enable receiver */
  37. #define GIRBIL_ECAN 0x04 /* Cancel self emmited data */
  38. #define GIRBIL_ECHO 0x08 /* Echo control characters */
  39. /* LED Current Register (0x2) */
  40. #define GIRBIL_HIGH 0x20
  41. #define GIRBIL_MEDIUM 0x21
  42. #define GIRBIL_LOW 0x22
  43. /* Baud register (0x3) */
  44. #define GIRBIL_2400 0x30
  45. #define GIRBIL_4800 0x31
  46. #define GIRBIL_9600 0x32
  47. #define GIRBIL_19200 0x33
  48. #define GIRBIL_38400 0x34
  49. #define GIRBIL_57600 0x35
  50. #define GIRBIL_115200 0x36
  51. /* Mode register (0x4) */
  52. #define GIRBIL_IRDA 0x40
  53. #define GIRBIL_ASK 0x41
  54. /* Control register 2 (0x5) */
  55. #define GIRBIL_LOAD 0x51 /* Load the new baud rate value */
  56. static struct dongle_reg dongle = {
  57. .type = IRDA_GIRBIL_DONGLE,
  58. .open = girbil_open,
  59. .close = girbil_close,
  60. .reset = girbil_reset,
  61. .change_speed = girbil_change_speed,
  62. .owner = THIS_MODULE,
  63. };
  64. static int __init girbil_init(void)
  65. {
  66. return irda_device_register_dongle(&dongle);
  67. }
  68. static void __exit girbil_cleanup(void)
  69. {
  70. irda_device_unregister_dongle(&dongle);
  71. }
  72. static void girbil_open(dongle_t *self, struct qos_info *qos)
  73. {
  74. qos->baud_rate.bits &= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
  75. qos->min_turn_time.bits = 0x03;
  76. }
  77. static void girbil_close(dongle_t *self)
  78. {
  79. /* Power off dongle */
  80. self->set_dtr_rts(self->dev, FALSE, FALSE);
  81. }
  82. /*
  83. * Function girbil_change_speed (dev, speed)
  84. *
  85. * Set the speed for the Girbil type dongle.
  86. *
  87. */
  88. static int girbil_change_speed(struct irda_task *task)
  89. {
  90. dongle_t *self = (dongle_t *) task->instance;
  91. __u32 speed = (__u32) task->param;
  92. __u8 control[2];
  93. int ret = 0;
  94. self->speed_task = task;
  95. switch (task->state) {
  96. case IRDA_TASK_INIT:
  97. /* Need to reset the dongle and go to 9600 bps before
  98. programming */
  99. if (irda_task_execute(self, girbil_reset, NULL, task,
  100. (void *) speed))
  101. {
  102. /* Dongle need more time to reset */
  103. irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
  104. /* Give reset 1 sec to finish */
  105. ret = msecs_to_jiffies(1000);
  106. }
  107. break;
  108. case IRDA_TASK_CHILD_WAIT:
  109. IRDA_WARNING("%s(), resetting dongle timed out!\n",
  110. __FUNCTION__);
  111. ret = -1;
  112. break;
  113. case IRDA_TASK_CHILD_DONE:
  114. /* Set DTR and Clear RTS to enter command mode */
  115. self->set_dtr_rts(self->dev, FALSE, TRUE);
  116. switch (speed) {
  117. case 9600:
  118. default:
  119. control[0] = GIRBIL_9600;
  120. break;
  121. case 19200:
  122. control[0] = GIRBIL_19200;
  123. break;
  124. case 34800:
  125. control[0] = GIRBIL_38400;
  126. break;
  127. case 57600:
  128. control[0] = GIRBIL_57600;
  129. break;
  130. case 115200:
  131. control[0] = GIRBIL_115200;
  132. break;
  133. }
  134. control[1] = GIRBIL_LOAD;
  135. /* Write control bytes */
  136. self->write(self->dev, control, 2);
  137. irda_task_next_state(task, IRDA_TASK_WAIT);
  138. ret = msecs_to_jiffies(100);
  139. break;
  140. case IRDA_TASK_WAIT:
  141. /* Go back to normal mode */
  142. self->set_dtr_rts(self->dev, TRUE, TRUE);
  143. irda_task_next_state(task, IRDA_TASK_DONE);
  144. self->speed_task = NULL;
  145. break;
  146. default:
  147. IRDA_ERROR("%s(), unknown state %d\n",
  148. __FUNCTION__, task->state);
  149. irda_task_next_state(task, IRDA_TASK_DONE);
  150. self->speed_task = NULL;
  151. ret = -1;
  152. break;
  153. }
  154. return ret;
  155. }
  156. /*
  157. * Function girbil_reset (driver)
  158. *
  159. * This function resets the girbil dongle.
  160. *
  161. * Algorithm:
  162. * 0. set RTS, and wait at least 5 ms
  163. * 1. clear RTS
  164. */
  165. static int girbil_reset(struct irda_task *task)
  166. {
  167. dongle_t *self = (dongle_t *) task->instance;
  168. __u8 control = GIRBIL_TXEN | GIRBIL_RXEN;
  169. int ret = 0;
  170. self->reset_task = task;
  171. switch (task->state) {
  172. case IRDA_TASK_INIT:
  173. /* Reset dongle */
  174. self->set_dtr_rts(self->dev, TRUE, FALSE);
  175. irda_task_next_state(task, IRDA_TASK_WAIT1);
  176. /* Sleep at least 5 ms */
  177. ret = msecs_to_jiffies(20);
  178. break;
  179. case IRDA_TASK_WAIT1:
  180. /* Set DTR and clear RTS to enter command mode */
  181. self->set_dtr_rts(self->dev, FALSE, TRUE);
  182. irda_task_next_state(task, IRDA_TASK_WAIT2);
  183. ret = msecs_to_jiffies(20);
  184. break;
  185. case IRDA_TASK_WAIT2:
  186. /* Write control byte */
  187. self->write(self->dev, &control, 1);
  188. irda_task_next_state(task, IRDA_TASK_WAIT3);
  189. ret = msecs_to_jiffies(20);
  190. break;
  191. case IRDA_TASK_WAIT3:
  192. /* Go back to normal mode */
  193. self->set_dtr_rts(self->dev, TRUE, TRUE);
  194. irda_task_next_state(task, IRDA_TASK_DONE);
  195. self->reset_task = NULL;
  196. break;
  197. default:
  198. IRDA_ERROR("%s(), unknown state %d\n",
  199. __FUNCTION__, task->state);
  200. irda_task_next_state(task, IRDA_TASK_DONE);
  201. self->reset_task = NULL;
  202. ret = -1;
  203. break;
  204. }
  205. return ret;
  206. }
  207. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  208. MODULE_DESCRIPTION("Greenwich GIrBIL dongle driver");
  209. MODULE_LICENSE("GPL");
  210. MODULE_ALIAS("irda-dongle-4"); /* IRDA_GIRBIL_DONGLE */
  211. /*
  212. * Function init_module (void)
  213. *
  214. * Initialize Girbil module
  215. *
  216. */
  217. module_init(girbil_init);
  218. /*
  219. * Function cleanup_module (void)
  220. *
  221. * Cleanup Girbil module
  222. *
  223. */
  224. module_exit(girbil_cleanup);