i2c-dev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. i2c-dev.c - i2c-bus driver, char device interface
  3. Copyright (C) 1995-97 Simon G. Vogl
  4. Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
  5. Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. MA 02110-1301 USA.
  18. */
  19. /* Note that this is a complete rewrite of Simon Vogl's i2c-dev module.
  20. But I have used so much of his original code and ideas that it seems
  21. only fair to recognize him as co-author -- Frodo */
  22. /* The I2C_RDWR ioctl code is written by Kolja Waschk <waschk@telos.de> */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/device.h>
  26. #include <linux/notifier.h>
  27. #include <linux/fs.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/list.h>
  31. #include <linux/i2c.h>
  32. #include <linux/i2c-dev.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/uaccess.h>
  35. /*
  36. * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a
  37. * slave (i2c_client) with which messages will be exchanged. It's coupled
  38. * with a character special file which is accessed by user mode drivers.
  39. *
  40. * The list of i2c_dev structures is parallel to the i2c_adapter lists
  41. * maintained by the driver model, and is updated using bus notifications.
  42. */
  43. struct i2c_dev {
  44. struct list_head list;
  45. struct i2c_adapter *adap;
  46. struct device *dev;
  47. };
  48. #define I2C_MINORS 256
  49. static LIST_HEAD(i2c_dev_list);
  50. static DEFINE_SPINLOCK(i2c_dev_list_lock);
  51. static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
  52. {
  53. struct i2c_dev *i2c_dev;
  54. spin_lock(&i2c_dev_list_lock);
  55. list_for_each_entry(i2c_dev, &i2c_dev_list, list) {
  56. if (i2c_dev->adap->nr == index)
  57. goto found;
  58. }
  59. i2c_dev = NULL;
  60. found:
  61. spin_unlock(&i2c_dev_list_lock);
  62. return i2c_dev;
  63. }
  64. static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
  65. {
  66. struct i2c_dev *i2c_dev;
  67. if (adap->nr >= I2C_MINORS) {
  68. printk(KERN_ERR "i2c-dev: Out of device minors (%d)\n",
  69. adap->nr);
  70. return ERR_PTR(-ENODEV);
  71. }
  72. i2c_dev = kzalloc(sizeof(*i2c_dev), GFP_KERNEL);
  73. if (!i2c_dev)
  74. return ERR_PTR(-ENOMEM);
  75. i2c_dev->adap = adap;
  76. spin_lock(&i2c_dev_list_lock);
  77. list_add_tail(&i2c_dev->list, &i2c_dev_list);
  78. spin_unlock(&i2c_dev_list_lock);
  79. return i2c_dev;
  80. }
  81. static void return_i2c_dev(struct i2c_dev *i2c_dev)
  82. {
  83. spin_lock(&i2c_dev_list_lock);
  84. list_del(&i2c_dev->list);
  85. spin_unlock(&i2c_dev_list_lock);
  86. kfree(i2c_dev);
  87. }
  88. static ssize_t name_show(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. struct i2c_dev *i2c_dev = i2c_dev_get_by_minor(MINOR(dev->devt));
  92. if (!i2c_dev)
  93. return -ENODEV;
  94. return sprintf(buf, "%s\n", i2c_dev->adap->name);
  95. }
  96. static DEVICE_ATTR_RO(name);
  97. static struct attribute *i2c_attrs[] = {
  98. &dev_attr_name.attr,
  99. NULL,
  100. };
  101. ATTRIBUTE_GROUPS(i2c);
  102. /* ------------------------------------------------------------------------- */
  103. /*
  104. * After opening an instance of this character special file, a file
  105. * descriptor starts out associated only with an i2c_adapter (and bus).
  106. *
  107. * Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg
  108. * traffic to any devices on the bus used by that adapter. That's because
  109. * the i2c_msg vectors embed all the addressing information they need, and
  110. * are submitted directly to an i2c_adapter. However, SMBus-only adapters
  111. * don't support that interface.
  112. *
  113. * To use read()/write() system calls on that file descriptor, or to use
  114. * SMBus interfaces (and work with SMBus-only hosts!), you must first issue
  115. * an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl. That configures an anonymous
  116. * (never registered) i2c_client so it holds the addressing information
  117. * needed by those system calls and by this SMBus interface.
  118. */
  119. static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
  120. loff_t *offset)
  121. {
  122. char *tmp;
  123. int ret;
  124. struct i2c_client *client = file->private_data;
  125. if (count > 8192)
  126. count = 8192;
  127. tmp = kmalloc(count, GFP_KERNEL);
  128. if (tmp == NULL)
  129. return -ENOMEM;
  130. pr_debug("i2c-dev: i2c-%d reading %zu bytes.\n",
  131. iminor(file->f_path.dentry->d_inode), count);
  132. ret = i2c_master_recv(client, tmp, count);
  133. if (ret >= 0)
  134. ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
  135. kfree(tmp);
  136. return ret;
  137. }
  138. static ssize_t i2cdev_write(struct file *file, const char __user *buf,
  139. size_t count, loff_t *offset)
  140. {
  141. int ret;
  142. char *tmp;
  143. struct i2c_client *client = file->private_data;
  144. if (count > 8192)
  145. count = 8192;
  146. tmp = memdup_user(buf, count);
  147. if (IS_ERR(tmp))
  148. return PTR_ERR(tmp);
  149. pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",
  150. iminor(file->f_path.dentry->d_inode), count);
  151. ret = i2c_master_send(client, tmp, count);
  152. kfree(tmp);
  153. return ret;
  154. }
  155. static int i2cdev_check(struct device *dev, void *addrp)
  156. {
  157. struct i2c_client *client = i2c_verify_client(dev);
  158. if (!client || client->addr != *(unsigned int *)addrp)
  159. return 0;
  160. return dev->driver ? -EBUSY : 0;
  161. }
  162. /* walk up mux tree */
  163. static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
  164. {
  165. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  166. int result;
  167. result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
  168. if (!result && parent)
  169. result = i2cdev_check_mux_parents(parent, addr);
  170. return result;
  171. }
  172. /* recurse down mux tree */
  173. static int i2cdev_check_mux_children(struct device *dev, void *addrp)
  174. {
  175. int result;
  176. if (dev->type == &i2c_adapter_type)
  177. result = device_for_each_child(dev, addrp,
  178. i2cdev_check_mux_children);
  179. else
  180. result = i2cdev_check(dev, addrp);
  181. return result;
  182. }
  183. /* This address checking function differs from the one in i2c-core
  184. in that it considers an address with a registered device, but no
  185. driver bound to it, as NOT busy. */
  186. static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  187. {
  188. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  189. int result = 0;
  190. if (parent)
  191. result = i2cdev_check_mux_parents(parent, addr);
  192. if (!result)
  193. result = device_for_each_child(&adapter->dev, &addr,
  194. i2cdev_check_mux_children);
  195. return result;
  196. }
  197. static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client,
  198. unsigned long arg)
  199. {
  200. struct i2c_rdwr_ioctl_data rdwr_arg;
  201. struct i2c_msg *rdwr_pa;
  202. u8 __user **data_ptrs;
  203. int i, res;
  204. if (copy_from_user(&rdwr_arg,
  205. (struct i2c_rdwr_ioctl_data __user *)arg,
  206. sizeof(rdwr_arg)))
  207. return -EFAULT;
  208. /* Put an arbitrary limit on the number of messages that can
  209. * be sent at once */
  210. if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
  211. return -EINVAL;
  212. rdwr_pa = memdup_user(rdwr_arg.msgs,
  213. rdwr_arg.nmsgs * sizeof(struct i2c_msg));
  214. if (IS_ERR(rdwr_pa))
  215. return PTR_ERR(rdwr_pa);
  216. data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL);
  217. if (data_ptrs == NULL) {
  218. kfree(rdwr_pa);
  219. return -ENOMEM;
  220. }
  221. res = 0;
  222. for (i = 0; i < rdwr_arg.nmsgs; i++) {
  223. /* Limit the size of the message to a sane amount;
  224. * and don't let length change either. */
  225. if ((rdwr_pa[i].len > 8192) ||
  226. (rdwr_pa[i].flags & I2C_M_RECV_LEN)) {
  227. res = -EINVAL;
  228. break;
  229. }
  230. data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
  231. rdwr_pa[i].buf = memdup_user(data_ptrs[i], rdwr_pa[i].len);
  232. if (IS_ERR(rdwr_pa[i].buf)) {
  233. res = PTR_ERR(rdwr_pa[i].buf);
  234. break;
  235. }
  236. }
  237. if (res < 0) {
  238. int j;
  239. for (j = 0; j < i; ++j)
  240. kfree(rdwr_pa[j].buf);
  241. kfree(data_ptrs);
  242. kfree(rdwr_pa);
  243. return res;
  244. }
  245. res = i2c_transfer(client->adapter, rdwr_pa, rdwr_arg.nmsgs);
  246. while (i-- > 0) {
  247. if (res >= 0 && (rdwr_pa[i].flags & I2C_M_RD)) {
  248. if (copy_to_user(data_ptrs[i], rdwr_pa[i].buf,
  249. rdwr_pa[i].len))
  250. res = -EFAULT;
  251. }
  252. kfree(rdwr_pa[i].buf);
  253. }
  254. kfree(data_ptrs);
  255. kfree(rdwr_pa);
  256. return res;
  257. }
  258. static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
  259. unsigned long arg)
  260. {
  261. struct i2c_smbus_ioctl_data data_arg;
  262. union i2c_smbus_data temp;
  263. int datasize, res;
  264. if (copy_from_user(&data_arg,
  265. (struct i2c_smbus_ioctl_data __user *) arg,
  266. sizeof(struct i2c_smbus_ioctl_data)))
  267. return -EFAULT;
  268. if ((data_arg.size != I2C_SMBUS_BYTE) &&
  269. (data_arg.size != I2C_SMBUS_QUICK) &&
  270. (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
  271. (data_arg.size != I2C_SMBUS_WORD_DATA) &&
  272. (data_arg.size != I2C_SMBUS_PROC_CALL) &&
  273. (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
  274. (data_arg.size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
  275. (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
  276. (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
  277. dev_dbg(&client->adapter->dev,
  278. "size out of range (%x) in ioctl I2C_SMBUS.\n",
  279. data_arg.size);
  280. return -EINVAL;
  281. }
  282. /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
  283. so the check is valid if size==I2C_SMBUS_QUICK too. */
  284. if ((data_arg.read_write != I2C_SMBUS_READ) &&
  285. (data_arg.read_write != I2C_SMBUS_WRITE)) {
  286. dev_dbg(&client->adapter->dev,
  287. "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
  288. data_arg.read_write);
  289. return -EINVAL;
  290. }
  291. /* Note that command values are always valid! */
  292. if ((data_arg.size == I2C_SMBUS_QUICK) ||
  293. ((data_arg.size == I2C_SMBUS_BYTE) &&
  294. (data_arg.read_write == I2C_SMBUS_WRITE)))
  295. /* These are special: we do not use data */
  296. return i2c_smbus_xfer(client->adapter, client->addr,
  297. client->flags, data_arg.read_write,
  298. data_arg.command, data_arg.size, NULL);
  299. if (data_arg.data == NULL) {
  300. dev_dbg(&client->adapter->dev,
  301. "data is NULL pointer in ioctl I2C_SMBUS.\n");
  302. return -EINVAL;
  303. }
  304. if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
  305. (data_arg.size == I2C_SMBUS_BYTE))
  306. datasize = sizeof(data_arg.data->byte);
  307. else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
  308. (data_arg.size == I2C_SMBUS_PROC_CALL))
  309. datasize = sizeof(data_arg.data->word);
  310. else /* size == smbus block, i2c block, or block proc. call */
  311. datasize = sizeof(data_arg.data->block);
  312. if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
  313. (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  314. (data_arg.size == I2C_SMBUS_I2C_BLOCK_DATA) ||
  315. (data_arg.read_write == I2C_SMBUS_WRITE)) {
  316. if (copy_from_user(&temp, data_arg.data, datasize))
  317. return -EFAULT;
  318. }
  319. if (data_arg.size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
  320. /* Convert old I2C block commands to the new
  321. convention. This preserves binary compatibility. */
  322. data_arg.size = I2C_SMBUS_I2C_BLOCK_DATA;
  323. if (data_arg.read_write == I2C_SMBUS_READ)
  324. temp.block[0] = I2C_SMBUS_BLOCK_MAX;
  325. }
  326. res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  327. data_arg.read_write, data_arg.command, data_arg.size, &temp);
  328. if (!res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
  329. (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  330. (data_arg.read_write == I2C_SMBUS_READ))) {
  331. if (copy_to_user(data_arg.data, &temp, datasize))
  332. return -EFAULT;
  333. }
  334. return res;
  335. }
  336. static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  337. {
  338. struct i2c_client *client = file->private_data;
  339. unsigned long funcs;
  340. dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
  341. cmd, arg);
  342. switch (cmd) {
  343. case I2C_SLAVE:
  344. case I2C_SLAVE_FORCE:
  345. /* NOTE: devices set up to work with "new style" drivers
  346. * can't use I2C_SLAVE, even when the device node is not
  347. * bound to a driver. Only I2C_SLAVE_FORCE will work.
  348. *
  349. * Setting the PEC flag here won't affect kernel drivers,
  350. * which will be using the i2c_client node registered with
  351. * the driver model core. Likewise, when that client has
  352. * the PEC flag already set, the i2c-dev driver won't see
  353. * (or use) this setting.
  354. */
  355. if ((arg > 0x3ff) ||
  356. (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
  357. return -EINVAL;
  358. if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
  359. return -EBUSY;
  360. /* REVISIT: address could become busy later */
  361. client->addr = arg;
  362. return 0;
  363. case I2C_TENBIT:
  364. if (arg)
  365. client->flags |= I2C_M_TEN;
  366. else
  367. client->flags &= ~I2C_M_TEN;
  368. return 0;
  369. case I2C_PEC:
  370. if (arg)
  371. client->flags |= I2C_CLIENT_PEC;
  372. else
  373. client->flags &= ~I2C_CLIENT_PEC;
  374. return 0;
  375. case I2C_FUNCS:
  376. funcs = i2c_get_functionality(client->adapter);
  377. return put_user(funcs, (unsigned long __user *)arg);
  378. case I2C_RDWR:
  379. return i2cdev_ioctl_rdrw(client, arg);
  380. case I2C_SMBUS:
  381. return i2cdev_ioctl_smbus(client, arg);
  382. case I2C_RETRIES:
  383. client->adapter->retries = arg;
  384. break;
  385. case I2C_TIMEOUT:
  386. /* For historical reasons, user-space sets the timeout
  387. * value in units of 10 ms.
  388. */
  389. client->adapter->timeout = msecs_to_jiffies(arg * 10);
  390. break;
  391. default:
  392. /* NOTE: returning a fault code here could cause trouble
  393. * in buggy userspace code. Some old kernel bugs returned
  394. * zero in this case, and userspace code might accidentally
  395. * have depended on that bug.
  396. */
  397. return -ENOTTY;
  398. }
  399. return 0;
  400. }
  401. static int i2cdev_open(struct inode *inode, struct file *file)
  402. {
  403. unsigned int minor = iminor(inode);
  404. struct i2c_client *client;
  405. struct i2c_adapter *adap;
  406. struct i2c_dev *i2c_dev;
  407. i2c_dev = i2c_dev_get_by_minor(minor);
  408. if (!i2c_dev)
  409. return -ENODEV;
  410. adap = i2c_get_adapter(i2c_dev->adap->nr);
  411. if (!adap)
  412. return -ENODEV;
  413. /* This creates an anonymous i2c_client, which may later be
  414. * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
  415. *
  416. * This client is ** NEVER REGISTERED ** with the driver model
  417. * or I2C core code!! It just holds private copies of addressing
  418. * information and maybe a PEC flag.
  419. */
  420. client = kzalloc(sizeof(*client), GFP_KERNEL);
  421. if (!client) {
  422. i2c_put_adapter(adap);
  423. return -ENOMEM;
  424. }
  425. snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
  426. client->adapter = adap;
  427. file->private_data = client;
  428. return 0;
  429. }
  430. static int i2cdev_release(struct inode *inode, struct file *file)
  431. {
  432. struct i2c_client *client = file->private_data;
  433. i2c_put_adapter(client->adapter);
  434. kfree(client);
  435. file->private_data = NULL;
  436. return 0;
  437. }
  438. static const struct file_operations i2cdev_fops = {
  439. .owner = THIS_MODULE,
  440. .llseek = no_llseek,
  441. .read = i2cdev_read,
  442. .write = i2cdev_write,
  443. .unlocked_ioctl = i2cdev_ioctl,
  444. .open = i2cdev_open,
  445. .release = i2cdev_release,
  446. };
  447. /* ------------------------------------------------------------------------- */
  448. static struct class *i2c_dev_class;
  449. static int i2cdev_attach_adapter(struct device *dev, void *dummy)
  450. {
  451. struct i2c_adapter *adap;
  452. struct i2c_dev *i2c_dev;
  453. int res;
  454. if (dev->type != &i2c_adapter_type)
  455. return 0;
  456. adap = to_i2c_adapter(dev);
  457. i2c_dev = get_free_i2c_dev(adap);
  458. if (IS_ERR(i2c_dev))
  459. return PTR_ERR(i2c_dev);
  460. /* register this i2c device with the driver core */
  461. i2c_dev->dev = device_create(i2c_dev_class, &adap->dev,
  462. MKDEV(I2C_MAJOR, adap->nr), NULL,
  463. "i2c-%d", adap->nr);
  464. if (IS_ERR(i2c_dev->dev)) {
  465. res = PTR_ERR(i2c_dev->dev);
  466. goto error;
  467. }
  468. pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
  469. adap->name, adap->nr);
  470. return 0;
  471. error:
  472. return_i2c_dev(i2c_dev);
  473. return res;
  474. }
  475. static int i2cdev_detach_adapter(struct device *dev, void *dummy)
  476. {
  477. struct i2c_adapter *adap;
  478. struct i2c_dev *i2c_dev;
  479. if (dev->type != &i2c_adapter_type)
  480. return 0;
  481. adap = to_i2c_adapter(dev);
  482. i2c_dev = i2c_dev_get_by_minor(adap->nr);
  483. if (!i2c_dev) /* attach_adapter must have failed */
  484. return 0;
  485. return_i2c_dev(i2c_dev);
  486. device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
  487. pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
  488. return 0;
  489. }
  490. static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
  491. void *data)
  492. {
  493. struct device *dev = data;
  494. switch (action) {
  495. case BUS_NOTIFY_ADD_DEVICE:
  496. return i2cdev_attach_adapter(dev, NULL);
  497. case BUS_NOTIFY_DEL_DEVICE:
  498. return i2cdev_detach_adapter(dev, NULL);
  499. }
  500. return 0;
  501. }
  502. static struct notifier_block i2cdev_notifier = {
  503. .notifier_call = i2cdev_notifier_call,
  504. };
  505. /* ------------------------------------------------------------------------- */
  506. /*
  507. * module load/unload record keeping
  508. */
  509. static int __init i2c_dev_init(void)
  510. {
  511. int res;
  512. printk(KERN_INFO "i2c /dev entries driver\n");
  513. res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);
  514. if (res)
  515. goto out;
  516. i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
  517. if (IS_ERR(i2c_dev_class)) {
  518. res = PTR_ERR(i2c_dev_class);
  519. goto out_unreg_chrdev;
  520. }
  521. i2c_dev_class->dev_groups = i2c_groups;
  522. /* Keep track of adapters which will be added or removed later */
  523. res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
  524. if (res)
  525. goto out_unreg_class;
  526. /* Bind to already existing adapters right away */
  527. i2c_for_each_dev(NULL, i2cdev_attach_adapter);
  528. return 0;
  529. out_unreg_class:
  530. class_destroy(i2c_dev_class);
  531. out_unreg_chrdev:
  532. unregister_chrdev(I2C_MAJOR, "i2c");
  533. out:
  534. printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
  535. return res;
  536. }
  537. static void __exit i2c_dev_exit(void)
  538. {
  539. bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
  540. i2c_for_each_dev(NULL, i2cdev_detach_adapter);
  541. class_destroy(i2c_dev_class);
  542. unregister_chrdev(I2C_MAJOR, "i2c");
  543. }
  544. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  545. "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  546. MODULE_DESCRIPTION("I2C /dev entries driver");
  547. MODULE_LICENSE("GPL");
  548. module_init(i2c_dev_init);
  549. module_exit(i2c_dev_exit);