Commit d068ebc2 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: hda - Move some codes up to hdac_bus struct

A few basic codes for communicating over HD-audio bus are moved to
struct hdac_bus now.  It has only command and get_response ops in
addition to the unsolicited event handling.

Note that the codec-side tracing support is disabled temporarily
during this transition due to the code shuffling.  It will be
re-enabled later once when all pieces are settled down.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent e3d280fc
...@@ -6,6 +6,11 @@ ...@@ -6,6 +6,11 @@
#define __SOUND_HDAUDIO_H #define __SOUND_HDAUDIO_H
#include <linux/device.h> #include <linux/device.h>
#include <sound/hda_verbs.h>
struct hdac_bus;
struct hdac_device;
struct hdac_driver;
/* /*
* exported bus type * exported bus type
...@@ -18,6 +23,9 @@ extern struct bus_type snd_hda_bus_type; ...@@ -18,6 +23,9 @@ extern struct bus_type snd_hda_bus_type;
struct hdac_device { struct hdac_device {
struct device dev; struct device dev;
int type; int type;
struct hdac_bus *bus;
unsigned int addr; /* codec address */
struct list_head list; /* list point for bus codec_list */
}; };
/* device/driver type used for matching */ /* device/driver type used for matching */
...@@ -35,8 +43,61 @@ struct hdac_driver { ...@@ -35,8 +43,61 @@ struct hdac_driver {
struct device_driver driver; struct device_driver driver;
int type; int type;
int (*match)(struct hdac_device *dev, struct hdac_driver *drv); int (*match)(struct hdac_device *dev, struct hdac_driver *drv);
void (*unsol_event)(struct hdac_device *dev, unsigned int event);
}; };
#define drv_to_hdac_driver(_drv) container_of(_drv, struct hdac_driver, driver) #define drv_to_hdac_driver(_drv) container_of(_drv, struct hdac_driver, driver)
/*
* HD-audio bus base driver
*/
struct hdac_bus_ops {
/* send a single command */
int (*command)(struct hdac_bus *bus, unsigned int cmd);
/* get a response from the last command */
int (*get_response)(struct hdac_bus *bus, unsigned int addr,
unsigned int *res);
};
#define HDA_UNSOL_QUEUE_SIZE 64
struct hdac_bus {
struct device *dev;
const struct hdac_bus_ops *ops;
/* codec linked list */
struct list_head codec_list;
unsigned int num_codecs;
/* link caddr -> codec */
struct hdac_device *caddr_tbl[HDA_MAX_CODEC_ADDRESS + 1];
/* unsolicited event queue */
u32 unsol_queue[HDA_UNSOL_QUEUE_SIZE * 2]; /* ring buffer */
unsigned int unsol_rp, unsol_wp;
struct work_struct unsol_work;
/* bit flags of powered codecs */
unsigned long codec_powered;
/* flags */
bool sync_write:1; /* sync after verb write */
/* locks */
struct mutex cmd_mutex;
};
int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
const struct hdac_bus_ops *ops);
void snd_hdac_bus_exit(struct hdac_bus *bus);
int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
unsigned int cmd, unsigned int *res);
int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
unsigned int cmd, unsigned int *res);
void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex);
int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec);
void snd_hdac_bus_remove_device(struct hdac_bus *bus,
struct hdac_device *codec);
#endif /* __SOUND_HDAUDIO_H */ #endif /* __SOUND_HDAUDIO_H */
snd-hda-core-objs := hda_bus_type.o snd-hda-core-objs := hda_bus_type.o hdac_bus.o
obj-$(CONFIG_SND_HDA_CORE) += snd-hda-core.o obj-$(CONFIG_SND_HDA_CORE) += snd-hda-core.o
/*
* HD-audio core bus driver
*/
#include <linux/init.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/export.h>
#include <sound/hdaudio.h>
static void process_unsol_events(struct work_struct *work);
/**
* snd_hdac_bus_init - initialize a HD-audio bas bus
* @bus: the pointer to bus object
*
* Returns 0 if successful, or a negative error code.
*/
int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
const struct hdac_bus_ops *ops)
{
memset(bus, 0, sizeof(*bus));
bus->dev = dev;
bus->ops = ops;
INIT_LIST_HEAD(&bus->codec_list);
INIT_WORK(&bus->unsol_work, process_unsol_events);
mutex_init(&bus->cmd_mutex);
return 0;
}
EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
/**
* snd_hdac_bus_exit - clean up a HD-audio bas bus
* @bus: the pointer to bus object
*/
void snd_hdac_bus_exit(struct hdac_bus *bus)
{
WARN_ON(!list_empty(&bus->codec_list));
cancel_work_sync(&bus->unsol_work);
}
EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
/**
* snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
* @bus: bus object
* @cmd: HD-audio encoded verb
* @res: pointer to store the response, NULL if performing asynchronously
*
* Returns 0 if successful, or a negative error code.
*/
int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
unsigned int cmd, unsigned int *res)
{
int err;
mutex_lock(&bus->cmd_mutex);
err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
mutex_unlock(&bus->cmd_mutex);
return err;
}
EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb);
/**
* snd_hdac_bus_exec_verb_unlocked - unlocked version
* @bus: bus object
* @cmd: HD-audio encoded verb
* @res: pointer to store the response, NULL if performing asynchronously
*
* Returns 0 if successful, or a negative error code.
*/
int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
unsigned int cmd, unsigned int *res)
{
unsigned int tmp;
int err;
if (cmd == ~0)
return -EINVAL;
if (res)
*res = -1;
else if (bus->sync_write)
res = &tmp;
for (;;) {
err = bus->ops->command(bus, cmd);
if (err != -EAGAIN)
break;
/* process pending verbs */
err = bus->ops->get_response(bus, addr, &tmp);
if (err)
break;
}
if (!err && res)
err = bus->ops->get_response(bus, addr, res);
return err;
}
EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
/**
* snd_hdac_bus_queue_event - add an unsolicited event to queue
* @bus: the BUS
* @res: unsolicited event (lower 32bit of RIRB entry)
* @res_ex: codec addr and flags (upper 32bit or RIRB entry)
*
* Adds the given event to the queue. The events are processed in
* the workqueue asynchronously. Call this function in the interrupt
* hanlder when RIRB receives an unsolicited event.
*/
void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
{
unsigned int wp;
if (!bus)
return;
wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
bus->unsol_wp = wp;
wp <<= 1;
bus->unsol_queue[wp] = res;
bus->unsol_queue[wp + 1] = res_ex;
schedule_work(&bus->unsol_work);
}
EXPORT_SYMBOL_GPL(snd_hdac_bus_queue_event);
/*
* process queued unsolicited events
*/
static void process_unsol_events(struct work_struct *work)
{
struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
struct hdac_device *codec;
struct hdac_driver *drv;
unsigned int rp, caddr, res;
while (bus->unsol_rp != bus->unsol_wp) {
rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
bus->unsol_rp = rp;
rp <<= 1;
res = bus->unsol_queue[rp];
caddr = bus->unsol_queue[rp + 1];
if (!(caddr & (1 << 4))) /* no unsolicited event? */
continue;
codec = bus->caddr_tbl[caddr & 0x0f];
if (!codec || !codec->dev.driver)
continue;
drv = drv_to_hdac_driver(codec->dev.driver);
if (drv->unsol_event)
drv->unsol_event(codec, res);
}
}
int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
{
if (bus->caddr_tbl[codec->addr]) {
dev_err(bus->dev, "address 0x%x is already occupied\n",
codec->addr);
return -EBUSY;
}
list_add_tail(&codec->list, &bus->codec_list);
bus->caddr_tbl[codec->addr] = codec;
set_bit(codec->addr, &bus->codec_powered);
bus->num_codecs++;
return 0;
}
EXPORT_SYMBOL_GPL(snd_hdac_bus_add_device);
void snd_hdac_bus_remove_device(struct hdac_bus *bus,
struct hdac_device *codec)
{
WARN_ON(bus != codec->bus);
if (list_empty(&codec->list))
return;
list_del_init(&codec->list);
bus->caddr_tbl[codec->addr] = NULL;
clear_bit(codec->addr, &bus->codec_powered);
bus->num_codecs--;
}
EXPORT_SYMBOL_GPL(snd_hdac_bus_remove_device);
...@@ -74,6 +74,15 @@ static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv) ...@@ -74,6 +74,15 @@ static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
return 0; return 0;
} }
/* process an unsolicited event */
static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
{
struct hda_codec *codec = container_of(dev, struct hda_codec, core);
if (codec->patch_ops.unsol_event)
codec->patch_ops.unsol_event(codec, ev);
}
/* reset the codec name from the preset */ /* reset the codec name from the preset */
static int codec_refresh_name(struct hda_codec *codec, const char *name) static int codec_refresh_name(struct hda_codec *codec, const char *name)
{ {
...@@ -163,6 +172,7 @@ int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name, ...@@ -163,6 +172,7 @@ int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
drv->core.driver.pm = &hda_codec_driver_pm; drv->core.driver.pm = &hda_codec_driver_pm;
drv->core.type = HDA_DEV_LEGACY; drv->core.type = HDA_DEV_LEGACY;
drv->core.match = hda_codec_match; drv->core.match = hda_codec_match;
drv->core.unsol_event = hda_codec_unsol_event;
return driver_register(&drv->core.driver); return driver_register(&drv->core.driver);
} }
EXPORT_SYMBOL_GPL(__hda_codec_driver_register); EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
......
This diff is collapsed.
...@@ -98,16 +98,6 @@ struct hda_bus_ops { ...@@ -98,16 +98,6 @@ struct hda_bus_ops {
#endif #endif
}; };
/* unsolicited event handler */
#define HDA_UNSOL_QUEUE_SIZE 64
struct hda_bus_unsolicited {
/* ring buffer */
u32 queue[HDA_UNSOL_QUEUE_SIZE * 2];
unsigned int rp, wp;
/* workqueue */
struct work_struct work;
};
/* /*
* codec bus * codec bus
* *
...@@ -115,6 +105,8 @@ struct hda_bus_unsolicited { ...@@ -115,6 +105,8 @@ struct hda_bus_unsolicited {
* A hda_bus contains several codecs in the list codec_list. * A hda_bus contains several codecs in the list codec_list.
*/ */
struct hda_bus { struct hda_bus {
struct hdac_bus core;
struct snd_card *card; struct snd_card *card;
void *private_data; void *private_data;
...@@ -122,25 +114,14 @@ struct hda_bus { ...@@ -122,25 +114,14 @@ struct hda_bus {
const char *modelname; const char *modelname;
struct hda_bus_ops ops; struct hda_bus_ops ops;
/* codec linked list */
struct list_head codec_list;
unsigned int num_codecs;
/* link caddr -> codec */
struct hda_codec *caddr_tbl[HDA_MAX_CODEC_ADDRESS + 1];
struct mutex cmd_mutex;
struct mutex prepare_mutex; struct mutex prepare_mutex;
/* unsolicited event queue */
struct hda_bus_unsolicited unsol;
/* assigned PCMs */ /* assigned PCMs */
DECLARE_BITMAP(pcm_dev_bits, SNDRV_PCM_DEVICES); DECLARE_BITMAP(pcm_dev_bits, SNDRV_PCM_DEVICES);
/* misc op flags */ /* misc op flags */
unsigned int needs_damn_long_delay :1; unsigned int needs_damn_long_delay :1;
unsigned int allow_bus_reset:1; /* allow bus reset at fatal error */ unsigned int allow_bus_reset:1; /* allow bus reset at fatal error */
unsigned int sync_write:1; /* sync after verb write */
/* status for codec/controller */ /* status for codec/controller */
unsigned int shutdown :1; /* being unloaded */ unsigned int shutdown :1; /* being unloaded */
unsigned int rirb_error:1; /* error in codec communication */ unsigned int rirb_error:1; /* error in codec communication */
...@@ -149,7 +130,6 @@ struct hda_bus { ...@@ -149,7 +130,6 @@ struct hda_bus {
unsigned int no_response_fallback:1; /* don't fallback at RIRB error */ unsigned int no_response_fallback:1; /* don't fallback at RIRB error */
int primary_dig_out_type; /* primary digital out PCM type */ int primary_dig_out_type; /* primary digital out PCM type */
unsigned long codec_powered; /* bit flags of powered codecs */
}; };
/* /*
...@@ -281,7 +261,6 @@ struct hda_codec { ...@@ -281,7 +261,6 @@ struct hda_codec {
struct hda_bus *bus; struct hda_bus *bus;
struct snd_card *card; struct snd_card *card;
unsigned int addr; /* codec addr*/ unsigned int addr; /* codec addr*/
struct list_head list; /* list point */
hda_nid_t afg; /* AFG node id */ hda_nid_t afg; /* AFG node id */
hda_nid_t mfg; /* MFG node id */ hda_nid_t mfg; /* MFG node id */
...@@ -413,6 +392,9 @@ struct hda_codec { ...@@ -413,6 +392,9 @@ struct hda_codec {
#define dev_to_hda_codec(_dev) container_of(_dev, struct hda_codec, core.dev) #define dev_to_hda_codec(_dev) container_of(_dev, struct hda_codec, core.dev)
#define hda_codec_dev(_dev) (&(_dev)->core.dev) #define hda_codec_dev(_dev) (&(_dev)->core.dev)
#define list_for_each_codec(c, bus) \
list_for_each_entry(c, &(bus)->core.codec_list, core.list)
/* direction */ /* direction */
enum { enum {
HDA_INPUT, HDA_OUTPUT HDA_INPUT, HDA_OUTPUT
...@@ -473,7 +455,11 @@ void snd_hda_sequence_write(struct hda_codec *codec, ...@@ -473,7 +455,11 @@ void snd_hda_sequence_write(struct hda_codec *codec,
const struct hda_verb *seq); const struct hda_verb *seq);
/* unsolicited event */ /* unsolicited event */
int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex); static inline void
snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
{
snd_hdac_bus_queue_event(&bus->core, res, res_ex);
}
/* cached write */ /* cached write */
int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
......
...@@ -1764,12 +1764,12 @@ static int probe_codec(struct azx *chip, int addr) ...@@ -1764,12 +1764,12 @@ static int probe_codec(struct azx *chip, int addr)
(AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID; (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
unsigned int res; unsigned int res;
mutex_lock(&chip->bus->cmd_mutex); mutex_lock(&chip->bus->core.cmd_mutex);
chip->probing = 1; chip->probing = 1;
azx_send_cmd(chip->bus, cmd); azx_send_cmd(chip->bus, cmd);
res = azx_get_response(chip->bus, addr); res = azx_get_response(chip->bus, addr);
chip->probing = 0; chip->probing = 0;
mutex_unlock(&chip->bus->cmd_mutex); mutex_unlock(&chip->bus->core.cmd_mutex);
if (res == -1) if (res == -1)
return -EIO; return -EIO;
dev_dbg(chip->card->dev, "codec #%d probed OK\n", addr); dev_dbg(chip->card->dev, "codec #%d probed OK\n", addr);
...@@ -1848,7 +1848,7 @@ int azx_bus_create(struct azx *chip, const char *model) ...@@ -1848,7 +1848,7 @@ int azx_bus_create(struct azx *chip, const char *model)
*/ */
if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) { if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
dev_dbg(chip->card->dev, "Enable sync_write for stable communication\n"); dev_dbg(chip->card->dev, "Enable sync_write for stable communication\n");
bus->sync_write = 1; bus->core.sync_write = 1;
bus->allow_bus_reset = 1; bus->allow_bus_reset = 1;
} }
...@@ -1913,7 +1913,7 @@ EXPORT_SYMBOL_GPL(azx_probe_codecs); ...@@ -1913,7 +1913,7 @@ EXPORT_SYMBOL_GPL(azx_probe_codecs);
int azx_codec_configure(struct azx *chip) int azx_codec_configure(struct azx *chip)
{ {
struct hda_codec *codec; struct hda_codec *codec;
list_for_each_entry(codec, &chip->bus->codec_list, list) { list_for_each_codec(codec, chip->bus) {
snd_hda_codec_configure(codec); snd_hda_codec_configure(codec);
} }
return 0; return 0;
......
...@@ -891,7 +891,7 @@ static int azx_runtime_resume(struct device *dev) ...@@ -891,7 +891,7 @@ static int azx_runtime_resume(struct device *dev)
bus = chip->bus; bus = chip->bus;
if (status && bus) { if (status && bus) {
list_for_each_entry(codec, &bus->codec_list, list) list_for_each_codec(codec, bus)
if (status & (1 << codec->addr)) if (status & (1 << codec->addr))
schedule_delayed_work(&codec->jackpoll_work, schedule_delayed_work(&codec->jackpoll_work,
codec->jackpoll_interval); codec->jackpoll_interval);
...@@ -919,7 +919,7 @@ static int azx_runtime_idle(struct device *dev) ...@@ -919,7 +919,7 @@ static int azx_runtime_idle(struct device *dev)
return 0; return 0;
if (!power_save_controller || !azx_has_pm_runtime(chip) || if (!power_save_controller || !azx_has_pm_runtime(chip) ||
chip->bus->codec_powered) chip->bus->core.codec_powered)
return -EBUSY; return -EBUSY;
return 0; return 0;
......
...@@ -552,7 +552,7 @@ static void parse_codec_mode(char *buf, struct hda_bus *bus, ...@@ -552,7 +552,7 @@ static void parse_codec_mode(char *buf, struct hda_bus *bus,
*codecp = NULL; *codecp = NULL;
if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) { if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
list_for_each_entry(codec, &bus->codec_list, list) { list_for_each_codec(codec, bus) {
if ((vendorid <= 0 || codec->vendor_id == vendorid) && if ((vendorid <= 0 || codec->vendor_id == vendorid) &&
(subid <= 0 || codec->subsystem_id == subid) && (subid <= 0 || codec->subsystem_id == subid) &&
codec->addr == caddr) { codec->addr == caddr) {
......
...@@ -919,10 +919,10 @@ static int patch_conexant_auto(struct hda_codec *codec) ...@@ -919,10 +919,10 @@ static int patch_conexant_auto(struct hda_codec *codec)
* which falls into the single-cmd mode. * which falls into the single-cmd mode.
* Better to make reset, then. * Better to make reset, then.
*/ */
if (!codec->bus->sync_write) { if (!codec->bus->core.sync_write) {
codec_info(codec, codec_info(codec,
"Enable sync_write for stable communication\n"); "Enable sync_write for stable communication\n");
codec->bus->sync_write = 1; codec->bus->core.sync_write = 1;
codec->bus->allow_bus_reset = 1; codec->bus->allow_bus_reset = 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment