capi.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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/if_ppp.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_register_device(capinc_tty_driver, minor, NULL);
  188. if (IS_ERR(dev))
  189. goto err_out2;
  190. return mp;
  191. err_out2:
  192. spin_lock(&capiminors_lock);
  193. capiminors[minor] = NULL;
  194. spin_unlock(&capiminors_lock);
  195. err_out1:
  196. kfree(mp);
  197. return NULL;
  198. }
  199. static void capiminor_destroy(struct kref *kref)
  200. {
  201. struct capiminor *mp = container_of(kref, struct capiminor, kref);
  202. kfree_skb(mp->outskb);
  203. skb_queue_purge(&mp->inqueue);
  204. skb_queue_purge(&mp->outqueue);
  205. capiminor_del_all_ack(mp);
  206. kfree(mp);
  207. }
  208. static struct capiminor *capiminor_get(unsigned int minor)
  209. {
  210. struct capiminor *mp;
  211. spin_lock(&capiminors_lock);
  212. mp = capiminors[minor];
  213. if (mp)
  214. kref_get(&mp->kref);
  215. spin_unlock(&capiminors_lock);
  216. return mp;
  217. }
  218. static inline void capiminor_put(struct capiminor *mp)
  219. {
  220. kref_put(&mp->kref, capiminor_destroy);
  221. }
  222. static void capiminor_free(struct capiminor *mp)
  223. {
  224. tty_unregister_device(capinc_tty_driver, mp->minor);
  225. spin_lock(&capiminors_lock);
  226. capiminors[mp->minor] = NULL;
  227. spin_unlock(&capiminors_lock);
  228. capiminor_put(mp);
  229. }
  230. /* -------- struct capincci ----------------------------------------- */
  231. static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
  232. {
  233. if (cdev->userflags & CAPIFLAG_HIGHJACKING)
  234. np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
  235. }
  236. static void capincci_free_minor(struct capincci *np)
  237. {
  238. struct capiminor *mp = np->minorp;
  239. struct tty_struct *tty;
  240. if (mp) {
  241. tty = tty_port_tty_get(&mp->port);
  242. if (tty) {
  243. tty_vhangup(tty);
  244. tty_kref_put(tty);
  245. }
  246. capiminor_free(mp);
  247. }
  248. }
  249. static inline unsigned int capincci_minor_opencount(struct capincci *np)
  250. {
  251. struct capiminor *mp = np->minorp;
  252. unsigned int count = 0;
  253. struct tty_struct *tty;
  254. if (mp) {
  255. tty = tty_port_tty_get(&mp->port);
  256. if (tty) {
  257. count = tty->count;
  258. tty_kref_put(tty);
  259. }
  260. }
  261. return count;
  262. }
  263. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  264. static inline void
  265. capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
  266. static inline void capincci_free_minor(struct capincci *np) { }
  267. static inline unsigned int capincci_minor_opencount(struct capincci *np)
  268. {
  269. return 0;
  270. }
  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. static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
  295. {
  296. struct capincci *np;
  297. list_for_each_entry(np, &cdev->nccis, list)
  298. if (np->ncci == ncci)
  299. return np;
  300. return NULL;
  301. }
  302. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  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 tty_struct *tty;
  466. struct capiminor *mp;
  467. u16 datahandle;
  468. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  469. struct capincci *np;
  470. mutex_lock(&cdev->lock);
  471. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
  472. u16 info = CAPIMSG_U16(skb->data, 12); // Info field
  473. if ((info & 0xff00) == 0)
  474. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  475. }
  476. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
  477. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  478. if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
  479. skb_queue_tail(&cdev->recvqueue, skb);
  480. wake_up_interruptible(&cdev->recvwait);
  481. goto unlock_out;
  482. }
  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. #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
  491. skb_queue_tail(&cdev->recvqueue, skb);
  492. wake_up_interruptible(&cdev->recvwait);
  493. #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  494. mp = np->minorp;
  495. if (!mp) {
  496. skb_queue_tail(&cdev->recvqueue, skb);
  497. wake_up_interruptible(&cdev->recvwait);
  498. goto unlock_out;
  499. }
  500. if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
  501. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
  502. pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
  503. datahandle, skb->len-CAPIMSG_LEN(skb->data));
  504. skb_queue_tail(&mp->inqueue, skb);
  505. handle_minor_recv(mp);
  506. } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
  507. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
  508. pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
  509. datahandle,
  510. CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
  511. kfree_skb(skb);
  512. capiminor_del_ack(mp, datahandle);
  513. tty = tty_port_tty_get(&mp->port);
  514. if (tty) {
  515. tty_wakeup(tty);
  516. tty_kref_put(tty);
  517. }
  518. handle_minor_send(mp);
  519. } else {
  520. /* ups, let capi application handle it :-) */
  521. skb_queue_tail(&cdev->recvqueue, skb);
  522. wake_up_interruptible(&cdev->recvwait);
  523. }
  524. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  525. unlock_out:
  526. mutex_unlock(&cdev->lock);
  527. }
  528. /* -------- file_operations for capidev ----------------------------- */
  529. static ssize_t
  530. capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  531. {
  532. struct capidev *cdev = file->private_data;
  533. struct sk_buff *skb;
  534. size_t copied;
  535. int err;
  536. if (!cdev->ap.applid)
  537. return -ENODEV;
  538. skb = skb_dequeue(&cdev->recvqueue);
  539. if (!skb) {
  540. if (file->f_flags & O_NONBLOCK)
  541. return -EAGAIN;
  542. err = wait_event_interruptible(cdev->recvwait,
  543. (skb = skb_dequeue(&cdev->recvqueue)));
  544. if (err)
  545. return err;
  546. }
  547. if (skb->len > count) {
  548. skb_queue_head(&cdev->recvqueue, skb);
  549. return -EMSGSIZE;
  550. }
  551. if (copy_to_user(buf, skb->data, skb->len)) {
  552. skb_queue_head(&cdev->recvqueue, skb);
  553. return -EFAULT;
  554. }
  555. copied = skb->len;
  556. kfree_skb(skb);
  557. return copied;
  558. }
  559. static ssize_t
  560. capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  561. {
  562. struct capidev *cdev = file->private_data;
  563. struct sk_buff *skb;
  564. u16 mlen;
  565. if (!cdev->ap.applid)
  566. return -ENODEV;
  567. skb = alloc_skb(count, GFP_USER);
  568. if (!skb)
  569. return -ENOMEM;
  570. if (copy_from_user(skb_put(skb, count), buf, count)) {
  571. kfree_skb(skb);
  572. return -EFAULT;
  573. }
  574. mlen = CAPIMSG_LEN(skb->data);
  575. if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
  576. if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
  577. kfree_skb(skb);
  578. return -EINVAL;
  579. }
  580. } else {
  581. if (mlen != count) {
  582. kfree_skb(skb);
  583. return -EINVAL;
  584. }
  585. }
  586. CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
  587. if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
  588. mutex_lock(&cdev->lock);
  589. capincci_free(cdev, CAPIMSG_NCCI(skb->data));
  590. mutex_unlock(&cdev->lock);
  591. }
  592. cdev->errcode = capi20_put_message(&cdev->ap, skb);
  593. if (cdev->errcode) {
  594. kfree_skb(skb);
  595. return -EIO;
  596. }
  597. return count;
  598. }
  599. static unsigned int
  600. capi_poll(struct file *file, poll_table * wait)
  601. {
  602. struct capidev *cdev = file->private_data;
  603. unsigned int mask = 0;
  604. if (!cdev->ap.applid)
  605. return POLLERR;
  606. poll_wait(file, &(cdev->recvwait), wait);
  607. mask = POLLOUT | POLLWRNORM;
  608. if (!skb_queue_empty(&cdev->recvqueue))
  609. mask |= POLLIN | POLLRDNORM;
  610. return mask;
  611. }
  612. static int
  613. capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  614. {
  615. struct capidev *cdev = file->private_data;
  616. capi_ioctl_struct data;
  617. int retval = -EINVAL;
  618. void __user *argp = (void __user *)arg;
  619. switch (cmd) {
  620. case CAPI_REGISTER:
  621. mutex_lock(&cdev->lock);
  622. if (cdev->ap.applid) {
  623. retval = -EEXIST;
  624. goto register_out;
  625. }
  626. if (copy_from_user(&cdev->ap.rparam, argp,
  627. sizeof(struct capi_register_params))) {
  628. retval = -EFAULT;
  629. goto register_out;
  630. }
  631. cdev->ap.private = cdev;
  632. cdev->ap.recv_message = capi_recv_message;
  633. cdev->errcode = capi20_register(&cdev->ap);
  634. retval = (int)cdev->ap.applid;
  635. if (cdev->errcode) {
  636. cdev->ap.applid = 0;
  637. retval = -EIO;
  638. }
  639. register_out:
  640. mutex_unlock(&cdev->lock);
  641. return retval;
  642. case CAPI_GET_VERSION:
  643. {
  644. if (copy_from_user(&data.contr, argp,
  645. sizeof(data.contr)))
  646. return -EFAULT;
  647. cdev->errcode = capi20_get_version(data.contr, &data.version);
  648. if (cdev->errcode)
  649. return -EIO;
  650. if (copy_to_user(argp, &data.version,
  651. sizeof(data.version)))
  652. return -EFAULT;
  653. }
  654. return 0;
  655. case CAPI_GET_SERIAL:
  656. {
  657. if (copy_from_user(&data.contr, argp,
  658. sizeof(data.contr)))
  659. return -EFAULT;
  660. cdev->errcode = capi20_get_serial (data.contr, data.serial);
  661. if (cdev->errcode)
  662. return -EIO;
  663. if (copy_to_user(argp, data.serial,
  664. sizeof(data.serial)))
  665. return -EFAULT;
  666. }
  667. return 0;
  668. case CAPI_GET_PROFILE:
  669. {
  670. if (copy_from_user(&data.contr, argp,
  671. sizeof(data.contr)))
  672. return -EFAULT;
  673. if (data.contr == 0) {
  674. cdev->errcode = capi20_get_profile(data.contr, &data.profile);
  675. if (cdev->errcode)
  676. return -EIO;
  677. retval = copy_to_user(argp,
  678. &data.profile.ncontroller,
  679. sizeof(data.profile.ncontroller));
  680. } else {
  681. cdev->errcode = capi20_get_profile(data.contr, &data.profile);
  682. if (cdev->errcode)
  683. return -EIO;
  684. retval = copy_to_user(argp, &data.profile,
  685. sizeof(data.profile));
  686. }
  687. if (retval)
  688. return -EFAULT;
  689. }
  690. return 0;
  691. case CAPI_GET_MANUFACTURER:
  692. {
  693. if (copy_from_user(&data.contr, argp,
  694. sizeof(data.contr)))
  695. return -EFAULT;
  696. cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
  697. if (cdev->errcode)
  698. return -EIO;
  699. if (copy_to_user(argp, data.manufacturer,
  700. sizeof(data.manufacturer)))
  701. return -EFAULT;
  702. }
  703. return 0;
  704. case CAPI_GET_ERRCODE:
  705. data.errcode = cdev->errcode;
  706. cdev->errcode = CAPI_NOERROR;
  707. if (arg) {
  708. if (copy_to_user(argp, &data.errcode,
  709. sizeof(data.errcode)))
  710. return -EFAULT;
  711. }
  712. return data.errcode;
  713. case CAPI_INSTALLED:
  714. if (capi20_isinstalled() == CAPI_NOERROR)
  715. return 0;
  716. return -ENXIO;
  717. case CAPI_MANUFACTURER_CMD:
  718. {
  719. struct capi_manufacturer_cmd mcmd;
  720. if (!capable(CAP_SYS_ADMIN))
  721. return -EPERM;
  722. if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
  723. return -EFAULT;
  724. return capi20_manufacturer(mcmd.cmd, mcmd.data);
  725. }
  726. return 0;
  727. case CAPI_SET_FLAGS:
  728. case CAPI_CLR_FLAGS: {
  729. unsigned userflags;
  730. if (copy_from_user(&userflags, argp, sizeof(userflags)))
  731. return -EFAULT;
  732. mutex_lock(&cdev->lock);
  733. if (cmd == CAPI_SET_FLAGS)
  734. cdev->userflags |= userflags;
  735. else
  736. cdev->userflags &= ~userflags;
  737. mutex_unlock(&cdev->lock);
  738. return 0;
  739. }
  740. case CAPI_GET_FLAGS:
  741. if (copy_to_user(argp, &cdev->userflags,
  742. sizeof(cdev->userflags)))
  743. return -EFAULT;
  744. return 0;
  745. case CAPI_NCCI_OPENCOUNT: {
  746. struct capincci *nccip;
  747. unsigned ncci;
  748. int count = 0;
  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. count = capincci_minor_opencount(nccip);
  755. mutex_unlock(&cdev->lock);
  756. return count;
  757. }
  758. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  759. case CAPI_NCCI_GETUNIT: {
  760. struct capincci *nccip;
  761. struct capiminor *mp;
  762. unsigned ncci;
  763. int unit = -ESRCH;
  764. if (copy_from_user(&ncci, argp, sizeof(ncci)))
  765. return -EFAULT;
  766. mutex_lock(&cdev->lock);
  767. nccip = capincci_find(cdev, (u32)ncci);
  768. if (nccip) {
  769. mp = nccip->minorp;
  770. if (mp)
  771. unit = mp->minor;
  772. }
  773. mutex_unlock(&cdev->lock);
  774. return unit;
  775. }
  776. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  777. default:
  778. return -EINVAL;
  779. }
  780. }
  781. static long
  782. capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  783. {
  784. int ret;
  785. mutex_lock(&capi_mutex);
  786. ret = capi_ioctl(file, cmd, arg);
  787. mutex_unlock(&capi_mutex);
  788. return ret;
  789. }
  790. static int capi_open(struct inode *inode, struct file *file)
  791. {
  792. struct capidev *cdev;
  793. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  794. if (!cdev)
  795. return -ENOMEM;
  796. mutex_init(&cdev->lock);
  797. skb_queue_head_init(&cdev->recvqueue);
  798. init_waitqueue_head(&cdev->recvwait);
  799. INIT_LIST_HEAD(&cdev->nccis);
  800. file->private_data = cdev;
  801. mutex_lock(&capidev_list_lock);
  802. list_add_tail(&cdev->list, &capidev_list);
  803. mutex_unlock(&capidev_list_lock);
  804. return nonseekable_open(inode, file);
  805. }
  806. static int capi_release(struct inode *inode, struct file *file)
  807. {
  808. struct capidev *cdev = file->private_data;
  809. mutex_lock(&capidev_list_lock);
  810. list_del(&cdev->list);
  811. mutex_unlock(&capidev_list_lock);
  812. if (cdev->ap.applid)
  813. capi20_release(&cdev->ap);
  814. skb_queue_purge(&cdev->recvqueue);
  815. capincci_free(cdev, 0xffffffff);
  816. kfree(cdev);
  817. return 0;
  818. }
  819. static const struct file_operations capi_fops =
  820. {
  821. .owner = THIS_MODULE,
  822. .llseek = no_llseek,
  823. .read = capi_read,
  824. .write = capi_write,
  825. .poll = capi_poll,
  826. .unlocked_ioctl = capi_unlocked_ioctl,
  827. .open = capi_open,
  828. .release = capi_release,
  829. };
  830. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  831. /* -------- tty_operations for capincci ----------------------------- */
  832. static int
  833. capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  834. {
  835. int idx = tty->index;
  836. struct capiminor *mp = capiminor_get(idx);
  837. int ret = tty_init_termios(tty);
  838. if (ret == 0) {
  839. tty_driver_kref_get(driver);
  840. tty->count++;
  841. tty->driver_data = mp;
  842. driver->ttys[idx] = tty;
  843. } else
  844. capiminor_put(mp);
  845. return ret;
  846. }
  847. static void capinc_tty_cleanup(struct tty_struct *tty)
  848. {
  849. struct capiminor *mp = tty->driver_data;
  850. tty->driver_data = NULL;
  851. capiminor_put(mp);
  852. }
  853. static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
  854. {
  855. struct capiminor *mp = tty->driver_data;
  856. int err;
  857. err = tty_port_open(&mp->port, tty, filp);
  858. if (err)
  859. return err;
  860. handle_minor_recv(mp);
  861. return 0;
  862. }
  863. static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
  864. {
  865. struct capiminor *mp = tty->driver_data;
  866. tty_port_close(&mp->port, tty, filp);
  867. }
  868. static int capinc_tty_write(struct tty_struct *tty,
  869. const unsigned char *buf, int count)
  870. {
  871. struct capiminor *mp = tty->driver_data;
  872. struct sk_buff *skb;
  873. pr_debug("capinc_tty_write(count=%d)\n", count);
  874. spin_lock_bh(&mp->outlock);
  875. skb = mp->outskb;
  876. if (skb) {
  877. mp->outskb = NULL;
  878. __skb_queue_tail(&mp->outqueue, skb);
  879. mp->outbytes += skb->len;
  880. }
  881. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
  882. if (!skb) {
  883. printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
  884. spin_unlock_bh(&mp->outlock);
  885. return -ENOMEM;
  886. }
  887. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  888. memcpy(skb_put(skb, count), buf, count);
  889. __skb_queue_tail(&mp->outqueue, skb);
  890. mp->outbytes += skb->len;
  891. spin_unlock_bh(&mp->outlock);
  892. handle_minor_send(mp);
  893. return count;
  894. }
  895. static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
  896. {
  897. struct capiminor *mp = tty->driver_data;
  898. bool invoke_send = false;
  899. struct sk_buff *skb;
  900. int ret = 1;
  901. pr_debug("capinc_put_char(%u)\n", ch);
  902. spin_lock_bh(&mp->outlock);
  903. skb = mp->outskb;
  904. if (skb) {
  905. if (skb_tailroom(skb) > 0) {
  906. *(skb_put(skb, 1)) = ch;
  907. goto unlock_out;
  908. }
  909. mp->outskb = NULL;
  910. __skb_queue_tail(&mp->outqueue, skb);
  911. mp->outbytes += skb->len;
  912. invoke_send = true;
  913. }
  914. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
  915. if (skb) {
  916. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  917. *(skb_put(skb, 1)) = ch;
  918. mp->outskb = skb;
  919. } else {
  920. printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
  921. ret = 0;
  922. }
  923. unlock_out:
  924. spin_unlock_bh(&mp->outlock);
  925. if (invoke_send)
  926. handle_minor_send(mp);
  927. return ret;
  928. }
  929. static void capinc_tty_flush_chars(struct tty_struct *tty)
  930. {
  931. struct capiminor *mp = tty->driver_data;
  932. struct sk_buff *skb;
  933. pr_debug("capinc_tty_flush_chars\n");
  934. spin_lock_bh(&mp->outlock);
  935. skb = mp->outskb;
  936. if (skb) {
  937. mp->outskb = NULL;
  938. __skb_queue_tail(&mp->outqueue, skb);
  939. mp->outbytes += skb->len;
  940. spin_unlock_bh(&mp->outlock);
  941. handle_minor_send(mp);
  942. } else
  943. spin_unlock_bh(&mp->outlock);
  944. handle_minor_recv(mp);
  945. }
  946. static int capinc_tty_write_room(struct tty_struct *tty)
  947. {
  948. struct capiminor *mp = tty->driver_data;
  949. int room;
  950. room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
  951. room *= CAPI_MAX_BLKSIZE;
  952. pr_debug("capinc_tty_write_room = %d\n", room);
  953. return room;
  954. }
  955. static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
  956. {
  957. struct capiminor *mp = tty->driver_data;
  958. pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
  959. mp->outbytes, mp->nack,
  960. skb_queue_len(&mp->outqueue),
  961. skb_queue_len(&mp->inqueue));
  962. return mp->outbytes;
  963. }
  964. static int capinc_tty_ioctl(struct tty_struct *tty,
  965. unsigned int cmd, unsigned long arg)
  966. {
  967. return -ENOIOCTLCMD;
  968. }
  969. static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
  970. {
  971. pr_debug("capinc_tty_set_termios\n");
  972. }
  973. static void capinc_tty_throttle(struct tty_struct *tty)
  974. {
  975. struct capiminor *mp = tty->driver_data;
  976. pr_debug("capinc_tty_throttle\n");
  977. mp->ttyinstop = 1;
  978. }
  979. static void capinc_tty_unthrottle(struct tty_struct *tty)
  980. {
  981. struct capiminor *mp = tty->driver_data;
  982. pr_debug("capinc_tty_unthrottle\n");
  983. mp->ttyinstop = 0;
  984. handle_minor_recv(mp);
  985. }
  986. static void capinc_tty_stop(struct tty_struct *tty)
  987. {
  988. struct capiminor *mp = tty->driver_data;
  989. pr_debug("capinc_tty_stop\n");
  990. mp->ttyoutstop = 1;
  991. }
  992. static void capinc_tty_start(struct tty_struct *tty)
  993. {
  994. struct capiminor *mp = tty->driver_data;
  995. pr_debug("capinc_tty_start\n");
  996. mp->ttyoutstop = 0;
  997. handle_minor_send(mp);
  998. }
  999. static void capinc_tty_hangup(struct tty_struct *tty)
  1000. {
  1001. struct capiminor *mp = tty->driver_data;
  1002. pr_debug("capinc_tty_hangup\n");
  1003. tty_port_hangup(&mp->port);
  1004. }
  1005. static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
  1006. {
  1007. pr_debug("capinc_tty_break_ctl(%d)\n", state);
  1008. return 0;
  1009. }
  1010. static void capinc_tty_flush_buffer(struct tty_struct *tty)
  1011. {
  1012. pr_debug("capinc_tty_flush_buffer\n");
  1013. }
  1014. static void capinc_tty_set_ldisc(struct tty_struct *tty)
  1015. {
  1016. pr_debug("capinc_tty_set_ldisc\n");
  1017. }
  1018. static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
  1019. {
  1020. pr_debug("capinc_tty_send_xchar(%d)\n", ch);
  1021. }
  1022. static const struct tty_operations capinc_ops = {
  1023. .open = capinc_tty_open,
  1024. .close = capinc_tty_close,
  1025. .write = capinc_tty_write,
  1026. .put_char = capinc_tty_put_char,
  1027. .flush_chars = capinc_tty_flush_chars,
  1028. .write_room = capinc_tty_write_room,
  1029. .chars_in_buffer = capinc_tty_chars_in_buffer,
  1030. .ioctl = capinc_tty_ioctl,
  1031. .set_termios = capinc_tty_set_termios,
  1032. .throttle = capinc_tty_throttle,
  1033. .unthrottle = capinc_tty_unthrottle,
  1034. .stop = capinc_tty_stop,
  1035. .start = capinc_tty_start,
  1036. .hangup = capinc_tty_hangup,
  1037. .break_ctl = capinc_tty_break_ctl,
  1038. .flush_buffer = capinc_tty_flush_buffer,
  1039. .set_ldisc = capinc_tty_set_ldisc,
  1040. .send_xchar = capinc_tty_send_xchar,
  1041. .install = capinc_tty_install,
  1042. .cleanup = capinc_tty_cleanup,
  1043. };
  1044. static int __init capinc_tty_init(void)
  1045. {
  1046. struct tty_driver *drv;
  1047. int err;
  1048. if (capi_ttyminors > CAPINC_MAX_PORTS)
  1049. capi_ttyminors = CAPINC_MAX_PORTS;
  1050. if (capi_ttyminors <= 0)
  1051. capi_ttyminors = CAPINC_NR_PORTS;
  1052. capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
  1053. GFP_KERNEL);
  1054. if (!capiminors)
  1055. return -ENOMEM;
  1056. drv = alloc_tty_driver(capi_ttyminors);
  1057. if (!drv) {
  1058. kfree(capiminors);
  1059. return -ENOMEM;
  1060. }
  1061. drv->owner = THIS_MODULE;
  1062. drv->driver_name = "capi_nc";
  1063. drv->name = "capi";
  1064. drv->major = 0;
  1065. drv->minor_start = 0;
  1066. drv->type = TTY_DRIVER_TYPE_SERIAL;
  1067. drv->subtype = SERIAL_TYPE_NORMAL;
  1068. drv->init_termios = tty_std_termios;
  1069. drv->init_termios.c_iflag = ICRNL;
  1070. drv->init_termios.c_oflag = OPOST | ONLCR;
  1071. drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1072. drv->init_termios.c_lflag = 0;
  1073. drv->flags =
  1074. TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
  1075. TTY_DRIVER_DYNAMIC_DEV;
  1076. tty_set_operations(drv, &capinc_ops);
  1077. err = tty_register_driver(drv);
  1078. if (err) {
  1079. put_tty_driver(drv);
  1080. kfree(capiminors);
  1081. printk(KERN_ERR "Couldn't register capi_nc driver\n");
  1082. return err;
  1083. }
  1084. capinc_tty_driver = drv;
  1085. return 0;
  1086. }
  1087. static void __exit capinc_tty_exit(void)
  1088. {
  1089. tty_unregister_driver(capinc_tty_driver);
  1090. put_tty_driver(capinc_tty_driver);
  1091. kfree(capiminors);
  1092. }
  1093. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1094. static inline int capinc_tty_init(void)
  1095. {
  1096. return 0;
  1097. }
  1098. static inline void capinc_tty_exit(void) { }
  1099. #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1100. /* -------- /proc functions ----------------------------------------- */
  1101. /*
  1102. * /proc/capi/capi20:
  1103. * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
  1104. */
  1105. static int capi20_proc_show(struct seq_file *m, void *v)
  1106. {
  1107. struct capidev *cdev;
  1108. struct list_head *l;
  1109. mutex_lock(&capidev_list_lock);
  1110. list_for_each(l, &capidev_list) {
  1111. cdev = list_entry(l, struct capidev, list);
  1112. seq_printf(m, "0 %d %lu %lu %lu %lu\n",
  1113. cdev->ap.applid,
  1114. cdev->ap.nrecvctlpkt,
  1115. cdev->ap.nrecvdatapkt,
  1116. cdev->ap.nsentctlpkt,
  1117. cdev->ap.nsentdatapkt);
  1118. }
  1119. mutex_unlock(&capidev_list_lock);
  1120. return 0;
  1121. }
  1122. static int capi20_proc_open(struct inode *inode, struct file *file)
  1123. {
  1124. return single_open(file, capi20_proc_show, NULL);
  1125. }
  1126. static const struct file_operations capi20_proc_fops = {
  1127. .owner = THIS_MODULE,
  1128. .open = capi20_proc_open,
  1129. .read = seq_read,
  1130. .llseek = seq_lseek,
  1131. .release = single_release,
  1132. };
  1133. /*
  1134. * /proc/capi/capi20ncci:
  1135. * applid ncci
  1136. */
  1137. static int capi20ncci_proc_show(struct seq_file *m, void *v)
  1138. {
  1139. struct capidev *cdev;
  1140. struct capincci *np;
  1141. mutex_lock(&capidev_list_lock);
  1142. list_for_each_entry(cdev, &capidev_list, list) {
  1143. mutex_lock(&cdev->lock);
  1144. list_for_each_entry(np, &cdev->nccis, list)
  1145. seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
  1146. mutex_unlock(&cdev->lock);
  1147. }
  1148. mutex_unlock(&capidev_list_lock);
  1149. return 0;
  1150. }
  1151. static int capi20ncci_proc_open(struct inode *inode, struct file *file)
  1152. {
  1153. return single_open(file, capi20ncci_proc_show, NULL);
  1154. }
  1155. static const struct file_operations capi20ncci_proc_fops = {
  1156. .owner = THIS_MODULE,
  1157. .open = capi20ncci_proc_open,
  1158. .read = seq_read,
  1159. .llseek = seq_lseek,
  1160. .release = single_release,
  1161. };
  1162. static void __init proc_init(void)
  1163. {
  1164. proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
  1165. proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
  1166. }
  1167. static void __exit proc_exit(void)
  1168. {
  1169. remove_proc_entry("capi/capi20", NULL);
  1170. remove_proc_entry("capi/capi20ncci", NULL);
  1171. }
  1172. /* -------- init function and module interface ---------------------- */
  1173. static int __init capi_init(void)
  1174. {
  1175. const char *compileinfo;
  1176. int major_ret;
  1177. major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
  1178. if (major_ret < 0) {
  1179. printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
  1180. return major_ret;
  1181. }
  1182. capi_class = class_create(THIS_MODULE, "capi");
  1183. if (IS_ERR(capi_class)) {
  1184. unregister_chrdev(capi_major, "capi20");
  1185. return PTR_ERR(capi_class);
  1186. }
  1187. device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
  1188. if (capinc_tty_init() < 0) {
  1189. device_destroy(capi_class, MKDEV(capi_major, 0));
  1190. class_destroy(capi_class);
  1191. unregister_chrdev(capi_major, "capi20");
  1192. return -ENOMEM;
  1193. }
  1194. proc_init();
  1195. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  1196. compileinfo = " (middleware)";
  1197. #else
  1198. compileinfo = " (no middleware)";
  1199. #endif
  1200. printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
  1201. capi_major, compileinfo);
  1202. return 0;
  1203. }
  1204. static void __exit capi_exit(void)
  1205. {
  1206. proc_exit();
  1207. device_destroy(capi_class, MKDEV(capi_major, 0));
  1208. class_destroy(capi_class);
  1209. unregister_chrdev(capi_major, "capi20");
  1210. capinc_tty_exit();
  1211. }
  1212. module_init(capi_init);
  1213. module_exit(capi_exit);