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