kcapi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /* $Id: kcapi.c,v 1.1.2.8 2004/03/26 19:57:20 armin Exp $
  2. *
  3. * Kernel CAPI 2.0 Module
  4. *
  5. * Copyright 1999 by Carsten Paeth <calle@calle.de>
  6. * Copyright 2002 by Kai Germaschewski <kai@germaschewski.name>
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. */
  12. #define AVMB1_COMPAT
  13. #include "kcapi.h"
  14. #include <linux/module.h>
  15. #include <linux/mm.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/sched.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/capi.h>
  24. #include <linux/kernelcapi.h>
  25. #include <linux/init.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/isdn/capicmd.h>
  31. #include <linux/isdn/capiutil.h>
  32. #ifdef AVMB1_COMPAT
  33. #include <linux/b1lli.h>
  34. #endif
  35. #include <linux/mutex.h>
  36. #include <linux/rcupdate.h>
  37. static int showcapimsgs = 0;
  38. static struct workqueue_struct *kcapi_wq;
  39. MODULE_DESCRIPTION("CAPI4Linux: kernel CAPI layer");
  40. MODULE_AUTHOR("Carsten Paeth");
  41. MODULE_LICENSE("GPL");
  42. module_param(showcapimsgs, uint, 0);
  43. /* ------------------------------------------------------------- */
  44. struct capictr_event {
  45. struct work_struct work;
  46. unsigned int type;
  47. u32 controller;
  48. };
  49. /* ------------------------------------------------------------- */
  50. static struct capi_version driver_version = {2, 0, 1, 1<<4};
  51. static char driver_serial[CAPI_SERIAL_LEN] = "0004711";
  52. static char capi_manufakturer[64] = "AVM Berlin";
  53. #define NCCI2CTRL(ncci) (((ncci) >> 24) & 0x7f)
  54. LIST_HEAD(capi_drivers);
  55. DEFINE_MUTEX(capi_drivers_lock);
  56. struct capi_ctr *capi_controller[CAPI_MAXCONTR];
  57. DEFINE_MUTEX(capi_controller_lock);
  58. struct capi20_appl *capi_applications[CAPI_MAXAPPL];
  59. static int ncontrollers;
  60. static BLOCKING_NOTIFIER_HEAD(ctr_notifier_list);
  61. /* -------- controller ref counting -------------------------------------- */
  62. static inline struct capi_ctr *
  63. capi_ctr_get(struct capi_ctr *ctr)
  64. {
  65. if (!try_module_get(ctr->owner))
  66. return NULL;
  67. return ctr;
  68. }
  69. static inline void
  70. capi_ctr_put(struct capi_ctr *ctr)
  71. {
  72. module_put(ctr->owner);
  73. }
  74. /* ------------------------------------------------------------- */
  75. static inline struct capi_ctr *get_capi_ctr_by_nr(u16 contr)
  76. {
  77. if (contr - 1 >= CAPI_MAXCONTR)
  78. return NULL;
  79. return capi_controller[contr - 1];
  80. }
  81. static inline struct capi20_appl *__get_capi_appl_by_nr(u16 applid)
  82. {
  83. lockdep_assert_held(&capi_controller_lock);
  84. if (applid - 1 >= CAPI_MAXAPPL)
  85. return NULL;
  86. return capi_applications[applid - 1];
  87. }
  88. static inline struct capi20_appl *get_capi_appl_by_nr(u16 applid)
  89. {
  90. if (applid - 1 >= CAPI_MAXAPPL)
  91. return NULL;
  92. return rcu_dereference(capi_applications[applid - 1]);
  93. }
  94. /* -------- util functions ------------------------------------ */
  95. static inline int capi_cmd_valid(u8 cmd)
  96. {
  97. switch (cmd) {
  98. case CAPI_ALERT:
  99. case CAPI_CONNECT:
  100. case CAPI_CONNECT_ACTIVE:
  101. case CAPI_CONNECT_B3_ACTIVE:
  102. case CAPI_CONNECT_B3:
  103. case CAPI_CONNECT_B3_T90_ACTIVE:
  104. case CAPI_DATA_B3:
  105. case CAPI_DISCONNECT_B3:
  106. case CAPI_DISCONNECT:
  107. case CAPI_FACILITY:
  108. case CAPI_INFO:
  109. case CAPI_LISTEN:
  110. case CAPI_MANUFACTURER:
  111. case CAPI_RESET_B3:
  112. case CAPI_SELECT_B_PROTOCOL:
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. static inline int capi_subcmd_valid(u8 subcmd)
  118. {
  119. switch (subcmd) {
  120. case CAPI_REQ:
  121. case CAPI_CONF:
  122. case CAPI_IND:
  123. case CAPI_RESP:
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. /* ------------------------------------------------------------ */
  129. static void
  130. register_appl(struct capi_ctr *ctr, u16 applid, capi_register_params *rparam)
  131. {
  132. ctr = capi_ctr_get(ctr);
  133. if (ctr)
  134. ctr->register_appl(ctr, applid, rparam);
  135. else
  136. printk(KERN_WARNING "%s: cannot get controller resources\n",
  137. __func__);
  138. }
  139. static void release_appl(struct capi_ctr *ctr, u16 applid)
  140. {
  141. DBG("applid %#x", applid);
  142. ctr->release_appl(ctr, applid);
  143. capi_ctr_put(ctr);
  144. }
  145. static void notify_up(u32 contr)
  146. {
  147. struct capi20_appl *ap;
  148. struct capi_ctr *ctr;
  149. u16 applid;
  150. mutex_lock(&capi_controller_lock);
  151. if (showcapimsgs & 1)
  152. printk(KERN_DEBUG "kcapi: notify up contr %d\n", contr);
  153. ctr = get_capi_ctr_by_nr(contr);
  154. if (ctr) {
  155. if (ctr->state == CAPI_CTR_RUNNING)
  156. goto unlock_out;
  157. ctr->state = CAPI_CTR_RUNNING;
  158. for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
  159. ap = __get_capi_appl_by_nr(applid);
  160. if (ap)
  161. register_appl(ctr, applid, &ap->rparam);
  162. }
  163. wake_up_interruptible_all(&ctr->state_wait_queue);
  164. } else
  165. printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
  166. unlock_out:
  167. mutex_unlock(&capi_controller_lock);
  168. }
  169. static void ctr_down(struct capi_ctr *ctr, int new_state)
  170. {
  171. struct capi20_appl *ap;
  172. u16 applid;
  173. if (ctr->state == CAPI_CTR_DETECTED || ctr->state == CAPI_CTR_DETACHED)
  174. return;
  175. ctr->state = new_state;
  176. memset(ctr->manu, 0, sizeof(ctr->manu));
  177. memset(&ctr->version, 0, sizeof(ctr->version));
  178. memset(&ctr->profile, 0, sizeof(ctr->profile));
  179. memset(ctr->serial, 0, sizeof(ctr->serial));
  180. for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
  181. ap = __get_capi_appl_by_nr(applid);
  182. if (ap)
  183. capi_ctr_put(ctr);
  184. }
  185. wake_up_interruptible_all(&ctr->state_wait_queue);
  186. }
  187. static void notify_down(u32 contr)
  188. {
  189. struct capi_ctr *ctr;
  190. mutex_lock(&capi_controller_lock);
  191. if (showcapimsgs & 1)
  192. printk(KERN_DEBUG "kcapi: notify down contr %d\n", contr);
  193. ctr = get_capi_ctr_by_nr(contr);
  194. if (ctr)
  195. ctr_down(ctr, CAPI_CTR_DETECTED);
  196. else
  197. printk(KERN_WARNING "%s: invalid contr %d\n", __func__, contr);
  198. mutex_unlock(&capi_controller_lock);
  199. }
  200. static int
  201. notify_handler(struct notifier_block *nb, unsigned long val, void *v)
  202. {
  203. u32 contr = (long)v;
  204. switch (val) {
  205. case CAPICTR_UP:
  206. notify_up(contr);
  207. break;
  208. case CAPICTR_DOWN:
  209. notify_down(contr);
  210. break;
  211. }
  212. return NOTIFY_OK;
  213. }
  214. static void do_notify_work(struct work_struct *work)
  215. {
  216. struct capictr_event *event =
  217. container_of(work, struct capictr_event, work);
  218. blocking_notifier_call_chain(&ctr_notifier_list, event->type,
  219. (void *)(long)event->controller);
  220. kfree(event);
  221. }
  222. /*
  223. * The notifier will result in adding/deleteing of devices. Devices can
  224. * only removed in user process, not in bh.
  225. */
  226. static int notify_push(unsigned int event_type, u32 controller)
  227. {
  228. struct capictr_event *event = kmalloc(sizeof(*event), GFP_ATOMIC);
  229. if (!event)
  230. return -ENOMEM;
  231. INIT_WORK(&event->work, do_notify_work);
  232. event->type = event_type;
  233. event->controller = controller;
  234. queue_work(kcapi_wq, &event->work);
  235. return 0;
  236. }
  237. int register_capictr_notifier(struct notifier_block *nb)
  238. {
  239. return blocking_notifier_chain_register(&ctr_notifier_list, nb);
  240. }
  241. EXPORT_SYMBOL_GPL(register_capictr_notifier);
  242. int unregister_capictr_notifier(struct notifier_block *nb)
  243. {
  244. return blocking_notifier_chain_unregister(&ctr_notifier_list, nb);
  245. }
  246. EXPORT_SYMBOL_GPL(unregister_capictr_notifier);
  247. /* -------- Receiver ------------------------------------------ */
  248. static void recv_handler(struct work_struct *work)
  249. {
  250. struct sk_buff *skb;
  251. struct capi20_appl *ap =
  252. container_of(work, struct capi20_appl, recv_work);
  253. if ((!ap) || (ap->release_in_progress))
  254. return;
  255. mutex_lock(&ap->recv_mtx);
  256. while ((skb = skb_dequeue(&ap->recv_queue))) {
  257. if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_IND)
  258. ap->nrecvdatapkt++;
  259. else
  260. ap->nrecvctlpkt++;
  261. ap->recv_message(ap, skb);
  262. }
  263. mutex_unlock(&ap->recv_mtx);
  264. }
  265. /**
  266. * capi_ctr_handle_message() - handle incoming CAPI message
  267. * @ctr: controller descriptor structure.
  268. * @appl: application ID.
  269. * @skb: message.
  270. *
  271. * Called by hardware driver to pass a CAPI message to the application.
  272. */
  273. void capi_ctr_handle_message(struct capi_ctr *ctr, u16 appl,
  274. struct sk_buff *skb)
  275. {
  276. struct capi20_appl *ap;
  277. int showctl = 0;
  278. u8 cmd, subcmd;
  279. _cdebbuf *cdb;
  280. if (ctr->state != CAPI_CTR_RUNNING) {
  281. cdb = capi_message2str(skb->data);
  282. if (cdb) {
  283. printk(KERN_INFO "kcapi: controller [%03d] not active, got: %s",
  284. ctr->cnr, cdb->buf);
  285. cdebbuf_free(cdb);
  286. } else
  287. printk(KERN_INFO "kcapi: controller [%03d] not active, cannot trace\n",
  288. ctr->cnr);
  289. goto error;
  290. }
  291. cmd = CAPIMSG_COMMAND(skb->data);
  292. subcmd = CAPIMSG_SUBCOMMAND(skb->data);
  293. if (cmd == CAPI_DATA_B3 && subcmd == CAPI_IND) {
  294. ctr->nrecvdatapkt++;
  295. if (ctr->traceflag > 2)
  296. showctl |= 2;
  297. } else {
  298. ctr->nrecvctlpkt++;
  299. if (ctr->traceflag)
  300. showctl |= 2;
  301. }
  302. showctl |= (ctr->traceflag & 1);
  303. if (showctl & 2) {
  304. if (showctl & 1) {
  305. printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u\n",
  306. ctr->cnr, CAPIMSG_APPID(skb->data),
  307. capi_cmd2str(cmd, subcmd),
  308. CAPIMSG_LEN(skb->data));
  309. } else {
  310. cdb = capi_message2str(skb->data);
  311. if (cdb) {
  312. printk(KERN_DEBUG "kcapi: got [%03d] %s\n",
  313. ctr->cnr, cdb->buf);
  314. cdebbuf_free(cdb);
  315. } else
  316. printk(KERN_DEBUG "kcapi: got [%03d] id#%d %s len=%u, cannot trace\n",
  317. ctr->cnr, CAPIMSG_APPID(skb->data),
  318. capi_cmd2str(cmd, subcmd),
  319. CAPIMSG_LEN(skb->data));
  320. }
  321. }
  322. rcu_read_lock();
  323. ap = get_capi_appl_by_nr(CAPIMSG_APPID(skb->data));
  324. if (!ap) {
  325. rcu_read_unlock();
  326. cdb = capi_message2str(skb->data);
  327. if (cdb) {
  328. printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s)\n",
  329. CAPIMSG_APPID(skb->data), cdb->buf);
  330. cdebbuf_free(cdb);
  331. } else
  332. printk(KERN_ERR "kcapi: handle_message: applid %d state released (%s) cannot trace\n",
  333. CAPIMSG_APPID(skb->data),
  334. capi_cmd2str(cmd, subcmd));
  335. goto error;
  336. }
  337. skb_queue_tail(&ap->recv_queue, skb);
  338. queue_work(kcapi_wq, &ap->recv_work);
  339. rcu_read_unlock();
  340. return;
  341. error:
  342. kfree_skb(skb);
  343. }
  344. EXPORT_SYMBOL(capi_ctr_handle_message);
  345. /**
  346. * capi_ctr_ready() - signal CAPI controller ready
  347. * @ctr: controller descriptor structure.
  348. *
  349. * Called by hardware driver to signal that the controller is up and running.
  350. */
  351. void capi_ctr_ready(struct capi_ctr *ctr)
  352. {
  353. printk(KERN_NOTICE "kcapi: controller [%03d] \"%s\" ready.\n",
  354. ctr->cnr, ctr->name);
  355. notify_push(CAPICTR_UP, ctr->cnr);
  356. }
  357. EXPORT_SYMBOL(capi_ctr_ready);
  358. /**
  359. * capi_ctr_down() - signal CAPI controller not ready
  360. * @ctr: controller descriptor structure.
  361. *
  362. * Called by hardware driver to signal that the controller is down and
  363. * unavailable for use.
  364. */
  365. void capi_ctr_down(struct capi_ctr *ctr)
  366. {
  367. printk(KERN_NOTICE "kcapi: controller [%03d] down.\n", ctr->cnr);
  368. notify_push(CAPICTR_DOWN, ctr->cnr);
  369. }
  370. EXPORT_SYMBOL(capi_ctr_down);
  371. /**
  372. * capi_ctr_suspend_output() - suspend controller
  373. * @ctr: controller descriptor structure.
  374. *
  375. * Called by hardware driver to stop data flow.
  376. *
  377. * Note: The caller is responsible for synchronizing concurrent state changes
  378. * as well as invocations of capi_ctr_handle_message.
  379. */
  380. void capi_ctr_suspend_output(struct capi_ctr *ctr)
  381. {
  382. if (!ctr->blocked) {
  383. printk(KERN_DEBUG "kcapi: controller [%03d] suspend\n",
  384. ctr->cnr);
  385. ctr->blocked = 1;
  386. }
  387. }
  388. EXPORT_SYMBOL(capi_ctr_suspend_output);
  389. /**
  390. * capi_ctr_resume_output() - resume controller
  391. * @ctr: controller descriptor structure.
  392. *
  393. * Called by hardware driver to resume data flow.
  394. *
  395. * Note: The caller is responsible for synchronizing concurrent state changes
  396. * as well as invocations of capi_ctr_handle_message.
  397. */
  398. void capi_ctr_resume_output(struct capi_ctr *ctr)
  399. {
  400. if (ctr->blocked) {
  401. printk(KERN_DEBUG "kcapi: controller [%03d] resumed\n",
  402. ctr->cnr);
  403. ctr->blocked = 0;
  404. }
  405. }
  406. EXPORT_SYMBOL(capi_ctr_resume_output);
  407. /* ------------------------------------------------------------- */
  408. /**
  409. * attach_capi_ctr() - register CAPI controller
  410. * @ctr: controller descriptor structure.
  411. *
  412. * Called by hardware driver to register a controller with the CAPI subsystem.
  413. * Return value: 0 on success, error code < 0 on error
  414. */
  415. int attach_capi_ctr(struct capi_ctr *ctr)
  416. {
  417. int i;
  418. mutex_lock(&capi_controller_lock);
  419. for (i = 0; i < CAPI_MAXCONTR; i++) {
  420. if (!capi_controller[i])
  421. break;
  422. }
  423. if (i == CAPI_MAXCONTR) {
  424. mutex_unlock(&capi_controller_lock);
  425. printk(KERN_ERR "kcapi: out of controller slots\n");
  426. return -EBUSY;
  427. }
  428. capi_controller[i] = ctr;
  429. ctr->nrecvctlpkt = 0;
  430. ctr->nrecvdatapkt = 0;
  431. ctr->nsentctlpkt = 0;
  432. ctr->nsentdatapkt = 0;
  433. ctr->cnr = i + 1;
  434. ctr->state = CAPI_CTR_DETECTED;
  435. ctr->blocked = 0;
  436. ctr->traceflag = showcapimsgs;
  437. init_waitqueue_head(&ctr->state_wait_queue);
  438. sprintf(ctr->procfn, "capi/controllers/%d", ctr->cnr);
  439. ctr->procent = proc_create_data(ctr->procfn, 0, NULL, ctr->proc_fops, ctr);
  440. ncontrollers++;
  441. mutex_unlock(&capi_controller_lock);
  442. printk(KERN_NOTICE "kcapi: controller [%03d]: %s attached\n",
  443. ctr->cnr, ctr->name);
  444. return 0;
  445. }
  446. EXPORT_SYMBOL(attach_capi_ctr);
  447. /**
  448. * detach_capi_ctr() - unregister CAPI controller
  449. * @ctr: controller descriptor structure.
  450. *
  451. * Called by hardware driver to remove the registration of a controller
  452. * with the CAPI subsystem.
  453. * Return value: 0 on success, error code < 0 on error
  454. */
  455. int detach_capi_ctr(struct capi_ctr *ctr)
  456. {
  457. int err = 0;
  458. mutex_lock(&capi_controller_lock);
  459. ctr_down(ctr, CAPI_CTR_DETACHED);
  460. if (capi_controller[ctr->cnr - 1] != ctr) {
  461. err = -EINVAL;
  462. goto unlock_out;
  463. }
  464. capi_controller[ctr->cnr - 1] = NULL;
  465. ncontrollers--;
  466. if (ctr->procent)
  467. remove_proc_entry(ctr->procfn, NULL);
  468. printk(KERN_NOTICE "kcapi: controller [%03d]: %s unregistered\n",
  469. ctr->cnr, ctr->name);
  470. unlock_out:
  471. mutex_unlock(&capi_controller_lock);
  472. return err;
  473. }
  474. EXPORT_SYMBOL(detach_capi_ctr);
  475. /**
  476. * register_capi_driver() - register CAPI driver
  477. * @driver: driver descriptor structure.
  478. *
  479. * Called by hardware driver to register itself with the CAPI subsystem.
  480. */
  481. void register_capi_driver(struct capi_driver *driver)
  482. {
  483. mutex_lock(&capi_drivers_lock);
  484. list_add_tail(&driver->list, &capi_drivers);
  485. mutex_unlock(&capi_drivers_lock);
  486. }
  487. EXPORT_SYMBOL(register_capi_driver);
  488. /**
  489. * unregister_capi_driver() - unregister CAPI driver
  490. * @driver: driver descriptor structure.
  491. *
  492. * Called by hardware driver to unregister itself from the CAPI subsystem.
  493. */
  494. void unregister_capi_driver(struct capi_driver *driver)
  495. {
  496. mutex_lock(&capi_drivers_lock);
  497. list_del(&driver->list);
  498. mutex_unlock(&capi_drivers_lock);
  499. }
  500. EXPORT_SYMBOL(unregister_capi_driver);
  501. /* ------------------------------------------------------------- */
  502. /* -------- CAPI2.0 Interface ---------------------------------- */
  503. /* ------------------------------------------------------------- */
  504. /**
  505. * capi20_isinstalled() - CAPI 2.0 operation CAPI_INSTALLED
  506. *
  507. * Return value: CAPI result code (CAPI_NOERROR if at least one ISDN controller
  508. * is ready for use, CAPI_REGNOTINSTALLED otherwise)
  509. */
  510. u16 capi20_isinstalled(void)
  511. {
  512. u16 ret = CAPI_REGNOTINSTALLED;
  513. int i;
  514. mutex_lock(&capi_controller_lock);
  515. for (i = 0; i < CAPI_MAXCONTR; i++)
  516. if (capi_controller[i] &&
  517. capi_controller[i]->state == CAPI_CTR_RUNNING) {
  518. ret = CAPI_NOERROR;
  519. break;
  520. }
  521. mutex_unlock(&capi_controller_lock);
  522. return ret;
  523. }
  524. EXPORT_SYMBOL(capi20_isinstalled);
  525. /**
  526. * capi20_register() - CAPI 2.0 operation CAPI_REGISTER
  527. * @ap: CAPI application descriptor structure.
  528. *
  529. * Register an application's presence with CAPI.
  530. * A unique application ID is assigned and stored in @ap->applid.
  531. * After this function returns successfully, the message receive
  532. * callback function @ap->recv_message() may be called at any time
  533. * until capi20_release() has been called for the same @ap.
  534. * Return value: CAPI result code
  535. */
  536. u16 capi20_register(struct capi20_appl *ap)
  537. {
  538. int i;
  539. u16 applid;
  540. DBG("");
  541. if (ap->rparam.datablklen < 128)
  542. return CAPI_LOGBLKSIZETOSMALL;
  543. ap->nrecvctlpkt = 0;
  544. ap->nrecvdatapkt = 0;
  545. ap->nsentctlpkt = 0;
  546. ap->nsentdatapkt = 0;
  547. mutex_init(&ap->recv_mtx);
  548. skb_queue_head_init(&ap->recv_queue);
  549. INIT_WORK(&ap->recv_work, recv_handler);
  550. ap->release_in_progress = 0;
  551. mutex_lock(&capi_controller_lock);
  552. for (applid = 1; applid <= CAPI_MAXAPPL; applid++) {
  553. if (capi_applications[applid - 1] == NULL)
  554. break;
  555. }
  556. if (applid > CAPI_MAXAPPL) {
  557. mutex_unlock(&capi_controller_lock);
  558. return CAPI_TOOMANYAPPLS;
  559. }
  560. ap->applid = applid;
  561. capi_applications[applid - 1] = ap;
  562. for (i = 0; i < CAPI_MAXCONTR; i++) {
  563. if (!capi_controller[i] ||
  564. capi_controller[i]->state != CAPI_CTR_RUNNING)
  565. continue;
  566. register_appl(capi_controller[i], applid, &ap->rparam);
  567. }
  568. mutex_unlock(&capi_controller_lock);
  569. if (showcapimsgs & 1) {
  570. printk(KERN_DEBUG "kcapi: appl %d up\n", applid);
  571. }
  572. return CAPI_NOERROR;
  573. }
  574. EXPORT_SYMBOL(capi20_register);
  575. /**
  576. * capi20_release() - CAPI 2.0 operation CAPI_RELEASE
  577. * @ap: CAPI application descriptor structure.
  578. *
  579. * Terminate an application's registration with CAPI.
  580. * After this function returns successfully, the message receive
  581. * callback function @ap->recv_message() will no longer be called.
  582. * Return value: CAPI result code
  583. */
  584. u16 capi20_release(struct capi20_appl *ap)
  585. {
  586. int i;
  587. DBG("applid %#x", ap->applid);
  588. mutex_lock(&capi_controller_lock);
  589. ap->release_in_progress = 1;
  590. capi_applications[ap->applid - 1] = NULL;
  591. synchronize_rcu();
  592. for (i = 0; i < CAPI_MAXCONTR; i++) {
  593. if (!capi_controller[i] ||
  594. capi_controller[i]->state != CAPI_CTR_RUNNING)
  595. continue;
  596. release_appl(capi_controller[i], ap->applid);
  597. }
  598. mutex_unlock(&capi_controller_lock);
  599. flush_workqueue(kcapi_wq);
  600. skb_queue_purge(&ap->recv_queue);
  601. if (showcapimsgs & 1) {
  602. printk(KERN_DEBUG "kcapi: appl %d down\n", ap->applid);
  603. }
  604. return CAPI_NOERROR;
  605. }
  606. EXPORT_SYMBOL(capi20_release);
  607. /**
  608. * capi20_put_message() - CAPI 2.0 operation CAPI_PUT_MESSAGE
  609. * @ap: CAPI application descriptor structure.
  610. * @skb: CAPI message.
  611. *
  612. * Transfer a single message to CAPI.
  613. * Return value: CAPI result code
  614. */
  615. u16 capi20_put_message(struct capi20_appl *ap, struct sk_buff *skb)
  616. {
  617. struct capi_ctr *ctr;
  618. int showctl = 0;
  619. u8 cmd, subcmd;
  620. DBG("applid %#x", ap->applid);
  621. if (ncontrollers == 0)
  622. return CAPI_REGNOTINSTALLED;
  623. if ((ap->applid == 0) || ap->release_in_progress)
  624. return CAPI_ILLAPPNR;
  625. if (skb->len < 12
  626. || !capi_cmd_valid(CAPIMSG_COMMAND(skb->data))
  627. || !capi_subcmd_valid(CAPIMSG_SUBCOMMAND(skb->data)))
  628. return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
  629. /*
  630. * The controller reference is protected by the existence of the
  631. * application passed to us. We assume that the caller properly
  632. * synchronizes this service with capi20_release.
  633. */
  634. ctr = get_capi_ctr_by_nr(CAPIMSG_CONTROLLER(skb->data));
  635. if (!ctr || ctr->state != CAPI_CTR_RUNNING)
  636. return CAPI_REGNOTINSTALLED;
  637. if (ctr->blocked)
  638. return CAPI_SENDQUEUEFULL;
  639. cmd = CAPIMSG_COMMAND(skb->data);
  640. subcmd = CAPIMSG_SUBCOMMAND(skb->data);
  641. if (cmd == CAPI_DATA_B3 && subcmd== CAPI_REQ) {
  642. ctr->nsentdatapkt++;
  643. ap->nsentdatapkt++;
  644. if (ctr->traceflag > 2)
  645. showctl |= 2;
  646. } else {
  647. ctr->nsentctlpkt++;
  648. ap->nsentctlpkt++;
  649. if (ctr->traceflag)
  650. showctl |= 2;
  651. }
  652. showctl |= (ctr->traceflag & 1);
  653. if (showctl & 2) {
  654. if (showctl & 1) {
  655. printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u\n",
  656. CAPIMSG_CONTROLLER(skb->data),
  657. CAPIMSG_APPID(skb->data),
  658. capi_cmd2str(cmd, subcmd),
  659. CAPIMSG_LEN(skb->data));
  660. } else {
  661. _cdebbuf *cdb = capi_message2str(skb->data);
  662. if (cdb) {
  663. printk(KERN_DEBUG "kcapi: put [%03d] %s\n",
  664. CAPIMSG_CONTROLLER(skb->data),
  665. cdb->buf);
  666. cdebbuf_free(cdb);
  667. } else
  668. printk(KERN_DEBUG "kcapi: put [%03d] id#%d %s len=%u cannot trace\n",
  669. CAPIMSG_CONTROLLER(skb->data),
  670. CAPIMSG_APPID(skb->data),
  671. capi_cmd2str(cmd, subcmd),
  672. CAPIMSG_LEN(skb->data));
  673. }
  674. }
  675. return ctr->send_message(ctr, skb);
  676. }
  677. EXPORT_SYMBOL(capi20_put_message);
  678. /**
  679. * capi20_get_manufacturer() - CAPI 2.0 operation CAPI_GET_MANUFACTURER
  680. * @contr: controller number.
  681. * @buf: result buffer (64 bytes).
  682. *
  683. * Retrieve information about the manufacturer of the specified ISDN controller
  684. * or (for @contr == 0) the driver itself.
  685. * Return value: CAPI result code
  686. */
  687. u16 capi20_get_manufacturer(u32 contr, u8 *buf)
  688. {
  689. struct capi_ctr *ctr;
  690. u16 ret;
  691. if (contr == 0) {
  692. strlcpy(buf, capi_manufakturer, CAPI_MANUFACTURER_LEN);
  693. return CAPI_NOERROR;
  694. }
  695. mutex_lock(&capi_controller_lock);
  696. ctr = get_capi_ctr_by_nr(contr);
  697. if (ctr && ctr->state == CAPI_CTR_RUNNING) {
  698. strlcpy(buf, ctr->manu, CAPI_MANUFACTURER_LEN);
  699. ret = CAPI_NOERROR;
  700. } else
  701. ret = CAPI_REGNOTINSTALLED;
  702. mutex_unlock(&capi_controller_lock);
  703. return ret;
  704. }
  705. EXPORT_SYMBOL(capi20_get_manufacturer);
  706. /**
  707. * capi20_get_version() - CAPI 2.0 operation CAPI_GET_VERSION
  708. * @contr: controller number.
  709. * @verp: result structure.
  710. *
  711. * Retrieve version information for the specified ISDN controller
  712. * or (for @contr == 0) the driver itself.
  713. * Return value: CAPI result code
  714. */
  715. u16 capi20_get_version(u32 contr, struct capi_version *verp)
  716. {
  717. struct capi_ctr *ctr;
  718. u16 ret;
  719. if (contr == 0) {
  720. *verp = driver_version;
  721. return CAPI_NOERROR;
  722. }
  723. mutex_lock(&capi_controller_lock);
  724. ctr = get_capi_ctr_by_nr(contr);
  725. if (ctr && ctr->state == CAPI_CTR_RUNNING) {
  726. memcpy(verp, &ctr->version, sizeof(capi_version));
  727. ret = CAPI_NOERROR;
  728. } else
  729. ret = CAPI_REGNOTINSTALLED;
  730. mutex_unlock(&capi_controller_lock);
  731. return ret;
  732. }
  733. EXPORT_SYMBOL(capi20_get_version);
  734. /**
  735. * capi20_get_serial() - CAPI 2.0 operation CAPI_GET_SERIAL_NUMBER
  736. * @contr: controller number.
  737. * @serial: result buffer (8 bytes).
  738. *
  739. * Retrieve the serial number of the specified ISDN controller
  740. * or (for @contr == 0) the driver itself.
  741. * Return value: CAPI result code
  742. */
  743. u16 capi20_get_serial(u32 contr, u8 *serial)
  744. {
  745. struct capi_ctr *ctr;
  746. u16 ret;
  747. if (contr == 0) {
  748. strlcpy(serial, driver_serial, CAPI_SERIAL_LEN);
  749. return CAPI_NOERROR;
  750. }
  751. mutex_lock(&capi_controller_lock);
  752. ctr = get_capi_ctr_by_nr(contr);
  753. if (ctr && ctr->state == CAPI_CTR_RUNNING) {
  754. strlcpy(serial, ctr->serial, CAPI_SERIAL_LEN);
  755. ret = CAPI_NOERROR;
  756. } else
  757. ret = CAPI_REGNOTINSTALLED;
  758. mutex_unlock(&capi_controller_lock);
  759. return ret;
  760. }
  761. EXPORT_SYMBOL(capi20_get_serial);
  762. /**
  763. * capi20_get_profile() - CAPI 2.0 operation CAPI_GET_PROFILE
  764. * @contr: controller number.
  765. * @profp: result structure.
  766. *
  767. * Retrieve capability information for the specified ISDN controller
  768. * or (for @contr == 0) the number of installed controllers.
  769. * Return value: CAPI result code
  770. */
  771. u16 capi20_get_profile(u32 contr, struct capi_profile *profp)
  772. {
  773. struct capi_ctr *ctr;
  774. u16 ret;
  775. if (contr == 0) {
  776. profp->ncontroller = ncontrollers;
  777. return CAPI_NOERROR;
  778. }
  779. mutex_lock(&capi_controller_lock);
  780. ctr = get_capi_ctr_by_nr(contr);
  781. if (ctr && ctr->state == CAPI_CTR_RUNNING) {
  782. memcpy(profp, &ctr->profile, sizeof(struct capi_profile));
  783. ret = CAPI_NOERROR;
  784. } else
  785. ret = CAPI_REGNOTINSTALLED;
  786. mutex_unlock(&capi_controller_lock);
  787. return ret;
  788. }
  789. EXPORT_SYMBOL(capi20_get_profile);
  790. /* Must be called with capi_controller_lock held. */
  791. static int wait_on_ctr_state(struct capi_ctr *ctr, unsigned int state)
  792. {
  793. DEFINE_WAIT(wait);
  794. int retval = 0;
  795. ctr = capi_ctr_get(ctr);
  796. if (!ctr)
  797. return -ESRCH;
  798. for (;;) {
  799. prepare_to_wait(&ctr->state_wait_queue, &wait,
  800. TASK_INTERRUPTIBLE);
  801. if (ctr->state == state)
  802. break;
  803. if (ctr->state == CAPI_CTR_DETACHED) {
  804. retval = -ESRCH;
  805. break;
  806. }
  807. if (signal_pending(current)) {
  808. retval = -EINTR;
  809. break;
  810. }
  811. mutex_unlock(&capi_controller_lock);
  812. schedule();
  813. mutex_lock(&capi_controller_lock);
  814. }
  815. finish_wait(&ctr->state_wait_queue, &wait);
  816. capi_ctr_put(ctr);
  817. return retval;
  818. }
  819. #ifdef AVMB1_COMPAT
  820. static int old_capi_manufacturer(unsigned int cmd, void __user *data)
  821. {
  822. avmb1_loadandconfigdef ldef;
  823. avmb1_extcarddef cdef;
  824. avmb1_resetdef rdef;
  825. capicardparams cparams;
  826. struct capi_ctr *ctr;
  827. struct capi_driver *driver = NULL;
  828. capiloaddata ldata;
  829. struct list_head *l;
  830. int retval;
  831. switch (cmd) {
  832. case AVMB1_ADDCARD:
  833. case AVMB1_ADDCARD_WITH_TYPE:
  834. if (cmd == AVMB1_ADDCARD) {
  835. if ((retval = copy_from_user(&cdef, data,
  836. sizeof(avmb1_carddef))))
  837. return -EFAULT;
  838. cdef.cardtype = AVM_CARDTYPE_B1;
  839. } else {
  840. if ((retval = copy_from_user(&cdef, data,
  841. sizeof(avmb1_extcarddef))))
  842. return -EFAULT;
  843. }
  844. cparams.port = cdef.port;
  845. cparams.irq = cdef.irq;
  846. cparams.cardnr = cdef.cardnr;
  847. mutex_lock(&capi_drivers_lock);
  848. switch (cdef.cardtype) {
  849. case AVM_CARDTYPE_B1:
  850. list_for_each(l, &capi_drivers) {
  851. driver = list_entry(l, struct capi_driver, list);
  852. if (strcmp(driver->name, "b1isa") == 0)
  853. break;
  854. }
  855. break;
  856. case AVM_CARDTYPE_T1:
  857. list_for_each(l, &capi_drivers) {
  858. driver = list_entry(l, struct capi_driver, list);
  859. if (strcmp(driver->name, "t1isa") == 0)
  860. break;
  861. }
  862. break;
  863. default:
  864. driver = NULL;
  865. break;
  866. }
  867. if (!driver) {
  868. printk(KERN_ERR "kcapi: driver not loaded.\n");
  869. retval = -EIO;
  870. } else if (!driver->add_card) {
  871. printk(KERN_ERR "kcapi: driver has no add card function.\n");
  872. retval = -EIO;
  873. } else
  874. retval = driver->add_card(driver, &cparams);
  875. mutex_unlock(&capi_drivers_lock);
  876. return retval;
  877. case AVMB1_LOAD:
  878. case AVMB1_LOAD_AND_CONFIG:
  879. if (cmd == AVMB1_LOAD) {
  880. if (copy_from_user(&ldef, data,
  881. sizeof(avmb1_loaddef)))
  882. return -EFAULT;
  883. ldef.t4config.len = 0;
  884. ldef.t4config.data = NULL;
  885. } else {
  886. if (copy_from_user(&ldef, data,
  887. sizeof(avmb1_loadandconfigdef)))
  888. return -EFAULT;
  889. }
  890. mutex_lock(&capi_controller_lock);
  891. ctr = get_capi_ctr_by_nr(ldef.contr);
  892. if (!ctr) {
  893. retval = -EINVAL;
  894. goto load_unlock_out;
  895. }
  896. if (ctr->load_firmware == NULL) {
  897. printk(KERN_DEBUG "kcapi: load: no load function\n");
  898. retval = -ESRCH;
  899. goto load_unlock_out;
  900. }
  901. if (ldef.t4file.len <= 0) {
  902. printk(KERN_DEBUG "kcapi: load: invalid parameter: length of t4file is %d ?\n", ldef.t4file.len);
  903. retval = -EINVAL;
  904. goto load_unlock_out;
  905. }
  906. if (ldef.t4file.data == NULL) {
  907. printk(KERN_DEBUG "kcapi: load: invalid parameter: dataptr is 0\n");
  908. retval = -EINVAL;
  909. goto load_unlock_out;
  910. }
  911. ldata.firmware.user = 1;
  912. ldata.firmware.data = ldef.t4file.data;
  913. ldata.firmware.len = ldef.t4file.len;
  914. ldata.configuration.user = 1;
  915. ldata.configuration.data = ldef.t4config.data;
  916. ldata.configuration.len = ldef.t4config.len;
  917. if (ctr->state != CAPI_CTR_DETECTED) {
  918. printk(KERN_INFO "kcapi: load: contr=%d not in detect state\n", ldef.contr);
  919. retval = -EBUSY;
  920. goto load_unlock_out;
  921. }
  922. ctr->state = CAPI_CTR_LOADING;
  923. retval = ctr->load_firmware(ctr, &ldata);
  924. if (retval) {
  925. ctr->state = CAPI_CTR_DETECTED;
  926. goto load_unlock_out;
  927. }
  928. retval = wait_on_ctr_state(ctr, CAPI_CTR_RUNNING);
  929. load_unlock_out:
  930. mutex_unlock(&capi_controller_lock);
  931. return retval;
  932. case AVMB1_RESETCARD:
  933. if (copy_from_user(&rdef, data, sizeof(avmb1_resetdef)))
  934. return -EFAULT;
  935. retval = 0;
  936. mutex_lock(&capi_controller_lock);
  937. ctr = get_capi_ctr_by_nr(rdef.contr);
  938. if (!ctr) {
  939. retval = -ESRCH;
  940. goto reset_unlock_out;
  941. }
  942. if (ctr->state == CAPI_CTR_DETECTED)
  943. goto reset_unlock_out;
  944. if (ctr->reset_ctr == NULL) {
  945. printk(KERN_DEBUG "kcapi: reset: no reset function\n");
  946. retval = -ESRCH;
  947. goto reset_unlock_out;
  948. }
  949. ctr->reset_ctr(ctr);
  950. retval = wait_on_ctr_state(ctr, CAPI_CTR_DETECTED);
  951. reset_unlock_out:
  952. mutex_unlock(&capi_controller_lock);
  953. return retval;
  954. }
  955. return -EINVAL;
  956. }
  957. #endif
  958. /**
  959. * capi20_manufacturer() - CAPI 2.0 operation CAPI_MANUFACTURER
  960. * @cmd: command.
  961. * @data: parameter.
  962. *
  963. * Perform manufacturer specific command.
  964. * Return value: CAPI result code
  965. */
  966. int capi20_manufacturer(unsigned int cmd, void __user *data)
  967. {
  968. struct capi_ctr *ctr;
  969. int retval;
  970. switch (cmd) {
  971. #ifdef AVMB1_COMPAT
  972. case AVMB1_LOAD:
  973. case AVMB1_LOAD_AND_CONFIG:
  974. case AVMB1_RESETCARD:
  975. case AVMB1_GET_CARDINFO:
  976. case AVMB1_REMOVECARD:
  977. return old_capi_manufacturer(cmd, data);
  978. #endif
  979. case KCAPI_CMD_TRACE:
  980. {
  981. kcapi_flagdef fdef;
  982. if (copy_from_user(&fdef, data, sizeof(kcapi_flagdef)))
  983. return -EFAULT;
  984. mutex_lock(&capi_controller_lock);
  985. ctr = get_capi_ctr_by_nr(fdef.contr);
  986. if (ctr) {
  987. ctr->traceflag = fdef.flag;
  988. printk(KERN_INFO "kcapi: contr [%03d] set trace=%d\n",
  989. ctr->cnr, ctr->traceflag);
  990. retval = 0;
  991. } else
  992. retval = -ESRCH;
  993. mutex_unlock(&capi_controller_lock);
  994. return retval;
  995. }
  996. case KCAPI_CMD_ADDCARD:
  997. {
  998. struct list_head *l;
  999. struct capi_driver *driver = NULL;
  1000. capicardparams cparams;
  1001. kcapi_carddef cdef;
  1002. if ((retval = copy_from_user(&cdef, data, sizeof(cdef))))
  1003. return -EFAULT;
  1004. cparams.port = cdef.port;
  1005. cparams.irq = cdef.irq;
  1006. cparams.membase = cdef.membase;
  1007. cparams.cardnr = cdef.cardnr;
  1008. cparams.cardtype = 0;
  1009. cdef.driver[sizeof(cdef.driver)-1] = 0;
  1010. mutex_lock(&capi_drivers_lock);
  1011. list_for_each(l, &capi_drivers) {
  1012. driver = list_entry(l, struct capi_driver, list);
  1013. if (strcmp(driver->name, cdef.driver) == 0)
  1014. break;
  1015. }
  1016. if (driver == NULL) {
  1017. printk(KERN_ERR "kcapi: driver \"%s\" not loaded.\n",
  1018. cdef.driver);
  1019. retval = -ESRCH;
  1020. } else if (!driver->add_card) {
  1021. printk(KERN_ERR "kcapi: driver \"%s\" has no add card function.\n", cdef.driver);
  1022. retval = -EIO;
  1023. } else
  1024. retval = driver->add_card(driver, &cparams);
  1025. mutex_unlock(&capi_drivers_lock);
  1026. return retval;
  1027. }
  1028. default:
  1029. printk(KERN_ERR "kcapi: manufacturer command %d unknown.\n",
  1030. cmd);
  1031. break;
  1032. }
  1033. return -EINVAL;
  1034. }
  1035. EXPORT_SYMBOL(capi20_manufacturer);
  1036. /* ------------------------------------------------------------- */
  1037. /* -------- Init & Cleanup ------------------------------------- */
  1038. /* ------------------------------------------------------------- */
  1039. /*
  1040. * init / exit functions
  1041. */
  1042. static struct notifier_block capictr_nb = {
  1043. .notifier_call = notify_handler,
  1044. .priority = INT_MAX,
  1045. };
  1046. static int __init kcapi_init(void)
  1047. {
  1048. int err;
  1049. kcapi_wq = alloc_workqueue("kcapi", 0, 0);
  1050. if (!kcapi_wq)
  1051. return -ENOMEM;
  1052. register_capictr_notifier(&capictr_nb);
  1053. err = cdebug_init();
  1054. if (err) {
  1055. unregister_capictr_notifier(&capictr_nb);
  1056. destroy_workqueue(kcapi_wq);
  1057. return err;
  1058. }
  1059. kcapi_proc_init();
  1060. return 0;
  1061. }
  1062. static void __exit kcapi_exit(void)
  1063. {
  1064. kcapi_proc_exit();
  1065. unregister_capictr_notifier(&capictr_nb);
  1066. cdebug_exit();
  1067. destroy_workqueue(kcapi_wq);
  1068. }
  1069. module_init(kcapi_init);
  1070. module_exit(kcapi_exit);