i2c-dev.c 18 KB

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