char_dev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. if (new_min < old_min && new_max > old_max) {
  114. ret = -EBUSY;
  115. goto out;
  116. }
  117. }
  118. cd->next = *cp;
  119. *cp = cd;
  120. mutex_unlock(&chrdevs_lock);
  121. return cd;
  122. out:
  123. mutex_unlock(&chrdevs_lock);
  124. kfree(cd);
  125. return ERR_PTR(ret);
  126. }
  127. static struct char_device_struct *
  128. __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
  129. {
  130. struct char_device_struct *cd = NULL, **cp;
  131. int i = major_to_index(major);
  132. mutex_lock(&chrdevs_lock);
  133. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  134. if ((*cp)->major == major &&
  135. (*cp)->baseminor == baseminor &&
  136. (*cp)->minorct == minorct)
  137. break;
  138. if (*cp) {
  139. cd = *cp;
  140. *cp = cd->next;
  141. }
  142. mutex_unlock(&chrdevs_lock);
  143. return cd;
  144. }
  145. /**
  146. * register_chrdev_region() - register a range of device numbers
  147. * @from: the first in the desired range of device numbers; must include
  148. * the major number.
  149. * @count: the number of consecutive device numbers required
  150. * @name: the name of the device or driver.
  151. *
  152. * Return value is zero on success, a negative error code on failure.
  153. */
  154. int register_chrdev_region(dev_t from, unsigned count, const char *name)
  155. {
  156. struct char_device_struct *cd;
  157. dev_t to = from + count;
  158. dev_t n, next;
  159. for (n = from; n < to; n = next) {
  160. next = MKDEV(MAJOR(n)+1, 0);
  161. if (next > to)
  162. next = to;
  163. cd = __register_chrdev_region(MAJOR(n), MINOR(n),
  164. next - n, name);
  165. if (IS_ERR(cd))
  166. goto fail;
  167. }
  168. return 0;
  169. fail:
  170. to = n;
  171. for (n = from; n < to; n = next) {
  172. next = MKDEV(MAJOR(n)+1, 0);
  173. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  174. }
  175. return PTR_ERR(cd);
  176. }
  177. /**
  178. * alloc_chrdev_region() - register a range of char device numbers
  179. * @dev: output parameter for first assigned number
  180. * @baseminor: first of the requested range of minor numbers
  181. * @count: the number of minor numbers required
  182. * @name: the name of the associated device or driver
  183. *
  184. * Allocates a range of char device numbers. The major number will be
  185. * chosen dynamically, and returned (along with the first minor number)
  186. * in @dev. Returns zero or a negative error code.
  187. */
  188. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
  189. const char *name)
  190. {
  191. struct char_device_struct *cd;
  192. cd = __register_chrdev_region(0, baseminor, count, name);
  193. if (IS_ERR(cd))
  194. return PTR_ERR(cd);
  195. *dev = MKDEV(cd->major, cd->baseminor);
  196. return 0;
  197. }
  198. /**
  199. * __register_chrdev() - create and register a cdev occupying a range of minors
  200. * @major: major device number or 0 for dynamic allocation
  201. * @baseminor: first of the requested range of minor numbers
  202. * @count: the number of minor numbers required
  203. * @name: name of this range of devices
  204. * @fops: file operations associated with this devices
  205. *
  206. * If @major == 0 this functions will dynamically allocate a major and return
  207. * its number.
  208. *
  209. * If @major > 0 this function will attempt to reserve a device with the given
  210. * major number and will return zero on success.
  211. *
  212. * Returns a -ve errno on failure.
  213. *
  214. * The name of this device has nothing to do with the name of the device in
  215. * /dev. It only helps to keep track of the different owners of devices. If
  216. * your module name has only one type of devices it's ok to use e.g. the name
  217. * of the module here.
  218. */
  219. int __register_chrdev(unsigned int major, unsigned int baseminor,
  220. unsigned int count, const char *name,
  221. const struct file_operations *fops)
  222. {
  223. struct char_device_struct *cd;
  224. struct cdev *cdev;
  225. int err = -ENOMEM;
  226. cd = __register_chrdev_region(major, baseminor, count, name);
  227. if (IS_ERR(cd))
  228. return PTR_ERR(cd);
  229. cdev = cdev_alloc();
  230. if (!cdev)
  231. goto out2;
  232. cdev->owner = fops->owner;
  233. cdev->ops = fops;
  234. kobject_set_name(&cdev->kobj, "%s", name);
  235. err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
  236. if (err)
  237. goto out;
  238. cd->cdev = cdev;
  239. return major ? 0 : cd->major;
  240. out:
  241. kobject_put(&cdev->kobj);
  242. out2:
  243. kfree(__unregister_chrdev_region(cd->major, baseminor, count));
  244. return err;
  245. }
  246. /**
  247. * unregister_chrdev_region() - unregister a range of device numbers
  248. * @from: the first in the range of numbers to unregister
  249. * @count: the number of device numbers to unregister
  250. *
  251. * This function will unregister a range of @count device numbers,
  252. * starting with @from. The caller should normally be the one who
  253. * allocated those numbers in the first place...
  254. */
  255. void unregister_chrdev_region(dev_t from, unsigned count)
  256. {
  257. dev_t to = from + count;
  258. dev_t n, next;
  259. for (n = from; n < to; n = next) {
  260. next = MKDEV(MAJOR(n)+1, 0);
  261. if (next > to)
  262. next = to;
  263. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  264. }
  265. }
  266. /**
  267. * __unregister_chrdev - unregister and destroy a cdev
  268. * @major: major device number
  269. * @baseminor: first of the range of minor numbers
  270. * @count: the number of minor numbers this cdev is occupying
  271. * @name: name of this range of devices
  272. *
  273. * Unregister and destroy the cdev occupying the region described by
  274. * @major, @baseminor and @count. This function undoes what
  275. * __register_chrdev() did.
  276. */
  277. void __unregister_chrdev(unsigned int major, unsigned int baseminor,
  278. unsigned int count, const char *name)
  279. {
  280. struct char_device_struct *cd;
  281. cd = __unregister_chrdev_region(major, baseminor, count);
  282. if (cd && cd->cdev)
  283. cdev_del(cd->cdev);
  284. kfree(cd);
  285. }
  286. static DEFINE_SPINLOCK(cdev_lock);
  287. static struct kobject *cdev_get(struct cdev *p)
  288. {
  289. struct module *owner = p->owner;
  290. struct kobject *kobj;
  291. if (owner && !try_module_get(owner))
  292. return NULL;
  293. kobj = kobject_get(&p->kobj);
  294. if (!kobj)
  295. module_put(owner);
  296. return kobj;
  297. }
  298. void cdev_put(struct cdev *p)
  299. {
  300. if (p) {
  301. struct module *owner = p->owner;
  302. kobject_put(&p->kobj);
  303. module_put(owner);
  304. }
  305. }
  306. /*
  307. * Called every time a character special file is opened
  308. */
  309. static int chrdev_open(struct inode *inode, struct file *filp)
  310. {
  311. const struct file_operations *fops;
  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. fops = fops_get(p->ops);
  343. if (!fops)
  344. goto out_cdev_put;
  345. replace_fops(filp, fops);
  346. if (filp->f_op->open) {
  347. ret = filp->f_op->open(inode, filp);
  348. if (ret)
  349. goto out_cdev_put;
  350. }
  351. return 0;
  352. out_cdev_put:
  353. cdev_put(p);
  354. return ret;
  355. }
  356. void cd_forget(struct inode *inode)
  357. {
  358. spin_lock(&cdev_lock);
  359. list_del_init(&inode->i_devices);
  360. inode->i_cdev = NULL;
  361. inode->i_mapping = &inode->i_data;
  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. .llseek = noop_llseek,
  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. int error;
  407. p->dev = dev;
  408. p->count = count;
  409. error = kobj_map(cdev_map, dev, count, NULL,
  410. exact_match, exact_lock, p);
  411. if (error)
  412. return error;
  413. kobject_get(p->kobj.parent);
  414. return 0;
  415. }
  416. static void cdev_unmap(dev_t dev, unsigned count)
  417. {
  418. kobj_unmap(cdev_map, dev, count);
  419. }
  420. /**
  421. * cdev_del() - remove a cdev from the system
  422. * @p: the cdev structure to be removed
  423. *
  424. * cdev_del() removes @p from the system, possibly freeing the structure
  425. * itself.
  426. */
  427. void cdev_del(struct cdev *p)
  428. {
  429. cdev_unmap(p->dev, p->count);
  430. kobject_put(&p->kobj);
  431. }
  432. static void cdev_default_release(struct kobject *kobj)
  433. {
  434. struct cdev *p = container_of(kobj, struct cdev, kobj);
  435. struct kobject *parent = kobj->parent;
  436. cdev_purge(p);
  437. kobject_put(parent);
  438. }
  439. static void cdev_dynamic_release(struct kobject *kobj)
  440. {
  441. struct cdev *p = container_of(kobj, struct cdev, kobj);
  442. struct kobject *parent = kobj->parent;
  443. cdev_purge(p);
  444. kfree(p);
  445. kobject_put(parent);
  446. }
  447. static struct kobj_type ktype_cdev_default = {
  448. .release = cdev_default_release,
  449. };
  450. static struct kobj_type ktype_cdev_dynamic = {
  451. .release = cdev_dynamic_release,
  452. };
  453. /**
  454. * cdev_alloc() - allocate a cdev structure
  455. *
  456. * Allocates and returns a cdev structure, or NULL on failure.
  457. */
  458. struct cdev *cdev_alloc(void)
  459. {
  460. struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
  461. if (p) {
  462. INIT_LIST_HEAD(&p->list);
  463. kobject_init(&p->kobj, &ktype_cdev_dynamic);
  464. }
  465. return p;
  466. }
  467. /**
  468. * cdev_init() - initialize a cdev structure
  469. * @cdev: the structure to initialize
  470. * @fops: the file_operations for this device
  471. *
  472. * Initializes @cdev, remembering @fops, making it ready to add to the
  473. * system with cdev_add().
  474. */
  475. void cdev_init(struct cdev *cdev, const struct file_operations *fops)
  476. {
  477. memset(cdev, 0, sizeof *cdev);
  478. INIT_LIST_HEAD(&cdev->list);
  479. kobject_init(&cdev->kobj, &ktype_cdev_default);
  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);