core.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright 2008 by Karsten Keil <kkeil@novell.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/stddef.h>
  17. #include <linux/module.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/mISDNif.h>
  20. #include "core.h"
  21. static u_int debug;
  22. MODULE_AUTHOR("Karsten Keil");
  23. MODULE_LICENSE("GPL");
  24. module_param(debug, uint, S_IRUGO | S_IWUSR);
  25. static u64 device_ids;
  26. #define MAX_DEVICE_ID 63
  27. static LIST_HEAD(Bprotocols);
  28. static DEFINE_RWLOCK(bp_lock);
  29. static void mISDN_dev_release(struct device *dev)
  30. {
  31. /* nothing to do: the device is part of its parent's data structure */
  32. }
  33. static ssize_t _show_id(struct device *dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  37. if (!mdev)
  38. return -ENODEV;
  39. return sprintf(buf, "%d\n", mdev->id);
  40. }
  41. static ssize_t _show_nrbchan(struct device *dev,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  45. if (!mdev)
  46. return -ENODEV;
  47. return sprintf(buf, "%d\n", mdev->nrbchan);
  48. }
  49. static ssize_t _show_d_protocols(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  53. if (!mdev)
  54. return -ENODEV;
  55. return sprintf(buf, "%d\n", mdev->Dprotocols);
  56. }
  57. static ssize_t _show_b_protocols(struct device *dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  61. if (!mdev)
  62. return -ENODEV;
  63. return sprintf(buf, "%d\n", mdev->Bprotocols | get_all_Bprotocols());
  64. }
  65. static ssize_t _show_protocol(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  69. if (!mdev)
  70. return -ENODEV;
  71. return sprintf(buf, "%d\n", mdev->D.protocol);
  72. }
  73. static ssize_t _show_name(struct device *dev,
  74. struct device_attribute *attr, char *buf)
  75. {
  76. strcpy(buf, dev_name(dev));
  77. return strlen(buf);
  78. }
  79. #if 0 /* hangs */
  80. static ssize_t _set_name(struct device *dev, struct device_attribute *attr,
  81. const char *buf, size_t count)
  82. {
  83. int err = 0;
  84. char *out = kmalloc(count + 1, GFP_KERNEL);
  85. if (!out)
  86. return -ENOMEM;
  87. memcpy(out, buf, count);
  88. if (count && out[count - 1] == '\n')
  89. out[--count] = 0;
  90. if (count)
  91. err = device_rename(dev, out);
  92. kfree(out);
  93. return (err < 0) ? err : count;
  94. }
  95. #endif
  96. static ssize_t _show_channelmap(struct device *dev,
  97. struct device_attribute *attr, char *buf)
  98. {
  99. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  100. char *bp = buf;
  101. int i;
  102. for (i = 0; i <= mdev->nrbchan; i++)
  103. *bp++ = test_channelmap(i, mdev->channelmap) ? '1' : '0';
  104. return bp - buf;
  105. }
  106. static struct device_attribute mISDN_dev_attrs[] = {
  107. __ATTR(id, S_IRUGO, _show_id, NULL),
  108. __ATTR(d_protocols, S_IRUGO, _show_d_protocols, NULL),
  109. __ATTR(b_protocols, S_IRUGO, _show_b_protocols, NULL),
  110. __ATTR(protocol, S_IRUGO, _show_protocol, NULL),
  111. __ATTR(channelmap, S_IRUGO, _show_channelmap, NULL),
  112. __ATTR(nrbchan, S_IRUGO, _show_nrbchan, NULL),
  113. __ATTR(name, S_IRUGO, _show_name, NULL),
  114. /* __ATTR(name, S_IRUGO | S_IWUSR, _show_name, _set_name), */
  115. {}
  116. };
  117. #ifdef CONFIG_HOTPLUG
  118. static int mISDN_uevent(struct device *dev, struct kobj_uevent_env *env)
  119. {
  120. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  121. if (!mdev)
  122. return 0;
  123. if (add_uevent_var(env, "nchans=%d", mdev->nrbchan))
  124. return -ENOMEM;
  125. return 0;
  126. }
  127. #endif
  128. static void mISDN_class_release(struct class *cls)
  129. {
  130. /* do nothing, it's static */
  131. }
  132. static struct class mISDN_class = {
  133. .name = "mISDN",
  134. .owner = THIS_MODULE,
  135. #ifdef CONFIG_HOTPLUG
  136. .dev_uevent = mISDN_uevent,
  137. #endif
  138. .dev_attrs = mISDN_dev_attrs,
  139. .dev_release = mISDN_dev_release,
  140. .class_release = mISDN_class_release,
  141. };
  142. static int
  143. _get_mdevice(struct device *dev, void *id)
  144. {
  145. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  146. if (!mdev)
  147. return 0;
  148. if (mdev->id != *(u_int *)id)
  149. return 0;
  150. return 1;
  151. }
  152. struct mISDNdevice
  153. *get_mdevice(u_int id)
  154. {
  155. return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
  156. _get_mdevice));
  157. }
  158. static int
  159. _get_mdevice_count(struct device *dev, void *cnt)
  160. {
  161. *(int *)cnt += 1;
  162. return 0;
  163. }
  164. int
  165. get_mdevice_count(void)
  166. {
  167. int cnt = 0;
  168. class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
  169. return cnt;
  170. }
  171. static int
  172. get_free_devid(void)
  173. {
  174. u_int i;
  175. for (i = 0; i <= MAX_DEVICE_ID; i++)
  176. if (!test_and_set_bit(i, (u_long *)&device_ids))
  177. break;
  178. if (i > MAX_DEVICE_ID)
  179. return -EBUSY;
  180. return i;
  181. }
  182. int
  183. mISDN_register_device(struct mISDNdevice *dev,
  184. struct device *parent, char *name)
  185. {
  186. int err;
  187. err = get_free_devid();
  188. if (err < 0)
  189. goto error1;
  190. dev->id = err;
  191. device_initialize(&dev->dev);
  192. if (name && name[0])
  193. dev_set_name(&dev->dev, "%s", name);
  194. else
  195. dev_set_name(&dev->dev, "mISDN%d", dev->id);
  196. if (debug & DEBUG_CORE)
  197. printk(KERN_DEBUG "mISDN_register %s %d\n",
  198. dev_name(&dev->dev), dev->id);
  199. err = create_stack(dev);
  200. if (err)
  201. goto error1;
  202. dev->dev.class = &mISDN_class;
  203. dev->dev.platform_data = dev;
  204. dev->dev.parent = parent;
  205. dev_set_drvdata(&dev->dev, dev);
  206. err = device_add(&dev->dev);
  207. if (err)
  208. goto error3;
  209. return 0;
  210. error3:
  211. delete_stack(dev);
  212. return err;
  213. error1:
  214. return err;
  215. }
  216. EXPORT_SYMBOL(mISDN_register_device);
  217. void
  218. mISDN_unregister_device(struct mISDNdevice *dev) {
  219. if (debug & DEBUG_CORE)
  220. printk(KERN_DEBUG "mISDN_unregister %s %d\n",
  221. dev_name(&dev->dev), dev->id);
  222. /* sysfs_remove_link(&dev->dev.kobj, "device"); */
  223. device_del(&dev->dev);
  224. dev_set_drvdata(&dev->dev, NULL);
  225. test_and_clear_bit(dev->id, (u_long *)&device_ids);
  226. delete_stack(dev);
  227. put_device(&dev->dev);
  228. }
  229. EXPORT_SYMBOL(mISDN_unregister_device);
  230. u_int
  231. get_all_Bprotocols(void)
  232. {
  233. struct Bprotocol *bp;
  234. u_int m = 0;
  235. read_lock(&bp_lock);
  236. list_for_each_entry(bp, &Bprotocols, list)
  237. m |= bp->Bprotocols;
  238. read_unlock(&bp_lock);
  239. return m;
  240. }
  241. struct Bprotocol *
  242. get_Bprotocol4mask(u_int m)
  243. {
  244. struct Bprotocol *bp;
  245. read_lock(&bp_lock);
  246. list_for_each_entry(bp, &Bprotocols, list)
  247. if (bp->Bprotocols & m) {
  248. read_unlock(&bp_lock);
  249. return bp;
  250. }
  251. read_unlock(&bp_lock);
  252. return NULL;
  253. }
  254. struct Bprotocol *
  255. get_Bprotocol4id(u_int id)
  256. {
  257. u_int m;
  258. if (id < ISDN_P_B_START || id > 63) {
  259. printk(KERN_WARNING "%s id not in range %d\n",
  260. __func__, id);
  261. return NULL;
  262. }
  263. m = 1 << (id & ISDN_P_B_MASK);
  264. return get_Bprotocol4mask(m);
  265. }
  266. int
  267. mISDN_register_Bprotocol(struct Bprotocol *bp)
  268. {
  269. u_long flags;
  270. struct Bprotocol *old;
  271. if (debug & DEBUG_CORE)
  272. printk(KERN_DEBUG "%s: %s/%x\n", __func__,
  273. bp->name, bp->Bprotocols);
  274. old = get_Bprotocol4mask(bp->Bprotocols);
  275. if (old) {
  276. printk(KERN_WARNING
  277. "register duplicate protocol old %s/%x new %s/%x\n",
  278. old->name, old->Bprotocols, bp->name, bp->Bprotocols);
  279. return -EBUSY;
  280. }
  281. write_lock_irqsave(&bp_lock, flags);
  282. list_add_tail(&bp->list, &Bprotocols);
  283. write_unlock_irqrestore(&bp_lock, flags);
  284. return 0;
  285. }
  286. EXPORT_SYMBOL(mISDN_register_Bprotocol);
  287. void
  288. mISDN_unregister_Bprotocol(struct Bprotocol *bp)
  289. {
  290. u_long flags;
  291. if (debug & DEBUG_CORE)
  292. printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
  293. bp->Bprotocols);
  294. write_lock_irqsave(&bp_lock, flags);
  295. list_del(&bp->list);
  296. write_unlock_irqrestore(&bp_lock, flags);
  297. }
  298. EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
  299. static int
  300. mISDNInit(void)
  301. {
  302. int err;
  303. printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
  304. MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
  305. mISDN_init_clock(&debug);
  306. mISDN_initstack(&debug);
  307. err = class_register(&mISDN_class);
  308. if (err)
  309. goto error1;
  310. err = mISDN_inittimer(&debug);
  311. if (err)
  312. goto error2;
  313. err = l1_init(&debug);
  314. if (err)
  315. goto error3;
  316. err = Isdnl2_Init(&debug);
  317. if (err)
  318. goto error4;
  319. err = misdn_sock_init(&debug);
  320. if (err)
  321. goto error5;
  322. return 0;
  323. error5:
  324. Isdnl2_cleanup();
  325. error4:
  326. l1_cleanup();
  327. error3:
  328. mISDN_timer_cleanup();
  329. error2:
  330. class_unregister(&mISDN_class);
  331. error1:
  332. return err;
  333. }
  334. static void mISDN_cleanup(void)
  335. {
  336. misdn_sock_cleanup();
  337. Isdnl2_cleanup();
  338. l1_cleanup();
  339. mISDN_timer_cleanup();
  340. class_unregister(&mISDN_class);
  341. printk(KERN_DEBUG "mISDNcore unloaded\n");
  342. }
  343. module_init(mISDNInit);
  344. module_exit(mISDN_cleanup);