1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * oxfw.h - a part of driver for OXFW970/971 based devices
4  *
5  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/firewire.h>
10 #include <linux/firewire-constants.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include <linux/compat.h>
16 #include <linux/sched/signal.h>
17 
18 #include <sound/control.h>
19 #include <sound/core.h>
20 #include <sound/initval.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/info.h>
24 #include <sound/rawmidi.h>
25 #include <sound/firewire.h>
26 #include <sound/hwdep.h>
27 
28 #include "../lib.h"
29 #include "../fcp.h"
30 #include "../packets-buffer.h"
31 #include "../iso-resources.h"
32 #include "../amdtp-am824.h"
33 #include "../cmp.h"
34 
35 enum snd_oxfw_quirk {
36 	// Postpone transferring packets during handling asynchronous transaction. As a result,
37 	// next isochronous packet includes more events than one packet can include.
38 	SND_OXFW_QUIRK_JUMBO_PAYLOAD = 0x01,
39 	// The dbs field of CIP header in tx packet is wrong.
40 	SND_OXFW_QUIRK_WRONG_DBS = 0x02,
41 	// Blocking transmission mode is used.
42 	SND_OXFW_QUIRK_BLOCKING_TRANSMISSION = 0x04,
43 	// Stanton SCS1.d and SCS1.m support unique transaction.
44 	SND_OXFW_QUIRK_SCS_TRANSACTION = 0x08,
45 	// Apogee Duet FireWire ignores data blocks in packet with NO_INFO for audio data
46 	// processing, while output level meter moves. Any value in syt field of packet takes
47 	// the device to process audio data even if the value is invalid in a point of
48 	// IEC 61883-1/6.
49 	SND_OXFW_QUIRK_IGNORE_NO_INFO_PACKET = 0x10,
50 	// Loud Technologies Mackie Onyx 1640i seems to configure OXFW971 ASIC so that it decides
51 	// event frequency according to events in received isochronous packets. The device looks to
52 	// performs media clock recovery voluntarily. In the recovery, the packets with NO_INFO
53 	// are ignored, thus driver should transfer packets with timestamp.
54 	SND_OXFW_QUIRK_VOLUNTARY_RECOVERY = 0x20,
55 };
56 
57 /* This is an arbitrary number for convinience. */
58 #define	SND_OXFW_STREAM_FORMAT_ENTRIES	10
59 struct snd_oxfw {
60 	struct snd_card *card;
61 	struct fw_unit *unit;
62 	struct mutex mutex;
63 	spinlock_t lock;
64 
65 	// The combination of snd_oxfw_quirk enumeration-constants.
66 	unsigned int quirks;
67 	bool has_output;
68 	bool has_input;
69 	u8 *tx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES];
70 	u8 *rx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES];
71 	bool assumed;
72 	struct cmp_connection out_conn;
73 	struct cmp_connection in_conn;
74 	struct amdtp_stream tx_stream;
75 	struct amdtp_stream rx_stream;
76 	unsigned int substreams_count;
77 
78 	unsigned int midi_input_ports;
79 	unsigned int midi_output_ports;
80 
81 	int dev_lock_count;
82 	bool dev_lock_changed;
83 	wait_queue_head_t hwdep_wait;
84 
85 	void *spec;
86 
87 	struct amdtp_domain domain;
88 };
89 
90 /*
91  * AV/C Stream Format Information Specification 1.1 Working Draft
92  * (Apr 2005, 1394TA)
93  */
94 int avc_stream_set_format(struct fw_unit *unit, enum avc_general_plug_dir dir,
95 			  unsigned int pid, u8 *format, unsigned int len);
96 int avc_stream_get_format(struct fw_unit *unit,
97 			  enum avc_general_plug_dir dir, unsigned int pid,
98 			  u8 *buf, unsigned int *len, unsigned int eid);
99 static inline int
avc_stream_get_format_single(struct fw_unit * unit,enum avc_general_plug_dir dir,unsigned int pid,u8 * buf,unsigned int * len)100 avc_stream_get_format_single(struct fw_unit *unit,
101 			     enum avc_general_plug_dir dir, unsigned int pid,
102 			     u8 *buf, unsigned int *len)
103 {
104 	return avc_stream_get_format(unit, dir, pid, buf, len, 0xff);
105 }
106 static inline int
avc_stream_get_format_list(struct fw_unit * unit,enum avc_general_plug_dir dir,unsigned int pid,u8 * buf,unsigned int * len,unsigned int eid)107 avc_stream_get_format_list(struct fw_unit *unit,
108 			   enum avc_general_plug_dir dir, unsigned int pid,
109 			   u8 *buf, unsigned int *len,
110 			   unsigned int eid)
111 {
112 	return avc_stream_get_format(unit, dir, pid, buf, len, eid);
113 }
114 
115 /*
116  * AV/C Digital Interface Command Set General Specification 4.2
117  * (Sep 2004, 1394TA)
118  */
119 int avc_general_inquiry_sig_fmt(struct fw_unit *unit, unsigned int rate,
120 				enum avc_general_plug_dir dir,
121 				unsigned short pid);
122 
123 int snd_oxfw_stream_init_duplex(struct snd_oxfw *oxfw);
124 int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
125 				   struct amdtp_stream *stream,
126 				   unsigned int rate, unsigned int pcm_channels,
127 				   unsigned int frames_per_period,
128 				   unsigned int frames_per_buffer);
129 int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw);
130 void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw);
131 void snd_oxfw_stream_destroy_duplex(struct snd_oxfw *oxfw);
132 void snd_oxfw_stream_update_duplex(struct snd_oxfw *oxfw);
133 
134 struct snd_oxfw_stream_formation {
135 	unsigned int rate;
136 	unsigned int pcm;
137 	unsigned int midi;
138 };
139 int snd_oxfw_stream_parse_format(u8 *format,
140 				 struct snd_oxfw_stream_formation *formation);
141 int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw,
142 				enum avc_general_plug_dir dir,
143 				struct snd_oxfw_stream_formation *formation);
144 
145 int snd_oxfw_stream_discover(struct snd_oxfw *oxfw);
146 
147 void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw);
148 int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw);
149 void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw);
150 
151 int snd_oxfw_create_pcm(struct snd_oxfw *oxfw);
152 
153 void snd_oxfw_proc_init(struct snd_oxfw *oxfw);
154 
155 int snd_oxfw_create_midi(struct snd_oxfw *oxfw);
156 
157 int snd_oxfw_create_hwdep(struct snd_oxfw *oxfw);
158 
159 int snd_oxfw_add_spkr(struct snd_oxfw *oxfw, bool is_lacie);
160 int snd_oxfw_scs1x_add(struct snd_oxfw *oxfw);
161 void snd_oxfw_scs1x_update(struct snd_oxfw *oxfw);
162