Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  *  linux/drivers/acorn/scsi/powertec.c
  3  *
  4  *  Copyright (C) 1997-2003 Russell King
  5  *
  6  * This program is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License version 2 as
  8  * published by the Free Software Foundation.
  9  */
 10 #include <linux/module.h>
 11 #include <linux/blkdev.h>
 12 #include <linux/kernel.h>
 13 #include <linux/string.h>
 14 #include <linux/ioport.h>
 15 #include <linux/sched.h>
 16 #include <linux/proc_fs.h>
 17 #include <linux/delay.h>
 18 #include <linux/interrupt.h>
 19 #include <linux/init.h>
 20 #include <linux/dma-mapping.h>
 21 
 22 #include <asm/dma.h>
 23 #include <asm/ecard.h>
 24 #include <asm/io.h>
 25 #include <asm/irq.h>
 26 #include <asm/pgtable.h>
 27 
 28 #include "../scsi.h"
 29 #include <scsi/scsi_host.h>
 30 #include "fas216.h"
 31 #include "scsi.h"
 32 
 33 #include <scsi/scsicam.h>
 34 
 35 #define POWERTEC_FAS216_OFFSET  0x3000
 36 #define POWERTEC_FAS216_SHIFT   6
 37 
 38 #define POWERTEC_INTR_STATUS    0x2000
 39 #define POWERTEC_INTR_BIT       0x80
 40 
 41 #define POWERTEC_RESET_CONTROL  0x1018
 42 #define POWERTEC_RESET_BIT      1
 43 
 44 #define POWERTEC_TERM_CONTROL   0x2018
 45 #define POWERTEC_TERM_ENABLE    1
 46 
 47 #define POWERTEC_INTR_CONTROL   0x101c
 48 #define POWERTEC_INTR_ENABLE    1
 49 #define POWERTEC_INTR_DISABLE   0
 50 
 51 #define VERSION "1.10 (19/01/2003 2.5.59)"
 52 
 53 /*
 54  * Use term=0,1,0,0,0 to turn terminators on/off.
 55  * One entry per slot.
 56  */
 57 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
 58 
 59 #define NR_SG   256
 60 
 61 struct powertec_info {
 62         FAS216_Info             info;
 63         struct expansion_card   *ec;
 64         void                    *term_port;
 65         unsigned int            term_ctl;
 66         struct scatterlist      sg[NR_SG];
 67 };
 68 
 69 /* Prototype: void powertecscsi_irqenable(ec, irqnr)
 70  * Purpose  : Enable interrupts on Powertec SCSI card
 71  * Params   : ec    - expansion card structure
 72  *          : irqnr - interrupt number
 73  */
 74 static void
 75 powertecscsi_irqenable(struct expansion_card *ec, int irqnr)
 76 {
 77         writeb(POWERTEC_INTR_ENABLE, ec->irq_data);
 78 }
 79 
 80 /* Prototype: void powertecscsi_irqdisable(ec, irqnr)
 81  * Purpose  : Disable interrupts on Powertec SCSI card
 82  * Params   : ec    - expansion card structure
 83  *          : irqnr - interrupt number
 84  */
 85 static void
 86 powertecscsi_irqdisable(struct expansion_card *ec, int irqnr)
 87 {
 88         writeb(POWERTEC_INTR_DISABLE, ec->irq_data);
 89 }
 90 
 91 static const expansioncard_ops_t powertecscsi_ops = {
 92         .irqenable      = powertecscsi_irqenable,
 93         .irqdisable     = powertecscsi_irqdisable,
 94 };
 95 
 96 /* Prototype: void powertecscsi_terminator_ctl(host, on_off)
 97  * Purpose  : Turn the Powertec SCSI terminators on or off
 98  * Params   : host   - card to turn on/off
 99  *          : on_off - !0 to turn on, 0 to turn off
100  */
101 static void
102 powertecscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
103 {
104         struct powertec_info *info = (struct powertec_info *)host->hostdata;
105 
106         info->term_ctl = on_off ? POWERTEC_TERM_ENABLE : 0;
107         writeb(info->term_ctl, info->term_port);
108 }
109 
110 /* Prototype: void powertecscsi_intr(irq, *dev_id, *regs)
111  * Purpose  : handle interrupts from Powertec SCSI card
112  * Params   : irq    - interrupt number
113  *            dev_id - user-defined (Scsi_Host structure)
114  *            regs   - processor registers at interrupt
115  */
116 static irqreturn_t
117 powertecscsi_intr(int irq, void *dev_id, struct pt_regs *regs)
118 {
119         struct powertec_info *info = dev_id;
120 
121         return fas216_intr(&info->info);
122 }
123 
124 /* Prototype: fasdmatype_t powertecscsi_dma_setup(host, SCpnt, direction, min_type)
125  * Purpose  : initialises DMA/PIO
126  * Params   : host      - host
127  *            SCpnt     - command
128  *            direction - DMA on to/off of card
129  *            min_type  - minimum DMA support that we must have for this transfer
130  * Returns  : type of transfer to be performed
131  */
132 static fasdmatype_t
133 powertecscsi_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
134                        fasdmadir_t direction, fasdmatype_t min_type)
135 {
136         struct powertec_info *info = (struct powertec_info *)host->hostdata;
137         struct device *dev = scsi_get_device(host);
138         int dmach = host->dma_channel;
139 
140         if (info->info.ifcfg.capabilities & FASCAP_DMA &&
141             min_type == fasdma_real_all) {
142                 int bufs, map_dir, dma_dir;
143 
144                 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
145 
146                 if (direction == DMA_OUT)
147                         map_dir = DMA_TO_DEVICE,
148                         dma_dir = DMA_MODE_WRITE;
149                 else
150                         map_dir = DMA_FROM_DEVICE,
151                         dma_dir = DMA_MODE_READ;
152 
153                 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
154 
155                 disable_dma(dmach);
156                 set_dma_sg(dmach, info->sg, bufs + 1);
157                 set_dma_mode(dmach, dma_dir);
158                 enable_dma(dmach);
159                 return fasdma_real_all;
160         }
161 
162         /*
163          * If we're not doing DMA,
164          *  we'll do slow PIO
165          */
166         return fasdma_pio;
167 }
168 
169 /* Prototype: int powertecscsi_dma_stop(host, SCpnt)
170  * Purpose  : stops DMA/PIO
171  * Params   : host  - host
172  *            SCpnt - command
173  */
174 static void
175 powertecscsi_dma_stop(struct Scsi_Host *host, Scsi_Pointer *SCp)
176 {
177         if (host->dma_channel != NO_DMA)
178                 disable_dma(host->dma_channel);
179 }
180 
181 /* Prototype: const char *powertecscsi_info(struct Scsi_Host * host)
182  * Purpose  : returns a descriptive string about this interface,
183  * Params   : host - driver host structure to return info for.
184  * Returns  : pointer to a static buffer containing null terminated string.
185  */
186 const char *powertecscsi_info(struct Scsi_Host *host)
187 {
188         struct powertec_info *info = (struct powertec_info *)host->hostdata;
189         static char string[150];
190 
191         sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
192                 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
193                 VERSION, info->term_ctl ? "n" : "ff");
194 
195         return string;
196 }
197 
198 /* Prototype: int powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
199  * Purpose  : Set a driver specific function
200  * Params   : host   - host to setup
201  *          : buffer - buffer containing string describing operation
202  *          : length - length of string
203  * Returns  : -EINVAL, or 0
204  */
205 static int
206 powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
207 {
208         int ret = length;
209 
210         if (length >= 12 && strncmp(buffer, "POWERTECSCSI", 12) == 0) {
211                 buffer += 12;
212                 length -= 12;
213 
214                 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
215                         if (buffer[5] == '1')
216                                 powertecscsi_terminator_ctl(host, 1);
217                         else if (buffer[5] == '')
218                                 powertecscsi_terminator_ctl(host, 0);
219                         else
220                                 ret = -EINVAL;
221                 } else
222                         ret = -EINVAL;
223         } else
224                 ret = -EINVAL;
225 
226         return ret;
227 }
228 
229 /* Prototype: int powertecscsi_proc_info(char *buffer, char **start, off_t offset,
230  *                                      int length, int host_no, int inout)
231  * Purpose  : Return information about the driver to a user process accessing
232  *            the /proc filesystem.
233  * Params   : buffer  - a buffer to write information to
234  *            start   - a pointer into this buffer set by this routine to the start
235  *                      of the required information.
236  *            offset  - offset into information that we have read upto.
237  *            length  - length of buffer
238  *            inout   - 0 for reading, 1 for writing.
239  * Returns  : length of data written to buffer.
240  */
241 int powertecscsi_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
242                             int length, int inout)
243 {
244         struct powertec_info *info;
245         char *p = buffer;
246         int pos;
247 
248         if (inout == 1)
249                 return powertecscsi_set_proc_info(host, buffer, length);
250 
251         info = (struct powertec_info *)host->hostdata;
252 
253         p += sprintf(p, "PowerTec SCSI driver v%s\n", VERSION);
254         p += fas216_print_host(&info->info, p);
255         p += sprintf(p, "Term    : o%s\n",
256                         info->term_ctl ? "n" : "ff");
257 
258         p += fas216_print_stats(&info->info, p);
259         p += fas216_print_devices(&info->info, p);
260 
261         *start = buffer + offset;
262         pos = p - buffer - offset;
263         if (pos > length)
264                 pos = length;
265 
266         return pos;
267 }
268 
269 static ssize_t powertecscsi_show_term(struct device *dev, char *buf)
270 {
271         struct expansion_card *ec = ECARD_DEV(dev);
272         struct Scsi_Host *host = ecard_get_drvdata(ec);
273         struct powertec_info *info = (struct powertec_info *)host->hostdata;
274 
275         return sprintf(buf, "%d\n", info->term_ctl ? 1 : 0);
276 }
277 
278 static ssize_t
279 powertecscsi_store_term(struct device *dev, const char *buf, size_t len)
280 {
281         struct expansion_card *ec = ECARD_DEV(dev);
282         struct Scsi_Host *host = ecard_get_drvdata(ec);
283 
284         if (len > 1)
285                 powertecscsi_terminator_ctl(host, buf[0] != '');
286 
287         return len;
288 }
289 
290 static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
291                    powertecscsi_show_term, powertecscsi_store_term);
292 
293 static Scsi_Host_Template powertecscsi_template = {
294         .module                         = THIS_MODULE,
295         .proc_info                      = powertecscsi_proc_info,
296         .name                           = "PowerTec SCSI",
297         .info                           = powertecscsi_info,
298         .queuecommand                   = fas216_queue_command,
299         .eh_host_reset_handler          = fas216_eh_host_reset,
300         .eh_bus_reset_handler           = fas216_eh_bus_reset,
301         .eh_device_reset_handler        = fas216_eh_device_reset,
302         .eh_abort_handler               = fas216_eh_abort,
303 
304         .can_queue                      = 8,
305         .this_id                        = 7,
306         .sg_tablesize                   = SG_ALL,
307         .cmd_per_lun                    = 2,
308         .use_clustering                 = ENABLE_CLUSTERING,
309         .proc_name                      = "powertec",
310 };
311 
312 static int __devinit
313 powertecscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
314 {
315         struct Scsi_Host *host;
316         struct powertec_info *info;
317         unsigned long resbase, reslen;
318         unsigned char *base;
319         int ret;
320 
321         ret = ecard_request_resources(ec);
322         if (ret)
323                 goto out;
324 
325         resbase = ecard_resource_start(ec, ECARD_RES_IOCFAST);
326         reslen = ecard_resource_len(ec, ECARD_RES_IOCFAST);
327         base = ioremap(resbase, reslen);
328         if (!base) {
329                 ret = -ENOMEM;
330                 goto out_region;
331         }
332 
333         host = scsi_host_alloc(&powertecscsi_template,
334                                sizeof (struct powertec_info));
335         if (!host) {
336                 ret = -ENOMEM;
337                 goto out_unmap;
338         }
339 
340         host->base        = (unsigned long)base;
341         host->irq         = ec->irq;
342         host->dma_channel = ec->dma;
343 
344         ec->irqaddr     = base + POWERTEC_INTR_STATUS;
345         ec->irqmask     = POWERTEC_INTR_BIT;
346         ec->irq_data    = base + POWERTEC_INTR_CONTROL;
347         ec->ops         = &powertecscsi_ops;
348 
349         ecard_set_drvdata(ec, host);
350 
351         info = (struct powertec_info *)host->hostdata;
352         info->term_port = base + POWERTEC_TERM_CONTROL;
353         powertecscsi_terminator_ctl(host, term[ec->slot_no]);
354 
355         device_create_file(&ec->dev, &dev_attr_bus_term);
356 
357         info->info.scsi.io_base         = base + POWERTEC_FAS216_OFFSET;
358         info->info.scsi.io_shift        = POWERTEC_FAS216_SHIFT;
359         info->info.scsi.irq             = host->irq;
360         info->info.ifcfg.clockrate      = 40; /* MHz */
361         info->info.ifcfg.select_timeout = 255;
362         info->info.ifcfg.asyncperiod    = 200; /* ns */
363         info->info.ifcfg.sync_max_depth = 7;
364         info->info.ifcfg.cntl3          = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
365         info->info.ifcfg.disconnect_ok  = 1;
366         info->info.ifcfg.wide_max_size  = 0;
367         info->info.ifcfg.capabilities   = 0;
368         info->info.dma.setup            = powertecscsi_dma_setup;
369         info->info.dma.pseudo           = NULL;
370         info->info.dma.stop             = powertecscsi_dma_stop;
371 
372         ret = fas216_init(host);
373         if (ret)
374                 goto out_free;
375 
376         ret = request_irq(host->irq, powertecscsi_intr,
377                           SA_INTERRUPT, "powertec", info);
378         if (ret) {
379                 printk("scsi%d: IRQ%d not free: %d\n",
380                        host->host_no, host->irq, ret);
381                 goto out_release;
382         }
383 
384         if (host->dma_channel != NO_DMA) {
385                 if (request_dma(host->dma_channel, "powertec")) {
386                         printk("scsi%d: DMA%d not free, using PIO\n",
387                                host->host_no, host->dma_channel);
388                         host->dma_channel = NO_DMA;
389                 } else {
390                         set_dma_speed(host->dma_channel, 180);
391                         info->info.ifcfg.capabilities |= FASCAP_DMA;
392                 }
393         }
394 
395         ret = fas216_add(host, &ec->dev);
396         if (ret == 0)
397                 goto out;
398 
399         if (host->dma_channel != NO_DMA)
400                 free_dma(host->dma_channel);
401         free_irq(host->irq, host);
402 
403  out_release:
404         fas216_release(host);
405 
406  out_free:
407         device_remove_file(&ec->dev, &dev_attr_bus_term);
408         scsi_host_put(host);
409 
410  out_unmap:
411         iounmap(base);
412 
413  out_region:
414         ecard_release_resources(ec);
415 
416  out:
417         return ret;
418 }
419 
420 static void __devexit powertecscsi_remove(struct expansion_card *ec)
421 {
422         struct Scsi_Host *host = ecard_get_drvdata(ec);
423         struct powertecscsi_info *info = (struct powertecscsi_info *)host->hostdata;
424 
425         ecard_set_drvdata(ec, NULL);
426         fas216_remove(host);
427 
428         device_remove_file(&ec->dev, &dev_attr_bus_term);
429 
430         if (host->dma_channel != NO_DMA)
431                 free_dma(host->dma_channel);
432         free_irq(host->irq, info);
433 
434         iounmap((void *)host->base);
435 
436         fas216_release(host);
437         scsi_host_put(host);
438         ecard_release_resources(ec);
439 }
440 
441 static const struct ecard_id powertecscsi_cids[] = {
442         { MANU_ALSYSTEMS, PROD_ALSYS_SCSIATAPI },
443         { 0xffff, 0xffff },
444 };
445 
446 static struct ecard_driver powertecscsi_driver = {
447         .probe          = powertecscsi_probe,
448         .remove         = __devexit_p(powertecscsi_remove),
449         .id_table       = powertecscsi_cids,
450         .drv = {
451                 .name           = "powertecscsi",
452         },
453 };
454 
455 static int __init powertecscsi_init(void)
456 {
457         return ecard_register_driver(&powertecscsi_driver);
458 }
459 
460 static void __exit powertecscsi_exit(void)
461 {
462         ecard_remove_driver(&powertecscsi_driver);
463 }
464 
465 module_init(powertecscsi_init);
466 module_exit(powertecscsi_exit);
467 
468 MODULE_AUTHOR("Russell King");
469 MODULE_DESCRIPTION("Powertec SCSI driver");
470 MODULE_PARM(term, "1-8i");
471 MODULE_PARM_DESC(term, "SCSI bus termination");
472 MODULE_LICENSE("GPL");
473 
  This page was automatically generated by the LXR engine.