summaryrefslogtreecommitdiff
path: root/src/hal/utils/upci.c
blob: 90a05d3ebfb3d2da24708e18174b170eef474058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/*************************************************************************

 Copyright (C) 2007 John Kasunich (jmkasunich at fastmail dot fm)

 This library contains functions to scan the PCI bus, find a particular
 PCI card, and read or write memory and I/O locations on a PCI card.

**************************************************************************

This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General
Public License as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111 USA

THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE
TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of
harming persons must have provisions for completely removing power
from all motors, etc, before persons enter any danger area.  All
machinery must be designed to comply with local and national safety
codes, and the authors of this software can not, and do not, take
any responsibility for such compliance.

This code was written as part of the EMC HAL project.  For more
information, go to www.linuxcnc.org.

*************************************************************************/

#define _GNU_SOURCE /* getline() */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <linux/types.h>
#include "upci.h"

/************************************************************************/

/* Assorted typedefs */

/* this is an standarized struct that is present in the
   configuration space of every PCI card */
struct cfg_info {
    __u16 vendor_id;
    __u16 device_id;
    __u16 command;
    __u16 status;
    __u32 class_rev;
    __u32 misc_0;
    __u32 base[6];
    __u32 cardbus;
    __u16 ss_vendor_id;
    __u16 ss_device_id;
    __u32 rom_addr;
    __u32 reserved[2];
    __u32 misc_1;
};

/* internal structs used by upci */
struct region_info {
    enum upci_region_type type;
    __u32 base_addr;
    __u32 size;
    int region_is_mapped;
    void *mapped_ptr;
};

struct dev_info {
    struct upci_dev_info p;		/* public info */
    struct cfg_info cfg;		/* raw data from device */
    struct region_info regions[6];	/* region info */
};

/************************************************************************/

#define MAX_DEVICES 100
#define MAX_REGIONS (MAX_DEVICES*6)

/* we get our info from the proc filesystem */
#define DEVICES_FILE	"/proc/bus/pci/devices"
#define DEVICE_FILE	"/proc/bus/pci/%02x/%02x.%01x"

/* number of discovered devices */
static int num_devs = 0;
/* pointers to all discovered devices (others are null) */
static struct dev_info *devices[MAX_DEVICES] = { NULL };
/* pointers to all mapped regions (others are null) */
static struct region_info *regions[MAX_REGIONS] = { NULL };
/* file descriptor for /dev/mem */
static int memfd = -1;
/* reference counters, increment each time a region is opened */
static int memaccess = 0;	/* non-zero if /dev/mem is open */
static int ioaccess = 0;	/* non-zero if iopl > 0 */

/************************************************************************/

static void errmsg(const char *funct, const char *fmt, ...)
{
    va_list vp;

    va_start(vp, fmt);
    fprintf(stderr, "ERROR in %s(): ", funct);
    vfprintf(stderr, fmt, vp);
    fprintf(stderr, "\n");
    va_end(vp);
}

void upci_reset(void)
{
    int d, r;
    struct dev_info *dev;
    struct region_info *region;

    /* traverse list */
    for ( d = 0 ; d < num_devs ; d++ ) {
	dev = devices[d];
	if ( dev != NULL ) {
	    for ( r = 0 ; r < 6 ; r++ ) {
		region = &(dev->regions[r]);
		if ( region->region_is_mapped ) {
		    /* unmap any mapped memory regions */
		    munmap ( region->mapped_ptr, region->size );
		}
	    }
	    /* free data structures */
	    free(dev);
	    devices[d] = NULL;
	}
    }
    /* release I/O port access */
    if ( ioaccess ) {
	iopl(0);
	ioaccess = 0;
    }
    /* release raw memory access */
    if ( memaccess ) {
	close (memfd);
	memaccess = 0;
    }
    num_devs = 0;
}

