capi.c 35 KB

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