capi.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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. unsigned int minor;
  67. struct capi20_appl *ap;
  68. u32 ncci;
  69. atomic_t datahandle;
  70. atomic_t msgid;
  71. struct tty_port port;
  72. int ttyinstop;
  73. int ttyoutstop;
  74. struct sk_buff_head inqueue;
  75. struct sk_buff_head outqueue;
  76. int outbytes;
  77. struct sk_buff *outskb;
  78. spinlock_t outlock;
  79. /* transmit path */
  80. struct list_head ackqueue;
  81. int nack;
  82. spinlock_t ackqlock;
  83. };
  84. struct capincci {
  85. struct list_head list;
  86. u32 ncci;
  87. struct capidev *cdev;
  88. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  89. struct capiminor *minorp;
  90. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  91. };
  92. struct capidev {
  93. struct list_head list;
  94. struct capi20_appl ap;
  95. u16 errcode;
  96. unsigned userflags;
  97. struct sk_buff_head recvqueue;
  98. wait_queue_head_t recvwait;
  99. struct list_head nccis;
  100. struct mutex lock;
  101. };
  102. /* -------- global variables ---------------------------------------- */
  103. static DEFINE_MUTEX(capidev_list_lock);
  104. static LIST_HEAD(capidev_list);
  105. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  106. static DEFINE_SPINLOCK(capiminors_lock);
  107. static struct capiminor **capiminors;
  108. static struct tty_driver *capinc_tty_driver;
  109. /* -------- datahandles --------------------------------------------- */
  110. static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
  111. {
  112. struct ackqueue_entry *n;
  113. n = kmalloc(sizeof(*n), GFP_ATOMIC);
  114. if (unlikely(!n)) {
  115. printk(KERN_ERR "capi: alloc datahandle failed\n");
  116. return -1;
  117. }
  118. n->datahandle = datahandle;
  119. INIT_LIST_HEAD(&n->list);
  120. spin_lock_bh(&mp->ackqlock);
  121. list_add_tail(&n->list, &mp->ackqueue);
  122. mp->nack++;
  123. spin_unlock_bh(&mp->ackqlock);
  124. return 0;
  125. }
  126. static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
  127. {
  128. struct ackqueue_entry *p, *tmp;
  129. spin_lock_bh(&mp->ackqlock);
  130. list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
  131. if (p->datahandle == datahandle) {
  132. list_del(&p->list);
  133. mp->nack--;
  134. spin_unlock_bh(&mp->ackqlock);
  135. kfree(p);
  136. return 0;
  137. }
  138. }
  139. spin_unlock_bh(&mp->ackqlock);
  140. return -1;
  141. }
  142. static void capiminor_del_all_ack(struct capiminor *mp)
  143. {
  144. struct ackqueue_entry *p, *tmp;
  145. list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
  146. list_del(&p->list);
  147. kfree(p);
  148. mp->nack--;
  149. }
  150. }
  151. /* -------- struct capiminor ---------------------------------------- */
  152. static void capiminor_destroy(struct tty_port *port)
  153. {
  154. struct capiminor *mp = container_of(port, struct capiminor, port);
  155. kfree_skb(mp->outskb);
  156. skb_queue_purge(&mp->inqueue);
  157. skb_queue_purge(&mp->outqueue);
  158. capiminor_del_all_ack(mp);
  159. kfree(mp);
  160. }
  161. static const struct tty_port_operations capiminor_port_ops = {
  162. .destruct = capiminor_destroy,
  163. };
  164. static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
  165. {
  166. struct capiminor *mp;
  167. struct device *dev;
  168. unsigned int minor;
  169. mp = kzalloc(sizeof(*mp), GFP_KERNEL);
  170. if (!mp) {
  171. printk(KERN_ERR "capi: can't alloc capiminor\n");
  172. return NULL;
  173. }
  174. mp->ap = ap;
  175. mp->ncci = ncci;
  176. INIT_LIST_HEAD(&mp->ackqueue);
  177. spin_lock_init(&mp->ackqlock);
  178. skb_queue_head_init(&mp->inqueue);
  179. skb_queue_head_init(&mp->outqueue);
  180. spin_lock_init(&mp->outlock);
  181. tty_port_init(&mp->port);
  182. mp->port.ops = &capiminor_port_ops;
  183. /* Allocate the least unused minor number. */
  184. spin_lock(&capiminors_lock);
  185. for (minor = 0; minor < capi_ttyminors; minor++)
  186. if (!capiminors[minor]) {
  187. capiminors[minor] = mp;
  188. break;
  189. }
  190. spin_unlock(&capiminors_lock);
  191. if (minor == capi_ttyminors) {
  192. printk(KERN_NOTICE "capi: out of minors\n");
  193. goto err_out1;
  194. }
  195. mp->minor = minor;
  196. dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
  197. NULL);
  198. if (IS_ERR(dev))
  199. goto err_out2;
  200. return mp;
  201. err_out2:
  202. spin_lock(&capiminors_lock);
  203. capiminors[minor] = NULL;
  204. spin_unlock(&capiminors_lock);
  205. err_out1:
  206. tty_port_put(&mp->port);
  207. return NULL;
  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. tty_port_get(&mp->port);
  216. spin_unlock(&capiminors_lock);
  217. return mp;
  218. }
  219. static inline void capiminor_put(struct capiminor *mp)
  220. {
  221. tty_port_put(&mp->port);
  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 capiminor *mp;
  463. u16 datahandle;
  464. struct capincci *np;
  465. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  466. mutex_lock(&cdev->lock);
  467. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
  468. u16 info = CAPIMSG_U16(skb->data, 12); // Info field
  469. if ((info & 0xff00) == 0)
  470. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  471. }
  472. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
  473. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  474. if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
  475. skb_queue_tail(&cdev->recvqueue, skb);
  476. wake_up_interruptible(&cdev->recvwait);
  477. goto unlock_out;
  478. }
  479. #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
  480. skb_queue_tail(&cdev->recvqueue, skb);
  481. wake_up_interruptible(&cdev->recvwait);
  482. #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  483. np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
  484. if (!np) {
  485. printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
  486. skb_queue_tail(&cdev->recvqueue, skb);
  487. wake_up_interruptible(&cdev->recvwait);
  488. goto unlock_out;
  489. }
  490. mp = np->minorp;
  491. if (!mp) {
  492. skb_queue_tail(&cdev->recvqueue, skb);
  493. wake_up_interruptible(&cdev->recvwait);
  494. goto unlock_out;
  495. }
  496. if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
  497. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
  498. pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
  499. datahandle, skb->len-CAPIMSG_LEN(skb->data));
  500. skb_queue_tail(&mp->inqueue, skb);
  501. handle_minor_recv(mp);
  502. } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
  503. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
  504. pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
  505. datahandle,
  506. CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
  507. kfree_skb(skb);
  508. capiminor_del_ack(mp, datahandle);
  509. tty_port_tty_wakeup(&mp->port);
  510. handle_minor_send(mp);
  511. } else {
  512. /* ups, let capi application handle it :-) */
  513. skb_queue_tail(&cdev->recvqueue, skb);
  514. wake_up_interruptible(&cdev->recvwait);
  515. }
  516. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  517. unlock_out:
  518. mutex_unlock(&cdev->lock);
  519. }
  520. /* -------- file_operations for capidev ----------------------------- */
  521. static ssize_t
  522. capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  523. {
  524. struct capidev *cdev = file->private_data;
  525. struct sk_buff *skb;
  526. size_t copied;
  527. int err;
  528. if (!cdev->ap.applid)
  529. return -ENODEV;
  530. skb = skb_dequeue(&cdev->recvqueue);
  531. if (!skb) {
  532. if (file->f_flags & O_NONBLOCK)
  533. return -EAGAIN;
  534. err = wait_event_interruptible(cdev->recvwait,
  535. (skb = skb_dequeue(&cdev->recvqueue)));
  536. if (err)
  537. return err;
  538. }
  539. if (skb->len > count) {
  540. skb_queue_head(&cdev->recvqueue, skb);
  541. return -EMSGSIZE;
  542. }
  543. if (copy_to_user(buf, skb->data, skb->len)) {
  544. skb_queue_head(&cdev->recvqueue, skb);
  545. return -EFAULT;
  546. }
  547. copied = skb->len;
  548. kfree_skb(skb);
  549. return copied;
  550. }
  551. static ssize_t
  552. capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  553. {
  554. struct capidev *cdev = file->private_data;
  555. struct sk_buff *skb;
  556. u16 mlen;
  557. if (!cdev->ap.applid)
  558. return -ENODEV;
  559. skb = alloc_skb(count, GFP_USER);
  560. if (!skb)
  561. return -ENOMEM;
  562. if (copy_from_user(skb_put(skb, count), buf, count)) {
  563. kfree_skb(skb);
  564. return -EFAULT;
  565. }
  566. mlen = CAPIMSG_LEN(skb->data);
  567. if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
  568. if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
  569. kfree_skb(skb);
  570. return -EINVAL;
  571. }
  572. } else {
  573. if (mlen != count) {
  574. kfree_skb(skb);
  575. return -EINVAL;
  576. }
  577. }
  578. CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
  579. if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
  580. mutex_lock(&cdev->lock);
  581. capincci_free(cdev, CAPIMSG_NCCI(skb->data));
  582. mutex_unlock(&cdev->lock);
  583. }
  584. cdev->errcode = capi20_put_message(&cdev->ap, skb);
  585. if (cdev->errcode) {
  586. kfree_skb(skb);
  587. return -EIO;
  588. }
  589. return count;
  590. }
  591. static unsigned int
  592. capi_poll(struct file *file, poll_table *wait)
  593. {
  594. struct capidev *cdev = file->private_data;
  595. unsigned int mask = 0;
  596. if (!cdev->ap.applid)
  597. return POLLERR;
  598. poll_wait(file, &(cdev->recvwait), wait);
  599. mask = POLLOUT | POLLWRNORM;
  600. if (!skb_queue_empty(&cdev->recvqueue))
  601. mask |= POLLIN | POLLRDNORM;
  602. return mask;
  603. }
  604. static int
  605. capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  606. {
  607. struct capidev *cdev = file->private_data;
  608. capi_ioctl_struct data;
  609. int retval = -EINVAL;
  610. void __user *argp = (void __user *)arg;
  611. switch (cmd) {
  612. case CAPI_REGISTER:
  613. mutex_lock(&cdev->lock);
  614. if (cdev->ap.applid) {
  615. retval = -EEXIST;
  616. goto register_out;
  617. }
  618. if (copy_from_user(&cdev->ap.rparam, argp,
  619. sizeof(struct capi_register_params))) {
  620. retval = -EFAULT;
  621. goto register_out;
  622. }
  623. cdev->ap.private = cdev;
  624. cdev->ap.recv_message = capi_recv_message;
  625. cdev->errcode = capi20_register(&cdev->ap);
  626. retval = (int)cdev->ap.applid;
  627. if (cdev->errcode) {
  628. cdev->ap.applid = 0;
  629. retval = -EIO;
  630. }
  631. register_out:
  632. mutex_unlock(&cdev->lock);
  633. return retval;
  634. case CAPI_GET_VERSION:
  635. if (copy_from_user(&data.contr, argp,
  636. sizeof(data.contr)))
  637. return -EFAULT;
  638. cdev->errcode = capi20_get_version(data.contr, &data.version);
  639. if (cdev->errcode)
  640. return -EIO;
  641. if (copy_to_user(argp, &data.version,
  642. sizeof(data.version)))
  643. return -EFAULT;
  644. return 0;
  645. case CAPI_GET_SERIAL:
  646. if (copy_from_user(&data.contr, argp,
  647. sizeof(data.contr)))
  648. return -EFAULT;
  649. cdev->errcode = capi20_get_serial(data.contr, data.serial);
  650. if (cdev->errcode)
  651. return -EIO;
  652. if (copy_to_user(argp, data.serial,
  653. sizeof(data.serial)))
  654. return -EFAULT;
  655. return 0;
  656. case CAPI_GET_PROFILE:
  657. if (copy_from_user(&data.contr, argp,
  658. sizeof(data.contr)))
  659. return -EFAULT;
  660. if (data.contr == 0) {
  661. cdev->errcode = capi20_get_profile(data.contr, &data.profile);
  662. if (cdev->errcode)
  663. return -EIO;
  664. retval = copy_to_user(argp,
  665. &data.profile.ncontroller,
  666. sizeof(data.profile.ncontroller));
  667. } else {
  668. cdev->errcode = capi20_get_profile(data.contr, &data.profile);
  669. if (cdev->errcode)
  670. return -EIO;
  671. retval = copy_to_user(argp, &data.profile,
  672. sizeof(data.profile));
  673. }
  674. if (retval)
  675. return -EFAULT;
  676. return 0;
  677. case CAPI_GET_MANUFACTURER:
  678. if (copy_from_user(&data.contr, argp,
  679. sizeof(data.contr)))
  680. return -EFAULT;
  681. cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
  682. if (cdev->errcode)
  683. return -EIO;
  684. if (copy_to_user(argp, data.manufacturer,
  685. sizeof(data.manufacturer)))
  686. return -EFAULT;
  687. return 0;
  688. case CAPI_GET_ERRCODE:
  689. data.errcode = cdev->errcode;
  690. cdev->errcode = CAPI_NOERROR;
  691. if (arg) {
  692. if (copy_to_user(argp, &data.errcode,
  693. sizeof(data.errcode)))
  694. return -EFAULT;
  695. }
  696. return data.errcode;
  697. case CAPI_INSTALLED:
  698. if (capi20_isinstalled() == CAPI_NOERROR)
  699. return 0;
  700. return -ENXIO;
  701. case CAPI_MANUFACTURER_CMD: {
  702. struct capi_manufacturer_cmd mcmd;
  703. if (!capable(CAP_SYS_ADMIN))
  704. return -EPERM;
  705. if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
  706. return -EFAULT;
  707. return capi20_manufacturer(mcmd.cmd, mcmd.data);
  708. }
  709. case CAPI_SET_FLAGS:
  710. case CAPI_CLR_FLAGS: {
  711. unsigned userflags;
  712. if (copy_from_user(&userflags, argp, sizeof(userflags)))
  713. return -EFAULT;
  714. mutex_lock(&cdev->lock);
  715. if (cmd == CAPI_SET_FLAGS)
  716. cdev->userflags |= userflags;
  717. else
  718. cdev->userflags &= ~userflags;
  719. mutex_unlock(&cdev->lock);
  720. return 0;
  721. }
  722. case CAPI_GET_FLAGS:
  723. if (copy_to_user(argp, &cdev->userflags,
  724. sizeof(cdev->userflags)))
  725. return -EFAULT;
  726. return 0;
  727. #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
  728. case CAPI_NCCI_OPENCOUNT:
  729. return 0;
  730. #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  731. case CAPI_NCCI_OPENCOUNT: {
  732. struct capincci *nccip;
  733. unsigned ncci;
  734. int count = 0;
  735. if (copy_from_user(&ncci, argp, sizeof(ncci)))
  736. return -EFAULT;
  737. mutex_lock(&cdev->lock);
  738. nccip = capincci_find(cdev, (u32)ncci);
  739. if (nccip)
  740. count = capincci_minor_opencount(nccip);
  741. mutex_unlock(&cdev->lock);
  742. return count;
  743. }
  744. case CAPI_NCCI_GETUNIT: {
  745. struct capincci *nccip;
  746. struct capiminor *mp;
  747. unsigned ncci;
  748. int unit = -ESRCH;
  749. if (copy_from_user(&ncci, argp, sizeof(ncci)))
  750. return -EFAULT;
  751. mutex_lock(&cdev->lock);
  752. nccip = capincci_find(cdev, (u32)ncci);
  753. if (nccip) {
  754. mp = nccip->minorp;
  755. if (mp)
  756. unit = mp->minor;
  757. }
  758. mutex_unlock(&cdev->lock);
  759. return unit;
  760. }
  761. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  762. default:
  763. return -EINVAL;
  764. }
  765. }
  766. static long
  767. capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  768. {
  769. int ret;
  770. mutex_lock(&capi_mutex);
  771. ret = capi_ioctl(file, cmd, arg);
  772. mutex_unlock(&capi_mutex);
  773. return ret;
  774. }
  775. static int capi_open(struct inode *inode, struct file *file)
  776. {
  777. struct capidev *cdev;
  778. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  779. if (!cdev)
  780. return -ENOMEM;
  781. mutex_init(&cdev->lock);
  782. skb_queue_head_init(&cdev->recvqueue);
  783. init_waitqueue_head(&cdev->recvwait);
  784. INIT_LIST_HEAD(&cdev->nccis);
  785. file->private_data = cdev;
  786. mutex_lock(&capidev_list_lock);
  787. list_add_tail(&cdev->list, &capidev_list);
  788. mutex_unlock(&capidev_list_lock);
  789. return nonseekable_open(inode, file);
  790. }
  791. static int capi_release(struct inode *inode, struct file *file)
  792. {
  793. struct capidev *cdev = file->private_data;
  794. mutex_lock(&capidev_list_lock);
  795. list_del(&cdev->list);
  796. mutex_unlock(&capidev_list_lock);
  797. if (cdev->ap.applid)
  798. capi20_release(&cdev->ap);
  799. skb_queue_purge(&cdev->recvqueue);
  800. capincci_free(cdev, 0xffffffff);
  801. kfree(cdev);
  802. return 0;
  803. }
  804. static const struct file_operations capi_fops =
  805. {
  806. .owner = THIS_MODULE,
  807. .llseek = no_llseek,
  808. .read = capi_read,
  809. .write = capi_write,
  810. .poll = capi_poll,
  811. .unlocked_ioctl = capi_unlocked_ioctl,
  812. .open = capi_open,
  813. .release = capi_release,
  814. };
  815. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  816. /* -------- tty_operations for capincci ----------------------------- */
  817. static int
  818. capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  819. {
  820. struct capiminor *mp = capiminor_get(tty->index);
  821. int ret = tty_standard_install(driver, tty);
  822. if (ret == 0)
  823. tty->driver_data = mp;
  824. else
  825. capiminor_put(mp);
  826. return ret;
  827. }
  828. static void capinc_tty_cleanup(struct tty_struct *tty)
  829. {
  830. struct capiminor *mp = tty->driver_data;
  831. tty->driver_data = NULL;
  832. capiminor_put(mp);
  833. }
  834. static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
  835. {
  836. struct capiminor *mp = tty->driver_data;
  837. int err;
  838. err = tty_port_open(&mp->port, tty, filp);
  839. if (err)
  840. return err;
  841. handle_minor_recv(mp);
  842. return 0;
  843. }
  844. static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
  845. {
  846. struct capiminor *mp = tty->driver_data;
  847. tty_port_close(&mp->port, tty, filp);
  848. }
  849. static int capinc_tty_write(struct tty_struct *tty,
  850. const unsigned char *buf, int count)
  851. {
  852. struct capiminor *mp = tty->driver_data;
  853. struct sk_buff *skb;
  854. pr_debug("capinc_tty_write(count=%d)\n", count);
  855. spin_lock_bh(&mp->outlock);
  856. skb = mp->outskb;
  857. if (skb) {
  858. mp->outskb = NULL;
  859. __skb_queue_tail(&mp->outqueue, skb);
  860. mp->outbytes += skb->len;
  861. }
  862. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
  863. if (!skb) {
  864. printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
  865. spin_unlock_bh(&mp->outlock);
  866. return -ENOMEM;
  867. }
  868. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  869. memcpy(skb_put(skb, count), buf, count);
  870. __skb_queue_tail(&mp->outqueue, skb);
  871. mp->outbytes += skb->len;
  872. spin_unlock_bh(&mp->outlock);
  873. handle_minor_send(mp);
  874. return count;
  875. }
  876. static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
  877. {
  878. struct capiminor *mp = tty->driver_data;
  879. bool invoke_send = false;
  880. struct sk_buff *skb;
  881. int ret = 1;
  882. pr_debug("capinc_put_char(%u)\n", ch);
  883. spin_lock_bh(&mp->outlock);
  884. skb = mp->outskb;
  885. if (skb) {
  886. if (skb_tailroom(skb) > 0) {
  887. *(skb_put(skb, 1)) = ch;
  888. goto unlock_out;
  889. }
  890. mp->outskb = NULL;
  891. __skb_queue_tail(&mp->outqueue, skb);
  892. mp->outbytes += skb->len;
  893. invoke_send = true;
  894. }
  895. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
  896. if (skb) {
  897. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  898. *(skb_put(skb, 1)) = ch;
  899. mp->outskb = skb;
  900. } else {
  901. printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
  902. ret = 0;
  903. }
  904. unlock_out:
  905. spin_unlock_bh(&mp->outlock);
  906. if (invoke_send)
  907. handle_minor_send(mp);
  908. return ret;
  909. }
  910. static void capinc_tty_flush_chars(struct tty_struct *tty)
  911. {
  912. struct capiminor *mp = tty->driver_data;
  913. struct sk_buff *skb;
  914. pr_debug("capinc_tty_flush_chars\n");
  915. spin_lock_bh(&mp->outlock);
  916. skb = mp->outskb;
  917. if (skb) {
  918. mp->outskb = NULL;
  919. __skb_queue_tail(&mp->outqueue, skb);
  920. mp->outbytes += skb->len;
  921. spin_unlock_bh(&mp->outlock);
  922. handle_minor_send(mp);
  923. } else
  924. spin_unlock_bh(&mp->outlock);
  925. handle_minor_recv(mp);
  926. }
  927. static int capinc_tty_write_room(struct tty_struct *tty)
  928. {
  929. struct capiminor *mp = tty->driver_data;
  930. int room;
  931. room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
  932. room *= CAPI_MAX_BLKSIZE;
  933. pr_debug("capinc_tty_write_room = %d\n", room);
  934. return room;
  935. }
  936. static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
  937. {
  938. struct capiminor *mp = tty->driver_data;
  939. pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
  940. mp->outbytes, mp->nack,
  941. skb_queue_len(&mp->outqueue),
  942. skb_queue_len(&mp->inqueue));
  943. return mp->outbytes;
  944. }
  945. static int capinc_tty_ioctl(struct tty_struct *tty,
  946. unsigned int cmd, unsigned long arg)
  947. {
  948. return -ENOIOCTLCMD;
  949. }
  950. static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  951. {
  952. pr_debug("capinc_tty_set_termios\n");
  953. }
  954. static void capinc_tty_throttle(struct tty_struct *tty)
  955. {
  956. struct capiminor *mp = tty->driver_data;
  957. pr_debug("capinc_tty_throttle\n");
  958. mp->ttyinstop = 1;
  959. }
  960. static void capinc_tty_unthrottle(struct tty_struct *tty)
  961. {
  962. struct capiminor *mp = tty->driver_data;
  963. pr_debug("capinc_tty_unthrottle\n");
  964. mp->ttyinstop = 0;
  965. handle_minor_recv(mp);
  966. }
  967. static void capinc_tty_stop(struct tty_struct *tty)
  968. {
  969. struct capiminor *mp = tty->driver_data;
  970. pr_debug("capinc_tty_stop\n");
  971. mp->ttyoutstop = 1;
  972. }
  973. static void capinc_tty_start(struct tty_struct *tty)
  974. {
  975. struct capiminor *mp = tty->driver_data;
  976. pr_debug("capinc_tty_start\n");
  977. mp->ttyoutstop = 0;
  978. handle_minor_send(mp);
  979. }
  980. static void capinc_tty_hangup(struct tty_struct *tty)
  981. {
  982. struct capiminor *mp = tty->driver_data;
  983. pr_debug("capinc_tty_hangup\n");
  984. tty_port_hangup(&mp->port);
  985. }
  986. static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
  987. {
  988. pr_debug("capinc_tty_break_ctl(%d)\n", state);
  989. return 0;
  990. }
  991. static void capinc_tty_flush_buffer(struct tty_struct *tty)
  992. {
  993. pr_debug("capinc_tty_flush_buffer\n");
  994. }
  995. static void capinc_tty_set_ldisc(struct tty_struct *tty)
  996. {
  997. pr_debug("capinc_tty_set_ldisc\n");
  998. }
  999. static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
  1000. {
  1001. pr_debug("capinc_tty_send_xchar(%d)\n", ch);
  1002. }
  1003. static const struct tty_operations capinc_ops = {
  1004. .open = capinc_tty_open,
  1005. .close = capinc_tty_close,
  1006. .write = capinc_tty_write,
  1007. .put_char = capinc_tty_put_char,
  1008. .flush_chars = capinc_tty_flush_chars,
  1009. .write_room = capinc_tty_write_room,
  1010. .chars_in_buffer = capinc_tty_chars_in_buffer,
  1011. .ioctl = capinc_tty_ioctl,
  1012. .set_termios = capinc_tty_set_termios,
  1013. .throttle = capinc_tty_throttle,
  1014. .unthrottle = capinc_tty_unthrottle,
  1015. .stop = capinc_tty_stop,
  1016. .start = capinc_tty_start,
  1017. .hangup = capinc_tty_hangup,
  1018. .break_ctl = capinc_tty_break_ctl,
  1019. .flush_buffer = capinc_tty_flush_buffer,
  1020. .set_ldisc = capinc_tty_set_ldisc,
  1021. .send_xchar = capinc_tty_send_xchar,
  1022. .install = capinc_tty_install,
  1023. .cleanup = capinc_tty_cleanup,
  1024. };
  1025. static int __init capinc_tty_init(void)
  1026. {
  1027. struct tty_driver *drv;
  1028. int err;
  1029. if (capi_ttyminors > CAPINC_MAX_PORTS)
  1030. capi_ttyminors = CAPINC_MAX_PORTS;
  1031. if (capi_ttyminors <= 0)
  1032. capi_ttyminors = CAPINC_NR_PORTS;
  1033. capiminors = kzalloc(sizeof(struct capiminor *) * capi_ttyminors,
  1034. GFP_KERNEL);
  1035. if (!capiminors)
  1036. return -ENOMEM;
  1037. drv = alloc_tty_driver(capi_ttyminors);
  1038. if (!drv) {
  1039. kfree(capiminors);
  1040. return -ENOMEM;
  1041. }
  1042. drv->driver_name = "capi_nc";
  1043. drv->name = "capi!";
  1044. drv->major = 0;
  1045. drv->minor_start = 0;
  1046. drv->type = TTY_DRIVER_TYPE_SERIAL;
  1047. drv->subtype = SERIAL_TYPE_NORMAL;
  1048. drv->init_termios = tty_std_termios;
  1049. drv->init_termios.c_iflag = ICRNL;
  1050. drv->init_termios.c_oflag = OPOST | ONLCR;
  1051. drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1052. drv->init_termios.c_lflag = 0;
  1053. drv->flags =
  1054. TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
  1055. TTY_DRIVER_DYNAMIC_DEV;
  1056. tty_set_operations(drv, &capinc_ops);
  1057. err = tty_register_driver(drv);
  1058. if (err) {
  1059. put_tty_driver(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. put_tty_driver(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 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. static int capi20_proc_open(struct inode *inode, struct file *file)
  1103. {
  1104. return single_open(file, capi20_proc_show, NULL);
  1105. }
  1106. static const struct file_operations capi20_proc_fops = {
  1107. .owner = THIS_MODULE,
  1108. .open = capi20_proc_open,
  1109. .read = seq_read,
  1110. .llseek = seq_lseek,
  1111. .release = single_release,
  1112. };
  1113. /*
  1114. * /proc/capi/capi20ncci:
  1115. * applid ncci
  1116. */
  1117. static int capi20ncci_proc_show(struct seq_file *m, void *v)
  1118. {
  1119. struct capidev *cdev;
  1120. struct capincci *np;
  1121. mutex_lock(&capidev_list_lock);
  1122. list_for_each_entry(cdev, &capidev_list, list) {
  1123. mutex_lock(&cdev->lock);
  1124. list_for_each_entry(np, &cdev->nccis, list)
  1125. seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
  1126. mutex_unlock(&cdev->lock);
  1127. }
  1128. mutex_unlock(&capidev_list_lock);
  1129. return 0;
  1130. }
  1131. static int capi20ncci_proc_open(struct inode *inode, struct file *file)
  1132. {
  1133. return single_open(file, capi20ncci_proc_show, NULL);
  1134. }
  1135. static const struct file_operations capi20ncci_proc_fops = {
  1136. .owner = THIS_MODULE,
  1137. .open = capi20ncci_proc_open,
  1138. .read = seq_read,
  1139. .llseek = seq_lseek,
  1140. .release = single_release,
  1141. };
  1142. static void __init proc_init(void)
  1143. {
  1144. proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
  1145. proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
  1146. }
  1147. static void __exit proc_exit(void)
  1148. {
  1149. remove_proc_entry("capi/capi20", NULL);
  1150. remove_proc_entry("capi/capi20ncci", NULL);
  1151. }
  1152. /* -------- init function and module interface ---------------------- */
  1153. static int __init capi_init(void)
  1154. {
  1155. const char *compileinfo;
  1156. int major_ret;
  1157. major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
  1158. if (major_ret < 0) {
  1159. printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
  1160. return major_ret;
  1161. }
  1162. capi_class = class_create(THIS_MODULE, "capi");
  1163. if (IS_ERR(capi_class)) {
  1164. unregister_chrdev(capi_major, "capi20");
  1165. return PTR_ERR(capi_class);
  1166. }
  1167. device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi20");
  1168. if (capinc_tty_init() < 0) {
  1169. device_destroy(capi_class, MKDEV(capi_major, 0));
  1170. class_destroy(capi_class);
  1171. unregister_chrdev(capi_major, "capi20");
  1172. return -ENOMEM;
  1173. }
  1174. proc_init();
  1175. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  1176. compileinfo = " (middleware)";
  1177. #else
  1178. compileinfo = " (no middleware)";
  1179. #endif
  1180. printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
  1181. capi_major, compileinfo);
  1182. return 0;
  1183. }
  1184. static void __exit capi_exit(void)
  1185. {
  1186. proc_exit();
  1187. device_destroy(capi_class, MKDEV(capi_major, 0));
  1188. class_destroy(capi_class);
  1189. unregister_chrdev(capi_major, "capi20");
  1190. capinc_tty_exit();
  1191. }
  1192. module_init(capi_init);
  1193. module_exit(capi_exit);