capi.c 36 KB

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