aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2015-03-03 11:33:10 -0500
committerTakashi Iwai <tiwai@suse.de>2015-03-23 08:19:35 -0400
commit71fc4c7ef5ef2d0ddd22f0545ede4c135b554b84 (patch)
tree2fad250a6c6f229aabd6b1aab41d24a50da511d9
parente311782acd196d17d25b323d115709c50c8f7d3f (diff)
ALSA: hda - Move generic array helpers to core lib
This will be used by the regmap support. Signed-off-by: Takashi Iwai <tiwai@suse.de>
-rw-r--r--include/sound/hdaudio.h33
-rw-r--r--sound/hda/Makefile3
-rw-r--r--sound/hda/array.c49
-rw-r--r--sound/pci/hda/hda_codec.c46
-rw-r--r--sound/pci/hda/hda_codec.h30
5 files changed, 84 insertions, 77 deletions
diff --git a/include/sound/hdaudio.h b/include/sound/hdaudio.h
index 675614dc2b88..3abdd3f16528 100644
--- a/include/sound/hdaudio.h
+++ b/include/sound/hdaudio.h
@@ -22,6 +22,17 @@ struct hdac_widget_tree;
22extern struct bus_type snd_hda_bus_type; 22extern struct bus_type snd_hda_bus_type;
23 23
24/* 24/*
25 * generic arrays
26 */
27struct snd_array {
28 unsigned int used;
29 unsigned int alloced;
30 unsigned int elem_size;
31 unsigned int alloc_align;
32 void *list;
33};
34
35/*
25 * HD-audio codec base device 36 * HD-audio codec base device
26 */ 37 */
27struct hdac_device { 38struct hdac_device {
@@ -178,4 +189,26 @@ static inline void snd_hdac_codec_link_down(struct hdac_device *codec)
178 clear_bit(codec->addr, &codec->bus->codec_powered); 189 clear_bit(codec->addr, &codec->bus->codec_powered);
179} 190}
180 191
192/*
193 * generic array helpers
194 */
195void *snd_array_new(struct snd_array *array);
196void snd_array_free(struct snd_array *array);
197static inline void snd_array_init(struct snd_array *array, unsigned int size,
198 unsigned int align)
199{
200 array->elem_size = size;
201 array->alloc_align = align;
202}
203
204static inline void *snd_array_elem(struct snd_array *array, unsigned int idx)
205{
206 return array->list + idx * array->elem_size;
207}
208
209static inline unsigned int snd_array_index(struct snd_array *array, void *ptr)
210{
211 return (unsigned long)(ptr - array->list) / array->elem_size;
212}
213
181#endif /* __SOUND_HDAUDIO_H */ 214#endif /* __SOUND_HDAUDIO_H */
diff --git a/sound/hda/Makefile b/sound/hda/Makefile
index eec5da03b41f..e508ba1102cb 100644
--- a/sound/hda/Makefile
+++ b/sound/hda/Makefile
@@ -1,4 +1,5 @@
1snd-hda-core-objs := hda_bus_type.o hdac_bus.o hdac_device.o hdac_sysfs.o 1snd-hda-core-objs := hda_bus_type.o hdac_bus.o hdac_device.o hdac_sysfs.o \
2 array.o
2 3
3snd-hda-core-objs += trace.o 4snd-hda-core-objs += trace.o
4CFLAGS_trace.o := -I$(src) 5CFLAGS_trace.o := -I$(src)
diff --git a/sound/hda/array.c b/sound/hda/array.c
new file mode 100644
index 000000000000..516795baa7db
--- /dev/null
+++ b/sound/hda/array.c
@@ -0,0 +1,49 @@
1/*
2 * generic arrays
3 */
4
5#include <linux/slab.h>
6#include <sound/core.h>
7#include <sound/hdaudio.h>
8
9/**
10 * snd_array_new - get a new element from the given array
11 * @array: the array object
12 *
13 * Get a new element from the given array. If it exceeds the
14 * pre-allocated array size, re-allocate the array.
15 *
16 * Returns NULL if allocation failed.
17 */
18void *snd_array_new(struct snd_array *array)
19{
20 if (snd_BUG_ON(!array->elem_size))
21 return NULL;
22 if (array->used >= array->alloced) {
23 int num = array->alloced + array->alloc_align;
24 int size = (num + 1) * array->elem_size;
25 void *nlist;
26 if (snd_BUG_ON(num >= 4096))
27 return NULL;
28 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
29 if (!nlist)
30 return NULL;
31 array->list = nlist;
32 array->alloced = num;
33 }
34 return snd_array_elem(array, array->used++);
35}
36EXPORT_SYMBOL_GPL(snd_array_new);
37
38/**
39 * snd_array_free - free the given array elements
40 * @array: the array object
41 */
42void snd_array_free(struct snd_array *array)
43{
44 kfree(array->list);
45 array->used = 0;
46 array->alloced = 0;
47 array->list = NULL;
48}
49EXPORT_SYMBOL_GPL(snd_array_free);
diff --git a/sound/pci/hda/hda_codec.c b/sound/pci/hda/hda_codec.c
index 145cae7903b6..10e257ff9084 100644
--- a/sound/pci/hda/hda_codec.c
+++ b/sound/pci/hda/hda_codec.c
@@ -5012,52 +5012,6 @@ void snd_hda_bus_reset(struct hda_bus *bus)
5012} 5012}
5013EXPORT_SYMBOL_GPL(snd_hda_bus_reset); 5013EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
5014 5014
5015/*
5016 * generic arrays
5017 */
5018
5019/**
5020 * snd_array_new - get a new element from the given array
5021 * @array: the array object
5022 *
5023 * Get a new element from the given array. If it exceeds the
5024 * pre-allocated array size, re-allocate the array.
5025 *
5026 * Returns NULL if allocation failed.
5027 */
5028void *snd_array_new(struct snd_array *array)
5029{
5030 if (snd_BUG_ON(!array->elem_size))
5031 return NULL;
5032 if (array->used >= array->alloced) {
5033 int num = array->alloced + array->alloc_align;
5034 int size = (num + 1) * array->elem_size;
5035 void *nlist;
5036 if (snd_BUG_ON(num >= 4096))
5037 return NULL;
5038 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5039 if (!nlist)
5040 return NULL;
5041 array->list = nlist;
5042 array->alloced = num;
5043 }
5044 return snd_array_elem(array, array->used++);
5045}
5046EXPORT_SYMBOL_GPL(snd_array_new);
5047
5048/**
5049 * snd_array_free - free the given array elements
5050 * @array: the array object
5051 */
5052void snd_array_free(struct snd_array *array)
5053{
5054 kfree(array->list);
5055 array->used = 0;
5056 array->alloced = 0;
5057 array->list = NULL;
5058}
5059EXPORT_SYMBOL_GPL(snd_array_free);
5060
5061/** 5015/**
5062 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer 5016 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5063 * @pcm: PCM caps bits 5017 * @pcm: PCM caps bits
diff --git a/sound/pci/hda/hda_codec.h b/sound/pci/hda/hda_codec.h
index 76776164623d..3068163b3db2 100644
--- a/sound/pci/hda/hda_codec.h
+++ b/sound/pci/hda/hda_codec.h
@@ -30,36 +30,6 @@
30#include <sound/hda_verbs.h> 30#include <sound/hda_verbs.h>
31 31
32/* 32/*
33 * generic arrays
34 */
35struct snd_array {
36 unsigned int used;
37 unsigned int alloced;
38 unsigned int elem_size;
39 unsigned int alloc_align;
40 void *list;
41};
42
43void *snd_array_new(struct snd_array *array);
44void snd_array_free(struct snd_array *array);
45static inline void snd_array_init(struct snd_array *array, unsigned int size,
46 unsigned int align)
47{
48 array->elem_size = size;
49 array->alloc_align = align;
50}
51
52static inline void *snd_array_elem(struct snd_array *array, unsigned int idx)
53{
54 return array->list + idx * array->elem_size;
55}
56
57static inline unsigned int snd_array_index(struct snd_array *array, void *ptr)
58{
59 return (unsigned long)(ptr - array->list) / array->elem_size;
60}
61
62/*
63 * Structures 33 * Structures
64 */ 34 */
65 35