blob: b4827ff45d68a761f77c292313d75f3c478a0b12 [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;
121 int rcode, err, errors = 0;
122
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 */
135 rcode = fw_run_transaction(device->card,
136 TCODE_LOCK_COMPARE_SWAP,
137 device->node_id,
138 dice->owner_generation,
139 device->max_speed,
140 global_address(dice, GLOBAL_OWNER),
141 buffer, 2 * 8);
142
143 if (rcode == RCODE_COMPLETE) {
144 if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER)) {
145 err = 0;
146 } else {
147 dev_err(&dice->unit->device,
148 "device is already in use\n");
149 err = -EBUSY;
150 }
151 break;
152 }
153 if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
154 dev_err(&dice->unit->device,
155 "setting device owner failed: %s\n",
156 fw_rcode_string(rcode));
157 err = -EIO;
158 break;
159 }
160 msleep(20);
161 }
162
163 kfree(buffer);
164
165 return err;
166}
167
168static int dice_owner_update(struct dice *dice)
169{
170 struct fw_device *device = fw_parent_device(dice->unit);
171 __be64 *buffer;
172 int rcode, err, errors = 0;
173
174 if (dice->owner_generation == -1)
175 return 0;
176
177 buffer = kmalloc(2 * 8, GFP_KERNEL);
178 if (!buffer)
179 return -ENOMEM;
180
181 for (;;) {
182 buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
183 buffer[1] = cpu_to_be64(
184 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
185 dice->notification_handler.offset);
186
187 dice->owner_generation = device->generation;
188 smp_rmb(); /* node_id vs. generation */
189 rcode = fw_run_transaction(device->card,
190 TCODE_LOCK_COMPARE_SWAP,
191 device->node_id,
192 dice->owner_generation,
193 device->max_speed,
194 global_address(dice, GLOBAL_OWNER),
195 buffer, 2 * 8);
196
197 if (rcode == RCODE_COMPLETE) {
198 if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER)) {
199 err = 0;
200 } else {
201 dev_err(&dice->unit->device,
202 "device is already in use\n");
203 err = -EBUSY;
204 }
205 break;
206 }
207 if (rcode == RCODE_GENERATION) {
208 err = 0; /* try again later */
209 break;
210 }
211 if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
212 dev_err(&dice->unit->device,
213 "setting device owner failed: %s\n",
214 fw_rcode_string(rcode));
215 err = -EIO;
216 break;
217 }
218 msleep(20);
219 }
220
221 kfree(buffer);
222
223 if (err < 0)
224 dice->owner_generation = -1;
225
226 return err;
227}
228
229static void dice_owner_clear(struct dice *dice)
230{
231 struct fw_device *device = fw_parent_device(dice->unit);
232 __be64 *buffer;
233 int rcode, errors = 0;
234
235 buffer = kmalloc(2 * 8, GFP_KERNEL);
236 if (!buffer)
237 return;
238
239 for (;;) {
240 buffer[0] = cpu_to_be64(
241 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
242 dice->notification_handler.offset);
243 buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
244
245 rcode = fw_run_transaction(device->card,
246 TCODE_LOCK_COMPARE_SWAP,
247 device->node_id,
248 dice->owner_generation,
249 device->max_speed,
250 global_address(dice, GLOBAL_OWNER),
251 buffer, 2 * 8);
252
253 if (rcode == RCODE_COMPLETE)
254 break;
255 if (rcode == RCODE_GENERATION)
256 break;
257 if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
258 dev_err(&dice->unit->device,
259 "clearing device owner failed: %s\n",
260 fw_rcode_string(rcode));
261 break;
262 }
263 msleep(20);
264 }
265
266 kfree(buffer);
267
268 dice->owner_generation = -1;
269}
270
271static int dice_enable_set(struct dice *dice)
272{
273 struct fw_device *device = fw_parent_device(dice->unit);
274 __be32 value;
275 int rcode, err, errors = 0;
276
Clemens Ladisch54e72f02011-09-04 22:15:54 +0200277 value = cpu_to_be32(1);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200278 for (;;) {
279 rcode = fw_run_transaction(device->card,
280 TCODE_WRITE_QUADLET_REQUEST,
281 device->node_id,
282 dice->owner_generation,
283 device->max_speed,
284 global_address(dice, GLOBAL_ENABLE),
285 &value, 4);
286 if (rcode == RCODE_COMPLETE) {
287 dice->global_enabled = true;
288 err = 0;
289 break;
290 }
291 if (rcode == RCODE_GENERATION) {
292 err = -EAGAIN;
293 break;
294 }
295 if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
296 dev_err(&dice->unit->device,
297 "device enabling failed: %s\n",
298 fw_rcode_string(rcode));
299 err = -EIO;
300 break;
301 }
302 msleep(20);
303 }
304
305 return err;
306}
307
308static void dice_enable_clear(struct dice *dice)
309{
310 struct fw_device *device = fw_parent_device(dice->unit);
311 __be32 value;
312 int rcode, errors = 0;
313
314 value = 0;
315 for (;;) {
316 rcode = fw_run_transaction(device->card,
317 TCODE_WRITE_QUADLET_REQUEST,
318 device->node_id,
319 dice->owner_generation,
320 device->max_speed,
321 global_address(dice, GLOBAL_ENABLE),
322 &value, 4);
323 if (rcode == RCODE_COMPLETE ||
324 rcode == RCODE_GENERATION)
325 break;
326 if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
327 dev_err(&dice->unit->device,
328 "device disabling failed: %s\n",
329 fw_rcode_string(rcode));
330 break;
331 }
332 msleep(20);
333 }
334 dice->global_enabled = false;
335}
336
337static void dice_notification(struct fw_card *card, struct fw_request *request,
338 int tcode, int destination, int source,
339 int generation, unsigned long long offset,
340 void *data, size_t length, void *callback_data)
341{
342 struct dice *dice = callback_data;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200343 unsigned long flags;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200344
345 if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
346 fw_send_response(card, request, RCODE_TYPE_ERROR);
347 return;
348 }
349 if ((offset & 3) != 0) {
350 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
351 return;
352 }
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200353 spin_lock_irqsave(&dice->lock, flags);
354 dice->notification_bits |= be32_to_cpup(data);
355 spin_unlock_irqrestore(&dice->lock, flags);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200356 fw_send_response(card, request, RCODE_COMPLETE);
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200357 wake_up(&dice->hwdep_wait);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200358}
359
360static int dice_open(struct snd_pcm_substream *substream)
361{
362 static const struct snd_pcm_hardware hardware = {
363 .info = SNDRV_PCM_INFO_MMAP |
364 SNDRV_PCM_INFO_MMAP_VALID |
365 SNDRV_PCM_INFO_BATCH |
366 SNDRV_PCM_INFO_INTERLEAVED |
367 SNDRV_PCM_INFO_BLOCK_TRANSFER,
368 .formats = AMDTP_OUT_PCM_FORMAT_BITS,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200369 .buffer_bytes_max = 16 * 1024 * 1024,
370 .period_bytes_min = 1,
371 .period_bytes_max = UINT_MAX,
372 .periods_min = 1,
373 .periods_max = UINT_MAX,
374 };
375 struct dice *dice = substream->private_data;
376 struct snd_pcm_runtime *runtime = substream->runtime;
Clemens Ladisch341682c2011-09-04 22:12:06 +0200377 __be32 clock_sel, number_audio, number_midi;
378 unsigned int rate;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200379 int err;
380
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200381 err = dice_try_lock(dice);
382 if (err < 0)
383 goto error;
384
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200385 err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
Clemens Ladisch341682c2011-09-04 22:12:06 +0200386 global_address(dice, GLOBAL_CLOCK_SELECT),
387 &clock_sel, 4);
388 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200389 goto err_lock;
Clemens Ladisch341682c2011-09-04 22:12:06 +0200390 rate = (be32_to_cpu(clock_sel) & CLOCK_RATE_MASK) >> CLOCK_RATE_SHIFT;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200391 if (rate >= ARRAY_SIZE(dice_rates)) {
392 err = -ENXIO;
393 goto err_lock;
394 }
Clemens Ladisch341682c2011-09-04 22:12:06 +0200395 rate = dice_rates[rate];
396
397 err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200398 rx_address(dice, RX_NUMBER_AUDIO),
399 &number_audio, 4);
400 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200401 goto err_lock;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200402 err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
403 rx_address(dice, RX_NUMBER_MIDI),
404 &number_midi, 4);
405 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200406 goto err_lock;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200407
408 runtime->hw = hardware;
Clemens Ladisch341682c2011-09-04 22:12:06 +0200409
410 runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
411 snd_pcm_limit_hw_rates(runtime);
412
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200413 runtime->hw.channels_min = be32_to_cpu(number_audio);
414 runtime->hw.channels_max = be32_to_cpu(number_audio);
415
Clemens Ladisch341682c2011-09-04 22:12:06 +0200416 amdtp_out_stream_set_rate(&dice->stream, rate);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200417 amdtp_out_stream_set_pcm(&dice->stream, be32_to_cpu(number_audio));
418 amdtp_out_stream_set_midi(&dice->stream, be32_to_cpu(number_midi));
419
420 err = snd_pcm_hw_constraint_minmax(runtime,
421 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
422 5000, 8192000);
423 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200424 goto err_lock;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200425
426 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
427 if (err < 0)
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200428 goto err_lock;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200429
430 return 0;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200431
432err_lock:
433 dice_unlock(dice);
434error:
435 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200436}
437
438static int dice_close(struct snd_pcm_substream *substream)
439{
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200440 struct dice *dice = substream->private_data;
441
442 dice_unlock(dice);
443
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200444 return 0;
445}
446
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200447static int dice_stream_start_packets(struct dice *dice)
448{
449 int err;
450
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200451 if (amdtp_out_stream_running(&dice->stream))
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200452 return 0;
453
454 err = amdtp_out_stream_start(&dice->stream, dice->resources.channel,
455 fw_parent_device(dice->unit)->max_speed);
456 if (err < 0)
457 return err;
458
459 err = dice_enable_set(dice);
460 if (err < 0) {
461 amdtp_out_stream_stop(&dice->stream);
462 return err;
463 }
464
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200465 return 0;
466}
467
468static int dice_stream_start(struct dice *dice)
469{
470 __be32 channel;
471 int err;
472
473 if (!dice->resources.allocated) {
474 err = fw_iso_resources_allocate(&dice->resources,
475 amdtp_out_stream_get_max_payload(&dice->stream),
476 fw_parent_device(dice->unit)->max_speed);
477 if (err < 0)
478 goto error;
479
480 channel = cpu_to_be32(dice->resources.channel);
481 err = snd_fw_transaction(dice->unit,
482 TCODE_WRITE_QUADLET_REQUEST,
483 rx_address(dice, RX_ISOCHRONOUS),
484 &channel, 4);
485 if (err < 0)
486 goto err_resources;
487 }
488
489 err = dice_stream_start_packets(dice);
490 if (err < 0)
491 goto err_rx_channel;
492
493 return 0;
494
495err_rx_channel:
496 channel = cpu_to_be32((u32)-1);
497 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
498 rx_address(dice, RX_ISOCHRONOUS), &channel, 4);
499err_resources:
500 fw_iso_resources_free(&dice->resources);
501error:
502 return err;
503}
504
505static void dice_stream_stop_packets(struct dice *dice)
506{
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200507 if (amdtp_out_stream_running(&dice->stream)) {
508 dice_enable_clear(dice);
509 amdtp_out_stream_stop(&dice->stream);
510 }
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200511}
512
513static void dice_stream_stop(struct dice *dice)
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200514{
515 __be32 channel;
516
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200517 dice_stream_stop_packets(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200518
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200519 if (!dice->resources.allocated)
520 return;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200521
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200522 channel = cpu_to_be32((u32)-1);
523 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
524 rx_address(dice, RX_ISOCHRONOUS), &channel, 4);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200525
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200526 fw_iso_resources_free(&dice->resources);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200527}
528
529static int dice_hw_params(struct snd_pcm_substream *substream,
530 struct snd_pcm_hw_params *hw_params)
531{
532 struct dice *dice = substream->private_data;
533 int err;
534
535 mutex_lock(&dice->mutex);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200536 dice_stream_stop(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200537 mutex_unlock(&dice->mutex);
538
539 err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
540 params_buffer_bytes(hw_params));
541 if (err < 0)
542 goto error;
543
544 amdtp_out_stream_set_pcm_format(&dice->stream,
545 params_format(hw_params));
546
547 return 0;
548
549error:
550 return err;
551}
552
553static int dice_hw_free(struct snd_pcm_substream *substream)
554{
555 struct dice *dice = substream->private_data;
556
557 mutex_lock(&dice->mutex);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200558 dice_stream_stop(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200559 mutex_unlock(&dice->mutex);
560
561 return snd_pcm_lib_free_vmalloc_buffer(substream);
562}
563
564static int dice_prepare(struct snd_pcm_substream *substream)
565{
566 struct dice *dice = substream->private_data;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200567 int err;
568
569 mutex_lock(&dice->mutex);
570
571 if (amdtp_out_streaming_error(&dice->stream))
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200572 dice_stream_stop_packets(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200573
Clemens Ladisch6abce9e2011-09-04 22:11:14 +0200574 err = dice_stream_start(dice);
575 if (err < 0) {
576 mutex_unlock(&dice->mutex);
577 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200578 }
579
580 mutex_unlock(&dice->mutex);
581
582 amdtp_out_stream_pcm_prepare(&dice->stream);
583
584 return 0;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200585}
586
587static int dice_trigger(struct snd_pcm_substream *substream, int cmd)
588{
589 struct dice *dice = substream->private_data;
590 struct snd_pcm_substream *pcm;
591
592 switch (cmd) {
593 case SNDRV_PCM_TRIGGER_START:
594 pcm = substream;
595 break;
596 case SNDRV_PCM_TRIGGER_STOP:
597 pcm = NULL;
598 break;
599 default:
600 return -EINVAL;
601 }
602 amdtp_out_stream_pcm_trigger(&dice->stream, pcm);
603
604 return 0;
605}
606
607static snd_pcm_uframes_t dice_pointer(struct snd_pcm_substream *substream)
608{
609 struct dice *dice = substream->private_data;
610
611 return amdtp_out_stream_pcm_pointer(&dice->stream);
612}
613
614static int dice_create_pcm(struct dice *dice)
615{
616 static struct snd_pcm_ops ops = {
617 .open = dice_open,
618 .close = dice_close,
619 .ioctl = snd_pcm_lib_ioctl,
620 .hw_params = dice_hw_params,
621 .hw_free = dice_hw_free,
622 .prepare = dice_prepare,
623 .trigger = dice_trigger,
624 .pointer = dice_pointer,
625 .page = snd_pcm_lib_get_vmalloc_page,
626 .mmap = snd_pcm_lib_mmap_vmalloc,
627 };
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200628 struct snd_pcm *pcm;
629 int err;
630
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200631 err = snd_pcm_new(dice->card, "DICE", 0, 1, 0, &pcm);
632 if (err < 0)
633 return err;
634 pcm->private_data = dice;
635 strcpy(pcm->name, dice->card->shortname);
636 dice->pcm = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
637 dice->pcm->ops = &ops;
638
639 return 0;
640}
641
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200642static long dice_hwdep_read(struct snd_hwdep *hwdep, char __user *buf,
643 long count, loff_t *offset)
644{
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200645 struct dice *dice = hwdep->private_data;
646 DEFINE_WAIT(wait);
647 union snd_firewire_event event;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200648
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200649 spin_lock_irq(&dice->lock);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200650
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200651 while (!dice->dev_lock_changed && dice->notification_bits == 0) {
652 prepare_to_wait(&dice->hwdep_wait, &wait, TASK_INTERRUPTIBLE);
653 spin_unlock_irq(&dice->lock);
654 schedule();
655 finish_wait(&dice->hwdep_wait, &wait);
656 if (signal_pending(current))
657 return -ERESTARTSYS;
658 spin_lock_irq(&dice->lock);
659 }
660
661 memset(&event, 0, sizeof(event));
662 if (dice->dev_lock_changed) {
663 event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS;
664 event.lock_status.status = dice->dev_lock_count > 0;
665 dice->dev_lock_changed = false;
666
667 count = min(count, (long)sizeof(event.lock_status));
668 } else {
669 event.dice_notification.type = SNDRV_FIREWIRE_EVENT_DICE_NOTIFICATION;
670 event.dice_notification.notification = dice->notification_bits;
671 dice->notification_bits = 0;
672
673 count = min(count, (long)sizeof(event.dice_notification));
674 }
675
676 spin_unlock_irq(&dice->lock);
677
678 if (copy_to_user(buf, &event, count))
679 return -EFAULT;
680
681 return count;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200682}
683
684static unsigned int dice_hwdep_poll(struct snd_hwdep *hwdep, struct file *file,
685 poll_table *wait)
686{
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200687 struct dice *dice = hwdep->private_data;
688 unsigned int events;
689
690 poll_wait(file, &dice->hwdep_wait, wait);
691
692 spin_lock_irq(&dice->lock);
693 if (dice->dev_lock_changed || dice->notification_bits != 0)
694 events = POLLIN | POLLRDNORM;
695 else
696 events = 0;
697 spin_unlock_irq(&dice->lock);
698
699 return events;
700}
701
702static int dice_hwdep_get_info(struct dice *dice, void __user *arg)
703{
704 struct fw_device *dev = fw_parent_device(dice->unit);
705 struct snd_firewire_get_info info;
706
707 memset(&info, 0, sizeof(info));
708 info.type = SNDRV_FIREWIRE_TYPE_DICE;
709 info.card = dev->card->index;
710 *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]);
711 *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]);
712 strlcpy(info.device_name, dev_name(&dev->device),
713 sizeof(info.device_name));
714
715 if (copy_to_user(arg, &info, sizeof(info)))
716 return -EFAULT;
717
718 return 0;
719}
720
721static int dice_hwdep_lock(struct dice *dice)
722{
723 int err;
724
725 spin_lock_irq(&dice->lock);
726
727 if (dice->dev_lock_count == 0) {
728 dice->dev_lock_count = -1;
729 err = 0;
730 } else {
731 err = -EBUSY;
732 }
733
734 spin_unlock_irq(&dice->lock);
735
736 return err;
737}
738
739static int dice_hwdep_unlock(struct dice *dice)
740{
741 int err;
742
743 spin_lock_irq(&dice->lock);
744
745 if (dice->dev_lock_count == -1) {
746 dice->dev_lock_count = 0;
747 err = 0;
748 } else {
749 err = -EBADFD;
750 }
751
752 spin_unlock_irq(&dice->lock);
753
754 return err;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200755}
756
Clemens Ladisch9dd81e32011-09-04 22:14:54 +0200757static int dice_hwdep_release(struct snd_hwdep *hwdep, struct file *file)
758{
759 struct dice *dice = hwdep->private_data;
760
761 spin_lock_irq(&dice->lock);
762 if (dice->dev_lock_count == -1)
763 dice->dev_lock_count = 0;
764 spin_unlock_irq(&dice->lock);
765
766 return 0;
767}
768
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200769static int dice_hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file,
770 unsigned int cmd, unsigned long arg)
771{
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200772 struct dice *dice = hwdep->private_data;
773
774 switch (cmd) {
775 case SNDRV_FIREWIRE_IOCTL_GET_INFO:
776 return dice_hwdep_get_info(dice, (void __user *)arg);
777 case SNDRV_FIREWIRE_IOCTL_LOCK:
778 return dice_hwdep_lock(dice);
779 case SNDRV_FIREWIRE_IOCTL_UNLOCK:
780 return dice_hwdep_unlock(dice);
781 default:
782 return -ENOIOCTLCMD;
783 }
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200784}
785
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200786#ifdef CONFIG_COMPAT
787static int dice_hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file,
788 unsigned int cmd, unsigned long arg)
789{
790 return dice_hwdep_ioctl(hwdep, file, cmd,
791 (unsigned long)compat_ptr(arg));
792}
793#else
794#define dice_hwdep_compat_ioctl NULL
795#endif
796
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200797static int dice_create_hwdep(struct dice *dice)
798{
799 static const struct snd_hwdep_ops ops = {
800 .read = dice_hwdep_read,
Clemens Ladisch9dd81e32011-09-04 22:14:54 +0200801 .release = dice_hwdep_release,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200802 .poll = dice_hwdep_poll,
803 .ioctl = dice_hwdep_ioctl,
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200804 .ioctl_compat = dice_hwdep_compat_ioctl,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200805 };
806 struct snd_hwdep *hwdep;
807 int err;
808
809 err = snd_hwdep_new(dice->card, "DICE", 0, &hwdep);
810 if (err < 0)
811 return err;
812 strcpy(hwdep->name, "DICE");
813 hwdep->iface = SNDRV_HWDEP_IFACE_FW_DICE;
814 hwdep->ops = ops;
815 hwdep->private_data = dice;
816 hwdep->exclusive = true;
817
818 return 0;
819}
820
821static void dice_card_free(struct snd_card *card)
822{
823 struct dice *dice = card->private_data;
824
825 amdtp_out_stream_destroy(&dice->stream);
826 fw_core_remove_address_handler(&dice->notification_handler);
827 mutex_destroy(&dice->mutex);
828}
829
Clemens Ladischcbab3282011-09-04 22:16:02 +0200830#define DICE_CATEGORY_ID 0x04
831
832static int dice_interface_check(struct fw_unit *unit)
833{
834 static const int min_values[10] = {
835 10, 0x64 / 4,
836 10, 0x18 / 4,
837 10, 0x18 / 4,
838 0, 0,
839 0, 0,
840 };
841 struct fw_device *device = fw_parent_device(unit);
842 struct fw_csr_iterator it;
843 int key, value, vendor = -1, model = -1, err;
844 unsigned int i;
845 __be32 pointers[ARRAY_SIZE(min_values)];
846 __be32 version;
847
848 /*
849 * Check that GUID and unit directory are constructed according to DICE
850 * rules, i.e., that the specifier ID is the GUID's OUI, and that the
851 * GUID chip ID consists of the 8-bit DICE category ID, the 10-bit
852 * product ID, and a 22-bit serial number.
853 */
854 fw_csr_iterator_init(&it, unit->directory);
855 while (fw_csr_iterator_next(&it, &key, &value)) {
856 switch (key) {
857 case CSR_SPECIFIER_ID:
858 vendor = value;
859 break;
860 case CSR_MODEL:
861 model = value;
862 break;
863 }
864 }
865 if (device->config_rom[3] != ((vendor << 8) | DICE_CATEGORY_ID) ||
866 device->config_rom[4] >> 22 != model)
867 return -ENODEV;
868
869 /*
870 * Check that the sub address spaces exist and are located inside the
871 * private address space. The minimum values are chosen so that all
872 * minimally required registers are included.
873 */
874 err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
875 DICE_PRIVATE_SPACE,
876 pointers, sizeof(pointers));
877 if (err < 0)
878 return -ENODEV;
879 for (i = 0; i < ARRAY_SIZE(pointers); ++i) {
880 value = be32_to_cpu(pointers[i]);
881 if (value < min_values[i] || value >= 0x40000)
882 return -ENODEV;
883 }
884
885 /*
886 * Check that the implemented DICE driver specification major version
887 * number matches.
888 */
889 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
890 DICE_PRIVATE_SPACE +
891 be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
892 &version, 4);
893 if (err < 0)
894 return -ENODEV;
895 if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
896 dev_err(&unit->device,
897 "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
898 return -ENODEV;
899 }
900
901 return 0;
902}
903
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200904static int dice_init_offsets(struct dice *dice)
905{
906 __be32 pointers[6];
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200907 int err;
908
909 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
Clemens Ladischcbab3282011-09-04 22:16:02 +0200910 DICE_PRIVATE_SPACE,
911 pointers, sizeof(pointers));
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200912 if (err < 0)
913 return err;
914
915 dice->global_offset = be32_to_cpu(pointers[0]) * 4;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200916 dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200917
918 return 0;
919}
920
921static void dice_card_strings(struct dice *dice)
922{
923 struct snd_card *card = dice->card;
924 struct fw_device *dev = fw_parent_device(dice->unit);
925 char vendor[32], model[32];
926 unsigned int i;
927 int err;
928
929 strcpy(card->driver, "DICE");
930
931 strcpy(card->shortname, "DICE");
932 BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
933 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
934 global_address(dice, GLOBAL_NICK_NAME),
935 card->shortname, sizeof(card->shortname));
936 if (err >= 0) {
937 /* DICE strings are returned in "always-wrong" endianness */
938 BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
939 for (i = 0; i < sizeof(card->shortname); i += 4)
940 swab32s((u32 *)&card->shortname[i]);
941 card->shortname[sizeof(card->shortname) - 1] = '\0';
942 }
943
944 strcpy(vendor, "?");
945 fw_csr_string(dev->config_rom + 5, CSR_VENDOR, vendor, sizeof(vendor));
946 strcpy(model, "?");
947 fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
948 snprintf(card->longname, sizeof(card->longname),
Clemens Ladischcbab3282011-09-04 22:16:02 +0200949 "%s %s (serial %u) at %s, S%d",
950 vendor, model, dev->config_rom[4] & 0x3fffff,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200951 dev_name(&dice->unit->device), 100 << dev->max_speed);
952
953 strcpy(card->mixername, "DICE");
954}
955
956static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
957{
958 struct snd_card *card;
959 struct dice *dice;
Clemens Ladisch341682c2011-09-04 22:12:06 +0200960 __be32 clock_sel;
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200961 int err;
962
Clemens Ladischcbab3282011-09-04 22:16:02 +0200963 err = dice_interface_check(unit);
964 if (err < 0)
965 return err;
966
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200967 err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*dice), &card);
968 if (err < 0)
969 return err;
970 snd_card_set_dev(card, &unit->device);
971
972 dice = card->private_data;
973 dice->card = card;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200974 spin_lock_init(&dice->lock);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200975 mutex_init(&dice->mutex);
976 dice->unit = unit;
Clemens Ladisch0c29c912011-09-04 22:14:15 +0200977 init_waitqueue_head(&dice->hwdep_wait);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200978
979 err = dice_init_offsets(dice);
980 if (err < 0)
981 goto err_mutex;
982
983 dice->notification_handler.length = 4;
984 dice->notification_handler.address_callback = dice_notification;
985 dice->notification_handler.callback_data = dice;
986 err = fw_core_add_address_handler(&dice->notification_handler,
987 &fw_high_memory_region);
988 if (err < 0)
989 goto err_mutex;
990
991 err = fw_iso_resources_init(&dice->resources, unit);
992 if (err < 0)
993 goto err_notification_handler;
994 dice->resources.channels_mask = 0x00000000ffffffffuLL;
995
Clemens Ladische84d15f2011-09-04 22:12:48 +0200996 err = amdtp_out_stream_init(&dice->stream, unit, CIP_BLOCKING);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +0200997 if (err < 0)
998 goto err_resources;
999
1000 err = dice_owner_set(dice);
1001 if (err < 0)
1002 goto err_stream;
1003
1004 card->private_free = dice_card_free;
1005
1006 dice_card_strings(dice);
1007
Clemens Ladisch341682c2011-09-04 22:12:06 +02001008 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
1009 global_address(dice, GLOBAL_CLOCK_SELECT),
1010 &clock_sel, 4);
1011 if (err < 0)
1012 goto error;
1013 clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
1014 clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
1015 err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
1016 global_address(dice, GLOBAL_CLOCK_SELECT),
1017 &clock_sel, 4);
1018 if (err < 0)
1019 goto error;
1020
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001021 err = dice_create_pcm(dice);
1022 if (err < 0)
1023 goto error;
1024
1025 err = dice_create_hwdep(dice);
1026 if (err < 0)
1027 goto error;
1028
1029 err = snd_card_register(card);
1030 if (err < 0)
1031 goto error;
1032
1033 dev_set_drvdata(&unit->device, dice);
1034
1035 return 0;
1036
1037err_stream:
1038 amdtp_out_stream_destroy(&dice->stream);
1039err_resources:
1040 fw_iso_resources_destroy(&dice->resources);
1041err_notification_handler:
1042 fw_core_remove_address_handler(&dice->notification_handler);
1043err_mutex:
1044 mutex_destroy(&dice->mutex);
1045error:
1046 snd_card_free(card);
1047 return err;
1048}
1049
1050static void dice_remove(struct fw_unit *unit)
1051{
1052 struct dice *dice = dev_get_drvdata(&unit->device);
1053
Clemens Ladisch4ed31f202011-09-04 22:13:09 +02001054 mutex_lock(&dice->mutex);
1055
1056 amdtp_out_stream_pcm_abort(&dice->stream);
1057
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001058 snd_card_disconnect(dice->card);
1059
Clemens Ladisch6abce9e2011-09-04 22:11:14 +02001060 dice_stream_stop(dice);
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001061 dice_owner_clear(dice);
Clemens Ladisch4ed31f202011-09-04 22:13:09 +02001062
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001063 mutex_unlock(&dice->mutex);
1064
1065 snd_card_free_when_closed(dice->card);
1066}
1067
1068static void dice_bus_reset(struct fw_unit *unit)
1069{
1070 struct dice *dice = dev_get_drvdata(&unit->device);
1071
1072 mutex_lock(&dice->mutex);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +02001073
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001074 /*
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001075 * On a bus reset, the DICE firmware disables streaming and then goes
1076 * off contemplating its own navel for hundreds of milliseconds before
1077 * it can react to any of our attempts to reenable streaming. This
1078 * means that we lose synchronization anyway, so we force our streams
1079 * to stop so that the application can restart them in an orderly
1080 * manner.
1081 */
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001082 amdtp_out_stream_pcm_abort(&dice->stream);
Clemens Ladisch6abce9e2011-09-04 22:11:14 +02001083 dice_stream_stop_packets(dice);
1084
1085 dice_owner_update(dice);
1086
1087 fw_iso_resources_update(&dice->resources);
1088
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001089 mutex_unlock(&dice->mutex);
1090}
1091
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001092#define DICE_INTERFACE 0x000001
1093
1094static const struct ieee1394_device_id dice_id_table[] = {
1095 {
Clemens Ladischcbab3282011-09-04 22:16:02 +02001096 .match_flags = IEEE1394_MATCH_VERSION,
1097 .version = DICE_INTERFACE,
Clemens Ladisch82fbb4f2011-09-04 22:04:49 +02001098 },
1099 { }
1100};
1101MODULE_DEVICE_TABLE(ieee1394, dice_id_table);
1102
1103static struct fw_driver dice_driver = {
1104 .driver = {
1105 .owner = THIS_MODULE,
1106 .name = KBUILD_MODNAME,
1107 .bus = &fw_bus_type,
1108 },
1109 .probe = dice_probe,
1110 .update = dice_bus_reset,
1111 .remove = dice_remove,
1112 .id_table = dice_id_table,
1113};
1114
1115static int __init alsa_dice_init(void)
1116{
1117 return driver_register(&dice_driver.driver);
1118}
1119
1120static void __exit alsa_dice_exit(void)
1121{
1122 driver_unregister(&dice_driver.driver);
1123}
1124
1125module_init(alsa_dice_init);
1126module_exit(alsa_dice_exit);