zftape-init.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:12k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  *      Copyright (C) 1996, 1997 Claus-Justus Heine.
  3.  This program is free software; you can redistribute it and/or modify
  4.  it under the terms of the GNU General Public License as published by
  5.  the Free Software Foundation; either version 2, or (at your option)
  6.  any later version.
  7.  This program is distributed in the hope that it will be useful,
  8.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  GNU General Public License for more details.
  11.  You should have received a copy of the GNU General Public License
  12.  along with this program; see the file COPYING.  If not, write to
  13.  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  14.  *
  15.  *      This file contains the code that registers the zftape frontend 
  16.  *      to the ftape floppy tape driver for Linux
  17.  */
  18. #include <linux/config.h>
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <linux/version.h>
  22. #include <linux/fs.h>
  23. #include <asm/segment.h>
  24. #include <linux/kernel.h>
  25. #include <linux/signal.h>
  26. #include <linux/major.h>
  27. #include <linux/slab.h>
  28. #ifdef CONFIG_KMOD
  29. #include <linux/kmod.h>
  30. #endif
  31. #include <linux/fcntl.h>
  32. #include <linux/wrapper.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/devfs_fs_kernel.h>
  35. #include <linux/zftape.h>
  36. #include <linux/init.h>
  37. #include "../zftape/zftape-init.h"
  38. #include "../zftape/zftape-read.h"
  39. #include "../zftape/zftape-write.h"
  40. #include "../zftape/zftape-ctl.h"
  41. #include "../zftape/zftape-buffers.h"
  42. #include "../zftape/zftape_syms.h"
  43. char zft_src[] __initdata = "$Source: /homes/cvs/ftape-stacked/ftape/zftape/zftape-init.c,v $";
  44. char zft_rev[] __initdata = "$Revision: 1.8 $";
  45. char zft_dat[] __initdata = "$Date: 1997/11/06 00:48:56 $";
  46. MODULE_AUTHOR("(c) 1996, 1997 Claus-Justus Heine "
  47.       "(claus@momo.math.rwth-aachen.de)");
  48. MODULE_DESCRIPTION(ZFTAPE_VERSION " - "
  49.    "VFS interface for the Linux floppy tape driver. "
  50.    "Support for QIC-113 compatible volume table "
  51.    "and builtin compression (lzrw3 algorithm)");
  52. MODULE_SUPPORTED_DEVICE("char-major-27");
  53. MODULE_LICENSE("GPL");
  54. /*      Global vars.
  55.  */
  56. struct zft_cmpr_ops *zft_cmpr_ops = NULL;
  57. const ftape_info *zft_status;
  58. /*      Local vars.
  59.  */
  60. static int busy_flag = 0;
  61. static sigset_t orig_sigmask;
  62. /*  the interface to the kernel vfs layer
  63.  */
  64. /* Note about llseek():
  65.  *
  66.  * st.c and tpqic.c update fp->f_pos but don't implment llseek() and
  67.  * initialize the llseek component of the file_ops struct with NULL.
  68.  * This means that the user will get the default seek, but the tape
  69.  * device will not respect the new position, but happily read from the
  70.  * old position. Think a zftape specific llseek() function would be
  71.  * better, returning -ESPIPE. TODO.
  72.  */
  73. static int  zft_open (struct inode *ino, struct file *filep);
  74. static int zft_close(struct inode *ino, struct file *filep);
  75. static int  zft_ioctl(struct inode *ino, struct file *filep,
  76.       unsigned int command, unsigned long arg);
  77. static int  zft_mmap(struct file *filep, struct vm_area_struct *vma);
  78. static ssize_t zft_read (struct file *fp, char *buff,
  79.  size_t req_len, loff_t *ppos);
  80. static ssize_t zft_write(struct file *fp, const char *buff,
  81.  size_t req_len, loff_t *ppos);
  82. static struct file_operations zft_cdev =
  83. {
  84. owner: THIS_MODULE,
  85. read: zft_read,
  86. write: zft_write,
  87. ioctl: zft_ioctl,
  88. mmap: zft_mmap,
  89. open: zft_open,
  90. release: zft_close,
  91. };
  92. /*      Open floppy tape device
  93.  */
  94. static int zft_open(struct inode *ino, struct file *filep)
  95. {
  96. int result;
  97. TRACE_FUN(ft_t_flow);
  98. TRACE(ft_t_flow, "called for minor %d", MINOR(ino->i_rdev));
  99. if (busy_flag) {
  100. TRACE_ABORT(-EBUSY, ft_t_warn, "failed: already busy");
  101. }
  102. busy_flag = 1;
  103. if ((MINOR(ino->i_rdev) & ~(ZFT_MINOR_OP_MASK | FTAPE_NO_REWIND))
  104.      > 
  105.     FTAPE_SEL_D) {
  106. busy_flag = 0;
  107. TRACE_ABORT(-ENXIO, ft_t_err, "failed: illegal unit nr");
  108. }
  109. orig_sigmask = current->blocked;
  110. sigfillset(&current->blocked);
  111. result = _zft_open(MINOR(ino->i_rdev), filep->f_flags & O_ACCMODE);
  112. if (result < 0) {
  113. current->blocked = orig_sigmask; /* restore mask */
  114. busy_flag = 0;
  115. TRACE_ABORT(result, ft_t_err, "_ftape_open failed");
  116. } else {
  117. /* Mask signals that will disturb proper operation of the
  118.  * program that is calling.
  119.  */
  120. current->blocked = orig_sigmask;
  121. sigaddsetmask (&current->blocked, _DO_BLOCK);
  122. TRACE_EXIT 0;
  123. }
  124. }
  125. /*      Close floppy tape device
  126.  */
  127. static int zft_close(struct inode *ino, struct file *filep)
  128. {
  129. int result;
  130. TRACE_FUN(ft_t_flow);
  131. lock_kernel();
  132. if (!busy_flag || MINOR(ino->i_rdev) != zft_unit) {
  133. TRACE(ft_t_err, "failed: not busy or wrong unit");
  134. unlock_kernel();
  135. TRACE_EXIT 0;
  136. }
  137. sigfillset(&current->blocked);
  138. result = _zft_close();
  139. if (result < 0) {
  140. TRACE(ft_t_err, "_zft_close failed");
  141. }
  142. current->blocked = orig_sigmask; /* restore before open state */
  143. busy_flag = 0;
  144. unlock_kernel();
  145. TRACE_EXIT 0;
  146. }
  147. /*      Ioctl for floppy tape device
  148.  */
  149. static int zft_ioctl(struct inode *ino, struct file *filep,
  150.      unsigned int command, unsigned long arg)
  151. {
  152. int result = -EIO;
  153. sigset_t old_sigmask;
  154. TRACE_FUN(ft_t_flow);
  155. if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
  156. TRACE_ABORT(-EIO, ft_t_err,
  157.     "failed: not busy, failure or wrong unit");
  158. }
  159. old_sigmask = current->blocked; /* save mask */
  160. sigfillset(&current->blocked);
  161. /* This will work as long as sizeof(void *) == sizeof(long) */
  162. result = _zft_ioctl(command, (void *) arg);
  163. current->blocked = old_sigmask; /* restore mask */
  164. TRACE_EXIT result;
  165. }
  166. /*      Ioctl for floppy tape device
  167.  */
  168. static int  zft_mmap(struct file *filep, struct vm_area_struct *vma)
  169. {
  170. int result = -EIO;
  171. sigset_t old_sigmask;
  172. TRACE_FUN(ft_t_flow);
  173. if (!busy_flag || 
  174.     MINOR(filep->f_dentry->d_inode->i_rdev) != zft_unit || 
  175.     ft_failure)
  176. {
  177. TRACE_ABORT(-EIO, ft_t_err,
  178.     "failed: not busy, failure or wrong unit");
  179. }
  180. old_sigmask = current->blocked; /* save mask */
  181. sigfillset(&current->blocked);
  182. lock_kernel();
  183. if ((result = ftape_mmap(vma)) >= 0) {
  184. #ifndef MSYNC_BUG_WAS_FIXED
  185. static struct vm_operations_struct dummy = { NULL, };
  186. vma->vm_ops = &dummy;
  187. #endif
  188. }
  189. unlock_kernel();
  190. current->blocked = old_sigmask; /* restore mask */
  191. TRACE_EXIT result;
  192. }
  193. /*      Read from floppy tape device
  194.  */
  195. static ssize_t zft_read(struct file *fp, char *buff,
  196. size_t req_len, loff_t *ppos)
  197. {
  198. int result = -EIO;
  199. sigset_t old_sigmask;
  200. struct inode *ino = fp->f_dentry->d_inode;
  201. TRACE_FUN(ft_t_flow);
  202. TRACE(ft_t_data_flow, "called with count: %ld", (unsigned long)req_len);
  203. if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
  204. TRACE_ABORT(-EIO, ft_t_err,
  205.     "failed: not busy, failure or wrong unit");
  206. }
  207. old_sigmask = current->blocked; /* save mask */
  208. sigfillset(&current->blocked);
  209. result = _zft_read(buff, req_len);
  210. current->blocked = old_sigmask; /* restore mask */
  211. TRACE(ft_t_data_flow, "return with count: %d", result);
  212. TRACE_EXIT result;
  213. }
  214. /*      Write to tape device
  215.  */
  216. static ssize_t zft_write(struct file *fp, const char *buff,
  217.  size_t req_len, loff_t *ppos)
  218. {
  219. int result = -EIO;
  220. sigset_t old_sigmask;
  221. struct inode *ino = fp->f_dentry->d_inode;
  222. TRACE_FUN(ft_t_flow);
  223. TRACE(ft_t_flow, "called with count: %ld", (unsigned long)req_len);
  224. if (!busy_flag || MINOR(ino->i_rdev) != zft_unit || ft_failure) {
  225. TRACE_ABORT(-EIO, ft_t_err,
  226.     "failed: not busy, failure or wrong unit");
  227. }
  228. old_sigmask = current->blocked; /* save mask */
  229. sigfillset(&current->blocked);
  230. result = _zft_write(buff, req_len);
  231. current->blocked = old_sigmask; /* restore mask */
  232. TRACE(ft_t_data_flow, "return with count: %d", result);
  233. TRACE_EXIT result;
  234. }
  235. /*                    END OF VFS INTERFACE 
  236.  *          
  237.  *****************************************************************************/
  238. /*  driver/module initialization
  239.  */
  240. /*  the compression module has to call this function to hook into the zftape 
  241.  *  code
  242.  */
  243. int zft_cmpr_register(struct zft_cmpr_ops *new_ops)
  244. {
  245. TRACE_FUN(ft_t_flow);
  246. if (zft_cmpr_ops != NULL) {
  247. TRACE_EXIT -EBUSY;
  248. } else {
  249. zft_cmpr_ops = new_ops;
  250. TRACE_EXIT 0;
  251. }
  252. }
  253. struct zft_cmpr_ops *zft_cmpr_unregister(void)
  254. {
  255. struct zft_cmpr_ops *old_ops = zft_cmpr_ops;
  256. TRACE_FUN(ft_t_flow);
  257. zft_cmpr_ops = NULL;
  258. TRACE_EXIT old_ops;
  259. }
  260. /*  lock the zft-compressor() module.
  261.  */
  262. int zft_cmpr_lock(int try_to_load)
  263. {
  264. if (zft_cmpr_ops == NULL) {
  265. #ifdef CONFIG_KMOD
  266. if (try_to_load) {
  267. request_module("zft-compressor");
  268. if (zft_cmpr_ops == NULL) {
  269. return -ENOSYS;
  270. }
  271. } else {
  272. return -ENOSYS;
  273. }
  274. #else
  275. return -ENOSYS;
  276. #endif
  277. }
  278. (*zft_cmpr_ops->lock)();
  279. return 0;
  280. }
  281. #ifdef CONFIG_ZFT_COMPRESSOR
  282. extern int zft_compressor_init(void);
  283. #endif
  284. /*  Called by modules package when installing the driver or by kernel
  285.  *  during the initialization phase
  286.  */
  287. int __init zft_init(void)
  288. {
  289. int i;
  290. TRACE_FUN(ft_t_flow);
  291. #ifdef MODULE
  292. printk(KERN_INFO ZFTAPE_VERSION "n");
  293.         if (TRACE_LEVEL >= ft_t_info) {
  294. printk(
  295. KERN_INFO
  296. "(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)n"
  297. KERN_INFO
  298. "vfs interface for ftape floppy tape driver.n"
  299. KERN_INFO
  300. "Support for QIC-113 compatible volume table, dynamic memory allocationn"
  301. KERN_INFO
  302. "and builtin compression (lzrw3 algorithm).n"
  303. KERN_INFO
  304. "Compiled for Linux version %s"
  305. #ifdef MODVERSIONS
  306.        " with versioned symbols"
  307. #endif
  308.        "n", UTS_RELEASE);
  309.         }
  310. #else /* !MODULE */
  311. /* print a short no-nonsense boot message */
  312. printk(KERN_INFO ZFTAPE_VERSION " for Linux " UTS_RELEASE "n");
  313. #endif /* MODULE */
  314. TRACE(ft_t_info, "zft_init @ 0x%p", zft_init);
  315. TRACE(ft_t_info,
  316.       "installing zftape VFS interface for ftape driver ...");
  317. TRACE_CATCH(devfs_register_chrdev(QIC117_TAPE_MAJOR, "zft", &zft_cdev),);
  318. for (i = 0; i < 4; i++) {
  319. char devname[9];
  320. sprintf (devname, "qft%i", i);
  321. devfs_register (NULL, devname, DEVFS_FL_DEFAULT,
  322.         QIC117_TAPE_MAJOR, i,
  323. S_IFCHR | S_IRUSR | S_IWUSR,
  324. &zft_cdev, NULL);
  325. sprintf (devname, "nqft%i", i);
  326. devfs_register (NULL, devname, DEVFS_FL_DEFAULT,
  327. QIC117_TAPE_MAJOR, i + 4,
  328. S_IFCHR | S_IRUSR | S_IWUSR,
  329. &zft_cdev, NULL);
  330. sprintf (devname, "zqft%i", i);
  331. devfs_register (NULL, devname, DEVFS_FL_DEFAULT,
  332. QIC117_TAPE_MAJOR, i + 16,
  333. S_IFCHR | S_IRUSR | S_IWUSR,
  334. &zft_cdev, NULL);
  335. sprintf (devname, "nzqft%i", i);
  336. devfs_register (NULL, devname, DEVFS_FL_DEFAULT,
  337. QIC117_TAPE_MAJOR, i + 20,
  338. S_IFCHR | S_IRUSR | S_IWUSR,
  339. &zft_cdev, NULL);
  340. sprintf (devname, "rawqft%i", i);
  341. devfs_register (NULL, devname, DEVFS_FL_DEFAULT,
  342. QIC117_TAPE_MAJOR, i + 32,
  343. S_IFCHR | S_IRUSR | S_IWUSR,
  344. &zft_cdev, NULL);
  345. sprintf (devname, "nrawqft%i", i);
  346. devfs_register (NULL, devname, DEVFS_FL_DEFAULT,
  347. QIC117_TAPE_MAJOR, i + 36,
  348. S_IFCHR | S_IRUSR | S_IWUSR,
  349. &zft_cdev, NULL);
  350. }
  351. #ifdef CONFIG_ZFT_COMPRESSOR
  352. (void)zft_compressor_init();
  353. #endif
  354. zft_status = ftape_get_status(); /*  fetch global data of ftape 
  355.   *  hardware driver 
  356.   */
  357. TRACE_EXIT 0;
  358. }
  359. #ifdef MODULE
  360. /* Called by modules package before trying to unload the module
  361.  */
  362. static int can_unload(void)
  363. {
  364. return (GET_USE_COUNT(THIS_MODULE)||zft_dirty()||busy_flag)?-EBUSY:0;
  365. }
  366. /* Called by modules package when installing the driver
  367.  */
  368. int init_module(void)
  369. {
  370. if (!mod_member_present(&__this_module, can_unload)) {
  371. return -EBUSY;
  372. }
  373. __this_module.can_unload = can_unload;
  374. return zft_init();
  375. }
  376. /* Called by modules package when removing the driver 
  377.  */
  378. void cleanup_module(void)
  379. {
  380. int i;
  381. char devname[9];
  382. TRACE_FUN(ft_t_flow);
  383. if (devfs_unregister_chrdev(QIC117_TAPE_MAJOR, "zft") != 0) {
  384. TRACE(ft_t_warn, "failed");
  385. } else {
  386. TRACE(ft_t_info, "successful");
  387. }
  388.         for (i = 0; i < 4; i++) {
  389. sprintf(devname, "qft%i", i);
  390. devfs_unregister(devfs_find_handle(NULL, devname, QIC117_TAPE_MAJOR, i, DEVFS_SPECIAL_CHR, 0));
  391. sprintf(devname, "nqft%i", i);
  392. devfs_unregister(devfs_find_handle(NULL, devname, QIC117_TAPE_MAJOR, i + 4, DEVFS_SPECIAL_CHR, 0));
  393. sprintf(devname, "zqft%i", i);
  394. devfs_unregister(devfs_find_handle(NULL, devname, QIC117_TAPE_MAJOR, i + 16, DEVFS_SPECIAL_CHR, 0));
  395. sprintf(devname, "nzqft%i", i);
  396. devfs_unregister(devfs_find_handle(NULL, devname, QIC117_TAPE_MAJOR, i + 20, DEVFS_SPECIAL_CHR, 0));
  397. sprintf(devname, "rawqft%i", i);
  398. devfs_unregister(devfs_find_handle(NULL, devname, QIC117_TAPE_MAJOR, i + 32, DEVFS_SPECIAL_CHR, 0));
  399. sprintf(devname, "nrawqft%i", i);
  400. devfs_unregister(devfs_find_handle(NULL, devname, QIC117_TAPE_MAJOR, i + 36, DEVFS_SPECIAL_CHR, 0));
  401. }
  402. zft_uninit_mem(); /* release remaining memory, if any */
  403.         printk(KERN_INFO "zftape successfully unloaded.n");
  404. TRACE_EXIT;
  405. }
  406. #endif /* MODULE */