zftape-init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/fs.h>
  22. #include <linux/kernel.h>
  23. #include <linux/signal.h>
  24. #include <linux/major.h>
  25. #include <linux/slab.h>
  26. #ifdef CONFIG_KMOD
  27. #include <linux/kmod.h>
  28. #endif
  29. #include <linux/fcntl.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/zftape.h>
  32. #include <linux/init.h>
  33. #include <linux/device.h>
  34. #include "../zftape/zftape-init.h"
  35. #include "../zftape/zftape-read.h"
  36. #include "../zftape/zftape-write.h"
  37. #include "../zftape/zftape-ctl.h"
  38. #include "../zftape/zftape-buffers.h"
  39. MODULE_AUTHOR("(c) 1996, 1997 Claus-Justus Heine "
  40. "(claus@momo.math.rwth-aachen.de)");
  41. MODULE_DESCRIPTION(ZFTAPE_VERSION " - "
  42. "VFS interface for the Linux floppy tape driver. "
  43. "Support for QIC-113 compatible volume table "
  44. "and builtin compression (lzrw3 algorithm)");
  45. MODULE_SUPPORTED_DEVICE("char-major-27");
  46. MODULE_LICENSE("GPL");
  47. /* Global vars.
  48. */
  49. struct zft_cmpr_ops *zft_cmpr_ops = NULL;
  50. const ftape_info *zft_status;
  51. /* Local vars.
  52. */
  53. static unsigned long busy_flag;
  54. static sigset_t orig_sigmask;
  55. /* the interface to the kernel vfs layer
  56. */
  57. /* Note about llseek():
  58. *
  59. * st.c and tpqic.c update fp->f_pos but don't implment llseek() and
  60. * initialize the llseek component of the file_ops struct with NULL.
  61. * This means that the user will get the default seek, but the tape
  62. * device will not respect the new position, but happily read from the
  63. * old position. Think a zftape specific llseek() function would be
  64. * better, returning -ESPIPE. TODO.
  65. */
  66. static int zft_open (struct inode *ino, struct file *filep);
  67. static int zft_close(struct inode *ino, struct file *filep);
  68. static int zft_ioctl(struct inode *ino, struct file *filep,
  69. unsigned int command, unsigned long arg);
  70. static int zft_mmap(struct file *filep, struct vm_area_struct *vma);
  71. static ssize_t zft_read (struct file *fp, char __user *buff,
  72. size_t req_len, loff_t *ppos);
  73. static ssize_t zft_write(struct file *fp, const char __user *buff,
  74. size_t req_len, loff_t *ppos);
  75. static struct file_operations zft_cdev =
  76. {
  77. .owner = THIS_MODULE,
  78. .read = zft_read,
  79. .write = zft_write,
  80. .ioctl = zft_ioctl,
  81. .mmap = zft_mmap,
  82. .open = zft_open,
  83. .release = zft_close,
  84. };
  85. static struct class *zft_class;
  86. /* Open floppy tape device
  87. */
  88. static int zft_open(struct inode *ino, struct file *filep)
  89. {
  90. int result;
  91. TRACE_FUN(ft_t_flow);
  92. nonseekable_open(ino, filep);
  93. TRACE(ft_t_flow, "called for minor %d", iminor(ino));
  94. if ( test_and_set_bit(0,&busy_flag) ) {
  95. TRACE_ABORT(-EBUSY, ft_t_warn, "failed: already busy");
  96. }
  97. if ((iminor(ino) & ~(ZFT_MINOR_OP_MASK | FTAPE_NO_REWIND))
  98. >
  99. FTAPE_SEL_D) {
  100. clear_bit(0,&busy_flag);
  101. TRACE_ABORT(-ENXIO, ft_t_err, "failed: invalid unit nr");
  102. }
  103. orig_sigmask = current->blocked;
  104. sigfillset(&current->blocked);
  105. result = _zft_open(iminor(ino), filep->f_flags & O_ACCMODE);
  106. if (result < 0) {
  107. current->blocked = orig_sigmask; /* restore mask */
  108. clear_bit(0,&busy_flag);
  109. TRACE_ABORT(result, ft_t_err, "_ftape_open failed");
  110. } else {
  111. /* Mask signals that will disturb proper operation of the
  112. * program that is calling.
  113. */
  114. current->blocked = orig_sigmask;
  115. sigaddsetmask (&current->blocked, _DO_BLOCK);
  116. TRACE_EXIT 0;
  117. }
  118. }
  119. /* Close floppy tape device
  120. */
  121. static int zft_close(struct inode *ino, struct file *filep)
  122. {
  123. int result;
  124. TRACE_FUN(ft_t_flow);
  125. if ( !test_bit(0,&busy_flag) || iminor(ino) != zft_unit) {
  126. TRACE(ft_t_err, "failed: not busy or wrong unit");
  127. TRACE_EXIT 0;
  128. }
  129. sigfillset(&current->blocked);
  130. result = _zft_close();
  131. if (result < 0) {
  132. TRACE(ft_t_err, "_zft_close failed");
  133. }
  134. current->blocked = orig_sigmask; /* restore before open state */
  135. clear_bit(0,&busy_flag);
  136. TRACE_EXIT 0;
  137. }
  138. /* Ioctl for floppy tape device
  139. */
  140. static int zft_ioctl(struct inode *ino, struct file *filep,
  141. unsigned int command, unsigned long arg)
  142. {
  143. int result = -EIO;
  144. sigset_t old_sigmask;
  145. TRACE_FUN(ft_t_flow);
  146. if ( !test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
  147. TRACE_ABORT(-EIO, ft_t_err,
  148. "failed: not busy, failure or wrong unit");
  149. }
  150. old_sigmask = current->blocked; /* save mask */
  151. sigfillset(&current->blocked);
  152. /* This will work as long as sizeof(void *) == sizeof(long) */
  153. result = _zft_ioctl(command, (void __user *) arg);
  154. current->blocked = old_sigmask; /* restore mask */
  155. TRACE_EXIT result;
  156. }
  157. /* Ioctl for floppy tape device
  158. */
  159. static int zft_mmap(struct file *filep, struct vm_area_struct *vma)
  160. {
  161. int result = -EIO;
  162. sigset_t old_sigmask;
  163. TRACE_FUN(ft_t_flow);
  164. if ( !test_bit(0,&busy_flag) ||
  165. iminor(filep->f_dentry->d_inode) != zft_unit ||
  166. ft_failure)
  167. {
  168. TRACE_ABORT(-EIO, ft_t_err,
  169. "failed: not busy, failure or wrong unit");
  170. }
  171. old_sigmask = current->blocked; /* save mask */
  172. sigfillset(&current->blocked);
  173. if ((result = ftape_mmap(vma)) >= 0) {
  174. #ifndef MSYNC_BUG_WAS_FIXED
  175. static struct vm_operations_struct dummy = { NULL, };
  176. vma->vm_ops = &dummy;
  177. #endif
  178. }
  179. current->blocked = old_sigmask; /* restore mask */
  180. TRACE_EXIT result;
  181. }
  182. /* Read from floppy tape device
  183. */
  184. static ssize_t zft_read(struct file *fp, char __user *buff,
  185. size_t req_len, loff_t *ppos)
  186. {
  187. int result = -EIO;
  188. sigset_t old_sigmask;
  189. struct inode *ino = fp->f_dentry->d_inode;
  190. TRACE_FUN(ft_t_flow);
  191. TRACE(ft_t_data_flow, "called with count: %ld", (unsigned long)req_len);
  192. if (!test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
  193. TRACE_ABORT(-EIO, ft_t_err,
  194. "failed: not busy, failure or wrong unit");
  195. }
  196. old_sigmask = current->blocked; /* save mask */
  197. sigfillset(&current->blocked);
  198. result = _zft_read(buff, req_len);
  199. current->blocked = old_sigmask; /* restore mask */
  200. TRACE(ft_t_data_flow, "return with count: %d", result);
  201. TRACE_EXIT result;
  202. }
  203. /* Write to tape device
  204. */
  205. static ssize_t zft_write(struct file *fp, const char __user *buff,
  206. size_t req_len, loff_t *ppos)
  207. {
  208. int result = -EIO;
  209. sigset_t old_sigmask;
  210. struct inode *ino = fp->f_dentry->d_inode;
  211. TRACE_FUN(ft_t_flow);
  212. TRACE(ft_t_flow, "called with count: %ld", (unsigned long)req_len);
  213. if (!test_bit(0,&busy_flag) || iminor(ino) != zft_unit || ft_failure) {
  214. TRACE_ABORT(-EIO, ft_t_err,
  215. "failed: not busy, failure or wrong unit");
  216. }
  217. old_sigmask = current->blocked; /* save mask */
  218. sigfillset(&current->blocked);
  219. result = _zft_write(buff, req_len);
  220. current->blocked = old_sigmask; /* restore mask */
  221. TRACE(ft_t_data_flow, "return with count: %d", result);
  222. TRACE_EXIT result;
  223. }
  224. /* END OF VFS INTERFACE
  225. *
  226. *****************************************************************************/
  227. /* driver/module initialization
  228. */
  229. /* the compression module has to call this function to hook into the zftape
  230. * code
  231. */
  232. int zft_cmpr_register(struct zft_cmpr_ops *new_ops)
  233. {
  234. TRACE_FUN(ft_t_flow);
  235. if (zft_cmpr_ops != NULL) {
  236. TRACE_EXIT -EBUSY;
  237. } else {
  238. zft_cmpr_ops = new_ops;
  239. TRACE_EXIT 0;
  240. }
  241. }
  242. /* lock the zft-compressor() module.
  243. */
  244. int zft_cmpr_lock(int try_to_load)
  245. {
  246. if (zft_cmpr_ops == NULL) {
  247. #ifdef CONFIG_KMOD
  248. if (try_to_load) {
  249. request_module("zft-compressor");
  250. if (zft_cmpr_ops == NULL) {
  251. return -ENOSYS;
  252. }
  253. } else {
  254. return -ENOSYS;
  255. }
  256. #else
  257. return -ENOSYS;
  258. #endif
  259. }
  260. (*zft_cmpr_ops->lock)();
  261. return 0;
  262. }
  263. #ifdef CONFIG_ZFT_COMPRESSOR
  264. extern int zft_compressor_init(void);
  265. #endif
  266. /* Called by modules package when installing the driver or by kernel
  267. * during the initialization phase
  268. */
  269. int __init zft_init(void)
  270. {
  271. int i;
  272. TRACE_FUN(ft_t_flow);
  273. #ifdef MODULE
  274. printk(KERN_INFO ZFTAPE_VERSION "\n");
  275. if (TRACE_LEVEL >= ft_t_info) {
  276. printk(
  277. KERN_INFO
  278. "(c) 1996, 1997 Claus-Justus Heine (claus@momo.math.rwth-aachen.de)\n"
  279. KERN_INFO
  280. "vfs interface for ftape floppy tape driver.\n"
  281. KERN_INFO
  282. "Support for QIC-113 compatible volume table, dynamic memory allocation\n"
  283. KERN_INFO
  284. "and builtin compression (lzrw3 algorithm).\n");
  285. }
  286. #else /* !MODULE */
  287. /* print a short no-nonsense boot message */
  288. printk(KERN_INFO ZFTAPE_VERSION "\n");
  289. #endif /* MODULE */
  290. TRACE(ft_t_info, "zft_init @ 0x%p", zft_init);
  291. TRACE(ft_t_info,
  292. "installing zftape VFS interface for ftape driver ...");
  293. TRACE_CATCH(register_chrdev(QIC117_TAPE_MAJOR, "zft", &zft_cdev),);
  294. zft_class = class_create(THIS_MODULE, "zft");
  295. for (i = 0; i < 4; i++) {
  296. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i), NULL, "qft%i", i);
  297. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 4), NULL, "nqft%i", i);
  298. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 16), NULL, "zqft%i", i);
  299. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 20), NULL, "nzqft%i", i);
  300. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 32), NULL, "rawqft%i", i);
  301. class_device_create(zft_class, NULL, MKDEV(QIC117_TAPE_MAJOR, i + 36), NULL, "nrawrawqft%i", i);
  302. }
  303. #ifdef CONFIG_ZFT_COMPRESSOR
  304. (void)zft_compressor_init();
  305. #endif
  306. zft_status = ftape_get_status(); /* fetch global data of ftape
  307. * hardware driver
  308. */
  309. TRACE_EXIT 0;
  310. }
  311. /* Called by modules package when removing the driver
  312. */
  313. static void zft_exit(void)
  314. {
  315. int i;
  316. TRACE_FUN(ft_t_flow);
  317. if (unregister_chrdev(QIC117_TAPE_MAJOR, "zft") != 0) {
  318. TRACE(ft_t_warn, "failed");
  319. } else {
  320. TRACE(ft_t_info, "successful");
  321. }
  322. for (i = 0; i < 4; i++) {
  323. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i));
  324. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 4));
  325. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 16));
  326. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 20));
  327. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 32));
  328. class_device_destroy(zft_class, MKDEV(QIC117_TAPE_MAJOR, i + 36));
  329. }
  330. class_destroy(zft_class);
  331. zft_uninit_mem(); /* release remaining memory, if any */
  332. printk(KERN_INFO "zftape successfully unloaded.\n");
  333. TRACE_EXIT;
  334. }
  335. module_init(zft_init);
  336. module_exit(zft_exit);