capi.c 34 KB

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