char_dev.c 13 KB

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