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