capi.c 32 KB

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