capi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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 ackqueue_entry {
  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. atomic_t datahandle;
  75. atomic_t msgid;
  76. struct tty_port port;
  77. int ttyinstop;
  78. int ttyoutstop;
  79. struct sk_buff *ttyskb;
  80. struct sk_buff_head inqueue;
  81. struct sk_buff_head outqueue;
  82. int outbytes;
  83. /* transmit path */
  84. struct list_head ackqueue;
  85. int nack;
  86. spinlock_t ackqlock;
  87. };
  88. /* FIXME: The following lock is a sledgehammer-workaround to a
  89. * locking issue with the capiminor (and maybe other) data structure(s).
  90. * Access to this data is done in a racy way and crashes the machine with
  91. * a FritzCard DSL driver; sooner or later. This is a workaround
  92. * which trades scalability vs stability, so it doesn't crash the kernel anymore.
  93. * The correct (and scalable) fix for the issue seems to require
  94. * an API change to the drivers... . */
  95. static DEFINE_SPINLOCK(workaround_lock);
  96. struct capincci {
  97. struct list_head list;
  98. u32 ncci;
  99. struct capidev *cdev;
  100. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  101. struct capiminor *minorp;
  102. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  103. };
  104. struct capidev {
  105. struct list_head list;
  106. struct capi20_appl ap;
  107. u16 errcode;
  108. unsigned userflags;
  109. struct sk_buff_head recvqueue;
  110. wait_queue_head_t recvwait;
  111. struct list_head nccis;
  112. struct mutex lock;
  113. };
  114. /* -------- global variables ---------------------------------------- */
  115. static DEFINE_MUTEX(capidev_list_lock);
  116. static LIST_HEAD(capidev_list);
  117. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  118. static DEFINE_SPINLOCK(capiminors_lock);
  119. static struct capiminor **capiminors;
  120. static struct tty_driver *capinc_tty_driver;
  121. /* -------- datahandles --------------------------------------------- */
  122. static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
  123. {
  124. struct ackqueue_entry *n;
  125. unsigned long flags;
  126. n = kmalloc(sizeof(*n), GFP_ATOMIC);
  127. if (unlikely(!n)) {
  128. printk(KERN_ERR "capi: alloc datahandle failed\n");
  129. return -1;
  130. }
  131. n->datahandle = datahandle;
  132. INIT_LIST_HEAD(&n->list);
  133. spin_lock_irqsave(&mp->ackqlock, flags);
  134. list_add_tail(&n->list, &mp->ackqueue);
  135. mp->nack++;
  136. spin_unlock_irqrestore(&mp->ackqlock, flags);
  137. return 0;
  138. }
  139. static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
  140. {
  141. struct ackqueue_entry *p, *tmp;
  142. unsigned long flags;
  143. spin_lock_irqsave(&mp->ackqlock, flags);
  144. list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
  145. if (p->datahandle == datahandle) {
  146. list_del(&p->list);
  147. kfree(p);
  148. mp->nack--;
  149. spin_unlock_irqrestore(&mp->ackqlock, flags);
  150. return 0;
  151. }
  152. }
  153. spin_unlock_irqrestore(&mp->ackqlock, flags);
  154. return -1;
  155. }
  156. static void capiminor_del_all_ack(struct capiminor *mp)
  157. {
  158. struct ackqueue_entry *p, *tmp;
  159. unsigned long flags;
  160. spin_lock_irqsave(&mp->ackqlock, flags);
  161. list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
  162. list_del(&p->list);
  163. kfree(p);
  164. mp->nack--;
  165. }
  166. spin_unlock_irqrestore(&mp->ackqlock, flags);
  167. }
  168. /* -------- struct capiminor ---------------------------------------- */
  169. static const struct tty_port_operations capiminor_port_ops; /* we have none */
  170. static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
  171. {
  172. struct capiminor *mp;
  173. struct device *dev;
  174. unsigned int minor;
  175. mp = kzalloc(sizeof(*mp), GFP_KERNEL);
  176. if (!mp) {
  177. printk(KERN_ERR "capi: can't alloc capiminor\n");
  178. return NULL;
  179. }
  180. kref_init(&mp->kref);
  181. mp->ap = ap;
  182. mp->ncci = ncci;
  183. INIT_LIST_HEAD(&mp->ackqueue);
  184. spin_lock_init(&mp->ackqlock);
  185. skb_queue_head_init(&mp->inqueue);
  186. skb_queue_head_init(&mp->outqueue);
  187. tty_port_init(&mp->port);
  188. mp->port.ops = &capiminor_port_ops;
  189. /* Allocate the least unused minor number. */
  190. spin_lock(&capiminors_lock);
  191. for (minor = 0; minor < capi_ttyminors; minor++)
  192. if (!capiminors[minor]) {
  193. capiminors[minor] = mp;
  194. break;
  195. }
  196. spin_unlock(&capiminors_lock);
  197. if (minor == capi_ttyminors) {
  198. printk(KERN_NOTICE "capi: out of minors\n");
  199. goto err_out1;
  200. }
  201. mp->minor = minor;
  202. dev = tty_register_device(capinc_tty_driver, minor, NULL);
  203. if (IS_ERR(dev))
  204. goto err_out2;
  205. return mp;
  206. err_out2:
  207. spin_lock(&capiminors_lock);
  208. capiminors[minor] = NULL;
  209. spin_unlock(&capiminors_lock);
  210. err_out1:
  211. kfree(mp);
  212. return NULL;
  213. }
  214. static void capiminor_destroy(struct kref *kref)
  215. {
  216. struct capiminor *mp = container_of(kref, struct capiminor, kref);
  217. kfree_skb(mp->ttyskb);
  218. skb_queue_purge(&mp->inqueue);
  219. skb_queue_purge(&mp->outqueue);
  220. capiminor_del_all_ack(mp);
  221. kfree(mp);
  222. }
  223. static struct capiminor *capiminor_get(unsigned int minor)
  224. {
  225. struct capiminor *mp;
  226. spin_lock(&capiminors_lock);
  227. mp = capiminors[minor];
  228. if (mp)
  229. kref_get(&mp->kref);
  230. spin_unlock(&capiminors_lock);
  231. return mp;
  232. }
  233. static inline void capiminor_put(struct capiminor *mp)
  234. {
  235. kref_put(&mp->kref, capiminor_destroy);
  236. }
  237. static void capiminor_free(struct capiminor *mp)
  238. {
  239. tty_unregister_device(capinc_tty_driver, mp->minor);
  240. spin_lock(&capiminors_lock);
  241. capiminors[mp->minor] = NULL;
  242. spin_unlock(&capiminors_lock);
  243. capiminor_put(mp);
  244. }
  245. /* -------- struct capincci ----------------------------------------- */
  246. static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
  247. {
  248. struct capiminor *mp;
  249. dev_t device;
  250. if (!(cdev->userflags & CAPIFLAG_HIGHJACKING))
  251. return;
  252. mp = np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
  253. if (mp) {
  254. device = MKDEV(capinc_tty_driver->major, mp->minor);
  255. mp->capifs_dentry = capifs_new_ncci(mp->minor, device);
  256. }
  257. }
  258. static void capincci_free_minor(struct capincci *np)
  259. {
  260. struct capiminor *mp = np->minorp;
  261. struct tty_struct *tty;
  262. if (mp) {
  263. capifs_free_ncci(mp->capifs_dentry);
  264. tty = tty_port_tty_get(&mp->port);
  265. if (tty) {
  266. tty_vhangup(tty);
  267. tty_kref_put(tty);
  268. }
  269. capiminor_free(mp);
  270. }
  271. }
  272. static inline unsigned int capincci_minor_opencount(struct capincci *np)
  273. {
  274. struct capiminor *mp = np->minorp;
  275. unsigned int count = 0;
  276. struct tty_struct *tty;
  277. if (mp) {
  278. tty = tty_port_tty_get(&mp->port);
  279. if (tty) {
  280. count = tty->count;
  281. tty_kref_put(tty);
  282. }
  283. }
  284. return count;
  285. }
  286. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  287. static inline void
  288. capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
  289. static inline void capincci_free_minor(struct capincci *np) { }
  290. static inline unsigned int capincci_minor_opencount(struct capincci *np)
  291. {
  292. return 0;
  293. }
  294. #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  295. static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
  296. {
  297. struct capincci *np;
  298. np = kzalloc(sizeof(*np), GFP_KERNEL);
  299. if (!np)
  300. return NULL;
  301. np->ncci = ncci;
  302. np->cdev = cdev;
  303. capincci_alloc_minor(cdev, np);
  304. list_add_tail(&np->list, &cdev->nccis);
  305. return np;
  306. }
  307. static void capincci_free(struct capidev *cdev, u32 ncci)
  308. {
  309. struct capincci *np, *tmp;
  310. list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
  311. if (ncci == 0xffffffff || np->ncci == ncci) {
  312. capincci_free_minor(np);
  313. list_del(&np->list);
  314. kfree(np);
  315. }
  316. }
  317. static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
  318. {
  319. struct capincci *np;
  320. list_for_each_entry(np, &cdev->nccis, list)
  321. if (np->ncci == ncci)
  322. return np;
  323. return NULL;
  324. }
  325. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  326. /* -------- handle data queue --------------------------------------- */
  327. static struct sk_buff *
  328. gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
  329. {
  330. struct sk_buff *nskb;
  331. nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_ATOMIC);
  332. if (nskb) {
  333. u16 datahandle = CAPIMSG_U16(skb->data,CAPIMSG_BASELEN+4+4+2);
  334. unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
  335. capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
  336. capimsg_setu16(s, 2, mp->ap->applid);
  337. capimsg_setu8 (s, 4, CAPI_DATA_B3);
  338. capimsg_setu8 (s, 5, CAPI_RESP);
  339. capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
  340. capimsg_setu32(s, 8, mp->ncci);
  341. capimsg_setu16(s, 12, datahandle);
  342. }
  343. return nskb;
  344. }
  345. static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
  346. {
  347. unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
  348. struct tty_struct *tty;
  349. struct sk_buff *nskb;
  350. u16 errcode, datahandle;
  351. struct tty_ldisc *ld;
  352. int ret = -1;
  353. tty = tty_port_tty_get(&mp->port);
  354. if (!tty) {
  355. #ifdef _DEBUG_DATAFLOW
  356. printk(KERN_DEBUG "capi: currently no receiver\n");
  357. #endif
  358. return -1;
  359. }
  360. ld = tty_ldisc_ref(tty);
  361. if (!ld) {
  362. /* fatal error, do not requeue */
  363. ret = 0;
  364. kfree_skb(skb);
  365. goto deref_tty;
  366. }
  367. if (ld->ops->receive_buf == NULL) {
  368. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  369. printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
  370. #endif
  371. /* fatal error, do not requeue */
  372. goto free_skb;
  373. }
  374. if (mp->ttyinstop) {
  375. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  376. printk(KERN_DEBUG "capi: recv tty throttled\n");
  377. #endif
  378. goto deref_ldisc;
  379. }
  380. if (tty->receive_room < datalen) {
  381. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  382. printk(KERN_DEBUG "capi: no room in tty\n");
  383. #endif
  384. goto deref_ldisc;
  385. }
  386. nskb = gen_data_b3_resp_for(mp, skb);
  387. if (!nskb) {
  388. printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
  389. goto deref_ldisc;
  390. }
  391. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
  392. errcode = capi20_put_message(mp->ap, nskb);
  393. if (errcode == CAPI_NOERROR) {
  394. skb_pull(skb, CAPIMSG_LEN(skb->data));
  395. #ifdef _DEBUG_DATAFLOW
  396. printk(KERN_DEBUG "capi: DATA_B3_RESP %u len=%d => ldisc\n",
  397. datahandle, skb->len);
  398. #endif
  399. ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
  400. } else {
  401. printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
  402. errcode);
  403. kfree_skb(nskb);
  404. if (errcode == CAPI_SENDQUEUEFULL)
  405. goto deref_ldisc;
  406. }
  407. free_skb:
  408. ret = 0;
  409. kfree_skb(skb);
  410. deref_ldisc:
  411. tty_ldisc_deref(ld);
  412. deref_tty:
  413. tty_kref_put(tty);
  414. return ret;
  415. }
  416. static void handle_minor_recv(struct capiminor *mp)
  417. {
  418. struct sk_buff *skb;
  419. while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
  420. if (handle_recv_skb(mp, skb) < 0) {
  421. skb_queue_head(&mp->inqueue, skb);
  422. return;
  423. }
  424. }
  425. static int handle_minor_send(struct capiminor *mp)
  426. {
  427. struct tty_struct *tty;
  428. struct sk_buff *skb;
  429. u16 len;
  430. int count = 0;
  431. u16 errcode;
  432. u16 datahandle;
  433. tty = tty_port_tty_get(&mp->port);
  434. if (!tty)
  435. return 0;
  436. if (mp->ttyoutstop) {
  437. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  438. printk(KERN_DEBUG "capi: send: tty stopped\n");
  439. #endif
  440. tty_kref_put(tty);
  441. return 0;
  442. }
  443. while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
  444. datahandle = atomic_inc_return(&mp->datahandle);
  445. len = (u16)skb->len;
  446. skb_push(skb, CAPI_DATA_B3_REQ_LEN);
  447. memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
  448. capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
  449. capimsg_setu16(skb->data, 2, mp->ap->applid);
  450. capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
  451. capimsg_setu8 (skb->data, 5, CAPI_REQ);
  452. capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
  453. capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
  454. capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
  455. capimsg_setu16(skb->data, 16, len); /* Data length */
  456. capimsg_setu16(skb->data, 18, datahandle);
  457. capimsg_setu16(skb->data, 20, 0); /* Flags */
  458. if (capiminor_add_ack(mp, datahandle) < 0) {
  459. skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
  460. skb_queue_head(&mp->outqueue, skb);
  461. tty_kref_put(tty);
  462. return count;
  463. }
  464. errcode = capi20_put_message(mp->ap, skb);
  465. if (errcode == CAPI_NOERROR) {
  466. count++;
  467. mp->outbytes -= len;
  468. #ifdef _DEBUG_DATAFLOW
  469. printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
  470. datahandle, len);
  471. #endif
  472. continue;
  473. }
  474. capiminor_del_ack(mp, datahandle);
  475. if (errcode == CAPI_SENDQUEUEFULL) {
  476. skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
  477. skb_queue_head(&mp->outqueue, skb);
  478. break;
  479. }
  480. /* ups, drop packet */
  481. printk(KERN_ERR "capi: put_message = %x\n", errcode);
  482. mp->outbytes -= len;
  483. kfree_skb(skb);
  484. }
  485. tty_kref_put(tty);
  486. return count;
  487. }
  488. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  489. /* -------- function called by lower level -------------------------- */
  490. static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
  491. {
  492. struct capidev *cdev = ap->private;
  493. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  494. struct tty_struct *tty;
  495. struct capiminor *mp;
  496. u16 datahandle;
  497. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  498. struct capincci *np;
  499. unsigned long flags;
  500. mutex_lock(&cdev->lock);
  501. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
  502. u16 info = CAPIMSG_U16(skb->data, 12); // Info field
  503. if ((info & 0xff00) == 0)
  504. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  505. }
  506. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
  507. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  508. spin_lock_irqsave(&workaround_lock, flags);
  509. if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
  510. skb_queue_tail(&cdev->recvqueue, skb);
  511. wake_up_interruptible(&cdev->recvwait);
  512. goto unlock_out;
  513. }
  514. np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
  515. if (!np) {
  516. printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
  517. skb_queue_tail(&cdev->recvqueue, skb);
  518. wake_up_interruptible(&cdev->recvwait);
  519. goto unlock_out;
  520. }
  521. #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
  522. skb_queue_tail(&cdev->recvqueue, skb);
  523. wake_up_interruptible(&cdev->recvwait);
  524. #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  525. mp = np->minorp;
  526. if (!mp) {
  527. skb_queue_tail(&cdev->recvqueue, skb);
  528. wake_up_interruptible(&cdev->recvwait);
  529. goto unlock_out;
  530. }
  531. if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
  532. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
  533. #ifdef _DEBUG_DATAFLOW
  534. printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
  535. datahandle, skb->len-CAPIMSG_LEN(skb->data));
  536. #endif
  537. skb_queue_tail(&mp->inqueue, skb);
  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. spin_unlock_irqrestore(&workaround_lock, flags);
  927. return count;
  928. }
  929. static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
  930. {
  931. struct capiminor *mp = tty->driver_data;
  932. struct sk_buff *skb;
  933. unsigned long flags;
  934. int ret = 1;
  935. #ifdef _DEBUG_TTYFUNCS
  936. printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
  937. #endif
  938. spin_lock_irqsave(&workaround_lock, flags);
  939. skb = mp->ttyskb;
  940. if (skb) {
  941. if (skb_tailroom(skb) > 0) {
  942. *(skb_put(skb, 1)) = ch;
  943. spin_unlock_irqrestore(&workaround_lock, flags);
  944. return 1;
  945. }
  946. mp->ttyskb = NULL;
  947. skb_queue_tail(&mp->outqueue, skb);
  948. mp->outbytes += skb->len;
  949. (void)handle_minor_send(mp);
  950. }
  951. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
  952. if (skb) {
  953. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  954. *(skb_put(skb, 1)) = ch;
  955. mp->ttyskb = skb;
  956. } else {
  957. printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
  958. ret = 0;
  959. }
  960. spin_unlock_irqrestore(&workaround_lock, flags);
  961. return ret;
  962. }
  963. static void capinc_tty_flush_chars(struct tty_struct *tty)
  964. {
  965. struct capiminor *mp = tty->driver_data;
  966. struct sk_buff *skb;
  967. unsigned long flags;
  968. #ifdef _DEBUG_TTYFUNCS
  969. printk(KERN_DEBUG "capinc_tty_flush_chars\n");
  970. #endif
  971. spin_lock_irqsave(&workaround_lock, flags);
  972. skb = mp->ttyskb;
  973. if (skb) {
  974. mp->ttyskb = NULL;
  975. skb_queue_tail(&mp->outqueue, skb);
  976. mp->outbytes += skb->len;
  977. (void)handle_minor_send(mp);
  978. }
  979. (void)handle_minor_recv(mp);
  980. spin_unlock_irqrestore(&workaround_lock, flags);
  981. }
  982. static int capinc_tty_write_room(struct tty_struct *tty)
  983. {
  984. struct capiminor *mp = tty->driver_data;
  985. int room;
  986. room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
  987. room *= CAPI_MAX_BLKSIZE;
  988. #ifdef _DEBUG_TTYFUNCS
  989. printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
  990. #endif
  991. return room;
  992. }
  993. static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
  994. {
  995. struct capiminor *mp = tty->driver_data;
  996. #ifdef _DEBUG_TTYFUNCS
  997. printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
  998. mp->outbytes, mp->nack,
  999. skb_queue_len(&mp->outqueue),
  1000. skb_queue_len(&mp->inqueue));
  1001. #endif
  1002. return mp->outbytes;
  1003. }
  1004. static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
  1005. unsigned int cmd, unsigned long arg)
  1006. {
  1007. int error = 0;
  1008. switch (cmd) {
  1009. default:
  1010. error = n_tty_ioctl_helper(tty, file, cmd, arg);
  1011. break;
  1012. }
  1013. return error;
  1014. }
  1015. static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
  1016. {
  1017. #ifdef _DEBUG_TTYFUNCS
  1018. printk(KERN_DEBUG "capinc_tty_set_termios\n");
  1019. #endif
  1020. }
  1021. static void capinc_tty_throttle(struct tty_struct *tty)
  1022. {
  1023. struct capiminor *mp = tty->driver_data;
  1024. #ifdef _DEBUG_TTYFUNCS
  1025. printk(KERN_DEBUG "capinc_tty_throttle\n");
  1026. #endif
  1027. mp->ttyinstop = 1;
  1028. }
  1029. static void capinc_tty_unthrottle(struct tty_struct *tty)
  1030. {
  1031. struct capiminor *mp = tty->driver_data;
  1032. unsigned long flags;
  1033. #ifdef _DEBUG_TTYFUNCS
  1034. printk(KERN_DEBUG "capinc_tty_unthrottle\n");
  1035. #endif
  1036. spin_lock_irqsave(&workaround_lock, flags);
  1037. mp->ttyinstop = 0;
  1038. handle_minor_recv(mp);
  1039. spin_unlock_irqrestore(&workaround_lock, flags);
  1040. }
  1041. static void capinc_tty_stop(struct tty_struct *tty)
  1042. {
  1043. struct capiminor *mp = tty->driver_data;
  1044. #ifdef _DEBUG_TTYFUNCS
  1045. printk(KERN_DEBUG "capinc_tty_stop\n");
  1046. #endif
  1047. mp->ttyoutstop = 1;
  1048. }
  1049. static void capinc_tty_start(struct tty_struct *tty)
  1050. {
  1051. struct capiminor *mp = tty->driver_data;
  1052. unsigned long flags;
  1053. #ifdef _DEBUG_TTYFUNCS
  1054. printk(KERN_DEBUG "capinc_tty_start\n");
  1055. #endif
  1056. spin_lock_irqsave(&workaround_lock, flags);
  1057. mp->ttyoutstop = 0;
  1058. (void)handle_minor_send(mp);
  1059. spin_unlock_irqrestore(&workaround_lock, flags);
  1060. }
  1061. static void capinc_tty_hangup(struct tty_struct *tty)
  1062. {
  1063. struct capiminor *mp = tty->driver_data;
  1064. #ifdef _DEBUG_TTYFUNCS
  1065. printk(KERN_DEBUG "capinc_tty_hangup\n");
  1066. #endif
  1067. tty_port_hangup(&mp->port);
  1068. }
  1069. static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
  1070. {
  1071. #ifdef _DEBUG_TTYFUNCS
  1072. printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
  1073. #endif
  1074. return 0;
  1075. }
  1076. static void capinc_tty_flush_buffer(struct tty_struct *tty)
  1077. {
  1078. #ifdef _DEBUG_TTYFUNCS
  1079. printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
  1080. #endif
  1081. }
  1082. static void capinc_tty_set_ldisc(struct tty_struct *tty)
  1083. {
  1084. #ifdef _DEBUG_TTYFUNCS
  1085. printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
  1086. #endif
  1087. }
  1088. static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
  1089. {
  1090. #ifdef _DEBUG_TTYFUNCS
  1091. printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
  1092. #endif
  1093. }
  1094. static const struct tty_operations capinc_ops = {
  1095. .open = capinc_tty_open,
  1096. .close = capinc_tty_close,
  1097. .write = capinc_tty_write,
  1098. .put_char = capinc_tty_put_char,
  1099. .flush_chars = capinc_tty_flush_chars,
  1100. .write_room = capinc_tty_write_room,
  1101. .chars_in_buffer = capinc_tty_chars_in_buffer,
  1102. .ioctl = capinc_tty_ioctl,
  1103. .set_termios = capinc_tty_set_termios,
  1104. .throttle = capinc_tty_throttle,
  1105. .unthrottle = capinc_tty_unthrottle,
  1106. .stop = capinc_tty_stop,
  1107. .start = capinc_tty_start,
  1108. .hangup = capinc_tty_hangup,
  1109. .break_ctl = capinc_tty_break_ctl,
  1110. .flush_buffer = capinc_tty_flush_buffer,
  1111. .set_ldisc = capinc_tty_set_ldisc,
  1112. .send_xchar = capinc_tty_send_xchar,
  1113. .install = capinc_tty_install,
  1114. .cleanup = capinc_tty_cleanup,
  1115. };
  1116. static int __init capinc_tty_init(void)
  1117. {
  1118. struct tty_driver *drv;
  1119. int err;
  1120. if (capi_ttyminors > CAPINC_MAX_PORTS)
  1121. capi_ttyminors = CAPINC_MAX_PORTS;
  1122. if (capi_ttyminors <= 0)
  1123. capi_ttyminors = CAPINC_NR_PORTS;
  1124. capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
  1125. GFP_KERNEL);
  1126. if (!capiminors)
  1127. return -ENOMEM;
  1128. drv = alloc_tty_driver(capi_ttyminors);
  1129. if (!drv) {
  1130. kfree(capiminors);
  1131. return -ENOMEM;
  1132. }
  1133. drv->owner = THIS_MODULE;
  1134. drv->driver_name = "capi_nc";
  1135. drv->name = "capi";
  1136. drv->major = 0;
  1137. drv->minor_start = 0;
  1138. drv->type = TTY_DRIVER_TYPE_SERIAL;
  1139. drv->subtype = SERIAL_TYPE_NORMAL;
  1140. drv->init_termios = tty_std_termios;
  1141. drv->init_termios.c_iflag = ICRNL;
  1142. drv->init_termios.c_oflag = OPOST | ONLCR;
  1143. drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1144. drv->init_termios.c_lflag = 0;
  1145. drv->flags =
  1146. TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
  1147. TTY_DRIVER_DYNAMIC_DEV;
  1148. tty_set_operations(drv, &capinc_ops);
  1149. err = tty_register_driver(drv);
  1150. if (err) {
  1151. put_tty_driver(drv);
  1152. kfree(capiminors);
  1153. printk(KERN_ERR "Couldn't register capi_nc driver\n");
  1154. return err;
  1155. }
  1156. capinc_tty_driver = drv;
  1157. return 0;
  1158. }
  1159. static void __exit capinc_tty_exit(void)
  1160. {
  1161. tty_unregister_driver(capinc_tty_driver);
  1162. put_tty_driver(capinc_tty_driver);
  1163. kfree(capiminors);
  1164. }
  1165. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1166. static inline int capinc_tty_init(void)
  1167. {
  1168. return 0;
  1169. }
  1170. static inline void capinc_tty_exit(void) { }
  1171. #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1172. /* -------- /proc functions ----------------------------------------- */
  1173. /*
  1174. * /proc/capi/capi20:
  1175. * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
  1176. */
  1177. static int capi20_proc_show(struct seq_file *m, void *v)
  1178. {
  1179. struct capidev *cdev;
  1180. struct list_head *l;
  1181. mutex_lock(&capidev_list_lock);
  1182. list_for_each(l, &capidev_list) {
  1183. cdev = list_entry(l, struct capidev, list);
  1184. seq_printf(m, "0 %d %lu %lu %lu %lu\n",
  1185. cdev->ap.applid,
  1186. cdev->ap.nrecvctlpkt,
  1187. cdev->ap.nrecvdatapkt,
  1188. cdev->ap.nsentctlpkt,
  1189. cdev->ap.nsentdatapkt);
  1190. }
  1191. mutex_unlock(&capidev_list_lock);
  1192. return 0;
  1193. }
  1194. static int capi20_proc_open(struct inode *inode, struct file *file)
  1195. {
  1196. return single_open(file, capi20_proc_show, NULL);
  1197. }
  1198. static const struct file_operations capi20_proc_fops = {
  1199. .owner = THIS_MODULE,
  1200. .open = capi20_proc_open,
  1201. .read = seq_read,
  1202. .llseek = seq_lseek,
  1203. .release = single_release,
  1204. };
  1205. /*
  1206. * /proc/capi/capi20ncci:
  1207. * applid ncci
  1208. */
  1209. static int capi20ncci_proc_show(struct seq_file *m, void *v)
  1210. {
  1211. struct capidev *cdev;
  1212. struct capincci *np;
  1213. mutex_lock(&capidev_list_lock);
  1214. list_for_each_entry(cdev, &capidev_list, list) {
  1215. mutex_lock(&cdev->lock);
  1216. list_for_each_entry(np, &cdev->nccis, list)
  1217. seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
  1218. mutex_unlock(&cdev->lock);
  1219. }
  1220. mutex_unlock(&capidev_list_lock);
  1221. return 0;
  1222. }
  1223. static int capi20ncci_proc_open(struct inode *inode, struct file *file)
  1224. {
  1225. return single_open(file, capi20ncci_proc_show, NULL);
  1226. }
  1227. static const struct file_operations capi20ncci_proc_fops = {
  1228. .owner = THIS_MODULE,
  1229. .open = capi20ncci_proc_open,
  1230. .read = seq_read,
  1231. .llseek = seq_lseek,
  1232. .release = single_release,
  1233. };
  1234. static void __init proc_init(void)
  1235. {
  1236. proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
  1237. proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
  1238. }
  1239. static void __exit proc_exit(void)
  1240. {
  1241. remove_proc_entry("capi/capi20", NULL);
  1242. remove_proc_entry("capi/capi20ncci", NULL);
  1243. }
  1244. /* -------- init function and module interface ---------------------- */
  1245. static int __init capi_init(void)
  1246. {
  1247. const char *compileinfo;
  1248. int major_ret;
  1249. major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
  1250. if (major_ret < 0) {
  1251. printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
  1252. return major_ret;
  1253. }
  1254. capi_class = class_create(THIS_MODULE, "capi");
  1255. if (IS_ERR(capi_class)) {
  1256. unregister_chrdev(capi_major, "capi20");
  1257. return PTR_ERR(capi_class);
  1258. }
  1259. device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
  1260. if (capinc_tty_init() < 0) {
  1261. device_destroy(capi_class, MKDEV(capi_major, 0));
  1262. class_destroy(capi_class);
  1263. unregister_chrdev(capi_major, "capi20");
  1264. return -ENOMEM;
  1265. }
  1266. proc_init();
  1267. #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
  1268. compileinfo = " (middleware+capifs)";
  1269. #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
  1270. compileinfo = " (no capifs)";
  1271. #else
  1272. compileinfo = " (no middleware)";
  1273. #endif
  1274. printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
  1275. capi_major, compileinfo);
  1276. return 0;
  1277. }
  1278. static void __exit capi_exit(void)
  1279. {
  1280. proc_exit();
  1281. device_destroy(capi_class, MKDEV(capi_major, 0));
  1282. class_destroy(capi_class);
  1283. unregister_chrdev(capi_major, "capi20");
  1284. capinc_tty_exit();
  1285. }
  1286. module_init(capi_init);
  1287. module_exit(capi_exit);