home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / install / cdrom.c < prev    next >
C/C++ Source or Header  |  1997-11-05  |  4KB  |  181 lines

  1. #include <fcntl.h>
  2. #include <linux/types.h>
  3. #include <linux/cdrom.h>
  4. #include <sys/ioctl.h>
  5. #include <strings.h>
  6. #include <newt.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9.  
  10. #include "devices.h"
  11. #include "install.h"
  12. #include "log.h"
  13. #include "newt.h"
  14. #include "scsi.h"
  15. #include "windows.h"
  16.  
  17. #define CD_IDE 1
  18. #define CD_SCSI 2
  19. #define CD_OTHER 3
  20.  
  21. static struct { char * modname, * devname; } transTable[] = {
  22.     { "cm206", "cm206cd" },
  23.     { "sonycd535", "cdu535" },
  24.     { NULL, NULL }
  25. } ;
  26.  
  27.  
  28. #ifdef __i386__
  29. static int setupCDdevicePanel(int * type) {
  30.     newtComponent form, label, listbox, okay, cancel, answer;
  31.  
  32.     newtCenteredWindow(35, 11, "CDROM type");
  33.  
  34.     label = newtLabel(1, 1, "What type of CDROM do you have?");
  35.     listbox = newtListbox(10, 3, 0, NEWT_LISTBOX_RETURNEXIT);
  36.  
  37.     newtListboxAddEntry(listbox, "SCSI", (void *) CD_SCSI);
  38.     newtListboxAddEntry(listbox, "Other CDROM", (void *) CD_OTHER);
  39.     
  40.     okay = newtButton(5, 7, "Ok");
  41.     cancel = newtButton(20, 7, "Cancel");
  42.  
  43.     form = newtForm(NULL, NULL, 0);
  44.     newtFormAddComponents(form, label, listbox, okay, cancel, NULL);
  45.  
  46.     answer = newtRunForm(form);
  47.  
  48.     if (answer != cancel)
  49.     *type = (int) newtListboxGetCurrent(listbox);
  50.  
  51.     newtFormDestroy(form);
  52.     newtPopWindow();
  53.  
  54.     if (answer == cancel) return INST_CANCEL;
  55.  
  56.     return 0;
  57. }
  58. #endif /* __i386__ */
  59.  
  60. #ifndef __sparc__
  61. int findAtapi(char ** cddev) {
  62.     struct deviceInfo * ide;
  63.     int i;
  64.  
  65.     if (ideGetDevices(&ide)) return INST_ERROR;
  66.  
  67.     i = 0;
  68.     while (ide[i].deviceName) {
  69.     if (ide[i].type == DEVICE_CDROM) {
  70.         *cddev = ide[i].deviceName;
  71.         return 0;
  72.     }
  73.     i++;
  74.     }
  75.  
  76.     return INST_ERROR;
  77. }
  78. #endif /* ! __sparc__ */
  79.     
  80. int setupCDdevice(char ** cddev, struct driversLoaded ** dl) {
  81.     int type, rc = 0;
  82.     struct driversLoaded * d;
  83.     int i;
  84.     int done = 0;
  85.  
  86.     #ifndef __sparc__
  87.     /* Let's see if any CDROM's are already available */
  88.     if (!findAtapi(cddev)) {
  89.         logMessage("using device %s", *cddev);
  90.         done = 1;
  91.     }
  92.     #endif
  93.  
  94.     if (!done) {
  95.     #if defined(__sparc__) || defined(__alpha__)
  96.         /* It must be SCSI -- we'll give an error later if this is wrong */
  97.         setupSCSIInterfaces(1, dl);
  98.         *cddev = "scd0";
  99.         done =1;
  100.     #else
  101.         if (!findSCSIcdrom(cddev)) {
  102.         done = 1;
  103.         } else if (kickstart) {
  104.         setupSCSIInterfaces(1, dl);
  105.         if (!findSCSIcdrom(cddev)) done = 1;
  106.         }
  107.     #endif
  108.     }
  109.  
  110.     #ifdef __i386__
  111.     while (rc || !done) {
  112.         rc = setupCDdevicePanel(&type);
  113.         if (rc) return rc;
  114.  
  115.         switch (type) {
  116.           case CD_SCSI:
  117.         setupSCSIInterfaces(1, dl);
  118.         *cddev = "scd0";
  119.         done = 1;
  120.         break;
  121.  
  122.           case CD_OTHER:
  123.         rc = loadDeviceDriver(DRIVER_CDROM, dl, 0);
  124.         if (!rc) {
  125.             d = *dl;
  126.             *cddev = "bad_device";
  127.             while (d) {
  128.             if (d->type == DRIVER_CDROM) {
  129.                 *cddev = d->module;
  130.                 break;
  131.             }
  132.             d = d->next;
  133.             }
  134.  
  135.             for (i = 0; transTable[i].modname; i++) {
  136.             if (!strcmp(*cddev, transTable[i].modname)) {
  137.                 *cddev = transTable[i].devname;
  138.                 break;
  139.             }
  140.             }
  141.             done = 1;
  142.         }
  143.         break;
  144.         }
  145.     } 
  146.     #endif /* __i386__ */
  147.  
  148.     winStatus(35, 3, "CDROM", "Initializing CDROM...");
  149.     sleep(2);
  150.     newtPopWindow();
  151.  
  152.     return 0;
  153. }
  154.  
  155. int removeCDmodule(struct driversLoaded ** dl) {
  156.     /* this wil fail silently if no CD module has been loaded */
  157.     removeDeviceDriver(DRIVER_CDROM, dl);
  158.  
  159.     return 0;
  160. }
  161.  
  162. int findSCSIcdrom(char ** cddev) {
  163.     struct deviceInfo * scsi;
  164.     int i;
  165.  
  166.     if (!scsiDeviceAvailable()) return INST_ERROR;
  167.  
  168.     if (scsiGetDevices(&scsi)) return INST_ERROR;
  169.  
  170.     i = 0;
  171.     while (scsi[i].deviceName) {
  172.     if (scsi[i].type == DEVICE_CDROM) {
  173.         *cddev = scsi[i].deviceName;
  174.         return 0;
  175.     }
  176.     i++;
  177.     }
  178.  
  179.     return INST_ERROR;
  180. }
  181.