capi.c 37 KB

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