3 * Copyright (C) 2002 MandrakeSoft S.A.
8 * http://www.linux-mandrake.com/
9 * http://www.mandrakesoft.com/
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Major modifications made for the V3VEE project
27 * The V3VEE Project is a joint project between Northwestern University
28 * and the University of New Mexico. You can find out more at
29 * http://www.v3vee.org
31 * Copyright (c) 2008, Zheng Cui <cuizheng@cs.unm.edu>
32 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
33 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
34 * All rights reserved for original changes
39 #include <devices/ramdisk.h>
40 #include <palacios/vmm.h>
41 #include <devices/cdrom.h>
42 #include <devices/ide.h>
47 #define PrintDebug(_f, _a...)
55 * Data type definitions
58 #define INDEX_PULSE_CYCLE 10
63 #define INTR_REASON_BIT_ERR 0x01
64 #define UNABLE_FIND_TAT_CHANNEL_ERR 0x02
66 #define READ_BUF_GT_512 0x04
70 #define PRI_DATA_PORT 0x1f0
71 #define PRI_FEATURES_PORT 0x1f1
72 #define PRI_SECT_CNT_PORT 0x1f2
73 #define PRI_SECT_ADDR1_PORT 0x1f3
74 #define PRI_SECT_ADDR2_PORT 0x1f4
75 #define PRI_SECT_ADDR3_PORT 0x1f5
76 #define PRI_DRV_SEL_PORT 0x1f6
77 #define PRI_CMD_PORT 0x1f7
78 #define PRI_CTRL_PORT 0x3f6
79 #define PRI_ADDR_REG_PORT 0x3f7
81 #define SEC_DATA_PORT 0x170
82 #define SEC_FEATURES_PORT 0x171
83 #define SEC_SECT_CNT_PORT 0x172
84 #define SEC_SECT_ADDR1_PORT 0x173
85 #define SEC_SECT_ADDR2_PORT 0x174
86 #define SEC_SECT_ADDR3_PORT 0x175
87 #define SEC_DRV_SEL_PORT 0x176
88 #define SEC_CMD_PORT 0x177
89 #define SEC_CTRL_PORT 0x376
90 #define SEC_ADDR_REG_PORT 0x377
93 #define PACKET_SIZE 12
97 static const char cdrom_str[] = "CD-ROM";
98 static const char harddisk_str[] = "HARDDISK";
99 static const char none_str[] = "NONE";
102 static inline const char * device_type_to_str(device_type_t type) {
116 static inline void write_features(struct channel_t * channel, uchar_t value) {
117 channel->drives[0].controller.features = value;
118 channel->drives[1].controller.features = value;
122 static inline void write_sector_count(struct channel_t * channel, uchar_t value) {
123 channel->drives[0].controller.sector_count = value;
124 channel->drives[1].controller.sector_count = value;
127 static inline void write_sector_number(struct channel_t * channel, uchar_t value) {
128 channel->drives[0].controller.sector_no = value;
129 channel->drives[1].controller.sector_no = value;
133 static inline void write_cylinder_low(struct channel_t * channel, uchar_t value) {
134 channel->drives[0].controller.cylinder_no &= 0xff00;
135 channel->drives[0].controller.cylinder_no |= value;
136 channel->drives[1].controller.cylinder_no &= 0xff00;
137 channel->drives[1].controller.cylinder_no |= value;
140 static inline void write_cylinder_high(struct channel_t * channel, uchar_t value) {
141 ushort_t val2 = value;
143 channel->drives[0].controller.cylinder_no &= 0x00ff;
144 channel->drives[0].controller.cylinder_no |= (val2 & 0xff00);
146 channel->drives[1].controller.cylinder_no &= 0x00ff;
147 channel->drives[1].controller.cylinder_no |= (val2 & 0xff00);
150 static inline void write_head_no(struct channel_t * channel, uchar_t value) {
151 channel->drives[0].controller.head_no = value;
152 channel->drives[1].controller.head_no = value;
155 static inline void write_lba_mode(struct channel_t * channel, uchar_t value) {
156 channel->drives[0].controller.lba_mode = value;
157 channel->drives[1].controller.lba_mode = value;
161 static inline uint_t get_channel_no(struct ramdisk_t * ramdisk, struct channel_t * channel) {
162 return (((uchar_t *)channel - (uchar_t *)(ramdisk->channels)) / sizeof(struct channel_t));
165 static inline uint_t get_drive_no(struct channel_t * channel, struct drive_t * drive) {
166 return (((uchar_t *)drive - (uchar_t*)(channel->drives)) / sizeof(struct drive_t));
169 static inline struct drive_t * get_selected_drive(struct channel_t * channel) {
170 return &(channel->drives[channel->drive_select]);
174 static inline int is_primary_port(struct ramdisk_t * ramdisk, ushort_t port) {
178 case PRI_FEATURES_PORT:
179 case PRI_SECT_CNT_PORT:
180 case PRI_SECT_ADDR1_PORT:
181 case PRI_SECT_ADDR2_PORT:
182 case PRI_SECT_ADDR3_PORT:
183 case PRI_DRV_SEL_PORT:
194 static inline int is_secondary_port(struct ramdisk_t * ramdisk, ushort_t port) {
198 case SEC_FEATURES_PORT:
199 case SEC_SECT_CNT_PORT:
200 case SEC_SECT_ADDR1_PORT:
201 case SEC_SECT_ADDR2_PORT:
202 case SEC_SECT_ADDR3_PORT:
203 case SEC_DRV_SEL_PORT:
212 static inline int num_drives_on_channel(struct channel_t * channel) {
213 if ((channel->drives[0].device_type == IDE_NONE) &&
214 (channel->drives[1].device_type == IDE_NONE)) {
216 } else if ((channel->drives[0].device_type != IDE_NONE) &&
217 (channel->drives[1].device_type != IDE_NONE)) {
226 static inline uchar_t extract_bits(uchar_t * buf, uint_t buf_offset, uint_t bit_offset, uint_t num_bits) {
227 uchar_t val = buf[buf_offset];
228 val = val >> bit_offset;
229 val &= ((1 << num_bits) -1);
234 static inline uchar_t get_packet_field(struct channel_t * channel, uint_t packet_offset, uint_t bit_offset, uint_t num_bits) {
235 struct drive_t * drive = get_selected_drive(channel);
236 return extract_bits(drive->controller.buffer, packet_offset, bit_offset, num_bits);
240 static inline uchar_t get_packet_byte(struct channel_t * channel, uint_t offset) {
241 struct drive_t * drive = get_selected_drive(channel);
242 return drive->controller.buffer[offset];
245 static inline uint16_t get_packet_word(struct channel_t * channel, uint_t offset) {
246 struct drive_t * drive = get_selected_drive(channel);
247 uint16_t val = drive->controller.buffer[offset];
249 val |= drive->controller.buffer[offset + 1];
254 static inline uint16_t rd_read_16bit(const uint8_t* buf) {
255 return (buf[0] << 8) | buf[1];
260 static inline uint32_t rd_read_32bit(const uint8_t* buf) {
261 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
264 ////////////////////////////////////////////////////////////////////////////
272 static void rd_init_mode_sense_single(struct vm_device * dev, struct channel_t * channel, const void * src, int size);
274 static void rd_command_aborted(struct vm_device * dev, struct channel_t * channel, unsigned value);
279 static int handle_atapi_packet_command(struct vm_device * dev,
280 struct channel_t * channel,
283 static int rd_init_send_atapi_command(struct vm_device * dev,
284 struct channel_t * channel,
285 Bit8u command, int req_length,
286 int alloc_length, bool lazy);
288 static void rd_ready_to_send_atapi(struct vm_device * dev,
289 struct channel_t * channel);
291 static void rd_atapi_cmd_error(struct vm_device * dev,
292 struct channel_t * channel,
293 sense_t sense_key, asc_t asc);
295 static void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel);
296 static void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel);
303 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel);
304 static void rd_lower_irq(struct vm_device *dev, Bit32u irq);
315 static void rd_print_state(struct ramdisk_t *ramdisk);
316 static int check_bit_fields(struct controller_t * controller);
318 ////////////////////////////////////////////////////////////////////
324 int v3_ramdisk_register_cdrom(struct vm_device * dev, uint_t busID, uint_t driveID, struct cdrom_ops* cd, void * private_data) {
325 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
326 struct channel_t * channel = &(ramdisk->channels[busID]);
327 struct drive_t * drive = &(channel->drives[driveID]);
328 struct controller_t * controller = &(drive->controller);
332 if (drive->device_type != IDE_NONE) {
333 PrintError("Device already registered at this location\n");
341 strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
343 while (strlen((char *)(drive->model_no)) < 40) {
344 strcat ((char*)(drive->model_no), " ");
347 PrintDebug("CDROM on target %d/%d\n", busID, driveID);
349 drive->device_type = IDE_CDROM;
350 drive->cdrom.locked = 0;
351 drive->sense.sense_key = SENSE_NONE;
352 drive->sense.asc = 0;
353 drive->sense.ascq = 0;
355 drive->private_data = private_data;
358 if (check_bit_fields(controller) == INTR_REASON_BIT_ERR) {
359 PrintDebug("interrupt reason: bit field error\n");
360 return INTR_REASON_BIT_ERR;
364 controller->sector_count = 0;
366 drive->cdrom.cd = cd;
368 PrintDebug("\t\tCD on ata%d-%d: '%s'\n",
372 if(drive->cdrom.cd->insert_cdrom(drive->private_data)) {
373 PrintDebug("\t\tMedia present in CD-ROM drive\n");
374 drive->cdrom.ready = 1;
375 drive->cdrom.capacity = drive->cdrom.cd->capacity(drive->private_data);
376 PrintDebug("\t\tCDROM capacity is %d\n", drive->cdrom.capacity);
378 PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
379 drive->cdrom.ready = 0;
386 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
389 struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
391 PrintDebug("[rd_init_harddrive]\n");
393 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
394 memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
397 for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
398 struct channel_t * channel = &(channels[channel_num]);
400 channel->ioaddr1 = 0x0;
401 channel->ioaddr2 = 0x0;
404 for (device = 0; device < 2; device++){
405 struct drive_t * drive = &(channel->drives[device]);
406 struct controller_t * controller = &(drive->controller);
408 controller->status.busy = 0;
409 controller->status.drive_ready = 1;
410 controller->status.write_fault = 0;
411 controller->status.seek_complete = 1;
412 controller->status.drq = 0;
413 controller->status.corrected_data = 0;
414 controller->status.index_pulse = 0;
415 controller->status.index_pulse_count = 0;
416 controller->status.err = 0;
418 controller->error_register = 0x01; // diagnostic code: no error
419 controller->head_no = 0;
420 controller->sector_count = 1;
421 controller->sector_no = 1;
422 controller->cylinder_no = 0;
423 controller->current_command = 0x00;
424 controller->buffer_index = 0;
426 controller->control.reset = 0;
427 controller->control.disable_irq = 0;
428 controller->reset_in_progress = 0;
430 controller->sectors_per_block = 0x80;
431 controller->lba_mode = 0;
434 controller->features = 0;
437 drive->device_type = IDE_NONE;
440 strncpy((char*)(drive->model_no), "", 40);
441 while(strlen((char *)(drive->model_no)) < 40) {
442 strcat ((char*)(drive->model_no), " ");
449 rd_print_state(ramdisk);
456 static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
461 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
466 ////////////////////////////////////////////////////////////////////
470 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
471 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
472 struct channel_t * channel = NULL;
473 struct drive_t * drive = NULL;
474 struct controller_t * controller = NULL;
478 if (is_primary_port(ramdisk, port)) {
479 channel = &(ramdisk->channels[0]);
480 } else if (is_secondary_port(ramdisk, port)) {
481 channel = &(ramdisk->channels[1]);
483 PrintError("Invalid Port: %d\n", port);
487 drive = get_selected_drive(channel);
488 controller = &(drive->controller);
491 PrintDebug("[read_data_handler] IO Read at 0x%x, on drive %d/%d (current_cmd = 0x%02x)\n",
493 get_channel_no(ramdisk, channel),
494 get_drive_no(channel, drive),
495 controller->current_command);
497 switch (controller->current_command) {
498 case 0xec: // IDENTIFY DEVICE
503 controller->status.busy = 0;
504 controller->status.drive_ready = 1;
505 controller->status.write_fault = 0;
506 controller->status.seek_complete = 1;
507 controller->status.corrected_data = 0;
508 controller->status.err = 0;
511 value32 = controller->buffer[index];
515 value32 |= (controller->buffer[index] << 8);
520 value32 |= (controller->buffer[index] << 16);
521 value32 |= (controller->buffer[index+1] << 24);
525 controller->buffer_index = index;
528 memcpy(dst, controller->buffer + controller->buffer_index, length);
529 controller->buffer_index += length;
531 if (controller->buffer_index >= 512) {
532 controller->status.drq = 0;
537 case 0xa0: //send packet cmd
539 uint_t index = controller->buffer_index;
542 PrintDebug("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n",
543 drive->atapi.command,
545 drive->cdrom.remaining_blocks);
547 // Load block if necessary
551 PrintError("\t\tindex > 2048 : 0x%x\n", index);
555 switch (drive->atapi.command) {
556 case 0x28: // read (10)
557 case 0xa8: // read (12)
560 if (!(drive->cdrom.ready)) {
561 PrintError("\t\tRead with CDROM not ready\n");
565 drive->cdrom.cd->read_block(drive->private_data, controller->buffer,
566 drive->cdrom.next_lba);
567 drive->cdrom.next_lba++;
568 drive->cdrom.remaining_blocks--;
571 if (!(drive->cdrom.remaining_blocks)) {
572 PrintDebug("\t\tLast READ block loaded {CDROM}\n");
574 PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
575 drive->cdrom.remaining_blocks);
578 // one block transfered, start at beginning
582 default: // no need to load a new block
590 value32 = controller->buffer[index + increment];
594 value32 |= (controller->buffer[index + increment] << 8);
599 value32 |= (controller->buffer[index + increment] << 16);
600 value32 |= (controller->buffer[index + increment + 1] << 24);
604 controller->buffer_index = index + increment;
605 controller->drq_index += increment;
608 /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
610 memcpy(dst, controller->buffer + index, length);
612 controller->buffer_index = index + length;
613 controller->drq_index += length;
618 if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
619 controller->status.drq = 0;
620 controller->drq_index = 0;
622 drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
624 if (drive->atapi.total_bytes_remaining > 0) {
625 // one or more blocks remaining (works only for single block commands)
627 PrintDebug("\t\tPACKET drq bytes read\n");
628 controller->interrupt_reason.i_o = 1;
629 controller->status.busy = 0;
630 controller->status.drq = 1;
631 controller->interrupt_reason.c_d = 0;
633 // set new byte count if last block
634 if (drive->atapi.total_bytes_remaining < controller->byte_count) {
635 controller->byte_count = drive->atapi.total_bytes_remaining;
637 drive->atapi.drq_bytes = controller->byte_count;
639 rd_raise_interrupt(dev, channel);
642 PrintDebug("\t\tPACKET all bytes read\n");
644 controller->interrupt_reason.i_o = 1;
645 controller->interrupt_reason.c_d = 1;
646 controller->status.drive_ready = 1;
647 controller->interrupt_reason.rel = 0;
648 controller->status.busy = 0;
649 controller->status.drq = 0;
650 controller->status.err = 0;
652 rd_raise_interrupt(dev, channel);
660 PrintDebug("\t\tread need support more command: %02x\n", controller->current_command);
670 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
671 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
672 struct channel_t * channel = NULL;
673 struct drive_t * drive = NULL;
674 struct controller_t * controller = NULL;
676 if (is_primary_port(ramdisk, port)) {
677 channel = &(ramdisk->channels[0]);
678 } else if (is_secondary_port(ramdisk, port)) {
679 channel = &(ramdisk->channels[1]);
681 PrintError("Invalid Port: %d\n", port);
685 drive = get_selected_drive(channel);
686 controller = &(drive->controller);
689 PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n",
690 port, controller->current_command);
694 //PrintDebug("[write_data_handler]\n");
695 switch (controller->current_command) {
696 case 0x30: // WRITE SECTORS
697 PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
702 if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
709 PrintError("\t\tIO write(0x%x): current command is %02xh\n",
710 port, controller->current_command);
725 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
726 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
727 struct channel_t * channel = NULL;
728 struct drive_t * drive = NULL;
729 struct controller_t * controller = NULL;
734 if (is_primary_port(ramdisk, port)) {
735 channel = &(ramdisk->channels[0]);
736 } else if (is_secondary_port(ramdisk, port)) {
737 channel = &(ramdisk->channels[1]);
739 PrintError("Invalid Port: %d\n", port);
743 drive = get_selected_drive(channel);
744 controller = &(drive->controller);
747 PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n",
748 port, get_channel_no(ramdisk, channel),
749 channel->drive_select);
752 if (num_drives_on_channel(channel) == 0) {
753 // (mch) Just return zero for these registers
754 memset(dst, 0, length);
758 (controller->status.busy << 7) |
759 (controller->status.drive_ready << 6) |
760 (controller->status.write_fault << 5) |
761 (controller->status.seek_complete << 4) |
762 (controller->status.drq << 3) |
763 (controller->status.corrected_data << 2) |
764 (controller->status.index_pulse << 1) |
765 (controller->status.err) );
768 memcpy(dst, &val, length);
770 controller->status.index_pulse_count++;
771 controller->status.index_pulse = 0;
773 if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
774 controller->status.index_pulse = 1;
775 controller->status.index_pulse_count = 0;
779 if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
780 rd_lower_irq(dev, channel->irq);
783 PrintDebug("\t\tRead STATUS = 0x%x\n", *(uchar_t *)dst);
790 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
791 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
792 struct channel_t * channel = NULL;
793 struct drive_t * drive = NULL;
794 struct controller_t * controller = NULL;
795 uchar_t value = *(uchar_t *)src;
798 PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
802 if (is_primary_port(ramdisk, port)) {
803 channel = &(ramdisk->channels[0]);
804 } else if (is_secondary_port(ramdisk, port)) {
805 channel = &(ramdisk->channels[1]);
807 PrintError("Invalid Port: %d\n", port);
811 drive = get_selected_drive(channel);
812 controller = &(drive->controller);
815 PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
816 port, get_channel_no(ramdisk, channel),
817 channel->drive_select,
822 case 0xa1: // IDENTIFY PACKET DEVICE
824 if (drive->device_type == IDE_CDROM) {
825 controller->current_command = value;
826 controller->error_register = 0;
828 controller->status.busy = 0;
829 controller->status.drive_ready = 1;
830 controller->status.write_fault = 0;
831 controller->status.drq = 1;
832 controller->status.err = 0;
834 controller->status.seek_complete = 1;
835 controller->status.corrected_data = 0;
837 controller->buffer_index = 0;
838 rd_raise_interrupt(dev, channel);
839 rd_identify_ATAPI_drive(dev, channel);
841 rd_command_aborted(dev, channel, 0xa1);
845 case 0xa0: // SEND PACKET (atapi)
847 if (drive->device_type == IDE_CDROM) {
850 if (controller->features & (1 << 0)) {
851 PrintError("\t\tPACKET-DMA not supported");
855 if (controller->features & (1 << 1)) {
856 PrintError("\t\tPACKET-overlapped not supported");
860 // We're already ready!
861 controller->sector_count = 1;
862 controller->status.busy = 0;
863 controller->status.write_fault = 0;
866 controller->status.drq = 1;
867 controller->status.err = 0;
869 // NOTE: no interrupt here
870 controller->current_command = value;
871 controller->buffer_index = 0;
873 rd_command_aborted (dev, channel, 0xa0);
878 PrintError("\t\tneed translate command %2x\n", value);
880 /* JRL THIS NEEDS TO CHANGE */
888 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
889 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
890 struct channel_t * channel = NULL;
891 struct drive_t * master_drive = NULL;
892 struct drive_t * slave_drive = NULL;
893 struct controller_t * controller = NULL;
894 uchar_t value = *(uchar_t *)src;
895 rd_bool prev_control_reset;
898 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
902 if (is_primary_port(ramdisk, port)) {
903 channel = &(ramdisk->channels[0]);
904 } else if (is_secondary_port(ramdisk, port)) {
905 channel = &(ramdisk->channels[1]);
907 PrintError("Invalid Port: %d\n", port);
911 master_drive = &(channel->drives[0]);
912 slave_drive = &(channel->drives[1]);
914 controller = &(get_selected_drive(channel)->controller);
917 PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n",
918 port, get_channel_no(ramdisk, channel),
919 channel->drive_select,
922 // (mch) Even if device 1 was selected, a write to this register
923 // goes to device 0 (if device 1 is absent)
925 prev_control_reset = controller->control.reset;
929 PrintDebug("RESET Signaled\n");
932 master_drive->controller.control.reset = value & 0x04;
933 slave_drive->controller.control.reset = value & 0x04;
935 // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq = value & 0x02;
936 master_drive->controller.control.disable_irq = value & 0x02;
937 slave_drive->controller.control.disable_irq = value & 0x02;
939 PrintDebug("\t\tadpater control reg: reset controller = %d\n",
940 (unsigned) (controller->control.reset) ? 1 : 0);
941 PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
942 (unsigned) (controller->control.disable_irq) ? 1 : 0);
944 if ((!prev_control_reset) && (controller->control.reset)) {
947 // transition from 0 to 1 causes all drives to reset
948 PrintDebug("\t\thard drive: RESET\n");
950 // (mch) Set BSY, drive not ready
951 for (id = 0; id < 2; id++) {
952 struct controller_t * ctrl = NULL;
955 ctrl = &(master_drive->controller);
956 } else if (id == 1) {
957 ctrl = &(slave_drive->controller);
960 ctrl->status.busy = 1;
961 ctrl->status.drive_ready = 0;
962 ctrl->reset_in_progress = 1;
964 ctrl->status.write_fault = 0;
965 ctrl->status.seek_complete = 1;
966 ctrl->status.drq = 0;
967 ctrl->status.corrected_data = 0;
968 ctrl->status.err = 0;
970 ctrl->error_register = 0x01; // diagnostic code: no error
972 ctrl->current_command = 0x00;
973 ctrl->buffer_index = 0;
975 ctrl->sectors_per_block = 0x80;
978 ctrl->control.disable_irq = 0;
981 rd_lower_irq(dev, channel->irq);
983 } else if ((controller->reset_in_progress) &&
984 (!controller->control.reset)) {
986 // Clear BSY and DRDY
987 PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
989 for (id = 0; id < 2; id++) {
990 struct controller_t * ctrl = NULL;
991 struct drive_t * drv = NULL;
994 ctrl = &(master_drive->controller);
996 } else if (id == 1) {
997 ctrl = &(slave_drive->controller);
1001 ctrl->status.busy = 0;
1002 ctrl->status.drive_ready = 1;
1003 ctrl->reset_in_progress = 0;
1006 if (drv->device_type == IDE_DISK) {
1007 PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1009 ctrl->sector_count = 1;
1010 ctrl->sector_no = 1;
1011 ctrl->cylinder_no = 0;
1014 ctrl->sector_count = 1;
1015 ctrl->sector_no = 1;
1016 ctrl->cylinder_no = 0xeb14;
1021 PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n",
1022 master_drive->controller.control.disable_irq);
1023 PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n",
1024 slave_drive->controller.control.disable_irq);
1029 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1030 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1031 struct channel_t * channel = NULL;
1032 struct drive_t * drive = NULL;
1033 struct controller_t * controller = NULL;
1037 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1041 if (is_primary_port(ramdisk, port)) {
1042 channel = &(ramdisk->channels[0]);
1043 } else if (is_secondary_port(ramdisk, port)) {
1044 channel = &(ramdisk->channels[1]);
1046 PrintError("Invalid Port: %d\n", port);
1050 drive = get_selected_drive(channel);
1051 controller = &(drive->controller);
1054 PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n",
1055 port, get_channel_no(ramdisk, channel),
1056 channel->drive_select,
1057 controller->current_command);
1061 case PRI_FEATURES_PORT:
1062 case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1064 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1066 controller->status.err = 0;
1068 PrintDebug("\t\tRead FEATURES = 0x%x\n", val);
1070 *(uchar_t *)dst = val;
1076 case PRI_SECT_CNT_PORT:
1077 case SEC_SECT_CNT_PORT: // hard disk sector count / interrupt reason 0x1f2
1079 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1080 PrintDebug("\t\tRead SECTOR COUNT = 0x%x\n", val);
1081 *(uchar_t *)dst = val;
1086 case PRI_SECT_ADDR1_PORT:
1087 case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1089 uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1091 PrintDebug("\t\tRead SECTOR ADDR1 = 0x%x\n", val);
1093 *(uchar_t *)dst = val;
1099 case PRI_SECT_ADDR2_PORT:
1100 case SEC_SECT_ADDR2_PORT: // cylinder low 0x1f4
1102 // -- WARNING : On real hardware the controller registers are shared between drives.
1103 // So we must respond even if the select device is not present. Some OS uses this fact
1104 // to detect the disks.... minix2 for example
1105 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no & 0x00ff);
1107 PrintDebug("\t\tRead SECTOR ADDR2 = 0x%x\n", val);
1109 *(uchar_t *)dst = val;
1115 case PRI_SECT_ADDR3_PORT:
1116 case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1118 // -- WARNING : On real hardware the controller registers are shared between drives.
1119 // So we must respond even if the select device is not present. Some OS uses this fact
1120 // to detect the disks.... minix2 for example
1121 uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1123 PrintDebug("\t\tRead SECTOR ADDR3 = 0x%x\n", val);
1125 *(uchar_t *)dst = val;
1130 case PRI_DRV_SEL_PORT:
1131 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1133 // b7 Extended data field for ECC
1134 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1135 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1136 // b6 1=LBA mode, 0=CHS mode
1140 uchar_t val = ((1 << 7) |
1141 ((controller->lba_mode > 0) << 6) |
1142 (1 << 5) | // 01b = 512 sector size
1143 (channel->drive_select << 4) |
1144 (controller->head_no << 0));
1146 PrintDebug("\t\tRead DRIVE SELECT = 0x%x\n", val);
1147 *(uchar_t *)dst = val;
1152 case PRI_ADDR_REG_PORT:
1153 case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1155 // Obsolete and unsupported register. Not driven by hard
1156 // disk controller. Report all 1's. If floppy controller
1157 // is handling this address, it will call this function
1158 // set/clear D7 (the only bit it handles), then return
1159 // the combined value
1160 *(uchar_t *)dst = 0xff;
1165 PrintError("Invalid Port: %d\n", port);
1173 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1174 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1175 struct channel_t * channel = NULL;
1176 struct drive_t * drive = NULL;
1177 struct controller_t * controller = NULL;
1178 uchar_t value = *(uchar_t *)src;
1181 PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1185 if (is_primary_port(ramdisk, port)) {
1186 channel = &(ramdisk->channels[0]);
1187 } else if (is_secondary_port(ramdisk, port)) {
1188 channel = &(ramdisk->channels[1]);
1190 PrintError("Invalid Port: %d\n", port);
1194 drive = get_selected_drive(channel);
1195 controller = &(drive->controller);
1198 PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n",
1199 port, value, get_channel_no(ramdisk, channel));
1203 case PRI_FEATURES_PORT:
1204 case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1206 write_features(channel, value);
1209 case PRI_SECT_CNT_PORT:
1210 case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1212 write_sector_count(channel, value);
1215 case PRI_SECT_ADDR1_PORT:
1216 case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1218 write_sector_number(channel, value);
1221 case PRI_SECT_ADDR2_PORT:
1222 case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1224 write_cylinder_low(channel, value);
1227 case PRI_SECT_ADDR3_PORT:
1228 case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1230 write_cylinder_high(channel, value);
1233 case PRI_DRV_SEL_PORT:
1234 case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1236 // b7 Extended data field for ECC
1237 // b6/b5: Used to be sector size. 00=256,01=512,10=1024,11=128
1238 // Since 512 was always used, bit 6 was taken to mean LBA mode:
1239 // b6 1=LBA mode, 0=CHS mode
1246 PrintDebug("\tDrive Select value=%x\n", value);
1248 if ((value & 0xa0) != 0xa0) {
1249 PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1252 write_head_no(channel, value & 0xf);
1253 if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1254 PrintDebug("\t\tenabling LBA mode\n");
1257 write_lba_mode(channel, (value >> 6) & 1);
1261 if (drive->cdrom.cd) {
1262 PrintDebug("\t\tSetting LBA on CDROM: %d\n", (value >> 6) & 1);
1263 drive->cdrom.cd->set_LBA(drive->private_data, (value >> 6) & 1);
1267 channel->drive_select = (value >> 4) & 0x01;
1268 drive = get_selected_drive(channel);
1270 if (drive->device_type == IDE_NONE) {
1271 PrintDebug("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1272 channel->drive_select, get_channel_no(ramdisk, channel));
1274 controller->error_register = 0x04; // aborted
1275 controller->status.err = 1;
1281 PrintError("\t\thard drive: io write to unhandled port 0x%x (value = %c)\n", port, value);
1292 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1294 // struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1295 struct drive_t * drive = get_selected_drive(channel);
1296 struct controller_t * controller = &(drive->controller);
1298 PrintDebug("[raise_interrupt] disable_irq = %02x\n", controller->control.disable_irq);
1300 if (!(controller->control.disable_irq)) {
1303 PrintDebug("\t\tRaising interrupt %d {%s}\n\n", irq, device_type_to_str(drive->device_type));
1305 dev->vm->vm_ops.raise_irq(dev->vm, irq);
1307 PrintDebug("\t\tirq is disabled\n");
1313 static void rd_lower_irq(struct vm_device *dev, Bit32u irq) // __attribute__(regparm(1))
1315 PrintDebug("[lower_irq] irq = %d\n", irq);
1316 dev->vm->vm_ops.lower_irq(dev->vm, irq);
1325 //////////////////////////////////////////////////////////////////////////
1333 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1334 struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1335 struct drive_t * drive = get_selected_drive(channel);
1336 struct controller_t * controller = &(drive->controller);
1338 if (controller->buffer_index >= PACKET_SIZE) {
1339 PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n",
1340 controller->buffer_index);
1344 controller->buffer[controller->buffer_index] = value;
1345 controller->buffer[controller->buffer_index + 1] = (value >> 8);
1346 controller->buffer_index += 2;
1349 /* if packet completely writtten */
1350 if (controller->buffer_index >= PACKET_SIZE) {
1351 // complete command received
1352 Bit8u atapi_command = controller->buffer[0];
1354 PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1356 switch (atapi_command) {
1357 case 0x00: // test unit ready
1359 if (drive->cdrom.ready) {
1360 rd_atapi_cmd_nop(dev, channel);
1362 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1365 rd_raise_interrupt(dev, channel);
1369 case 0x03: // request sense
1371 int alloc_length = controller->buffer[4];
1373 if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1378 controller->buffer[0] = 0x70 | (1 << 7);
1379 controller->buffer[1] = 0;
1380 controller->buffer[2] = drive->sense.sense_key;
1381 controller->buffer[3] = drive->sense.information.arr[0];
1382 controller->buffer[4] = drive->sense.information.arr[1];
1383 controller->buffer[5] = drive->sense.information.arr[2];
1384 controller->buffer[6] = drive->sense.information.arr[3];
1385 controller->buffer[7] = 17 - 7;
1386 controller->buffer[8] = drive->sense.specific_inf.arr[0];
1387 controller->buffer[9] = drive->sense.specific_inf.arr[1];
1388 controller->buffer[10] = drive->sense.specific_inf.arr[2];
1389 controller->buffer[11] = drive->sense.specific_inf.arr[3];
1390 controller->buffer[12] = drive->sense.asc;
1391 controller->buffer[13] = drive->sense.ascq;
1392 controller->buffer[14] = drive->sense.fruc;
1393 controller->buffer[15] = drive->sense.key_spec.arr[0];
1394 controller->buffer[16] = drive->sense.key_spec.arr[1];
1395 controller->buffer[17] = drive->sense.key_spec.arr[2];
1397 rd_ready_to_send_atapi(dev, channel);
1400 case 0x1b: // start stop unit
1402 //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1403 rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1404 rd_bool Start = (controller->buffer[4] >> 0) & 1;
1407 if ((!LoEj) && (!Start)) {
1408 PrintError("FIXME: Stop disc not implemented\n");
1410 rd_atapi_cmd_nop(dev, channel);
1411 rd_raise_interrupt(dev, channel);
1412 } else if (!LoEj && Start) { // start (spin up) the disc
1414 drive->cdrom.cd->start_cdrom(drive->private_data);
1416 PrintError("FIXME: ATAPI start disc not reading TOC\n");
1417 rd_atapi_cmd_nop(dev, channel);
1418 rd_raise_interrupt(dev, channel);
1419 } else if (LoEj && !Start) { // Eject the disc
1420 rd_atapi_cmd_nop(dev, channel);
1422 if (drive->cdrom.ready) {
1424 drive->cdrom.cd->eject_cdrom(drive->private_data);
1426 drive->cdrom.ready = 0;
1427 //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1428 //bx_gui->update_drive_status_buttons();
1430 rd_raise_interrupt(dev, channel);
1431 } else { // Load the disc
1432 // My guess is that this command only closes the tray, that's a no-op for us
1433 rd_atapi_cmd_nop(dev, channel);
1434 rd_raise_interrupt(dev, channel);
1438 case 0xbd: // mechanism status
1440 uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1442 if (alloc_length == 0) {
1443 PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1447 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1451 controller->buffer[0] = 0; // reserved for non changers
1452 controller->buffer[1] = 0; // reserved for non changers
1454 controller->buffer[2] = 0; // Current LBA (TODO!)
1455 controller->buffer[3] = 0; // Current LBA (TODO!)
1456 controller->buffer[4] = 0; // Current LBA (TODO!)
1458 controller->buffer[5] = 1; // one slot
1460 controller->buffer[6] = 0; // slot table length
1461 controller->buffer[7] = 0; // slot table length
1463 rd_ready_to_send_atapi(dev, channel);
1466 case 0x5a: // mode sense
1468 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1470 Bit8u PC = controller->buffer[2] >> 6;
1471 Bit8u PageCode = controller->buffer[2] & 0x3f;
1474 case 0x0: // current values
1477 case 0x01: // error recovery
1480 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1484 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1485 sizeof(struct error_recovery_t));
1486 rd_ready_to_send_atapi(dev, channel);
1489 case 0x2a: // CD-ROM capabilities & mech. status
1492 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1496 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1498 controller->buffer[8] = 0x2a;
1499 controller->buffer[9] = 0x12;
1500 controller->buffer[10] = 0x00;
1501 controller->buffer[11] = 0x00;
1502 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1503 controller->buffer[12] = 0x70;
1504 controller->buffer[13] = (3 << 5);
1505 controller->buffer[14] = (unsigned char) (1 |
1506 (drive->cdrom.locked ? (1 << 1) : 0) |
1509 controller->buffer[15] = 0x00;
1510 controller->buffer[16] = (706 >> 8) & 0xff;
1511 controller->buffer[17] = 706 & 0xff;
1512 controller->buffer[18] = 0;
1513 controller->buffer[19] = 2;
1514 controller->buffer[20] = (512 >> 8) & 0xff;
1515 controller->buffer[21] = 512 & 0xff;
1516 controller->buffer[22] = (706 >> 8) & 0xff;
1517 controller->buffer[23] = 706 & 0xff;
1518 controller->buffer[24] = 0;
1519 controller->buffer[25] = 0;
1520 controller->buffer[26] = 0;
1521 controller->buffer[27] = 0;
1522 rd_ready_to_send_atapi(dev, channel);
1525 case 0x0d: // CD-ROM
1526 case 0x0e: // CD-ROM audio control
1529 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1531 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1532 ASC_INV_FIELD_IN_CMD_PACKET);
1533 rd_raise_interrupt(dev, channel);
1538 // not implemeted by this device
1539 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1541 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1542 ASC_INV_FIELD_IN_CMD_PACKET);
1543 rd_raise_interrupt(dev, channel);
1549 case 0x1: // changeable values
1552 case 0x01: // error recovery
1553 case 0x0d: // CD-ROM
1554 case 0x0e: // CD-ROM audio control
1555 case 0x2a: // CD-ROM capabilities & mech. status
1558 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1560 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1561 ASC_INV_FIELD_IN_CMD_PACKET);
1562 rd_raise_interrupt(dev, channel);
1567 // not implemeted by this device
1568 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1570 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1571 ASC_INV_FIELD_IN_CMD_PACKET);
1572 rd_raise_interrupt(dev, channel);
1578 case 0x2: // default values
1581 case 0x01: // error recovery
1582 case 0x0d: // CD-ROM
1583 case 0x0e: // CD-ROM audio control
1584 case 0x2a: // CD-ROM capabilities & mech. status
1586 PrintError("cdrom: MODE SENSE (dflt), code=%x\n",
1592 // not implemeted by this device
1593 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1595 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1596 ASC_INV_FIELD_IN_CMD_PACKET);
1597 rd_raise_interrupt(dev, channel);
1603 case 0x3: // saved values not implemented
1605 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1606 rd_raise_interrupt(dev, channel);
1611 PrintError("Should not get here!\n");
1618 case 0x12: // inquiry
1620 uint8_t alloc_length = controller->buffer[4];
1622 if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1626 controller->buffer[0] = 0x05; // CD-ROM
1627 controller->buffer[1] = 0x80; // Removable
1628 controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1629 controller->buffer[3] = 0x21; // ATAPI-2, as specified
1630 controller->buffer[4] = 31; // additional length (total 36)
1631 controller->buffer[5] = 0x00; // reserved
1632 controller->buffer[6] = 0x00; // reserved
1633 controller->buffer[7] = 0x00; // reserved
1636 const char* vendor_id = "VTAB ";
1638 for (i = 0; i < 8; i++) {
1639 controller->buffer[8+i] = vendor_id[i];
1643 const char* product_id = "Turbo CD-ROM ";
1644 for (i = 0; i < 16; i++) {
1645 controller->buffer[16+i] = product_id[i];
1648 // Product Revision level
1649 const char* rev_level = "1.0 ";
1650 for (i = 0; i < 4; i++) {
1651 controller->buffer[32 + i] = rev_level[i];
1654 rd_ready_to_send_atapi(dev, channel);
1657 case 0x25: // read cd-rom capacity
1659 // no allocation length???
1660 if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1664 if (drive->cdrom.ready) {
1665 uint32_t capacity = drive->cdrom.capacity;
1667 PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1669 controller->buffer[0] = (capacity >> 24) & 0xff;
1670 controller->buffer[1] = (capacity >> 16) & 0xff;
1671 controller->buffer[2] = (capacity >> 8) & 0xff;
1672 controller->buffer[3] = (capacity >> 0) & 0xff;
1673 controller->buffer[4] = (2048 >> 24) & 0xff;
1674 controller->buffer[5] = (2048 >> 16) & 0xff;
1675 controller->buffer[6] = (2048 >> 8) & 0xff;
1676 controller->buffer[7] = (2048 >> 0) & 0xff;
1678 rd_ready_to_send_atapi(dev, channel);
1680 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1681 rd_raise_interrupt(dev, channel);
1687 case 0xbe: // read cd
1689 if (drive->cdrom.ready) {
1690 PrintError("Read CD with CD present not implemented\n");
1691 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1692 rd_raise_interrupt(dev, channel);
1694 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1695 rd_raise_interrupt(dev, channel);
1699 case 0x43: // read toc
1701 if (drive->cdrom.ready) {
1703 bool msf = (controller->buffer[1] >> 1) & 1;
1704 uint8_t starting_track = controller->buffer[6];
1706 uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1708 uint8_t format = (controller->buffer[9] >> 6);
1711 PrintDebug("Reading CDROM TOC: Format=%d (byte count=%d) (toc length:%d)\n",
1712 format, controller->byte_count, toc_length);
1717 if (!(drive->cdrom.cd->read_toc(drive->private_data, controller->buffer,
1718 &toc_length, msf, starting_track))) {
1719 PrintError("CDROM: Reading Table of Contents Failed\n");
1720 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1721 ASC_INV_FIELD_IN_CMD_PACKET);
1722 rd_raise_interrupt(dev, channel);
1726 if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1730 rd_ready_to_send_atapi(dev, channel);
1735 // multi session stuff. we ignore this and emulate a single session only
1737 if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1741 controller->buffer[0] = 0;
1742 controller->buffer[1] = 0x0a;
1743 controller->buffer[2] = 1;
1744 controller->buffer[3] = 1;
1746 for (i = 0; i < 8; i++) {
1747 controller->buffer[4 + i] = 0;
1750 rd_ready_to_send_atapi(dev, channel);
1755 PrintError("(READ TOC) Format %d not supported\n", format);
1759 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1760 rd_raise_interrupt(dev, channel);
1764 case 0x28: // read (10)
1765 case 0xa8: // read (12)
1768 uint32_t transfer_length;
1769 if (atapi_command == 0x28) {
1770 transfer_length = rd_read_16bit(controller->buffer + 7);
1772 transfer_length = rd_read_32bit(controller->buffer + 6);
1775 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1777 if (!(drive->cdrom.ready)) {
1778 PrintError("CDROM Error: Not Ready (ATA%d/%d)\n",
1779 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1780 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1781 rd_raise_interrupt(dev, channel);
1785 if (transfer_length == 0) {
1786 rd_atapi_cmd_nop(dev, channel);
1787 rd_raise_interrupt(dev, channel);
1788 PrintError("READ(%d) with transfer length 0, ok\n",
1789 (atapi_command == 0x28) ? 10 : 12);
1793 if (lba + transfer_length > drive->cdrom.capacity) {
1794 PrintError("CDROM Error: Capacity exceeded [capacity=%d] (ATA%d/%d)\n",
1795 drive->cdrom.capacity,
1796 get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1797 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1798 rd_raise_interrupt(dev, channel);
1802 PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n",
1803 (atapi_command == 0x28) ? 10 : 12,
1804 lba, transfer_length);
1807 if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1808 transfer_length * 2048, true) == -1) {
1809 PrintError("CDROM Error: Atapi command send error\n");
1813 drive->cdrom.remaining_blocks = transfer_length;
1814 drive->cdrom.next_lba = lba;
1815 rd_ready_to_send_atapi(dev, channel);
1820 uint32_t lba = rd_read_32bit(controller->buffer + 2);
1822 if (!(drive->cdrom.ready)) {
1823 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1824 rd_raise_interrupt(dev, channel);
1828 if (lba > drive->cdrom.capacity) {
1829 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1830 rd_raise_interrupt(dev, channel);
1834 PrintDebug("\t\tcdrom: SEEK (ignored)\n");
1836 rd_atapi_cmd_nop(dev, channel);
1837 rd_raise_interrupt(dev, channel);
1841 case 0x1e: // prevent/allow medium removal
1844 if (drive->cdrom.ready) {
1845 drive->cdrom.locked = controller->buffer[4] & 1;
1846 rd_atapi_cmd_nop(dev, channel);
1848 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1851 rd_raise_interrupt(dev, channel);
1855 case 0x42: // read sub-channel
1857 //bool msf = get_packet_field(channel,1, 1, 1);
1858 bool sub_q = get_packet_field(channel,2, 6, 1);
1859 //uint8_t data_format = get_packet_byte(channel,3);
1860 //uint8_t track_number = get_packet_byte(channel,6);
1861 uint16_t alloc_length = get_packet_word(channel,7);
1866 UNUSED(data_format);
1867 UNUSED(track_number);
1869 if (!(drive->cdrom.ready)) {
1870 rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1871 rd_raise_interrupt(dev, channel);
1873 controller->buffer[0] = 0;
1874 controller->buffer[1] = 0; // audio not supported
1875 controller->buffer[2] = 0;
1876 controller->buffer[3] = 0;
1878 int ret_len = 4; // header size
1880 if (sub_q) { // !sub_q == header only
1881 PrintError("Read sub-channel with SubQ not implemented\n");
1882 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1883 ASC_INV_FIELD_IN_CMD_PACKET);
1884 rd_raise_interrupt(dev, channel);
1887 if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1890 rd_ready_to_send_atapi(dev, channel);
1894 case 0x51: // read disc info
1896 // no-op to keep the Linux CD-ROM driver happy
1897 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1898 rd_raise_interrupt(dev, channel);
1901 case 0x55: // mode select
1902 case 0xa6: // load/unload cd
1903 case 0x4b: // pause/resume
1904 case 0x45: // play audio
1905 case 0x47: // play audio msf
1906 case 0xbc: // play cd
1907 case 0xb9: // read cd msf
1908 case 0x44: // read header
1910 case 0xbb: // set cd speed
1911 case 0x4e: // stop play/scan
1914 PrintError("ATAPI command 0x%x not implemented yet\n",
1916 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1917 rd_raise_interrupt(dev, channel);
1920 PrintError("Unknown ATAPI command 0x%x (%d)\n",
1921 atapi_command, atapi_command);
1922 // We'd better signal the error if the user chose to continue
1923 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1924 rd_raise_interrupt(dev, channel);
1936 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
1938 struct drive_t * drive = &(channel->drives[channel->drive_select]);
1939 struct controller_t * controller = &(drive->controller);
1941 // controller->byte_count is a union of controller->cylinder_no;
1942 // lazy is used to force a data read in the buffer at the next read.
1944 PrintDebug("[rd_init_send_atapi_cmd]\n");
1946 if (controller->byte_count == 0xffff) {
1947 controller->byte_count = 0xfffe;
1950 if ((controller->byte_count & 1) &&
1951 !(alloc_length <= controller->byte_count)) {
1953 PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n",
1954 controller->byte_count,
1956 controller->byte_count - 1);
1958 controller->byte_count -= 1;
1961 if (controller->byte_count == 0) {
1962 PrintError("\t\tATAPI command with zero byte count\n");
1966 if (alloc_length < 0) {
1967 PrintError("\t\tAllocation length < 0\n");
1971 if (alloc_length == 0) {
1972 alloc_length = controller->byte_count;
1975 controller->interrupt_reason.i_o = 1;
1976 controller->interrupt_reason.c_d = 0;
1977 controller->status.busy = 0;
1978 controller->status.drq = 1;
1979 controller->status.err = 0;
1981 // no bytes transfered yet
1983 controller->buffer_index = 2048;
1985 controller->buffer_index = 0;
1988 controller->drq_index = 0;
1990 if (controller->byte_count > req_length) {
1991 controller->byte_count = req_length;
1994 if (controller->byte_count > alloc_length) {
1995 controller->byte_count = alloc_length;
1998 drive->atapi.command = command;
1999 drive->atapi.drq_bytes = controller->byte_count;
2000 drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2003 // // bias drq_bytes and total_bytes_remaining
2004 // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2005 // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2013 void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2014 PrintDebug("[rd_ready_to_send_atapi]\n");
2016 rd_raise_interrupt(dev, channel);
2023 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2025 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2026 struct controller_t * controller = &(drive->controller);
2028 #ifdef DEBUG_RAMDISK
2030 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2031 PrintDebug("[rd_atapi_cmd_error]\n");
2032 PrintDebug("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n",
2033 get_channel_no(ramdisk, channel), sense_key, asc);
2037 controller->error_register = sense_key << 4;
2038 controller->interrupt_reason.i_o = 1;
2039 controller->interrupt_reason.c_d = 1;
2040 controller->interrupt_reason.rel = 0;
2041 controller->status.busy = 0;
2042 controller->status.drive_ready = 1;
2043 controller->status.write_fault = 0;
2044 controller->status.drq = 0;
2045 controller->status.err = 1;
2047 drive->sense.sense_key = sense_key;
2048 drive->sense.asc = asc;
2049 drive->sense.ascq = 0;
2054 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2056 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2057 struct controller_t * controller = &(drive->controller);
2059 PrintDebug("[rd_atapi_cmd_nop]\n");
2060 controller->interrupt_reason.i_o = 1;
2061 controller->interrupt_reason.c_d = 1;
2062 controller->interrupt_reason.rel = 0;
2063 controller->status.busy = 0;
2064 controller->status.drive_ready = 1;
2065 controller->status.drq = 0;
2066 controller->status.err = 0;
2072 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2074 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2075 struct controller_t * controller = &(drive->controller);
2079 const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2080 const char* firmware = "ALPHA1 ";
2082 drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2084 for (i = 1; i <= 9; i++) {
2085 drive->id_drive[i] = 0;
2088 for (i = 0; i < 10; i++) {
2089 drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2090 (serial_number[(i * 2) + 1]));
2093 for (i = 20; i <= 22; i++) {
2094 drive->id_drive[i] = 0;
2097 for (i = 0; i < strlen(firmware)/2; i++) {
2098 drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2099 (firmware[(i * 2) + 1]));
2101 V3_ASSERT((23 + i) == 27);
2103 for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2104 drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2105 (drive->model_no[(i * 2) + 1]));
2107 V3_ASSERT((27 + i) == 47);
2109 drive->id_drive[47] = 0;
2110 drive->id_drive[48] = 1; // 32 bits access
2112 drive->id_drive[49] = (1 << 9); // LBA supported
2114 drive->id_drive[50] = 0;
2115 drive->id_drive[51] = 0;
2116 drive->id_drive[52] = 0;
2118 drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2120 for (i = 54; i <= 62; i++) {
2121 drive->id_drive[i] = 0;
2124 // copied from CFA540A
2125 drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2126 drive->id_drive[64] = 0x0001; // PIO
2127 drive->id_drive[65] = 0x00b4;
2128 drive->id_drive[66] = 0x00b4;
2129 drive->id_drive[67] = 0x012c;
2130 drive->id_drive[68] = 0x00b4;
2132 drive->id_drive[69] = 0;
2133 drive->id_drive[70] = 0;
2134 drive->id_drive[71] = 30; // faked
2135 drive->id_drive[72] = 30; // faked
2136 drive->id_drive[73] = 0;
2137 drive->id_drive[74] = 0;
2139 drive->id_drive[75] = 0;
2141 for (i = 76; i <= 79; i++) {
2142 drive->id_drive[i] = 0;
2145 drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2146 drive->id_drive[81] = 0;
2147 drive->id_drive[82] = 0;
2148 drive->id_drive[83] = 0;
2149 drive->id_drive[84] = 0;
2150 drive->id_drive[85] = 0;
2151 drive->id_drive[86] = 0;
2152 drive->id_drive[87] = 0;
2153 drive->id_drive[88] = 0;
2155 for (i = 89; i <= 126; i++) {
2156 drive->id_drive[i] = 0;
2159 drive->id_drive[127] = 0;
2160 drive->id_drive[128] = 0;
2162 for (i = 129; i <= 159; i++) {
2163 drive->id_drive[i] = 0;
2166 for (i = 160; i <= 255; i++) {
2167 drive->id_drive[i] = 0;
2170 // now convert the id_drive array (native 256 word format) to
2171 // the controller buffer (512 bytes)
2173 for (i = 0; i <= 255; i++) {
2174 temp16 = drive->id_drive[i];
2175 controller->buffer[i * 2] = temp16 & 0x00ff;
2176 controller->buffer[i * 2 + 1] = temp16 >> 8;
2189 void rd_init_mode_sense_single(struct vm_device * dev,
2190 struct channel_t * channel, const void* src, int size)
2192 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2193 struct controller_t * controller = &(drive->controller);
2195 PrintDebug("[rd_init_mode_sense_single]\n");
2198 controller->buffer[0] = (size + 6) >> 8;
2199 controller->buffer[1] = (size + 6) & 0xff;
2200 controller->buffer[2] = 0x70; // no media present
2201 controller->buffer[3] = 0; // reserved
2202 controller->buffer[4] = 0; // reserved
2203 controller->buffer[5] = 0; // reserved
2204 controller->buffer[6] = 0; // reserved
2205 controller->buffer[7] = 0; // reserved
2208 memcpy(controller->buffer + 8, src, size);
2213 static void rd_command_aborted(struct vm_device * dev,
2214 struct channel_t * channel, unsigned value) {
2215 struct drive_t * drive = &(channel->drives[channel->drive_select]);
2216 struct controller_t * controller = &(drive->controller);
2218 PrintDebug("[rd_command_aborted]\n");
2219 PrintDebug("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2221 controller->current_command = 0;
2222 controller->status.busy = 0;
2223 controller->status.drive_ready = 1;
2224 controller->status.err = 1;
2225 controller->error_register = 0x04; // command ABORTED
2226 controller->status.drq = 0;
2227 controller->status.seek_complete = 0;
2228 controller->status.corrected_data = 0;
2229 controller->buffer_index = 0;
2231 rd_raise_interrupt(dev, channel);
2235 static int ramdisk_init_device(struct vm_device *dev) {
2236 struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2238 PrintDebug("Initializing Ramdisk\n");
2241 rd_init_hardware(ramdisk);
2244 dev_hook_io(dev, PRI_CTRL_PORT,
2245 &read_status_port, &write_ctrl_port);
2247 dev_hook_io(dev, PRI_DATA_PORT,
2248 &read_data_port, &write_data_port);
2249 dev_hook_io(dev, PRI_FEATURES_PORT,
2250 &read_general_port, &write_general_port);
2251 dev_hook_io(dev, PRI_SECT_CNT_PORT,
2252 &read_general_port, &write_general_port);
2253 dev_hook_io(dev, PRI_SECT_ADDR1_PORT,
2254 &read_general_port, &write_general_port);
2255 dev_hook_io(dev, PRI_SECT_ADDR2_PORT,
2256 &read_general_port, &write_general_port);
2257 dev_hook_io(dev, PRI_SECT_ADDR3_PORT,
2258 &read_general_port, &write_general_port);
2259 dev_hook_io(dev, PRI_DRV_SEL_PORT,
2260 &read_general_port, &write_general_port);
2261 dev_hook_io(dev, PRI_CMD_PORT,
2262 &read_status_port, &write_cmd_port);
2265 dev_hook_io(dev, SEC_CTRL_PORT,
2266 &read_status_port, &write_ctrl_port);
2268 dev_hook_io(dev, SEC_DATA_PORT,
2269 &read_data_port, &write_data_port);
2270 dev_hook_io(dev, SEC_FEATURES_PORT,
2271 &read_general_port, &write_general_port);
2272 dev_hook_io(dev, SEC_SECT_CNT_PORT,
2273 &read_general_port, &write_general_port);
2274 dev_hook_io(dev, SEC_SECT_ADDR1_PORT,
2275 &read_general_port, &write_general_port);
2276 dev_hook_io(dev, SEC_SECT_ADDR2_PORT,
2277 &read_general_port, &write_general_port);
2278 dev_hook_io(dev, SEC_SECT_ADDR3_PORT,
2279 &read_general_port, &write_general_port);
2280 dev_hook_io(dev, SEC_DRV_SEL_PORT,
2281 &read_general_port, &write_general_port);
2282 dev_hook_io(dev, SEC_CMD_PORT,
2283 &read_status_port, &write_cmd_port);
2287 dev_hook_io(dev, SEC_ADDR_REG_PORT,
2288 &read_general_port, &write_general_port);
2290 dev_hook_io(dev, PRI_ADDR_REG_PORT,
2291 &read_general_port, &write_general_port);
2300 static int ramdisk_deinit_device(struct vm_device *dev) {
2301 struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2302 rd_close_harddrive(ramdisk);
2306 static struct vm_device_ops dev_ops = {
2307 .init = ramdisk_init_device,
2308 .deinit = ramdisk_deinit_device,
2317 struct vm_device *create_ramdisk()
2320 struct ramdisk_t *ramdisk;
2321 ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));
2322 V3_ASSERT(ramdisk != NULL);
2324 PrintDebug("[create_ramdisk]\n");
2326 struct vm_device *device = create_device("RAMDISK", &dev_ops, ramdisk);
2334 #ifdef DEBUG_RAMDISK
2336 static void rd_print_state(struct ramdisk_t * ramdisk) {
2339 struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2342 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2343 memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2346 PrintDebug("sizeof(*channels) = %d\n", sizeof(*channels));
2347 PrintDebug("sizeof(channles->drives[0].controller) = %d\n", sizeof((channels->drives[0].controller)));
2348 PrintDebug("sizeof(channles->drives[0].cdrom) = %d\n", sizeof((channels->drives[0].cdrom)));
2349 PrintDebug("sizeof(channles->drives[0].sense) = %d\n", sizeof((channels->drives[0].sense)));
2350 PrintDebug("sizeof(channles->drives[0].atapi) = %d\n", sizeof((channels->drives[0].atapi)));
2353 PrintDebug("sizeof(channles->drives[0].controller.status) = %d\n",
2354 sizeof((channels->drives[0].controller.status)));
2355 PrintDebug("sizeof(channles->drives[0].controller.sector_count) = %d\n",
2356 sizeof((channels->drives[0].controller.sector_count)));
2357 PrintDebug("sizeof(channles->drives[0].controller.interrupt_reason) = %d\n",
2358 sizeof((channels->drives[0].controller.interrupt_reason)));
2360 PrintDebug("sizeof(channles->drives[0].controller.cylinder_no) = %d\n",
2361 sizeof((channels->drives[0].controller.cylinder_no)));
2362 PrintDebug("sizeof(channles->drives[0].controller.byte_count) = %d\n",
2363 sizeof((channels->drives[0].controller.byte_count)));
2366 PrintDebug("sizeof(channles->drives[0].controller.control) = %d\n",
2367 sizeof((channels->drives[0].controller.control)));
2370 for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2372 for (device = 0; device < 2; device++){
2374 // Initialize controller state, even if device is not present
2375 PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2377 channels[channel].drives[device].controller.status.busy);
2378 PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n",
2380 channels[channel].drives[device].controller.status.drive_ready);
2381 PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n",
2383 channels[channel].drives[device].controller.status.write_fault);
2384 PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n",
2386 channels[channel].drives[device].controller.status.seek_complete);
2387 PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n",
2389 channels[channel].drives[device].controller.status.drq);
2390 PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n",
2392 channels[channel].drives[device].controller.status.corrected_data);
2393 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n",
2395 channels[channel].drives[device].controller.status.index_pulse);
2396 PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n",
2398 channels[channel].drives[device].controller.status.index_pulse_count);
2399 PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n",
2401 channels[channel].drives[device].controller.status.err);
2404 PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n",
2406 channels[channel].drives[device].controller.error_register);
2407 PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n",
2409 channels[channel].drives[device].controller.head_no);
2410 PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n",
2412 channels[channel].drives[device].controller.sector_count);
2413 PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n",
2415 channels[channel].drives[device].controller.sector_no);
2416 PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n",
2418 channels[channel].drives[device].controller.cylinder_no);
2419 PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n",
2421 channels[channel].drives[device].controller.current_command);
2422 PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n",
2424 channels[channel].drives[device].controller.buffer_index);
2427 PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n",
2429 channels[channel].drives[device].controller.control.reset);
2430 PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n",
2432 channels[channel].drives[device].controller.control.disable_irq);
2435 PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n",
2437 channels[channel].drives[device].controller.reset_in_progress);
2438 PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n",
2440 channels[channel].drives[device].controller.sectors_per_block);
2441 PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n",
2443 channels[channel].drives[device].controller.lba_mode);
2444 PrintDebug("channels[%d].drives[%d].controller.features = %d\n",
2446 channels[channel].drives[device].controller.features);
2449 PrintDebug("channels[%d].drives[%d].model_no = %s\n",
2451 channels[channel].drives[device].model_no);
2452 PrintDebug("channels[%d].drives[%d].device_type = %d\n",
2454 channels[channel].drives[device].device_type);
2455 PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n",
2457 channels[channel].drives[device].cdrom.locked);
2458 PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n",
2460 channels[channel].drives[device].sense.sense_key);
2461 PrintDebug("channels[%d].drives[%d].sense.asc = %d\n",
2463 channels[channel].drives[device].sense.asc);
2464 PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n",
2466 channels[channel].drives[device].sense.ascq);
2470 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n",
2472 channels[channel].drives[device].controller.interrupt_reason.c_d);
2474 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n",
2476 channels[channel].drives[device].controller.interrupt_reason.i_o);
2478 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n",
2480 channels[channel].drives[device].controller.interrupt_reason.rel);
2482 PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n",
2484 channels[channel].drives[device].controller.interrupt_reason.tag);
2486 PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n",
2488 channels[channel].drives[device].cdrom.ready);
2498 static int check_bit_fields(struct controller_t * controller) {
2500 controller->sector_count = 0;
2501 controller->interrupt_reason.c_d = 1;
2502 if (controller->sector_count != 0x01) {
2503 return INTR_REASON_BIT_ERR;
2506 controller->sector_count = 0;
2507 controller->interrupt_reason.i_o = 1;
2508 if (controller->sector_count != 0x02) {
2509 return INTR_REASON_BIT_ERR;
2512 controller->sector_count = 0;
2513 controller->interrupt_reason.rel = 1;
2514 if (controller->sector_count != 0x04) {
2515 return INTR_REASON_BIT_ERR;
2518 controller->sector_count = 0;
2519 controller->interrupt_reason.tag = 3;
2520 if (controller->sector_count != 0x18) {
2521 return INTR_REASON_BIT_ERR;