capi.c 32 KB

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