capi.c 32 KB

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