capi.c 37 KB

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