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. 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 show_adapter_name(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(name, S_IRUGO, show_adapter_name, NULL);
  97. /* ------------------------------------------------------------------------- */
  98. /*
  99. * After opening an instance of this character special file, a file
  100. * descriptor starts out associated only with an i2c_adapter (and bus).
  101. *
  102. * Using the I2C_RDWR ioctl(), you can then *immediately* issue i2c_msg
  103. * traffic to any devices on the bus used by that adapter. That's because
  104. * the i2c_msg vectors embed all the addressing information they need, and
  105. * are submitted directly to an i2c_adapter. However, SMBus-only adapters
  106. * don't support that interface.
  107. *
  108. * To use read()/write() system calls on that file descriptor, or to use
  109. * SMBus interfaces (and work with SMBus-only hosts!), you must first issue
  110. * an I2C_SLAVE (or I2C_SLAVE_FORCE) ioctl. That configures an anonymous
  111. * (never registered) i2c_client so it holds the addressing information
  112. * needed by those system calls and by this SMBus interface.
  113. */
  114. static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
  115. loff_t *offset)
  116. {
  117. char *tmp;
  118. int ret;
  119. struct i2c_client *client = file->private_data;
  120. if (count > 8192)
  121. count = 8192;
  122. tmp = kmalloc(count, GFP_KERNEL);
  123. if (tmp == NULL)
  124. return -ENOMEM;
  125. pr_debug("i2c-dev: i2c-%d reading %zu bytes.\n",
  126. iminor(file_inode(file)), count);
  127. ret = i2c_master_recv(client, tmp, count);
  128. if (ret >= 0)
  129. ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
  130. kfree(tmp);
  131. return ret;
  132. }
  133. static ssize_t i2cdev_write(struct file *file, const char __user *buf,
  134. size_t count, loff_t *offset)
  135. {
  136. int ret;
  137. char *tmp;
  138. struct i2c_client *client = file->private_data;
  139. if (count > 8192)
  140. count = 8192;
  141. tmp = memdup_user(buf, count);
  142. if (IS_ERR(tmp))
  143. return PTR_ERR(tmp);
  144. pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",
  145. iminor(file_inode(file)), count);
  146. ret = i2c_master_send(client, tmp, count);
  147. kfree(tmp);
  148. return ret;
  149. }
  150. static int i2cdev_check(struct device *dev, void *addrp)
  151. {
  152. struct i2c_client *client = i2c_verify_client(dev);
  153. if (!client || client->addr != *(unsigned int *)addrp)
  154. return 0;
  155. return dev->driver ? -EBUSY : 0;
  156. }
  157. /* walk up mux tree */
  158. static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
  159. {
  160. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  161. int result;
  162. result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
  163. if (!result && parent)
  164. result = i2cdev_check_mux_parents(parent, addr);
  165. return result;
  166. }
  167. /* recurse down mux tree */
  168. static int i2cdev_check_mux_children(struct device *dev, void *addrp)
  169. {
  170. int result;
  171. if (dev->type == &i2c_adapter_type)
  172. result = device_for_each_child(dev, addrp,
  173. i2cdev_check_mux_children);
  174. else
  175. result = i2cdev_check(dev, addrp);
  176. return result;
  177. }
  178. /* This address checking function differs from the one in i2c-core
  179. in that it considers an address with a registered device, but no
  180. driver bound to it, as NOT busy. */
  181. static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  182. {
  183. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  184. int result = 0;
  185. if (parent)
  186. result = i2cdev_check_mux_parents(parent, addr);
  187. if (!result)
  188. result = device_for_each_child(&adapter->dev, &addr,
  189. i2cdev_check_mux_children);
  190. return result;
  191. }
  192. static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client,
  193. unsigned long arg)
  194. {
  195. struct i2c_rdwr_ioctl_data rdwr_arg;
  196. struct i2c_msg *rdwr_pa;
  197. u8 __user **data_ptrs;
  198. int i, res;
  199. if (copy_from_user(&rdwr_arg,
  200. (struct i2c_rdwr_ioctl_data __user *)arg,
  201. sizeof(rdwr_arg)))
  202. return -EFAULT;
  203. /* Put an arbitrary limit on the number of messages that can
  204. * be sent at once */
  205. if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
  206. return -EINVAL;
  207. rdwr_pa = memdup_user(rdwr_arg.msgs,
  208. rdwr_arg.nmsgs * sizeof(struct i2c_msg));
  209. if (IS_ERR(rdwr_pa))
  210. return PTR_ERR(rdwr_pa);
  211. data_ptrs = kmalloc(rdwr_arg.nmsgs * sizeof(u8 __user *), GFP_KERNEL);
  212. if (data_ptrs == NULL) {
  213. kfree(rdwr_pa);
  214. return -ENOMEM;
  215. }
  216. res = 0;
  217. for (i = 0; i < rdwr_arg.nmsgs; i++) {
  218. /* Limit the size of the message to a sane amount */
  219. if (rdwr_pa[i].len > 8192) {
  220. res = -EINVAL;
  221. break;
  222. }
  223. data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
  224. rdwr_pa[i].buf = memdup_user(data_ptrs[i], rdwr_pa[i].len);
  225. if (IS_ERR(rdwr_pa[i].buf)) {
  226. res = PTR_ERR(rdwr_pa[i].buf);
  227. break;
  228. }
  229. /*
  230. * If the message length is received from the slave (similar
  231. * to SMBus block read), we must ensure that the buffer will
  232. * be large enough to cope with a message length of
  233. * I2C_SMBUS_BLOCK_MAX as this is the maximum underlying bus
  234. * drivers allow. The first byte in the buffer must be
  235. * pre-filled with the number of extra bytes, which must be
  236. * at least one to hold the message length, but can be
  237. * greater (for example to account for a checksum byte at
  238. * the end of the message.)
  239. */
  240. if (rdwr_pa[i].flags & I2C_M_RECV_LEN) {
  241. if (!(rdwr_pa[i].flags & I2C_M_RD) ||
  242. rdwr_pa[i].buf[0] < 1 ||
  243. rdwr_pa[i].len < rdwr_pa[i].buf[0] +
  244. I2C_SMBUS_BLOCK_MAX) {
  245. res = -EINVAL;
  246. break;
  247. }
  248. rdwr_pa[i].len = rdwr_pa[i].buf[0];
  249. }
  250. }
  251. if (res < 0) {
  252. int j;
  253. for (j = 0; j < i; ++j)
  254. kfree(rdwr_pa[j].buf);
  255. kfree(data_ptrs);
  256. kfree(rdwr_pa);
  257. return res;
  258. }
  259. res = i2c_transfer(client->adapter, rdwr_pa, rdwr_arg.nmsgs);
  260. while (i-- > 0) {
  261. if (res >= 0 && (rdwr_pa[i].flags & I2C_M_RD)) {
  262. if (copy_to_user(data_ptrs[i], rdwr_pa[i].buf,
  263. rdwr_pa[i].len))
  264. res = -EFAULT;
  265. }
  266. kfree(rdwr_pa[i].buf);
  267. }
  268. kfree(data_ptrs);
  269. kfree(rdwr_pa);
  270. return res;
  271. }
  272. static noinline int i2cdev_ioctl_smbus(struct i2c_client *client,
  273. unsigned long arg)
  274. {
  275. struct i2c_smbus_ioctl_data data_arg;
  276. union i2c_smbus_data temp;
  277. int datasize, res;
  278. if (copy_from_user(&data_arg,
  279. (struct i2c_smbus_ioctl_data __user *) arg,
  280. sizeof(struct i2c_smbus_ioctl_data)))
  281. return -EFAULT;
  282. if ((data_arg.size != I2C_SMBUS_BYTE) &&
  283. (data_arg.size != I2C_SMBUS_QUICK) &&
  284. (data_arg.size != I2C_SMBUS_BYTE_DATA) &&
  285. (data_arg.size != I2C_SMBUS_WORD_DATA) &&
  286. (data_arg.size != I2C_SMBUS_PROC_CALL) &&
  287. (data_arg.size != I2C_SMBUS_BLOCK_DATA) &&
  288. (data_arg.size != I2C_SMBUS_I2C_BLOCK_BROKEN) &&
  289. (data_arg.size != I2C_SMBUS_I2C_BLOCK_DATA) &&
  290. (data_arg.size != I2C_SMBUS_BLOCK_PROC_CALL)) {
  291. dev_dbg(&client->adapter->dev,
  292. "size out of range (%x) in ioctl I2C_SMBUS.\n",
  293. data_arg.size);
  294. return -EINVAL;
  295. }
  296. /* Note that I2C_SMBUS_READ and I2C_SMBUS_WRITE are 0 and 1,
  297. so the check is valid if size==I2C_SMBUS_QUICK too. */
  298. if ((data_arg.read_write != I2C_SMBUS_READ) &&
  299. (data_arg.read_write != I2C_SMBUS_WRITE)) {
  300. dev_dbg(&client->adapter->dev,
  301. "read_write out of range (%x) in ioctl I2C_SMBUS.\n",
  302. data_arg.read_write);
  303. return -EINVAL;
  304. }
  305. /* Note that command values are always valid! */
  306. if ((data_arg.size == I2C_SMBUS_QUICK) ||
  307. ((data_arg.size == I2C_SMBUS_BYTE) &&
  308. (data_arg.read_write == I2C_SMBUS_WRITE)))
  309. /* These are special: we do not use data */
  310. return i2c_smbus_xfer(client->adapter, client->addr,
  311. client->flags, data_arg.read_write,
  312. data_arg.command, data_arg.size, NULL);
  313. if (data_arg.data == NULL) {
  314. dev_dbg(&client->adapter->dev,
  315. "data is NULL pointer in ioctl I2C_SMBUS.\n");
  316. return -EINVAL;
  317. }
  318. if ((data_arg.size == I2C_SMBUS_BYTE_DATA) ||
  319. (data_arg.size == I2C_SMBUS_BYTE))
  320. datasize = sizeof(data_arg.data->byte);
  321. else if ((data_arg.size == I2C_SMBUS_WORD_DATA) ||
  322. (data_arg.size == I2C_SMBUS_PROC_CALL))
  323. datasize = sizeof(data_arg.data->word);
  324. else /* size == smbus block, i2c block, or block proc. call */
  325. datasize = sizeof(data_arg.data->block);
  326. if ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
  327. (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  328. (data_arg.size == I2C_SMBUS_I2C_BLOCK_DATA) ||
  329. (data_arg.read_write == I2C_SMBUS_WRITE)) {
  330. if (copy_from_user(&temp, data_arg.data, datasize))
  331. return -EFAULT;
  332. }
  333. if (data_arg.size == I2C_SMBUS_I2C_BLOCK_BROKEN) {
  334. /* Convert old I2C block commands to the new
  335. convention. This preserves binary compatibility. */
  336. data_arg.size = I2C_SMBUS_I2C_BLOCK_DATA;
  337. if (data_arg.read_write == I2C_SMBUS_READ)
  338. temp.block[0] = I2C_SMBUS_BLOCK_MAX;
  339. }
  340. res = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  341. data_arg.read_write, data_arg.command, data_arg.size, &temp);
  342. if (!res && ((data_arg.size == I2C_SMBUS_PROC_CALL) ||
  343. (data_arg.size == I2C_SMBUS_BLOCK_PROC_CALL) ||
  344. (data_arg.read_write == I2C_SMBUS_READ))) {
  345. if (copy_to_user(data_arg.data, &temp, datasize))
  346. return -EFAULT;
  347. }
  348. return res;
  349. }
  350. static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  351. {
  352. struct i2c_client *client = file->private_data;
  353. unsigned long funcs;
  354. dev_dbg(&client->adapter->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n",
  355. cmd, arg);
  356. switch (cmd) {
  357. case I2C_SLAVE:
  358. case I2C_SLAVE_FORCE:
  359. /* NOTE: devices set up to work with "new style" drivers
  360. * can't use I2C_SLAVE, even when the device node is not
  361. * bound to a driver. Only I2C_SLAVE_FORCE will work.
  362. *
  363. * Setting the PEC flag here won't affect kernel drivers,
  364. * which will be using the i2c_client node registered with
  365. * the driver model core. Likewise, when that client has
  366. * the PEC flag already set, the i2c-dev driver won't see
  367. * (or use) this setting.
  368. */
  369. if ((arg > 0x3ff) ||
  370. (((client->flags & I2C_M_TEN) == 0) && arg > 0x7f))
  371. return -EINVAL;
  372. if (cmd == I2C_SLAVE && i2cdev_check_addr(client->adapter, arg))
  373. return -EBUSY;
  374. /* REVISIT: address could become busy later */
  375. client->addr = arg;
  376. return 0;
  377. case I2C_TENBIT:
  378. if (arg)
  379. client->flags |= I2C_M_TEN;
  380. else
  381. client->flags &= ~I2C_M_TEN;
  382. return 0;
  383. case I2C_PEC:
  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_rdrw(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. struct i2c_dev *i2c_dev;
  421. i2c_dev = i2c_dev_get_by_minor(minor);
  422. if (!i2c_dev)
  423. return -ENODEV;
  424. adap = i2c_get_adapter(i2c_dev->adap->nr);
  425. if (!adap)
  426. return -ENODEV;
  427. /* This creates an anonymous i2c_client, which may later be
  428. * pointed to some address using I2C_SLAVE or I2C_SLAVE_FORCE.
  429. *
  430. * This client is ** NEVER REGISTERED ** with the driver model
  431. * or I2C core code!! It just holds private copies of addressing
  432. * information and maybe a PEC flag.
  433. */
  434. client = kzalloc(sizeof(*client), GFP_KERNEL);
  435. if (!client) {
  436. i2c_put_adapter(adap);
  437. return -ENOMEM;
  438. }
  439. snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr);
  440. client->adapter = adap;
  441. file->private_data = client;
  442. return 0;
  443. }
  444. static int i2cdev_release(struct inode *inode, struct file *file)
  445. {
  446. struct i2c_client *client = file->private_data;
  447. i2c_put_adapter(client->adapter);
  448. kfree(client);
  449. file->private_data = NULL;
  450. return 0;
  451. }
  452. static const struct file_operations i2cdev_fops = {
  453. .owner = THIS_MODULE,
  454. .llseek = no_llseek,
  455. .read = i2cdev_read,
  456. .write = i2cdev_write,
  457. .unlocked_ioctl = i2cdev_ioctl,
  458. .open = i2cdev_open,
  459. .release = i2cdev_release,
  460. };
  461. /* ------------------------------------------------------------------------- */
  462. static struct class *i2c_dev_class;
  463. static int i2cdev_attach_adapter(struct device *dev, void *dummy)
  464. {
  465. struct i2c_adapter *adap;
  466. struct i2c_dev *i2c_dev;
  467. int res;
  468. if (dev->type != &i2c_adapter_type)
  469. return 0;
  470. adap = to_i2c_adapter(dev);
  471. i2c_dev = get_free_i2c_dev(adap);
  472. if (IS_ERR(i2c_dev))
  473. return PTR_ERR(i2c_dev);
  474. /* register this i2c device with the driver core */
  475. i2c_dev->dev = device_create(i2c_dev_class, &adap->dev,
  476. MKDEV(I2C_MAJOR, adap->nr), NULL,
  477. "i2c-%d", adap->nr);
  478. if (IS_ERR(i2c_dev->dev)) {
  479. res = PTR_ERR(i2c_dev->dev);
  480. goto error;
  481. }
  482. res = device_create_file(i2c_dev->dev, &dev_attr_name);
  483. if (res)
  484. goto error_destroy;
  485. pr_debug("i2c-dev: adapter [%s] registered as minor %d\n",
  486. adap->name, adap->nr);
  487. return 0;
  488. error_destroy:
  489. device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
  490. error:
  491. return_i2c_dev(i2c_dev);
  492. return res;
  493. }
  494. static int i2cdev_detach_adapter(struct device *dev, void *dummy)
  495. {
  496. struct i2c_adapter *adap;
  497. struct i2c_dev *i2c_dev;
  498. if (dev->type != &i2c_adapter_type)
  499. return 0;
  500. adap = to_i2c_adapter(dev);
  501. i2c_dev = i2c_dev_get_by_minor(adap->nr);
  502. if (!i2c_dev) /* attach_adapter must have failed */
  503. return 0;
  504. device_remove_file(i2c_dev->dev, &dev_attr_name);
  505. return_i2c_dev(i2c_dev);
  506. device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr));
  507. pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name);
  508. return 0;
  509. }
  510. static int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action,
  511. void *data)
  512. {
  513. struct device *dev = data;
  514. switch (action) {
  515. case BUS_NOTIFY_ADD_DEVICE:
  516. return i2cdev_attach_adapter(dev, NULL);
  517. case BUS_NOTIFY_DEL_DEVICE:
  518. return i2cdev_detach_adapter(dev, NULL);
  519. }
  520. return 0;
  521. }
  522. static struct notifier_block i2cdev_notifier = {
  523. .notifier_call = i2cdev_notifier_call,
  524. };
  525. /* ------------------------------------------------------------------------- */
  526. /*
  527. * module load/unload record keeping
  528. */
  529. static int __init i2c_dev_init(void)
  530. {
  531. int res;
  532. printk(KERN_INFO "i2c /dev entries driver\n");
  533. res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops);
  534. if (res)
  535. goto out;
  536. i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
  537. if (IS_ERR(i2c_dev_class)) {
  538. res = PTR_ERR(i2c_dev_class);
  539. goto out_unreg_chrdev;
  540. }
  541. /* Keep track of adapters which will be added or removed later */
  542. res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
  543. if (res)
  544. goto out_unreg_class;
  545. /* Bind to already existing adapters right away */
  546. i2c_for_each_dev(NULL, i2cdev_attach_adapter);
  547. return 0;
  548. out_unreg_class:
  549. class_destroy(i2c_dev_class);
  550. out_unreg_chrdev:
  551. unregister_chrdev(I2C_MAJOR, "i2c");
  552. out:
  553. printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
  554. return res;
  555. }
  556. static void __exit i2c_dev_exit(void)
  557. {
  558. bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier);
  559. i2c_for_each_dev(NULL, i2cdev_detach_adapter);
  560. class_destroy(i2c_dev_class);
  561. unregister_chrdev(I2C_MAJOR, "i2c");
  562. }
  563. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  564. "Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  565. MODULE_DESCRIPTION("I2C /dev entries driver");
  566. MODULE_LICENSE("GPL");
  567. module_init(i2c_dev_init);
  568. module_exit(i2c_dev_exit);