[PATCH] v4l: keep tvaudio driver away from saa7146
[linux-2.6/history.git] / drivers / ide / ide-taskfile.c
blob182980eec4773edfe4e12ff2be1af477f2f2e60d
1 /*
2 * linux/drivers/ide/ide-taskfile.c Version 0.38 March 05, 2003
4 * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
5 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
6 * Copyright (C) 2001-2002 Klaus Smolin
7 * IBM Storage Technology Division
8 * Copyright (C) 2003-2004 Bartlomiej Zolnierkiewicz
10 * The big the bad and the ugly.
12 * Problems to be fixed because of BH interface or the lack therefore.
14 * Fill me in stupid !!!
16 * HOST:
17 * General refers to the Controller and Driver "pair".
18 * DATA HANDLER:
19 * Under the context of Linux it generally refers to an interrupt handler.
20 * However, it correctly describes the 'HOST'
21 * DATA BLOCK:
22 * The amount of data needed to be transfered as predefined in the
23 * setup of the device.
24 * STORAGE ATOMIC:
25 * The 'DATA BLOCK' associated to the 'DATA HANDLER', and can be as
26 * small as a single sector or as large as the entire command block
27 * request.
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/kernel.h>
35 #include <linux/timer.h>
36 #include <linux/mm.h>
37 #include <linux/interrupt.h>
38 #include <linux/major.h>
39 #include <linux/errno.h>
40 #include <linux/genhd.h>
41 #include <linux/blkpg.h>
42 #include <linux/slab.h>
43 #include <linux/pci.h>
44 #include <linux/delay.h>
45 #include <linux/hdreg.h>
46 #include <linux/ide.h>
47 #include <linux/bitops.h>
49 #include <asm/byteorder.h>
50 #include <asm/irq.h>
51 #include <asm/uaccess.h>
52 #include <asm/io.h>
54 #define DEBUG_TASKFILE 0 /* unset when fixed */
56 static void ata_bswap_data (void *buffer, int wcount)
58 u16 *p = buffer;
60 while (wcount--) {
61 *p = *p << 8 | *p >> 8; p++;
62 *p = *p << 8 | *p >> 8; p++;
66 static void taskfile_input_data(ide_drive_t *drive, void *buffer, u32 wcount)
68 HWIF(drive)->ata_input_data(drive, buffer, wcount);
69 if (drive->bswap)
70 ata_bswap_data(buffer, wcount);
73 static void taskfile_output_data(ide_drive_t *drive, void *buffer, u32 wcount)
75 if (drive->bswap) {
76 ata_bswap_data(buffer, wcount);
77 HWIF(drive)->ata_output_data(drive, buffer, wcount);
78 ata_bswap_data(buffer, wcount);
79 } else {
80 HWIF(drive)->ata_output_data(drive, buffer, wcount);
84 int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
86 ide_task_t args;
87 memset(&args, 0, sizeof(ide_task_t));
88 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
89 if (drive->media == ide_disk)
90 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_IDENTIFY;
91 else
92 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_PIDENTIFY;
93 args.command_type = IDE_DRIVE_TASK_IN;
94 args.data_phase = TASKFILE_IN;
95 args.handler = &task_in_intr;
96 return ide_raw_taskfile(drive, &args, buf);
99 ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
101 ide_hwif_t *hwif = HWIF(drive);
102 task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
103 hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
104 u8 HIHI = (drive->addressing == 1) ? 0xE0 : 0xEF;
106 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
107 if (IDE_CONTROL_REG) {
108 /* clear nIEN */
109 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
111 SELECT_MASK(drive, 0);
113 if (drive->addressing == 1) {
114 hwif->OUTB(hobfile->feature, IDE_FEATURE_REG);
115 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
116 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
117 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
118 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
121 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
122 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
123 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
124 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
125 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
127 hwif->OUTB((taskfile->device_head & HIHI) | drive->select.all, IDE_SELECT_REG);
129 if (task->handler != NULL) {
130 if (task->prehandler != NULL) {
131 hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
132 ndelay(400); /* FIXME */
133 return task->prehandler(drive, task->rq);
135 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
136 return ide_started;
139 if (!drive->using_dma)
140 return ide_stopped;
142 switch (taskfile->command) {
143 case WIN_WRITEDMA_ONCE:
144 case WIN_WRITEDMA:
145 case WIN_WRITEDMA_EXT:
146 case WIN_READDMA_ONCE:
147 case WIN_READDMA:
148 case WIN_READDMA_EXT:
149 case WIN_IDENTIFY_DMA:
150 if (!hwif->dma_setup(drive)) {
151 hwif->dma_exec_cmd(drive, taskfile->command);
152 hwif->dma_start(drive);
153 return ide_started;
155 break;
156 default:
157 if (task->handler == NULL)
158 return ide_stopped;
161 return ide_stopped;
164 EXPORT_SYMBOL(do_rw_taskfile);
167 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
169 ide_startstop_t set_multmode_intr (ide_drive_t *drive)
171 ide_hwif_t *hwif = HWIF(drive);
172 u8 stat;
174 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
175 drive->mult_count = drive->mult_req;
176 } else {
177 drive->mult_req = drive->mult_count = 0;
178 drive->special.b.recalibrate = 1;
179 (void) ide_dump_status(drive, "set_multmode", stat);
181 return ide_stopped;
184 EXPORT_SYMBOL(set_multmode_intr);
187 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
189 ide_startstop_t set_geometry_intr (ide_drive_t *drive)
191 ide_hwif_t *hwif = HWIF(drive);
192 int retries = 5;
193 u8 stat;
195 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
196 udelay(10);
198 if (OK_STAT(stat, READY_STAT, BAD_STAT))
199 return ide_stopped;
201 if (stat & (ERR_STAT|DRQ_STAT))
202 return DRIVER(drive)->error(drive, "set_geometry_intr", stat);
204 if (HWGROUP(drive)->handler != NULL)
205 BUG();
206 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
207 return ide_started;
210 EXPORT_SYMBOL(set_geometry_intr);
213 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
215 ide_startstop_t recal_intr (ide_drive_t *drive)
217 ide_hwif_t *hwif = HWIF(drive);
218 u8 stat;
220 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG), READY_STAT, BAD_STAT))
221 return DRIVER(drive)->error(drive, "recal_intr", stat);
222 return ide_stopped;
225 EXPORT_SYMBOL(recal_intr);
228 * Handler for commands without a data phase
230 ide_startstop_t task_no_data_intr (ide_drive_t *drive)
232 ide_task_t *args = HWGROUP(drive)->rq->special;
233 ide_hwif_t *hwif = HWIF(drive);
234 u8 stat;
236 local_irq_enable();
237 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),READY_STAT,BAD_STAT)) {
238 return DRIVER(drive)->error(drive, "task_no_data_intr", stat);
239 /* calls ide_end_drive_cmd */
241 if (args)
242 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
244 return ide_stopped;
247 EXPORT_SYMBOL(task_no_data_intr);
249 static u8 wait_drive_not_busy(ide_drive_t *drive)
251 ide_hwif_t *hwif = HWIF(drive);
252 int retries = 100;
253 u8 stat;
256 * Last sector was transfered, wait until drive is ready.
257 * This can take up to 10 usec, but we will wait max 1 ms
258 * (drive_cmd_intr() waits that long).
260 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
261 udelay(10);
263 if (!retries)
264 printk(KERN_ERR "%s: drive still BUSY!\n", drive->name);
266 return stat;
269 static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
271 ide_hwif_t *hwif = drive->hwif;
272 struct scatterlist *sg = hwif->sg_table;
273 struct page *page;
274 #ifdef CONFIG_HIGHMEM
275 unsigned long flags;
276 #endif
277 unsigned int offset;
278 u8 *buf;
280 page = sg[hwif->cursg].page;
281 offset = sg[hwif->cursg].offset + hwif->cursg_ofs * SECTOR_SIZE;
283 /* get the current page and offset */
284 page = nth_page(page, (offset >> PAGE_SHIFT));
285 offset %= PAGE_SIZE;
287 #ifdef CONFIG_HIGHMEM
288 local_irq_save(flags);
289 #endif
290 buf = kmap_atomic(page, KM_BIO_SRC_IRQ) + offset;
292 hwif->nleft--;
293 hwif->cursg_ofs++;
295 if ((hwif->cursg_ofs * SECTOR_SIZE) == sg[hwif->cursg].length) {
296 hwif->cursg++;
297 hwif->cursg_ofs = 0;
300 /* do the actual data transfer */
301 if (write)
302 taskfile_output_data(drive, buf, SECTOR_WORDS);
303 else
304 taskfile_input_data(drive, buf, SECTOR_WORDS);
306 kunmap_atomic(buf, KM_BIO_SRC_IRQ);
307 #ifdef CONFIG_HIGHMEM
308 local_irq_restore(flags);
309 #endif
312 static void ide_pio_multi(ide_drive_t *drive, unsigned int write)
314 unsigned int nsect;
316 nsect = min_t(unsigned int, drive->hwif->nleft, drive->mult_count);
317 while (nsect--)
318 ide_pio_sector(drive, write);
321 static inline void ide_pio_datablock(ide_drive_t *drive, struct request *rq,
322 unsigned int write)
324 if (rq->bio) /* fs request */
325 rq->errors = 0;
327 switch (drive->hwif->data_phase) {
328 case TASKFILE_MULTI_IN:
329 case TASKFILE_MULTI_OUT:
330 ide_pio_multi(drive, write);
331 break;
332 default:
333 ide_pio_sector(drive, write);
334 break;
338 static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
339 const char *s, u8 stat)
341 if (rq->bio) {
342 ide_hwif_t *hwif = drive->hwif;
343 int sectors = hwif->nsect - hwif->nleft;
345 switch (hwif->data_phase) {
346 case TASKFILE_IN:
347 if (hwif->nleft)
348 break;
349 /* fall through */
350 case TASKFILE_OUT:
351 sectors--;
352 break;
353 case TASKFILE_MULTI_IN:
354 if (hwif->nleft)
355 break;
356 /* fall through */
357 case TASKFILE_MULTI_OUT:
358 sectors -= drive->mult_count;
359 default:
360 break;
363 if (sectors > 0)
364 drive->driver->end_request(drive, 1, sectors);
366 return drive->driver->error(drive, s, stat);
369 static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
371 if (rq->flags & REQ_DRIVE_TASKFILE) {
372 ide_task_t *task = rq->special;
374 if (task->tf_out_flags.all) {
375 u8 err = drive->hwif->INB(IDE_ERROR_REG);
376 ide_end_drive_cmd(drive, stat, err);
377 return;
380 drive->driver->end_request(drive, 1, rq->hard_nr_sectors);
384 * Handler for command with PIO data-in phase (Read/Read Multiple).
386 ide_startstop_t task_in_intr (ide_drive_t *drive)
388 ide_hwif_t *hwif = drive->hwif;
389 struct request *rq = HWGROUP(drive)->rq;
390 u8 stat = hwif->INB(IDE_STATUS_REG);
392 /* new way for dealing with premature shared PCI interrupts */
393 if (!OK_STAT(stat, DATA_READY, BAD_R_STAT)) {
394 if (stat & (ERR_STAT | DRQ_STAT))
395 return task_error(drive, rq, __FUNCTION__, stat);
396 /* No data yet, so wait for another IRQ. */
397 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
398 return ide_started;
401 ide_pio_datablock(drive, rq, 0);
403 /* If it was the last datablock check status and finish transfer. */
404 if (!hwif->nleft) {
405 stat = wait_drive_not_busy(drive);
406 if (!OK_STAT(stat, 0, BAD_R_STAT))
407 return task_error(drive, rq, __FUNCTION__, stat);
408 task_end_request(drive, rq, stat);
409 return ide_stopped;
412 /* Still data left to transfer. */
413 ide_set_handler(drive, &task_in_intr, WAIT_WORSTCASE, NULL);
415 return ide_started;
417 EXPORT_SYMBOL(task_in_intr);
420 * Handler for command with PIO data-out phase (Write/Write Multiple).
422 static ide_startstop_t task_out_intr (ide_drive_t *drive)
424 ide_hwif_t *hwif = drive->hwif;
425 struct request *rq = HWGROUP(drive)->rq;
426 u8 stat = hwif->INB(IDE_STATUS_REG);
428 if (!OK_STAT(stat, DRIVE_READY, drive->bad_wstat))
429 return task_error(drive, rq, __FUNCTION__, stat);
431 /* Deal with unexpected ATA data phase. */
432 if (((stat & DRQ_STAT) == 0) ^ !hwif->nleft)
433 return task_error(drive, rq, __FUNCTION__, stat);
435 if (!hwif->nleft) {
436 task_end_request(drive, rq, stat);
437 return ide_stopped;
440 /* Still data left to transfer. */
441 ide_pio_datablock(drive, rq, 1);
442 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
444 return ide_started;
447 ide_startstop_t pre_task_out_intr (ide_drive_t *drive, struct request *rq)
449 ide_startstop_t startstop;
451 if (ide_wait_stat(&startstop, drive, DATA_READY,
452 drive->bad_wstat, WAIT_DRQ)) {
453 printk(KERN_ERR "%s: no DRQ after issuing %sWRITE%s\n",
454 drive->name,
455 drive->hwif->data_phase ? "MULT" : "",
456 drive->addressing ? "_EXT" : "");
457 return startstop;
460 if (!drive->unmask)
461 local_irq_disable();
463 ide_set_handler(drive, &task_out_intr, WAIT_WORSTCASE, NULL);
464 ide_pio_datablock(drive, rq, 1);
466 return ide_started;
468 EXPORT_SYMBOL(pre_task_out_intr);
470 static int ide_diag_taskfile(ide_drive_t *drive, ide_task_t *args, unsigned long data_size, u8 *buf)
472 struct request rq;
474 memset(&rq, 0, sizeof(rq));
475 rq.flags = REQ_DRIVE_TASKFILE;
476 rq.buffer = buf;
479 * (ks) We transfer currently only whole sectors.
480 * This is suffient for now. But, it would be great,
481 * if we would find a solution to transfer any size.
482 * To support special commands like READ LONG.
484 if (args->command_type != IDE_DRIVE_TASK_NO_DATA) {
485 if (data_size == 0)
486 rq.nr_sectors = (args->hobRegister[IDE_NSECTOR_OFFSET] << 8) | args->tfRegister[IDE_NSECTOR_OFFSET];
487 else
488 rq.nr_sectors = data_size / SECTOR_SIZE;
490 if (!rq.nr_sectors) {
491 printk(KERN_ERR "%s: in/out command without data\n",
492 drive->name);
493 return -EFAULT;
496 rq.hard_nr_sectors = rq.nr_sectors;
497 rq.hard_cur_sectors = rq.current_nr_sectors = rq.nr_sectors;
499 if (args->command_type == IDE_DRIVE_TASK_RAW_WRITE)
500 rq.flags |= REQ_RW;
503 rq.special = args;
504 return ide_do_drive_cmd(drive, &rq, ide_wait);
507 int ide_raw_taskfile (ide_drive_t *drive, ide_task_t *args, u8 *buf)
509 return ide_diag_taskfile(drive, args, 0, buf);
512 EXPORT_SYMBOL(ide_raw_taskfile);
514 int ide_taskfile_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
516 ide_task_request_t *req_task;
517 ide_task_t args;
518 u8 *outbuf = NULL;
519 u8 *inbuf = NULL;
520 task_ioreg_t *argsptr = args.tfRegister;
521 task_ioreg_t *hobsptr = args.hobRegister;
522 int err = 0;
523 int tasksize = sizeof(struct ide_task_request_s);
524 int taskin = 0;
525 int taskout = 0;
526 u8 io_32bit = drive->io_32bit;
527 char __user *buf = (char __user *)arg;
529 // printk("IDE Taskfile ...\n");
531 req_task = kmalloc(tasksize, GFP_KERNEL);
532 if (req_task == NULL) return -ENOMEM;
533 memset(req_task, 0, tasksize);
534 if (copy_from_user(req_task, buf, tasksize)) {
535 kfree(req_task);
536 return -EFAULT;
539 taskout = (int) req_task->out_size;
540 taskin = (int) req_task->in_size;
542 if (taskout) {
543 int outtotal = tasksize;
544 outbuf = kmalloc(taskout, GFP_KERNEL);
545 if (outbuf == NULL) {
546 err = -ENOMEM;
547 goto abort;
549 memset(outbuf, 0, taskout);
550 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
551 err = -EFAULT;
552 goto abort;
556 if (taskin) {
557 int intotal = tasksize + taskout;
558 inbuf = kmalloc(taskin, GFP_KERNEL);
559 if (inbuf == NULL) {
560 err = -ENOMEM;
561 goto abort;
563 memset(inbuf, 0, taskin);
564 if (copy_from_user(inbuf, buf + intotal, taskin)) {
565 err = -EFAULT;
566 goto abort;
570 memset(&args, 0, sizeof(ide_task_t));
571 memcpy(argsptr, req_task->io_ports, HDIO_DRIVE_TASK_HDR_SIZE);
572 memcpy(hobsptr, req_task->hob_ports, HDIO_DRIVE_HOB_HDR_SIZE);
574 args.tf_in_flags = req_task->in_flags;
575 args.tf_out_flags = req_task->out_flags;
576 args.data_phase = req_task->data_phase;
577 args.command_type = req_task->req_cmd;
579 drive->io_32bit = 0;
580 switch(req_task->data_phase) {
581 case TASKFILE_OUT_DMAQ:
582 case TASKFILE_OUT_DMA:
583 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
584 break;
585 case TASKFILE_IN_DMAQ:
586 case TASKFILE_IN_DMA:
587 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
588 break;
589 case TASKFILE_MULTI_OUT:
590 if (!drive->mult_count) {
591 /* (hs): give up if multcount is not set */
592 printk(KERN_ERR "%s: %s Multimode Write " \
593 "multcount is not set\n",
594 drive->name, __FUNCTION__);
595 err = -EPERM;
596 goto abort;
598 /* fall through */
599 case TASKFILE_OUT:
600 args.prehandler = &pre_task_out_intr;
601 args.handler = &task_out_intr;
602 err = ide_diag_taskfile(drive, &args, taskout, outbuf);
603 break;
604 case TASKFILE_MULTI_IN:
605 if (!drive->mult_count) {
606 /* (hs): give up if multcount is not set */
607 printk(KERN_ERR "%s: %s Multimode Read failure " \
608 "multcount is not set\n",
609 drive->name, __FUNCTION__);
610 err = -EPERM;
611 goto abort;
613 /* fall through */
614 case TASKFILE_IN:
615 args.handler = &task_in_intr;
616 err = ide_diag_taskfile(drive, &args, taskin, inbuf);
617 break;
618 case TASKFILE_NO_DATA:
619 args.handler = &task_no_data_intr;
620 err = ide_diag_taskfile(drive, &args, 0, NULL);
621 break;
622 default:
623 err = -EFAULT;
624 goto abort;
627 memcpy(req_task->io_ports, &(args.tfRegister), HDIO_DRIVE_TASK_HDR_SIZE);
628 memcpy(req_task->hob_ports, &(args.hobRegister), HDIO_DRIVE_HOB_HDR_SIZE);
629 req_task->in_flags = args.tf_in_flags;
630 req_task->out_flags = args.tf_out_flags;
632 if (copy_to_user(buf, req_task, tasksize)) {
633 err = -EFAULT;
634 goto abort;
636 if (taskout) {
637 int outtotal = tasksize;
638 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
639 err = -EFAULT;
640 goto abort;
643 if (taskin) {
644 int intotal = tasksize + taskout;
645 if (copy_to_user(buf + intotal, inbuf, taskin)) {
646 err = -EFAULT;
647 goto abort;
650 abort:
651 kfree(req_task);
652 if (outbuf != NULL)
653 kfree(outbuf);
654 if (inbuf != NULL)
655 kfree(inbuf);
657 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);
659 drive->io_32bit = io_32bit;
661 return err;
664 int ide_wait_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, u8 feature, u8 sectors, u8 *buf)
666 struct request rq;
667 u8 buffer[4];
669 if (!buf)
670 buf = buffer;
671 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
672 ide_init_drive_cmd(&rq);
673 rq.buffer = buf;
674 *buf++ = cmd;
675 *buf++ = nsect;
676 *buf++ = feature;
677 *buf++ = sectors;
678 return ide_do_drive_cmd(drive, &rq, ide_wait);
682 * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
684 int ide_cmd_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
686 int err = 0;
687 u8 args[4], *argbuf = args;
688 u8 xfer_rate = 0;
689 int argsize = 4;
690 ide_task_t tfargs;
692 if (NULL == (void *) arg) {
693 struct request rq;
694 ide_init_drive_cmd(&rq);
695 return ide_do_drive_cmd(drive, &rq, ide_wait);
698 if (copy_from_user(args, (void __user *)arg, 4))
699 return -EFAULT;
701 memset(&tfargs, 0, sizeof(ide_task_t));
702 tfargs.tfRegister[IDE_FEATURE_OFFSET] = args[2];
703 tfargs.tfRegister[IDE_NSECTOR_OFFSET] = args[3];
704 tfargs.tfRegister[IDE_SECTOR_OFFSET] = args[1];
705 tfargs.tfRegister[IDE_LCYL_OFFSET] = 0x00;
706 tfargs.tfRegister[IDE_HCYL_OFFSET] = 0x00;
707 tfargs.tfRegister[IDE_SELECT_OFFSET] = 0x00;
708 tfargs.tfRegister[IDE_COMMAND_OFFSET] = args[0];
710 if (args[3]) {
711 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
712 argbuf = kmalloc(argsize, GFP_KERNEL);
713 if (argbuf == NULL)
714 return -ENOMEM;
715 memcpy(argbuf, args, 4);
717 if (set_transfer(drive, &tfargs)) {
718 xfer_rate = args[1];
719 if (ide_ata66_check(drive, &tfargs))
720 goto abort;
723 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
725 if (!err && xfer_rate) {
726 /* active-retuning-calls future */
727 ide_set_xfer_rate(drive, xfer_rate);
728 ide_driveid_update(drive);
730 abort:
731 if (copy_to_user((void __user *)arg, argbuf, argsize))
732 err = -EFAULT;
733 if (argsize > 4)
734 kfree(argbuf);
735 return err;
738 static int ide_wait_cmd_task(ide_drive_t *drive, u8 *buf)
740 struct request rq;
742 ide_init_drive_cmd(&rq);
743 rq.flags = REQ_DRIVE_TASK;
744 rq.buffer = buf;
745 return ide_do_drive_cmd(drive, &rq, ide_wait);
749 * FIXME : this needs to map into at taskfile. <andre@linux-ide.org>
751 int ide_task_ioctl (ide_drive_t *drive, unsigned int cmd, unsigned long arg)
753 void __user *p = (void __user *)arg;
754 int err = 0;
755 u8 args[7], *argbuf = args;
756 int argsize = 7;
758 if (copy_from_user(args, p, 7))
759 return -EFAULT;
760 err = ide_wait_cmd_task(drive, argbuf);
761 if (copy_to_user(p, argbuf, argsize))
762 err = -EFAULT;
763 return err;
767 * NOTICE: This is additions from IBM to provide a discrete interface,
768 * for selective taskregister access operations. Nice JOB Klaus!!!
769 * Glad to be able to work and co-develop this with you and IBM.
771 ide_startstop_t flagged_taskfile (ide_drive_t *drive, ide_task_t *task)
773 ide_hwif_t *hwif = HWIF(drive);
774 task_struct_t *taskfile = (task_struct_t *) task->tfRegister;
775 hob_struct_t *hobfile = (hob_struct_t *) task->hobRegister;
776 #if DEBUG_TASKFILE
777 u8 status;
778 #endif
780 if (task->data_phase == TASKFILE_MULTI_IN ||
781 task->data_phase == TASKFILE_MULTI_OUT) {
782 if (!drive->mult_count) {
783 printk(KERN_ERR "%s: multimode not set!\n", drive->name);
784 return ide_stopped;
789 * (ks) Check taskfile in/out flags.
790 * If set, then execute as it is defined.
791 * If not set, then define default settings.
792 * The default values are:
793 * write and read all taskfile registers (except data)
794 * write and read the hob registers (sector,nsector,lcyl,hcyl)
796 if (task->tf_out_flags.all == 0) {
797 task->tf_out_flags.all = IDE_TASKFILE_STD_OUT_FLAGS;
798 if (drive->addressing == 1)
799 task->tf_out_flags.all |= (IDE_HOB_STD_OUT_FLAGS << 8);
802 if (task->tf_in_flags.all == 0) {
803 task->tf_in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
804 if (drive->addressing == 1)
805 task->tf_in_flags.all |= (IDE_HOB_STD_IN_FLAGS << 8);
808 /* ALL Command Block Executions SHALL clear nIEN, unless otherwise */
809 if (IDE_CONTROL_REG)
810 /* clear nIEN */
811 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
812 SELECT_MASK(drive, 0);
814 #if DEBUG_TASKFILE
815 status = hwif->INB(IDE_STATUS_REG);
816 if (status & 0x80) {
817 printk("flagged_taskfile -> Bad status. Status = %02x. wait 100 usec ...\n", status);
818 udelay(100);
819 status = hwif->INB(IDE_STATUS_REG);
820 printk("flagged_taskfile -> Status = %02x\n", status);
822 #endif
824 if (task->tf_out_flags.b.data) {
825 u16 data = taskfile->data + (hobfile->data << 8);
826 hwif->OUTW(data, IDE_DATA_REG);
829 /* (ks) send hob registers first */
830 if (task->tf_out_flags.b.nsector_hob)
831 hwif->OUTB(hobfile->sector_count, IDE_NSECTOR_REG);
832 if (task->tf_out_flags.b.sector_hob)
833 hwif->OUTB(hobfile->sector_number, IDE_SECTOR_REG);
834 if (task->tf_out_flags.b.lcyl_hob)
835 hwif->OUTB(hobfile->low_cylinder, IDE_LCYL_REG);
836 if (task->tf_out_flags.b.hcyl_hob)
837 hwif->OUTB(hobfile->high_cylinder, IDE_HCYL_REG);
839 /* (ks) Send now the standard registers */
840 if (task->tf_out_flags.b.error_feature)
841 hwif->OUTB(taskfile->feature, IDE_FEATURE_REG);
842 /* refers to number of sectors to transfer */
843 if (task->tf_out_flags.b.nsector)
844 hwif->OUTB(taskfile->sector_count, IDE_NSECTOR_REG);
845 /* refers to sector offset or start sector */
846 if (task->tf_out_flags.b.sector)
847 hwif->OUTB(taskfile->sector_number, IDE_SECTOR_REG);
848 if (task->tf_out_flags.b.lcyl)
849 hwif->OUTB(taskfile->low_cylinder, IDE_LCYL_REG);
850 if (task->tf_out_flags.b.hcyl)
851 hwif->OUTB(taskfile->high_cylinder, IDE_HCYL_REG);
854 * (ks) In the flagged taskfile approch, we will used all specified
855 * registers and the register value will not be changed. Except the
856 * select bit (master/slave) in the drive_head register. We must make
857 * sure that the desired drive is selected.
859 hwif->OUTB(taskfile->device_head | drive->select.all, IDE_SELECT_REG);
860 switch(task->data_phase) {
862 case TASKFILE_OUT_DMAQ:
863 case TASKFILE_OUT_DMA:
864 case TASKFILE_IN_DMAQ:
865 case TASKFILE_IN_DMA:
866 hwif->dma_setup(drive);
867 hwif->dma_exec_cmd(drive, taskfile->command);
868 hwif->dma_start(drive);
869 break;
871 default:
872 if (task->handler == NULL)
873 return ide_stopped;
875 /* Issue the command */
876 if (task->prehandler) {
877 hwif->OUTBSYNC(drive, taskfile->command, IDE_COMMAND_REG);
878 ndelay(400); /* FIXME */
879 return task->prehandler(drive, task->rq);
881 ide_execute_command(drive, taskfile->command, task->handler, WAIT_WORSTCASE, NULL);
884 return ide_started;