i2c-dev.c 18 KB

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