firewire.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #ifndef _LINUX_FIREWIRE_H
  2. #define _LINUX_FIREWIRE_H
  3. #include <linux/completion.h>
  4. #include <linux/device.h>
  5. #include <linux/dma-mapping.h>
  6. #include <linux/kernel.h>
  7. #include <linux/kref.h>
  8. #include <linux/list.h>
  9. #include <linux/mutex.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/timer.h>
  13. #include <linux/types.h>
  14. #include <linux/workqueue.h>
  15. #include <asm/atomic.h>
  16. #include <asm/byteorder.h>
  17. #define fw_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, ## args)
  18. #define fw_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
  19. static inline void fw_memcpy_from_be32(void *_dst, void *_src, size_t size)
  20. {
  21. u32 *dst = _dst;
  22. __be32 *src = _src;
  23. int i;
  24. for (i = 0; i < size / 4; i++)
  25. dst[i] = be32_to_cpu(src[i]);
  26. }
  27. static inline void fw_memcpy_to_be32(void *_dst, void *_src, size_t size)
  28. {
  29. fw_memcpy_from_be32(_dst, _src, size);
  30. }
  31. #define CSR_REGISTER_BASE 0xfffff0000000ULL
  32. /* register offsets are relative to CSR_REGISTER_BASE */
  33. #define CSR_STATE_CLEAR 0x0
  34. #define CSR_STATE_SET 0x4
  35. #define CSR_NODE_IDS 0x8
  36. #define CSR_RESET_START 0xc
  37. #define CSR_SPLIT_TIMEOUT_HI 0x18
  38. #define CSR_SPLIT_TIMEOUT_LO 0x1c
  39. #define CSR_CYCLE_TIME 0x200
  40. #define CSR_BUS_TIME 0x204
  41. #define CSR_BUSY_TIMEOUT 0x210
  42. #define CSR_BUS_MANAGER_ID 0x21c
  43. #define CSR_BANDWIDTH_AVAILABLE 0x220
  44. #define CSR_CHANNELS_AVAILABLE 0x224
  45. #define CSR_CHANNELS_AVAILABLE_HI 0x224
  46. #define CSR_CHANNELS_AVAILABLE_LO 0x228
  47. #define CSR_BROADCAST_CHANNEL 0x234
  48. #define CSR_CONFIG_ROM 0x400
  49. #define CSR_CONFIG_ROM_END 0x800
  50. #define CSR_FCP_COMMAND 0xB00
  51. #define CSR_FCP_RESPONSE 0xD00
  52. #define CSR_FCP_END 0xF00
  53. #define CSR_TOPOLOGY_MAP 0x1000
  54. #define CSR_TOPOLOGY_MAP_END 0x1400
  55. #define CSR_SPEED_MAP 0x2000
  56. #define CSR_SPEED_MAP_END 0x3000
  57. #define CSR_OFFSET 0x40
  58. #define CSR_LEAF 0x80
  59. #define CSR_DIRECTORY 0xc0
  60. #define CSR_DESCRIPTOR 0x01
  61. #define CSR_VENDOR 0x03
  62. #define CSR_HARDWARE_VERSION 0x04
  63. #define CSR_NODE_CAPABILITIES 0x0c
  64. #define CSR_UNIT 0x11
  65. #define CSR_SPECIFIER_ID 0x12
  66. #define CSR_VERSION 0x13
  67. #define CSR_DEPENDENT_INFO 0x14
  68. #define CSR_MODEL 0x17
  69. #define CSR_INSTANCE 0x18
  70. #define CSR_DIRECTORY_ID 0x20
  71. struct fw_csr_iterator {
  72. u32 *p;
  73. u32 *end;
  74. };
  75. void fw_csr_iterator_init(struct fw_csr_iterator *ci, u32 *p);
  76. int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value);
  77. extern struct bus_type fw_bus_type;
  78. struct fw_card_driver;
  79. struct fw_node;
  80. struct fw_card {
  81. const struct fw_card_driver *driver;
  82. struct device *device;
  83. struct kref kref;
  84. struct completion done;
  85. int node_id;
  86. int generation;
  87. int current_tlabel;
  88. u64 tlabel_mask;
  89. struct list_head transaction_list;
  90. struct timer_list flush_timer;
  91. unsigned long reset_jiffies;
  92. unsigned long long guid;
  93. unsigned max_receive;
  94. int link_speed;
  95. int config_rom_generation;
  96. spinlock_t lock; /* Take this lock when handling the lists in
  97. * this struct. */
  98. struct fw_node *local_node;
  99. struct fw_node *root_node;
  100. struct fw_node *irm_node;
  101. u8 color; /* must be u8 to match the definition in struct fw_node */
  102. int gap_count;
  103. bool beta_repeaters_present;
  104. int index;
  105. struct list_head link;
  106. /* Work struct for BM duties. */
  107. struct delayed_work work;
  108. int bm_retries;
  109. int bm_generation;
  110. bool broadcast_channel_allocated;
  111. u32 broadcast_channel;
  112. u32 topology_map[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4];
  113. };
  114. static inline struct fw_card *fw_card_get(struct fw_card *card)
  115. {
  116. kref_get(&card->kref);
  117. return card;
  118. }
  119. void fw_card_release(struct kref *kref);
  120. static inline void fw_card_put(struct fw_card *card)
  121. {
  122. kref_put(&card->kref, fw_card_release);
  123. }
  124. struct fw_attribute_group {
  125. struct attribute_group *groups[2];
  126. struct attribute_group group;
  127. struct attribute *attrs[12];
  128. };
  129. enum fw_device_state {
  130. FW_DEVICE_INITIALIZING,
  131. FW_DEVICE_RUNNING,
  132. FW_DEVICE_GONE,
  133. FW_DEVICE_SHUTDOWN,
  134. };
  135. /*
  136. * Note, fw_device.generation always has to be read before fw_device.node_id.
  137. * Use SMP memory barriers to ensure this. Otherwise requests will be sent
  138. * to an outdated node_id if the generation was updated in the meantime due
  139. * to a bus reset.
  140. *
  141. * Likewise, fw-core will take care to update .node_id before .generation so
  142. * that whenever fw_device.generation is current WRT the actual bus generation,
  143. * fw_device.node_id is guaranteed to be current too.
  144. *
  145. * The same applies to fw_device.card->node_id vs. fw_device.generation.
  146. *
  147. * fw_device.config_rom and fw_device.config_rom_length may be accessed during
  148. * the lifetime of any fw_unit belonging to the fw_device, before device_del()
  149. * was called on the last fw_unit. Alternatively, they may be accessed while
  150. * holding fw_device_rwsem.
  151. */
  152. struct fw_device {
  153. atomic_t state;
  154. struct fw_node *node;
  155. int node_id;
  156. int generation;
  157. unsigned max_speed;
  158. struct fw_card *card;
  159. struct device device;
  160. struct mutex client_list_mutex;
  161. struct list_head client_list;
  162. u32 *config_rom;
  163. size_t config_rom_length;
  164. int config_rom_retries;
  165. unsigned is_local:1;
  166. unsigned max_rec:4;
  167. unsigned cmc:1;
  168. unsigned irmc:1;
  169. unsigned bc_implemented:2;
  170. struct delayed_work work;
  171. struct fw_attribute_group attribute_group;
  172. };
  173. static inline struct fw_device *fw_device(struct device *dev)
  174. {
  175. return container_of(dev, struct fw_device, device);
  176. }
  177. static inline int fw_device_is_shutdown(struct fw_device *device)
  178. {
  179. return atomic_read(&device->state) == FW_DEVICE_SHUTDOWN;
  180. }
  181. static inline struct fw_device *fw_device_get(struct fw_device *device)
  182. {
  183. get_device(&device->device);
  184. return device;
  185. }
  186. static inline void fw_device_put(struct fw_device *device)
  187. {
  188. put_device(&device->device);
  189. }
  190. int fw_device_enable_phys_dma(struct fw_device *device);
  191. /*
  192. * fw_unit.directory must not be accessed after device_del(&fw_unit.device).
  193. */
  194. struct fw_unit {
  195. struct device device;
  196. u32 *directory;
  197. struct fw_attribute_group attribute_group;
  198. };
  199. static inline struct fw_unit *fw_unit(struct device *dev)
  200. {
  201. return container_of(dev, struct fw_unit, device);
  202. }
  203. static inline struct fw_unit *fw_unit_get(struct fw_unit *unit)
  204. {
  205. get_device(&unit->device);
  206. return unit;
  207. }
  208. static inline void fw_unit_put(struct fw_unit *unit)
  209. {
  210. put_device(&unit->device);
  211. }
  212. static inline struct fw_device *fw_parent_device(struct fw_unit *unit)
  213. {
  214. return fw_device(unit->device.parent);
  215. }
  216. struct ieee1394_device_id;
  217. struct fw_driver {
  218. struct device_driver driver;
  219. /* Called when the parent device sits through a bus reset. */
  220. void (*update)(struct fw_unit *unit);
  221. const struct ieee1394_device_id *id_table;
  222. };
  223. struct fw_packet;
  224. struct fw_request;
  225. typedef void (*fw_packet_callback_t)(struct fw_packet *packet,
  226. struct fw_card *card, int status);
  227. typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
  228. void *data, size_t length,
  229. void *callback_data);
  230. /*
  231. * Important note: The callback must guarantee that either fw_send_response()
  232. * or kfree() is called on the @request.
  233. */
  234. typedef void (*fw_address_callback_t)(struct fw_card *card,
  235. struct fw_request *request,
  236. int tcode, int destination, int source,
  237. int generation, int speed,
  238. unsigned long long offset,
  239. void *data, size_t length,
  240. void *callback_data);
  241. struct fw_packet {
  242. int speed;
  243. int generation;
  244. u32 header[4];
  245. size_t header_length;
  246. void *payload;
  247. size_t payload_length;
  248. dma_addr_t payload_bus;
  249. u32 timestamp;
  250. /*
  251. * This callback is called when the packet transmission has
  252. * completed; for successful transmission, the status code is
  253. * the ack received from the destination, otherwise it's a
  254. * negative errno: ENOMEM, ESTALE, ETIMEDOUT, ENODEV, EIO.
  255. * The callback can be called from tasklet context and thus
  256. * must never block.
  257. */
  258. fw_packet_callback_t callback;
  259. int ack;
  260. struct list_head link;
  261. void *driver_data;
  262. };
  263. struct fw_transaction {
  264. int node_id; /* The generation is implied; it is always the current. */
  265. int tlabel;
  266. int timestamp;
  267. struct list_head link;
  268. struct fw_packet packet;
  269. /*
  270. * The data passed to the callback is valid only during the
  271. * callback.
  272. */
  273. fw_transaction_callback_t callback;
  274. void *callback_data;
  275. };
  276. struct fw_address_handler {
  277. u64 offset;
  278. size_t length;
  279. fw_address_callback_t address_callback;
  280. void *callback_data;
  281. struct list_head link;
  282. };
  283. struct fw_address_region {
  284. u64 start;
  285. u64 end;
  286. };
  287. extern const struct fw_address_region fw_high_memory_region;
  288. int fw_core_add_address_handler(struct fw_address_handler *handler,
  289. const struct fw_address_region *region);
  290. void fw_core_remove_address_handler(struct fw_address_handler *handler);
  291. void fw_send_response(struct fw_card *card,
  292. struct fw_request *request, int rcode);
  293. void fw_send_request(struct fw_card *card, struct fw_transaction *t,
  294. int tcode, int destination_id, int generation, int speed,
  295. unsigned long long offset, void *payload, size_t length,
  296. fw_transaction_callback_t callback, void *callback_data);
  297. int fw_cancel_transaction(struct fw_card *card,
  298. struct fw_transaction *transaction);
  299. int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
  300. int generation, int speed, unsigned long long offset,
  301. void *payload, size_t length);
  302. static inline int fw_stream_packet_destination_id(int tag, int channel, int sy)
  303. {
  304. return tag << 14 | channel << 8 | sy;
  305. }
  306. struct fw_descriptor {
  307. struct list_head link;
  308. size_t length;
  309. u32 immediate;
  310. u32 key;
  311. const u32 *data;
  312. };
  313. int fw_core_add_descriptor(struct fw_descriptor *desc);
  314. void fw_core_remove_descriptor(struct fw_descriptor *desc);
  315. /*
  316. * The iso packet format allows for an immediate header/payload part
  317. * stored in 'header' immediately after the packet info plus an
  318. * indirect payload part that is pointer to by the 'payload' field.
  319. * Applications can use one or the other or both to implement simple
  320. * low-bandwidth streaming (e.g. audio) or more advanced
  321. * scatter-gather streaming (e.g. assembling video frame automatically).
  322. */
  323. struct fw_iso_packet {
  324. u16 payload_length; /* Length of indirect payload. */
  325. u32 interrupt:1; /* Generate interrupt on this packet */
  326. u32 skip:1; /* Set to not send packet at all. */
  327. u32 tag:2;
  328. u32 sy:4;
  329. u32 header_length:8; /* Length of immediate header. */
  330. u32 header[0];
  331. };
  332. #define FW_ISO_CONTEXT_TRANSMIT 0
  333. #define FW_ISO_CONTEXT_RECEIVE 1
  334. #define FW_ISO_CONTEXT_MATCH_TAG0 1
  335. #define FW_ISO_CONTEXT_MATCH_TAG1 2
  336. #define FW_ISO_CONTEXT_MATCH_TAG2 4
  337. #define FW_ISO_CONTEXT_MATCH_TAG3 8
  338. #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15
  339. /*
  340. * An iso buffer is just a set of pages mapped for DMA in the
  341. * specified direction. Since the pages are to be used for DMA, they
  342. * are not mapped into the kernel virtual address space. We store the
  343. * DMA address in the page private. The helper function
  344. * fw_iso_buffer_map() will map the pages into a given vma.
  345. */
  346. struct fw_iso_buffer {
  347. enum dma_data_direction direction;
  348. struct page **pages;
  349. int page_count;
  350. };
  351. int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
  352. int page_count, enum dma_data_direction direction);
  353. void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
  354. struct fw_iso_context;
  355. typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
  356. u32 cycle, size_t header_length,
  357. void *header, void *data);
  358. struct fw_iso_context {
  359. struct fw_card *card;
  360. int type;
  361. int channel;
  362. int speed;
  363. size_t header_size;
  364. fw_iso_callback_t callback;
  365. void *callback_data;
  366. };
  367. struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
  368. int type, int channel, int speed, size_t header_size,
  369. fw_iso_callback_t callback, void *callback_data);
  370. int fw_iso_context_queue(struct fw_iso_context *ctx,
  371. struct fw_iso_packet *packet,
  372. struct fw_iso_buffer *buffer,
  373. unsigned long payload);
  374. int fw_iso_context_start(struct fw_iso_context *ctx,
  375. int cycle, int sync, int tags);
  376. int fw_iso_context_stop(struct fw_iso_context *ctx);
  377. void fw_iso_context_destroy(struct fw_iso_context *ctx);
  378. #endif /* _LINUX_FIREWIRE_H */