2 * FireDTV driver (formerly known as FireSAT)
4 * Copyright (C) 2004 Andreas Monitzer <andy@monitzer.com>
5 * Copyright (C) 2008 Henrik Kurelid <henrik@kurelid.se>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/mutex.h>
16 #include <linux/types.h>
18 #include <asm/byteorder.h>
27 #define CMP_OUTPUT_PLUG_CONTROL_REG_0 0xfffff0000904ULL
29 static int cmp_read(struct firesat *firesat, void *buf, u64 addr, size_t len)
33 if (mutex_lock_interruptible(&firesat->avc_mutex))
36 ret = hpsb_node_read(firesat->ud->ne, addr, buf, len);
38 dev_err(&firesat->ud->device, "CMP: read I/O error\n");
40 mutex_unlock(&firesat->avc_mutex);
44 static int cmp_lock(struct firesat *firesat, void *data, u64 addr, __be32 arg,
49 if (mutex_lock_interruptible(&firesat->avc_mutex))
52 ret = hpsb_node_lock(firesat->ud->ne, addr, ext_tcode, data,
53 (__force quadlet_t)arg);
55 dev_err(&firesat->ud->device, "CMP: lock I/O error\n");
57 mutex_unlock(&firesat->avc_mutex);
61 static inline u32 get_opcr(__be32 opcr, u32 mask, u32 shift)
63 return (be32_to_cpu(opcr) >> shift) & mask;
66 static inline void set_opcr(__be32 *opcr, u32 value, u32 mask, u32 shift)
68 *opcr &= ~cpu_to_be32(mask << shift);
69 *opcr |= cpu_to_be32((value & mask) << shift);
72 #define get_opcr_online(v) get_opcr((v), 0x1, 31)
73 #define get_opcr_p2p_connections(v) get_opcr((v), 0x3f, 24)
74 #define get_opcr_channel(v) get_opcr((v), 0x3f, 16)
76 #define set_opcr_p2p_connections(p, v) set_opcr((p), (v), 0x3f, 24)
77 #define set_opcr_channel(p, v) set_opcr((p), (v), 0x3f, 16)
78 #define set_opcr_data_rate(p, v) set_opcr((p), (v), 0x3, 14)
79 #define set_opcr_overhead_id(p, v) set_opcr((p), (v), 0xf, 10)
81 int cmp_establish_pp_connection(struct firesat *firesat, int plug, int channel)
83 __be32 old_opcr, opcr;
84 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
88 ret = cmp_read(firesat, &opcr, opcr_address, 4);
93 if (!get_opcr_online(opcr)) {
94 dev_err(&firesat->ud->device, "CMP: output offline\n");
100 if (get_opcr_p2p_connections(opcr)) {
101 if (get_opcr_channel(opcr) != channel) {
102 dev_err(&firesat->ud->device,
103 "CMP: cannot change channel\n");
106 dev_info(&firesat->ud->device,
107 "CMP: overlaying existing connection\n");
109 /* We don't allocate isochronous resources. */
111 set_opcr_channel(&opcr, channel);
112 set_opcr_data_rate(&opcr, IEEE1394_SPEED_400);
114 /* FIXME: this is for the worst case - optimize */
115 set_opcr_overhead_id(&opcr, 0);
117 /* FIXME: allocate isochronous channel and bandwidth at IRM */
120 set_opcr_p2p_connections(&opcr, get_opcr_p2p_connections(opcr) + 1);
122 ret = cmp_lock(firesat, &opcr, opcr_address, old_opcr, 2);
126 if (old_opcr != opcr) {
128 * FIXME: if old_opcr.P2P_Connections > 0,
129 * deallocate isochronous channel and bandwidth at IRM
132 if (++attempts < 6) /* arbitrary limit */
140 void cmp_break_pp_connection(struct firesat *firesat, int plug, int channel)
142 __be32 old_opcr, opcr;
143 u64 opcr_address = CMP_OUTPUT_PLUG_CONTROL_REG_0 + (plug << 2);
146 if (cmp_read(firesat, &opcr, opcr_address, 4) < 0)
150 if (!get_opcr_online(opcr) || !get_opcr_p2p_connections(opcr) ||
151 get_opcr_channel(opcr) != channel) {
152 dev_err(&firesat->ud->device, "CMP: no connection to break\n");
157 set_opcr_p2p_connections(&opcr, get_opcr_p2p_connections(opcr) - 1);
159 if (cmp_lock(firesat, &opcr, opcr_address, old_opcr, 2) < 0)
162 if (old_opcr != opcr) {
164 * FIXME: if old_opcr.P2P_Connections == 1, i.e. we were last
165 * owner, deallocate isochronous channel and bandwidth at IRM
168 if (++attempts < 6) /* arbitrary limit */