int upci_scan_bus(void)
{
    FILE *f;
    char *lineptr;
    size_t linelen;
    int linenum, r, n;
    struct dev_info *dev, *ndev;
    struct cfg_info cfg;
    __u32 vendordev;
    __u16 busdevfunc;
    char dev_fname[256];
    int fd;

    /* delete any previous list */
    upci_reset();
    /* we use /proc as the source of all knowledge */
    f = fopen(DEVICES_FILE, "r");
    if (f == NULL) {
	errmsg(__func__, "opening '%s': %s", DEVICES_FILE, strerror(errno));
	goto cleanup1;
    }
    lineptr = NULL;
    linelen = 0;
    linenum = 0;
    while ( 1 ) {
	if ( num_devs == MAX_DEVICES ) {
	    errmsg(__func__, "devices list full (%d entries)", num_devs);
	    /* return the ones that we already found, ignore others */
	    free(lineptr);
	    fclose(f);
	    return num_devs;
	}
	/* get a line */
	if(getline(&lineptr, &linelen, f) < 0) {
	    /* end of file, done */
	    free(lineptr);
	    fclose(f);
	    return num_devs;
	}
	++linenum;
	/* allocate a structure */
	dev = (struct dev_info *)(malloc(sizeof(struct dev_info)));
	if ( dev == NULL ) {
	    errmsg(__func__,"malloc failure");
	    goto cleanup2;
	}
	devices[num_devs++] = dev;
	n = sscanf(lineptr,
	    "%hx %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
	    &busdevfunc, &vendordev, &(dev->p.irq),
	    &dev->p.base_addr[0],  &dev->p.base_addr[1], &dev->p.base_addr[2],
	    &dev->p.base_addr[3],  &dev->p.base_addr[4], &dev->p.base_addr[5],
	    &dev->p.rom_base_addr,
	    &dev->p.size[0], &dev->p.size[1], &dev->p.size[2],
	    &dev->p.size[3], &dev->p.size[4], &dev->p.size[5],
	    &dev->p.rom_size);
	if (n != 17) {
	    errmsg(__func__, "only %d items on %s line %d", n, DEVICES_FILE, linenum);
	    goto cleanup3;
	}
	/* separate the packed fields */
	dev->p.vendor_id = (vendordev >> 16) & 0xFFFF;
	dev->p.device_id =  vendordev & 0xFFFF;
	dev->p.bus = (busdevfunc >> 8) & 0x00FF;
	dev->p.dev = (busdevfunc >> 3) & 0x001F;
	dev->p.func = busdevfunc & 0x0007;
	/* fix up region info: remove embedded info in low bits,
	   copy to region_info structs, init various fields */
	for ( r = 0 ; r < 6 ; r++ ) {
	    if ( dev->p.base_addr[r] & 1 ) {
		/* region is I/O */
		dev->p.region_type[r] = UPCI_REG_IO;
		dev->p.base_addr[r] &= ~0x03;
	    } else {
		/* region is memory */
		dev->p.region_type[r] = UPCI_REG_MEM;
		dev->p.base_addr[r] &= ~0x0F;
	    }
	    dev->regions[r].type = dev->p.region_type[r];
	    dev->regions[r].base_addr = dev->p.base_addr[r];
	    dev->regions[r].size = dev->p.size[r];
	    dev->regions[r].region_is_mapped = 0;
	    dev->regions[r].mapped_ptr = NULL;
	}
	/* the subsystem info isn't stored in DEVICES_FILE, need to
	   look deeper to find it */
	snprintf(dev_fname, 255, DEVICE_FILE, dev->p.bus, dev->p.dev, dev->p.func);
	fd = open(dev_fname, O_RDONLY);
	if (fd < 0) {
	    errmsg(__func__, "opening '%s': %s", dev_fname, strerror(errno));
	    goto cleanup3;
	}
	n = read(fd, &cfg, sizeof(struct cfg_info));
	if ( n != sizeof(struct cfg_info) ) {
	    errmsg(__func__, "reading '%s': %s", dev_fname, strerror(errno));
	    goto cleanup4;
	}
	close(fd);
	dev->p.ss_vendor_id = cfg.ss_vendor_id;
	dev->p.ss_device_id = cfg.ss_device_id;
	dev->p.instance = 0;
	/* look for previous devices with the same vendor, device,
	   and subsystem codes.  start at end of list and work forward */
	n = num_devs - 2;
	while ( n >= 0 ) {
	    ndev = devices[n];
	    if (( dev->p.vendor_id == ndev->p.vendor_id ) &&
		( dev->p.device_id == ndev->p.device_id ) &&
		( dev->p.ss_vendor_id == ndev->p.ss_vendor_id ) &&
		( dev->p.ss_device_id == ndev->p.ss_device_id )) {
		/* found a match */
		dev->p.instance = ndev->p.instance + 1;
		/* don't need to look farther */
		break;
	    }
	    n--;
	}
    }
    /* loop never falls through, success returns from inside the loop */
    /* errors jump here */
cleanup4:
    close(fd);
cleanup3:
    free(dev);
    num_devs --;
cleanup2:
    free(lineptr);
    fclose(f);
cleanup1:
    upci_reset();
    return -1;
}

