capi.c 36 KB

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