ALSA: fireworks: add MIDI output
[alsa-kprivate.git] / sound / firewire / fireworks.c
1 /*
2  * fireworks.c - driver for Firewire devices from Echo Digital Audio
3  *
4  * Copyright (c) 2009-2010 Clemens Ladisch
5  *
6  *
7  * This driver is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 2.
9  *
10  * This driver is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this driver; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/firewire.h>
20 #include <linux/firewire-constants.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/initval.h>
25 #include <sound/control.h>
26 #include <sound/pcm.h>
27 #include <sound/rawmidi.h>
28 #include "fireworks.h"
29
30 MODULE_DESCRIPTION("Echo Fireworks driver");
31 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
32 MODULE_LICENSE("GPL v2");
33
34 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
35 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
36 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
37 static unsigned int dump_tx_packets;
38 static unsigned int dump_rx_packets;
39 static bool dump_fcp_packets;
40
41 module_param_array(index, int, NULL, 0444);
42 MODULE_PARM_DESC(index, "card index");
43 module_param_array(id, charp, NULL, 0444);
44 MODULE_PARM_DESC(id, "ID string");
45 module_param_array(enable, bool, NULL, 0444);
46 MODULE_PARM_DESC(enable, "enable card");
47 module_param(dump_tx_packets, uint, 0644);
48 MODULE_PARM_DESC(dump_tx_packets, "log this many sent iso packets");
49 module_param(dump_rx_packets, uint, 0644);
50 MODULE_PARM_DESC(dump_rx_packets, "log this many received iso packets");
51 module_param(dump_fcp_packets, bool, 0644);
52 MODULE_PARM_DESC(dump_fcp_packets, "log all FCP commands and responses");
53
54 #define PLAYBACK_QUEUE_LENGTH   40
55 #define CAPTURE_QUEUE_LENGTH    40
56
57 #define PLAYBACK_CHANNEL 17 /* XXX */
58 #define CAPTURE_CHANNEL 23 /* XXX */
59
60 #define MIDI_FIFO_SIZE 4096
61
62 #define MAX_MIDI_OUTPUTS 2
63 #define MAX_MIDI_INPUTS 2
64
65 #define FIXED_RATE 44100 /* XXX */
66 #define FIXED_RATE_SFC 1
67
68 #define MY_ERV_PENDING          0xffffffff
69 #define MY_ERV_BUS_RESET        0xfffffffe
70
71 #define EFC_FLAG_VERSION_1      0x1
72
73 /*
74  * This structure is arbitrarily named "echofire" and not "fireworks"
75  * because the latter's abbreviation would be "fw", which is somewhat
76  * ambiguous in a Firewire driver.
77  */
78 struct echofire {
79         struct snd_card *card;
80         struct fw_device *device;
81         struct fw_unit *unit;
82         int card_index;
83         bool disconnect;
84
85         struct mutex mutex;
86         spinlock_t lock;
87
88         struct mutex fcp_mutex;
89         spinlock_t fcp_lock;
90         wait_queue_head_t fcp_response_wait;
91         unsigned int sequence_number;
92         __be32 expected_response_category;
93         __be32 expected_response_command;
94         unsigned int return_value;
95         __be32 *response_data;
96         unsigned int response_quadlets;
97         struct fw_address_handler fcp_response_handler;
98
99         unsigned int syt_interval;
100
101         unsigned int fw_capture_users;
102         bool fw_capture_running;
103         bool capture_data_received;
104         unsigned int capture_audio_frame_bytes;
105         unsigned int capture_packet_bytes_max;
106         __be32 cip_0_expected_value;
107         u32 cip_1_expected_fdf;
108         struct fw_iso_buffer capture_iso_buffer;
109         u32 capture_cycle;
110         unsigned int capture_packet_index;
111         void *capture_packets[CAPTURE_QUEUE_LENGTH];
112         unsigned int capture_packet_offset[CAPTURE_QUEUE_LENGTH];
113         struct fw_iso_context *capture_iso_context;
114         wait_queue_head_t capture_data_wait;
115         u32 last_opcr;
116
117         unsigned int fw_playback_users;
118         bool fw_playback_running;
119         unsigned int playback_audio_frame_bytes;
120         unsigned int playback_packet_bytes_max;
121         u32 playback_cip_0;
122         unsigned int playback_dbc;
123         struct fw_iso_buffer playback_iso_buffer;
124         unsigned int playback_packet_index;
125         void *playback_packets[PLAYBACK_QUEUE_LENGTH];
126         unsigned int playback_packet_offset[PLAYBACK_QUEUE_LENGTH];
127         struct fw_iso_context *playback_iso_context;
128         u32 last_ipcr;
129
130         unsigned int midi_output_count;
131         unsigned long midi_output_running;
132         struct {
133                 struct snd_rawmidi_substream *substream;
134                 int fifo_filled;
135                 int fifo_max;
136         } midi_outputs[MAX_MIDI_OUTPUTS];
137
138         unsigned int midi_input_count;
139         unsigned long midi_input_running;
140         struct snd_rawmidi_substream *midi_inputs[MAX_MIDI_INPUTS];
141
142         unsigned int pcm_playback_channels;
143         unsigned int pcm_playback_period_pos;
144         unsigned int pcm_playback_buffer_pos;
145         struct snd_pcm_substream *pcm_playback_substream;
146         bool pcm_playback_running;
147
148         unsigned int pcm_capture_channels;
149         unsigned int pcm_capture_period_pos;
150         unsigned int pcm_capture_buffer_pos;
151         struct snd_pcm_substream *pcm_capture_substream;
152         bool pcm_capture_running;
153
154         unsigned polled_meters_count;
155 };
156
157 #define ef_err(ef, format, arg...) dev_err(&(ef)->device->device, format, ##arg)
158
159 static DEFINE_MUTEX(devices_mutex);
160 static unsigned int devices_used;
161
162 static const char *rcode_string(unsigned int rcode)
163 {
164         static const char *const names[] = {
165                 [RCODE_COMPLETE] = "complete",
166                 [RCODE_CONFLICT_ERROR] = "conflict error",
167                 [RCODE_DATA_ERROR] = "data error",
168                 [RCODE_TYPE_ERROR] = "type error",
169                 [RCODE_ADDRESS_ERROR] = "address error",
170                 [RCODE_SEND_ERROR] = "send error",
171                 [RCODE_CANCELLED] = "cancelled",
172                 [RCODE_BUSY] = "busy",
173                 [RCODE_GENERATION] = "generation",
174                 [RCODE_NO_ACK] = "no ack",
175         };
176
177         if (rcode < ARRAY_SIZE(names) && names[rcode])
178                 return names[rcode];
179         else
180                 return "unknown";
181 }
182
183 static const char *erv_string(unsigned int return_value)
184 {
185         static const char *const names[] = {
186                 "OK",
187                 "bad",
188                 "bad command",
189                 "comm err",
190                 "bad quad count",
191                 "unsupported",
192                 "1394 timeout",
193                 "DSP timeout",
194                 "bad rate",
195                 "bad clock",
196                 "bad channel",
197                 "bad pan",
198                 "flash busy",
199                 "bad mirror",
200                 "bad LED",
201                 "bad parameter",
202         };
203
204         if (return_value < ARRAY_SIZE(names))
205                 return names[return_value];
206         else if (return_value == MY_ERV_PENDING)
207                 return "no response";
208         else
209                 return "unknown";
210 }
211
212 static void fcp_response_callback(struct fw_card *card,
213                                   struct fw_request *request,
214                                   int tcode, int destination, int source,
215                                   int generation, unsigned long long offset,
216                                   void *data, size_t length,
217                                   void *callback_data)
218 {
219         struct echofire *ef = callback_data;
220         int device_generation;
221         const __be32 *quadlets;
222         const struct efc_header *header;
223         unsigned int count;
224         unsigned long flags;
225
226         if (offset != CSR_REGISTER_BASE + CSR_FCP_RESPONSE)
227                 return;
228
229         device_generation = ef->device->generation;
230         smp_rmb();
231         if (generation != device_generation ||
232             source != ef->device->node_id)
233                 return;
234
235         if (unlikely(dump_fcp_packets))
236                 print_hex_dump_bytes("FCP response ", DUMP_PREFIX_OFFSET,
237                                      data, length);
238
239         if (length < 12)
240                 return;
241         quadlets = (__force __be32 *)data;
242         /*
243          * AV/C response:
244          * cts=0, ACCEPTED=9, unit=ff, VENDOR-DEPENDENT=00,
245          * company_ID=000000, padding=0000
246          */
247         if (quadlets[0] != cpu_to_be32(0x09ff0000) ||
248             quadlets[1] != cpu_to_be32(0x00000000))
249                 return;
250         header = (struct efc_header *)&quadlets[2];
251         count = be32_to_cpu(header->length);
252         if (count < 6 || count > 126 || length < 8 + 4 * count)
253                 return;
254         spin_lock_irqsave(&ef->fcp_lock, flags);
255         if (header->category == ef->expected_response_category &&
256             header->command == ef->expected_response_command) {
257                 ef->return_value = be32_to_cpu(header->retval);
258                 if (ef->return_value == 0 && ef->response_quadlets > 0) {
259                         count -= 6;
260                         count = min(count, ef->response_quadlets);
261                         memcpy(ef->response_data, &quadlets[8], count * 4);
262                         ef->response_quadlets = count;
263                 }
264                 wake_up(&ef->fcp_response_wait);
265         }
266         spin_unlock_irqrestore(&ef->fcp_lock, flags);
267 }
268
269 static void fcp_handle_bus_reset(struct echofire *ef)
270 {
271         spin_lock_irq(&ef->fcp_lock);
272         if (ef->return_value == MY_ERV_PENDING) {
273                 ef->return_value = MY_ERV_BUS_RESET;
274                 wake_up(&ef->fcp_response_wait);
275         }
276         spin_unlock_irq(&ef->fcp_lock);
277 }
278
279 /* buf must be DMA-able */
280 static int submit_fcp_command(struct echofire *ef,
281                               const void *buf, size_t length)
282 {
283         unsigned int tries = 0;
284         int generation, rcode;
285
286         for (;;) {
287                 if (unlikely(dump_fcp_packets))
288                         print_hex_dump_bytes("FCP command ", DUMP_PREFIX_OFFSET,
289                                              buf, length);
290                 generation = ef->device->generation;
291                 smp_rmb();
292                 rcode = fw_run_transaction(ef->device->card,
293                                            TCODE_WRITE_BLOCK_REQUEST,
294                                            ef->device->node_id, generation,
295                                            ef->device->max_speed,
296                                            CSR_REGISTER_BASE + CSR_FCP_COMMAND,
297                                            (void *)buf, length);
298                 switch (rcode) {
299                 case RCODE_COMPLETE:
300                         return 0;
301                 case RCODE_CANCELLED:
302                 case RCODE_BUSY:
303                 case RCODE_NO_ACK:
304                         if (++tries >= 3) {
305                                 ef_err(ef, "FCP write failed: %#x: %s\n",
306                                        rcode, rcode_string(rcode));
307                                 return -ETIMEDOUT;
308                         }
309                         break;
310                 case RCODE_GENERATION:
311                         msleep(1);
312                         break;
313                 default:
314                         ef_err(ef, "FCP write failed: %#x: %s\n",
315                                rcode, rcode_string(rcode));
316                         return -EIO;
317                 }
318         }
319 }
320
321 static int _efc_transaction(struct echofire *ef, unsigned int flags,
322                             unsigned int category, unsigned int command,
323                             const u32 *parameters, unsigned int param_count,
324                             void *response, unsigned int response_quadlets)
325 {
326         unsigned int cmdbuf_bytes;
327         __be32 *cmdbuf;
328         struct efc_header *header;
329         unsigned int tries = 0, i;
330         int err;
331
332         cmdbuf_bytes = 32 + param_count * 4;
333         cmdbuf = kmalloc(cmdbuf_bytes, GFP_KERNEL);
334         if (!cmdbuf)
335                 return -ENOMEM;
336         /*
337          * AV/C command:
338          * cts=0, CONTROL=0, unit=ff, VENDOR-DEPENDENT=00,
339          * company_ID=000000, padding=0000
340          */
341         cmdbuf[0] = cpu_to_be32(0x00ff0000);
342         cmdbuf[1] = cpu_to_be32(0x00000000);
343         header = (struct efc_header *)&cmdbuf[2];
344         header->length = cpu_to_be32(6 + param_count);
345         header->version = cpu_to_be32(flags & EFC_FLAG_VERSION_1 ? 1 : 0);
346         header->category = cpu_to_be32(category);
347         header->command = cpu_to_be32(command);
348         header->retval = cpu_to_be32(0);
349         for (i = 0; i < param_count; ++i)
350                 cmdbuf[8 + i] = cpu_to_be32(parameters[i]);
351
352         mutex_lock(&ef->fcp_mutex);
353
354         spin_lock_irq(&ef->fcp_lock);
355
356         ef->expected_response_category = cpu_to_be32(category);
357         ef->expected_response_command = cpu_to_be32(command);
358         ef->response_data = response;
359
360         for (;;) {
361                 header->seqnum = cpu_to_be32(ef->sequence_number++);
362                 ef->return_value = MY_ERV_PENDING;
363                 ef->response_quadlets = response_quadlets;
364
365                 spin_unlock_irq(&ef->fcp_lock);
366
367                 err = submit_fcp_command(ef, cmdbuf, cmdbuf_bytes);
368                 if (err < 0)
369                         goto error_out;
370
371                 wait_event_timeout(ef->fcp_response_wait,
372                                    ef->return_value != MY_ERV_PENDING,
373                                    msecs_to_jiffies(120));
374
375                 spin_lock_irq(&ef->fcp_lock);
376                 if (ef->return_value == ERV_OK) {
377                         if (ef->response_quadlets == response_quadlets) {
378                                 err = 0;
379                         } else {
380                                 ef_err(ef, "[%u/%u] FCP response too short\n",
381                                        category, command);
382                                 err = -ENODATA;
383                         }
384                         break;
385                 } else if (ef->return_value == MY_ERV_BUS_RESET) {
386                         spin_unlock_irq(&ef->fcp_lock);
387                         msleep(1);
388                         spin_lock_irq(&ef->fcp_lock);
389                 } else if (ef->return_value == MY_ERV_PENDING ||
390                            ef->return_value == ERV_1394_TIMEOUT ||
391                            ef->return_value == ERV_DSP_TIMEOUT ||
392                            ef->return_value == ERV_FLASH_BUSY) {
393                         if (++tries >= 3) {
394                                 ef_err(ef, "command [%u/%u] failed: %#x: %s\n",
395                                        category, command, ef->return_value,
396                                        erv_string(ef->return_value));
397                                 err = -ETIMEDOUT;
398                                 break;
399                         }
400                 } else {
401                         ef_err(ef, "command [%u/%u] failed: %#x: %s\n",
402                                category, command, ef->return_value,
403                                erv_string(ef->return_value));
404                         err = -EIO;
405                         break;
406                 }
407         }
408         ef->response_quadlets = 0;
409         spin_unlock_irq(&ef->fcp_lock);
410
411 error_out:
412         mutex_unlock(&ef->fcp_mutex);
413         kfree(cmdbuf);
414         return err;
415 }
416
417 static int efc_transaction(struct echofire *ef,
418                            unsigned int category, unsigned int command,
419                            const u32 *parameters, unsigned int param_count,
420                            void *response, unsigned int response_quadlets)
421 {
422         return _efc_transaction(ef, 0, category, command, parameters,
423                                 param_count, response, response_quadlets);
424 }
425
426 static int echofire_hwinfo_get_caps(struct echofire *ef,
427                                     struct efc_hardware_info *hwinfo)
428 {
429         return _efc_transaction(ef, EFC_FLAG_VERSION_1,
430                                 EFC_CAT_HARDWARE_INFO,
431                                 EFC_CMD_HW_HWINFO_GET_CAPS,
432                                 NULL, 0, hwinfo, sizeof(*hwinfo) / 4);
433 }
434
435 static int determine_polled_meters_count(struct echofire *ef)
436 {
437         struct efc_polled_values values;
438         int err;
439
440         err = efc_transaction(ef, EFC_CAT_HARDWARE_INFO,
441                               EFC_CMD_HW_GET_POLLED,
442                               NULL, 0, &values, 9);
443         if (err < 0)
444                 return err;
445         ef->polled_meters_count = be32_to_cpu(values.nb_output_meters);
446         ef->polled_meters_count += be32_to_cpu(values.nb_input_meters);
447         if (ef->polled_meters_count > 100) {
448                 ef_err(ef, "invalid polled meters count\n");
449                 return -ENXIO;
450         }
451         return 0;
452 }
453
454 static int echofire_get_polled_values(struct echofire *ef,
455                                       struct efc_polled_values *values)
456 {
457         return efc_transaction(ef, EFC_CAT_HARDWARE_INFO,
458                                EFC_CMD_HW_GET_POLLED,
459                                NULL, 0, values, 9 + ef->polled_meters_count);
460 }
461
462 static int echofire_identify(struct echofire *ef)
463 {
464         return efc_transaction(ef, EFC_CAT_HARDWARE_CONTROL,
465                                EFC_CMD_HWCTRL_IDENTIFY, NULL, 0, NULL, 0);
466 }
467
468 static int cmp_add_playback_connection(struct echofire *ef)
469 {
470         __be32 *buf;
471         unsigned int old, new, try, rcode;
472         int generation, err;
473
474         buf = kmalloc(8, GFP_KERNEL);
475         if (!buf)
476                 return -ENOMEM;
477
478         /* TODO: allocate channel and bandwidth */
479         old = ef->last_ipcr;
480         for (try = 0; try < 5; ++try) {
481                 new = old;
482                 new &= ~0x7f3f0000;
483                 new |= (1 << 24) | (PLAYBACK_CHANNEL << 16);
484                 buf[0] = cpu_to_be32(old);
485                 buf[1] = cpu_to_be32(new);
486                 generation = ef->device->generation;
487                 smp_rmb();
488                 rcode = fw_run_transaction(ef->device->card,
489                                            TCODE_LOCK_COMPARE_SWAP,
490                                            ef->device->node_id, generation,
491                                            ef->device->max_speed,
492                                            CSR_REGISTER_BASE + CSR_IPCR(0),
493                                            buf, 8);
494                 switch (rcode) {
495                 case RCODE_GENERATION:
496                         break;
497                 case RCODE_COMPLETE:
498                         if (be32_to_cpup(buf) == old) {
499                                 ef->last_ipcr = new;
500                                 err = 0;
501                                 goto finish;
502                         }
503                         old = be32_to_cpup(buf);
504                         if (old & 0x7f000000) {
505                                 /* we do not support overlaying a connection */
506                                 ef_err(ef, "playback stream already in use\n");
507                                 err = -EBUSY;
508                                 goto finish;
509                         }
510                 }
511         }
512         ef_err(ef, "iPCR modification failed: %#x: %s\n",
513                rcode, rcode_string(rcode));
514         err = -EIO;
515 finish:
516         kfree(buf);
517         return err;
518 }
519
520 static int cmp_restore_playback_connection(struct echofire *ef)
521 {
522         __be32 *buf;
523         unsigned int old, new, try, rcode;
524         int generation, err;
525
526         buf = kmalloc(8, GFP_KERNEL);
527         if (!buf)
528                 return -ENOMEM;
529
530         old = ef->last_ipcr & ~0x7f000000;
531         for (try = 0; try < 5; ++try) {
532                 new = old + (1 << 24);
533                 buf[0] = cpu_to_be32(old);
534                 buf[1] = cpu_to_be32(new);
535                 generation = ef->device->generation;
536                 smp_rmb();
537                 rcode = fw_run_transaction(ef->device->card,
538                                            TCODE_LOCK_COMPARE_SWAP,
539                                            ef->device->node_id, generation,
540                                            ef->device->max_speed,
541                                            CSR_REGISTER_BASE + CSR_IPCR(0),
542                                            buf, 8);
543                 switch (rcode) {
544                 case RCODE_GENERATION:
545                         /* try again later */
546                         err = 0;
547                         goto finish;
548                 case RCODE_COMPLETE:
549                         if (be32_to_cpup(buf) == old) {
550                                 ef->last_ipcr = new;
551                                 err = 0;
552                                 goto finish;
553                         }
554                         old = be32_to_cpup(buf);
555                         if ((old & 0x003f0000) != (new & 0x003f0000)) {
556                                 ef_err(ef, "channel number changed\n");
557                                 err = -EBUSY;
558                                 goto finish;
559                         }
560                         if ((old & 0x7f000000) == 0x3f000000) {
561                                 ef_err(ef, "too many connections\n");
562                                 err = -EBUSY;
563                                 goto finish;
564                         }
565                 }
566         }
567         ef_err(ef, "iPCR modification failed: %#x: %s\n",
568                rcode, rcode_string(rcode));
569         err = -EIO;
570 finish:
571         kfree(buf);
572         return err;
573 }
574
575 static int cmp_remove_playback_connection(struct echofire *ef)
576 {
577         __be32 *buf;
578         unsigned int old, new, try, rcode;
579         int generation, err;
580
581         buf = kmalloc(8, GFP_KERNEL);
582         if (!buf)
583                 return -ENOMEM;
584
585         old = ef->last_ipcr;
586         for (try = 0; try < 5; ++try) {
587                 new = old - (1 << 24);
588                 buf[0] = cpu_to_be32(old);
589                 buf[1] = cpu_to_be32(new);
590                 generation = ef->device->generation;
591                 smp_rmb();
592                 rcode = fw_run_transaction(ef->device->card,
593                                            TCODE_LOCK_COMPARE_SWAP,
594                                            ef->device->node_id, generation,
595                                            ef->device->max_speed,
596                                            CSR_REGISTER_BASE + CSR_IPCR(0),
597                                            buf, 8);
598                 switch (rcode) {
599                 case RCODE_GENERATION:
600                         err = 0;
601                         goto finish;
602                 case RCODE_COMPLETE:
603                         if (be32_to_cpup(buf) == old) {
604                                 ef->last_ipcr = new;
605                                 /* TODO: deallocate channel and bandwidth */
606                                 err = 0;
607                                 goto finish;
608                         }
609                         old = be32_to_cpup(buf);
610                         if ((old & (0x3f << 24)) == 0) {
611                                 /* somebody else broke our connection */
612                                 ef->last_ipcr = new;
613                                 err = 0;
614                                 goto finish;
615                         }
616                 }
617         }
618         ef_err(ef, "iPCR modification failed: %#x: %s\n",
619                rcode, rcode_string(rcode));
620         err = -EIO;
621 finish:
622         kfree(buf);
623         return err;
624 }
625
626 static int cmp_add_capture_connection(struct echofire *ef)
627 {
628         __be32 *buf;
629         unsigned int old, new, try, rcode;
630         int generation, err;
631
632         buf = kmalloc(8, GFP_KERNEL);
633         if (!buf)
634                 return -ENOMEM;
635
636         /* TODO: allocate channel and bandwidth */
637         old = ef->last_opcr;
638         for (try = 0; try < 5; ++try) {
639                 new = old;
640                 new &= ~0x003ffc00;
641                 new |= (1 << 24) | (CAPTURE_CHANNEL << 16) |
642                         (SCODE_400 << 14) | (1 << 10);
643                 buf[0] = cpu_to_be32(old);
644                 buf[1] = cpu_to_be32(new);
645                 generation = ef->device->generation;
646                 smp_rmb();
647                 rcode = fw_run_transaction(ef->device->card,
648                                            TCODE_LOCK_COMPARE_SWAP,
649                                            ef->device->node_id, generation,
650                                            ef->device->max_speed,
651                                            CSR_REGISTER_BASE + CSR_OPCR(0),
652                                            buf, 8);
653                 switch (rcode) {
654                 case RCODE_GENERATION:
655                         break;
656                 case RCODE_COMPLETE:
657                         if (be32_to_cpup(buf) == old) {
658                                 ef->last_opcr = new;
659                                 err = 0;
660                                 goto finish;
661                         }
662                         old = be32_to_cpup(buf);
663                         if (old & 0x7f000000) {
664                                 /* we do not support overlaying a connection */
665                                 ef_err(ef, "capture stream already in use\n");
666                                 err = -EBUSY;
667                                 goto finish;
668                         }
669                 }
670         }
671         ef_err(ef, "oPCR modification failed: %#x: %s\n",
672                rcode, rcode_string(rcode));
673         err = -EIO;
674 finish:
675         kfree(buf);
676         return err;
677 }
678
679 static int cmp_restore_capture_connection(struct echofire *ef)
680 {
681         __be32 *buf;
682         unsigned int old, new, try, rcode;
683         int generation, err;
684
685         buf = kmalloc(8, GFP_KERNEL);
686         if (!buf)
687                 return -ENOMEM;
688
689         old = ef->last_opcr & ~0x7f000000;
690         for (try = 0; try < 5; ++try) {
691                 new = old + (1 << 24);
692                 buf[0] = cpu_to_be32(old);
693                 buf[1] = cpu_to_be32(new);
694                 generation = ef->device->generation;
695                 smp_rmb();
696                 rcode = fw_run_transaction(ef->device->card,
697                                            TCODE_LOCK_COMPARE_SWAP,
698                                            ef->device->node_id, generation,
699                                            ef->device->max_speed,
700                                            CSR_REGISTER_BASE + CSR_OPCR(0),
701                                            buf, 8);
702                 switch (rcode) {
703                 case RCODE_GENERATION:
704                         /* try again later */
705                         err = 0;
706                         goto finish;
707                 case RCODE_COMPLETE:
708                         if (be32_to_cpup(buf) == old) {
709                                 ef->last_opcr = new;
710                                 err = 0;
711                                 goto finish;
712                         }
713                         old = be32_to_cpup(buf);
714                         if ((old & 0x003f0000) != (new & 0x003f0000)) {
715                                 ef_err(ef, "channel number changed\n");
716                                 err = -EBUSY;
717                                 goto finish;
718                         }
719                         if ((old & 0x7f000000) == 0x3f000000) {
720                                 ef_err(ef, "too many connections\n");
721                                 err = -EBUSY;
722                                 goto finish;
723                         }
724                 }
725         }
726         ef_err(ef, "oPCR modification failed: %#x: %s\n",
727                rcode, rcode_string(rcode));
728         err = -EIO;
729 finish:
730         kfree(buf);
731         return err;
732 }
733
734 static int cmp_remove_capture_connection(struct echofire *ef)
735 {
736         __be32 *buf;
737         unsigned int old, new, try, rcode;
738         int generation, err;
739
740         buf = kmalloc(8, GFP_KERNEL);
741         if (!buf)
742                 return -ENOMEM;
743
744         old = ef->last_opcr;
745         for (try = 0; try < 5; ++try) {
746                 new = old - (1 << 24);
747                 buf[0] = cpu_to_be32(old);
748                 buf[1] = cpu_to_be32(new);
749                 generation = ef->device->generation;
750                 smp_rmb();
751                 rcode = fw_run_transaction(ef->device->card,
752                                            TCODE_LOCK_COMPARE_SWAP,
753                                            ef->device->node_id, generation,
754                                            ef->device->max_speed,
755                                            CSR_REGISTER_BASE + CSR_OPCR(0),
756                                            buf, 8);
757                 switch (rcode) {
758                 case RCODE_GENERATION:
759                         err = 0;
760                         goto finish;
761                 case RCODE_COMPLETE:
762                         if (be32_to_cpup(buf) == old) {
763                                 ef->last_opcr = new;
764                                 /* TODO: deallocate channel and bandwidth */
765                                 err = 0;
766                                 goto finish;
767                         }
768                         old = be32_to_cpup(buf);
769                         if ((old & (0x3f << 24)) == 0) {
770                                 /* somebody else broke our connection */
771                                 ef->last_opcr = new;
772                                 err = 0;
773                                 goto finish;
774                         }
775                 }
776         }
777         ef_err(ef, "oPCR modification failed: %#x: %s\n",
778                rcode, rcode_string(rcode));
779         err = -EIO;
780 finish:
781         kfree(buf);
782         return err;
783 }
784
785 static void playback_iso_callback(struct fw_iso_context *context,
786                                   u32 cycle, size_t header_length,
787                                   void *header, void *data)
788 {
789         /* playback queueing is handled by capture callback */
790 }
791
792 static int queue_skip_packet(struct echofire *ef)
793 {
794         struct fw_iso_packet packet;
795
796         packet.payload_length = 0;
797         packet.interrupt = 0;
798         packet.skip = 1;
799         packet.tag = 0;
800         packet.sy = 0;
801         packet.header_length = 0;
802
803         return fw_iso_context_queue(ef->playback_iso_context, &packet, NULL, 0);
804 }
805
806 static int allocate_playback_buffer(struct echofire *ef)
807 {
808         unsigned int packet_size, packets_per_page, pages;
809         unsigned int i, page_index, offset_in_page;
810         void *p;
811         int err;
812
813         packet_size = L1_CACHE_ALIGN(ef->playback_packet_bytes_max);
814         packets_per_page = PAGE_SIZE / packet_size;
815         if (WARN_ON(!packets_per_page))
816                 return -ENXIO;
817         pages = DIV_ROUND_UP(ARRAY_SIZE(ef->playback_packets),
818                              packets_per_page);
819         err = fw_iso_buffer_init(&ef->playback_iso_buffer, ef->device->card,
820                                  pages, DMA_TO_DEVICE);
821         if (err < 0)
822                 return err;
823
824         for (i = 0; i < ARRAY_SIZE(ef->playback_packets); ++i) {
825                 page_index = i / packets_per_page;
826                 p = page_address(ef->playback_iso_buffer.pages[page_index]);
827                 if (!p) {
828                         fw_iso_buffer_destroy(&ef->playback_iso_buffer,
829                                               ef->device->card);
830                         ef_err(ef, "DMA page has no kernel virtual address\n");
831                         return -ENOMEM;
832                 }
833                 offset_in_page = (i % packets_per_page) * packet_size;
834                 ef->playback_packets[i] = p + offset_in_page;
835                 ef->playback_packet_offset[i] =
836                                 page_index * PAGE_SIZE + offset_in_page;
837         }
838         ef->playback_packet_index = 0;
839         return 0;
840 }
841
842 static inline unsigned int cycles_add(unsigned int cycles, unsigned int add)
843 {
844         cycles += add;
845         if ((cycles & 0x1fff) >= 8000)
846                 cycles += (1 << 13) - 8000;
847         return cycles;
848 }
849
850 static int fw_playback_start(struct echofire *ef)
851 {
852         unsigned int start_time;
853         unsigned long flags;
854         int err;
855
856         err = wait_event_interruptible(ef->capture_data_wait,
857                                        ef->capture_data_received ||
858                                        !ef->fw_capture_running);
859         if (err < 0)
860                 return err;
861         if (ef->disconnect)
862                 return -ENODEV;
863         if (!ef->fw_capture_running)
864                 return -EIO;
865
866         ef->playback_audio_frame_bytes =
867                 4 * (ef->pcm_playback_channels + (ef->midi_output_count > 0));
868         ef->playback_packet_bytes_max =
869                 8 + ef->syt_interval * ef->playback_audio_frame_bytes;
870         ef->playback_cip_0 = 0x00000000 | ((ef->device->node_id & 0x3f) << 24) |
871                 ((ef->playback_audio_frame_bytes / 4) << 16);
872         ef->playback_dbc = 0;
873
874         err = allocate_playback_buffer(ef);
875         if (err < 0)
876                 return err;
877
878         ef->playback_iso_context = fw_iso_context_create
879                 (ef->device->card, FW_ISO_CONTEXT_TRANSMIT, PLAYBACK_CHANNEL,
880                  ef->device->max_speed, 0, playback_iso_callback, ef);
881         if (IS_ERR(ef->playback_iso_context)) {
882                 err = PTR_ERR(ef->playback_iso_context);
883                 goto err_buffer;
884         }
885
886         /* we need at least one queued packet when starting */
887         err = queue_skip_packet(ef);
888         if (err < 0)
889                 goto err_context;
890
891         spin_lock_irqsave(&ef->lock, flags);
892         start_time = cycles_add(ef->capture_cycle, 32);
893         err = fw_iso_context_start(ef->playback_iso_context, start_time, 0, 0);
894         ef->fw_playback_running = err >= 0;
895         spin_unlock_irqrestore(&ef->lock, flags);
896         if (err < 0)
897                 goto err_context;
898
899         err = cmp_add_playback_connection(ef);
900         if (err < 0)
901                 goto err_running;
902
903         return 0;
904
905 err_running:
906         ef->fw_playback_running = false;
907 err_context:
908         fw_iso_context_destroy(ef->playback_iso_context);
909 err_buffer:
910         fw_iso_buffer_destroy(&ef->playback_iso_buffer, ef->device->card);
911         return err;
912 }
913
914 static void fw_playback_stop(struct echofire *ef)
915 {
916         ef->fw_playback_running = false;
917         cmp_remove_playback_connection(ef);
918         fw_iso_context_stop(ef->playback_iso_context);
919         fw_iso_context_destroy(ef->playback_iso_context);
920         fw_iso_buffer_destroy(&ef->playback_iso_buffer, ef->device->card);
921 }
922
923 static int fw_playback_open(struct echofire *ef)
924 {
925         int err;
926
927         mutex_lock(&ef->mutex);
928         if (ef->fw_playback_users == 0) {
929                 err = fw_playback_start(ef);
930                 if (err < 0) {
931                         mutex_unlock(&ef->mutex);
932                         return err;
933                 }
934         }
935         ef->fw_playback_users++;
936         mutex_unlock(&ef->mutex);
937         return 0;
938 }
939
940 static void fw_playback_close(struct echofire *ef)
941 {
942         mutex_lock(&ef->mutex);
943         ef->fw_playback_users--;
944         if (ef->fw_playback_users == 0)
945                 fw_playback_stop(ef);
946         mutex_unlock(&ef->mutex);
947 }
948
949 static void fill_midi_quadlets(struct echofire *ef, void *data,
950                                unsigned int data_blocks)
951 {
952         unsigned int fw_frame_size, dbc, ports, i, port;
953         __be32 *dst;
954         u8 byte;
955
956         fw_frame_size = ef->playback_audio_frame_bytes;
957         dbc = ef->playback_dbc;
958         ports = ef->midi_output_count;
959         for (i = 0; i < data_blocks; ++i) {
960                 dst = data + i * fw_frame_size;
961                 *dst = cpu_to_be32(0x80000000);
962                 port = (dbc + i) & 7;
963                 if (port >= ports)
964                         continue;
965                 if (ef->midi_outputs[port].fifo_filled > 0)
966                         ef->midi_outputs[port].fifo_filled -= 22727; /* XXX */
967                 if (ef->midi_outputs[port].fifo_filled >=
968                                         ef->midi_outputs[port].fifo_max)
969                         continue;
970                 if (!test_bit(port, &ef->midi_output_running))
971                         continue;
972                 if (snd_rawmidi_transmit(ef->midi_outputs[port].substream,
973                                          &byte, 1) != 1) {
974                         __clear_bit(port, &ef->midi_output_running);
975                         continue;
976                 }
977                 ef->midi_outputs[port].fifo_filled += FIXED_RATE;
978                 *dst = cpu_to_be32(0x81000000) | cpu_to_be32(byte << 16);
979         }
980 }
981
982 static bool fill_playback_packet(struct echofire *ef, void *data,
983                                  unsigned int data_blocks)
984 {
985         struct snd_pcm_runtime *runtime;
986         unsigned int channels, pcm_frame_size, fw_frame_size;
987         unsigned int i, c, remaining_frames;
988         const void *src;
989         __be32 *dst;
990
991         channels = ef->pcm_playback_channels;
992         pcm_frame_size = 4 * channels;
993         fw_frame_size = ef->playback_audio_frame_bytes;
994
995         if (ef->midi_output_count)
996                 fill_midi_quadlets(ef, data + pcm_frame_size, data_blocks);
997
998         if (unlikely(!ef->pcm_playback_running)) {
999                 for (i = 0; i < data_blocks; ++i) {
1000                         dst = data + i * fw_frame_size;
1001                         for (c = 0; c < channels; ++c)
1002                                 *dst++ = cpu_to_be32(0x40000000);
1003                 }
1004                 return false;
1005         }
1006
1007         runtime = ef->pcm_playback_substream->runtime;
1008         src = runtime->dma_area +
1009                         ef->pcm_playback_buffer_pos * runtime->frame_bits / 8;
1010         remaining_frames = runtime->buffer_size - ef->pcm_playback_buffer_pos;
1011         switch (runtime->format) {
1012         case SNDRV_PCM_FORMAT_S32:
1013                 for (i = 0; i < data_blocks; ++i) {
1014                         dst = data + i * fw_frame_size;
1015                         for (c = 0; c < channels; ++c) {
1016                                 *dst = cpu_to_be32(*(const u32 *)src >> 8) |
1017                                         cpu_to_be32(0x40000000);
1018                                 src += 4;
1019                                 dst++;
1020                         }
1021                         remaining_frames--;
1022                         if (!remaining_frames)
1023                                 src = runtime->dma_area;
1024                 }
1025                 break;
1026         case SNDRV_PCM_FORMAT_S16:
1027                 for (i = 0; i < data_blocks; ++i) {
1028                         dst = data + i * fw_frame_size;
1029                         for (c = 0; c < channels; ++c) {
1030                                 *dst = cpu_to_be32(*(const u16 *)src << 8) |
1031                                         cpu_to_be32(0x40000000);
1032                                 src += 2;
1033                                 dst++;
1034                         }
1035                         remaining_frames--;
1036                         if (!remaining_frames)
1037                                 src = runtime->dma_area;
1038                 }
1039                 break;
1040         }
1041
1042         ef->pcm_playback_buffer_pos += data_blocks;
1043         if (ef->pcm_playback_buffer_pos >= runtime->buffer_size)
1044                 ef->pcm_playback_buffer_pos -= runtime->buffer_size;
1045         ef->pcm_playback_period_pos += data_blocks;
1046         if (ef->pcm_playback_period_pos >= runtime->period_size) {
1047                 ef->pcm_playback_period_pos -= runtime->period_size;
1048                 return true;
1049         } else {
1050                 return false;
1051         }
1052 }
1053
1054 static int queue_playback_packet(struct echofire *ef, u32 cip1,
1055                                  unsigned int data_blocks)
1056 {
1057         struct fw_iso_packet packet;
1058         unsigned int index, offset;
1059         __be32 *data;
1060         bool period_elapsed;
1061         int err;
1062
1063         index = ef->playback_packet_index;
1064         offset = ef->playback_packet_offset[index];
1065
1066         data = ef->playback_packets[index];
1067         data[0] = cpu_to_be32(ef->playback_cip_0 | (ef->playback_dbc & 0xff));
1068         data[1] = cpu_to_be32(cip1);
1069         if (data_blocks) {
1070                 period_elapsed = fill_playback_packet(ef, data + 2,
1071                                                       data_blocks);
1072                 packet.payload_length =
1073                         8 + data_blocks * ef->playback_audio_frame_bytes;
1074         } else {
1075                 period_elapsed = false;
1076                 packet.payload_length = 8;
1077         }
1078         packet.interrupt = 1;
1079         packet.skip = 0;
1080         packet.tag = 1;
1081         packet.sy = 0;
1082         packet.header_length = 0;
1083
1084         err = fw_iso_context_queue(ef->playback_iso_context, &packet,
1085                                    &ef->playback_iso_buffer, offset);
1086         if (err < 0)
1087                 return err;
1088
1089         if (unlikely(dump_tx_packets > 0)) {
1090                 dump_tx_packets--;
1091                 print_hex_dump_bytes("iso tx ", DUMP_PREFIX_OFFSET,
1092                                      data, packet.payload_length);
1093         }
1094
1095         ef->playback_dbc += data_blocks;
1096
1097         if (++index >= ARRAY_SIZE(ef->playback_packets))
1098                 index = 0;
1099         ef->playback_packet_index = index;
1100
1101         return period_elapsed ? 1 : 0;
1102 }
1103
1104 static int queue_capture_packet(struct echofire *ef, unsigned int index)
1105 {
1106         struct fw_iso_packet packet;
1107
1108         packet.payload_length = ef->capture_packet_bytes_max;
1109         packet.interrupt = 1;
1110         packet.skip = 0;
1111         packet.tag = 0;
1112         packet.sy = 0;
1113         packet.header_length = 4;
1114         return fw_iso_context_queue(ef->capture_iso_context, &packet,
1115                                     &ef->capture_iso_buffer,
1116                                     ef->capture_packet_offset[index]);
1117 }
1118
1119 static bool handle_capture_pcm_data(struct echofire *ef,
1120                                     void *data, unsigned int data_blocks)
1121 {
1122         struct snd_pcm_runtime *runtime;
1123         unsigned int channels, pcm_frame_size, fw_frame_size;
1124         unsigned int i, c, remaining_frames;
1125         __be32 *src;
1126         void *dst;
1127
1128         runtime = ef->pcm_capture_substream->runtime;
1129         channels = ef->pcm_capture_channels;
1130         pcm_frame_size = 4 * channels;
1131         fw_frame_size = ef->capture_audio_frame_bytes;
1132         dst = runtime->dma_area +
1133                         ef->pcm_capture_buffer_pos * runtime->frame_bits / 8;
1134         remaining_frames = runtime->buffer_size - ef->pcm_capture_buffer_pos;
1135         switch (runtime->format) {
1136         case SNDRV_PCM_FORMAT_S32:
1137                 for (i = 0; i < data_blocks; ++i) {
1138                         src = data + i * fw_frame_size;
1139                         for (c = 0; c < channels; ++c) {
1140                                 *(u32 *)dst = be32_to_cpup(src) << 8;
1141                                 src++;
1142                                 dst += 4;
1143                         }
1144                         remaining_frames--;
1145                         if (!remaining_frames)
1146                                 dst = runtime->dma_area;
1147                 }
1148                 break;
1149         case SNDRV_PCM_FORMAT_S16:
1150                 for (i = 0; i < data_blocks; ++i) {
1151                         src = data + i * fw_frame_size;
1152                         for (c = 0; c < channels; ++c) {
1153                                 *(u16 *)dst = be32_to_cpup(src) >> 8;
1154                                 src++;
1155                                 dst += 2;
1156                         }
1157                         remaining_frames--;
1158                         if (!remaining_frames)
1159                                 dst = runtime->dma_area;
1160                 }
1161                 break;
1162         case SNDRV_PCM_FORMAT_S24_BE:
1163                 for (i = 0; i < data_blocks; ++i) {
1164                         src = data + i * fw_frame_size;
1165                         memcpy(dst, src, pcm_frame_size);
1166                         dst += pcm_frame_size;
1167                         remaining_frames--;
1168                         if (!remaining_frames)
1169                                 dst = runtime->dma_area;
1170                 }
1171                 break;
1172         }
1173
1174         ef->pcm_capture_buffer_pos += data_blocks;
1175         if (ef->pcm_capture_buffer_pos >= runtime->buffer_size)
1176                 ef->pcm_capture_buffer_pos -= runtime->buffer_size;
1177         ef->pcm_capture_period_pos += data_blocks;
1178         if (ef->pcm_capture_period_pos >= runtime->period_size) {
1179                 ef->pcm_capture_period_pos -= runtime->period_size;
1180                 return true;
1181         } else {
1182                 return false;
1183         }
1184 }
1185
1186 static void handle_capture_midi_data(struct echofire *ef,
1187                                      void *data, unsigned int length)
1188 {
1189         unsigned int port, step;
1190         const u8 *m, *end;
1191
1192         end = data + length;
1193         step = 8 * ef->capture_audio_frame_bytes;
1194         data += 4 * ef->pcm_capture_channels;
1195         for (port = 0; port < ef->midi_input_count; ++port) {
1196                 if (!test_bit(port, &ef->midi_input_running))
1197                         continue;
1198                 for (m = data + 4 * port; m + 3 < end; m += step) {
1199                         if (m[0] < 0x81 || m[0] > 0x83)
1200                                 continue;
1201                         snd_rawmidi_receive(ef->midi_inputs[port],
1202                                             &m[1], m[0] - 0x80);
1203                 }
1204         }
1205 }
1206
1207 static void capture_cip_header_error(struct echofire *ef,
1208                                      __be32 cip0_be, u32 cip1)
1209 {
1210         u32 cip0 = be32_to_cpu(cip0_be);
1211         unsigned int dbs;
1212
1213         ef_err(ef, "capture iso header error:");
1214         if ((cip0 & 0x80000000) != 0)
1215                 printk(KERN_CONT " eoh!=0");
1216         if ((cip0 & 0x40000000) != 0)
1217                 printk(KERN_CONT " form_0!=0");
1218         dbs = ef->capture_audio_frame_bytes / 4;
1219         if ((cip0 & 0x00ff0000) != (dbs << 16))
1220                 printk(KERN_CONT " dbs!=%#x", dbs);
1221         if ((cip0 & 0x0000c000) != 0)
1222                 printk(KERN_CONT " fn!=0");
1223         if ((cip0 & 0x00003800) != 0)
1224                 printk(KERN_CONT " qpc!=0");
1225         if ((cip0 & 0x00000400) != 0)
1226                 printk(KERN_CONT " sph!=0");
1227         if ((cip1 & 0x80000000) != 0x80000000)
1228                 printk(KERN_CONT " eoh!=1");
1229         if ((cip1 & 0x40000000) != 0)
1230                 printk(KERN_CONT " form_1!=0");
1231         if ((cip1 & 0x3f000000) != 0x10000000)
1232                 printk(KERN_CONT " fmt!=0x10");
1233         if ((cip1 & 0x00ff0000) != 0x00ff0000 &&
1234             (cip1 & 0x00ff0000) != ef->cip_1_expected_fdf)
1235                 printk(KERN_CONT " fdf!=%#x", ef->cip_1_expected_fdf >> 16);
1236         printk(KERN_CONT "\n");
1237 }
1238
1239 static void capture_iso_callback(struct fw_iso_context *context,
1240                                  u32 cycle, size_t header_length,
1241                                  void *header, void *data)
1242 {
1243         struct echofire *ef = data;
1244         __be32 *packet_data;
1245         unsigned int length, cip1, data_blocks;
1246         __be32 cip0_be;
1247         unsigned long flags;
1248         bool period_elapsed, playback_period_elapsed = false;
1249         int err;
1250
1251         packet_data = ef->capture_packets[ef->capture_packet_index];
1252         prefetch(packet_data);
1253         length = be32_to_cpup((__be32 *)header) >> 16;
1254
1255         if (unlikely(!ef->fw_capture_running))
1256                 return;
1257
1258         if (unlikely(dump_rx_packets > 0)) {
1259                 dump_rx_packets--;
1260                 print_hex_dump_bytes("iso rx ", DUMP_PREFIX_OFFSET,
1261                                      packet_data, length);
1262         }
1263
1264         if (unlikely(length < 8)) {
1265                 data_blocks = 0;
1266                 length = 0;
1267                 cip1 = 0x90ffffff;
1268         } else {
1269                 data_blocks = (length - 8) / ef->capture_audio_frame_bytes;
1270                 /*
1271                  * check constant fields in CIP header: eoh=0, form_0=0, dbs,
1272                  * fn=00, qpc=000, sph=0; eoh=1, form_1=0, fmt=010000
1273                  */
1274                 cip0_be = packet_data[0] & cpu_to_be32(0xc0fffc00);
1275                 cip1 = be32_to_cpu(packet_data[1]);
1276                 if (unlikely(cip0_be != ef->cip_0_expected_value ||
1277                              (cip1 & 0xff000000) != 0x90000000 ||
1278                              ((cip1 & 0x00ff0000) != 0x00ff0000 &&
1279                               (cip1 & 0x00ff0000) != ef->cip_1_expected_fdf))) {
1280                         capture_cip_header_error(ef, cip0_be, cip1);
1281                         goto error;
1282                 }
1283                 packet_data += 2;
1284                 length -= 8;
1285         }
1286
1287         spin_lock_irqsave(&ef->lock, flags);
1288         if (ef->pcm_capture_running && data_blocks > 0)
1289                 period_elapsed =
1290                         handle_capture_pcm_data(ef, packet_data, data_blocks);
1291         else
1292                 period_elapsed = false;
1293         if (ef->midi_input_count)
1294                 handle_capture_midi_data(ef, packet_data, length);
1295         ef->capture_cycle = cycle;
1296         if (ef->fw_playback_running) {
1297                 err = queue_playback_packet(ef, cip1, data_blocks);
1298                 if (unlikely(err < 0)) {
1299                         ef_err(ef, "packet queueing error: %d", err);
1300                         goto error;
1301                 } else {
1302                         playback_period_elapsed = err > 0;
1303                 }
1304         }
1305         spin_unlock_irqrestore(&ef->lock, flags);
1306
1307         err = queue_capture_packet(ef, ef->capture_packet_index);
1308         if (unlikely(err < 0)) {
1309                 ef_err(ef, "packet queueing error: %d", err);
1310 error:
1311                 ef->fw_capture_running = false;
1312                 /* TODO: stop pcm+midi streams */
1313                 return;
1314         }
1315
1316         ef->capture_packet_index++;
1317         if (ef->capture_packet_index >= ARRAY_SIZE(ef->capture_packets))
1318                 ef->capture_packet_index = 0;
1319
1320         if (playback_period_elapsed)
1321                 snd_pcm_period_elapsed(ef->pcm_playback_substream);
1322         if (period_elapsed)
1323                 snd_pcm_period_elapsed(ef->pcm_capture_substream);
1324 }
1325
1326 static void first_capture_iso_callback(struct fw_iso_context *context,
1327                                        u32 cycle, size_t header_length,
1328                                        void *header, void *data)
1329 {
1330         struct echofire *ef = data;
1331         unsigned int length;
1332
1333         length = be32_to_cpup((__be32 *)header) >> 16;
1334         if (length > 8) { /* more than just a CIP header? */
1335                 context->callback.sc = capture_iso_callback;
1336                 ef->capture_data_received = true;
1337                 wake_up(&ef->capture_data_wait);
1338         }
1339         capture_iso_callback(context, cycle, header_length, header, data);
1340 }
1341
1342 static int allocate_capture_buffer(struct echofire *ef)
1343 {
1344         unsigned int packet_size, packets_per_page, pages;
1345         unsigned int i, page_index, offset_in_page;
1346         void *p;
1347         int err;
1348
1349         packet_size = L1_CACHE_ALIGN(ef->capture_packet_bytes_max);
1350         packets_per_page = PAGE_SIZE / packet_size;
1351         if (WARN_ON(!packets_per_page))
1352                 return -ENXIO;
1353         pages = DIV_ROUND_UP(ARRAY_SIZE(ef->capture_packets),
1354                              packets_per_page);
1355         err = fw_iso_buffer_init(&ef->capture_iso_buffer, ef->device->card,
1356                                  pages, DMA_FROM_DEVICE);
1357         if (err < 0)
1358                 return err;
1359
1360         for (i = 0; i < ARRAY_SIZE(ef->capture_packets); ++i) {
1361                 page_index = i / packets_per_page;
1362                 p = page_address(ef->capture_iso_buffer.pages[page_index]);
1363                 if (!p) {
1364                         fw_iso_buffer_destroy(&ef->capture_iso_buffer,
1365                                               ef->device->card);
1366                         ef_err(ef, "DMA page has no kernel virtual address\n");
1367                         return -ENOMEM;
1368                 }
1369                 offset_in_page = (i % packets_per_page) * packet_size;
1370                 ef->capture_packets[i] = p + offset_in_page;
1371                 ef->capture_packet_offset[i] =
1372                                 page_index * PAGE_SIZE + offset_in_page;
1373         }
1374         ef->capture_packet_index = 0;
1375         return 0;
1376 }
1377
1378 static int fw_capture_start(struct echofire *ef)
1379 {
1380         unsigned int i;
1381         int err;
1382
1383         if (ef->disconnect)
1384                 return -ENODEV;
1385
1386         ef->capture_audio_frame_bytes =
1387                 4 * (ef->pcm_capture_channels + (ef->midi_input_count > 0));
1388         ef->capture_packet_bytes_max =
1389                 8 + ef->syt_interval * ef->capture_audio_frame_bytes;
1390         ef->cip_0_expected_value =
1391                 cpu_to_be32((ef->capture_audio_frame_bytes / 4) << 16);
1392         ef->cip_1_expected_fdf = FIXED_RATE_SFC << 16;
1393
1394         err = allocate_capture_buffer(ef);
1395         if (err < 0)
1396                 return err;
1397
1398         ef->capture_data_received = false;
1399         ef->capture_iso_context = fw_iso_context_create
1400                 (ef->device->card, FW_ISO_CONTEXT_RECEIVE, CAPTURE_CHANNEL,
1401                  ef->device->max_speed, 4, first_capture_iso_callback, ef);
1402         if (IS_ERR(ef->capture_iso_context)) {
1403                 err = PTR_ERR(ef->capture_iso_context);
1404                 goto err_buffer;
1405         }
1406
1407         for (i = 0; i < ARRAY_SIZE(ef->capture_packets); ++i) {
1408                 err = queue_capture_packet(ef, i);
1409                 if (err < 0)
1410                         goto err_context;
1411         }
1412
1413         err = cmp_add_capture_connection(ef);
1414         if (err < 0)
1415                 goto err_context;
1416
1417         ef->fw_capture_running = true;
1418         err = fw_iso_context_start(ef->capture_iso_context, -1, 0,
1419                                    FW_ISO_CONTEXT_MATCH_TAG1);
1420         if (err < 0)
1421                 goto err_running;
1422
1423         return 0;
1424
1425 err_running:
1426         ef->fw_capture_running = false;
1427         cmp_remove_capture_connection(ef);
1428 err_context:
1429         fw_iso_context_destroy(ef->capture_iso_context);
1430 err_buffer:
1431         fw_iso_buffer_destroy(&ef->capture_iso_buffer, ef->device->card);
1432         return err;
1433 }
1434
1435 static void fw_capture_stop(struct echofire *ef)
1436 {
1437         ef->fw_capture_running = false;
1438         cmp_remove_capture_connection(ef);
1439         fw_iso_context_stop(ef->capture_iso_context);
1440         fw_iso_context_destroy(ef->capture_iso_context);
1441         fw_iso_buffer_destroy(&ef->capture_iso_buffer, ef->device->card);
1442 }
1443
1444 static int fw_capture_open(struct echofire *ef)
1445 {
1446         int err;
1447
1448         mutex_lock(&ef->mutex);
1449         if (ef->fw_capture_users == 0) {
1450                 err = fw_capture_start(ef);
1451                 if (err < 0) {
1452                         mutex_unlock(&ef->mutex);
1453                         return err;
1454                 }
1455         }
1456         ef->fw_capture_users++;
1457         mutex_unlock(&ef->mutex);
1458         return 0;
1459 }
1460
1461 static void fw_capture_close(struct echofire *ef)
1462 {
1463         mutex_lock(&ef->mutex);
1464         ef->fw_capture_users--;
1465         if (ef->fw_capture_users == 0)
1466                 fw_capture_stop(ef);
1467         mutex_unlock(&ef->mutex);
1468 }
1469
1470 static int init_substream(struct echofire *ef,
1471                           struct snd_pcm_substream *substream)
1472 {
1473         static const struct snd_pcm_hardware hardware = {
1474                 .info = SNDRV_PCM_INFO_MMAP |
1475                         SNDRV_PCM_INFO_MMAP_VALID |
1476                         SNDRV_PCM_INFO_BATCH |
1477                         SNDRV_PCM_INFO_INTERLEAVED |
1478                         SNDRV_PCM_INFO_BLOCK_TRANSFER |
1479                         SNDRV_PCM_INFO_SYNC_START |
1480                         SNDRV_PCM_INFO_FIFO_IN_FRAMES,
1481                 .formats = SNDRV_PCM_FMTBIT_S16 |
1482                            SNDRV_PCM_FMTBIT_S32,
1483                 .rate_min = FIXED_RATE,
1484                 .rate_max = FIXED_RATE,
1485                 .buffer_bytes_max = 32 * 1024 * 1024,
1486                 .period_bytes_min = 1,
1487                 .period_bytes_max = UINT_MAX,
1488                 .periods_min = 1,
1489                 .periods_max = UINT_MAX,
1490                 .fifo_size = 8,
1491         };
1492         int err;
1493
1494         substream->runtime->hw = hardware;
1495         substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(FIXED_RATE);
1496         /* substream->runtime->hw.fifo_size += TODO */
1497         substream->runtime->delay = substream->runtime->hw.fifo_size;
1498         err = snd_pcm_hw_constraint_minmax(substream->runtime,
1499                                            SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1500                                            500, 8192000);
1501         if (err < 0)
1502                 return err;
1503         err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
1504         if (err < 0)
1505                 return err;
1506         snd_pcm_set_sync(substream);
1507         return 0;
1508 }
1509
1510 static int pcm_playback_open(struct snd_pcm_substream *substream)
1511 {
1512         struct echofire *ef = substream->private_data;
1513         int err;
1514
1515         err = init_substream(ef, substream);
1516         if (err < 0)
1517                 return err;
1518         substream->runtime->hw.channels_min = ef->pcm_playback_channels;
1519         substream->runtime->hw.channels_max = ef->pcm_playback_channels;
1520
1521         err = fw_capture_open(ef);
1522         if (err < 0)
1523                 return err;
1524         err = fw_playback_open(ef);
1525         if (err < 0) {
1526                 fw_capture_close(ef);
1527                 return err;
1528         }
1529         return 0;
1530 }
1531
1532 static int pcm_capture_open(struct snd_pcm_substream *substream)
1533 {
1534         struct echofire *ef = substream->private_data;
1535         int err;
1536
1537         err = init_substream(ef, substream);
1538         if (err < 0)
1539                 return err;
1540         substream->runtime->hw.formats |= SNDRV_PCM_FMTBIT_S24_BE;
1541         substream->runtime->hw.channels_min = ef->pcm_capture_channels;
1542         substream->runtime->hw.channels_max = ef->pcm_capture_channels;
1543
1544         return fw_capture_open(ef);
1545 }
1546
1547 static int pcm_playback_close(struct snd_pcm_substream *substream)
1548 {
1549         struct echofire *ef = substream->private_data;
1550
1551         fw_playback_close(ef);
1552         fw_capture_close(ef);
1553         return 0;
1554 }
1555
1556 static int pcm_capture_close(struct snd_pcm_substream *substream)
1557 {
1558         struct echofire *ef = substream->private_data;
1559
1560         fw_capture_close(ef);
1561         return 0;
1562 }
1563
1564 static int pcm_hw_params(struct snd_pcm_substream *substream,
1565                          struct snd_pcm_hw_params *hw_params)
1566 {
1567         struct echofire *ef = substream->private_data;
1568
1569         if (ef->disconnect)
1570                 return -ENODEV;
1571         return snd_pcm_lib_alloc_vmalloc_buffer(substream,
1572                                                 params_buffer_bytes(hw_params));
1573 }
1574
1575 static int pcm_hw_free(struct snd_pcm_substream *substream)
1576 {
1577         return snd_pcm_lib_free_vmalloc_buffer(substream);
1578 }
1579
1580 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
1581 {
1582         struct echofire *ef = substream->private_data;
1583
1584         if (ef->disconnect)
1585                 return -ENODEV;
1586         if (!ef->fw_capture_running)
1587                 return -EIO;
1588
1589         ef->pcm_playback_period_pos = 0;
1590         ef->pcm_playback_buffer_pos = 0;
1591         return 0;
1592 }
1593
1594 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
1595 {
1596         struct echofire *ef = substream->private_data;
1597         int err;
1598
1599         err = wait_event_interruptible(ef->capture_data_wait,
1600                                        ef->capture_data_received ||
1601                                        !ef->fw_capture_running);
1602         if (err < 0)
1603                 return err;
1604         if (ef->disconnect)
1605                 return -ENODEV;
1606         if (!ef->fw_capture_running)
1607                 return -EIO;
1608
1609         ef->pcm_capture_period_pos = 0;
1610         ef->pcm_capture_buffer_pos = 0;
1611         return 0;
1612 }
1613
1614 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1615 {
1616         struct echofire *ef = substream->private_data;
1617         struct snd_pcm_substream *s;
1618         bool do_playback = false, do_capture = false, run;
1619
1620         if (ef->disconnect)
1621                 return -ENODEV;
1622
1623         switch (cmd) {
1624         case SNDRV_PCM_TRIGGER_START:
1625                 run = true;
1626                 break;
1627         case SNDRV_PCM_TRIGGER_STOP:
1628                 run = false;
1629                 break;
1630         default:
1631                 return -EINVAL;
1632         }
1633
1634         snd_pcm_group_for_each_entry(s, substream) {
1635                 if (s == ef->pcm_playback_substream) {
1636                         if (run && !ef->fw_playback_running)
1637                                 return -EIO;
1638                         do_playback = true;
1639                 } else if (s == ef->pcm_capture_substream) {
1640                         if (run && !ef->fw_capture_running)
1641                                 return -EIO;
1642                         do_capture = true;
1643                 }
1644         }
1645         if (do_playback)
1646                 snd_pcm_trigger_done(ef->pcm_playback_substream, substream);
1647         if (do_capture)
1648                 snd_pcm_trigger_done(ef->pcm_capture_substream, substream);
1649
1650         spin_lock(&ef->lock);
1651         if (do_playback)
1652                 ef->pcm_playback_running = run;
1653         if (do_capture)
1654                 ef->pcm_capture_running = run;
1655         spin_unlock(&ef->lock);
1656         return 0;
1657 }
1658
1659 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *subs)
1660 {
1661         struct echofire *ef = subs->private_data;
1662         unsigned long flags;
1663         unsigned int pos;
1664
1665         spin_lock_irqsave(&ef->lock, flags);
1666         pos = ef->pcm_playback_buffer_pos;
1667         spin_unlock_irqrestore(&ef->lock, flags);
1668         return pos;
1669 }
1670
1671 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *subs)
1672 {
1673         struct echofire *ef = subs->private_data;
1674         unsigned long flags;
1675         unsigned int pos;
1676
1677         spin_lock_irqsave(&ef->lock, flags);
1678         pos = ef->pcm_capture_buffer_pos;
1679         spin_unlock_irqrestore(&ef->lock, flags);
1680         return pos;
1681 }
1682
1683 static struct snd_pcm_ops pcm_playback_ops = {
1684         .open = pcm_playback_open,
1685         .close = pcm_playback_close,
1686         .ioctl = snd_pcm_lib_ioctl,
1687         .hw_params = pcm_hw_params,
1688         .hw_free = pcm_hw_free,
1689         .prepare = pcm_playback_prepare,
1690         .trigger = pcm_trigger,
1691         .pointer = pcm_playback_pointer,
1692         .page = snd_pcm_lib_get_vmalloc_page,
1693         .mmap = snd_pcm_lib_mmap_vmalloc,
1694 };
1695
1696 static struct snd_pcm_ops pcm_capture_ops = {
1697         .open = pcm_capture_open,
1698         .close = pcm_capture_close,
1699         .ioctl = snd_pcm_lib_ioctl,
1700         .hw_params = pcm_hw_params,
1701         .hw_free = pcm_hw_free,
1702         .prepare = pcm_capture_prepare,
1703         .trigger = pcm_trigger,
1704         .pointer = pcm_capture_pointer,
1705         .page = snd_pcm_lib_get_vmalloc_page,
1706 };
1707
1708 static int create_pcm_devices(struct echofire *ef)
1709 {
1710         struct snd_pcm *pcm;
1711         int err;
1712
1713         err = snd_pcm_new(ef->card, "Fireworks", 0, 1, 1, &pcm);
1714         if (err < 0)
1715                 return err;
1716         pcm->private_data = ef;
1717         snprintf(pcm->name, sizeof(pcm->name), "%s PCM", ef->card->shortname);
1718         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
1719         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
1720         ef->pcm_playback_substream =
1721                         pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1722         ef->pcm_capture_substream =
1723                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
1724         return 0;
1725 }
1726
1727 static int midi_output_open(struct snd_rawmidi_substream *substream)
1728 {
1729         struct echofire *ef = substream->rmidi->private_data;
1730         int err;
1731
1732         err = fw_capture_open(ef);
1733         if (err < 0)
1734                 return err;
1735         err = fw_playback_open(ef);
1736         if (err < 0) {
1737                 fw_capture_close(ef);
1738                 return err;
1739         }
1740         return 0;
1741 }
1742
1743 static int midi_output_close(struct snd_rawmidi_substream *substream)
1744 {
1745         struct echofire *ef = substream->rmidi->private_data;
1746
1747         fw_playback_close(ef);
1748         fw_capture_close(ef);
1749         return 0;
1750 }
1751
1752 static void midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1753 {
1754         struct echofire *ef = substream->rmidi->private_data;
1755         unsigned long flags;
1756
1757         spin_lock_irqsave(&ef->lock, flags);
1758         if (up)
1759                 __set_bit(substream->number, &ef->midi_output_running);
1760         else
1761                 __clear_bit(substream->number, &ef->midi_output_running);
1762         spin_unlock_irqrestore(&ef->lock, flags);
1763 }
1764
1765 static void midi_output_drain(struct snd_rawmidi_substream *substream)
1766 {
1767         msleep(4 + 1); /* FIXME: magic */
1768 }
1769
1770 static struct snd_rawmidi_ops midi_output_ops = {
1771         .open = midi_output_open,
1772         .close = midi_output_close,
1773         .trigger = midi_output_trigger,
1774         .drain = midi_output_drain,
1775 };
1776
1777 static int midi_input_open(struct snd_rawmidi_substream *substream)
1778 {
1779         struct echofire *ef = substream->rmidi->private_data;
1780
1781         return fw_capture_open(ef);
1782 }
1783
1784 static int midi_input_close(struct snd_rawmidi_substream *substream)
1785 {
1786         struct echofire *ef = substream->rmidi->private_data;
1787
1788         fw_capture_close(ef);
1789         return 0;
1790 }
1791
1792 static void midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1793 {
1794         struct echofire *ef = substream->rmidi->private_data;
1795         unsigned long flags;
1796
1797         spin_lock_irqsave(&ef->lock, flags);
1798         if (up)
1799                 __set_bit(substream->number, &ef->midi_input_running);
1800         else
1801                 __clear_bit(substream->number, &ef->midi_input_running);
1802         spin_unlock_irqrestore(&ef->lock, flags);
1803 }
1804
1805 static struct snd_rawmidi_ops midi_input_ops = {
1806         .open = midi_input_open,
1807         .close = midi_input_close,
1808         .trigger = midi_input_trigger,
1809 };
1810
1811 static void set_midi_substream_names(struct echofire *ef,
1812                                      struct snd_rawmidi_str *str)
1813 {
1814         struct snd_rawmidi_substream *subs;
1815
1816         if (str->substream_count > 1)
1817                 list_for_each_entry(subs, &str->substreams, list)
1818                         snprintf(subs->name, sizeof(subs->name),
1819                                  "%s MIDI %d",
1820                                  ef->card->shortname, subs->number + 1);
1821 }
1822
1823 static int create_midi_ports(struct echofire *ef)
1824 {
1825         struct snd_rawmidi *rmidi;
1826         struct snd_rawmidi_str *str;
1827         struct snd_rawmidi_substream *subs;
1828         int err;
1829
1830         err = snd_rawmidi_new(ef->card, "Fireworks", 0,
1831                               ef->midi_output_count, ef->midi_input_count,
1832                               &rmidi);
1833         if (err < 0)
1834                 return err;
1835         snprintf(rmidi->name, sizeof(rmidi->name),
1836                  "%s MIDI", ef->card->shortname);
1837         rmidi->private_data = ef;
1838         if (ef->midi_output_count) {
1839                 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
1840                 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
1841                                     &midi_output_ops);
1842                 str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
1843                 list_for_each_entry(subs, &str->substreams, list)
1844                         ef->midi_outputs[subs->number].substream = subs;
1845                 set_midi_substream_names(ef, str);
1846         }
1847         if (ef->midi_input_count) {
1848                 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
1849                 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
1850                                     &midi_input_ops);
1851                 str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
1852                 list_for_each_entry(subs, &str->substreams, list)
1853                         ef->midi_inputs[subs->number] = subs;
1854                 set_midi_substream_names(ef, str);
1855         }
1856         if (ef->midi_output_count && ef->midi_input_count)
1857                 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
1858         return 0;
1859 }
1860
1861 static int hw_caps_ctl_info(struct snd_kcontrol *ctl,
1862                             struct snd_ctl_elem_info *info)
1863 {
1864         info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1865         info->count = sizeof(struct efc_hardware_info);
1866         return 0;
1867 }
1868
1869 static int hw_caps_ctl_get(struct snd_kcontrol *ctl,
1870                            struct snd_ctl_elem_value *value)
1871 {
1872         struct echofire *ef = ctl->private_data;
1873
1874         return echofire_hwinfo_get_caps(ef, (struct efc_hardware_info *)
1875                                         value->value.bytes.data);
1876 }
1877
1878 static int hw_info_poll_ctl_info(struct snd_kcontrol *ctl,
1879                                  struct snd_ctl_elem_info *info)
1880 {
1881         struct echofire *ef = ctl->private_data;
1882
1883         info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1884         info->count = 4 * (9 + ef->polled_meters_count);
1885         return 0;
1886 }
1887
1888 static int hw_info_poll_ctl_get(struct snd_kcontrol *ctl,
1889                                 struct snd_ctl_elem_value *value)
1890 {
1891         struct echofire *ef = ctl->private_data;
1892
1893         return echofire_get_polled_values(ef, (struct efc_polled_values *)
1894                                           value->value.bytes.data);
1895 }
1896
1897 static int hw_ctrl_identify_ctl_tlv(struct snd_kcontrol *ctl, int op,
1898                                     unsigned int size, unsigned int __user *tlv)
1899 {
1900         struct echofire *ef = ctl->private_data;
1901
1902         return echofire_identify(ef);
1903 }
1904
1905 static const struct snd_kcontrol_new mixer_controls[] = {
1906         {
1907                 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1908                 .name = "Hardware Capabilities",
1909                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1910                 .info = hw_caps_ctl_info,
1911                 .get = hw_caps_ctl_get,
1912         },
1913         {
1914                 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1915                 .name = "Polled Values",
1916                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1917                           SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1918                 .info = hw_info_poll_ctl_info,
1919                 .get = hw_info_poll_ctl_get,
1920         },
1921 };
1922 static const struct snd_kcontrol_new identify_ctl = {
1923         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1924         .name = "Identify",
1925         .access = SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
1926                   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
1927         .info = snd_ctl_boolean_mono_info,
1928         .tlv = { .c = hw_ctrl_identify_ctl_tlv },
1929 };
1930
1931 static int create_mixer_controls(struct echofire *ef)
1932 {
1933         unsigned int i;
1934         int err;
1935
1936         for (i = 0; i < ARRAY_SIZE(mixer_controls); ++i) {
1937                 err = snd_ctl_add(ef->card,
1938                                   snd_ctl_new1(&mixer_controls[i], ef));
1939                 if (err < 0)
1940                         return err;
1941         }
1942         if (true /* TODO: which models? */) {
1943                 err = snd_ctl_add(ef->card, snd_ctl_new1(&identify_ctl, ef));
1944                 if (err < 0)
1945                         return err;
1946         }
1947         return 0;
1948 }
1949
1950 static int __devinit get_hardware_info(struct echofire *ef)
1951 {
1952         struct efc_hardware_info *hwinfo;
1953         unsigned int version;
1954         char version_str[12];
1955         int err;
1956
1957         hwinfo = kmalloc(sizeof(*hwinfo), GFP_KERNEL);
1958         if (!hwinfo)
1959                 return -ENOMEM;
1960
1961         err = echofire_hwinfo_get_caps(ef, hwinfo);
1962         if (err < 0)
1963                 goto error_out;
1964
1965         /* ensure the strings are zero-terminated */
1966         hwinfo->vendor_name[sizeof(hwinfo->vendor_name) - 1] = 0;
1967         hwinfo->model_name[sizeof(hwinfo->model_name) - 1] = 0;
1968
1969         ef->midi_output_count = be32_to_cpu(hwinfo->nb_midi_out);
1970         ef->midi_input_count = be32_to_cpu(hwinfo->nb_midi_in);
1971         ef->pcm_capture_channels =
1972                                 be32_to_cpu(hwinfo->nb_1394_capture_channels);
1973         ef->pcm_playback_channels =
1974                                 be32_to_cpu(hwinfo->nb_1394_playback_channels);
1975
1976         if (ef->midi_output_count > MAX_MIDI_OUTPUTS ||
1977             ef->midi_input_count > MAX_MIDI_INPUTS ||
1978             ef->pcm_capture_channels < 1 || ef->pcm_capture_channels > 100 ||
1979             ef->pcm_playback_channels < 1 || ef->pcm_playback_channels > 100) {
1980                 ef_err(ef, "invalid value in hardware information\n");
1981                 err = -ENXIO;
1982                 goto error_out;
1983         }
1984
1985         strcpy(ef->card->driver, "Fireworks");
1986         strcpy(ef->card->shortname, hwinfo->model_name);
1987         version = be32_to_cpu(hwinfo->arm_version);
1988         err = sprintf(version_str, "%u.%u",
1989                       (version >> 24) & 0xff, (version >> 8) & 0xff);
1990         if (version & 0xff)
1991                 sprintf(version_str + err, ".%u", version & 0xff);
1992         snprintf(ef->card->longname, sizeof(ef->card->longname),
1993                  "%s %s v%s, GUID %08x%08x at %s, S%d",
1994                  hwinfo->vendor_name, hwinfo->model_name, version_str,
1995                  be32_to_cpu(hwinfo->guid_hi), be32_to_cpu(hwinfo->guid_lo),
1996                  dev_name(&ef->unit->device), 100 << ef->device->max_speed);
1997         strcpy(ef->card->mixername, hwinfo->model_name);
1998
1999 error_out:
2000         kfree(hwinfo);
2001         return err;
2002 }
2003
2004 static void echofire_update(struct fw_unit *unit)
2005 {
2006         struct snd_card *card = dev_get_drvdata(&unit->device);
2007         struct echofire *ef = card->private_data;
2008         unsigned long flags;
2009
2010         fcp_handle_bus_reset(ef);
2011         mutex_lock(&ef->mutex);
2012         if (ef->fw_capture_running) {
2013                 if (cmp_restore_capture_connection(ef) < 0) {
2014                         ef->fw_capture_running = false;
2015                         ef->fw_playback_running = false;
2016                         /* TODO: stop pcm+midi streams */
2017                 }
2018         }
2019         if (ef->fw_playback_running) {
2020                 spin_lock_irqsave(&ef->lock, flags);
2021                 ef->playback_cip_0 &= ~(0x3f << 24);
2022                 ef->playback_cip_0 |= (ef->device->node_id & 0x3f) << 24;
2023                 spin_unlock_irqrestore(&ef->lock, flags);
2024                 if (cmp_restore_playback_connection(ef) < 0) {
2025                         ef->fw_playback_running = false;
2026                         /* TODO: stop pcm+midi streams */
2027                 }
2028         }
2029         mutex_unlock(&ef->mutex);
2030 }
2031
2032 static bool __devinit match_echofire_device_name(struct fw_unit *unit)
2033 {
2034         static const char *const models[] = {
2035                 /* Echo Digital Audio */
2036                 "AudioFire2",
2037                 "AudioFire4",
2038                 "AudioFire8",
2039                 "AudioFire8a",
2040                 "AudioFirePre8",
2041                 "AudioFire12",
2042                 "Fireworks8",
2043                 "Fireworks HDMI",
2044                 /* Mackie */
2045                 "Onyx 400F",
2046                 "Onyx 1200F",
2047                 /* Gibson */
2048                 "RIP",
2049                 "Audiopunk",
2050                 "Goldtop",
2051         };
2052         char name[16];
2053         unsigned int i;
2054
2055         if (fw_csr_string(unit->directory, CSR_MODEL, name, sizeof(name)) < 0)
2056                 return false;
2057         for (i = 0; i < ARRAY_SIZE(models); i++)
2058                 if (!strcasecmp(name, models[i]))
2059                         return true;
2060         return false;
2061 }
2062
2063 static void echofire_card_free(struct snd_card *card)
2064 {
2065         struct echofire *ef = card->private_data;
2066
2067         if (ef->fcp_response_handler.offset)
2068                 fw_core_remove_address_handler(&ef->fcp_response_handler);
2069         if (ef->card_index >= 0) {
2070                 mutex_lock(&devices_mutex);
2071                 devices_used &= ~(1 << ef->card_index);
2072                 mutex_unlock(&devices_mutex);
2073         }
2074         mutex_destroy(&ef->fcp_mutex);
2075         mutex_destroy(&ef->mutex);
2076 }
2077
2078 static int __devinit echofire_probe(struct device *dev)
2079 {
2080         static const struct fw_address_region fcp_response_region = {
2081                 .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
2082                 .end = CSR_REGISTER_BASE + CSR_FCP_END,
2083         };
2084         struct fw_unit *unit = fw_unit(dev);
2085         int card_index;
2086         struct snd_card *card;
2087         struct echofire *ef;
2088         u32 clock_params[3];
2089         unsigned int i;
2090         int err;
2091
2092         if (!match_echofire_device_name(unit))
2093                 return -ENODEV;
2094
2095         mutex_lock(&devices_mutex);
2096         for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
2097                 if (!(devices_used & (1 << card_index)) && enable[card_index])
2098                         break;
2099         if (card_index >= SNDRV_CARDS) {
2100                 err = -ENOENT;
2101                 goto error_mutex;
2102         }
2103         err = snd_card_create(index[card_index], id[card_index], THIS_MODULE,
2104                               sizeof(*ef), &card);
2105         if (err < 0)
2106                 goto error_mutex;
2107         card->private_free = echofire_card_free;
2108
2109         ef = card->private_data;
2110         ef->card = card;
2111         ef->device = fw_parent_device(unit);
2112         ef->unit = unit;
2113         ef->card_index = -1;
2114         mutex_init(&ef->mutex);
2115         spin_lock_init(&ef->lock);
2116         mutex_init(&ef->fcp_mutex);
2117         spin_lock_init(&ef->fcp_lock);
2118         init_waitqueue_head(&ef->fcp_response_wait);
2119         init_waitqueue_head(&ef->capture_data_wait);
2120         ef->last_opcr = 1 << 31;
2121         ef->last_ipcr = 1 << 31;
2122
2123         ef->fcp_response_handler.length = 0x200;
2124         ef->fcp_response_handler.address_callback = fcp_response_callback;
2125         ef->fcp_response_handler.callback_data = ef;
2126         err = fw_core_add_address_handler(&ef->fcp_response_handler,
2127                                           &fcp_response_region);
2128         if (err < 0) {
2129                 ef_err(ef, "cannot register FCP response handler\n");
2130                 goto error_card;
2131         }
2132
2133         snd_card_set_dev(card, dev);
2134
2135         err = get_hardware_info(ef);
2136         if (err < 0)
2137                 goto error_card;
2138
2139         /* hack */
2140         clock_params[0] = EFC_CMD_HW_CLOCK_INTERNAL;
2141         clock_params[1] = FIXED_RATE;
2142         clock_params[2] = 0;
2143         err = efc_transaction(ef, EFC_CAT_HARDWARE_CONTROL, EFC_CMD_HWCTRL_SET_CLOCK,
2144                               clock_params, 3, NULL, 0);
2145         if (err < 0)
2146                 goto error_card;
2147         ef->syt_interval = 8;
2148         for (i = 0; i < MAX_MIDI_OUTPUTS; ++i)
2149                 ef->midi_outputs[i].fifo_max =
2150                                         (MIDI_FIFO_SIZE - 1) * FIXED_RATE;
2151
2152         err = determine_polled_meters_count(ef);
2153         if (err < 0)
2154                 goto error_card;
2155
2156         if (ef->midi_output_count || ef->midi_input_count) {
2157                 err = create_midi_ports(ef);
2158                 if (err < 0)
2159                         goto error_card;
2160         }
2161
2162         err = create_pcm_devices(ef);
2163         if (err < 0)
2164                 goto error_card;
2165
2166         err = create_mixer_controls(ef);
2167         if (err < 0)
2168                 goto error_card;
2169
2170         err = snd_card_register(card);
2171         if (err < 0)
2172                 goto error_card;
2173
2174         dev_set_drvdata(dev, card);
2175         devices_used |= 1 << card_index;
2176         ef->card_index = card_index;
2177         mutex_unlock(&devices_mutex);
2178         return 0;
2179
2180 error_card:
2181         mutex_unlock(&devices_mutex);
2182         snd_card_free(card);
2183         return err;
2184
2185 error_mutex:
2186         mutex_unlock(&devices_mutex);
2187         return err;
2188 }
2189
2190 static int __devexit echofire_remove(struct device *dev)
2191 {
2192         struct snd_card *card = dev_get_drvdata(dev);
2193         struct echofire *ef = card->private_data;
2194
2195         ef->disconnect = true;
2196         snd_card_disconnect(card);
2197         /* TODO: make sure there are no pending fw requests */
2198         snd_card_free_when_closed(card);
2199         return 0;
2200 }
2201
2202 #define VENDOR_GIBSON                   0x00075b
2203 #define  MODEL_GIBSON_RIP               0x00afb2
2204 /* #define  MODEL_GIBSON_GOLDTOP        0x?????? */
2205
2206 #define VENDOR_LOUD                     0x000ff2
2207 #define  MODEL_MACKIE_400F              0x00400f
2208 #define  MODEL_MACKIE_1200F             0x01200f
2209
2210 #define VENDOR_ECHO_DIGITAL_AUDIO       0x001486
2211 #define  MODEL_ECHO_AUDIOFIRE_2         0x000af2
2212 #define  MODEL_ECHO_AUDIOFIRE_4         0x000af4
2213 #define  MODEL_ECHO_AUDIOFIRE_8         0x000af8
2214 /* #define  MODEL_ECHO_AUDIOFIRE_8A     0x?????? */
2215 /* #define  MODEL_ECHO_AUDIOFIRE_PRE8   0x?????? */
2216 #define  MODEL_ECHO_AUDIOFIRE_12        0x00af12
2217 #define  MODEL_ECHO_FIREWORKS_8         0x0000f8
2218 #define  MODEL_ECHO_FIREWORKS_HDMI      0x00afd1
2219
2220 #define SPECIFIER_1394TA                0x00a02d
2221
2222 static const struct ieee1394_device_id echofire_id_table[] = {
2223         {
2224                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
2225                                IEEE1394_MATCH_SPECIFIER_ID,
2226                 .vendor_id = VENDOR_ECHO_DIGITAL_AUDIO,
2227                 .specifier_id = SPECIFIER_1394TA,
2228         },
2229         {
2230                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
2231                                IEEE1394_MATCH_SPECIFIER_ID,
2232                 .vendor_id = VENDOR_GIBSON,
2233                 .specifier_id = SPECIFIER_1394TA,
2234         },
2235         {
2236                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
2237                                IEEE1394_MATCH_MODEL_ID,
2238                 .vendor_id = VENDOR_LOUD,
2239                 .model_id = MODEL_MACKIE_400F,
2240         },
2241         {
2242                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
2243                                IEEE1394_MATCH_MODEL_ID,
2244                 .vendor_id = VENDOR_LOUD,
2245                 .model_id = MODEL_MACKIE_1200F,
2246         },
2247         {}
2248 };
2249 MODULE_DEVICE_TABLE(ieee1394, echofire_id_table);
2250
2251 static struct fw_driver echofire_driver = {
2252         .driver = {
2253                 .owner = THIS_MODULE,
2254                 .name = "snd-fireworks",
2255                 .bus = &fw_bus_type,
2256                 .probe = echofire_probe,
2257                 .remove = __devexit_p(echofire_remove),
2258         },
2259         .update = echofire_update,
2260         .id_table = echofire_id_table,
2261 };
2262
2263 static int __init alsa_card_echofire_init(void)
2264 {
2265         return driver_register(&echofire_driver.driver);
2266 }
2267
2268 static void __exit alsa_card_echofire_exit(void)
2269 {
2270         driver_unregister(&echofire_driver.driver);
2271         mutex_destroy(&devices_mutex);
2272 }
2273
2274 module_init(alsa_card_echofire_init);
2275 module_exit(alsa_card_echofire_exit);