char_dev.c 13 KB

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