divasi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* $Id: divasi.c,v 1.25.6.2 2005/01/31 12:22:20 armin Exp $
  2. *
  3. * Driver for Eicon DIVA Server ISDN cards.
  4. * User Mode IDI Interface
  5. *
  6. * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
  7. * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/smp_lock.h>
  18. #include <linux/poll.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/devfs_fs_kernel.h>
  22. #include <asm/uaccess.h>
  23. #include "platform.h"
  24. #include "di_defs.h"
  25. #include "divasync.h"
  26. #include "um_xdi.h"
  27. #include "um_idi.h"
  28. static char *main_revision = "$Revision: 1.25.6.2 $";
  29. static int major;
  30. MODULE_DESCRIPTION("User IDI Interface for Eicon ISDN cards");
  31. MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
  32. MODULE_SUPPORTED_DEVICE("DIVA card driver");
  33. MODULE_LICENSE("GPL");
  34. typedef struct _diva_um_idi_os_context {
  35. wait_queue_head_t read_wait;
  36. wait_queue_head_t close_wait;
  37. struct timer_list diva_timer_id;
  38. int aborted;
  39. int adapter_nr;
  40. } diva_um_idi_os_context_t;
  41. static char *DRIVERNAME = "Eicon DIVA - User IDI (http://www.melware.net)";
  42. static char *DRIVERLNAME = "diva_idi";
  43. static char *DEVNAME = "DivasIDI";
  44. char *DRIVERRELEASE_IDI = "2.0";
  45. extern int idifunc_init(void);
  46. extern void idifunc_finit(void);
  47. /*
  48. * helper functions
  49. */
  50. static char *getrev(const char *revision)
  51. {
  52. char *rev;
  53. char *p;
  54. if ((p = strchr(revision, ':'))) {
  55. rev = p + 2;
  56. p = strchr(rev, '$');
  57. *--p = 0;
  58. } else
  59. rev = "1.0";
  60. return rev;
  61. }
  62. /*
  63. * LOCALS
  64. */
  65. static ssize_t um_idi_read(struct file *file, char __user *buf, size_t count,
  66. loff_t * offset);
  67. static ssize_t um_idi_write(struct file *file, const char __user *buf,
  68. size_t count, loff_t * offset);
  69. static unsigned int um_idi_poll(struct file *file, poll_table * wait);
  70. static int um_idi_open(struct inode *inode, struct file *file);
  71. static int um_idi_release(struct inode *inode, struct file *file);
  72. static int remove_entity(void *entity);
  73. static void diva_um_timer_function(unsigned long data);
  74. /*
  75. * proc entry
  76. */
  77. extern struct proc_dir_entry *proc_net_eicon;
  78. static struct proc_dir_entry *um_idi_proc_entry = NULL;
  79. static int
  80. um_idi_proc_read(char *page, char **start, off_t off, int count, int *eof,
  81. void *data)
  82. {
  83. int len = 0;
  84. char tmprev[32];
  85. len += sprintf(page + len, "%s\n", DRIVERNAME);
  86. len += sprintf(page + len, "name : %s\n", DRIVERLNAME);
  87. len += sprintf(page + len, "release : %s\n", DRIVERRELEASE_IDI);
  88. strcpy(tmprev, main_revision);
  89. len += sprintf(page + len, "revision : %s\n", getrev(tmprev));
  90. len += sprintf(page + len, "build : %s\n", DIVA_BUILD);
  91. len += sprintf(page + len, "major : %d\n", major);
  92. if (off + count >= len)
  93. *eof = 1;
  94. if (len < off)
  95. return 0;
  96. *start = page + off;
  97. return ((count < len - off) ? count : len - off);
  98. }
  99. static int DIVA_INIT_FUNCTION create_um_idi_proc(void)
  100. {
  101. um_idi_proc_entry = create_proc_entry(DRIVERLNAME,
  102. S_IFREG | S_IRUGO | S_IWUSR,
  103. proc_net_eicon);
  104. if (!um_idi_proc_entry)
  105. return (0);
  106. um_idi_proc_entry->read_proc = um_idi_proc_read;
  107. um_idi_proc_entry->owner = THIS_MODULE;
  108. return (1);
  109. }
  110. static void remove_um_idi_proc(void)
  111. {
  112. if (um_idi_proc_entry) {
  113. remove_proc_entry(DRIVERLNAME, proc_net_eicon);
  114. um_idi_proc_entry = NULL;
  115. }
  116. }
  117. static struct file_operations divas_idi_fops = {
  118. .owner = THIS_MODULE,
  119. .llseek = no_llseek,
  120. .read = um_idi_read,
  121. .write = um_idi_write,
  122. .poll = um_idi_poll,
  123. .open = um_idi_open,
  124. .release = um_idi_release
  125. };
  126. static void divas_idi_unregister_chrdev(void)
  127. {
  128. devfs_remove(DEVNAME);
  129. unregister_chrdev(major, DEVNAME);
  130. }
  131. static int DIVA_INIT_FUNCTION divas_idi_register_chrdev(void)
  132. {
  133. if ((major = register_chrdev(0, DEVNAME, &divas_idi_fops)) < 0)
  134. {
  135. printk(KERN_ERR "%s: failed to create /dev entry.\n",
  136. DRIVERLNAME);
  137. return (0);
  138. }
  139. devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
  140. return (1);
  141. }
  142. /*
  143. ** Driver Load
  144. */
  145. static int DIVA_INIT_FUNCTION divasi_init(void)
  146. {
  147. char tmprev[50];
  148. int ret = 0;
  149. printk(KERN_INFO "%s\n", DRIVERNAME);
  150. printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_IDI);
  151. strcpy(tmprev, main_revision);
  152. printk("%s Build: %s\n", getrev(tmprev), DIVA_BUILD);
  153. if (!divas_idi_register_chrdev()) {
  154. ret = -EIO;
  155. goto out;
  156. }
  157. if (!create_um_idi_proc()) {
  158. divas_idi_unregister_chrdev();
  159. printk(KERN_ERR "%s: failed to create proc entry.\n",
  160. DRIVERLNAME);
  161. ret = -EIO;
  162. goto out;
  163. }
  164. if (!(idifunc_init())) {
  165. remove_um_idi_proc();
  166. divas_idi_unregister_chrdev();
  167. printk(KERN_ERR "%s: failed to connect to DIDD.\n",
  168. DRIVERLNAME);
  169. ret = -EIO;
  170. goto out;
  171. }
  172. printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
  173. out:
  174. return (ret);
  175. }
  176. /*
  177. ** Driver Unload
  178. */
  179. static void DIVA_EXIT_FUNCTION divasi_exit(void)
  180. {
  181. idifunc_finit();
  182. remove_um_idi_proc();
  183. divas_idi_unregister_chrdev();
  184. printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
  185. }
  186. module_init(divasi_init);
  187. module_exit(divasi_exit);
  188. /*
  189. * FILE OPERATIONS
  190. */
  191. static int
  192. divas_um_idi_copy_to_user(void *os_handle, void *dst, const void *src,
  193. int length)
  194. {
  195. memcpy(dst, src, length);
  196. return (length);
  197. }
  198. static ssize_t
  199. um_idi_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
  200. {
  201. diva_um_idi_os_context_t *p_os;
  202. int ret = -EINVAL;
  203. void *data;
  204. if (!file->private_data) {
  205. return (-ENODEV);
  206. }
  207. if (!
  208. (p_os =
  209. (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
  210. private_data)))
  211. {
  212. return (-ENODEV);
  213. }
  214. if (p_os->aborted) {
  215. return (-ENODEV);
  216. }
  217. if (!(data = diva_os_malloc(0, count))) {
  218. return (-ENOMEM);
  219. }
  220. ret = diva_um_idi_read(file->private_data,
  221. file, data, count,
  222. divas_um_idi_copy_to_user);
  223. switch (ret) {
  224. case 0: /* no message available */
  225. ret = (-EAGAIN);
  226. break;
  227. case (-1): /* adapter was removed */
  228. ret = (-ENODEV);
  229. break;
  230. case (-2): /* message_length > length of user buffer */
  231. ret = (-EFAULT);
  232. break;
  233. }
  234. if (ret > 0) {
  235. if (copy_to_user(buf, data, ret)) {
  236. ret = (-EFAULT);
  237. }
  238. }
  239. diva_os_free(0, data);
  240. DBG_TRC(("read: ret %d", ret));
  241. return (ret);
  242. }
  243. static int
  244. divas_um_idi_copy_from_user(void *os_handle, void *dst, const void *src,
  245. int length)
  246. {
  247. memcpy(dst, src, length);
  248. return (length);
  249. }
  250. static int um_idi_open_adapter(struct file *file, int adapter_nr)
  251. {
  252. diva_um_idi_os_context_t *p_os;
  253. void *e =
  254. divas_um_idi_create_entity((dword) adapter_nr, (void *) file);
  255. if (!(file->private_data = e)) {
  256. return (0);
  257. }
  258. p_os = (diva_um_idi_os_context_t *) diva_um_id_get_os_context(e);
  259. init_waitqueue_head(&p_os->read_wait);
  260. init_waitqueue_head(&p_os->close_wait);
  261. init_timer(&p_os->diva_timer_id);
  262. p_os->diva_timer_id.function = (void *) diva_um_timer_function;
  263. p_os->diva_timer_id.data = (unsigned long) p_os;
  264. p_os->aborted = 0;
  265. p_os->adapter_nr = adapter_nr;
  266. return (1);
  267. }
  268. static ssize_t
  269. um_idi_write(struct file *file, const char __user *buf, size_t count,
  270. loff_t * offset)
  271. {
  272. diva_um_idi_os_context_t *p_os;
  273. int ret = -EINVAL;
  274. void *data;
  275. int adapter_nr = 0;
  276. if (!file->private_data) {
  277. /* the first write() selects the adapter_nr */
  278. if (count == sizeof(int)) {
  279. if (copy_from_user
  280. ((void *) &adapter_nr, buf,
  281. count)) return (-EFAULT);
  282. if (!(um_idi_open_adapter(file, adapter_nr)))
  283. return (-ENODEV);
  284. return (count);
  285. } else
  286. return (-ENODEV);
  287. }
  288. if (!(p_os =
  289. (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
  290. private_data)))
  291. {
  292. return (-ENODEV);
  293. }
  294. if (p_os->aborted) {
  295. return (-ENODEV);
  296. }
  297. if (!(data = diva_os_malloc(0, count))) {
  298. return (-ENOMEM);
  299. }
  300. if (copy_from_user(data, buf, count)) {
  301. ret = -EFAULT;
  302. } else {
  303. ret = diva_um_idi_write(file->private_data,
  304. file, data, count,
  305. divas_um_idi_copy_from_user);
  306. switch (ret) {
  307. case 0: /* no space available */
  308. ret = (-EAGAIN);
  309. break;
  310. case (-1): /* adapter was removed */
  311. ret = (-ENODEV);
  312. break;
  313. case (-2): /* length of user buffer > max message_length */
  314. ret = (-EFAULT);
  315. break;
  316. }
  317. }
  318. diva_os_free(0, data);
  319. DBG_TRC(("write: ret %d", ret));
  320. return (ret);
  321. }
  322. static unsigned int um_idi_poll(struct file *file, poll_table * wait)
  323. {
  324. diva_um_idi_os_context_t *p_os;
  325. if (!file->private_data) {
  326. return (POLLERR);
  327. }
  328. if ((!(p_os =
  329. (diva_um_idi_os_context_t *)
  330. diva_um_id_get_os_context(file->private_data)))
  331. || p_os->aborted) {
  332. return (POLLERR);
  333. }
  334. poll_wait(file, &p_os->read_wait, wait);
  335. if (p_os->aborted) {
  336. return (POLLERR);
  337. }
  338. switch (diva_user_mode_idi_ind_ready(file->private_data, file)) {
  339. case (-1):
  340. return (POLLERR);
  341. case 0:
  342. return (0);
  343. }
  344. return (POLLIN | POLLRDNORM);
  345. }
  346. static int um_idi_open(struct inode *inode, struct file *file)
  347. {
  348. return (0);
  349. }
  350. static int um_idi_release(struct inode *inode, struct file *file)
  351. {
  352. diva_um_idi_os_context_t *p_os;
  353. unsigned int adapter_nr;
  354. int ret = 0;
  355. if (!(file->private_data)) {
  356. ret = -ENODEV;
  357. goto out;
  358. }
  359. if (!(p_os =
  360. (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->private_data))) {
  361. ret = -ENODEV;
  362. goto out;
  363. }
  364. adapter_nr = p_os->adapter_nr;
  365. if ((ret = remove_entity(file->private_data))) {
  366. goto out;
  367. }
  368. if (divas_um_idi_delete_entity
  369. ((int) adapter_nr, file->private_data)) {
  370. ret = -ENODEV;
  371. goto out;
  372. }
  373. out:
  374. return (ret);
  375. }
  376. int diva_os_get_context_size(void)
  377. {
  378. return (sizeof(diva_um_idi_os_context_t));
  379. }
  380. void diva_os_wakeup_read(void *os_context)
  381. {
  382. diva_um_idi_os_context_t *p_os =
  383. (diva_um_idi_os_context_t *) os_context;
  384. wake_up_interruptible(&p_os->read_wait);
  385. }
  386. void diva_os_wakeup_close(void *os_context)
  387. {
  388. diva_um_idi_os_context_t *p_os =
  389. (diva_um_idi_os_context_t *) os_context;
  390. wake_up_interruptible(&p_os->close_wait);
  391. }
  392. static
  393. void diva_um_timer_function(unsigned long data)
  394. {
  395. diva_um_idi_os_context_t *p_os = (diva_um_idi_os_context_t *) data;
  396. p_os->aborted = 1;
  397. wake_up_interruptible(&p_os->read_wait);
  398. wake_up_interruptible(&p_os->close_wait);
  399. DBG_ERR(("entity removal watchdog"))
  400. }
  401. /*
  402. ** If application exits without entity removal this function will remove
  403. ** entity and block until removal is complete
  404. */
  405. static int remove_entity(void *entity)
  406. {
  407. struct task_struct *curtask = current;
  408. diva_um_idi_os_context_t *p_os;
  409. diva_um_idi_stop_wdog(entity);
  410. if (!entity) {
  411. DBG_FTL(("Zero entity on remove"))
  412. return (0);
  413. }
  414. if (!(p_os =
  415. (diva_um_idi_os_context_t *)
  416. diva_um_id_get_os_context(entity))) {
  417. DBG_FTL(("Zero entity os context on remove"))
  418. return (0);
  419. }
  420. if (!divas_um_idi_entity_assigned(entity) || p_os->aborted) {
  421. /*
  422. Entity is not assigned, also can be removed
  423. */
  424. return (0);
  425. }
  426. DBG_TRC(("E(%08x) check remove", entity))
  427. /*
  428. If adapter not answers on remove request inside of
  429. 10 Sec, then adapter is dead
  430. */
  431. diva_um_idi_start_wdog(entity);
  432. {
  433. DECLARE_WAITQUEUE(wait, curtask);
  434. add_wait_queue(&p_os->close_wait, &wait);
  435. for (;;) {
  436. set_current_state(TASK_INTERRUPTIBLE);
  437. if (!divas_um_idi_entity_start_remove(entity)
  438. || p_os->aborted) {
  439. break;
  440. }
  441. schedule();
  442. }
  443. set_current_state(TASK_RUNNING);
  444. remove_wait_queue(&p_os->close_wait, &wait);
  445. }
  446. DBG_TRC(("E(%08x) start remove", entity))
  447. {
  448. DECLARE_WAITQUEUE(wait, curtask);
  449. add_wait_queue(&p_os->close_wait, &wait);
  450. for (;;) {
  451. set_current_state(TASK_INTERRUPTIBLE);
  452. if (!divas_um_idi_entity_assigned(entity)
  453. || p_os->aborted) {
  454. break;
  455. }
  456. schedule();
  457. }
  458. set_current_state(TASK_RUNNING);
  459. remove_wait_queue(&p_os->close_wait, &wait);
  460. }
  461. DBG_TRC(("E(%08x) remove complete, aborted:%d", entity,
  462. p_os->aborted))
  463. diva_um_idi_stop_wdog(entity);
  464. p_os->aborted = 0;
  465. return (0);
  466. }
  467. /*
  468. * timer watchdog
  469. */
  470. void diva_um_idi_start_wdog(void *entity)
  471. {
  472. diva_um_idi_os_context_t *p_os;
  473. if (entity &&
  474. ((p_os =
  475. (diva_um_idi_os_context_t *)
  476. diva_um_id_get_os_context(entity)))) {
  477. mod_timer(&p_os->diva_timer_id, jiffies + 10 * HZ);
  478. }
  479. }
  480. void diva_um_idi_stop_wdog(void *entity)
  481. {
  482. diva_um_idi_os_context_t *p_os;
  483. if (entity &&
  484. ((p_os =
  485. (diva_um_idi_os_context_t *)
  486. diva_um_id_get_os_context(entity)))) {
  487. del_timer(&p_os->diva_timer_id);
  488. }
  489. }