root/lm-sensors/trunk/kernel/chips/ddcmon.c @ 1101

Revision 1101, 11.7 KB (checked in by mds, 12 years ago)

Change from sensors_x to i2c_x for x =

_sysctl_real
_register_entry
_proc_real
_detect
_deregister_entry
_chips_data

which were the global symbols renamed in i2c-proc.[ch] in the
i2c package.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    ddcmon.c - Part of lm_sensors, Linux kernel modules for hardware
3               monitoring
4    Copyright (c) 1998, 1999, 2000  Frodo Looijaard <frodol@dds.nl>,
5    Philip Edelbrock <phil@netroedge.com>,
6    and Mark Studebaker <mdsxyz123@yahoo.com>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include <linux/module.h>
24#include <linux/version.h>
25#include <linux/malloc.h>
26#include <linux/i2c.h>
27#include "sensors.h"
28#include "version.h"
29
30#include <linux/init.h>
31
32#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,2,18)) || \
33    (LINUX_VERSION_CODE == KERNEL_VERSION(2,3,0))
34#define init_MUTEX(s) do { *(s) = MUTEX; } while(0)
35#endif
36
37#if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,13)
38#define THIS_MODULE NULL
39#endif
40
41/* Addresses to scan */
42static unsigned short normal_i2c[] = { 0x50, SENSORS_I2C_END };
43static unsigned short normal_i2c_range[] = { SENSORS_I2C_END };
44static unsigned int normal_isa[] = { SENSORS_ISA_END };
45static unsigned int normal_isa_range[] = { SENSORS_ISA_END };
46
47/* Insmod parameters */
48SENSORS_INSMOD_1(ddcmon);
49
50/* Many constants specified below */
51
52/* DDCMON registers */
53#define DDCMON_REG_ID 0x08
54#define DDCMON_REG_SERIAL 0x0C
55#define DDCMON_REG_HORSIZE 0x15
56#define DDCMON_REG_VERSIZE 0x16
57#define DDCMON_REG_TIMINGS 0x23
58#define DDCMON_REG_TIMBASE 0x36
59#define DDCMON_REG_TIMINCR 18
60#define DDCMON_REG_TIMNUM   4
61#define DDCMON_REG_TIMOFFSET 5
62#define DDCMON_REG_CHECKSUM 0x7f
63
64/* Size of DDCMON in bytes */
65#define DDCMON_SIZE 128
66
67/* Each client has this additional data */
68struct ddcmon_data {
69        int sysctl_id;
70
71        struct semaphore update_lock;
72        char valid;             /* !=0 if following fields are valid */
73        unsigned long last_updated;     /* In jiffies */
74
75        u8 data[DDCMON_SIZE];   /* Register values */
76        int memtype;
77};
78
79#ifdef MODULE
80extern int init_module(void);
81extern int cleanup_module(void);
82#endif                          /* MODULE */
83
84#ifdef MODULE
85static
86#else
87extern
88#endif
89int __init sensors_ddcmon_init(void);
90static int __init ddcmon_cleanup(void);
91
92static int ddcmon_attach_adapter(struct i2c_adapter *adapter);
93static int ddcmon_detect(struct i2c_adapter *adapter, int address,
94                         unsigned short flags, int kind);
95static int ddcmon_detach_client(struct i2c_client *client);
96static int ddcmon_command(struct i2c_client *client, unsigned int cmd,
97                          void *arg);
98
99static void ddcmon_inc_use(struct i2c_client *client);
100static void ddcmon_dec_use(struct i2c_client *client);
101
102static void ddcmon_idcall(struct i2c_client *client, int operation,
103                            int ctl_name, int *nrels_mag, long *results);
104static void ddcmon_size(struct i2c_client *client, int operation,
105                            int ctl_name, int *nrels_mag, long *results);
106static void ddcmon_sync(struct i2c_client *client, int operation,
107                            int ctl_name, int *nrels_mag, long *results);
108static void ddcmon_timings(struct i2c_client *client, int operation,
109                            int ctl_name, int *nrels_mag, long *results);
110static void ddcmon_serial(struct i2c_client *client, int operation,
111                            int ctl_name, int *nrels_mag, long *results);
112static void ddcmon_update_client(struct i2c_client *client);
113
114
115/* This is the driver that will be inserted */
116static struct i2c_driver ddcmon_driver = {
117        /* name */ "DDCMON READER",
118        /* id */ I2C_DRIVERID_DDCMON,
119        /* flags */ I2C_DF_NOTIFY,
120        /* attach_adapter */ &ddcmon_attach_adapter,
121        /* detach_client */ &ddcmon_detach_client,
122        /* command */ &ddcmon_command,
123        /* inc_use */ &ddcmon_inc_use,
124        /* dec_use */ &ddcmon_dec_use
125};
126
127/* These files are created for each detected DDCMON. This is just a template;
128   though at first sight, you might think we could use a statically
129   allocated list, we need some way to get back to the parent - which
130   is done through one of the 'extra' fields which are initialized
131   when a new copy is allocated. */
132static ctl_table ddcmon_dir_table_template[] = {
133        {DDCMON_SYSCTL_ID, "ID", NULL, 0, 0444, NULL, &i2c_proc_real,
134         &i2c_sysctl_real, NULL, &ddcmon_idcall},
135        {DDCMON_SYSCTL_SIZE, "size", NULL, 0, 0444, NULL, &i2c_proc_real,
136         &i2c_sysctl_real, NULL, &ddcmon_size},
137        {DDCMON_SYSCTL_SYNC, "sync", NULL, 0, 0444, NULL,
138         &i2c_proc_real, &i2c_sysctl_real, NULL, &ddcmon_sync},
139        {DDCMON_SYSCTL_TIMINGS, "timings", NULL, 0, 0444, NULL,
140         &i2c_proc_real, &i2c_sysctl_real, NULL, &ddcmon_timings},
141        {DDCMON_SYSCTL_SERIAL, "serial", NULL, 0, 0444, NULL,
142         &i2c_proc_real, &i2c_sysctl_real, NULL, &ddcmon_serial},
143        {0}
144};
145
146/* Used by init/cleanup */
147static int __initdata ddcmon_initialized = 0;
148
149static int ddcmon_id = 0;
150
151int ddcmon_attach_adapter(struct i2c_adapter *adapter)
152{
153        return i2c_detect(adapter, &addr_data, ddcmon_detect);
154}
155
156/* This function is called by i2c_detect */
157int ddcmon_detect(struct i2c_adapter *adapter, int address,
158                  unsigned short flags, int kind)
159{
160        int i, cs;
161        struct i2c_client *new_client;
162        struct ddcmon_data *data;
163        int err = 0;
164        const char *type_name, *client_name;
165
166        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
167                    goto ERROR0;
168
169        /* OK. For now, we presume we have a valid client. We now create the
170           client structure, even though we cannot fill it completely yet.
171           But it allows us to access ddcmon_{read,write}_value. */
172        if (!(new_client = kmalloc(sizeof(struct i2c_client) +
173                                   sizeof(struct ddcmon_data),
174                                   GFP_KERNEL))) {
175                err = -ENOMEM;
176                goto ERROR0;
177        }
178
179        data = (struct ddcmon_data *) (new_client + 1);
180        new_client->addr = address;
181        new_client->data = data;
182        new_client->adapter = adapter;
183        new_client->driver = &ddcmon_driver;
184        new_client->flags = 0;
185
186        /* Now, we do the remaining detection. */
187        /* Verify the first 8 locations 0x00FFFFFFFFFFFF00 */
188        /* Allow force and force_ddcmon arguments */
189        if(kind < 0)
190        {
191                for(i = 0; i < 8; i++) {
192                        cs = i2c_smbus_read_byte_data(new_client, i);
193                        if(i == 0 || i == 7) {
194                                if(cs != 0)
195                                        goto ERROR1;
196                        } else if(cs != 0xff)
197                                goto ERROR1;
198                }
199        }
200
201        type_name = "ddcmon";
202        client_name = "DDC Monitor";
203
204        /* Fill in the remaining client fields and put it in the global list */
205        strcpy(new_client->name, client_name);
206
207        new_client->id = ddcmon_id++;
208        data->valid = 0;
209        init_MUTEX(&data->update_lock);
210
211        /* Tell the I2C layer a new client has arrived */
212        if ((err = i2c_attach_client(new_client)))
213                goto ERROR3;
214
215        /* Register a new directory entry with module sensors */
216        if ((i = i2c_register_entry(new_client, type_name,
217                                        ddcmon_dir_table_template,
218                                        THIS_MODULE)) < 0) {
219                err = i;
220                goto ERROR4;
221        }
222        data->sysctl_id = i;
223
224        return 0;
225
226      ERROR4:
227        i2c_detach_client(new_client);
228      ERROR3:
229      ERROR1:
230        kfree(new_client);
231      ERROR0:
232        return err;
233}
234
235int ddcmon_detach_client(struct i2c_client *client)
236{
237        int err;
238
239        i2c_deregister_entry(((struct ddcmon_data *) (client->data))->
240                                 sysctl_id);
241        if ((err = i2c_detach_client(client))) {
242                printk
243                    ("ddcmon.o: Client deregistration failed, client not detached.\n");
244                return err;
245        }
246        kfree(client);
247        return 0;
248}
249
250/* No commands defined yet */
251int ddcmon_command(struct i2c_client *client, unsigned int cmd, void *arg)
252{
253        return 0;
254}
255
256void ddcmon_inc_use(struct i2c_client *client)
257{
258#ifdef MODULE
259        MOD_INC_USE_COUNT;
260#endif
261}
262
263void ddcmon_dec_use(struct i2c_client *client)
264{
265#ifdef MODULE
266        MOD_DEC_USE_COUNT;
267#endif
268}
269
270void ddcmon_update_client(struct i2c_client *client)
271{
272        struct ddcmon_data *data = client->data;
273        int i;
274
275        down(&data->update_lock);
276
277        if ((jiffies - data->last_updated > 300 * HZ) ||
278            (jiffies < data->last_updated) || !data->valid) {
279                if (i2c_smbus_write_byte(client, 0)) {
280#ifdef DEBUG
281                        printk("ddcmon read start has failed!\n");
282#endif
283                }
284                for (i = 0; i < DDCMON_SIZE; i++) {
285                        data->data[i] = (u8) i2c_smbus_read_byte(client);
286                }
287
288                data->last_updated = jiffies;
289                data->valid = 1;
290        }
291
292        up(&data->update_lock);
293}
294
295
296void ddcmon_idcall(struct i2c_client *client, int operation,
297                   int ctl_name, int *nrels_mag, long *results)
298{
299        struct ddcmon_data *data = client->data;
300
301        if (operation == SENSORS_PROC_REAL_INFO)
302                *nrels_mag = 0;
303        else if (operation == SENSORS_PROC_REAL_READ) {
304                ddcmon_update_client(client);
305                results[0] = data->data[DDCMON_REG_ID + 1] |
306                             (data->data[DDCMON_REG_ID] << 8) |
307                             (data->data[DDCMON_REG_ID + 3] << 16) |
308                             (data->data[DDCMON_REG_ID + 2] << 24);
309                *nrels_mag = 1;
310        }
311}
312
313void ddcmon_size(struct i2c_client *client, int operation,
314                 int ctl_name, int *nrels_mag, long *results)
315{
316        struct ddcmon_data *data = client->data;
317
318        if (operation == SENSORS_PROC_REAL_INFO)
319                *nrels_mag = 0;
320        else if (operation == SENSORS_PROC_REAL_READ) {
321                ddcmon_update_client(client);
322                results[0] = data->data[DDCMON_REG_VERSIZE];
323                results[1] = data->data[DDCMON_REG_HORSIZE];
324                *nrels_mag = 2;
325        }
326}
327
328void ddcmon_sync(struct i2c_client *client, int operation,
329                    int ctl_name, int *nrels_mag, long *results)
330{
331        int i, j;
332        struct ddcmon_data *data = client->data;
333
334        if (operation == SENSORS_PROC_REAL_INFO)
335                *nrels_mag = 0;
336        else if (operation == SENSORS_PROC_REAL_READ) {
337                ddcmon_update_client(client);
338                *nrels_mag = 4;
339                /* look for sync entry */
340                for(i = DDCMON_REG_TIMBASE;
341                    i < DDCMON_REG_TIMBASE +
342                        (DDCMON_REG_TIMNUM * DDCMON_REG_TIMINCR);
343                    i += DDCMON_REG_TIMINCR) {
344                        if(data->data[i] == 0) {
345                                for(j = 0; j < 4; j++)
346                                        results[j] = data->data[i + j +
347                                                        DDCMON_REG_TIMOFFSET];
348                                return;
349                        }
350                }
351                for(j = 0; j < 4; j++)
352                        results[j] = 0;
353        }
354}
355
356void ddcmon_timings(struct i2c_client *client, int operation,
357                    int ctl_name, int *nrels_mag, long *results)
358{
359        struct ddcmon_data *data = client->data;
360
361        if (operation == SENSORS_PROC_REAL_INFO)
362                *nrels_mag = 0;
363        else if (operation == SENSORS_PROC_REAL_READ) {
364                ddcmon_update_client(client);
365                results[0] = data->data[DDCMON_REG_TIMINGS] |
366                             (data->data[DDCMON_REG_TIMINGS + 1] << 8) |
367                             (data->data[DDCMON_REG_TIMINGS + 2] << 16);
368                *nrels_mag = 1;
369        }
370}
371
372void ddcmon_serial(struct i2c_client *client, int operation,
373                    int ctl_name, int *nrels_mag, long *results)
374{
375        struct ddcmon_data *data = client->data;
376
377        if (operation == SENSORS_PROC_REAL_INFO)
378                *nrels_mag = 0;
379        else if (operation == SENSORS_PROC_REAL_READ) {
380                ddcmon_update_client(client);
381                results[0] = data->data[DDCMON_REG_SERIAL] |
382                             (data->data[DDCMON_REG_SERIAL + 1] << 8) |
383                             (data->data[DDCMON_REG_SERIAL + 2] << 16) |
384                             (data->data[DDCMON_REG_SERIAL + 3] << 24);
385                *nrels_mag = 1;
386        }
387}
388
389int __init sensors_ddcmon_init(void)
390{
391        int res;
392
393        printk("ddcmon.o version %s (%s)\n", LM_VERSION, LM_DATE);
394        ddcmon_initialized = 0;
395        if ((res = i2c_add_driver(&ddcmon_driver))) {
396                printk
397                    ("ddcmon.o: Driver registration failed, module not inserted.\n");
398                ddcmon_cleanup();
399                return res;
400        }
401        ddcmon_initialized++;
402        return 0;
403}
404
405int __init ddcmon_cleanup(void)
406{
407        int res;
408
409        if (ddcmon_initialized >= 1) {
410                if ((res = i2c_del_driver(&ddcmon_driver))) {
411                        printk
412                            ("ddcmon.o: Driver deregistration failed, module not removed.\n");
413                        return res;
414                }
415        } else
416                ddcmon_initialized--;
417        return 0;
418}
419
420EXPORT_NO_SYMBOLS;
421
422#ifdef MODULE
423
424MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
425              "Philip Edelbrock <phil@netroedge .com>, "
426              "and Mark Studebaker <mdsxyz123@yahoo.com>");
427MODULE_DESCRIPTION("DDCMON driver");
428
429int init_module(void)
430{
431        return sensors_ddcmon_init();
432}
433
434int cleanup_module(void)
435{
436        return ddcmon_cleanup();
437}
438
439#endif                          /* MODULE */
Note: See TracBrowser for help on using the browser.