PATADiskDevice.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <Kernel/Devices/PATAChannel.h>
  2. #include <Kernel/Devices/PATADiskDevice.h>
  3. NonnullRefPtr<PATADiskDevice> PATADiskDevice::create(PATAChannel& channel, DriveType type, int major, int minor)
  4. {
  5. return adopt(*new PATADiskDevice(channel, type, major, minor));
  6. }
  7. PATADiskDevice::PATADiskDevice(PATAChannel& channel, DriveType type, int major, int minor)
  8. : DiskDevice(major, minor)
  9. , m_drive_type(type)
  10. , m_channel(channel)
  11. {
  12. }
  13. PATADiskDevice::~PATADiskDevice()
  14. {
  15. }
  16. const char* PATADiskDevice::class_name() const
  17. {
  18. return "PATADiskDevice";
  19. }
  20. bool PATADiskDevice::read_blocks(unsigned index, u16 count, u8* out)
  21. {
  22. if (m_channel.m_bus_master_base && m_channel.m_dma_enabled.resource() && !m_channel.m_force_pio.resource())
  23. return read_sectors_with_dma(index, count, out);
  24. return read_sectors(index, count, out);
  25. }
  26. bool PATADiskDevice::read_block(unsigned index, u8* out) const
  27. {
  28. return const_cast<PATADiskDevice*>(this)->read_blocks(index, 1, out);
  29. }
  30. bool PATADiskDevice::write_blocks(unsigned index, u16 count, const u8* data)
  31. {
  32. if (m_channel.m_bus_master_base && m_channel.m_dma_enabled.resource())
  33. return write_sectors_with_dma(index, count, data);
  34. for (unsigned i = 0; i < count; ++i) {
  35. if (!write_sectors(index + i, 1, data + i * 512))
  36. return false;
  37. }
  38. return true;
  39. }
  40. bool PATADiskDevice::write_block(unsigned index, const u8* data)
  41. {
  42. return write_blocks(index, 1, data);
  43. }
  44. void PATADiskDevice::set_drive_geometry(u16 cyls, u16 heads, u16 spt)
  45. {
  46. m_cylinders = cyls;
  47. m_heads = heads;
  48. m_sectors_per_track = spt;
  49. }
  50. bool PATADiskDevice::read_sectors_with_dma(u32 lba, u16 count, u8* outbuf)
  51. {
  52. return m_channel.ata_read_sectors_with_dma(lba, count, outbuf, is_slave());
  53. }
  54. bool PATADiskDevice::read_sectors(u32 start_sector, u16 count, u8* outbuf)
  55. {
  56. return m_channel.ata_read_sectors(start_sector, count, outbuf, is_slave());
  57. }
  58. bool PATADiskDevice::write_sectors_with_dma(u32 lba, u16 count, const u8* inbuf)
  59. {
  60. return m_channel.ata_write_sectors_with_dma(lba, count, inbuf, is_slave());
  61. }
  62. bool PATADiskDevice::write_sectors(u32 start_sector, u16 count, const u8* inbuf)
  63. {
  64. return m_channel.ata_write_sectors(start_sector, count, inbuf, is_slave());
  65. }
  66. bool PATADiskDevice::is_slave() const
  67. {
  68. return m_drive_type == DriveType::Slave;
  69. }