capi.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
  2. *
  3. * CAPI 2.0 Interface for Linux
  4. *
  5. * Copyright 1996 by Carsten Paeth <calle@calle.de>
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/major.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/fcntl.h>
  18. #include <linux/fs.h>
  19. #include <linux/signal.h>
  20. #include <linux/mutex.h>
  21. #include <linux/mm.h>
  22. #include <linux/timer.h>
  23. #include <linux/wait.h>
  24. #include <linux/tty.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/ppp_defs.h>
  27. #include <linux/ppp-ioctl.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/poll.h>
  32. #include <linux/capi.h>
  33. #include <linux/kernelcapi.h>
  34. #include <linux/init.h>
  35. #include <linux/device.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/isdn/capiutil.h>
  38. #include <linux/isdn/capicmd.h>
  39. MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
  40. MODULE_AUTHOR("Carsten Paeth");
  41. MODULE_LICENSE("GPL");
  42. /* -------- driver information -------------------------------------- */
  43. static DEFINE_MUTEX(capi_mutex);
  44. static struct class *capi_class;
  45. static int capi_major = 68; /* allocated */
  46. module_param_named(major, capi_major, uint, 0);
  47. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  48. #define CAPINC_NR_PORTS 32
  49. #define CAPINC_MAX_PORTS 256
  50. static int capi_ttyminors = CAPINC_NR_PORTS;
  51. module_param_named(ttyminors, capi_ttyminors, uint, 0);
  52. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  53. /* -------- defines ------------------------------------------------- */
  54. #define CAPINC_MAX_RECVQUEUE 10
  55. #define CAPINC_MAX_SENDQUEUE 10
  56. #define CAPI_MAX_BLKSIZE 2048
  57. /* -------- data structures ----------------------------------------- */
  58. struct capidev;
  59. struct capincci;
  60. struct capiminor;
  61. struct ackqueue_entry {
  62. struct list_head list;
  63. u16 datahandle;
  64. };
  65. struct capiminor {
  66. struct kref kref;
  67. unsigned int minor;
  68. struct capi20_appl *ap;
  69. u32 ncci;
  70. atomic_t datahandle;
  71. atomic_t msgid;
  72. struct tty_port port;
  73. int ttyinstop;
  74. int ttyoutstop;
  75. struct sk_buff_head inqueue;
  76. struct sk_buff_head outqueue;
  77. int outbytes;
  78. struct sk_buff *outskb;
  79. spinlock_t outlock;
  80. /* transmit path */
  81. struct list_head ackqueue;
  82. int nack;
  83. spinlock_t ackqlock;
  84. };
  85. struct capincci {
  86. struct list_head list;
  87. u32 ncci;
  88. struct capidev *cdev;
  89. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  90. struct capiminor *minorp;
  91. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  92. };
  93. struct capidev {
  94. struct list_head list;
  95. struct capi20_appl ap;
  96. u16 errcode;
  97. unsigned userflags;
  98. struct sk_buff_head recvqueue;
  99. wait_queue_head_t recvwait;
  100. struct list_head nccis;
  101. struct mutex lock;
  102. };
  103. /* -------- global variables ---------------------------------------- */
  104. static DEFINE_MUTEX(capidev_list_lock);
  105. static LIST_HEAD(capidev_list);
  106. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  107. static DEFINE_SPINLOCK(capiminors_lock);
  108. static struct capiminor **capiminors;
  109. static struct tty_driver *capinc_tty_driver;
  110. /* -------- datahandles --------------------------------------------- */
  111. static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
  112. {
  113. struct ackqueue_entry *n;
  114. n = kmalloc(sizeof(*n), GFP_ATOMIC);
  115. if (unlikely(!n)) {
  116. printk(KERN_ERR "capi: alloc datahandle failed\n");
  117. return -1;
  118. }
  119. n->datahandle = datahandle;
  120. INIT_LIST_HEAD(&n->list);
  121. spin_lock_bh(&mp->ackqlock);
  122. list_add_tail(&n->list, &mp->ackqueue);
  123. mp->nack++;
  124. spin_unlock_bh(&mp->ackqlock);
  125. return 0;
  126. }
  127. static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
  128. {
  129. struct ackqueue_entry *p, *tmp;
  130. spin_lock_bh(&mp->ackqlock);
  131. list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
  132. if (p->datahandle == datahandle) {
  133. list_del(&p->list);
  134. mp->nack--;
  135. spin_unlock_bh(&mp->ackqlock);
  136. kfree(p);
  137. return 0;
  138. }
  139. }
  140. spin_unlock_bh(&mp->ackqlock);
  141. return -1;
  142. }
  143. static void capiminor_del_all_ack(struct capiminor *mp)
  144. {
  145. struct ackqueue_entry *p, *tmp;
  146. list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
  147. list_del(&p->list);
  148. kfree(p);
  149. mp->nack--;
  150. }
  151. }
  152. /* -------- struct capiminor ---------------------------------------- */
  153. static const struct tty_port_operations capiminor_port_ops; /* we have none */
  154. static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
  155. {
  156. struct capiminor *mp;
  157. struct device *dev;
  158. unsigned int minor;
  159. mp = kzalloc(sizeof(*mp), GFP_KERNEL);
  160. if (!mp) {
  161. printk(KERN_ERR "capi: can't alloc capiminor\n");
  162. return NULL;
  163. }
  164. kref_init(&mp->kref);
  165. mp->ap = ap;
  166. mp->ncci = ncci;
  167. INIT_LIST_HEAD(&mp->ackqueue);
  168. spin_lock_init(&mp->ackqlock);
  169. skb_queue_head_init(&mp->inqueue);
  170. skb_queue_head_init(&mp->outqueue);
  171. spin_lock_init(&mp->outlock);
  172. tty_port_init(&mp->port);
  173. mp->port.ops = &capiminor_port_ops;
  174. /* Allocate the least unused minor number. */
  175. spin_lock(&capiminors_lock);
  176. for (minor = 0; minor < capi_ttyminors; minor++)
  177. if (!capiminors[minor]) {
  178. capiminors[minor] = mp;
  179. break;
  180. }
  181. spin_unlock(&capiminors_lock);
  182. if (minor == capi_ttyminors) {
  183. printk(KERN_NOTICE "capi: out of minors\n");
  184. goto err_out1;
  185. }
  186. mp->minor = minor;
  187. dev = tty_register_device(capinc_tty_driver, minor, NULL);
  188. if (IS_ERR(dev))
  189. goto err_out2;
  190. return mp;
  191. err_out2:
  192. spin_lock(&capiminors_lock);
  193. capiminors[minor] = NULL;
  194. spin_unlock(&capiminors_lock);
  195. err_out1:
  196. kfree(mp);
  197. return NULL;
  198. }
  199. static void capiminor_destroy(struct kref *kref)
  200. {
  201. struct capiminor *mp = container_of(kref, struct capiminor, kref);
  202. kfree_skb(mp->outskb);
  203. skb_queue_purge(&mp->inqueue);
  204. skb_queue_purge(&mp->outqueue);
  205. capiminor_del_all_ack(mp);
  206. kfree(mp);
  207. }
  208. static struct capiminor *capiminor_get(unsigned int minor)
  209. {
  210. struct capiminor *mp;
  211. spin_lock(&capiminors_lock);
  212. mp = capiminors[minor];
  213. if (mp)
  214. kref_get(&mp->kref);
  215. spin_unlock(&capiminors_lock);
  216. return mp;
  217. }
  218. static inline void capiminor_put(struct capiminor *mp)
  219. {
  220. kref_put(&mp->kref, capiminor_destroy);
  221. }
  222. static void capiminor_free(struct capiminor *mp)
  223. {
  224. tty_unregister_device(capinc_tty_driver, mp->minor);
  225. spin_lock(&capiminors_lock);
  226. capiminors[mp->minor] = NULL;
  227. spin_unlock(&capiminors_lock);
  228. capiminor_put(mp);
  229. }
  230. /* -------- struct capincci ----------------------------------------- */
  231. static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
  232. {
  233. if (cdev->userflags & CAPIFLAG_HIGHJACKING)
  234. np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
  235. }
  236. static void capincci_free_minor(struct capincci *np)
  237. {
  238. struct capiminor *mp = np->minorp;
  239. struct tty_struct *tty;
  240. if (mp) {
  241. tty = tty_port_tty_get(&mp->port);
  242. if (tty) {
  243. tty_vhangup(tty);
  244. tty_kref_put(tty);
  245. }
  246. capiminor_free(mp);
  247. }
  248. }
  249. static inline unsigned int capincci_minor_opencount(struct capincci *np)
  250. {
  251. struct capiminor *mp = np->minorp;
  252. unsigned int count = 0;
  253. struct tty_struct *tty;
  254. if (mp) {
  255. tty = tty_port_tty_get(&mp->port);
  256. if (tty) {
  257. count = tty->count;
  258. tty_kref_put(tty);
  259. }
  260. }
  261. return count;
  262. }
  263. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  264. static inline void
  265. capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
  266. static inline void capincci_free_minor(struct capincci *np) { }
  267. static inline unsigned int capincci_minor_opencount(struct capincci *np)
  268. {
  269. return 0;
  270. }
  271. #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  272. static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
  273. {
  274. struct capincci *np;
  275. np = kzalloc(sizeof(*np), GFP_KERNEL);
  276. if (!np)
  277. return NULL;
  278. np->ncci = ncci;
  279. np->cdev = cdev;
  280. capincci_alloc_minor(cdev, np);
  281. list_add_tail(&np->list, &cdev->nccis);
  282. return np;
  283. }
  284. static void capincci_free(struct capidev *cdev, u32 ncci)
  285. {
  286. struct capincci *np, *tmp;
  287. list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
  288. if (ncci == 0xffffffff || np->ncci == ncci) {
  289. capincci_free_minor(np);
  290. list_del(&np->list);
  291. kfree(np);
  292. }
  293. }
  294. static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
  295. {
  296. struct capincci *np;
  297. list_for_each_entry(np, &cdev->nccis, list)
  298. if (np->ncci == ncci)
  299. return np;
  300. return NULL;
  301. }
  302. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  303. /* -------- handle data queue --------------------------------------- */
  304. static struct sk_buff *
  305. gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
  306. {
  307. struct sk_buff *nskb;
  308. nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
  309. if (nskb) {
  310. u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
  311. unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
  312. capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
  313. capimsg_setu16(s, 2, mp->ap->applid);
  314. capimsg_setu8 (s, 4, CAPI_DATA_B3);
  315. capimsg_setu8 (s, 5, CAPI_RESP);
  316. capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
  317. capimsg_setu32(s, 8, mp->ncci);
  318. capimsg_setu16(s, 12, datahandle);
  319. }
  320. return nskb;
  321. }
  322. static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
  323. {
  324. unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
  325. struct tty_struct *tty;
  326. struct sk_buff *nskb;
  327. u16 errcode, datahandle;
  328. struct tty_ldisc *ld;
  329. int ret = -1;
  330. tty = tty_port_tty_get(&mp->port);
  331. if (!tty) {
  332. pr_debug("capi: currently no receiver\n");
  333. return -1;
  334. }
  335. ld = tty_ldisc_ref(tty);
  336. if (!ld) {
  337. /* fatal error, do not requeue */
  338. ret = 0;
  339. kfree_skb(skb);
  340. goto deref_tty;
  341. }
  342. if (ld->ops->receive_buf == NULL) {
  343. pr_debug("capi: ldisc has no receive_buf function\n");
  344. /* fatal error, do not requeue */
  345. goto free_skb;
  346. }
  347. if (mp->ttyinstop) {
  348. pr_debug("capi: recv tty throttled\n");
  349. goto deref_ldisc;
  350. }
  351. if (tty->receive_room < datalen) {
  352. pr_debug("capi: no room in tty\n");
  353. goto deref_ldisc;
  354. }
  355. nskb = gen_data_b3_resp_for(mp, skb);
  356. if (!nskb) {
  357. printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
  358. goto deref_ldisc;
  359. }
  360. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
  361. errcode = capi20_put_message(mp->ap, nskb);
  362. if (errcode == CAPI_NOERROR) {
  363. skb_pull(skb, CAPIMSG_LEN(skb->data));
  364. pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
  365. datahandle, skb->len);
  366. ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
  367. } else {
  368. printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
  369. errcode);
  370. kfree_skb(nskb);
  371. if (errcode == CAPI_SENDQUEUEFULL)
  372. goto deref_ldisc;
  373. }
  374. free_skb:
  375. ret = 0;
  376. kfree_skb(skb);
  377. deref_ldisc:
  378. tty_ldisc_deref(ld);
  379. deref_tty:
  380. tty_kref_put(tty);
  381. return ret;
  382. }
  383. static void handle_minor_recv(struct capiminor *mp)
  384. {
  385. struct sk_buff *skb;
  386. while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
  387. if (handle_recv_skb(mp, skb) < 0) {
  388. skb_queue_head(&mp->inqueue, skb);
  389. return;
  390. }
  391. }
  392. static void handle_minor_send(struct capiminor *mp)
  393. {
  394. struct tty_struct *tty;
  395. struct sk_buff *skb;
  396. u16 len;
  397. u16 errcode;
  398. u16 datahandle;
  399. tty = tty_port_tty_get(&mp->port);
  400. if (!tty)
  401. return;
  402. if (mp->ttyoutstop) {
  403. pr_debug("capi: send: tty stopped\n");
  404. tty_kref_put(tty);
  405. return;
  406. }
  407. while (1) {
  408. spin_lock_bh(&mp->outlock);
  409. skb = __skb_dequeue(&mp->outqueue);
  410. if (!skb) {
  411. spin_unlock_bh(&mp->outlock);
  412. break;
  413. }
  414. len = (u16)skb->len;
  415. mp->outbytes -= len;
  416. spin_unlock_bh(&mp->outlock);
  417. datahandle = atomic_inc_return(&mp->datahandle);
  418. skb_push(skb, CAPI_DATA_B3_REQ_LEN);
  419. memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
  420. capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
  421. capimsg_setu16(skb->data, 2, mp->ap->applid);
  422. capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
  423. capimsg_setu8 (skb->data, 5, CAPI_REQ);
  424. capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
  425. capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
  426. capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
  427. capimsg_setu16(skb->data, 16, len); /* Data length */
  428. capimsg_setu16(skb->data, 18, datahandle);
  429. capimsg_setu16(skb->data, 20, 0); /* Flags */
  430. if (capiminor_add_ack(mp, datahandle) < 0) {
  431. skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
  432. spin_lock_bh(&mp->outlock);
  433. __skb_queue_head(&mp->outqueue, skb);
  434. mp->outbytes += len;
  435. spin_unlock_bh(&mp->outlock);
  436. break;
  437. }
  438. errcode = capi20_put_message(mp->ap, skb);
  439. if (errcode == CAPI_NOERROR) {
  440. pr_debug("capi: DATA_B3_REQ %u len=%u\n",
  441. datahandle, len);
  442. continue;
  443. }
  444. capiminor_del_ack(mp, datahandle);
  445. if (errcode == CAPI_SENDQUEUEFULL) {
  446. skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
  447. spin_lock_bh(&mp->outlock);
  448. __skb_queue_head(&mp->outqueue, skb);
  449. mp->outbytes += len;
  450. spin_unlock_bh(&mp->outlock);
  451. break;
  452. }
  453. /* ups, drop packet */
  454. printk(KERN_ERR "capi: put_message = %x\n", errcode);
  455. kfree_skb(skb);
  456. }
  457. tty_kref_put(tty);
  458. }
  459. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  460. /* -------- function called by lower level -------------------------- */
  461. static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
  462. {
  463. struct capidev *cdev = ap->private;
  464. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  465. struct tty_struct *tty;
  466. struct capiminor *mp;
  467. u16 datahandle;
  468. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  469. struct capincci *np;
  470. mutex_lock(&cdev->lock);
  471. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
  472. u16 info = CAPIMSG_U16(skb->data, 12); // Info field
  473. if ((info & 0xff00) == 0)
  474. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  475. }
  476. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
  477. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  478. if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
  479. skb_queue_tail(&cdev->recvqueue, skb);
  480. wake_up_interruptible(&cdev->recvwait);
  481. goto unlock_out;
  482. }
  483. np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
  484. if (!np) {
  485. printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
  486. skb_queue_tail(&cdev->recvqueue, skb);
  487. wake_up_interruptible(&cdev->recvwait);
  488. goto unlock_out;
  489. }
  490. #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
  491. skb_queue_tail(&cdev->recvqueue, skb);
  492. wake_up_interruptible(&cdev->recvwait);
  493. #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  494. mp = np->minorp;
  495. if (!mp) {
  496. skb_queue_tail(&cdev->recvqueue, skb);
  497. wake_up_interruptible(&cdev->recvwait);
  498. goto unlock_out;
  499. }
  500. if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
  501. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
  502. pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
  503. datahandle, skb->len-CAPIMSG_LEN(skb->data));
  504. skb_queue_tail(&mp->inqueue, skb);
  505. handle_minor_recv(mp);
  506. } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
  507. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
  508. pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
  509. datahandle,
  510. CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
  511. kfree_skb(skb);
  512. capiminor_del_ack(mp, datahandle);
  513. tty = tty_port_tty_get(&mp->port);
  514. if (tty) {
  515. tty_wakeup(tty);
  516. tty_kref_put(tty);
  517. }
  518. handle_minor_send(mp);
  519. } else {
  520. /* ups, let capi application handle it :-) */
  521. skb_queue_tail(&cdev->recvqueue, skb);
  522. wake_up_interruptible(&cdev->recvwait);
  523. }
  524. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  525. unlock_out:
  526. mutex_unlock(&cdev->lock);
  527. }
  528. /* -------- file_operations for capidev ----------------------------- */
  529. static ssize_t
  530. capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  531. {
  532. struct capidev *cdev = file->private_data;
  533. struct sk_buff *skb;
  534. size_t copied;
  535. int err;
  536. if (!cdev->ap.applid)
  537. return -ENODEV;
  538. skb = skb_dequeue(&cdev->recvqueue);
  539. if (!skb) {
  540. if (file->f_flags & O_NONBLOCK)
  541. return -EAGAIN;
  542. err = wait_event_interruptible(cdev->recvwait,
  543. (skb = skb_dequeue(&cdev->recvqueue)));
  544. if (err)
  545. return err;
  546. }
  547. if (skb->len > count) {
  548. skb_queue_head(&cdev->recvqueue, skb);
  549. return -EMSGSIZE;
  550. }
  551. if (copy_to_user(buf, skb->data, skb->len)) {
  552. skb_queue_head(&cdev->recvqueue, skb);
  553. return -EFAULT;
  554. }
  555. copied = skb->len;
  556. kfree_skb(skb);
  557. return copied;
  558. }
  559. static ssize_t
  560. capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  561. {
  562. struct capidev *cdev = file->private_data;
  563. struct sk_buff *skb;
  564. u16 mlen;
  565. if (!cdev->ap.applid)
  566. return -ENODEV;
  567. skb = alloc_skb(count, GFP_USER);
  568. if (!skb)
  569. return -ENOMEM;
  570. if (copy_from_user(skb_put(skb, count), buf, count)) {
  571. kfree_skb(skb);
  572. return -EFAULT;
  573. }
  574. mlen = CAPIMSG_LEN(skb->data);
  575. if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
  576. if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
  577. kfree_skb(skb);
  578. return -EINVAL;
  579. }
  580. } else {
  581. if (mlen != count) {
  582. kfree_skb(skb);
  583. return -EINVAL;
  584. }
  585. }
  586. CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
  587. if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
  588. mutex_lock(&cdev->lock);
  589. capincci_free(cdev, CAPIMSG_NCCI(skb->data));
  590. mutex_unlock(&cdev->lock);
  591. }
  592. cdev->errcode = capi20_put_message(&cdev->ap, skb);
  593. if (cdev->errcode) {
  594. kfree_skb(skb);
  595. return -EIO;
  596. }
  597. return count;
  598. }
  599. static unsigned int
  600. capi_poll(struct file *file, poll_table *wait)
  601. {
  602. struct capidev *cdev = file->private_data;
  603. unsigned int mask = 0;
  604. if (!cdev->ap.applid)
  605. return POLLERR;
  606. poll_wait(file, &(cdev->recvwait), wait);
  607. mask = POLLOUT | POLLWRNORM;
  608. if (!skb_queue_empty(&cdev->recvqueue))
  609. mask |= POLLIN | POLLRDNORM;
  610. return mask;
  611. }
  612. static int
  613. capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  614. {
  615. struct capidev *cdev = file->private_data;
  616. capi_ioctl_struct data;
  617. int retval = -EINVAL;
  618. void __user *argp = (void __user *)arg;
  619. switch (cmd) {
  620. case CAPI_REGISTER:
  621. mutex_lock(&cdev->lock);
  622. if (cdev->ap.applid) {
  623. retval = -EEXIST;
  624. goto register_out;
  625. }
  626. if (copy_from_user(&cdev->ap.rparam, argp,
  627. sizeof(struct capi_register_params))) {
  628. retval = -EFAULT;
  629. goto register_out;
  630. }
  631. cdev->ap.private = cdev;
  632. cdev->ap.recv_message = capi_recv_message;
  633. cdev->errcode = capi20_register(&cdev->ap);
  634. retval = (int)cdev->ap.applid;
  635. if (cdev->errcode) {
  636. cdev->ap.applid = 0;
  637. retval = -EIO;
  638. }
  639. register_out:
  640. mutex_unlock(&cdev->lock);
  641. return retval;
  642. case CAPI_GET_VERSION:
  643. {
  644. if (copy_from_user(&data.contr, argp,
  645. sizeof(data.contr)))
  646. return -EFAULT;
  647. cdev->errcode = capi20_get_version(data.contr, &data.version);
  648. if (cdev->errcode)
  649. return -EIO;
  650. if (copy_to_user(argp, &data.version,
  651. sizeof(data.version)))
  652. return -EFAULT;
  653. }
  654. return 0;
  655. case CAPI_GET_SERIAL:
  656. {
  657. if (copy_from_user(&data.contr, argp,
  658. sizeof(data.contr)))
  659. return -EFAULT;
  660. cdev->errcode = capi20_get_serial(data.contr, data.serial);
  661. if (cdev->errcode)
  662. return -EIO;
  663. if (copy_to_user(argp, data.serial,
  664. sizeof(data.serial)))
  665. return -EFAULT;
  666. }
  667. return 0;
  668. case CAPI_GET_PROFILE:
  669. {
  670. if (copy_from_user(&data.contr, argp,
  671. sizeof(data.contr)))
  672. return -EFAULT;
  673. if (data.contr == 0) {
  674. cdev->errcode = capi20_get_profile(data.contr, &data.profile);
  675. if (cdev->errcode)
  676. return -EIO;
  677. retval = copy_to_user(argp,
  678. &data.profile.ncontroller,
  679. sizeof(data.profile.ncontroller));
  680. } else {
  681. cdev->errcode = capi20_get_profile(data.contr, &data.profile);
  682. if (cdev->errcode)
  683. return -EIO;
  684. retval = copy_to_user(argp, &data.profile,
  685. sizeof(data.profile));
  686. }
  687. if (retval)
  688. return -EFAULT;
  689. }
  690. return 0;
  691. case CAPI_GET_MANUFACTURER:
  692. {
  693. if (copy_from_user(&data.contr, argp,
  694. sizeof(data.contr)))
  695. return -EFAULT;
  696. cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
  697. if (cdev->errcode)
  698. return -EIO;
  699. if (copy_to_user(argp, data.manufacturer,
  700. sizeof(data.manufacturer)))
  701. return -EFAULT;
  702. }
  703. return 0;
  704. case CAPI_GET_ERRCODE:
  705. data.errcode = cdev->errcode;
  706. cdev->errcode = CAPI_NOERROR;
  707. if (arg) {
  708. if (copy_to_user(argp, &data.errcode,
  709. sizeof(data.errcode)))
  710. return -EFAULT;
  711. }
  712. return data.errcode;
  713. case CAPI_INSTALLED:
  714. if (capi20_isinstalled() == CAPI_NOERROR)
  715. return 0;
  716. return -ENXIO;
  717. case CAPI_MANUFACTURER_CMD:
  718. {
  719. struct capi_manufacturer_cmd mcmd;
  720. if (!capable(CAP_SYS_ADMIN))
  721. return -EPERM;
  722. if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
  723. return -EFAULT;
  724. return capi20_manufacturer(mcmd.cmd, mcmd.data);
  725. }
  726. return 0;
  727. case CAPI_SET_FLAGS:
  728. case CAPI_CLR_FLAGS: {
  729. unsigned userflags;
  730. if (copy_from_user(&userflags, argp, sizeof(userflags)))
  731. return -EFAULT;
  732. mutex_lock(&cdev->lock);
  733. if (cmd == CAPI_SET_FLAGS)
  734. cdev->userflags |= userflags;
  735. else
  736. cdev->userflags &= ~userflags;
  737. mutex_unlock(&cdev->lock);
  738. return 0;
  739. }
  740. case CAPI_GET_FLAGS:
  741. if (copy_to_user(argp, &cdev->userflags,
  742. sizeof(cdev->userflags)))
  743. return -EFAULT;
  744. return 0;
  745. case CAPI_NCCI_OPENCOUNT: {
  746. struct capincci *nccip;
  747. unsigned ncci;
  748. int count = 0;
  749. if (copy_from_user(&ncci, argp, sizeof(ncci)))
  750. return -EFAULT;
  751. mutex_lock(&cdev->lock);
  752. nccip = capincci_find(cdev, (u32)ncci);
  753. if (nccip)
  754. count = capincci_minor_opencount(nccip);
  755. mutex_unlock(&cdev->lock);
  756. return count;
  757. }
  758. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  759. case CAPI_NCCI_GETUNIT: {
  760. struct capincci *nccip;
  761. struct capiminor *mp;
  762. unsigned ncci;
  763. int unit = -ESRCH;
  764. if (copy_from_user(&ncci, argp, sizeof(ncci)))
  765. return -EFAULT;
  766. mutex_lock(&cdev->lock);
  767. nccip = capincci_find(cdev, (u32)ncci);
  768. if (nccip) {
  769. mp = nccip->minorp;
  770. if (mp)
  771. unit = mp->minor;
  772. }
  773. mutex_unlock(&cdev->lock);
  774. return unit;
  775. }
  776. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  777. default:
  778. return -EINVAL;
  779. }
  780. }
  781. static long
  782. capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  783. {
  784. int ret;
  785. mutex_lock(&capi_mutex);
  786. ret = capi_ioctl(file, cmd, arg);
  787. mutex_unlock(&capi_mutex);
  788. return ret;
  789. }
  790. static int capi_open(struct inode *inode, struct file *file)
  791. {
  792. struct capidev *cdev;
  793. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  794. if (!cdev)
  795. return -ENOMEM;
  796. mutex_init(&cdev->lock);
  797. skb_queue_head_init(&cdev->recvqueue);
  798. init_waitqueue_head(&cdev->recvwait);
  799. INIT_LIST_HEAD(&cdev->nccis);
  800. file->private_data = cdev;
  801. mutex_lock(&capidev_list_lock);
  802. list_add_tail(&cdev->list, &capidev_list);
  803. mutex_unlock(&capidev_list_lock);
  804. return nonseekable_open(inode, file);
  805. }
  806. static int capi_release(struct inode *inode, struct file *file)
  807. {
  808. struct capidev *cdev = file->private_data;
  809. mutex_lock(&capidev_list_lock);
  810. list_del(&cdev->list);
  811. mutex_unlock(&capidev_list_lock);
  812. if (cdev->ap.applid)
  813. capi20_release(&cdev->ap);
  814. skb_queue_purge(&cdev->recvqueue);
  815. capincci_free(cdev, 0xffffffff);
  816. kfree(cdev);
  817. return 0;
  818. }
  819. static const struct file_operations capi_fops =
  820. {
  821. .owner = THIS_MODULE,
  822. .llseek = no_llseek,
  823. .read = capi_read,
  824. .write = capi_write,
  825. .poll = capi_poll,
  826. .unlocked_ioctl = capi_unlocked_ioctl,
  827. .open = capi_open,
  828. .release = capi_release,
  829. };
  830. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  831. /* -------- tty_operations for capincci ----------------------------- */
  832. static int
  833. capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  834. {
  835. struct capiminor *mp = capiminor_get(tty->index);
  836. int ret = tty_standard_install(driver, tty);
  837. if (ret == 0)
  838. tty->driver_data = mp;
  839. else
  840. capiminor_put(mp);
  841. return ret;
  842. }
  843. static void capinc_tty_cleanup(struct tty_struct *tty)
  844. {
  845. struct capiminor *mp = tty->driver_data;
  846. tty->driver_data = NULL;
  847. capiminor_put(mp);
  848. }
  849. static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
  850. {
  851. struct capiminor *mp = tty->driver_data;
  852. int err;
  853. err = tty_port_open(&mp->port, tty, filp);
  854. if (err)
  855. return err;
  856. handle_minor_recv(mp);
  857. return 0;
  858. }
  859. static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
  860. {
  861. struct capiminor *mp = tty->driver_data;
  862. tty_port_close(&mp->port, tty, filp);
  863. }
  864. static int capinc_tty_write(struct tty_struct *tty,
  865. const unsigned char *buf, int count)
  866. {
  867. struct capiminor *mp = tty->driver_data;
  868. struct sk_buff *skb;
  869. pr_debug("capinc_tty_write(count=%d)\n", count);
  870. spin_lock_bh(&mp->outlock);
  871. skb = mp->outskb;
  872. if (skb) {
  873. mp->outskb = NULL;
  874. __skb_queue_tail(&mp->outqueue, skb);
  875. mp->outbytes += skb->len;
  876. }
  877. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
  878. if (!skb) {
  879. printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
  880. spin_unlock_bh(&mp->outlock);
  881. return -ENOMEM;
  882. }
  883. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  884. memcpy(skb_put(skb, count), buf, count);
  885. __skb_queue_tail(&mp->outqueue, skb);
  886. mp->outbytes += skb->len;
  887. spin_unlock_bh(&mp->outlock);
  888. handle_minor_send(mp);
  889. return count;
  890. }
  891. static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
  892. {
  893. struct capiminor *mp = tty->driver_data;
  894. bool invoke_send = false;
  895. struct sk_buff *skb;
  896. int ret = 1;
  897. pr_debug("capinc_put_char(%u)\n", ch);
  898. spin_lock_bh(&mp->outlock);
  899. skb = mp->outskb;
  900. if (skb) {
  901. if (skb_tailroom(skb) > 0) {
  902. *(skb_put(skb, 1)) = ch;
  903. goto unlock_out;
  904. }
  905. mp->outskb = NULL;
  906. __skb_queue_tail(&mp->outqueue, skb);
  907. mp->outbytes += skb->len;
  908. invoke_send = true;
  909. }
  910. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
  911. if (skb) {
  912. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  913. *(skb_put(skb, 1)) = ch;
  914. mp->outskb = skb;
  915. } else {
  916. printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
  917. ret = 0;
  918. }
  919. unlock_out:
  920. spin_unlock_bh(&mp->outlock);
  921. if (invoke_send)
  922. handle_minor_send(mp);
  923. return ret;
  924. }
  925. static void capinc_tty_flush_chars(struct tty_struct *tty)
  926. {
  927. struct capiminor *mp = tty->driver_data;
  928. struct sk_buff *skb;
  929. pr_debug("capinc_tty_flush_chars\n");
  930. spin_lock_bh(&mp->outlock);
  931. skb = mp->outskb;
  932. if (skb) {
  933. mp->outskb = NULL;
  934. __skb_queue_tail(&mp->outqueue, skb);
  935. mp->outbytes += skb->len;
  936. spin_unlock_bh(&mp->outlock);
  937. handle_minor_send(mp);
  938. } else
  939. spin_unlock_bh(&mp->outlock);
  940. handle_minor_recv(mp);
  941. }
  942. static int capinc_tty_write_room(struct tty_struct *tty)
  943. {
  944. struct capiminor *mp = tty->driver_data;
  945. int room;
  946. room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
  947. room *= CAPI_MAX_BLKSIZE;
  948. pr_debug("capinc_tty_write_room = %d\n", room);
  949. return room;
  950. }
  951. static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
  952. {
  953. struct capiminor *mp = tty->driver_data;
  954. pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
  955. mp->outbytes, mp->nack,
  956. skb_queue_len(&mp->outqueue),
  957. skb_queue_len(&mp->inqueue));
  958. return mp->outbytes;
  959. }
  960. static int capinc_tty_ioctl(struct tty_struct *tty,
  961. unsigned int cmd, unsigned long arg)
  962. {
  963. return -ENOIOCTLCMD;
  964. }
  965. static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  966. {
  967. pr_debug("capinc_tty_set_termios\n");
  968. }
  969. static void capinc_tty_throttle(struct tty_struct *tty)
  970. {
  971. struct capiminor *mp = tty->driver_data;
  972. pr_debug("capinc_tty_throttle\n");
  973. mp->ttyinstop = 1;
  974. }
  975. static void capinc_tty_unthrottle(struct tty_struct *tty)
  976. {
  977. struct capiminor *mp = tty->driver_data;
  978. pr_debug("capinc_tty_unthrottle\n");
  979. mp->ttyinstop = 0;
  980. handle_minor_recv(mp);
  981. }
  982. static void capinc_tty_stop(struct tty_struct *tty)
  983. {
  984. struct capiminor *mp = tty->driver_data;
  985. pr_debug("capinc_tty_stop\n");
  986. mp->ttyoutstop = 1;
  987. }
  988. static void capinc_tty_start(struct tty_struct *tty)
  989. {
  990. struct capiminor *mp = tty->driver_data;
  991. pr_debug("capinc_tty_start\n");
  992. mp->ttyoutstop = 0;
  993. handle_minor_send(mp);
  994. }
  995. static void capinc_tty_hangup(struct tty_struct *tty)
  996. {
  997. struct capiminor *mp = tty->driver_data;
  998. pr_debug("capinc_tty_hangup\n");
  999. tty_port_hangup(&mp->port);
  1000. }
  1001. static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
  1002. {
  1003. pr_debug("capinc_tty_break_ctl(%d)\n", state);
  1004. return 0;
  1005. }
  1006. static void capinc_tty_flush_buffer(struct tty_struct *tty)
  1007. {
  1008. pr_debug("capinc_tty_flush_buffer\n");
  1009. }
  1010. static void capinc_tty_set_ldisc(struct tty_struct *tty)
  1011. {
  1012. pr_debug("capinc_tty_set_ldisc\n");
  1013. }
  1014. static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
  1015. {
  1016. pr_debug("capinc_tty_send_xchar(%d)\n", ch);
  1017. }
  1018. static const struct tty_operations capinc_ops = {
  1019. .open = capinc_tty_open,
  1020. .close = capinc_tty_close,
  1021. .write = capinc_tty_write,
  1022. .put_char = capinc_tty_put_char,
  1023. .flush_chars = capinc_tty_flush_chars,
  1024. .write_room = capinc_tty_write_room,
  1025. .chars_in_buffer = capinc_tty_chars_in_buffer,
  1026. .ioctl = capinc_tty_ioctl,
  1027. .set_termios = capinc_tty_set_termios,
  1028. .throttle = capinc_tty_throttle,
  1029. .unthrottle = capinc_tty_unthrottle,
  1030. .stop = capinc_tty_stop,
  1031. .start = capinc_tty_start,
  1032. .hangup = capinc_tty_hangup,
  1033. .break_ctl = capinc_tty_break_ctl,
  1034. .flush_buffer = capinc_tty_flush_buffer,
  1035. .set_ldisc = capinc_tty_set_ldisc,
  1036. .send_xchar = capinc_tty_send_xchar,
  1037. .install = capinc_tty_install,
  1038. .cleanup = capinc_tty_cleanup,
  1039. };
  1040. static int __init capinc_tty_init(void)
  1041. {
  1042. struct tty_driver *drv;
  1043. int err;
  1044. if (capi_ttyminors > CAPINC_MAX_PORTS)
  1045. capi_ttyminors = CAPINC_MAX_PORTS;
  1046. if (capi_ttyminors <= 0)
  1047. capi_ttyminors = CAPINC_NR_PORTS;
  1048. capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
  1049. GFP_KERNEL);
  1050. if (!capiminors)
  1051. return -ENOMEM;
  1052. drv = alloc_tty_driver(capi_ttyminors);
  1053. if (!drv) {
  1054. kfree(capiminors);
  1055. return -ENOMEM;
  1056. }
  1057. drv->driver_name = "capi_nc";
  1058. drv->name = "capi";
  1059. drv->major = 0;
  1060. drv->minor_start = 0;
  1061. drv->type = TTY_DRIVER_TYPE_SERIAL;
  1062. drv->subtype = SERIAL_TYPE_NORMAL;
  1063. drv->init_termios = tty_std_termios;
  1064. drv->init_termios.c_iflag = ICRNL;
  1065. drv->init_termios.c_oflag = OPOST | ONLCR;
  1066. drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1067. drv->init_termios.c_lflag = 0;
  1068. drv->flags =
  1069. TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
  1070. TTY_DRIVER_DYNAMIC_DEV;
  1071. tty_set_operations(drv, &capinc_ops);
  1072. err = tty_register_driver(drv);
  1073. if (err) {
  1074. put_tty_driver(drv);
  1075. kfree(capiminors);
  1076. printk(KERN_ERR "Couldn't register capi_nc driver\n");
  1077. return err;
  1078. }
  1079. capinc_tty_driver = drv;
  1080. return 0;
  1081. }
  1082. static void __exit capinc_tty_exit(void)
  1083. {
  1084. tty_unregister_driver(capinc_tty_driver);
  1085. put_tty_driver(capinc_tty_driver);
  1086. kfree(capiminors);
  1087. }
  1088. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1089. static inline int capinc_tty_init(void)
  1090. {
  1091. return 0;
  1092. }
  1093. static inline void capinc_tty_exit(void) { }
  1094. #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1095. /* -------- /proc functions ----------------------------------------- */
  1096. /*
  1097. * /proc/capi/capi20:
  1098. * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
  1099. */
  1100. static int capi20_proc_show(struct seq_file *m, void *v)
  1101. {
  1102. struct capidev *cdev;
  1103. struct list_head *l;
  1104. mutex_lock(&capidev_list_lock);
  1105. list_for_each(l, &capidev_list) {
  1106. cdev = list_entry(l, struct capidev, list);
  1107. seq_printf(m, "0 %d %lu %lu %lu %lu\n",
  1108. cdev->ap.applid,
  1109. cdev->ap.nrecvctlpkt,
  1110. cdev->ap.nrecvdatapkt,
  1111. cdev->ap.nsentctlpkt,
  1112. cdev->ap.nsentdatapkt);
  1113. }
  1114. mutex_unlock(&capidev_list_lock);
  1115. return 0;
  1116. }
  1117. static int capi20_proc_open(struct inode *inode, struct file *file)
  1118. {
  1119. return single_open(file, capi20_proc_show, NULL);
  1120. }
  1121. static const struct file_operations capi20_proc_fops = {
  1122. .owner = THIS_MODULE,
  1123. .open = capi20_proc_open,
  1124. .read = seq_read,
  1125. .llseek = seq_lseek,
  1126. .release = single_release,
  1127. };
  1128. /*
  1129. * /proc/capi/capi20ncci:
  1130. * applid ncci
  1131. */
  1132. static int capi20ncci_proc_show(struct seq_file *m, void *v)
  1133. {
  1134. struct capidev *cdev;
  1135. struct capincci *np;
  1136. mutex_lock(&capidev_list_lock);
  1137. list_for_each_entry(cdev, &capidev_list, list) {
  1138. mutex_lock(&cdev->lock);
  1139. list_for_each_entry(np, &cdev->nccis, list)
  1140. seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
  1141. mutex_unlock(&cdev->lock);
  1142. }
  1143. mutex_unlock(&capidev_list_lock);
  1144. return 0;
  1145. }
  1146. static int capi20ncci_proc_open(struct inode *inode, struct file *file)
  1147. {
  1148. return single_open(file, capi20ncci_proc_show, NULL);
  1149. }
  1150. static const struct file_operations capi20ncci_proc_fops = {
  1151. .owner = THIS_MODULE,
  1152. .open = capi20ncci_proc_open,
  1153. .read = seq_read,
  1154. .llseek = seq_lseek,
  1155. .release = single_release,
  1156. };
  1157. static void __init proc_init(void)
  1158. {
  1159. proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
  1160. proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
  1161. }
  1162. static void __exit proc_exit(void)
  1163. {
  1164. remove_proc_entry("capi/capi20", NULL);
  1165. remove_proc_entry("capi/capi20ncci", NULL);
  1166. }
  1167. /* -------- init function and module interface ---------------------- */
  1168. static int __init capi_init(void)
  1169. {
  1170. const char *compileinfo;
  1171. int major_ret;
  1172. major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
  1173. if (major_ret < 0) {
  1174. printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
  1175. return major_ret;
  1176. }
  1177. capi_class = class_create(THIS_MODULE, "capi");
  1178. if (IS_ERR(capi_class)) {
  1179. unregister_chrdev(capi_major, "capi20");
  1180. return PTR_ERR(capi_class);
  1181. }
  1182. device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
  1183. if (capinc_tty_init() < 0) {
  1184. device_destroy(capi_class, MKDEV(capi_major, 0));
  1185. class_destroy(capi_class);
  1186. unregister_chrdev(capi_major, "capi20");
  1187. return -ENOMEM;
  1188. }
  1189. proc_init();
  1190. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  1191. compileinfo = " (middleware)";
  1192. #else
  1193. compileinfo = " (no middleware)";
  1194. #endif
  1195. printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
  1196. capi_major, compileinfo);
  1197. return 0;
  1198. }
  1199. static void __exit capi_exit(void)
  1200. {
  1201. proc_exit();
  1202. device_destroy(capi_class, MKDEV(capi_major, 0));
  1203. class_destroy(capi_class);
  1204. unregister_chrdev(capi_major, "capi20");
  1205. capinc_tty_exit();
  1206. }
  1207. module_init(capi_init);
  1208. module_exit(capi_exit);