1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2023 Ondrej Zary
4 * based on paride.c by Grant R. Guenther <grant@torque.net>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/parport.h>
9 #include <linux/pata_parport.h>
11 #define DRV_NAME "pata_parport"
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);
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])");
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).
28 static void pi_connect(struct pi_adapter *pi)
30 parport_claim_or_block(pi->pardev);
31 pi->proto->connect(pi);
34 static void pi_disconnect(struct pi_adapter *pi)
36 pi->proto->disconnect(pi);
37 parport_release(pi->pardev);
40 static void pata_parport_dev_select(struct ata_port *ap, unsigned int device)
42 struct pi_adapter *pi = ap->host->private_data;
48 tmp = ATA_DEVICE_OBS | ATA_DEV1;
50 pi->proto->write_regr(pi, 0, ATA_REG_DEVICE, tmp);
54 static bool pata_parport_devchk(struct ata_port *ap, unsigned int device)
56 struct pi_adapter *pi = ap->host->private_data;
59 pata_parport_dev_select(ap, device);
61 pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0x55);
62 pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0xaa);
64 pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 0xaa);
65 pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0x55);
67 pi->proto->write_regr(pi, 0, ATA_REG_NSECT, 055);
68 pi->proto->write_regr(pi, 0, ATA_REG_LBAL, 0xaa);
70 nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
71 lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
73 return (nsect == 0x55) && (lbal == 0xaa);
76 static int pata_parport_bus_softreset(struct ata_port *ap, unsigned int devmask,
77 unsigned long deadline)
79 struct pi_adapter *pi = ap->host->private_data;
81 /* software reset. causes dev0 to be selected */
82 pi->proto->write_regr(pi, 1, 6, ap->ctl);
84 pi->proto->write_regr(pi, 1, 6, ap->ctl | ATA_SRST);
86 pi->proto->write_regr(pi, 1, 6, ap->ctl);
87 ap->last_ctl = ap->ctl;
89 /* wait the port to become ready */
90 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
93 static int pata_parport_softreset(struct ata_link *link, unsigned int *classes,
94 unsigned long deadline)
96 struct ata_port *ap = link->ap;
97 unsigned int devmask = 0;
101 /* determine if device 0/1 are present */
102 if (pata_parport_devchk(ap, 0))
104 if (pata_parport_devchk(ap, 1))
107 /* select device 0 again */
108 pata_parport_dev_select(ap, 0);
110 /* issue bus reset */
111 rc = pata_parport_bus_softreset(ap, devmask, deadline);
112 if (rc && rc != -ENODEV) {
113 ata_link_err(link, "SRST failed (errno=%d)\n", rc);
117 /* determine by signature whether we have ATA or ATAPI devices */
118 classes[0] = ata_sff_dev_classify(&link->device[0],
119 devmask & (1 << 0), &err);
121 classes[1] = ata_sff_dev_classify(&link->device[1],
122 devmask & (1 << 1), &err);
127 static u8 pata_parport_check_status(struct ata_port *ap)
129 struct pi_adapter *pi = ap->host->private_data;
131 return pi->proto->read_regr(pi, 0, ATA_REG_STATUS);
134 static u8 pata_parport_check_altstatus(struct ata_port *ap)
136 struct pi_adapter *pi = ap->host->private_data;
138 return pi->proto->read_regr(pi, 1, 6);
141 static void pata_parport_tf_load(struct ata_port *ap,
142 const struct ata_taskfile *tf)
144 struct pi_adapter *pi = ap->host->private_data;
146 if (tf->ctl != ap->last_ctl) {
147 pi->proto->write_regr(pi, 1, 6, tf->ctl);
148 ap->last_ctl = tf->ctl;
152 if (tf->flags & ATA_TFLAG_ISADDR) {
153 if (tf->flags & ATA_TFLAG_LBA48) {
154 pi->proto->write_regr(pi, 0, ATA_REG_FEATURE,
156 pi->proto->write_regr(pi, 0, ATA_REG_NSECT,
158 pi->proto->write_regr(pi, 0, ATA_REG_LBAL,
160 pi->proto->write_regr(pi, 0, ATA_REG_LBAM,
162 pi->proto->write_regr(pi, 0, ATA_REG_LBAH,
165 pi->proto->write_regr(pi, 0, ATA_REG_FEATURE, tf->feature);
166 pi->proto->write_regr(pi, 0, ATA_REG_NSECT, tf->nsect);
167 pi->proto->write_regr(pi, 0, ATA_REG_LBAL, tf->lbal);
168 pi->proto->write_regr(pi, 0, ATA_REG_LBAM, tf->lbam);
169 pi->proto->write_regr(pi, 0, ATA_REG_LBAH, tf->lbah);
172 if (tf->flags & ATA_TFLAG_DEVICE)
173 pi->proto->write_regr(pi, 0, ATA_REG_DEVICE, tf->device);
178 static void pata_parport_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
180 struct pi_adapter *pi = ap->host->private_data;
182 tf->status = pi->proto->read_regr(pi, 0, ATA_REG_STATUS);
183 tf->error = pi->proto->read_regr(pi, 0, ATA_REG_ERR);
184 tf->nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
185 tf->lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
186 tf->lbam = pi->proto->read_regr(pi, 0, ATA_REG_LBAM);
187 tf->lbah = pi->proto->read_regr(pi, 0, ATA_REG_LBAH);
188 tf->device = pi->proto->read_regr(pi, 0, ATA_REG_DEVICE);
190 if (tf->flags & ATA_TFLAG_LBA48) {
191 pi->proto->write_regr(pi, 1, 6, tf->ctl | ATA_HOB);
192 tf->hob_feature = pi->proto->read_regr(pi, 0, ATA_REG_ERR);
193 tf->hob_nsect = pi->proto->read_regr(pi, 0, ATA_REG_NSECT);
194 tf->hob_lbal = pi->proto->read_regr(pi, 0, ATA_REG_LBAL);
195 tf->hob_lbam = pi->proto->read_regr(pi, 0, ATA_REG_LBAM);
196 tf->hob_lbah = pi->proto->read_regr(pi, 0, ATA_REG_LBAH);
197 pi->proto->write_regr(pi, 1, 6, tf->ctl);
198 ap->last_ctl = tf->ctl;
202 static void pata_parport_exec_command(struct ata_port *ap,
203 const struct ata_taskfile *tf)
205 struct pi_adapter *pi = ap->host->private_data;
207 pi->proto->write_regr(pi, 0, ATA_REG_CMD, tf->command);
211 static unsigned int pata_parport_data_xfer(struct ata_queued_cmd *qc,
212 unsigned char *buf, unsigned int buflen, int rw)
214 struct ata_port *ap = qc->dev->link->ap;
215 struct pi_adapter *pi = ap->host->private_data;
218 pi->proto->read_block(pi, buf, buflen);
220 pi->proto->write_block(pi, buf, buflen);
225 static void pata_parport_drain_fifo(struct ata_queued_cmd *qc)
229 struct pi_adapter *pi;
232 /* We only need to flush incoming data when a command was running */
233 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
237 pi = ap->host->private_data;
238 /* Drain up to 64K of data before we give up this recovery method */
239 for (count = 0; (pata_parport_check_status(ap) & ATA_DRQ)
240 && count < 65536; count += 2) {
241 pi->proto->read_block(pi, junk, 2);
245 ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
248 static struct ata_port_operations pata_parport_port_ops = {
249 .inherits = &ata_sff_port_ops,
251 .softreset = pata_parport_softreset,
254 .sff_dev_select = pata_parport_dev_select,
255 .sff_check_status = pata_parport_check_status,
256 .sff_check_altstatus = pata_parport_check_altstatus,
257 .sff_tf_load = pata_parport_tf_load,
258 .sff_tf_read = pata_parport_tf_read,
259 .sff_exec_command = pata_parport_exec_command,
260 .sff_data_xfer = pata_parport_data_xfer,
261 .sff_drain_fifo = pata_parport_drain_fifo,
264 static const struct ata_port_info pata_parport_port_info = {
265 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_PIO_POLLING,
266 .pio_mask = ATA_PIO0,
268 .port_ops = &pata_parport_port_ops,
271 static void pi_release(struct pi_adapter *pi)
273 parport_unregister_device(pi->pardev);
274 if (pi->proto->release_proto)
275 pi->proto->release_proto(pi);
276 module_put(pi->proto->owner);
279 static int default_test_proto(struct pi_adapter *pi, char *scratch)
284 pi->proto->connect(pi);
286 for (j = 0; j < 2; j++) {
287 pi->proto->write_regr(pi, 0, 6, 0xa0 + j * 0x10);
288 for (k = 0; k < 256; k++) {
289 pi->proto->write_regr(pi, 0, 2, k ^ 0xaa);
290 pi->proto->write_regr(pi, 0, 3, k ^ 0x55);
291 if (pi->proto->read_regr(pi, 0, 2) != (k ^ 0xaa))
295 pi->proto->disconnect(pi);
297 dev_dbg(&pi->dev, "%s: port 0x%x, mode %d, test=(%d,%d)\n",
298 pi->proto->name, pi->port, pi->mode, e[0], e[1]);
300 return e[0] && e[1]; /* not here if both > 0 */
303 static int pi_test_proto(struct pi_adapter *pi, char *scratch)
307 parport_claim_or_block(pi->pardev);
308 if (pi->proto->test_proto)
309 res = pi->proto->test_proto(pi, scratch, 1);
311 res = default_test_proto(pi, scratch);
312 parport_release(pi->pardev);
317 static bool pi_probe_mode(struct pi_adapter *pi, int max, char *scratch)
321 if (pi->mode != -1) {
325 if (pi->mode >= pi->proto->epp_first)
327 if (range == 8 && pi->port % 8)
329 return !pi_test_proto(pi, scratch);
332 for (pi->mode = 0; pi->mode < max; pi->mode++) {
334 if (pi->mode >= pi->proto->epp_first)
336 if (range == 8 && pi->port % 8)
338 if (!pi_test_proto(pi, scratch))
345 static bool pi_probe_unit(struct pi_adapter *pi, int unit, char *scratch)
354 e = pi->proto->max_units;
357 if (pi->proto->test_port) {
358 parport_claim_or_block(pi->pardev);
359 max = pi->proto->test_port(pi);
360 parport_release(pi->pardev);
362 max = pi->proto->max_mode;
365 if (pi->proto->probe_unit) {
366 parport_claim_or_block(pi->pardev);
367 for (pi->unit = s; pi->unit < e; pi->unit++) {
368 if (pi->proto->probe_unit(pi)) {
369 parport_release(pi->pardev);
370 return pi_probe_mode(pi, max, scratch);
373 parport_release(pi->pardev);
377 return pi_probe_mode(pi, max, scratch);
380 static void pata_parport_dev_release(struct device *dev)
382 struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
384 ida_free(&pata_parport_bus_dev_ids, dev->id);
388 static void pata_parport_bus_release(struct device *dev)
390 /* nothing to do here but required to avoid warning on device removal */
393 static struct bus_type pata_parport_bus_type = {
397 static struct device pata_parport_bus = {
398 .init_name = DRV_NAME,
399 .release = pata_parport_bus_release,
402 static struct scsi_host_template pata_parport_sht = {
403 PATA_PARPORT_SHT("pata_parport")
406 struct pi_device_match {
407 struct parport *parport;
408 struct pi_protocol *proto;
411 static int pi_find_dev(struct device *dev, void *data)
413 struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
414 struct pi_device_match *match = data;
416 return pi->pardev->port == match->parport && pi->proto == match->proto;
419 static struct pi_adapter *pi_init_one(struct parport *parport,
420 struct pi_protocol *pr, int mode, int unit, int delay)
422 struct pardev_cb par_cb = { };
424 const struct ata_port_info *ppi[] = { &pata_parport_port_info };
425 struct ata_host *host;
426 struct pi_adapter *pi;
427 struct pi_device_match match = { .parport = parport, .proto = pr };
431 * Abort if there's a device already registered on the same parport
432 * using the same protocol.
434 if (bus_for_each_dev(&pata_parport_bus_type, NULL, &match, pi_find_dev))
437 id = ida_alloc(&pata_parport_bus_dev_ids, GFP_KERNEL);
441 pi = kzalloc(sizeof(struct pi_adapter), GFP_KERNEL);
443 ida_free(&pata_parport_bus_dev_ids, id);
447 /* set up pi->dev before pi_probe_unit() so it can use dev_printk() */
448 pi->dev.parent = &pata_parport_bus;
449 pi->dev.bus = &pata_parport_bus_type;
450 pi->dev.driver = &pr->driver;
451 pi->dev.release = pata_parport_dev_release;
453 dev_set_name(&pi->dev, "pata_parport.%u", pi->dev.id);
454 if (device_register(&pi->dev)) {
455 put_device(&pi->dev);
456 /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
462 if (!try_module_get(pi->proto->owner))
464 if (pi->proto->init_proto && pi->proto->init_proto(pi) < 0)
467 pi->delay = (delay == -1) ? pi->proto->default_delay : delay;
469 pi->port = parport->base;
472 pi->pardev = parport_register_dev_model(parport, DRV_NAME, &par_cb, id);
476 if (!pi_probe_unit(pi, unit, scratch)) {
477 dev_info(&pi->dev, "Adapter not found\n");
478 goto out_unreg_parport;
481 pi->proto->log_adapter(pi, scratch, 1);
483 host = ata_host_alloc_pinfo(&pi->pardev->dev, ppi, 1);
485 goto out_unreg_parport;
486 dev_set_drvdata(&pi->dev, host);
487 host->private_data = pi;
489 ata_port_desc(host->ports[0], "port %s", pi->pardev->port->name);
490 ata_port_desc(host->ports[0], "protocol %s", pi->proto->name);
493 if (ata_host_activate(host, 0, NULL, 0, &pata_parport_sht))
501 parport_unregister_device(pi->pardev);
502 if (pi->proto->release_proto)
503 pi->proto->release_proto(pi);
505 module_put(pi->proto->owner);
507 device_unregister(&pi->dev);
508 /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
512 int pata_parport_register_driver(struct pi_protocol *pr)
515 struct parport *parport;
518 pr->driver.bus = &pata_parport_bus_type;
519 pr->driver.name = pr->name;
520 error = driver_register(&pr->driver);
524 mutex_lock(&pi_mutex);
525 error = idr_alloc(&protocols, pr, 0, 0, GFP_KERNEL);
527 driver_unregister(&pr->driver);
528 mutex_unlock(&pi_mutex);
532 pr_info("pata_parport: protocol %s registered\n", pr->name);
535 /* probe all parports using this protocol */
536 idr_for_each_entry(&parport_list, parport, port_num)
537 pi_init_one(parport, pr, -1, 0, -1);
539 mutex_unlock(&pi_mutex);
543 EXPORT_SYMBOL_GPL(pata_parport_register_driver);
545 void pata_parport_unregister_driver(struct pi_protocol *pr)
547 struct pi_protocol *pr_iter;
550 mutex_lock(&pi_mutex);
551 idr_for_each_entry(&protocols, pr_iter, id) {
555 idr_remove(&protocols, id);
556 mutex_unlock(&pi_mutex);
557 driver_unregister(&pr->driver);
559 EXPORT_SYMBOL_GPL(pata_parport_unregister_driver);
561 static ssize_t new_device_store(struct bus_type *bus, const char *buf,
564 char port[12] = "auto";
565 char protocol[8] = "auto";
566 int mode = -1, unit = -1, delay = -1;
567 struct pi_protocol *pr, *pr_wanted;
568 struct device_driver *drv;
569 struct parport *parport;
570 int port_num, port_wanted, pr_num;
573 if (sscanf(buf, "%11s %7s %d %d %d",
574 port, protocol, &mode, &unit, &delay) < 1)
577 if (sscanf(port, "parport%u", &port_wanted) < 1) {
578 if (strcmp(port, "auto")) {
579 pr_err("invalid port name %s\n", port);
585 drv = driver_find(protocol, &pata_parport_bus_type);
587 if (strcmp(protocol, "auto")) {
588 pr_err("protocol %s not found\n", protocol);
593 pr_wanted = container_of(drv, struct pi_protocol, driver);
596 mutex_lock(&pi_mutex);
597 /* walk all parports */
598 idr_for_each_entry(&parport_list, parport, port_num) {
599 if (port_num == port_wanted || port_wanted == -1) {
600 parport = parport_find_number(port_num);
602 pr_err("no such port %s\n", port);
603 mutex_unlock(&pi_mutex);
606 /* walk all protocols */
607 idr_for_each_entry(&protocols, pr, pr_num) {
608 if (pr == pr_wanted || !pr_wanted)
609 if (pi_init_one(parport, pr, mode, unit,
613 parport_put_port(parport);
616 mutex_unlock(&pi_mutex);
622 static BUS_ATTR_WO(new_device);
624 static void pi_remove_one(struct device *dev)
626 struct ata_host *host = dev_get_drvdata(dev);
627 struct pi_adapter *pi = host->private_data;
629 ata_host_detach(host);
632 device_unregister(dev);
633 /* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
636 static ssize_t delete_device_store(struct bus_type *bus, const char *buf,
641 mutex_lock(&pi_mutex);
642 dev = bus_find_device_by_name(bus, NULL, buf);
644 mutex_unlock(&pi_mutex);
650 mutex_unlock(&pi_mutex);
654 static BUS_ATTR_WO(delete_device);
656 static void pata_parport_attach(struct parport *port)
658 struct pi_protocol *pr;
661 mutex_lock(&pi_mutex);
662 id = idr_alloc(&parport_list, port, port->number, port->number,
665 mutex_unlock(&pi_mutex);
670 /* probe this port using all protocols */
671 idr_for_each_entry(&protocols, pr, pr_num)
672 pi_init_one(port, pr, -1, 0, -1);
674 mutex_unlock(&pi_mutex);
677 static int pi_remove_port(struct device *dev, void *p)
679 struct ata_host *host = dev_get_drvdata(dev);
680 struct pi_adapter *pi = host->private_data;
682 if (pi->pardev->port == p)
688 static void pata_parport_detach(struct parport *port)
690 mutex_lock(&pi_mutex);
691 bus_for_each_dev(&pata_parport_bus_type, NULL, port, pi_remove_port);
692 idr_remove(&parport_list, port->number);
693 mutex_unlock(&pi_mutex);
696 static struct parport_driver pata_parport_driver = {
698 .match_port = pata_parport_attach,
699 .detach = pata_parport_detach,
703 static __init int pata_parport_init(void)
707 error = bus_register(&pata_parport_bus_type);
709 pr_err("failed to register pata_parport bus, error: %d\n", error);
713 error = device_register(&pata_parport_bus);
715 pr_err("failed to register pata_parport bus, error: %d\n", error);
716 goto out_unregister_bus;
719 error = bus_create_file(&pata_parport_bus_type, &bus_attr_new_device);
721 pr_err("unable to create sysfs file, error: %d\n", error);
722 goto out_unregister_dev;
725 error = bus_create_file(&pata_parport_bus_type, &bus_attr_delete_device);
727 pr_err("unable to create sysfs file, error: %d\n", error);
731 error = parport_register_driver(&pata_parport_driver);
733 pr_err("unable to register parport driver, error: %d\n", error);
740 bus_remove_file(&pata_parport_bus_type, &bus_attr_delete_device);
742 bus_remove_file(&pata_parport_bus_type, &bus_attr_new_device);
744 device_unregister(&pata_parport_bus);
746 bus_unregister(&pata_parport_bus_type);
750 static __exit void pata_parport_exit(void)
752 parport_unregister_driver(&pata_parport_driver);
753 bus_remove_file(&pata_parport_bus_type, &bus_attr_new_device);
754 bus_remove_file(&pata_parport_bus_type, &bus_attr_delete_device);
755 device_unregister(&pata_parport_bus);
756 bus_unregister(&pata_parport_bus_type);
759 MODULE_AUTHOR("Ondrej Zary");
760 MODULE_DESCRIPTION("driver for parallel port ATA adapters");
761 MODULE_LICENSE("GPL");
762 MODULE_ALIAS("paride");
764 module_init(pata_parport_init);
765 module_exit(pata_parport_exit);