capi.c 32 KB

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