2 * Copyright (C) 2000 Jens Axboe <axboe@suse.de>
3 * Copyright (C) 2001-2004 Peter Osterlund <petero2@telia.com>
5 * May be copied or modified under the terms of the GNU General Public
6 * License. See linux/COPYING for more information.
8 * Packet writing layer for ATAPI and SCSI CD-R, CD-RW, DVD-R, and
9 * DVD-RW devices (aka an exercise in block layer masturbation)
12 * TODO: (circa order of when I will fix it)
13 * - Only able to write on CD-RW media right now.
14 * - check host application code on media and set it in write page
15 * - interface for UDF <-> packet to negotiate a new location when a write
17 * - handle OPC, especially for -RW media
19 * Theory of operation:
21 * We use a custom make_request_fn function that forwards reads directly to
22 * the underlying CD device. Write requests are either attached directly to
23 * a live packet_data object, or simply stored sequentially in a list for
24 * later processing by the kcdrwd kernel thread. This driver doesn't use
25 * any elevator functionally as defined by the elevator_s struct, but the
26 * underlying CD device uses a standard elevator.
28 * This strategy makes it possible to do very late merging of IO requests.
29 * A new bio sent to pkt_make_request can be merged with a live packet_data
30 * object even if the object is in the data gathering state.
32 *************************************************************************/
34 #define VERSION_CODE "v0.2.0a 2004-07-14 Jens Axboe (axboe@suse.de) and petero2@telia.com"
36 #include <linux/pktcdvd.h>
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/types.h>
40 #include <linux/kernel.h>
41 #include <linux/kthread.h>
42 #include <linux/errno.h>
43 #include <linux/spinlock.h>
44 #include <linux/file.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/miscdevice.h>
48 #include <linux/suspend.h>
49 #include <scsi/scsi_cmnd.h>
50 #include <scsi/scsi_ioctl.h>
52 #include <asm/uaccess.h>
55 #define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
57 #define DPRINTK(fmt, args...)
61 #define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
63 #define VPRINTK(fmt, args...)
66 #define MAX_SPEED 0xffff
68 #define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
70 static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
71 static struct proc_dir_entry *pkt_proc;
73 static struct semaphore ctl_mutex; /* Serialize open/close/setup/teardown */
74 static mempool_t *psd_pool;
77 static void pkt_bio_finished(struct pktcdvd_device *pd)
79 BUG_ON(atomic_read(&pd->cdrw.pending_bios) <= 0);
80 if (atomic_dec_and_test(&pd->cdrw.pending_bios)) {
81 VPRINTK("pktcdvd: queue empty\n");
82 atomic_set(&pd->iosched.attention, 1);
87 static void pkt_bio_destructor(struct bio *bio)
89 kfree(bio->bi_io_vec);
93 static struct bio *pkt_bio_alloc(int nr_iovecs)
95 struct bio_vec *bvl = NULL;
98 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
103 bvl = kmalloc(nr_iovecs * sizeof(struct bio_vec), GFP_KERNEL);
106 memset(bvl, 0, nr_iovecs * sizeof(struct bio_vec));
108 bio->bi_max_vecs = nr_iovecs;
109 bio->bi_io_vec = bvl;
110 bio->bi_destructor = pkt_bio_destructor;
121 * Allocate a packet_data struct
123 static struct packet_data *pkt_alloc_packet_data(void)
126 struct packet_data *pkt;
128 pkt = kmalloc(sizeof(struct packet_data), GFP_KERNEL);
131 memset(pkt, 0, sizeof(struct packet_data));
133 pkt->w_bio = pkt_bio_alloc(PACKET_MAX_SIZE);
137 for (i = 0; i < PAGES_PER_PACKET; i++) {
138 pkt->pages[i] = alloc_page(GFP_KERNEL);
142 for (i = 0; i < PAGES_PER_PACKET; i++)
143 clear_page(page_address(pkt->pages[i]));
145 spin_lock_init(&pkt->lock);
147 for (i = 0; i < PACKET_MAX_SIZE; i++) {
148 struct bio *bio = pkt_bio_alloc(1);
151 pkt->r_bios[i] = bio;
157 for (i = 0; i < PACKET_MAX_SIZE; i++) {
158 struct bio *bio = pkt->r_bios[i];
164 for (i = 0; i < PAGES_PER_PACKET; i++)
166 __free_page(pkt->pages[i]);
175 * Free a packet_data struct
177 static void pkt_free_packet_data(struct packet_data *pkt)
181 for (i = 0; i < PACKET_MAX_SIZE; i++) {
182 struct bio *bio = pkt->r_bios[i];
186 for (i = 0; i < PAGES_PER_PACKET; i++)
187 __free_page(pkt->pages[i]);
192 static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
194 struct packet_data *pkt, *next;
196 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
198 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
199 pkt_free_packet_data(pkt);
203 static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
205 struct packet_data *pkt;
207 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
208 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
209 spin_lock_init(&pd->cdrw.active_list_lock);
210 while (nr_packets > 0) {
211 pkt = pkt_alloc_packet_data();
213 pkt_shrink_pktlist(pd);
216 pkt->id = nr_packets;
218 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
224 static void *pkt_rb_alloc(int gfp_mask, void *data)
226 return kmalloc(sizeof(struct pkt_rb_node), gfp_mask);
229 static void pkt_rb_free(void *ptr, void *data)
234 static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
236 struct rb_node *n = rb_next(&node->rb_node);
239 return rb_entry(n, struct pkt_rb_node, rb_node);
242 static inline void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
244 rb_erase(&node->rb_node, &pd->bio_queue);
245 mempool_free(node, pd->rb_pool);
246 pd->bio_queue_size--;
247 BUG_ON(pd->bio_queue_size < 0);
251 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
253 static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
255 struct rb_node *n = pd->bio_queue.rb_node;
256 struct rb_node *next;
257 struct pkt_rb_node *tmp;
260 BUG_ON(pd->bio_queue_size > 0);
265 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
266 if (s <= tmp->bio->bi_sector)
275 if (s > tmp->bio->bi_sector) {
276 tmp = pkt_rbtree_next(tmp);
280 BUG_ON(s > tmp->bio->bi_sector);
285 * Insert a node into the pd->bio_queue rb tree.
287 static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
289 struct rb_node **p = &pd->bio_queue.rb_node;
290 struct rb_node *parent = NULL;
291 sector_t s = node->bio->bi_sector;
292 struct pkt_rb_node *tmp;
296 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
297 if (s < tmp->bio->bi_sector)
302 rb_link_node(&node->rb_node, parent, p);
303 rb_insert_color(&node->rb_node, &pd->bio_queue);
304 pd->bio_queue_size++;
308 * Add a bio to a single linked list defined by its head and tail pointers.
310 static inline void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
314 BUG_ON((*list_head) == NULL);
315 (*list_tail)->bi_next = bio;
318 BUG_ON((*list_head) != NULL);
325 * Remove and return the first bio from a single linked list defined by its
326 * head and tail pointers.
328 static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
332 if (*list_head == NULL)
336 *list_head = bio->bi_next;
337 if (*list_head == NULL)
345 * Send a packet_command to the underlying block device and
346 * wait for completion.
348 static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
350 char sense[SCSI_SENSE_BUFFERSIZE];
353 DECLARE_COMPLETION(wait);
356 q = bdev_get_queue(pd->bdev);
358 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? WRITE : READ,
361 rq->rq_disk = pd->bdev->bd_disk;
365 rq->data = cgc->buffer;
366 rq->data_len = cgc->buflen;
368 memset(sense, 0, sizeof(sense));
370 rq->flags |= REQ_BLOCK_PC | REQ_HARDBARRIER;
372 rq->flags |= REQ_QUIET;
373 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
374 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
375 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
378 rq->flags |= REQ_NOMERGE;
380 elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
381 generic_unplug_device(q);
382 wait_for_completion(&wait);
392 * A generic sense dump / resolve mechanism should be implemented across
393 * all ATAPI + SCSI devices.
395 static void pkt_dump_sense(struct packet_command *cgc)
397 static char *info[9] = { "No sense", "Recovered error", "Not ready",
398 "Medium error", "Hardware error", "Illegal request",
399 "Unit attention", "Data protect", "Blank check" };
401 struct request_sense *sense = cgc->sense;
404 for (i = 0; i < CDROM_PACKET_SIZE; i++)
405 printk(" %02x", cgc->cmd[i]);
409 printk("no sense\n");
413 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
415 if (sense->sense_key > 8) {
416 printk(" (INVALID)\n");
420 printk(" (%s)\n", info[sense->sense_key]);
424 * flush the drive cache to media
426 static int pkt_flush_cache(struct pktcdvd_device *pd)
428 struct packet_command cgc;
430 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
431 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
435 * the IMMED bit -- we default to not setting it, although that
436 * would allow a much faster close, this is safer
441 return pkt_generic_packet(pd, &cgc);
445 * speed is given as the normal factor, e.g. 4 for 4x
447 static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
449 struct packet_command cgc;
450 struct request_sense sense;
453 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
455 cgc.cmd[0] = GPCMD_SET_SPEED;
456 cgc.cmd[2] = (read_speed >> 8) & 0xff;
457 cgc.cmd[3] = read_speed & 0xff;
458 cgc.cmd[4] = (write_speed >> 8) & 0xff;
459 cgc.cmd[5] = write_speed & 0xff;
461 if ((ret = pkt_generic_packet(pd, &cgc)))
462 pkt_dump_sense(&cgc);
468 * Queue a bio for processing by the low-level CD device. Must be called
469 * from process context.
471 static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio, int high_prio_read)
473 spin_lock(&pd->iosched.lock);
474 if (bio_data_dir(bio) == READ) {
475 pkt_add_list_last(bio, &pd->iosched.read_queue,
476 &pd->iosched.read_queue_tail);
478 pd->iosched.high_prio_read = 1;
480 pkt_add_list_last(bio, &pd->iosched.write_queue,
481 &pd->iosched.write_queue_tail);
483 spin_unlock(&pd->iosched.lock);
485 atomic_set(&pd->iosched.attention, 1);
486 wake_up(&pd->wqueue);
490 * Process the queued read/write requests. This function handles special
491 * requirements for CDRW drives:
492 * - A cache flush command must be inserted before a read request if the
493 * previous request was a write.
494 * - Switching between reading and writing is slow, so don't it more often
496 * - Set the read speed according to current usage pattern. When only reading
497 * from the device, it's best to use the highest possible read speed, but
498 * when switching often between reading and writing, it's better to have the
499 * same read and write speeds.
500 * - Reads originating from user space should have higher priority than reads
501 * originating from pkt_gather_data, because some process is usually waiting
502 * on reads of the first kind.
504 static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
508 if (atomic_read(&pd->iosched.attention) == 0)
510 atomic_set(&pd->iosched.attention, 0);
512 q = bdev_get_queue(pd->bdev);
516 int reads_queued, writes_queued, high_prio_read;
518 spin_lock(&pd->iosched.lock);
519 reads_queued = (pd->iosched.read_queue != NULL);
520 writes_queued = (pd->iosched.write_queue != NULL);
522 pd->iosched.high_prio_read = 0;
523 high_prio_read = pd->iosched.high_prio_read;
524 spin_unlock(&pd->iosched.lock);
526 if (!reads_queued && !writes_queued)
529 if (pd->iosched.writing) {
530 if (high_prio_read || (!writes_queued && reads_queued)) {
531 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
532 VPRINTK("pktcdvd: write, waiting\n");
536 pd->iosched.writing = 0;
539 if (!reads_queued && writes_queued) {
540 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
541 VPRINTK("pktcdvd: read, waiting\n");
544 pd->iosched.writing = 1;
548 spin_lock(&pd->iosched.lock);
549 if (pd->iosched.writing) {
550 bio = pkt_get_list_first(&pd->iosched.write_queue,
551 &pd->iosched.write_queue_tail);
553 bio = pkt_get_list_first(&pd->iosched.read_queue,
554 &pd->iosched.read_queue_tail);
556 spin_unlock(&pd->iosched.lock);
561 if (bio_data_dir(bio) == READ)
562 pd->iosched.successive_reads += bio->bi_size >> 10;
564 pd->iosched.successive_reads = 0;
565 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
566 if (pd->read_speed == pd->write_speed) {
567 pd->read_speed = MAX_SPEED;
568 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
571 if (pd->read_speed != pd->write_speed) {
572 pd->read_speed = pd->write_speed;
573 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
577 atomic_inc(&pd->cdrw.pending_bios);
578 generic_make_request(bio);
583 * Special care is needed if the underlying block device has a small
584 * max_phys_segments value.
586 static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
588 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
590 * The cdrom device can handle one segment/frame
592 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
594 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
596 * We can handle this case at the expense of some extra memory
597 * copies during write operations
599 set_bit(PACKET_MERGE_SEGS, &pd->flags);
602 printk("pktcdvd: cdrom max_phys_segments too small\n");
608 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
610 static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs,
611 struct page *dst_page, int dst_offs)
613 unsigned int copy_size = CD_FRAMESIZE;
615 while (copy_size > 0) {
616 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
617 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
618 src_bvl->bv_offset + offs;
619 void *vto = page_address(dst_page) + dst_offs;
620 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
623 memcpy(vto, vfrom, len);
624 kunmap_atomic(vfrom, KM_USER0);
634 * Copy all data for this packet to pkt->pages[], so that
635 * a) The number of required segments for the write bio is minimized, which
636 * is necessary for some scsi controllers.
637 * b) The data can be used as cache to avoid read requests if we receive a
638 * new write request for the same zone.
640 static void pkt_make_local_copy(struct packet_data *pkt, struct page **pages, int *offsets)
644 /* Copy all data to pkt->pages[] */
647 for (f = 0; f < pkt->frames; f++) {
648 if (pages[f] != pkt->pages[p]) {
649 void *vfrom = kmap_atomic(pages[f], KM_USER0) + offsets[f];
650 void *vto = page_address(pkt->pages[p]) + offs;
651 memcpy(vto, vfrom, CD_FRAMESIZE);
652 kunmap_atomic(vfrom, KM_USER0);
653 pages[f] = pkt->pages[p];
656 BUG_ON(offsets[f] != offs);
658 offs += CD_FRAMESIZE;
659 if (offs >= PAGE_SIZE) {
660 BUG_ON(offs > PAGE_SIZE);
667 static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
669 struct packet_data *pkt = bio->bi_private;
670 struct pktcdvd_device *pd = pkt->pd;
676 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
677 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
680 atomic_inc(&pkt->io_errors);
681 if (atomic_dec_and_test(&pkt->io_wait)) {
682 atomic_inc(&pkt->run_sm);
683 wake_up(&pd->wqueue);
685 pkt_bio_finished(pd);
690 static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
692 struct packet_data *pkt = bio->bi_private;
693 struct pktcdvd_device *pd = pkt->pd;
699 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
701 pd->stats.pkt_ended++;
703 pkt_bio_finished(pd);
704 atomic_dec(&pkt->io_wait);
705 atomic_inc(&pkt->run_sm);
706 wake_up(&pd->wqueue);
711 * Schedule reads for the holes in a packet
713 static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
718 char written[PACKET_MAX_SIZE];
720 BUG_ON(!pkt->orig_bios);
722 atomic_set(&pkt->io_wait, 0);
723 atomic_set(&pkt->io_errors, 0);
725 if (pkt->cache_valid) {
726 VPRINTK("pkt_gather_data: zone %llx cached\n",
727 (unsigned long long)pkt->sector);
732 * Figure out which frames we need to read before we can write.
734 memset(written, 0, sizeof(written));
735 spin_lock(&pkt->lock);
736 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
737 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
738 int num_frames = bio->bi_size / CD_FRAMESIZE;
739 BUG_ON(first_frame < 0);
740 BUG_ON(first_frame + num_frames > pkt->frames);
741 for (f = first_frame; f < first_frame + num_frames; f++)
744 spin_unlock(&pkt->lock);
747 * Schedule reads for missing parts of the packet.
749 for (f = 0; f < pkt->frames; f++) {
753 bio = pkt->r_bios[f];
755 bio->bi_max_vecs = 1;
756 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
757 bio->bi_bdev = pd->bdev;
758 bio->bi_end_io = pkt_end_io_read;
759 bio->bi_private = pkt;
761 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
762 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
763 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
764 f, pkt->pages[p], offset);
765 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
768 atomic_inc(&pkt->io_wait);
770 pkt_queue_bio(pd, bio, 0);
775 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
776 frames_read, (unsigned long long)pkt->sector);
777 pd->stats.pkt_started++;
778 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
779 pd->stats.secs_w += pd->settings.size;
783 * Find a packet matching zone, or the least recently used packet if
786 static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
788 struct packet_data *pkt;
790 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
791 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
792 list_del_init(&pkt->list);
793 if (pkt->sector != zone)
794 pkt->cache_valid = 0;
801 static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
803 if (pkt->cache_valid) {
804 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
806 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
811 * recover a failed write, query for relocation if possible
813 * returns 1 if recovery is possible, or 0 if not
816 static int pkt_start_recovery(struct packet_data *pkt)
819 * FIXME. We need help from the file system to implement
824 struct request *rq = pkt->rq;
825 struct pktcdvd_device *pd = rq->rq_disk->private_data;
826 struct block_device *pkt_bdev;
827 struct super_block *sb = NULL;
828 unsigned long old_block, new_block;
831 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
833 sb = get_super(pkt_bdev);
840 if (!sb->s_op || !sb->s_op->relocate_blocks)
843 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
844 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
847 new_sector = new_block * (CD_FRAMESIZE >> 9);
848 pkt->sector = new_sector;
850 pkt->bio->bi_sector = new_sector;
851 pkt->bio->bi_next = NULL;
852 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
853 pkt->bio->bi_idx = 0;
855 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
856 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
857 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
858 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
859 BUG_ON(pkt->bio->bi_private != pkt);
870 static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
873 static const char *state_name[] = {
874 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
876 enum packet_data_state old_state = pkt->state;
877 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
878 state_name[old_state], state_name[state]);
884 * Scan the work queue to see if we can start a new packet.
885 * returns non-zero if any work was done.
887 static int pkt_handle_queue(struct pktcdvd_device *pd)
889 struct packet_data *pkt, *p;
890 struct bio *bio = NULL;
891 sector_t zone = 0; /* Suppress gcc warning */
892 struct pkt_rb_node *node, *first_node;
895 VPRINTK("handle_queue\n");
897 atomic_set(&pd->scan_queue, 0);
899 if (list_empty(&pd->cdrw.pkt_free_list)) {
900 VPRINTK("handle_queue: no pkt\n");
905 * Try to find a zone we are not already working on.
907 spin_lock(&pd->lock);
908 first_node = pkt_rbtree_find(pd, pd->current_sector);
910 n = rb_first(&pd->bio_queue);
912 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
917 zone = ZONE(bio->bi_sector, pd);
918 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
919 if (p->sector == zone)
924 node = pkt_rbtree_next(node);
926 n = rb_first(&pd->bio_queue);
928 node = rb_entry(n, struct pkt_rb_node, rb_node);
930 if (node == first_node)
933 spin_unlock(&pd->lock);
935 VPRINTK("handle_queue: no bio\n");
939 pkt = pkt_get_packet_data(pd, zone);
942 pd->current_sector = zone + pd->settings.size;
944 pkt->frames = pd->settings.size >> 2;
945 BUG_ON(pkt->frames > PACKET_MAX_SIZE);
949 * Scan work queue for bios in the same zone and link them
952 spin_lock(&pd->lock);
953 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
954 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
956 VPRINTK("pkt_handle_queue: found zone=%llx\n",
957 (unsigned long long)ZONE(bio->bi_sector, pd));
958 if (ZONE(bio->bi_sector, pd) != zone)
960 pkt_rbtree_erase(pd, node);
961 spin_lock(&pkt->lock);
962 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
963 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
964 spin_unlock(&pkt->lock);
966 spin_unlock(&pd->lock);
968 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
969 pkt_set_state(pkt, PACKET_WAITING_STATE);
970 atomic_set(&pkt->run_sm, 1);
972 spin_lock(&pd->cdrw.active_list_lock);
973 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
974 spin_unlock(&pd->cdrw.active_list_lock);
980 * Assemble a bio to write one packet and queue the bio for processing
981 * by the underlying block device.
983 static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
986 struct page *pages[PACKET_MAX_SIZE];
987 int offsets[PACKET_MAX_SIZE];
991 for (f = 0; f < pkt->frames; f++) {
992 pages[f] = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
993 offsets[f] = (f * CD_FRAMESIZE) % PAGE_SIZE;
997 * Fill-in pages[] and offsets[] with data from orig_bios.
1000 spin_lock(&pkt->lock);
1001 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1002 int segment = bio->bi_idx;
1004 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1005 int num_frames = bio->bi_size / CD_FRAMESIZE;
1006 BUG_ON(first_frame < 0);
1007 BUG_ON(first_frame + num_frames > pkt->frames);
1008 for (f = first_frame; f < first_frame + num_frames; f++) {
1009 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1011 while (src_offs >= src_bvl->bv_len) {
1012 src_offs -= src_bvl->bv_len;
1014 BUG_ON(segment >= bio->bi_vcnt);
1015 src_bvl = bio_iovec_idx(bio, segment);
1018 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1019 pages[f] = src_bvl->bv_page;
1020 offsets[f] = src_bvl->bv_offset + src_offs;
1022 pkt_copy_bio_data(bio, segment, src_offs,
1023 pages[f], offsets[f]);
1025 src_offs += CD_FRAMESIZE;
1029 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1030 spin_unlock(&pkt->lock);
1032 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1033 frames_write, (unsigned long long)pkt->sector);
1034 BUG_ON(frames_write != pkt->write_size);
1036 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1037 pkt_make_local_copy(pkt, pages, offsets);
1038 pkt->cache_valid = 1;
1040 pkt->cache_valid = 0;
1043 /* Start the write request */
1044 bio_init(pkt->w_bio);
1045 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1046 pkt->w_bio->bi_sector = pkt->sector;
1047 pkt->w_bio->bi_bdev = pd->bdev;
1048 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1049 pkt->w_bio->bi_private = pkt;
1050 for (f = 0; f < pkt->frames; f++) {
1051 if ((f + 1 < pkt->frames) && (pages[f + 1] == pages[f]) &&
1052 (offsets[f + 1] = offsets[f] + CD_FRAMESIZE)) {
1053 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE * 2, offsets[f]))
1057 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE, offsets[f]))
1061 VPRINTK("pktcdvd: vcnt=%d\n", pkt->w_bio->bi_vcnt);
1063 atomic_set(&pkt->io_wait, 1);
1064 pkt->w_bio->bi_rw = WRITE;
1065 pkt_queue_bio(pd, pkt->w_bio, 0);
1068 static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1070 struct bio *bio, *next;
1073 pkt->cache_valid = 0;
1075 /* Finish all bios corresponding to this packet */
1076 bio = pkt->orig_bios;
1078 next = bio->bi_next;
1079 bio->bi_next = NULL;
1080 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1083 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1086 static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1090 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1093 switch (pkt->state) {
1094 case PACKET_WAITING_STATE:
1095 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1098 pkt->sleep_time = 0;
1099 pkt_gather_data(pd, pkt);
1100 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1103 case PACKET_READ_WAIT_STATE:
1104 if (atomic_read(&pkt->io_wait) > 0)
1107 if (atomic_read(&pkt->io_errors) > 0) {
1108 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1110 pkt_start_write(pd, pkt);
1114 case PACKET_WRITE_WAIT_STATE:
1115 if (atomic_read(&pkt->io_wait) > 0)
1118 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1119 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1121 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1125 case PACKET_RECOVERY_STATE:
1126 if (pkt_start_recovery(pkt)) {
1127 pkt_start_write(pd, pkt);
1129 VPRINTK("No recovery possible\n");
1130 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1134 case PACKET_FINISHED_STATE:
1135 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1136 pkt_finish_packet(pkt, uptodate);
1146 static void pkt_handle_packets(struct pktcdvd_device *pd)
1148 struct packet_data *pkt, *next;
1150 VPRINTK("pkt_handle_packets\n");
1153 * Run state machine for active packets
1155 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1156 if (atomic_read(&pkt->run_sm) > 0) {
1157 atomic_set(&pkt->run_sm, 0);
1158 pkt_run_state_machine(pd, pkt);
1163 * Move no longer active packets to the free list
1165 spin_lock(&pd->cdrw.active_list_lock);
1166 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1167 if (pkt->state == PACKET_FINISHED_STATE) {
1168 list_del(&pkt->list);
1169 pkt_put_packet_data(pd, pkt);
1170 pkt_set_state(pkt, PACKET_IDLE_STATE);
1171 atomic_set(&pd->scan_queue, 1);
1174 spin_unlock(&pd->cdrw.active_list_lock);
1177 static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1179 struct packet_data *pkt;
1182 for (i = 0; i <= PACKET_NUM_STATES; i++)
1185 spin_lock(&pd->cdrw.active_list_lock);
1186 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1187 states[pkt->state]++;
1189 spin_unlock(&pd->cdrw.active_list_lock);
1193 * kcdrwd is woken up when writes have been queued for one of our
1194 * registered devices
1196 static int kcdrwd(void *foobar)
1198 struct pktcdvd_device *pd = foobar;
1199 struct packet_data *pkt;
1200 long min_sleep_time, residue;
1202 set_user_nice(current, -20);
1205 DECLARE_WAITQUEUE(wait, current);
1208 * Wait until there is something to do
1210 add_wait_queue(&pd->wqueue, &wait);
1212 set_current_state(TASK_INTERRUPTIBLE);
1214 /* Check if we need to run pkt_handle_queue */
1215 if (atomic_read(&pd->scan_queue) > 0)
1218 /* Check if we need to run the state machine for some packet */
1219 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1220 if (atomic_read(&pkt->run_sm) > 0)
1224 /* Check if we need to process the iosched queues */
1225 if (atomic_read(&pd->iosched.attention) != 0)
1228 /* Otherwise, go to sleep */
1229 if (PACKET_DEBUG > 1) {
1230 int states[PACKET_NUM_STATES];
1231 pkt_count_states(pd, states);
1232 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1233 states[0], states[1], states[2], states[3],
1234 states[4], states[5]);
1237 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1238 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1239 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1240 min_sleep_time = pkt->sleep_time;
1243 generic_unplug_device(bdev_get_queue(pd->bdev));
1245 VPRINTK("kcdrwd: sleeping\n");
1246 residue = schedule_timeout(min_sleep_time);
1247 VPRINTK("kcdrwd: wake up\n");
1249 /* make swsusp happy with our thread */
1250 if (current->flags & PF_FREEZE)
1251 refrigerator(PF_FREEZE);
1253 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1254 if (!pkt->sleep_time)
1256 pkt->sleep_time -= min_sleep_time - residue;
1257 if (pkt->sleep_time <= 0) {
1258 pkt->sleep_time = 0;
1259 atomic_inc(&pkt->run_sm);
1263 if (signal_pending(current)) {
1264 flush_signals(current);
1266 if (kthread_should_stop())
1270 set_current_state(TASK_RUNNING);
1271 remove_wait_queue(&pd->wqueue, &wait);
1273 if (kthread_should_stop())
1277 * if pkt_handle_queue returns true, we can queue
1280 while (pkt_handle_queue(pd))
1284 * Handle packet state machine
1286 pkt_handle_packets(pd);
1289 * Handle iosched queues
1291 pkt_iosched_process_queue(pd);
1297 static void pkt_print_settings(struct pktcdvd_device *pd)
1299 printk("pktcdvd: %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1300 printk("%u blocks, ", pd->settings.size >> 2);
1301 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1304 static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc,
1305 int page_code, int page_control)
1307 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1309 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1310 cgc->cmd[2] = page_code | (page_control << 6);
1311 cgc->cmd[7] = cgc->buflen >> 8;
1312 cgc->cmd[8] = cgc->buflen & 0xff;
1313 cgc->data_direction = CGC_DATA_READ;
1314 return pkt_generic_packet(pd, cgc);
1317 static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1319 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1320 memset(cgc->buffer, 0, 2);
1321 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1322 cgc->cmd[1] = 0x10; /* PF */
1323 cgc->cmd[7] = cgc->buflen >> 8;
1324 cgc->cmd[8] = cgc->buflen & 0xff;
1325 cgc->data_direction = CGC_DATA_WRITE;
1326 return pkt_generic_packet(pd, cgc);
1329 static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1331 struct packet_command cgc;
1334 /* set up command and get the disc info */
1335 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1336 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1337 cgc.cmd[8] = cgc.buflen = 2;
1340 if ((ret = pkt_generic_packet(pd, &cgc)))
1343 /* not all drives have the same disc_info length, so requeue
1344 * packet with the length the drive tells us it can supply
1346 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1347 sizeof(di->disc_information_length);
1349 if (cgc.buflen > sizeof(disc_information))
1350 cgc.buflen = sizeof(disc_information);
1352 cgc.cmd[8] = cgc.buflen;
1353 return pkt_generic_packet(pd, &cgc);
1356 static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1358 struct packet_command cgc;
1361 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1362 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1363 cgc.cmd[1] = type & 3;
1364 cgc.cmd[4] = (track & 0xff00) >> 8;
1365 cgc.cmd[5] = track & 0xff;
1369 if ((ret = pkt_generic_packet(pd, &cgc)))
1372 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1373 sizeof(ti->track_information_length);
1375 if (cgc.buflen > sizeof(track_information))
1376 cgc.buflen = sizeof(track_information);
1378 cgc.cmd[8] = cgc.buflen;
1379 return pkt_generic_packet(pd, &cgc);
1382 static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1384 disc_information di;
1385 track_information ti;
1389 if ((ret = pkt_get_disc_info(pd, &di)))
1392 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1393 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1396 /* if this track is blank, try the previous. */
1399 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1403 /* if last recorded field is valid, return it. */
1405 *last_written = be32_to_cpu(ti.last_rec_address);
1407 /* make it up instead */
1408 *last_written = be32_to_cpu(ti.track_start) +
1409 be32_to_cpu(ti.track_size);
1411 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1417 * write mode select package based on pd->settings
1419 static int pkt_set_write_settings(struct pktcdvd_device *pd)
1421 struct packet_command cgc;
1422 struct request_sense sense;
1423 write_param_page *wp;
1427 /* doesn't apply to DVD+RW */
1428 if (pd->mmc3_profile == 0x1a)
1431 memset(buffer, 0, sizeof(buffer));
1432 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1434 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1435 pkt_dump_sense(&cgc);
1439 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1440 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1441 if (size > sizeof(buffer))
1442 size = sizeof(buffer);
1447 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1449 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1450 pkt_dump_sense(&cgc);
1455 * write page is offset header + block descriptor length
1457 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1459 wp->fp = pd->settings.fp;
1460 wp->track_mode = pd->settings.track_mode;
1461 wp->write_type = pd->settings.write_type;
1462 wp->data_block_type = pd->settings.block_mode;
1464 wp->multi_session = 0;
1466 #ifdef PACKET_USE_LS
1471 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1472 wp->session_format = 0;
1474 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1475 wp->session_format = 0x20;
1479 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1485 printk("pktcdvd: write mode wrong %d\n", wp->data_block_type);
1488 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1490 cgc.buflen = cgc.cmd[8] = size;
1491 if ((ret = pkt_mode_select(pd, &cgc))) {
1492 pkt_dump_sense(&cgc);
1496 pkt_print_settings(pd);
1501 * 0 -- we can write to this track, 1 -- we can't
1503 static int pkt_good_track(track_information *ti)
1506 * only good for CD-RW at the moment, not DVD-RW
1510 * FIXME: only for FP
1516 * "good" settings as per Mt Fuji.
1518 if (ti->rt == 0 && ti->blank == 0 && ti->packet == 1)
1521 if (ti->rt == 0 && ti->blank == 1 && ti->packet == 1)
1524 if (ti->rt == 1 && ti->blank == 0 && ti->packet == 1)
1527 printk("pktcdvd: bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1532 * 0 -- we can write to this disc, 1 -- we can't
1534 static int pkt_good_disc(struct pktcdvd_device *pd, disc_information *di)
1536 switch (pd->mmc3_profile) {
1537 case 0x0a: /* CD-RW */
1538 case 0xffff: /* MMC3 not supported */
1540 case 0x1a: /* DVD+RW */
1541 case 0x13: /* DVD-RW */
1544 printk("pktcdvd: Wrong disc profile (%x)\n", pd->mmc3_profile);
1549 * for disc type 0xff we should probably reserve a new track.
1550 * but i'm not sure, should we leave this to user apps? probably.
1552 if (di->disc_type == 0xff) {
1553 printk("pktcdvd: Unknown disc. No track?\n");
1557 if (di->disc_type != 0x20 && di->disc_type != 0) {
1558 printk("pktcdvd: Wrong disc type (%x)\n", di->disc_type);
1562 if (di->erasable == 0) {
1563 printk("pktcdvd: Disc not erasable\n");
1567 if (di->border_status == PACKET_SESSION_RESERVED) {
1568 printk("pktcdvd: Can't write to last track (reserved)\n");
1575 static int pkt_probe_settings(struct pktcdvd_device *pd)
1577 struct packet_command cgc;
1578 unsigned char buf[12];
1579 disc_information di;
1580 track_information ti;
1583 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1584 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1586 ret = pkt_generic_packet(pd, &cgc);
1587 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1589 memset(&di, 0, sizeof(disc_information));
1590 memset(&ti, 0, sizeof(track_information));
1592 if ((ret = pkt_get_disc_info(pd, &di))) {
1593 printk("failed get_disc\n");
1597 if (pkt_good_disc(pd, &di))
1600 switch (pd->mmc3_profile) {
1601 case 0x1a: /* DVD+RW */
1602 printk("pktcdvd: inserted media is DVD+RW\n");
1604 case 0x13: /* DVD-RW */
1605 printk("pktcdvd: inserted media is DVD-RW\n");
1608 printk("pktcdvd: inserted media is CD-R%s\n", di.erasable ? "W" : "");
1611 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1613 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1614 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1615 printk("pktcdvd: failed get_track\n");
1619 if (pkt_good_track(&ti)) {
1620 printk("pktcdvd: can't write to this track\n");
1625 * we keep packet size in 512 byte units, makes it easier to
1626 * deal with request calculations.
1628 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1629 if (pd->settings.size == 0) {
1630 printk("pktcdvd: detected zero packet size!\n");
1631 pd->settings.size = 128;
1633 pd->settings.fp = ti.fp;
1634 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1637 pd->nwa = be32_to_cpu(ti.next_writable);
1638 set_bit(PACKET_NWA_VALID, &pd->flags);
1642 * in theory we could use lra on -RW media as well and just zero
1643 * blocks that haven't been written yet, but in practice that
1644 * is just a no-go. we'll use that for -R, naturally.
1647 pd->lra = be32_to_cpu(ti.last_rec_address);
1648 set_bit(PACKET_LRA_VALID, &pd->flags);
1650 pd->lra = 0xffffffff;
1651 set_bit(PACKET_LRA_VALID, &pd->flags);
1657 pd->settings.link_loss = 7;
1658 pd->settings.write_type = 0; /* packet */
1659 pd->settings.track_mode = ti.track_mode;
1662 * mode1 or mode2 disc
1664 switch (ti.data_mode) {
1666 pd->settings.block_mode = PACKET_BLOCK_MODE1;
1669 pd->settings.block_mode = PACKET_BLOCK_MODE2;
1672 printk("pktcdvd: unknown data mode\n");
1679 * enable/disable write caching on drive
1681 static int pkt_write_caching(struct pktcdvd_device *pd, int set)
1683 struct packet_command cgc;
1684 struct request_sense sense;
1685 unsigned char buf[64];
1688 memset(buf, 0, sizeof(buf));
1689 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1691 cgc.buflen = pd->mode_offset + 12;
1694 * caching mode page might not be there, so quiet this command
1698 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
1701 buf[pd->mode_offset + 10] |= (!!set << 2);
1703 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
1704 ret = pkt_mode_select(pd, &cgc);
1706 printk("pktcdvd: write caching control failed\n");
1707 pkt_dump_sense(&cgc);
1708 } else if (!ret && set)
1709 printk("pktcdvd: enabled write caching on %s\n", pd->name);
1713 static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
1715 struct packet_command cgc;
1717 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1718 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1719 cgc.cmd[4] = lockflag ? 1 : 0;
1720 return pkt_generic_packet(pd, &cgc);
1724 * Returns drive maximum write speed
1726 static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
1728 struct packet_command cgc;
1729 struct request_sense sense;
1730 unsigned char buf[256+18];
1731 unsigned char *cap_buf;
1734 memset(buf, 0, sizeof(buf));
1735 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
1736 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
1739 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1741 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
1742 sizeof(struct mode_page_header);
1743 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1745 pkt_dump_sense(&cgc);
1750 offset = 20; /* Obsoleted field, used by older drives */
1751 if (cap_buf[1] >= 28)
1752 offset = 28; /* Current write speed selected */
1753 if (cap_buf[1] >= 30) {
1754 /* If the drive reports at least one "Logical Unit Write
1755 * Speed Performance Descriptor Block", use the information
1756 * in the first block. (contains the highest speed)
1758 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
1763 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
1767 /* These tables from cdrecord - I don't have orange book */
1768 /* standard speed CD-RW (1-4x) */
1769 static char clv_to_speed[16] = {
1770 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1771 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1773 /* high speed CD-RW (-10x) */
1774 static char hs_clv_to_speed[16] = {
1775 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1776 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1778 /* ultra high speed CD-RW */
1779 static char us_clv_to_speed[16] = {
1780 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1781 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
1785 * reads the maximum media speed from ATIP
1787 static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
1789 struct packet_command cgc;
1790 struct request_sense sense;
1791 unsigned char buf[64];
1792 unsigned int size, st, sp;
1795 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
1797 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1799 cgc.cmd[2] = 4; /* READ ATIP */
1801 ret = pkt_generic_packet(pd, &cgc);
1803 pkt_dump_sense(&cgc);
1806 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
1807 if (size > sizeof(buf))
1810 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1812 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1816 ret = pkt_generic_packet(pd, &cgc);
1818 pkt_dump_sense(&cgc);
1822 if (!buf[6] & 0x40) {
1823 printk("pktcdvd: Disc type is not CD-RW\n");
1826 if (!buf[6] & 0x4) {
1827 printk("pktcdvd: A1 values on media are not valid, maybe not CDRW?\n");
1831 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
1833 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
1835 /* Info from cdrecord */
1837 case 0: /* standard speed */
1838 *speed = clv_to_speed[sp];
1840 case 1: /* high speed */
1841 *speed = hs_clv_to_speed[sp];
1843 case 2: /* ultra high speed */
1844 *speed = us_clv_to_speed[sp];
1847 printk("pktcdvd: Unknown disc sub-type %d\n",st);
1851 printk("pktcdvd: Max. media speed: %d\n",*speed);
1854 printk("pktcdvd: Unknown speed %d for sub-type %d\n",sp,st);
1859 static int pkt_perform_opc(struct pktcdvd_device *pd)
1861 struct packet_command cgc;
1862 struct request_sense sense;
1865 VPRINTK("pktcdvd: Performing OPC\n");
1867 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1869 cgc.timeout = 60*HZ;
1870 cgc.cmd[0] = GPCMD_SEND_OPC;
1872 if ((ret = pkt_generic_packet(pd, &cgc)))
1873 pkt_dump_sense(&cgc);
1877 static int pkt_open_write(struct pktcdvd_device *pd)
1880 unsigned int write_speed, media_write_speed, read_speed;
1882 if ((ret = pkt_probe_settings(pd))) {
1883 DPRINTK("pktcdvd: %s failed probe\n", pd->name);
1887 if ((ret = pkt_set_write_settings(pd))) {
1888 DPRINTK("pktcdvd: %s failed saving write settings\n", pd->name);
1892 pkt_write_caching(pd, USE_WCACHING);
1894 if ((ret = pkt_get_max_speed(pd, &write_speed)))
1895 write_speed = 16 * 177;
1896 switch (pd->mmc3_profile) {
1897 case 0x13: /* DVD-RW */
1898 case 0x1a: /* DVD+RW */
1899 DPRINTK("pktcdvd: write speed %ukB/s\n", write_speed);
1902 if ((ret = pkt_media_speed(pd, &media_write_speed)))
1903 media_write_speed = 16;
1904 write_speed = min(write_speed, media_write_speed * 177);
1905 DPRINTK("pktcdvd: write speed %ux\n", write_speed / 176);
1908 read_speed = write_speed;
1910 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
1911 DPRINTK("pktcdvd: %s couldn't set write speed\n", pd->name);
1914 pd->write_speed = write_speed;
1915 pd->read_speed = read_speed;
1917 if ((ret = pkt_perform_opc(pd))) {
1918 DPRINTK("pktcdvd: %s Optimum Power Calibration failed\n", pd->name);
1925 * called at open time.
1927 static int pkt_open_dev(struct pktcdvd_device *pd, int write)
1934 * We need to re-open the cdrom device without O_NONBLOCK to be able
1935 * to read/write from/to it. It is already opened in O_NONBLOCK mode
1936 * so bdget() can't fail.
1938 bdget(pd->bdev->bd_dev);
1939 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
1942 if ((ret = pkt_get_last_written(pd, &lba))) {
1943 printk("pktcdvd: pkt_get_last_written failed\n");
1947 set_capacity(pd->disk, lba << 2);
1948 set_capacity(pd->bdev->bd_disk, lba << 2);
1949 bd_set_size(pd->bdev, (loff_t)lba << 11);
1951 q = bdev_get_queue(pd->bdev);
1953 if ((ret = pkt_open_write(pd)))
1956 * Some CDRW drives can not handle writes larger than one packet,
1957 * even if the size is a multiple of the packet size.
1959 spin_lock_irq(q->queue_lock);
1960 blk_queue_max_sectors(q, pd->settings.size);
1961 spin_unlock_irq(q->queue_lock);
1962 set_bit(PACKET_WRITABLE, &pd->flags);
1964 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1965 clear_bit(PACKET_WRITABLE, &pd->flags);
1968 if ((ret = pkt_set_segment_merging(pd, q)))
1972 printk("pktcdvd: %lukB available on disc\n", lba << 1);
1977 blkdev_put(pd->bdev);
1983 * called when the device is closed. makes sure that the device flushes
1984 * the internal cache before we close.
1986 static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
1988 if (flush && pkt_flush_cache(pd))
1989 DPRINTK("pktcdvd: %s not flushing cache\n", pd->name);
1991 pkt_lock_door(pd, 0);
1993 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1994 blkdev_put(pd->bdev);
1997 static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
1999 if (dev_minor >= MAX_WRITERS)
2001 return pkt_devs[dev_minor];
2004 static int pkt_open(struct inode *inode, struct file *file)
2006 struct pktcdvd_device *pd = NULL;
2009 VPRINTK("pktcdvd: entering open\n");
2012 pd = pkt_find_dev_from_minor(iminor(inode));
2017 BUG_ON(pd->refcnt < 0);
2020 if (pd->refcnt == 1) {
2021 if (pkt_open_dev(pd, file->f_mode & FMODE_WRITE)) {
2026 * needed here as well, since ext2 (among others) may change
2027 * the blocksize at mount time
2029 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2038 VPRINTK("pktcdvd: failed open (%d)\n", ret);
2043 static int pkt_close(struct inode *inode, struct file *file)
2045 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2050 BUG_ON(pd->refcnt < 0);
2051 if (pd->refcnt == 0) {
2052 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2053 pkt_release_dev(pd, flush);
2060 static void *psd_pool_alloc(int gfp_mask, void *data)
2062 return kmalloc(sizeof(struct packet_stacked_data), gfp_mask);
2065 static void psd_pool_free(void *ptr, void *data)
2070 static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2072 struct packet_stacked_data *psd = bio->bi_private;
2073 struct pktcdvd_device *pd = psd->pd;
2079 bio_endio(psd->bio, psd->bio->bi_size, err);
2080 mempool_free(psd, psd_pool);
2081 pkt_bio_finished(pd);
2085 static int pkt_make_request(request_queue_t *q, struct bio *bio)
2087 struct pktcdvd_device *pd;
2088 char b[BDEVNAME_SIZE];
2090 struct packet_data *pkt;
2091 int was_empty, blocked_bio;
2092 struct pkt_rb_node *node;
2096 printk("pktcdvd: %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2101 * Clone READ bios so we can have our own bi_end_io callback.
2103 if (bio_data_dir(bio) == READ) {
2104 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2105 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2109 cloned_bio->bi_bdev = pd->bdev;
2110 cloned_bio->bi_private = psd;
2111 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2112 pd->stats.secs_r += bio->bi_size >> 9;
2113 pkt_queue_bio(pd, cloned_bio, 1);
2117 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2118 printk("pktcdvd: WRITE for ro device %s (%llu)\n",
2119 pd->name, (unsigned long long)bio->bi_sector);
2123 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2124 printk("pktcdvd: wrong bio size\n");
2128 blk_queue_bounce(q, &bio);
2130 zone = ZONE(bio->bi_sector, pd);
2131 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2132 (unsigned long long)bio->bi_sector,
2133 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2135 /* Check if we have to split the bio */
2137 struct bio_pair *bp;
2141 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2142 if (last_zone != zone) {
2143 BUG_ON(last_zone != zone + pd->settings.size);
2144 first_sectors = last_zone - bio->bi_sector;
2145 bp = bio_split(bio, bio_split_pool, first_sectors);
2147 pkt_make_request(q, &bp->bio1);
2148 pkt_make_request(q, &bp->bio2);
2149 bio_pair_release(bp);
2155 * If we find a matching packet in state WAITING or READ_WAIT, we can
2156 * just append this bio to that packet.
2158 spin_lock(&pd->cdrw.active_list_lock);
2160 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2161 if (pkt->sector == zone) {
2162 spin_lock(&pkt->lock);
2163 if ((pkt->state == PACKET_WAITING_STATE) ||
2164 (pkt->state == PACKET_READ_WAIT_STATE)) {
2165 pkt_add_list_last(bio, &pkt->orig_bios,
2166 &pkt->orig_bios_tail);
2167 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2168 if ((pkt->write_size >= pkt->frames) &&
2169 (pkt->state == PACKET_WAITING_STATE)) {
2170 atomic_inc(&pkt->run_sm);
2171 wake_up(&pd->wqueue);
2173 spin_unlock(&pkt->lock);
2174 spin_unlock(&pd->cdrw.active_list_lock);
2179 spin_unlock(&pkt->lock);
2182 spin_unlock(&pd->cdrw.active_list_lock);
2185 * No matching packet found. Store the bio in the work queue.
2187 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2190 spin_lock(&pd->lock);
2191 BUG_ON(pd->bio_queue_size < 0);
2192 was_empty = (pd->bio_queue_size == 0);
2193 pkt_rbtree_insert(pd, node);
2194 spin_unlock(&pd->lock);
2197 * Wake up the worker thread.
2199 atomic_set(&pd->scan_queue, 1);
2201 /* This wake_up is required for correct operation */
2202 wake_up(&pd->wqueue);
2203 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2205 * This wake up is not required for correct operation,
2206 * but improves performance in some cases.
2208 wake_up(&pd->wqueue);
2212 bio_io_error(bio, bio->bi_size);
2218 static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2220 struct pktcdvd_device *pd = q->queuedata;
2221 sector_t zone = ZONE(bio->bi_sector, pd);
2222 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2223 int remaining = (pd->settings.size << 9) - used;
2227 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2228 * boundary, pkt_make_request() will split the bio.
2230 remaining2 = PAGE_SIZE - bio->bi_size;
2231 remaining = max(remaining, remaining2);
2233 BUG_ON(remaining < 0);
2237 static void pkt_init_queue(struct pktcdvd_device *pd)
2239 request_queue_t *q = pd->disk->queue;
2241 blk_queue_make_request(q, pkt_make_request);
2242 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2243 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2244 blk_queue_merge_bvec(q, pkt_merge_bvec);
2248 static int pkt_seq_show(struct seq_file *m, void *p)
2250 struct pktcdvd_device *pd = m->private;
2252 char bdev_buf[BDEVNAME_SIZE];
2253 int states[PACKET_NUM_STATES];
2255 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2256 bdevname(pd->bdev, bdev_buf));
2258 seq_printf(m, "\nSettings:\n");
2259 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2261 if (pd->settings.write_type == 0)
2265 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2267 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2268 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2270 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2272 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2274 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2278 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2280 seq_printf(m, "\nStatistics:\n");
2281 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2282 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2283 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2284 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2285 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2287 seq_printf(m, "\nMisc:\n");
2288 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2289 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2290 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2291 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2292 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2293 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2295 seq_printf(m, "\nQueue state:\n");
2296 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2297 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2298 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2300 pkt_count_states(pd, states);
2301 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2302 states[0], states[1], states[2], states[3], states[4], states[5]);
2307 static int pkt_seq_open(struct inode *inode, struct file *file)
2309 return single_open(file, pkt_seq_show, PDE(inode)->data);
2312 static struct file_operations pkt_proc_fops = {
2313 .open = pkt_seq_open,
2315 .llseek = seq_lseek,
2316 .release = single_release
2319 static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2323 char b[BDEVNAME_SIZE];
2324 struct proc_dir_entry *proc;
2325 struct block_device *bdev;
2327 if (pd->pkt_dev == dev) {
2328 printk("pktcdvd: Recursive setup not allowed\n");
2331 for (i = 0; i < MAX_WRITERS; i++) {
2332 struct pktcdvd_device *pd2 = pkt_devs[i];
2335 if (pd2->bdev->bd_dev == dev) {
2336 printk("pktcdvd: %s already setup\n", bdevname(pd2->bdev, b));
2339 if (pd2->pkt_dev == dev) {
2340 printk("pktcdvd: Can't chain pktcdvd devices\n");
2348 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2352 /* This is safe, since we have a reference from open(). */
2353 __module_get(THIS_MODULE);
2355 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2356 printk("pktcdvd: not enough memory for buffers\n");
2362 set_blocksize(bdev, CD_FRAMESIZE);
2366 atomic_set(&pd->cdrw.pending_bios, 0);
2367 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2368 if (IS_ERR(pd->cdrw.thread)) {
2369 printk("pktcdvd: can't start kernel thread\n");
2374 proc = create_proc_entry(pd->name, 0, pkt_proc);
2377 proc->proc_fops = &pkt_proc_fops;
2379 DPRINTK("pktcdvd: writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2383 pkt_shrink_pktlist(pd);
2386 /* This is safe: open() is still holding a reference. */
2387 module_put(THIS_MODULE);
2391 static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2393 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2395 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2400 * forward selected CDROM ioctls to CD-ROM, for UDF
2402 case CDROMMULTISESSION:
2403 case CDROMREADTOCENTRY:
2404 case CDROM_LAST_WRITTEN:
2405 case CDROM_SEND_PACKET:
2406 case SCSI_IOCTL_SEND_COMMAND:
2407 return ioctl_by_bdev(pd->bdev, cmd, arg);
2411 * The door gets locked when the device is opened, so we
2412 * have to unlock it or else the eject command fails.
2414 pkt_lock_door(pd, 0);
2415 return ioctl_by_bdev(pd->bdev, cmd, arg);
2418 printk("pktcdvd: Unknown ioctl for %s (%x)\n", pd->name, cmd);
2425 static int pkt_media_changed(struct gendisk *disk)
2427 struct pktcdvd_device *pd = disk->private_data;
2428 struct gendisk *attached_disk;
2434 attached_disk = pd->bdev->bd_disk;
2437 return attached_disk->fops->media_changed(attached_disk);
2440 static struct block_device_operations pktcdvd_ops = {
2441 .owner = THIS_MODULE,
2443 .release = pkt_close,
2445 .media_changed = pkt_media_changed,
2449 * Set up mapping from pktcdvd device to CD-ROM device.
2451 static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
2455 struct pktcdvd_device *pd;
2456 struct gendisk *disk;
2457 dev_t dev = new_decode_dev(ctrl_cmd->dev);
2459 for (idx = 0; idx < MAX_WRITERS; idx++)
2462 if (idx == MAX_WRITERS) {
2463 printk("pktcdvd: max %d writers supported\n", MAX_WRITERS);
2467 pd = kmalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2470 memset(pd, 0, sizeof(struct pktcdvd_device));
2472 pd->rb_pool = mempool_create(PKT_RB_POOL_SIZE, pkt_rb_alloc, pkt_rb_free, NULL);
2476 disk = alloc_disk(1);
2481 spin_lock_init(&pd->lock);
2482 spin_lock_init(&pd->iosched.lock);
2483 sprintf(pd->name, "pktcdvd%d", idx);
2484 init_waitqueue_head(&pd->wqueue);
2485 pd->bio_queue = RB_ROOT;
2487 disk->major = pkt_major;
2488 disk->first_minor = idx;
2489 disk->fops = &pktcdvd_ops;
2490 disk->flags = GENHD_FL_REMOVABLE;
2491 sprintf(disk->disk_name, "pktcdvd%d", idx);
2492 disk->private_data = pd;
2493 disk->queue = blk_alloc_queue(GFP_KERNEL);
2497 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2498 ret = pkt_new_dev(pd, dev);
2504 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2508 blk_put_queue(disk->queue);
2513 mempool_destroy(pd->rb_pool);
2519 * Tear down mapping from pktcdvd device to CD-ROM device.
2521 static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
2523 struct pktcdvd_device *pd;
2525 dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
2527 for (idx = 0; idx < MAX_WRITERS; idx++) {
2529 if (pd && (pd->pkt_dev == pkt_dev))
2532 if (idx == MAX_WRITERS) {
2533 DPRINTK("pktcdvd: dev not setup\n");
2540 if (!IS_ERR(pd->cdrw.thread))
2541 kthread_stop(pd->cdrw.thread);
2543 blkdev_put(pd->bdev);
2545 pkt_shrink_pktlist(pd);
2547 remove_proc_entry(pd->name, pkt_proc);
2548 DPRINTK("pktcdvd: writer %s unmapped\n", pd->name);
2550 del_gendisk(pd->disk);
2551 blk_put_queue(pd->disk->queue);
2554 pkt_devs[idx] = NULL;
2555 mempool_destroy(pd->rb_pool);
2558 /* This is safe: open() is still holding a reference. */
2559 module_put(THIS_MODULE);
2563 static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2565 struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2567 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2568 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2571 ctrl_cmd->pkt_dev = 0;
2573 ctrl_cmd->num_devices = MAX_WRITERS;
2576 static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2578 void __user *argp = (void __user *)arg;
2579 struct pkt_ctrl_command ctrl_cmd;
2582 if (cmd != PACKET_CTRL_CMD)
2585 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2588 switch (ctrl_cmd.command) {
2589 case PKT_CTRL_CMD_SETUP:
2590 if (!capable(CAP_SYS_ADMIN))
2593 ret = pkt_setup_dev(&ctrl_cmd);
2596 case PKT_CTRL_CMD_TEARDOWN:
2597 if (!capable(CAP_SYS_ADMIN))
2600 ret = pkt_remove_dev(&ctrl_cmd);
2603 case PKT_CTRL_CMD_STATUS:
2605 pkt_get_status(&ctrl_cmd);
2612 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
2618 static struct file_operations pkt_ctl_fops = {
2619 .ioctl = pkt_ctl_ioctl,
2620 .owner = THIS_MODULE,
2623 static struct miscdevice pkt_misc = {
2624 .minor = MISC_DYNAMIC_MINOR,
2626 .devfs_name = "pktcdvd/control",
2627 .fops = &pkt_ctl_fops
2634 psd_pool = mempool_create(PSD_POOL_SIZE, psd_pool_alloc, psd_pool_free, NULL);
2638 ret = register_blkdev(pkt_major, "pktcdvd");
2640 printk("pktcdvd: Unable to register block device\n");
2646 ret = misc_register(&pkt_misc);
2648 printk("pktcdvd: Unable to register misc device\n");
2652 init_MUTEX(&ctl_mutex);
2654 pkt_proc = proc_mkdir("pktcdvd", proc_root_driver);
2656 DPRINTK("pktcdvd: %s\n", VERSION_CODE);
2660 unregister_blkdev(pkt_major, "pktcdvd");
2662 mempool_destroy(psd_pool);
2668 remove_proc_entry("pktcdvd", proc_root_driver);
2669 misc_deregister(&pkt_misc);
2670 unregister_blkdev(pkt_major, "pktcdvd");
2671 mempool_destroy(psd_pool);
2674 MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
2675 MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
2676 MODULE_LICENSE("GPL");
2678 module_init(pkt_init);
2679 module_exit(pkt_exit);