Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000 Jens Axboe <axboe@suse.de> |
| 3 | * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com> |
| 4 | * |
| 5 | * May be copied or modified under the terms of the GNU General Public |
| 6 | * License. See linux/COPYING for more information. |
| 7 | * |
| 8 | * Packet writing layer for ATAPI and SCSI CD-R, CD-RW, DVD-R, and |
| 9 | * DVD-RW devices. |
| 10 | * |
| 11 | */ |
| 12 | #ifndef __PKTCDVD_H |
| 13 | #define __PKTCDVD_H |
| 14 | |
| 15 | #include <linux/types.h> |
| 16 | |
| 17 | /* |
| 18 | * 1 for normal debug messages, 2 is very verbose. 0 to turn it off. |
| 19 | */ |
| 20 | #define PACKET_DEBUG 1 |
| 21 | |
| 22 | #define MAX_WRITERS 8 |
| 23 | |
| 24 | #define PKT_RB_POOL_SIZE 512 |
| 25 | |
| 26 | /* |
| 27 | * How long we should hold a non-full packet before starting data gathering. |
| 28 | */ |
| 29 | #define PACKET_WAIT_TIME (HZ * 5 / 1000) |
| 30 | |
| 31 | /* |
| 32 | * use drive write caching -- we need deferred error handling to be |
| 33 | * able to sucessfully recover with this option (drive will return good |
| 34 | * status as soon as the cdb is validated). |
| 35 | */ |
| 36 | #if defined(CONFIG_CDROM_PKTCDVD_WCACHE) |
| 37 | #define USE_WCACHING 1 |
| 38 | #else |
| 39 | #define USE_WCACHING 0 |
| 40 | #endif |
| 41 | |
| 42 | /* |
| 43 | * No user-servicable parts beyond this point -> |
| 44 | */ |
| 45 | |
| 46 | /* |
| 47 | * device types |
| 48 | */ |
| 49 | #define PACKET_CDR 1 |
| 50 | #define PACKET_CDRW 2 |
| 51 | #define PACKET_DVDR 3 |
| 52 | #define PACKET_DVDRW 4 |
| 53 | |
| 54 | /* |
| 55 | * flags |
| 56 | */ |
| 57 | #define PACKET_WRITABLE 1 /* pd is writable */ |
| 58 | #define PACKET_NWA_VALID 2 /* next writable address valid */ |
| 59 | #define PACKET_LRA_VALID 3 /* last recorded address valid */ |
| 60 | #define PACKET_MERGE_SEGS 4 /* perform segment merging to keep */ |
| 61 | /* underlying cdrom device happy */ |
| 62 | |
| 63 | /* |
| 64 | * Disc status -- from READ_DISC_INFO |
| 65 | */ |
| 66 | #define PACKET_DISC_EMPTY 0 |
| 67 | #define PACKET_DISC_INCOMPLETE 1 |
| 68 | #define PACKET_DISC_COMPLETE 2 |
| 69 | #define PACKET_DISC_OTHER 3 |
| 70 | |
| 71 | /* |
| 72 | * write type, and corresponding data block type |
| 73 | */ |
| 74 | #define PACKET_MODE1 1 |
| 75 | #define PACKET_MODE2 2 |
| 76 | #define PACKET_BLOCK_MODE1 8 |
| 77 | #define PACKET_BLOCK_MODE2 10 |
| 78 | |
| 79 | /* |
| 80 | * Last session/border status |
| 81 | */ |
| 82 | #define PACKET_SESSION_EMPTY 0 |
| 83 | #define PACKET_SESSION_INCOMPLETE 1 |
| 84 | #define PACKET_SESSION_RESERVED 2 |
| 85 | #define PACKET_SESSION_COMPLETE 3 |
| 86 | |
| 87 | #define PACKET_MCN "4a656e734178626f65323030300000" |
| 88 | |
| 89 | #undef PACKET_USE_LS |
| 90 | |
| 91 | #define PKT_CTRL_CMD_SETUP 0 |
| 92 | #define PKT_CTRL_CMD_TEARDOWN 1 |
| 93 | #define PKT_CTRL_CMD_STATUS 2 |
| 94 | |
| 95 | struct pkt_ctrl_command { |
| 96 | __u32 command; /* in: Setup, teardown, status */ |
| 97 | __u32 dev_index; /* in/out: Device index */ |
| 98 | __u32 dev; /* in/out: Device nr for cdrw device */ |
| 99 | __u32 pkt_dev; /* in/out: Device nr for packet device */ |
| 100 | __u32 num_devices; /* out: Largest device index + 1 */ |
| 101 | __u32 padding; /* Not used */ |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * packet ioctls |
| 106 | */ |
| 107 | #define PACKET_IOCTL_MAGIC ('X') |
| 108 | #define PACKET_CTRL_CMD _IOWR(PACKET_IOCTL_MAGIC, 1, struct pkt_ctrl_command) |
| 109 | |
| 110 | #ifdef __KERNEL__ |
| 111 | #include <linux/blkdev.h> |
| 112 | #include <linux/completion.h> |
| 113 | #include <linux/cdrom.h> |
| 114 | |
| 115 | struct packet_settings |
| 116 | { |
Phillip Susi | a460ad6 | 2006-02-04 23:27:44 -0800 | [diff] [blame^] | 117 | __u32 size; /* packet size in (512 byte) sectors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | __u8 fp; /* fixed packets */ |
| 119 | __u8 link_loss; /* the rest is specified |
| 120 | * as per Mt Fuji */ |
| 121 | __u8 write_type; |
| 122 | __u8 track_mode; |
| 123 | __u8 block_mode; |
| 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * Very crude stats for now |
| 128 | */ |
| 129 | struct packet_stats |
| 130 | { |
| 131 | unsigned long pkt_started; |
| 132 | unsigned long pkt_ended; |
| 133 | unsigned long secs_w; |
| 134 | unsigned long secs_rg; |
| 135 | unsigned long secs_r; |
| 136 | }; |
| 137 | |
| 138 | struct packet_cdrw |
| 139 | { |
| 140 | struct list_head pkt_free_list; |
| 141 | struct list_head pkt_active_list; |
| 142 | spinlock_t active_list_lock; /* Serialize access to pkt_active_list */ |
| 143 | struct task_struct *thread; |
| 144 | atomic_t pending_bios; |
| 145 | }; |
| 146 | |
| 147 | /* |
| 148 | * Switch to high speed reading after reading this many kilobytes |
| 149 | * with no interspersed writes. |
| 150 | */ |
| 151 | #define HI_SPEED_SWITCH 512 |
| 152 | |
| 153 | struct packet_iosched |
| 154 | { |
| 155 | atomic_t attention; /* Set to non-zero when queue processing is needed */ |
| 156 | int writing; /* Non-zero when writing, zero when reading */ |
| 157 | spinlock_t lock; /* Protecting read/write queue manipulations */ |
| 158 | struct bio *read_queue; |
| 159 | struct bio *read_queue_tail; |
| 160 | struct bio *write_queue; |
| 161 | struct bio *write_queue_tail; |
Peter Osterlund | 46c271b | 2005-06-23 00:10:02 -0700 | [diff] [blame] | 162 | sector_t last_write; /* The sector where the last write ended */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | int successive_reads; |
| 164 | }; |
| 165 | |
| 166 | /* |
| 167 | * 32 buffers of 2048 bytes |
| 168 | */ |
Peter Osterlund | 610827d | 2005-09-13 01:25:29 -0700 | [diff] [blame] | 169 | #if (PAGE_SIZE % CD_FRAMESIZE) != 0 |
| 170 | #error "PAGE_SIZE must be a multiple of CD_FRAMESIZE" |
| 171 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | #define PACKET_MAX_SIZE 32 |
| 173 | #define PAGES_PER_PACKET (PACKET_MAX_SIZE * CD_FRAMESIZE / PAGE_SIZE) |
| 174 | #define PACKET_MAX_SECTORS (PACKET_MAX_SIZE * CD_FRAMESIZE >> 9) |
| 175 | |
| 176 | enum packet_data_state { |
| 177 | PACKET_IDLE_STATE, /* Not used at the moment */ |
| 178 | PACKET_WAITING_STATE, /* Waiting for more bios to arrive, so */ |
| 179 | /* we don't have to do as much */ |
| 180 | /* data gathering */ |
| 181 | PACKET_READ_WAIT_STATE, /* Waiting for reads to fill in holes */ |
| 182 | PACKET_WRITE_WAIT_STATE, /* Waiting for the write to complete */ |
| 183 | PACKET_RECOVERY_STATE, /* Recover after read/write errors */ |
| 184 | PACKET_FINISHED_STATE, /* After write has finished */ |
| 185 | |
| 186 | PACKET_NUM_STATES /* Number of possible states */ |
| 187 | }; |
| 188 | |
| 189 | /* |
| 190 | * Information needed for writing a single packet |
| 191 | */ |
| 192 | struct pktcdvd_device; |
| 193 | |
| 194 | struct packet_data |
| 195 | { |
| 196 | struct list_head list; |
| 197 | |
| 198 | spinlock_t lock; /* Lock protecting state transitions and */ |
| 199 | /* orig_bios list */ |
| 200 | |
| 201 | struct bio *orig_bios; /* Original bios passed to pkt_make_request */ |
| 202 | struct bio *orig_bios_tail;/* that will be handled by this packet */ |
| 203 | int write_size; /* Total size of all bios in the orig_bios */ |
| 204 | /* list, measured in number of frames */ |
| 205 | |
| 206 | struct bio *w_bio; /* The bio we will send to the real CD */ |
| 207 | /* device once we have all data for the */ |
| 208 | /* packet we are going to write */ |
| 209 | sector_t sector; /* First sector in this packet */ |
| 210 | int frames; /* Number of frames in this packet */ |
| 211 | |
| 212 | enum packet_data_state state; /* Current state */ |
| 213 | atomic_t run_sm; /* Incremented whenever the state */ |
| 214 | /* machine needs to be run */ |
| 215 | long sleep_time; /* Set this to non-zero to make the state */ |
| 216 | /* machine run after this many jiffies. */ |
| 217 | |
| 218 | atomic_t io_wait; /* Number of pending IO operations */ |
| 219 | atomic_t io_errors; /* Number of read/write errors during IO */ |
| 220 | |
| 221 | struct bio *r_bios[PACKET_MAX_SIZE]; /* bios to use during data gathering */ |
| 222 | struct page *pages[PAGES_PER_PACKET]; |
| 223 | |
| 224 | int cache_valid; /* If non-zero, the data for the zone defined */ |
| 225 | /* by the sector variable is completely cached */ |
| 226 | /* in the pages[] vector. */ |
| 227 | |
| 228 | int id; /* ID number for debugging */ |
| 229 | struct pktcdvd_device *pd; |
| 230 | }; |
| 231 | |
| 232 | struct pkt_rb_node { |
| 233 | struct rb_node rb_node; |
| 234 | struct bio *bio; |
| 235 | }; |
| 236 | |
| 237 | struct packet_stacked_data |
| 238 | { |
| 239 | struct bio *bio; /* Original read request bio */ |
| 240 | struct pktcdvd_device *pd; |
| 241 | }; |
| 242 | #define PSD_POOL_SIZE 64 |
| 243 | |
| 244 | struct pktcdvd_device |
| 245 | { |
| 246 | struct block_device *bdev; /* dev attached */ |
| 247 | dev_t pkt_dev; /* our dev */ |
| 248 | char name[20]; |
| 249 | struct packet_settings settings; |
| 250 | struct packet_stats stats; |
| 251 | int refcnt; /* Open count */ |
| 252 | int write_speed; /* current write speed, kB/s */ |
| 253 | int read_speed; /* current read speed, kB/s */ |
| 254 | unsigned long offset; /* start offset */ |
| 255 | __u8 mode_offset; /* 0 / 8 */ |
| 256 | __u8 type; |
| 257 | unsigned long flags; |
| 258 | __u16 mmc3_profile; |
| 259 | __u32 nwa; /* next writable address */ |
| 260 | __u32 lra; /* last recorded address */ |
| 261 | struct packet_cdrw cdrw; |
| 262 | wait_queue_head_t wqueue; |
| 263 | |
| 264 | spinlock_t lock; /* Serialize access to bio_queue */ |
| 265 | struct rb_root bio_queue; /* Work queue of bios we need to handle */ |
| 266 | int bio_queue_size; /* Number of nodes in bio_queue */ |
| 267 | sector_t current_sector; /* Keep track of where the elevator is */ |
| 268 | atomic_t scan_queue; /* Set to non-zero when pkt_handle_queue */ |
| 269 | /* needs to be run. */ |
| 270 | mempool_t *rb_pool; /* mempool for pkt_rb_node allocations */ |
| 271 | |
| 272 | struct packet_iosched iosched; |
| 273 | struct gendisk *disk; |
| 274 | }; |
| 275 | |
| 276 | #endif /* __KERNEL__ */ |
| 277 | |
| 278 | #endif /* __PKTCDVD_H */ |