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/kernel.h>
14 #include <linux/mutex.h>
15 #include <linux/types.h>
19 #include <ieee1394_core.h>
20 #include <ieee1394_transactions.h>
29 __u8 PTPConnCount : 6 ; // Point to point connect. counter
30 __u8 BrConnCount : 1 ; // Broadcast connection counter
31 __u8 OnLine : 1 ; // On Line
33 __u8 ChNr : 6 ; // Channel number
34 __u8 Res : 2 ; // Reserved
36 __u8 PayloadHi : 2 ; // Payoad high bits
37 __u8 OvhdID : 4 ; // Overhead ID
38 __u8 DataRate : 2 ; // Data Rate
40 __u8 PayloadLo ; // Payoad low byte
43 #define FIRESAT_SPEED IEEE1394_SPEED_400
45 static int cmp_read(struct firesat *firesat, void *buf, u64 addr, size_t len)
49 if (mutex_lock_interruptible(&firesat->avc_mutex))
52 ret = hpsb_read(firesat->host, firesat->nodeentry->nodeid,
53 firesat->nodeentry->generation, addr, buf, len);
55 mutex_unlock(&firesat->avc_mutex);
59 static int cmp_lock(struct firesat *firesat, quadlet_t *data, u64 addr,
60 quadlet_t arg, int ext_tcode)
64 if (mutex_lock_interruptible(&firesat->avc_mutex))
67 ret = hpsb_lock(firesat->host, firesat->nodeentry->nodeid,
68 firesat->nodeentry->generation,
69 addr, ext_tcode, data, arg);
71 mutex_unlock(&firesat->avc_mutex);
75 //try establishing a point-to-point connection (may be interrupted by a busreset
76 int try_CMPEstablishPPconnection(struct firesat *firesat, int output_plug, int iso_channel) {
77 unsigned int BWU; //bandwidth to allocate
79 quadlet_t old_oPCR,test_oPCR = 0x0;
80 u64 oPCR_address=0xfffff0000904ull+(output_plug << 2);
81 int result=cmp_read(firesat, &test_oPCR, oPCR_address, 4);
83 /* printk(KERN_INFO "%s: nodeid = %d\n",__func__,firesat->nodeentry->nodeid); */
86 printk("%s: cannot read oPCR\n", __func__);
89 /* printk(KERN_INFO "%s: oPCR = %08x\n",__func__,test_oPCR); */
91 OPCR *hilf= (OPCR*) &test_oPCR;
94 printk("%s: Output offline; oPCR: %08x\n", __func__, test_oPCR);
100 if (hilf->PTPConnCount) {
101 if (hilf->ChNr != iso_channel) {
102 printk("%s: Output plug has already connection on channel %u; cannot change it to channel %u\n",__func__,hilf->ChNr,iso_channel);
105 printk(KERN_INFO "%s: Overlaying existing connection; connection counter was: %u\n",__func__, hilf->PTPConnCount);
106 BWU=0; //we allocate no bandwidth (is this necessary?)
108 hilf->ChNr=iso_channel;
109 hilf->DataRate=FIRESAT_SPEED;
111 hilf->OvhdID=0; //FIXME: that is for worst case -> optimize
112 BWU=hilf->OvhdID?hilf->OvhdID*32:512;
113 BWU += (hilf->PayloadLo + (hilf->PayloadHi << 8) +3) * (2 << (3-hilf->DataRate));
114 /* if (allocate_1394_resources(iso_channel,BWU))
116 cout << "Allocation of resources failed\n";
121 hilf->PTPConnCount++;
123 /* printk(KERN_INFO "%s: trying compare_swap...\n",__func__); */
124 /* printk(KERN_INFO "%s: oPCR_old: %08x, oPCR_new: %08x\n",__func__, old_oPCR, new_oPCR); */
125 result=cmp_lock(firesat, &test_oPCR, oPCR_address, old_oPCR, 2);
128 printk("%s: cannot compare_swap oPCR\n",__func__);
131 if ((old_oPCR != test_oPCR) && (!((OPCR*) &old_oPCR)->PTPConnCount))
133 printk("%s: change of oPCR failed -> freeing resources\n",__func__);
134 // hilf= (OPCR*) &new_oPCR;
135 // unsigned int BWU=hilf->OvhdID?hilf->OvhdID*32:512;
136 // BWU += (hilf->Payload+3) * (2 << (3-hilf->DataRate));
137 /* if (deallocate_1394_resources(iso_channel,BWU))
140 cout << "Deallocation of resources failed\n";
146 while (old_oPCR != test_oPCR);
151 //try breaking a point-to-point connection (may be interrupted by a busreset
152 int try_CMPBreakPPconnection(struct firesat *firesat, int output_plug,int iso_channel) {
153 quadlet_t old_oPCR,test_oPCR;
155 u64 oPCR_address=0xfffff0000904ull+(output_plug << 2);
156 int result=cmp_read(firesat, &test_oPCR, oPCR_address, 4);
158 /* printk(KERN_INFO "%s\n",__func__); */
161 printk("%s: cannot read oPCR\n", __func__);
165 OPCR *hilf= (OPCR*) &test_oPCR;
167 if (!hilf->OnLine || !hilf->PTPConnCount || hilf->ChNr != iso_channel) {
168 printk("%s: Output plug does not have PtP-connection on that channel; oPCR: %08x\n", __func__, test_oPCR);
173 hilf->PTPConnCount--;
176 // printk(KERN_INFO "%s: trying compare_swap...\n", __func__);
177 result=cmp_lock(firesat, &test_oPCR, oPCR_address, old_oPCR, 2);
179 printk("%s: cannot compare_swap oPCR\n",__func__);
184 } while (old_oPCR != test_oPCR);
186 /* hilf = (OPCR*) &old_oPCR;
187 if (hilf->PTPConnCount == 1) { // if we were the last owner of this connection
188 cout << "deallocating 1394 resources\n";
189 unsigned int BWU=hilf->OvhdID?hilf->OvhdID*32:512;
190 BWU += (hilf->PayloadLo + (hilf->PayloadHi << 8) +3) * (2 << (3-hilf->DataRate));
191 if (deallocate_1394_resources(iso_channel,BWU))
193 cout << "Deallocation of resources failed\n";