Merge tag 'fsverity-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt
[linux-2.6-block.git] / drivers / block / paride / pcd.c
1 /* 
2         pcd.c   (c) 1997-8  Grant R. Guenther <grant@torque.net>
3                             Under the terms of the GNU General Public License.
4
5         This is a high-level driver for parallel port ATAPI CD-ROM
6         drives based on chips supported by the paride module.
7
8         By default, the driver will autoprobe for a single parallel
9         port ATAPI CD-ROM drive, but if their individual parameters are
10         specified, the driver can handle up to 4 drives.
11
12         The behaviour of the pcd driver can be altered by setting
13         some parameters from the insmod command line.  The following
14         parameters are adjustable:
15
16             drive0      These four arguments can be arrays of       
17             drive1      1-6 integers as follows:
18             drive2
19             drive3      <prt>,<pro>,<uni>,<mod>,<slv>,<dly>
20
21                         Where,
22
23                 <prt>   is the base of the parallel port address for
24                         the corresponding drive.  (required)
25
26                 <pro>   is the protocol number for the adapter that
27                         supports this drive.  These numbers are
28                         logged by 'paride' when the protocol modules
29                         are initialised.  (0 if not given)
30
31                 <uni>   for those adapters that support chained
32                         devices, this is the unit selector for the
33                         chain of devices on the given port.  It should
34                         be zero for devices that don't support chaining.
35                         (0 if not given)
36
37                 <mod>   this can be -1 to choose the best mode, or one
38                         of the mode numbers supported by the adapter.
39                         (-1 if not given)
40
41                 <slv>   ATAPI CD-ROMs can be jumpered to master or slave.
42                         Set this to 0 to choose the master drive, 1 to
43                         choose the slave, -1 (the default) to choose the
44                         first drive found.
45
46                 <dly>   some parallel ports require the driver to 
47                         go more slowly.  -1 sets a default value that
48                         should work with the chosen protocol.  Otherwise,
49                         set this to a small integer, the larger it is
50                         the slower the port i/o.  In some cases, setting
51                         this to zero will speed up the device. (default -1)
52                         
53             major       You may use this parameter to override the
54                         default major number (46) that this driver
55                         will use.  Be sure to change the device
56                         name as well.
57
58             name        This parameter is a character string that
59                         contains the name the kernel will use for this
60                         device (in /proc output, for instance).
61                         (default "pcd")
62
63             verbose     This parameter controls the amount of logging
64                         that the driver will do.  Set it to 0 for
65                         normal operation, 1 to see autoprobe progress
66                         messages, or 2 to see additional debugging
67                         output.  (default 0)
68   
69             nice        This parameter controls the driver's use of
70                         idle CPU time, at the expense of some speed.
71  
72         If this driver is built into the kernel, you can use the
73         following kernel command line parameters, with the same values
74         as the corresponding module parameters listed above:
75
76             pcd.drive0
77             pcd.drive1
78             pcd.drive2
79             pcd.drive3
80             pcd.nice
81
82         In addition, you can use the parameter pcd.disable to disable
83         the driver entirely.
84
85 */
86
87 /* Changes:
88
89         1.01    GRG 1998.01.24  Added test unit ready support
90         1.02    GRG 1998.05.06  Changes to pcd_completion, ready_wait,
91                                 and loosen interpretation of ATAPI
92                                 standard for clearing error status.
93                                 Use spinlocks. Eliminate sti().
94         1.03    GRG 1998.06.16  Eliminated an Ugh
95         1.04    GRG 1998.08.15  Added extra debugging, improvements to
96                                 pcd_completion, use HZ in loop timing
97         1.05    GRG 1998.08.16  Conformed to "Uniform CD-ROM" standard
98         1.06    GRG 1998.08.19  Added audio ioctl support
99         1.07    GRG 1998.09.24  Increased reset timeout, added jumbo support
100
101 */
102
103 #define PCD_VERSION     "1.07"
104 #define PCD_MAJOR       46
105 #define PCD_NAME        "pcd"
106 #define PCD_UNITS       4
107
108 /* Here are things one can override from the insmod command.
109    Most are autoprobed by paride unless set here.  Verbose is off
110    by default.
111
112 */
113
114 static int verbose = 0;
115 static int major = PCD_MAJOR;
116 static char *name = PCD_NAME;
117 static int nice = 0;
118 static int disable = 0;
119
120 static int drive0[6] = { 0, 0, 0, -1, -1, -1 };
121 static int drive1[6] = { 0, 0, 0, -1, -1, -1 };
122 static int drive2[6] = { 0, 0, 0, -1, -1, -1 };
123 static int drive3[6] = { 0, 0, 0, -1, -1, -1 };
124
125 static int (*drives[4])[6] = {&drive0, &drive1, &drive2, &drive3};
126 static int pcd_drive_count;
127
128 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_SLV, D_DLY};
129
130 /* end of parameters */
131
132 #include <linux/module.h>
133 #include <linux/init.h>
134 #include <linux/errno.h>
135 #include <linux/fs.h>
136 #include <linux/kernel.h>
137 #include <linux/delay.h>
138 #include <linux/cdrom.h>
139 #include <linux/spinlock.h>
140 #include <linux/blk-mq.h>
141 #include <linux/mutex.h>
142 #include <linux/uaccess.h>
143
144 static DEFINE_MUTEX(pcd_mutex);
145 static DEFINE_SPINLOCK(pcd_lock);
146
147 module_param(verbose, int, 0644);
148 module_param(major, int, 0);
149 module_param(name, charp, 0);
150 module_param(nice, int, 0);
151 module_param_array(drive0, int, NULL, 0);
152 module_param_array(drive1, int, NULL, 0);
153 module_param_array(drive2, int, NULL, 0);
154 module_param_array(drive3, int, NULL, 0);
155
156 #include "paride.h"
157 #include "pseudo.h"
158
159 #define PCD_RETRIES          5
160 #define PCD_TMO            800  /* timeout in jiffies */
161 #define PCD_DELAY           50  /* spin delay in uS */
162 #define PCD_READY_TMO       20  /* in seconds */
163 #define PCD_RESET_TMO      100  /* in tenths of a second */
164
165 #define PCD_SPIN        (1000000*PCD_TMO)/(HZ*PCD_DELAY)
166
167 #define IDE_ERR         0x01
168 #define IDE_DRQ         0x08
169 #define IDE_READY       0x40
170 #define IDE_BUSY        0x80
171
172 static int pcd_open(struct cdrom_device_info *cdi, int purpose);
173 static void pcd_release(struct cdrom_device_info *cdi);
174 static int pcd_drive_status(struct cdrom_device_info *cdi, int slot_nr);
175 static unsigned int pcd_check_events(struct cdrom_device_info *cdi,
176                                      unsigned int clearing, int slot_nr);
177 static int pcd_tray_move(struct cdrom_device_info *cdi, int position);
178 static int pcd_lock_door(struct cdrom_device_info *cdi, int lock);
179 static int pcd_drive_reset(struct cdrom_device_info *cdi);
180 static int pcd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn);
181 static int pcd_audio_ioctl(struct cdrom_device_info *cdi,
182                            unsigned int cmd, void *arg);
183 static int pcd_packet(struct cdrom_device_info *cdi,
184                       struct packet_command *cgc);
185
186 static int pcd_detect(void);
187 static void pcd_probe_capabilities(void);
188 static void do_pcd_read_drq(void);
189 static blk_status_t pcd_queue_rq(struct blk_mq_hw_ctx *hctx,
190                                  const struct blk_mq_queue_data *bd);
191 static void do_pcd_read(void);
192
193 struct pcd_unit {
194         struct pi_adapter pia;  /* interface to paride layer */
195         struct pi_adapter *pi;
196         int drive;              /* master/slave */
197         int last_sense;         /* result of last request sense */
198         int changed;            /* media change seen */
199         int present;            /* does this unit exist ? */
200         char *name;             /* pcd0, pcd1, etc */
201         struct cdrom_device_info info;  /* uniform cdrom interface */
202         struct gendisk *disk;
203         struct blk_mq_tag_set tag_set;
204         struct list_head rq_list;
205 };
206
207 static struct pcd_unit pcd[PCD_UNITS];
208
209 static char pcd_scratch[64];
210 static char pcd_buffer[2048];   /* raw block buffer */
211 static int pcd_bufblk = -1;     /* block in buffer, in CD units,
212                                    -1 for nothing there. See also
213                                    pd_unit.
214                                  */
215
216 /* the variables below are used mainly in the I/O request engine, which
217    processes only one request at a time.
218 */
219
220 static struct pcd_unit *pcd_current; /* current request's drive */
221 static struct request *pcd_req;
222 static int pcd_retries;         /* retries on current request */
223 static int pcd_busy;            /* request being processed ? */
224 static int pcd_sector;          /* address of next requested sector */
225 static int pcd_count;           /* number of blocks still to do */
226 static char *pcd_buf;           /* buffer for request in progress */
227 static void *par_drv;           /* reference of parport driver */
228
229 /* kernel glue structures */
230
231 static int pcd_block_open(struct block_device *bdev, fmode_t mode)
232 {
233         struct pcd_unit *cd = bdev->bd_disk->private_data;
234         int ret;
235
236         check_disk_change(bdev);
237
238         mutex_lock(&pcd_mutex);
239         ret = cdrom_open(&cd->info, bdev, mode);
240         mutex_unlock(&pcd_mutex);
241
242         return ret;
243 }
244
245 static void pcd_block_release(struct gendisk *disk, fmode_t mode)
246 {
247         struct pcd_unit *cd = disk->private_data;
248         mutex_lock(&pcd_mutex);
249         cdrom_release(&cd->info, mode);
250         mutex_unlock(&pcd_mutex);
251 }
252
253 static int pcd_block_ioctl(struct block_device *bdev, fmode_t mode,
254                                 unsigned cmd, unsigned long arg)
255 {
256         struct pcd_unit *cd = bdev->bd_disk->private_data;
257         int ret;
258
259         mutex_lock(&pcd_mutex);
260         ret = cdrom_ioctl(&cd->info, bdev, mode, cmd, arg);
261         mutex_unlock(&pcd_mutex);
262
263         return ret;
264 }
265
266 static unsigned int pcd_block_check_events(struct gendisk *disk,
267                                            unsigned int clearing)
268 {
269         struct pcd_unit *cd = disk->private_data;
270         return cdrom_check_events(&cd->info, clearing);
271 }
272
273 static const struct block_device_operations pcd_bdops = {
274         .owner          = THIS_MODULE,
275         .open           = pcd_block_open,
276         .release        = pcd_block_release,
277         .ioctl          = pcd_block_ioctl,
278         .check_events   = pcd_block_check_events,
279 };
280
281 static const struct cdrom_device_ops pcd_dops = {
282         .open           = pcd_open,
283         .release        = pcd_release,
284         .drive_status   = pcd_drive_status,
285         .check_events   = pcd_check_events,
286         .tray_move      = pcd_tray_move,
287         .lock_door      = pcd_lock_door,
288         .get_mcn        = pcd_get_mcn,
289         .reset          = pcd_drive_reset,
290         .audio_ioctl    = pcd_audio_ioctl,
291         .generic_packet = pcd_packet,
292         .capability     = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
293                           CDC_MCN | CDC_MEDIA_CHANGED | CDC_RESET |
294                           CDC_PLAY_AUDIO | CDC_GENERIC_PACKET | CDC_CD_R |
295                           CDC_CD_RW,
296 };
297
298 static const struct blk_mq_ops pcd_mq_ops = {
299         .queue_rq       = pcd_queue_rq,
300 };
301
302 static void pcd_init_units(void)
303 {
304         struct pcd_unit *cd;
305         int unit;
306
307         pcd_drive_count = 0;
308         for (unit = 0, cd = pcd; unit < PCD_UNITS; unit++, cd++) {
309                 struct gendisk *disk = alloc_disk(1);
310
311                 if (!disk)
312                         continue;
313
314                 disk->queue = blk_mq_init_sq_queue(&cd->tag_set, &pcd_mq_ops,
315                                                    1, BLK_MQ_F_SHOULD_MERGE);
316                 if (IS_ERR(disk->queue)) {
317                         disk->queue = NULL;
318                         put_disk(disk);
319                         continue;
320                 }
321
322                 INIT_LIST_HEAD(&cd->rq_list);
323                 disk->queue->queuedata = cd;
324                 blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH);
325                 cd->disk = disk;
326                 cd->pi = &cd->pia;
327                 cd->present = 0;
328                 cd->last_sense = 0;
329                 cd->changed = 1;
330                 cd->drive = (*drives[unit])[D_SLV];
331                 if ((*drives[unit])[D_PRT])
332                         pcd_drive_count++;
333
334                 cd->name = &cd->info.name[0];
335                 snprintf(cd->name, sizeof(cd->info.name), "%s%d", name, unit);
336                 cd->info.ops = &pcd_dops;
337                 cd->info.handle = cd;
338                 cd->info.speed = 0;
339                 cd->info.capacity = 1;
340                 cd->info.mask = 0;
341                 disk->major = major;
342                 disk->first_minor = unit;
343                 strcpy(disk->disk_name, cd->name);      /* umm... */
344                 disk->fops = &pcd_bdops;
345                 disk->flags = GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
346                 disk->events = DISK_EVENT_MEDIA_CHANGE;
347         }
348 }
349
350 static int pcd_open(struct cdrom_device_info *cdi, int purpose)
351 {
352         struct pcd_unit *cd = cdi->handle;
353         if (!cd->present)
354                 return -ENODEV;
355         return 0;
356 }
357
358 static void pcd_release(struct cdrom_device_info *cdi)
359 {
360 }
361
362 static inline int status_reg(struct pcd_unit *cd)
363 {
364         return pi_read_regr(cd->pi, 1, 6);
365 }
366
367 static inline int read_reg(struct pcd_unit *cd, int reg)
368 {
369         return pi_read_regr(cd->pi, 0, reg);
370 }
371
372 static inline void write_reg(struct pcd_unit *cd, int reg, int val)
373 {
374         pi_write_regr(cd->pi, 0, reg, val);
375 }
376
377 static int pcd_wait(struct pcd_unit *cd, int go, int stop, char *fun, char *msg)
378 {
379         int j, r, e, s, p;
380
381         j = 0;
382         while ((((r = status_reg(cd)) & go) || (stop && (!(r & stop))))
383                && (j++ < PCD_SPIN))
384                 udelay(PCD_DELAY);
385
386         if ((r & (IDE_ERR & stop)) || (j > PCD_SPIN)) {
387                 s = read_reg(cd, 7);
388                 e = read_reg(cd, 1);
389                 p = read_reg(cd, 2);
390                 if (j > PCD_SPIN)
391                         e |= 0x100;
392                 if (fun)
393                         printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
394                                " loop=%d phase=%d\n",
395                                cd->name, fun, msg, r, s, e, j, p);
396                 return (s << 8) + r;
397         }
398         return 0;
399 }
400
401 static int pcd_command(struct pcd_unit *cd, char *cmd, int dlen, char *fun)
402 {
403         pi_connect(cd->pi);
404
405         write_reg(cd, 6, 0xa0 + 0x10 * cd->drive);
406
407         if (pcd_wait(cd, IDE_BUSY | IDE_DRQ, 0, fun, "before command")) {
408                 pi_disconnect(cd->pi);
409                 return -1;
410         }
411
412         write_reg(cd, 4, dlen % 256);
413         write_reg(cd, 5, dlen / 256);
414         write_reg(cd, 7, 0xa0); /* ATAPI packet command */
415
416         if (pcd_wait(cd, IDE_BUSY, IDE_DRQ, fun, "command DRQ")) {
417                 pi_disconnect(cd->pi);
418                 return -1;
419         }
420
421         if (read_reg(cd, 2) != 1) {
422                 printk("%s: %s: command phase error\n", cd->name, fun);
423                 pi_disconnect(cd->pi);
424                 return -1;
425         }
426
427         pi_write_block(cd->pi, cmd, 12);
428
429         return 0;
430 }
431
432 static int pcd_completion(struct pcd_unit *cd, char *buf, char *fun)
433 {
434         int r, d, p, n, k, j;
435
436         r = -1;
437         k = 0;
438         j = 0;
439
440         if (!pcd_wait(cd, IDE_BUSY, IDE_DRQ | IDE_READY | IDE_ERR,
441                       fun, "completion")) {
442                 r = 0;
443                 while (read_reg(cd, 7) & IDE_DRQ) {
444                         d = read_reg(cd, 4) + 256 * read_reg(cd, 5);
445                         n = (d + 3) & 0xfffc;
446                         p = read_reg(cd, 2) & 3;
447
448                         if ((p == 2) && (n > 0) && (j == 0)) {
449                                 pi_read_block(cd->pi, buf, n);
450                                 if (verbose > 1)
451                                         printk("%s: %s: Read %d bytes\n",
452                                                cd->name, fun, n);
453                                 r = 0;
454                                 j++;
455                         } else {
456                                 if (verbose > 1)
457                                         printk
458                                             ("%s: %s: Unexpected phase %d, d=%d, k=%d\n",
459                                              cd->name, fun, p, d, k);
460                                 if (verbose < 2)
461                                         printk_once(
462                                             "%s: WARNING: ATAPI phase errors\n",
463                                             cd->name);
464                                 mdelay(1);
465                         }
466                         if (k++ > PCD_TMO) {
467                                 printk("%s: Stuck DRQ\n", cd->name);
468                                 break;
469                         }
470                         if (pcd_wait
471                             (cd, IDE_BUSY, IDE_DRQ | IDE_READY | IDE_ERR, fun,
472                              "completion")) {
473                                 r = -1;
474                                 break;
475                         }
476                 }
477         }
478
479         pi_disconnect(cd->pi);
480
481         return r;
482 }
483
484 static void pcd_req_sense(struct pcd_unit *cd, char *fun)
485 {
486         char rs_cmd[12] = { 0x03, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
487         char buf[16];
488         int r, c;
489
490         r = pcd_command(cd, rs_cmd, 16, "Request sense");
491         mdelay(1);
492         if (!r)
493                 pcd_completion(cd, buf, "Request sense");
494
495         cd->last_sense = -1;
496         c = 2;
497         if (!r) {
498                 if (fun)
499                         printk("%s: %s: Sense key: %x, ASC: %x, ASQ: %x\n",
500                                cd->name, fun, buf[2] & 0xf, buf[12], buf[13]);
501                 c = buf[2] & 0xf;
502                 cd->last_sense =
503                     c | ((buf[12] & 0xff) << 8) | ((buf[13] & 0xff) << 16);
504         }
505         if ((c == 2) || (c == 6))
506                 cd->changed = 1;
507 }
508
509 static int pcd_atapi(struct pcd_unit *cd, char *cmd, int dlen, char *buf, char *fun)
510 {
511         int r;
512
513         r = pcd_command(cd, cmd, dlen, fun);
514         mdelay(1);
515         if (!r)
516                 r = pcd_completion(cd, buf, fun);
517         if (r)
518                 pcd_req_sense(cd, fun);
519
520         return r;
521 }
522
523 static int pcd_packet(struct cdrom_device_info *cdi, struct packet_command *cgc)
524 {
525         return pcd_atapi(cdi->handle, cgc->cmd, cgc->buflen, cgc->buffer,
526                          "generic packet");
527 }
528
529 #define DBMSG(msg)      ((verbose>1)?(msg):NULL)
530
531 static unsigned int pcd_check_events(struct cdrom_device_info *cdi,
532                                      unsigned int clearing, int slot_nr)
533 {
534         struct pcd_unit *cd = cdi->handle;
535         int res = cd->changed;
536         if (res)
537                 cd->changed = 0;
538         return res ? DISK_EVENT_MEDIA_CHANGE : 0;
539 }
540
541 static int pcd_lock_door(struct cdrom_device_info *cdi, int lock)
542 {
543         char un_cmd[12] = { 0x1e, 0, 0, 0, lock, 0, 0, 0, 0, 0, 0, 0 };
544
545         return pcd_atapi(cdi->handle, un_cmd, 0, pcd_scratch,
546                          lock ? "lock door" : "unlock door");
547 }
548
549 static int pcd_tray_move(struct cdrom_device_info *cdi, int position)
550 {
551         char ej_cmd[12] = { 0x1b, 0, 0, 0, 3 - position, 0, 0, 0, 0, 0, 0, 0 };
552
553         return pcd_atapi(cdi->handle, ej_cmd, 0, pcd_scratch,
554                          position ? "eject" : "close tray");
555 }
556
557 static void pcd_sleep(int cs)
558 {
559         schedule_timeout_interruptible(cs);
560 }
561
562 static int pcd_reset(struct pcd_unit *cd)
563 {
564         int i, k, flg;
565         int expect[5] = { 1, 1, 1, 0x14, 0xeb };
566
567         pi_connect(cd->pi);
568         write_reg(cd, 6, 0xa0 + 0x10 * cd->drive);
569         write_reg(cd, 7, 8);
570
571         pcd_sleep(20 * HZ / 1000);      /* delay a bit */
572
573         k = 0;
574         while ((k++ < PCD_RESET_TMO) && (status_reg(cd) & IDE_BUSY))
575                 pcd_sleep(HZ / 10);
576
577         flg = 1;
578         for (i = 0; i < 5; i++)
579                 flg &= (read_reg(cd, i + 1) == expect[i]);
580
581         if (verbose) {
582                 printk("%s: Reset (%d) signature = ", cd->name, k);
583                 for (i = 0; i < 5; i++)
584                         printk("%3x", read_reg(cd, i + 1));
585                 if (!flg)
586                         printk(" (incorrect)");
587                 printk("\n");
588         }
589
590         pi_disconnect(cd->pi);
591         return flg - 1;
592 }
593
594 static int pcd_drive_reset(struct cdrom_device_info *cdi)
595 {
596         return pcd_reset(cdi->handle);
597 }
598
599 static int pcd_ready_wait(struct pcd_unit *cd, int tmo)
600 {
601         char tr_cmd[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
602         int k, p;
603
604         k = 0;
605         while (k < tmo) {
606                 cd->last_sense = 0;
607                 pcd_atapi(cd, tr_cmd, 0, NULL, DBMSG("test unit ready"));
608                 p = cd->last_sense;
609                 if (!p)
610                         return 0;
611                 if (!(((p & 0xffff) == 0x0402) || ((p & 0xff) == 6)))
612                         return p;
613                 k++;
614                 pcd_sleep(HZ);
615         }
616         return 0x000020;        /* timeout */
617 }
618
619 static int pcd_drive_status(struct cdrom_device_info *cdi, int slot_nr)
620 {
621         char rc_cmd[12] = { 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
622         struct pcd_unit *cd = cdi->handle;
623
624         if (pcd_ready_wait(cd, PCD_READY_TMO))
625                 return CDS_DRIVE_NOT_READY;
626         if (pcd_atapi(cd, rc_cmd, 8, pcd_scratch, DBMSG("check media")))
627                 return CDS_NO_DISC;
628         return CDS_DISC_OK;
629 }
630
631 static int pcd_identify(struct pcd_unit *cd, char *id)
632 {
633         int k, s;
634         char id_cmd[12] = { 0x12, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0 };
635
636         pcd_bufblk = -1;
637
638         s = pcd_atapi(cd, id_cmd, 36, pcd_buffer, "identify");
639
640         if (s)
641                 return -1;
642         if ((pcd_buffer[0] & 0x1f) != 5) {
643                 if (verbose)
644                         printk("%s: %s is not a CD-ROM\n",
645                                cd->name, cd->drive ? "Slave" : "Master");
646                 return -1;
647         }
648         memcpy(id, pcd_buffer + 16, 16);
649         id[16] = 0;
650         k = 16;
651         while ((k >= 0) && (id[k] <= 0x20)) {
652                 id[k] = 0;
653                 k--;
654         }
655
656         printk("%s: %s: %s\n", cd->name, cd->drive ? "Slave" : "Master", id);
657
658         return 0;
659 }
660
661 /*
662  * returns  0, with id set if drive is detected
663  *          -1, if drive detection failed
664  */
665 static int pcd_probe(struct pcd_unit *cd, int ms, char *id)
666 {
667         if (ms == -1) {
668                 for (cd->drive = 0; cd->drive <= 1; cd->drive++)
669                         if (!pcd_reset(cd) && !pcd_identify(cd, id))
670                                 return 0;
671         } else {
672                 cd->drive = ms;
673                 if (!pcd_reset(cd) && !pcd_identify(cd, id))
674                         return 0;
675         }
676         return -1;
677 }
678
679 static void pcd_probe_capabilities(void)
680 {
681         int unit, r;
682         char buffer[32];
683         char cmd[12] = { 0x5a, 1 << 3, 0x2a, 0, 0, 0, 0, 18, 0, 0, 0, 0 };
684         struct pcd_unit *cd;
685
686         for (unit = 0, cd = pcd; unit < PCD_UNITS; unit++, cd++) {
687                 if (!cd->present)
688                         continue;
689                 r = pcd_atapi(cd, cmd, 18, buffer, "mode sense capabilities");
690                 if (r)
691                         continue;
692                 /* we should now have the cap page */
693                 if ((buffer[11] & 1) == 0)
694                         cd->info.mask |= CDC_CD_R;
695                 if ((buffer[11] & 2) == 0)
696                         cd->info.mask |= CDC_CD_RW;
697                 if ((buffer[12] & 1) == 0)
698                         cd->info.mask |= CDC_PLAY_AUDIO;
699                 if ((buffer[14] & 1) == 0)
700                         cd->info.mask |= CDC_LOCK;
701                 if ((buffer[14] & 8) == 0)
702                         cd->info.mask |= CDC_OPEN_TRAY;
703                 if ((buffer[14] >> 6) == 0)
704                         cd->info.mask |= CDC_CLOSE_TRAY;
705         }
706 }
707
708 static int pcd_detect(void)
709 {
710         char id[18];
711         int k, unit;
712         struct pcd_unit *cd;
713
714         printk("%s: %s version %s, major %d, nice %d\n",
715                name, name, PCD_VERSION, major, nice);
716
717         par_drv = pi_register_driver(name);
718         if (!par_drv) {
719                 pr_err("failed to register %s driver\n", name);
720                 return -1;
721         }
722
723         k = 0;
724         if (pcd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
725                 cd = pcd;
726                 if (cd->disk && pi_init(cd->pi, 1, -1, -1, -1, -1, -1,
727                             pcd_buffer, PI_PCD, verbose, cd->name)) {
728                         if (!pcd_probe(cd, -1, id)) {
729                                 cd->present = 1;
730                                 k++;
731                         } else
732                                 pi_release(cd->pi);
733                 }
734         } else {
735                 for (unit = 0, cd = pcd; unit < PCD_UNITS; unit++, cd++) {
736                         int *conf = *drives[unit];
737                         if (!conf[D_PRT])
738                                 continue;
739                         if (!cd->disk)
740                                 continue;
741                         if (!pi_init(cd->pi, 0, conf[D_PRT], conf[D_MOD],
742                                      conf[D_UNI], conf[D_PRO], conf[D_DLY],
743                                      pcd_buffer, PI_PCD, verbose, cd->name)) 
744                                 continue;
745                         if (!pcd_probe(cd, conf[D_SLV], id)) {
746                                 cd->present = 1;
747                                 k++;
748                         } else
749                                 pi_release(cd->pi);
750                 }
751         }
752         if (k)
753                 return 0;
754
755         printk("%s: No CD-ROM drive found\n", name);
756         for (unit = 0, cd = pcd; unit < PCD_UNITS; unit++, cd++) {
757                 if (!cd->disk)
758                         continue;
759                 blk_cleanup_queue(cd->disk->queue);
760                 cd->disk->queue = NULL;
761                 blk_mq_free_tag_set(&cd->tag_set);
762                 put_disk(cd->disk);
763         }
764         pi_unregister_driver(par_drv);
765         return -1;
766 }
767
768 /* I/O request processing */
769 static int pcd_queue;
770
771 static int set_next_request(void)
772 {
773         struct pcd_unit *cd;
774         int old_pos = pcd_queue;
775
776         do {
777                 cd = &pcd[pcd_queue];
778                 if (++pcd_queue == PCD_UNITS)
779                         pcd_queue = 0;
780                 if (cd->present && !list_empty(&cd->rq_list)) {
781                         pcd_req = list_first_entry(&cd->rq_list, struct request,
782                                                         queuelist);
783                         list_del_init(&pcd_req->queuelist);
784                         blk_mq_start_request(pcd_req);
785                         break;
786                 }
787         } while (pcd_queue != old_pos);
788
789         return pcd_req != NULL;
790 }
791
792 static void pcd_request(void)
793 {
794         struct pcd_unit *cd;
795
796         if (pcd_busy)
797                 return;
798
799         if (!pcd_req && !set_next_request())
800                 return;
801
802         cd = pcd_req->rq_disk->private_data;
803         if (cd != pcd_current)
804                 pcd_bufblk = -1;
805         pcd_current = cd;
806         pcd_sector = blk_rq_pos(pcd_req);
807         pcd_count = blk_rq_cur_sectors(pcd_req);
808         pcd_buf = bio_data(pcd_req->bio);
809         pcd_busy = 1;
810         ps_set_intr(do_pcd_read, NULL, 0, nice);
811 }
812
813 static blk_status_t pcd_queue_rq(struct blk_mq_hw_ctx *hctx,
814                                  const struct blk_mq_queue_data *bd)
815 {
816         struct pcd_unit *cd = hctx->queue->queuedata;
817
818         if (rq_data_dir(bd->rq) != READ) {
819                 blk_mq_start_request(bd->rq);
820                 return BLK_STS_IOERR;
821         }
822
823         spin_lock_irq(&pcd_lock);
824         list_add_tail(&bd->rq->queuelist, &cd->rq_list);
825         pcd_request();
826         spin_unlock_irq(&pcd_lock);
827
828         return BLK_STS_OK;
829 }
830
831 static inline void next_request(blk_status_t err)
832 {
833         unsigned long saved_flags;
834
835         spin_lock_irqsave(&pcd_lock, saved_flags);
836         if (!blk_update_request(pcd_req, err, blk_rq_cur_bytes(pcd_req))) {
837                 __blk_mq_end_request(pcd_req, err);
838                 pcd_req = NULL;
839         }
840         pcd_busy = 0;
841         pcd_request();
842         spin_unlock_irqrestore(&pcd_lock, saved_flags);
843 }
844
845 static int pcd_ready(void)
846 {
847         return (((status_reg(pcd_current) & (IDE_BUSY | IDE_DRQ)) == IDE_DRQ));
848 }
849
850 static void pcd_transfer(void)
851 {
852
853         while (pcd_count && (pcd_sector / 4 == pcd_bufblk)) {
854                 int o = (pcd_sector % 4) * 512;
855                 memcpy(pcd_buf, pcd_buffer + o, 512);
856                 pcd_count--;
857                 pcd_buf += 512;
858                 pcd_sector++;
859         }
860 }
861
862 static void pcd_start(void)
863 {
864         int b, i;
865         char rd_cmd[12] = { 0xa8, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 };
866
867         pcd_bufblk = pcd_sector / 4;
868         b = pcd_bufblk;
869         for (i = 0; i < 4; i++) {
870                 rd_cmd[5 - i] = b & 0xff;
871                 b = b >> 8;
872         }
873
874         if (pcd_command(pcd_current, rd_cmd, 2048, "read block")) {
875                 pcd_bufblk = -1;
876                 next_request(BLK_STS_IOERR);
877                 return;
878         }
879
880         mdelay(1);
881
882         ps_set_intr(do_pcd_read_drq, pcd_ready, PCD_TMO, nice);
883 }
884
885 static void do_pcd_read(void)
886 {
887         pcd_busy = 1;
888         pcd_retries = 0;
889         pcd_transfer();
890         if (!pcd_count) {
891                 next_request(0);
892                 return;
893         }
894
895         pi_do_claimed(pcd_current->pi, pcd_start);
896 }
897
898 static void do_pcd_read_drq(void)
899 {
900         unsigned long saved_flags;
901
902         if (pcd_completion(pcd_current, pcd_buffer, "read block")) {
903                 if (pcd_retries < PCD_RETRIES) {
904                         mdelay(1);
905                         pcd_retries++;
906                         pi_do_claimed(pcd_current->pi, pcd_start);
907                         return;
908                 }
909                 pcd_bufblk = -1;
910                 next_request(BLK_STS_IOERR);
911                 return;
912         }
913
914         do_pcd_read();
915         spin_lock_irqsave(&pcd_lock, saved_flags);
916         pcd_request();
917         spin_unlock_irqrestore(&pcd_lock, saved_flags);
918 }
919
920 /* the audio_ioctl stuff is adapted from sr_ioctl.c */
921
922 static int pcd_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
923 {
924         struct pcd_unit *cd = cdi->handle;
925
926         switch (cmd) {
927
928         case CDROMREADTOCHDR:
929
930                 {
931                         char cmd[12] =
932                             { GPCMD_READ_TOC_PMA_ATIP, 0, 0, 0, 0, 0, 0, 0, 12,
933                          0, 0, 0 };
934                         struct cdrom_tochdr *tochdr =
935                             (struct cdrom_tochdr *) arg;
936                         char buffer[32];
937                         int r;
938
939                         r = pcd_atapi(cd, cmd, 12, buffer, "read toc header");
940
941                         tochdr->cdth_trk0 = buffer[2];
942                         tochdr->cdth_trk1 = buffer[3];
943
944                         return r ? -EIO : 0;
945                 }
946
947         case CDROMREADTOCENTRY:
948
949                 {
950                         char cmd[12] =
951                             { GPCMD_READ_TOC_PMA_ATIP, 0, 0, 0, 0, 0, 0, 0, 12,
952                          0, 0, 0 };
953
954                         struct cdrom_tocentry *tocentry =
955                             (struct cdrom_tocentry *) arg;
956                         unsigned char buffer[32];
957                         int r;
958
959                         cmd[1] =
960                             (tocentry->cdte_format == CDROM_MSF ? 0x02 : 0);
961                         cmd[6] = tocentry->cdte_track;
962
963                         r = pcd_atapi(cd, cmd, 12, buffer, "read toc entry");
964
965                         tocentry->cdte_ctrl = buffer[5] & 0xf;
966                         tocentry->cdte_adr = buffer[5] >> 4;
967                         tocentry->cdte_datamode =
968                             (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
969                         if (tocentry->cdte_format == CDROM_MSF) {
970                                 tocentry->cdte_addr.msf.minute = buffer[9];
971                                 tocentry->cdte_addr.msf.second = buffer[10];
972                                 tocentry->cdte_addr.msf.frame = buffer[11];
973                         } else
974                                 tocentry->cdte_addr.lba =
975                                     (((((buffer[8] << 8) + buffer[9]) << 8)
976                                       + buffer[10]) << 8) + buffer[11];
977
978                         return r ? -EIO : 0;
979                 }
980
981         default:
982
983                 return -ENOSYS;
984         }
985 }
986
987 static int pcd_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
988 {
989         char cmd[12] =
990             { GPCMD_READ_SUBCHANNEL, 0, 0x40, 2, 0, 0, 0, 0, 24, 0, 0, 0 };
991         char buffer[32];
992
993         if (pcd_atapi(cdi->handle, cmd, 24, buffer, "get mcn"))
994                 return -EIO;
995
996         memcpy(mcn->medium_catalog_number, buffer + 9, 13);
997         mcn->medium_catalog_number[13] = 0;
998
999         return 0;
1000 }
1001
1002 static int __init pcd_init(void)
1003 {
1004         struct pcd_unit *cd;
1005         int unit;
1006
1007         if (disable)
1008                 return -EINVAL;
1009
1010         pcd_init_units();
1011
1012         if (pcd_detect())
1013                 return -ENODEV;
1014
1015         /* get the atapi capabilities page */
1016         pcd_probe_capabilities();
1017
1018         if (register_blkdev(major, name)) {
1019                 for (unit = 0, cd = pcd; unit < PCD_UNITS; unit++, cd++) {
1020                         if (!cd->disk)
1021                                 continue;
1022
1023                         blk_cleanup_queue(cd->disk->queue);
1024                         blk_mq_free_tag_set(&cd->tag_set);
1025                         put_disk(cd->disk);
1026                 }
1027                 return -EBUSY;
1028         }
1029
1030         for (unit = 0, cd = pcd; unit < PCD_UNITS; unit++, cd++) {
1031                 if (cd->present) {
1032                         register_cdrom(&cd->info);
1033                         cd->disk->private_data = cd;
1034                         add_disk(cd->disk);
1035                 }
1036         }
1037
1038         return 0;
1039 }
1040
1041 static void __exit pcd_exit(void)
1042 {
1043         struct pcd_unit *cd;
1044         int unit;
1045
1046         for (unit = 0, cd = pcd; unit < PCD_UNITS; unit++, cd++) {
1047                 if (!cd->disk)
1048                         continue;
1049
1050                 if (cd->present) {
1051                         del_gendisk(cd->disk);
1052                         pi_release(cd->pi);
1053                         unregister_cdrom(&cd->info);
1054                 }
1055                 blk_cleanup_queue(cd->disk->queue);
1056                 blk_mq_free_tag_set(&cd->tag_set);
1057                 put_disk(cd->disk);
1058         }
1059         unregister_blkdev(major, name);
1060         pi_unregister_driver(par_drv);
1061 }
1062
1063 MODULE_LICENSE("GPL");
1064 module_init(pcd_init)
1065 module_exit(pcd_exit)