core.c 8.4 KB

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