capi.c 34 KB

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