ALSA: hda/realtek - change the location for one of two front mics
[linux-2.6-block.git] / sound / pci / emu10k1 / memory.c
CommitLineData
1da177e4 1/*
c1017a4c 2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
3 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
4 *
5 * EMU10K1 memory page allocation (PTB area)
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
1da177e4 24#include <linux/pci.h>
5a0e3ad6 25#include <linux/gfp.h>
1da177e4 26#include <linux/time.h>
62932df8 27#include <linux/mutex.h>
d81a6d71 28#include <linux/export.h>
62932df8 29
1da177e4
LT
30#include <sound/core.h>
31#include <sound/emu10k1.h>
32
33/* page arguments of these two macros are Emu page (4096 bytes), not like
34 * aligned pages in others
35 */
36#define __set_ptb_entry(emu,page,addr) \
541b9bad
MS
37 (((__le32 *)(emu)->ptb_pages.area)[page] = \
38 cpu_to_le32(((addr) << (emu->address_mode)) | (page)))
39#define __get_ptb_entry(emu, page) \
40 (le32_to_cpu(((__le32 *)(emu)->ptb_pages.area)[page]))
1da177e4
LT
41
42#define UNIT_PAGES (PAGE_SIZE / EMUPAGESIZE)
7241ea55
PZ
43#define MAX_ALIGN_PAGES0 (MAXPAGES0 / UNIT_PAGES)
44#define MAX_ALIGN_PAGES1 (MAXPAGES1 / UNIT_PAGES)
1da177e4
LT
45/* get aligned page from offset address */
46#define get_aligned_page(offset) ((offset) >> PAGE_SHIFT)
47/* get offset address from aligned page */
48#define aligned_page_offset(page) ((page) << PAGE_SHIFT)
49
541b9bad 50#if PAGE_SIZE == EMUPAGESIZE && !IS_ENABLED(CONFIG_DYNAMIC_DEBUG)
1da177e4
LT
51/* fill PTB entrie(s) corresponding to page with addr */
52#define set_ptb_entry(emu,page,addr) __set_ptb_entry(emu,page,addr)
53/* fill PTB entrie(s) corresponding to page with silence pointer */
54#define set_silent_ptb(emu,page) __set_ptb_entry(emu,page,emu->silent_page.addr)
55#else
56/* fill PTB entries -- we need to fill UNIT_PAGES entries */
eb4698f3 57static inline void set_ptb_entry(struct snd_emu10k1 *emu, int page, dma_addr_t addr)
1da177e4
LT
58{
59 int i;
60 page *= UNIT_PAGES;
61 for (i = 0; i < UNIT_PAGES; i++, page++) {
62 __set_ptb_entry(emu, page, addr);
541b9bad
MS
63 dev_dbg(emu->card->dev, "mapped page %d to entry %.8x\n", page,
64 (unsigned int)__get_ptb_entry(emu, page));
1da177e4
LT
65 addr += EMUPAGESIZE;
66 }
67}
eb4698f3 68static inline void set_silent_ptb(struct snd_emu10k1 *emu, int page)
1da177e4
LT
69{
70 int i;
71 page *= UNIT_PAGES;
541b9bad 72 for (i = 0; i < UNIT_PAGES; i++, page++) {
1da177e4
LT
73 /* do not increment ptr */
74 __set_ptb_entry(emu, page, emu->silent_page.addr);
541b9bad
MS
75 dev_dbg(emu->card->dev, "mapped silent page %d to entry %.8x\n",
76 page, (unsigned int)__get_ptb_entry(emu, page));
77 }
1da177e4
LT
78}
79#endif /* PAGE_SIZE */
80
81
82/*
83 */
eb4698f3
TI
84static int synth_alloc_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);
85static int synth_free_pages(struct snd_emu10k1 *hw, struct snd_emu10k1_memblk *blk);
1da177e4 86
eb4698f3 87#define get_emu10k1_memblk(l,member) list_entry(l, struct snd_emu10k1_memblk, member)
1da177e4
LT
88
89
90/* initialize emu10k1 part */
eb4698f3 91static void emu10k1_memblk_init(struct snd_emu10k1_memblk *blk)
1da177e4
LT
92{
93 blk->mapped_page = -1;
94 INIT_LIST_HEAD(&blk->mapped_link);
95 INIT_LIST_HEAD(&blk->mapped_order_link);
96 blk->map_locked = 0;
97
98 blk->first_page = get_aligned_page(blk->mem.offset);
99 blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1);
100 blk->pages = blk->last_page - blk->first_page + 1;
101}
102
103/*
104 * search empty region on PTB with the given size
105 *
106 * if an empty region is found, return the page and store the next mapped block
107 * in nextp
108 * if not found, return a negative error code.
109 */
eb4698f3 110static int search_empty_map_area(struct snd_emu10k1 *emu, int npages, struct list_head **nextp)
1da177e4 111{
a4463c92 112 int page = 1, found_page = -ENOMEM;
1da177e4
LT
113 int max_size = npages;
114 int size;
115 struct list_head *candidate = &emu->mapped_link_head;
116 struct list_head *pos;
117
118 list_for_each (pos, &emu->mapped_link_head) {
eb4698f3 119 struct snd_emu10k1_memblk *blk = get_emu10k1_memblk(pos, mapped_link);
da3cec35
TI
120 if (blk->mapped_page < 0)
121 continue;
1da177e4
LT
122 size = blk->mapped_page - page;
123 if (size == npages) {
124 *nextp = pos;
125 return page;
126 }
127 else if (size > max_size) {
128 /* we look for the maximum empty hole */
129 max_size = size;
130 candidate = pos;
131 found_page = page;
132 }
133 page = blk->mapped_page + blk->pages;
134 }
7241ea55 135 size = (emu->address_mode ? MAX_ALIGN_PAGES1 : MAX_ALIGN_PAGES0) - page;
1da177e4
LT
136 if (size >= max_size) {
137 *nextp = pos;
138 return page;
139 }
140 *nextp = candidate;
141 return found_page;
142}
143
144/*
145 * map a memory block onto emu10k1's PTB
146 *
147 * call with memblk_lock held
148 */
eb4698f3 149static int map_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4
LT
150{
151 int page, pg;
152 struct list_head *next;
153
154 page = search_empty_map_area(emu, blk->pages, &next);
155 if (page < 0) /* not found */
156 return page;
a4463c92
MS
157 if (page == 0) {
158 dev_err(emu->card->dev, "trying to map zero (reserved) page\n");
159 return -EINVAL;
160 }
1da177e4
LT
161 /* insert this block in the proper position of mapped list */
162 list_add_tail(&blk->mapped_link, next);
163 /* append this as a newest block in order list */
164 list_add_tail(&blk->mapped_order_link, &emu->mapped_order_link_head);
165 blk->mapped_page = page;
166 /* fill PTB */
167 for (pg = blk->first_page; pg <= blk->last_page; pg++) {
168 set_ptb_entry(emu, page, emu->page_addr_table[pg]);
169 page++;
170 }
171 return 0;
172}
173
174/*
175 * unmap the block
176 * return the size of resultant empty pages
177 *
178 * call with memblk_lock held
179 */
eb4698f3 180static int unmap_memblk(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4
LT
181{
182 int start_page, end_page, mpage, pg;
183 struct list_head *p;
eb4698f3 184 struct snd_emu10k1_memblk *q;
1da177e4
LT
185
186 /* calculate the expected size of empty region */
187 if ((p = blk->mapped_link.prev) != &emu->mapped_link_head) {
188 q = get_emu10k1_memblk(p, mapped_link);
189 start_page = q->mapped_page + q->pages;
190 } else
a4463c92 191 start_page = 1;
1da177e4
LT
192 if ((p = blk->mapped_link.next) != &emu->mapped_link_head) {
193 q = get_emu10k1_memblk(p, mapped_link);
194 end_page = q->mapped_page;
195 } else
7241ea55 196 end_page = (emu->address_mode ? MAX_ALIGN_PAGES1 : MAX_ALIGN_PAGES0);
1da177e4
LT
197
198 /* remove links */
199 list_del(&blk->mapped_link);
200 list_del(&blk->mapped_order_link);
201 /* clear PTB */
202 mpage = blk->mapped_page;
203 for (pg = blk->first_page; pg <= blk->last_page; pg++) {
204 set_silent_ptb(emu, mpage);
205 mpage++;
206 }
207 blk->mapped_page = -1;
208 return end_page - start_page; /* return the new empty size */
209}
210
211/*
212 * search empty pages with the given size, and create a memory block
213 *
214 * unlike synth_alloc the memory block is aligned to the page start
215 */
eb4698f3
TI
216static struct snd_emu10k1_memblk *
217search_empty(struct snd_emu10k1 *emu, int size)
1da177e4
LT
218{
219 struct list_head *p;
eb4698f3 220 struct snd_emu10k1_memblk *blk;
1da177e4
LT
221 int page, psize;
222
223 psize = get_aligned_page(size + PAGE_SIZE -1);
224 page = 0;
225 list_for_each(p, &emu->memhdr->block) {
226 blk = get_emu10k1_memblk(p, mem.list);
227 if (page + psize <= blk->first_page)
228 goto __found_pages;
229 page = blk->last_page + 1;
230 }
231 if (page + psize > emu->max_cache_pages)
232 return NULL;
233
234__found_pages:
235 /* create a new memory block */
eb4698f3 236 blk = (struct snd_emu10k1_memblk *)__snd_util_memblk_new(emu->memhdr, psize << PAGE_SHIFT, p->prev);
1da177e4
LT
237 if (blk == NULL)
238 return NULL;
239 blk->mem.offset = aligned_page_offset(page); /* set aligned offset */
240 emu10k1_memblk_init(blk);
241 return blk;
242}
243
244
245/*
246 * check if the given pointer is valid for pages
247 */
eb4698f3 248static int is_valid_page(struct snd_emu10k1 *emu, dma_addr_t addr)
1da177e4
LT
249{
250 if (addr & ~emu->dma_mask) {
6f002b02
TI
251 dev_err(emu->card->dev,
252 "max memory size is 0x%lx (addr = 0x%lx)!!\n",
253 emu->dma_mask, (unsigned long)addr);
1da177e4
LT
254 return 0;
255 }
256 if (addr & (EMUPAGESIZE-1)) {
6f002b02 257 dev_err(emu->card->dev, "page is not aligned\n");
1da177e4
LT
258 return 0;
259 }
260 return 1;
261}
262
263/*
264 * map the given memory block on PTB.
265 * if the block is already mapped, update the link order.
25985edc 266 * if no empty pages are found, tries to release unused memory blocks
1da177e4
LT
267 * and retry the mapping.
268 */
eb4698f3 269int snd_emu10k1_memblk_map(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4
LT
270{
271 int err;
272 int size;
273 struct list_head *p, *nextp;
eb4698f3 274 struct snd_emu10k1_memblk *deleted;
1da177e4
LT
275 unsigned long flags;
276
277 spin_lock_irqsave(&emu->memblk_lock, flags);
278 if (blk->mapped_page >= 0) {
279 /* update order link */
292f2b62
WY
280 list_move_tail(&blk->mapped_order_link,
281 &emu->mapped_order_link_head);
1da177e4
LT
282 spin_unlock_irqrestore(&emu->memblk_lock, flags);
283 return 0;
284 }
285 if ((err = map_memblk(emu, blk)) < 0) {
286 /* no enough page - try to unmap some blocks */
287 /* starting from the oldest block */
288 p = emu->mapped_order_link_head.next;
289 for (; p != &emu->mapped_order_link_head; p = nextp) {
290 nextp = p->next;
291 deleted = get_emu10k1_memblk(p, mapped_order_link);
292 if (deleted->map_locked)
293 continue;
294 size = unmap_memblk(emu, deleted);
295 if (size >= blk->pages) {
296 /* ok the empty region is enough large */
297 err = map_memblk(emu, blk);
298 break;
299 }
300 }
301 }
302 spin_unlock_irqrestore(&emu->memblk_lock, flags);
303 return err;
304}
305
2dd31dee
TI
306EXPORT_SYMBOL(snd_emu10k1_memblk_map);
307
1da177e4
LT
308/*
309 * page allocation for DMA
310 */
eb4698f3
TI
311struct snd_util_memblk *
312snd_emu10k1_alloc_pages(struct snd_emu10k1 *emu, struct snd_pcm_substream *substream)
1da177e4 313{
eb4698f3 314 struct snd_pcm_runtime *runtime = substream->runtime;
eb4698f3
TI
315 struct snd_util_memhdr *hdr;
316 struct snd_emu10k1_memblk *blk;
1da177e4
LT
317 int page, err, idx;
318
da3cec35
TI
319 if (snd_BUG_ON(!emu))
320 return NULL;
321 if (snd_BUG_ON(runtime->dma_bytes <= 0 ||
7241ea55 322 runtime->dma_bytes >= (emu->address_mode ? MAXPAGES1 : MAXPAGES0) * EMUPAGESIZE))
da3cec35 323 return NULL;
1da177e4 324 hdr = emu->memhdr;
da3cec35
TI
325 if (snd_BUG_ON(!hdr))
326 return NULL;
1da177e4 327
56385a12
JK
328 idx = runtime->period_size >= runtime->buffer_size ?
329 (emu->delay_pcm_irq * 2) : 0;
62932df8 330 mutex_lock(&hdr->block_mutex);
56385a12 331 blk = search_empty(emu, runtime->dma_bytes + idx);
1da177e4 332 if (blk == NULL) {
62932df8 333 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
334 return NULL;
335 }
336 /* fill buffer addresses but pointers are not stored so that
337 * snd_free_pci_page() is not called in in synth_free()
338 */
339 idx = 0;
340 for (page = blk->first_page; page <= blk->last_page; page++, idx++) {
77a23f26 341 unsigned long ofs = idx << PAGE_SHIFT;
1da177e4 342 dma_addr_t addr;
fcfb7866
TI
343 if (ofs >= runtime->dma_bytes)
344 addr = emu->silent_page.addr;
345 else
346 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
1da177e4 347 if (! is_valid_page(emu, addr)) {
6f002b02
TI
348 dev_err(emu->card->dev,
349 "emu: failure page = %d\n", idx);
62932df8 350 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
351 return NULL;
352 }
353 emu->page_addr_table[page] = addr;
354 emu->page_ptr_table[page] = NULL;
355 }
356
357 /* set PTB entries */
358 blk->map_locked = 1; /* do not unmap this block! */
359 err = snd_emu10k1_memblk_map(emu, blk);
360 if (err < 0) {
eb4698f3 361 __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk);
62932df8 362 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
363 return NULL;
364 }
62932df8 365 mutex_unlock(&hdr->block_mutex);
eb4698f3 366 return (struct snd_util_memblk *)blk;
1da177e4
LT
367}
368
369
370/*
371 * release DMA buffer from page table
372 */
eb4698f3 373int snd_emu10k1_free_pages(struct snd_emu10k1 *emu, struct snd_util_memblk *blk)
1da177e4 374{
da3cec35
TI
375 if (snd_BUG_ON(!emu || !blk))
376 return -EINVAL;
1da177e4
LT
377 return snd_emu10k1_synth_free(emu, blk);
378}
379
04f8773a
MS
380/*
381 * allocate DMA pages, widening the allocation if necessary
382 *
383 * See the comment above snd_emu10k1_detect_iommu() in emu10k1_main.c why
384 * this might be needed.
385 *
386 * If you modify this function check whether __synth_free_pages() also needs
387 * changes.
388 */
389int snd_emu10k1_alloc_pages_maybe_wider(struct snd_emu10k1 *emu, size_t size,
390 struct snd_dma_buffer *dmab)
391{
392 if (emu->iommu_workaround) {
393 size_t npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
394 size_t size_real = npages * PAGE_SIZE;
395
396 /*
397 * The device has been observed to accesses up to 256 extra
398 * bytes, but use 1k to be safe.
399 */
400 if (size_real < size + 1024)
401 size += PAGE_SIZE;
402 }
403
404 return snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
405 snd_dma_pci_data(emu->pci), size, dmab);
406}
1da177e4
LT
407
408/*
409 * memory allocation using multiple pages (for synth)
410 * Unlike the DMA allocation above, non-contiguous pages are assined.
411 */
412
413/*
414 * allocate a synth sample area
415 */
eb4698f3
TI
416struct snd_util_memblk *
417snd_emu10k1_synth_alloc(struct snd_emu10k1 *hw, unsigned int size)
1da177e4 418{
eb4698f3
TI
419 struct snd_emu10k1_memblk *blk;
420 struct snd_util_memhdr *hdr = hw->memhdr;
1da177e4 421
62932df8 422 mutex_lock(&hdr->block_mutex);
eb4698f3 423 blk = (struct snd_emu10k1_memblk *)__snd_util_mem_alloc(hdr, size);
1da177e4 424 if (blk == NULL) {
62932df8 425 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
426 return NULL;
427 }
428 if (synth_alloc_pages(hw, blk)) {
eb4698f3 429 __snd_util_mem_free(hdr, (struct snd_util_memblk *)blk);
62932df8 430 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
431 return NULL;
432 }
433 snd_emu10k1_memblk_map(hw, blk);
62932df8 434 mutex_unlock(&hdr->block_mutex);
eb4698f3 435 return (struct snd_util_memblk *)blk;
1da177e4
LT
436}
437
2dd31dee 438EXPORT_SYMBOL(snd_emu10k1_synth_alloc);
1da177e4
LT
439
440/*
441 * free a synth sample area
442 */
443int
eb4698f3 444snd_emu10k1_synth_free(struct snd_emu10k1 *emu, struct snd_util_memblk *memblk)
1da177e4 445{
eb4698f3
TI
446 struct snd_util_memhdr *hdr = emu->memhdr;
447 struct snd_emu10k1_memblk *blk = (struct snd_emu10k1_memblk *)memblk;
1da177e4
LT
448 unsigned long flags;
449
62932df8 450 mutex_lock(&hdr->block_mutex);
1da177e4
LT
451 spin_lock_irqsave(&emu->memblk_lock, flags);
452 if (blk->mapped_page >= 0)
453 unmap_memblk(emu, blk);
454 spin_unlock_irqrestore(&emu->memblk_lock, flags);
455 synth_free_pages(emu, blk);
456 __snd_util_mem_free(hdr, memblk);
62932df8 457 mutex_unlock(&hdr->block_mutex);
1da177e4
LT
458 return 0;
459}
460
2dd31dee 461EXPORT_SYMBOL(snd_emu10k1_synth_free);
1da177e4
LT
462
463/* check new allocation range */
eb4698f3
TI
464static void get_single_page_range(struct snd_util_memhdr *hdr,
465 struct snd_emu10k1_memblk *blk,
466 int *first_page_ret, int *last_page_ret)
1da177e4
LT
467{
468 struct list_head *p;
eb4698f3 469 struct snd_emu10k1_memblk *q;
1da177e4
LT
470 int first_page, last_page;
471 first_page = blk->first_page;
472 if ((p = blk->mem.list.prev) != &hdr->block) {
473 q = get_emu10k1_memblk(p, mem.list);
474 if (q->last_page == first_page)
475 first_page++; /* first page was already allocated */
476 }
477 last_page = blk->last_page;
478 if ((p = blk->mem.list.next) != &hdr->block) {
479 q = get_emu10k1_memblk(p, mem.list);
480 if (q->first_page == last_page)
481 last_page--; /* last page was already allocated */
482 }
483 *first_page_ret = first_page;
484 *last_page_ret = last_page;
485}
486
a5003fc0
TI
487/* release allocated pages */
488static void __synth_free_pages(struct snd_emu10k1 *emu, int first_page,
489 int last_page)
490{
055e0ae1 491 struct snd_dma_buffer dmab;
a5003fc0
TI
492 int page;
493
055e0ae1
MS
494 dmab.dev.type = SNDRV_DMA_TYPE_DEV;
495 dmab.dev.dev = snd_dma_pci_data(emu->pci);
496
a5003fc0 497 for (page = first_page; page <= last_page; page++) {
055e0ae1
MS
498 if (emu->page_ptr_table[page] == NULL)
499 continue;
500 dmab.area = emu->page_ptr_table[page];
501 dmab.addr = emu->page_addr_table[page];
04f8773a
MS
502
503 /*
504 * please keep me in sync with logic in
505 * snd_emu10k1_alloc_pages_maybe_wider()
506 */
055e0ae1 507 dmab.bytes = PAGE_SIZE;
04f8773a
MS
508 if (emu->iommu_workaround)
509 dmab.bytes *= 2;
510
055e0ae1 511 snd_dma_free_pages(&dmab);
a5003fc0
TI
512 emu->page_addr_table[page] = 0;
513 emu->page_ptr_table[page] = NULL;
514 }
515}
516
1da177e4
LT
517/*
518 * allocate kernel pages
519 */
eb4698f3 520static int synth_alloc_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4
LT
521{
522 int page, first_page, last_page;
055e0ae1 523 struct snd_dma_buffer dmab;
1da177e4
LT
524
525 emu10k1_memblk_init(blk);
526 get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
527 /* allocate kernel pages */
528 for (page = first_page; page <= last_page; page++) {
04f8773a
MS
529 if (snd_emu10k1_alloc_pages_maybe_wider(emu, PAGE_SIZE,
530 &dmab) < 0)
055e0ae1
MS
531 goto __fail;
532 if (!is_valid_page(emu, dmab.addr)) {
533 snd_dma_free_pages(&dmab);
534 goto __fail;
1da177e4 535 }
055e0ae1
MS
536 emu->page_addr_table[page] = dmab.addr;
537 emu->page_ptr_table[page] = dmab.area;
1da177e4
LT
538 }
539 return 0;
055e0ae1
MS
540
541__fail:
542 /* release allocated pages */
543 last_page = page - 1;
544 __synth_free_pages(emu, first_page, last_page);
545
546 return -ENOMEM;
1da177e4
LT
547}
548
549/*
550 * free pages
551 */
eb4698f3 552static int synth_free_pages(struct snd_emu10k1 *emu, struct snd_emu10k1_memblk *blk)
1da177e4 553{
a5003fc0 554 int first_page, last_page;
1da177e4
LT
555
556 get_single_page_range(emu->memhdr, blk, &first_page, &last_page);
a5003fc0 557 __synth_free_pages(emu, first_page, last_page);
1da177e4
LT
558 return 0;
559}
560
561/* calculate buffer pointer from offset address */
eb4698f3 562static inline void *offset_ptr(struct snd_emu10k1 *emu, int page, int offset)
1da177e4
LT
563{
564 char *ptr;
da3cec35
TI
565 if (snd_BUG_ON(page < 0 || page >= emu->max_cache_pages))
566 return NULL;
1da177e4
LT
567 ptr = emu->page_ptr_table[page];
568 if (! ptr) {
6f002b02
TI
569 dev_err(emu->card->dev,
570 "access to NULL ptr: page = %d\n", page);
1da177e4
LT
571 return NULL;
572 }
573 ptr += offset & (PAGE_SIZE - 1);
574 return (void*)ptr;
575}
576
577/*
578 * bzero(blk + offset, size)
579 */
eb4698f3
TI
580int snd_emu10k1_synth_bzero(struct snd_emu10k1 *emu, struct snd_util_memblk *blk,
581 int offset, int size)
1da177e4
LT
582{
583 int page, nextofs, end_offset, temp, temp1;
584 void *ptr;
eb4698f3 585 struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk;
1da177e4
LT
586
587 offset += blk->offset & (PAGE_SIZE - 1);
588 end_offset = offset + size;
589 page = get_aligned_page(offset);
590 do {
591 nextofs = aligned_page_offset(page + 1);
592 temp = nextofs - offset;
593 temp1 = end_offset - offset;
594 if (temp1 < temp)
595 temp = temp1;
596 ptr = offset_ptr(emu, page + p->first_page, offset);
597 if (ptr)
598 memset(ptr, 0, temp);
599 offset = nextofs;
600 page++;
601 } while (offset < end_offset);
602 return 0;
603}
604
2dd31dee
TI
605EXPORT_SYMBOL(snd_emu10k1_synth_bzero);
606
1da177e4
LT
607/*
608 * copy_from_user(blk + offset, data, size)
609 */
eb4698f3
TI
610int snd_emu10k1_synth_copy_from_user(struct snd_emu10k1 *emu, struct snd_util_memblk *blk,
611 int offset, const char __user *data, int size)
1da177e4
LT
612{
613 int page, nextofs, end_offset, temp, temp1;
614 void *ptr;
eb4698f3 615 struct snd_emu10k1_memblk *p = (struct snd_emu10k1_memblk *)blk;
1da177e4
LT
616
617 offset += blk->offset & (PAGE_SIZE - 1);
618 end_offset = offset + size;
619 page = get_aligned_page(offset);
620 do {
621 nextofs = aligned_page_offset(page + 1);
622 temp = nextofs - offset;
623 temp1 = end_offset - offset;
624 if (temp1 < temp)
625 temp = temp1;
626 ptr = offset_ptr(emu, page + p->first_page, offset);
627 if (ptr && copy_from_user(ptr, data, temp))
628 return -EFAULT;
629 offset = nextofs;
630 data += temp;
631 page++;
632 } while (offset < end_offset);
633 return 0;
634}
2dd31dee
TI
635
636EXPORT_SYMBOL(snd_emu10k1_synth_copy_from_user);