capi.c 36 KB

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