getting device descriptors / file handler in kernel space ?

I've got some I2c stuff running from userspace. But originally I want
to write a kernel module, initialising all the i2c devices and handle
the transfer.
But what do I have to do to get a device descriptor / filehandler for /
dev/i2c-1 ... in a kernel module? open does not work ...
Same for writing/reading files ...

I’ve got some I2c stuff running from userspace.


Would you mind sharing the necessary changes to enable access to the I2C port on the expansion connector?

(At least I assume this is what you are using)

Sharing the userspace code would also be very much appreciated!


Enabled i2c dev modules in the kernel?
Nishanth Menon

No changes to do, just some lines in userspace. Take a look and the
Some line added - far away from being perfect, just as an example.

But again to all others: How to do a fd=open("/dev/i2c_1",O_RDWR ) in
kernelspace or what is the way to get a file descriptor?

Sorry? The devices are available, otherwise I couldn't access them fro
But I will do all this inside a driver, a loadable kernel module...

You need to use i2c_transfer API calls if you need to access it from
kernel space. see [1] for sample drivers. [2] is a very simplistic
Nishanth Menon

Please forgive my ignorance, but is there a mechanism to use i2c
userspace tools from kernel space?

I always thought one should write an i2c driver and client for the
related i2c device.

I remember in DaVinci tree there was a generic driver/client to
read/write from an i2c address. They were using this generic driver to
communicate with various devices like i2c expanders, msp430 based rtc,
etc. I used that driver to write a driver for tsc2004 touchscreen


There are two ways to access i2c:
a) using i2c-dev driver in kernel space and a userapp to interface to
the i2c-dev (this basically uses a "dummy client").
b) as you mentioned using the kernel client-driver architecture.
[1] has more information on the architecture in general.
Nishanth Menon

I did use the way you showed me - and it works. Thanks!
Originally I wanted to use the way at it was described in the kernel-

res = i2c_smbus_read_word_data(file,register);

That's way I asked for the file descriptor...