core.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #ifndef _FIREWIRE_CORE_H
  2. #define _FIREWIRE_CORE_H
  3. #include <linux/dma-mapping.h>
  4. #include <linux/fs.h>
  5. #include <linux/list.h>
  6. #include <linux/idr.h>
  7. #include <linux/mm_types.h>
  8. #include <linux/rwsem.h>
  9. #include <linux/slab.h>
  10. #include <linux/types.h>
  11. #include <asm/atomic.h>
  12. struct device;
  13. struct fw_card;
  14. struct fw_device;
  15. struct fw_iso_buffer;
  16. struct fw_iso_context;
  17. struct fw_iso_packet;
  18. struct fw_node;
  19. struct fw_packet;
  20. /* -card */
  21. /* bitfields within the PHY registers */
  22. #define PHY_LINK_ACTIVE 0x80
  23. #define PHY_CONTENDER 0x40
  24. #define PHY_BUS_RESET 0x40
  25. #define PHY_BUS_SHORT_RESET 0x40
  26. #define BANDWIDTH_AVAILABLE_INITIAL 4915
  27. #define BROADCAST_CHANNEL_INITIAL (1 << 31 | 31)
  28. #define BROADCAST_CHANNEL_VALID (1 << 30)
  29. struct fw_card_driver {
  30. /*
  31. * Enable the given card with the given initial config rom.
  32. * This function is expected to activate the card, and either
  33. * enable the PHY or set the link_on bit and initiate a bus
  34. * reset.
  35. */
  36. int (*enable)(struct fw_card *card, u32 *config_rom, size_t length);
  37. int (*update_phy_reg)(struct fw_card *card, int address,
  38. int clear_bits, int set_bits);
  39. /*
  40. * Update the config rom for an enabled card. This function
  41. * should change the config rom that is presented on the bus
  42. * an initiate a bus reset.
  43. */
  44. int (*set_config_rom)(struct fw_card *card,
  45. u32 *config_rom, size_t length);
  46. void (*send_request)(struct fw_card *card, struct fw_packet *packet);
  47. void (*send_response)(struct fw_card *card, struct fw_packet *packet);
  48. /* Calling cancel is valid once a packet has been submitted. */
  49. int (*cancel_packet)(struct fw_card *card, struct fw_packet *packet);
  50. /*
  51. * Allow the specified node ID to do direct DMA out and in of
  52. * host memory. The card will disable this for all node when
  53. * a bus reset happens, so driver need to reenable this after
  54. * bus reset. Returns 0 on success, -ENODEV if the card
  55. * doesn't support this, -ESTALE if the generation doesn't
  56. * match.
  57. */
  58. int (*enable_phys_dma)(struct fw_card *card,
  59. int node_id, int generation);
  60. u64 (*get_bus_time)(struct fw_card *card);
  61. struct fw_iso_context *
  62. (*allocate_iso_context)(struct fw_card *card,
  63. int type, int channel, size_t header_size);
  64. void (*free_iso_context)(struct fw_iso_context *ctx);
  65. int (*start_iso)(struct fw_iso_context *ctx,
  66. s32 cycle, u32 sync, u32 tags);
  67. int (*queue_iso)(struct fw_iso_context *ctx,
  68. struct fw_iso_packet *packet,
  69. struct fw_iso_buffer *buffer,
  70. unsigned long payload);
  71. int (*stop_iso)(struct fw_iso_context *ctx);
  72. };
  73. void fw_card_initialize(struct fw_card *card,
  74. const struct fw_card_driver *driver, struct device *device);
  75. int fw_card_add(struct fw_card *card,
  76. u32 max_receive, u32 link_speed, u64 guid);
  77. void fw_core_remove_card(struct fw_card *card);
  78. int fw_core_initiate_bus_reset(struct fw_card *card, int short_reset);
  79. int fw_compute_block_crc(u32 *block);
  80. void fw_schedule_bm_work(struct fw_card *card, unsigned long delay);
  81. struct fw_descriptor {
  82. struct list_head link;
  83. size_t length;
  84. u32 immediate;
  85. u32 key;
  86. const u32 *data;
  87. };
  88. int fw_core_add_descriptor(struct fw_descriptor *desc);
  89. void fw_core_remove_descriptor(struct fw_descriptor *desc);
  90. /* -cdev */
  91. extern const struct file_operations fw_device_ops;
  92. void fw_device_cdev_update(struct fw_device *device);
  93. void fw_device_cdev_remove(struct fw_device *device);
  94. /* -device */
  95. extern struct rw_semaphore fw_device_rwsem;
  96. extern struct idr fw_device_idr;
  97. extern int fw_cdev_major;
  98. struct fw_device *fw_device_get_by_devt(dev_t devt);
  99. int fw_device_set_broadcast_channel(struct device *dev, void *gen);
  100. void fw_node_event(struct fw_card *card, struct fw_node *node, int event);
  101. /* -iso */
  102. /*
  103. * The iso packet format allows for an immediate header/payload part
  104. * stored in 'header' immediately after the packet info plus an
  105. * indirect payload part that is pointer to by the 'payload' field.
  106. * Applications can use one or the other or both to implement simple
  107. * low-bandwidth streaming (e.g. audio) or more advanced
  108. * scatter-gather streaming (e.g. assembling video frame automatically).
  109. */
  110. struct fw_iso_packet {
  111. u16 payload_length; /* Length of indirect payload. */
  112. u32 interrupt:1; /* Generate interrupt on this packet */
  113. u32 skip:1; /* Set to not send packet at all. */
  114. u32 tag:2;
  115. u32 sy:4;
  116. u32 header_length:8; /* Length of immediate header. */
  117. u32 header[0];
  118. };
  119. #define FW_ISO_CONTEXT_TRANSMIT 0
  120. #define FW_ISO_CONTEXT_RECEIVE 1
  121. #define FW_ISO_CONTEXT_MATCH_TAG0 1
  122. #define FW_ISO_CONTEXT_MATCH_TAG1 2
  123. #define FW_ISO_CONTEXT_MATCH_TAG2 4
  124. #define FW_ISO_CONTEXT_MATCH_TAG3 8
  125. #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15
  126. /*
  127. * An iso buffer is just a set of pages mapped for DMA in the
  128. * specified direction. Since the pages are to be used for DMA, they
  129. * are not mapped into the kernel virtual address space. We store the
  130. * DMA address in the page private. The helper function
  131. * fw_iso_buffer_map() will map the pages into a given vma.
  132. */
  133. struct fw_iso_buffer {
  134. enum dma_data_direction direction;
  135. struct page **pages;
  136. int page_count;
  137. };
  138. typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
  139. u32 cycle, size_t header_length,
  140. void *header, void *data);
  141. struct fw_iso_context {
  142. struct fw_card *card;
  143. int type;
  144. int channel;
  145. int speed;
  146. size_t header_size;
  147. fw_iso_callback_t callback;
  148. void *callback_data;
  149. };
  150. int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  151. int page_count, enum dma_data_direction direction);
  152. int fw_iso_buffer_map(struct fw_iso_buffer *buffer, struct vm_area_struct *vma);
  153. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
  154. struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
  155. int type, int channel, int speed, size_t header_size,
  156. fw_iso_callback_t callback, void *callback_data);
  157. int fw_iso_context_queue(struct fw_iso_context *ctx,
  158. struct fw_iso_packet *packet,
  159. struct fw_iso_buffer *buffer,
  160. unsigned long payload);
  161. int fw_iso_context_start(struct fw_iso_context *ctx,
  162. int cycle, int sync, int tags);
  163. int fw_iso_context_stop(struct fw_iso_context *ctx);
  164. void fw_iso_context_destroy(struct fw_iso_context *ctx);
  165. void fw_iso_resource_manage(struct fw_card *card, int generation,
  166. u64 channels_mask, int *channel, int *bandwidth, bool allocate);
  167. /* -topology */
  168. enum {
  169. FW_NODE_CREATED,
  170. FW_NODE_UPDATED,
  171. FW_NODE_DESTROYED,
  172. FW_NODE_LINK_ON,
  173. FW_NODE_LINK_OFF,
  174. FW_NODE_INITIATED_RESET,
  175. };
  176. struct fw_node {
  177. u16 node_id;
  178. u8 color;
  179. u8 port_count;
  180. u8 link_on:1;
  181. u8 initiated_reset:1;
  182. u8 b_path:1;
  183. u8 phy_speed:2; /* As in the self ID packet. */
  184. u8 max_speed:2; /* Minimum of all phy-speeds on the path from the
  185. * local node to this node. */
  186. u8 max_depth:4; /* Maximum depth to any leaf node */
  187. u8 max_hops:4; /* Max hops in this sub tree */
  188. atomic_t ref_count;
  189. /* For serializing node topology into a list. */
  190. struct list_head link;
  191. /* Upper layer specific data. */
  192. void *data;
  193. struct fw_node *ports[0];
  194. };
  195. static inline struct fw_node *fw_node_get(struct fw_node *node)
  196. {
  197. atomic_inc(&node->ref_count);
  198. return node;
  199. }
  200. static inline void fw_node_put(struct fw_node *node)
  201. {
  202. if (atomic_dec_and_test(&node->ref_count))
  203. kfree(node);
  204. }
  205. void fw_core_handle_bus_reset(struct fw_card *card, int node_id,
  206. int generation, int self_id_count, u32 *self_ids);
  207. void fw_destroy_nodes(struct fw_card *card);
  208. /*
  209. * Check whether new_generation is the immediate successor of old_generation.
  210. * Take counter roll-over at 255 (as per OHCI) into account.
  211. */
  212. static inline bool is_next_generation(int new_generation, int old_generation)
  213. {
  214. return (new_generation & 0xff) == ((old_generation + 1) & 0xff);
  215. }
  216. /* -transaction */
  217. #define TCODE_IS_READ_REQUEST(tcode) (((tcode) & ~1) == 4)
  218. #define TCODE_IS_BLOCK_PACKET(tcode) (((tcode) & 1) != 0)
  219. #define TCODE_IS_REQUEST(tcode) (((tcode) & 2) == 0)
  220. #define TCODE_IS_RESPONSE(tcode) (((tcode) & 2) != 0)
  221. #define TCODE_HAS_REQUEST_DATA(tcode) (((tcode) & 12) != 4)
  222. #define TCODE_HAS_RESPONSE_DATA(tcode) (((tcode) & 12) != 0)
  223. #define LOCAL_BUS 0xffc0
  224. void fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
  225. void fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
  226. void fw_fill_response(struct fw_packet *response, u32 *request_header,
  227. int rcode, void *payload, size_t length);
  228. void fw_flush_transactions(struct fw_card *card);
  229. void fw_send_phy_config(struct fw_card *card,
  230. int node_id, int generation, int gap_count);
  231. static inline int fw_stream_packet_destination_id(int tag, int channel, int sy)
  232. {
  233. return tag << 14 | channel << 8 | sy;
  234. }
  235. #endif /* _FIREWIRE_CORE_H */