capi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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. unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
  350. struct tty_struct *tty;
  351. struct sk_buff *nskb;
  352. u16 errcode, datahandle;
  353. struct tty_ldisc *ld;
  354. int ret = -1;
  355. tty = tty_port_tty_get(&mp->port);
  356. if (!tty) {
  357. #ifdef _DEBUG_DATAFLOW
  358. printk(KERN_DEBUG "capi: currently no receiver\n");
  359. #endif
  360. return -1;
  361. }
  362. ld = tty_ldisc_ref(tty);
  363. if (!ld) {
  364. /* fatal error, do not requeue */
  365. ret = 0;
  366. kfree_skb(skb);
  367. goto deref_tty;
  368. }
  369. if (ld->ops->receive_buf == NULL) {
  370. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  371. printk(KERN_DEBUG "capi: ldisc has no receive_buf function\n");
  372. #endif
  373. /* fatal error, do not requeue */
  374. goto free_skb;
  375. }
  376. if (mp->ttyinstop) {
  377. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  378. printk(KERN_DEBUG "capi: recv tty throttled\n");
  379. #endif
  380. goto deref_ldisc;
  381. }
  382. if (tty->receive_room < datalen) {
  383. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  384. printk(KERN_DEBUG "capi: no room in tty\n");
  385. #endif
  386. goto deref_ldisc;
  387. }
  388. nskb = gen_data_b3_resp_for(mp, skb);
  389. if (!nskb) {
  390. printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
  391. goto deref_ldisc;
  392. }
  393. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
  394. errcode = capi20_put_message(mp->ap, nskb);
  395. if (errcode == CAPI_NOERROR) {
  396. 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. } else {
  403. printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
  404. errcode);
  405. kfree_skb(nskb);
  406. if (errcode == CAPI_SENDQUEUEFULL)
  407. goto deref_ldisc;
  408. }
  409. free_skb:
  410. ret = 0;
  411. kfree_skb(skb);
  412. deref_ldisc:
  413. tty_ldisc_deref(ld);
  414. deref_tty:
  415. tty_kref_put(tty);
  416. return ret;
  417. }
  418. static void handle_minor_recv(struct capiminor *mp)
  419. {
  420. struct sk_buff *skb;
  421. while ((skb = skb_dequeue(&mp->inqueue)) != NULL) {
  422. unsigned int len = skb->len;
  423. mp->inbytes -= len;
  424. if (handle_recv_skb(mp, skb) < 0) {
  425. skb_queue_head(&mp->inqueue, skb);
  426. mp->inbytes += len;
  427. return;
  428. }
  429. }
  430. }
  431. static int handle_minor_send(struct capiminor *mp)
  432. {
  433. struct tty_struct *tty;
  434. struct sk_buff *skb;
  435. u16 len;
  436. int count = 0;
  437. u16 errcode;
  438. u16 datahandle;
  439. tty = tty_port_tty_get(&mp->port);
  440. if (!tty)
  441. return 0;
  442. if (mp->ttyoutstop) {
  443. #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
  444. printk(KERN_DEBUG "capi: send: tty stopped\n");
  445. #endif
  446. tty_kref_put(tty);
  447. return 0;
  448. }
  449. while ((skb = skb_dequeue(&mp->outqueue)) != NULL) {
  450. datahandle = mp->datahandle;
  451. len = (u16)skb->len;
  452. skb_push(skb, CAPI_DATA_B3_REQ_LEN);
  453. memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
  454. capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
  455. capimsg_setu16(skb->data, 2, mp->ap->applid);
  456. capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
  457. capimsg_setu8 (skb->data, 5, CAPI_REQ);
  458. capimsg_setu16(skb->data, 6, mp->msgid++);
  459. capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
  460. capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
  461. capimsg_setu16(skb->data, 16, len); /* Data length */
  462. capimsg_setu16(skb->data, 18, datahandle);
  463. capimsg_setu16(skb->data, 20, 0); /* Flags */
  464. if (capiminor_add_ack(mp, datahandle) < 0) {
  465. skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
  466. skb_queue_head(&mp->outqueue, skb);
  467. tty_kref_put(tty);
  468. return count;
  469. }
  470. errcode = capi20_put_message(mp->ap, skb);
  471. if (errcode == CAPI_NOERROR) {
  472. mp->datahandle++;
  473. count++;
  474. mp->outbytes -= len;
  475. #ifdef _DEBUG_DATAFLOW
  476. printk(KERN_DEBUG "capi: DATA_B3_REQ %u len=%u\n",
  477. datahandle, len);
  478. #endif
  479. continue;
  480. }
  481. capiminor_del_ack(mp, datahandle);
  482. if (errcode == CAPI_SENDQUEUEFULL) {
  483. skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
  484. skb_queue_head(&mp->outqueue, skb);
  485. break;
  486. }
  487. /* ups, drop packet */
  488. printk(KERN_ERR "capi: put_message = %x\n", errcode);
  489. mp->outbytes -= len;
  490. kfree_skb(skb);
  491. }
  492. tty_kref_put(tty);
  493. return count;
  494. }
  495. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  496. /* -------- function called by lower level -------------------------- */
  497. static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
  498. {
  499. struct capidev *cdev = ap->private;
  500. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  501. struct tty_struct *tty;
  502. struct capiminor *mp;
  503. u16 datahandle;
  504. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  505. struct capincci *np;
  506. unsigned long flags;
  507. mutex_lock(&cdev->lock);
  508. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
  509. u16 info = CAPIMSG_U16(skb->data, 12); // Info field
  510. if ((info & 0xff00) == 0)
  511. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  512. }
  513. if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
  514. capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
  515. spin_lock_irqsave(&workaround_lock, flags);
  516. if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
  517. skb_queue_tail(&cdev->recvqueue, skb);
  518. wake_up_interruptible(&cdev->recvwait);
  519. goto unlock_out;
  520. }
  521. np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
  522. if (!np) {
  523. printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
  524. skb_queue_tail(&cdev->recvqueue, skb);
  525. wake_up_interruptible(&cdev->recvwait);
  526. goto unlock_out;
  527. }
  528. #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
  529. skb_queue_tail(&cdev->recvqueue, skb);
  530. wake_up_interruptible(&cdev->recvwait);
  531. #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  532. mp = np->minorp;
  533. if (!mp) {
  534. skb_queue_tail(&cdev->recvqueue, skb);
  535. wake_up_interruptible(&cdev->recvwait);
  536. goto unlock_out;
  537. }
  538. if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
  539. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+4+2);
  540. #ifdef _DEBUG_DATAFLOW
  541. printk(KERN_DEBUG "capi_signal: DATA_B3_IND %u len=%d\n",
  542. datahandle, skb->len-CAPIMSG_LEN(skb->data));
  543. #endif
  544. skb_queue_tail(&mp->inqueue, skb);
  545. mp->inbytes += skb->len;
  546. handle_minor_recv(mp);
  547. } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
  548. datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4);
  549. #ifdef _DEBUG_DATAFLOW
  550. printk(KERN_DEBUG "capi_signal: DATA_B3_CONF %u 0x%x\n",
  551. datahandle,
  552. CAPIMSG_U16(skb->data, CAPIMSG_BASELEN+4+2));
  553. #endif
  554. kfree_skb(skb);
  555. (void)capiminor_del_ack(mp, datahandle);
  556. tty = tty_port_tty_get(&mp->port);
  557. if (tty) {
  558. tty_wakeup(tty);
  559. tty_kref_put(tty);
  560. }
  561. (void)handle_minor_send(mp);
  562. } else {
  563. /* ups, let capi application handle it :-) */
  564. skb_queue_tail(&cdev->recvqueue, skb);
  565. wake_up_interruptible(&cdev->recvwait);
  566. }
  567. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  568. unlock_out:
  569. spin_unlock_irqrestore(&workaround_lock, flags);
  570. mutex_unlock(&cdev->lock);
  571. }
  572. /* -------- file_operations for capidev ----------------------------- */
  573. static ssize_t
  574. capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  575. {
  576. struct capidev *cdev = (struct capidev *)file->private_data;
  577. struct sk_buff *skb;
  578. size_t copied;
  579. int err;
  580. if (!cdev->ap.applid)
  581. return -ENODEV;
  582. skb = skb_dequeue(&cdev->recvqueue);
  583. if (!skb) {
  584. if (file->f_flags & O_NONBLOCK)
  585. return -EAGAIN;
  586. err = wait_event_interruptible(cdev->recvwait,
  587. (skb = skb_dequeue(&cdev->recvqueue)));
  588. if (err)
  589. return err;
  590. }
  591. if (skb->len > count) {
  592. skb_queue_head(&cdev->recvqueue, skb);
  593. return -EMSGSIZE;
  594. }
  595. if (copy_to_user(buf, skb->data, skb->len)) {
  596. skb_queue_head(&cdev->recvqueue, skb);
  597. return -EFAULT;
  598. }
  599. copied = skb->len;
  600. kfree_skb(skb);
  601. return copied;
  602. }
  603. static ssize_t
  604. capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  605. {
  606. struct capidev *cdev = (struct capidev *)file->private_data;
  607. struct sk_buff *skb;
  608. u16 mlen;
  609. if (!cdev->ap.applid)
  610. return -ENODEV;
  611. skb = alloc_skb(count, GFP_USER);
  612. if (!skb)
  613. return -ENOMEM;
  614. if (copy_from_user(skb_put(skb, count), buf, count)) {
  615. kfree_skb(skb);
  616. return -EFAULT;
  617. }
  618. mlen = CAPIMSG_LEN(skb->data);
  619. if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
  620. if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
  621. kfree_skb(skb);
  622. return -EINVAL;
  623. }
  624. } else {
  625. if (mlen != count) {
  626. kfree_skb(skb);
  627. return -EINVAL;
  628. }
  629. }
  630. CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
  631. if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
  632. mutex_lock(&cdev->lock);
  633. capincci_free(cdev, CAPIMSG_NCCI(skb->data));
  634. mutex_unlock(&cdev->lock);
  635. }
  636. cdev->errcode = capi20_put_message(&cdev->ap, skb);
  637. if (cdev->errcode) {
  638. kfree_skb(skb);
  639. return -EIO;
  640. }
  641. return count;
  642. }
  643. static unsigned int
  644. capi_poll(struct file *file, poll_table * wait)
  645. {
  646. struct capidev *cdev = (struct capidev *)file->private_data;
  647. unsigned int mask = 0;
  648. if (!cdev->ap.applid)
  649. return POLLERR;
  650. poll_wait(file, &(cdev->recvwait), wait);
  651. mask = POLLOUT | POLLWRNORM;
  652. if (!skb_queue_empty(&cdev->recvqueue))
  653. mask |= POLLIN | POLLRDNORM;
  654. return mask;
  655. }
  656. static int
  657. capi_ioctl(struct inode *inode, struct file *file,
  658. unsigned int cmd, unsigned long arg)
  659. {
  660. struct capidev *cdev = file->private_data;
  661. capi_ioctl_struct data;
  662. int retval = -EINVAL;
  663. void __user *argp = (void __user *)arg;
  664. switch (cmd) {
  665. case CAPI_REGISTER:
  666. mutex_lock(&cdev->lock);
  667. if (cdev->ap.applid) {
  668. retval = -EEXIST;
  669. goto register_out;
  670. }
  671. if (copy_from_user(&cdev->ap.rparam, argp,
  672. sizeof(struct capi_register_params))) {
  673. retval = -EFAULT;
  674. goto register_out;
  675. }
  676. cdev->ap.private = cdev;
  677. cdev->ap.recv_message = capi_recv_message;
  678. cdev->errcode = capi20_register(&cdev->ap);
  679. retval = (int)cdev->ap.applid;
  680. if (cdev->errcode) {
  681. cdev->ap.applid = 0;
  682. retval = -EIO;
  683. }
  684. register_out:
  685. mutex_unlock(&cdev->lock);
  686. return retval;
  687. case CAPI_GET_VERSION:
  688. {
  689. if (copy_from_user(&data.contr, argp,
  690. sizeof(data.contr)))
  691. return -EFAULT;
  692. cdev->errcode = capi20_get_version(data.contr, &data.version);
  693. if (cdev->errcode)
  694. return -EIO;
  695. if (copy_to_user(argp, &data.version,
  696. sizeof(data.version)))
  697. return -EFAULT;
  698. }
  699. return 0;
  700. case CAPI_GET_SERIAL:
  701. {
  702. if (copy_from_user(&data.contr, argp,
  703. sizeof(data.contr)))
  704. return -EFAULT;
  705. cdev->errcode = capi20_get_serial (data.contr, data.serial);
  706. if (cdev->errcode)
  707. return -EIO;
  708. if (copy_to_user(argp, data.serial,
  709. sizeof(data.serial)))
  710. return -EFAULT;
  711. }
  712. return 0;
  713. case CAPI_GET_PROFILE:
  714. {
  715. if (copy_from_user(&data.contr, argp,
  716. sizeof(data.contr)))
  717. return -EFAULT;
  718. if (data.contr == 0) {
  719. cdev->errcode = capi20_get_profile(data.contr, &data.profile);
  720. if (cdev->errcode)
  721. return -EIO;
  722. retval = copy_to_user(argp,
  723. &data.profile.ncontroller,
  724. sizeof(data.profile.ncontroller));
  725. } else {
  726. cdev->errcode = capi20_get_profile(data.contr, &data.profile);
  727. if (cdev->errcode)
  728. return -EIO;
  729. retval = copy_to_user(argp, &data.profile,
  730. sizeof(data.profile));
  731. }
  732. if (retval)
  733. return -EFAULT;
  734. }
  735. return 0;
  736. case CAPI_GET_MANUFACTURER:
  737. {
  738. if (copy_from_user(&data.contr, argp,
  739. sizeof(data.contr)))
  740. return -EFAULT;
  741. cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
  742. if (cdev->errcode)
  743. return -EIO;
  744. if (copy_to_user(argp, data.manufacturer,
  745. sizeof(data.manufacturer)))
  746. return -EFAULT;
  747. }
  748. return 0;
  749. case CAPI_GET_ERRCODE:
  750. data.errcode = cdev->errcode;
  751. cdev->errcode = CAPI_NOERROR;
  752. if (arg) {
  753. if (copy_to_user(argp, &data.errcode,
  754. sizeof(data.errcode)))
  755. return -EFAULT;
  756. }
  757. return data.errcode;
  758. case CAPI_INSTALLED:
  759. if (capi20_isinstalled() == CAPI_NOERROR)
  760. return 0;
  761. return -ENXIO;
  762. case CAPI_MANUFACTURER_CMD:
  763. {
  764. struct capi_manufacturer_cmd mcmd;
  765. if (!capable(CAP_SYS_ADMIN))
  766. return -EPERM;
  767. if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
  768. return -EFAULT;
  769. return capi20_manufacturer(mcmd.cmd, mcmd.data);
  770. }
  771. return 0;
  772. case CAPI_SET_FLAGS:
  773. case CAPI_CLR_FLAGS: {
  774. unsigned userflags;
  775. if (copy_from_user(&userflags, argp, sizeof(userflags)))
  776. return -EFAULT;
  777. mutex_lock(&cdev->lock);
  778. if (cmd == CAPI_SET_FLAGS)
  779. cdev->userflags |= userflags;
  780. else
  781. cdev->userflags &= ~userflags;
  782. mutex_unlock(&cdev->lock);
  783. return 0;
  784. }
  785. case CAPI_GET_FLAGS:
  786. if (copy_to_user(argp, &cdev->userflags,
  787. sizeof(cdev->userflags)))
  788. return -EFAULT;
  789. return 0;
  790. case CAPI_NCCI_OPENCOUNT: {
  791. struct capincci *nccip;
  792. unsigned ncci;
  793. int count = 0;
  794. if (copy_from_user(&ncci, argp, sizeof(ncci)))
  795. return -EFAULT;
  796. mutex_lock(&cdev->lock);
  797. nccip = capincci_find(cdev, (u32)ncci);
  798. if (nccip)
  799. count = capincci_minor_opencount(nccip);
  800. mutex_unlock(&cdev->lock);
  801. return count;
  802. }
  803. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  804. case CAPI_NCCI_GETUNIT: {
  805. struct capincci *nccip;
  806. struct capiminor *mp;
  807. unsigned ncci;
  808. int unit = -ESRCH;
  809. if (copy_from_user(&ncci, argp, sizeof(ncci)))
  810. return -EFAULT;
  811. mutex_lock(&cdev->lock);
  812. nccip = capincci_find(cdev, (u32)ncci);
  813. if (nccip) {
  814. mp = nccip->minorp;
  815. if (mp)
  816. unit = mp->minor;
  817. }
  818. mutex_unlock(&cdev->lock);
  819. return unit;
  820. }
  821. #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
  822. default:
  823. return -EINVAL;
  824. }
  825. }
  826. static int capi_open(struct inode *inode, struct file *file)
  827. {
  828. struct capidev *cdev;
  829. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  830. if (!cdev)
  831. return -ENOMEM;
  832. mutex_init(&cdev->lock);
  833. skb_queue_head_init(&cdev->recvqueue);
  834. init_waitqueue_head(&cdev->recvwait);
  835. INIT_LIST_HEAD(&cdev->nccis);
  836. file->private_data = cdev;
  837. mutex_lock(&capidev_list_lock);
  838. list_add_tail(&cdev->list, &capidev_list);
  839. mutex_unlock(&capidev_list_lock);
  840. return nonseekable_open(inode, file);
  841. }
  842. static int capi_release(struct inode *inode, struct file *file)
  843. {
  844. struct capidev *cdev = file->private_data;
  845. mutex_lock(&capidev_list_lock);
  846. list_del(&cdev->list);
  847. mutex_unlock(&capidev_list_lock);
  848. if (cdev->ap.applid)
  849. capi20_release(&cdev->ap);
  850. skb_queue_purge(&cdev->recvqueue);
  851. capincci_free(cdev, 0xffffffff);
  852. kfree(cdev);
  853. return 0;
  854. }
  855. static const struct file_operations capi_fops =
  856. {
  857. .owner = THIS_MODULE,
  858. .llseek = no_llseek,
  859. .read = capi_read,
  860. .write = capi_write,
  861. .poll = capi_poll,
  862. .ioctl = capi_ioctl,
  863. .open = capi_open,
  864. .release = capi_release,
  865. };
  866. #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
  867. /* -------- tty_operations for capincci ----------------------------- */
  868. static int
  869. capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  870. {
  871. int idx = tty->index;
  872. struct capiminor *mp = capiminor_get(idx);
  873. int ret = tty_init_termios(tty);
  874. if (ret == 0) {
  875. tty_driver_kref_get(driver);
  876. tty->count++;
  877. tty->driver_data = mp;
  878. driver->ttys[idx] = tty;
  879. } else
  880. capiminor_put(mp);
  881. return ret;
  882. }
  883. static void capinc_tty_cleanup(struct tty_struct *tty)
  884. {
  885. struct capiminor *mp = tty->driver_data;
  886. tty->driver_data = NULL;
  887. capiminor_put(mp);
  888. }
  889. static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
  890. {
  891. struct capiminor *mp = tty->driver_data;
  892. unsigned long flags;
  893. int err;
  894. err = tty_port_open(&mp->port, tty, filp);
  895. if (err)
  896. return err;
  897. spin_lock_irqsave(&workaround_lock, flags);
  898. handle_minor_recv(mp);
  899. spin_unlock_irqrestore(&workaround_lock, flags);
  900. return 0;
  901. }
  902. static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
  903. {
  904. struct capiminor *mp = tty->driver_data;
  905. tty_port_close(&mp->port, tty, filp);
  906. }
  907. static int capinc_tty_write(struct tty_struct *tty,
  908. const unsigned char *buf, int count)
  909. {
  910. struct capiminor *mp = tty->driver_data;
  911. struct sk_buff *skb;
  912. unsigned long flags;
  913. #ifdef _DEBUG_TTYFUNCS
  914. printk(KERN_DEBUG "capinc_tty_write(count=%d)\n", count);
  915. #endif
  916. spin_lock_irqsave(&workaround_lock, flags);
  917. skb = mp->ttyskb;
  918. if (skb) {
  919. mp->ttyskb = NULL;
  920. skb_queue_tail(&mp->outqueue, skb);
  921. mp->outbytes += skb->len;
  922. }
  923. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+count, GFP_ATOMIC);
  924. if (!skb) {
  925. printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
  926. spin_unlock_irqrestore(&workaround_lock, flags);
  927. return -ENOMEM;
  928. }
  929. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  930. memcpy(skb_put(skb, count), buf, count);
  931. skb_queue_tail(&mp->outqueue, skb);
  932. mp->outbytes += skb->len;
  933. (void)handle_minor_send(mp);
  934. spin_unlock_irqrestore(&workaround_lock, flags);
  935. return count;
  936. }
  937. static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
  938. {
  939. struct capiminor *mp = tty->driver_data;
  940. struct sk_buff *skb;
  941. unsigned long flags;
  942. int ret = 1;
  943. #ifdef _DEBUG_TTYFUNCS
  944. printk(KERN_DEBUG "capinc_put_char(%u)\n", ch);
  945. #endif
  946. spin_lock_irqsave(&workaround_lock, flags);
  947. skb = mp->ttyskb;
  948. if (skb) {
  949. if (skb_tailroom(skb) > 0) {
  950. *(skb_put(skb, 1)) = ch;
  951. spin_unlock_irqrestore(&workaround_lock, flags);
  952. return 1;
  953. }
  954. mp->ttyskb = NULL;
  955. skb_queue_tail(&mp->outqueue, skb);
  956. mp->outbytes += skb->len;
  957. (void)handle_minor_send(mp);
  958. }
  959. skb = alloc_skb(CAPI_DATA_B3_REQ_LEN+CAPI_MAX_BLKSIZE, GFP_ATOMIC);
  960. if (skb) {
  961. skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
  962. *(skb_put(skb, 1)) = ch;
  963. mp->ttyskb = skb;
  964. } else {
  965. printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
  966. ret = 0;
  967. }
  968. spin_unlock_irqrestore(&workaround_lock, flags);
  969. return ret;
  970. }
  971. static void capinc_tty_flush_chars(struct tty_struct *tty)
  972. {
  973. struct capiminor *mp = tty->driver_data;
  974. struct sk_buff *skb;
  975. unsigned long flags;
  976. #ifdef _DEBUG_TTYFUNCS
  977. printk(KERN_DEBUG "capinc_tty_flush_chars\n");
  978. #endif
  979. spin_lock_irqsave(&workaround_lock, flags);
  980. skb = mp->ttyskb;
  981. if (skb) {
  982. mp->ttyskb = NULL;
  983. skb_queue_tail(&mp->outqueue, skb);
  984. mp->outbytes += skb->len;
  985. (void)handle_minor_send(mp);
  986. }
  987. (void)handle_minor_recv(mp);
  988. spin_unlock_irqrestore(&workaround_lock, flags);
  989. }
  990. static int capinc_tty_write_room(struct tty_struct *tty)
  991. {
  992. struct capiminor *mp = tty->driver_data;
  993. int room;
  994. room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
  995. room *= CAPI_MAX_BLKSIZE;
  996. #ifdef _DEBUG_TTYFUNCS
  997. printk(KERN_DEBUG "capinc_tty_write_room = %d\n", room);
  998. #endif
  999. return room;
  1000. }
  1001. static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
  1002. {
  1003. struct capiminor *mp = tty->driver_data;
  1004. #ifdef _DEBUG_TTYFUNCS
  1005. printk(KERN_DEBUG "capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
  1006. mp->outbytes, mp->nack,
  1007. skb_queue_len(&mp->outqueue),
  1008. skb_queue_len(&mp->inqueue));
  1009. #endif
  1010. return mp->outbytes;
  1011. }
  1012. static int capinc_tty_ioctl(struct tty_struct *tty, struct file * file,
  1013. unsigned int cmd, unsigned long arg)
  1014. {
  1015. int error = 0;
  1016. switch (cmd) {
  1017. default:
  1018. error = n_tty_ioctl_helper(tty, file, cmd, arg);
  1019. break;
  1020. }
  1021. return error;
  1022. }
  1023. static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
  1024. {
  1025. #ifdef _DEBUG_TTYFUNCS
  1026. printk(KERN_DEBUG "capinc_tty_set_termios\n");
  1027. #endif
  1028. }
  1029. static void capinc_tty_throttle(struct tty_struct *tty)
  1030. {
  1031. struct capiminor *mp = tty->driver_data;
  1032. #ifdef _DEBUG_TTYFUNCS
  1033. printk(KERN_DEBUG "capinc_tty_throttle\n");
  1034. #endif
  1035. mp->ttyinstop = 1;
  1036. }
  1037. static void capinc_tty_unthrottle(struct tty_struct *tty)
  1038. {
  1039. struct capiminor *mp = tty->driver_data;
  1040. unsigned long flags;
  1041. #ifdef _DEBUG_TTYFUNCS
  1042. printk(KERN_DEBUG "capinc_tty_unthrottle\n");
  1043. #endif
  1044. spin_lock_irqsave(&workaround_lock, flags);
  1045. mp->ttyinstop = 0;
  1046. handle_minor_recv(mp);
  1047. spin_unlock_irqrestore(&workaround_lock, flags);
  1048. }
  1049. static void capinc_tty_stop(struct tty_struct *tty)
  1050. {
  1051. struct capiminor *mp = tty->driver_data;
  1052. #ifdef _DEBUG_TTYFUNCS
  1053. printk(KERN_DEBUG "capinc_tty_stop\n");
  1054. #endif
  1055. mp->ttyoutstop = 1;
  1056. }
  1057. static void capinc_tty_start(struct tty_struct *tty)
  1058. {
  1059. struct capiminor *mp = tty->driver_data;
  1060. unsigned long flags;
  1061. #ifdef _DEBUG_TTYFUNCS
  1062. printk(KERN_DEBUG "capinc_tty_start\n");
  1063. #endif
  1064. spin_lock_irqsave(&workaround_lock, flags);
  1065. mp->ttyoutstop = 0;
  1066. (void)handle_minor_send(mp);
  1067. spin_unlock_irqrestore(&workaround_lock, flags);
  1068. }
  1069. static void capinc_tty_hangup(struct tty_struct *tty)
  1070. {
  1071. struct capiminor *mp = tty->driver_data;
  1072. #ifdef _DEBUG_TTYFUNCS
  1073. printk(KERN_DEBUG "capinc_tty_hangup\n");
  1074. #endif
  1075. tty_port_hangup(&mp->port);
  1076. }
  1077. static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
  1078. {
  1079. #ifdef _DEBUG_TTYFUNCS
  1080. printk(KERN_DEBUG "capinc_tty_break_ctl(%d)\n", state);
  1081. #endif
  1082. return 0;
  1083. }
  1084. static void capinc_tty_flush_buffer(struct tty_struct *tty)
  1085. {
  1086. #ifdef _DEBUG_TTYFUNCS
  1087. printk(KERN_DEBUG "capinc_tty_flush_buffer\n");
  1088. #endif
  1089. }
  1090. static void capinc_tty_set_ldisc(struct tty_struct *tty)
  1091. {
  1092. #ifdef _DEBUG_TTYFUNCS
  1093. printk(KERN_DEBUG "capinc_tty_set_ldisc\n");
  1094. #endif
  1095. }
  1096. static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
  1097. {
  1098. #ifdef _DEBUG_TTYFUNCS
  1099. printk(KERN_DEBUG "capinc_tty_send_xchar(%d)\n", ch);
  1100. #endif
  1101. }
  1102. static const struct tty_operations capinc_ops = {
  1103. .open = capinc_tty_open,
  1104. .close = capinc_tty_close,
  1105. .write = capinc_tty_write,
  1106. .put_char = capinc_tty_put_char,
  1107. .flush_chars = capinc_tty_flush_chars,
  1108. .write_room = capinc_tty_write_room,
  1109. .chars_in_buffer = capinc_tty_chars_in_buffer,
  1110. .ioctl = capinc_tty_ioctl,
  1111. .set_termios = capinc_tty_set_termios,
  1112. .throttle = capinc_tty_throttle,
  1113. .unthrottle = capinc_tty_unthrottle,
  1114. .stop = capinc_tty_stop,
  1115. .start = capinc_tty_start,
  1116. .hangup = capinc_tty_hangup,
  1117. .break_ctl = capinc_tty_break_ctl,
  1118. .flush_buffer = capinc_tty_flush_buffer,
  1119. .set_ldisc = capinc_tty_set_ldisc,
  1120. .send_xchar = capinc_tty_send_xchar,
  1121. .install = capinc_tty_install,
  1122. .cleanup = capinc_tty_cleanup,
  1123. };
  1124. static int __init capinc_tty_init(void)
  1125. {
  1126. struct tty_driver *drv;
  1127. int err;
  1128. if (capi_ttyminors > CAPINC_MAX_PORTS)
  1129. capi_ttyminors = CAPINC_MAX_PORTS;
  1130. if (capi_ttyminors <= 0)
  1131. capi_ttyminors = CAPINC_NR_PORTS;
  1132. capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
  1133. GFP_KERNEL);
  1134. if (!capiminors)
  1135. return -ENOMEM;
  1136. drv = alloc_tty_driver(capi_ttyminors);
  1137. if (!drv) {
  1138. kfree(capiminors);
  1139. return -ENOMEM;
  1140. }
  1141. drv->owner = THIS_MODULE;
  1142. drv->driver_name = "capi_nc";
  1143. drv->name = "capi";
  1144. drv->major = 0;
  1145. drv->minor_start = 0;
  1146. drv->type = TTY_DRIVER_TYPE_SERIAL;
  1147. drv->subtype = SERIAL_TYPE_NORMAL;
  1148. drv->init_termios = tty_std_termios;
  1149. drv->init_termios.c_iflag = ICRNL;
  1150. drv->init_termios.c_oflag = OPOST | ONLCR;
  1151. drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1152. drv->init_termios.c_lflag = 0;
  1153. drv->flags =
  1154. TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
  1155. TTY_DRIVER_DYNAMIC_DEV;
  1156. tty_set_operations(drv, &capinc_ops);
  1157. err = tty_register_driver(drv);
  1158. if (err) {
  1159. put_tty_driver(drv);
  1160. kfree(capiminors);
  1161. printk(KERN_ERR "Couldn't register capi_nc driver\n");
  1162. return err;
  1163. }
  1164. capinc_tty_driver = drv;
  1165. return 0;
  1166. }
  1167. static void __exit capinc_tty_exit(void)
  1168. {
  1169. tty_unregister_driver(capinc_tty_driver);
  1170. put_tty_driver(capinc_tty_driver);
  1171. kfree(capiminors);
  1172. }
  1173. #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1174. static inline int capinc_tty_init(void)
  1175. {
  1176. return 0;
  1177. }
  1178. static inline void capinc_tty_exit(void) { }
  1179. #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
  1180. /* -------- /proc functions ----------------------------------------- */
  1181. /*
  1182. * /proc/capi/capi20:
  1183. * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
  1184. */
  1185. static int capi20_proc_show(struct seq_file *m, void *v)
  1186. {
  1187. struct capidev *cdev;
  1188. struct list_head *l;
  1189. mutex_lock(&capidev_list_lock);
  1190. list_for_each(l, &capidev_list) {
  1191. cdev = list_entry(l, struct capidev, list);
  1192. seq_printf(m, "0 %d %lu %lu %lu %lu\n",
  1193. cdev->ap.applid,
  1194. cdev->ap.nrecvctlpkt,
  1195. cdev->ap.nrecvdatapkt,
  1196. cdev->ap.nsentctlpkt,
  1197. cdev->ap.nsentdatapkt);
  1198. }
  1199. mutex_unlock(&capidev_list_lock);
  1200. return 0;
  1201. }
  1202. static int capi20_proc_open(struct inode *inode, struct file *file)
  1203. {
  1204. return single_open(file, capi20_proc_show, NULL);
  1205. }
  1206. static const struct file_operations capi20_proc_fops = {
  1207. .owner = THIS_MODULE,
  1208. .open = capi20_proc_open,
  1209. .read = seq_read,
  1210. .llseek = seq_lseek,
  1211. .release = single_release,
  1212. };
  1213. /*
  1214. * /proc/capi/capi20ncci:
  1215. * applid ncci
  1216. */
  1217. static int capi20ncci_proc_show(struct seq_file *m, void *v)
  1218. {
  1219. struct capidev *cdev;
  1220. struct capincci *np;
  1221. mutex_lock(&capidev_list_lock);
  1222. list_for_each_entry(cdev, &capidev_list, list) {
  1223. mutex_lock(&cdev->lock);
  1224. list_for_each_entry(np, &cdev->nccis, list)
  1225. seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
  1226. mutex_unlock(&cdev->lock);
  1227. }
  1228. mutex_unlock(&capidev_list_lock);
  1229. return 0;
  1230. }
  1231. static int capi20ncci_proc_open(struct inode *inode, struct file *file)
  1232. {
  1233. return single_open(file, capi20ncci_proc_show, NULL);
  1234. }
  1235. static const struct file_operations capi20ncci_proc_fops = {
  1236. .owner = THIS_MODULE,
  1237. .open = capi20ncci_proc_open,
  1238. .read = seq_read,
  1239. .llseek = seq_lseek,
  1240. .release = single_release,
  1241. };
  1242. static void __init proc_init(void)
  1243. {
  1244. proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
  1245. proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
  1246. }
  1247. static void __exit proc_exit(void)
  1248. {
  1249. remove_proc_entry("capi/capi20", NULL);
  1250. remove_proc_entry("capi/capi20ncci", NULL);
  1251. }
  1252. /* -------- init function and module interface ---------------------- */
  1253. static int __init capi_init(void)
  1254. {
  1255. const char *compileinfo;
  1256. int major_ret;
  1257. major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
  1258. if (major_ret < 0) {
  1259. printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
  1260. return major_ret;
  1261. }
  1262. capi_class = class_create(THIS_MODULE, "capi");
  1263. if (IS_ERR(capi_class)) {
  1264. unregister_chrdev(capi_major, "capi20");
  1265. return PTR_ERR(capi_class);
  1266. }
  1267. device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
  1268. if (capinc_tty_init() < 0) {
  1269. device_destroy(capi_class, MKDEV(capi_major, 0));
  1270. class_destroy(capi_class);
  1271. unregister_chrdev(capi_major, "capi20");
  1272. return -ENOMEM;
  1273. }
  1274. proc_init();
  1275. #if defined(CONFIG_ISDN_CAPI_CAPIFS) || defined(CONFIG_ISDN_CAPI_CAPIFS_MODULE)
  1276. compileinfo = " (middleware+capifs)";
  1277. #elif defined(CONFIG_ISDN_CAPI_MIDDLEWARE)
  1278. compileinfo = " (no capifs)";
  1279. #else
  1280. compileinfo = " (no middleware)";
  1281. #endif
  1282. printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
  1283. capi_major, compileinfo);
  1284. return 0;
  1285. }
  1286. static void __exit capi_exit(void)
  1287. {
  1288. proc_exit();
  1289. device_destroy(capi_class, MKDEV(capi_major, 0));
  1290. class_destroy(capi_class);
  1291. unregister_chrdev(capi_major, "capi20");
  1292. capinc_tty_exit();
  1293. }
  1294. module_init(capi_init);
  1295. module_exit(capi_exit);