char_dev.c 13 KB

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