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