char_dev.c 13 KB

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