1 #ifndef _FIREWIRE_CORE_H
2 #define _FIREWIRE_CORE_H
4 #include <linux/dma-mapping.h>
6 #include <linux/list.h>
8 #include <linux/mm_types.h>
9 #include <linux/rwsem.h>
10 #include <linux/slab.h>
11 #include <linux/types.h>
13 #include <asm/atomic.h>
19 struct fw_iso_context;
27 /* bitfields within the PHY registers */
28 #define PHY_LINK_ACTIVE 0x80
29 #define PHY_CONTENDER 0x40
30 #define PHY_BUS_RESET 0x40
31 #define PHY_BUS_SHORT_RESET 0x40
33 #define BANDWIDTH_AVAILABLE_INITIAL 4915
34 #define BROADCAST_CHANNEL_INITIAL (1 << 31 | 31)
35 #define BROADCAST_CHANNEL_VALID (1 << 30)
37 struct fw_card_driver {
39 * Enable the given card with the given initial config rom.
40 * This function is expected to activate the card, and either
41 * enable the PHY or set the link_on bit and initiate a bus
44 int (*enable)(struct fw_card *card, u32 *config_rom, size_t length);
46 int (*update_phy_reg)(struct fw_card *card, int address,
47 int clear_bits, int set_bits);
50 * Update the config rom for an enabled card. This function
51 * should change the config rom that is presented on the bus
52 * an initiate a bus reset.
54 int (*set_config_rom)(struct fw_card *card,
55 u32 *config_rom, size_t length);
57 void (*send_request)(struct fw_card *card, struct fw_packet *packet);
58 void (*send_response)(struct fw_card *card, struct fw_packet *packet);
59 /* Calling cancel is valid once a packet has been submitted. */
60 int (*cancel_packet)(struct fw_card *card, struct fw_packet *packet);
63 * Allow the specified node ID to do direct DMA out and in of
64 * host memory. The card will disable this for all node when
65 * a bus reset happens, so driver need to reenable this after
66 * bus reset. Returns 0 on success, -ENODEV if the card
67 * doesn't support this, -ESTALE if the generation doesn't
70 int (*enable_phys_dma)(struct fw_card *card,
71 int node_id, int generation);
73 u64 (*get_bus_time)(struct fw_card *card);
75 struct fw_iso_context *
76 (*allocate_iso_context)(struct fw_card *card,
77 int type, int channel, size_t header_size);
78 void (*free_iso_context)(struct fw_iso_context *ctx);
80 int (*start_iso)(struct fw_iso_context *ctx,
81 s32 cycle, u32 sync, u32 tags);
83 int (*queue_iso)(struct fw_iso_context *ctx,
84 struct fw_iso_packet *packet,
85 struct fw_iso_buffer *buffer,
86 unsigned long payload);
88 int (*stop_iso)(struct fw_iso_context *ctx);
91 void fw_card_initialize(struct fw_card *card,
92 const struct fw_card_driver *driver, struct device *device);
93 int fw_card_add(struct fw_card *card,
94 u32 max_receive, u32 link_speed, u64 guid);
95 void fw_core_remove_card(struct fw_card *card);
96 int fw_core_initiate_bus_reset(struct fw_card *card, int short_reset);
97 int fw_compute_block_crc(u32 *block);
98 void fw_schedule_bm_work(struct fw_card *card, unsigned long delay);
100 struct fw_descriptor {
101 struct list_head link;
108 int fw_core_add_descriptor(struct fw_descriptor *desc);
109 void fw_core_remove_descriptor(struct fw_descriptor *desc);
114 extern const struct file_operations fw_device_ops;
116 void fw_device_cdev_update(struct fw_device *device);
117 void fw_device_cdev_remove(struct fw_device *device);
122 extern struct rw_semaphore fw_device_rwsem;
123 extern struct idr fw_device_idr;
124 extern int fw_cdev_major;
126 struct fw_device *fw_device_get_by_devt(dev_t devt);
127 void fw_device_set_broadcast_channel(struct fw_device *device, int generation);
128 void fw_node_event(struct fw_card *card, struct fw_node *node, int event);
134 * The iso packet format allows for an immediate header/payload part
135 * stored in 'header' immediately after the packet info plus an
136 * indirect payload part that is pointer to by the 'payload' field.
137 * Applications can use one or the other or both to implement simple
138 * low-bandwidth streaming (e.g. audio) or more advanced
139 * scatter-gather streaming (e.g. assembling video frame automatically).
141 struct fw_iso_packet {
142 u16 payload_length; /* Length of indirect payload. */
143 u32 interrupt:1; /* Generate interrupt on this packet */
144 u32 skip:1; /* Set to not send packet at all. */
147 u32 header_length:8; /* Length of immediate header. */
151 #define FW_ISO_CONTEXT_TRANSMIT 0
152 #define FW_ISO_CONTEXT_RECEIVE 1
154 #define FW_ISO_CONTEXT_MATCH_TAG0 1
155 #define FW_ISO_CONTEXT_MATCH_TAG1 2
156 #define FW_ISO_CONTEXT_MATCH_TAG2 4
157 #define FW_ISO_CONTEXT_MATCH_TAG3 8
158 #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15
161 * An iso buffer is just a set of pages mapped for DMA in the
162 * specified direction. Since the pages are to be used for DMA, they
163 * are not mapped into the kernel virtual address space. We store the
164 * DMA address in the page private. The helper function
165 * fw_iso_buffer_map() will map the pages into a given vma.
167 struct fw_iso_buffer {
168 enum dma_data_direction direction;
173 typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
174 u32 cycle, size_t header_length,
175 void *header, void *data);
177 struct fw_iso_context {
178 struct fw_card *card;
183 fw_iso_callback_t callback;
187 int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
188 int page_count, enum dma_data_direction direction);
189 int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma);
190 void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
192 struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
193 int type, int channel, int speed, size_t header_size,
194 fw_iso_callback_t callback, void *callback_data);
195 int fw_iso_context_queue(struct fw_iso_context *ctx,
196 struct fw_iso_packet *packet,
197 struct fw_iso_buffer *buffer,
198 unsigned long payload);
199 int fw_iso_context_start(struct fw_iso_context *ctx,
200 int cycle, int sync, int tags);
201 int fw_iso_context_stop(struct fw_iso_context *ctx);
202 void fw_iso_context_destroy(struct fw_iso_context *ctx);
204 void fw_iso_resource_manage(struct fw_card *card, int generation,
205 u64 channels_mask, int *channel, int *bandwidth, bool allocate);
216 FW_NODE_INITIATED_RESET,
224 u8 initiated_reset:1;
226 u8 phy_speed:2; /* As in the self ID packet. */
227 u8 max_speed:2; /* Minimum of all phy-speeds on the path from the
228 * local node to this node. */
229 u8 max_depth:4; /* Maximum depth to any leaf node */
230 u8 max_hops:4; /* Max hops in this sub tree */
233 /* For serializing node topology into a list. */
234 struct list_head link;
236 /* Upper layer specific data. */
239 struct fw_node *ports[0];
242 static inline struct fw_node *fw_node_get(struct fw_node *node)
244 atomic_inc(&node->ref_count);
249 static inline void fw_node_put(struct fw_node *node)
251 if (atomic_dec_and_test(&node->ref_count))
255 void fw_core_handle_bus_reset(struct fw_card *card, int node_id,
256 int generation, int self_id_count, u32 *self_ids);
257 void fw_destroy_nodes(struct fw_card *card);
260 * Check whether new_generation is the immediate successor of old_generation.
261 * Take counter roll-over at 255 (as per OHCI) into account.
263 static inline bool is_next_generation(int new_generation, int old_generation)
265 return (new_generation & 0xff) == ((old_generation + 1) & 0xff);
271 #define TCODE_IS_READ_REQUEST(tcode) (((tcode) & ~1) == 4)
272 #define TCODE_IS_BLOCK_PACKET(tcode) (((tcode) & 1) != 0)
273 #define TCODE_IS_REQUEST(tcode) (((tcode) & 2) == 0)
274 #define TCODE_IS_RESPONSE(tcode) (((tcode) & 2) != 0)
275 #define TCODE_HAS_REQUEST_DATA(tcode) (((tcode) & 12) != 4)
276 #define TCODE_HAS_RESPONSE_DATA(tcode) (((tcode) & 12) != 0)
278 #define LOCAL_BUS 0xffc0
280 void fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
281 void fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
282 void fw_fill_response(struct fw_packet *response, u32 *request_header,
283 int rcode, void *payload, size_t length);
284 void fw_flush_transactions(struct fw_card *card);
285 void fw_send_phy_config(struct fw_card *card,
286 int node_id, int generation, int gap_count);
288 static inline int fw_stream_packet_destination_id(int tag, int channel, int sy)
290 return tag << 14 | channel << 8 | sy;
293 #endif /* _FIREWIRE_CORE_H */