capi.c 36 KB

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