2 * HD-audio core bus driver
5 #include <linux/init.h>
6 #include <linux/device.h>
7 #include <linux/module.h>
8 #include <linux/export.h>
9 #include <sound/hdaudio.h>
12 static void process_unsol_events(struct work_struct
*work
);
15 * snd_hdac_bus_init - initialize a HD-audio bas bus
16 * @bus: the pointer to bus object
18 * Returns 0 if successful, or a negative error code.
20 int snd_hdac_bus_init(struct hdac_bus
*bus
, struct device
*dev
,
21 const struct hdac_bus_ops
*ops
)
23 memset(bus
, 0, sizeof(*bus
));
26 INIT_LIST_HEAD(&bus
->codec_list
);
27 INIT_WORK(&bus
->unsol_work
, process_unsol_events
);
28 mutex_init(&bus
->cmd_mutex
);
31 EXPORT_SYMBOL_GPL(snd_hdac_bus_init
);
34 * snd_hdac_bus_exit - clean up a HD-audio bas bus
35 * @bus: the pointer to bus object
37 void snd_hdac_bus_exit(struct hdac_bus
*bus
)
39 WARN_ON(!list_empty(&bus
->codec_list
));
40 cancel_work_sync(&bus
->unsol_work
);
42 EXPORT_SYMBOL_GPL(snd_hdac_bus_exit
);
45 * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
47 * @cmd: HD-audio encoded verb
48 * @res: pointer to store the response, NULL if performing asynchronously
50 * Returns 0 if successful, or a negative error code.
52 int snd_hdac_bus_exec_verb(struct hdac_bus
*bus
, unsigned int addr
,
53 unsigned int cmd
, unsigned int *res
)
57 mutex_lock(&bus
->cmd_mutex
);
58 err
= snd_hdac_bus_exec_verb_unlocked(bus
, addr
, cmd
, res
);
59 mutex_unlock(&bus
->cmd_mutex
);
62 EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb
);
65 * snd_hdac_bus_exec_verb_unlocked - unlocked version
67 * @cmd: HD-audio encoded verb
68 * @res: pointer to store the response, NULL if performing asynchronously
70 * Returns 0 if successful, or a negative error code.
72 int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus
*bus
, unsigned int addr
,
73 unsigned int cmd
, unsigned int *res
)
83 else if (bus
->sync_write
)
86 trace_hda_send_cmd(bus
, cmd
);
87 err
= bus
->ops
->command(bus
, cmd
);
90 /* process pending verbs */
91 err
= bus
->ops
->get_response(bus
, addr
, &tmp
);
96 err
= bus
->ops
->get_response(bus
, addr
, res
);
97 trace_hda_get_response(bus
, addr
, *res
);
101 EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked
);
104 * snd_hdac_bus_queue_event - add an unsolicited event to queue
106 * @res: unsolicited event (lower 32bit of RIRB entry)
107 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
109 * Adds the given event to the queue. The events are processed in
110 * the workqueue asynchronously. Call this function in the interrupt
111 * hanlder when RIRB receives an unsolicited event.
113 void snd_hdac_bus_queue_event(struct hdac_bus
*bus
, u32 res
, u32 res_ex
)
120 trace_hda_unsol_event(bus
, res
, res_ex
);
121 wp
= (bus
->unsol_wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
125 bus
->unsol_queue
[wp
] = res
;
126 bus
->unsol_queue
[wp
+ 1] = res_ex
;
128 schedule_work(&bus
->unsol_work
);
130 EXPORT_SYMBOL_GPL(snd_hdac_bus_queue_event
);
133 * process queued unsolicited events
135 static void process_unsol_events(struct work_struct
*work
)
137 struct hdac_bus
*bus
= container_of(work
, struct hdac_bus
, unsol_work
);
138 struct hdac_device
*codec
;
139 struct hdac_driver
*drv
;
140 unsigned int rp
, caddr
, res
;
142 while (bus
->unsol_rp
!= bus
->unsol_wp
) {
143 rp
= (bus
->unsol_rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
146 res
= bus
->unsol_queue
[rp
];
147 caddr
= bus
->unsol_queue
[rp
+ 1];
148 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
150 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
151 if (!codec
|| !codec
->dev
.driver
)
153 drv
= drv_to_hdac_driver(codec
->dev
.driver
);
154 if (drv
->unsol_event
)
155 drv
->unsol_event(codec
, res
);
159 int snd_hdac_bus_add_device(struct hdac_bus
*bus
, struct hdac_device
*codec
)
161 if (bus
->caddr_tbl
[codec
->addr
]) {
162 dev_err(bus
->dev
, "address 0x%x is already occupied\n",
167 list_add_tail(&codec
->list
, &bus
->codec_list
);
168 bus
->caddr_tbl
[codec
->addr
] = codec
;
169 set_bit(codec
->addr
, &bus
->codec_powered
);
173 EXPORT_SYMBOL_GPL(snd_hdac_bus_add_device
);
175 void snd_hdac_bus_remove_device(struct hdac_bus
*bus
,
176 struct hdac_device
*codec
)
178 WARN_ON(bus
!= codec
->bus
);
179 if (list_empty(&codec
->list
))
181 list_del_init(&codec
->list
);
182 bus
->caddr_tbl
[codec
->addr
] = NULL
;
183 clear_bit(codec
->addr
, &bus
->codec_powered
);
186 EXPORT_SYMBOL_GPL(snd_hdac_bus_remove_device
);