capi.c 36 KB

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