zftape-init.c 11 KB

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