capi.c 38 KB

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