3 * \brief HwDep Interface (hardware dependent)
4 * \author Jaroslav Kysela <perex@perex.cz>
7 * HwDep (hardware dependent) Interface is designed for individual hardware
8 * access. This interface does not cover any API specification.
11 * Hardware dependent Interface - main file
12 * Copyright (c) 2000 by Jaroslav Kysela <perex@perex.cz>
15 * This library is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as
17 * published by the Free Software Foundation; either version 2.1 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <sys/ioctl.h>
37 #include "hwdep_local.h"
39 static int snd_hwdep_open_conf(snd_hwdep_t **hwdep,
40 const char *name, snd_config_t *hwdep_root,
41 snd_config_t *hwdep_conf, int mode)
46 snd_config_t *conf, *type_conf = NULL;
47 snd_config_iterator_t i, next;
49 const char *lib = NULL, *open_name = NULL;
50 int (*open_func)(snd_hwdep_t **, const char *, snd_config_t *, snd_config_t *, int) = NULL;
52 extern void *snd_hwdep_open_symbols(void);
55 if (snd_config_get_type(hwdep_conf) != SND_CONFIG_TYPE_COMPOUND) {
57 SNDERR("Invalid type for HWDEP %s definition", name);
59 SNDERR("Invalid type for HWDEP definition");
62 err = snd_config_search(hwdep_conf, "type", &conf);
64 SNDERR("type is not defined");
67 err = snd_config_get_id(conf, &id);
69 SNDERR("unable to get id");
72 err = snd_config_get_string(conf, &str);
74 SNDERR("Invalid type for %s", id);
77 err = snd_config_search_definition(hwdep_root, "hwdep_type", str, &type_conf);
79 if (snd_config_get_type(type_conf) != SND_CONFIG_TYPE_COMPOUND) {
80 SNDERR("Invalid type for HWDEP type %s definition", str);
84 snd_config_for_each(i, next, type_conf) {
85 snd_config_t *n = snd_config_iterator_entry(i);
87 if (snd_config_get_id(n, &id) < 0)
89 if (strcmp(id, "comment") == 0)
91 if (strcmp(id, "lib") == 0) {
92 err = snd_config_get_string(n, &lib);
94 SNDERR("Invalid type for %s", id);
99 if (strcmp(id, "open") == 0) {
100 err = snd_config_get_string(n, &open_name);
102 SNDERR("Invalid type for %s", id);
107 SNDERR("Unknown field %s", id);
114 snprintf(buf, sizeof(buf), "_snd_hwdep_%s_open", str);
117 snd_hwdep_open_symbols();
119 h = snd_dlopen(lib, RTLD_NOW);
121 open_func = snd_dlsym(h, open_name, SND_DLSYM_VERSION(SND_HWDEP_DLSYM_VERSION));
124 SNDERR("Cannot open shared library %s", lib);
126 } else if (!open_func) {
127 SNDERR("symbol %s is not defined inside %s", open_name, lib);
133 snd_config_delete(type_conf);
135 err = open_func(hwdep, name, hwdep_root, hwdep_conf, mode);
137 (*hwdep)->dl_handle = h;
145 static int snd_hwdep_open_noupdate(snd_hwdep_t **hwdep, snd_config_t *root, const char *name, int mode)
148 snd_config_t *hwdep_conf;
149 err = snd_config_search_definition(root, "hwdep", name, &hwdep_conf);
151 SNDERR("Unknown HwDep %s", name);
154 err = snd_hwdep_open_conf(hwdep, name, root, hwdep_conf, mode);
155 snd_config_delete(hwdep_conf);
160 * \brief Opens a new connection to the HwDep interface.
161 * \param hwdep Returned handle (NULL if not wanted)
162 * \param name ASCII identifier of the HwDep handle
163 * \param mode Open mode
164 * \return 0 on success otherwise a negative error code
166 * Opens a new connection to the HwDep interface specified with
167 * an ASCII identifier and mode.
169 int snd_hwdep_open(snd_hwdep_t **hwdep, const char *name, int mode)
172 assert(hwdep && name);
173 err = snd_config_update();
176 return snd_hwdep_open_noupdate(hwdep, snd_config, name, mode);
180 * \brief Opens a new connection to the HwDep interface using local configuration
181 * \param hwdep Returned handle (NULL if not wanted)
182 * \param name ASCII identifier of the HwDep handle
183 * \param mode Open mode
184 * \param lconf The local configuration tree
185 * \return 0 on success otherwise a negative error code
187 * Opens a new connection to the HwDep interface specified with
188 * an ASCII identifier and mode.
190 int snd_hwdep_open_lconf(snd_hwdep_t **hwdep, const char *name,
191 int mode, snd_config_t *lconf)
193 assert(hwdep && name && lconf);
194 return snd_hwdep_open_noupdate(hwdep, lconf, name, mode);
198 * \brief close HwDep handle
199 * \param hwdep HwDep handle
200 * \return 0 on success otherwise a negative error code
202 * Closes the specified HwDep handle and frees all associated
205 int snd_hwdep_close(snd_hwdep_t *hwdep)
209 err = hwdep->ops->close(hwdep);
210 if (hwdep->dl_handle)
211 snd_dlclose(hwdep->dl_handle);
218 * \brief get identifier of HwDep handle
219 * \param hwdep a Hwdep handle
220 * \return ascii identifier of HwDep handle
222 * Returns the ASCII identifier of given HwDep handle. It's the same
223 * identifier specified in snd_hwdep_open().
225 const char *snd_hwdep_name(snd_hwdep_t *hwdep)
232 * \brief get type of HwDep handle
233 * \param hwdep a HwDep handle
234 * \return type of HwDep handle
236 * Returns the type #snd_hwdep_type_t of given HwDep handle.
238 snd_hwdep_type_t snd_hwdep_type(snd_hwdep_t *hwdep)
245 * \brief get count of poll descriptors for HwDep handle
246 * \param hwdep HwDep handle
247 * \return count of poll descriptors
249 int snd_hwdep_poll_descriptors_count(snd_hwdep_t *hwdep)
256 * \brief get poll descriptors
257 * \param hwdep HwDep handle
258 * \param pfds array of poll descriptors
259 * \param space space in the poll descriptor array
260 * \return count of filled descriptors
262 int snd_hwdep_poll_descriptors(snd_hwdep_t *hwdep, struct pollfd *pfds, unsigned int space)
266 pfds->fd = hwdep->poll_fd;
267 switch (hwdep->mode & O_ACCMODE) {
269 pfds->events = POLLOUT|POLLERR|POLLNVAL;
272 pfds->events = POLLIN|POLLERR|POLLNVAL;
275 pfds->events = POLLOUT|POLLIN|POLLERR|POLLNVAL;
286 * \brief get returned events from poll descriptors
287 * \param hwdep HwDep handle
288 * \param pfds array of poll descriptors
289 * \param nfds count of poll descriptors
290 * \param revents returned events
291 * \return zero if success, otherwise a negative error code
293 int snd_hwdep_poll_descriptors_revents(snd_hwdep_t *hwdep, struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
295 assert(hwdep && pfds && revents);
297 *revents = pfds->revents;
304 * \brief set nonblock mode
305 * \param hwdep HwDep handle
306 * \param nonblock 0 = block, 1 = nonblock mode
307 * \return 0 on success otherwise a negative error code
309 int snd_hwdep_nonblock(snd_hwdep_t *hwdep, int nonblock)
313 if ((err = hwdep->ops->nonblock(hwdep, nonblock)) < 0)
316 hwdep->mode |= SND_HWDEP_OPEN_NONBLOCK;
318 hwdep->mode &= ~SND_HWDEP_OPEN_NONBLOCK;
323 * \brief get size of the snd_hwdep_info_t structure in bytes
324 * \return size of the snd_hwdep_info_t structure in bytes
326 size_t snd_hwdep_info_sizeof()
328 return sizeof(snd_hwdep_info_t);
332 * \brief allocate a new snd_hwdep_info_t structure
333 * \param info returned pointer
334 * \return 0 on success otherwise a negative error code if fails
336 * Allocates a new snd_hwdep_info_t structure using the standard
337 * malloc C library function.
339 int snd_hwdep_info_malloc(snd_hwdep_info_t **info)
342 *info = calloc(1, sizeof(snd_hwdep_info_t));
349 * \brief frees the snd_hwdep_info_t structure
350 * \param info pointer to the snd_hwdep_info_t structure to free
352 * Frees the given snd_hwdep_info_t structure using the standard
353 * free C library function.
355 void snd_hwdep_info_free(snd_hwdep_info_t *info)
362 * \brief copy one snd_hwdep_info_t structure to another
363 * \param dst destination snd_hwdep_info_t structure
364 * \param src source snd_hwdep_info_t structure
366 void snd_hwdep_info_copy(snd_hwdep_info_t *dst, const snd_hwdep_info_t *src)
373 * \brief get hwdep card number
374 * \param obj pointer to a snd_hwdep_info_t structure
375 * \return hwdep card number
377 int snd_hwdep_info_get_card(const snd_hwdep_info_t *obj)
384 * \brief get hwdep device number
385 * \param info pointer to a snd_hwdep_info_t structure
386 * \return hwdep device number
388 unsigned int snd_hwdep_info_get_device(const snd_hwdep_info_t *info)
395 * \brief get hwdep driver identifier
396 * \param obj pointer to a snd_hwdep_info_t structure
397 * \return hwdep driver identifier
399 const char *snd_hwdep_info_get_id(const snd_hwdep_info_t *obj)
402 return (const char *)obj->id;
406 * \brief get hwdep driver name
407 * \param obj pointer to a snd_hwdep_info_t structure
408 * \return hwdep driver name
410 const char *snd_hwdep_info_get_name(const snd_hwdep_info_t *obj)
413 return (const char *)obj->name;
417 * \brief get hwdep protocol interface
418 * \param obj pointer to a snd_hwdep_info_t structure
419 * \return hwdep protocol interface
421 snd_hwdep_iface_t snd_hwdep_info_get_iface(const snd_hwdep_info_t *obj)
428 * \brief set hwdep device number
429 * \param obj pointer to a snd_hwdep_info_t structure
430 * \param val hwdep device
432 void snd_hwdep_info_set_device(snd_hwdep_info_t *obj, unsigned int val)
439 * \brief get information about HwDep handle
440 * \param hwdep HwDep handle
441 * \param info pointer to a snd_hwdep_info_t structure to be filled
442 * \return 0 on success otherwise a negative error code
444 int snd_hwdep_info(snd_hwdep_t *hwdep, snd_hwdep_info_t * info)
448 return hwdep->ops->info(hwdep, info);
452 * \brief do hardware dependent ioctl
453 * \param hwdep HwDep handle
454 * \param request ioctl command
455 * \param arg ioctl argument
456 * \return 0 on success otherwise a negative error code
458 int snd_hwdep_ioctl(snd_hwdep_t *hwdep, unsigned int request, void * arg)
461 return hwdep->ops->ioctl(hwdep, request, arg);
465 * \brief write bytes using HwDep handle
466 * \param hwdep HwDep handle
467 * \param buffer buffer containing bytes to write
468 * \param size output buffer size in bytes
470 ssize_t snd_hwdep_write(snd_hwdep_t *hwdep, const void *buffer, size_t size)
473 assert(((hwdep->mode & O_ACCMODE) == O_WRONLY) || ((hwdep->mode & O_ACCMODE) == O_RDWR));
474 assert(buffer || size == 0);
475 return hwdep->ops->write(hwdep, buffer, size);
479 * \brief read bytes using HwDep handle
480 * \param hwdep HwDep handle
481 * \param buffer buffer to store the input bytes
482 * \param size input buffer size in bytes
484 ssize_t snd_hwdep_read(snd_hwdep_t *hwdep, void *buffer, size_t size)
487 assert(((hwdep->mode & O_ACCMODE) == O_RDONLY) || ((hwdep->mode & O_ACCMODE) == O_RDWR));
488 assert(buffer || size == 0);
489 return (hwdep->ops->read)(hwdep, buffer, size);
493 * \brief get the DSP status information
494 * \param hwdep HwDep handle
495 * \param info pointer to a snd_hwdep_dsp_status_t structure to be filled
496 * \return 0 on success otherwise a negative error code
498 int snd_hwdep_dsp_status(snd_hwdep_t *hwdep, snd_hwdep_dsp_status_t *info)
502 return hwdep->ops->ioctl(hwdep, SNDRV_HWDEP_IOCTL_DSP_STATUS, (void*)info);
506 * \brief load the DSP block
507 * \param hwdep HwDep handle
508 * \param block pointer to a snd_hwdep_dsp_image_t structure to transfer
509 * \return 0 on success otherwise a negative error code
511 int snd_hwdep_dsp_load(snd_hwdep_t *hwdep, snd_hwdep_dsp_image_t *block)
515 return hwdep->ops->ioctl(hwdep, SNDRV_HWDEP_IOCTL_DSP_LOAD, (void*)block);
519 * \brief get size of the snd_hwdep_dsp_status_t structure in bytes
520 * \return size of the snd_hwdep_dsp_status_t structure in bytes
522 size_t snd_hwdep_dsp_status_sizeof()
524 return sizeof(snd_hwdep_dsp_status_t);
528 * \brief allocate a new snd_hwdep_dsp_status_t structure
529 * \param info returned pointer
530 * \return 0 on success otherwise a negative error code if fails
532 * Allocates a new snd_hwdep_dsp_status_t structure using the standard
533 * malloc C library function.
535 int snd_hwdep_dsp_status_malloc(snd_hwdep_dsp_status_t **info)
538 *info = calloc(1, sizeof(snd_hwdep_dsp_status_t));
545 * \brief frees the snd_hwdep_dsp_status_t structure
546 * \param info pointer to the snd_hwdep_dsp_status_t structure to free
548 * Frees the given snd_hwdep_dsp_status_t structure using the standard
549 * free C library function.
551 void snd_hwdep_dsp_status_free(snd_hwdep_dsp_status_t *info)
558 * \brief copy one snd_hwdep_dsp_status_t structure to another
559 * \param dst destination snd_hwdep_dsp_status_t structure
560 * \param src source snd_hwdep_dsp_status_t structure
562 void snd_hwdep_dsp_status_copy(snd_hwdep_dsp_status_t *dst, const snd_hwdep_dsp_status_t *src)
569 * \brief get the driver version of dsp loader
570 * \param obj pointer to a snd_hwdep_dsp_status_t structure
571 * \return the driver version
573 unsigned int snd_hwdep_dsp_status_get_version(const snd_hwdep_dsp_status_t *obj)
580 * \brief get the driver id of dsp loader
581 * \param obj pointer to a snd_hwdep_dsp_status_t structure
582 * \return the driver id string
584 const char *snd_hwdep_dsp_status_get_id(const snd_hwdep_dsp_status_t *obj)
587 return (const char *)obj->id;
591 * \brief get number of dsp blocks
592 * \param obj pointer to a snd_hwdep_dsp_status_t structure
593 * \return number of dsp blocks
595 unsigned int snd_hwdep_dsp_status_get_num_dsps(const snd_hwdep_dsp_status_t *obj)
598 return obj->num_dsps;
602 * \brief get the bit flags of the loaded dsp blocks
603 * \param info pointer to a snd_hwdep_dsp_status_t structure
604 * \return the big flags of the loaded dsp blocks
606 unsigned int snd_hwdep_dsp_status_get_dsp_loaded(const snd_hwdep_dsp_status_t *info)
609 return info->dsp_loaded;
613 * \brief get the chip status of dsp loader
614 * \param obj pointer to a snd_hwdep_dsp_status_t structure
615 * \return non-zero if all DSP blocks are loaded and the chip is ready
617 unsigned int snd_hwdep_dsp_status_get_chip_ready(const snd_hwdep_dsp_status_t *obj)
620 return obj->chip_ready;
624 * \brief get size of the snd_hwdep_dsp_image_t structure in bytes
625 * \return size of the snd_hwdep_dsp_image_t structure in bytes
627 size_t snd_hwdep_dsp_image_sizeof()
629 return sizeof(snd_hwdep_dsp_image_t);
633 * \brief allocate a new snd_hwdep_dsp_image_t structure
634 * \param info returned pointer
635 * \return 0 on success otherwise a negative error code if fails
637 * Allocates a new snd_hwdep_dsp_image_t structure using the standard
638 * malloc C library function.
640 int snd_hwdep_dsp_image_malloc(snd_hwdep_dsp_image_t **info)
643 *info = calloc(1, sizeof(snd_hwdep_dsp_image_t));
650 * \brief frees the snd_hwdep_dsp_image_t structure
651 * \param info pointer to the snd_hwdep_dsp_image_t structure to free
653 * Frees the given snd_hwdep_dsp_image_t structure using the standard
654 * free C library function.
656 void snd_hwdep_dsp_image_free(snd_hwdep_dsp_image_t *info)
663 * \brief copy one snd_hwdep_dsp_image_t structure to another
664 * \param dst destination snd_hwdep_dsp_image_t structure
665 * \param src source snd_hwdep_dsp_image_t structure
667 void snd_hwdep_dsp_image_copy(snd_hwdep_dsp_image_t *dst, const snd_hwdep_dsp_image_t *src)
674 * \brief get the DSP block index
675 * \param obj pointer to a snd_hwdep_dsp_image_t structure
676 * \return the index of the DSP block
678 unsigned int snd_hwdep_dsp_image_get_index(const snd_hwdep_dsp_image_t *obj)
685 * \brief get the name of the DSP block
686 * \param obj pointer to a snd_hwdep_dsp_image_t structure
687 * \return the name string of the DSP block
689 const char *snd_hwdep_dsp_image_get_name(const snd_hwdep_dsp_image_t *obj)
692 return (const char *)obj->name;
696 * \brief get the length of the DSP block
697 * \param obj pointer to a snd_hwdep_dsp_image_t structure
698 * \return the length of the DSP block in bytes
700 size_t snd_hwdep_dsp_image_get_length(const snd_hwdep_dsp_image_t *obj)
707 * \brief get the image pointer of the DSP block
708 * \param obj pointer to a snd_hwdep_dsp_image_t structure
709 * \return the image pointer of the DSP block
711 const void *snd_hwdep_dsp_image_get_image(const snd_hwdep_dsp_image_t *obj)
718 * \brief set the DSP block index
719 * \param obj pointer to a snd_hwdep_dsp_image_t structure
720 * \param index the index value to set
722 void snd_hwdep_dsp_image_set_index(snd_hwdep_dsp_image_t *obj, unsigned int index)
729 * \brief set the name of the DSP block
730 * \param obj pointer to a snd_hwdep_dsp_image_t structure
731 * \param name the name string
733 void snd_hwdep_dsp_image_set_name(snd_hwdep_dsp_image_t *obj, const char *name)
736 strncpy((char *)obj->name, name, sizeof(obj->name));
737 obj->name[sizeof(obj->name)-1] = 0;
741 * \brief set the DSP block length
742 * \param obj pointer to a snd_hwdep_dsp_image_t structure
743 * \param length the length of the DSP block
745 void snd_hwdep_dsp_image_set_length(snd_hwdep_dsp_image_t *obj, size_t length)
748 obj->length = length;
752 * \brief set the DSP block image pointer
753 * \param obj pointer to a snd_hwdep_dsp_image_t structure
754 * \param image the DSP image pointer
756 void snd_hwdep_dsp_image_set_image(snd_hwdep_dsp_image_t *obj, void *image)