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
|
/*
* libuio - UserspaceIO helper library
*
* Copyright (C) 2011 Benedikt Spranger
* based on libUIO by Hans J. Koch
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include "libuio_internal.h"
/**
* @mainpage
*
* This manual documents the libuio C API.
*/
/**
* @defgroup libuio_public public available libuio functions
* @ingroup libuio
* @brief public functions
*/
/**
* @defgroup libuio_base libuio base functions
* @ingroup libuio_public
* @brief public base functions
* @{
*/
static const char *sysfs = "/sys";
static int uio_unmap (struct uio_map_t *uio_map)
{
int ret;
ret = munmap (uio_map->map, uio_map->size);
if (ret)
g_warning (_("munmap: %s\n"), g_strerror (errno));
else
uio_map->map = MAP_FAILED;
return ret;
}
/**
* Set sysfs mount point
* @param sysfs_mpoint path to sysfs mount point
*/
void uio_setsysfs_point (const char *sysfs_mpoint)
{
sysfs = sysfs_mpoint;
}
/**
* get UIO device name
* @param info UIO device info struct
* @returns UIO device name or NULL on failure
*/
char *uio_get_name(struct uio_info_t* info)
{
if (!info)
return NULL;
return info->name;
}
/**
* get UIO device node name
* @param info UIO device info struct
* @returns UIO device node name or NULL on failure
*/
char *uio_get_devname(struct uio_info_t* info)
{
if (!info)
return NULL;
return info->devname;
}
/**
* get UIO driver version
* @param info UIO device info struct
* @returns UIO device version or NULL on failure
*/
char *uio_get_version(struct uio_info_t* info)
{
if (!info)
return NULL;
return info->version;
}
/**
* get UIO device major number
* @param info UIO device info struct
* @returns UIO device node major or 0 on failure
*/
int uio_get_major(struct uio_info_t* info)
{
if (!info)
return 0;
return major (info->devid);
}
/**
* get UIO device minor number
* @param info UIO device info struct
* @returns UIO device node minor or 0 on failure
*/
int uio_get_minor(struct uio_info_t* info)
{
if (!info)
return 0;
return minor (info->devid);
}
/**
* get UIO device id
* @param info UIO device info struct
* @returns UIO device id or 0 on failure
*/
dev_t uio_get_devid(struct uio_info_t* info)
{
if (!info)
return 0;
return info->devid;
}
/**
* get UIO device file descriptor
* @param info UIO device info struct
* @returns UIO device file descriptor -1 on failure
*/
int uio_get_fd(struct uio_info_t* info)
{
if (!info)
return -1;
return info->fd;
}
/**
* free UIO device information struct
* @param info UIO device info struct
*/
void uio_free_info(struct uio_info_t* info)
{
if (info)
{
if (info->path)
free (info->path);
if (info->name)
free (info->name);
if (info->version)
free (info->version);
if (info->maps)
free (info->maps);
if (info->devname)
free (info->devname);
free (info);
}
}
/**
* find UIO devices
* @returns device list or NULL on failure
*/
struct uio_info_t **uio_find_devices ()
{
struct dirent **namelist;
struct uio_info_t **info;
char sysfsname [PATH_MAX];
int i, t = 0, nr;
snprintf (sysfsname, sizeof (sysfsname), "%s/class/uio", sysfs);
nr = scandir (sysfsname, &namelist, 0, alphasort);
if (nr < 0)
{
g_warning (_("scandir: %s\n"), g_strerror (errno));
return NULL;
}
info = calloc (nr, sizeof (struct uio_info_t *));
if (!info)
{
errno = ENOMEM;
g_warning (_("calloc: %s\n"), g_strerror (errno));
goto out;
}
for (i = 0; i < nr; i++)
{
if (!strcmp (namelist [i]->d_name, ".") ||
!strcmp (namelist [i]->d_name, ".."))
continue;
info [t++] = create_uio_info (sysfsname, namelist [i]->d_name);
}
out:
for (i = 0; i < nr; i++)
free (namelist [i]);
free (namelist);
return info;
}
/**
* find UIO devices by UIO name
* @param uio_name UIO name
* @returns device info or NULL on failure
*/
struct uio_info_t *uio_find_by_uio_name (char *uio_name)
{
struct uio_info_t *info = NULL, **list, **uio_list;
char *name;
if (!uio_name)
return NULL;
uio_list = uio_find_devices ();
if (!uio_list)
return NULL;
for (list = uio_list; *list; list++)
{
struct uio_info_t *candidate = *list;
name = uio_get_name (candidate);
if (!strcmp (name, uio_name)) {
info = candidate;
break;
}
}
free (uio_list);
return info;
}
/**
* find UIO devices by UIO enumeration number
* @param uio_num UIO enumeration number
* @returns device info or NULL on failure
*/
struct uio_info_t *uio_find_by_uio_num (int uio_num)
{
struct uio_info_t *info;
char sysfsname [PATH_MAX];
char name [PATH_MAX];
snprintf (sysfsname, sizeof (sysfsname), "%s/class/uio", sysfs);
snprintf (name, sizeof (name), "uio%d", uio_num);
info = create_uio_info (sysfsname, name);
if (errno)
{
uio_free_info (info);
info = NULL;
}
return info;
}
/**
* find a UIO device by base address in memory map
* @param base address of a memory map member
* @returns device info or NULL on failure
*/
struct uio_info_t *uio_find_by_base_addr (unsigned int base_addr)
{
struct uio_info_t *info = NULL, **list, **uio_list;
int mapc, mapnum, found = 0;
uio_list = uio_find_devices();
if (!uio_list)
return NULL;
for (list = uio_list; *list; list++)
{
struct uio_info_t *candidate = *list;
/* get number of maps and go through each checking the base address */
mapnum = uio_get_maxmap(candidate);
for (mapc = 0; mapc < mapnum; mapc++)
{
if (base_addr == uio_get_mem_addr(candidate, mapc))
{
info = candidate;
found = 1;
break;
}
}
if (found)
break;
}
free (uio_list);
return info;
}
/**
* open a UIO device (try to map to given address)
* @param info UIO device info stuct
* @param ptr try to map at ptr
* @returns 0 on success or -1 on failure and errno is set
*/
int uio_open_fix (struct uio_info_t* info, void *ptr)
{
int fd, i;
if (!info)
{
errno = EINVAL;
g_warning (_("uio_open: %s\n"), g_strerror (errno));
return -1;
}
fd = open (info->devname, O_RDWR);
if (fd < 0)
{
g_warning (_("open: %s\n"), g_strerror (errno));
return -1;
}
for (i = 0; i < info->maxmap; i++)
{
info->maps [i].map = mmap (ptr, info->maps [i].size,
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, i * getpagesize());
if (info->maps[i].map == MAP_FAILED) {
while (--i >= 0)
uio_unmap (&info->maps [i]);
g_warning (_("mmap: %s\n"), g_strerror (errno));
return -1;
}
if (ptr)
ptr += info->maps [i].size;
}
info->fd = fd;
return 0;
}
/**
* open a UIO device
* @param info UIO device info stuct
* @returns 0 on success or -1 on failure and errno is set
*/
int uio_open (struct uio_info_t* info)
{
return uio_open_fix (info, NULL);
}
/**
* open a UIO device (COW)
* @param info UIO device info stuct
* @returns 0 on success or -1 on failure and errno is set
*/
int uio_open_private (struct uio_info_t* info)
{
int fd, i;
if (!info)
{
errno = EINVAL;
g_warning (_("uio_open: %s\n"), g_strerror (errno));
return -1;
}
fd = open (info->devname, O_RDWR);
if (fd < 0)
{
g_warning (_("open: %s\n"), g_strerror (errno));
return -1;
}
for (i = 0; i < info->maxmap; i++) {
info->maps [i].map = mmap (NULL, info->maps [i].size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, i * getpagesize());
if (info->maps[i].map == MAP_FAILED) {
while (--i >= 0)
uio_unmap(&info->maps [i]);
g_warning (_("mmap: %s\n"), g_strerror (errno));
return -1;
}
}
info->fd = fd;
return 0;
}
/**
* close a UIO device
* @param info UIO device info struct
* @returns 0 on success or -1 on failure and errno is set
*/
int uio_close (struct uio_info_t* info)
{
int i;
if (!info)
{
errno = EINVAL;
g_warning (_("uio_close: %s\n"), g_strerror (errno));
return -1;
}
for (i = 0; i < info->maxmap; i++)
if (info->maps [i].map != MAP_FAILED)
uio_unmap(&info->maps [i]);
close (info->fd);
return 0;
}
/** @} */
|