Merge branch 'linux-2.6'
[linux-2.6] / 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                 IRDA_WARNING("%s(), resetting dongle timed out!\n",
127                              __FUNCTION__);
128                 ret = -1;
129                 break;
130         case IRDA_TASK_CHILD_DONE:
131                 /* Set DTR and Clear RTS to enter command mode */
132                 self->set_dtr_rts(self->dev, FALSE, TRUE);
133
134                 switch (speed) {
135                 case 9600:
136                 default:
137                         control[0] = GIRBIL_9600;
138                         break;
139                 case 19200:
140                         control[0] = GIRBIL_19200;
141                         break;
142                 case 34800:
143                         control[0] = GIRBIL_38400;
144                         break;
145                 case 57600:
146                         control[0] = GIRBIL_57600;
147                         break;
148                 case 115200:
149                         control[0] = GIRBIL_115200;
150                         break;
151                 }
152                 control[1] = GIRBIL_LOAD;
153                 
154                 /* Write control bytes */
155                 self->write(self->dev, control, 2);
156                 irda_task_next_state(task, IRDA_TASK_WAIT);
157                 ret = msecs_to_jiffies(100);
158                 break;
159         case IRDA_TASK_WAIT:
160                 /* Go back to normal mode */
161                 self->set_dtr_rts(self->dev, TRUE, TRUE);
162                 irda_task_next_state(task, IRDA_TASK_DONE);
163                 self->speed_task = NULL;
164                 break;
165         default:
166                 IRDA_ERROR("%s(), unknown state %d\n",
167                            __FUNCTION__, task->state);
168                 irda_task_next_state(task, IRDA_TASK_DONE);
169                 self->speed_task = NULL;
170                 ret = -1;
171                 break;
172         }
173         return ret;
174 }
175
176 /*
177  * Function girbil_reset (driver)
178  *
179  *      This function resets the girbil dongle.
180  *
181  *      Algorithm:
182  *        0. set RTS, and wait at least 5 ms 
183  *        1. clear RTS 
184  */
185 static int girbil_reset(struct irda_task *task)
186 {
187         dongle_t *self = (dongle_t *) task->instance;
188         __u8 control = GIRBIL_TXEN | GIRBIL_RXEN;
189         int ret = 0;
190
191         self->reset_task = task;
192
193         switch (task->state) {
194         case IRDA_TASK_INIT:
195                 /* Reset dongle */
196                 self->set_dtr_rts(self->dev, TRUE, FALSE);
197                 irda_task_next_state(task, IRDA_TASK_WAIT1);
198                 /* Sleep at least 5 ms */
199                 ret = msecs_to_jiffies(20);
200                 break;
201         case IRDA_TASK_WAIT1:
202                 /* Set DTR and clear RTS to enter command mode */
203                 self->set_dtr_rts(self->dev, FALSE, TRUE);
204                 irda_task_next_state(task, IRDA_TASK_WAIT2);
205                 ret = msecs_to_jiffies(20);
206                 break;
207         case IRDA_TASK_WAIT2:
208                 /* Write control byte */
209                 self->write(self->dev, &control, 1);
210                 irda_task_next_state(task, IRDA_TASK_WAIT3);
211                 ret = msecs_to_jiffies(20);
212                 break;
213         case IRDA_TASK_WAIT3:
214                 /* Go back to normal mode */
215                 self->set_dtr_rts(self->dev, TRUE, TRUE);
216                 irda_task_next_state(task, IRDA_TASK_DONE);
217                 self->reset_task = NULL;
218                 break;
219         default:
220                 IRDA_ERROR("%s(), unknown state %d\n",
221                            __FUNCTION__, task->state);
222                 irda_task_next_state(task, IRDA_TASK_DONE);
223                 self->reset_task = NULL;
224                 ret = -1;
225                 break;
226         }
227         return ret;
228 }
229
230 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
231 MODULE_DESCRIPTION("Greenwich GIrBIL dongle driver");
232 MODULE_LICENSE("GPL");
233 MODULE_ALIAS("irda-dongle-4"); /* IRDA_GIRBIL_DONGLE */
234         
235 /*
236  * Function init_module (void)
237  *
238  *    Initialize Girbil module
239  *
240  */
241 module_init(girbil_init);
242
243 /*
244  * Function cleanup_module (void)
245  *
246  *    Cleanup Girbil module
247  *
248  */
249 module_exit(girbil_cleanup);
250