char_dev.c 10 KB

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