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.


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