char_dev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * linux/fs/char_dev.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/init.h>
  7. #include <linux/fs.h>
  8. #include <linux/kdev_t.h>
  9. #include <linux/slab.h>
  10. #include <linux/string.h>
  11. #include <linux/major.h>
  12. #include <linux/errno.h>
  13. #include <linux/module.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/kobject.h>
  16. #include <linux/kobj_map.h>
  17. #include <linux/cdev.h>
  18. #include <linux/mutex.h>
  19. #include <linux/backing-dev.h>
  20. #include <linux/tty.h>
  21. #include "internal.h"
  22. static struct kobj_map *cdev_map;
  23. static DEFINE_MUTEX(chrdevs_lock);
  24. static struct char_device_struct {
  25. struct char_device_struct *next;
  26. unsigned int major;
  27. unsigned int baseminor;
  28. int minorct;
  29. char name[64];
  30. struct cdev *cdev; /* will die */
  31. } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
  32. /* index in the above */
  33. static inline int major_to_index(unsigned major)
  34. {
  35. return major % CHRDEV_MAJOR_HASH_SIZE;
  36. }
  37. #ifdef CONFIG_PROC_FS
  38. void chrdev_show(struct seq_file *f, off_t offset)
  39. {
  40. struct char_device_struct *cd;
  41. if (offset < CHRDEV_MAJOR_HASH_SIZE) {
  42. mutex_lock(&chrdevs_lock);
  43. for (cd = chrdevs[offset]; cd; cd = cd->next)
  44. seq_printf(f, "%3d %s\n", cd->major, cd->name);
  45. mutex_unlock(&chrdevs_lock);
  46. }
  47. }
  48. #endif /* CONFIG_PROC_FS */
  49. /*
  50. * Register a single major with a specified minor range.
  51. *
  52. * If major == 0 this functions will dynamically allocate a major and return
  53. * its number.
  54. *
  55. * If major > 0 this function will attempt to reserve the passed range of
  56. * minors and will return zero on success.
  57. *
  58. * Returns a -ve errno on failure.
  59. */
  60. static struct char_device_struct *
  61. __register_chrdev_region(unsigned int major, unsigned int baseminor,
  62. int minorct, const char *name)
  63. {
  64. struct char_device_struct *cd, **cp;
  65. int ret = 0;
  66. int i;
  67. cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
  68. if (cd == NULL)
  69. return ERR_PTR(-ENOMEM);
  70. mutex_lock(&chrdevs_lock);
  71. /* temporary */
  72. if (major == 0) {
  73. for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
  74. if (chrdevs[i] == NULL)
  75. break;
  76. }
  77. if (i == 0) {
  78. ret = -EBUSY;
  79. goto out;
  80. }
  81. major = i;
  82. }
  83. cd->major = major;
  84. cd->baseminor = baseminor;
  85. cd->minorct = minorct;
  86. strlcpy(cd->name, name, sizeof(cd->name));
  87. i = major_to_index(major);
  88. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  89. if ((*cp)->major > major ||
  90. ((*cp)->major == major &&
  91. (((*cp)->baseminor >= baseminor) ||
  92. ((*cp)->baseminor + (*cp)->minorct > baseminor))))
  93. break;
  94. /* Check for overlapping minor ranges. */
  95. if (*cp && (*cp)->major == major) {
  96. int old_min = (*cp)->baseminor;
  97. int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
  98. int new_min = baseminor;
  99. int new_max = baseminor + minorct - 1;
  100. /* New driver overlaps from the left. */
  101. if (new_max >= old_min && new_max <= old_max) {
  102. ret = -EBUSY;
  103. goto out;
  104. }
  105. /* New driver overlaps from the right. */
  106. if (new_min <= old_max && new_min >= old_min) {
  107. ret = -EBUSY;
  108. goto out;
  109. }
  110. }
  111. cd->next = *cp;
  112. *cp = cd;
  113. mutex_unlock(&chrdevs_lock);
  114. return cd;
  115. out:
  116. mutex_unlock(&chrdevs_lock);
  117. kfree(cd);
  118. return ERR_PTR(ret);
  119. }
  120. static struct char_device_struct *
  121. __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
  122. {
  123. struct char_device_struct *cd = NULL, **cp;
  124. int i = major_to_index(major);
  125. mutex_lock(&chrdevs_lock);
  126. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  127. if ((*cp)->major == major &&
  128. (*cp)->baseminor == baseminor &&
  129. (*cp)->minorct == minorct)
  130. break;
  131. if (*cp) {
  132. cd = *cp;
  133. *cp = cd->next;
  134. }
  135. mutex_unlock(&chrdevs_lock);
  136. return cd;
  137. }
  138. /**
  139. * register_chrdev_region() - register a range of device numbers
  140. * @from: the first in the desired range of device numbers; must include
  141. * the major number.
  142. * @count: the number of consecutive device numbers required
  143. * @name: the name of the device or driver.
  144. *
  145. * Return value is zero on success, a negative error code on failure.
  146. */
  147. int register_chrdev_region(dev_t from, unsigned count, const char *name)
  148. {
  149. struct char_device_struct *cd;
  150. dev_t to = from + count;
  151. dev_t n, next;
  152. for (n = from; n < to; n = next) {
  153. next = MKDEV(MAJOR(n)+1, 0);
  154. if (next > to)
  155. next = to;
  156. cd = __register_chrdev_region(MAJOR(n), MINOR(n),
  157. next - n, name);
  158. if (IS_ERR(cd))
  159. goto fail;
  160. }
  161. return 0;
  162. fail:
  163. to = n;
  164. for (n = from; n < to; n = next) {
  165. next = MKDEV(MAJOR(n)+1, 0);
  166. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  167. }
  168. return PTR_ERR(cd);
  169. }
  170. /**
  171. * alloc_chrdev_region() - register a range of char device numbers
  172. * @dev: output parameter for first assigned number
  173. * @baseminor: first of the requested range of minor numbers
  174. * @count: the number of minor numbers required
  175. * @name: the name of the associated device or driver
  176. *
  177. * Allocates a range of char device numbers. The major number will be
  178. * chosen dynamically, and returned (along with the first minor number)
  179. * in @dev. Returns zero or a negative error code.
  180. */
  181. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
  182. const char *name)
  183. {
  184. struct char_device_struct *cd;
  185. cd = __register_chrdev_region(0, baseminor, count, name);
  186. if (IS_ERR(cd))
  187. return PTR_ERR(cd);
  188. *dev = MKDEV(cd->major, cd->baseminor);
  189. return 0;
  190. }
  191. /**
  192. * __register_chrdev() - create and register a cdev occupying a range of minors
  193. * @major: major device number or 0 for dynamic allocation
  194. * @baseminor: first of the requested range of minor numbers
  195. * @count: the number of minor numbers required
  196. * @name: name of this range of devices
  197. * @fops: file operations associated with this devices
  198. *
  199. * If @major == 0 this functions will dynamically allocate a major and return
  200. * its number.
  201. *
  202. * If @major > 0 this function will attempt to reserve a device with the given
  203. * major number and will return zero on success.
  204. *
  205. * Returns a -ve errno on failure.
  206. *
  207. * The name of this device has nothing to do with the name of the device in
  208. * /dev. It only helps to keep track of the different owners of devices. If
  209. * your module name has only one type of devices it's ok to use e.g. the name
  210. * of the module here.
  211. */
  212. int __register_chrdev(unsigned int major, unsigned int baseminor,
  213. unsigned int count, const char *name,
  214. const struct file_operations *fops)
  215. {
  216. struct char_device_struct *cd;
  217. struct cdev *cdev;
  218. int err = -ENOMEM;
  219. cd = __register_chrdev_region(major, baseminor, count, name);
  220. if (IS_ERR(cd))
  221. return PTR_ERR(cd);
  222. cdev = cdev_alloc();
  223. if (!cdev)
  224. goto out2;
  225. cdev->owner = fops->owner;
  226. cdev->ops = fops;
  227. kobject_set_name(&cdev->kobj, "%s", name);
  228. err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
  229. if (err)
  230. goto out;
  231. cd->cdev = cdev;
  232. return major ? 0 : cd->major;
  233. out:
  234. kobject_put(&cdev->kobj);
  235. out2:
  236. kfree(__unregister_chrdev_region(cd->major, baseminor, count));
  237. return err;
  238. }
  239. /**
  240. * unregister_chrdev_region() - unregister a range of device numbers
  241. * @from: the first in the range of numbers to unregister
  242. * @count: the number of device numbers to unregister
  243. *
  244. * This function will unregister a range of @count device numbers,
  245. * starting with @from. The caller should normally be the one who
  246. * allocated those numbers in the first place...
  247. */
  248. void unregister_chrdev_region(dev_t from, unsigned count)
  249. {
  250. dev_t to = from + count;
  251. dev_t n, next;
  252. for (n = from; n < to; n = next) {
  253. next = MKDEV(MAJOR(n)+1, 0);
  254. if (next > to)
  255. next = to;
  256. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  257. }
  258. }
  259. /**
  260. * __unregister_chrdev - unregister and destroy a cdev
  261. * @major: major device number
  262. * @baseminor: first of the range of minor numbers
  263. * @count: the number of minor numbers this cdev is occupying
  264. * @name: name of this range of devices
  265. *
  266. * Unregister and destroy the cdev occupying the region described by
  267. * @major, @baseminor and @count. This function undoes what
  268. * __register_chrdev() did.
  269. */
  270. void __unregister_chrdev(unsigned int major, unsigned int baseminor,
  271. unsigned int count, const char *name)
  272. {
  273. struct char_device_struct *cd;
  274. cd = __unregister_chrdev_region(major, baseminor, count);
  275. if (cd && cd->cdev)
  276. cdev_del(cd->cdev);
  277. kfree(cd);
  278. }
  279. static DEFINE_SPINLOCK(cdev_lock);
  280. static struct kobject *cdev_get(struct cdev *p)
  281. {
  282. struct module *owner = p->owner;
  283. struct kobject *kobj;
  284. if (owner && !try_module_get(owner))
  285. return NULL;
  286. kobj = kobject_get(&p->kobj);
  287. if (!kobj)
  288. module_put(owner);
  289. return kobj;
  290. }
  291. void cdev_put(struct cdev *p)
  292. {
  293. if (p) {
  294. struct module *owner = p->owner;
  295. kobject_put(&p->kobj);
  296. module_put(owner);
  297. }
  298. }
  299. /*
  300. * Called every time a character special file is opened
  301. */
  302. static int chrdev_open(struct inode *inode, struct file *filp)
  303. {
  304. const struct file_operations *fops;
  305. struct cdev *p;
  306. struct cdev *new = NULL;
  307. int ret = 0;
  308. spin_lock(&cdev_lock);
  309. p = inode->i_cdev;
  310. if (!p) {
  311. struct kobject *kobj;
  312. int idx;
  313. spin_unlock(&cdev_lock);
  314. kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
  315. if (!kobj)
  316. return -ENXIO;
  317. new = container_of(kobj, struct cdev, kobj);
  318. spin_lock(&cdev_lock);
  319. /* Check i_cdev again in case somebody beat us to it while
  320. we dropped the lock. */
  321. p = inode->i_cdev;
  322. if (!p) {
  323. inode->i_cdev = p = new;
  324. list_add(&inode->i_devices, &p->list);
  325. new = NULL;
  326. } else if (!cdev_get(p))
  327. ret = -ENXIO;
  328. } else if (!cdev_get(p))
  329. ret = -ENXIO;
  330. spin_unlock(&cdev_lock);
  331. cdev_put(new);
  332. if (ret)
  333. return ret;
  334. ret = -ENXIO;
  335. fops = fops_get(p->ops);
  336. if (!fops)
  337. goto out_cdev_put;
  338. replace_fops(filp, fops);
  339. if (filp->f_op->open) {
  340. ret = filp->f_op->open(inode, filp);
  341. if (ret)
  342. goto out_cdev_put;
  343. }
  344. return 0;
  345. out_cdev_put:
  346. cdev_put(p);
  347. return ret;
  348. }
  349. void cd_forget(struct inode *inode)
  350. {
  351. spin_lock(&cdev_lock);
  352. list_del_init(&inode->i_devices);
  353. inode->i_cdev = NULL;
  354. spin_unlock(&cdev_lock);
  355. }
  356. static void cdev_purge(struct cdev *cdev)
  357. {
  358. spin_lock(&cdev_lock);
  359. while (!list_empty(&cdev->list)) {
  360. struct inode *inode;
  361. inode = container_of(cdev->list.next, struct inode, i_devices);
  362. list_del_init(&inode->i_devices);
  363. inode->i_cdev = NULL;
  364. }
  365. spin_unlock(&cdev_lock);
  366. }
  367. /*
  368. * Dummy default file-operations: the only thing this does
  369. * is contain the open that then fills in the correct operations
  370. * depending on the special file...
  371. */
  372. const struct file_operations def_chr_fops = {
  373. .open = chrdev_open,
  374. .llseek = noop_llseek,
  375. };
  376. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  377. {
  378. struct cdev *p = data;
  379. return &p->kobj;
  380. }
  381. static int exact_lock(dev_t dev, void *data)
  382. {
  383. struct cdev *p = data;
  384. return cdev_get(p) ? 0 : -1;
  385. }
  386. /**
  387. * cdev_add() - add a char device to the system
  388. * @p: the cdev structure for the device
  389. * @dev: the first device number for which this device is responsible
  390. * @count: the number of consecutive minor numbers corresponding to this
  391. * device
  392. *
  393. * cdev_add() adds the device represented by @p to the system, making it
  394. * live immediately. A negative error code is returned on failure.
  395. */
  396. int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  397. {
  398. int error;
  399. p->dev = dev;
  400. p->count = count;
  401. error = kobj_map(cdev_map, dev, count, NULL,
  402. exact_match, exact_lock, p);
  403. if (error)
  404. return error;
  405. kobject_get(p->kobj.parent);
  406. return 0;
  407. }
  408. static void cdev_unmap(dev_t dev, unsigned count)
  409. {
  410. kobj_unmap(cdev_map, dev, count);
  411. }
  412. /**
  413. * cdev_del() - remove a cdev from the system
  414. * @p: the cdev structure to be removed
  415. *
  416. * cdev_del() removes @p from the system, possibly freeing the structure
  417. * itself.
  418. */
  419. void cdev_del(struct cdev *p)
  420. {
  421. cdev_unmap(p->dev, p->count);
  422. kobject_put(&p->kobj);
  423. }
  424. static void cdev_default_release(struct kobject *kobj)
  425. {
  426. struct cdev *p = container_of(kobj, struct cdev, kobj);
  427. struct kobject *parent = kobj->parent;
  428. cdev_purge(p);
  429. kobject_put(parent);
  430. }
  431. static void cdev_dynamic_release(struct kobject *kobj)
  432. {
  433. struct cdev *p = container_of(kobj, struct cdev, kobj);
  434. struct kobject *parent = kobj->parent;
  435. cdev_purge(p);
  436. kfree(p);
  437. kobject_put(parent);
  438. }
  439. static struct kobj_type ktype_cdev_default = {
  440. .release = cdev_default_release,
  441. };
  442. static struct kobj_type ktype_cdev_dynamic = {
  443. .release = cdev_dynamic_release,
  444. };
  445. /**
  446. * cdev_alloc() - allocate a cdev structure
  447. *
  448. * Allocates and returns a cdev structure, or NULL on failure.
  449. */
  450. struct cdev *cdev_alloc(void)
  451. {
  452. struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
  453. if (p) {
  454. INIT_LIST_HEAD(&p->list);
  455. kobject_init(&p->kobj, &ktype_cdev_dynamic);
  456. }
  457. return p;
  458. }
  459. /**
  460. * cdev_init() - initialize a cdev structure
  461. * @cdev: the structure to initialize
  462. * @fops: the file_operations for this device
  463. *
  464. * Initializes @cdev, remembering @fops, making it ready to add to the
  465. * system with cdev_add().
  466. */
  467. void cdev_init(struct cdev *cdev, const struct file_operations *fops)
  468. {
  469. memset(cdev, 0, sizeof *cdev);
  470. INIT_LIST_HEAD(&cdev->list);
  471. kobject_init(&cdev->kobj, &ktype_cdev_default);
  472. cdev->ops = fops;
  473. }
  474. static struct kobject *base_probe(dev_t dev, int *part, void *data)
  475. {
  476. if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
  477. /* Make old-style 2.4 aliases work */
  478. request_module("char-major-%d", MAJOR(dev));
  479. return NULL;
  480. }
  481. void __init chrdev_init(void)
  482. {
  483. cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
  484. }
  485. /* Let modules do char dev stuff */
  486. EXPORT_SYMBOL(register_chrdev_region);
  487. EXPORT_SYMBOL(unregister_chrdev_region);
  488. EXPORT_SYMBOL(alloc_chrdev_region);
  489. EXPORT_SYMBOL(cdev_init);
  490. EXPORT_SYMBOL(cdev_alloc);
  491. EXPORT_SYMBOL(cdev_del);
  492. EXPORT_SYMBOL(cdev_add);
  493. EXPORT_SYMBOL(__register_chrdev);
  494. EXPORT_SYMBOL(__unregister_chrdev);