blob: 57ceb13f281514fc3acb9dddbfcd313b56d7eb56 [file] [log] [blame]
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001/*
2 * TC Applied Technologies Digital Interface Communications Engine driver
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
7
Clemens Ladisch0c29c912011-09-04 22:14:15 +02008#include <linux/compat.h>
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02009#include <linux/delay.h>
10#include <linux/device.h>
11#include <linux/firewire.h>
12#include <linux/firewire-constants.h>
13#include <linux/module.h>
14#include <linux/mod_devicetable.h>
15#include <linux/mutex.h>
16#include <linux/slab.h>
Clemens Ladisch0c29c912011-09-04 22:14:15 +020017#include <linux/spinlock.h>
18#include <linux/wait.h>
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020019#include <sound/control.h>
20#include <sound/core.h>
Clemens Ladisch0c29c912011-09-04 22:14:15 +020021#include <sound/firewire.h>
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020022#include <sound/hwdep.h>
23#include <sound/initval.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include "amdtp.h"
27#include "iso-resources.h"
28#include "lib.h"
Clemens Ladisch54e72f02011-09-04 22:15:54 +020029#include "dice-interface.h"
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020030
31
32struct dice {
33 struct snd_card *card;
34 struct fw_unit *unit;
Clemens Ladisch0c29c912011-09-04 22:14:15 +020035 spinlock_t lock;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020036 struct mutex mutex;
37 unsigned int global_offset;
38 unsigned int rx_offset;
39 struct fw_address_handler notification_handler;
40 int owner_generation;
Clemens Ladisch0c29c912011-09-04 22:14:15 +020041 int dev_lock_count; /* > 0 driver, < 0 userspace */
42 bool dev_lock_changed;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020043 bool global_enabled;
Clemens Ladisch0c29c912011-09-04 22:14:15 +020044 wait_queue_head_t hwdep_wait;
45 u32 notification_bits;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +020046 struct snd_pcm_substream *pcm;
47 struct fw_iso_resources resources;
48 struct amdtp_out_stream stream;
49};
50
51MODULE_DESCRIPTION("DICE driver");
52MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
53MODULE_LICENSE("GPL v2");
54
Clemens Ladisch341682c2011-09-04 22:12:06 +020055static const unsigned int dice_rates[] = {
56 [0] = 32000,
57 [1] = 44100,
58 [2] = 48000,
59 [3] = 88200,
60 [4] = 96000,
61 [5] = 176400,
62 [6] = 192000,
63};
64
Clemens Ladisch0c29c912011-09-04 22:14:15 +020065static void dice_lock_changed(struct dice *dice)
66{
67 dice->dev_lock_changed = true;
68 wake_up(&dice->hwdep_wait);
69}
70
71static int dice_try_lock(struct dice *dice)
72{
73 int err;
74
75 spin_lock_irq(&dice->lock);
76
77 if (dice->dev_lock_count < 0) {
78 err = -EBUSY;
79 goto out;
80 }
81
82 if (dice->dev_lock_count++ == 0)
83 dice_lock_changed(dice);
84 err = 0;
85
86out:
87 spin_unlock_irq(&dice->lock);
88
89 return err;
90}
91
92static void dice_unlock(struct dice *dice)
93{
94 spin_lock_irq(&dice->lock);
95
96 if (WARN_ON(dice->dev_lock_count <= 0))
97 goto out;
98
99 if (--dice->dev_lock_count == 0)
100 dice_lock_changed(dice);
101
102out:
103 spin_unlock_irq(&dice->lock);
104}
105
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200106static inline u64 global_address(struct dice *dice, unsigned int offset)
107{
108 return DICE_PRIVATE_SPACE + dice->global_offset + offset;
109}
110
111// TODO: rx index
112static inline u64 rx_address(struct dice *dice, unsigned int offset)
113{
114 return DICE_PRIVATE_SPACE + dice->rx_offset + offset;
115}
116
117static int dice_owner_set(struct dice *dice)
118{
119 struct fw_device *device = fw_parent_device(dice->unit);
120 __be64 *buffer;
Clemens Ladisch1b704852011-09-04 22:17:38 +0200121 int err, errors = 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200122
123 buffer = kmalloc(2 * 8, GFP_KERNEL);
124 if (!buffer)
125 return -ENOMEM;
126
127 for (;;) {
128 buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
129 buffer[1] = cpu_to_be64(
130 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
131 dice->notification_handler.offset);
132
133 dice->owner_generation = device->generation;
134 smp_rmb(); /* node_id vs. generation */
Clemens Ladisch1b704852011-09-04 22:17:38 +0200135 err = snd_fw_transaction(dice->unit,
136 TCODE_LOCK_COMPARE_SWAP,
137 global_address(dice, GLOBAL_OWNER),
138 buffer, 2 * 8,
139 FW_FIXED_GENERATION |
140 dice->owner_generation);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200141
Clemens Ladisch1b704852011-09-04 22:17:38 +0200142 if (err == 0) {
143 if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200144 dev_err(&dice->unit->device,
145 "device is already in use\n");
146 err = -EBUSY;
147 }
148 break;
149 }
Clemens Ladisch1b704852011-09-04 22:17:38 +0200150 if (err != -EAGAIN || ++errors >= 3)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200151 break;
Clemens Ladisch1b704852011-09-04 22:17:38 +0200152
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200153 msleep(20);
154 }
155
156 kfree(buffer);
157
158 return err;
159}
160
161static int dice_owner_update(struct dice *dice)
162{
163 struct fw_device *device = fw_parent_device(dice->unit);
164 __be64 *buffer;
Clemens Ladisch1b704852011-09-04 22:17:38 +0200165 int err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200166
167 if (dice->owner_generation == -1)
168 return 0;
169
170 buffer = kmalloc(2 * 8, GFP_KERNEL);
171 if (!buffer)
172 return -ENOMEM;
173
Clemens Ladisch1b704852011-09-04 22:17:38 +0200174 buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
175 buffer[1] = cpu_to_be64(
176 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
177 dice->notification_handler.offset);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200178
Clemens Ladisch1b704852011-09-04 22:17:38 +0200179 dice->owner_generation = device->generation;
180 smp_rmb(); /* node_id vs. generation */
181 err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
182 global_address(dice, GLOBAL_OWNER),
183 buffer, 2 * 8,
184 FW_FIXED_GENERATION | dice->owner_generation);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200185
Clemens Ladisch1b704852011-09-04 22:17:38 +0200186 if (err == 0) {
187 if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200188 dev_err(&dice->unit->device,
Clemens Ladisch1b704852011-09-04 22:17:38 +0200189 "device is already in use\n");
190 err = -EBUSY;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200191 }
Clemens Ladisch1b704852011-09-04 22:17:38 +0200192 } else if (err == -EAGAIN) {
193 err = 0; /* try again later */
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200194 }
195
196 kfree(buffer);
197
198 if (err < 0)
199 dice->owner_generation = -1;
200
201 return err;
202}
203
204static void dice_owner_clear(struct dice *dice)
205{
206 struct fw_device *device = fw_parent_device(dice->unit);
207 __be64 *buffer;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200208
209 buffer = kmalloc(2 * 8, GFP_KERNEL);
210 if (!buffer)
211 return;
212
Clemens Ladisch1b704852011-09-04 22:17:38 +0200213 buffer[0] = cpu_to_be64(
214 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
215 dice->notification_handler.offset);
216 buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
217 snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
218 global_address(dice, GLOBAL_OWNER),
219 buffer, 2 * 8, FW_QUIET |
220 FW_FIXED_GENERATION | dice->owner_generation);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200221
222 kfree(buffer);
223
224 dice->owner_generation = -1;
225}
226
227static int dice_enable_set(struct dice *dice)
228{
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200229 __be32 value;
Clemens Ladisch1b704852011-09-04 22:17:38 +0200230 int err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200231
Clemens Ladisch54e72f02011-09-04 22:15:54 +0200232 value = cpu_to_be32(1);
Clemens Ladisch1b704852011-09-04 22:17:38 +0200233 err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
234 global_address(dice, GLOBAL_ENABLE),
235 &value, 4,
236 FW_FIXED_GENERATION | dice->owner_generation);
237 if (err < 0)
238 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200239
Clemens Ladisch1b704852011-09-04 22:17:38 +0200240 dice->global_enabled = true;
241
242 return 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200243}
244
245static void dice_enable_clear(struct dice *dice)
246{
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200247 __be32 value;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200248
Clemens Ladischeadce072011-09-04 22:17:45 +0200249 if (!dice->global_enabled)
250 return;
251
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200252 value = 0;
Clemens Ladisch1b704852011-09-04 22:17:38 +0200253 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
254 global_address(dice, GLOBAL_ENABLE),
255 &value, 4, FW_QUIET |
256 FW_FIXED_GENERATION | dice->owner_generation);
257
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200258 dice->global_enabled = false;
259}
260
261static void dice_notification(struct fw_card *card, struct fw_request *request,
262 int tcode, int destination, int source,
263 int generation, unsigned long long offset,
264 void *data, size_t length, void *callback_data)
265{
266 struct dice *dice = callback_data;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200267 unsigned long flags;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200268
269 if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
270 fw_send_response(card, request, RCODE_TYPE_ERROR);
271 return;
272 }
273 if ((offset & 3) != 0) {
274 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
275 return;
276 }
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200277 spin_lock_irqsave(&dice->lock, flags);
278 dice->notification_bits |= be32_to_cpup(data);
279 spin_unlock_irqrestore(&dice->lock, flags);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200280 fw_send_response(card, request, RCODE_COMPLETE);
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200281 wake_up(&dice->hwdep_wait);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200282}
283
284static int dice_open(struct snd_pcm_substream *substream)
285{
286 static const struct snd_pcm_hardware hardware = {
287 .info = SNDRV_PCM_INFO_MMAP |
288 SNDRV_PCM_INFO_MMAP_VALID |
289 SNDRV_PCM_INFO_BATCH |
290 SNDRV_PCM_INFO_INTERLEAVED |
291 SNDRV_PCM_INFO_BLOCK_TRANSFER,
292 .formats = AMDTP_OUT_PCM_FORMAT_BITS,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200293 .buffer_bytes_max = 16 * 1024 * 1024,
294 .period_bytes_min = 1,
295 .period_bytes_max = UINT_MAX,
296 .periods_min = 1,
297 .periods_max = UINT_MAX,
298 };
299 struct dice *dice = substream->private_data;
300 struct snd_pcm_runtime *runtime = substream->runtime;
Clemens Ladischa644a942011-09-04 22:17:31 +0200301 __be32 clock_sel, data[2];
302 unsigned int rate_index, number_audio, number_midi;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200303 int err;
304
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200305 err = dice_try_lock(dice);
306 if (err < 0)
307 goto error;
308
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200309 err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
Clemens Ladisch341682c2011-09-04 22:12:06 +0200310 global_address(dice, GLOBAL_CLOCK_SELECT),
Clemens Ladisch1b704852011-09-04 22:17:38 +0200311 &clock_sel, 4, 0);
Clemens Ladisch341682c2011-09-04 22:12:06 +0200312 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200313 goto err_lock;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200314 rate_index = (be32_to_cpu(clock_sel) & CLOCK_RATE_MASK)
315 >> CLOCK_RATE_SHIFT;
316 if (rate_index >= ARRAY_SIZE(dice_rates)) {
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200317 err = -ENXIO;
318 goto err_lock;
319 }
Clemens Ladisch341682c2011-09-04 22:12:06 +0200320
Clemens Ladischa644a942011-09-04 22:17:31 +0200321 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200322 rx_address(dice, RX_NUMBER_AUDIO),
Clemens Ladisch1b704852011-09-04 22:17:38 +0200323 data, 2 * 4, 0);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200324 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200325 goto err_lock;
Clemens Ladischa644a942011-09-04 22:17:31 +0200326 number_audio = be32_to_cpu(data[0]);
327 number_midi = be32_to_cpu(data[1]);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200328
329 runtime->hw = hardware;
Clemens Ladisch341682c2011-09-04 22:12:06 +0200330
Clemens Ladischa644a942011-09-04 22:17:31 +0200331 runtime->hw.rates = snd_pcm_rate_to_rate_bit(dice_rates[rate_index]);
Clemens Ladisch341682c2011-09-04 22:12:06 +0200332 snd_pcm_limit_hw_rates(runtime);
333
Clemens Ladischa644a942011-09-04 22:17:31 +0200334 runtime->hw.channels_min = number_audio;
335 runtime->hw.channels_max = number_audio;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200336
Clemens Ladischa644a942011-09-04 22:17:31 +0200337 amdtp_out_stream_set_parameters(&dice->stream, dice_rates[rate_index],
338 number_audio, number_midi);
Clemens Ladischa7304e32011-09-04 22:16:10 +0200339
340 err = snd_pcm_hw_constraint_step(runtime, 0,
341 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
342 amdtp_syt_intervals[rate_index]);
343 if (err < 0)
344 goto err_lock;
345 err = snd_pcm_hw_constraint_step(runtime, 0,
346 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
347 amdtp_syt_intervals[rate_index]);
348 if (err < 0)
349 goto err_lock;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200350
351 err = snd_pcm_hw_constraint_minmax(runtime,
352 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
Clemens Ladisch435a9be2011-09-04 22:17:51 +0200353 5000, UINT_MAX);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200354 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200355 goto err_lock;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200356
357 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
358 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200359 goto err_lock;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200360
361 return 0;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200362
363err_lock:
364 dice_unlock(dice);
365error:
366 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200367}
368
369static int dice_close(struct snd_pcm_substream *substream)
370{
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200371 struct dice *dice = substream->private_data;
372
373 dice_unlock(dice);
374
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200375 return 0;
376}
377
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200378static int dice_stream_start_packets(struct dice *dice)
379{
380 int err;
381
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200382 if (amdtp_out_stream_running(&dice->stream))
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200383 return 0;
384
385 err = amdtp_out_stream_start(&dice->stream, dice->resources.channel,
386 fw_parent_device(dice->unit)->max_speed);
387 if (err < 0)
388 return err;
389
390 err = dice_enable_set(dice);
391 if (err < 0) {
392 amdtp_out_stream_stop(&dice->stream);
393 return err;
394 }
395
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200396 return 0;
397}
398
399static int dice_stream_start(struct dice *dice)
400{
401 __be32 channel;
402 int err;
403
404 if (!dice->resources.allocated) {
405 err = fw_iso_resources_allocate(&dice->resources,
406 amdtp_out_stream_get_max_payload(&dice->stream),
407 fw_parent_device(dice->unit)->max_speed);
408 if (err < 0)
409 goto error;
410
411 channel = cpu_to_be32(dice->resources.channel);
412 err = snd_fw_transaction(dice->unit,
413 TCODE_WRITE_QUADLET_REQUEST,
414 rx_address(dice, RX_ISOCHRONOUS),
Clemens Ladisch1b704852011-09-04 22:17:38 +0200415 &channel, 4, 0);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200416 if (err < 0)
417 goto err_resources;
418 }
419
420 err = dice_stream_start_packets(dice);
421 if (err < 0)
422 goto err_rx_channel;
423
424 return 0;
425
426err_rx_channel:
427 channel = cpu_to_be32((u32)-1);
428 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
Clemens Ladisch1b704852011-09-04 22:17:38 +0200429 rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200430err_resources:
431 fw_iso_resources_free(&dice->resources);
432error:
433 return err;
434}
435
436static void dice_stream_stop_packets(struct dice *dice)
437{
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200438 if (amdtp_out_stream_running(&dice->stream)) {
439 dice_enable_clear(dice);
440 amdtp_out_stream_stop(&dice->stream);
441 }
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200442}
443
444static void dice_stream_stop(struct dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200445{
446 __be32 channel;
447
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200448 dice_stream_stop_packets(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200449
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200450 if (!dice->resources.allocated)
451 return;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200452
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200453 channel = cpu_to_be32((u32)-1);
454 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
Clemens Ladisch1b704852011-09-04 22:17:38 +0200455 rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200456
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200457 fw_iso_resources_free(&dice->resources);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200458}
459
460static int dice_hw_params(struct snd_pcm_substream *substream,
461 struct snd_pcm_hw_params *hw_params)
462{
463 struct dice *dice = substream->private_data;
464 int err;
465
466 mutex_lock(&dice->mutex);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200467 dice_stream_stop(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200468 mutex_unlock(&dice->mutex);
469
470 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
471 params_buffer_bytes(hw_params));
472 if (err < 0)
473 goto error;
474
475 amdtp_out_stream_set_pcm_format(&dice->stream,
476 params_format(hw_params));
477
478 return 0;
479
480error:
481 return err;
482}
483
484static int dice_hw_free(struct snd_pcm_substream *substream)
485{
486 struct dice *dice = substream->private_data;
487
488 mutex_lock(&dice->mutex);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200489 dice_stream_stop(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200490 mutex_unlock(&dice->mutex);
491
492 return snd_pcm_lib_free_vmalloc_buffer(substream);
493}
494
495static int dice_prepare(struct snd_pcm_substream *substream)
496{
497 struct dice *dice = substream->private_data;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200498 int err;
499
500 mutex_lock(&dice->mutex);
501
502 if (amdtp_out_streaming_error(&dice->stream))
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200503 dice_stream_stop_packets(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200504
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200505 err = dice_stream_start(dice);
506 if (err < 0) {
507 mutex_unlock(&dice->mutex);
508 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200509 }
510
511 mutex_unlock(&dice->mutex);
512
513 amdtp_out_stream_pcm_prepare(&dice->stream);
514
515 return 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200516}
517
518static int dice_trigger(struct snd_pcm_substream *substream, int cmd)
519{
520 struct dice *dice = substream->private_data;
521 struct snd_pcm_substream *pcm;
522
523 switch (cmd) {
524 case SNDRV_PCM_TRIGGER_START:
525 pcm = substream;
526 break;
527 case SNDRV_PCM_TRIGGER_STOP:
528 pcm = NULL;
529 break;
530 default:
531 return -EINVAL;
532 }
533 amdtp_out_stream_pcm_trigger(&dice->stream, pcm);
534
535 return 0;
536}
537
538static snd_pcm_uframes_t dice_pointer(struct snd_pcm_substream *substream)
539{
540 struct dice *dice = substream->private_data;
541
542 return amdtp_out_stream_pcm_pointer(&dice->stream);
543}
544
545static int dice_create_pcm(struct dice *dice)
546{
547 static struct snd_pcm_ops ops = {
548 .open = dice_open,
549 .close = dice_close,
550 .ioctl = snd_pcm_lib_ioctl,
551 .hw_params = dice_hw_params,
552 .hw_free = dice_hw_free,
553 .prepare = dice_prepare,
554 .trigger = dice_trigger,
555 .pointer = dice_pointer,
556 .page = snd_pcm_lib_get_vmalloc_page,
557 .mmap = snd_pcm_lib_mmap_vmalloc,
558 };
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200559 struct snd_pcm *pcm;
560 int err;
561
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200562 err = snd_pcm_new(dice->card, "DICE", 0, 1, 0, &pcm);
563 if (err < 0)
564 return err;
565 pcm->private_data = dice;
566 strcpy(pcm->name, dice->card->shortname);
567 dice->pcm = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
568 dice->pcm->ops = &ops;
569
570 return 0;
571}
572
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200573static long dice_hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
574 long count, loff_t *offset)
575{
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200576 struct dice *dice = hwdep->private_data;
577 DEFINE_WAIT(wait);
578 union snd_firewire_event event;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200579
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200580 spin_lock_irq(&dice->lock);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200581
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200582 while (!dice->dev_lock_changed && dice->notification_bits == 0) {
583 prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
584 spin_unlock_irq(&dice->lock);
585 schedule();
586 finish_wait(&dice->hwdep_wait, &wait);
587 if (signal_pending(current))
588 return -ERESTARTSYS;
589 spin_lock_irq(&dice->lock);
590 }
591
592 memset(&event, 0, sizeof(event));
593 if (dice->dev_lock_changed) {
594 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
595 event.lock_status.status = dice->dev_lock_count > 0;
596 dice->dev_lock_changed = false;
597
598 count = min(count, (long)sizeof(event.lock_status));
599 } else {
600 event.dice_notification.type = SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
601 event.dice_notification.notification = dice->notification_bits;
602 dice->notification_bits = 0;
603
604 count = min(count, (long)sizeof(event.dice_notification));
605 }
606
607 spin_unlock_irq(&dice->lock);
608
609 if (copy_to_user(buf, &event, count))
610 return -EFAULT;
611
612 return count;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200613}
614
615static unsigned int dice_hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
616 poll_table *wait)
617{
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200618 struct dice *dice = hwdep->private_data;
619 unsigned int events;
620
621 poll_wait(file, &dice->hwdep_wait, wait);
622
623 spin_lock_irq(&dice->lock);
624 if (dice->dev_lock_changed || dice->notification_bits != 0)
625 events = POLLIN | POLLRDNORM;
626 else
627 events = 0;
628 spin_unlock_irq(&dice->lock);
629
630 return events;
631}
632
633static int dice_hwdep_get_info(struct dice *dice, void __user *arg)
634{
635 struct fw_device *dev = fw_parent_device(dice->unit);
636 struct snd_firewire_get_info info;
637
638 memset(&info, 0, sizeof(info));
639 info.type = SNDRV_FIREWIRE_TYPE_DICE;
640 info.card = dev->card->index;
641 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
642 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
643 strlcpy(info.device_name, dev_name(&dev->device),
644 sizeof(info.device_name));
645
646 if (copy_to_user(arg, &info, sizeof(info)))
647 return -EFAULT;
648
649 return 0;
650}
651
652static int dice_hwdep_lock(struct dice *dice)
653{
654 int err;
655
656 spin_lock_irq(&dice->lock);
657
658 if (dice->dev_lock_count == 0) {
659 dice->dev_lock_count = -1;
660 err = 0;
661 } else {
662 err = -EBUSY;
663 }
664
665 spin_unlock_irq(&dice->lock);
666
667 return err;
668}
669
670static int dice_hwdep_unlock(struct dice *dice)
671{
672 int err;
673
674 spin_lock_irq(&dice->lock);
675
676 if (dice->dev_lock_count == -1) {
677 dice->dev_lock_count = 0;
678 err = 0;
679 } else {
680 err = -EBADFD;
681 }
682
683 spin_unlock_irq(&dice->lock);
684
685 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200686}
687
Clemens Ladisch9dd81e32011-09-04 22:14:54 +0200688static int dice_hwdep_release(struct snd_hwdep *hwdep, struct file *file)
689{
690 struct dice *dice = hwdep->private_data;
691
692 spin_lock_irq(&dice->lock);
693 if (dice->dev_lock_count == -1)
694 dice->dev_lock_count = 0;
695 spin_unlock_irq(&dice->lock);
696
697 return 0;
698}
699
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200700static int dice_hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
701 unsigned int cmd, unsigned long arg)
702{
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200703 struct dice *dice = hwdep->private_data;
704
705 switch (cmd) {
706 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
707 return dice_hwdep_get_info(dice, (void __user *)arg);
708 case SNDRV_FIREWIRE_IOCTL_LOCK:
709 return dice_hwdep_lock(dice);
710 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
711 return dice_hwdep_unlock(dice);
712 default:
713 return -ENOIOCTLCMD;
714 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200715}
716
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200717#ifdef CONFIG_COMPAT
718static int dice_hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
719 unsigned int cmd, unsigned long arg)
720{
721 return dice_hwdep_ioctl(hwdep, file, cmd,
722 (unsigned long)compat_ptr(arg));
723}
724#else
725#define dice_hwdep_compat_ioctl NULL
726#endif
727
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200728static int dice_create_hwdep(struct dice *dice)
729{
730 static const struct snd_hwdep_ops ops = {
731 .read = dice_hwdep_read,
Clemens Ladisch9dd81e32011-09-04 22:14:54 +0200732 .release = dice_hwdep_release,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200733 .poll = dice_hwdep_poll,
734 .ioctl = dice_hwdep_ioctl,
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200735 .ioctl_compat = dice_hwdep_compat_ioctl,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200736 };
737 struct snd_hwdep *hwdep;
738 int err;
739
740 err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
741 if (err < 0)
742 return err;
743 strcpy(hwdep->name, "DICE");
744 hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
745 hwdep->ops = ops;
746 hwdep->private_data = dice;
747 hwdep->exclusive = true;
748
749 return 0;
750}
751
752static void dice_card_free(struct snd_card *card)
753{
754 struct dice *dice = card->private_data;
755
756 amdtp_out_stream_destroy(&dice->stream);
757 fw_core_remove_address_handler(&dice->notification_handler);
758 mutex_destroy(&dice->mutex);
759}
760
Clemens Ladischcbab3282011-09-04 22:16:02 +0200761#define DICE_CATEGORY_ID 0x04
762
763static int dice_interface_check(struct fw_unit *unit)
764{
765 static const int min_values[10] = {
766 10, 0x64 / 4,
767 10, 0x18 / 4,
768 10, 0x18 / 4,
769 0, 0,
770 0, 0,
771 };
772 struct fw_device *device = fw_parent_device(unit);
773 struct fw_csr_iterator it;
774 int key, value, vendor = -1, model = -1, err;
775 unsigned int i;
776 __be32 pointers[ARRAY_SIZE(min_values)];
777 __be32 version;
778
779 /*
780 * Check that GUID and unit directory are constructed according to DICE
781 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
782 * GUID chip ID consists of the 8-bit DICE category ID, the 10-bit
783 * product ID, and a 22-bit serial number.
784 */
785 fw_csr_iterator_init(&it, unit->directory);
786 while (fw_csr_iterator_next(&it, &key, &value)) {
787 switch (key) {
788 case CSR_SPECIFIER_ID:
789 vendor = value;
790 break;
791 case CSR_MODEL:
792 model = value;
793 break;
794 }
795 }
796 if (device->config_rom[3] != ((vendor << 8) | DICE_CATEGORY_ID) ||
797 device->config_rom[4] >> 22 != model)
798 return -ENODEV;
799
800 /*
801 * Check that the sub address spaces exist and are located inside the
802 * private address space. The minimum values are chosen so that all
803 * minimally required registers are included.
804 */
805 err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
806 DICE_PRIVATE_SPACE,
Clemens Ladisch1b704852011-09-04 22:17:38 +0200807 pointers, sizeof(pointers), 0);
Clemens Ladischcbab3282011-09-04 22:16:02 +0200808 if (err < 0)
809 return -ENODEV;
810 for (i = 0; i < ARRAY_SIZE(pointers); ++i) {
811 value = be32_to_cpu(pointers[i]);
812 if (value < min_values[i] || value >= 0x40000)
813 return -ENODEV;
814 }
815
816 /*
817 * Check that the implemented DICE driver specification major version
818 * number matches.
819 */
820 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
821 DICE_PRIVATE_SPACE +
822 be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
Clemens Ladisch1b704852011-09-04 22:17:38 +0200823 &version, 4, 0);
Clemens Ladischcbab3282011-09-04 22:16:02 +0200824 if (err < 0)
825 return -ENODEV;
826 if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
827 dev_err(&unit->device,
828 "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
829 return -ENODEV;
830 }
831
832 return 0;
833}
834
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200835static int dice_init_offsets(struct dice *dice)
836{
837 __be32 pointers[6];
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200838 int err;
839
840 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
Clemens Ladischcbab3282011-09-04 22:16:02 +0200841 DICE_PRIVATE_SPACE,
Clemens Ladisch1b704852011-09-04 22:17:38 +0200842 pointers, sizeof(pointers), 0);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200843 if (err < 0)
844 return err;
845
846 dice->global_offset = be32_to_cpu(pointers[0]) * 4;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200847 dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200848
849 return 0;
850}
851
852static void dice_card_strings(struct dice *dice)
853{
854 struct snd_card *card = dice->card;
855 struct fw_device *dev = fw_parent_device(dice->unit);
856 char vendor[32], model[32];
857 unsigned int i;
858 int err;
859
860 strcpy(card->driver, "DICE");
861
862 strcpy(card->shortname, "DICE");
863 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
864 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
865 global_address(dice, GLOBAL_NICK_NAME),
Clemens Ladisch1b704852011-09-04 22:17:38 +0200866 card->shortname, sizeof(card->shortname), 0);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200867 if (err >= 0) {
868 /* DICE strings are returned in "always-wrong" endianness */
869 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
870 for (i = 0; i < sizeof(card->shortname); i += 4)
871 swab32s((u32 *)&card->shortname[i]);
872 card->shortname[sizeof(card->shortname) - 1] = '\0';
873 }
874
875 strcpy(vendor, "?");
876 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
877 strcpy(model, "?");
878 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
879 snprintf(card->longname, sizeof(card->longname),
Clemens Ladischcbab3282011-09-04 22:16:02 +0200880 "%s %s (serial %u) at %s, S%d",
881 vendor, model, dev->config_rom[4] & 0x3fffff,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200882 dev_name(&dice->unit->device), 100 << dev->max_speed);
883
884 strcpy(card->mixername, "DICE");
885}
886
887static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
888{
889 struct snd_card *card;
890 struct dice *dice;
Clemens Ladisch341682c2011-09-04 22:12:06 +0200891 __be32 clock_sel;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200892 int err;
893
Clemens Ladischcbab3282011-09-04 22:16:02 +0200894 err = dice_interface_check(unit);
895 if (err < 0)
896 return err;
897
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200898 err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*dice), &card);
899 if (err < 0)
900 return err;
901 snd_card_set_dev(card, &unit->device);
902
903 dice = card->private_data;
904 dice->card = card;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200905 spin_lock_init(&dice->lock);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200906 mutex_init(&dice->mutex);
907 dice->unit = unit;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200908 init_waitqueue_head(&dice->hwdep_wait);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200909
910 err = dice_init_offsets(dice);
911 if (err < 0)
912 goto err_mutex;
913
914 dice->notification_handler.length = 4;
915 dice->notification_handler.address_callback = dice_notification;
916 dice->notification_handler.callback_data = dice;
917 err = fw_core_add_address_handler(&dice->notification_handler,
918 &fw_high_memory_region);
919 if (err < 0)
920 goto err_mutex;
921
922 err = fw_iso_resources_init(&dice->resources, unit);
923 if (err < 0)
924 goto err_notification_handler;
925 dice->resources.channels_mask = 0x00000000ffffffffuLL;
926
Clemens Ladischa7304e32011-09-04 22:16:10 +0200927 err = amdtp_out_stream_init(&dice->stream, unit,
928 CIP_BLOCKING | CIP_HI_DUALWIRE);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200929 if (err < 0)
930 goto err_resources;
931
932 err = dice_owner_set(dice);
933 if (err < 0)
934 goto err_stream;
935
936 card->private_free = dice_card_free;
937
938 dice_card_strings(dice);
939
Clemens Ladisch341682c2011-09-04 22:12:06 +0200940 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
941 global_address(dice, GLOBAL_CLOCK_SELECT),
Clemens Ladisch1b704852011-09-04 22:17:38 +0200942 &clock_sel, 4, 0);
Clemens Ladisch341682c2011-09-04 22:12:06 +0200943 if (err < 0)
944 goto error;
945 clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
946 clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
947 err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
948 global_address(dice, GLOBAL_CLOCK_SELECT),
Clemens Ladisch1b704852011-09-04 22:17:38 +0200949 &clock_sel, 4, 0);
Clemens Ladisch341682c2011-09-04 22:12:06 +0200950 if (err < 0)
951 goto error;
952
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200953 err = dice_create_pcm(dice);
954 if (err < 0)
955 goto error;
956
957 err = dice_create_hwdep(dice);
958 if (err < 0)
959 goto error;
960
961 err = snd_card_register(card);
962 if (err < 0)
963 goto error;
964
965 dev_set_drvdata(&unit->device, dice);
966
967 return 0;
968
969err_stream:
970 amdtp_out_stream_destroy(&dice->stream);
971err_resources:
972 fw_iso_resources_destroy(&dice->resources);
973err_notification_handler:
974 fw_core_remove_address_handler(&dice->notification_handler);
975err_mutex:
976 mutex_destroy(&dice->mutex);
977error:
978 snd_card_free(card);
979 return err;
980}
981
982static void dice_remove(struct fw_unit *unit)
983{
984 struct dice *dice = dev_get_drvdata(&unit->device);
985
Clemens Ladisch4ed31f202011-09-04 22:13:09 +0200986 mutex_lock(&dice->mutex);
987
988 amdtp_out_stream_pcm_abort(&dice->stream);
989
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200990 snd_card_disconnect(dice->card);
991
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200992 dice_stream_stop(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200993 dice_owner_clear(dice);
Clemens Ladisch4ed31f202011-09-04 22:13:09 +0200994
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200995 mutex_unlock(&dice->mutex);
996
997 snd_card_free_when_closed(dice->card);
998}
999
1000static void dice_bus_reset(struct fw_unit *unit)
1001{
1002 struct dice *dice = dev_get_drvdata(&unit->device);
1003
1004 mutex_lock(&dice->mutex);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +02001005
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001006 /*
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001007 * On a bus reset, the DICE firmware disables streaming and then goes
1008 * off contemplating its own navel for hundreds of milliseconds before
1009 * it can react to any of our attempts to reenable streaming. This
1010 * means that we lose synchronization anyway, so we force our streams
1011 * to stop so that the application can restart them in an orderly
1012 * manner.
1013 */
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001014 amdtp_out_stream_pcm_abort(&dice->stream);
Clemens Ladischeadce072011-09-04 22:17:45 +02001015
1016 dice->global_enabled = false;
Clemens Ladisch6abce9e2011-09-04 22:11:14 +02001017 dice_stream_stop_packets(dice);
1018
1019 dice_owner_update(dice);
1020
1021 fw_iso_resources_update(&dice->resources);
1022
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001023 mutex_unlock(&dice->mutex);
1024}
1025
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001026#define DICE_INTERFACE 0x000001
1027
1028static const struct ieee1394_device_id dice_id_table[] = {
1029 {
Clemens Ladischcbab3282011-09-04 22:16:02 +02001030 .match_flags = IEEE1394_MATCH_VERSION,
1031 .version = DICE_INTERFACE,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001032 },
1033 { }
1034};
1035MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
1036
1037static struct fw_driver dice_driver = {
1038 .driver = {
1039 .owner = THIS_MODULE,
1040 .name = KBUILD_MODNAME,
1041 .bus = &fw_bus_type,
1042 },
1043 .probe = dice_probe,
1044 .update = dice_bus_reset,
1045 .remove = dice_remove,
1046 .id_table = dice_id_table,
1047};
1048
1049static int __init alsa_dice_init(void)
1050{
1051 return driver_register(&dice_driver.driver);
1052}
1053
1054static void __exit alsa_dice_exit(void)
1055{
1056 driver_unregister(&dice_driver.driver);
1057}
1058
1059module_init(alsa_dice_init);
1060module_exit(alsa_dice_exit);