blob: 58d01c8207995d1f18162ec47df8b072e9605ca3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 (aka an exercise in block layer masturbation)
10 *
11 *
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
16 * fails.
17 * - handle OPC, especially for -RW media
18 *
19 * Theory of operation:
20 *
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.
27 *
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.
31 *
32 *************************************************************************/
33
34#define VERSION_CODE "v0.2.0a 2004-07-14 Jens Axboe (axboe@suse.de) and petero2@telia.com"
35
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>
51
52#include <asm/uaccess.h>
53
54#if PACKET_DEBUG
55#define DPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
56#else
57#define DPRINTK(fmt, args...)
58#endif
59
60#if PACKET_DEBUG > 1
61#define VPRINTK(fmt, args...) printk(KERN_NOTICE fmt, ##args)
62#else
63#define VPRINTK(fmt, args...)
64#endif
65
66#define MAX_SPEED 0xffff
67
68#define ZONE(sector, pd) (((sector) + (pd)->offset) & ~((pd)->settings.size - 1))
69
70static struct pktcdvd_device *pkt_devs[MAX_WRITERS];
71static struct proc_dir_entry *pkt_proc;
72static int pkt_major;
73static struct semaphore ctl_mutex; /* Serialize open/close/setup/teardown */
74static mempool_t *psd_pool;
75
76
77static void pkt_bio_finished(struct pktcdvd_device *pd)
78{
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);
83 wake_up(&pd->wqueue);
84 }
85}
86
87static void pkt_bio_destructor(struct bio *bio)
88{
89 kfree(bio->bi_io_vec);
90 kfree(bio);
91}
92
93static struct bio *pkt_bio_alloc(int nr_iovecs)
94{
95 struct bio_vec *bvl = NULL;
96 struct bio *bio;
97
98 bio = kmalloc(sizeof(struct bio), GFP_KERNEL);
99 if (!bio)
100 goto no_bio;
101 bio_init(bio);
102
103 bvl = kmalloc(nr_iovecs * sizeof(struct bio_vec), GFP_KERNEL);
104 if (!bvl)
105 goto no_bvl;
106 memset(bvl, 0, nr_iovecs * sizeof(struct bio_vec));
107
108 bio->bi_max_vecs = nr_iovecs;
109 bio->bi_io_vec = bvl;
110 bio->bi_destructor = pkt_bio_destructor;
111
112 return bio;
113
114 no_bvl:
115 kfree(bio);
116 no_bio:
117 return NULL;
118}
119
120/*
121 * Allocate a packet_data struct
122 */
123static struct packet_data *pkt_alloc_packet_data(void)
124{
125 int i;
126 struct packet_data *pkt;
127
128 pkt = kmalloc(sizeof(struct packet_data), GFP_KERNEL);
129 if (!pkt)
130 goto no_pkt;
131 memset(pkt, 0, sizeof(struct packet_data));
132
133 pkt->w_bio = pkt_bio_alloc(PACKET_MAX_SIZE);
134 if (!pkt->w_bio)
135 goto no_bio;
136
137 for (i = 0; i < PAGES_PER_PACKET; i++) {
138 pkt->pages[i] = alloc_page(GFP_KERNEL|__GFP_ZERO);
139 if (!pkt->pages[i])
140 goto no_page;
141 }
142
143 spin_lock_init(&pkt->lock);
144
145 for (i = 0; i < PACKET_MAX_SIZE; i++) {
146 struct bio *bio = pkt_bio_alloc(1);
147 if (!bio)
148 goto no_rd_bio;
149 pkt->r_bios[i] = bio;
150 }
151
152 return pkt;
153
154no_rd_bio:
155 for (i = 0; i < PACKET_MAX_SIZE; i++) {
156 struct bio *bio = pkt->r_bios[i];
157 if (bio)
158 bio_put(bio);
159 }
160
161no_page:
162 for (i = 0; i < PAGES_PER_PACKET; i++)
163 if (pkt->pages[i])
164 __free_page(pkt->pages[i]);
165 bio_put(pkt->w_bio);
166no_bio:
167 kfree(pkt);
168no_pkt:
169 return NULL;
170}
171
172/*
173 * Free a packet_data struct
174 */
175static void pkt_free_packet_data(struct packet_data *pkt)
176{
177 int i;
178
179 for (i = 0; i < PACKET_MAX_SIZE; i++) {
180 struct bio *bio = pkt->r_bios[i];
181 if (bio)
182 bio_put(bio);
183 }
184 for (i = 0; i < PAGES_PER_PACKET; i++)
185 __free_page(pkt->pages[i]);
186 bio_put(pkt->w_bio);
187 kfree(pkt);
188}
189
190static void pkt_shrink_pktlist(struct pktcdvd_device *pd)
191{
192 struct packet_data *pkt, *next;
193
194 BUG_ON(!list_empty(&pd->cdrw.pkt_active_list));
195
196 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_free_list, list) {
197 pkt_free_packet_data(pkt);
198 }
199}
200
201static int pkt_grow_pktlist(struct pktcdvd_device *pd, int nr_packets)
202{
203 struct packet_data *pkt;
204
205 INIT_LIST_HEAD(&pd->cdrw.pkt_free_list);
206 INIT_LIST_HEAD(&pd->cdrw.pkt_active_list);
207 spin_lock_init(&pd->cdrw.active_list_lock);
208 while (nr_packets > 0) {
209 pkt = pkt_alloc_packet_data();
210 if (!pkt) {
211 pkt_shrink_pktlist(pd);
212 return 0;
213 }
214 pkt->id = nr_packets;
215 pkt->pd = pd;
216 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
217 nr_packets--;
218 }
219 return 1;
220}
221
222static void *pkt_rb_alloc(unsigned int __nocast gfp_mask, void *data)
223{
224 return kmalloc(sizeof(struct pkt_rb_node), gfp_mask);
225}
226
227static void pkt_rb_free(void *ptr, void *data)
228{
229 kfree(ptr);
230}
231
232static inline struct pkt_rb_node *pkt_rbtree_next(struct pkt_rb_node *node)
233{
234 struct rb_node *n = rb_next(&node->rb_node);
235 if (!n)
236 return NULL;
237 return rb_entry(n, struct pkt_rb_node, rb_node);
238}
239
240static inline void pkt_rbtree_erase(struct pktcdvd_device *pd, struct pkt_rb_node *node)
241{
242 rb_erase(&node->rb_node, &pd->bio_queue);
243 mempool_free(node, pd->rb_pool);
244 pd->bio_queue_size--;
245 BUG_ON(pd->bio_queue_size < 0);
246}
247
248/*
249 * Find the first node in the pd->bio_queue rb tree with a starting sector >= s.
250 */
251static struct pkt_rb_node *pkt_rbtree_find(struct pktcdvd_device *pd, sector_t s)
252{
253 struct rb_node *n = pd->bio_queue.rb_node;
254 struct rb_node *next;
255 struct pkt_rb_node *tmp;
256
257 if (!n) {
258 BUG_ON(pd->bio_queue_size > 0);
259 return NULL;
260 }
261
262 for (;;) {
263 tmp = rb_entry(n, struct pkt_rb_node, rb_node);
264 if (s <= tmp->bio->bi_sector)
265 next = n->rb_left;
266 else
267 next = n->rb_right;
268 if (!next)
269 break;
270 n = next;
271 }
272
273 if (s > tmp->bio->bi_sector) {
274 tmp = pkt_rbtree_next(tmp);
275 if (!tmp)
276 return NULL;
277 }
278 BUG_ON(s > tmp->bio->bi_sector);
279 return tmp;
280}
281
282/*
283 * Insert a node into the pd->bio_queue rb tree.
284 */
285static void pkt_rbtree_insert(struct pktcdvd_device *pd, struct pkt_rb_node *node)
286{
287 struct rb_node **p = &pd->bio_queue.rb_node;
288 struct rb_node *parent = NULL;
289 sector_t s = node->bio->bi_sector;
290 struct pkt_rb_node *tmp;
291
292 while (*p) {
293 parent = *p;
294 tmp = rb_entry(parent, struct pkt_rb_node, rb_node);
295 if (s < tmp->bio->bi_sector)
296 p = &(*p)->rb_left;
297 else
298 p = &(*p)->rb_right;
299 }
300 rb_link_node(&node->rb_node, parent, p);
301 rb_insert_color(&node->rb_node, &pd->bio_queue);
302 pd->bio_queue_size++;
303}
304
305/*
306 * Add a bio to a single linked list defined by its head and tail pointers.
307 */
308static inline void pkt_add_list_last(struct bio *bio, struct bio **list_head, struct bio **list_tail)
309{
310 bio->bi_next = NULL;
311 if (*list_tail) {
312 BUG_ON((*list_head) == NULL);
313 (*list_tail)->bi_next = bio;
314 (*list_tail) = bio;
315 } else {
316 BUG_ON((*list_head) != NULL);
317 (*list_head) = bio;
318 (*list_tail) = bio;
319 }
320}
321
322/*
323 * Remove and return the first bio from a single linked list defined by its
324 * head and tail pointers.
325 */
326static inline struct bio *pkt_get_list_first(struct bio **list_head, struct bio **list_tail)
327{
328 struct bio *bio;
329
330 if (*list_head == NULL)
331 return NULL;
332
333 bio = *list_head;
334 *list_head = bio->bi_next;
335 if (*list_head == NULL)
336 *list_tail = NULL;
337
338 bio->bi_next = NULL;
339 return bio;
340}
341
342/*
343 * Send a packet_command to the underlying block device and
344 * wait for completion.
345 */
346static int pkt_generic_packet(struct pktcdvd_device *pd, struct packet_command *cgc)
347{
348 char sense[SCSI_SENSE_BUFFERSIZE];
349 request_queue_t *q;
350 struct request *rq;
351 DECLARE_COMPLETION(wait);
352 int err = 0;
353
354 q = bdev_get_queue(pd->bdev);
355
356 rq = blk_get_request(q, (cgc->data_direction == CGC_DATA_WRITE) ? WRITE : READ,
357 __GFP_WAIT);
358 rq->errors = 0;
359 rq->rq_disk = pd->bdev->bd_disk;
360 rq->bio = NULL;
361 rq->buffer = NULL;
362 rq->timeout = 60*HZ;
363 rq->data = cgc->buffer;
364 rq->data_len = cgc->buflen;
365 rq->sense = sense;
366 memset(sense, 0, sizeof(sense));
367 rq->sense_len = 0;
368 rq->flags |= REQ_BLOCK_PC | REQ_HARDBARRIER;
369 if (cgc->quiet)
370 rq->flags |= REQ_QUIET;
371 memcpy(rq->cmd, cgc->cmd, CDROM_PACKET_SIZE);
372 if (sizeof(rq->cmd) > CDROM_PACKET_SIZE)
373 memset(rq->cmd + CDROM_PACKET_SIZE, 0, sizeof(rq->cmd) - CDROM_PACKET_SIZE);
374
375 rq->ref_count++;
376 rq->flags |= REQ_NOMERGE;
377 rq->waiting = &wait;
378 rq->end_io = blk_end_sync_rq;
379 elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 1);
380 generic_unplug_device(q);
381 wait_for_completion(&wait);
382
383 if (rq->errors)
384 err = -EIO;
385
386 blk_put_request(rq);
387 return err;
388}
389
390/*
391 * A generic sense dump / resolve mechanism should be implemented across
392 * all ATAPI + SCSI devices.
393 */
394static void pkt_dump_sense(struct packet_command *cgc)
395{
396 static char *info[9] = { "No sense", "Recovered error", "Not ready",
397 "Medium error", "Hardware error", "Illegal request",
398 "Unit attention", "Data protect", "Blank check" };
399 int i;
400 struct request_sense *sense = cgc->sense;
401
402 printk("pktcdvd:");
403 for (i = 0; i < CDROM_PACKET_SIZE; i++)
404 printk(" %02x", cgc->cmd[i]);
405 printk(" - ");
406
407 if (sense == NULL) {
408 printk("no sense\n");
409 return;
410 }
411
412 printk("sense %02x.%02x.%02x", sense->sense_key, sense->asc, sense->ascq);
413
414 if (sense->sense_key > 8) {
415 printk(" (INVALID)\n");
416 return;
417 }
418
419 printk(" (%s)\n", info[sense->sense_key]);
420}
421
422/*
423 * flush the drive cache to media
424 */
425static int pkt_flush_cache(struct pktcdvd_device *pd)
426{
427 struct packet_command cgc;
428
429 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
430 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
431 cgc.quiet = 1;
432
433 /*
434 * the IMMED bit -- we default to not setting it, although that
435 * would allow a much faster close, this is safer
436 */
437#if 0
438 cgc.cmd[1] = 1 << 1;
439#endif
440 return pkt_generic_packet(pd, &cgc);
441}
442
443/*
444 * speed is given as the normal factor, e.g. 4 for 4x
445 */
446static int pkt_set_speed(struct pktcdvd_device *pd, unsigned write_speed, unsigned read_speed)
447{
448 struct packet_command cgc;
449 struct request_sense sense;
450 int ret;
451
452 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
453 cgc.sense = &sense;
454 cgc.cmd[0] = GPCMD_SET_SPEED;
455 cgc.cmd[2] = (read_speed >> 8) & 0xff;
456 cgc.cmd[3] = read_speed & 0xff;
457 cgc.cmd[4] = (write_speed >> 8) & 0xff;
458 cgc.cmd[5] = write_speed & 0xff;
459
460 if ((ret = pkt_generic_packet(pd, &cgc)))
461 pkt_dump_sense(&cgc);
462
463 return ret;
464}
465
466/*
467 * Queue a bio for processing by the low-level CD device. Must be called
468 * from process context.
469 */
Peter Osterlund46c271b2005-06-23 00:10:02 -0700470static void pkt_queue_bio(struct pktcdvd_device *pd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 spin_lock(&pd->iosched.lock);
473 if (bio_data_dir(bio) == READ) {
474 pkt_add_list_last(bio, &pd->iosched.read_queue,
475 &pd->iosched.read_queue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
477 pkt_add_list_last(bio, &pd->iosched.write_queue,
478 &pd->iosched.write_queue_tail);
479 }
480 spin_unlock(&pd->iosched.lock);
481
482 atomic_set(&pd->iosched.attention, 1);
483 wake_up(&pd->wqueue);
484}
485
486/*
487 * Process the queued read/write requests. This function handles special
488 * requirements for CDRW drives:
489 * - A cache flush command must be inserted before a read request if the
490 * previous request was a write.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700491 * - Switching between reading and writing is slow, so don't do it more often
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * than necessary.
Peter Osterlund46c271b2005-06-23 00:10:02 -0700493 * - Optimize for throughput at the expense of latency. This means that streaming
494 * writes will never be interrupted by a read, but if the drive has to seek
495 * before the next write, switch to reading instead if there are any pending
496 * read requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * - Set the read speed according to current usage pattern. When only reading
498 * from the device, it's best to use the highest possible read speed, but
499 * when switching often between reading and writing, it's better to have the
500 * same read and write speeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
502static void pkt_iosched_process_queue(struct pktcdvd_device *pd)
503{
504 request_queue_t *q;
505
506 if (atomic_read(&pd->iosched.attention) == 0)
507 return;
508 atomic_set(&pd->iosched.attention, 0);
509
510 q = bdev_get_queue(pd->bdev);
511
512 for (;;) {
513 struct bio *bio;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700514 int reads_queued, writes_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 spin_lock(&pd->iosched.lock);
517 reads_queued = (pd->iosched.read_queue != NULL);
518 writes_queued = (pd->iosched.write_queue != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 spin_unlock(&pd->iosched.lock);
520
521 if (!reads_queued && !writes_queued)
522 break;
523
524 if (pd->iosched.writing) {
Peter Osterlund46c271b2005-06-23 00:10:02 -0700525 int need_write_seek = 1;
526 spin_lock(&pd->iosched.lock);
527 bio = pd->iosched.write_queue;
528 spin_unlock(&pd->iosched.lock);
529 if (bio && (bio->bi_sector == pd->iosched.last_write))
530 need_write_seek = 0;
531 if (need_write_seek && reads_queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
533 VPRINTK("pktcdvd: write, waiting\n");
534 break;
535 }
536 pkt_flush_cache(pd);
537 pd->iosched.writing = 0;
538 }
539 } else {
540 if (!reads_queued && writes_queued) {
541 if (atomic_read(&pd->cdrw.pending_bios) > 0) {
542 VPRINTK("pktcdvd: read, waiting\n");
543 break;
544 }
545 pd->iosched.writing = 1;
546 }
547 }
548
549 spin_lock(&pd->iosched.lock);
550 if (pd->iosched.writing) {
551 bio = pkt_get_list_first(&pd->iosched.write_queue,
552 &pd->iosched.write_queue_tail);
553 } else {
554 bio = pkt_get_list_first(&pd->iosched.read_queue,
555 &pd->iosched.read_queue_tail);
556 }
557 spin_unlock(&pd->iosched.lock);
558
559 if (!bio)
560 continue;
561
562 if (bio_data_dir(bio) == READ)
563 pd->iosched.successive_reads += bio->bi_size >> 10;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700564 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 pd->iosched.successive_reads = 0;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700566 pd->iosched.last_write = bio->bi_sector + bio_sectors(bio);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (pd->iosched.successive_reads >= HI_SPEED_SWITCH) {
569 if (pd->read_speed == pd->write_speed) {
570 pd->read_speed = MAX_SPEED;
571 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
572 }
573 } else {
574 if (pd->read_speed != pd->write_speed) {
575 pd->read_speed = pd->write_speed;
576 pkt_set_speed(pd, pd->write_speed, pd->read_speed);
577 }
578 }
579
580 atomic_inc(&pd->cdrw.pending_bios);
581 generic_make_request(bio);
582 }
583}
584
585/*
586 * Special care is needed if the underlying block device has a small
587 * max_phys_segments value.
588 */
589static int pkt_set_segment_merging(struct pktcdvd_device *pd, request_queue_t *q)
590{
591 if ((pd->settings.size << 9) / CD_FRAMESIZE <= q->max_phys_segments) {
592 /*
593 * The cdrom device can handle one segment/frame
594 */
595 clear_bit(PACKET_MERGE_SEGS, &pd->flags);
596 return 0;
597 } else if ((pd->settings.size << 9) / PAGE_SIZE <= q->max_phys_segments) {
598 /*
599 * We can handle this case at the expense of some extra memory
600 * copies during write operations
601 */
602 set_bit(PACKET_MERGE_SEGS, &pd->flags);
603 return 0;
604 } else {
605 printk("pktcdvd: cdrom max_phys_segments too small\n");
606 return -EIO;
607 }
608}
609
610/*
611 * Copy CD_FRAMESIZE bytes from src_bio into a destination page
612 */
613static void pkt_copy_bio_data(struct bio *src_bio, int seg, int offs, struct page *dst_page, int dst_offs)
614{
615 unsigned int copy_size = CD_FRAMESIZE;
616
617 while (copy_size > 0) {
618 struct bio_vec *src_bvl = bio_iovec_idx(src_bio, seg);
619 void *vfrom = kmap_atomic(src_bvl->bv_page, KM_USER0) +
620 src_bvl->bv_offset + offs;
621 void *vto = page_address(dst_page) + dst_offs;
622 int len = min_t(int, copy_size, src_bvl->bv_len - offs);
623
624 BUG_ON(len < 0);
625 memcpy(vto, vfrom, len);
626 kunmap_atomic(vfrom, KM_USER0);
627
628 seg++;
629 offs = 0;
630 dst_offs += len;
631 copy_size -= len;
632 }
633}
634
635/*
636 * Copy all data for this packet to pkt->pages[], so that
637 * a) The number of required segments for the write bio is minimized, which
638 * is necessary for some scsi controllers.
639 * b) The data can be used as cache to avoid read requests if we receive a
640 * new write request for the same zone.
641 */
642static void pkt_make_local_copy(struct packet_data *pkt, struct page **pages, int *offsets)
643{
644 int f, p, offs;
645
646 /* Copy all data to pkt->pages[] */
647 p = 0;
648 offs = 0;
649 for (f = 0; f < pkt->frames; f++) {
650 if (pages[f] != pkt->pages[p]) {
651 void *vfrom = kmap_atomic(pages[f], KM_USER0) + offsets[f];
652 void *vto = page_address(pkt->pages[p]) + offs;
653 memcpy(vto, vfrom, CD_FRAMESIZE);
654 kunmap_atomic(vfrom, KM_USER0);
655 pages[f] = pkt->pages[p];
656 offsets[f] = offs;
657 } else {
658 BUG_ON(offsets[f] != offs);
659 }
660 offs += CD_FRAMESIZE;
661 if (offs >= PAGE_SIZE) {
662 BUG_ON(offs > PAGE_SIZE);
663 offs = 0;
664 p++;
665 }
666 }
667}
668
669static int pkt_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
670{
671 struct packet_data *pkt = bio->bi_private;
672 struct pktcdvd_device *pd = pkt->pd;
673 BUG_ON(!pd);
674
675 if (bio->bi_size)
676 return 1;
677
678 VPRINTK("pkt_end_io_read: bio=%p sec0=%llx sec=%llx err=%d\n", bio,
679 (unsigned long long)pkt->sector, (unsigned long long)bio->bi_sector, err);
680
681 if (err)
682 atomic_inc(&pkt->io_errors);
683 if (atomic_dec_and_test(&pkt->io_wait)) {
684 atomic_inc(&pkt->run_sm);
685 wake_up(&pd->wqueue);
686 }
687 pkt_bio_finished(pd);
688
689 return 0;
690}
691
692static int pkt_end_io_packet_write(struct bio *bio, unsigned int bytes_done, int err)
693{
694 struct packet_data *pkt = bio->bi_private;
695 struct pktcdvd_device *pd = pkt->pd;
696 BUG_ON(!pd);
697
698 if (bio->bi_size)
699 return 1;
700
701 VPRINTK("pkt_end_io_packet_write: id=%d, err=%d\n", pkt->id, err);
702
703 pd->stats.pkt_ended++;
704
705 pkt_bio_finished(pd);
706 atomic_dec(&pkt->io_wait);
707 atomic_inc(&pkt->run_sm);
708 wake_up(&pd->wqueue);
709 return 0;
710}
711
712/*
713 * Schedule reads for the holes in a packet
714 */
715static void pkt_gather_data(struct pktcdvd_device *pd, struct packet_data *pkt)
716{
717 int frames_read = 0;
718 struct bio *bio;
719 int f;
720 char written[PACKET_MAX_SIZE];
721
722 BUG_ON(!pkt->orig_bios);
723
724 atomic_set(&pkt->io_wait, 0);
725 atomic_set(&pkt->io_errors, 0);
726
727 if (pkt->cache_valid) {
728 VPRINTK("pkt_gather_data: zone %llx cached\n",
729 (unsigned long long)pkt->sector);
730 goto out_account;
731 }
732
733 /*
734 * Figure out which frames we need to read before we can write.
735 */
736 memset(written, 0, sizeof(written));
737 spin_lock(&pkt->lock);
738 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
739 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
740 int num_frames = bio->bi_size / CD_FRAMESIZE;
741 BUG_ON(first_frame < 0);
742 BUG_ON(first_frame + num_frames > pkt->frames);
743 for (f = first_frame; f < first_frame + num_frames; f++)
744 written[f] = 1;
745 }
746 spin_unlock(&pkt->lock);
747
748 /*
749 * Schedule reads for missing parts of the packet.
750 */
751 for (f = 0; f < pkt->frames; f++) {
752 int p, offset;
753 if (written[f])
754 continue;
755 bio = pkt->r_bios[f];
756 bio_init(bio);
757 bio->bi_max_vecs = 1;
758 bio->bi_sector = pkt->sector + f * (CD_FRAMESIZE >> 9);
759 bio->bi_bdev = pd->bdev;
760 bio->bi_end_io = pkt_end_io_read;
761 bio->bi_private = pkt;
762
763 p = (f * CD_FRAMESIZE) / PAGE_SIZE;
764 offset = (f * CD_FRAMESIZE) % PAGE_SIZE;
765 VPRINTK("pkt_gather_data: Adding frame %d, page:%p offs:%d\n",
766 f, pkt->pages[p], offset);
767 if (!bio_add_page(bio, pkt->pages[p], CD_FRAMESIZE, offset))
768 BUG();
769
770 atomic_inc(&pkt->io_wait);
771 bio->bi_rw = READ;
Peter Osterlund46c271b2005-06-23 00:10:02 -0700772 pkt_queue_bio(pd, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 frames_read++;
774 }
775
776out_account:
777 VPRINTK("pkt_gather_data: need %d frames for zone %llx\n",
778 frames_read, (unsigned long long)pkt->sector);
779 pd->stats.pkt_started++;
780 pd->stats.secs_rg += frames_read * (CD_FRAMESIZE >> 9);
781 pd->stats.secs_w += pd->settings.size;
782}
783
784/*
785 * Find a packet matching zone, or the least recently used packet if
786 * there is no match.
787 */
788static struct packet_data *pkt_get_packet_data(struct pktcdvd_device *pd, int zone)
789{
790 struct packet_data *pkt;
791
792 list_for_each_entry(pkt, &pd->cdrw.pkt_free_list, list) {
793 if (pkt->sector == zone || pkt->list.next == &pd->cdrw.pkt_free_list) {
794 list_del_init(&pkt->list);
795 if (pkt->sector != zone)
796 pkt->cache_valid = 0;
797 break;
798 }
799 }
800 return pkt;
801}
802
803static void pkt_put_packet_data(struct pktcdvd_device *pd, struct packet_data *pkt)
804{
805 if (pkt->cache_valid) {
806 list_add(&pkt->list, &pd->cdrw.pkt_free_list);
807 } else {
808 list_add_tail(&pkt->list, &pd->cdrw.pkt_free_list);
809 }
810}
811
812/*
813 * recover a failed write, query for relocation if possible
814 *
815 * returns 1 if recovery is possible, or 0 if not
816 *
817 */
818static int pkt_start_recovery(struct packet_data *pkt)
819{
820 /*
821 * FIXME. We need help from the file system to implement
822 * recovery handling.
823 */
824 return 0;
825#if 0
826 struct request *rq = pkt->rq;
827 struct pktcdvd_device *pd = rq->rq_disk->private_data;
828 struct block_device *pkt_bdev;
829 struct super_block *sb = NULL;
830 unsigned long old_block, new_block;
831 sector_t new_sector;
832
833 pkt_bdev = bdget(kdev_t_to_nr(pd->pkt_dev));
834 if (pkt_bdev) {
835 sb = get_super(pkt_bdev);
836 bdput(pkt_bdev);
837 }
838
839 if (!sb)
840 return 0;
841
842 if (!sb->s_op || !sb->s_op->relocate_blocks)
843 goto out;
844
845 old_block = pkt->sector / (CD_FRAMESIZE >> 9);
846 if (sb->s_op->relocate_blocks(sb, old_block, &new_block))
847 goto out;
848
849 new_sector = new_block * (CD_FRAMESIZE >> 9);
850 pkt->sector = new_sector;
851
852 pkt->bio->bi_sector = new_sector;
853 pkt->bio->bi_next = NULL;
854 pkt->bio->bi_flags = 1 << BIO_UPTODATE;
855 pkt->bio->bi_idx = 0;
856
857 BUG_ON(pkt->bio->bi_rw != (1 << BIO_RW));
858 BUG_ON(pkt->bio->bi_vcnt != pkt->frames);
859 BUG_ON(pkt->bio->bi_size != pkt->frames * CD_FRAMESIZE);
860 BUG_ON(pkt->bio->bi_end_io != pkt_end_io_packet_write);
861 BUG_ON(pkt->bio->bi_private != pkt);
862
863 drop_super(sb);
864 return 1;
865
866out:
867 drop_super(sb);
868 return 0;
869#endif
870}
871
872static inline void pkt_set_state(struct packet_data *pkt, enum packet_data_state state)
873{
874#if PACKET_DEBUG > 1
875 static const char *state_name[] = {
876 "IDLE", "WAITING", "READ_WAIT", "WRITE_WAIT", "RECOVERY", "FINISHED"
877 };
878 enum packet_data_state old_state = pkt->state;
879 VPRINTK("pkt %2d : s=%6llx %s -> %s\n", pkt->id, (unsigned long long)pkt->sector,
880 state_name[old_state], state_name[state]);
881#endif
882 pkt->state = state;
883}
884
885/*
886 * Scan the work queue to see if we can start a new packet.
887 * returns non-zero if any work was done.
888 */
889static int pkt_handle_queue(struct pktcdvd_device *pd)
890{
891 struct packet_data *pkt, *p;
892 struct bio *bio = NULL;
893 sector_t zone = 0; /* Suppress gcc warning */
894 struct pkt_rb_node *node, *first_node;
895 struct rb_node *n;
896
897 VPRINTK("handle_queue\n");
898
899 atomic_set(&pd->scan_queue, 0);
900
901 if (list_empty(&pd->cdrw.pkt_free_list)) {
902 VPRINTK("handle_queue: no pkt\n");
903 return 0;
904 }
905
906 /*
907 * Try to find a zone we are not already working on.
908 */
909 spin_lock(&pd->lock);
910 first_node = pkt_rbtree_find(pd, pd->current_sector);
911 if (!first_node) {
912 n = rb_first(&pd->bio_queue);
913 if (n)
914 first_node = rb_entry(n, struct pkt_rb_node, rb_node);
915 }
916 node = first_node;
917 while (node) {
918 bio = node->bio;
919 zone = ZONE(bio->bi_sector, pd);
920 list_for_each_entry(p, &pd->cdrw.pkt_active_list, list) {
Peter Osterlund7baeb6a2005-05-16 21:53:42 -0700921 if (p->sector == zone) {
922 bio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 goto try_next_bio;
Peter Osterlund7baeb6a2005-05-16 21:53:42 -0700924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 break;
927try_next_bio:
928 node = pkt_rbtree_next(node);
929 if (!node) {
930 n = rb_first(&pd->bio_queue);
931 if (n)
932 node = rb_entry(n, struct pkt_rb_node, rb_node);
933 }
934 if (node == first_node)
935 node = NULL;
936 }
937 spin_unlock(&pd->lock);
938 if (!bio) {
939 VPRINTK("handle_queue: no bio\n");
940 return 0;
941 }
942
943 pkt = pkt_get_packet_data(pd, zone);
944 BUG_ON(!pkt);
945
946 pd->current_sector = zone + pd->settings.size;
947 pkt->sector = zone;
948 pkt->frames = pd->settings.size >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 pkt->write_size = 0;
950
951 /*
952 * Scan work queue for bios in the same zone and link them
953 * to this packet.
954 */
955 spin_lock(&pd->lock);
956 VPRINTK("pkt_handle_queue: looking for zone %llx\n", (unsigned long long)zone);
957 while ((node = pkt_rbtree_find(pd, zone)) != NULL) {
958 bio = node->bio;
959 VPRINTK("pkt_handle_queue: found zone=%llx\n",
960 (unsigned long long)ZONE(bio->bi_sector, pd));
961 if (ZONE(bio->bi_sector, pd) != zone)
962 break;
963 pkt_rbtree_erase(pd, node);
964 spin_lock(&pkt->lock);
965 pkt_add_list_last(bio, &pkt->orig_bios, &pkt->orig_bios_tail);
966 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
967 spin_unlock(&pkt->lock);
968 }
969 spin_unlock(&pd->lock);
970
971 pkt->sleep_time = max(PACKET_WAIT_TIME, 1);
972 pkt_set_state(pkt, PACKET_WAITING_STATE);
973 atomic_set(&pkt->run_sm, 1);
974
975 spin_lock(&pd->cdrw.active_list_lock);
976 list_add(&pkt->list, &pd->cdrw.pkt_active_list);
977 spin_unlock(&pd->cdrw.active_list_lock);
978
979 return 1;
980}
981
982/*
983 * Assemble a bio to write one packet and queue the bio for processing
984 * by the underlying block device.
985 */
986static void pkt_start_write(struct pktcdvd_device *pd, struct packet_data *pkt)
987{
988 struct bio *bio;
989 struct page *pages[PACKET_MAX_SIZE];
990 int offsets[PACKET_MAX_SIZE];
991 int f;
992 int frames_write;
993
994 for (f = 0; f < pkt->frames; f++) {
995 pages[f] = pkt->pages[(f * CD_FRAMESIZE) / PAGE_SIZE];
996 offsets[f] = (f * CD_FRAMESIZE) % PAGE_SIZE;
997 }
998
999 /*
1000 * Fill-in pages[] and offsets[] with data from orig_bios.
1001 */
1002 frames_write = 0;
1003 spin_lock(&pkt->lock);
1004 for (bio = pkt->orig_bios; bio; bio = bio->bi_next) {
1005 int segment = bio->bi_idx;
1006 int src_offs = 0;
1007 int first_frame = (bio->bi_sector - pkt->sector) / (CD_FRAMESIZE >> 9);
1008 int num_frames = bio->bi_size / CD_FRAMESIZE;
1009 BUG_ON(first_frame < 0);
1010 BUG_ON(first_frame + num_frames > pkt->frames);
1011 for (f = first_frame; f < first_frame + num_frames; f++) {
1012 struct bio_vec *src_bvl = bio_iovec_idx(bio, segment);
1013
1014 while (src_offs >= src_bvl->bv_len) {
1015 src_offs -= src_bvl->bv_len;
1016 segment++;
1017 BUG_ON(segment >= bio->bi_vcnt);
1018 src_bvl = bio_iovec_idx(bio, segment);
1019 }
1020
1021 if (src_bvl->bv_len - src_offs >= CD_FRAMESIZE) {
1022 pages[f] = src_bvl->bv_page;
1023 offsets[f] = src_bvl->bv_offset + src_offs;
1024 } else {
1025 pkt_copy_bio_data(bio, segment, src_offs,
1026 pages[f], offsets[f]);
1027 }
1028 src_offs += CD_FRAMESIZE;
1029 frames_write++;
1030 }
1031 }
1032 pkt_set_state(pkt, PACKET_WRITE_WAIT_STATE);
1033 spin_unlock(&pkt->lock);
1034
1035 VPRINTK("pkt_start_write: Writing %d frames for zone %llx\n",
1036 frames_write, (unsigned long long)pkt->sector);
1037 BUG_ON(frames_write != pkt->write_size);
1038
1039 if (test_bit(PACKET_MERGE_SEGS, &pd->flags) || (pkt->write_size < pkt->frames)) {
1040 pkt_make_local_copy(pkt, pages, offsets);
1041 pkt->cache_valid = 1;
1042 } else {
1043 pkt->cache_valid = 0;
1044 }
1045
1046 /* Start the write request */
1047 bio_init(pkt->w_bio);
1048 pkt->w_bio->bi_max_vecs = PACKET_MAX_SIZE;
1049 pkt->w_bio->bi_sector = pkt->sector;
1050 pkt->w_bio->bi_bdev = pd->bdev;
1051 pkt->w_bio->bi_end_io = pkt_end_io_packet_write;
1052 pkt->w_bio->bi_private = pkt;
1053 for (f = 0; f < pkt->frames; f++) {
1054 if ((f + 1 < pkt->frames) && (pages[f + 1] == pages[f]) &&
1055 (offsets[f + 1] = offsets[f] + CD_FRAMESIZE)) {
1056 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE * 2, offsets[f]))
1057 BUG();
1058 f++;
1059 } else {
1060 if (!bio_add_page(pkt->w_bio, pages[f], CD_FRAMESIZE, offsets[f]))
1061 BUG();
1062 }
1063 }
1064 VPRINTK("pktcdvd: vcnt=%d\n", pkt->w_bio->bi_vcnt);
1065
1066 atomic_set(&pkt->io_wait, 1);
1067 pkt->w_bio->bi_rw = WRITE;
Peter Osterlund46c271b2005-06-23 00:10:02 -07001068 pkt_queue_bio(pd, pkt->w_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071static void pkt_finish_packet(struct packet_data *pkt, int uptodate)
1072{
1073 struct bio *bio, *next;
1074
1075 if (!uptodate)
1076 pkt->cache_valid = 0;
1077
1078 /* Finish all bios corresponding to this packet */
1079 bio = pkt->orig_bios;
1080 while (bio) {
1081 next = bio->bi_next;
1082 bio->bi_next = NULL;
1083 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
1084 bio = next;
1085 }
1086 pkt->orig_bios = pkt->orig_bios_tail = NULL;
1087}
1088
1089static void pkt_run_state_machine(struct pktcdvd_device *pd, struct packet_data *pkt)
1090{
1091 int uptodate;
1092
1093 VPRINTK("run_state_machine: pkt %d\n", pkt->id);
1094
1095 for (;;) {
1096 switch (pkt->state) {
1097 case PACKET_WAITING_STATE:
1098 if ((pkt->write_size < pkt->frames) && (pkt->sleep_time > 0))
1099 return;
1100
1101 pkt->sleep_time = 0;
1102 pkt_gather_data(pd, pkt);
1103 pkt_set_state(pkt, PACKET_READ_WAIT_STATE);
1104 break;
1105
1106 case PACKET_READ_WAIT_STATE:
1107 if (atomic_read(&pkt->io_wait) > 0)
1108 return;
1109
1110 if (atomic_read(&pkt->io_errors) > 0) {
1111 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1112 } else {
1113 pkt_start_write(pd, pkt);
1114 }
1115 break;
1116
1117 case PACKET_WRITE_WAIT_STATE:
1118 if (atomic_read(&pkt->io_wait) > 0)
1119 return;
1120
1121 if (test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags)) {
1122 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1123 } else {
1124 pkt_set_state(pkt, PACKET_RECOVERY_STATE);
1125 }
1126 break;
1127
1128 case PACKET_RECOVERY_STATE:
1129 if (pkt_start_recovery(pkt)) {
1130 pkt_start_write(pd, pkt);
1131 } else {
1132 VPRINTK("No recovery possible\n");
1133 pkt_set_state(pkt, PACKET_FINISHED_STATE);
1134 }
1135 break;
1136
1137 case PACKET_FINISHED_STATE:
1138 uptodate = test_bit(BIO_UPTODATE, &pkt->w_bio->bi_flags);
1139 pkt_finish_packet(pkt, uptodate);
1140 return;
1141
1142 default:
1143 BUG();
1144 break;
1145 }
1146 }
1147}
1148
1149static void pkt_handle_packets(struct pktcdvd_device *pd)
1150{
1151 struct packet_data *pkt, *next;
1152
1153 VPRINTK("pkt_handle_packets\n");
1154
1155 /*
1156 * Run state machine for active packets
1157 */
1158 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1159 if (atomic_read(&pkt->run_sm) > 0) {
1160 atomic_set(&pkt->run_sm, 0);
1161 pkt_run_state_machine(pd, pkt);
1162 }
1163 }
1164
1165 /*
1166 * Move no longer active packets to the free list
1167 */
1168 spin_lock(&pd->cdrw.active_list_lock);
1169 list_for_each_entry_safe(pkt, next, &pd->cdrw.pkt_active_list, list) {
1170 if (pkt->state == PACKET_FINISHED_STATE) {
1171 list_del(&pkt->list);
1172 pkt_put_packet_data(pd, pkt);
1173 pkt_set_state(pkt, PACKET_IDLE_STATE);
1174 atomic_set(&pd->scan_queue, 1);
1175 }
1176 }
1177 spin_unlock(&pd->cdrw.active_list_lock);
1178}
1179
1180static void pkt_count_states(struct pktcdvd_device *pd, int *states)
1181{
1182 struct packet_data *pkt;
1183 int i;
1184
1185 for (i = 0; i <= PACKET_NUM_STATES; i++)
1186 states[i] = 0;
1187
1188 spin_lock(&pd->cdrw.active_list_lock);
1189 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1190 states[pkt->state]++;
1191 }
1192 spin_unlock(&pd->cdrw.active_list_lock);
1193}
1194
1195/*
1196 * kcdrwd is woken up when writes have been queued for one of our
1197 * registered devices
1198 */
1199static int kcdrwd(void *foobar)
1200{
1201 struct pktcdvd_device *pd = foobar;
1202 struct packet_data *pkt;
1203 long min_sleep_time, residue;
1204
1205 set_user_nice(current, -20);
1206
1207 for (;;) {
1208 DECLARE_WAITQUEUE(wait, current);
1209
1210 /*
1211 * Wait until there is something to do
1212 */
1213 add_wait_queue(&pd->wqueue, &wait);
1214 for (;;) {
1215 set_current_state(TASK_INTERRUPTIBLE);
1216
1217 /* Check if we need to run pkt_handle_queue */
1218 if (atomic_read(&pd->scan_queue) > 0)
1219 goto work_to_do;
1220
1221 /* Check if we need to run the state machine for some packet */
1222 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1223 if (atomic_read(&pkt->run_sm) > 0)
1224 goto work_to_do;
1225 }
1226
1227 /* Check if we need to process the iosched queues */
1228 if (atomic_read(&pd->iosched.attention) != 0)
1229 goto work_to_do;
1230
1231 /* Otherwise, go to sleep */
1232 if (PACKET_DEBUG > 1) {
1233 int states[PACKET_NUM_STATES];
1234 pkt_count_states(pd, states);
1235 VPRINTK("kcdrwd: i:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
1236 states[0], states[1], states[2], states[3],
1237 states[4], states[5]);
1238 }
1239
1240 min_sleep_time = MAX_SCHEDULE_TIMEOUT;
1241 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1242 if (pkt->sleep_time && pkt->sleep_time < min_sleep_time)
1243 min_sleep_time = pkt->sleep_time;
1244 }
1245
1246 generic_unplug_device(bdev_get_queue(pd->bdev));
1247
1248 VPRINTK("kcdrwd: sleeping\n");
1249 residue = schedule_timeout(min_sleep_time);
1250 VPRINTK("kcdrwd: wake up\n");
1251
1252 /* make swsusp happy with our thread */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001253 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
1256 if (!pkt->sleep_time)
1257 continue;
1258 pkt->sleep_time -= min_sleep_time - residue;
1259 if (pkt->sleep_time <= 0) {
1260 pkt->sleep_time = 0;
1261 atomic_inc(&pkt->run_sm);
1262 }
1263 }
1264
1265 if (signal_pending(current)) {
1266 flush_signals(current);
1267 }
1268 if (kthread_should_stop())
1269 break;
1270 }
1271work_to_do:
1272 set_current_state(TASK_RUNNING);
1273 remove_wait_queue(&pd->wqueue, &wait);
1274
1275 if (kthread_should_stop())
1276 break;
1277
1278 /*
1279 * if pkt_handle_queue returns true, we can queue
1280 * another request.
1281 */
1282 while (pkt_handle_queue(pd))
1283 ;
1284
1285 /*
1286 * Handle packet state machine
1287 */
1288 pkt_handle_packets(pd);
1289
1290 /*
1291 * Handle iosched queues
1292 */
1293 pkt_iosched_process_queue(pd);
1294 }
1295
1296 return 0;
1297}
1298
1299static void pkt_print_settings(struct pktcdvd_device *pd)
1300{
1301 printk("pktcdvd: %s packets, ", pd->settings.fp ? "Fixed" : "Variable");
1302 printk("%u blocks, ", pd->settings.size >> 2);
1303 printk("Mode-%c disc\n", pd->settings.block_mode == 8 ? '1' : '2');
1304}
1305
1306static int pkt_mode_sense(struct pktcdvd_device *pd, struct packet_command *cgc, int page_code, int page_control)
1307{
1308 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1309
1310 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1311 cgc->cmd[2] = page_code | (page_control << 6);
1312 cgc->cmd[7] = cgc->buflen >> 8;
1313 cgc->cmd[8] = cgc->buflen & 0xff;
1314 cgc->data_direction = CGC_DATA_READ;
1315 return pkt_generic_packet(pd, cgc);
1316}
1317
1318static int pkt_mode_select(struct pktcdvd_device *pd, struct packet_command *cgc)
1319{
1320 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1321 memset(cgc->buffer, 0, 2);
1322 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1323 cgc->cmd[1] = 0x10; /* PF */
1324 cgc->cmd[7] = cgc->buflen >> 8;
1325 cgc->cmd[8] = cgc->buflen & 0xff;
1326 cgc->data_direction = CGC_DATA_WRITE;
1327 return pkt_generic_packet(pd, cgc);
1328}
1329
1330static int pkt_get_disc_info(struct pktcdvd_device *pd, disc_information *di)
1331{
1332 struct packet_command cgc;
1333 int ret;
1334
1335 /* set up command and get the disc info */
1336 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
1337 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
1338 cgc.cmd[8] = cgc.buflen = 2;
1339 cgc.quiet = 1;
1340
1341 if ((ret = pkt_generic_packet(pd, &cgc)))
1342 return ret;
1343
1344 /* not all drives have the same disc_info length, so requeue
1345 * packet with the length the drive tells us it can supply
1346 */
1347 cgc.buflen = be16_to_cpu(di->disc_information_length) +
1348 sizeof(di->disc_information_length);
1349
1350 if (cgc.buflen > sizeof(disc_information))
1351 cgc.buflen = sizeof(disc_information);
1352
1353 cgc.cmd[8] = cgc.buflen;
1354 return pkt_generic_packet(pd, &cgc);
1355}
1356
1357static int pkt_get_track_info(struct pktcdvd_device *pd, __u16 track, __u8 type, track_information *ti)
1358{
1359 struct packet_command cgc;
1360 int ret;
1361
1362 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
1363 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
1364 cgc.cmd[1] = type & 3;
1365 cgc.cmd[4] = (track & 0xff00) >> 8;
1366 cgc.cmd[5] = track & 0xff;
1367 cgc.cmd[8] = 8;
1368 cgc.quiet = 1;
1369
1370 if ((ret = pkt_generic_packet(pd, &cgc)))
1371 return ret;
1372
1373 cgc.buflen = be16_to_cpu(ti->track_information_length) +
1374 sizeof(ti->track_information_length);
1375
1376 if (cgc.buflen > sizeof(track_information))
1377 cgc.buflen = sizeof(track_information);
1378
1379 cgc.cmd[8] = cgc.buflen;
1380 return pkt_generic_packet(pd, &cgc);
1381}
1382
1383static int pkt_get_last_written(struct pktcdvd_device *pd, long *last_written)
1384{
1385 disc_information di;
1386 track_information ti;
1387 __u32 last_track;
1388 int ret = -1;
1389
1390 if ((ret = pkt_get_disc_info(pd, &di)))
1391 return ret;
1392
1393 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
1394 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1395 return ret;
1396
1397 /* if this track is blank, try the previous. */
1398 if (ti.blank) {
1399 last_track--;
1400 if ((ret = pkt_get_track_info(pd, last_track, 1, &ti)))
1401 return ret;
1402 }
1403
1404 /* if last recorded field is valid, return it. */
1405 if (ti.lra_v) {
1406 *last_written = be32_to_cpu(ti.last_rec_address);
1407 } else {
1408 /* make it up instead */
1409 *last_written = be32_to_cpu(ti.track_start) +
1410 be32_to_cpu(ti.track_size);
1411 if (ti.free_blocks)
1412 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
1413 }
1414 return 0;
1415}
1416
1417/*
1418 * write mode select package based on pd->settings
1419 */
1420static int pkt_set_write_settings(struct pktcdvd_device *pd)
1421{
1422 struct packet_command cgc;
1423 struct request_sense sense;
1424 write_param_page *wp;
1425 char buffer[128];
1426 int ret, size;
1427
1428 /* doesn't apply to DVD+RW or DVD-RAM */
1429 if ((pd->mmc3_profile == 0x1a) || (pd->mmc3_profile == 0x12))
1430 return 0;
1431
1432 memset(buffer, 0, sizeof(buffer));
1433 init_cdrom_command(&cgc, buffer, sizeof(*wp), CGC_DATA_READ);
1434 cgc.sense = &sense;
1435 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1436 pkt_dump_sense(&cgc);
1437 return ret;
1438 }
1439
1440 size = 2 + ((buffer[0] << 8) | (buffer[1] & 0xff));
1441 pd->mode_offset = (buffer[6] << 8) | (buffer[7] & 0xff);
1442 if (size > sizeof(buffer))
1443 size = sizeof(buffer);
1444
1445 /*
1446 * now get it all
1447 */
1448 init_cdrom_command(&cgc, buffer, size, CGC_DATA_READ);
1449 cgc.sense = &sense;
1450 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WRITE_PARMS_PAGE, 0))) {
1451 pkt_dump_sense(&cgc);
1452 return ret;
1453 }
1454
1455 /*
1456 * write page is offset header + block descriptor length
1457 */
1458 wp = (write_param_page *) &buffer[sizeof(struct mode_page_header) + pd->mode_offset];
1459
1460 wp->fp = pd->settings.fp;
1461 wp->track_mode = pd->settings.track_mode;
1462 wp->write_type = pd->settings.write_type;
1463 wp->data_block_type = pd->settings.block_mode;
1464
1465 wp->multi_session = 0;
1466
1467#ifdef PACKET_USE_LS
1468 wp->link_size = 7;
1469 wp->ls_v = 1;
1470#endif
1471
1472 if (wp->data_block_type == PACKET_BLOCK_MODE1) {
1473 wp->session_format = 0;
1474 wp->subhdr2 = 0x20;
1475 } else if (wp->data_block_type == PACKET_BLOCK_MODE2) {
1476 wp->session_format = 0x20;
1477 wp->subhdr2 = 8;
1478#if 0
1479 wp->mcn[0] = 0x80;
1480 memcpy(&wp->mcn[1], PACKET_MCN, sizeof(wp->mcn) - 1);
1481#endif
1482 } else {
1483 /*
1484 * paranoia
1485 */
1486 printk("pktcdvd: write mode wrong %d\n", wp->data_block_type);
1487 return 1;
1488 }
1489 wp->packet_size = cpu_to_be32(pd->settings.size >> 2);
1490
1491 cgc.buflen = cgc.cmd[8] = size;
1492 if ((ret = pkt_mode_select(pd, &cgc))) {
1493 pkt_dump_sense(&cgc);
1494 return ret;
1495 }
1496
1497 pkt_print_settings(pd);
1498 return 0;
1499}
1500
1501/*
1502 * 0 -- we can write to this track, 1 -- we can't
1503 */
1504static int pkt_good_track(track_information *ti)
1505{
1506 /*
1507 * only good for CD-RW at the moment, not DVD-RW
1508 */
1509
1510 /*
1511 * FIXME: only for FP
1512 */
1513 if (ti->fp == 0)
1514 return 0;
1515
1516 /*
1517 * "good" settings as per Mt Fuji.
1518 */
1519 if (ti->rt == 0 && ti->blank == 0 && ti->packet == 1)
1520 return 0;
1521
1522 if (ti->rt == 0 && ti->blank == 1 && ti->packet == 1)
1523 return 0;
1524
1525 if (ti->rt == 1 && ti->blank == 0 && ti->packet == 1)
1526 return 0;
1527
1528 printk("pktcdvd: bad state %d-%d-%d\n", ti->rt, ti->blank, ti->packet);
1529 return 1;
1530}
1531
1532/*
1533 * 0 -- we can write to this disc, 1 -- we can't
1534 */
1535static int pkt_good_disc(struct pktcdvd_device *pd, disc_information *di)
1536{
1537 switch (pd->mmc3_profile) {
1538 case 0x0a: /* CD-RW */
1539 case 0xffff: /* MMC3 not supported */
1540 break;
1541 case 0x1a: /* DVD+RW */
1542 case 0x13: /* DVD-RW */
1543 case 0x12: /* DVD-RAM */
1544 return 0;
1545 default:
1546 printk("pktcdvd: Wrong disc profile (%x)\n", pd->mmc3_profile);
1547 return 1;
1548 }
1549
1550 /*
1551 * for disc type 0xff we should probably reserve a new track.
1552 * but i'm not sure, should we leave this to user apps? probably.
1553 */
1554 if (di->disc_type == 0xff) {
1555 printk("pktcdvd: Unknown disc. No track?\n");
1556 return 1;
1557 }
1558
1559 if (di->disc_type != 0x20 && di->disc_type != 0) {
1560 printk("pktcdvd: Wrong disc type (%x)\n", di->disc_type);
1561 return 1;
1562 }
1563
1564 if (di->erasable == 0) {
1565 printk("pktcdvd: Disc not erasable\n");
1566 return 1;
1567 }
1568
1569 if (di->border_status == PACKET_SESSION_RESERVED) {
1570 printk("pktcdvd: Can't write to last track (reserved)\n");
1571 return 1;
1572 }
1573
1574 return 0;
1575}
1576
1577static int pkt_probe_settings(struct pktcdvd_device *pd)
1578{
1579 struct packet_command cgc;
1580 unsigned char buf[12];
1581 disc_information di;
1582 track_information ti;
1583 int ret, track;
1584
1585 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1586 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
1587 cgc.cmd[8] = 8;
1588 ret = pkt_generic_packet(pd, &cgc);
1589 pd->mmc3_profile = ret ? 0xffff : buf[6] << 8 | buf[7];
1590
1591 memset(&di, 0, sizeof(disc_information));
1592 memset(&ti, 0, sizeof(track_information));
1593
1594 if ((ret = pkt_get_disc_info(pd, &di))) {
1595 printk("failed get_disc\n");
1596 return ret;
1597 }
1598
1599 if (pkt_good_disc(pd, &di))
1600 return -ENXIO;
1601
1602 switch (pd->mmc3_profile) {
1603 case 0x1a: /* DVD+RW */
1604 printk("pktcdvd: inserted media is DVD+RW\n");
1605 break;
1606 case 0x13: /* DVD-RW */
1607 printk("pktcdvd: inserted media is DVD-RW\n");
1608 break;
1609 case 0x12: /* DVD-RAM */
1610 printk("pktcdvd: inserted media is DVD-RAM\n");
1611 break;
1612 default:
1613 printk("pktcdvd: inserted media is CD-R%s\n", di.erasable ? "W" : "");
1614 break;
1615 }
1616 pd->type = di.erasable ? PACKET_CDRW : PACKET_CDR;
1617
1618 track = 1; /* (di.last_track_msb << 8) | di.last_track_lsb; */
1619 if ((ret = pkt_get_track_info(pd, track, 1, &ti))) {
1620 printk("pktcdvd: failed get_track\n");
1621 return ret;
1622 }
1623
1624 if (pkt_good_track(&ti)) {
1625 printk("pktcdvd: can't write to this track\n");
1626 return -ENXIO;
1627 }
1628
1629 /*
1630 * we keep packet size in 512 byte units, makes it easier to
1631 * deal with request calculations.
1632 */
1633 pd->settings.size = be32_to_cpu(ti.fixed_packet_size) << 2;
1634 if (pd->settings.size == 0) {
1635 printk("pktcdvd: detected zero packet size!\n");
1636 pd->settings.size = 128;
1637 }
Peter Osterlundd0272e72005-09-13 01:25:27 -07001638 if (pd->settings.size > PACKET_MAX_SECTORS) {
1639 printk("pktcdvd: packet size is too big\n");
1640 return -ENXIO;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 pd->settings.fp = ti.fp;
1643 pd->offset = (be32_to_cpu(ti.track_start) << 2) & (pd->settings.size - 1);
1644
1645 if (ti.nwa_v) {
1646 pd->nwa = be32_to_cpu(ti.next_writable);
1647 set_bit(PACKET_NWA_VALID, &pd->flags);
1648 }
1649
1650 /*
1651 * in theory we could use lra on -RW media as well and just zero
1652 * blocks that haven't been written yet, but in practice that
1653 * is just a no-go. we'll use that for -R, naturally.
1654 */
1655 if (ti.lra_v) {
1656 pd->lra = be32_to_cpu(ti.last_rec_address);
1657 set_bit(PACKET_LRA_VALID, &pd->flags);
1658 } else {
1659 pd->lra = 0xffffffff;
1660 set_bit(PACKET_LRA_VALID, &pd->flags);
1661 }
1662
1663 /*
1664 * fine for now
1665 */
1666 pd->settings.link_loss = 7;
1667 pd->settings.write_type = 0; /* packet */
1668 pd->settings.track_mode = ti.track_mode;
1669
1670 /*
1671 * mode1 or mode2 disc
1672 */
1673 switch (ti.data_mode) {
1674 case PACKET_MODE1:
1675 pd->settings.block_mode = PACKET_BLOCK_MODE1;
1676 break;
1677 case PACKET_MODE2:
1678 pd->settings.block_mode = PACKET_BLOCK_MODE2;
1679 break;
1680 default:
1681 printk("pktcdvd: unknown data mode\n");
1682 return 1;
1683 }
1684 return 0;
1685}
1686
1687/*
1688 * enable/disable write caching on drive
1689 */
1690static int pkt_write_caching(struct pktcdvd_device *pd, int set)
1691{
1692 struct packet_command cgc;
1693 struct request_sense sense;
1694 unsigned char buf[64];
1695 int ret;
1696
1697 memset(buf, 0, sizeof(buf));
1698 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1699 cgc.sense = &sense;
1700 cgc.buflen = pd->mode_offset + 12;
1701
1702 /*
1703 * caching mode page might not be there, so quiet this command
1704 */
1705 cgc.quiet = 1;
1706
1707 if ((ret = pkt_mode_sense(pd, &cgc, GPMODE_WCACHING_PAGE, 0)))
1708 return ret;
1709
1710 buf[pd->mode_offset + 10] |= (!!set << 2);
1711
1712 cgc.buflen = cgc.cmd[8] = 2 + ((buf[0] << 8) | (buf[1] & 0xff));
1713 ret = pkt_mode_select(pd, &cgc);
1714 if (ret) {
1715 printk("pktcdvd: write caching control failed\n");
1716 pkt_dump_sense(&cgc);
1717 } else if (!ret && set)
1718 printk("pktcdvd: enabled write caching on %s\n", pd->name);
1719 return ret;
1720}
1721
1722static int pkt_lock_door(struct pktcdvd_device *pd, int lockflag)
1723{
1724 struct packet_command cgc;
1725
1726 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1727 cgc.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1728 cgc.cmd[4] = lockflag ? 1 : 0;
1729 return pkt_generic_packet(pd, &cgc);
1730}
1731
1732/*
1733 * Returns drive maximum write speed
1734 */
1735static int pkt_get_max_speed(struct pktcdvd_device *pd, unsigned *write_speed)
1736{
1737 struct packet_command cgc;
1738 struct request_sense sense;
1739 unsigned char buf[256+18];
1740 unsigned char *cap_buf;
1741 int ret, offset;
1742
1743 memset(buf, 0, sizeof(buf));
1744 cap_buf = &buf[sizeof(struct mode_page_header) + pd->mode_offset];
1745 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_UNKNOWN);
1746 cgc.sense = &sense;
1747
1748 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1749 if (ret) {
1750 cgc.buflen = pd->mode_offset + cap_buf[1] + 2 +
1751 sizeof(struct mode_page_header);
1752 ret = pkt_mode_sense(pd, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1753 if (ret) {
1754 pkt_dump_sense(&cgc);
1755 return ret;
1756 }
1757 }
1758
1759 offset = 20; /* Obsoleted field, used by older drives */
1760 if (cap_buf[1] >= 28)
1761 offset = 28; /* Current write speed selected */
1762 if (cap_buf[1] >= 30) {
1763 /* If the drive reports at least one "Logical Unit Write
1764 * Speed Performance Descriptor Block", use the information
1765 * in the first block. (contains the highest speed)
1766 */
1767 int num_spdb = (cap_buf[30] << 8) + cap_buf[31];
1768 if (num_spdb > 0)
1769 offset = 34;
1770 }
1771
1772 *write_speed = (cap_buf[offset] << 8) | cap_buf[offset + 1];
1773 return 0;
1774}
1775
1776/* These tables from cdrecord - I don't have orange book */
1777/* standard speed CD-RW (1-4x) */
1778static char clv_to_speed[16] = {
1779 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1780 0, 2, 4, 6, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1781};
1782/* high speed CD-RW (-10x) */
1783static char hs_clv_to_speed[16] = {
1784 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1785 0, 2, 4, 6, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1786};
1787/* ultra high speed CD-RW */
1788static char us_clv_to_speed[16] = {
1789 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1790 0, 2, 4, 8, 0, 0,16, 0,24,32,40,48, 0, 0, 0, 0
1791};
1792
1793/*
1794 * reads the maximum media speed from ATIP
1795 */
1796static int pkt_media_speed(struct pktcdvd_device *pd, unsigned *speed)
1797{
1798 struct packet_command cgc;
1799 struct request_sense sense;
1800 unsigned char buf[64];
1801 unsigned int size, st, sp;
1802 int ret;
1803
1804 init_cdrom_command(&cgc, buf, 2, CGC_DATA_READ);
1805 cgc.sense = &sense;
1806 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1807 cgc.cmd[1] = 2;
1808 cgc.cmd[2] = 4; /* READ ATIP */
1809 cgc.cmd[8] = 2;
1810 ret = pkt_generic_packet(pd, &cgc);
1811 if (ret) {
1812 pkt_dump_sense(&cgc);
1813 return ret;
1814 }
1815 size = ((unsigned int) buf[0]<<8) + buf[1] + 2;
1816 if (size > sizeof(buf))
1817 size = sizeof(buf);
1818
1819 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1820 cgc.sense = &sense;
1821 cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1822 cgc.cmd[1] = 2;
1823 cgc.cmd[2] = 4;
1824 cgc.cmd[8] = size;
1825 ret = pkt_generic_packet(pd, &cgc);
1826 if (ret) {
1827 pkt_dump_sense(&cgc);
1828 return ret;
1829 }
1830
1831 if (!buf[6] & 0x40) {
1832 printk("pktcdvd: Disc type is not CD-RW\n");
1833 return 1;
1834 }
1835 if (!buf[6] & 0x4) {
1836 printk("pktcdvd: A1 values on media are not valid, maybe not CDRW?\n");
1837 return 1;
1838 }
1839
1840 st = (buf[6] >> 3) & 0x7; /* disc sub-type */
1841
1842 sp = buf[16] & 0xf; /* max speed from ATIP A1 field */
1843
1844 /* Info from cdrecord */
1845 switch (st) {
1846 case 0: /* standard speed */
1847 *speed = clv_to_speed[sp];
1848 break;
1849 case 1: /* high speed */
1850 *speed = hs_clv_to_speed[sp];
1851 break;
1852 case 2: /* ultra high speed */
1853 *speed = us_clv_to_speed[sp];
1854 break;
1855 default:
1856 printk("pktcdvd: Unknown disc sub-type %d\n",st);
1857 return 1;
1858 }
1859 if (*speed) {
1860 printk("pktcdvd: Max. media speed: %d\n",*speed);
1861 return 0;
1862 } else {
1863 printk("pktcdvd: Unknown speed %d for sub-type %d\n",sp,st);
1864 return 1;
1865 }
1866}
1867
1868static int pkt_perform_opc(struct pktcdvd_device *pd)
1869{
1870 struct packet_command cgc;
1871 struct request_sense sense;
1872 int ret;
1873
1874 VPRINTK("pktcdvd: Performing OPC\n");
1875
1876 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1877 cgc.sense = &sense;
1878 cgc.timeout = 60*HZ;
1879 cgc.cmd[0] = GPCMD_SEND_OPC;
1880 cgc.cmd[1] = 1;
1881 if ((ret = pkt_generic_packet(pd, &cgc)))
1882 pkt_dump_sense(&cgc);
1883 return ret;
1884}
1885
1886static int pkt_open_write(struct pktcdvd_device *pd)
1887{
1888 int ret;
1889 unsigned int write_speed, media_write_speed, read_speed;
1890
1891 if ((ret = pkt_probe_settings(pd))) {
1892 DPRINTK("pktcdvd: %s failed probe\n", pd->name);
1893 return -EIO;
1894 }
1895
1896 if ((ret = pkt_set_write_settings(pd))) {
1897 DPRINTK("pktcdvd: %s failed saving write settings\n", pd->name);
1898 return -EIO;
1899 }
1900
1901 pkt_write_caching(pd, USE_WCACHING);
1902
1903 if ((ret = pkt_get_max_speed(pd, &write_speed)))
1904 write_speed = 16 * 177;
1905 switch (pd->mmc3_profile) {
1906 case 0x13: /* DVD-RW */
1907 case 0x1a: /* DVD+RW */
1908 case 0x12: /* DVD-RAM */
1909 DPRINTK("pktcdvd: write speed %ukB/s\n", write_speed);
1910 break;
1911 default:
1912 if ((ret = pkt_media_speed(pd, &media_write_speed)))
1913 media_write_speed = 16;
1914 write_speed = min(write_speed, media_write_speed * 177);
1915 DPRINTK("pktcdvd: write speed %ux\n", write_speed / 176);
1916 break;
1917 }
1918 read_speed = write_speed;
1919
1920 if ((ret = pkt_set_speed(pd, write_speed, read_speed))) {
1921 DPRINTK("pktcdvd: %s couldn't set write speed\n", pd->name);
1922 return -EIO;
1923 }
1924 pd->write_speed = write_speed;
1925 pd->read_speed = read_speed;
1926
1927 if ((ret = pkt_perform_opc(pd))) {
1928 DPRINTK("pktcdvd: %s Optimum Power Calibration failed\n", pd->name);
1929 }
1930
1931 return 0;
1932}
1933
1934/*
1935 * called at open time.
1936 */
1937static int pkt_open_dev(struct pktcdvd_device *pd, int write)
1938{
1939 int ret;
1940 long lba;
1941 request_queue_t *q;
1942
1943 /*
1944 * We need to re-open the cdrom device without O_NONBLOCK to be able
1945 * to read/write from/to it. It is already opened in O_NONBLOCK mode
1946 * so bdget() can't fail.
1947 */
1948 bdget(pd->bdev->bd_dev);
1949 if ((ret = blkdev_get(pd->bdev, FMODE_READ, O_RDONLY)))
1950 goto out;
1951
1952 if ((ret = pkt_get_last_written(pd, &lba))) {
1953 printk("pktcdvd: pkt_get_last_written failed\n");
1954 goto out_putdev;
1955 }
1956
1957 set_capacity(pd->disk, lba << 2);
1958 set_capacity(pd->bdev->bd_disk, lba << 2);
1959 bd_set_size(pd->bdev, (loff_t)lba << 11);
1960
1961 q = bdev_get_queue(pd->bdev);
1962 if (write) {
1963 if ((ret = pkt_open_write(pd)))
1964 goto out_putdev;
1965 /*
1966 * Some CDRW drives can not handle writes larger than one packet,
1967 * even if the size is a multiple of the packet size.
1968 */
1969 spin_lock_irq(q->queue_lock);
1970 blk_queue_max_sectors(q, pd->settings.size);
1971 spin_unlock_irq(q->queue_lock);
1972 set_bit(PACKET_WRITABLE, &pd->flags);
1973 } else {
1974 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
1975 clear_bit(PACKET_WRITABLE, &pd->flags);
1976 }
1977
1978 if ((ret = pkt_set_segment_merging(pd, q)))
1979 goto out_putdev;
1980
1981 if (write)
1982 printk("pktcdvd: %lukB available on disc\n", lba << 1);
1983
1984 return 0;
1985
1986out_putdev:
1987 blkdev_put(pd->bdev);
1988out:
1989 return ret;
1990}
1991
1992/*
1993 * called when the device is closed. makes sure that the device flushes
1994 * the internal cache before we close.
1995 */
1996static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
1997{
1998 if (flush && pkt_flush_cache(pd))
1999 DPRINTK("pktcdvd: %s not flushing cache\n", pd->name);
2000
2001 pkt_lock_door(pd, 0);
2002
2003 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2004 blkdev_put(pd->bdev);
2005}
2006
2007static struct pktcdvd_device *pkt_find_dev_from_minor(int dev_minor)
2008{
2009 if (dev_minor >= MAX_WRITERS)
2010 return NULL;
2011 return pkt_devs[dev_minor];
2012}
2013
2014static int pkt_open(struct inode *inode, struct file *file)
2015{
2016 struct pktcdvd_device *pd = NULL;
2017 int ret;
2018
2019 VPRINTK("pktcdvd: entering open\n");
2020
2021 down(&ctl_mutex);
2022 pd = pkt_find_dev_from_minor(iminor(inode));
2023 if (!pd) {
2024 ret = -ENODEV;
2025 goto out;
2026 }
2027 BUG_ON(pd->refcnt < 0);
2028
2029 pd->refcnt++;
Peter Osterlund46f4e1b2005-05-20 13:59:06 -07002030 if (pd->refcnt > 1) {
2031 if ((file->f_mode & FMODE_WRITE) &&
2032 !test_bit(PACKET_WRITABLE, &pd->flags)) {
2033 ret = -EBUSY;
2034 goto out_dec;
2035 }
2036 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (pkt_open_dev(pd, file->f_mode & FMODE_WRITE)) {
2038 ret = -EIO;
2039 goto out_dec;
2040 }
2041 /*
2042 * needed here as well, since ext2 (among others) may change
2043 * the blocksize at mount time
2044 */
2045 set_blocksize(inode->i_bdev, CD_FRAMESIZE);
2046 }
2047
2048 up(&ctl_mutex);
2049 return 0;
2050
2051out_dec:
2052 pd->refcnt--;
2053out:
2054 VPRINTK("pktcdvd: failed open (%d)\n", ret);
2055 up(&ctl_mutex);
2056 return ret;
2057}
2058
2059static int pkt_close(struct inode *inode, struct file *file)
2060{
2061 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2062 int ret = 0;
2063
2064 down(&ctl_mutex);
2065 pd->refcnt--;
2066 BUG_ON(pd->refcnt < 0);
2067 if (pd->refcnt == 0) {
2068 int flush = test_bit(PACKET_WRITABLE, &pd->flags);
2069 pkt_release_dev(pd, flush);
2070 }
2071 up(&ctl_mutex);
2072 return ret;
2073}
2074
2075
2076static void *psd_pool_alloc(unsigned int __nocast gfp_mask, void *data)
2077{
2078 return kmalloc(sizeof(struct packet_stacked_data), gfp_mask);
2079}
2080
2081static void psd_pool_free(void *ptr, void *data)
2082{
2083 kfree(ptr);
2084}
2085
2086static int pkt_end_io_read_cloned(struct bio *bio, unsigned int bytes_done, int err)
2087{
2088 struct packet_stacked_data *psd = bio->bi_private;
2089 struct pktcdvd_device *pd = psd->pd;
2090
2091 if (bio->bi_size)
2092 return 1;
2093
2094 bio_put(bio);
2095 bio_endio(psd->bio, psd->bio->bi_size, err);
2096 mempool_free(psd, psd_pool);
2097 pkt_bio_finished(pd);
2098 return 0;
2099}
2100
2101static int pkt_make_request(request_queue_t *q, struct bio *bio)
2102{
2103 struct pktcdvd_device *pd;
2104 char b[BDEVNAME_SIZE];
2105 sector_t zone;
2106 struct packet_data *pkt;
2107 int was_empty, blocked_bio;
2108 struct pkt_rb_node *node;
2109
2110 pd = q->queuedata;
2111 if (!pd) {
2112 printk("pktcdvd: %s incorrect request queue\n", bdevname(bio->bi_bdev, b));
2113 goto end_io;
2114 }
2115
2116 /*
2117 * Clone READ bios so we can have our own bi_end_io callback.
2118 */
2119 if (bio_data_dir(bio) == READ) {
2120 struct bio *cloned_bio = bio_clone(bio, GFP_NOIO);
2121 struct packet_stacked_data *psd = mempool_alloc(psd_pool, GFP_NOIO);
2122
2123 psd->pd = pd;
2124 psd->bio = bio;
2125 cloned_bio->bi_bdev = pd->bdev;
2126 cloned_bio->bi_private = psd;
2127 cloned_bio->bi_end_io = pkt_end_io_read_cloned;
2128 pd->stats.secs_r += bio->bi_size >> 9;
Peter Osterlund46c271b2005-06-23 00:10:02 -07002129 pkt_queue_bio(pd, cloned_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return 0;
2131 }
2132
2133 if (!test_bit(PACKET_WRITABLE, &pd->flags)) {
2134 printk("pktcdvd: WRITE for ro device %s (%llu)\n",
2135 pd->name, (unsigned long long)bio->bi_sector);
2136 goto end_io;
2137 }
2138
2139 if (!bio->bi_size || (bio->bi_size % CD_FRAMESIZE)) {
2140 printk("pktcdvd: wrong bio size\n");
2141 goto end_io;
2142 }
2143
2144 blk_queue_bounce(q, &bio);
2145
2146 zone = ZONE(bio->bi_sector, pd);
2147 VPRINTK("pkt_make_request: start = %6llx stop = %6llx\n",
2148 (unsigned long long)bio->bi_sector,
2149 (unsigned long long)(bio->bi_sector + bio_sectors(bio)));
2150
2151 /* Check if we have to split the bio */
2152 {
2153 struct bio_pair *bp;
2154 sector_t last_zone;
2155 int first_sectors;
2156
2157 last_zone = ZONE(bio->bi_sector + bio_sectors(bio) - 1, pd);
2158 if (last_zone != zone) {
2159 BUG_ON(last_zone != zone + pd->settings.size);
2160 first_sectors = last_zone - bio->bi_sector;
2161 bp = bio_split(bio, bio_split_pool, first_sectors);
2162 BUG_ON(!bp);
2163 pkt_make_request(q, &bp->bio1);
2164 pkt_make_request(q, &bp->bio2);
2165 bio_pair_release(bp);
2166 return 0;
2167 }
2168 }
2169
2170 /*
2171 * If we find a matching packet in state WAITING or READ_WAIT, we can
2172 * just append this bio to that packet.
2173 */
2174 spin_lock(&pd->cdrw.active_list_lock);
2175 blocked_bio = 0;
2176 list_for_each_entry(pkt, &pd->cdrw.pkt_active_list, list) {
2177 if (pkt->sector == zone) {
2178 spin_lock(&pkt->lock);
2179 if ((pkt->state == PACKET_WAITING_STATE) ||
2180 (pkt->state == PACKET_READ_WAIT_STATE)) {
2181 pkt_add_list_last(bio, &pkt->orig_bios,
2182 &pkt->orig_bios_tail);
2183 pkt->write_size += bio->bi_size / CD_FRAMESIZE;
2184 if ((pkt->write_size >= pkt->frames) &&
2185 (pkt->state == PACKET_WAITING_STATE)) {
2186 atomic_inc(&pkt->run_sm);
2187 wake_up(&pd->wqueue);
2188 }
2189 spin_unlock(&pkt->lock);
2190 spin_unlock(&pd->cdrw.active_list_lock);
2191 return 0;
2192 } else {
2193 blocked_bio = 1;
2194 }
2195 spin_unlock(&pkt->lock);
2196 }
2197 }
2198 spin_unlock(&pd->cdrw.active_list_lock);
2199
2200 /*
2201 * No matching packet found. Store the bio in the work queue.
2202 */
2203 node = mempool_alloc(pd->rb_pool, GFP_NOIO);
2204 BUG_ON(!node);
2205 node->bio = bio;
2206 spin_lock(&pd->lock);
2207 BUG_ON(pd->bio_queue_size < 0);
2208 was_empty = (pd->bio_queue_size == 0);
2209 pkt_rbtree_insert(pd, node);
2210 spin_unlock(&pd->lock);
2211
2212 /*
2213 * Wake up the worker thread.
2214 */
2215 atomic_set(&pd->scan_queue, 1);
2216 if (was_empty) {
2217 /* This wake_up is required for correct operation */
2218 wake_up(&pd->wqueue);
2219 } else if (!list_empty(&pd->cdrw.pkt_free_list) && !blocked_bio) {
2220 /*
2221 * This wake up is not required for correct operation,
2222 * but improves performance in some cases.
2223 */
2224 wake_up(&pd->wqueue);
2225 }
2226 return 0;
2227end_io:
2228 bio_io_error(bio, bio->bi_size);
2229 return 0;
2230}
2231
2232
2233
2234static int pkt_merge_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *bvec)
2235{
2236 struct pktcdvd_device *pd = q->queuedata;
2237 sector_t zone = ZONE(bio->bi_sector, pd);
2238 int used = ((bio->bi_sector - zone) << 9) + bio->bi_size;
2239 int remaining = (pd->settings.size << 9) - used;
2240 int remaining2;
2241
2242 /*
2243 * A bio <= PAGE_SIZE must be allowed. If it crosses a packet
2244 * boundary, pkt_make_request() will split the bio.
2245 */
2246 remaining2 = PAGE_SIZE - bio->bi_size;
2247 remaining = max(remaining, remaining2);
2248
2249 BUG_ON(remaining < 0);
2250 return remaining;
2251}
2252
2253static void pkt_init_queue(struct pktcdvd_device *pd)
2254{
2255 request_queue_t *q = pd->disk->queue;
2256
2257 blk_queue_make_request(q, pkt_make_request);
2258 blk_queue_hardsect_size(q, CD_FRAMESIZE);
2259 blk_queue_max_sectors(q, PACKET_MAX_SECTORS);
2260 blk_queue_merge_bvec(q, pkt_merge_bvec);
2261 q->queuedata = pd;
2262}
2263
2264static int pkt_seq_show(struct seq_file *m, void *p)
2265{
2266 struct pktcdvd_device *pd = m->private;
2267 char *msg;
2268 char bdev_buf[BDEVNAME_SIZE];
2269 int states[PACKET_NUM_STATES];
2270
2271 seq_printf(m, "Writer %s mapped to %s:\n", pd->name,
2272 bdevname(pd->bdev, bdev_buf));
2273
2274 seq_printf(m, "\nSettings:\n");
2275 seq_printf(m, "\tpacket size:\t\t%dkB\n", pd->settings.size / 2);
2276
2277 if (pd->settings.write_type == 0)
2278 msg = "Packet";
2279 else
2280 msg = "Unknown";
2281 seq_printf(m, "\twrite type:\t\t%s\n", msg);
2282
2283 seq_printf(m, "\tpacket type:\t\t%s\n", pd->settings.fp ? "Fixed" : "Variable");
2284 seq_printf(m, "\tlink loss:\t\t%d\n", pd->settings.link_loss);
2285
2286 seq_printf(m, "\ttrack mode:\t\t%d\n", pd->settings.track_mode);
2287
2288 if (pd->settings.block_mode == PACKET_BLOCK_MODE1)
2289 msg = "Mode 1";
2290 else if (pd->settings.block_mode == PACKET_BLOCK_MODE2)
2291 msg = "Mode 2";
2292 else
2293 msg = "Unknown";
2294 seq_printf(m, "\tblock mode:\t\t%s\n", msg);
2295
2296 seq_printf(m, "\nStatistics:\n");
2297 seq_printf(m, "\tpackets started:\t%lu\n", pd->stats.pkt_started);
2298 seq_printf(m, "\tpackets ended:\t\t%lu\n", pd->stats.pkt_ended);
2299 seq_printf(m, "\twritten:\t\t%lukB\n", pd->stats.secs_w >> 1);
2300 seq_printf(m, "\tread gather:\t\t%lukB\n", pd->stats.secs_rg >> 1);
2301 seq_printf(m, "\tread:\t\t\t%lukB\n", pd->stats.secs_r >> 1);
2302
2303 seq_printf(m, "\nMisc:\n");
2304 seq_printf(m, "\treference count:\t%d\n", pd->refcnt);
2305 seq_printf(m, "\tflags:\t\t\t0x%lx\n", pd->flags);
2306 seq_printf(m, "\tread speed:\t\t%ukB/s\n", pd->read_speed);
2307 seq_printf(m, "\twrite speed:\t\t%ukB/s\n", pd->write_speed);
2308 seq_printf(m, "\tstart offset:\t\t%lu\n", pd->offset);
2309 seq_printf(m, "\tmode page offset:\t%u\n", pd->mode_offset);
2310
2311 seq_printf(m, "\nQueue state:\n");
2312 seq_printf(m, "\tbios queued:\t\t%d\n", pd->bio_queue_size);
2313 seq_printf(m, "\tbios pending:\t\t%d\n", atomic_read(&pd->cdrw.pending_bios));
2314 seq_printf(m, "\tcurrent sector:\t\t0x%llx\n", (unsigned long long)pd->current_sector);
2315
2316 pkt_count_states(pd, states);
2317 seq_printf(m, "\tstate:\t\t\ti:%d ow:%d rw:%d ww:%d rec:%d fin:%d\n",
2318 states[0], states[1], states[2], states[3], states[4], states[5]);
2319
2320 return 0;
2321}
2322
2323static int pkt_seq_open(struct inode *inode, struct file *file)
2324{
2325 return single_open(file, pkt_seq_show, PDE(inode)->data);
2326}
2327
2328static struct file_operations pkt_proc_fops = {
2329 .open = pkt_seq_open,
2330 .read = seq_read,
2331 .llseek = seq_lseek,
2332 .release = single_release
2333};
2334
2335static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2336{
2337 int i;
2338 int ret = 0;
2339 char b[BDEVNAME_SIZE];
2340 struct proc_dir_entry *proc;
2341 struct block_device *bdev;
2342
2343 if (pd->pkt_dev == dev) {
2344 printk("pktcdvd: Recursive setup not allowed\n");
2345 return -EBUSY;
2346 }
2347 for (i = 0; i < MAX_WRITERS; i++) {
2348 struct pktcdvd_device *pd2 = pkt_devs[i];
2349 if (!pd2)
2350 continue;
2351 if (pd2->bdev->bd_dev == dev) {
2352 printk("pktcdvd: %s already setup\n", bdevname(pd2->bdev, b));
2353 return -EBUSY;
2354 }
2355 if (pd2->pkt_dev == dev) {
2356 printk("pktcdvd: Can't chain pktcdvd devices\n");
2357 return -EBUSY;
2358 }
2359 }
2360
2361 bdev = bdget(dev);
2362 if (!bdev)
2363 return -ENOMEM;
2364 ret = blkdev_get(bdev, FMODE_READ, O_RDONLY | O_NONBLOCK);
2365 if (ret)
2366 return ret;
2367
2368 /* This is safe, since we have a reference from open(). */
2369 __module_get(THIS_MODULE);
2370
2371 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2372 printk("pktcdvd: not enough memory for buffers\n");
2373 ret = -ENOMEM;
2374 goto out_mem;
2375 }
2376
2377 pd->bdev = bdev;
2378 set_blocksize(bdev, CD_FRAMESIZE);
2379
2380 pkt_init_queue(pd);
2381
2382 atomic_set(&pd->cdrw.pending_bios, 0);
2383 pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->name);
2384 if (IS_ERR(pd->cdrw.thread)) {
2385 printk("pktcdvd: can't start kernel thread\n");
2386 ret = -ENOMEM;
2387 goto out_thread;
2388 }
2389
2390 proc = create_proc_entry(pd->name, 0, pkt_proc);
2391 if (proc) {
2392 proc->data = pd;
2393 proc->proc_fops = &pkt_proc_fops;
2394 }
2395 DPRINTK("pktcdvd: writer %s mapped to %s\n", pd->name, bdevname(bdev, b));
2396 return 0;
2397
2398out_thread:
2399 pkt_shrink_pktlist(pd);
2400out_mem:
2401 blkdev_put(bdev);
2402 /* This is safe: open() is still holding a reference. */
2403 module_put(THIS_MODULE);
2404 return ret;
2405}
2406
2407static int pkt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2408{
2409 struct pktcdvd_device *pd = inode->i_bdev->bd_disk->private_data;
2410
2411 VPRINTK("pkt_ioctl: cmd %x, dev %d:%d\n", cmd, imajor(inode), iminor(inode));
2412 BUG_ON(!pd);
2413
2414 switch (cmd) {
2415 /*
2416 * forward selected CDROM ioctls to CD-ROM, for UDF
2417 */
2418 case CDROMMULTISESSION:
2419 case CDROMREADTOCENTRY:
2420 case CDROM_LAST_WRITTEN:
2421 case CDROM_SEND_PACKET:
2422 case SCSI_IOCTL_SEND_COMMAND:
Peter Osterlund118326e2005-05-14 00:58:30 -07002423 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 case CDROMEJECT:
2426 /*
2427 * The door gets locked when the device is opened, so we
2428 * have to unlock it or else the eject command fails.
2429 */
2430 pkt_lock_door(pd, 0);
Peter Osterlund118326e2005-05-14 00:58:30 -07002431 return blkdev_ioctl(pd->bdev->bd_inode, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 default:
2434 printk("pktcdvd: Unknown ioctl for %s (%x)\n", pd->name, cmd);
2435 return -ENOTTY;
2436 }
2437
2438 return 0;
2439}
2440
2441static int pkt_media_changed(struct gendisk *disk)
2442{
2443 struct pktcdvd_device *pd = disk->private_data;
2444 struct gendisk *attached_disk;
2445
2446 if (!pd)
2447 return 0;
2448 if (!pd->bdev)
2449 return 0;
2450 attached_disk = pd->bdev->bd_disk;
2451 if (!attached_disk)
2452 return 0;
2453 return attached_disk->fops->media_changed(attached_disk);
2454}
2455
2456static struct block_device_operations pktcdvd_ops = {
2457 .owner = THIS_MODULE,
2458 .open = pkt_open,
2459 .release = pkt_close,
2460 .ioctl = pkt_ioctl,
2461 .media_changed = pkt_media_changed,
2462};
2463
2464/*
2465 * Set up mapping from pktcdvd device to CD-ROM device.
2466 */
2467static int pkt_setup_dev(struct pkt_ctrl_command *ctrl_cmd)
2468{
2469 int idx;
2470 int ret = -ENOMEM;
2471 struct pktcdvd_device *pd;
2472 struct gendisk *disk;
2473 dev_t dev = new_decode_dev(ctrl_cmd->dev);
2474
2475 for (idx = 0; idx < MAX_WRITERS; idx++)
2476 if (!pkt_devs[idx])
2477 break;
2478 if (idx == MAX_WRITERS) {
2479 printk("pktcdvd: max %d writers supported\n", MAX_WRITERS);
2480 return -EBUSY;
2481 }
2482
2483 pd = kmalloc(sizeof(struct pktcdvd_device), GFP_KERNEL);
2484 if (!pd)
2485 return ret;
2486 memset(pd, 0, sizeof(struct pktcdvd_device));
2487
2488 pd->rb_pool = mempool_create(PKT_RB_POOL_SIZE, pkt_rb_alloc, pkt_rb_free, NULL);
2489 if (!pd->rb_pool)
2490 goto out_mem;
2491
2492 disk = alloc_disk(1);
2493 if (!disk)
2494 goto out_mem;
2495 pd->disk = disk;
2496
2497 spin_lock_init(&pd->lock);
2498 spin_lock_init(&pd->iosched.lock);
2499 sprintf(pd->name, "pktcdvd%d", idx);
2500 init_waitqueue_head(&pd->wqueue);
2501 pd->bio_queue = RB_ROOT;
2502
2503 disk->major = pkt_major;
2504 disk->first_minor = idx;
2505 disk->fops = &pktcdvd_ops;
2506 disk->flags = GENHD_FL_REMOVABLE;
2507 sprintf(disk->disk_name, "pktcdvd%d", idx);
2508 disk->private_data = pd;
2509 disk->queue = blk_alloc_queue(GFP_KERNEL);
2510 if (!disk->queue)
2511 goto out_mem2;
2512
2513 pd->pkt_dev = MKDEV(disk->major, disk->first_minor);
2514 ret = pkt_new_dev(pd, dev);
2515 if (ret)
2516 goto out_new_dev;
2517
2518 add_disk(disk);
2519 pkt_devs[idx] = pd;
2520 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2521 return 0;
2522
2523out_new_dev:
2524 blk_put_queue(disk->queue);
2525out_mem2:
2526 put_disk(disk);
2527out_mem:
2528 if (pd->rb_pool)
2529 mempool_destroy(pd->rb_pool);
2530 kfree(pd);
2531 return ret;
2532}
2533
2534/*
2535 * Tear down mapping from pktcdvd device to CD-ROM device.
2536 */
2537static int pkt_remove_dev(struct pkt_ctrl_command *ctrl_cmd)
2538{
2539 struct pktcdvd_device *pd;
2540 int idx;
2541 dev_t pkt_dev = new_decode_dev(ctrl_cmd->pkt_dev);
2542
2543 for (idx = 0; idx < MAX_WRITERS; idx++) {
2544 pd = pkt_devs[idx];
2545 if (pd && (pd->pkt_dev == pkt_dev))
2546 break;
2547 }
2548 if (idx == MAX_WRITERS) {
2549 DPRINTK("pktcdvd: dev not setup\n");
2550 return -ENXIO;
2551 }
2552
2553 if (pd->refcnt > 0)
2554 return -EBUSY;
2555
2556 if (!IS_ERR(pd->cdrw.thread))
2557 kthread_stop(pd->cdrw.thread);
2558
2559 blkdev_put(pd->bdev);
2560
2561 pkt_shrink_pktlist(pd);
2562
2563 remove_proc_entry(pd->name, pkt_proc);
2564 DPRINTK("pktcdvd: writer %s unmapped\n", pd->name);
2565
2566 del_gendisk(pd->disk);
2567 blk_put_queue(pd->disk->queue);
2568 put_disk(pd->disk);
2569
2570 pkt_devs[idx] = NULL;
2571 mempool_destroy(pd->rb_pool);
2572 kfree(pd);
2573
2574 /* This is safe: open() is still holding a reference. */
2575 module_put(THIS_MODULE);
2576 return 0;
2577}
2578
2579static void pkt_get_status(struct pkt_ctrl_command *ctrl_cmd)
2580{
2581 struct pktcdvd_device *pd = pkt_find_dev_from_minor(ctrl_cmd->dev_index);
2582 if (pd) {
2583 ctrl_cmd->dev = new_encode_dev(pd->bdev->bd_dev);
2584 ctrl_cmd->pkt_dev = new_encode_dev(pd->pkt_dev);
2585 } else {
2586 ctrl_cmd->dev = 0;
2587 ctrl_cmd->pkt_dev = 0;
2588 }
2589 ctrl_cmd->num_devices = MAX_WRITERS;
2590}
2591
2592static int pkt_ctl_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2593{
2594 void __user *argp = (void __user *)arg;
2595 struct pkt_ctrl_command ctrl_cmd;
2596 int ret = 0;
2597
2598 if (cmd != PACKET_CTRL_CMD)
2599 return -ENOTTY;
2600
2601 if (copy_from_user(&ctrl_cmd, argp, sizeof(struct pkt_ctrl_command)))
2602 return -EFAULT;
2603
2604 switch (ctrl_cmd.command) {
2605 case PKT_CTRL_CMD_SETUP:
2606 if (!capable(CAP_SYS_ADMIN))
2607 return -EPERM;
2608 down(&ctl_mutex);
2609 ret = pkt_setup_dev(&ctrl_cmd);
2610 up(&ctl_mutex);
2611 break;
2612 case PKT_CTRL_CMD_TEARDOWN:
2613 if (!capable(CAP_SYS_ADMIN))
2614 return -EPERM;
2615 down(&ctl_mutex);
2616 ret = pkt_remove_dev(&ctrl_cmd);
2617 up(&ctl_mutex);
2618 break;
2619 case PKT_CTRL_CMD_STATUS:
2620 down(&ctl_mutex);
2621 pkt_get_status(&ctrl_cmd);
2622 up(&ctl_mutex);
2623 break;
2624 default:
2625 return -ENOTTY;
2626 }
2627
2628 if (copy_to_user(argp, &ctrl_cmd, sizeof(struct pkt_ctrl_command)))
2629 return -EFAULT;
2630 return ret;
2631}
2632
2633
2634static struct file_operations pkt_ctl_fops = {
2635 .ioctl = pkt_ctl_ioctl,
2636 .owner = THIS_MODULE,
2637};
2638
2639static struct miscdevice pkt_misc = {
2640 .minor = MISC_DYNAMIC_MINOR,
2641 .name = "pktcdvd",
2642 .devfs_name = "pktcdvd/control",
2643 .fops = &pkt_ctl_fops
2644};
2645
2646static int __init pkt_init(void)
2647{
2648 int ret;
2649
2650 psd_pool = mempool_create(PSD_POOL_SIZE, psd_pool_alloc, psd_pool_free, NULL);
2651 if (!psd_pool)
2652 return -ENOMEM;
2653
2654 ret = register_blkdev(pkt_major, "pktcdvd");
2655 if (ret < 0) {
2656 printk("pktcdvd: Unable to register block device\n");
2657 goto out2;
2658 }
2659 if (!pkt_major)
2660 pkt_major = ret;
2661
2662 ret = misc_register(&pkt_misc);
2663 if (ret) {
2664 printk("pktcdvd: Unable to register misc device\n");
2665 goto out;
2666 }
2667
2668 init_MUTEX(&ctl_mutex);
2669
2670 pkt_proc = proc_mkdir("pktcdvd", proc_root_driver);
2671
2672 DPRINTK("pktcdvd: %s\n", VERSION_CODE);
2673 return 0;
2674
2675out:
2676 unregister_blkdev(pkt_major, "pktcdvd");
2677out2:
2678 mempool_destroy(psd_pool);
2679 return ret;
2680}
2681
2682static void __exit pkt_exit(void)
2683{
2684 remove_proc_entry("pktcdvd", proc_root_driver);
2685 misc_deregister(&pkt_misc);
2686 unregister_blkdev(pkt_major, "pktcdvd");
2687 mempool_destroy(psd_pool);
2688}
2689
2690MODULE_DESCRIPTION("Packet writing layer for CD/DVD drives");
2691MODULE_AUTHOR("Jens Axboe <axboe@suse.de>");
2692MODULE_LICENSE("GPL");
2693
2694module_init(pkt_init);
2695module_exit(pkt_exit);