capi.c 37 KB

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