provide SIZE_MAX
[AROS.git] / workbench / hidds / i2c / i2cdeviceclass.c
blobbc564d38d2e5b141f04625cc06002f72a9bd8c2f
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
26 #undef HiddAttrBase
28 #define HiddI2CAttrBase (SD(cl)->hiddI2CAB)
29 #define HiddI2CDeviceAttrBase (SD(cl)->hiddI2CDeviceAB)
30 #define HiddAttrBase (SD(cl)->hiddAB)
32 BOOL METHOD(I2CDev, Hidd_I2CDevice, Read)
34 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
36 return I2C_WriteRead(dev->driver, o, NULL, 0, msg->readBuffer, msg->readLength);
39 BOOL METHOD(I2CDev, Hidd_I2CDevice, ReadByte)
41 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
43 return I2C_WriteRead(dev->driver, o, &msg->subaddr, 1, msg->data, 1);
46 BOOL METHOD(I2CDev, Hidd_I2CDevice, ReadBytes)
48 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
50 return I2C_WriteRead(dev->driver, o, &msg->subaddr, 1, msg->data, msg->length);
53 BOOL METHOD(I2CDev, Hidd_I2CDevice, ReadStatus)
55 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
57 return I2C_WriteRead(dev->driver, o, NULL, 0, msg->status, 1);
60 BOOL METHOD(I2CDev, Hidd_I2CDevice, ReadWord)
62 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
63 BOOL r;
64 UBYTE buff[2];
66 if ((r = I2C_WriteRead(dev->driver, o, &msg->subaddr, 1, buff, 2)))
68 *msg->data = (buff[0] << 8) | buff[1];
71 return r;
74 BOOL METHOD(I2CDev, Hidd_I2CDevice, Write)
76 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
78 return I2C_WriteRead(dev->driver, o, msg->writeBuffer, msg->writeLength, NULL, 0);
81 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteByte)
83 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
84 UBYTE buff[2];
86 buff[0] = msg->subaddr;
87 buff[1] = msg->data;
89 return I2C_WriteRead(dev->driver, o, buff, 2, NULL, 0);
92 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteBytes)
94 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
95 BOOL r = TRUE;
96 ULONG nWrite = msg->length;
97 UBYTE *WriteBuffer = msg->data;
99 if (nWrite > 0)
101 if ((r = I2C_Address(dev->driver, o, dev->address & ~1)))
103 if ((r = I2C_PutByte(dev->driver, o, msg->subaddr)))
105 for (; nWrite > 0; WriteBuffer++, nWrite--)
106 if (!(r = I2C_PutByte(dev->driver, o, *WriteBuffer)))
107 break;
110 I2C_Stop(dev->driver, o);
114 return r;
117 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteWord)
119 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
120 UBYTE buff[3];
122 buff[0] = msg->subaddr;
123 buff[1] = msg->data >> 8;
124 buff[2] = msg->data & 0xff;
126 return I2C_WriteRead(dev->driver, o, buff, 2, NULL, 0);
129 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteVec)
131 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
132 BOOL r = TRUE;
133 int s = 0;
134 ULONG nValues = msg->length;
135 UBYTE *vec = msg->data;
137 if (nValues > 0)
139 for (; nValues > 0; nValues--, vec+=2)
141 if (!(r = I2C_Address(dev->driver, o, dev->address & ~1)))
142 break;
144 s++;
146 if (!(r = I2C_PutByte(dev->driver, o, vec[0])))
147 break;
149 if (!(r = I2C_PutByte(dev->driver, o, vec[1])))
150 break;
153 if (s > 0) I2C_Stop(dev->driver, o);
156 return r;
159 BOOL METHOD(I2CDev, Hidd_I2CDevice, WriteRead)
161 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
163 return I2C_WriteRead(dev->driver, o, msg->writeBuffer, msg->writeLength, msg->readBuffer, msg->readLength);
166 /*** Root */
168 OOP_Object *METHOD(I2CDev, Root, New)
170 o = (OOP_Object *)OOP_DoSuperMethod(cl, o, (OOP_Msg) msg);
171 if (o)
173 struct TagItem *tag, *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;
241 if (dev->AcknTimeout == -1)
243 OOP_GetAttr(driver, aHidd_I2C_AcknTimeout, &val);
244 dev->AcknTimeout = val;
246 if (dev->BitTimeout == -1)
248 OOP_GetAttr(driver, aHidd_I2C_BitTimeout, &val);
249 dev->BitTimeout = val;
251 if (dev->ByteTimeout == -1)
253 OOP_GetAttr(driver, aHidd_I2C_ByteTimeout, &val);
254 dev->ByteTimeout = val;
256 if (dev->HoldTime == -1)
258 OOP_GetAttr(driver, aHidd_I2C_HoldTime, &val);
259 dev->HoldTime = val;
261 if (dev->RiseFallTime == -1)
263 OOP_GetAttr(driver, aHidd_I2C_RiseFallTime, &val);
264 dev->RiseFallTime = val;
266 if (dev->StartTimeout == -1)
268 OOP_GetAttr(driver, aHidd_I2C_StartTimeout, &val);
269 dev->StartTimeout = val;
272 else
274 OOP_MethodID disp_mid = OOP_GetMethodID((STRPTR)IID_Root, moRoot_Dispose);
275 OOP_CoerceMethod(cl, o, (OOP_Msg) &disp_mid);
276 o = NULL;
280 return o;
283 void METHOD(I2CDev, Root, Get)
285 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
286 ULONG idx;
288 if (IS_I2CDEV_ATTR(msg->attrID, idx))
290 switch (idx)
292 case aoHidd_I2CDevice_Driver:
293 *msg->storage = (IPTR)dev->driver;
294 break;
296 case aoHidd_I2CDevice_Address:
297 *msg->storage = dev->address;
298 break;
300 case aoHidd_I2CDevice_Name:
301 *msg->storage = (IPTR)dev->name;
302 break;
304 case aoHidd_I2CDevice_HoldTime:
305 *msg->storage = dev->HoldTime;
306 break;
308 case aoHidd_I2CDevice_BitTimeout:
309 *msg->storage = dev->BitTimeout;
310 break;
312 case aoHidd_I2CDevice_ByteTimeout:
313 *msg->storage = dev->ByteTimeout;
314 break;
316 case aoHidd_I2CDevice_AcknTimeout:
317 *msg->storage = dev->AcknTimeout;
318 break;
320 case aoHidd_I2CDevice_StartTimeout:
321 *msg->storage = dev->StartTimeout;
322 break;
324 case aoHidd_I2CDevice_RiseFallTime:
325 *msg->storage = dev->RiseFallTime;
326 break;
329 else
331 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
335 void METHOD(I2CDev, Root, Set)
337 tDevData *dev = (tDevData *)OOP_INST_DATA(cl, o);
338 ULONG idx;
339 struct TagItem *tag, *tags = msg->attrList;
341 while ((tag = NextTagItem(&tags)))
343 if (IS_I2CDEV_ATTR(tag->ti_Tag, idx))
345 switch (idx)
347 case aoHidd_I2CDevice_HoldTime:
348 dev->HoldTime = tag->ti_Data;
349 break;
351 case aoHidd_I2CDevice_BitTimeout:
352 dev->BitTimeout = tag->ti_Data;
353 break;
355 case aoHidd_I2CDevice_ByteTimeout:
356 dev->ByteTimeout = tag->ti_Data;
357 break;
359 case aoHidd_I2CDevice_AcknTimeout:
360 dev->AcknTimeout = tag->ti_Data;
361 break;
363 case aoHidd_I2CDevice_StartTimeout:
364 dev->StartTimeout = tag->ti_Data;
365 break;
367 case aoHidd_I2CDevice_RiseFallTime:
368 dev->RiseFallTime = tag->ti_Data;
369 break;