int upci_print_device_info(int devnum)
{
    struct upci_dev_info *dev;
    int n;

    if (( devnum < 0 ) || ( devnum >= num_devs )) {
	errmsg(__func__, "device %d not found", devnum);
	return -1;
    }
    dev = &(devices[devnum]->p);
    printf("PCI device %2d at bus/device/function %02x/%02x/%01x\n",
	devnum, dev->bus, dev->dev, dev->func );
    printf("Vendor/Device/SSVendor/SSDevice: %04x/%04x/%04x/%04x\n",
	dev->vendor_id, dev->device_id, dev->ss_vendor_id, dev->ss_device_id );
    for ( n = 0 ; n < 6 ; n++ ) {
	if ( dev->size[n] > 0 ) {
	    if ( dev->region_type[n] == UPCI_REG_IO ) {
		/* I/O region */
		printf("Region %d: I/O %u bytes at %04x\n",
		    n, dev->size[n], dev->base_addr[n] );
	    } else {
		/* memory region */
		printf("Region %d: MEM %u bytes at %08x\n",
		    n, dev->size[n], dev->base_addr[n] );
	    }
	}
    }
    return 0;
}

int upci_get_device_info(struct upci_dev_info *p, int devnum)
{
    if (( devnum < 0 ) || ( devnum >= num_devs )) {
	return -1;
    }
    if ( p == NULL ) {
	return -1;
    }
    /* copy from our private structure into the user's one */
    *p = devices[devnum]->p;
    return 0;
}

int upci_find_device(struct upci_dev_info *p)
{
    int n;
    struct dev_info *dev;

    n = 0;
    while ( n < num_devs ) {
	dev = devices[n];
	if (( dev->p.vendor_id == p->vendor_id ) &&
	    ( dev->p.device_id == p->device_id ) &&
	    ( dev->p.ss_vendor_id == p->ss_vendor_id ) &&
	    ( dev->p.ss_device_id == p->ss_device_id ) &&
	    ( dev->p.instance == p->instance )) {
	    /* found a match */
	    return n;
	}
	n++;
    }
    return -1;
}

static int incr_io_usage ( void )
{
    int retval, eno;

    /* make sure we can do I/O */
    if ( ioaccess == 0 ) {
	/* enable access */
	/* this needs priviliges */
	if (seteuid(0) != 0) {
	    errmsg(__func__, "need root privilges (or setuid root)");
	    return -1;
	}
	/* do it */
	retval = iopl(3);
	eno = errno;
	/* drop priviliges */
	if(seteuid(getuid()) != 0)
	{
	    errmsg(__func__, "unable to drop root privileges");
	    /* Don't continue past this point, because following code may
	     * execute with unexpected privileges
	     */
	    _exit(99);
	}
	/* check result */
	if(retval < 0 || iopl(3) < 0) {
	    errmsg(__func__,"opening I/O ports: %s", strerror(eno));
	    return -1;
	}
    }
    /* increment reference count */
    ioaccess++;
    return 0;
}

static void decr_io_usage ( void )
{
    if ( ioaccess <= 0 ) {
	/* should not be calling decr if already zero */
	return;
    }
    /* decrement reference count */
    ioaccess--;
    if ( ioaccess == 0 ) {
	/* release I/O priveleges */
	iopl(0);
    }
}

