This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / w1 / w1.c
1 /*
2  *      w1.c
3  *
4  * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5  * 
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <asm/atomic.h>
23 #include <asm/delay.h>
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/list.h>
29 #include <linux/interrupt.h>
30 #include <linux/spinlock.h>
31 #include <linux/timer.h>
32 #include <linux/device.h>
33 #include <linux/slab.h>
34 #include <linux/sched.h>
35 #include <linux/suspend.h>
36
37 #include "w1.h"
38 #include "w1_io.h"
39 #include "w1_log.h"
40 #include "w1_int.h"
41 #include "w1_family.h"
42 #include "w1_netlink.h"
43
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
46 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
47
48 static int w1_timeout = 5 * HZ;
49 int w1_max_slave_count = 10;
50
51 module_param_named(timeout, w1_timeout, int, 0);
52 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
53
54 spinlock_t w1_mlock = SPIN_LOCK_UNLOCKED;
55 LIST_HEAD(w1_masters);
56
57 static pid_t control_thread;
58 static int control_needs_exit;
59 static DECLARE_COMPLETION(w1_control_complete);
60 static DECLARE_WAIT_QUEUE_HEAD(w1_control_wait);
61
62 static int w1_master_match(struct device *dev, struct device_driver *drv)
63 {
64         return 1;
65 }
66
67 static int w1_master_probe(struct device *dev)
68 {
69         return -ENODEV;
70 }
71
72 static int w1_master_remove(struct device *dev)
73 {
74         return 0;
75 }
76
77 static void w1_master_release(struct device *dev)
78 {
79         struct w1_master *md = container_of(dev, struct w1_master, dev);
80
81         complete(&md->dev_released);
82 }
83
84 static void w1_slave_release(struct device *dev)
85 {
86         struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
87
88         complete(&sl->dev_released);
89 }
90
91 static ssize_t w1_default_read_name(struct device *dev, char *buf)
92 {
93         return sprintf(buf, "No family registered.\n");
94 }
95
96 static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
97                      size_t count)
98 {
99         return sprintf(buf, "No family registered.\n");
100 }
101
102 struct bus_type w1_bus_type = {
103         .name = "w1",
104         .match = w1_master_match,
105 };
106
107 struct device_driver w1_driver = {
108         .name = "w1_driver",
109         .bus = &w1_bus_type,
110         .probe = w1_master_probe,
111         .remove = w1_master_remove,
112 };
113
114 struct device w1_device = {
115         .parent = NULL,
116         .bus = &w1_bus_type,
117         .bus_id = "w1 bus master",
118         .driver = &w1_driver,
119         .release = &w1_master_release
120 };
121
122 static struct device_attribute w1_slave_attribute = {
123         .attr = {
124                         .name = "name",
125                         .mode = S_IRUGO,
126                         .owner = THIS_MODULE
127         },
128         .show = &w1_default_read_name,
129 };
130
131 static struct device_attribute w1_slave_attribute_val = {
132         .attr = {
133                         .name = "value",
134                         .mode = S_IRUGO,
135                         .owner = THIS_MODULE
136         },
137         .show = &w1_default_read_name,
138 };
139
140 static ssize_t w1_master_attribute_show(struct device *dev, char *buf)
141 {
142         return sprintf(buf, "please fix me\n");
143 #if 0
144         struct w1_master *md = container_of(dev, struct w1_master, dev);
145         int c = PAGE_SIZE;
146
147         if (down_interruptible(&md->mutex))
148                 return -EBUSY;
149
150         c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", md->name);
151         c -= snprintf(buf + PAGE_SIZE - c, c,
152                        "bus_master=0x%p, timeout=%d, max_slave_count=%d, attempts=%lu\n",
153                        md->bus_master, w1_timeout, md->max_slave_count,
154                        md->attempts);
155         c -= snprintf(buf + PAGE_SIZE - c, c, "%d slaves: ",
156                        md->slave_count);
157         if (md->slave_count == 0)
158                 c -= snprintf(buf + PAGE_SIZE - c, c, "no.\n");
159         else {
160                 struct list_head *ent, *n;
161                 struct w1_slave *sl;
162
163                 list_for_each_safe(ent, n, &md->slist) {
164                         sl = list_entry(ent, struct w1_slave, w1_slave_entry);
165
166                         c -= snprintf(buf + PAGE_SIZE - c, c, "%s[%p] ",
167                                        sl->name, sl);
168                 }
169                 c -= snprintf(buf + PAGE_SIZE - c, c, "\n");
170         }
171
172         up(&md->mutex);
173
174         return PAGE_SIZE - c;
175 #endif
176 }
177
178 struct device_attribute w1_master_attribute = {
179         .attr = {
180                         .name = "w1_master_stats",
181                         .mode = S_IRUGO,
182                         .owner = THIS_MODULE,
183         },
184         .show = &w1_master_attribute_show,
185 };
186
187 static struct bin_attribute w1_slave_bin_attribute = {
188         .attr = {
189                         .name = "w1_slave",
190                         .mode = S_IRUGO,
191                         .owner = THIS_MODULE,
192         },
193         .size = W1_SLAVE_DATA_SIZE,
194         .read = &w1_default_read_bin,
195 };
196
197 static int __w1_attach_slave_device(struct w1_slave *sl)
198 {
199         int err;
200
201         sl->dev.parent = &sl->master->dev;
202         sl->dev.driver = sl->master->driver;
203         sl->dev.bus = &w1_bus_type;
204         sl->dev.release = &w1_slave_release;
205
206         snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
207                   "%x-%llx",
208                   (unsigned int) sl->reg_num.family,
209                   (unsigned long long) sl->reg_num.id);
210         snprintf (&sl->name[0], sizeof(sl->name),
211                   "%x-%llx",
212                   (unsigned int) sl->reg_num.family,
213                   (unsigned long long) sl->reg_num.id);
214
215         dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
216                 &sl->dev.bus_id[0]);
217
218         err = device_register(&sl->dev);
219         if (err < 0) {
220                 dev_err(&sl->dev,
221                          "Device registration [%s] failed. err=%d\n",
222                          sl->dev.bus_id, err);
223                 return err;
224         }
225
226         w1_slave_bin_attribute.read = sl->family->fops->rbin;
227         w1_slave_attribute.show = sl->family->fops->rname;
228         w1_slave_attribute_val.show = sl->family->fops->rval;
229         w1_slave_attribute_val.attr.name = sl->family->fops->rvalname;
230
231         err = device_create_file(&sl->dev, &w1_slave_attribute);
232         if (err < 0) {
233                 dev_err(&sl->dev,
234                          "sysfs file creation for [%s] failed. err=%d\n",
235                          sl->dev.bus_id, err);
236                 device_unregister(&sl->dev);
237                 return err;
238         }
239
240         err = device_create_file(&sl->dev, &w1_slave_attribute_val);
241         if (err < 0) {
242                 dev_err(&sl->dev,
243                          "sysfs file creation for [%s] failed. err=%d\n",
244                          sl->dev.bus_id, err);
245                 device_remove_file(&sl->dev, &w1_slave_attribute);
246                 device_unregister(&sl->dev);
247                 return err;
248         }
249
250         err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_bin_attribute);
251         if (err < 0) {
252                 dev_err(&sl->dev,
253                          "sysfs file creation for [%s] failed. err=%d\n",
254                          sl->dev.bus_id, err);
255                 device_remove_file(&sl->dev, &w1_slave_attribute);
256                 device_remove_file(&sl->dev, &w1_slave_attribute_val);
257                 device_unregister(&sl->dev);
258                 return err;
259         }
260
261         list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
262
263         return 0;
264 }
265
266 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
267 {
268         struct w1_slave *sl;
269         struct w1_family *f;
270         int err;
271
272         sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
273         if (!sl) {
274                 dev_err(&dev->dev,
275                          "%s: failed to allocate new slave device.\n",
276                          __func__);
277                 return -ENOMEM;
278         }
279
280         memset(sl, 0, sizeof(*sl));
281
282         sl->owner = THIS_MODULE;
283         sl->master = dev;
284
285         memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
286         atomic_set(&sl->refcnt, 0);
287         init_completion(&sl->dev_released);
288
289         spin_lock(&w1_flock);
290         f = w1_family_registered(rn->family);
291         if (!f) {
292                 spin_unlock(&w1_flock);
293                 dev_info(&dev->dev, "Family %x is not registered.\n",
294                           rn->family);
295                 kfree(sl);
296                 return -ENODEV;
297         }
298         __w1_family_get(f);
299         spin_unlock(&w1_flock);
300
301         sl->family = f;
302
303
304         err = __w1_attach_slave_device(sl);
305         if (err < 0) {
306                 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
307                          sl->name);
308                 w1_family_put(sl->family);
309                 kfree(sl);
310                 return err;
311         }
312
313         dev->slave_count++;
314
315         return 0;
316 }
317
318 static void w1_slave_detach(struct w1_slave *sl)
319 {
320         dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
321
322         while (atomic_read(&sl->refcnt))
323                 schedule_timeout(10);
324
325         sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_bin_attribute);
326         device_remove_file(&sl->dev, &w1_slave_attribute);
327         device_unregister(&sl->dev);
328         w1_family_put(sl->family);
329 }
330
331 static void w1_search(struct w1_master *dev)
332 {
333         u64 last, rn, tmp;
334         int i, count = 0, slave_count;
335         int last_family_desc, last_zero, last_device;
336         int search_bit, id_bit, comp_bit, desc_bit;
337         struct list_head *ent;
338         struct w1_slave *sl;
339         int family_found = 0;
340         struct w1_netlink_msg msg;
341
342         dev->attempts++;
343
344         memset(&msg, 0, sizeof(msg));
345
346         search_bit = id_bit = comp_bit = 0;
347         rn = tmp = last = 0;
348         last_device = last_zero = last_family_desc = 0;
349
350         desc_bit = 64;
351
352         while (!(id_bit && comp_bit) && !last_device
353                 && count++ < dev->max_slave_count) {
354                 last = rn;
355                 rn = 0;
356
357                 last_family_desc = 0;
358
359                 /*
360                  * Reset bus and all 1-wire device state machines
361                  * so they can respond to our requests.
362                  *
363                  * Return 0 - device(s) present, 1 - no devices present.
364                  */
365                 if (w1_reset_bus(dev)) {
366                         dev_info(&dev->dev, "No devices present on the wire.\n");
367                         break;
368                 }
369
370 #if 1
371                 memset(&msg, 0, sizeof(msg));
372
373                 w1_write_8(dev, W1_SEARCH);
374                 for (i = 0; i < 64; ++i) {
375                         /*
376                          * Read 2 bits from bus.
377                          * All who don't sleep must send ID bit and COMPLEMENT ID bit.
378                          * They actually are ANDed between all senders.
379                          */
380                         id_bit = w1_read_bit(dev);
381                         comp_bit = w1_read_bit(dev);
382
383                         if (id_bit && comp_bit)
384                                 break;
385
386                         if (id_bit == 0 && comp_bit == 0) {
387                                 if (i == desc_bit)
388                                         search_bit = 1;
389                                 else if (i > desc_bit)
390                                         search_bit = 0;
391                                 else
392                                         search_bit = ((last >> i) & 0x1);
393
394                                 if (search_bit == 0) {
395                                         last_zero = i;
396                                         if (last_zero < 9)
397                                                 last_family_desc = last_zero;
398                                 }
399
400                         }
401                         else
402                                 search_bit = id_bit;
403
404                         tmp = search_bit;
405                         rn |= (tmp << i);
406
407                         /*
408                          * Write 1 bit to bus
409                          * and make all who don't have "search_bit" in "i"'th position
410                          * in it's registration number sleep.
411                          */
412                         w1_write_bit(dev, search_bit);
413
414                 }
415 #endif
416                 msg.id.w1_id = rn;
417                 msg.val = w1_calc_crc8((u8 *) & rn, 7);
418                 w1_netlink_send(dev, &msg);
419
420                 if (desc_bit == last_zero)
421                         last_device = 1;
422
423                 desc_bit = last_zero;
424
425                 slave_count = 0;
426                 list_for_each(ent, &dev->slist) {
427                         struct w1_reg_num *tmp;
428
429                         tmp = (struct w1_reg_num *) &rn;
430
431                         sl = list_entry(ent, struct w1_slave, w1_slave_entry);
432
433                         if (sl->reg_num.family == tmp->family &&
434                             sl->reg_num.id == tmp->id &&
435                             sl->reg_num.crc == tmp->crc)
436                                 break;
437                         else if (sl->reg_num.family == tmp->family) {
438                                 family_found = 1;
439                                 break;
440                         }
441
442                         slave_count++;
443                 }
444
445                 if (slave_count == dev->slave_count &&
446                     msg.val && (*((__u8 *) & msg.val) == msg.id.id.crc)) {
447                         w1_attach_slave_device(dev, (struct w1_reg_num *) &rn);
448                 }
449         }
450 }
451
452 int w1_control(void *data)
453 {
454         struct w1_slave *sl;
455         struct w1_master *dev;
456         struct list_head *ent, *ment, *n, *mn;
457         int err, have_to_wait = 0, timeout;
458
459         daemonize("w1_control");
460         allow_signal(SIGTERM);
461
462         while (!control_needs_exit || have_to_wait) {
463                 have_to_wait = 0;
464
465                 timeout = w1_timeout;
466                 do {
467                         timeout = interruptible_sleep_on_timeout(&w1_control_wait, timeout);
468                         if (current->flags & PF_FREEZE)
469                                 refrigerator(PF_FREEZE);
470                 } while (!signal_pending(current) && (timeout > 0));
471
472                 if (signal_pending(current))
473                         flush_signals(current);
474
475                 list_for_each_safe(ment, mn, &w1_masters) {
476                         dev = list_entry(ment, struct w1_master, w1_master_entry);
477
478                         if (!control_needs_exit && !dev->need_exit)
479                                 continue;
480                         /*
481                          * Little race: we can create thread but not set the flag.
482                          * Get a chance for external process to set flag up.
483                          */
484                         if (!dev->initialized) {
485                                 have_to_wait = 1;
486                                 continue;
487                         }
488
489                         spin_lock(&w1_mlock);
490                         list_del(&dev->w1_master_entry);
491                         spin_unlock(&w1_mlock);
492
493                         if (control_needs_exit) {
494                                 dev->need_exit = 1;
495
496                                 err = kill_proc(dev->kpid, SIGTERM, 1);
497                                 if (err)
498                                         dev_err(&dev->dev,
499                                                  "Failed to send signal to w1 kernel thread %d.\n",
500                                                  dev->kpid);
501                         }
502
503                         wait_for_completion(&dev->dev_exited);
504
505                         list_for_each_safe(ent, n, &dev->slist) {
506                                 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
507
508                                 if (!sl)
509                                         dev_warn(&dev->dev,
510                                                   "%s: slave entry is NULL.\n",
511                                                   __func__);
512                                 else {
513                                         list_del(&sl->w1_slave_entry);
514
515                                         w1_slave_detach(sl);
516                                         kfree(sl);
517                                 }
518                         }
519                         device_remove_file(&dev->dev, &w1_master_attribute);
520                         atomic_dec(&dev->refcnt);
521                 }
522         }
523
524         complete_and_exit(&w1_control_complete, 0);
525 }
526
527 int w1_process(void *data)
528 {
529         struct w1_master *dev = (struct w1_master *) data;
530         unsigned long timeout;
531
532         daemonize("%s", dev->name);
533         allow_signal(SIGTERM);
534
535         while (!dev->need_exit) {
536                 timeout = w1_timeout;
537                 do {
538                         timeout = interruptible_sleep_on_timeout(&dev->kwait, timeout);
539                         if (current->flags & PF_FREEZE)
540                                 refrigerator(PF_FREEZE);
541                 } while (!signal_pending(current) && (timeout > 0));
542
543                 if (signal_pending(current))
544                         flush_signals(current);
545
546                 if (dev->need_exit)
547                         break;
548
549                 if (!dev->initialized)
550                         continue;
551
552                 if (down_interruptible(&dev->mutex))
553                         continue;
554                 w1_search(dev);
555                 up(&dev->mutex);
556         }
557
558         atomic_dec(&dev->refcnt);
559         complete_and_exit(&dev->dev_exited, 0);
560
561         return 0;
562 }
563
564 int w1_init(void)
565 {
566         int retval;
567
568         printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
569
570         retval = bus_register(&w1_bus_type);
571         if (retval) {
572                 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
573                 goto err_out_exit_init;
574         }
575
576         retval = driver_register(&w1_driver);
577         if (retval) {
578                 printk(KERN_ERR
579                         "Failed to register master driver. err=%d.\n",
580                         retval);
581                 goto err_out_bus_unregister;
582         }
583
584         control_thread = kernel_thread(&w1_control, NULL, 0);
585         if (control_thread < 0) {
586                 printk(KERN_ERR "Failed to create control thread. err=%d\n",
587                         control_thread);
588                 retval = control_thread;
589                 goto err_out_driver_unregister;
590         }
591
592         return 0;
593
594 err_out_driver_unregister:
595         driver_unregister(&w1_driver);
596
597 err_out_bus_unregister:
598         bus_unregister(&w1_bus_type);
599
600 err_out_exit_init:
601         return retval;
602 }
603
604 void w1_fini(void)
605 {
606         struct w1_master *dev;
607         struct list_head *ent, *n;
608
609         list_for_each_safe(ent, n, &w1_masters) {
610                 dev = list_entry(ent, struct w1_master, w1_master_entry);
611                 __w1_remove_master_device(dev);
612         }
613
614         control_needs_exit = 1;
615
616         wait_for_completion(&w1_control_complete);
617
618         driver_unregister(&w1_driver);
619         bus_unregister(&w1_bus_type);
620 }
621
622 module_init(w1_init);
623 module_exit(w1_fini);