capi.c 36 KB

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