capi.c 36 KB

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