char_dev.c 13 KB

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