capi.c 32 KB

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