Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 2 | * ds_w1_bridge.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 <linux/module.h> |
| 23 | #include <linux/types.h> |
| 24 | |
| 25 | #include "../w1/w1.h" |
| 26 | #include "../w1/w1_int.h" |
| 27 | #include "dscore.h" |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static struct ds_device *ds_dev; |
| 30 | static struct w1_bus_master *ds_bus_master; |
| 31 | |
| 32 | static u8 ds9490r_touch_bit(unsigned long data, u8 bit) |
| 33 | { |
| 34 | u8 ret; |
| 35 | struct ds_device *dev = (struct ds_device *)data; |
| 36 | |
| 37 | if (ds_touch_bit(dev, bit, &ret)) |
| 38 | return 0; |
| 39 | |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | static void ds9490r_write_bit(unsigned long data, u8 bit) |
| 44 | { |
| 45 | struct ds_device *dev = (struct ds_device *)data; |
| 46 | |
| 47 | ds_write_bit(dev, bit); |
| 48 | } |
| 49 | |
| 50 | static void ds9490r_write_byte(unsigned long data, u8 byte) |
| 51 | { |
| 52 | struct ds_device *dev = (struct ds_device *)data; |
| 53 | |
| 54 | ds_write_byte(dev, byte); |
| 55 | } |
| 56 | |
| 57 | static u8 ds9490r_read_bit(unsigned long data) |
| 58 | { |
| 59 | struct ds_device *dev = (struct ds_device *)data; |
| 60 | int err; |
| 61 | u8 bit = 0; |
| 62 | |
| 63 | err = ds_touch_bit(dev, 1, &bit); |
| 64 | if (err) |
| 65 | return 0; |
| 66 | //err = ds_read_bit(dev, &bit); |
| 67 | //if (err) |
| 68 | // return 0; |
| 69 | |
| 70 | return bit & 1; |
| 71 | } |
| 72 | |
| 73 | static u8 ds9490r_read_byte(unsigned long data) |
| 74 | { |
| 75 | struct ds_device *dev = (struct ds_device *)data; |
| 76 | int err; |
| 77 | u8 byte = 0; |
| 78 | |
| 79 | err = ds_read_byte(dev, &byte); |
| 80 | if (err) |
| 81 | return 0; |
| 82 | |
| 83 | return byte; |
| 84 | } |
| 85 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 86 | static void ds9490r_write_block(unsigned long data, const u8 *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | struct ds_device *dev = (struct ds_device *)data; |
| 89 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 90 | ds_write_block(dev, (u8 *)buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static u8 ds9490r_read_block(unsigned long data, u8 *buf, int len) |
| 94 | { |
| 95 | struct ds_device *dev = (struct ds_device *)data; |
| 96 | int err; |
| 97 | |
| 98 | err = ds_read_block(dev, buf, len); |
| 99 | if (err < 0) |
| 100 | return 0; |
| 101 | |
| 102 | return len; |
| 103 | } |
| 104 | |
| 105 | static u8 ds9490r_reset(unsigned long data) |
| 106 | { |
| 107 | struct ds_device *dev = (struct ds_device *)data; |
| 108 | struct ds_status st; |
| 109 | int err; |
| 110 | |
| 111 | memset(&st, 0, sizeof(st)); |
| 112 | |
| 113 | err = ds_reset(dev, &st); |
| 114 | if (err) |
| 115 | return 1; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int __devinit ds_w1_init(void) |
| 121 | { |
| 122 | int err; |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | ds_bus_master = kmalloc(sizeof(*ds_bus_master), GFP_KERNEL); |
| 125 | if (!ds_bus_master) { |
| 126 | printk(KERN_ERR "Failed to allocate DS9490R USB<->W1 bus_master structure.\n"); |
| 127 | return -ENOMEM; |
| 128 | } |
| 129 | |
| 130 | ds_dev = ds_get_device(); |
| 131 | if (!ds_dev) { |
| 132 | printk(KERN_ERR "DS9490R is not registered.\n"); |
| 133 | err = -ENODEV; |
| 134 | goto err_out_free_bus_master; |
| 135 | } |
| 136 | |
| 137 | memset(ds_bus_master, 0, sizeof(*ds_bus_master)); |
| 138 | |
Evgeniy Polyakov | 8949d2a | 2005-08-03 15:14:50 +0400 | [diff] [blame] | 139 | ds_bus_master->data = (unsigned long)ds_dev; |
| 140 | ds_bus_master->touch_bit = &ds9490r_touch_bit; |
| 141 | ds_bus_master->read_bit = &ds9490r_read_bit; |
| 142 | ds_bus_master->write_bit = &ds9490r_write_bit; |
| 143 | ds_bus_master->read_byte = &ds9490r_read_byte; |
| 144 | ds_bus_master->write_byte = &ds9490r_write_byte; |
| 145 | ds_bus_master->read_block = &ds9490r_read_block; |
| 146 | ds_bus_master->write_block = &ds9490r_write_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | ds_bus_master->reset_bus = &ds9490r_reset; |
| 148 | |
| 149 | err = w1_add_master_device(ds_bus_master); |
| 150 | if (err) |
| 151 | goto err_out_put_device; |
| 152 | |
| 153 | return 0; |
| 154 | |
| 155 | err_out_put_device: |
| 156 | ds_put_device(ds_dev); |
| 157 | err_out_free_bus_master: |
| 158 | kfree(ds_bus_master); |
| 159 | |
| 160 | return err; |
| 161 | } |
| 162 | |
| 163 | static void __devexit ds_w1_fini(void) |
| 164 | { |
| 165 | w1_remove_master_device(ds_bus_master); |
| 166 | ds_put_device(ds_dev); |
| 167 | kfree(ds_bus_master); |
| 168 | } |
| 169 | |
| 170 | module_init(ds_w1_init); |
| 171 | module_exit(ds_w1_fini); |
| 172 | |
| 173 | MODULE_LICENSE("GPL"); |
| 174 | MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>"); |