more changes on original files
[linux-2.4.git] / drivers / block / paride / bpck6.c
1 /*
2         backpack.c (c) 2001 Micro Solutions Inc.
3                 Released under the terms of the GNU General Public license
4
5         backpack.c is a low-level protocol driver for the Micro Solutions
6                 "BACKPACK" parallel port IDE adapter
7                 (Works on Series 6 drives)
8
9         Written by: Ken Hahn     (linux-dev@micro-solutions.com)
10                     Clive Turvey (linux-dev@micro-solutions.com)
11
12 */
13
14 /*
15    This is Ken's linux wrapper for the PPC library
16    Version 1.0.0 is the backpack driver for which source is not available
17    Version 2.0.0 is the first to have source released 
18    Version 2.0.1 is the "Cox-ified" source code 
19    Version 2.0.2 - fixed version string usage, and made ppc functions static 
20    Version 2.0.2ac - additional cleanup (privptr now not private to fix 64bit
21                    platforms), use memset, rename clashing PPC define.
22 */
23
24
25 /* PARAMETERS */
26 int verbose=0; /* set this to 1 to see debugging messages and whatnot */
27
28 #define BACKPACK_VERSION "2.0.2ac"
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <asm/io.h>
35
36 #if defined(CONFIG_PARPORT_MODULE)||defined(CONFIG_PARPORT)
37 #include <linux/parport.h>
38 #endif
39
40 #include "ppc6lnx.c"
41 #include "paride.h"
42
43  
44
45 #define PPCSTRUCT(pi) ((PPC_STORAGE *)(pi->privptr))
46
47 /****************************************************************/
48 /*
49  ATAPI CDROM DRIVE REGISTERS
50 */
51 #define ATAPI_DATA       0      /* data port                  */
52 #define ATAPI_ERROR      1      /* error register (read)      */
53 #define ATAPI_FEATURES   1      /* feature register (write)   */
54 #define ATAPI_INT_REASON 2      /* interrupt reason register  */
55 #define ATAPI_COUNT_LOW  4      /* byte count register (low)  */
56 #define ATAPI_COUNT_HIGH 5      /* byte count register (high) */
57 #define ATAPI_DRIVE_SEL  6      /* drive select register      */
58 #define ATAPI_STATUS     7      /* status port (read)         */
59 #define ATAPI_COMMAND    7      /* command port (write)       */
60 #define ATAPI_ALT_STATUS 0x0e /* alternate status reg (read) */
61 #define ATAPI_DEVICE_CONTROL 0x0e /* device control (write)   */
62 /****************************************************************/
63
64 static int bpck6_read_regr(PIA *pi, int cont, int reg)
65 {
66         unsigned int out;
67
68         /* check for bad settings */
69         if (reg<0 || reg>7 || cont<0 || cont>2)
70         {
71                 return(-1);
72         }
73         out=ppc6_rd_port(PPCSTRUCT(pi),cont?reg|8:reg);
74         return(out);
75 }
76
77 static void bpck6_write_regr(PIA *pi, int cont, int reg, int val)
78 {
79         /* check for bad settings */
80         if (reg>=0 && reg<=7 && cont>=0 && cont<=1)
81         {
82                 ppc6_wr_port(PPCSTRUCT(pi),cont?reg|8:reg,(u8)val);
83         }
84 }
85
86 static void bpck6_write_block( PIA *pi, char * buf, int len )
87 {
88         ppc6_wr_port16_blk(PPCSTRUCT(pi),ATAPI_DATA,buf,(u32)len>>1); 
89 }
90
91 static void bpck6_read_block( PIA *pi, char * buf, int len )
92 {
93         ppc6_rd_port16_blk(PPCSTRUCT(pi),ATAPI_DATA,buf,(u32)len>>1);
94 }
95
96 static void bpck6_connect ( PIA *pi  )
97 {
98         if(verbose)
99         {
100                 printk(KERN_DEBUG "connect\n");
101         }
102
103         if(pi->mode >=2)
104         {
105                 PPCSTRUCT(pi)->mode=4+pi->mode-2;       
106         }
107         else if(pi->mode==1)
108         {
109                 PPCSTRUCT(pi)->mode=3;  
110         }
111         else
112         {
113                 PPCSTRUCT(pi)->mode=1;          
114         }
115
116         ppc6_open(PPCSTRUCT(pi));  
117         ppc6_wr_extout(PPCSTRUCT(pi),0x3);
118 }
119
120 static void bpck6_disconnect ( PIA *pi )
121 {
122         if(verbose)
123         {
124                 printk("disconnect\n");
125         }
126         ppc6_wr_extout(PPCSTRUCT(pi),0x0);
127         ppc6_close(PPCSTRUCT(pi));
128 }
129
130 static int bpck6_test_port ( PIA *pi )   /* check for 8-bit port */
131 {
132         if(verbose)
133         {
134                 printk(KERN_DEBUG "PARPORT indicates modes=%x for lp=0x%lx\n",
135                         ((struct pardevice*)(pi->pardev))->port->modes,
136                         ((struct pardevice *)(pi->pardev))->port->base); 
137         }
138
139         /*copy over duplicate stuff.. initialize state info*/
140         PPCSTRUCT(pi)->ppc_id=pi->unit;
141         PPCSTRUCT(pi)->lpt_addr=pi->port;
142
143 #ifdef CONFIG_PARPORT_PC_MODULE
144 #define CONFIG_PARPORT_PC
145 #endif
146
147 #ifdef CONFIG_PARPORT_PC
148         /* look at the parport device to see if what modes we can use */
149         if(((struct pardevice *)(pi->pardev))->port->modes & 
150                 (PARPORT_MODE_EPP)
151           )
152         {
153                 return 5; /* Can do EPP*/
154         }
155         else if(((struct pardevice *)(pi->pardev))->port->modes & 
156                         (PARPORT_MODE_TRISTATE)
157                )
158         {
159                 return 2;
160         }
161         else /*Just flat SPP*/
162         {
163                 return 1;
164         }
165 #else
166         /* there is no way of knowing what kind of port we have
167            default to the highest mode possible */
168         return 5;
169 #endif
170 }
171
172 static int bpck6_probe_unit ( PIA *pi )
173 {
174         int out;
175
176         if(verbose)
177         {
178                 printk(KERN_DEBUG "PROBE UNIT %x on port:%x\n",pi->unit,pi->port);
179         }
180
181         /*SET PPC UNIT NUMBER*/
182         PPCSTRUCT(pi)->ppc_id=pi->unit;
183
184         /*LOWER DOWN TO UNIDIRECTIONAL*/
185         PPCSTRUCT(pi)->mode=1;          
186
187         out=ppc6_open(PPCSTRUCT(pi));
188
189         if(verbose)
190         {
191                 printk(KERN_DEBUG "ppc_open returned %2x\n",out);
192         }
193
194         if(out)
195         {
196                 ppc6_close(PPCSTRUCT(pi));
197                 if(verbose)
198                 {
199                         printk(KERN_DEBUG "leaving probe\n");
200                 }
201                return(1);
202         }
203         else
204         {
205                 if(verbose)
206                 {
207                         printk(KERN_DEBUG "Failed open\n");
208                 }
209                 return(0);
210         }
211 }
212
213 static void bpck6_log_adapter( PIA *pi, char * scratch, int verbose )
214 {
215         char *mode_string[5]=
216                 {"4-bit","8-bit","EPP-8","EPP-16","EPP-32"};
217
218         printk("%s: BACKPACK Protocol Driver V"BACKPACK_VERSION"\n",pi->device);
219         printk("%s: Copyright 2001 by Micro Solutions, Inc., DeKalb IL.\n",pi->device);
220         printk("%s: BACKPACK %s, Micro Solutions BACKPACK Drive at 0x%x\n",
221                 pi->device,BACKPACK_VERSION,pi->port);
222         printk("%s: Unit: %d Mode:%d (%s) Delay %d\n",pi->device,
223                 pi->unit,pi->mode,mode_string[pi->mode],pi->delay);
224 }
225
226 static void bpck6_init_proto(PIA *pi)
227 {
228         /* allocate a state structure for this item */
229         pi->privptr=kmalloc(sizeof(PPC_STORAGE),GFP_KERNEL);
230
231         if(pi->privptr==NULL)
232         {
233                 printk(KERN_ERR "%s: ERROR COULDN'T ALLOCATE MEMORY\n",pi->device); 
234                 return;
235         }
236         else
237         {       
238                 MOD_INC_USE_COUNT; 
239         }
240
241         memset(pi->privptr, 0, sizeof(PPC_STORAGE));
242 }
243
244 static void bpck6_release_proto(PIA *pi)
245 {
246         MOD_DEC_USE_COUNT;
247         /* free after use count decremented so that we aren't using it
248                 when it is decremented */
249         kfree(pi->privptr); 
250 }
251
252 struct pi_protocol bpck6 = { "bpck6", /* name for proto*/
253                                 0, /* index into proto table */
254                                 5, /* max mode =5 */
255                                 2, /* 2-5 use epp (need 8 ports) */
256                                 0, /* no delay (not used anyway) */
257                                 255, /* we can have units  up to 255 */
258                                 bpck6_write_regr,
259                                 bpck6_read_regr,
260                                 bpck6_write_block,
261                                 bpck6_read_block,
262                                 bpck6_connect,
263                                 bpck6_disconnect,
264                                 bpck6_test_port,
265                                 bpck6_probe_unit,
266                                 0,
267                                 bpck6_log_adapter,
268                                 bpck6_init_proto,
269                                 bpck6_release_proto
270                         };
271
272
273 EXPORT_SYMBOL(bpck6_write_regr);
274 EXPORT_SYMBOL(bpck6_read_regr);
275 EXPORT_SYMBOL(bpck6_write_block);
276 EXPORT_SYMBOL(bpck6_read_block);
277 EXPORT_SYMBOL(bpck6_connect);
278 EXPORT_SYMBOL(bpck6_disconnect);
279 EXPORT_SYMBOL(bpck6_test_port);
280 EXPORT_SYMBOL(bpck6_probe_unit);
281 EXPORT_SYMBOL(bpck6_log_adapter);
282 EXPORT_SYMBOL(bpck6_init_proto);
283 EXPORT_SYMBOL(bpck6_release_proto);
284
285 /*---------------------------MODULE STUFF-----------------------*/
286
287 #ifdef MODULE
288 /*module information*/
289
290 int init_module(void)
291 {
292         printk(KERN_INFO "bpck6: BACKPACK Protocol Driver V"BACKPACK_VERSION"\n");
293         printk(KERN_INFO "bpck6: Copyright 2001 by Micro Solutions, Inc., DeKalb IL. USA\n");
294
295         if(verbose)
296         {
297                 printk(KERN_DEBUG "bpck6: verbose debug enabled.\n");
298         }
299
300         return pi_register(&bpck6) - 1;  
301 }
302
303 void cleanup_module(void)
304 {
305         pi_unregister(&bpck6);
306 }
307
308 MODULE_AUTHOR("Micro Solutions Inc.");
309 MODULE_DESCRIPTION("BACKPACK Protocol module, compatible with PARIDE");
310 MODULE_PARM(verbose,"i");
311
312 #endif
313