ata: pata_parport: implement set_devctl
[platform/kernel/linux-starfive.git] / drivers / ata / pata_parport / pata_parport.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2023 Ondrej Zary
4  * based on paride.c by Grant R. Guenther <grant@torque.net>
5  */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/parport.h>
9 #include "pata_parport.h"
10
11 #define DRV_NAME "pata_parport"
12
13 static DEFINE_IDR(parport_list);
14 static DEFINE_IDR(protocols);
15 static DEFINE_IDA(pata_parport_bus_dev_ids);
16 static DEFINE_MUTEX(pi_mutex);
17
18 static bool probe = true;
19 module_param(probe, bool, 0644);
20 MODULE_PARM_DESC(probe, "Enable automatic device probing (0=off, 1=on [default])");
21
22 /*
23  * libata drivers cannot sleep so this driver claims parport before activating
24  * the ata host and keeps it claimed (and protocol connected) until the ata
25  * host is removed. Unfortunately, this means that you cannot use any chained
26  * devices (neither other pata_parport devices nor a printer).
27  */
28 static void pi_connect(struct pi_adapter *pi)
29 {
30         parport_claim_or_block(pi->pardev);
31         pi->proto->connect(pi);
32 }
33
34 static void pi_disconnect(struct pi_adapter *pi)
35 {
36         pi->proto->disconnect(pi);
37         parport_release(pi->pardev);
38 }
39
40 static void pata_parport_dev_select(struct ata_port *ap, unsigned int device)
41 {
42         struct pi_adapter *pi = ap->host->private_data;
43         u8 tmp;
44
45         if (device == 0)
46                 tmp = ATA_DEVICE_OBS;
47         else
48                 tmp = ATA_DEVICE_OBS | ATA_DEV1;
49
50         pi->proto->write_regr(pi, 0, ATA_REG_DEVICE, tmp);
51         ata_sff_pause(ap);
52 }
53
54 static void pata_parport_set_devctl(struct ata_port *ap, u8 ctl)
55 {
56         struct pi_adapter *pi = ap->host->private_data;
57
58         pi->proto->write_regr(pi, 1, 6, ctl);
59 }
60
61 static bool pata_parport_devchk(struct ata_port *ap, unsigned int device)
62 {
63         struct pi_adapter *pi = ap->host->private_data;
64         u8 nsect, lbal;
65
66         pata_parport_dev_select(ap, device);
67
68         pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0x55);
69         pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0xaa);
70
71         pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0xaa);
72         pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0x55);
73
74         pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0x55);
75         pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0xaa);
76
77         nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
78         lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
79
80         return (nsect == 0x55) && (lbal == 0xaa);
81 }
82
83 static int pata_parport_bus_softreset(struct ata_port *ap, unsigned int devmask,
84                                       unsigned long deadline)
85 {
86         struct pi_adapter *pi = ap->host->private_data;
87
88         /* software reset.  causes dev0 to be selected */
89         pi->proto->write_regr(pi, 1, 6, ap->ctl);
90         udelay(20);
91         pi->proto->write_regr(pi, 1, 6, ap->ctl | ATA_SRST);
92         udelay(20);
93         pi->proto->write_regr(pi, 1, 6, ap->ctl);
94         ap->last_ctl = ap->ctl;
95
96         /* wait the port to become ready */
97         return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
98 }
99
100 static int pata_parport_softreset(struct ata_link *link, unsigned int *classes,
101                                   unsigned long deadline)
102 {
103         struct ata_port *ap = link->ap;
104         unsigned int devmask = 0;
105         int rc;
106         u8 err;
107
108         /* determine if device 0/1 are present */
109         if (pata_parport_devchk(ap, 0))
110                 devmask |= (1 << 0);
111         if (pata_parport_devchk(ap, 1))
112                 devmask |= (1 << 1);
113
114         /* select device 0 again */
115         pata_parport_dev_select(ap, 0);
116
117         /* issue bus reset */
118         rc = pata_parport_bus_softreset(ap, devmask, deadline);
119         if (rc && rc != -ENODEV) {
120                 ata_link_err(link, "SRST failed (errno=%d)\n", rc);
121                 return rc;
122         }
123
124         /* determine by signature whether we have ATA or ATAPI devices */
125         classes[0] = ata_sff_dev_classify(&link->device[0],
126                                           devmask & (1 << 0), &err);
127         if (err != 0x81)
128                 classes[1] = ata_sff_dev_classify(&link->device[1],
129                                                   devmask & (1 << 1), &err);
130
131         return 0;
132 }
133
134 static u8 pata_parport_check_status(struct ata_port *ap)
135 {
136         struct pi_adapter *pi = ap->host->private_data;
137
138         return pi->proto->read_regr(pi, 0, ATA_REG_STATUS);
139 }
140
141 static u8 pata_parport_check_altstatus(struct ata_port *ap)
142 {
143         struct pi_adapter *pi = ap->host->private_data;
144
145         return pi->proto->read_regr(pi, 1, 6);
146 }
147
148 static void pata_parport_tf_load(struct ata_port *ap,
149                                  const struct ata_taskfile *tf)
150 {
151         struct pi_adapter *pi = ap->host->private_data;
152
153         if (tf->ctl != ap->last_ctl) {
154                 pi->proto->write_regr(pi, 1, 6, tf->ctl);
155                 ap->last_ctl = tf->ctl;
156                 ata_wait_idle(ap);
157         }
158
159         if (tf->flags & ATA_TFLAG_ISADDR) {
160                 if (tf->flags & ATA_TFLAG_LBA48) {
161                         pi->proto->write_regr(pi, 0, ATA_REG_FEATURE,
162                                               tf->hob_feature);
163                         pi->proto->write_regr(pi, 0, ATA_REG_NSECT,
164                                               tf->hob_nsect);
165                         pi->proto->write_regr(pi, 0, ATA_REG_LBAL,
166                                               tf->hob_lbal);
167                         pi->proto->write_regr(pi, 0, ATA_REG_LBAM,
168                                               tf->hob_lbam);
169                         pi->proto->write_regr(pi, 0, ATA_REG_LBAH,
170                                               tf->hob_lbah);
171                 }
172                 pi->proto->write_regr(pi, 0, ATA_REG_FEATURE, tf->feature);
173                 pi->proto->write_regr(pi, 0, ATA_REG_NSECT, tf->nsect);
174                 pi->proto->write_regr(pi, 0, ATA_REG_LBAL, tf->lbal);
175                 pi->proto->write_regr(pi, 0, ATA_REG_LBAM, tf->lbam);
176                 pi->proto->write_regr(pi, 0, ATA_REG_LBAH, tf->lbah);
177         }
178
179         if (tf->flags & ATA_TFLAG_DEVICE)
180                 pi->proto->write_regr(pi, 0, ATA_REG_DEVICE, tf->device);
181
182         ata_wait_idle(ap);
183 }
184
185 static void pata_parport_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
186 {
187         struct pi_adapter *pi = ap->host->private_data;
188
189         tf->status = pi->proto->read_regr(pi, 0, ATA_REG_STATUS);
190         tf->error = pi->proto->read_regr(pi, 0, ATA_REG_ERR);
191         tf->nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
192         tf->lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
193         tf->lbam = pi->proto->read_regr(pi, 0, ATA_REG_LBAM);
194         tf->lbah = pi->proto->read_regr(pi, 0, ATA_REG_LBAH);
195         tf->device = pi->proto->read_regr(pi, 0, ATA_REG_DEVICE);
196
197         if (tf->flags & ATA_TFLAG_LBA48) {
198                 pi->proto->write_regr(pi, 1, 6, tf->ctl | ATA_HOB);
199                 tf->hob_feature = pi->proto->read_regr(pi, 0, ATA_REG_ERR);
200                 tf->hob_nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
201                 tf->hob_lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
202                 tf->hob_lbam = pi->proto->read_regr(pi, 0, ATA_REG_LBAM);
203                 tf->hob_lbah = pi->proto->read_regr(pi, 0, ATA_REG_LBAH);
204                 pi->proto->write_regr(pi, 1, 6, tf->ctl);
205                 ap->last_ctl = tf->ctl;
206         }
207 }
208
209 static void pata_parport_exec_command(struct ata_port *ap,
210                                       const struct ata_taskfile *tf)
211 {
212         struct pi_adapter *pi = ap->host->private_data;
213
214         pi->proto->write_regr(pi, 0, ATA_REG_CMD, tf->command);
215         ata_sff_pause(ap);
216 }
217
218 static unsigned int pata_parport_data_xfer(struct ata_queued_cmd *qc,
219                                 unsigned char *buf, unsigned int buflen, int rw)
220 {
221         struct ata_port *ap = qc->dev->link->ap;
222         struct pi_adapter *pi = ap->host->private_data;
223
224         if (rw == READ)
225                 pi->proto->read_block(pi, buf, buflen);
226         else
227                 pi->proto->write_block(pi, buf, buflen);
228
229         return buflen;
230 }
231
232 static void pata_parport_drain_fifo(struct ata_queued_cmd *qc)
233 {
234         int count;
235         struct ata_port *ap;
236         struct pi_adapter *pi;
237         char junk[2];
238
239         /* We only need to flush incoming data when a command was running */
240         if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
241                 return;
242
243         ap = qc->ap;
244         pi = ap->host->private_data;
245         /* Drain up to 64K of data before we give up this recovery method */
246         for (count = 0; (pata_parport_check_status(ap) & ATA_DRQ)
247                                                 && count < 65536; count += 2) {
248                 pi->proto->read_block(pi, junk, 2);
249         }
250
251         if (count)
252                 ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
253 }
254
255 static struct ata_port_operations pata_parport_port_ops = {
256         .inherits               = &ata_sff_port_ops,
257
258         .softreset              = pata_parport_softreset,
259         .hardreset              = NULL,
260
261         .sff_dev_select         = pata_parport_dev_select,
262         .sff_set_devctl         = pata_parport_set_devctl,
263         .sff_check_status       = pata_parport_check_status,
264         .sff_check_altstatus    = pata_parport_check_altstatus,
265         .sff_tf_load            = pata_parport_tf_load,
266         .sff_tf_read            = pata_parport_tf_read,
267         .sff_exec_command       = pata_parport_exec_command,
268         .sff_data_xfer          = pata_parport_data_xfer,
269         .sff_drain_fifo         = pata_parport_drain_fifo,
270 };
271
272 static const struct ata_port_info pata_parport_port_info = {
273         .flags          = ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_POLLING,
274         .pio_mask       = ATA_PIO0,
275         /* No DMA */
276         .port_ops       = &pata_parport_port_ops,
277 };
278
279 static void pi_release(struct pi_adapter *pi)
280 {
281         parport_unregister_device(pi->pardev);
282         if (pi->proto->release_proto)
283                 pi->proto->release_proto(pi);
284         module_put(pi->proto->owner);
285 }
286
287 static int default_test_proto(struct pi_adapter *pi)
288 {
289         int j, k;
290         int e[2] = { 0, 0 };
291
292         pi->proto->connect(pi);
293
294         for (j = 0; j < 2; j++) {
295                 pi->proto->write_regr(pi, 0, 6, 0xa0 + j * 0x10);
296                 for (k = 0; k < 256; k++) {
297                         pi->proto->write_regr(pi, 0, 2, k ^ 0xaa);
298                         pi->proto->write_regr(pi, 0, 3, k ^ 0x55);
299                         if (pi->proto->read_regr(pi, 0, 2) != (k ^ 0xaa))
300                                 e[j]++;
301                 }
302         }
303         pi->proto->disconnect(pi);
304
305         dev_dbg(&pi->dev, "%s: port 0x%x, mode %d, test=(%d,%d)\n",
306                 pi->proto->name, pi->port, pi->mode, e[0], e[1]);
307
308         return e[0] && e[1];    /* not here if both > 0 */
309 }
310
311 static int pi_test_proto(struct pi_adapter *pi)
312 {
313         int res;
314
315         parport_claim_or_block(pi->pardev);
316         if (pi->proto->test_proto)
317                 res = pi->proto->test_proto(pi);
318         else
319                 res = default_test_proto(pi);
320         parport_release(pi->pardev);
321
322         return res;
323 }
324
325 static bool pi_probe_mode(struct pi_adapter *pi, int max)
326 {
327         int best, range;
328
329         if (pi->mode != -1) {
330                 if (pi->mode >= max)
331                         return false;
332                 range = 3;
333                 if (pi->mode >= pi->proto->epp_first)
334                         range = 8;
335                 if (range == 8 && pi->port % 8)
336                         return false;
337                 return !pi_test_proto(pi);
338         }
339         best = -1;
340         for (pi->mode = 0; pi->mode < max; pi->mode++) {
341                 range = 3;
342                 if (pi->mode >= pi->proto->epp_first)
343                         range = 8;
344                 if (range == 8 && pi->port % 8)
345                         break;
346                 if (!pi_test_proto(pi))
347                         best = pi->mode;
348         }
349         pi->mode = best;
350         return best > -1;
351 }
352
353 static bool pi_probe_unit(struct pi_adapter *pi, int unit)
354 {
355         int max, s, e;
356
357         s = unit;
358         e = s + 1;
359
360         if (s == -1) {
361                 s = 0;
362                 e = pi->proto->max_units;
363         }
364
365         if (pi->proto->test_port) {
366                 parport_claim_or_block(pi->pardev);
367                 max = pi->proto->test_port(pi);
368                 parport_release(pi->pardev);
369         } else {
370                 max = pi->proto->max_mode;
371         }
372
373         if (pi->proto->probe_unit) {
374                 parport_claim_or_block(pi->pardev);
375                 for (pi->unit = s; pi->unit < e; pi->unit++) {
376                         if (pi->proto->probe_unit(pi)) {
377                                 parport_release(pi->pardev);
378                                 return pi_probe_mode(pi, max);
379                         }
380                 }
381                 parport_release(pi->pardev);
382                 return false;
383         }
384
385         return pi_probe_mode(pi, max);
386 }
387
388 static void pata_parport_dev_release(struct device *dev)
389 {
390         struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
391
392         ida_free(&pata_parport_bus_dev_ids, dev->id);
393         kfree(pi);
394 }
395
396 static void pata_parport_bus_release(struct device *dev)
397 {
398         /* nothing to do here but required to avoid warning on device removal */
399 }
400
401 static struct bus_type pata_parport_bus_type = {
402         .name = DRV_NAME,
403 };
404
405 static struct device pata_parport_bus = {
406         .init_name = DRV_NAME,
407         .release = pata_parport_bus_release,
408 };
409
410 static const struct scsi_host_template pata_parport_sht = {
411         PATA_PARPORT_SHT("pata_parport")
412 };
413
414 struct pi_device_match {
415         struct parport *parport;
416         struct pi_protocol *proto;
417 };
418
419 static int pi_find_dev(struct device *dev, void *data)
420 {
421         struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
422         struct pi_device_match *match = data;
423
424         return pi->pardev->port == match->parport && pi->proto == match->proto;
425 }
426
427 static struct pi_adapter *pi_init_one(struct parport *parport,
428                         struct pi_protocol *pr, int mode, int unit, int delay)
429 {
430         struct pardev_cb par_cb = { };
431         const struct ata_port_info *ppi[] = { &pata_parport_port_info };
432         struct ata_host *host;
433         struct pi_adapter *pi;
434         struct pi_device_match match = { .parport = parport, .proto = pr };
435         int id;
436
437         /*
438          * Abort if there's a device already registered on the same parport
439          * using the same protocol.
440          */
441         if (bus_for_each_dev(&pata_parport_bus_type, NULL, &match, pi_find_dev))
442                 return NULL;
443
444         id = ida_alloc(&pata_parport_bus_dev_ids, GFP_KERNEL);
445         if (id < 0)
446                 return NULL;
447
448         pi = kzalloc(sizeof(struct pi_adapter), GFP_KERNEL);
449         if (!pi) {
450                 ida_free(&pata_parport_bus_dev_ids, id);
451                 return NULL;
452         }
453
454         /* set up pi->dev before pi_probe_unit() so it can use dev_printk() */
455         pi->dev.parent = &pata_parport_bus;
456         pi->dev.bus = &pata_parport_bus_type;
457         pi->dev.driver = &pr->driver;
458         pi->dev.release = pata_parport_dev_release;
459         pi->dev.id = id;
460         dev_set_name(&pi->dev, "pata_parport.%u", pi->dev.id);
461         if (device_register(&pi->dev)) {
462                 put_device(&pi->dev);
463                 /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
464                 return NULL;
465         }
466
467         pi->proto = pr;
468
469         if (!try_module_get(pi->proto->owner))
470                 goto out_unreg_dev;
471         if (pi->proto->init_proto && pi->proto->init_proto(pi) < 0)
472                 goto out_module_put;
473
474         pi->delay = (delay == -1) ? pi->proto->default_delay : delay;
475         pi->mode = mode;
476         pi->port = parport->base;
477
478         par_cb.private = pi;
479         pi->pardev = parport_register_dev_model(parport, DRV_NAME, &par_cb, id);
480         if (!pi->pardev)
481                 goto out_module_put;
482
483         if (!pi_probe_unit(pi, unit)) {
484                 dev_info(&pi->dev, "Adapter not found\n");
485                 goto out_unreg_parport;
486         }
487
488         pi->proto->log_adapter(pi);
489
490         host = ata_host_alloc_pinfo(&pi->pardev->dev, ppi, 1);
491         if (!host)
492                 goto out_unreg_parport;
493         dev_set_drvdata(&pi->dev, host);
494         host->private_data = pi;
495
496         ata_port_desc(host->ports[0], "port %s", pi->pardev->port->name);
497         ata_port_desc(host->ports[0], "protocol %s", pi->proto->name);
498
499         pi_connect(pi);
500         if (ata_host_activate(host, 0, NULL, 0, &pata_parport_sht))
501                 goto out_disconnect;
502
503         return pi;
504
505 out_disconnect:
506         pi_disconnect(pi);
507 out_unreg_parport:
508         parport_unregister_device(pi->pardev);
509         if (pi->proto->release_proto)
510                 pi->proto->release_proto(pi);
511 out_module_put:
512         module_put(pi->proto->owner);
513 out_unreg_dev:
514         device_unregister(&pi->dev);
515         /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
516         return NULL;
517 }
518
519 int pata_parport_register_driver(struct pi_protocol *pr)
520 {
521         int error;
522         struct parport *parport;
523         int port_num;
524
525         pr->driver.bus = &pata_parport_bus_type;
526         pr->driver.name = pr->name;
527         error = driver_register(&pr->driver);
528         if (error)
529                 return error;
530
531         mutex_lock(&pi_mutex);
532         error = idr_alloc(&protocols, pr, 0, 0, GFP_KERNEL);
533         if (error < 0) {
534                 driver_unregister(&pr->driver);
535                 mutex_unlock(&pi_mutex);
536                 return error;
537         }
538
539         pr_info("pata_parport: protocol %s registered\n", pr->name);
540
541         if (probe) {
542                 /* probe all parports using this protocol */
543                 idr_for_each_entry(&parport_list, parport, port_num)
544                         pi_init_one(parport, pr, -1, -1, -1);
545         }
546         mutex_unlock(&pi_mutex);
547
548         return 0;
549 }
550 EXPORT_SYMBOL_GPL(pata_parport_register_driver);
551
552 void pata_parport_unregister_driver(struct pi_protocol *pr)
553 {
554         struct pi_protocol *pr_iter;
555         int id = -1;
556
557         mutex_lock(&pi_mutex);
558         idr_for_each_entry(&protocols, pr_iter, id) {
559                 if (pr_iter == pr)
560                         break;
561         }
562         idr_remove(&protocols, id);
563         mutex_unlock(&pi_mutex);
564         driver_unregister(&pr->driver);
565 }
566 EXPORT_SYMBOL_GPL(pata_parport_unregister_driver);
567
568 static ssize_t new_device_store(const struct bus_type *bus, const char *buf, size_t count)
569 {
570         char port[12] = "auto";
571         char protocol[8] = "auto";
572         int mode = -1, unit = -1, delay = -1;
573         struct pi_protocol *pr, *pr_wanted;
574         struct device_driver *drv;
575         struct parport *parport;
576         int port_num, port_wanted, pr_num;
577         bool ok = false;
578
579         if (sscanf(buf, "%11s %7s %d %d %d",
580                         port, protocol, &mode, &unit, &delay) < 1)
581                 return -EINVAL;
582
583         if (sscanf(port, "parport%u", &port_wanted) < 1) {
584                 if (strcmp(port, "auto")) {
585                         pr_err("invalid port name %s\n", port);
586                         return -EINVAL;
587                 }
588                 port_wanted = -1;
589         }
590
591         drv = driver_find(protocol, &pata_parport_bus_type);
592         if (!drv) {
593                 if (strcmp(protocol, "auto")) {
594                         pr_err("protocol %s not found\n", protocol);
595                         return -EINVAL;
596                 }
597                 pr_wanted = NULL;
598         } else {
599                 pr_wanted = container_of(drv, struct pi_protocol, driver);
600         }
601
602         mutex_lock(&pi_mutex);
603         /* walk all parports */
604         idr_for_each_entry(&parport_list, parport, port_num) {
605                 if (port_num == port_wanted || port_wanted == -1) {
606                         parport = parport_find_number(port_num);
607                         if (!parport) {
608                                 pr_err("no such port %s\n", port);
609                                 mutex_unlock(&pi_mutex);
610                                 return -ENODEV;
611                         }
612                         /* walk all protocols */
613                         idr_for_each_entry(&protocols, pr, pr_num) {
614                                 if (pr == pr_wanted || !pr_wanted)
615                                         if (pi_init_one(parport, pr, mode, unit,
616                                                         delay))
617                                                 ok = true;
618                         }
619                         parport_put_port(parport);
620                 }
621         }
622         mutex_unlock(&pi_mutex);
623         if (!ok)
624                 return -ENODEV;
625
626         return count;
627 }
628 static BUS_ATTR_WO(new_device);
629
630 static void pi_remove_one(struct device *dev)
631 {
632         struct ata_host *host = dev_get_drvdata(dev);
633         struct pi_adapter *pi = host->private_data;
634
635         ata_host_detach(host);
636         pi_disconnect(pi);
637         pi_release(pi);
638         device_unregister(dev);
639         /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
640 }
641
642 static ssize_t delete_device_store(const struct bus_type *bus, const char *buf, size_t count)
643 {
644         struct device *dev;
645
646         mutex_lock(&pi_mutex);
647         dev = bus_find_device_by_name(bus, NULL, buf);
648         if (!dev) {
649                 mutex_unlock(&pi_mutex);
650                 return -ENODEV;
651         }
652
653         pi_remove_one(dev);
654         put_device(dev);
655         mutex_unlock(&pi_mutex);
656
657         return count;
658 }
659 static BUS_ATTR_WO(delete_device);
660
661 static void pata_parport_attach(struct parport *port)
662 {
663         struct pi_protocol *pr;
664         int pr_num, id;
665
666         mutex_lock(&pi_mutex);
667         id = idr_alloc(&parport_list, port, port->number, port->number,
668                        GFP_KERNEL);
669         if (id < 0) {
670                 mutex_unlock(&pi_mutex);
671                 return;
672         }
673
674         if (probe) {
675                 /* probe this port using all protocols */
676                 idr_for_each_entry(&protocols, pr, pr_num)
677                         pi_init_one(port, pr, -1, -1, -1);
678         }
679         mutex_unlock(&pi_mutex);
680 }
681
682 static int pi_remove_port(struct device *dev, void *p)
683 {
684         struct ata_host *host = dev_get_drvdata(dev);
685         struct pi_adapter *pi = host->private_data;
686
687         if (pi->pardev->port == p)
688                 pi_remove_one(dev);
689
690         return 0;
691 }
692
693 static void pata_parport_detach(struct parport *port)
694 {
695         mutex_lock(&pi_mutex);
696         bus_for_each_dev(&pata_parport_bus_type, NULL, port, pi_remove_port);
697         idr_remove(&parport_list, port->number);
698         mutex_unlock(&pi_mutex);
699 }
700
701 static struct parport_driver pata_parport_driver = {
702         .name = DRV_NAME,
703         .match_port = pata_parport_attach,
704         .detach = pata_parport_detach,
705         .devmodel = true,
706 };
707
708 static __init int pata_parport_init(void)
709 {
710         int error;
711
712         error = bus_register(&pata_parport_bus_type);
713         if (error) {
714                 pr_err("failed to register pata_parport bus, error: %d\n", error);
715                 return error;
716         }
717
718         error = device_register(&pata_parport_bus);
719         if (error) {
720                 pr_err("failed to register pata_parport bus, error: %d\n", error);
721                 goto out_unregister_bus;
722         }
723
724         error = bus_create_file(&pata_parport_bus_type, &bus_attr_new_device);
725         if (error) {
726                 pr_err("unable to create sysfs file, error: %d\n", error);
727                 goto out_unregister_dev;
728         }
729
730         error = bus_create_file(&pata_parport_bus_type, &bus_attr_delete_device);
731         if (error) {
732                 pr_err("unable to create sysfs file, error: %d\n", error);
733                 goto out_remove_new;
734         }
735
736         error = parport_register_driver(&pata_parport_driver);
737         if (error) {
738                 pr_err("unable to register parport driver, error: %d\n", error);
739                 goto out_remove_del;
740         }
741
742         return 0;
743
744 out_remove_del:
745         bus_remove_file(&pata_parport_bus_type, &bus_attr_delete_device);
746 out_remove_new:
747         bus_remove_file(&pata_parport_bus_type, &bus_attr_new_device);
748 out_unregister_dev:
749         device_unregister(&pata_parport_bus);
750 out_unregister_bus:
751         bus_unregister(&pata_parport_bus_type);
752         return error;
753 }
754
755 static __exit void pata_parport_exit(void)
756 {
757         parport_unregister_driver(&pata_parport_driver);
758         bus_remove_file(&pata_parport_bus_type, &bus_attr_new_device);
759         bus_remove_file(&pata_parport_bus_type, &bus_attr_delete_device);
760         device_unregister(&pata_parport_bus);
761         bus_unregister(&pata_parport_bus_type);
762 }
763
764 MODULE_AUTHOR("Ondrej Zary");
765 MODULE_DESCRIPTION("driver for parallel port ATA adapters");
766 MODULE_LICENSE("GPL");
767 MODULE_ALIAS("paride");
768
769 module_init(pata_parport_init);
770 module_exit(pata_parport_exit);