i2c-dev.c 18 KB

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