capi.c 32 KB

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