Fixed problem with getting parent block from paths that contain parent
[tangerine.git] / arch / common / hidd.i2c / i2cdeviceclass.c
blob8438fbc772cdfee8159cbf7bb1a5839e85c25fae
1 /*
2 Copyright © 2004, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: PCI device class
6 Lang: English
7 */
9 #include <exec/types.h>
10 #include <hidd/i2c.h>
11 #include <oop/oop.h>
13 #include <utility/tagitem.h>
15 #include <proto/exec.h>
16 #include <proto/utility.h>
17 #include <proto/oop.h>
19 #include "i2c.h"
21 #define DEBUG 1
22 #include <aros/debug.h>
24 #undef HiddI2CAttrBase
25 #undef HiddI2CDeviceAttrBase
27 #define HiddI2CAttrBase (SD(cl)->hiddI2CAB)
28 #define HiddI2CDeviceAttrBase (SD(cl)->hiddI2CDeviceAB)
29 #define HiddAttrBase (SD(cl)->hiddAB)
31 BOOL METHOD(I2CDev, Hidd_I2CDevice, Read)
33 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
35 return I2C_WriteRead(dev->driver, o, NULL, 0, msg->readBuffer, msg->readLength);
38 BOOL METHOD(I2CDev, Hidd_I2CDevice, ReadByte)
40 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
42 return I2C_WriteRead(dev->driver, o, &msg->subaddr, 1, msg->data, 1);
45 BOOL METHOD(I2CDev, Hidd_I2CDevice, ReadBytes)
47 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
49 return I2C_WriteRead(dev->driver, o, &msg->subaddr, 1, msg->data, msg->length);
52 BOOL METHOD(I2CDev, Hidd_I2CDevice, ReadStatus)
54 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
56 return I2C_WriteRead(dev->driver, o, NULL, 0, msg->status, 1);
59 BOOL METHOD(I2CDev, Hidd_I2CDevice, ReadWord)
61 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
62 BOOL r;
63 UBYTE buff[2];
65 if ((r = I2C_WriteRead(dev->driver, o, &msg->subaddr, 1, buff, 2)))
67 *msg->data = (buff[0] << 8) | buff[1];
70 return r;
73 BOOL METHOD(I2CDev, Hidd_I2CDevice, Write)
75 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
77 return I2C_WriteRead(dev->driver, o, msg->writeBuffer, msg->writeLength, NULL, 0);
80 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteByte)
82 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
83 UBYTE buff[2];
85 buff[0] = msg->subaddr;
86 buff[1] = msg->data;
88 return I2C_WriteRead(dev->driver, o, buff, 2, NULL, 0);
91 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteBytes)
93 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
94 BOOL r = TRUE;
95 ULONG nWrite = msg->length;
96 UBYTE *WriteBuffer = msg->data;
98 if (nWrite > 0)
100 if ((r = I2C_Address(dev->driver, o, dev->address & ~1)))
102 if ((r = I2C_PutByte(dev->driver, o, msg->subaddr)))
104 for (; nWrite > 0; WriteBuffer++, nWrite--)
105 if (!(r = I2C_PutByte(dev->driver, o, *WriteBuffer)))
106 break;
109 I2C_Stop(dev->driver, o);
113 return r;
116 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteWord)
118 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
119 UBYTE buff[3];
121 buff[0] = msg->subaddr;
122 buff[1] = msg->data >> 8;
123 buff[2] = msg->data & 0xff;
125 return I2C_WriteRead(dev->driver, o, buff, 2, NULL, 0);
128 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteVec)
130 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
131 BOOL r = TRUE;
132 int s = 0;
133 ULONG nValues = msg->length;
134 UBYTE *vec = msg->data;
136 if (nValues > 0)
138 for (; nValues > 0; nValues--, vec+=2)
140 if (!(r = I2C_Address(dev->driver, o, dev->address & ~1)))
141 break;
143 s++;
145 if (!(r = I2C_PutByte(dev->driver, o, vec[0])))
146 break;
148 if (!(r = I2C_PutByte(dev->driver, o, vec[1])))
149 break;
152 if (s > 0) I2C_Stop(dev->driver, o);
155 return r;
158 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteRead)
160 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
162 return I2C_WriteRead(dev->driver, o, msg->writeBuffer, msg->writeLength, msg->readBuffer, msg->readLength);
165 /*** Root */
167 OOP_Object *METHOD(I2CDev, Root, New)
169 o = (OOP_Object *)OOP_DoSuperMethod(cl, o, (OOP_Msg) msg);
170 if (o)
172 struct TagItem *tag;
173 const struct TagItem *tags = msg->attrList;
174 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
175 OOP_Object *driver = NULL;
176 UWORD address = 0;
178 dev->HoldTime = -1;
179 dev->AcknTimeout = -1;
180 dev->BitTimeout = -1;
181 dev->ByteTimeout = -1;
182 dev->RiseFallTime = -1;
183 dev->StartTimeout = -1;
185 dev->name = (STRPTR)"dev?";
187 tags=msg->attrList;
189 while((tag = NextTagItem(&tags)))
191 ULONG idx;
193 if (IS_I2CDEV_ATTR(tag->ti_Tag, idx))
195 switch(idx)
197 case aoHidd_I2CDevice_Driver:
198 dev->driver = (OOP_Object*)tag->ti_Data;
199 driver = dev->driver;
200 break;
202 case aoHidd_I2CDevice_Address:
203 dev->address = tag->ti_Data;
204 address = dev->address;
205 break;
207 case aoHidd_I2CDevice_Name:
208 dev->name = (STRPTR)tag->ti_Data;
209 break;
211 case aoHidd_I2CDevice_HoldTime:
212 dev->HoldTime = tag->ti_Data;
213 break;
215 case aoHidd_I2CDevice_BitTimeout:
216 dev->BitTimeout = tag->ti_Data;
217 break;
219 case aoHidd_I2CDevice_ByteTimeout:
220 dev->ByteTimeout = tag->ti_Data;
221 break;
223 case aoHidd_I2CDevice_AcknTimeout:
224 dev->AcknTimeout = tag->ti_Data;
225 break;
227 case aoHidd_I2CDevice_StartTimeout:
228 dev->StartTimeout = tag->ti_Data;
229 break;
231 case aoHidd_I2CDevice_RiseFallTime:
232 dev->RiseFallTime = tag->ti_Data;
233 break;
238 if (driver && address)
240 IPTR val;
242 if (dev->AcknTimeout == -1)
244 OOP_GetAttr(driver, aHidd_I2C_AcknTimeout, &val);
245 dev->AcknTimeout = val;
248 if (dev->BitTimeout == -1)
250 OOP_GetAttr(driver, aHidd_I2C_BitTimeout, &val);
251 dev->BitTimeout = val;
254 if (dev->ByteTimeout == -1)
256 OOP_GetAttr(driver, aHidd_I2C_ByteTimeout, &val);
257 dev->ByteTimeout = val;
260 if (dev->HoldTime == -1)
262 OOP_GetAttr(driver, aHidd_I2C_HoldTime, &val);
263 dev->HoldTime = val;
266 if (dev->RiseFallTime == -1)
268 OOP_GetAttr(driver, aHidd_I2C_RiseFallTime, &val);
269 dev->RiseFallTime = val;
272 if (dev->StartTimeout == -1)
274 OOP_GetAttr(driver, aHidd_I2C_StartTimeout, &val);
275 dev->StartTimeout = val;
278 else
280 OOP_MethodID disp_mid = OOP_GetMethodID((STRPTR)IID_Root, moRoot_Dispose);
281 OOP_CoerceMethod(cl, o, (OOP_Msg) &disp_mid);
282 o = NULL;
286 return o;
289 void METHOD(I2CDev, Root, Get)
291 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
292 ULONG idx;
294 if (IS_I2CDEV_ATTR(msg->attrID, idx))
296 switch (idx)
298 case aoHidd_I2CDevice_Driver:
299 *msg->storage = (IPTR)dev->driver;
300 break;
302 case aoHidd_I2CDevice_Address:
303 *msg->storage = dev->address;
304 break;
306 case aoHidd_I2CDevice_Name:
307 *msg->storage = (IPTR)dev->name;
308 break;
310 case aoHidd_I2CDevice_HoldTime:
311 *msg->storage = dev->HoldTime;
312 break;
314 case aoHidd_I2CDevice_BitTimeout:
315 *msg->storage = dev->BitTimeout;
316 break;
318 case aoHidd_I2CDevice_ByteTimeout:
319 *msg->storage = dev->ByteTimeout;
320 break;
322 case aoHidd_I2CDevice_AcknTimeout:
323 *msg->storage = dev->AcknTimeout;
324 break;
326 case aoHidd_I2CDevice_StartTimeout:
327 *msg->storage = dev->StartTimeout;
328 break;
330 case aoHidd_I2CDevice_RiseFallTime:
331 *msg->storage = dev->RiseFallTime;
332 break;
335 else
337 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
341 void METHOD(I2CDev, Root, Set)
343 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
344 ULONG idx;
345 struct TagItem *tag;
346 const struct TagItem *tags = msg->attrList;
348 while ((tag = NextTagItem(&tags)))
350 if (IS_I2CDEV_ATTR(tag->ti_Tag, idx))
352 switch (idx)
354 case aoHidd_I2CDevice_HoldTime:
355 dev->HoldTime = tag->ti_Data;
356 break;
358 case aoHidd_I2CDevice_BitTimeout:
359 dev->BitTimeout = tag->ti_Data;
360 break;
362 case aoHidd_I2CDevice_ByteTimeout:
363 dev->ByteTimeout = tag->ti_Data;
364 break;
366 case aoHidd_I2CDevice_AcknTimeout:
367 dev->AcknTimeout = tag->ti_Data;
368 break;
370 case aoHidd_I2CDevice_StartTimeout:
371 dev->StartTimeout = tag->ti_Data;
372 break;
374 case aoHidd_I2CDevice_RiseFallTime:
375 dev->RiseFallTime = tag->ti_Data;
376 break;