capi.c 32 KB

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