capi.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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_REFCOUNT /* alloc/free and open/close debug */
  45. #undef _DEBUG_TTYFUNCS /* call to tty_driver */
  46. #undef _DEBUG_DATAFLOW /* data flow */
  47. /* -------- driver information -------------------------------------- */
  48. static struct class *capi_class;
  49. static int capi_major = 68; /* allocated */
  50. module_param_named(major, capi_major, uint, 0);
  51. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  52. #define CAPINC_NR_PORTS 32
  53. #define CAPINC_MAX_PORTS 256
  54. static int capi_ttyminors = CAPINC_NR_PORTS;
  55. module_param_named(ttyminors, capi_ttyminors, uint, 0);
  56. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  57. /* -------- defines ------------------------------------------------- */
  58. #define CAPINC_MAX_RECVQUEUE 10
  59. #define CAPINC_MAX_SENDQUEUE 10
  60. #define CAPI_MAX_BLKSIZE 2048
  61. /* -------- data structures ----------------------------------------- */
  62. struct capidev;
  63. struct capincci;
  64. struct capiminor;
  65. struct datahandle_queue {
  66. struct list_head list;
  67. u16 datahandle;
  68. };
  69. struct capiminor {
  70. struct kref kref;
  71. unsigned int minor;
  72. struct dentry *capifs_dentry;
  73. struct capi20_appl *ap;
  74. u32 ncci;
  75. u16 datahandle;
  76. u16 msgid;
  77. struct tty_port port;
  78. int ttyinstop;
  79. int ttyoutstop;
  80. struct sk_buff *ttyskb;
  81. atomic_t ttyopencount;
  82. struct sk_buff_head inqueue;
  83. int inbytes;
  84. struct sk_buff_head outqueue;
  85. int outbytes;
  86. /* transmit path */
  87. struct list_head ackqueue;
  88. int nack;
  89. spinlock_t ackqlock;
  90. };
  91. /* FIXME: The following lock is a sledgehammer-workaround to a
  92. * locking issue with the capiminor (and maybe other) data structure(s).
  93. * Access to this data is done in a racy way and crashes the machine with
  94. * a FritzCard DSL driver; sooner or later. This is a workaround
  95. * which trades scalability vs stability, so it doesn't crash the kernel anymore.
  96. * The correct (and scalable) fix for the issue seems to require
  97. * an API change to the drivers... . */
  98. static DEFINE_SPINLOCK(workaround_lock);
  99. struct capincci {
  100. struct list_head list;
  101. u32 ncci;
  102. struct capidev *cdev;
  103. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  104. struct capiminor *minorp;
  105. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  106. };
  107. struct capidev {
  108. struct list_head list;
  109. struct capi20_appl ap;
  110. u16 errcode;
  111. unsigned userflags;
  112. struct sk_buff_head recvqueue;
  113. wait_queue_head_t recvwait;
  114. struct list_head nccis;
  115. struct mutex lock;
  116. };
  117. /* -------- global variables ---------------------------------------- */
  118. static DEFINE_MUTEX(capidev_list_lock);
  119. static LIST_HEAD(capidev_list);
  120. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  121. static DEFINE_SPINLOCK(capiminors_lock);
  122. static struct capiminor **capiminors;
  123. static struct tty_driver *capinc_tty_driver;
  124. /* -------- datahandles --------------------------------------------- */
  125. static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
  126. {
  127. struct datahandle_queue *n;
  128. unsigned long flags;
  129. n = kmalloc(sizeof(*n), GFP_ATOMIC);
  130. if (unlikely(!n)) {
  131. printk(KERN_ERR "capi: alloc datahandle failed\n");
  132. return -1;
  133. }
  134. n->datahandle = datahandle;
  135. INIT_LIST_HEAD(&n->list);
  136. spin_lock_irqsave(&mp->ackqlock, flags);
  137. list_add_tail(&n->list, &mp->ackqueue);
  138. mp->nack++;
  139. spin_unlock_irqrestore(&mp->ackqlock, flags);
  140. return 0;
  141. }
  142. static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
  143. {
  144. struct datahandle_queue *p, *tmp;
  145. unsigned long flags;
  146. spin_lock_irqsave(&mp->ackqlock, flags);
  147. list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
  148. if (p->datahandle == datahandle) {
  149. list_del(&p->list);
  150. kfree(p);
  151. mp->nack--;
  152. spin_unlock_irqrestore(&mp->ackqlock, flags);
  153. return 0;
  154. }
  155. }
  156. spin_unlock_irqrestore(&mp->ackqlock, flags);
  157. return -1;
  158. }
  159. static void capiminor_del_all_ack(struct capiminor *mp)
  160. {
  161. struct datahandle_queue *p, *tmp;
  162. unsigned long flags;
  163. spin_lock_irqsave(&mp->ackqlock, flags);
  164. list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
  165. list_del(&p->list);
  166. kfree(p);
  167. mp->nack--;
  168. }
  169. spin_unlock_irqrestore(&mp->ackqlock, flags);
  170. }
  171. /* -------- struct capiminor ---------------------------------------- */
  172. static const struct tty_port_operations capiminor_port_ops; /* we have none */
  173. static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
  174. {
  175. struct capiminor *mp;
  176. struct device *dev;
  177. unsigned int minor;
  178. mp = kzalloc(sizeof(*mp), GFP_KERNEL);
  179. if (!mp) {
  180. printk(KERN_ERR "capi: can't alloc capiminor\n");
  181. return NULL;
  182. }
  183. kref_init(&mp->kref);
  184. mp->ap = ap;
  185. mp->ncci = ncci;
  186. mp->msgid = 0;
  187. atomic_set(&mp->ttyopencount,0);
  188. INIT_LIST_HEAD(&mp->ackqueue);
  189. spin_lock_init(&mp->ackqlock);
  190. skb_queue_head_init(&mp->inqueue);
  191. skb_queue_head_init(&mp->outqueue);
  192. tty_port_init(&mp->port);
  193. mp->port.ops = &capiminor_port_ops;
  194. /* Allocate the least unused minor number. */
  195. spin_lock(&capiminors_lock);
  196. for (minor = 0; minor < capi_ttyminors; minor++)
  197. if (!capiminors[minor]) {
  198. capiminors[minor] = mp;
  199. break;
  200. }
  201. spin_unlock(&capiminors_lock);
  202. if (minor == capi_ttyminors) {
  203. printk(KERN_NOTICE "capi: out of minors\n");
  204. goto err_out1;
  205. }
  206. mp->minor = minor;
  207. dev = tty_register_device(capinc_tty_driver, minor, NULL);
  208. if (IS_ERR(dev))
  209. goto err_out2;
  210. return mp;
  211. err_out2:
  212. spin_lock(&capiminors_lock);
  213. capiminors[minor] = NULL;
  214. spin_unlock(&capiminors_lock);
  215. err_out1:
  216. kfree(mp);
  217. return NULL;
  218. }
  219. static void capiminor_destroy(struct kref *kref)
  220. {
  221. struct capiminor *mp = container_of(kref, struct capiminor, kref);
  222. kfree_skb(mp->ttyskb);
  223. skb_queue_purge(&mp->inqueue);
  224. skb_queue_purge(&mp->outqueue);
  225. capiminor_del_all_ack(mp);
  226. kfree(mp);
  227. }
  228. static struct capiminor *capiminor_get(unsigned int minor)
  229. {
  230. struct capiminor *mp;
  231. spin_lock(&capiminors_lock);
  232. mp = capiminors[minor];
  233. if (mp)
  234. kref_get(&mp->kref);
  235. spin_unlock(&capiminors_lock);
  236. return mp;
  237. }
  238. static inline void capiminor_put(struct capiminor *mp)
  239. {
  240. kref_put(&mp->kref, capiminor_destroy);
  241. }
  242. static void capiminor_free(struct capiminor *mp)
  243. {
  244. tty_unregister_device(capinc_tty_driver, mp->minor);
  245. spin_lock(&capiminors_lock);
  246. capiminors[mp->minor] = NULL;
  247. spin_unlock(&capiminors_lock);
  248. capiminor_put(mp);
  249. }
  250. /* -------- struct capincci ----------------------------------------- */
  251. static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
  252. {
  253. struct capiminor *mp;
  254. dev_t device;
  255. if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
  256. return;
  257. mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
  258. if (mp) {
  259. device = MKDEV(capinc_tty_driver->major, mp->minor);
  260. mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
  261. }
  262. }
  263. static void capincci_free_minor(struct capincci *np)
  264. {
  265. struct capiminor *mp = np->minorp;
  266. struct tty_struct *tty;
  267. if (mp) {
  268. capifs_free_ncci(mp->capifs_dentry);
  269. tty = tty_port_tty_get(&mp->port);
  270. if (tty) {
  271. tty_vhangup(tty);
  272. tty_kref_put(tty);
  273. }
  274. capiminor_free(mp);
  275. }
  276. }
  277. static inline unsigned int capincci_minor_opencount(struct capincci *np)
  278. {
  279. struct capiminor *mp = np->minorp;
  280. return mp ? atomic_read(&mp->ttyopencount) : 0;
  281. }
  282. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  283. static inline void
  284. capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
  285. static inline void capincci_free_minor(struct capincci *np) { }
  286. static inline unsigned int capincci_minor_opencount(struct capincci *np)
  287. {
  288. return 0;
  289. }
  290. #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  291. static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
  292. {
  293. struct capincci *np;
  294. np = kzalloc(sizeof(*np), GFP_KERNEL);
  295. if (!np)
  296. return NULL;
  297. np->ncci = ncci;
  298. np->cdev = cdev;
  299. capincci_alloc_minor(cdev, np);
  300. list_add_tail(&np->list, &cdev->nccis);
  301. return np;
  302. }
  303. static void capincci_free(struct capidev *cdev, u32 ncci)
  304. {
  305. struct capincci *np, *tmp;
  306. list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
  307. if (ncci == 0xffffffff || np->ncci == ncci) {
  308. capincci_free_minor(np);
  309. list_del(&np->list);
  310. kfree(np);
  311. }
  312. }
  313. static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
  314. {
  315. struct capincci *np;
  316. list_for_each_entry(np, &cdev->nccis, list)
  317. if (np->ncci == ncci)
  318. return np;
  319. return NULL;
  320. }
  321. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  322. /* -------- handle data queue --------------------------------------- */
  323. static struct sk_buff *
  324. gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
  325. {
  326. struct sk_buff *nskb;
  327. nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
  328. if (nskb) {
  329. u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
  330. unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
  331. capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
  332. capimsg_setu16(s, 2, mp->ap->applid);
  333. capimsg_setu8 (s, 4, CAPI_DATA_B3);
  334. capimsg_setu8 (s, 5, CAPI_RESP);
  335. capimsg_setu16(s, 6, mp->msgid++);
  336. capimsg_setu32(s, 8, mp->ncci);
  337. capimsg_setu16(s, 12, datahandle);
  338. }
  339. return nskb;
  340. }
  341. static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
  342. {
  343. struct tty_struct *tty;
  344. struct sk_buff *nskb;
  345. int datalen;
  346. u16 errcode, datahandle;
  347. struct tty_ldisc *ld;
  348. int ret = -1;
  349. datalen = skb->len - CAPIMSG_LEN(skb->data);
  350. tty = tty_port_tty_get(&mp->port);
  351. if (!tty) {
  352. #ifdef _DEBUG_DATAFLOW
  353. printk(KERN_DEBUG "capi: currently no receiver\n");
  354. #endif
  355. return -1;
  356. }
  357. ld = tty_ldisc_ref(tty);
  358. if (!ld)
  359. goto out1;
  360. if (ld->ops->receive_buf == NULL) {
  361. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  362. printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
  363. #endif
  364. goto out2;
  365. }
  366. if (mp->ttyinstop) {
  367. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  368. printk(KERN_DEBUG "capi: recv tty throttled\n");
  369. #endif
  370. goto out2;
  371. }
  372. if (tty->receive_room < datalen) {
  373. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  374. printk(KERN_DEBUG "capi: no room in tty\n");
  375. #endif
  376. goto out2;
  377. }
  378. if ((nskb = gen_data_b3_resp_for(mp, skb)) == NULL) {
  379. printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
  380. goto out2;
  381. }
  382. datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4);
  383. errcode = capi20_put_message(mp->ap, nskb);
  384. if (errcode != CAPI_NOERROR) {
  385. printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
  386. errcode);
  387. kfree_skb(nskb);
  388. goto out2;
  389. }
  390. (void)skb_pull(skb, CAPIMSG_LEN(skb->data));
  391. #ifdef _DEBUG_DATAFLOW
  392. printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
  393. datahandle, skb->len);
  394. #endif
  395. ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
  396. kfree_skb(skb);
  397. ret = 0;
  398. out2:
  399. tty_ldisc_deref(ld);
  400. out1:
  401. tty_kref_put(tty);
  402. return ret;
  403. }
  404. static void handle_minor_recv(struct capiminor *mp)
  405. {
  406. struct sk_buff *skb;
  407. while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
  408. unsigned int len = skb->len;
  409. mp->inbytes -= len;
  410. if (handle_recv_skb(mp, skb) < 0) {
  411. skb_queue_head(&mp->inqueue, skb);
  412. mp->inbytes += len;
  413. return;
  414. }
  415. }
  416. }
  417. static int handle_minor_send(struct capiminor *mp)
  418. {
  419. struct tty_struct *tty;
  420. struct sk_buff *skb;
  421. u16 len;
  422. int count = 0;
  423. u16 errcode;
  424. u16 datahandle;
  425. tty = tty_port_tty_get(&mp->port);
  426. if (!tty)
  427. return 0;
  428. if (mp->ttyoutstop) {
  429. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  430. printk(KERN_DEBUG "capi: send: tty stopped\n");
  431. #endif
  432. tty_kref_put(tty);
  433. return 0;
  434. }
  435. while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
  436. datahandle = mp->datahandle;
  437. len = (u16)skb->len;
  438. skb_push(skb, CAPI_DATA_B3_REQ_LEN);
  439. memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
  440. capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
  441. capimsg_setu16(skb->data, 2, mp->ap->applid);
  442. capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
  443. capimsg_setu8 (skb->data, 5, CAPI_REQ);
  444. capimsg_setu16(skb->data, 6, mp->msgid++);
  445. capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
  446. capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
  447. capimsg_setu16(skb->data, 16, len); /* Data length */
  448. capimsg_setu16(skb->data, 18, datahandle);
  449. capimsg_setu16(skb->data, 20, 0); /* Flags */
  450. if (capiminor_add_ack(mp, datahandle) < 0) {
  451. skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
  452. skb_queue_head(&mp->outqueue, skb);
  453. tty_kref_put(tty);
  454. return count;
  455. }
  456. errcode = capi20_put_message(mp->ap, skb);
  457. if (errcode == CAPI_NOERROR) {
  458. mp->datahandle++;
  459. count++;
  460. mp->outbytes -= len;
  461. #ifdef _DEBUG_DATAFLOW
  462. printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
  463. datahandle, len);
  464. #endif
  465. continue;
  466. }
  467. capiminor_del_ack(mp, datahandle);
  468. if (errcode == CAPI_SENDQUEUEFULL) {
  469. skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
  470. skb_queue_head(&mp->outqueue, skb);
  471. break;
  472. }
  473. /* ups, drop packet */
  474. printk(KERN_ERR "capi: put_message = %x\n", errcode);
  475. mp->outbytes -= len;
  476. kfree_skb(skb);
  477. }
  478. tty_kref_put(tty);
  479. return count;
  480. }
  481. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  482. /* -------- function called by lower level -------------------------- */
  483. static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
  484. {
  485. struct capidev *cdev = ap->private;
  486. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  487. struct tty_struct *tty;
  488. struct capiminor *mp;
  489. u16 datahandle;
  490. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  491. struct capincci *np;
  492. unsigned long flags;
  493. mutex_lock(&cdev->lock);
  494. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
  495. u16 info = CAPIMSG_U16(skb->data, 12); // Info field
  496. if ((info & 0xff00) == 0)
  497. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  498. }
  499. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
  500. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  501. spin_lock_irqsave(&workaround_lock, flags);
  502. if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
  503. skb_queue_tail(&cdev->recvqueue, skb);
  504. wake_up_interruptible(&cdev->recvwait);
  505. goto unlock_out;
  506. }
  507. np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
  508. if (!np) {
  509. printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
  510. skb_queue_tail(&cdev->recvqueue, skb);
  511. wake_up_interruptible(&cdev->recvwait);
  512. goto unlock_out;
  513. }
  514. #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
  515. skb_queue_tail(&cdev->recvqueue, skb);
  516. wake_up_interruptible(&cdev->recvwait);
  517. #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  518. mp = np->minorp;
  519. if (!mp) {
  520. skb_queue_tail(&cdev->recvqueue, skb);
  521. wake_up_interruptible(&cdev->recvwait);
  522. goto unlock_out;
  523. }
  524. if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
  525. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
  526. #ifdef _DEBUG_DATAFLOW
  527. printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
  528. datahandle, skb->len-CAPIMSG_LEN(skb->data));
  529. #endif
  530. skb_queue_tail(&mp->inqueue, skb);
  531. mp->inbytes += skb->len;
  532. handle_minor_recv(mp);
  533. } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
  534. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
  535. #ifdef _DEBUG_DATAFLOW
  536. printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
  537. datahandle,
  538. CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
  539. #endif
  540. kfree_skb(skb);
  541. (void)capiminor_del_ack(mp, datahandle);
  542. tty = tty_port_tty_get(&mp->port);
  543. if (tty) {
  544. tty_wakeup(tty);
  545. tty_kref_put(tty);
  546. }
  547. (void)handle_minor_send(mp);
  548. } else {
  549. /* ups, let capi application handle it :-) */
  550. skb_queue_tail(&cdev->recvqueue, skb);
  551. wake_up_interruptible(&cdev->recvwait);
  552. }
  553. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  554. unlock_out:
  555. spin_unlock_irqrestore(&workaround_lock, flags);
  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. unsigned long flags;
  879. int err;
  880. err = tty_port_open(&mp->port, tty, filp);
  881. if (err)
  882. return err;
  883. spin_lock_irqsave(&workaround_lock, flags);
  884. atomic_inc(&mp->ttyopencount);
  885. #ifdef _DEBUG_REFCOUNT
  886. printk(KERN_DEBUG "capinc_tty_open ocount=%d\n", atomic_read(&mp->ttyopencount));
  887. #endif
  888. handle_minor_recv(mp);
  889. spin_unlock_irqrestore(&workaround_lock, flags);
  890. return 0;
  891. }
  892. static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
  893. {
  894. struct capiminor *mp = tty->driver_data;
  895. if (atomic_dec_and_test(&mp->ttyopencount)) {
  896. #ifdef _DEBUG_REFCOUNT
  897. printk(KERN_DEBUG "capinc_tty_close lastclose\n");
  898. #endif
  899. }
  900. #ifdef _DEBUG_REFCOUNT
  901. printk(KERN_DEBUG "capinc_tty_close ocount=%d\n", atomic_read(&mp->ttyopencount));
  902. #endif
  903. #ifdef _DEBUG_REFCOUNT
  904. printk(KERN_DEBUG "capinc_tty_close\n");
  905. #endif
  906. tty_port_close(&mp->port, tty, filp);
  907. }
  908. static int capinc_tty_write(struct tty_struct *tty,
  909. const unsigned char *buf, int count)
  910. {
  911. struct capiminor *mp = tty->driver_data;
  912. struct sk_buff *skb;
  913. unsigned long flags;
  914. #ifdef _DEBUG_TTYFUNCS
  915. printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
  916. #endif
  917. spin_lock_irqsave(&workaround_lock, flags);
  918. skb = mp->ttyskb;
  919. if (skb) {
  920. mp->ttyskb = NULL;
  921. skb_queue_tail(&mp->outqueue, skb);
  922. mp->outbytes += skb->len;
  923. }
  924. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
  925. if (!skb) {
  926. printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
  927. spin_unlock_irqrestore(&workaround_lock, flags);
  928. return -ENOMEM;
  929. }
  930. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  931. memcpy(skb_put(skb, count), buf, count);
  932. skb_queue_tail(&mp->outqueue, skb);
  933. mp->outbytes += skb->len;
  934. (void)handle_minor_send(mp);
  935. (void)handle_minor_recv(mp);
  936. spin_unlock_irqrestore(&workaround_lock, flags);
  937. return count;
  938. }
  939. static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
  940. {
  941. struct capiminor *mp = tty->driver_data;
  942. struct sk_buff *skb;
  943. unsigned long flags;
  944. int ret = 1;
  945. #ifdef _DEBUG_TTYFUNCS
  946. printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
  947. #endif
  948. spin_lock_irqsave(&workaround_lock, flags);
  949. skb = mp->ttyskb;
  950. if (skb) {
  951. if (skb_tailroom(skb) > 0) {
  952. *(skb_put(skb, 1)) = ch;
  953. spin_unlock_irqrestore(&workaround_lock, flags);
  954. return 1;
  955. }
  956. mp->ttyskb = NULL;
  957. skb_queue_tail(&mp->outqueue, skb);
  958. mp->outbytes += skb->len;
  959. (void)handle_minor_send(mp);
  960. }
  961. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
  962. if (skb) {
  963. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  964. *(skb_put(skb, 1)) = ch;
  965. mp->ttyskb = skb;
  966. } else {
  967. printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
  968. ret = 0;
  969. }
  970. spin_unlock_irqrestore(&workaround_lock, flags);
  971. return ret;
  972. }
  973. static void capinc_tty_flush_chars(struct tty_struct *tty)
  974. {
  975. struct capiminor *mp = tty->driver_data;
  976. struct sk_buff *skb;
  977. unsigned long flags;
  978. #ifdef _DEBUG_TTYFUNCS
  979. printk(KERN_DEBUG "capinc_tty_flush_chars\n");
  980. #endif
  981. spin_lock_irqsave(&workaround_lock, flags);
  982. skb = mp->ttyskb;
  983. if (skb) {
  984. mp->ttyskb = NULL;
  985. skb_queue_tail(&mp->outqueue, skb);
  986. mp->outbytes += skb->len;
  987. (void)handle_minor_send(mp);
  988. }
  989. (void)handle_minor_recv(mp);
  990. spin_unlock_irqrestore(&workaround_lock, flags);
  991. }
  992. static int capinc_tty_write_room(struct tty_struct *tty)
  993. {
  994. struct capiminor *mp = tty->driver_data;
  995. int room;
  996. room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
  997. room *= CAPI_MAX_BLKSIZE;
  998. #ifdef _DEBUG_TTYFUNCS
  999. printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
  1000. #endif
  1001. return room;
  1002. }
  1003. static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
  1004. {
  1005. struct capiminor *mp = tty->driver_data;
  1006. #ifdef _DEBUG_TTYFUNCS
  1007. printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
  1008. mp->outbytes, mp->nack,
  1009. skb_queue_len(&mp->outqueue),
  1010. skb_queue_len(&mp->inqueue));
  1011. #endif
  1012. return mp->outbytes;
  1013. }
  1014. static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
  1015. unsigned int cmd, unsigned long arg)
  1016. {
  1017. int error = 0;
  1018. switch (cmd) {
  1019. default:
  1020. error = n_tty_ioctl_helper(tty, file, cmd, arg);
  1021. break;
  1022. }
  1023. return error;
  1024. }
  1025. static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
  1026. {
  1027. #ifdef _DEBUG_TTYFUNCS
  1028. printk(KERN_DEBUG "capinc_tty_set_termios\n");
  1029. #endif
  1030. }
  1031. static void capinc_tty_throttle(struct tty_struct *tty)
  1032. {
  1033. struct capiminor *mp = tty->driver_data;
  1034. #ifdef _DEBUG_TTYFUNCS
  1035. printk(KERN_DEBUG "capinc_tty_throttle\n");
  1036. #endif
  1037. mp->ttyinstop = 1;
  1038. }
  1039. static void capinc_tty_unthrottle(struct tty_struct *tty)
  1040. {
  1041. struct capiminor *mp = tty->driver_data;
  1042. unsigned long flags;
  1043. #ifdef _DEBUG_TTYFUNCS
  1044. printk(KERN_DEBUG "capinc_tty_unthrottle\n");
  1045. #endif
  1046. spin_lock_irqsave(&workaround_lock, flags);
  1047. mp->ttyinstop = 0;
  1048. handle_minor_recv(mp);
  1049. spin_unlock_irqrestore(&workaround_lock, flags);
  1050. }
  1051. static void capinc_tty_stop(struct tty_struct *tty)
  1052. {
  1053. struct capiminor *mp = tty->driver_data;
  1054. #ifdef _DEBUG_TTYFUNCS
  1055. printk(KERN_DEBUG "capinc_tty_stop\n");
  1056. #endif
  1057. mp->ttyoutstop = 1;
  1058. }
  1059. static void capinc_tty_start(struct tty_struct *tty)
  1060. {
  1061. struct capiminor *mp = tty->driver_data;
  1062. unsigned long flags;
  1063. #ifdef _DEBUG_TTYFUNCS
  1064. printk(KERN_DEBUG "capinc_tty_start\n");
  1065. #endif
  1066. spin_lock_irqsave(&workaround_lock, flags);
  1067. mp->ttyoutstop = 0;
  1068. (void)handle_minor_send(mp);
  1069. spin_unlock_irqrestore(&workaround_lock, flags);
  1070. }
  1071. static void capinc_tty_hangup(struct tty_struct *tty)
  1072. {
  1073. struct capiminor *mp = tty->driver_data;
  1074. #ifdef _DEBUG_TTYFUNCS
  1075. printk(KERN_DEBUG "capinc_tty_hangup\n");
  1076. #endif
  1077. tty_port_hangup(&mp->port);
  1078. }
  1079. static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
  1080. {
  1081. #ifdef _DEBUG_TTYFUNCS
  1082. printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
  1083. #endif
  1084. return 0;
  1085. }
  1086. static void capinc_tty_flush_buffer(struct tty_struct *tty)
  1087. {
  1088. #ifdef _DEBUG_TTYFUNCS
  1089. printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
  1090. #endif
  1091. }
  1092. static void capinc_tty_set_ldisc(struct tty_struct *tty)
  1093. {
  1094. #ifdef _DEBUG_TTYFUNCS
  1095. printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
  1096. #endif
  1097. }
  1098. static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
  1099. {
  1100. #ifdef _DEBUG_TTYFUNCS
  1101. printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
  1102. #endif
  1103. }
  1104. static const struct tty_operations capinc_ops = {
  1105. .open = capinc_tty_open,
  1106. .close = capinc_tty_close,
  1107. .write = capinc_tty_write,
  1108. .put_char = capinc_tty_put_char,
  1109. .flush_chars = capinc_tty_flush_chars,
  1110. .write_room = capinc_tty_write_room,
  1111. .chars_in_buffer = capinc_tty_chars_in_buffer,
  1112. .ioctl = capinc_tty_ioctl,
  1113. .set_termios = capinc_tty_set_termios,
  1114. .throttle = capinc_tty_throttle,
  1115. .unthrottle = capinc_tty_unthrottle,
  1116. .stop = capinc_tty_stop,
  1117. .start = capinc_tty_start,
  1118. .hangup = capinc_tty_hangup,
  1119. .break_ctl = capinc_tty_break_ctl,
  1120. .flush_buffer = capinc_tty_flush_buffer,
  1121. .set_ldisc = capinc_tty_set_ldisc,
  1122. .send_xchar = capinc_tty_send_xchar,
  1123. .install = capinc_tty_install,
  1124. .cleanup = capinc_tty_cleanup,
  1125. };
  1126. static int __init capinc_tty_init(void)
  1127. {
  1128. struct tty_driver *drv;
  1129. int err;
  1130. if (capi_ttyminors > CAPINC_MAX_PORTS)
  1131. capi_ttyminors = CAPINC_MAX_PORTS;
  1132. if (capi_ttyminors <= 0)
  1133. capi_ttyminors = CAPINC_NR_PORTS;
  1134. capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
  1135. GFP_KERNEL);
  1136. if (!capiminors)
  1137. return -ENOMEM;
  1138. drv = alloc_tty_driver(capi_ttyminors);
  1139. if (!drv) {
  1140. kfree(capiminors);
  1141. return -ENOMEM;
  1142. }
  1143. drv->owner = THIS_MODULE;
  1144. drv->driver_name = "capi_nc";
  1145. drv->name = "capi";
  1146. drv->major = 0;
  1147. drv->minor_start = 0;
  1148. drv->type = TTY_DRIVER_TYPE_SERIAL;
  1149. drv->subtype = SERIAL_TYPE_NORMAL;
  1150. drv->init_termios = tty_std_termios;
  1151. drv->init_termios.c_iflag = ICRNL;
  1152. drv->init_termios.c_oflag = OPOST | ONLCR;
  1153. drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1154. drv->init_termios.c_lflag = 0;
  1155. drv->flags =
  1156. TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
  1157. TTY_DRIVER_DYNAMIC_DEV;
  1158. tty_set_operations(drv, &capinc_ops);
  1159. err = tty_register_driver(drv);
  1160. if (err) {
  1161. put_tty_driver(drv);
  1162. kfree(capiminors);
  1163. printk(KERN_ERR "Couldn't register capi_nc driver\n");
  1164. return err;
  1165. }
  1166. capinc_tty_driver = drv;
  1167. return 0;
  1168. }
  1169. static void __exit capinc_tty_exit(void)
  1170. {
  1171. tty_unregister_driver(capinc_tty_driver);
  1172. put_tty_driver(capinc_tty_driver);
  1173. kfree(capiminors);
  1174. }
  1175. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1176. static inline int capinc_tty_init(void)
  1177. {
  1178. return 0;
  1179. }
  1180. static inline void capinc_tty_exit(void) { }
  1181. #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1182. /* -------- /proc functions ----------------------------------------- */
  1183. /*
  1184. * /proc/capi/capi20:
  1185. * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
  1186. */
  1187. static int capi20_proc_show(struct seq_file *m, void *v)
  1188. {
  1189. struct capidev *cdev;
  1190. struct list_head *l;
  1191. mutex_lock(&capidev_list_lock);
  1192. list_for_each(l, &capidev_list) {
  1193. cdev = list_entry(l, struct capidev, list);
  1194. seq_printf(m, "0 %d %lu %lu %lu %lu\n",
  1195. cdev->ap.applid,
  1196. cdev->ap.nrecvctlpkt,
  1197. cdev->ap.nrecvdatapkt,
  1198. cdev->ap.nsentctlpkt,
  1199. cdev->ap.nsentdatapkt);
  1200. }
  1201. mutex_unlock(&capidev_list_lock);
  1202. return 0;
  1203. }
  1204. static int capi20_proc_open(struct inode *inode, struct file *file)
  1205. {
  1206. return single_open(file, capi20_proc_show, NULL);
  1207. }
  1208. static const struct file_operations capi20_proc_fops = {
  1209. .owner = THIS_MODULE,
  1210. .open = capi20_proc_open,
  1211. .read = seq_read,
  1212. .llseek = seq_lseek,
  1213. .release = single_release,
  1214. };
  1215. /*
  1216. * /proc/capi/capi20ncci:
  1217. * applid ncci
  1218. */
  1219. static int capi20ncci_proc_show(struct seq_file *m, void *v)
  1220. {
  1221. struct capidev *cdev;
  1222. struct capincci *np;
  1223. mutex_lock(&capidev_list_lock);
  1224. list_for_each_entry(cdev, &capidev_list, list) {
  1225. mutex_lock(&cdev->lock);
  1226. list_for_each_entry(np, &cdev->nccis, list)
  1227. seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
  1228. mutex_unlock(&cdev->lock);
  1229. }
  1230. mutex_unlock(&capidev_list_lock);
  1231. return 0;
  1232. }
  1233. static int capi20ncci_proc_open(struct inode *inode, struct file *file)
  1234. {
  1235. return single_open(file, capi20ncci_proc_show, NULL);
  1236. }
  1237. static const struct file_operations capi20ncci_proc_fops = {
  1238. .owner = THIS_MODULE,
  1239. .open = capi20ncci_proc_open,
  1240. .read = seq_read,
  1241. .llseek = seq_lseek,
  1242. .release = single_release,
  1243. };
  1244. static void __init proc_init(void)
  1245. {
  1246. proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
  1247. proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
  1248. }
  1249. static void __exit proc_exit(void)
  1250. {
  1251. remove_proc_entry("capi/capi20", NULL);
  1252. remove_proc_entry("capi/capi20ncci", NULL);
  1253. }
  1254. /* -------- init function and module interface ---------------------- */
  1255. static int __init capi_init(void)
  1256. {
  1257. const char *compileinfo;
  1258. int major_ret;
  1259. major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
  1260. if (major_ret < 0) {
  1261. printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
  1262. return major_ret;
  1263. }
  1264. capi_class = class_create(THIS_MODULE, "capi");
  1265. if (IS_ERR(capi_class)) {
  1266. unregister_chrdev(capi_major, "capi20");
  1267. return PTR_ERR(capi_class);
  1268. }
  1269. device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
  1270. if (capinc_tty_init() < 0) {
  1271. device_destroy(capi_class, MKDEV(capi_major, 0));
  1272. class_destroy(capi_class);
  1273. unregister_chrdev(capi_major, "capi20");
  1274. return -ENOMEM;
  1275. }
  1276. proc_init();
  1277. #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
  1278. compileinfo = " (middleware+capifs)";
  1279. #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
  1280. compileinfo = " (no capifs)";
  1281. #else
  1282. compileinfo = " (no middleware)";
  1283. #endif
  1284. printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
  1285. capi_major, compileinfo);
  1286. return 0;
  1287. }
  1288. static void __exit capi_exit(void)
  1289. {
  1290. proc_exit();
  1291. device_destroy(capi_class, MKDEV(capi_major, 0));
  1292. class_destroy(capi_class);
  1293. unregister_chrdev(capi_major, "capi20");
  1294. capinc_tty_exit();
  1295. }
  1296. module_init(capi_init);
  1297. module_exit(capi_exit);