static int incr_mem_usage ( void )
{
    int eno;

    /* make sure /dev/mem is open */
    if ( memaccess == 0 ) {
	/* open it */
	/* this needs priviliges */
	if (seteuid(0) != 0) {
	    errmsg(__func__, "need root privilges (or setuid root)");
	    return -1;
	}
	/* do it */
	memfd = open("/dev/mem", O_RDWR);
	eno = errno;
	/* drop priviliges */
	if(seteuid(getuid()) != 0)
	{
	    errmsg(__func__, "unable to drop root privileges");
	    /* Don't continue past this point, because following code may
	     * execute with unexpected privileges
	     */
	    _exit(99);
	}
	/* check result */
	if ( memfd < 0 ) {
	    errmsg(__func__,"can't open /dev/mem: %s", strerror(eno));
	    return -1;
	}
    }
    /* increment reference count */
    memaccess++;
    return 0;
}

static void decr_mem_usage ( void )
{
    if ( memaccess <= 0 ) {
	/* should not be calling decr if already zero */
	return;
    }
    /* decrement reference count */
    memaccess--;
    if ( memaccess == 0 ) {
	/* close /dev/mem */
	close(memfd);
    }
}

int upci_open_region(int devnum, int region_num)
{
    struct dev_info *dev;
    struct region_info *reg;
    int rd;

    if (( devnum < 0 ) || ( devnum >= num_devs )) {
	errmsg(__func__, "device %d not found", devnum);
	return -1;
    }
    dev = devices[devnum];
    if (( region_num < 0 ) || ( region_num >= 6 )) {
	errmsg(__func__, "bad region number %d", region_num);
	return -1;
    }
    reg = &(dev->regions[region_num]);
    if ( reg->size == 0 ) {
	errmsg(__func__, "region %d not found in device %d",
	    region_num, devnum);
	return -1;
    }
    /* calculate region descriptor */
    rd = devnum * 6 + region_num;
    if ( reg->region_is_mapped ) {
	/* already open, nothing to do */
	return rd;
    }
    if ( reg->type == UPCI_REG_IO ) {
	/* region is IO, no mapping needed */
	/* update reference counter */
	if ( incr_io_usage() != 0 ) {
	    errmsg(__func__, "no I/O access");
	    return -1;
	}
    } else {
	/* region is memory */
	/* update reference counter */
	if ( incr_mem_usage() != 0 ) {
	    return -1;
	}
	/* map the memory region */
	reg->mapped_ptr = mmap(0, reg->size, PROT_READ | PROT_WRITE,
	    MAP_SHARED, memfd, reg->base_addr);
	if ( reg->mapped_ptr == MAP_FAILED ) {
	    reg->mapped_ptr = NULL;
	    errmsg(__func__, "can't map /dev/mem for device %d, region %d: %s",
		devnum, region_num, strerror(errno));
	    decr_mem_usage();
	    return -1;
	}
    }
    /* mark region as mapped */
    reg->region_is_mapped = 1;
    regions[rd] = reg;
    return rd;
}

void upci_close_region(int rd)
{
    struct region_info *reg;

    if ((rd < 0 ) || ( rd >= MAX_REGIONS )) return;
    reg = regions[rd];
    if ( reg == NULL ) return;
    if ( reg->type == UPCI_REG_IO ) {
	/* region is IO, nothing to unmap */
	decr_io_usage();
    } else {
	/* region is memory, unmap it */
	munmap ( reg->mapped_ptr, reg->size );
	decr_mem_usage();
    }
    /* mark region as unmapped */
    reg->region_is_mapped = 0;
    regions[rd] = NULL;
}

__u8 upci_read_u8(int rd, __u32 offset)
{
    struct region_info *reg;
    int port;
    volatile __u8 *ptr, data;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) || 
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return 0;
    /* get the data */
    if ( reg->type == UPCI_REG_IO ) {
	/* region is IO */
	port = reg->base_addr + offset;
	data = inb(port);
    } else {
	/* region is memory */
	ptr = (__u8 *)(reg->mapped_ptr + offset);
	data = *ptr;
    }
    return data;
}

