capi.c 37 KB

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