capi.c 32 KB

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