capi.c 34 KB

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