capi.c 36 KB

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