capi.c 35 KB

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