2 * fireworks.c - driver for Firewire devices from Echo Digital Audio
4 * Copyright (c) 2009-2010 Clemens Ladisch
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.
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.
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/>.
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"
30 MODULE_DESCRIPTION("Echo Fireworks driver");
31 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
32 MODULE_LICENSE("GPL v2");
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;
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");
54 #define PLAYBACK_QUEUE_LENGTH 40
55 #define CAPTURE_QUEUE_LENGTH 40
57 #define PLAYBACK_CHANNEL 17 /* XXX */
58 #define CAPTURE_CHANNEL 23 /* XXX */
60 #define MIDI_FIFO_SIZE 4096
62 #define MAX_MIDI_OUTPUTS 2
63 #define MAX_MIDI_INPUTS 2
65 #define FIXED_RATE 44100 /* XXX */
66 #define FIXED_RATE_SFC 1
68 #define MY_ERV_PENDING 0xffffffff
69 #define MY_ERV_BUS_RESET 0xfffffffe
71 #define EFC_FLAG_VERSION_1 0x1
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.
79 struct snd_card *card;
80 struct fw_device *device;
88 struct mutex fcp_mutex;
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;
99 unsigned int syt_interval;
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;
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;
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;
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;
130 unsigned int midi_output_count;
131 unsigned long midi_output_running;
133 struct snd_rawmidi_substream *substream;
136 } midi_outputs[MAX_MIDI_OUTPUTS];
138 unsigned int midi_input_count;
139 unsigned long midi_input_running;
140 struct snd_rawmidi_substream *midi_inputs[MAX_MIDI_INPUTS];
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;
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;
154 unsigned polled_meters_count;
157 #define ef_err(ef, format, arg...) dev_err(&(ef)->device->device, format, ##arg)
159 static DEFINE_MUTEX(devices_mutex);
160 static unsigned int devices_used;
162 static const char *rcode_string(unsigned int rcode)
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",
177 if (rcode < ARRAY_SIZE(names) && names[rcode])
183 static const char *erv_string(unsigned int return_value)
185 static const char *const names[] = {
204 if (return_value < ARRAY_SIZE(names))
205 return names[return_value];
206 else if (return_value == MY_ERV_PENDING)
207 return "no response";
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,
219 struct echofire *ef = callback_data;
220 int device_generation;
221 const __be32 *quadlets;
222 const struct efc_header *header;
226 if (offset != CSR_REGISTER_BASE + CSR_FCP_RESPONSE)
229 device_generation = ef->device->generation;
231 if (generation != device_generation ||
232 source != ef->device->node_id)
235 if (unlikely(dump_fcp_packets))
236 print_hex_dump_bytes("FCP response ", DUMP_PREFIX_OFFSET,
241 quadlets = (__force __be32 *)data;
244 * cts=0, ACCEPTED=9, unit=ff, VENDOR-DEPENDENT=00,
245 * company_ID=000000, padding=0000
247 if (quadlets[0] != cpu_to_be32(0x09ff0000) ||
248 quadlets[1] != cpu_to_be32(0x00000000))
250 header = (struct efc_header *)&quadlets[2];
251 count = be32_to_cpu(header->length);
252 if (count < 6 || count > 126 || length < 8 + 4 * count)
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) {
260 count = min(count, ef->response_quadlets);
261 memcpy(ef->response_data, &quadlets[8], count * 4);
262 ef->response_quadlets = count;
264 wake_up(&ef->fcp_response_wait);
266 spin_unlock_irqrestore(&ef->fcp_lock, flags);
269 static void fcp_handle_bus_reset(struct echofire *ef)
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);
276 spin_unlock_irq(&ef->fcp_lock);
279 /* buf must be DMA-able */
280 static int submit_fcp_command(struct echofire *ef,
281 const void *buf, size_t length)
283 unsigned int tries = 0;
284 int generation, rcode;
287 if (unlikely(dump_fcp_packets))
288 print_hex_dump_bytes("FCP command ", DUMP_PREFIX_OFFSET,
290 generation = ef->device->generation;
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);
301 case RCODE_CANCELLED:
305 ef_err(ef, "FCP write failed: %#x: %s\n",
306 rcode, rcode_string(rcode));
310 case RCODE_GENERATION:
314 ef_err(ef, "FCP write failed: %#x: %s\n",
315 rcode, rcode_string(rcode));
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)
326 unsigned int cmdbuf_bytes;
328 struct efc_header *header;
329 unsigned int tries = 0, i;
332 cmdbuf_bytes = 32 + param_count * 4;
333 cmdbuf = kmalloc(cmdbuf_bytes, GFP_KERNEL);
338 * cts=0, CONTROL=0, unit=ff, VENDOR-DEPENDENT=00,
339 * company_ID=000000, padding=0000
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]);
352 mutex_lock(&ef->fcp_mutex);
354 spin_lock_irq(&ef->fcp_lock);
356 ef->expected_response_category = cpu_to_be32(category);
357 ef->expected_response_command = cpu_to_be32(command);
358 ef->response_data = response;
361 header->seqnum = cpu_to_be32(ef->sequence_number++);
362 ef->return_value = MY_ERV_PENDING;
363 ef->response_quadlets = response_quadlets;
365 spin_unlock_irq(&ef->fcp_lock);
367 err = submit_fcp_command(ef, cmdbuf, cmdbuf_bytes);
371 wait_event_timeout(ef->fcp_response_wait,
372 ef->return_value != MY_ERV_PENDING,
373 msecs_to_jiffies(120));
375 spin_lock_irq(&ef->fcp_lock);
376 if (ef->return_value == ERV_OK) {
377 if (ef->response_quadlets == response_quadlets) {
380 ef_err(ef, "[%u/%u] FCP response too short\n",
385 } else if (ef->return_value == MY_ERV_BUS_RESET) {
386 spin_unlock_irq(&ef->fcp_lock);
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) {
394 ef_err(ef, "command [%u/%u] failed: %#x: %s\n",
395 category, command, ef->return_value,
396 erv_string(ef->return_value));
401 ef_err(ef, "command [%u/%u] failed: %#x: %s\n",
402 category, command, ef->return_value,
403 erv_string(ef->return_value));
408 ef->response_quadlets = 0;
409 spin_unlock_irq(&ef->fcp_lock);
412 mutex_unlock(&ef->fcp_mutex);
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)
422 return _efc_transaction(ef, 0, category, command, parameters,
423 param_count, response, response_quadlets);
426 static int echofire_hwinfo_get_caps(struct echofire *ef,
427 struct efc_hardware_info *hwinfo)
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);
435 static int determine_polled_meters_count(struct echofire *ef)
437 struct efc_polled_values values;
440 err = efc_transaction(ef, EFC_CAT_HARDWARE_INFO,
441 EFC_CMD_HW_GET_POLLED,
442 NULL, 0, &values, 9);
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");
454 static int echofire_get_polled_values(struct echofire *ef,
455 struct efc_polled_values *values)
457 return efc_transaction(ef, EFC_CAT_HARDWARE_INFO,
458 EFC_CMD_HW_GET_POLLED,
459 NULL, 0, values, 9 + ef->polled_meters_count);
462 static int echofire_identify(struct echofire *ef)
464 return efc_transaction(ef, EFC_CAT_HARDWARE_CONTROL,
465 EFC_CMD_HWCTRL_IDENTIFY, NULL, 0, NULL, 0);
468 static int cmp_add_playback_connection(struct echofire *ef)
471 unsigned int old, new, try, rcode;
474 buf = kmalloc(8, GFP_KERNEL);
478 /* TODO: allocate channel and bandwidth */
480 for (try = 0; try < 5; ++try) {
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;
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),
495 case RCODE_GENERATION:
498 if (be32_to_cpup(buf) == old) {
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");
512 ef_err(ef, "iPCR modification failed: %#x: %s\n",
513 rcode, rcode_string(rcode));
520 static int cmp_restore_playback_connection(struct echofire *ef)
523 unsigned int old, new, try, rcode;
526 buf = kmalloc(8, GFP_KERNEL);
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;
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),
544 case RCODE_GENERATION:
545 /* try again later */
549 if (be32_to_cpup(buf) == old) {
554 old = be32_to_cpup(buf);
555 if ((old & 0x003f0000) != (new & 0x003f0000)) {
556 ef_err(ef, "channel number changed\n");
560 if ((old & 0x7f000000) == 0x3f000000) {
561 ef_err(ef, "too many connections\n");
567 ef_err(ef, "iPCR modification failed: %#x: %s\n",
568 rcode, rcode_string(rcode));
575 static int cmp_remove_playback_connection(struct echofire *ef)
578 unsigned int old, new, try, rcode;
581 buf = kmalloc(8, GFP_KERNEL);
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;
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),
599 case RCODE_GENERATION:
603 if (be32_to_cpup(buf) == old) {
605 /* TODO: deallocate channel and bandwidth */
609 old = be32_to_cpup(buf);
610 if ((old & (0x3f << 24)) == 0) {
611 /* somebody else broke our connection */
618 ef_err(ef, "iPCR modification failed: %#x: %s\n",
619 rcode, rcode_string(rcode));
626 static int cmp_add_capture_connection(struct echofire *ef)
629 unsigned int old, new, try, rcode;
632 buf = kmalloc(8, GFP_KERNEL);
636 /* TODO: allocate channel and bandwidth */
638 for (try = 0; try < 5; ++try) {
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;
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),
654 case RCODE_GENERATION:
657 if (be32_to_cpup(buf) == old) {
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");
671 ef_err(ef, "oPCR modification failed: %#x: %s\n",
672 rcode, rcode_string(rcode));
679 static int cmp_restore_capture_connection(struct echofire *ef)
682 unsigned int old, new, try, rcode;
685 buf = kmalloc(8, GFP_KERNEL);
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;
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),
703 case RCODE_GENERATION:
704 /* try again later */
708 if (be32_to_cpup(buf) == old) {
713 old = be32_to_cpup(buf);
714 if ((old & 0x003f0000) != (new & 0x003f0000)) {
715 ef_err(ef, "channel number changed\n");
719 if ((old & 0x7f000000) == 0x3f000000) {
720 ef_err(ef, "too many connections\n");
726 ef_err(ef, "oPCR modification failed: %#x: %s\n",
727 rcode, rcode_string(rcode));
734 static int cmp_remove_capture_connection(struct echofire *ef)
737 unsigned int old, new, try, rcode;
740 buf = kmalloc(8, GFP_KERNEL);
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;
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),
758 case RCODE_GENERATION:
762 if (be32_to_cpup(buf) == old) {
764 /* TODO: deallocate channel and bandwidth */
768 old = be32_to_cpup(buf);
769 if ((old & (0x3f << 24)) == 0) {
770 /* somebody else broke our connection */
777 ef_err(ef, "oPCR modification failed: %#x: %s\n",
778 rcode, rcode_string(rcode));
785 static void playback_iso_callback(struct fw_iso_context *context,
786 u32 cycle, size_t header_length,
787 void *header, void *data)
789 /* playback queueing is handled by capture callback */
792 static int queue_skip_packet(struct echofire *ef)
794 struct fw_iso_packet packet;
796 packet.payload_length = 0;
797 packet.interrupt = 0;
801 packet.header_length = 0;
803 return fw_iso_context_queue(ef->playback_iso_context, &packet, NULL, 0);
806 static int allocate_playback_buffer(struct echofire *ef)
808 unsigned int packet_size, packets_per_page, pages;
809 unsigned int i, page_index, offset_in_page;
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))
817 pages = DIV_ROUND_UP(ARRAY_SIZE(ef->playback_packets),
819 err = fw_iso_buffer_init(&ef->playback_iso_buffer, ef->device->card,
820 pages, DMA_TO_DEVICE);
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]);
828 fw_iso_buffer_destroy(&ef->playback_iso_buffer,
830 ef_err(ef, "DMA page has no kernel virtual address\n");
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;
838 ef->playback_packet_index = 0;
842 static inline unsigned int cycles_add(unsigned int cycles, unsigned int add)
845 if ((cycles & 0x1fff) >= 8000)
846 cycles += (1 << 13) - 8000;
850 static int fw_playback_start(struct echofire *ef)
852 unsigned int start_time;
856 err = wait_event_interruptible(ef->capture_data_wait,
857 ef->capture_data_received ||
858 !ef->fw_capture_running);
863 if (!ef->fw_capture_running)
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;
874 err = allocate_playback_buffer(ef);
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);
886 /* we need at least one queued packet when starting */
887 err = queue_skip_packet(ef);
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);
899 err = cmp_add_playback_connection(ef);
906 ef->fw_playback_running = false;
908 fw_iso_context_destroy(ef->playback_iso_context);
910 fw_iso_buffer_destroy(&ef->playback_iso_buffer, ef->device->card);
914 static void fw_playback_stop(struct echofire *ef)
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);
923 static int fw_playback_open(struct echofire *ef)
927 mutex_lock(&ef->mutex);
928 if (ef->fw_playback_users == 0) {
929 err = fw_playback_start(ef);
931 mutex_unlock(&ef->mutex);
935 ef->fw_playback_users++;
936 mutex_unlock(&ef->mutex);
940 static void fw_playback_close(struct echofire *ef)
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);
949 static void fill_midi_quadlets(struct echofire *ef, void *data,
950 unsigned int data_blocks)
952 unsigned int fw_frame_size, dbc, ports, i, port;
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;
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)
970 if (!test_bit(port, &ef->midi_output_running))
972 if (snd_rawmidi_transmit(ef->midi_outputs[port].substream,
974 __clear_bit(port, &ef->midi_output_running);
977 ef->midi_outputs[port].fifo_filled += FIXED_RATE;
978 *dst = cpu_to_be32(0x81000000) | cpu_to_be32(byte << 16);
982 static bool fill_playback_packet(struct echofire *ef, void *data,
983 unsigned int data_blocks)
985 struct snd_pcm_runtime *runtime;
986 unsigned int channels, pcm_frame_size, fw_frame_size;
987 unsigned int i, c, remaining_frames;
991 channels = ef->pcm_playback_channels;
992 pcm_frame_size = 4 * channels;
993 fw_frame_size = ef->playback_audio_frame_bytes;
995 if (ef->midi_output_count)
996 fill_midi_quadlets(ef, data + pcm_frame_size, data_blocks);
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);
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);
1022 if (!remaining_frames)
1023 src = runtime->dma_area;
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);
1036 if (!remaining_frames)
1037 src = runtime->dma_area;
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;
1054 static int queue_playback_packet(struct echofire *ef, u32 cip1,
1055 unsigned int data_blocks)
1057 struct fw_iso_packet packet;
1058 unsigned int index, offset;
1060 bool period_elapsed;
1063 index = ef->playback_packet_index;
1064 offset = ef->playback_packet_offset[index];
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);
1070 period_elapsed = fill_playback_packet(ef, data + 2,
1072 packet.payload_length =
1073 8 + data_blocks * ef->playback_audio_frame_bytes;
1075 period_elapsed = false;
1076 packet.payload_length = 8;
1078 packet.interrupt = 1;
1082 packet.header_length = 0;
1084 err = fw_iso_context_queue(ef->playback_iso_context, &packet,
1085 &ef->playback_iso_buffer, offset);
1089 if (unlikely(dump_tx_packets > 0)) {
1091 print_hex_dump_bytes("iso tx ", DUMP_PREFIX_OFFSET,
1092 data, packet.payload_length);
1095 ef->playback_dbc += data_blocks;
1097 if (++index >= ARRAY_SIZE(ef->playback_packets))
1099 ef->playback_packet_index = index;
1101 return period_elapsed ? 1 : 0;
1104 static int queue_capture_packet(struct echofire *ef, unsigned int index)
1106 struct fw_iso_packet packet;
1108 packet.payload_length = ef->capture_packet_bytes_max;
1109 packet.interrupt = 1;
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]);
1119 static bool handle_capture_pcm_data(struct echofire *ef,
1120 void *data, unsigned int data_blocks)
1122 struct snd_pcm_runtime *runtime;
1123 unsigned int channels, pcm_frame_size, fw_frame_size;
1124 unsigned int i, c, remaining_frames;
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;
1145 if (!remaining_frames)
1146 dst = runtime->dma_area;
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;
1158 if (!remaining_frames)
1159 dst = runtime->dma_area;
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;
1168 if (!remaining_frames)
1169 dst = runtime->dma_area;
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;
1186 static void handle_capture_midi_data(struct echofire *ef,
1187 void *data, unsigned int length)
1189 unsigned int port, step;
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))
1198 for (m = data + 4 * port; m + 3 < end; m += step) {
1199 if (m[0] < 0x81 || m[0] > 0x83)
1201 snd_rawmidi_receive(ef->midi_inputs[port],
1202 &m[1], m[0] - 0x80);
1207 static void capture_cip_header_error(struct echofire *ef,
1208 __be32 cip0_be, u32 cip1)
1210 u32 cip0 = be32_to_cpu(cip0_be);
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");
1239 static void capture_iso_callback(struct fw_iso_context *context,
1240 u32 cycle, size_t header_length,
1241 void *header, void *data)
1243 struct echofire *ef = data;
1244 __be32 *packet_data;
1245 unsigned int length, cip1, data_blocks;
1247 unsigned long flags;
1248 bool period_elapsed, playback_period_elapsed = false;
1251 packet_data = ef->capture_packets[ef->capture_packet_index];
1252 prefetch(packet_data);
1253 length = be32_to_cpup((__be32 *)header) >> 16;
1255 if (unlikely(!ef->fw_capture_running))
1258 if (unlikely(dump_rx_packets > 0)) {
1260 print_hex_dump_bytes("iso rx ", DUMP_PREFIX_OFFSET,
1261 packet_data, length);
1264 if (unlikely(length < 8)) {
1269 data_blocks = (length - 8) / ef->capture_audio_frame_bytes;
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
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);
1287 spin_lock_irqsave(&ef->lock, flags);
1288 if (ef->pcm_capture_running && data_blocks > 0)
1290 handle_capture_pcm_data(ef, packet_data, data_blocks);
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);
1302 playback_period_elapsed = err > 0;
1305 spin_unlock_irqrestore(&ef->lock, flags);
1307 err = queue_capture_packet(ef, ef->capture_packet_index);
1308 if (unlikely(err < 0)) {
1309 ef_err(ef, "packet queueing error: %d", err);
1311 ef->fw_capture_running = false;
1312 /* TODO: stop pcm+midi streams */
1316 ef->capture_packet_index++;
1317 if (ef->capture_packet_index >= ARRAY_SIZE(ef->capture_packets))
1318 ef->capture_packet_index = 0;
1320 if (playback_period_elapsed)
1321 snd_pcm_period_elapsed(ef->pcm_playback_substream);
1323 snd_pcm_period_elapsed(ef->pcm_capture_substream);
1326 static void first_capture_iso_callback(struct fw_iso_context *context,
1327 u32 cycle, size_t header_length,
1328 void *header, void *data)
1330 struct echofire *ef = data;
1331 unsigned int length;
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);
1339 capture_iso_callback(context, cycle, header_length, header, data);
1342 static int allocate_capture_buffer(struct echofire *ef)
1344 unsigned int packet_size, packets_per_page, pages;
1345 unsigned int i, page_index, offset_in_page;
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))
1353 pages = DIV_ROUND_UP(ARRAY_SIZE(ef->capture_packets),
1355 err = fw_iso_buffer_init(&ef->capture_iso_buffer, ef->device->card,
1356 pages, DMA_FROM_DEVICE);
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]);
1364 fw_iso_buffer_destroy(&ef->capture_iso_buffer,
1366 ef_err(ef, "DMA page has no kernel virtual address\n");
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;
1374 ef->capture_packet_index = 0;
1378 static int fw_capture_start(struct echofire *ef)
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;
1394 err = allocate_capture_buffer(ef);
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);
1407 for (i = 0; i < ARRAY_SIZE(ef->capture_packets); ++i) {
1408 err = queue_capture_packet(ef, i);
1413 err = cmp_add_capture_connection(ef);
1417 ef->fw_capture_running = true;
1418 err = fw_iso_context_start(ef->capture_iso_context, -1, 0,
1419 FW_ISO_CONTEXT_MATCH_TAG1);
1426 ef->fw_capture_running = false;
1427 cmp_remove_capture_connection(ef);
1429 fw_iso_context_destroy(ef->capture_iso_context);
1431 fw_iso_buffer_destroy(&ef->capture_iso_buffer, ef->device->card);
1435 static void fw_capture_stop(struct echofire *ef)
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);
1444 static int fw_capture_open(struct echofire *ef)
1448 mutex_lock(&ef->mutex);
1449 if (ef->fw_capture_users == 0) {
1450 err = fw_capture_start(ef);
1452 mutex_unlock(&ef->mutex);
1456 ef->fw_capture_users++;
1457 mutex_unlock(&ef->mutex);
1461 static void fw_capture_close(struct echofire *ef)
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);
1470 static int init_substream(struct echofire *ef,
1471 struct snd_pcm_substream *substream)
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,
1489 .periods_max = UINT_MAX,
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,
1503 err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
1506 snd_pcm_set_sync(substream);
1510 static int pcm_playback_open(struct snd_pcm_substream *substream)
1512 struct echofire *ef = substream->private_data;
1515 err = init_substream(ef, substream);
1518 substream->runtime->hw.channels_min = ef->pcm_playback_channels;
1519 substream->runtime->hw.channels_max = ef->pcm_playback_channels;
1521 err = fw_capture_open(ef);
1524 err = fw_playback_open(ef);
1526 fw_capture_close(ef);
1532 static int pcm_capture_open(struct snd_pcm_substream *substream)
1534 struct echofire *ef = substream->private_data;
1537 err = init_substream(ef, substream);
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;
1544 return fw_capture_open(ef);
1547 static int pcm_playback_close(struct snd_pcm_substream *substream)
1549 struct echofire *ef = substream->private_data;
1551 fw_playback_close(ef);
1552 fw_capture_close(ef);
1556 static int pcm_capture_close(struct snd_pcm_substream *substream)
1558 struct echofire *ef = substream->private_data;
1560 fw_capture_close(ef);
1564 static int pcm_hw_params(struct snd_pcm_substream *substream,
1565 struct snd_pcm_hw_params *hw_params)
1567 struct echofire *ef = substream->private_data;
1571 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
1572 params_buffer_bytes(hw_params));
1575 static int pcm_hw_free(struct snd_pcm_substream *substream)
1577 return snd_pcm_lib_free_vmalloc_buffer(substream);
1580 static int pcm_playback_prepare(struct snd_pcm_substream *substream)
1582 struct echofire *ef = substream->private_data;
1586 if (!ef->fw_capture_running)
1589 ef->pcm_playback_period_pos = 0;
1590 ef->pcm_playback_buffer_pos = 0;
1594 static int pcm_capture_prepare(struct snd_pcm_substream *substream)
1596 struct echofire *ef = substream->private_data;
1599 err = wait_event_interruptible(ef->capture_data_wait,
1600 ef->capture_data_received ||
1601 !ef->fw_capture_running);
1606 if (!ef->fw_capture_running)
1609 ef->pcm_capture_period_pos = 0;
1610 ef->pcm_capture_buffer_pos = 0;
1614 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1616 struct echofire *ef = substream->private_data;
1617 struct snd_pcm_substream *s;
1618 bool do_playback = false, do_capture = false, run;
1624 case SNDRV_PCM_TRIGGER_START:
1627 case SNDRV_PCM_TRIGGER_STOP:
1634 snd_pcm_group_for_each_entry(s, substream) {
1635 if (s == ef->pcm_playback_substream) {
1636 if (run && !ef->fw_playback_running)
1639 } else if (s == ef->pcm_capture_substream) {
1640 if (run && !ef->fw_capture_running)
1646 snd_pcm_trigger_done(ef->pcm_playback_substream, substream);
1648 snd_pcm_trigger_done(ef->pcm_capture_substream, substream);
1650 spin_lock(&ef->lock);
1652 ef->pcm_playback_running = run;
1654 ef->pcm_capture_running = run;
1655 spin_unlock(&ef->lock);
1659 static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *subs)
1661 struct echofire *ef = subs->private_data;
1662 unsigned long flags;
1665 spin_lock_irqsave(&ef->lock, flags);
1666 pos = ef->pcm_playback_buffer_pos;
1667 spin_unlock_irqrestore(&ef->lock, flags);
1671 static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *subs)
1673 struct echofire *ef = subs->private_data;
1674 unsigned long flags;
1677 spin_lock_irqsave(&ef->lock, flags);
1678 pos = ef->pcm_capture_buffer_pos;
1679 spin_unlock_irqrestore(&ef->lock, flags);
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,
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,
1708 static int create_pcm_devices(struct echofire *ef)
1710 struct snd_pcm *pcm;
1713 err = snd_pcm_new(ef->card, "Fireworks", 0, 1, 1, &pcm);
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;
1727 static int midi_output_open(struct snd_rawmidi_substream *substream)
1729 struct echofire *ef = substream->rmidi->private_data;
1732 err = fw_capture_open(ef);
1735 err = fw_playback_open(ef);
1737 fw_capture_close(ef);
1743 static int midi_output_close(struct snd_rawmidi_substream *substream)
1745 struct echofire *ef = substream->rmidi->private_data;
1747 fw_playback_close(ef);
1748 fw_capture_close(ef);
1752 static void midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1754 struct echofire *ef = substream->rmidi->private_data;
1755 unsigned long flags;
1757 spin_lock_irqsave(&ef->lock, flags);
1759 __set_bit(substream->number, &ef->midi_output_running);
1761 __clear_bit(substream->number, &ef->midi_output_running);
1762 spin_unlock_irqrestore(&ef->lock, flags);
1765 static void midi_output_drain(struct snd_rawmidi_substream *substream)
1767 msleep(4 + 1); /* FIXME: magic */
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,
1777 static int midi_input_open(struct snd_rawmidi_substream *substream)
1779 struct echofire *ef = substream->rmidi->private_data;
1781 return fw_capture_open(ef);
1784 static int midi_input_close(struct snd_rawmidi_substream *substream)
1786 struct echofire *ef = substream->rmidi->private_data;
1788 fw_capture_close(ef);
1792 static void midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1794 struct echofire *ef = substream->rmidi->private_data;
1795 unsigned long flags;
1797 spin_lock_irqsave(&ef->lock, flags);
1799 __set_bit(substream->number, &ef->midi_input_running);
1801 __clear_bit(substream->number, &ef->midi_input_running);
1802 spin_unlock_irqrestore(&ef->lock, flags);
1805 static struct snd_rawmidi_ops midi_input_ops = {
1806 .open = midi_input_open,
1807 .close = midi_input_close,
1808 .trigger = midi_input_trigger,
1811 static void set_midi_substream_names(struct echofire *ef,
1812 struct snd_rawmidi_str *str)
1814 struct snd_rawmidi_substream *subs;
1816 if (str->substream_count > 1)
1817 list_for_each_entry(subs, &str->substreams, list)
1818 snprintf(subs->name, sizeof(subs->name),
1820 ef->card->shortname, subs->number + 1);
1823 static int create_midi_ports(struct echofire *ef)
1825 struct snd_rawmidi *rmidi;
1826 struct snd_rawmidi_str *str;
1827 struct snd_rawmidi_substream *subs;
1830 err = snd_rawmidi_new(ef->card, "Fireworks", 0,
1831 ef->midi_output_count, ef->midi_input_count,
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,
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);
1847 if (ef->midi_input_count) {
1848 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
1849 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
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);
1856 if (ef->midi_output_count && ef->midi_input_count)
1857 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
1861 static int hw_caps_ctl_info(struct snd_kcontrol *ctl,
1862 struct snd_ctl_elem_info *info)
1864 info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1865 info->count = sizeof(struct efc_hardware_info);
1869 static int hw_caps_ctl_get(struct snd_kcontrol *ctl,
1870 struct snd_ctl_elem_value *value)
1872 struct echofire *ef = ctl->private_data;
1874 return echofire_hwinfo_get_caps(ef, (struct efc_hardware_info *)
1875 value->value.bytes.data);
1878 static int hw_info_poll_ctl_info(struct snd_kcontrol *ctl,
1879 struct snd_ctl_elem_info *info)
1881 struct echofire *ef = ctl->private_data;
1883 info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1884 info->count = 4 * (9 + ef->polled_meters_count);
1888 static int hw_info_poll_ctl_get(struct snd_kcontrol *ctl,
1889 struct snd_ctl_elem_value *value)
1891 struct echofire *ef = ctl->private_data;
1893 return echofire_get_polled_values(ef, (struct efc_polled_values *)
1894 value->value.bytes.data);
1897 static int hw_ctrl_identify_ctl_tlv(struct snd_kcontrol *ctl, int op,
1898 unsigned int size, unsigned int __user *tlv)
1900 struct echofire *ef = ctl->private_data;
1902 return echofire_identify(ef);
1905 static const struct snd_kcontrol_new mixer_controls[] = {
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,
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,
1922 static const struct snd_kcontrol_new identify_ctl = {
1923 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
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 },
1931 static int create_mixer_controls(struct echofire *ef)
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));
1942 if (true /* TODO: which models? */) {
1943 err = snd_ctl_add(ef->card, snd_ctl_new1(&identify_ctl, ef));
1950 static int __devinit get_hardware_info(struct echofire *ef)
1952 struct efc_hardware_info *hwinfo;
1953 unsigned int version;
1954 char version_str[12];
1957 hwinfo = kmalloc(sizeof(*hwinfo), GFP_KERNEL);
1961 err = echofire_hwinfo_get_caps(ef, hwinfo);
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;
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);
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");
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);
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);
2004 static void echofire_update(struct fw_unit *unit)
2006 struct snd_card *card = dev_get_drvdata(&unit->device);
2007 struct echofire *ef = card->private_data;
2008 unsigned long flags;
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 */
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 */
2029 mutex_unlock(&ef->mutex);
2032 static bool __devinit match_echofire_device_name(struct fw_unit *unit)
2034 static const char *const models[] = {
2035 /* Echo Digital Audio */
2055 if (fw_csr_string(unit->directory, CSR_MODEL, name, sizeof(name)) < 0)
2057 for (i = 0; i < ARRAY_SIZE(models); i++)
2058 if (!strcasecmp(name, models[i]))
2063 static void echofire_card_free(struct snd_card *card)
2065 struct echofire *ef = card->private_data;
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);
2074 mutex_destroy(&ef->fcp_mutex);
2075 mutex_destroy(&ef->mutex);
2078 static int __devinit echofire_probe(struct device *dev)
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,
2084 struct fw_unit *unit = fw_unit(dev);
2086 struct snd_card *card;
2087 struct echofire *ef;
2088 u32 clock_params[3];
2092 if (!match_echofire_device_name(unit))
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])
2099 if (card_index >= SNDRV_CARDS) {
2103 err = snd_card_create(index[card_index], id[card_index], THIS_MODULE,
2104 sizeof(*ef), &card);
2107 card->private_free = echofire_card_free;
2109 ef = card->private_data;
2111 ef->device = fw_parent_device(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;
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);
2129 ef_err(ef, "cannot register FCP response handler\n");
2133 snd_card_set_dev(card, dev);
2135 err = get_hardware_info(ef);
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);
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;
2152 err = determine_polled_meters_count(ef);
2156 if (ef->midi_output_count || ef->midi_input_count) {
2157 err = create_midi_ports(ef);
2162 err = create_pcm_devices(ef);
2166 err = create_mixer_controls(ef);
2170 err = snd_card_register(card);
2174 dev_set_drvdata(dev, card);
2175 devices_used |= 1 << card_index;
2176 ef->card_index = card_index;
2177 mutex_unlock(&devices_mutex);
2181 mutex_unlock(&devices_mutex);
2182 snd_card_free(card);
2186 mutex_unlock(&devices_mutex);
2190 static int __devexit echofire_remove(struct device *dev)
2192 struct snd_card *card = dev_get_drvdata(dev);
2193 struct echofire *ef = card->private_data;
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);
2202 #define VENDOR_GIBSON 0x00075b
2203 #define MODEL_GIBSON_RIP 0x00afb2
2204 /* #define MODEL_GIBSON_GOLDTOP 0x?????? */
2206 #define VENDOR_LOUD 0x000ff2
2207 #define MODEL_MACKIE_400F 0x00400f
2208 #define MODEL_MACKIE_1200F 0x01200f
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
2220 #define SPECIFIER_1394TA 0x00a02d
2222 static const struct ieee1394_device_id echofire_id_table[] = {
2224 .match_flags = IEEE1394_MATCH_VENDOR_ID |
2225 IEEE1394_MATCH_SPECIFIER_ID,
2226 .vendor_id = VENDOR_ECHO_DIGITAL_AUDIO,
2227 .specifier_id = SPECIFIER_1394TA,
2230 .match_flags = IEEE1394_MATCH_VENDOR_ID |
2231 IEEE1394_MATCH_SPECIFIER_ID,
2232 .vendor_id = VENDOR_GIBSON,
2233 .specifier_id = SPECIFIER_1394TA,
2236 .match_flags = IEEE1394_MATCH_VENDOR_ID |
2237 IEEE1394_MATCH_MODEL_ID,
2238 .vendor_id = VENDOR_LOUD,
2239 .model_id = MODEL_MACKIE_400F,
2242 .match_flags = IEEE1394_MATCH_VENDOR_ID |
2243 IEEE1394_MATCH_MODEL_ID,
2244 .vendor_id = VENDOR_LOUD,
2245 .model_id = MODEL_MACKIE_1200F,
2249 MODULE_DEVICE_TABLE(ieee1394, echofire_id_table);
2251 static struct fw_driver echofire_driver = {
2253 .owner = THIS_MODULE,
2254 .name = "snd-fireworks",
2255 .bus = &fw_bus_type,
2256 .probe = echofire_probe,
2257 .remove = __devexit_p(echofire_remove),
2259 .update = echofire_update,
2260 .id_table = echofire_id_table,
2263 static int __init alsa_card_echofire_init(void)
2265 return driver_register(&echofire_driver.driver);
2268 static void __exit alsa_card_echofire_exit(void)
2270 driver_unregister(&echofire_driver.driver);
2271 mutex_destroy(&devices_mutex);
2274 module_init(alsa_card_echofire_init);
2275 module_exit(alsa_card_echofire_exit);