USB: g_file_storage: ignore bulk-out data after invalid CBW
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / crypto / scatterwalk.c
blob9aeeb52004a5399d0a9b7a3484044b5de1e3083c
1 /*
2 * Cryptographic API.
4 * Cipher operations.
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 * 2002 Adam J. Richter <adam@yggdrasil.com>
8 * 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
17 #include <crypto/scatterwalk.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/scatterlist.h>
25 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
27 void *src = out ? buf : sgdata;
28 void *dst = out ? sgdata : buf;
30 memcpy(dst, src, nbytes);
33 void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
35 walk->sg = sg;
37 BUG_ON(!sg->length);
39 walk->offset = sg->offset;
41 EXPORT_SYMBOL_GPL(scatterwalk_start);
43 void *scatterwalk_map(struct scatter_walk *walk, int out)
45 return crypto_kmap(scatterwalk_page(walk), out) +
46 offset_in_page(walk->offset);
48 EXPORT_SYMBOL_GPL(scatterwalk_map);
50 static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
51 unsigned int more)
53 if (out) {
54 struct page *page;
56 page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
57 flush_dcache_page(page);
60 if (more) {
61 walk->offset += PAGE_SIZE - 1;
62 walk->offset &= PAGE_MASK;
63 if (walk->offset >= walk->sg->offset + walk->sg->length)
64 scatterwalk_start(walk, scatterwalk_sg_next(walk->sg));
68 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
70 if (!offset_in_page(walk->offset) || !more)
71 scatterwalk_pagedone(walk, out, more);
73 EXPORT_SYMBOL_GPL(scatterwalk_done);
75 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
76 size_t nbytes, int out)
78 for (;;) {
79 unsigned int len_this_page = scatterwalk_pagelen(walk);
80 u8 *vaddr;
82 if (len_this_page > nbytes)
83 len_this_page = nbytes;
85 vaddr = scatterwalk_map(walk, out);
86 memcpy_dir(buf, vaddr, len_this_page, out);
87 scatterwalk_unmap(vaddr, out);
89 scatterwalk_advance(walk, len_this_page);
91 if (nbytes == len_this_page)
92 break;
94 buf += len_this_page;
95 nbytes -= len_this_page;
97 scatterwalk_pagedone(walk, out, 1);
100 EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
102 void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
103 unsigned int start, unsigned int nbytes, int out)
105 struct scatter_walk walk;
106 unsigned int offset = 0;
108 if (!nbytes)
109 return;
111 for (;;) {
112 scatterwalk_start(&walk, sg);
114 if (start < offset + sg->length)
115 break;
117 offset += sg->length;
118 sg = scatterwalk_sg_next(sg);
121 scatterwalk_advance(&walk, start - offset);
122 scatterwalk_copychunks(buf, &walk, nbytes, out);
123 scatterwalk_done(&walk, out, 0);
125 EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);