Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added debugging output, DEBUG bug still an issue
[palacios.git] / palacios / src / devices / ramdisk.c
1 /* 
2  *
3  *   Copyright (C) 2002  MandrakeSoft S.A.
4  *
5  *     MandrakeSoft S.A.
6  *     43, rue d'Aboukir
7  *     75002 Paris - France
8  *     http://www.linux-mandrake.com/
9  *     http://www.mandrakesoft.com/
10  *
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.
15  *
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.
20  *
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
24  *
25  * Major modifications made for the V3VEE project
26  * 
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
30  * 
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
35  * 
36  */
37
38
39 #include <devices/ramdisk.h>
40 #include <palacios/vmm.h>
41 #include <devices/cdrom.h>
42 #include <devices/ide.h>
43
44
45 #ifndef TRACE_RAMDISK
46 #undef PrintTrace
47 #define PrintTrace(fmt, args...)
48 #endif
49
50
51 #ifndef DEBUG_RAMDISK
52 #undef PrintDebug
53 #define PrintDebug(fmt, args...)
54 #endif
55                                                  
56
57 /*
58  * Data type definitions
59  *
60  */
61 #define INDEX_PULSE_CYCLE 10
62
63
64
65
66 #define INTR_REASON_BIT_ERR           0x01
67 #define UNABLE_FIND_TAT_CHANNEL_ERR   0x02
68 #define DRQ_ERR                       0x03
69 #define READ_BUF_GT_512               0x04
70
71
72
73 #define PRI_DATA_PORT         0x1f0
74 #define PRI_FEATURES_PORT     0x1f1
75 #define PRI_SECT_CNT_PORT     0x1f2
76 #define PRI_SECT_ADDR1_PORT   0x1f3
77 #define PRI_SECT_ADDR2_PORT   0x1f4
78 #define PRI_SECT_ADDR3_PORT   0x1f5
79 #define PRI_DRV_SEL_PORT      0x1f6
80 #define PRI_CMD_PORT          0x1f7
81 #define PRI_CTRL_PORT         0x3f6
82 #define PRI_ADDR_REG_PORT     0x3f7
83
84 #define SEC_DATA_PORT         0x170
85 #define SEC_FEATURES_PORT     0x171
86 #define SEC_SECT_CNT_PORT     0x172
87 #define SEC_SECT_ADDR1_PORT   0x173
88 #define SEC_SECT_ADDR2_PORT   0x174
89 #define SEC_SECT_ADDR3_PORT   0x175
90 #define SEC_DRV_SEL_PORT      0x176
91 #define SEC_CMD_PORT          0x177
92 #define SEC_CTRL_PORT         0x376
93 #define SEC_ADDR_REG_PORT     0x377
94
95
96
97
98
99 #define PACKET_SIZE 12
100
101
102 /*
103  * Debug facilities
104  */
105
106 #define ATA_DETECT                     0xf0 //0X3E8
107 #define ATA_RESET                      0xf1 //0X3E9
108 #define ATA_CMD_DATA_IN                0xf2 //0X3EA
109 #define ATA_CMD_DATA_OUT               0xf3 //0X3EB
110 #define ATA_CMD_PACKET                 0xf4 //0X3EC
111 #define ATAPI_GET_SENSE                0xf5 //0X3ED
112 #define ATAPI_IS_READY                 0xf6 //0X3EE
113 #define ATAPI_IS_CDROM                 0xf7 //0X3EF
114
115 #define CDEMU_INIT                     0xf8 //0X2E8
116 #define CDEMU_ISACTIVE                 0xf9 //0X2E9
117 #define CDEMU_EMULATED_DRIVE           0xfa //0X2EA
118 #define CDROM_BOOT                     0xfb //0X2EB
119
120
121 #define HARD_DRIVE_POST                0xfc //0X2EC
122
123
124 #define ATA_DEVICE_NO                  0xfd //0X2ED
125 #define ATA_DEVICE_TYPE                0xfe //0X2ED
126
127 #define INT13_HARDDISK                 0xff //0x2ef
128 #define INT13_CDROM                    0xe0 //0x2f8
129 #define INT13_CDEMU                    0xe1 //0x2f9
130 #define INT13_ELTORITO                 0xe2 //0x2fa
131 #define INT13_DISKETTE_FUNCTION        0xe3 //0x2fb
132
133
134
135
136 static const char cdrom_str[] = "CD-ROM";
137 static const char harddisk_str[] = "HARDDISK";
138 static const char none_str[] = "NONE";
139
140
141 static inline const char * device_type_to_str(device_type_t type) {
142   switch (type) {
143   case IDE_DISK:
144     return harddisk_str;
145   case IDE_CDROM:
146     return cdrom_str;
147   case IDE_NONE:
148     return none_str;
149   default:
150     return NULL;
151   }
152 }
153
154
155 static inline void write_features(struct channel_t * channel, uchar_t value) {
156   channel->drives[0].controller.features = value;
157   channel->drives[1].controller.features = value;
158 }
159
160
161 static inline void write_sector_count(struct channel_t * channel, uchar_t value) {
162   channel->drives[0].controller.sector_count = value;
163   channel->drives[1].controller.sector_count = value;
164 }
165
166 static inline void write_sector_number(struct channel_t * channel, uchar_t value) {
167   channel->drives[0].controller.sector_no = value;
168   channel->drives[1].controller.sector_no = value;
169 }
170
171
172 static inline void write_cylinder_low(struct channel_t * channel, uchar_t value) {
173   channel->drives[0].controller.cylinder_no &= 0xff00;
174   channel->drives[0].controller.cylinder_no |= value;
175   channel->drives[1].controller.cylinder_no &= 0xff00;
176   channel->drives[1].controller.cylinder_no |= value;
177 }
178
179 static inline void write_cylinder_high(struct channel_t * channel, uchar_t value) {
180   ushort_t val2 = value;
181   val2 = val2 << 8;
182   channel->drives[0].controller.cylinder_no &= 0x00ff;
183   channel->drives[0].controller.cylinder_no |= (val2 & 0xff00);
184
185   channel->drives[1].controller.cylinder_no &= 0x00ff;
186   channel->drives[1].controller.cylinder_no |= (val2 & 0xff00);
187 }
188
189 static inline void write_head_no(struct channel_t * channel, uchar_t value) {
190   channel->drives[0].controller.head_no = value;
191   channel->drives[1].controller.head_no = value;
192 }
193
194 static inline void write_lba_mode(struct channel_t * channel, uchar_t value) {
195   channel->drives[0].controller.lba_mode = value;
196   channel->drives[1].controller.lba_mode = value;
197 }
198
199
200 static inline uint_t get_channel_no(struct ramdisk_t * ramdisk, struct channel_t * channel) {
201   return (((uchar_t *)channel - (uchar_t *)(ramdisk->channels)) / sizeof(struct channel_t));
202 }
203
204 static inline uint_t get_drive_no(struct channel_t * channel, struct drive_t * drive) {
205   return (((uchar_t *)drive - (uchar_t*)(channel->drives)) /  sizeof(struct drive_t));
206 }
207
208 static inline struct drive_t * get_selected_drive(struct channel_t * channel) {
209   return &(channel->drives[channel->drive_select]);
210 }
211
212
213 static inline int is_primary_port(struct ramdisk_t * ramdisk, ushort_t port) {
214   switch(port) 
215     {
216     case PRI_DATA_PORT:
217     case PRI_FEATURES_PORT:
218     case PRI_SECT_CNT_PORT:
219     case PRI_SECT_ADDR1_PORT:
220     case PRI_SECT_ADDR2_PORT:
221     case PRI_SECT_ADDR3_PORT:
222     case PRI_DRV_SEL_PORT:
223     case PRI_CMD_PORT:
224     case PRI_CTRL_PORT:
225       return 1;
226     default:
227       return 0;
228     }
229 }
230
231
232
233 static inline int is_secondary_port(struct ramdisk_t * ramdisk, ushort_t port) {
234   switch(port) 
235     {
236     case SEC_DATA_PORT:
237     case SEC_FEATURES_PORT:
238     case SEC_SECT_CNT_PORT:
239     case SEC_SECT_ADDR1_PORT:
240     case SEC_SECT_ADDR2_PORT:
241     case SEC_SECT_ADDR3_PORT:
242     case SEC_DRV_SEL_PORT:
243     case SEC_CMD_PORT:
244     case SEC_CTRL_PORT:
245       return 1;
246     default:
247       return 0;
248     }
249 }
250
251 static inline int num_drives_on_channel(struct channel_t * channel) {
252   if ((channel->drives[0].device_type == IDE_NONE) &&
253       (channel->drives[1].device_type == IDE_NONE)) {
254     return 0;
255   } else if ((channel->drives[0].device_type != IDE_NONE) &&
256              (channel->drives[1].device_type != IDE_NONE)) {
257     return 2;
258   } else {
259     return 1;
260   }
261 }
262
263
264
265 static inline uchar_t extract_bits(uchar_t * buf, uint_t buf_offset, uint_t bit_offset, uint_t num_bits) {
266   uchar_t val = buf[buf_offset];
267   val = val >> bit_offset;
268   val &= ((1 << num_bits) -1);
269   return val;
270 }
271
272
273 static inline uchar_t get_packet_field(struct channel_t * channel, uint_t packet_offset, uint_t bit_offset, uint_t num_bits) {
274   struct drive_t * drive = get_selected_drive(channel);
275   return extract_bits(drive->controller.buffer, packet_offset, bit_offset, num_bits);
276 }
277
278
279 static inline uchar_t get_packet_byte(struct channel_t * channel, uint_t offset) {
280   struct drive_t * drive = get_selected_drive(channel);
281   return drive->controller.buffer[offset];
282 }
283
284 static inline uint16_t get_packet_word(struct channel_t * channel, uint_t offset) {
285   struct drive_t * drive = get_selected_drive(channel);
286   uint16_t val = drive->controller.buffer[offset];
287   val = val << 8;
288   val |= drive->controller.buffer[offset + 1];
289   return val;
290 }
291
292
293 static inline uint16_t rd_read_16bit(const uint8_t* buf) {
294   return (buf[0] << 8) | buf[1];
295 }
296
297
298
299 static inline uint32_t rd_read_32bit(const uint8_t* buf) {
300   return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
301 }
302
303 ////////////////////////////////////////////////////////////////////////////
304
305
306 /*
307  * ATAPI routines
308  */
309
310
311 static void rd_init_mode_sense_single(struct vm_device * dev, struct channel_t * channel, const void * src, int size);
312
313 static void rd_command_aborted(struct vm_device * dev, struct channel_t * channel, unsigned value);
314
315
316
317
318 static int handle_atapi_packet_command(struct vm_device * dev, 
319                                        struct channel_t * channel, 
320                                        ushort_t val);
321
322 static int rd_init_send_atapi_command(struct vm_device * dev, 
323                                       struct channel_t * channel, 
324                                       Bit8u command, int req_length, 
325                                       int alloc_length, bool lazy);
326
327 static void rd_ready_to_send_atapi(struct vm_device * dev, 
328                                    struct channel_t * channel);
329
330 static void rd_atapi_cmd_error(struct vm_device * dev, 
331                                struct channel_t * channel, 
332                                sense_t sense_key, asc_t asc);
333
334 static void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel);
335 static void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel);
336
337
338
339 /*
340  * Interrupt handling
341  */
342 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel);
343 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel);
344
345
346
347 /*
348  * Helper routines
349  */
350
351
352
353 #ifdef DEBUG_RAMDISK
354 static void rd_print_state(struct ramdisk_t *ramdisk);
355 static int check_bit_fields(struct controller_t * controller);
356 #endif
357
358
359 ////////////////////////////////////////////////////////////////////
360
361
362
363
364
365 int v3_ramdisk_register_cdrom(struct vm_device * dev, uint_t busID, uint_t driveID, struct cdrom_ops* cd, void * private_data) {
366   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
367   struct channel_t * channel = &(ramdisk->channels[busID]);
368   struct drive_t * drive = &(channel->drives[driveID]);
369   struct controller_t * controller = &(drive->controller);
370
371
372   
373   if (drive->device_type != IDE_NONE) {
374     PrintError("Device already registered at this location\n");
375     return -1;
376   }
377
378
379   channel->irq =  15;
380
381   // Make model string
382   strncpy((char*)(drive->model_no), "V3VEE Ramdisk", 40);
383
384   while (strlen((char *)(drive->model_no)) < 40) {
385     strcat ((char*)(drive->model_no), " ");
386   }
387   
388   PrintDebug("CDROM on target %d/%d\n", busID, driveID);
389   
390   drive->device_type = IDE_CDROM;
391   drive->cdrom.locked = 0;
392   drive->sense.sense_key = SENSE_NONE;
393   drive->sense.asc = 0;
394   drive->sense.ascq = 0;
395   
396   drive->private_data = private_data;
397
398
399 #ifdef DEBUG_RAMDISK
400   if (check_bit_fields(controller) == INTR_REASON_BIT_ERR) {
401     PrintError("interrupt reason: bit field error\n");
402     return INTR_REASON_BIT_ERR;
403   }
404 #endif
405   
406   controller->sector_count = 0;
407
408   drive->cdrom.cd = cd;
409   
410   PrintDebug("\t\tCD on ata%d-%d: '%s'\n", 
411              busID, 
412              driveID, "");
413   
414   if(drive->cdrom.cd->insert_cdrom(drive->private_data)) {
415     PrintDebug("\t\tMedia present in CD-ROM drive\n");
416     drive->cdrom.ready = 1;
417     drive->cdrom.capacity = drive->cdrom.cd->capacity(drive->private_data);
418     PrintDebug("\t\tCDROM capacity is %d\n", drive->cdrom.capacity);
419   } else {                  
420     PrintDebug("\t\tCould not locate CD-ROM, continuing with media not present\n");
421     drive->cdrom.ready = 0;
422   }
423   
424   return 0;
425 }
426
427
428 static Bit32u rd_init_hardware(struct ramdisk_t *ramdisk) {
429   uint_t channel_num; 
430   uint_t device;
431   struct channel_t *channels = (struct channel_t *)(&(ramdisk->channels));
432
433   PrintDebug("[rd_init_harddrive]\n");
434
435   for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++) {
436     memset((char *)(channels + channel_num), 0, sizeof(struct channel_t));
437   }
438
439   for (channel_num = 0; channel_num < MAX_ATA_CHANNEL; channel_num++){
440     struct channel_t * channel = &(channels[channel_num]);
441
442     channel->ioaddr1 = 0x0;
443     channel->ioaddr2 = 0x0;
444     channel->irq = 0;
445
446     for (device = 0; device < 2; device++){
447       struct drive_t * drive = &(channel->drives[device]);
448       struct controller_t * controller = &(drive->controller);
449
450       controller->status.busy = 0;
451       controller->status.drive_ready = 1;
452       controller->status.write_fault = 0;
453       controller->status.seek_complete = 1;
454       controller->status.drq = 0;
455       controller->status.corrected_data = 0;
456       controller->status.index_pulse = 0;
457       controller->status.index_pulse_count = 0;
458       controller->status.err = 0;
459
460       controller->error_register = 0x01; // diagnostic code: no error
461       controller->head_no = 0;
462       controller->sector_count = 1;
463       controller->sector_no = 1;
464       controller->cylinder_no = 0;
465       controller->current_command = 0x00;
466       controller->buffer_index = 0;
467
468       controller->control.reset = 0;
469       controller->control.disable_irq = 0;
470       controller->reset_in_progress = 0;
471
472       controller->sectors_per_block = 0x80;
473       controller->lba_mode = 0;
474       
475       
476       controller->features = 0;
477         
478       // If not present
479       drive->device_type = IDE_NONE;
480
481       // Make model string
482       strncpy((char*)(drive->model_no), "", 40);
483       while(strlen((char *)(drive->model_no)) < 40) {
484         strcat ((char*)(drive->model_no), " ");
485       }
486
487     }//for device
488   }//for channel
489
490 #ifdef DEBUG_RAMDISK
491   rd_print_state(ramdisk);
492 #endif
493   return 0;
494 }
495
496
497 /*
498   static void rd_reset_harddrive(struct ramdisk_t *ramdisk, unsigned type) {
499   return;
500   }
501
502 */
503 static void rd_close_harddrive(struct ramdisk_t *ramdisk) {
504   return;
505 }
506
507
508 ////////////////////////////////////////////////////////////////////
509
510
511
512 static int read_data_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
513   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
514   struct channel_t * channel = NULL;
515   struct drive_t * drive = NULL;
516   struct controller_t * controller = NULL;
517
518
519
520   if (is_primary_port(ramdisk, port)) {
521     channel = &(ramdisk->channels[0]);
522   } else if (is_secondary_port(ramdisk, port)) {
523     channel = &(ramdisk->channels[1]);
524   } else {
525     PrintError("Invalid Port: %d\n", port);
526     return -1;
527   }
528   
529   drive = get_selected_drive(channel);
530   controller = &(drive->controller);
531
532
533   PrintTrace("[read_data_handler] IO Read at 0x%x, on drive %d/%d (current_cmd = 0x%02x)\n", 
534              port, 
535              get_channel_no(ramdisk, channel),
536              get_drive_no(channel, drive),
537              controller->current_command);
538
539   switch (controller->current_command) {
540   case 0xec:    // IDENTIFY DEVICE
541   case 0xa1:
542     {
543
544
545       controller->status.busy = 0;
546       controller->status.drive_ready = 1;
547       controller->status.write_fault = 0;
548       controller->status.seek_complete = 1;
549       controller->status.corrected_data = 0;
550       controller->status.err = 0;
551       
552       /*
553         value32 = controller->buffer[index];
554         index++;
555         
556         if (io_len >= 2) {
557         value32 |= (controller->buffer[index] << 8);
558         index++;
559         }
560         
561         if (io_len == 4) {
562         value32 |= (controller->buffer[index] << 16);
563         value32 |= (controller->buffer[index+1] << 24);
564         index += 2;
565         }
566         
567         controller->buffer_index = index;
568       */
569       /* JRL */
570       memcpy(dst, controller->buffer + controller->buffer_index, length);
571       controller->buffer_index += length;
572       
573       if (controller->buffer_index >= 512) {
574         controller->status.drq = 0;
575       }
576       
577       return length;
578     }
579   case 0xa0: //send packet cmd 
580     {
581       uint_t index = controller->buffer_index;
582
583       
584       PrintTrace("\t\tatapi.command(%02x), index(%d), cdrom.remaining_blocks(%d)\n", 
585                     drive->atapi.command, 
586                     index, 
587                     drive->cdrom.remaining_blocks);
588       
589       // Load block if necessary
590       if (index >= 2048) {
591         
592         if (index > 2048) {
593           PrintError("\t\tindex > 2048 : 0x%x\n", index);
594           return -1;
595         }
596         
597         switch (drive->atapi.command) {
598         case 0x28: // read (10)
599         case 0xa8: // read (12)
600           {
601     
602             if (!(drive->cdrom.ready)) {
603               PrintError("\t\tRead with CDROM not ready\n");
604               return -1;
605             } 
606             
607             drive->cdrom.cd->read_block(drive->private_data, controller->buffer,
608                                         drive->cdrom.next_lba);
609             drive->cdrom.next_lba++;
610             drive->cdrom.remaining_blocks--;
611             
612             
613             if (!(drive->cdrom.remaining_blocks)) {
614               PrintDebug("\t\tLast READ block loaded {CDROM}\n");
615             } else {
616               PrintDebug("\t\tREAD block loaded (%d remaining) {CDROM}\n",
617                          drive->cdrom.remaining_blocks);
618             }
619             
620             // one block transfered, start at beginning
621             index = 0;
622             break;
623           }
624         default: // no need to load a new block
625           break;
626         }
627       }
628     
629
630       /*
631         increment = 0;
632         value32 = controller->buffer[index + increment];
633         increment++;
634         
635         if (io_len >= 2) {
636         value32 |= (controller->buffer[index + increment] << 8);
637         increment++;
638         }
639         
640         if (io_len == 4) {
641         value32 |= (controller->buffer[index + increment] << 16);
642         value32 |= (controller->buffer[index + increment + 1] << 24);
643         increment += 2;
644         }
645
646         controller->buffer_index = index + increment;
647         controller->drq_index += increment;
648
649       */
650       /* JRL: CHECK THAT there is enough data in the buffer to copy.... */
651       {      
652         memcpy(dst, controller->buffer + index, length);
653         
654         controller->buffer_index  = index + length;
655         controller->drq_index += length;
656       }
657       
658       /* *** */
659       
660       if (controller->drq_index >= (unsigned)drive->atapi.drq_bytes) {
661         controller->status.drq = 0;
662         controller->drq_index = 0;
663         
664         drive->atapi.total_bytes_remaining -= drive->atapi.drq_bytes;
665         
666         if (drive->atapi.total_bytes_remaining > 0) {
667           // one or more blocks remaining (works only for single block commands)
668           
669           PrintDebug("\t\tPACKET drq bytes read\n");
670           controller->interrupt_reason.i_o = 1;
671           controller->status.busy = 0;
672           controller->status.drq = 1;
673           controller->interrupt_reason.c_d = 0;
674           
675           // set new byte count if last block
676           if (drive->atapi.total_bytes_remaining < controller->byte_count) {
677             controller->byte_count = drive->atapi.total_bytes_remaining;
678           }
679           drive->atapi.drq_bytes = controller->byte_count;
680           
681           rd_raise_interrupt(dev, channel);
682         } else {
683           // all bytes read
684           PrintDebug("\t\tPACKET all bytes read\n");
685           
686           controller->interrupt_reason.i_o = 1;
687           controller->interrupt_reason.c_d = 1;
688           controller->status.drive_ready = 1;
689           controller->interrupt_reason.rel = 0;
690           controller->status.busy = 0;
691           controller->status.drq = 0;
692           controller->status.err = 0;
693           
694           rd_raise_interrupt(dev, channel);
695         }
696       }
697       return length;
698       break;
699     }
700
701   default:
702     PrintError("\t\tunsupported command: %02x\n", controller->current_command);
703     break;
704   }
705
706   return -1;
707 }
708
709
710
711
712 static int write_data_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
713   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
714   struct channel_t * channel = NULL;
715   struct drive_t * drive = NULL;
716   struct controller_t * controller = NULL;
717
718   if (is_primary_port(ramdisk, port)) {
719     channel = &(ramdisk->channels[0]);
720   } else if (is_secondary_port(ramdisk, port)) {
721     channel = &(ramdisk->channels[1]);
722   } else {
723     PrintError("Invalid Port: %d\n", port);
724     return -1;
725   }
726   
727   drive = get_selected_drive(channel);
728   controller = &(drive->controller);
729
730   PrintDebug("[write_data_handler] IO write at 0x%x, current_cmd = 0x%02x\n", 
731              port, controller->current_command);
732   
733   switch (controller->current_command) {
734   case 0x30: // WRITE SECTORS
735     PrintError("\t\tneed to implement 0x30(write sector) to port 0x%x\n", port);
736     return -1;
737     
738   case 0xa0: // PACKET
739     
740     if (handle_atapi_packet_command(dev, channel, *(ushort_t *)src) == -1) {
741       PrintError("Error sending atapi packet command in PACKET write to data port\n");
742       return -1;
743     }
744
745     return length;
746     
747   default:
748     PrintError("\t\tIO write(0x%x): current command is %02xh\n", 
749                port, controller->current_command);
750
751     return -1;
752   }
753 }
754
755
756
757
758
759
760
761 static int read_status_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
762   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
763   struct channel_t * channel = NULL;
764   struct drive_t * drive = NULL;
765   struct controller_t * controller = NULL;
766
767
768   if (length != 1) {
769     PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
770     return -1;
771   }
772
773   if (is_primary_port(ramdisk, port)) {
774     channel = &(ramdisk->channels[0]);
775   } else if (is_secondary_port(ramdisk, port)) {
776     channel = &(ramdisk->channels[1]);
777   } else {
778     PrintError("Invalid Port: %d\n", port);
779     return -1;
780   }
781   
782   drive = get_selected_drive(channel);
783   controller = &(drive->controller);
784
785   PrintDebug("[read_status_handler] IO read at 0x%x, on drive %d/%d\n", 
786              port, get_channel_no(ramdisk, channel), 
787              channel->drive_select);
788
789
790   if (num_drives_on_channel(channel) == 0) {
791     PrintDebug("Setting value to zero because 0 devices on channel\n");
792     // (mch) Just return zero for these registers
793     memset(dst, 0, length);
794
795   } else {
796     uchar_t val = (
797                    (controller->status.busy << 7)            |
798                    (controller->status.drive_ready << 6)     |
799                    (controller->status.write_fault << 5)     |
800                    (controller->status.seek_complete << 4)   |
801                    (controller->status.drq << 3)             |
802                    (controller->status.corrected_data << 2)  |
803                    (controller->status.index_pulse << 1)     |
804                    (controller->status.err) );
805    
806     memcpy(dst, &val, length);
807
808     controller->status.index_pulse_count++;
809     controller->status.index_pulse = 0;
810     
811     if (controller->status.index_pulse_count >= INDEX_PULSE_CYCLE) {
812       controller->status.index_pulse = 1;
813       controller->status.index_pulse_count = 0;
814     }
815   }
816   
817   if ((port == SEC_CMD_PORT) || (port == PRI_CMD_PORT)) {
818     rd_lower_irq(dev, channel);
819   }
820   
821   return length;
822   
823 }
824
825
826 static int write_cmd_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
827   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
828   struct channel_t * channel = NULL;
829   struct drive_t * drive = NULL;
830   struct controller_t * controller = NULL;
831   uchar_t value = *(uchar_t *)src;
832
833   if (length != 1) {
834     PrintError("Invalid Command port write length: %d (port=%d)\n", length, port);
835     return -1;
836   }
837
838   if (is_primary_port(ramdisk, port)) {
839     channel = &(ramdisk->channels[0]);
840   } else if (is_secondary_port(ramdisk, port)) {
841     channel = &(ramdisk->channels[1]);
842   } else {
843     PrintError("Invalid Port: %d\n", port);
844     return -1;
845   }
846   
847   drive = get_selected_drive(channel);
848   controller = &(drive->controller);
849
850
851   PrintDebug("[write_command_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n", 
852              port, get_channel_no(ramdisk, channel), 
853              get_drive_no(channel, drive), 
854              value);
855
856   switch (value) {
857 #if 0
858   case 0xec: // IDENTIFY DEVICE
859     {
860
861       if (drive->device_type == IDE_NONE) {
862         PrintError("\t\tError: disk ata%d-%d not present, aborting\n", 
863                    get_channel_no(ramdisk, channel), 
864                    get_drive_no(channel, drive));
865         rd_command_aborted(dev, channel, value);
866         break;
867       } else if (drive->device_type == IDE_CDROM) {
868         PrintDebug("Identifying CDROM...Going to abort????\n");
869         controller->head_no        = 0;
870         controller->sector_count   = 1;
871         controller->sector_no      = 1;
872         controller->cylinder_no    = 0xeb14;
873         rd_command_aborted(dev, channel, 0xec);
874       } else {
875         PrintError("\t\tError: Want to identify HDD!!\n");
876         /*
877           SELECTED_CONTROLLER(channel).current_command = value;
878           SELECTED_CONTROLLER(channel).error_register = 0;
879           
880           // See ATA/ATAPI-4, 8.12
881           SELECTED_CONTROLLER(channel).status.busy  = 0;
882           SELECTED_CONTROLLER(channel).status.drive_ready = 1;
883           SELECTED_CONTROLLER(channel).status.write_fault = 0;
884           SELECTED_CONTROLLER(channel).status.drq   = 1;
885           SELECTED_CONTROLLER(channel).status.err   = 0;
886           
887           SELECTED_CONTROLLER(channel).status.seek_complete = 1;
888           SELECTED_CONTROLLER(channel).status.corrected_data = 0;
889           
890           SELECTED_CONTROLLER(channel).buffer_index = 0;
891           raise_interrupt(channel);
892           identify_drive(channel);
893         */
894       }
895
896     break;
897     }
898 #endif
899     // ATAPI commands
900   case 0xa1: // IDENTIFY PACKET DEVICE
901     {
902       if (drive->device_type == IDE_CDROM) {
903         controller->current_command = value;
904         controller->error_register = 0;
905         
906         controller->status.busy = 0;
907         controller->status.drive_ready = 1;
908         controller->status.write_fault = 0;
909         controller->status.drq   = 1;
910         controller->status.err   = 0;
911         
912         controller->status.seek_complete = 1;
913         controller->status.corrected_data = 0;
914         
915         controller->buffer_index = 0;
916         rd_raise_interrupt(dev, channel);
917         rd_identify_ATAPI_drive(dev, channel);
918       } else {
919         PrintError("Identifying non cdrom device not supported - ata %d/%d\n", 
920                    get_channel_no(ramdisk, channel),
921                    get_drive_no(channel, drive));
922         rd_command_aborted(dev, channel, 0xa1);
923       }
924       break;
925     }
926   case 0xa0: // SEND PACKET (atapi)
927     {
928       if (drive->device_type == IDE_CDROM) {
929         // PACKET
930         
931         if (controller->features & (1 << 0)) {
932           PrintError("\t\tPACKET-DMA not supported");
933           return -1;
934         }
935         
936         if (controller->features & (1 << 1)) {
937           PrintError("\t\tPACKET-overlapped not supported");
938           return -1;
939         }
940         
941         // We're already ready!
942         controller->sector_count = 1;
943         controller->status.busy = 0;
944         controller->status.write_fault = 0;
945
946         // serv bit??
947         controller->status.drq = 1;
948         controller->status.err = 0;
949         
950         // NOTE: no interrupt here
951         controller->current_command = value;
952         controller->buffer_index = 0;
953       } else {
954         PrintError("Sending packet to non cdrom device not supported\n");
955         rd_command_aborted (dev, channel, 0xa0);
956       }
957       break;
958     }
959   default:
960     PrintError("\t\tneed translate command %2x - ata %d\%d\n", value, 
961                get_channel_no(ramdisk, channel), 
962                get_drive_no(channel, drive));
963     //return -1;
964     /* JRL THIS NEEDS TO CHANGE */
965     return length;
966
967   }
968   return length;
969 }
970
971
972 static int write_ctrl_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
973   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
974   struct channel_t * channel = NULL;
975   struct drive_t * master_drive = NULL;
976   struct drive_t * slave_drive = NULL;
977   struct controller_t * controller = NULL;
978   uchar_t value = *(uchar_t *)src;
979   rd_bool prev_control_reset;
980
981   if (length != 1) {
982     PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
983     return -1;
984   }
985
986   if (is_primary_port(ramdisk, port)) {
987     channel = &(ramdisk->channels[0]);
988   } else if (is_secondary_port(ramdisk, port)) {
989     channel = &(ramdisk->channels[1]);
990   } else {
991     PrintError("Invalid Port: %d\n", port);
992     return -1;
993   }
994
995   master_drive = &(channel->drives[0]);
996   slave_drive = &(channel->drives[1]);
997
998   controller = &(get_selected_drive(channel)->controller);
999
1000
1001   PrintDebug("[write_control_handler] IO write at 0x%x, on drive %d/%d (val = 0x%x)\n", 
1002              port, get_channel_no(ramdisk, channel), 
1003              channel->drive_select, 
1004              value);
1005
1006   // (mch) Even if device 1 was selected, a write to this register
1007   // goes to device 0 (if device 1 is absent)
1008   
1009   prev_control_reset = controller->control.reset;
1010
1011   master_drive->controller.control.reset         = value & 0x04;
1012   slave_drive->controller.control.reset         = value & 0x04;
1013
1014   // CGS: was: SELECTED_CONTROLLER(channel).control.disable_irq    = value & 0x02;
1015   master_drive->controller.control.disable_irq = value & 0x02;
1016   slave_drive->controller.control.disable_irq = value & 0x02;
1017   
1018   PrintDebug("\t\tadpater control reg: reset controller = %d\n",
1019                 (unsigned) (controller->control.reset) ? 1 : 0);
1020   PrintDebug("\t\tadpater control reg: disable_irq(X) = %d\n",
1021                 (unsigned) (controller->control.disable_irq) ? 1 : 0);
1022   
1023   if ((!prev_control_reset) && (controller->control.reset)) {
1024     uint_t id = 0;
1025
1026     // transition from 0 to 1 causes all drives to reset
1027     PrintDebug("\t\thard drive: RESET\n");
1028     
1029     // (mch) Set BSY, drive not ready
1030     for (id = 0; id < 2; id++) {
1031       struct controller_t * ctrl = NULL;
1032
1033       if (id == 0) {
1034         ctrl = &(master_drive->controller);
1035       } else if (id == 1) {
1036         ctrl = &(slave_drive->controller);
1037       }
1038
1039       ctrl->status.busy           = 1;
1040       ctrl->status.drive_ready    = 0;
1041       ctrl->reset_in_progress     = 1;
1042       
1043       ctrl->status.write_fault    = 0;
1044       ctrl->status.seek_complete  = 1;
1045       ctrl->status.drq            = 0;
1046       ctrl->status.corrected_data = 0;
1047       ctrl->status.err            = 0;
1048       
1049       ctrl->error_register = 0x01; // diagnostic code: no error
1050       
1051       ctrl->current_command = 0x00;
1052       ctrl->buffer_index = 0;
1053       
1054       ctrl->sectors_per_block = 0x80;
1055       ctrl->lba_mode          = 0;
1056       
1057       ctrl->control.disable_irq = 0;
1058     }
1059
1060     rd_lower_irq(dev, channel);
1061
1062   } else if ((controller->reset_in_progress) &&
1063              (!controller->control.reset)) {
1064     uint_t id;
1065     // Clear BSY and DRDY
1066     PrintDebug("\t\tReset complete {%s}\n", device_type_to_str(get_selected_drive(channel)->device_type));
1067
1068     for (id = 0; id < 2; id++) {
1069       struct controller_t * ctrl = NULL;
1070       struct drive_t * drv = NULL;
1071
1072       if (id == 0) {
1073         ctrl = &(master_drive->controller);
1074         drv = master_drive;
1075       } else if (id == 1) {
1076         ctrl = &(slave_drive->controller);
1077         drv = slave_drive;
1078       }
1079
1080       ctrl->status.busy           = 0;
1081       ctrl->status.drive_ready    = 1;
1082       ctrl->reset_in_progress     = 0;
1083       
1084       // Device signature
1085       if (drv->device_type == IDE_DISK) {
1086         PrintDebug("\t\tdrive %d/%d is harddrive\n", get_channel_no(ramdisk, channel), id);
1087         ctrl->head_no        = 0;
1088         ctrl->sector_count   = 1;
1089         ctrl->sector_no      = 1;
1090         ctrl->cylinder_no    = 0;
1091       } else {
1092         ctrl->head_no        = 0;
1093         ctrl->sector_count   = 1;
1094         ctrl->sector_no      = 1;
1095         ctrl->cylinder_no    = 0xeb14;
1096       }
1097     }
1098   }
1099
1100   PrintDebug("\t\ts[0].controller.control.disable_irq = %02x\n", 
1101              master_drive->controller.control.disable_irq);
1102   PrintDebug("\t\ts[1].controller.control.disable_irq = %02x\n", 
1103              slave_drive->controller.control.disable_irq);
1104   return length;
1105 }
1106
1107
1108 static int read_general_port(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
1109   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
1110   struct channel_t * channel = NULL;
1111   struct drive_t * drive = NULL;
1112   struct controller_t * controller = NULL;
1113
1114
1115   if (length != 1) {
1116     PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1117     return -1;
1118   }
1119
1120   if (is_primary_port(ramdisk, port)) {
1121     channel = &(ramdisk->channels[0]);
1122   } else if (is_secondary_port(ramdisk, port)) {
1123     channel = &(ramdisk->channels[1]);
1124   } else {
1125     PrintError("Invalid Port: %d\n", port);
1126     return -1;
1127   }
1128   
1129   drive = get_selected_drive(channel);
1130   controller = &(drive->controller);
1131
1132
1133   PrintDebug("[read_general_handler] IO read addr at %x, on drive %d/%d, curcmd = %02x\n", 
1134              port, get_channel_no(ramdisk, channel), 
1135              channel->drive_select, 
1136              controller->current_command);
1137   
1138
1139   switch (port) {
1140   case PRI_FEATURES_PORT:
1141   case SEC_FEATURES_PORT: // hard disk error register 0x1f1
1142     {    
1143       uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->error_register;
1144       
1145       controller->status.err = 0;
1146       
1147       *(uchar_t *)dst = val;
1148       return length;
1149       
1150       break;
1151     }
1152
1153   case PRI_SECT_CNT_PORT:
1154   case SEC_SECT_CNT_PORT:  // hard disk sector count / interrupt reason 0x1f2
1155     {
1156       uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_count;
1157
1158       *(uchar_t *)dst = val;
1159       return length;
1160
1161       break;
1162     }
1163   case PRI_SECT_ADDR1_PORT:
1164   case SEC_SECT_ADDR1_PORT: // sector number 0x1f3
1165     {
1166       uchar_t val = (drive->device_type == IDE_NONE) ? 0 : controller->sector_no;
1167
1168       *(uchar_t *)dst = val;
1169       return length;
1170
1171       break;
1172     }
1173
1174   case PRI_SECT_ADDR2_PORT:
1175   case SEC_SECT_ADDR2_PORT:  // cylinder low 0x1f4  
1176     {
1177       // -- WARNING : On real hardware the controller registers are shared between drives. 
1178       // So we must respond even if the select device is not present. Some OS uses this fact 
1179       // to detect the disks.... minix2 for example
1180       uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no & 0x00ff);
1181
1182       *(uchar_t *)dst = val;
1183       return length;
1184
1185       break;      
1186   }
1187
1188   case PRI_SECT_ADDR3_PORT:
1189   case SEC_SECT_ADDR3_PORT: // cylinder high 0x1f5
1190     {
1191       // -- WARNING : On real hardware the controller registers are shared between drives. 
1192       // So we must respond even if the select device is not present. Some OS uses this fact 
1193       // to detect the disks.... minix2 for example
1194       uchar_t val = (num_drives_on_channel(channel) == 0) ? 0 : (controller->cylinder_no >> 8);
1195
1196       *(uchar_t *)dst = val;
1197       return length;
1198
1199       break;    
1200     }
1201   case PRI_DRV_SEL_PORT:
1202   case SEC_DRV_SEL_PORT:  // hard disk drive and head register 0x1f6
1203     {
1204       // b7 Extended data field for ECC
1205       // b6/b5: Used to be sector size.  00=256,01=512,10=1024,11=128
1206       //   Since 512 was always used, bit 6 was taken to mean LBA mode:
1207       //     b6 1=LBA mode, 0=CHS mode
1208       //     b5 1
1209       // b4: DRV
1210       // b3..0 HD3..HD0
1211       uchar_t val = ((1 << 7)                          |
1212                      ((controller->lba_mode > 0) << 6) |
1213                      (1 << 5)                          |            // 01b = 512 sector size
1214                      (channel->drive_select << 4)      |
1215                      (controller->head_no << 0));
1216       
1217       *(uchar_t *)dst = val;
1218       return length;
1219
1220       break;
1221     }
1222  case PRI_ADDR_REG_PORT:
1223  case SEC_ADDR_REG_PORT: // Hard Disk Address Register 0x3f7
1224    {
1225      // Obsolete and unsupported register.  Not driven by hard
1226      // disk controller.  Report all 1's.  If floppy controller
1227      // is handling this address, it will call this function
1228      // set/clear D7 (the only bit it handles), then return
1229      // the combined value
1230      *(uchar_t *)dst = 0xff;
1231      return length;
1232     }
1233
1234   default:
1235     PrintError("Invalid Port: %d\n", port);
1236     return -1;
1237   }
1238 }
1239
1240
1241
1242
1243 static int write_general_port(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
1244   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
1245   struct channel_t * channel = NULL;
1246   struct drive_t * drive = NULL;
1247   struct controller_t * controller = NULL;
1248   uchar_t value = *(uchar_t *)src;
1249
1250   if (length != 1) {
1251     PrintError("Invalid Status port read length: %d (port=%d)\n", length, port);
1252     return -1;
1253   }
1254
1255   if (is_primary_port(ramdisk, port)) {
1256     channel = &(ramdisk->channels[0]);
1257   } else if (is_secondary_port(ramdisk, port)) {
1258     channel = &(ramdisk->channels[1]);
1259   } else {
1260     PrintError("Invalid Port: %d\n", port);
1261     return -1;
1262   }
1263   
1264   drive = get_selected_drive(channel);
1265   controller = &(drive->controller);
1266
1267
1268   PrintDebug("[write_general_handler] IO write to port %x (val=0x%02x), channel = %d\n", 
1269              port,  value, get_channel_no(ramdisk, channel));
1270
1271   switch (port) {
1272
1273   case PRI_FEATURES_PORT:
1274   case SEC_FEATURES_PORT: // hard disk write precompensation 0x1f1
1275     {
1276       write_features(channel, value);
1277       break;
1278     }
1279   case PRI_SECT_CNT_PORT:
1280   case SEC_SECT_CNT_PORT: // hard disk sector count 0x1f2
1281     {
1282       write_sector_count(channel, value);
1283       break;
1284     }
1285   case PRI_SECT_ADDR1_PORT:
1286   case SEC_SECT_ADDR1_PORT: // hard disk sector number 0x1f3
1287     {
1288       write_sector_number(channel, value);
1289       break;
1290     }
1291   case PRI_SECT_ADDR2_PORT:
1292   case SEC_SECT_ADDR2_PORT: // hard disk cylinder low 0x1f4
1293     {
1294       write_cylinder_low(channel, value);
1295       break;
1296     }
1297   case PRI_SECT_ADDR3_PORT:
1298   case SEC_SECT_ADDR3_PORT: // hard disk cylinder high 0x1f5
1299     {
1300       write_cylinder_high(channel, value);
1301       break;
1302     }
1303   case PRI_DRV_SEL_PORT:
1304   case SEC_DRV_SEL_PORT: // hard disk drive and head register 0x1f6
1305     {
1306       // b7 Extended data field for ECC
1307       // b6/b5: Used to be sector size.  00=256,01=512,10=1024,11=128
1308       //   Since 512 was always used, bit 6 was taken to mean LBA mode:
1309       //     b6 1=LBA mode, 0=CHS mode
1310       //     b5 1
1311       // b4: DRV
1312       // b3..0 HD3..HD0
1313
1314       // 1x1xxxxx
1315
1316       PrintDebug("\tDrive Select value=%x\n", value);
1317
1318       if ((value & 0xa0) != 0xa0) { 
1319         PrintDebug("\t\tIO write 0x%x (%02x): not 1x1xxxxxb\n", port, (unsigned) value);
1320       }
1321       
1322       write_head_no(channel, value & 0xf);
1323       if ((controller->lba_mode == 0) && (((value >> 6) & 1) == 1)) {
1324         PrintDebug("\t\tenabling LBA mode\n");
1325       }
1326
1327       write_lba_mode(channel, (value >> 6) & 1);
1328
1329
1330
1331       if (drive->cdrom.cd) {
1332         PrintDebug("\t\tSetting LBA on CDROM: %d\n", (value >> 6) & 1);
1333         drive->cdrom.cd->set_LBA(drive->private_data, (value >> 6) & 1);
1334       }
1335       
1336
1337       channel->drive_select = (value >> 4) & 0x01;
1338       drive = get_selected_drive(channel);
1339
1340       if (drive->device_type == IDE_NONE) {
1341         PrintError("\t\tError: device set to %d which does not exist! channel = 0x%x\n",
1342                    channel->drive_select, get_channel_no(ramdisk, channel));
1343
1344         controller->error_register = 0x04; // aborted
1345         controller->status.err = 1;
1346       }
1347       
1348       break;
1349     }
1350   default:
1351     PrintError("\t\thard drive: io write to unhandled port 0x%x  (value = %c)\n", port, value);
1352     //return -1;
1353   }
1354
1355   return length;
1356 }
1357
1358
1359  
1360
1361
1362 static void rd_raise_interrupt(struct vm_device * dev, struct channel_t * channel) {
1363   //  struct ramdisk_t * ramdisk = (struct ramdisk_t *)(dev->private_data);
1364   struct drive_t * drive = get_selected_drive(channel);
1365   struct controller_t * controller = &(drive->controller);
1366
1367   PrintDebug("[raise_interrupt] disable_irq = 0x%02x\n", controller->control.disable_irq);
1368
1369   if (!(controller->control.disable_irq)) {
1370  
1371     PrintDebug("\t\tRaising interrupt %d {%s}\n\n", channel->irq, device_type_to_str(drive->device_type));
1372
1373     v3_raise_irq(dev->vm, channel->irq);
1374   } else {
1375     PrintError("\t\tRaising irq but irq is disabled\n");
1376   }
1377   
1378   return;
1379 }
1380
1381 static void rd_lower_irq(struct vm_device *dev, struct channel_t * channel) {
1382   PrintDebug("[lower_irq] irq = %d\n", channel->irq);
1383   v3_lower_irq(dev->vm, channel->irq);
1384 }
1385
1386
1387
1388
1389
1390
1391
1392 //////////////////////////////////////////////////////////////////////////
1393
1394 /*
1395  * ATAPI subroutines
1396  */
1397
1398
1399
1400 int handle_atapi_packet_command(struct vm_device * dev, struct channel_t * channel, ushort_t value) {
1401   struct ramdisk_t * ramdisk  = (struct ramdisk_t *)(dev->private_data);
1402   struct drive_t * drive = get_selected_drive(channel);
1403   struct controller_t * controller = &(drive->controller);
1404
1405   if (controller->buffer_index >= PACKET_SIZE) {
1406     PrintError("ATAPI packet exceeded maximum length: buffer_index (%d) >= PACKET_SIZE\n", 
1407                controller->buffer_index);
1408     return -1;
1409   }
1410
1411   controller->buffer[controller->buffer_index] = value;
1412   controller->buffer[controller->buffer_index + 1] = (value >> 8);
1413   controller->buffer_index += 2;
1414   
1415   
1416   /* if packet completely writtten */
1417   if (controller->buffer_index >= PACKET_SIZE) {
1418     // complete command received
1419     Bit8u atapi_command = controller->buffer[0];
1420     
1421     PrintDebug("\t\tcdrom: ATAPI command 0x%x started\n", atapi_command);
1422     
1423     switch (atapi_command) {
1424     case 0x00: // test unit ready
1425       {
1426         PrintDebug("Testing unit ready\n");
1427         if (drive->cdrom.ready) {
1428           rd_atapi_cmd_nop(dev, channel);
1429         } else {
1430           PrintError("CDROM not ready in test unit ready\n");
1431           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1432         }
1433         
1434         rd_raise_interrupt(dev, channel);
1435         
1436         break;
1437       }
1438     case 0x03:  // request sense
1439       {
1440         int alloc_length = controller->buffer[4];
1441
1442         if (rd_init_send_atapi_command(dev, channel, atapi_command, 18, alloc_length, false) == -1) {
1443           PrintError("Error sending atapi command in Request Sense\n");
1444           return -1;
1445         }
1446         
1447         // sense data
1448         controller->buffer[0] = 0x70 | (1 << 7);
1449         controller->buffer[1] = 0;
1450         controller->buffer[2] = drive->sense.sense_key;
1451         controller->buffer[3] = drive->sense.information.arr[0];
1452         controller->buffer[4] = drive->sense.information.arr[1];
1453         controller->buffer[5] = drive->sense.information.arr[2];
1454         controller->buffer[6] = drive->sense.information.arr[3];
1455         controller->buffer[7] = 17 - 7;
1456         controller->buffer[8] = drive->sense.specific_inf.arr[0];
1457         controller->buffer[9] = drive->sense.specific_inf.arr[1];
1458         controller->buffer[10] = drive->sense.specific_inf.arr[2];
1459         controller->buffer[11] = drive->sense.specific_inf.arr[3];
1460         controller->buffer[12] = drive->sense.asc;
1461         controller->buffer[13] = drive->sense.ascq;
1462         controller->buffer[14] = drive->sense.fruc;
1463         controller->buffer[15] = drive->sense.key_spec.arr[0];
1464         controller->buffer[16] = drive->sense.key_spec.arr[1];
1465         controller->buffer[17] = drive->sense.key_spec.arr[2];
1466         
1467         rd_ready_to_send_atapi(dev, channel);
1468         break;
1469       }
1470     case 0x1b:  // start stop unit
1471       {
1472         //bx_bool Immed = (controller->buffer[1] >> 0) & 1;
1473         rd_bool LoEj = (controller->buffer[4] >> 1) & 1;
1474         rd_bool Start = (controller->buffer[4] >> 0) & 1;
1475
1476         // stop the disc
1477         if ((!LoEj) && (!Start)) { 
1478           PrintError("FIXME: Stop disc not implemented\n");
1479
1480           rd_atapi_cmd_nop(dev, channel);
1481           rd_raise_interrupt(dev, channel);
1482
1483         } else if (!LoEj && Start) { // start (spin up) the disc
1484           
1485           drive->cdrom.cd->start_cdrom(drive->private_data);
1486           
1487           PrintError("FIXME: ATAPI start disc not reading TOC\n");
1488           rd_atapi_cmd_nop(dev, channel);
1489           rd_raise_interrupt(dev, channel);
1490
1491         } else if (LoEj && !Start) { // Eject the disc
1492           rd_atapi_cmd_nop(dev, channel);
1493           PrintDebug("Ejecting Disk\n");
1494           if (drive->cdrom.ready) {
1495             
1496             drive->cdrom.cd->eject_cdrom(drive->private_data);
1497             
1498             drive->cdrom.ready = 0;
1499             //bx_options.atadevice[channel][SLAVE_SELECTED(channel)].Ostatus->set(EJECTED);
1500             //bx_gui->update_drive_status_buttons();
1501           }
1502           rd_raise_interrupt(dev, channel);
1503
1504         } else { // Load the disc
1505           // My guess is that this command only closes the tray, that's a no-op for us
1506           rd_atapi_cmd_nop(dev, channel);
1507           rd_raise_interrupt(dev, channel);
1508         }
1509         break;
1510       }
1511     case 0xbd: // mechanism status
1512       {
1513         uint16_t alloc_length = rd_read_16bit(controller->buffer + 8);
1514         
1515         if (alloc_length == 0) {
1516           PrintError("Zero allocation length to MECHANISM STATUS not impl.\n");
1517           return -1;
1518         }
1519         
1520         if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, alloc_length, false) == -1) {
1521           PrintError("Error sending atapi command in mechanism status\n");
1522           return -1;
1523         }
1524         
1525         controller->buffer[0] = 0; // reserved for non changers
1526         controller->buffer[1] = 0; // reserved for non changers
1527         
1528         controller->buffer[2] = 0; // Current LBA (TODO!)
1529         controller->buffer[3] = 0; // Current LBA (TODO!)
1530         controller->buffer[4] = 0; // Current LBA (TODO!)
1531         
1532         controller->buffer[5] = 1; // one slot
1533         
1534         controller->buffer[6] = 0; // slot table length
1535         controller->buffer[7] = 0; // slot table length
1536         
1537         rd_ready_to_send_atapi(dev, channel);
1538         break;
1539       }
1540     case 0x5a:  // mode sense
1541       {
1542         uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1543         
1544         Bit8u PC = controller->buffer[2] >> 6;
1545         Bit8u PageCode = controller->buffer[2] & 0x3f;
1546         
1547         switch (PC) {
1548         case 0x0: // current values
1549           {
1550             switch (PageCode) {
1551             case 0x01: // error recovery
1552               {
1553                 
1554                 if (rd_init_send_atapi_command(dev, channel, atapi_command, sizeof(struct error_recovery_t) + 8, alloc_length, false) == -1) {
1555                   PrintError("Error sending atapi command in mode sense error recovery\n");
1556                   return -1;
1557                 }
1558                 
1559                 rd_init_mode_sense_single(dev, channel, &(drive->cdrom.current.error_recovery),
1560                                           sizeof(struct error_recovery_t));
1561                 rd_ready_to_send_atapi(dev, channel);
1562                 break;
1563               }
1564             case 0x2a: // CD-ROM capabilities & mech. status
1565               {
1566
1567                 if (rd_init_send_atapi_command(dev, channel, atapi_command, 28, alloc_length, false) == -1) {
1568                   PrintError("Error sending atapi command in CDROM caps/mech mode-sense\n");
1569                   return -1;
1570                 }
1571
1572                 rd_init_mode_sense_single(dev, channel, &(controller->buffer[8]), 28);
1573                 
1574                 controller->buffer[8] = 0x2a;
1575                 controller->buffer[9] = 0x12;
1576                 controller->buffer[10] = 0x00;
1577                 controller->buffer[11] = 0x00;
1578                 // Multisession, Mode 2 Form 2, Mode 2 Form 1
1579                 controller->buffer[12] = 0x70; 
1580                 controller->buffer[13] = (3 << 5);
1581                 controller->buffer[14] = (unsigned char) (1 |
1582                                                           (drive->cdrom.locked ? (1 << 1) : 0) |
1583                                                           (1 << 3) |
1584                                                           (1 << 5));
1585                 controller->buffer[15] = 0x00;
1586                 controller->buffer[16] = (706 >> 8) & 0xff;
1587                 controller->buffer[17] = 706 & 0xff;
1588                 controller->buffer[18] = 0;
1589                 controller->buffer[19] = 2;
1590                 controller->buffer[20] = (512 >> 8) & 0xff;
1591                 controller->buffer[21] = 512 & 0xff;
1592                 controller->buffer[22] = (706 >> 8) & 0xff;
1593                 controller->buffer[23] = 706 & 0xff;
1594                 controller->buffer[24] = 0;
1595                 controller->buffer[25] = 0;
1596                 controller->buffer[26] = 0;
1597                 controller->buffer[27] = 0;
1598                 rd_ready_to_send_atapi(dev, channel);
1599                 break;
1600               }
1601             case 0x0d: // CD-ROM
1602             case 0x0e: // CD-ROM audio control
1603             case 0x3f: // all
1604               {
1605                 PrintError("Ramdisk: cdrom: MODE SENSE (curr), code=%x not implemented yet\n",
1606                          PageCode);
1607                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1608                                    ASC_INV_FIELD_IN_CMD_PACKET);
1609                 rd_raise_interrupt(dev, channel);
1610                 break;
1611               }
1612             default:
1613               {
1614                 // not implemeted by this device
1615                 PrintError("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1616                               PC, PageCode);
1617                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1618                                    ASC_INV_FIELD_IN_CMD_PACKET);
1619                 rd_raise_interrupt(dev, channel);
1620                 break;
1621               }
1622             }
1623             break;
1624           }
1625         case 0x1: // changeable values
1626           {
1627             switch (PageCode) {
1628             case 0x01: // error recovery
1629             case 0x0d: // CD-ROM
1630             case 0x0e: // CD-ROM audio control
1631             case 0x2a: // CD-ROM capabilities & mech. status
1632             case 0x3f: // all
1633               {
1634                 PrintError("cdrom: MODE SENSE (chg), code=%x not implemented yet\n",
1635                            PageCode);
1636                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1637                                    ASC_INV_FIELD_IN_CMD_PACKET);
1638                 rd_raise_interrupt(dev, channel);
1639                 break;
1640               }
1641             default:
1642               {
1643                 // not implemeted by this device
1644                 PrintError("Changeable values of mode sense not supported by cdrom\n");
1645                 PrintDebug("\t\tcdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1646                            PC, PageCode);
1647                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1648                                    ASC_INV_FIELD_IN_CMD_PACKET);
1649                 rd_raise_interrupt(dev, channel);
1650                 break;
1651               }
1652             }
1653             break;
1654           }
1655         case 0x2: // default values
1656           {
1657             switch (PageCode) {
1658             case 0x01: // error recovery
1659             case 0x0d: // CD-ROM
1660             case 0x0e: // CD-ROM audio control
1661             case 0x2a: // CD-ROM capabilities & mech. status
1662             case 0x3f: // all
1663               PrintError("Default values of mode sense not supported by cdrom\n");
1664               PrintDebug("cdrom: MODE SENSE (dflt), code=%x\n",
1665                        PageCode);
1666               return -1;
1667               
1668             default:
1669               {
1670                 PrintError("Default values of mode sense not implemented in cdrom\n");
1671                 // not implemeted by this device
1672                 PrintDebug("cdrom: MODE SENSE PC=%x, PageCode=%x, not implemented by device\n",
1673                            PC, PageCode);
1674                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1675                                    ASC_INV_FIELD_IN_CMD_PACKET);
1676                 rd_raise_interrupt(dev, channel);
1677                 break;
1678               }
1679             }
1680             break;
1681           }
1682         case 0x3: // saved values not implemented
1683           {
1684             PrintError("\t\tSaved values not implemented in mode sense\n");
1685             rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1686             rd_raise_interrupt(dev, channel);
1687             break;
1688           }
1689         default:
1690           {
1691             PrintError("Unsupported Mode sense value\n");
1692             return -1;
1693             break;
1694           }
1695         }
1696         break;
1697       }
1698     case 0x12: // inquiry
1699       { 
1700         uint8_t alloc_length = controller->buffer[4];
1701         
1702         if (rd_init_send_atapi_command(dev, channel, atapi_command, 36, alloc_length, false) == -1) {
1703           PrintError("Error sending atapi command in inquiry\n");
1704           return -1;
1705         }
1706         
1707         controller->buffer[0] = 0x05; // CD-ROM
1708         controller->buffer[1] = 0x80; // Removable
1709         controller->buffer[2] = 0x00; // ISO, ECMA, ANSI version
1710         controller->buffer[3] = 0x21; // ATAPI-2, as specified
1711         controller->buffer[4] = 31; // additional length (total 36)
1712         controller->buffer[5] = 0x00; // reserved
1713         controller->buffer[6] = 0x00; // reserved
1714         controller->buffer[7] = 0x00; // reserved
1715         
1716         // Vendor ID
1717         const char* vendor_id = "VTAB    ";
1718         int i;
1719         for (i = 0; i < 8; i++) {
1720           controller->buffer[8+i] = vendor_id[i];
1721         }
1722
1723         // Product ID
1724         const char* product_id = "Turbo CD-ROM    ";
1725         for (i = 0; i < 16; i++) {
1726           controller->buffer[16+i] = product_id[i];
1727         }
1728
1729         // Product Revision level
1730         const char* rev_level = "1.0 "; 
1731         for (i = 0; i < 4; i++) {
1732           controller->buffer[32 + i] = rev_level[i];
1733         }
1734
1735         rd_ready_to_send_atapi(dev, channel);
1736         break;
1737       }
1738     case 0x25:  // read cd-rom capacity
1739       {
1740         // no allocation length???
1741         if (rd_init_send_atapi_command(dev, channel, atapi_command, 8, 8, false) == -1) {
1742           PrintError("Error sending atapi command in read cdrom capacity\n");
1743           return -1;
1744         }
1745         
1746         if (drive->cdrom.ready) {
1747           uint32_t capacity = drive->cdrom.capacity;
1748
1749           PrintDebug("\t\tCapacity is %d sectors (%d bytes)\n", capacity, capacity * 2048);
1750
1751           controller->buffer[0] = (capacity >> 24) & 0xff;
1752           controller->buffer[1] = (capacity >> 16) & 0xff;
1753           controller->buffer[2] = (capacity >> 8) & 0xff;
1754           controller->buffer[3] = (capacity >> 0) & 0xff;
1755           controller->buffer[4] = (2048 >> 24) & 0xff;
1756           controller->buffer[5] = (2048 >> 16) & 0xff;
1757           controller->buffer[6] = (2048 >> 8) & 0xff;
1758           controller->buffer[7] = (2048 >> 0) & 0xff;
1759
1760           rd_ready_to_send_atapi(dev, channel);
1761         } else {
1762           PrintError("CDROM not ready in read cdrom capacity\n");
1763           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1764           rd_raise_interrupt(dev, channel);
1765         }
1766         break;
1767       }
1768       
1769       
1770     case 0xbe:  // read cd
1771       {
1772         if (drive->cdrom.ready) {
1773           PrintError("Read CD with CD present not implemented\n");
1774           rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1775           rd_raise_interrupt(dev, channel);
1776         } else {
1777           PrintError("Drive not ready in read cd with CD present\n");
1778           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1779           rd_raise_interrupt(dev, channel);
1780         }
1781         break;
1782       }
1783     case 0x43: // read toc
1784       { 
1785         if (drive->cdrom.ready) {
1786           int toc_length;  
1787           bool msf = (controller->buffer[1] >> 1) & 1;
1788           uint8_t starting_track = controller->buffer[6];
1789           
1790           uint16_t alloc_length = rd_read_16bit(controller->buffer + 7);
1791           
1792           uint8_t format = (controller->buffer[9] >> 6);
1793           int i;
1794           switch (format) {
1795           case 0:
1796             {
1797               if (!(drive->cdrom.cd->read_toc(drive->private_data, controller->buffer,
1798                                               &toc_length, msf, starting_track))) {
1799                 PrintError("CDROM: Reading Table of Contents Failed\n");
1800                 rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1801                                    ASC_INV_FIELD_IN_CMD_PACKET);
1802                 rd_raise_interrupt(dev, channel);
1803                 break;
1804               }
1805               
1806               if (rd_init_send_atapi_command(dev, channel, atapi_command, toc_length, alloc_length, false) == -1) {
1807                 PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1808                 return -1;
1809               }
1810               
1811               rd_ready_to_send_atapi(dev, channel);
1812               
1813               break;
1814             }  
1815           case 1:
1816             // multi session stuff. we ignore this and emulate a single session only
1817             if (rd_init_send_atapi_command(dev, channel, atapi_command, 12, alloc_length, false) == -1) {
1818               PrintError("Failed to init send atapi command in read toc (fmt=%d)\n", format);
1819               return -1;
1820             }
1821             
1822             controller->buffer[0] = 0;
1823             controller->buffer[1] = 0x0a;
1824             controller->buffer[2] = 1;
1825             controller->buffer[3] = 1;
1826
1827             for (i = 0; i < 8; i++) {
1828               controller->buffer[4 + i] = 0;
1829             }
1830
1831             rd_ready_to_send_atapi(dev, channel);
1832             break;
1833             
1834           case 2:
1835           default:
1836             PrintError("(READ TOC) Format %d not supported\n", format);
1837             return -1;
1838           }
1839         } else {
1840           PrintError("CDROM not ready in read toc\n");
1841           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1842           rd_raise_interrupt(dev, channel);
1843         }
1844         break;
1845       }  
1846     case 0x28: // read (10)
1847     case 0xa8: // read (12)
1848       { 
1849         
1850         uint32_t transfer_length;
1851         if (atapi_command == 0x28) {
1852           transfer_length = rd_read_16bit(controller->buffer + 7);
1853         } else {
1854           transfer_length = rd_read_32bit(controller->buffer + 6);
1855         }
1856
1857         uint32_t lba = rd_read_32bit(controller->buffer + 2);
1858         
1859         if (!(drive->cdrom.ready)) {
1860           PrintError("CDROM Error: Not Ready (ATA%d/%d)\n", 
1861                      get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1862           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1863           rd_raise_interrupt(dev, channel);
1864           break;
1865         }
1866         
1867         if (transfer_length == 0) {
1868           PrintError("READ(%d) with transfer length 0, ok\n", 
1869                      (atapi_command == 0x28) ? 10 : 12);
1870           rd_atapi_cmd_nop(dev, channel);
1871           rd_raise_interrupt(dev, channel);
1872           break;
1873         }
1874         
1875         if (lba + transfer_length > drive->cdrom.capacity) {
1876           PrintError("CDROM Error: Capacity exceeded [capacity=%d] (ATA%d/%d)\n",
1877                      drive->cdrom.capacity,
1878                      get_channel_no(ramdisk, channel), get_drive_no(channel, drive));
1879           rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1880           rd_raise_interrupt(dev, channel);
1881           break;
1882         }
1883         
1884         PrintDebug("\t\tcdrom: READ (%d) LBA=%d LEN=%d\n", 
1885                    (atapi_command == 0x28) ? 10 : 12, 
1886                    lba, transfer_length);
1887         
1888         // handle command
1889         if (rd_init_send_atapi_command(dev, channel, atapi_command, transfer_length * 2048,
1890                                        transfer_length * 2048, true) == -1) {
1891           PrintError("CDROM Error: Atapi command send error\n");
1892           return -1;
1893         }
1894
1895         drive->cdrom.remaining_blocks = transfer_length;
1896         drive->cdrom.next_lba = lba;
1897         rd_ready_to_send_atapi(dev, channel);
1898         break;
1899       }
1900     case 0x2b:  // seek
1901       {
1902         uint32_t lba = rd_read_32bit(controller->buffer + 2);
1903
1904         if (!(drive->cdrom.ready)) {
1905           PrintError("CDROM not ready in seek\n");
1906           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1907           rd_raise_interrupt(dev, channel);
1908           break;
1909         }
1910         
1911         if (lba > drive->cdrom.capacity) {
1912           PrintError("LBA is greater than CDROM capacity in seek\n");
1913           rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1914           rd_raise_interrupt(dev, channel);
1915           break;
1916         }
1917         
1918         PrintError("\t\tcdrom: SEEK (ignored)\n");
1919
1920         rd_atapi_cmd_nop(dev, channel);
1921         rd_raise_interrupt(dev, channel);
1922
1923         break;
1924       }
1925     case 0x1e:  // prevent/allow medium removal
1926       {
1927
1928         if (drive->cdrom.ready) {
1929           drive->cdrom.locked = controller->buffer[4] & 1;
1930           rd_atapi_cmd_nop(dev, channel);
1931         } else {
1932           PrintError("CD not ready in prevent/allow medium removal\n");
1933           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1934         }
1935
1936         rd_raise_interrupt(dev, channel);
1937
1938         break;
1939       }
1940     case 0x42:  // read sub-channel
1941       {
1942         //bool msf = get_packet_field(channel,1, 1, 1);
1943         bool sub_q = get_packet_field(channel,2, 6, 1);
1944         //uint8_t data_format = get_packet_byte(channel,3);
1945         //uint8_t track_number = get_packet_byte(channel,6);
1946         uint16_t alloc_length = get_packet_word(channel,7);
1947         
1948
1949         /*
1950           UNUSED(msf);
1951           UNUSED(data_format);
1952           UNUSED(track_number);
1953         */
1954         if (!(drive->cdrom.ready)) {
1955           PrintError("CDROM not ready in read sub-channel\n");
1956           rd_atapi_cmd_error(dev, channel, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1957           rd_raise_interrupt(dev, channel);
1958         } else {
1959           controller->buffer[0] = 0;
1960           controller->buffer[1] = 0; // audio not supported
1961           controller->buffer[2] = 0;
1962           controller->buffer[3] = 0;
1963           
1964           int ret_len = 4; // header size
1965           
1966           if (sub_q) { // !sub_q == header only
1967             PrintError("Read sub-channel with SubQ not implemented\n");
1968             rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST,
1969                                ASC_INV_FIELD_IN_CMD_PACKET);
1970             rd_raise_interrupt(dev, channel);
1971           }
1972           
1973           if (rd_init_send_atapi_command(dev, channel, atapi_command, ret_len, alloc_length, false) == -1) {
1974             PrintError("Error sending atapi command in read sub-channel\n");
1975             return -1;
1976           }
1977           rd_ready_to_send_atapi(dev, channel);
1978         }
1979         break;
1980       }
1981     case 0x51:  // read disc info
1982       {
1983         // no-op to keep the Linux CD-ROM driver happy
1984         PrintError("Error: Read disk info no-op to keep the Linux CD-ROM driver happy\n");
1985         rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1986         rd_raise_interrupt(dev, channel);
1987         break;
1988       }
1989     case 0x55: // mode select
1990     case 0xa6: // load/unload cd
1991     case 0x4b: // pause/resume
1992     case 0x45: // play audio
1993     case 0x47: // play audio msf
1994     case 0xbc: // play cd
1995     case 0xb9: // read cd msf
1996     case 0x44: // read header
1997     case 0xba: // scan
1998     case 0xbb: // set cd speed
1999     case 0x4e: // stop play/scan
2000     case 0x46: // ???
2001     case 0x4a: // ???
2002       PrintError("ATAPI command 0x%x not implemented yet\n",
2003                  atapi_command);
2004       rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
2005       rd_raise_interrupt(dev, channel);
2006       break;
2007     default:
2008       PrintError("Unknown ATAPI command 0x%x (%d)\n",
2009                  atapi_command, atapi_command);
2010       // We'd better signal the error if the user chose to continue
2011       rd_atapi_cmd_error(dev, channel, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
2012       rd_raise_interrupt(dev, channel);
2013       break;
2014     }
2015   }
2016   return 0;
2017 }
2018
2019
2020
2021
2022 int rd_init_send_atapi_command(struct vm_device * dev, struct channel_t * channel, Bit8u command, int req_length, int alloc_length, bool lazy)
2023 {
2024   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2025   struct controller_t * controller = &(drive->controller);
2026
2027   // controller->byte_count is a union of controller->cylinder_no;
2028   // lazy is used to force a data read in the buffer at the next read.
2029   
2030   PrintDebug("[rd_init_send_atapi_cmd]\n");
2031
2032   if (controller->byte_count == 0xffff) {
2033     controller->byte_count = 0xfffe;
2034   }
2035
2036   if ((controller->byte_count & 1) && 
2037       !(alloc_length <= controller->byte_count)) {
2038       
2039     PrintDebug("\t\tOdd byte count (0x%04x) to ATAPI command 0x%02x, using 0x%x\n", 
2040                   controller->byte_count, 
2041                   command, 
2042                   controller->byte_count - 1);
2043     
2044     controller->byte_count -= 1;
2045   }
2046   
2047   if (controller->byte_count == 0) {
2048     PrintError("\t\tATAPI command with zero byte count\n");
2049     return -1;
2050   }
2051
2052   if (alloc_length < 0) {
2053     PrintError("\t\tAllocation length < 0\n");
2054     return -1;
2055   }
2056
2057   if (alloc_length == 0) {
2058     alloc_length = controller->byte_count;
2059   }
2060   
2061   controller->interrupt_reason.i_o = 1;
2062   controller->interrupt_reason.c_d = 0;
2063   controller->status.busy = 0;
2064   controller->status.drq = 1;
2065   controller->status.err = 0;
2066   
2067   // no bytes transfered yet
2068   if (lazy) {
2069     controller->buffer_index = 2048;
2070   } else {
2071     controller->buffer_index = 0;
2072   }
2073
2074   controller->drq_index = 0;
2075   
2076   if (controller->byte_count > req_length) {
2077     controller->byte_count = req_length;
2078   }
2079
2080   if (controller->byte_count > alloc_length) {
2081     controller->byte_count = alloc_length;
2082   }  
2083
2084   drive->atapi.command = command;
2085   drive->atapi.drq_bytes = controller->byte_count;
2086   drive->atapi.total_bytes_remaining = (req_length < alloc_length) ? req_length : alloc_length;
2087   
2088   // if (lazy) {
2089   // // bias drq_bytes and total_bytes_remaining
2090   // SELECTED_DRIVE(channel).atapi.drq_bytes += 2048;
2091   // SELECTED_DRIVE(channel).atapi.total_bytes_remaining += 2048;
2092   // }
2093
2094   return 0;
2095 }
2096
2097
2098
2099  void rd_ready_to_send_atapi(struct vm_device * dev, struct channel_t * channel) {
2100   PrintDebug("[rd_ready_to_send_atapi]\n");
2101
2102   rd_raise_interrupt(dev, channel);
2103 }
2104
2105
2106
2107
2108
2109 void rd_atapi_cmd_error(struct vm_device * dev, struct channel_t * channel, sense_t sense_key, asc_t asc)
2110 {
2111   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2112   struct controller_t * controller = &(drive->controller);
2113
2114
2115   struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2116   PrintError("[rd_atapi_cmd_error]\n");
2117   PrintError("Error: atapi_cmd_error channel=%02x key=%02x asc=%02x\n", 
2118              get_channel_no(ramdisk, channel), sense_key, asc);
2119   
2120
2121   controller->error_register = sense_key << 4;
2122   controller->interrupt_reason.i_o = 1;
2123   controller->interrupt_reason.c_d = 1;
2124   controller->interrupt_reason.rel = 0;
2125   controller->status.busy = 0;
2126   controller->status.drive_ready = 1;
2127   controller->status.write_fault = 0;
2128   controller->status.drq = 0;
2129   controller->status.err = 1;
2130   
2131   drive->sense.sense_key = sense_key;
2132   drive->sense.asc = asc;
2133   drive->sense.ascq = 0;
2134 }
2135
2136
2137
2138 void rd_atapi_cmd_nop(struct vm_device * dev, struct channel_t * channel)
2139 {
2140   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2141   struct controller_t * controller = &(drive->controller);
2142
2143   PrintDebug("[rd_atapi_cmd_nop]\n");
2144   controller->interrupt_reason.i_o = 1;
2145   controller->interrupt_reason.c_d = 1;
2146   controller->interrupt_reason.rel = 0;
2147   controller->status.busy = 0;
2148   controller->status.drive_ready = 1;
2149   controller->status.drq = 0;
2150   controller->status.err = 0;
2151 }
2152
2153
2154
2155
2156 void rd_identify_ATAPI_drive(struct vm_device * dev, struct channel_t * channel)
2157 {
2158   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2159   struct controller_t * controller = &(drive->controller);
2160
2161
2162   uint_t i;
2163   const char* serial_number = " VT00001\0\0\0\0\0\0\0\0\0\0\0\0";
2164   const char* firmware = "ALPHA1  ";
2165
2166   drive->id_drive[0] = (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0); // Removable CDROM, 50us response, 12 byte packets
2167
2168   for (i = 1; i <= 9; i++) {
2169     drive->id_drive[i] = 0;
2170   }
2171
2172   for (i = 0; i < 10; i++) {
2173     drive->id_drive[10 + i] = ((serial_number[i * 2] << 8) |
2174                                (serial_number[(i * 2) + 1]));
2175   }
2176
2177   for (i = 20; i <= 22; i++) {
2178     drive->id_drive[i] = 0;
2179   }
2180
2181   for (i = 0; i < strlen(firmware)/2; i++) {
2182     drive->id_drive[23 + i] = ((firmware[i * 2] << 8) |
2183                                (firmware[(i * 2) + 1]));
2184   }
2185   V3_ASSERT((23 + i) == 27);
2186   
2187   for (i = 0; i < strlen((char *)(drive->model_no)) / 2; i++) {
2188     drive->id_drive[27 + i] = ((drive->model_no[i * 2] << 8) |
2189                                (drive->model_no[(i * 2) + 1]));
2190   }
2191   V3_ASSERT((27 + i) == 47);
2192
2193   drive->id_drive[47] = 0;
2194   drive->id_drive[48] = 1; // 32 bits access
2195
2196   drive->id_drive[49] = (1 << 9); // LBA supported
2197
2198   drive->id_drive[50] = 0;
2199   drive->id_drive[51] = 0;
2200   drive->id_drive[52] = 0;
2201
2202   drive->id_drive[53] = 3; // words 64-70, 54-58 valid
2203
2204   for (i = 54; i <= 62; i++) {
2205     drive->id_drive[i] = 0;
2206   }
2207
2208   // copied from CFA540A
2209   drive->id_drive[63] = 0x0103; // variable (DMA stuff)
2210   drive->id_drive[64] = 0x0001; // PIO
2211   drive->id_drive[65] = 0x00b4;
2212   drive->id_drive[66] = 0x00b4;
2213   drive->id_drive[67] = 0x012c;
2214   drive->id_drive[68] = 0x00b4;
2215
2216   drive->id_drive[69] = 0;
2217   drive->id_drive[70] = 0;
2218   drive->id_drive[71] = 30; // faked
2219   drive->id_drive[72] = 30; // faked
2220   drive->id_drive[73] = 0;
2221   drive->id_drive[74] = 0;
2222
2223   drive->id_drive[75] = 0;
2224
2225   for (i = 76; i <= 79; i++) {
2226     drive->id_drive[i] = 0;
2227   }
2228
2229   drive->id_drive[80] = 0x1e; // supports up to ATA/ATAPI-4
2230   drive->id_drive[81] = 0;
2231   drive->id_drive[82] = 0;
2232   drive->id_drive[83] = 0;
2233   drive->id_drive[84] = 0;
2234   drive->id_drive[85] = 0;
2235   drive->id_drive[86] = 0;
2236   drive->id_drive[87] = 0;
2237   drive->id_drive[88] = 0;
2238
2239   for (i = 89; i <= 126; i++) {
2240     drive->id_drive[i] = 0;
2241   }
2242
2243   drive->id_drive[127] = 0;
2244   drive->id_drive[128] = 0;
2245
2246   for (i = 129; i <= 159; i++) {
2247     drive->id_drive[i] = 0;
2248   }
2249
2250   for (i = 160; i <= 255; i++) {
2251     drive->id_drive[i] = 0;
2252   }
2253
2254   // now convert the id_drive array (native 256 word format) to
2255   // the controller buffer (512 bytes)
2256   Bit16u temp16;
2257   for (i = 0; i <= 255; i++) {
2258     temp16 = drive->id_drive[i];
2259     controller->buffer[i * 2] = temp16 & 0x00ff;
2260     controller->buffer[i * 2 + 1] = temp16 >> 8;
2261   }
2262
2263   return;
2264 }
2265
2266
2267
2268
2269
2270
2271
2272 static 
2273 void rd_init_mode_sense_single(struct vm_device * dev, 
2274                                struct channel_t * channel, const void* src, int size)
2275 {
2276   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2277   struct controller_t * controller = &(drive->controller);
2278
2279   PrintDebug("[rd_init_mode_sense_single]\n");
2280
2281   // Header
2282   controller->buffer[0] = (size + 6) >> 8;
2283   controller->buffer[1] = (size + 6) & 0xff;
2284   controller->buffer[2] = 0x70; // no media present
2285   controller->buffer[3] = 0; // reserved
2286   controller->buffer[4] = 0; // reserved
2287   controller->buffer[5] = 0; // reserved
2288   controller->buffer[6] = 0; // reserved
2289   controller->buffer[7] = 0; // reserved
2290   
2291   // Data
2292   memcpy(controller->buffer + 8, src, size);
2293 }
2294
2295
2296
2297 static void rd_command_aborted(struct vm_device * dev, 
2298                                struct channel_t * channel, unsigned value) {
2299   struct drive_t * drive = &(channel->drives[channel->drive_select]);
2300   struct controller_t * controller = &(drive->controller);
2301
2302   PrintError("[rd_command_aborted]\n");
2303   PrintError("\t\taborting on command 0x%02x {%s}\n", value, device_type_to_str(drive->device_type));
2304
2305   controller->current_command = 0;
2306   controller->status.busy = 0;
2307   controller->status.drive_ready = 1;
2308   controller->status.err = 1;
2309   controller->error_register = 0x04; // command ABORTED
2310   controller->status.drq = 0;
2311   controller->status.seek_complete = 0;
2312   controller->status.corrected_data = 0;
2313   controller->buffer_index = 0;
2314
2315   rd_raise_interrupt(dev, channel);
2316 }
2317
2318
2319 static int ramdisk_init_device(struct vm_device *dev) {
2320   struct ramdisk_t *ramdisk= (struct ramdisk_t *)dev->private_data;
2321
2322   PrintDebug("Initializing Ramdisk\n");
2323
2324
2325   rd_init_hardware(ramdisk);
2326
2327
2328   dev_hook_io(dev, PRI_CTRL_PORT, 
2329               &read_status_port, &write_ctrl_port);
2330
2331   dev_hook_io(dev, PRI_DATA_PORT, 
2332               &read_data_port, &write_data_port);
2333   dev_hook_io(dev, PRI_FEATURES_PORT, 
2334               &read_general_port, &write_general_port);
2335   dev_hook_io(dev, PRI_SECT_CNT_PORT, 
2336               &read_general_port, &write_general_port);
2337   dev_hook_io(dev, PRI_SECT_ADDR1_PORT, 
2338               &read_general_port, &write_general_port);
2339   dev_hook_io(dev, PRI_SECT_ADDR2_PORT, 
2340               &read_general_port, &write_general_port);
2341   dev_hook_io(dev, PRI_SECT_ADDR3_PORT, 
2342               &read_general_port, &write_general_port);
2343   dev_hook_io(dev, PRI_DRV_SEL_PORT, 
2344               &read_general_port, &write_general_port);
2345   dev_hook_io(dev, PRI_CMD_PORT, 
2346               &read_status_port, &write_cmd_port);
2347
2348
2349   dev_hook_io(dev, SEC_CTRL_PORT, 
2350               &read_status_port, &write_ctrl_port);
2351
2352   dev_hook_io(dev, SEC_DATA_PORT, 
2353               &read_data_port, &write_data_port);
2354   dev_hook_io(dev, SEC_FEATURES_PORT, 
2355               &read_general_port, &write_general_port);
2356   dev_hook_io(dev, SEC_SECT_CNT_PORT, 
2357               &read_general_port, &write_general_port);
2358   dev_hook_io(dev, SEC_SECT_ADDR1_PORT, 
2359               &read_general_port, &write_general_port);
2360   dev_hook_io(dev, SEC_SECT_ADDR2_PORT, 
2361               &read_general_port, &write_general_port);
2362   dev_hook_io(dev, SEC_SECT_ADDR3_PORT, 
2363               &read_general_port, &write_general_port);
2364   dev_hook_io(dev, SEC_DRV_SEL_PORT, 
2365               &read_general_port, &write_general_port);
2366   dev_hook_io(dev, SEC_CMD_PORT, 
2367               &read_status_port, &write_cmd_port);
2368
2369   
2370
2371   dev_hook_io(dev, SEC_ADDR_REG_PORT, 
2372               &read_general_port, &write_general_port);
2373
2374   dev_hook_io(dev, PRI_ADDR_REG_PORT, 
2375               &read_general_port, &write_general_port);
2376
2377
2378
2379   return 0;
2380
2381 }
2382
2383
2384 static int ramdisk_deinit_device(struct vm_device *dev) {
2385   struct ramdisk_t *ramdisk = (struct ramdisk_t *)(dev->private_data);
2386   rd_close_harddrive(ramdisk);
2387   return 0;
2388 }
2389
2390 static struct vm_device_ops dev_ops = {
2391   .init = ramdisk_init_device,
2392   .deinit = ramdisk_deinit_device,
2393   .reset = NULL,
2394   .start = NULL,
2395   .stop = NULL,
2396 };
2397
2398
2399
2400
2401 struct vm_device *create_ramdisk()
2402 {
2403
2404   struct ramdisk_t *ramdisk;
2405   ramdisk = (struct ramdisk_t *)V3_Malloc(sizeof(struct ramdisk_t));  
2406   V3_ASSERT(ramdisk != NULL);  
2407
2408   PrintDebug("[create_ramdisk]\n");
2409
2410   struct vm_device *device = create_device("RAMDISK", &dev_ops, ramdisk);
2411
2412   return device;
2413 }
2414
2415
2416
2417
2418 #ifdef DEBUG_RAMDISK
2419
2420 static void rd_print_state(struct ramdisk_t * ramdisk) {
2421   uchar_t channel; 
2422   uchar_t device;
2423   struct channel_t * channels = (struct channel_t *)(&(ramdisk->channels));
2424
2425   /*
2426   for (channel = 0; channel < MAX_ATA_CHANNEL; channel++) {
2427     memset((char *)(channels + channel), 0, sizeof(struct channel_t));
2428   }
2429   */
2430   PrintDebug("sizeof(*channels) = %d\n", sizeof(*channels));
2431   PrintDebug("sizeof(channles->drives[0].controller) = %d\n", sizeof((channels->drives[0].controller)));
2432   PrintDebug("sizeof(channles->drives[0].cdrom) = %d\n", sizeof((channels->drives[0].cdrom)));
2433   PrintDebug("sizeof(channles->drives[0].sense) = %d\n", sizeof((channels->drives[0].sense)));
2434   PrintDebug("sizeof(channles->drives[0].atapi) = %d\n", sizeof((channels->drives[0].atapi)));
2435
2436
2437   PrintDebug("sizeof(channles->drives[0].controller.status) = %d\n", 
2438                 sizeof((channels->drives[0].controller.status)));
2439   PrintDebug("sizeof(channles->drives[0].controller.sector_count) = %d\n", 
2440                 sizeof((channels->drives[0].controller.sector_count)));
2441   PrintDebug("sizeof(channles->drives[0].controller.interrupt_reason) = %d\n", 
2442                 sizeof((channels->drives[0].controller.interrupt_reason)));
2443
2444   PrintDebug("sizeof(channles->drives[0].controller.cylinder_no) = %d\n", 
2445                 sizeof((channels->drives[0].controller.cylinder_no)));
2446   PrintDebug("sizeof(channles->drives[0].controller.byte_count) = %d\n", 
2447                 sizeof((channels->drives[0].controller.byte_count)));
2448
2449
2450   PrintDebug("sizeof(channles->drives[0].controller.control) = %d\n", 
2451                 sizeof((channels->drives[0].controller.control)));
2452
2453
2454   for (channel = 0; channel < MAX_ATA_CHANNEL; channel++){
2455   
2456     for (device = 0; device < 2; device++){
2457                   
2458       // Initialize controller state, even if device is not present
2459       PrintDebug("channels[%d].drives[%d].controller.status.busy = %d\n",
2460                     channel, device, 
2461                     channels[channel].drives[device].controller.status.busy);
2462       PrintDebug("channels[%d].drives[%d].controller.status.drive_ready = %d\n", 
2463                     channel, device, 
2464                     channels[channel].drives[device].controller.status.drive_ready);
2465       PrintDebug("channels[%d].drives[%d].controller.status.write_fault = %d\n", 
2466                     channel, device, 
2467                     channels[channel].drives[device].controller.status.write_fault);
2468       PrintDebug("channels[%d].drives[%d].controller.status.seek_complete = %d\n", 
2469                     channel, device, 
2470                     channels[channel].drives[device].controller.status.seek_complete);
2471       PrintDebug("channels[%d].drives[%d].controller.status.drq = %d\n", 
2472                     channel, device, 
2473                     channels[channel].drives[device].controller.status.drq);
2474       PrintDebug("channels[%d].drives[%d].controller.status.corrected_data = %d\n", 
2475                     channel, device, 
2476                     channels[channel].drives[device].controller.status.corrected_data);
2477       PrintDebug("channels[%d].drives[%d].controller.status.index_pulse = %d\n", 
2478                     channel, device, 
2479                     channels[channel].drives[device].controller.status.index_pulse);
2480       PrintDebug("channels[%d].drives[%d].controller.status.index_pulse_count = %d\n", 
2481                     channel, device, 
2482                     channels[channel].drives[device].controller.status.index_pulse_count);
2483       PrintDebug("channels[%d].drives[%d].controller.status.err = %d\n", 
2484                     channel, device, 
2485                     channels[channel].drives[device].controller.status.err);
2486
2487
2488       PrintDebug("channels[%d].drives[%d].controller.error_register = %d\n", 
2489                     channel, device, 
2490                     channels[channel].drives[device].controller.error_register);
2491       PrintDebug("channels[%d].drives[%d].controller.head_no = %d\n", 
2492                     channel, device, 
2493                     channels[channel].drives[device].controller.head_no);
2494       PrintDebug("channels[%d].drives[%d].controller.sector_count = %d\n", 
2495                     channel, device, 
2496                     channels[channel].drives[device].controller.sector_count);
2497       PrintDebug("channels[%d].drives[%d].controller.sector_no = %d\n", 
2498                     channel, device, 
2499                     channels[channel].drives[device].controller.sector_no);
2500       PrintDebug("channels[%d].drives[%d].controller.cylinder_no = %d\n", 
2501                     channel, device, 
2502                     channels[channel].drives[device].controller.cylinder_no);
2503       PrintDebug("channels[%d].drives[%d].controller.current_command = %02x\n", 
2504                     channel, device, 
2505                     channels[channel].drives[device].controller.current_command);
2506       PrintDebug("channels[%d].drives[%d].controller.buffer_index = %d\n", 
2507                     channel, device, 
2508                     channels[channel].drives[device].controller.buffer_index);
2509
2510
2511       PrintDebug("channels[%d].drives[%d].controller.control.reset = %d\n", 
2512                     channel, device, 
2513                     channels[channel].drives[device].controller.control.reset);
2514       PrintDebug("channels[%d].drives[%d].controller.control.disable_irq = %d\n", 
2515                     channel, device, 
2516                     channels[channel].drives[device].controller.control.disable_irq);
2517
2518
2519       PrintDebug("channels[%d].drives[%d].controller.reset_in_progress = %d\n", 
2520                     channel, device, 
2521                     channels[channel].drives[device].controller.reset_in_progress);
2522       PrintDebug("channels[%d].drives[%d].controller.sectors_per_block = %02x\n", 
2523                     channel, device, 
2524                     channels[channel].drives[device].controller.sectors_per_block); 
2525       PrintDebug("channels[%d].drives[%d].controller.lba_mode = %d\n", 
2526                     channel, device, 
2527                     channels[channel].drives[device].controller.lba_mode); 
2528       PrintDebug("channels[%d].drives[%d].controller.features = %d\n", 
2529                     channel, device, 
2530                     channels[channel].drives[device].controller.features); 
2531
2532
2533       PrintDebug("channels[%d].drives[%d].model_no = %s\n", 
2534                     channel, device, 
2535                     channels[channel].drives[device].model_no); 
2536       PrintDebug("channels[%d].drives[%d].device_type = %d\n", 
2537                     channel, device, 
2538                     channels[channel].drives[device].device_type); 
2539       PrintDebug("channels[%d].drives[%d].cdrom.locked = %d\n", 
2540                     channel, device, 
2541                     channels[channel].drives[device].cdrom.locked); 
2542       PrintDebug("channels[%d].drives[%d].sense.sense_key = %d\n", 
2543                     channel, device, 
2544                     channels[channel].drives[device].sense.sense_key); 
2545       PrintDebug("channels[%d].drives[%d].sense.asc = %d\n", 
2546                     channel, device, 
2547                     channels[channel].drives[device].sense.asc); 
2548       PrintDebug("channels[%d].drives[%d].sense.ascq = %d\n", 
2549                     channel, device, 
2550                     channels[channel].drives[device].sense.ascq); 
2551
2552
2553
2554       PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.c_d = %02x\n", 
2555                     channel, device, 
2556                     channels[channel].drives[device].controller.interrupt_reason.c_d);
2557
2558       PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.i_o = %02x\n", 
2559                     channel, device, 
2560                     channels[channel].drives[device].controller.interrupt_reason.i_o);
2561
2562       PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.rel = %02x\n", 
2563                     channel, device, 
2564                     channels[channel].drives[device].controller.interrupt_reason.rel);
2565
2566       PrintDebug("channels[%d].drives[%d].controller.interrupt_reason.tag = %02x\n", 
2567                     channel, device, 
2568                     channels[channel].drives[device].controller.interrupt_reason.tag);
2569
2570       PrintDebug("channels[%d].drives[%d].cdrom.ready = %d\n", 
2571                     channel, device, 
2572                     channels[channel].drives[device].cdrom.ready);
2573       
2574     }  //for device
2575   }  //for channel
2576   
2577   return;
2578 }
2579
2580 #if 0
2581 static void trace_info(ushort_t port, void *src, uint_t length) {
2582
2583   switch(port){
2584
2585   case 0x3e8:
2586     if (length == 1 && *((uchar_t*) src) == ATA_DETECT)
2587       PrintDebug("ata_detect()\n");
2588     break;
2589
2590   case 0x3e9:
2591     if (length == 1 && *((uchar_t*) src) == ATA_RESET)
2592       PrintDebug("ata_reset()\n");
2593     break;
2594
2595   case 0x3ea:
2596     if (length == 1 && *((uchar_t*) src) == ATA_CMD_DATA_IN)
2597       PrintDebug("ata_cmd_data_in()\n");
2598     break;
2599
2600   case 0x3eb:
2601     if (length == 1 && *((uchar_t*) src) == ATA_CMD_DATA_OUT)
2602       PrintDebug("ata_cmd_data_out()\n");
2603     break;
2604
2605   case 0x3ec:
2606     if (length == 1 && *((uchar_t*) src) == ATA_CMD_PACKET)
2607       PrintDebug("ata_cmd_packet()\n");
2608     break;
2609
2610   case 0x3ed:
2611     if (length == 1 && *((uchar_t*) src) == ATAPI_GET_SENSE)
2612       PrintDebug("atapi_get_sense()\n");
2613     break;
2614
2615   case 0x3ee:
2616     if (length == 1 && *((uchar_t*) src) == ATAPI_IS_READY)
2617       PrintDebug("atapi_is_ready()\n");
2618     break;
2619
2620   case 0x3ef:
2621     if (length == 1 && *((uchar_t*) src) == ATAPI_IS_CDROM)
2622       PrintDebug("atapi_is_cdrom()\n");
2623     break;
2624
2625
2626   case 0x2e8:
2627     if (length == 1 && *((uchar_t*) src) == CDEMU_INIT)
2628       PrintDebug("cdemu_init()\n");
2629     break;
2630
2631   case 0x2e9:
2632     if (length == 1 && *((uchar_t*) src) == CDEMU_ISACTIVE)
2633       PrintDebug("cdemu_isactive()\n");
2634     break;
2635
2636   case 0x2ea:
2637     if (length == 1 && *((uchar_t*) src) == CDEMU_EMULATED_DRIVE)
2638       PrintDebug("cdemu_emulated_drive()\n");
2639     break;
2640
2641   case 0x2eb:
2642     if (length == 1 && *((uchar_t*) src) == CDROM_BOOT)
2643       PrintDebug("cdrom_boot()\n");
2644     break;
2645
2646   case 0x2ec:
2647     if (length == 1 && *((uchar_t*) src) == HARD_DRIVE_POST)
2648       PrintDebug("ata_hard_drive_post()\n");
2649     break;
2650
2651   case 0x2ed:
2652     if (length == 1)
2653       PrintDebug("ata_device_no(%d)\n", *((uchar_t*) src));
2654     break;
2655
2656   case 0x2ee:
2657     if (length == 1)
2658       PrintDebug("ata_device_type(%d)\n", *((uchar_t*) src));
2659     break;
2660
2661   case 0x2ef:
2662     if (length == 1 && *((uchar_t*) src) == INT13_HARDDISK)
2663       PrintDebug("int13_harddrive()\n");
2664     break;
2665
2666   case 0x2f8:
2667     if (length == 1 && *((uchar_t*) src) == INT13_CDROM)
2668       PrintDebug("int13_cdrom()\n");
2669     break;
2670
2671   case 0x2f9:
2672     if (length == 1 && *((uchar_t*) src) == INT13_CDEMU)
2673       PrintDebug("int13_cdemu()\n");
2674     break;
2675
2676   case 0x2fa:
2677     if (length == 1 && *((uchar_t*) src) == INT13_ELTORITO)
2678       PrintDebug("int13_eltorito()\n");
2679     break;
2680
2681   case 0x2fb:
2682     if (length == 1 && *((uchar_t*) src) == INT13_DISKETTE_FUNCTION)
2683       PrintDebug("int13_diskette_function()\n");
2684     break;
2685
2686
2687   default:
2688     break;
2689   }
2690 }
2691
2692 #endif
2693
2694
2695 static int check_bit_fields(struct controller_t * controller) {
2696   //Check bit fields
2697   controller->sector_count = 0;
2698   controller->interrupt_reason.c_d = 1;
2699   if (controller->sector_count != 0x01) {
2700     return INTR_REASON_BIT_ERR;
2701   }
2702   
2703   controller->sector_count = 0;
2704   controller->interrupt_reason.i_o = 1;
2705   if (controller->sector_count != 0x02) {
2706     return INTR_REASON_BIT_ERR;
2707   }
2708   
2709   controller->sector_count = 0;
2710   controller->interrupt_reason.rel = 1;
2711   if (controller->sector_count != 0x04) {
2712     return INTR_REASON_BIT_ERR;
2713   }
2714   
2715   controller->sector_count = 0;
2716   controller->interrupt_reason.tag = 3;
2717   if (controller->sector_count != 0x18) {
2718     return INTR_REASON_BIT_ERR;
2719   }
2720   
2721   return 0;
2722 }
2723
2724 #endif