char_dev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * linux/fs/char_dev.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/config.h>
  7. #include <linux/init.h>
  8. #include <linux/fs.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/devfs_fs_kernel.h>
  16. #include <linux/kobject.h>
  17. #include <linux/kobj_map.h>
  18. #include <linux/cdev.h>
  19. #ifdef CONFIG_KMOD
  20. #include <linux/kmod.h>
  21. #endif
  22. static struct kobj_map *cdev_map;
  23. #define MAX_PROBE_HASH 255 /* random */
  24. static DECLARE_MUTEX(chrdevs_lock);
  25. static struct char_device_struct {
  26. struct char_device_struct *next;
  27. unsigned int major;
  28. unsigned int baseminor;
  29. int minorct;
  30. char name[64];
  31. struct file_operations *fops;
  32. struct cdev *cdev; /* will die */
  33. } *chrdevs[MAX_PROBE_HASH];
  34. /* index in the above */
  35. static inline int major_to_index(int major)
  36. {
  37. return major % MAX_PROBE_HASH;
  38. }
  39. struct chrdev_info {
  40. int index;
  41. struct char_device_struct *cd;
  42. };
  43. void *get_next_chrdev(void *dev)
  44. {
  45. struct chrdev_info *info;
  46. if (dev == NULL) {
  47. info = kmalloc(sizeof(*info), GFP_KERNEL);
  48. if (!info)
  49. goto out;
  50. info->index=0;
  51. info->cd = chrdevs[info->index];
  52. if (info->cd)
  53. goto out;
  54. } else {
  55. info = dev;
  56. }
  57. while (info->index < ARRAY_SIZE(chrdevs)) {
  58. if (info->cd)
  59. info->cd = info->cd->next;
  60. if (info->cd)
  61. goto out;
  62. /*
  63. * No devices on this chain, move to the next
  64. */
  65. info->index++;
  66. info->cd = (info->index < ARRAY_SIZE(chrdevs)) ?
  67. chrdevs[info->index] : NULL;
  68. if (info->cd)
  69. goto out;
  70. }
  71. out:
  72. return info;
  73. }
  74. void *acquire_chrdev_list(void)
  75. {
  76. down(&chrdevs_lock);
  77. return get_next_chrdev(NULL);
  78. }
  79. void release_chrdev_list(void *dev)
  80. {
  81. up(&chrdevs_lock);
  82. kfree(dev);
  83. }
  84. int count_chrdev_list(void)
  85. {
  86. struct char_device_struct *cd;
  87. int i, count;
  88. count = 0;
  89. for (i = 0; i < ARRAY_SIZE(chrdevs) ; i++) {
  90. for (cd = chrdevs[i]; cd; cd = cd->next)
  91. count++;
  92. }
  93. return count;
  94. }
  95. int get_chrdev_info(void *dev, int *major, char **name)
  96. {
  97. struct chrdev_info *info = dev;
  98. if (info->cd == NULL)
  99. return 1;
  100. *major = info->cd->major;
  101. *name = info->cd->name;
  102. return 0;
  103. }
  104. /*
  105. * Register a single major with a specified minor range.
  106. *
  107. * If major == 0 this functions will dynamically allocate a major and return
  108. * its number.
  109. *
  110. * If major > 0 this function will attempt to reserve the passed range of
  111. * minors and will return zero on success.
  112. *
  113. * Returns a -ve errno on failure.
  114. */
  115. static struct char_device_struct *
  116. __register_chrdev_region(unsigned int major, unsigned int baseminor,
  117. int minorct, const char *name)
  118. {
  119. struct char_device_struct *cd, **cp;
  120. int ret = 0;
  121. int i;
  122. cd = kmalloc(sizeof(struct char_device_struct), GFP_KERNEL);
  123. if (cd == NULL)
  124. return ERR_PTR(-ENOMEM);
  125. memset(cd, 0, sizeof(struct char_device_struct));
  126. down(&chrdevs_lock);
  127. /* temporary */
  128. if (major == 0) {
  129. for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
  130. if (chrdevs[i] == NULL)
  131. break;
  132. }
  133. if (i == 0) {
  134. ret = -EBUSY;
  135. goto out;
  136. }
  137. major = i;
  138. ret = major;
  139. }
  140. cd->major = major;
  141. cd->baseminor = baseminor;
  142. cd->minorct = minorct;
  143. strncpy(cd->name,name, 64);
  144. i = major_to_index(major);
  145. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  146. if ((*cp)->major > major ||
  147. ((*cp)->major == major && (*cp)->baseminor >= baseminor))
  148. break;
  149. if (*cp && (*cp)->major == major &&
  150. (*cp)->baseminor < baseminor + minorct) {
  151. ret = -EBUSY;
  152. goto out;
  153. }
  154. cd->next = *cp;
  155. *cp = cd;
  156. up(&chrdevs_lock);
  157. return cd;
  158. out:
  159. up(&chrdevs_lock);
  160. kfree(cd);
  161. return ERR_PTR(ret);
  162. }
  163. static struct char_device_struct *
  164. __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
  165. {
  166. struct char_device_struct *cd = NULL, **cp;
  167. int i = major_to_index(major);
  168. down(&chrdevs_lock);
  169. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  170. if ((*cp)->major == major &&
  171. (*cp)->baseminor == baseminor &&
  172. (*cp)->minorct == minorct)
  173. break;
  174. if (*cp) {
  175. cd = *cp;
  176. *cp = cd->next;
  177. }
  178. up(&chrdevs_lock);
  179. return cd;
  180. }
  181. int register_chrdev_region(dev_t from, unsigned count, const char *name)
  182. {
  183. struct char_device_struct *cd;
  184. dev_t to = from + count;
  185. dev_t n, next;
  186. for (n = from; n < to; n = next) {
  187. next = MKDEV(MAJOR(n)+1, 0);
  188. if (next > to)
  189. next = to;
  190. cd = __register_chrdev_region(MAJOR(n), MINOR(n),
  191. next - n, name);
  192. if (IS_ERR(cd))
  193. goto fail;
  194. }
  195. return 0;
  196. fail:
  197. to = n;
  198. for (n = from; n < to; n = next) {
  199. next = MKDEV(MAJOR(n)+1, 0);
  200. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  201. }
  202. return PTR_ERR(cd);
  203. }
  204. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
  205. const char *name)
  206. {
  207. struct char_device_struct *cd;
  208. cd = __register_chrdev_region(0, baseminor, count, name);
  209. if (IS_ERR(cd))
  210. return PTR_ERR(cd);
  211. *dev = MKDEV(cd->major, cd->baseminor);
  212. return 0;
  213. }
  214. int register_chrdev(unsigned int major, const char *name,
  215. struct file_operations *fops)
  216. {
  217. struct char_device_struct *cd;
  218. struct cdev *cdev;
  219. char *s;
  220. int err = -ENOMEM;
  221. cd = __register_chrdev_region(major, 0, 256, name);
  222. if (IS_ERR(cd))
  223. return PTR_ERR(cd);
  224. cdev = cdev_alloc();
  225. if (!cdev)
  226. goto out2;
  227. cdev->owner = fops->owner;
  228. cdev->ops = fops;
  229. kobject_set_name(&cdev->kobj, "%s", name);
  230. for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
  231. *s = '!';
  232. err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
  233. if (err)
  234. goto out;
  235. cd->cdev = cdev;
  236. return major ? 0 : cd->major;
  237. out:
  238. kobject_put(&cdev->kobj);
  239. out2:
  240. kfree(__unregister_chrdev_region(cd->major, 0, 256));
  241. return err;
  242. }
  243. void unregister_chrdev_region(dev_t from, unsigned count)
  244. {
  245. dev_t to = from + count;
  246. dev_t n, next;
  247. for (n = from; n < to; n = next) {
  248. next = MKDEV(MAJOR(n)+1, 0);
  249. if (next > to)
  250. next = to;
  251. kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
  252. }
  253. }
  254. int unregister_chrdev(unsigned int major, const char *name)
  255. {
  256. struct char_device_struct *cd;
  257. cd = __unregister_chrdev_region(major, 0, 256);
  258. if (cd && cd->cdev)
  259. cdev_del(cd->cdev);
  260. kfree(cd);
  261. return 0;
  262. }
  263. static DEFINE_SPINLOCK(cdev_lock);
  264. static struct kobject *cdev_get(struct cdev *p)
  265. {
  266. struct module *owner = p->owner;
  267. struct kobject *kobj;
  268. if (owner && !try_module_get(owner))
  269. return NULL;
  270. kobj = kobject_get(&p->kobj);
  271. if (!kobj)
  272. module_put(owner);
  273. return kobj;
  274. }
  275. void cdev_put(struct cdev *p)
  276. {
  277. if (p) {
  278. struct module *owner = p->owner;
  279. kobject_put(&p->kobj);
  280. module_put(owner);
  281. }
  282. }
  283. /*
  284. * Called every time a character special file is opened
  285. */
  286. int chrdev_open(struct inode * inode, struct file * filp)
  287. {
  288. struct cdev *p;
  289. struct cdev *new = NULL;
  290. int ret = 0;
  291. spin_lock(&cdev_lock);
  292. p = inode->i_cdev;
  293. if (!p) {
  294. struct kobject *kobj;
  295. int idx;
  296. spin_unlock(&cdev_lock);
  297. kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
  298. if (!kobj)
  299. return -ENXIO;
  300. new = container_of(kobj, struct cdev, kobj);
  301. spin_lock(&cdev_lock);
  302. p = inode->i_cdev;
  303. if (!p) {
  304. inode->i_cdev = p = new;
  305. inode->i_cindex = idx;
  306. list_add(&inode->i_devices, &p->list);
  307. new = NULL;
  308. } else if (!cdev_get(p))
  309. ret = -ENXIO;
  310. } else if (!cdev_get(p))
  311. ret = -ENXIO;
  312. spin_unlock(&cdev_lock);
  313. cdev_put(new);
  314. if (ret)
  315. return ret;
  316. filp->f_op = fops_get(p->ops);
  317. if (!filp->f_op) {
  318. cdev_put(p);
  319. return -ENXIO;
  320. }
  321. if (filp->f_op->open) {
  322. lock_kernel();
  323. ret = filp->f_op->open(inode,filp);
  324. unlock_kernel();
  325. }
  326. if (ret)
  327. cdev_put(p);
  328. return ret;
  329. }
  330. void cd_forget(struct inode *inode)
  331. {
  332. spin_lock(&cdev_lock);
  333. list_del_init(&inode->i_devices);
  334. inode->i_cdev = NULL;
  335. spin_unlock(&cdev_lock);
  336. }
  337. static void cdev_purge(struct cdev *cdev)
  338. {
  339. spin_lock(&cdev_lock);
  340. while (!list_empty(&cdev->list)) {
  341. struct inode *inode;
  342. inode = container_of(cdev->list.next, struct inode, i_devices);
  343. list_del_init(&inode->i_devices);
  344. inode->i_cdev = NULL;
  345. }
  346. spin_unlock(&cdev_lock);
  347. }
  348. /*
  349. * Dummy default file-operations: the only thing this does
  350. * is contain the open that then fills in the correct operations
  351. * depending on the special file...
  352. */
  353. struct file_operations def_chr_fops = {
  354. .open = chrdev_open,
  355. };
  356. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  357. {
  358. struct cdev *p = data;
  359. return &p->kobj;
  360. }
  361. static int exact_lock(dev_t dev, void *data)
  362. {
  363. struct cdev *p = data;
  364. return cdev_get(p) ? 0 : -1;
  365. }
  366. int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  367. {
  368. p->dev = dev;
  369. p->count = count;
  370. return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
  371. }
  372. static void cdev_unmap(dev_t dev, unsigned count)
  373. {
  374. kobj_unmap(cdev_map, dev, count);
  375. }
  376. void cdev_del(struct cdev *p)
  377. {
  378. cdev_unmap(p->dev, p->count);
  379. kobject_put(&p->kobj);
  380. }
  381. static void cdev_default_release(struct kobject *kobj)
  382. {
  383. struct cdev *p = container_of(kobj, struct cdev, kobj);
  384. cdev_purge(p);
  385. }
  386. static void cdev_dynamic_release(struct kobject *kobj)
  387. {
  388. struct cdev *p = container_of(kobj, struct cdev, kobj);
  389. cdev_purge(p);
  390. kfree(p);
  391. }
  392. static struct kobj_type ktype_cdev_default = {
  393. .release = cdev_default_release,
  394. };
  395. static struct kobj_type ktype_cdev_dynamic = {
  396. .release = cdev_dynamic_release,
  397. };
  398. struct cdev *cdev_alloc(void)
  399. {
  400. struct cdev *p = kmalloc(sizeof(struct cdev), GFP_KERNEL);
  401. if (p) {
  402. memset(p, 0, sizeof(struct cdev));
  403. p->kobj.ktype = &ktype_cdev_dynamic;
  404. INIT_LIST_HEAD(&p->list);
  405. kobject_init(&p->kobj);
  406. }
  407. return p;
  408. }
  409. void cdev_init(struct cdev *cdev, struct file_operations *fops)
  410. {
  411. memset(cdev, 0, sizeof *cdev);
  412. INIT_LIST_HEAD(&cdev->list);
  413. cdev->kobj.ktype = &ktype_cdev_default;
  414. kobject_init(&cdev->kobj);
  415. cdev->ops = fops;
  416. }
  417. static struct kobject *base_probe(dev_t dev, int *part, void *data)
  418. {
  419. if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
  420. /* Make old-style 2.4 aliases work */
  421. request_module("char-major-%d", MAJOR(dev));
  422. return NULL;
  423. }
  424. void __init chrdev_init(void)
  425. {
  426. cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
  427. }
  428. /* Let modules do char dev stuff */
  429. EXPORT_SYMBOL(register_chrdev_region);
  430. EXPORT_SYMBOL(unregister_chrdev_region);
  431. EXPORT_SYMBOL(alloc_chrdev_region);
  432. EXPORT_SYMBOL(cdev_init);
  433. EXPORT_SYMBOL(cdev_alloc);
  434. EXPORT_SYMBOL(cdev_del);
  435. EXPORT_SYMBOL(cdev_add);
  436. EXPORT_SYMBOL(register_chrdev);
  437. EXPORT_SYMBOL(unregister_chrdev);