core.c 8.3 KB

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