ds_w1_bridge.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * ds_w1_bridge.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. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include "../w1/w1.h"
  24. #include "../w1/w1_int.h"
  25. #include "dscore.h"
  26. static struct ds_device *ds_dev;
  27. static struct w1_bus_master *ds_bus_master;
  28. static u8 ds9490r_touch_bit(unsigned long data, u8 bit)
  29. {
  30. u8 ret;
  31. struct ds_device *dev = (struct ds_device *)data;
  32. if (ds_touch_bit(dev, bit, &ret))
  33. return 0;
  34. return ret;
  35. }
  36. static void ds9490r_write_bit(unsigned long data, u8 bit)
  37. {
  38. struct ds_device *dev = (struct ds_device *)data;
  39. ds_write_bit(dev, bit);
  40. }
  41. static void ds9490r_write_byte(unsigned long data, u8 byte)
  42. {
  43. struct ds_device *dev = (struct ds_device *)data;
  44. ds_write_byte(dev, byte);
  45. }
  46. static u8 ds9490r_read_bit(unsigned long data)
  47. {
  48. struct ds_device *dev = (struct ds_device *)data;
  49. int err;
  50. u8 bit = 0;
  51. err = ds_touch_bit(dev, 1, &bit);
  52. if (err)
  53. return 0;
  54. //err = ds_read_bit(dev, &bit);
  55. //if (err)
  56. // return 0;
  57. return bit & 1;
  58. }
  59. static u8 ds9490r_read_byte(unsigned long data)
  60. {
  61. struct ds_device *dev = (struct ds_device *)data;
  62. int err;
  63. u8 byte = 0;
  64. err = ds_read_byte(dev, &byte);
  65. if (err)
  66. return 0;
  67. return byte;
  68. }
  69. static void ds9490r_write_block(unsigned long data, const u8 *buf, int len)
  70. {
  71. struct ds_device *dev = (struct ds_device *)data;
  72. ds_write_block(dev, (u8 *)buf, len);
  73. }
  74. static u8 ds9490r_read_block(unsigned long data, u8 *buf, int len)
  75. {
  76. struct ds_device *dev = (struct ds_device *)data;
  77. int err;
  78. err = ds_read_block(dev, buf, len);
  79. if (err < 0)
  80. return 0;
  81. return len;
  82. }
  83. static u8 ds9490r_reset(unsigned long data)
  84. {
  85. struct ds_device *dev = (struct ds_device *)data;
  86. struct ds_status st;
  87. int err;
  88. memset(&st, 0, sizeof(st));
  89. err = ds_reset(dev, &st);
  90. if (err)
  91. return 1;
  92. return 0;
  93. }
  94. static int __devinit ds_w1_init(void)
  95. {
  96. int err;
  97. ds_bus_master = kmalloc(sizeof(*ds_bus_master), GFP_KERNEL);
  98. if (!ds_bus_master) {
  99. printk(KERN_ERR "Failed to allocate DS9490R USB<->W1 bus_master structure.\n");
  100. return -ENOMEM;
  101. }
  102. ds_dev = ds_get_device();
  103. if (!ds_dev) {
  104. printk(KERN_ERR "DS9490R is not registered.\n");
  105. err = -ENODEV;
  106. goto err_out_free_bus_master;
  107. }
  108. memset(ds_bus_master, 0, sizeof(*ds_bus_master));
  109. ds_bus_master->data = (unsigned long)ds_dev;
  110. ds_bus_master->touch_bit = &ds9490r_touch_bit;
  111. ds_bus_master->read_bit = &ds9490r_read_bit;
  112. ds_bus_master->write_bit = &ds9490r_write_bit;
  113. ds_bus_master->read_byte = &ds9490r_read_byte;
  114. ds_bus_master->write_byte = &ds9490r_write_byte;
  115. ds_bus_master->read_block = &ds9490r_read_block;
  116. ds_bus_master->write_block = &ds9490r_write_block;
  117. ds_bus_master->reset_bus = &ds9490r_reset;
  118. err = w1_add_master_device(ds_bus_master);
  119. if (err)
  120. goto err_out_put_device;
  121. return 0;
  122. err_out_put_device:
  123. ds_put_device(ds_dev);
  124. err_out_free_bus_master:
  125. kfree(ds_bus_master);
  126. return err;
  127. }
  128. static void __devexit ds_w1_fini(void)
  129. {
  130. w1_remove_master_device(ds_bus_master);
  131. ds_put_device(ds_dev);
  132. kfree(ds_bus_master);
  133. }
  134. module_init(ds_w1_init);
  135. module_exit(ds_w1_fini);
  136. MODULE_LICENSE("GPL");
  137. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");