xref: /linux/include/linux/firewire.h (revision aa7f13e25c8003b54726b6e1339ee0fa8a2e99a8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FIREWIRE_H
3 #define _LINUX_FIREWIRE_H
4 
5 #include <linux/completion.h>
6 #include <linux/device.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/kernel.h>
9 #include <linux/kref.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/spinlock.h>
13 #include <linux/sysfs.h>
14 #include <linux/timer.h>
15 #include <linux/types.h>
16 #include <linux/workqueue.h>
17 #include <linux/device-id/ieee1394.h>
18 
19 #include <linux/atomic.h>
20 #include <asm/byteorder.h>
21 
22 #define CSR_REGISTER_BASE		0xfffff0000000ULL
23 
24 /* register offsets are relative to CSR_REGISTER_BASE */
25 #define CSR_STATE_CLEAR			0x0
26 #define CSR_STATE_SET			0x4
27 #define CSR_NODE_IDS			0x8
28 #define CSR_RESET_START			0xc
29 #define CSR_SPLIT_TIMEOUT_HI		0x18
30 #define CSR_SPLIT_TIMEOUT_LO		0x1c
31 #define CSR_CYCLE_TIME			0x200
32 #define CSR_BUS_TIME			0x204
33 #define CSR_BUSY_TIMEOUT		0x210
34 #define CSR_PRIORITY_BUDGET		0x218
35 #define CSR_BUS_MANAGER_ID		0x21c
36 #define CSR_BANDWIDTH_AVAILABLE		0x220
37 #define CSR_CHANNELS_AVAILABLE		0x224
38 #define CSR_CHANNELS_AVAILABLE_HI	0x224
39 #define CSR_CHANNELS_AVAILABLE_LO	0x228
40 #define CSR_MAINT_UTILITY		0x230
41 #define CSR_BROADCAST_CHANNEL		0x234
42 #define CSR_CONFIG_ROM			0x400
43 #define CSR_CONFIG_ROM_END		0x800
44 #define CSR_OMPR			0x900
45 #define CSR_OPCR(i)			(0x904 + (i) * 4)
46 #define CSR_IMPR			0x980
47 #define CSR_IPCR(i)			(0x984 + (i) * 4)
48 #define CSR_FCP_COMMAND			0xB00
49 #define CSR_FCP_RESPONSE		0xD00
50 #define CSR_FCP_END			0xF00
51 #define CSR_TOPOLOGY_MAP		0x1000
52 #define CSR_TOPOLOGY_MAP_END		0x1400
53 #define CSR_SPEED_MAP			0x2000
54 #define CSR_SPEED_MAP_END		0x3000
55 
56 #define CSR_OFFSET		0x40
57 #define CSR_LEAF		0x80
58 #define CSR_DIRECTORY		0xc0
59 
60 #define CSR_DESCRIPTOR		0x01
61 #define CSR_VENDOR		0x03
62 #define CSR_HARDWARE_VERSION	0x04
63 #define CSR_UNIT		0x11
64 #define CSR_SPECIFIER_ID	0x12
65 #define CSR_VERSION		0x13
66 #define CSR_DEPENDENT_INFO	0x14
67 #define CSR_MODEL		0x17
68 #define CSR_DIRECTORY_ID	0x20
69 
70 struct fw_csr_iterator {
71 	const u32 *p;
72 	const u32 *end;
73 };
74 
75 void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p);
76 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value);
77 int fw_csr_string(const u32 *directory, int key, char *buf, size_t size);
78 
79 extern const struct bus_type fw_bus_type;
80 
81 struct fw_card_driver;
82 struct fw_node;
83 
84 struct fw_card {
85 	const struct fw_card_driver *driver;
86 	struct device *device;
87 	struct kref kref;
88 	struct completion done;
89 
90 	int node_id;
91 	int generation;
92 	u64 reset_jiffies;
93 
94 	struct {
95 		int current_tlabel __guarded_by(&lock);
96 		u64 tlabel_mask __guarded_by(&lock);
97 		struct list_head list __guarded_by(&lock);
98 		spinlock_t lock;
99 	} transactions;
100 
101 	struct {
102 		u32 hi __guarded_by(&lock);
103 		u32 lo __guarded_by(&lock);
104 		unsigned int cycles __guarded_by(&lock);
105 		unsigned int jiffies __guarded_by(&lock);
106 		spinlock_t lock;
107 	} split_timeout;
108 
109 	unsigned long long guid;
110 	unsigned max_receive;
111 	int link_speed;
112 	int config_rom_generation;
113 
114 	spinlock_t lock;
115 
116 	struct fw_node *local_node;
117 	struct fw_node *root_node;
118 	struct fw_node *irm_node;
119 	u8 color; /* must be u8 to match the definition in struct fw_node */
120 	int gap_count;
121 	bool beta_repeaters_present;
122 
123 	int index;
124 	struct list_head link;
125 
126 	struct delayed_work br_work; /* bus reset job */
127 	bool br_short;
128 
129 	struct delayed_work bm_work; /* bus manager job */
130 	int bm_retries;
131 	int bm_generation;
132 	int bm_node_id;
133 	bool bm_abdicate;
134 
135 	bool priority_budget_implemented;	/* controller feature */
136 	bool broadcast_channel_auto_allocated;	/* controller feature */
137 
138 	bool broadcast_channel_allocated;
139 	u32 broadcast_channel;
140 
141 	struct {
142 		__be32 buffer[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4] __guarded_by(&lock);
143 		spinlock_t lock;
144 	} topology_map;
145 
146 	__be32 maint_utility_register;
147 
148 	struct workqueue_struct *isoc_wq;
149 	struct workqueue_struct *async_wq;
150 };
151 
152 static inline struct fw_card *fw_card_get(struct fw_card *card)
153 {
154 	kref_get(&card->kref);
155 
156 	return card;
157 }
158 
159 void fw_card_release(struct kref *kref);
160 
161 static inline void fw_card_put(struct fw_card *card)
162 {
163 	kref_put(&card->kref, fw_card_release);
164 }
165 
166 int fw_card_read_cycle_time(struct fw_card *card, u32 *cycle_time);
167 
168 struct fw_attribute_group {
169 	struct attribute_group *groups[2];
170 	struct attribute_group group;
171 	struct attribute *attrs[13];
172 };
173 
174 enum fw_device_quirk {
175 	// See 10389536742c ("firewire: core: check for 1394a compliant IRM, fix inaccessibility of Sony camcorder").
176 	FW_DEVICE_QUIRK_IRM_IS_1394_1995_ONLY = BIT(0),
177 
178 	// See 6044565af458 ("firewire: core: fix unstable I/O with Canon camcorder").
179 	FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER = BIT(1),
180 
181 	// MOTU Audio Express transfers acknowledge packet with 0x10 for pending state.
182 	FW_DEVICE_QUIRK_ACK_PACKET_WITH_INVALID_PENDING_CODE = BIT(2),
183 
184 	// TASCAM FW-1082/FW-1804/FW-1884 often freezes when receiving S400 packets.
185 	FW_DEVICE_QUIRK_UNSTABLE_AT_S400 = BIT(3),
186 };
187 
188 enum fw_device_state {
189 	FW_DEVICE_INITIALIZING,
190 	FW_DEVICE_RUNNING,
191 	FW_DEVICE_GONE,
192 	FW_DEVICE_SHUTDOWN,
193 };
194 
195 /*
196  * Note, fw_device.generation always has to be read before fw_device.node_id.
197  * Use SMP memory barriers to ensure this.  Otherwise requests will be sent
198  * to an outdated node_id if the generation was updated in the meantime due
199  * to a bus reset.
200  *
201  * Likewise, fw-core will take care to update .node_id before .generation so
202  * that whenever fw_device.generation is current WRT the actual bus generation,
203  * fw_device.node_id is guaranteed to be current too.
204  *
205  * The same applies to fw_device.card->node_id vs. fw_device.generation.
206  *
207  * fw_device.config_rom and fw_device.config_rom_length may be accessed during
208  * the lifetime of any fw_unit belonging to the fw_device, before device_del()
209  * was called on the last fw_unit.  Alternatively, they may be accessed while
210  * holding fw_device_rwsem.
211  */
212 struct fw_device {
213 	atomic_t state;
214 	struct fw_node *node;
215 	int node_id;
216 	int generation;
217 	unsigned max_speed;
218 	struct fw_card *card;
219 	struct device device;
220 
221 	// A set of enum fw_device_quirk.
222 	int quirks;
223 
224 	struct mutex client_list_mutex;
225 	struct list_head client_list;
226 
227 	const u32 *config_rom __counted_by_ptr(config_rom_length);
228 	size_t config_rom_length;
229 	int config_rom_retries;
230 	unsigned is_local:1;
231 	unsigned max_rec:4;
232 	unsigned cmc:1;
233 	unsigned irmc:1;
234 	unsigned bc_implemented:2;
235 
236 	work_func_t workfn;
237 	struct delayed_work work;
238 	struct fw_attribute_group attribute_group;
239 };
240 
241 #define fw_device(dev)	container_of_const(dev, struct fw_device, device)
242 
243 static inline int fw_device_is_shutdown(struct fw_device *device)
244 {
245 	return atomic_read(&device->state) == FW_DEVICE_SHUTDOWN;
246 }
247 
248 int fw_device_enable_phys_dma(struct fw_device *device);
249 
250 /*
251  * fw_unit.directory must not be accessed after device_del(&fw_unit.device).
252  */
253 struct fw_unit {
254 	struct device device;
255 	const u32 *directory;
256 	struct fw_attribute_group attribute_group;
257 };
258 
259 #define fw_unit(dev)	container_of_const(dev, struct fw_unit, device)
260 
261 static inline struct fw_unit *fw_unit_get(struct fw_unit *unit)
262 {
263 	get_device(&unit->device);
264 
265 	return unit;
266 }
267 
268 static inline void fw_unit_put(struct fw_unit *unit)
269 {
270 	put_device(&unit->device);
271 }
272 
273 #define fw_parent_device(unit)	fw_device(unit->device.parent)
274 
275 struct fw_driver {
276 	struct device_driver driver;
277 	int (*probe)(struct fw_unit *unit, const struct ieee1394_device_id *id);
278 	/* Called when the parent device sits through a bus reset. */
279 	void (*update)(struct fw_unit *unit);
280 	void (*remove)(struct fw_unit *unit);
281 	const struct ieee1394_device_id *id_table;
282 };
283 
284 struct fw_packet;
285 struct fw_request;
286 
287 typedef void (*fw_packet_callback_t)(struct fw_packet *packet,
288 				     struct fw_card *card, int status);
289 
290 /**
291  * typedef fw_transaction_callback_t - Function to handle the result of outgoing transaction.
292  * @card: the interface to send the request at
293  * @rcode: the response code
294  * @data: the data for the response subaction
295  * @length: the length of data
296  * @callback_data: the data registered with this callback
297  *
298  * This callback is called from a workqueue in process context after the outgoing transaction is
299  * completed successfully or unsuccessfully.
300  *
301  * Among its parameters is the response code which is either one of the rcodes per IEEE 1394 or, in
302  * case of internal errors, the firewire-core specific %RCODE_SEND_ERROR. The other firewire-core
303  * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION, %RCODE_NO_ACK) denote
304  * transaction timeout, busy responder, stale request generation, or missing ACK respectively.
305  *
306  * Context: Process context.
307  */
308 typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
309 					  void *data, size_t length,
310 					  void *callback_data);
311 
312 /**
313  * typedef fw_transaction_callback_with_tstamp_t - Function to handle the result of outgoing
314  *						   transaction with hardware time stamps.
315  * @card: the interface to send the request at
316  * @rcode: the response code
317  * @request_tstamp: the time stamp at which the request subaction finished
318  * @response_tstamp: the time stamp at which the response subaction finished
319  * @data: the data for the response subaction
320  * @length: the length of data
321  * @callback_data: the data registered with this callback
322  *
323  * This callback is called from a workqueue in process context after the outgoing transaction is
324  * completed successfully or unsuccessfully.
325  *
326  * Among its parameters is the response code which is either one of the rcodes per IEEE 1394 or, in
327  * case of internal errors, the firewire-core specific %RCODE_SEND_ERROR. The other firewire-core
328  * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION, %RCODE_NO_ACK) denote
329  * transaction timeout, busy responder, stale request generation, or missing ACK respectively.
330  *
331  * The timestamp consists of the low order 3 bits of second field and the full 13 bits of count
332  * field of isochronous cycle time register.
333  *
334  * Context: Process context.
335  */
336 typedef void (*fw_transaction_callback_with_tstamp_t)(struct fw_card *card, int rcode,
337 					u32 request_tstamp, u32 response_tstamp, void *data,
338 					size_t length, void *callback_data);
339 
340 /**
341  * union fw_transaction_callback - A union to store the outgoing transaction callback.
342  * @without_tstamp: Callback without hardware timestamps.
343  * @with_tstamp: Callback with hardware timestamps.
344  */
345 union fw_transaction_callback {
346 	fw_transaction_callback_t without_tstamp;
347 	fw_transaction_callback_with_tstamp_t with_tstamp;
348 };
349 
350 /**
351  * typedef fw_address_callback_t - Function to handle the request of the incoming transaction.
352  * @card: the card instance which receives the request
353  * @request: the request instance.
354  * @tcode: the transaction code
355  * @destination: the destination node ID
356  * @source: the source node ID
357  * @generation: the bus generation in which the request was sent
358  * @offset: the destination offset in source node.
359  * @data: the request content if available.
360  * @length: the length of data.
361  * @callback_data: the data registered with this function.
362  *
363  * This callback handles an inbound request subaction.
364  *
365  * The contents of @request are opaque to the callback. The callback is responsible for calling
366  * fw_send_response() for @request, except for FCP registers, for which the core handles the
367  * response.
368  *
369  * Context: Process context.
370  */
371 typedef void (*fw_address_callback_t)(struct fw_card *card,
372 				      struct fw_request *request,
373 				      int tcode, int destination, int source,
374 				      int generation,
375 				      unsigned long long offset,
376 				      void *data, size_t length,
377 				      void *callback_data);
378 
379 struct fw_packet {
380 	int speed;
381 	int generation;
382 	u32 header[4];
383 	size_t header_length;
384 	void *payload __counted_by_ptr(payload_length);
385 	size_t payload_length;
386 	dma_addr_t payload_bus;
387 	bool payload_mapped;
388 	u32 timestamp;
389 
390 	// Used to handle the local-to-local packets in the AT request/response contexts.
391 	struct list_head link_for_local;
392 
393 	/*
394 	 * This callback is called when the packet transmission has completed.
395 	 * For successful transmission, the status code is the ack received
396 	 * from the destination.  Otherwise it is one of the juju-specific
397 	 * rcodes:  RCODE_SEND_ERROR, _CANCELLED, _BUSY, _GENERATION, _NO_ACK.
398 	 * The callback is called from a workqueue. It is not preferable to block it so long.
399 	 */
400 	fw_packet_callback_t callback;
401 	int ack;
402 	void *driver_data;
403 };
404 
405 /**
406  * struct fw_transaction - A structure to hold outgoing transaction state.
407  *
408  * All structure members are for internal use only. Once passed to __fw_send_request(), this
409  * structure holds the state of an outgoing transaction. It must not be freed until the callback
410  * reports the transaction result.
411  */
412 struct fw_transaction {
413 	/* private: */
414 	int node_id; /* The generation is implied; it is always the current. */
415 	int tlabel;
416 	struct list_head link;
417 	struct fw_card *card;
418 	bool is_split_transaction;
419 	struct timer_list split_timeout_timer;
420 	u32 split_timeout_cycle;
421 
422 	struct fw_packet packet;
423 
424 	/*
425 	 * The data passed to the callback is valid only during the
426 	 * callback.
427 	 */
428 	union fw_transaction_callback callback;
429 	bool with_tstamp;
430 	void *callback_data;
431 
432 	// For some error cases.
433 	struct work_struct error_work;
434 	int rcode;
435 	u32 response_timestamp;
436 };
437 
438 /**
439  * struct fw_address_handler - A handler for incoming transaction requests to a registered address
440  *			       region.
441  * @offset: The registered offset, available after successful registration.
442  * @length: The length of the address region in bytes, a multiple of a quadlet.
443  * @address_callback: Callback invoked when a transaction request is received for the registered
444  *		      address region.
445  * @callback_data: Data passed to @address_callback.
446  *
447  * This structure represents a handler registered to receive incoming asynchronous requests to a
448  * specific region of the address space on the local node.
449  */
450 struct fw_address_handler {
451 	u64 offset;
452 	u64 length;
453 	fw_address_callback_t address_callback;
454 	void *callback_data;
455 
456 	/* private: */
457 	struct list_head link;
458 	struct kref kref;
459 	struct completion done;
460 };
461 
462 /**
463  * struct fw_address_region - An address range.
464  * @start: The starting offset in bytes, a multiple of a quadlet.
465  * @end: The ending offset in bytes, a multiple of a quadlet.
466  *
467  * Represents a range of addresses in the local node's address space.
468  */
469 struct fw_address_region {
470 	u64 start;
471 	u64 end;
472 };
473 
474 extern const struct fw_address_region fw_high_memory_region;
475 
476 int fw_core_add_address_handler(struct fw_address_handler *handler,
477 				const struct fw_address_region *region);
478 void fw_core_remove_address_handler(struct fw_address_handler *handler);
479 void fw_send_response(struct fw_card *card,
480 		      struct fw_request *request, int rcode);
481 int fw_get_request_speed(struct fw_request *request);
482 u32 fw_request_get_timestamp(const struct fw_request *request);
483 
484 void __fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
485 		int destination_id, int generation, int speed, unsigned long long offset,
486 		void *payload, size_t length, union fw_transaction_callback callback,
487 		bool with_tstamp, void *callback_data);
488 
489 /**
490  * fw_send_request() - submit a request packet for transmission to generate callback for response
491  *		       subaction without time stamp.
492  * @card:		interface to send the request at
493  * @t:			transaction instance to which the request belongs
494  * @tcode:		transaction code
495  * @destination_id:	destination node ID, consisting of bus_ID and phy_ID
496  * @generation:		bus generation in which request and response are valid
497  * @speed:		transmission speed
498  * @offset:		48bit wide offset into destination's address space
499  * @payload:		data payload for the request subaction
500  * @length:		length of the payload, in bytes
501  * @callback:		function to be called when the transaction is completed
502  * @callback_data:	data to be passed to the transaction completion callback
503  *
504  * A variation of __fw_send_request() to generate callback for response subaction without time
505  * stamp.
506  *
507  * After the transaction is completed successfully or unsuccessfully, the @callback will be called
508  * in process context.
509  *
510  * Context: Any context.
511  */
512 static inline void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
513 				   int destination_id, int generation, int speed,
514 				   unsigned long long offset, void *payload, size_t length,
515 				   fw_transaction_callback_t callback, void *callback_data)
516 {
517 	union fw_transaction_callback cb = {
518 		.without_tstamp = callback,
519 	};
520 	__fw_send_request(card, t, tcode, destination_id, generation, speed, offset, payload,
521 			  length, cb, false, callback_data);
522 }
523 
524 /**
525  * fw_send_request_with_tstamp() - submit a request packet for transmission to generate callback for
526  *				   response with time stamp.
527  * @card:		interface to send the request at
528  * @t:			transaction instance to which the request belongs
529  * @tcode:		transaction code
530  * @destination_id:	destination node ID, consisting of bus_ID and phy_ID
531  * @generation:		bus generation in which request and response are valid
532  * @speed:		transmission speed
533  * @offset:		48bit wide offset into destination's address space
534  * @payload:		data payload for the request subaction
535  * @length:		length of the payload, in bytes
536  * @callback:		function to be called when the transaction is completed
537  * @callback_data:	data to be passed to the transaction completion callback
538  *
539  * A variation of __fw_send_request() to generate callback for response subaction with time stamp.
540  *
541  * After the transaction is completed successfully or unsuccessfully, the @callback will be called
542  * in process context.
543  *
544  * Context: Any context.
545  */
546 static inline void fw_send_request_with_tstamp(struct fw_card *card, struct fw_transaction *t,
547 	int tcode, int destination_id, int generation, int speed, unsigned long long offset,
548 	void *payload, size_t length, fw_transaction_callback_with_tstamp_t callback,
549 	void *callback_data)
550 {
551 	union fw_transaction_callback cb = {
552 		.with_tstamp = callback,
553 	};
554 	__fw_send_request(card, t, tcode, destination_id, generation, speed, offset, payload,
555 			  length, cb, true, callback_data);
556 }
557 
558 int fw_cancel_transaction(struct fw_card *card,
559 			  struct fw_transaction *transaction);
560 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
561 		       int generation, int speed, unsigned long long offset,
562 		       void *payload, size_t length);
563 const char *fw_rcode_string(int rcode);
564 
565 static inline int fw_stream_packet_destination_id(int tag, int channel, int sy)
566 {
567 	return tag << 14 | channel << 8 | sy;
568 }
569 
570 void fw_schedule_bus_reset(struct fw_card *card, bool delayed,
571 			   bool short_reset);
572 
573 struct fw_descriptor {
574 	struct list_head link;
575 	size_t length;
576 	u32 immediate;
577 	u32 key;
578 	const u32 *data;
579 };
580 
581 int fw_core_add_descriptor(struct fw_descriptor *desc);
582 void fw_core_remove_descriptor(struct fw_descriptor *desc);
583 
584 /*
585  * The iso packet format allows for an immediate header/payload part
586  * stored in 'header' immediately after the packet info plus an
587  * indirect payload part that is pointer to by the 'payload' field.
588  * Applications can use one or the other or both to implement simple
589  * low-bandwidth streaming (e.g. audio) or more advanced
590  * scatter-gather streaming (e.g. assembling video frame automatically).
591  */
592 struct fw_iso_packet {
593 	u16 payload_length;	/* Length of indirect payload		*/
594 	u32 interrupt:1;	/* Generate interrupt on this packet	*/
595 	u32 skip:1;		/* tx: Set to not send packet at all	*/
596 				/* rx: Sync bit, wait for matching sy	*/
597 	u32 tag:2;		/* tx: Tag in packet header		*/
598 	u32 sy:4;		/* tx: Sy in packet header		*/
599 	u32 header_length:8;	/* Size of immediate header		*/
600 	u32 header[];		/* tx: Top of 1394 isoch. data_block	*/
601 };
602 
603 #define FW_ISO_CONTEXT_TRANSMIT			0
604 #define FW_ISO_CONTEXT_RECEIVE			1
605 #define FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL	2
606 
607 #define FW_ISO_CONTEXT_MATCH_TAG0	 1
608 #define FW_ISO_CONTEXT_MATCH_TAG1	 2
609 #define FW_ISO_CONTEXT_MATCH_TAG2	 4
610 #define FW_ISO_CONTEXT_MATCH_TAG3	 8
611 #define FW_ISO_CONTEXT_MATCH_ALL_TAGS	15
612 
613 /*
614  * An iso buffer is just a set of pages mapped for DMA in the
615  * specified direction.  Since the pages are to be used for DMA, they
616  * are not mapped into the kernel virtual address space.  We store the
617  * DMA address in the page private. The helper function
618  * fw_iso_buffer_map() will map the pages into a given vma.
619  */
620 struct fw_iso_buffer {
621 	enum dma_data_direction direction;
622 	struct page **pages;
623 	dma_addr_t *dma_addrs;
624 	int page_count;
625 };
626 
627 int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
628 		       int page_count, enum dma_data_direction direction);
629 void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
630 
631 struct fw_iso_context;
632 typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
633 				  u32 cycle, size_t header_length,
634 				  void *header, void *data);
635 typedef void (*fw_iso_mc_callback_t)(struct fw_iso_context *context,
636 				     dma_addr_t completed, void *data);
637 
638 union fw_iso_callback {
639 	fw_iso_callback_t sc;
640 	fw_iso_mc_callback_t mc;
641 };
642 
643 enum fw_iso_context_flag {
644 	FW_ISO_CONTEXT_FLAG_DROP_OVERFLOW_HEADERS = BIT(0),
645 };
646 
647 struct fw_iso_context {
648 	struct fw_card *card;
649 	struct work_struct work;
650 	int type;
651 	int channel;
652 	int speed;
653 	int flags;
654 	size_t header_size;
655 	size_t header_storage_size;
656 	union fw_iso_callback callback;
657 	void *callback_data;
658 };
659 
660 struct fw_iso_context *__fw_iso_context_create(struct fw_card *card, int type, int channel,
661 		int speed, size_t header_size, size_t header_storage_size,
662 		union fw_iso_callback callback, void *callback_data);
663 int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels);
664 int fw_iso_context_queue(struct fw_iso_context *ctx,
665 			 struct fw_iso_packet *packet,
666 			 struct fw_iso_buffer *buffer,
667 			 unsigned long payload);
668 void fw_iso_context_queue_flush(struct fw_iso_context *ctx);
669 int fw_iso_context_flush_completions(struct fw_iso_context *ctx);
670 
671 static inline struct fw_iso_context *fw_iso_context_create(struct fw_card *card, int type,
672 		int channel, int speed, size_t header_size, fw_iso_callback_t callback,
673 		void *callback_data)
674 {
675 	union fw_iso_callback cb = { .sc = callback };
676 
677 	return __fw_iso_context_create(card, type, channel, speed, header_size, PAGE_SIZE, cb,
678 				       callback_data);
679 }
680 
681 static inline struct fw_iso_context *fw_iso_context_create_with_header_storage_size(
682 		struct fw_card *card, int type, int channel, int speed, size_t header_size,
683 		size_t header_storage_size, fw_iso_callback_t callback, void *callback_data)
684 {
685 	union fw_iso_callback cb = { .sc = callback };
686 
687 	return __fw_iso_context_create(card, type, channel, speed, header_size, header_storage_size,
688 				       cb, callback_data);
689 }
690 
691 /**
692  * fw_iso_context_schedule_flush_completions() - schedule work item to process isochronous context.
693  * @ctx: the isochronous context
694  *
695  * Schedule a work item on workqueue to process the isochronous context. The registered callback
696  * function is called by the worker when a queued packet buffer with the interrupt flag is
697  * completed, either after transmission in the IT context or after being filled in the IR context.
698  * The callback function is also called when the header buffer in the context becomes full, If it
699  * is required to process the context in the current context, fw_iso_context_flush_completions() is
700  * available instead.
701  *
702  * Context: Any context.
703  */
704 static inline void fw_iso_context_schedule_flush_completions(struct fw_iso_context *ctx)
705 {
706 	queue_work(ctx->card->isoc_wq, &ctx->work);
707 }
708 
709 int fw_iso_context_start(struct fw_iso_context *ctx,
710 			 int cycle, int sync, int tags);
711 int fw_iso_context_stop(struct fw_iso_context *ctx);
712 void fw_iso_context_destroy(struct fw_iso_context *ctx);
713 void fw_iso_resource_manage(struct fw_card *card, int generation,
714 			    u64 channels_mask, int *channel, int *bandwidth,
715 			    bool allocate);
716 
717 extern struct workqueue_struct *fw_workqueue;
718 
719 #endif /* _LINUX_FIREWIRE_H */
720