capi.c 35 KB

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