char_dev.c 13 KB

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