usb gadget: g_ether spinlock recursion fix
[linux-2.6/kvm.git] / drivers / s390 / char / tape_char.c
blob97f75237bed6cbc3b2d64767a8e915e4eeee6740
1 /*
2 * drivers/s390/char/tape_char.c
3 * character device frontend for tape device driver
5 * S390 and zSeries version
6 * Copyright (C) 2001,2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Michael Holzheu <holzheu@de.ibm.com>
9 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
10 * Martin Schwidefsky <schwidefsky@de.ibm.com>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/proc_fs.h>
16 #include <linux/mtio.h>
18 #include <asm/uaccess.h>
20 #define TAPE_DBF_AREA tape_core_dbf
22 #include "tape.h"
23 #include "tape_std.h"
24 #include "tape_class.h"
26 #define PRINTK_HEADER "TAPE_CHAR: "
28 #define TAPECHAR_MAJOR 0 /* get dynamic major */
31 * file operation structure for tape character frontend
33 static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *);
34 static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
35 static int tapechar_open(struct inode *,struct file *);
36 static int tapechar_release(struct inode *,struct file *);
37 static int tapechar_ioctl(struct inode *, struct file *, unsigned int,
38 unsigned long);
39 static long tapechar_compat_ioctl(struct file *, unsigned int,
40 unsigned long);
42 static struct file_operations tape_fops =
44 .owner = THIS_MODULE,
45 .read = tapechar_read,
46 .write = tapechar_write,
47 .ioctl = tapechar_ioctl,
48 .compat_ioctl = tapechar_compat_ioctl,
49 .open = tapechar_open,
50 .release = tapechar_release,
53 static int tapechar_major = TAPECHAR_MAJOR;
56 * This function is called for every new tapedevice
58 int
59 tapechar_setup_device(struct tape_device * device)
61 char device_name[20];
63 sprintf(device_name, "ntibm%i", device->first_minor / 2);
64 device->nt = register_tape_dev(
65 &device->cdev->dev,
66 MKDEV(tapechar_major, device->first_minor),
67 &tape_fops,
68 device_name,
69 "non-rewinding"
71 device_name[0] = 'r';
72 device->rt = register_tape_dev(
73 &device->cdev->dev,
74 MKDEV(tapechar_major, device->first_minor + 1),
75 &tape_fops,
76 device_name,
77 "rewinding"
80 return 0;
83 void
84 tapechar_cleanup_device(struct tape_device *device)
86 unregister_tape_dev(device->rt);
87 device->rt = NULL;
88 unregister_tape_dev(device->nt);
89 device->nt = NULL;
93 * Terminate write command (we write two TMs and skip backward over last)
94 * This ensures that the tape is always correctly terminated.
95 * When the user writes afterwards a new file, he will overwrite the
96 * second TM and therefore one TM will remain to separate the
97 * two files on the tape...
99 static inline void
100 tapechar_terminate_write(struct tape_device *device)
102 if (tape_mtop(device, MTWEOF, 1) == 0 &&
103 tape_mtop(device, MTWEOF, 1) == 0)
104 tape_mtop(device, MTBSR, 1);
107 static inline int
108 tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
110 struct idal_buffer *new;
112 if (device->char_data.idal_buf != NULL &&
113 device->char_data.idal_buf->size == block_size)
114 return 0;
116 if (block_size > MAX_BLOCKSIZE) {
117 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
118 block_size, MAX_BLOCKSIZE);
119 PRINT_ERR("Invalid blocksize (%zd> %d)\n",
120 block_size, MAX_BLOCKSIZE);
121 return -EINVAL;
124 /* The current idal buffer is not correct. Allocate a new one. */
125 new = idal_buffer_alloc(block_size, 0);
126 if (new == NULL)
127 return -ENOMEM;
129 if (device->char_data.idal_buf != NULL)
130 idal_buffer_free(device->char_data.idal_buf);
132 device->char_data.idal_buf = new;
134 return 0;
138 * Tape device read function
140 ssize_t
141 tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
143 struct tape_device *device;
144 struct tape_request *request;
145 size_t block_size;
146 int rc;
148 DBF_EVENT(6, "TCHAR:read\n");
149 device = (struct tape_device *) filp->private_data;
152 * If the tape isn't terminated yet, do it now. And since we then
153 * are at the end of the tape there wouldn't be anything to read
154 * anyways. So we return immediatly.
156 if(device->required_tapemarks) {
157 return tape_std_terminate_write(device);
160 /* Find out block size to use */
161 if (device->char_data.block_size != 0) {
162 if (count < device->char_data.block_size) {
163 DBF_EVENT(3, "TCHAR:read smaller than block "
164 "size was requested\n");
165 return -EINVAL;
167 block_size = device->char_data.block_size;
168 } else {
169 block_size = count;
172 rc = tapechar_check_idalbuffer(device, block_size);
173 if (rc)
174 return rc;
176 #ifdef CONFIG_S390_TAPE_BLOCK
177 /* Changes position. */
178 device->blk_data.medium_changed = 1;
179 #endif
181 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
182 /* Let the discipline build the ccw chain. */
183 request = device->discipline->read_block(device, block_size);
184 if (IS_ERR(request))
185 return PTR_ERR(request);
186 /* Execute it. */
187 rc = tape_do_io(device, request);
188 if (rc == 0) {
189 rc = block_size - request->rescnt;
190 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
191 filp->f_pos += rc;
192 /* Copy data from idal buffer to user space. */
193 if (idal_buffer_to_user(device->char_data.idal_buf,
194 data, rc) != 0)
195 rc = -EFAULT;
197 tape_free_request(request);
198 return rc;
202 * Tape device write function
204 ssize_t
205 tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
207 struct tape_device *device;
208 struct tape_request *request;
209 size_t block_size;
210 size_t written;
211 int nblocks;
212 int i, rc;
214 DBF_EVENT(6, "TCHAR:write\n");
215 device = (struct tape_device *) filp->private_data;
216 /* Find out block size and number of blocks */
217 if (device->char_data.block_size != 0) {
218 if (count < device->char_data.block_size) {
219 DBF_EVENT(3, "TCHAR:write smaller than block "
220 "size was requested\n");
221 return -EINVAL;
223 block_size = device->char_data.block_size;
224 nblocks = count / block_size;
225 } else {
226 block_size = count;
227 nblocks = 1;
230 rc = tapechar_check_idalbuffer(device, block_size);
231 if (rc)
232 return rc;
234 #ifdef CONFIG_S390_TAPE_BLOCK
235 /* Changes position. */
236 device->blk_data.medium_changed = 1;
237 #endif
239 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
240 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
241 /* Let the discipline build the ccw chain. */
242 request = device->discipline->write_block(device, block_size);
243 if (IS_ERR(request))
244 return PTR_ERR(request);
245 rc = 0;
246 written = 0;
247 for (i = 0; i < nblocks; i++) {
248 /* Copy data from user space to idal buffer. */
249 if (idal_buffer_from_user(device->char_data.idal_buf,
250 data, block_size)) {
251 rc = -EFAULT;
252 break;
254 rc = tape_do_io(device, request);
255 if (rc)
256 break;
257 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
258 block_size - request->rescnt);
259 filp->f_pos += block_size - request->rescnt;
260 written += block_size - request->rescnt;
261 if (request->rescnt != 0)
262 break;
263 data += block_size;
265 tape_free_request(request);
266 if (rc == -ENOSPC) {
268 * Ok, the device has no more space. It has NOT written
269 * the block.
271 if (device->discipline->process_eov)
272 device->discipline->process_eov(device);
273 if (written > 0)
274 rc = 0;
279 * After doing a write we always need two tapemarks to correctly
280 * terminate the tape (one to terminate the file, the second to
281 * flag the end of recorded data.
282 * Since process_eov positions the tape in front of the written
283 * tapemark it doesn't hurt to write two marks again.
285 if (!rc)
286 device->required_tapemarks = 2;
288 return rc ? rc : written;
292 * Character frontend tape device open function.
295 tapechar_open (struct inode *inode, struct file *filp)
297 struct tape_device *device;
298 int minor, rc;
300 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
301 imajor(filp->f_dentry->d_inode),
302 iminor(filp->f_dentry->d_inode));
304 if (imajor(filp->f_dentry->d_inode) != tapechar_major)
305 return -ENODEV;
307 minor = iminor(filp->f_dentry->d_inode);
308 device = tape_get_device(minor / TAPE_MINORS_PER_DEV);
309 if (IS_ERR(device)) {
310 DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
311 return PTR_ERR(device);
315 rc = tape_open(device);
316 if (rc == 0) {
317 filp->private_data = device;
318 return nonseekable_open(inode, filp);
320 tape_put_device(device);
322 return rc;
326 * Character frontend tape device release function.
330 tapechar_release(struct inode *inode, struct file *filp)
332 struct tape_device *device;
334 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
335 device = (struct tape_device *) filp->private_data;
338 * If this is the rewinding tape minor then rewind. In that case we
339 * write all required tapemarks. Otherwise only one to terminate the
340 * file.
342 if ((iminor(inode) & 1) != 0) {
343 if (device->required_tapemarks)
344 tape_std_terminate_write(device);
345 tape_mtop(device, MTREW, 1);
346 } else {
347 if (device->required_tapemarks > 1) {
348 if (tape_mtop(device, MTWEOF, 1) == 0)
349 device->required_tapemarks--;
353 if (device->char_data.idal_buf != NULL) {
354 idal_buffer_free(device->char_data.idal_buf);
355 device->char_data.idal_buf = NULL;
357 tape_release(device);
358 filp->private_data = tape_put_device(device);
360 return 0;
364 * Tape device io controls.
366 static int
367 tapechar_ioctl(struct inode *inp, struct file *filp,
368 unsigned int no, unsigned long data)
370 struct tape_device *device;
371 int rc;
373 DBF_EVENT(6, "TCHAR:ioct\n");
375 device = (struct tape_device *) filp->private_data;
377 if (no == MTIOCTOP) {
378 struct mtop op;
380 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
381 return -EFAULT;
382 if (op.mt_count < 0)
383 return -EINVAL;
386 * Operations that change tape position should write final
387 * tapemarks.
389 switch (op.mt_op) {
390 case MTFSF:
391 case MTBSF:
392 case MTFSR:
393 case MTBSR:
394 case MTREW:
395 case MTOFFL:
396 case MTEOM:
397 case MTRETEN:
398 case MTBSFM:
399 case MTFSFM:
400 case MTSEEK:
401 #ifdef CONFIG_S390_TAPE_BLOCK
402 device->blk_data.medium_changed = 1;
403 #endif
404 if (device->required_tapemarks)
405 tape_std_terminate_write(device);
406 default:
409 rc = tape_mtop(device, op.mt_op, op.mt_count);
411 if (op.mt_op == MTWEOF && rc == 0) {
412 if (op.mt_count > device->required_tapemarks)
413 device->required_tapemarks = 0;
414 else
415 device->required_tapemarks -= op.mt_count;
417 return rc;
419 if (no == MTIOCPOS) {
420 /* MTIOCPOS: query the tape position. */
421 struct mtpos pos;
423 rc = tape_mtop(device, MTTELL, 1);
424 if (rc < 0)
425 return rc;
426 pos.mt_blkno = rc;
427 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
428 return -EFAULT;
429 return 0;
431 if (no == MTIOCGET) {
432 /* MTIOCGET: query the tape drive status. */
433 struct mtget get;
435 memset(&get, 0, sizeof(get));
436 get.mt_type = MT_ISUNKNOWN;
437 get.mt_resid = 0 /* device->devstat.rescnt */;
438 get.mt_dsreg = device->tape_state;
439 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
440 get.mt_gstat = 0;
441 get.mt_erreg = 0;
442 get.mt_fileno = 0;
443 get.mt_gstat = device->tape_generic_status;
445 if (device->medium_state == MS_LOADED) {
446 rc = tape_mtop(device, MTTELL, 1);
448 if (rc < 0)
449 return rc;
451 if (rc == 0)
452 get.mt_gstat |= GMT_BOT(~0);
454 get.mt_blkno = rc;
457 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
458 return -EFAULT;
460 return 0;
462 /* Try the discipline ioctl function. */
463 if (device->discipline->ioctl_fn == NULL)
464 return -EINVAL;
465 return device->discipline->ioctl_fn(device, no, data);
468 static long
469 tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
471 struct tape_device *device = filp->private_data;
472 int rval = -ENOIOCTLCMD;
474 if (device->discipline->ioctl_fn) {
475 lock_kernel();
476 rval = device->discipline->ioctl_fn(device, no, data);
477 unlock_kernel();
478 if (rval == -EINVAL)
479 rval = -ENOIOCTLCMD;
482 return rval;
486 * Initialize character device frontend.
489 tapechar_init (void)
491 dev_t dev;
493 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
494 return -1;
496 tapechar_major = MAJOR(dev);
497 PRINT_INFO("tape gets major %d for character devices\n", MAJOR(dev));
499 return 0;
503 * cleanup
505 void
506 tapechar_exit(void)
508 PRINT_INFO("tape releases major %d for character devices\n",
509 tapechar_major);
510 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);