| 1 | /* |
|---|
| 2 | i2c-dev.c - i2c-bus driver, char device interface |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 1995-97 Simon G. Vogl |
|---|
| 5 | Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl> |
|---|
| 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., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module. |
|---|
| 23 | But I have used so much of his original code and ideas that it seems |
|---|
| 24 | only fair to recognize him as co-author -- Frodo */ |
|---|
| 25 | |
|---|
| 26 | /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */ |
|---|
| 27 | |
|---|
| 28 | /* The devfs code is contributed by Philipp Matthias Hahn |
|---|
| 29 | <pmhahn@titan.lahn.de> */ |
|---|
| 30 | |
|---|
| 31 | /* $Id$ */ |
|---|
| 32 | |
|---|
| 33 | #include <linux/config.h> |
|---|
| 34 | #include <linux/kernel.h> |
|---|
| 35 | #include <linux/module.h> |
|---|
| 36 | #include <linux/fs.h> |
|---|
| 37 | #include <linux/malloc.h> |
|---|
| 38 | #include <linux/version.h> |
|---|
| 39 | #if LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) |
|---|
| 40 | #include <linux/smp_lock.h> |
|---|
| 41 | #endif /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */ |
|---|
| 42 | #ifdef CONFIG_DEVFS_FS |
|---|
| 43 | #include <linux/devfs_fs_kernel.h> |
|---|
| 44 | #endif |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /* If you want debugging uncomment: */ |
|---|
| 48 | /* #define DEBUG */ |
|---|
| 49 | |
|---|
| 50 | #include <linux/init.h> |
|---|
| 51 | #include <asm/uaccess.h> |
|---|
| 52 | |
|---|
| 53 | #include "i2c.h" |
|---|
| 54 | #include "i2c-dev.h" |
|---|
| 55 | |
|---|
| 56 | #ifdef MODULE |
|---|
| 57 | extern int init_module(void); |
|---|
| 58 | extern int cleanup_module(void); |
|---|
| 59 | #endif /* def MODULE */ |
|---|
| 60 | |
|---|
| 61 | /* struct file_operations changed too often in the 2.1 series for nice code */ |
|---|
| 62 | |
|---|
| 63 | static loff_t i2cdev_lseek (struct file *file, loff_t offset, int origin); |
|---|
| 64 | |
|---|
| 65 | static ssize_t i2cdev_read (struct file *file, char *buf, size_t count, |
|---|
| 66 | loff_t *offset); |
|---|
| 67 | static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count, |
|---|
| 68 | loff_t *offset); |
|---|
| 69 | |
|---|
| 70 | static int i2cdev_ioctl (struct inode *inode, struct file *file, |
|---|
| 71 | unsigned int cmd, unsigned long arg); |
|---|
| 72 | static int i2cdev_open (struct inode *inode, struct file *file); |
|---|
| 73 | |
|---|
| 74 | static int i2cdev_release (struct inode *inode, struct file *file); |
|---|
| 75 | |
|---|
| 76 | static int i2cdev_attach_adapter(struct i2c_adapter *adap); |
|---|
| 77 | static int i2cdev_detach_client(struct i2c_client *client); |
|---|
| 78 | static int i2cdev_command(struct i2c_client *client, unsigned int cmd, |
|---|
| 79 | void *arg); |
|---|
| 80 | |
|---|
| 81 | #ifdef MODULE |
|---|
| 82 | static |
|---|
| 83 | #else |
|---|
| 84 | extern |
|---|
| 85 | #endif |
|---|
| 86 | int __init i2c_dev_init(void); |
|---|
| 87 | static int i2cdev_cleanup(void); |
|---|
| 88 | |
|---|
| 89 | static struct file_operations i2cdev_fops = { |
|---|
| 90 | #if LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) |
|---|
| 91 | owner: THIS_MODULE, |
|---|
| 92 | #endif /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */ |
|---|
| 93 | llseek: i2cdev_lseek, |
|---|
| 94 | read: i2cdev_read, |
|---|
| 95 | write: i2cdev_write, |
|---|
| 96 | ioctl: i2cdev_ioctl, |
|---|
| 97 | open: i2cdev_open, |
|---|
| 98 | release: i2cdev_release, |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | #define I2CDEV_ADAPS_MAX I2C_ADAP_MAX |
|---|
| 102 | static struct i2c_adapter *i2cdev_adaps[I2CDEV_ADAPS_MAX]; |
|---|
| 103 | #ifdef CONFIG_DEVFS_FS |
|---|
| 104 | static devfs_handle_t devfs_i2c[I2CDEV_ADAPS_MAX]; |
|---|
| 105 | static devfs_handle_t devfs_handle = NULL; |
|---|
| 106 | #endif |
|---|
| 107 | |
|---|
| 108 | static struct i2c_driver i2cdev_driver = { |
|---|
| 109 | name: "i2c-dev dummy driver", |
|---|
| 110 | id: I2C_DRIVERID_I2CDEV, |
|---|
| 111 | flags: I2C_DF_DUMMY, |
|---|
| 112 | attach_adapter: i2cdev_attach_adapter, |
|---|
| 113 | detach_client: i2cdev_detach_client, |
|---|
| 114 | command: i2cdev_command, |
|---|
| 115 | /* inc_use: NULL, |
|---|
| 116 | dec_use: NULL, */ |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | static struct i2c_client i2cdev_client_template = { |
|---|
| 120 | name: "I2C /dev entry", |
|---|
| 121 | id: 1, |
|---|
| 122 | flags: 0, |
|---|
| 123 | addr: -1, |
|---|
| 124 | /* adapter: NULL, */ |
|---|
| 125 | driver: &i2cdev_driver, |
|---|
| 126 | /* data: NULL */ |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | static int i2cdev_initialized; |
|---|
| 130 | |
|---|
| 131 | /* Note that the lseek function is called llseek in 2.1 kernels. But things |
|---|
| 132 | are complicated enough as is. */ |
|---|
| 133 | loff_t i2cdev_lseek (struct file *file, loff_t offset, int origin) |
|---|
| 134 | { |
|---|
| 135 | #ifdef DEBUG |
|---|
| 136 | struct inode *inode = file->f_dentry->d_inode; |
|---|
| 137 | printk("i2c-dev.o: i2c-%d lseek to %ld bytes relative to %d.\n", |
|---|
| 138 | MINOR(inode->i_rdev),(long) offset,origin); |
|---|
| 139 | #endif /* DEBUG */ |
|---|
| 140 | return -ESPIPE; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | static ssize_t i2cdev_read (struct file *file, char *buf, size_t count, |
|---|
| 144 | loff_t *offset) |
|---|
| 145 | { |
|---|
| 146 | char *tmp; |
|---|
| 147 | int ret; |
|---|
| 148 | |
|---|
| 149 | #ifdef DEBUG |
|---|
| 150 | struct inode *inode = file->f_dentry->d_inode; |
|---|
| 151 | #endif /* DEBUG */ |
|---|
| 152 | |
|---|
| 153 | struct i2c_client *client = (struct i2c_client *)file->private_data; |
|---|
| 154 | |
|---|
| 155 | /* copy user space data to kernel space. */ |
|---|
| 156 | tmp = kmalloc(count,GFP_KERNEL); |
|---|
| 157 | if (tmp==NULL) |
|---|
| 158 | return -ENOMEM; |
|---|
| 159 | |
|---|
| 160 | #ifdef DEBUG |
|---|
| 161 | printk("i2c-dev.o: i2c-%d reading %d bytes.\n",MINOR(inode->i_rdev), |
|---|
| 162 | count); |
|---|
| 163 | #endif |
|---|
| 164 | |
|---|
| 165 | ret = i2c_master_recv(client,tmp,count); |
|---|
| 166 | if (ret >= 0) |
|---|
| 167 | ret = copy_to_user(buf,tmp,count)?-EFAULT:ret; |
|---|
| 168 | kfree(tmp); |
|---|
| 169 | return ret; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | static ssize_t i2cdev_write (struct file *file, const char *buf, size_t count, |
|---|
| 173 | loff_t *offset) |
|---|
| 174 | { |
|---|
| 175 | int ret; |
|---|
| 176 | char *tmp; |
|---|
| 177 | struct i2c_client *client = (struct i2c_client *)file->private_data; |
|---|
| 178 | |
|---|
| 179 | #ifdef DEBUG |
|---|
| 180 | struct inode *inode = file->f_dentry->d_inode; |
|---|
| 181 | #endif /* DEBUG */ |
|---|
| 182 | |
|---|
| 183 | /* copy user space data to kernel space. */ |
|---|
| 184 | tmp = kmalloc(count,GFP_KERNEL); |
|---|
| 185 | if (tmp==NULL) |
|---|
| 186 | return -ENOMEM; |
|---|
| 187 | if (copy_from_user(tmp,buf,count)) { |
|---|
| 188 | kfree(tmp); |
|---|
| 189 | return -EFAULT; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | #ifdef DEBUG |
|---|
| 193 | printk("i2c-dev.o: i2c-%d writing %d bytes.\n",MINOR(inode->i_rdev), |
|---|
| 194 | count); |
|---|
| 195 | #endif |
|---|
| 196 | ret = i2c_master_send(client,tmp,count); |
|---|
| 197 | kfree(tmp); |
|---|
| 198 | return ret; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | int i2cdev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, |
|---|
| 202 | unsigned long arg) |
|---|
| 203 | { |
|---|
| 204 | struct i2c_client *client = (struct i2c_client *)file->private_data; |
|---|
| 205 | struct i2c_rdwr_ioctl_data rdwr_arg; |
|---|
| 206 | struct i2c_smbus_ioctl_data data_arg; |
|---|
| 207 | union i2c_smbus_data temp; |
|---|
| 208 | struct i2c_msg *rdwr_pa; |
|---|
| 209 | int i,datasize,res; |
|---|
| 210 | unsigned long funcs; |
|---|
| 211 | |
|---|
| 212 | #ifdef DEBUG |
|---|
| 213 | printk("i2c-dev.o: i2c-%d ioctl, cmd: 0x%x, arg: %lx.\n", |
|---|
| 214 | MINOR(inode->i_rdev),cmd, arg); |
|---|
| 215 | #endif /* DEBUG */ |
|---|
| 216 | |
|---|
| 217 | switch ( cmd ) { |
|---|
| 218 | case I2C_SLAVE: |
|---|
| 219 | case I2C_SLAVE_FORCE: |
|---|
| 220 | if ((arg > 0x3ff) || |
|---|
| 221 | (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f)) |
|---|
| 222 | return -EINVAL; |
|---|
| 223 | if ((cmd == I2C_SLAVE) && i2c_check_addr(client->adapter,arg)) |
|---|
| 224 | return -EBUSY; |
|---|
| 225 | client->addr = arg; |
|---|
| 226 | return 0; |
|---|
| 227 | case I2C_TENBIT: |
|---|
| 228 | if (arg) |
|---|
| 229 | client->flags |= I2C_M_TEN; |
|---|
| 230 | else |
|---|
| 231 | client->flags &= ~I2C_M_TEN; |
|---|
| 232 | return 0; |
|---|
| 233 | case I2C_FUNCS: |
|---|
| 234 | funcs = i2c_get_functionality(client->adapter); |
|---|
| 235 | return (copy_to_user((unsigned long *)arg,&funcs, |
|---|
| 236 | sizeof(unsigned long)))?-EFAULT:0; |
|---|
| 237 | |
|---|
| 238 | case I2C_RDWR: |
|---|
| 239 | if (copy_from_user(&rdwr_arg, |
|---|
| 240 | (struct i2c_rdwr_ioctl_data *)arg, |
|---|
| 241 | sizeof(rdwr_arg))) |
|---|
| 242 | return -EFAULT; |
|---|
| 243 | |
|---|
| 244 | rdwr_pa = (struct i2c_msg *) |
|---|
| 245 | kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg), |
|---|
| 246 | GFP_KERNEL); |
|---|
| 247 | |
|---|
| 248 | if (rdwr_pa == NULL) return -ENOMEM; |
|---|
| 249 | |
|---|
| 250 | res = 0; |
|---|
| 251 | for( i=0; i<rdwr_arg.nmsgs; i++ ) |
|---|
| 252 | { |
|---|
| 253 | if(copy_from_user(&(rdwr_pa[i]), |
|---|
| 254 | &(rdwr_arg.msgs[i]), |
|---|
| 255 | sizeof(rdwr_pa[i]))) |
|---|
| 256 | { |
|---|
| 257 | res = -EFAULT; |
|---|
| 258 | break; |
|---|
| 259 | } |
|---|
| 260 | rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL); |
|---|
| 261 | if(rdwr_pa[i].buf == NULL) |
|---|
| 262 | { |
|---|
| 263 | res = -ENOMEM; |
|---|
| 264 | break; |
|---|
| 265 | } |
|---|
| 266 | if(copy_from_user(rdwr_pa[i].buf, |
|---|
| 267 | rdwr_arg.msgs[i].buf, |
|---|
| 268 | rdwr_pa[i].len)) |
|---|
| 269 | { |
|---|
| 270 | kfree(rdwr_pa[i].buf); |
|---|
| 271 | res = -EFAULT; |
|---|
| 272 | break; |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | if (!res) |
|---|
| 276 | { |
|---|
| 277 | res = i2c_transfer(client->adapter, |
|---|
| 278 | rdwr_pa, |
|---|
| 279 | rdwr_arg.nmsgs); |
|---|
| 280 | } |
|---|
| 281 | while(i-- > 0) |
|---|
| 282 | { |
|---|
| 283 | if( res>=0 && (rdwr_pa[i].flags & I2C_M_RD)) |
|---|
| 284 | { |
|---|
| 285 | if(copy_to_user( |
|---|
| 286 | rdwr_arg.msgs[i].buf, |
|---|
| 287 | rdwr_pa[i].buf, |
|---|
| 288 | rdwr_pa[i].len)) |
|---|
| 289 | { |
|---|
| 290 | res = -EFAULT; |
|---|
| 291 | } |
|---|
| 292 | } |
|---|
| 293 | kfree(rdwr_pa[i].buf); |
|---|
| 294 | } |
|---|
| 295 | kfree(rdwr_pa); |
|---|
| 296 | return res; |
|---|
| 297 | |
|---|
| 298 | case I2C_SMBUS: |
|---|
| 299 | if (copy_from_user(&data_arg, |
|---|
| 300 | (struct i2c_smbus_ioctl_data *) arg, |
|---|
| 301 | sizeof(struct i2c_smbus_ioctl_data))) |
|---|
| 302 | return -EFAULT; |
|---|
| 303 | if ((data_arg.size != I2C_SMBUS_BYTE) && |
|---|
| 304 | (data_arg.size != I2C_SMBUS_QUICK) && |
|---|
| 305 | (data_arg.size != I2C_SMBUS_BYTE_DATA) && |
|---|
| 306 | (data_arg.size != I2C_SMBUS_WORD_DATA) && |
|---|
| 307 | (data_arg.size != I2C_SMBUS_PROC_CALL) && |
|---|
| 308 | (data_arg.size != I2C_SMBUS_BLOCK_DATA) && |
|---|
| 309 | (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA)) { |
|---|
| 310 | #ifdef DEBUG |
|---|
| 311 | printk("i2c-dev.o: size out of range (%x) in ioctl I2C_SMBUS.\n", |
|---|
| 312 | data_arg.size); |
|---|
| 313 | #endif |
|---|
| 314 | return -EINVAL; |
|---|
| 315 | } |
|---|
| 316 | /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1, |
|---|
| 317 | so the check is valid if size==I2C_SMBUS_QUICK too. */ |
|---|
| 318 | if ((data_arg.read_write != I2C_SMBUS_READ) && |
|---|
| 319 | (data_arg.read_write != I2C_SMBUS_WRITE)) { |
|---|
| 320 | #ifdef DEBUG |
|---|
| 321 | printk("i2c-dev.o: read_write out of range (%x) in ioctl I2C_SMBUS.\n", |
|---|
| 322 | data_arg.read_write); |
|---|
| 323 | #endif |
|---|
| 324 | return -EINVAL; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | /* Note that command values are always valid! */ |
|---|
| 328 | |
|---|
| 329 | if ((data_arg.size == I2C_SMBUS_QUICK) || |
|---|
| 330 | ((data_arg.size == I2C_SMBUS_BYTE) && |
|---|
| 331 | (data_arg.read_write == I2C_SMBUS_WRITE))) |
|---|
| 332 | /* These are special: we do not use data */ |
|---|
| 333 | return i2c_smbus_xfer(client->adapter, client->addr, |
|---|
| 334 | client->flags, |
|---|
| 335 | data_arg.read_write, |
|---|
| 336 | data_arg.command, |
|---|
| 337 | data_arg.size, NULL); |
|---|
| 338 | |
|---|
| 339 | if (data_arg.data == NULL) { |
|---|
| 340 | #ifdef DEBUG |
|---|
| 341 | printk("i2c-dev.o: data is NULL pointer in ioctl I2C_SMBUS.\n"); |
|---|
| 342 | #endif |
|---|
| 343 | return -EINVAL; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | if ((data_arg.size == I2C_SMBUS_BYTE_DATA) || |
|---|
| 347 | (data_arg.size == I2C_SMBUS_BYTE)) |
|---|
| 348 | datasize = sizeof(data_arg.data->byte); |
|---|
| 349 | else if ((data_arg.size == I2C_SMBUS_WORD_DATA) || |
|---|
| 350 | (data_arg.size == I2C_SMBUS_PROC_CALL)) |
|---|
| 351 | datasize = sizeof(data_arg.data->word); |
|---|
| 352 | else /* size == I2C_SMBUS_BLOCK_DATA */ |
|---|
| 353 | datasize = sizeof(data_arg.data->block); |
|---|
| 354 | |
|---|
| 355 | if ((data_arg.size == I2C_SMBUS_PROC_CALL) || |
|---|
| 356 | (data_arg.read_write == I2C_SMBUS_WRITE)) { |
|---|
| 357 | if (copy_from_user(&temp, data_arg.data, datasize)) |
|---|
| 358 | return -EFAULT; |
|---|
| 359 | } |
|---|
| 360 | res = i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
|---|
| 361 | data_arg.read_write, |
|---|
| 362 | data_arg.command,data_arg.size,&temp); |
|---|
| 363 | if (! res && ((data_arg.size == I2C_SMBUS_PROC_CALL) || |
|---|
| 364 | (data_arg.read_write == I2C_SMBUS_READ))) { |
|---|
| 365 | if (copy_to_user(data_arg.data, &temp, datasize)) |
|---|
| 366 | return -EFAULT; |
|---|
| 367 | } |
|---|
| 368 | return res; |
|---|
| 369 | |
|---|
| 370 | default: |
|---|
| 371 | return i2c_control(client,cmd,arg); |
|---|
| 372 | } |
|---|
| 373 | return 0; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | int i2cdev_open (struct inode *inode, struct file *file) |
|---|
| 377 | { |
|---|
| 378 | unsigned int minor = MINOR(inode->i_rdev); |
|---|
| 379 | struct i2c_client *client; |
|---|
| 380 | |
|---|
| 381 | if ((minor >= I2CDEV_ADAPS_MAX) || ! (i2cdev_adaps[minor])) { |
|---|
| 382 | #ifdef DEBUG |
|---|
| 383 | printk("i2c-dev.o: Trying to open unattached adapter i2c-%d\n", |
|---|
| 384 | minor); |
|---|
| 385 | #endif |
|---|
| 386 | return -ENODEV; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /* Note that we here allocate a client for later use, but we will *not* |
|---|
| 390 | register this client! Yes, this is safe. No, it is not very clean. */ |
|---|
| 391 | if(! (client = kmalloc(sizeof(struct i2c_client),GFP_KERNEL))) |
|---|
| 392 | return -ENOMEM; |
|---|
| 393 | memcpy(client,&i2cdev_client_template,sizeof(struct i2c_client)); |
|---|
| 394 | client->adapter = i2cdev_adaps[minor]; |
|---|
| 395 | file->private_data = client; |
|---|
| 396 | |
|---|
| 397 | if (i2cdev_adaps[minor]->inc_use) |
|---|
| 398 | i2cdev_adaps[minor]->inc_use(i2cdev_adaps[minor]); |
|---|
| 399 | #if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,0) |
|---|
| 400 | MOD_INC_USE_COUNT; |
|---|
| 401 | #endif /* LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,0) */ |
|---|
| 402 | |
|---|
| 403 | #ifdef DEBUG |
|---|
| 404 | printk("i2c-dev.o: opened i2c-%d\n",minor); |
|---|
| 405 | #endif |
|---|
| 406 | return 0; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | static int i2cdev_release (struct inode *inode, struct file *file) |
|---|
| 410 | { |
|---|
| 411 | unsigned int minor = MINOR(inode->i_rdev); |
|---|
| 412 | kfree(file->private_data); |
|---|
| 413 | file->private_data=NULL; |
|---|
| 414 | #ifdef DEBUG |
|---|
| 415 | printk("i2c-dev.o: Closed: i2c-%d\n", minor); |
|---|
| 416 | #endif |
|---|
| 417 | #if LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,0) |
|---|
| 418 | MOD_DEC_USE_COUNT; |
|---|
| 419 | #else /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */ |
|---|
| 420 | lock_kernel(); |
|---|
| 421 | #endif /* LINUX_KERNEL_VERSION < KERNEL_VERSION(2,4,0) */ |
|---|
| 422 | if (i2cdev_adaps[minor]->dec_use) |
|---|
| 423 | i2cdev_adaps[minor]->dec_use(i2cdev_adaps[minor]); |
|---|
| 424 | #if LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) |
|---|
| 425 | unlock_kernel(); |
|---|
| 426 | #endif /* LINUX_KERNEL_VERSION >= KERNEL_VERSION(2,4,0) */ |
|---|
| 427 | return 0; |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | int i2cdev_attach_adapter(struct i2c_adapter *adap) |
|---|
| 431 | { |
|---|
| 432 | int i; |
|---|
| 433 | char name[8]; |
|---|
| 434 | |
|---|
| 435 | if ((i = i2c_adapter_id(adap)) < 0) { |
|---|
| 436 | printk("i2c-dev.o: Unknown adapter ?!?\n"); |
|---|
| 437 | return -ENODEV; |
|---|
| 438 | } |
|---|
| 439 | if (i >= I2CDEV_ADAPS_MAX) { |
|---|
| 440 | printk("i2c-dev.o: Adapter number too large?!? (%d)\n",i); |
|---|
| 441 | return -ENODEV; |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | sprintf (name, "%d", i); |
|---|
| 445 | if (! i2cdev_adaps[i]) { |
|---|
| 446 | i2cdev_adaps[i] = adap; |
|---|
| 447 | #ifdef CONFIG_DEVFS_FS |
|---|
| 448 | devfs_i2c[i] = devfs_register (devfs_handle, name, |
|---|
| 449 | DEVFS_FL_DEFAULT, I2C_MAJOR, i, |
|---|
| 450 | S_IFCHR | S_IRUSR | S_IWUSR, |
|---|
| 451 | &i2cdev_fops, NULL); |
|---|
| 452 | #endif |
|---|
| 453 | printk("i2c-dev.o: Registered '%s' as minor %d\n",adap->name,i); |
|---|
| 454 | } else { |
|---|
| 455 | /* This is actually a detach_adapter call! */ |
|---|
| 456 | #ifdef CONFIG_DEVFS_FS |
|---|
| 457 | devfs_unregister(devfs_i2c[i]); |
|---|
| 458 | #endif |
|---|
| 459 | i2cdev_adaps[i] = NULL; |
|---|
| 460 | #ifdef DEBUG |
|---|
| 461 | printk("i2c-dev.o: Adapter unregistered: %s\n",adap->name); |
|---|
| 462 | #endif |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | return 0; |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | int i2cdev_detach_client(struct i2c_client *client) |
|---|
| 469 | { |
|---|
| 470 | return 0; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | static int i2cdev_command(struct i2c_client *client, unsigned int cmd, |
|---|
| 474 | void *arg) |
|---|
| 475 | { |
|---|
| 476 | return -1; |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | int __init i2c_dev_init(void) |
|---|
| 480 | { |
|---|
| 481 | int res; |
|---|
| 482 | |
|---|
| 483 | printk("i2c-dev.o: i2c /dev entries driver module\n"); |
|---|
| 484 | |
|---|
| 485 | i2cdev_initialized = 0; |
|---|
| 486 | #ifdef CONFIG_DEVFS_FS |
|---|
| 487 | if (devfs_register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops)) { |
|---|
| 488 | #else |
|---|
| 489 | if (register_chrdev(I2C_MAJOR,"i2c",&i2cdev_fops)) { |
|---|
| 490 | #endif |
|---|
| 491 | printk("i2c-dev.o: unable to get major %d for i2c bus\n", |
|---|
| 492 | I2C_MAJOR); |
|---|
| 493 | return -EIO; |
|---|
| 494 | } |
|---|
| 495 | #ifdef CONFIG_DEVFS_FS |
|---|
| 496 | devfs_handle = devfs_mk_dir(NULL, "i2c", NULL); |
|---|
| 497 | #endif |
|---|
| 498 | i2cdev_initialized ++; |
|---|
| 499 | |
|---|
| 500 | if ((res = i2c_add_driver(&i2cdev_driver))) { |
|---|
| 501 | printk("i2c-dev.o: Driver registration failed, module not inserted.\n"); |
|---|
| 502 | i2cdev_cleanup(); |
|---|
| 503 | return res; |
|---|
| 504 | } |
|---|
| 505 | i2cdev_initialized ++; |
|---|
| 506 | return 0; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | int i2cdev_cleanup(void) |
|---|
| 510 | { |
|---|
| 511 | int res; |
|---|
| 512 | |
|---|
| 513 | if (i2cdev_initialized >= 2) { |
|---|
| 514 | if ((res = i2c_del_driver(&i2cdev_driver))) { |
|---|
| 515 | printk("i2c-dev.o: Driver deregistration failed, " |
|---|
| 516 | "module not removed.\n"); |
|---|
| 517 | return res; |
|---|
| 518 | } |
|---|
| 519 | i2cdev_initialized --; |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | if (i2cdev_initialized >= 1) { |
|---|
| 523 | #ifdef CONFIG_DEVFS_FS |
|---|
| 524 | devfs_unregister(devfs_handle); |
|---|
| 525 | if ((res = devfs_unregister_chrdev(I2C_MAJOR, "i2c"))) { |
|---|
| 526 | #else |
|---|
| 527 | if ((res = unregister_chrdev(I2C_MAJOR,"i2c"))) { |
|---|
| 528 | #endif |
|---|
| 529 | printk("i2c-dev.o: unable to release major %d for i2c bus\n", |
|---|
| 530 | I2C_MAJOR); |
|---|
| 531 | return res; |
|---|
| 532 | } |
|---|
| 533 | i2cdev_initialized --; |
|---|
| 534 | } |
|---|
| 535 | return 0; |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | EXPORT_NO_SYMBOLS; |
|---|
| 539 | |
|---|
| 540 | #ifdef MODULE |
|---|
| 541 | |
|---|
| 542 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
|---|
| 543 | MODULE_DESCRIPTION("I2C /dev entries driver"); |
|---|
| 544 | |
|---|
| 545 | int init_module(void) |
|---|
| 546 | { |
|---|
| 547 | return i2c_dev_init(); |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | int cleanup_module(void) |
|---|
| 551 | { |
|---|
| 552 | return i2cdev_cleanup(); |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | #endif /* def MODULE */ |
|---|
| 556 | |
|---|