capi.c 38 KB

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