core.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. 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. static void mISDN_class_release(struct class *cls)
  127. {
  128. /* do nothing, it's static */
  129. }
  130. static struct class mISDN_class = {
  131. .name = "mISDN",
  132. .owner = THIS_MODULE,
  133. .dev_uevent = mISDN_uevent,
  134. .dev_attrs = mISDN_dev_attrs,
  135. .dev_release = mISDN_dev_release,
  136. .class_release = mISDN_class_release,
  137. };
  138. static int
  139. _get_mdevice(struct device *dev, const void *id)
  140. {
  141. struct mISDNdevice *mdev = dev_to_mISDN(dev);
  142. if (!mdev)
  143. return 0;
  144. if (mdev->id != *(const u_int *)id)
  145. return 0;
  146. return 1;
  147. }
  148. struct mISDNdevice
  149. *get_mdevice(u_int id)
  150. {
  151. return dev_to_mISDN(class_find_device(&mISDN_class, NULL, &id,
  152. _get_mdevice));
  153. }
  154. static int
  155. _get_mdevice_count(struct device *dev, void *cnt)
  156. {
  157. *(int *)cnt += 1;
  158. return 0;
  159. }
  160. int
  161. get_mdevice_count(void)
  162. {
  163. int cnt = 0;
  164. class_for_each_device(&mISDN_class, NULL, &cnt, _get_mdevice_count);
  165. return cnt;
  166. }
  167. static int
  168. get_free_devid(void)
  169. {
  170. u_int i;
  171. for (i = 0; i <= MAX_DEVICE_ID; i++)
  172. if (!test_and_set_bit(i, (u_long *)&device_ids))
  173. break;
  174. if (i > MAX_DEVICE_ID)
  175. return -EBUSY;
  176. return i;
  177. }
  178. int
  179. mISDN_register_device(struct mISDNdevice *dev,
  180. struct device *parent, char *name)
  181. {
  182. int err;
  183. err = get_free_devid();
  184. if (err < 0)
  185. goto error1;
  186. dev->id = err;
  187. device_initialize(&dev->dev);
  188. if (name && name[0])
  189. dev_set_name(&dev->dev, "%s", name);
  190. else
  191. dev_set_name(&dev->dev, "mISDN%d", dev->id);
  192. if (debug & DEBUG_CORE)
  193. printk(KERN_DEBUG "mISDN_register %s %d\n",
  194. dev_name(&dev->dev), dev->id);
  195. err = create_stack(dev);
  196. if (err)
  197. goto error1;
  198. dev->dev.class = &mISDN_class;
  199. dev->dev.platform_data = dev;
  200. dev->dev.parent = parent;
  201. dev_set_drvdata(&dev->dev, dev);
  202. err = device_add(&dev->dev);
  203. if (err)
  204. goto error3;
  205. return 0;
  206. error3:
  207. delete_stack(dev);
  208. return err;
  209. error1:
  210. return err;
  211. }
  212. EXPORT_SYMBOL(mISDN_register_device);
  213. void
  214. mISDN_unregister_device(struct mISDNdevice *dev) {
  215. if (debug & DEBUG_CORE)
  216. printk(KERN_DEBUG "mISDN_unregister %s %d\n",
  217. dev_name(&dev->dev), dev->id);
  218. /* sysfs_remove_link(&dev->dev.kobj, "device"); */
  219. device_del(&dev->dev);
  220. dev_set_drvdata(&dev->dev, NULL);
  221. test_and_clear_bit(dev->id, (u_long *)&device_ids);
  222. delete_stack(dev);
  223. put_device(&dev->dev);
  224. }
  225. EXPORT_SYMBOL(mISDN_unregister_device);
  226. u_int
  227. get_all_Bprotocols(void)
  228. {
  229. struct Bprotocol *bp;
  230. u_int m = 0;
  231. read_lock(&bp_lock);
  232. list_for_each_entry(bp, &Bprotocols, list)
  233. m |= bp->Bprotocols;
  234. read_unlock(&bp_lock);
  235. return m;
  236. }
  237. struct Bprotocol *
  238. get_Bprotocol4mask(u_int m)
  239. {
  240. struct Bprotocol *bp;
  241. read_lock(&bp_lock);
  242. list_for_each_entry(bp, &Bprotocols, list)
  243. if (bp->Bprotocols & m) {
  244. read_unlock(&bp_lock);
  245. return bp;
  246. }
  247. read_unlock(&bp_lock);
  248. return NULL;
  249. }
  250. struct Bprotocol *
  251. get_Bprotocol4id(u_int id)
  252. {
  253. u_int m;
  254. if (id < ISDN_P_B_START || id > 63) {
  255. printk(KERN_WARNING "%s id not in range %d\n",
  256. __func__, id);
  257. return NULL;
  258. }
  259. m = 1 << (id & ISDN_P_B_MASK);
  260. return get_Bprotocol4mask(m);
  261. }
  262. int
  263. mISDN_register_Bprotocol(struct Bprotocol *bp)
  264. {
  265. u_long flags;
  266. struct Bprotocol *old;
  267. if (debug & DEBUG_CORE)
  268. printk(KERN_DEBUG "%s: %s/%x\n", __func__,
  269. bp->name, bp->Bprotocols);
  270. old = get_Bprotocol4mask(bp->Bprotocols);
  271. if (old) {
  272. printk(KERN_WARNING
  273. "register duplicate protocol old %s/%x new %s/%x\n",
  274. old->name, old->Bprotocols, bp->name, bp->Bprotocols);
  275. return -EBUSY;
  276. }
  277. write_lock_irqsave(&bp_lock, flags);
  278. list_add_tail(&bp->list, &Bprotocols);
  279. write_unlock_irqrestore(&bp_lock, flags);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(mISDN_register_Bprotocol);
  283. void
  284. mISDN_unregister_Bprotocol(struct Bprotocol *bp)
  285. {
  286. u_long flags;
  287. if (debug & DEBUG_CORE)
  288. printk(KERN_DEBUG "%s: %s/%x\n", __func__, bp->name,
  289. bp->Bprotocols);
  290. write_lock_irqsave(&bp_lock, flags);
  291. list_del(&bp->list);
  292. write_unlock_irqrestore(&bp_lock, flags);
  293. }
  294. EXPORT_SYMBOL(mISDN_unregister_Bprotocol);
  295. static const char *msg_no_channel = "<no channel>";
  296. static const char *msg_no_stack = "<no stack>";
  297. static const char *msg_no_stackdev = "<no stack device>";
  298. const char *mISDNDevName4ch(struct mISDNchannel *ch)
  299. {
  300. if (!ch)
  301. return msg_no_channel;
  302. if (!ch->st)
  303. return msg_no_stack;
  304. if (!ch->st->dev)
  305. return msg_no_stackdev;
  306. return dev_name(&ch->st->dev->dev);
  307. };
  308. EXPORT_SYMBOL(mISDNDevName4ch);
  309. static int
  310. mISDNInit(void)
  311. {
  312. int err;
  313. printk(KERN_INFO "Modular ISDN core version %d.%d.%d\n",
  314. MISDN_MAJOR_VERSION, MISDN_MINOR_VERSION, MISDN_RELEASE);
  315. mISDN_init_clock(&debug);
  316. mISDN_initstack(&debug);
  317. err = class_register(&mISDN_class);
  318. if (err)
  319. goto error1;
  320. err = mISDN_inittimer(&debug);
  321. if (err)
  322. goto error2;
  323. err = l1_init(&debug);
  324. if (err)
  325. goto error3;
  326. err = Isdnl2_Init(&debug);
  327. if (err)
  328. goto error4;
  329. err = misdn_sock_init(&debug);
  330. if (err)
  331. goto error5;
  332. return 0;
  333. error5:
  334. Isdnl2_cleanup();
  335. error4:
  336. l1_cleanup();
  337. error3:
  338. mISDN_timer_cleanup();
  339. error2:
  340. class_unregister(&mISDN_class);
  341. error1:
  342. return err;
  343. }
  344. static void mISDN_cleanup(void)
  345. {
  346. misdn_sock_cleanup();
  347. Isdnl2_cleanup();
  348. l1_cleanup();
  349. mISDN_timer_cleanup();
  350. class_unregister(&mISDN_class);
  351. printk(KERN_DEBUG "mISDNcore unloaded\n");
  352. }
  353. module_init(mISDNInit);
  354. module_exit(mISDN_cleanup);