capi.c 37 KB

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