Home | History | Annotate | Line # | Download | only in libpuffs
requests.c revision 1.1
      1 /*	$NetBSD: requests.c,v 1.1 2006/12/07 23:15:20 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the company nor the name of the author may be used to
     15  *    endorse or promote products derived from this software without specific
     16  *    prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #if !defined(lint)
     33 __RCSID("$NetBSD: requests.c,v 1.1 2006/12/07 23:15:20 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <sys/types.h>
     37 #include <sys/ioctl.h>
     38 
     39 #include <puffs.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 
     43 #include "puffs_priv.h"
     44 
     45 struct puffs_getreq *
     46 puffs_makegetreq(struct puffs_usermount *pu, uint8_t *buf, size_t buflen,
     47 	int maxops)
     48 {
     49 	struct puffs_getreq *pgr;
     50 
     51 	/* malloc first.  if malloc fails, we don't silently lose ops */
     52 	pgr = malloc(sizeof(struct puffs_getreq));
     53 	if (!pgr)
     54 		return NULL;
     55 
     56 	pgr->pgr_phg.phg_buf = buf;
     57 	pgr->pgr_phg.phg_buflen = buflen;
     58 	pgr->pgr_phg.phg_nops = maxops;
     59 
     60 	if (ioctl(pu->pu_fd, PUFFSGETOP, &pgr->pgr_phg) == -1) {
     61 		free(pgr);
     62 		return NULL;
     63 	}
     64 
     65 	pgr->pgr_nextpreq = pgr->pgr_phg.phg_buf;
     66 	pgr->pgr_advance = pgr->pgr_nextpreq->preq_buflen;
     67 	pgr->pgr_pu = pu;
     68 
     69 	return pgr;
     70 }
     71 
     72 struct puffs_req *
     73 puffs_getreq(struct puffs_getreq *pgr)
     74 {
     75 	struct puffs_req *preq;
     76 
     77 	if (pgr->pgr_phg.phg_nops == 0)
     78 		return NULL;
     79 
     80 	preq = pgr->pgr_nextpreq;
     81 	/*LINTED*/
     82 	pgr->pgr_nextpreq =
     83 	    (struct puffs_req*)((uint8_t*)preq + pgr->pgr_advance);
     84 	pgr->pgr_advance = pgr->pgr_nextpreq->preq_buflen;
     85 	pgr->pgr_phg.phg_nops--;
     86 
     87 	return preq;
     88 }
     89 
     90 int
     91 puffs_remaininggetreq(struct puffs_getreq *pgr)
     92 {
     93 
     94 	return pgr->pgr_phg.phg_nops;
     95 }
     96 
     97 void
     98 puffs_destroygetreq(struct puffs_getreq *pgr)
     99 {
    100 
    101 	free(pgr);
    102 }
    103 
    104 
    105 struct puffs_putreq *
    106 puffs_makeputreq(struct puffs_usermount *pu)
    107 {
    108 	struct puffs_putreq *ppr;
    109 
    110 	ppr = malloc(sizeof(struct puffs_putreq));
    111 	if (!ppr)
    112 		return NULL;
    113 
    114 	ppr->ppr_php.php_nops = 0;
    115 
    116 	ppr->ppr_pu = pu;
    117 	ppr->ppr_buf = &ppr->ppr_php.php_buf;
    118 	ppr->ppr_buflen = &ppr->ppr_php.php_buflen;
    119 	ppr->ppr_id = &ppr->ppr_php.php_id;
    120 
    121 	return ppr;
    122 }
    123 
    124 void
    125 puffs_putreq(struct puffs_putreq *ppr, struct puffs_req *preq)
    126 {
    127 
    128 	ppr->ppr_php.php_nops++;
    129 
    130 	/* store data */
    131 	*ppr->ppr_buf = preq;
    132 	*ppr->ppr_buflen = preq->preq_buflen;
    133 	*ppr->ppr_id = preq->preq_id;
    134 
    135 	/* and roll forward for next request */
    136 	ppr->ppr_buf = &preq->preq_nextbuf;
    137 	ppr->ppr_buflen = &preq->preq_buflen;
    138 	ppr->ppr_id = &preq->preq_id;
    139 }
    140 
    141 int
    142 puffs_putputreq(struct puffs_putreq *ppr)
    143 {
    144 
    145 	if (ppr->ppr_php.php_nops)
    146 		if (ioctl(ppr->ppr_pu->pu_fd, PUFFSPUTOP, &ppr->ppr_php) == -1)
    147 			return -1;
    148 
    149 	return 0;
    150 }
    151 
    152 void
    153 puffs_destroyputreq(struct puffs_putreq *ppr)
    154 {
    155 
    156 	free(ppr);
    157 }
    158