__s8 upci_read_s8(int rd, __u32 offset)
{
    struct region_info *reg;
    int port;
    volatile __s8 *ptr, data;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return 0;
    /* get the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	data = inb(port);
    } else {
	ptr = (__s8 *)(reg->mapped_ptr + offset);
	data = *ptr;
    }
    return data;
}

__u16 upci_read_u16(int rd, __u32 offset)
{
    struct region_info *reg;
    int port;
    volatile __u16 *ptr, data;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return 0;
    /* get the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	data = inw(port);
    } else {
	ptr = (__u16 *)(reg->mapped_ptr + offset);
	data = *ptr;
    }
    return data;
}

__s16 upci_read_s16(int rd, __u32 offset)
{
    struct region_info *reg;
    int port;
    volatile __s16 *ptr, data;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return 0;
    /* get the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	data = inw(port);
    } else {
	ptr = (__s16 *)(reg->mapped_ptr + offset);
	data = *ptr;
    }
    return data;
}

__u32 upci_read_u32(int rd, __u32 offset)
{
    struct region_info *reg;
    int port;
    volatile __u32 *ptr, data;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return 0;
    /* get the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	data = inl(port);
    } else {
	ptr = (__u32 *)(reg->mapped_ptr + offset);
	data = *ptr;
    }
    return data;
}

__s32 upci_read_s32(int rd, __u32 offset)
{
    struct region_info *reg;
    int port;
    volatile __s32 *ptr, data;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return 0;
    /* get the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	data = inl(port);
    } else {
	ptr = (__s32 *)(reg->mapped_ptr + offset);
	data = *ptr;
    }
    return data;
}

void upci_write_u8(int rd, __u32 offset, __u8 data)
{
    struct region_info *reg;
    int port;
    volatile __u8 *ptr;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) || 
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return;
    /* write the data */
    if ( reg->type == UPCI_REG_IO ) {
	/* region is IO */
	port = reg->base_addr + offset;
	outb(data, port);
    } else {
	/* region is memory */
	ptr = (__u8 *)(reg->mapped_ptr + offset);
	*ptr = data;
    }
    return;
}

void upci_write_s8(int rd, __u32 offset, __s8 data)
{
    struct region_info *reg;
    int port;
    volatile __s8 *ptr;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return;
    /* write the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	outb(data, port);
    } else {
	ptr = (__s8 *)(reg->mapped_ptr + offset);
	*ptr = data;
    }
    return;
}

void upci_write_u16(int rd, __u32 offset, __u16 data)
{
    struct region_info *reg;
    int port;
    volatile __u16 *ptr;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return;
    /* write the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	outw(data, port);
    } else {
	ptr = (__u16 *)(reg->mapped_ptr + offset);
	*ptr = data;
    }
    return;
}

void upci_write_s16(int rd, __u32 offset, __s16 data)
{
    struct region_info *reg;
    int port;
    volatile __s16 *ptr;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return;
    /* write the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	outw(data, port);
    } else {
	ptr = (__s16 *)(reg->mapped_ptr + offset);
	*ptr = data;
    }
    return;
}

void upci_write_u32(int rd, __u32 offset, __u32 data)
{
    struct region_info *reg;
    int port;
    volatile __u32 *ptr;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return;
    /* write the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	outl(data, port);
    } else {
	ptr = (__u32 *)(reg->mapped_ptr + offset);
	*ptr = data;
    }
    return;
}

void upci_write_s32(int rd, __u32 offset, __s32 data)
{
    struct region_info *reg;
    int port;
    volatile __s32 *ptr;

    /* test for out of range, not mapped, or offset beyond end of region */
    if ((rd < 0 ) || ( rd >= MAX_REGIONS ) ||
	( (reg = regions[rd]) == NULL ) || ( offset > reg->size )) return;
    /* write the data */
    if ( reg->type == UPCI_REG_IO ) {
	port = reg->base_addr + offset;
	outl(data, port);
    } else {
	ptr = (__s32 *)(reg->mapped_ptr + offset);
	*ptr = data;
    }
    return;
}